OSDN Git Service

2009-09-13 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009 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
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
95
96 static rtx last_var_location_insn;
97 #endif
98
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
101
102 /* Define this macro to be a nonzero value if the directory specifications
103     which are output in the debug info should end with a separator.  */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106    from generating indirect strings in DWARF2 debug information, for instance
107    if your target is stuck with an old version of GDB that is unable to
108    process them properly or uses VMS Debug.  */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
114
115 #ifndef DWARF2_FRAME_INFO
116 # ifdef DWARF2_DEBUGGING_INFO
117 #  define DWARF2_FRAME_INFO \
118   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
119 # else
120 #  define DWARF2_FRAME_INFO 0
121 # endif
122 #endif
123
124 /* Map register numbers held in the call frame info that gcc has
125    collected using DWARF_FRAME_REGNUM to those that should be output in
126    .debug_frame and .eh_frame.  */
127 #ifndef DWARF2_FRAME_REG_OUT
128 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
129 #endif
130
131 /* Save the result of dwarf2out_do_frame across PCH.  */
132 static GTY(()) bool saved_do_cfi_asm = 0;
133
134 /* Decide whether we want to emit frame unwind information for the current
135    translation unit.  */
136
137 int
138 dwarf2out_do_frame (void)
139 {
140   /* We want to emit correct CFA location expressions or lists, so we
141      have to return true if we're going to output debug info, even if
142      we're not going to output frame or unwind info.  */
143   return (write_symbols == DWARF2_DEBUG
144           || write_symbols == VMS_AND_DWARF2_DEBUG
145           || DWARF2_FRAME_INFO || saved_do_cfi_asm
146 #ifdef DWARF2_UNWIND_INFO
147           || (DWARF2_UNWIND_INFO
148               && (flag_unwind_tables
149                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
150 #endif
151           );
152 }
153
154 /* Decide whether to emit frame unwind via assembler directives.  */
155
156 int
157 dwarf2out_do_cfi_asm (void)
158 {
159   int enc;
160
161 #ifdef MIPS_DEBUGGING_INFO
162   return false;
163 #endif
164   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
165     return false;
166   if (saved_do_cfi_asm)
167     return true;
168   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
169     return false;
170
171   /* Make sure the personality encoding is one the assembler can support.
172      In particular, aligned addresses can't be handled.  */
173   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
174   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
175     return false;
176   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
177   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
178     return false;
179
180   saved_do_cfi_asm = true;
181   return true;
182 }
183
184 /* The size of the target's pointer type.  */
185 #ifndef PTR_SIZE
186 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
187 #endif
188
189 /* Array of RTXes referenced by the debugging information, which therefore
190    must be kept around forever.  */
191 static GTY(()) VEC(rtx,gc) *used_rtx_array;
192
193 /* A pointer to the base of a list of incomplete types which might be
194    completed at some later time.  incomplete_types_list needs to be a
195    VEC(tree,gc) because we want to tell the garbage collector about
196    it.  */
197 static GTY(()) VEC(tree,gc) *incomplete_types;
198
199 /* A pointer to the base of a table of references to declaration
200    scopes.  This table is a display which tracks the nesting
201    of declaration scopes at the current scope and containing
202    scopes.  This table is used to find the proper place to
203    define type declaration DIE's.  */
204 static GTY(()) VEC(tree,gc) *decl_scope_table;
205
206 /* Pointers to various DWARF2 sections.  */
207 static GTY(()) section *debug_info_section;
208 static GTY(()) section *debug_abbrev_section;
209 static GTY(()) section *debug_aranges_section;
210 static GTY(()) section *debug_macinfo_section;
211 static GTY(()) section *debug_line_section;
212 static GTY(()) section *debug_loc_section;
213 static GTY(()) section *debug_pubnames_section;
214 static GTY(()) section *debug_pubtypes_section;
215 static GTY(()) section *debug_str_section;
216 static GTY(()) section *debug_ranges_section;
217 static GTY(()) section *debug_frame_section;
218
219 /* Personality decl of current unit.  Used only when assembler does not support
220    personality CFI.  */
221 static GTY(()) rtx current_unit_personality;
222
223 /* How to start an assembler comment.  */
224 #ifndef ASM_COMMENT_START
225 #define ASM_COMMENT_START ";#"
226 #endif
227
228 typedef struct dw_cfi_struct *dw_cfi_ref;
229 typedef struct dw_fde_struct *dw_fde_ref;
230 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
231
232 /* Call frames are described using a sequence of Call Frame
233    Information instructions.  The register number, offset
234    and address fields are provided as possible operands;
235    their use is selected by the opcode field.  */
236
237 enum dw_cfi_oprnd_type {
238   dw_cfi_oprnd_unused,
239   dw_cfi_oprnd_reg_num,
240   dw_cfi_oprnd_offset,
241   dw_cfi_oprnd_addr,
242   dw_cfi_oprnd_loc
243 };
244
245 typedef union GTY(()) dw_cfi_oprnd_struct {
246   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
247   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
248   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
249   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
250 }
251 dw_cfi_oprnd;
252
253 typedef struct GTY(()) dw_cfi_struct {
254   dw_cfi_ref dw_cfi_next;
255   enum dwarf_call_frame_info dw_cfi_opc;
256   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
257     dw_cfi_oprnd1;
258   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
259     dw_cfi_oprnd2;
260 }
261 dw_cfi_node;
262
263 /* This is how we define the location of the CFA. We use to handle it
264    as REG + OFFSET all the time,  but now it can be more complex.
265    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
266    Instead of passing around REG and OFFSET, we pass a copy
267    of this structure.  */
268 typedef struct GTY(()) cfa_loc {
269   HOST_WIDE_INT offset;
270   HOST_WIDE_INT base_offset;
271   unsigned int reg;
272   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
273   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
274 } dw_cfa_location;
275
276 /* All call frame descriptions (FDE's) in the GCC generated DWARF
277    refer to a single Common Information Entry (CIE), defined at
278    the beginning of the .debug_frame section.  This use of a single
279    CIE obviates the need to keep track of multiple CIE's
280    in the DWARF generation routines below.  */
281
282 typedef struct GTY(()) dw_fde_struct {
283   tree decl;
284   const char *dw_fde_begin;
285   const char *dw_fde_current_label;
286   const char *dw_fde_end;
287   const char *dw_fde_hot_section_label;
288   const char *dw_fde_hot_section_end_label;
289   const char *dw_fde_unlikely_section_label;
290   const char *dw_fde_unlikely_section_end_label;
291   dw_cfi_ref dw_fde_cfi;
292   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
293   unsigned funcdef_number;
294   HOST_WIDE_INT stack_realignment;
295   /* Dynamic realign argument pointer register.  */
296   unsigned int drap_reg;
297   /* Virtual dynamic realign argument pointer register.  */
298   unsigned int vdrap_reg;
299   unsigned all_throwers_are_sibcalls : 1;
300   unsigned nothrow : 1;
301   unsigned uses_eh_lsda : 1;
302   /* Whether we did stack realign in this call frame.  */
303   unsigned stack_realign : 1;
304   /* Whether dynamic realign argument pointer register has been saved.  */
305   unsigned drap_reg_saved: 1;
306   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
307   unsigned in_std_section : 1;
308   /* True iff dw_fde_unlikely_section_label is in text_section or
309      cold_text_section.  */
310   unsigned cold_in_std_section : 1;
311   /* True iff switched sections.  */
312   unsigned dw_fde_switched_sections : 1;
313   /* True iff switching from cold to hot section.  */
314   unsigned dw_fde_switched_cold_to_hot : 1;
315 }
316 dw_fde_node;
317
318 /* Maximum size (in bytes) of an artificially generated label.  */
319 #define MAX_ARTIFICIAL_LABEL_BYTES      30
320
321 /* The size of addresses as they appear in the Dwarf 2 data.
322    Some architectures use word addresses to refer to code locations,
323    but Dwarf 2 info always uses byte addresses.  On such machines,
324    Dwarf 2 addresses need to be larger than the architecture's
325    pointers.  */
326 #ifndef DWARF2_ADDR_SIZE
327 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
328 #endif
329
330 /* The size in bytes of a DWARF field indicating an offset or length
331    relative to a debug info section, specified to be 4 bytes in the
332    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
333    as PTR_SIZE.  */
334
335 #ifndef DWARF_OFFSET_SIZE
336 #define DWARF_OFFSET_SIZE 4
337 #endif
338
339 /* According to the (draft) DWARF 3 specification, the initial length
340    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
341    bytes are 0xffffffff, followed by the length stored in the next 8
342    bytes.
343
344    However, the SGI/MIPS ABI uses an initial length which is equal to
345    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
346
347 #ifndef DWARF_INITIAL_LENGTH_SIZE
348 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
349 #endif
350
351 /* Round SIZE up to the nearest BOUNDARY.  */
352 #define DWARF_ROUND(SIZE,BOUNDARY) \
353   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
354
355 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
356 #ifndef DWARF_CIE_DATA_ALIGNMENT
357 #ifdef STACK_GROWS_DOWNWARD
358 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
359 #else
360 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
361 #endif
362 #endif
363
364 /* CIE identifier.  */
365 #if HOST_BITS_PER_WIDE_INT >= 64
366 #define DWARF_CIE_ID \
367   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
368 #else
369 #define DWARF_CIE_ID DW_CIE_ID
370 #endif
371
372 /* A pointer to the base of a table that contains frame description
373    information for each routine.  */
374 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
375
376 /* Number of elements currently allocated for fde_table.  */
377 static GTY(()) unsigned fde_table_allocated;
378
379 /* Number of elements in fde_table currently in use.  */
380 static GTY(()) unsigned fde_table_in_use;
381
382 /* Size (in elements) of increments by which we may expand the
383    fde_table.  */
384 #define FDE_TABLE_INCREMENT 256
385
386 /* Get the current fde_table entry we should use.  */
387
388 static inline dw_fde_ref
389 current_fde (void)
390 {
391   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
392 }
393
394 /* A list of call frame insns for the CIE.  */
395 static GTY(()) dw_cfi_ref cie_cfi_head;
396
397 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
398 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
399    attribute that accelerates the lookup of the FDE associated
400    with the subprogram.  This variable holds the table index of the FDE
401    associated with the current function (body) definition.  */
402 static unsigned current_funcdef_fde;
403 #endif
404
405 struct GTY(()) indirect_string_node {
406   const char *str;
407   unsigned int refcount;
408   enum dwarf_form form;
409   char *label;
410 };
411
412 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
413
414 /* True if the compilation unit has location entries that reference
415    debug strings.  */
416 static GTY(()) bool debug_str_hash_forced = false;
417
418 static GTY(()) int dw2_string_counter;
419 static GTY(()) unsigned long dwarf2out_cfi_label_num;
420
421 /* True if the compilation unit places functions in more than one section.  */
422 static GTY(()) bool have_multiple_function_sections = false;
423
424 /* Whether the default text and cold text sections have been used at all.  */
425
426 static GTY(()) bool text_section_used = false;
427 static GTY(()) bool cold_text_section_used = false;
428
429 /* The default cold text section.  */
430 static GTY(()) section *cold_text_section;
431
432 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
433
434 /* Forward declarations for functions defined in this file.  */
435
436 static char *stripattributes (const char *);
437 static const char *dwarf_cfi_name (unsigned);
438 static dw_cfi_ref new_cfi (void);
439 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
440 static void add_fde_cfi (const char *, dw_cfi_ref);
441 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
442 static void lookup_cfa (dw_cfa_location *);
443 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
444 #ifdef DWARF2_UNWIND_INFO
445 static void initial_return_save (rtx);
446 #endif
447 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
448                                           HOST_WIDE_INT);
449 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
450 static void output_cfi_directive (dw_cfi_ref);
451 static void output_call_frame_info (int);
452 static void dwarf2out_note_section_used (void);
453 static void dwarf2out_stack_adjust (rtx, bool);
454 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
455 static void flush_queued_reg_saves (void);
456 static bool clobbers_queued_reg_save (const_rtx);
457 static void dwarf2out_frame_debug_expr (rtx, const char *);
458
459 /* Support for complex CFA locations.  */
460 static void output_cfa_loc (dw_cfi_ref);
461 static void output_cfa_loc_raw (dw_cfi_ref);
462 static void get_cfa_from_loc_descr (dw_cfa_location *,
463                                     struct dw_loc_descr_struct *);
464 static struct dw_loc_descr_struct *build_cfa_loc
465   (dw_cfa_location *, HOST_WIDE_INT);
466 static struct dw_loc_descr_struct *build_cfa_aligned_loc
467   (HOST_WIDE_INT, HOST_WIDE_INT);
468 static void def_cfa_1 (const char *, dw_cfa_location *);
469
470 /* How to start an assembler comment.  */
471 #ifndef ASM_COMMENT_START
472 #define ASM_COMMENT_START ";#"
473 #endif
474
475 /* Data and reference forms for relocatable data.  */
476 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
477 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
478
479 #ifndef DEBUG_FRAME_SECTION
480 #define DEBUG_FRAME_SECTION     ".debug_frame"
481 #endif
482
483 #ifndef FUNC_BEGIN_LABEL
484 #define FUNC_BEGIN_LABEL        "LFB"
485 #endif
486
487 #ifndef FUNC_END_LABEL
488 #define FUNC_END_LABEL          "LFE"
489 #endif
490
491 #ifndef FRAME_BEGIN_LABEL
492 #define FRAME_BEGIN_LABEL       "Lframe"
493 #endif
494 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
495 #define CIE_END_LABEL           "LECIE"
496 #define FDE_LABEL               "LSFDE"
497 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
498 #define FDE_END_LABEL           "LEFDE"
499 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
500 #define LINE_NUMBER_END_LABEL   "LELT"
501 #define LN_PROLOG_AS_LABEL      "LASLTP"
502 #define LN_PROLOG_END_LABEL     "LELTP"
503 #define DIE_LABEL_PREFIX        "DW"
504
505 /* The DWARF 2 CFA column which tracks the return address.  Normally this
506    is the column for PC, or the first column after all of the hard
507    registers.  */
508 #ifndef DWARF_FRAME_RETURN_COLUMN
509 #ifdef PC_REGNUM
510 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
511 #else
512 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
513 #endif
514 #endif
515
516 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
517    default, we just provide columns for all registers.  */
518 #ifndef DWARF_FRAME_REGNUM
519 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
520 #endif
521 \f
522 /* Hook used by __throw.  */
523
524 rtx
525 expand_builtin_dwarf_sp_column (void)
526 {
527   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
528   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
529 }
530
531 /* Return a pointer to a copy of the section string name S with all
532    attributes stripped off, and an asterisk prepended (for assemble_name).  */
533
534 static inline char *
535 stripattributes (const char *s)
536 {
537   char *stripped = XNEWVEC (char, strlen (s) + 2);
538   char *p = stripped;
539
540   *p++ = '*';
541
542   while (*s && *s != ',')
543     *p++ = *s++;
544
545   *p = '\0';
546   return stripped;
547 }
548
549 /* MEM is a memory reference for the register size table, each element of
550    which has mode MODE.  Initialize column C as a return address column.  */
551
552 static void
553 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
554 {
555   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
556   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
557   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
558 }
559
560 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
561
562 static inline HOST_WIDE_INT
563 div_data_align (HOST_WIDE_INT off)
564 {
565   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
566   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
567   return r;
568 }
569
570 /* Return true if we need a signed version of a given opcode
571    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
572
573 static inline bool
574 need_data_align_sf_opcode (HOST_WIDE_INT off)
575 {
576   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
577 }
578
579 /* Generate code to initialize the register size table.  */
580
581 void
582 expand_builtin_init_dwarf_reg_sizes (tree address)
583 {
584   unsigned int i;
585   enum machine_mode mode = TYPE_MODE (char_type_node);
586   rtx addr = expand_normal (address);
587   rtx mem = gen_rtx_MEM (BLKmode, addr);
588   bool wrote_return_column = false;
589
590   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
591     {
592       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
593
594       if (rnum < DWARF_FRAME_REGISTERS)
595         {
596           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
597           enum machine_mode save_mode = reg_raw_mode[i];
598           HOST_WIDE_INT size;
599
600           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
601             save_mode = choose_hard_reg_mode (i, 1, true);
602           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
603             {
604               if (save_mode == VOIDmode)
605                 continue;
606               wrote_return_column = true;
607             }
608           size = GET_MODE_SIZE (save_mode);
609           if (offset < 0)
610             continue;
611
612           emit_move_insn (adjust_address (mem, mode, offset),
613                           gen_int_mode (size, mode));
614         }
615     }
616
617   if (!wrote_return_column)
618     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
619
620 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
621   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
622 #endif
623
624   targetm.init_dwarf_reg_sizes_extra (address);
625 }
626
627 /* Convert a DWARF call frame info. operation to its string name */
628
629 static const char *
630 dwarf_cfi_name (unsigned int cfi_opc)
631 {
632   switch (cfi_opc)
633     {
634     case DW_CFA_advance_loc:
635       return "DW_CFA_advance_loc";
636     case DW_CFA_offset:
637       return "DW_CFA_offset";
638     case DW_CFA_restore:
639       return "DW_CFA_restore";
640     case DW_CFA_nop:
641       return "DW_CFA_nop";
642     case DW_CFA_set_loc:
643       return "DW_CFA_set_loc";
644     case DW_CFA_advance_loc1:
645       return "DW_CFA_advance_loc1";
646     case DW_CFA_advance_loc2:
647       return "DW_CFA_advance_loc2";
648     case DW_CFA_advance_loc4:
649       return "DW_CFA_advance_loc4";
650     case DW_CFA_offset_extended:
651       return "DW_CFA_offset_extended";
652     case DW_CFA_restore_extended:
653       return "DW_CFA_restore_extended";
654     case DW_CFA_undefined:
655       return "DW_CFA_undefined";
656     case DW_CFA_same_value:
657       return "DW_CFA_same_value";
658     case DW_CFA_register:
659       return "DW_CFA_register";
660     case DW_CFA_remember_state:
661       return "DW_CFA_remember_state";
662     case DW_CFA_restore_state:
663       return "DW_CFA_restore_state";
664     case DW_CFA_def_cfa:
665       return "DW_CFA_def_cfa";
666     case DW_CFA_def_cfa_register:
667       return "DW_CFA_def_cfa_register";
668     case DW_CFA_def_cfa_offset:
669       return "DW_CFA_def_cfa_offset";
670
671     /* DWARF 3 */
672     case DW_CFA_def_cfa_expression:
673       return "DW_CFA_def_cfa_expression";
674     case DW_CFA_expression:
675       return "DW_CFA_expression";
676     case DW_CFA_offset_extended_sf:
677       return "DW_CFA_offset_extended_sf";
678     case DW_CFA_def_cfa_sf:
679       return "DW_CFA_def_cfa_sf";
680     case DW_CFA_def_cfa_offset_sf:
681       return "DW_CFA_def_cfa_offset_sf";
682
683     /* SGI/MIPS specific */
684     case DW_CFA_MIPS_advance_loc8:
685       return "DW_CFA_MIPS_advance_loc8";
686
687     /* GNU extensions */
688     case DW_CFA_GNU_window_save:
689       return "DW_CFA_GNU_window_save";
690     case DW_CFA_GNU_args_size:
691       return "DW_CFA_GNU_args_size";
692     case DW_CFA_GNU_negative_offset_extended:
693       return "DW_CFA_GNU_negative_offset_extended";
694
695     default:
696       return "DW_CFA_<unknown>";
697     }
698 }
699
700 /* Return a pointer to a newly allocated Call Frame Instruction.  */
701
702 static inline dw_cfi_ref
703 new_cfi (void)
704 {
705   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
706
707   cfi->dw_cfi_next = NULL;
708   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
709   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
710
711   return cfi;
712 }
713
714 /* Add a Call Frame Instruction to list of instructions.  */
715
716 static inline void
717 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
718 {
719   dw_cfi_ref *p;
720   dw_fde_ref fde = current_fde ();
721
722   /* When DRAP is used, CFA is defined with an expression.  Redefine
723      CFA may lead to a different CFA value.   */
724   /* ??? Of course, this heuristic fails when we're annotating epilogues,
725      because of course we'll always want to redefine the CFA back to the
726      stack pointer on the way out.  Where should we move this check?  */
727   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
728     switch (cfi->dw_cfi_opc)
729       {
730         case DW_CFA_def_cfa_register:
731         case DW_CFA_def_cfa_offset:
732         case DW_CFA_def_cfa_offset_sf:
733         case DW_CFA_def_cfa:
734         case DW_CFA_def_cfa_sf:
735           gcc_unreachable ();
736
737         default:
738           break;
739       }
740
741   /* Find the end of the chain.  */
742   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
743     ;
744
745   *p = cfi;
746 }
747
748 /* Generate a new label for the CFI info to refer to.  FORCE is true
749    if a label needs to be output even when using .cfi_* directives.  */
750
751 char *
752 dwarf2out_cfi_label (bool force)
753 {
754   static char label[20];
755
756   if (!force && dwarf2out_do_cfi_asm ())
757     {
758       /* In this case, we will be emitting the asm directive instead of
759          the label, so just return a placeholder to keep the rest of the
760          interfaces happy.  */
761       strcpy (label, "<do not output>");
762     }
763   else
764     {
765       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
766       ASM_OUTPUT_LABEL (asm_out_file, label);
767     }
768
769   return label;
770 }
771
772 /* True if remember_state should be emitted before following CFI directive.  */
773 static bool emit_cfa_remember;
774
775 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
776    or to the CIE if LABEL is NULL.  */
777
778 static void
779 add_fde_cfi (const char *label, dw_cfi_ref cfi)
780 {
781   dw_cfi_ref *list_head;
782
783   if (emit_cfa_remember)
784     {
785       dw_cfi_ref cfi_remember;
786
787       /* Emit the state save.  */
788       emit_cfa_remember = false;
789       cfi_remember = new_cfi (); 
790       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
791       add_fde_cfi (label, cfi_remember);
792     }
793
794   list_head = &cie_cfi_head;
795
796   if (dwarf2out_do_cfi_asm ())
797     {
798       if (label)
799         {
800           dw_fde_ref fde = current_fde ();
801
802           gcc_assert (fde != NULL);
803
804           /* We still have to add the cfi to the list so that lookup_cfa
805              works later on.  When -g2 and above we even need to force
806              emitting of CFI labels and add to list a DW_CFA_set_loc for
807              convert_cfa_to_fb_loc_list purposes.  If we're generating
808              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
809              convert_cfa_to_fb_loc_list.  */
810           if (dwarf_version == 2
811               && debug_info_level > DINFO_LEVEL_TERSE
812               && (write_symbols == DWARF2_DEBUG
813                   || write_symbols == VMS_AND_DWARF2_DEBUG))
814             {
815               switch (cfi->dw_cfi_opc)
816                 {
817                 case DW_CFA_def_cfa_offset:
818                 case DW_CFA_def_cfa_offset_sf:
819                 case DW_CFA_def_cfa_register:
820                 case DW_CFA_def_cfa:
821                 case DW_CFA_def_cfa_sf:
822                 case DW_CFA_def_cfa_expression:
823                 case DW_CFA_restore_state:
824                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
825                     label = dwarf2out_cfi_label (true);
826
827                   if (fde->dw_fde_current_label == NULL
828                       || strcmp (label, fde->dw_fde_current_label) != 0)
829                     {
830                       dw_cfi_ref xcfi;
831
832                       label = xstrdup (label);
833
834                       /* Set the location counter to the new label.  */
835                       xcfi = new_cfi ();
836                       /* It doesn't metter whether DW_CFA_set_loc
837                          or DW_CFA_advance_loc4 is added here, those aren't
838                          emitted into assembly, only looked up by
839                          convert_cfa_to_fb_loc_list.  */
840                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
841                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
842                       add_cfi (&fde->dw_fde_cfi, xcfi);
843                       fde->dw_fde_current_label = label;
844                     }
845                   break;
846                 default:
847                   break;
848                 }
849             }
850
851           output_cfi_directive (cfi);
852
853           list_head = &fde->dw_fde_cfi;
854         }
855       /* ??? If this is a CFI for the CIE, we don't emit.  This
856          assumes that the standard CIE contents that the assembler
857          uses matches the standard CIE contents that the compiler
858          uses.  This is probably a bad assumption.  I'm not quite
859          sure how to address this for now.  */
860     }
861   else if (label)
862     {
863       dw_fde_ref fde = current_fde ();
864
865       gcc_assert (fde != NULL);
866
867       if (*label == 0)
868         label = dwarf2out_cfi_label (false);
869
870       if (fde->dw_fde_current_label == NULL
871           || strcmp (label, fde->dw_fde_current_label) != 0)
872         {
873           dw_cfi_ref xcfi;
874
875           label = xstrdup (label);
876
877           /* Set the location counter to the new label.  */
878           xcfi = new_cfi ();
879           /* If we have a current label, advance from there, otherwise
880              set the location directly using set_loc.  */
881           xcfi->dw_cfi_opc = fde->dw_fde_current_label
882                              ? DW_CFA_advance_loc4
883                              : DW_CFA_set_loc;
884           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
885           add_cfi (&fde->dw_fde_cfi, xcfi);
886
887           fde->dw_fde_current_label = label;
888         }
889
890       list_head = &fde->dw_fde_cfi;
891     }
892
893   add_cfi (list_head, cfi);
894 }
895
896 /* Subroutine of lookup_cfa.  */
897
898 static void
899 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
900 {
901   switch (cfi->dw_cfi_opc)
902     {
903     case DW_CFA_def_cfa_offset:
904     case DW_CFA_def_cfa_offset_sf:
905       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
906       break;
907     case DW_CFA_def_cfa_register:
908       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
909       break;
910     case DW_CFA_def_cfa:
911     case DW_CFA_def_cfa_sf:
912       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
913       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
914       break;
915     case DW_CFA_def_cfa_expression:
916       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
917       break;
918
919     case DW_CFA_remember_state:
920       gcc_assert (!remember->in_use);
921       *remember = *loc;
922       remember->in_use = 1;
923       break;
924     case DW_CFA_restore_state:
925       gcc_assert (remember->in_use);
926       *loc = *remember;
927       remember->in_use = 0;
928       break;
929
930     default:
931       break;
932     }
933 }
934
935 /* Find the previous value for the CFA.  */
936
937 static void
938 lookup_cfa (dw_cfa_location *loc)
939 {
940   dw_cfi_ref cfi;
941   dw_fde_ref fde;
942   dw_cfa_location remember;
943
944   memset (loc, 0, sizeof (*loc));
945   loc->reg = INVALID_REGNUM;
946   remember = *loc;
947
948   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
949     lookup_cfa_1 (cfi, loc, &remember);
950
951   fde = current_fde ();
952   if (fde)
953     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
954       lookup_cfa_1 (cfi, loc, &remember);
955 }
956
957 /* The current rule for calculating the DWARF2 canonical frame address.  */
958 static dw_cfa_location cfa;
959
960 /* The register used for saving registers to the stack, and its offset
961    from the CFA.  */
962 static dw_cfa_location cfa_store;
963
964 /* The current save location around an epilogue.  */
965 static dw_cfa_location cfa_remember;
966
967 /* The running total of the size of arguments pushed onto the stack.  */
968 static HOST_WIDE_INT args_size;
969
970 /* The last args_size we actually output.  */
971 static HOST_WIDE_INT old_args_size;
972
973 /* Entry point to update the canonical frame address (CFA).
974    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
975    calculated from REG+OFFSET.  */
976
977 void
978 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
979 {
980   dw_cfa_location loc;
981   loc.indirect = 0;
982   loc.base_offset = 0;
983   loc.reg = reg;
984   loc.offset = offset;
985   def_cfa_1 (label, &loc);
986 }
987
988 /* Determine if two dw_cfa_location structures define the same data.  */
989
990 static bool
991 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
992 {
993   return (loc1->reg == loc2->reg
994           && loc1->offset == loc2->offset
995           && loc1->indirect == loc2->indirect
996           && (loc1->indirect == 0
997               || loc1->base_offset == loc2->base_offset));
998 }
999
1000 /* This routine does the actual work.  The CFA is now calculated from
1001    the dw_cfa_location structure.  */
1002
1003 static void
1004 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1005 {
1006   dw_cfi_ref cfi;
1007   dw_cfa_location old_cfa, loc;
1008
1009   cfa = *loc_p;
1010   loc = *loc_p;
1011
1012   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1013     cfa_store.offset = loc.offset;
1014
1015   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1016   lookup_cfa (&old_cfa);
1017
1018   /* If nothing changed, no need to issue any call frame instructions.  */
1019   if (cfa_equal_p (&loc, &old_cfa))
1020     return;
1021
1022   cfi = new_cfi ();
1023
1024   if (loc.reg == old_cfa.reg && !loc.indirect)
1025     {
1026       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1027          the CFA register did not change but the offset did.  The data 
1028          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1029          in the assembler via the .cfi_def_cfa_offset directive.  */
1030       if (loc.offset < 0)
1031         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1032       else
1033         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1034       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1035     }
1036
1037 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1038   else if (loc.offset == old_cfa.offset
1039            && old_cfa.reg != INVALID_REGNUM
1040            && !loc.indirect)
1041     {
1042       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1043          indicating the CFA register has changed to <register> but the
1044          offset has not changed.  */
1045       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1046       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1047     }
1048 #endif
1049
1050   else if (loc.indirect == 0)
1051     {
1052       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1053          indicating the CFA register has changed to <register> with
1054          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1055          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1056          directive.  */
1057       if (loc.offset < 0)
1058         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1059       else
1060         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1061       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1062       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1063     }
1064   else
1065     {
1066       /* Construct a DW_CFA_def_cfa_expression instruction to
1067          calculate the CFA using a full location expression since no
1068          register-offset pair is available.  */
1069       struct dw_loc_descr_struct *loc_list;
1070
1071       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1072       loc_list = build_cfa_loc (&loc, 0);
1073       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1074     }
1075
1076   add_fde_cfi (label, cfi);
1077 }
1078
1079 /* Add the CFI for saving a register.  REG is the CFA column number.
1080    LABEL is passed to add_fde_cfi.
1081    If SREG is -1, the register is saved at OFFSET from the CFA;
1082    otherwise it is saved in SREG.  */
1083
1084 static void
1085 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1086 {
1087   dw_cfi_ref cfi = new_cfi ();
1088   dw_fde_ref fde = current_fde ();
1089
1090   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1091
1092   /* When stack is aligned, store REG using DW_CFA_expression with
1093      FP.  */
1094   if (fde
1095       && fde->stack_realign
1096       && sreg == INVALID_REGNUM)
1097     {
1098       cfi->dw_cfi_opc = DW_CFA_expression;
1099       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1100       cfi->dw_cfi_oprnd1.dw_cfi_loc
1101         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1102     }
1103   else if (sreg == INVALID_REGNUM)
1104     {
1105       if (need_data_align_sf_opcode (offset))
1106         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1107       else if (reg & ~0x3f)
1108         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1109       else
1110         cfi->dw_cfi_opc = DW_CFA_offset;
1111       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1112     }
1113   else if (sreg == reg)
1114     cfi->dw_cfi_opc = DW_CFA_same_value;
1115   else
1116     {
1117       cfi->dw_cfi_opc = DW_CFA_register;
1118       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1119     }
1120
1121   add_fde_cfi (label, cfi);
1122 }
1123
1124 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1125    This CFI tells the unwinder that it needs to restore the window registers
1126    from the previous frame's window save area.
1127
1128    ??? Perhaps we should note in the CIE where windows are saved (instead of
1129    assuming 0(cfa)) and what registers are in the window.  */
1130
1131 void
1132 dwarf2out_window_save (const char *label)
1133 {
1134   dw_cfi_ref cfi = new_cfi ();
1135
1136   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1137   add_fde_cfi (label, cfi);
1138 }
1139
1140 /* Add a CFI to update the running total of the size of arguments
1141    pushed onto the stack.  */
1142
1143 void
1144 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1145 {
1146   dw_cfi_ref cfi;
1147
1148   if (size == old_args_size)
1149     return;
1150
1151   old_args_size = size;
1152
1153   cfi = new_cfi ();
1154   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1155   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1156   add_fde_cfi (label, cfi);
1157 }
1158
1159 /* Entry point for saving a register to the stack.  REG is the GCC register
1160    number.  LABEL and OFFSET are passed to reg_save.  */
1161
1162 void
1163 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1164 {
1165   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1166 }
1167
1168 /* Entry point for saving the return address in the stack.
1169    LABEL and OFFSET are passed to reg_save.  */
1170
1171 void
1172 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1173 {
1174   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1175 }
1176
1177 /* Entry point for saving the return address in a register.
1178    LABEL and SREG are passed to reg_save.  */
1179
1180 void
1181 dwarf2out_return_reg (const char *label, unsigned int sreg)
1182 {
1183   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1184 }
1185
1186 #ifdef DWARF2_UNWIND_INFO
1187 /* Record the initial position of the return address.  RTL is
1188    INCOMING_RETURN_ADDR_RTX.  */
1189
1190 static void
1191 initial_return_save (rtx rtl)
1192 {
1193   unsigned int reg = INVALID_REGNUM;
1194   HOST_WIDE_INT offset = 0;
1195
1196   switch (GET_CODE (rtl))
1197     {
1198     case REG:
1199       /* RA is in a register.  */
1200       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1201       break;
1202
1203     case MEM:
1204       /* RA is on the stack.  */
1205       rtl = XEXP (rtl, 0);
1206       switch (GET_CODE (rtl))
1207         {
1208         case REG:
1209           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1210           offset = 0;
1211           break;
1212
1213         case PLUS:
1214           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1215           offset = INTVAL (XEXP (rtl, 1));
1216           break;
1217
1218         case MINUS:
1219           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1220           offset = -INTVAL (XEXP (rtl, 1));
1221           break;
1222
1223         default:
1224           gcc_unreachable ();
1225         }
1226
1227       break;
1228
1229     case PLUS:
1230       /* The return address is at some offset from any value we can
1231          actually load.  For instance, on the SPARC it is in %i7+8. Just
1232          ignore the offset for now; it doesn't matter for unwinding frames.  */
1233       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1234       initial_return_save (XEXP (rtl, 0));
1235       return;
1236
1237     default:
1238       gcc_unreachable ();
1239     }
1240
1241   if (reg != DWARF_FRAME_RETURN_COLUMN)
1242     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1243 }
1244 #endif
1245
1246 /* Given a SET, calculate the amount of stack adjustment it
1247    contains.  */
1248
1249 static HOST_WIDE_INT
1250 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1251                      HOST_WIDE_INT cur_offset)
1252 {
1253   const_rtx src = SET_SRC (pattern);
1254   const_rtx dest = SET_DEST (pattern);
1255   HOST_WIDE_INT offset = 0;
1256   enum rtx_code code;
1257
1258   if (dest == stack_pointer_rtx)
1259     {
1260       code = GET_CODE (src);
1261
1262       /* Assume (set (reg sp) (reg whatever)) sets args_size
1263          level to 0.  */
1264       if (code == REG && src != stack_pointer_rtx)
1265         {
1266           offset = -cur_args_size;
1267 #ifndef STACK_GROWS_DOWNWARD
1268           offset = -offset;
1269 #endif
1270           return offset - cur_offset;
1271         }
1272
1273       if (! (code == PLUS || code == MINUS)
1274           || XEXP (src, 0) != stack_pointer_rtx
1275           || !CONST_INT_P (XEXP (src, 1)))
1276         return 0;
1277
1278       /* (set (reg sp) (plus (reg sp) (const_int))) */
1279       offset = INTVAL (XEXP (src, 1));
1280       if (code == PLUS)
1281         offset = -offset;
1282       return offset;
1283     }
1284
1285   if (MEM_P (src) && !MEM_P (dest))
1286     dest = src;
1287   if (MEM_P (dest))
1288     {
1289       /* (set (mem (pre_dec (reg sp))) (foo)) */
1290       src = XEXP (dest, 0);
1291       code = GET_CODE (src);
1292
1293       switch (code)
1294         {
1295         case PRE_MODIFY:
1296         case POST_MODIFY:
1297           if (XEXP (src, 0) == stack_pointer_rtx)
1298             {
1299               rtx val = XEXP (XEXP (src, 1), 1);
1300               /* We handle only adjustments by constant amount.  */
1301               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1302                           && CONST_INT_P (val));
1303               offset = -INTVAL (val);
1304               break;
1305             }
1306           return 0;
1307
1308         case PRE_DEC:
1309         case POST_DEC:
1310           if (XEXP (src, 0) == stack_pointer_rtx)
1311             {
1312               offset = GET_MODE_SIZE (GET_MODE (dest));
1313               break;
1314             }
1315           return 0;
1316
1317         case PRE_INC:
1318         case POST_INC:
1319           if (XEXP (src, 0) == stack_pointer_rtx)
1320             {
1321               offset = -GET_MODE_SIZE (GET_MODE (dest));
1322               break;
1323             }
1324           return 0;
1325
1326         default:
1327           return 0;
1328         }
1329     }
1330   else
1331     return 0;
1332
1333   return offset;
1334 }
1335
1336 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1337    indexed by INSN_UID.  */
1338
1339 static HOST_WIDE_INT *barrier_args_size;
1340
1341 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1342
1343 static HOST_WIDE_INT
1344 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1345                              VEC (rtx, heap) **next)
1346 {
1347   HOST_WIDE_INT offset = 0;
1348   int i;
1349
1350   if (! RTX_FRAME_RELATED_P (insn))
1351     {
1352       if (prologue_epilogue_contains (insn))
1353         /* Nothing */;
1354       else if (GET_CODE (PATTERN (insn)) == SET)
1355         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1356       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1357                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1358         {
1359           /* There may be stack adjustments inside compound insns.  Search
1360              for them.  */
1361           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1362             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1363               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1364                                              cur_args_size, offset);
1365         }
1366     }
1367   else
1368     {
1369       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1370
1371       if (expr)
1372         {
1373           expr = XEXP (expr, 0);
1374           if (GET_CODE (expr) == PARALLEL
1375               || GET_CODE (expr) == SEQUENCE)
1376             for (i = 1; i < XVECLEN (expr, 0); i++)
1377               {
1378                 rtx elem = XVECEXP (expr, 0, i);
1379
1380                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1381                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1382               }
1383         }
1384     }
1385
1386 #ifndef STACK_GROWS_DOWNWARD
1387   offset = -offset;
1388 #endif
1389
1390   cur_args_size += offset;
1391   if (cur_args_size < 0)
1392     cur_args_size = 0;
1393
1394   if (JUMP_P (insn))
1395     {
1396       rtx dest = JUMP_LABEL (insn);
1397
1398       if (dest)
1399         {
1400           if (barrier_args_size [INSN_UID (dest)] < 0)
1401             {
1402               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1403               VEC_safe_push (rtx, heap, *next, dest);
1404             }
1405         }
1406     }
1407
1408   return cur_args_size;
1409 }
1410
1411 /* Walk the whole function and compute args_size on BARRIERs.  */
1412
1413 static void
1414 compute_barrier_args_size (void)
1415 {
1416   int max_uid = get_max_uid (), i;
1417   rtx insn;
1418   VEC (rtx, heap) *worklist, *next, *tmp;
1419
1420   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1421   for (i = 0; i < max_uid; i++)
1422     barrier_args_size[i] = -1;
1423
1424   worklist = VEC_alloc (rtx, heap, 20);
1425   next = VEC_alloc (rtx, heap, 20);
1426   insn = get_insns ();
1427   barrier_args_size[INSN_UID (insn)] = 0;
1428   VEC_quick_push (rtx, worklist, insn);
1429   for (;;)
1430     {
1431       while (!VEC_empty (rtx, worklist))
1432         {
1433           rtx prev, body, first_insn;
1434           HOST_WIDE_INT cur_args_size;
1435
1436           first_insn = insn = VEC_pop (rtx, worklist);
1437           cur_args_size = barrier_args_size[INSN_UID (insn)];
1438           prev = prev_nonnote_insn (insn);
1439           if (prev && BARRIER_P (prev))
1440             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1441
1442           for (; insn; insn = NEXT_INSN (insn))
1443             {
1444               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1445                 continue;
1446               if (BARRIER_P (insn))
1447                 break;
1448
1449               if (LABEL_P (insn))
1450                 {
1451                   if (insn == first_insn)
1452                     continue;
1453                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1454                     {
1455                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1456                       continue;
1457                     }
1458                   else
1459                     {
1460                       /* The insns starting with this label have been
1461                          already scanned or are in the worklist.  */
1462                       break;
1463                     }
1464                 }
1465
1466               body = PATTERN (insn);
1467               if (GET_CODE (body) == SEQUENCE)
1468                 {
1469                   HOST_WIDE_INT dest_args_size = cur_args_size;
1470                   for (i = 1; i < XVECLEN (body, 0); i++)
1471                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1472                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1473                       dest_args_size
1474                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1475                                                        dest_args_size, &next);
1476                     else
1477                       cur_args_size
1478                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1479                                                        cur_args_size, &next);
1480
1481                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1482                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1483                                                  dest_args_size, &next);
1484                   else
1485                     cur_args_size
1486                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1487                                                      cur_args_size, &next);
1488                 }
1489               else
1490                 cur_args_size
1491                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1492             }
1493         }
1494
1495       if (VEC_empty (rtx, next))
1496         break;
1497
1498       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1499       tmp = next;
1500       next = worklist;
1501       worklist = tmp;
1502       VEC_truncate (rtx, next, 0);
1503     }
1504
1505   VEC_free (rtx, heap, worklist);
1506   VEC_free (rtx, heap, next);
1507 }
1508
1509
1510 /* Check INSN to see if it looks like a push or a stack adjustment, and
1511    make a note of it if it does.  EH uses this information to find out how
1512    much extra space it needs to pop off the stack.  */
1513
1514 static void
1515 dwarf2out_stack_adjust (rtx insn, bool after_p)
1516 {
1517   HOST_WIDE_INT offset;
1518   const char *label;
1519   int i;
1520
1521   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1522      with this function.  Proper support would require all frame-related
1523      insns to be marked, and to be able to handle saving state around
1524      epilogues textually in the middle of the function.  */
1525   if (prologue_epilogue_contains (insn))
1526     return;
1527
1528   /* If INSN is an instruction from target of an annulled branch, the
1529      effects are for the target only and so current argument size
1530      shouldn't change at all.  */
1531   if (final_sequence
1532       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1533       && INSN_FROM_TARGET_P (insn))
1534     return;
1535
1536   /* If only calls can throw, and we have a frame pointer,
1537      save up adjustments until we see the CALL_INSN.  */
1538   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1539     {
1540       if (CALL_P (insn) && !after_p)
1541         {
1542           /* Extract the size of the args from the CALL rtx itself.  */
1543           insn = PATTERN (insn);
1544           if (GET_CODE (insn) == PARALLEL)
1545             insn = XVECEXP (insn, 0, 0);
1546           if (GET_CODE (insn) == SET)
1547             insn = SET_SRC (insn);
1548           gcc_assert (GET_CODE (insn) == CALL);
1549           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1550         }
1551       return;
1552     }
1553
1554   if (CALL_P (insn) && !after_p)
1555     {
1556       if (!flag_asynchronous_unwind_tables)
1557         dwarf2out_args_size ("", args_size);
1558       return;
1559     }
1560   else if (BARRIER_P (insn))
1561     {
1562       /* Don't call compute_barrier_args_size () if the only
1563          BARRIER is at the end of function.  */
1564       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1565         compute_barrier_args_size ();
1566       if (barrier_args_size == NULL)
1567         offset = 0;
1568       else
1569         {
1570           offset = barrier_args_size[INSN_UID (insn)];
1571           if (offset < 0)
1572             offset = 0;
1573         }
1574
1575       offset -= args_size;
1576 #ifndef STACK_GROWS_DOWNWARD
1577       offset = -offset;
1578 #endif
1579     }
1580   else if (GET_CODE (PATTERN (insn)) == SET)
1581     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1582   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1583            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1584     {
1585       /* There may be stack adjustments inside compound insns.  Search
1586          for them.  */
1587       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1588         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1589           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1590                                          args_size, offset);
1591     }
1592   else
1593     return;
1594
1595   if (offset == 0)
1596     return;
1597
1598   label = dwarf2out_cfi_label (false);
1599   dwarf2out_args_size_adjust (offset, label);
1600 }
1601
1602 /* Adjust args_size based on stack adjustment OFFSET.  */
1603
1604 static void
1605 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1606 {
1607   if (cfa.reg == STACK_POINTER_REGNUM)
1608     cfa.offset += offset;
1609
1610   if (cfa_store.reg == STACK_POINTER_REGNUM)
1611     cfa_store.offset += offset;
1612
1613 #ifndef STACK_GROWS_DOWNWARD
1614   offset = -offset;
1615 #endif
1616
1617   args_size += offset;
1618   if (args_size < 0)
1619     args_size = 0;
1620
1621   def_cfa_1 (label, &cfa);
1622   if (flag_asynchronous_unwind_tables)
1623     dwarf2out_args_size (label, args_size);
1624 }
1625
1626 #endif
1627
1628 /* We delay emitting a register save until either (a) we reach the end
1629    of the prologue or (b) the register is clobbered.  This clusters
1630    register saves so that there are fewer pc advances.  */
1631
1632 struct GTY(()) queued_reg_save {
1633   struct queued_reg_save *next;
1634   rtx reg;
1635   HOST_WIDE_INT cfa_offset;
1636   rtx saved_reg;
1637 };
1638
1639 static GTY(()) struct queued_reg_save *queued_reg_saves;
1640
1641 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1642 struct GTY(()) reg_saved_in_data {
1643   rtx orig_reg;
1644   rtx saved_in_reg;
1645 };
1646
1647 /* A list of registers saved in other registers.
1648    The list intentionally has a small maximum capacity of 4; if your
1649    port needs more than that, you might consider implementing a
1650    more efficient data structure.  */
1651 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1652 static GTY(()) size_t num_regs_saved_in_regs;
1653
1654 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1655 static const char *last_reg_save_label;
1656
1657 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1658    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1659
1660 static void
1661 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1662 {
1663   struct queued_reg_save *q;
1664
1665   /* Duplicates waste space, but it's also necessary to remove them
1666      for correctness, since the queue gets output in reverse
1667      order.  */
1668   for (q = queued_reg_saves; q != NULL; q = q->next)
1669     if (REGNO (q->reg) == REGNO (reg))
1670       break;
1671
1672   if (q == NULL)
1673     {
1674       q = GGC_NEW (struct queued_reg_save);
1675       q->next = queued_reg_saves;
1676       queued_reg_saves = q;
1677     }
1678
1679   q->reg = reg;
1680   q->cfa_offset = offset;
1681   q->saved_reg = sreg;
1682
1683   last_reg_save_label = label;
1684 }
1685
1686 /* Output all the entries in QUEUED_REG_SAVES.  */
1687
1688 static void
1689 flush_queued_reg_saves (void)
1690 {
1691   struct queued_reg_save *q;
1692
1693   for (q = queued_reg_saves; q; q = q->next)
1694     {
1695       size_t i;
1696       unsigned int reg, sreg;
1697
1698       for (i = 0; i < num_regs_saved_in_regs; i++)
1699         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1700           break;
1701       if (q->saved_reg && i == num_regs_saved_in_regs)
1702         {
1703           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1704           num_regs_saved_in_regs++;
1705         }
1706       if (i != num_regs_saved_in_regs)
1707         {
1708           regs_saved_in_regs[i].orig_reg = q->reg;
1709           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1710         }
1711
1712       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1713       if (q->saved_reg)
1714         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1715       else
1716         sreg = INVALID_REGNUM;
1717       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1718     }
1719
1720   queued_reg_saves = NULL;
1721   last_reg_save_label = NULL;
1722 }
1723
1724 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1725    location for?  Or, does it clobber a register which we've previously
1726    said that some other register is saved in, and for which we now
1727    have a new location for?  */
1728
1729 static bool
1730 clobbers_queued_reg_save (const_rtx insn)
1731 {
1732   struct queued_reg_save *q;
1733
1734   for (q = queued_reg_saves; q; q = q->next)
1735     {
1736       size_t i;
1737       if (modified_in_p (q->reg, insn))
1738         return true;
1739       for (i = 0; i < num_regs_saved_in_regs; i++)
1740         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1741             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1742           return true;
1743     }
1744
1745   return false;
1746 }
1747
1748 /* Entry point for saving the first register into the second.  */
1749
1750 void
1751 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1752 {
1753   size_t i;
1754   unsigned int regno, sregno;
1755
1756   for (i = 0; i < num_regs_saved_in_regs; i++)
1757     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1758       break;
1759   if (i == num_regs_saved_in_regs)
1760     {
1761       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1762       num_regs_saved_in_regs++;
1763     }
1764   regs_saved_in_regs[i].orig_reg = reg;
1765   regs_saved_in_regs[i].saved_in_reg = sreg;
1766
1767   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1768   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1769   reg_save (label, regno, sregno, 0);
1770 }
1771
1772 /* What register, if any, is currently saved in REG?  */
1773
1774 static rtx
1775 reg_saved_in (rtx reg)
1776 {
1777   unsigned int regn = REGNO (reg);
1778   size_t i;
1779   struct queued_reg_save *q;
1780
1781   for (q = queued_reg_saves; q; q = q->next)
1782     if (q->saved_reg && regn == REGNO (q->saved_reg))
1783       return q->reg;
1784
1785   for (i = 0; i < num_regs_saved_in_regs; i++)
1786     if (regs_saved_in_regs[i].saved_in_reg
1787         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1788       return regs_saved_in_regs[i].orig_reg;
1789
1790   return NULL_RTX;
1791 }
1792
1793
1794 /* A temporary register holding an integral value used in adjusting SP
1795    or setting up the store_reg.  The "offset" field holds the integer
1796    value, not an offset.  */
1797 static dw_cfa_location cfa_temp;
1798
1799 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1800
1801 static void
1802 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1803 {
1804   memset (&cfa, 0, sizeof (cfa));
1805
1806   switch (GET_CODE (pat))
1807     {
1808     case PLUS:
1809       cfa.reg = REGNO (XEXP (pat, 0));
1810       cfa.offset = INTVAL (XEXP (pat, 1));
1811       break;
1812
1813     case REG:
1814       cfa.reg = REGNO (pat);
1815       break;
1816
1817     default:
1818       /* Recurse and define an expression.  */
1819       gcc_unreachable ();
1820     }
1821
1822   def_cfa_1 (label, &cfa);
1823 }
1824
1825 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1826
1827 static void
1828 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1829 {
1830   rtx src, dest;
1831
1832   gcc_assert (GET_CODE (pat) == SET);
1833   dest = XEXP (pat, 0);
1834   src = XEXP (pat, 1);
1835
1836   switch (GET_CODE (src))
1837     {
1838     case PLUS:
1839       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1840       cfa.offset -= INTVAL (XEXP (src, 1));
1841       break;
1842
1843     case REG:
1844         break;
1845
1846     default:
1847         gcc_unreachable ();
1848     }
1849
1850   cfa.reg = REGNO (dest);
1851   gcc_assert (cfa.indirect == 0);
1852
1853   def_cfa_1 (label, &cfa);
1854 }
1855
1856 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1857
1858 static void
1859 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1860 {
1861   HOST_WIDE_INT offset;
1862   rtx src, addr, span;
1863
1864   src = XEXP (set, 1);
1865   addr = XEXP (set, 0);
1866   gcc_assert (MEM_P (addr));
1867   addr = XEXP (addr, 0);
1868   
1869   /* As documented, only consider extremely simple addresses.  */
1870   switch (GET_CODE (addr))
1871     {
1872     case REG:
1873       gcc_assert (REGNO (addr) == cfa.reg);
1874       offset = -cfa.offset;
1875       break;
1876     case PLUS:
1877       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1878       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1879       break;
1880     default:
1881       gcc_unreachable ();
1882     }
1883
1884   span = targetm.dwarf_register_span (src);
1885
1886   /* ??? We'd like to use queue_reg_save, but we need to come up with
1887      a different flushing heuristic for epilogues.  */
1888   if (!span)
1889     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1890   else
1891     {
1892       /* We have a PARALLEL describing where the contents of SRC live.
1893          Queue register saves for each piece of the PARALLEL.  */
1894       int par_index;
1895       int limit;
1896       HOST_WIDE_INT span_offset = offset;
1897
1898       gcc_assert (GET_CODE (span) == PARALLEL);
1899
1900       limit = XVECLEN (span, 0);
1901       for (par_index = 0; par_index < limit; par_index++)
1902         {
1903           rtx elem = XVECEXP (span, 0, par_index);
1904
1905           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1906                     INVALID_REGNUM, span_offset);
1907           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1908         }
1909     }
1910 }
1911
1912 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1913
1914 static void
1915 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1916 {
1917   rtx src, dest;
1918   unsigned sregno, dregno;
1919
1920   src = XEXP (set, 1);
1921   dest = XEXP (set, 0);
1922
1923   if (src == pc_rtx)
1924     sregno = DWARF_FRAME_RETURN_COLUMN;
1925   else
1926     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1927
1928   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1929
1930   /* ??? We'd like to use queue_reg_save, but we need to come up with
1931      a different flushing heuristic for epilogues.  */
1932   reg_save (label, sregno, dregno, 0);
1933 }
1934
1935 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1936
1937 static void
1938 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1939 {
1940   dw_cfi_ref cfi = new_cfi ();
1941   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1942
1943   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1944   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1945
1946   add_fde_cfi (label, cfi);
1947 }
1948
1949 /* Record call frame debugging information for an expression EXPR,
1950    which either sets SP or FP (adjusting how we calculate the frame
1951    address) or saves a register to the stack or another register.
1952    LABEL indicates the address of EXPR.
1953
1954    This function encodes a state machine mapping rtxes to actions on
1955    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1956    users need not read the source code.
1957
1958   The High-Level Picture
1959
1960   Changes in the register we use to calculate the CFA: Currently we
1961   assume that if you copy the CFA register into another register, we
1962   should take the other one as the new CFA register; this seems to
1963   work pretty well.  If it's wrong for some target, it's simple
1964   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1965
1966   Changes in the register we use for saving registers to the stack:
1967   This is usually SP, but not always.  Again, we deduce that if you
1968   copy SP into another register (and SP is not the CFA register),
1969   then the new register is the one we will be using for register
1970   saves.  This also seems to work.
1971
1972   Register saves: There's not much guesswork about this one; if
1973   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1974   register save, and the register used to calculate the destination
1975   had better be the one we think we're using for this purpose.
1976   It's also assumed that a copy from a call-saved register to another
1977   register is saving that register if RTX_FRAME_RELATED_P is set on
1978   that instruction.  If the copy is from a call-saved register to
1979   the *same* register, that means that the register is now the same
1980   value as in the caller.
1981
1982   Except: If the register being saved is the CFA register, and the
1983   offset is nonzero, we are saving the CFA, so we assume we have to
1984   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1985   the intent is to save the value of SP from the previous frame.
1986
1987   In addition, if a register has previously been saved to a different
1988   register,
1989
1990   Invariants / Summaries of Rules
1991
1992   cfa          current rule for calculating the CFA.  It usually
1993                consists of a register and an offset.
1994   cfa_store    register used by prologue code to save things to the stack
1995                cfa_store.offset is the offset from the value of
1996                cfa_store.reg to the actual CFA
1997   cfa_temp     register holding an integral value.  cfa_temp.offset
1998                stores the value, which will be used to adjust the
1999                stack pointer.  cfa_temp is also used like cfa_store,
2000                to track stores to the stack via fp or a temp reg.
2001
2002   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2003                with cfa.reg as the first operand changes the cfa.reg and its
2004                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2005                cfa_temp.offset.
2006
2007   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2008                expression yielding a constant.  This sets cfa_temp.reg
2009                and cfa_temp.offset.
2010
2011   Rule 5:      Create a new register cfa_store used to save items to the
2012                stack.
2013
2014   Rules 10-14: Save a register to the stack.  Define offset as the
2015                difference of the original location and cfa_store's
2016                location (or cfa_temp's location if cfa_temp is used).
2017
2018   Rules 16-20: If AND operation happens on sp in prologue, we assume
2019                stack is realigned.  We will use a group of DW_OP_XXX
2020                expressions to represent the location of the stored
2021                register instead of CFA+offset.
2022
2023   The Rules
2024
2025   "{a,b}" indicates a choice of a xor b.
2026   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2027
2028   Rule 1:
2029   (set <reg1> <reg2>:cfa.reg)
2030   effects: cfa.reg = <reg1>
2031            cfa.offset unchanged
2032            cfa_temp.reg = <reg1>
2033            cfa_temp.offset = cfa.offset
2034
2035   Rule 2:
2036   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2037                               {<const_int>,<reg>:cfa_temp.reg}))
2038   effects: cfa.reg = sp if fp used
2039            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2040            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2041              if cfa_store.reg==sp
2042
2043   Rule 3:
2044   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2045   effects: cfa.reg = fp
2046            cfa_offset += +/- <const_int>
2047
2048   Rule 4:
2049   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2050   constraints: <reg1> != fp
2051                <reg1> != sp
2052   effects: cfa.reg = <reg1>
2053            cfa_temp.reg = <reg1>
2054            cfa_temp.offset = cfa.offset
2055
2056   Rule 5:
2057   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2058   constraints: <reg1> != fp
2059                <reg1> != sp
2060   effects: cfa_store.reg = <reg1>
2061            cfa_store.offset = cfa.offset - cfa_temp.offset
2062
2063   Rule 6:
2064   (set <reg> <const_int>)
2065   effects: cfa_temp.reg = <reg>
2066            cfa_temp.offset = <const_int>
2067
2068   Rule 7:
2069   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2070   effects: cfa_temp.reg = <reg1>
2071            cfa_temp.offset |= <const_int>
2072
2073   Rule 8:
2074   (set <reg> (high <exp>))
2075   effects: none
2076
2077   Rule 9:
2078   (set <reg> (lo_sum <exp> <const_int>))
2079   effects: cfa_temp.reg = <reg>
2080            cfa_temp.offset = <const_int>
2081
2082   Rule 10:
2083   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2084   effects: cfa_store.offset -= <const_int>
2085            cfa.offset = cfa_store.offset if cfa.reg == sp
2086            cfa.reg = sp
2087            cfa.base_offset = -cfa_store.offset
2088
2089   Rule 11:
2090   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2091   effects: cfa_store.offset += -/+ mode_size(mem)
2092            cfa.offset = cfa_store.offset if cfa.reg == sp
2093            cfa.reg = sp
2094            cfa.base_offset = -cfa_store.offset
2095
2096   Rule 12:
2097   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2098
2099        <reg2>)
2100   effects: cfa.reg = <reg1>
2101            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2102
2103   Rule 13:
2104   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2105   effects: cfa.reg = <reg1>
2106            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2107
2108   Rule 14:
2109   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2110   effects: cfa.reg = <reg1>
2111            cfa.base_offset = -cfa_temp.offset
2112            cfa_temp.offset -= mode_size(mem)
2113
2114   Rule 15:
2115   (set <reg> {unspec, unspec_volatile})
2116   effects: target-dependent
2117
2118   Rule 16:
2119   (set sp (and: sp <const_int>))
2120   constraints: cfa_store.reg == sp
2121   effects: current_fde.stack_realign = 1
2122            cfa_store.offset = 0
2123            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2124
2125   Rule 17:
2126   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2127   effects: cfa_store.offset += -/+ mode_size(mem)
2128
2129   Rule 18:
2130   (set (mem ({pre_inc, pre_dec} sp)) fp)
2131   constraints: fde->stack_realign == 1
2132   effects: cfa_store.offset = 0
2133            cfa.reg != HARD_FRAME_POINTER_REGNUM
2134
2135   Rule 19:
2136   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2137   constraints: fde->stack_realign == 1
2138                && cfa.offset == 0
2139                && cfa.indirect == 0
2140                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2141   effects: Use DW_CFA_def_cfa_expression to define cfa
2142            cfa.reg == fde->drap_reg
2143
2144   Rule 20:
2145   (set reg fde->drap_reg)
2146   constraints: fde->vdrap_reg == INVALID_REGNUM
2147   effects: fde->vdrap_reg = reg.
2148   (set mem fde->drap_reg)
2149   constraints: fde->drap_reg_saved == 1
2150   effects: none.  */
2151
2152 static void
2153 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2154 {
2155   rtx src, dest, span;
2156   HOST_WIDE_INT offset;
2157   dw_fde_ref fde;
2158
2159   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2160      the PARALLEL independently. The first element is always processed if
2161      it is a SET. This is for backward compatibility.   Other elements
2162      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2163      flag is set in them.  */
2164   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2165     {
2166       int par_index;
2167       int limit = XVECLEN (expr, 0);
2168       rtx elem;
2169
2170       /* PARALLELs have strict read-modify-write semantics, so we
2171          ought to evaluate every rvalue before changing any lvalue.
2172          It's cumbersome to do that in general, but there's an
2173          easy approximation that is enough for all current users:
2174          handle register saves before register assignments.  */
2175       if (GET_CODE (expr) == PARALLEL)
2176         for (par_index = 0; par_index < limit; par_index++)
2177           {
2178             elem = XVECEXP (expr, 0, par_index);
2179             if (GET_CODE (elem) == SET
2180                 && MEM_P (SET_DEST (elem))
2181                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2182               dwarf2out_frame_debug_expr (elem, label);
2183           }
2184
2185       for (par_index = 0; par_index < limit; par_index++)
2186         {
2187           elem = XVECEXP (expr, 0, par_index);
2188           if (GET_CODE (elem) == SET
2189               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2190               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2191             dwarf2out_frame_debug_expr (elem, label);
2192           else if (GET_CODE (elem) == SET
2193                    && par_index != 0
2194                    && !RTX_FRAME_RELATED_P (elem))
2195             {
2196               /* Stack adjustment combining might combine some post-prologue
2197                  stack adjustment into a prologue stack adjustment.  */
2198               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2199
2200               if (offset != 0)
2201                 dwarf2out_args_size_adjust (offset, label);
2202             }
2203         }
2204       return;
2205     }
2206
2207   gcc_assert (GET_CODE (expr) == SET);
2208
2209   src = SET_SRC (expr);
2210   dest = SET_DEST (expr);
2211
2212   if (REG_P (src))
2213     {
2214       rtx rsi = reg_saved_in (src);
2215       if (rsi)
2216         src = rsi;
2217     }
2218
2219   fde = current_fde ();
2220
2221   if (REG_P (src)
2222       && fde
2223       && fde->drap_reg == REGNO (src)
2224       && (fde->drap_reg_saved
2225           || REG_P (dest)))
2226     {
2227       /* Rule 20 */
2228       /* If we are saving dynamic realign argument pointer to a
2229          register, the destination is virtual dynamic realign
2230          argument pointer.  It may be used to access argument.  */
2231       if (REG_P (dest))
2232         {
2233           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2234           fde->vdrap_reg = REGNO (dest);
2235         }
2236       return;
2237     }
2238
2239   switch (GET_CODE (dest))
2240     {
2241     case REG:
2242       switch (GET_CODE (src))
2243         {
2244           /* Setting FP from SP.  */
2245         case REG:
2246           if (cfa.reg == (unsigned) REGNO (src))
2247             {
2248               /* Rule 1 */
2249               /* Update the CFA rule wrt SP or FP.  Make sure src is
2250                  relative to the current CFA register.
2251
2252                  We used to require that dest be either SP or FP, but the
2253                  ARM copies SP to a temporary register, and from there to
2254                  FP.  So we just rely on the backends to only set
2255                  RTX_FRAME_RELATED_P on appropriate insns.  */
2256               cfa.reg = REGNO (dest);
2257               cfa_temp.reg = cfa.reg;
2258               cfa_temp.offset = cfa.offset;
2259             }
2260           else
2261             {
2262               /* Saving a register in a register.  */
2263               gcc_assert (!fixed_regs [REGNO (dest)]
2264                           /* For the SPARC and its register window.  */
2265                           || (DWARF_FRAME_REGNUM (REGNO (src))
2266                               == DWARF_FRAME_RETURN_COLUMN));
2267
2268               /* After stack is aligned, we can only save SP in FP
2269                  if drap register is used.  In this case, we have
2270                  to restore stack pointer with the CFA value and we
2271                  don't generate this DWARF information.  */
2272               if (fde
2273                   && fde->stack_realign
2274                   && REGNO (src) == STACK_POINTER_REGNUM)
2275                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2276                             && fde->drap_reg != INVALID_REGNUM
2277                             && cfa.reg != REGNO (src));
2278               else
2279                 queue_reg_save (label, src, dest, 0);
2280             }
2281           break;
2282
2283         case PLUS:
2284         case MINUS:
2285         case LO_SUM:
2286           if (dest == stack_pointer_rtx)
2287             {
2288               /* Rule 2 */
2289               /* Adjusting SP.  */
2290               switch (GET_CODE (XEXP (src, 1)))
2291                 {
2292                 case CONST_INT:
2293                   offset = INTVAL (XEXP (src, 1));
2294                   break;
2295                 case REG:
2296                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2297                               == cfa_temp.reg);
2298                   offset = cfa_temp.offset;
2299                   break;
2300                 default:
2301                   gcc_unreachable ();
2302                 }
2303
2304               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2305                 {
2306                   /* Restoring SP from FP in the epilogue.  */
2307                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2308                   cfa.reg = STACK_POINTER_REGNUM;
2309                 }
2310               else if (GET_CODE (src) == LO_SUM)
2311                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2312                 ;
2313               else
2314                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2315
2316               if (GET_CODE (src) != MINUS)
2317                 offset = -offset;
2318               if (cfa.reg == STACK_POINTER_REGNUM)
2319                 cfa.offset += offset;
2320               if (cfa_store.reg == STACK_POINTER_REGNUM)
2321                 cfa_store.offset += offset;
2322             }
2323           else if (dest == hard_frame_pointer_rtx)
2324             {
2325               /* Rule 3 */
2326               /* Either setting the FP from an offset of the SP,
2327                  or adjusting the FP */
2328               gcc_assert (frame_pointer_needed);
2329
2330               gcc_assert (REG_P (XEXP (src, 0))
2331                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2332                           && CONST_INT_P (XEXP (src, 1)));
2333               offset = INTVAL (XEXP (src, 1));
2334               if (GET_CODE (src) != MINUS)
2335                 offset = -offset;
2336               cfa.offset += offset;
2337               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2338             }
2339           else
2340             {
2341               gcc_assert (GET_CODE (src) != MINUS);
2342
2343               /* Rule 4 */
2344               if (REG_P (XEXP (src, 0))
2345                   && REGNO (XEXP (src, 0)) == cfa.reg
2346                   && CONST_INT_P (XEXP (src, 1)))
2347                 {
2348                   /* Setting a temporary CFA register that will be copied
2349                      into the FP later on.  */
2350                   offset = - INTVAL (XEXP (src, 1));
2351                   cfa.offset += offset;
2352                   cfa.reg = REGNO (dest);
2353                   /* Or used to save regs to the stack.  */
2354                   cfa_temp.reg = cfa.reg;
2355                   cfa_temp.offset = cfa.offset;
2356                 }
2357
2358               /* Rule 5 */
2359               else if (REG_P (XEXP (src, 0))
2360                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2361                        && XEXP (src, 1) == stack_pointer_rtx)
2362                 {
2363                   /* Setting a scratch register that we will use instead
2364                      of SP for saving registers to the stack.  */
2365                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2366                   cfa_store.reg = REGNO (dest);
2367                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2368                 }
2369
2370               /* Rule 9 */
2371               else if (GET_CODE (src) == LO_SUM
2372                        && CONST_INT_P (XEXP (src, 1)))
2373                 {
2374                   cfa_temp.reg = REGNO (dest);
2375                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2376                 }
2377               else
2378                 gcc_unreachable ();
2379             }
2380           break;
2381
2382           /* Rule 6 */
2383         case CONST_INT:
2384           cfa_temp.reg = REGNO (dest);
2385           cfa_temp.offset = INTVAL (src);
2386           break;
2387
2388           /* Rule 7 */
2389         case IOR:
2390           gcc_assert (REG_P (XEXP (src, 0))
2391                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2392                       && CONST_INT_P (XEXP (src, 1)));
2393
2394           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2395             cfa_temp.reg = REGNO (dest);
2396           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2397           break;
2398
2399           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2400              which will fill in all of the bits.  */
2401           /* Rule 8 */
2402         case HIGH:
2403           break;
2404
2405           /* Rule 15 */
2406         case UNSPEC:
2407         case UNSPEC_VOLATILE:
2408           gcc_assert (targetm.dwarf_handle_frame_unspec);
2409           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2410           return;
2411
2412           /* Rule 16 */
2413         case AND:
2414           /* If this AND operation happens on stack pointer in prologue,
2415              we assume the stack is realigned and we extract the
2416              alignment.  */
2417           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2418             {
2419               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2420               fde->stack_realign = 1;
2421               fde->stack_realignment = INTVAL (XEXP (src, 1));
2422               cfa_store.offset = 0;
2423
2424               if (cfa.reg != STACK_POINTER_REGNUM
2425                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2426                 fde->drap_reg = cfa.reg;
2427             }
2428           return;
2429
2430         default:
2431           gcc_unreachable ();
2432         }
2433
2434       def_cfa_1 (label, &cfa);
2435       break;
2436
2437     case MEM:
2438
2439       /* Saving a register to the stack.  Make sure dest is relative to the
2440          CFA register.  */
2441       switch (GET_CODE (XEXP (dest, 0)))
2442         {
2443           /* Rule 10 */
2444           /* With a push.  */
2445         case PRE_MODIFY:
2446           /* We can't handle variable size modifications.  */
2447           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2448                       == CONST_INT);
2449           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2450
2451           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2452                       && cfa_store.reg == STACK_POINTER_REGNUM);
2453
2454           cfa_store.offset += offset;
2455           if (cfa.reg == STACK_POINTER_REGNUM)
2456             cfa.offset = cfa_store.offset;
2457
2458           offset = -cfa_store.offset;
2459           break;
2460
2461           /* Rule 11 */
2462         case PRE_INC:
2463         case PRE_DEC:
2464           offset = GET_MODE_SIZE (GET_MODE (dest));
2465           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2466             offset = -offset;
2467
2468           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2469                        == STACK_POINTER_REGNUM)
2470                       && cfa_store.reg == STACK_POINTER_REGNUM);
2471
2472           cfa_store.offset += offset;
2473
2474           /* Rule 18: If stack is aligned, we will use FP as a
2475              reference to represent the address of the stored
2476              regiser.  */
2477           if (fde
2478               && fde->stack_realign
2479               && src == hard_frame_pointer_rtx)
2480             {
2481               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2482               cfa_store.offset = 0;
2483             }
2484
2485           if (cfa.reg == STACK_POINTER_REGNUM)
2486             cfa.offset = cfa_store.offset;
2487
2488           offset = -cfa_store.offset;
2489           break;
2490
2491           /* Rule 12 */
2492           /* With an offset.  */
2493         case PLUS:
2494         case MINUS:
2495         case LO_SUM:
2496           {
2497             int regno;
2498
2499             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2500                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2501             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2502             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2503               offset = -offset;
2504
2505             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2506
2507             if (cfa_store.reg == (unsigned) regno)
2508               offset -= cfa_store.offset;
2509             else
2510               {
2511                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2512                 offset -= cfa_temp.offset;
2513               }
2514           }
2515           break;
2516
2517           /* Rule 13 */
2518           /* Without an offset.  */
2519         case REG:
2520           {
2521             int regno = REGNO (XEXP (dest, 0));
2522
2523             if (cfa_store.reg == (unsigned) regno)
2524               offset = -cfa_store.offset;
2525             else
2526               {
2527                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2528                 offset = -cfa_temp.offset;
2529               }
2530           }
2531           break;
2532
2533           /* Rule 14 */
2534         case POST_INC:
2535           gcc_assert (cfa_temp.reg
2536                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2537           offset = -cfa_temp.offset;
2538           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2539           break;
2540
2541         default:
2542           gcc_unreachable ();
2543         }
2544
2545         /* Rule 17 */
2546         /* If the source operand of this MEM operation is not a
2547            register, basically the source is return address.  Here
2548            we only care how much stack grew and we don't save it.  */
2549       if (!REG_P (src))
2550         break;
2551
2552       if (REGNO (src) != STACK_POINTER_REGNUM
2553           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2554           && (unsigned) REGNO (src) == cfa.reg)
2555         {
2556           /* We're storing the current CFA reg into the stack.  */
2557
2558           if (cfa.offset == 0)
2559             {
2560               /* Rule 19 */
2561               /* If stack is aligned, putting CFA reg into stack means
2562                  we can no longer use reg + offset to represent CFA.
2563                  Here we use DW_CFA_def_cfa_expression instead.  The
2564                  result of this expression equals to the original CFA
2565                  value.  */
2566               if (fde
2567                   && fde->stack_realign
2568                   && cfa.indirect == 0
2569                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2570                 {
2571                   dw_cfa_location cfa_exp;
2572
2573                   gcc_assert (fde->drap_reg == cfa.reg);
2574
2575                   cfa_exp.indirect = 1;
2576                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2577                   cfa_exp.base_offset = offset;
2578                   cfa_exp.offset = 0;
2579
2580                   fde->drap_reg_saved = 1;
2581
2582                   def_cfa_1 (label, &cfa_exp);
2583                   break;
2584                 }
2585
2586               /* If the source register is exactly the CFA, assume
2587                  we're saving SP like any other register; this happens
2588                  on the ARM.  */
2589               def_cfa_1 (label, &cfa);
2590               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2591               break;
2592             }
2593           else
2594             {
2595               /* Otherwise, we'll need to look in the stack to
2596                  calculate the CFA.  */
2597               rtx x = XEXP (dest, 0);
2598
2599               if (!REG_P (x))
2600                 x = XEXP (x, 0);
2601               gcc_assert (REG_P (x));
2602
2603               cfa.reg = REGNO (x);
2604               cfa.base_offset = offset;
2605               cfa.indirect = 1;
2606               def_cfa_1 (label, &cfa);
2607               break;
2608             }
2609         }
2610
2611       def_cfa_1 (label, &cfa);
2612       {
2613         span = targetm.dwarf_register_span (src);
2614
2615         if (!span)
2616           queue_reg_save (label, src, NULL_RTX, offset);
2617         else
2618           {
2619             /* We have a PARALLEL describing where the contents of SRC
2620                live.  Queue register saves for each piece of the
2621                PARALLEL.  */
2622             int par_index;
2623             int limit;
2624             HOST_WIDE_INT span_offset = offset;
2625
2626             gcc_assert (GET_CODE (span) == PARALLEL);
2627
2628             limit = XVECLEN (span, 0);
2629             for (par_index = 0; par_index < limit; par_index++)
2630               {
2631                 rtx elem = XVECEXP (span, 0, par_index);
2632
2633                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2634                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2635               }
2636           }
2637       }
2638       break;
2639
2640     default:
2641       gcc_unreachable ();
2642     }
2643 }
2644
2645 /* Record call frame debugging information for INSN, which either
2646    sets SP or FP (adjusting how we calculate the frame address) or saves a
2647    register to the stack.  If INSN is NULL_RTX, initialize our state.
2648
2649    If AFTER_P is false, we're being called before the insn is emitted,
2650    otherwise after.  Call instructions get invoked twice.  */
2651
2652 void
2653 dwarf2out_frame_debug (rtx insn, bool after_p)
2654 {
2655   const char *label;
2656   rtx note, n;
2657   bool handled_one = false;
2658
2659   if (insn == NULL_RTX)
2660     {
2661       size_t i;
2662
2663       /* Flush any queued register saves.  */
2664       flush_queued_reg_saves ();
2665
2666       /* Set up state for generating call frame debug info.  */
2667       lookup_cfa (&cfa);
2668       gcc_assert (cfa.reg
2669                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2670
2671       cfa.reg = STACK_POINTER_REGNUM;
2672       cfa_store = cfa;
2673       cfa_temp.reg = -1;
2674       cfa_temp.offset = 0;
2675
2676       for (i = 0; i < num_regs_saved_in_regs; i++)
2677         {
2678           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2679           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2680         }
2681       num_regs_saved_in_regs = 0;
2682
2683       if (barrier_args_size)
2684         {
2685           XDELETEVEC (barrier_args_size);
2686           barrier_args_size = NULL;
2687         }
2688       return;
2689     }
2690
2691   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2692     flush_queued_reg_saves ();
2693
2694   if (! RTX_FRAME_RELATED_P (insn))
2695     {
2696       if (!ACCUMULATE_OUTGOING_ARGS)
2697         dwarf2out_stack_adjust (insn, after_p);
2698       return;
2699     }
2700
2701   label = dwarf2out_cfi_label (false);
2702
2703   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2704     switch (REG_NOTE_KIND (note))
2705       {
2706       case REG_FRAME_RELATED_EXPR:
2707         insn = XEXP (note, 0);
2708         goto found;
2709
2710       case REG_CFA_DEF_CFA:
2711         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2712         handled_one = true;
2713         break;
2714
2715       case REG_CFA_ADJUST_CFA:
2716         n = XEXP (note, 0);
2717         if (n == NULL)
2718           {
2719             n = PATTERN (insn);
2720             if (GET_CODE (n) == PARALLEL)
2721               n = XVECEXP (n, 0, 0);
2722           }
2723         dwarf2out_frame_debug_adjust_cfa (n, label);
2724         handled_one = true;
2725         break;
2726
2727       case REG_CFA_OFFSET:
2728         n = XEXP (note, 0);
2729         if (n == NULL)
2730           n = single_set (insn);
2731         dwarf2out_frame_debug_cfa_offset (n, label);
2732         handled_one = true;
2733         break;
2734
2735       case REG_CFA_REGISTER:
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_cfa_register (n, label);
2744         handled_one = true;
2745         break;
2746
2747       case REG_CFA_RESTORE:
2748         n = XEXP (note, 0);
2749         if (n == NULL)
2750           {
2751             n = PATTERN (insn);
2752             if (GET_CODE (n) == PARALLEL)
2753               n = XVECEXP (n, 0, 0);
2754             n = XEXP (n, 0);
2755           }
2756         dwarf2out_frame_debug_cfa_restore (n, label);
2757         handled_one = true;
2758         break;
2759
2760       default:
2761         break;
2762       }
2763   if (handled_one)
2764     return;
2765
2766   insn = PATTERN (insn);
2767  found:
2768   dwarf2out_frame_debug_expr (insn, label);
2769 }
2770
2771 /* Determine if we need to save and restore CFI information around this
2772    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2773    we do need to save/restore, then emit the save now, and insert a
2774    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2775
2776 void
2777 dwarf2out_begin_epilogue (rtx insn)
2778 {
2779   bool saw_frp = false;
2780   rtx i;
2781
2782   /* Scan forward to the return insn, noticing if there are possible
2783      frame related insns.  */
2784   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2785     {
2786       if (!INSN_P (i))
2787         continue;
2788
2789       /* Look for both regular and sibcalls to end the block.  */
2790       if (returnjump_p (i))
2791         break;
2792       if (CALL_P (i) && SIBLING_CALL_P (i))
2793         break;
2794
2795       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2796         {
2797           int idx;
2798           rtx seq = PATTERN (i);
2799
2800           if (returnjump_p (XVECEXP (seq, 0, 0)))
2801             break;
2802           if (CALL_P (XVECEXP (seq, 0, 0))
2803               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2804             break;
2805
2806           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2807             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2808               saw_frp = true;
2809         }
2810
2811       if (RTX_FRAME_RELATED_P (i))
2812         saw_frp = true;
2813     }
2814
2815   /* If the port doesn't emit epilogue unwind info, we don't need a
2816      save/restore pair.  */
2817   if (!saw_frp)
2818     return;
2819
2820   /* Otherwise, search forward to see if the return insn was the last
2821      basic block of the function.  If so, we don't need save/restore.  */
2822   gcc_assert (i != NULL);
2823   i = next_real_insn (i);
2824   if (i == NULL)
2825     return;
2826
2827   /* Insert the restore before that next real insn in the stream, and before
2828      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2829      properly nested.  This should be after any label or alignment.  This
2830      will be pushed into the CFI stream by the function below.  */
2831   while (1)
2832     {
2833       rtx p = PREV_INSN (i);
2834       if (!NOTE_P (p))
2835         break;
2836       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2837         break;
2838       i = p;
2839     }
2840   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2841
2842   emit_cfa_remember = true;
2843
2844   /* And emulate the state save.  */
2845   gcc_assert (!cfa_remember.in_use);
2846   cfa_remember = cfa;
2847   cfa_remember.in_use = 1;
2848 }
2849
2850 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2851
2852 void
2853 dwarf2out_frame_debug_restore_state (void)
2854 {
2855   dw_cfi_ref cfi = new_cfi (); 
2856   const char *label = dwarf2out_cfi_label (false);
2857
2858   cfi->dw_cfi_opc = DW_CFA_restore_state;
2859   add_fde_cfi (label, cfi);
2860
2861   gcc_assert (cfa_remember.in_use);
2862   cfa = cfa_remember;
2863   cfa_remember.in_use = 0;
2864 }
2865
2866 #endif
2867
2868 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2869 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2870  (enum dwarf_call_frame_info cfi);
2871
2872 static enum dw_cfi_oprnd_type
2873 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2874 {
2875   switch (cfi)
2876     {
2877     case DW_CFA_nop:
2878     case DW_CFA_GNU_window_save:
2879     case DW_CFA_remember_state:
2880     case DW_CFA_restore_state:
2881       return dw_cfi_oprnd_unused;
2882
2883     case DW_CFA_set_loc:
2884     case DW_CFA_advance_loc1:
2885     case DW_CFA_advance_loc2:
2886     case DW_CFA_advance_loc4:
2887     case DW_CFA_MIPS_advance_loc8:
2888       return dw_cfi_oprnd_addr;
2889
2890     case DW_CFA_offset:
2891     case DW_CFA_offset_extended:
2892     case DW_CFA_def_cfa:
2893     case DW_CFA_offset_extended_sf:
2894     case DW_CFA_def_cfa_sf:
2895     case DW_CFA_restore:
2896     case DW_CFA_restore_extended:
2897     case DW_CFA_undefined:
2898     case DW_CFA_same_value:
2899     case DW_CFA_def_cfa_register:
2900     case DW_CFA_register:
2901       return dw_cfi_oprnd_reg_num;
2902
2903     case DW_CFA_def_cfa_offset:
2904     case DW_CFA_GNU_args_size:
2905     case DW_CFA_def_cfa_offset_sf:
2906       return dw_cfi_oprnd_offset;
2907
2908     case DW_CFA_def_cfa_expression:
2909     case DW_CFA_expression:
2910       return dw_cfi_oprnd_loc;
2911
2912     default:
2913       gcc_unreachable ();
2914     }
2915 }
2916
2917 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2918 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2919  (enum dwarf_call_frame_info cfi);
2920
2921 static enum dw_cfi_oprnd_type
2922 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2923 {
2924   switch (cfi)
2925     {
2926     case DW_CFA_def_cfa:
2927     case DW_CFA_def_cfa_sf:
2928     case DW_CFA_offset:
2929     case DW_CFA_offset_extended_sf:
2930     case DW_CFA_offset_extended:
2931       return dw_cfi_oprnd_offset;
2932
2933     case DW_CFA_register:
2934       return dw_cfi_oprnd_reg_num;
2935
2936     default:
2937       return dw_cfi_oprnd_unused;
2938     }
2939 }
2940
2941 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2942
2943 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2944    switch to the data section instead, and write out a synthetic start label
2945    for collect2 the first time around.  */
2946
2947 static void
2948 switch_to_eh_frame_section (bool back)
2949 {
2950   tree label;
2951
2952 #ifdef EH_FRAME_SECTION_NAME
2953   if (eh_frame_section == 0)
2954     {
2955       int flags;
2956
2957       if (EH_TABLES_CAN_BE_READ_ONLY)
2958         {
2959           int fde_encoding;
2960           int per_encoding;
2961           int lsda_encoding;
2962
2963           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2964                                                        /*global=*/0);
2965           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2966                                                        /*global=*/1);
2967           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2968                                                         /*global=*/0);
2969           flags = ((! flag_pic
2970                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2971                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2972                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2973                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2974                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2975                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2976                    ? 0 : SECTION_WRITE);
2977         }
2978       else
2979         flags = SECTION_WRITE;
2980       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2981     }
2982 #endif
2983
2984   if (eh_frame_section)
2985     switch_to_section (eh_frame_section);
2986   else
2987     {
2988       /* We have no special eh_frame section.  Put the information in
2989          the data section and emit special labels to guide collect2.  */
2990       switch_to_section (data_section);
2991
2992       if (!back)
2993         {
2994           label = get_file_function_name ("F");
2995           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2996           targetm.asm_out.globalize_label (asm_out_file,
2997                                            IDENTIFIER_POINTER (label));
2998           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2999         }
3000     }
3001 }
3002
3003 /* Switch [BACK] to the eh or debug frame table section, depending on
3004    FOR_EH.  */
3005
3006 static void
3007 switch_to_frame_table_section (int for_eh, bool back)
3008 {
3009   if (for_eh)
3010     switch_to_eh_frame_section (back);
3011   else
3012     {
3013       if (!debug_frame_section)
3014         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3015                                            SECTION_DEBUG, NULL);
3016       switch_to_section (debug_frame_section);
3017     }
3018 }
3019
3020 /* Output a Call Frame Information opcode and its operand(s).  */
3021
3022 static void
3023 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3024 {
3025   unsigned long r;
3026   HOST_WIDE_INT off;
3027
3028   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3029     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3030                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3031                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3032                          ((unsigned HOST_WIDE_INT)
3033                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3034   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3035     {
3036       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3037       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3038                            "DW_CFA_offset, column 0x%lx", r);
3039       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3040       dw2_asm_output_data_uleb128 (off, NULL);
3041     }
3042   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3043     {
3044       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3045       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3046                            "DW_CFA_restore, column 0x%lx", r);
3047     }
3048   else
3049     {
3050       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3051                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3052
3053       switch (cfi->dw_cfi_opc)
3054         {
3055         case DW_CFA_set_loc:
3056           if (for_eh)
3057             dw2_asm_output_encoded_addr_rtx (
3058                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3059                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3060                 false, NULL);
3061           else
3062             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3063                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3064           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3065           break;
3066
3067         case DW_CFA_advance_loc1:
3068           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3069                                 fde->dw_fde_current_label, NULL);
3070           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3071           break;
3072
3073         case DW_CFA_advance_loc2:
3074           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3075                                 fde->dw_fde_current_label, NULL);
3076           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3077           break;
3078
3079         case DW_CFA_advance_loc4:
3080           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3081                                 fde->dw_fde_current_label, NULL);
3082           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3083           break;
3084
3085         case DW_CFA_MIPS_advance_loc8:
3086           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3087                                 fde->dw_fde_current_label, NULL);
3088           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3089           break;
3090
3091         case DW_CFA_offset_extended:
3092           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3093           dw2_asm_output_data_uleb128 (r, NULL);
3094           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3095           dw2_asm_output_data_uleb128 (off, NULL);
3096           break;
3097
3098         case DW_CFA_def_cfa:
3099           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3100           dw2_asm_output_data_uleb128 (r, NULL);
3101           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3102           break;
3103
3104         case DW_CFA_offset_extended_sf:
3105           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3106           dw2_asm_output_data_uleb128 (r, NULL);
3107           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3108           dw2_asm_output_data_sleb128 (off, NULL);
3109           break;
3110
3111         case DW_CFA_def_cfa_sf:
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_sleb128 (off, NULL);
3116           break;
3117
3118         case DW_CFA_restore_extended:
3119         case DW_CFA_undefined:
3120         case DW_CFA_same_value:
3121         case DW_CFA_def_cfa_register:
3122           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3123           dw2_asm_output_data_uleb128 (r, NULL);
3124           break;
3125
3126         case DW_CFA_register:
3127           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3128           dw2_asm_output_data_uleb128 (r, NULL);
3129           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3130           dw2_asm_output_data_uleb128 (r, NULL);
3131           break;
3132
3133         case DW_CFA_def_cfa_offset:
3134         case DW_CFA_GNU_args_size:
3135           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3136           break;
3137
3138         case DW_CFA_def_cfa_offset_sf:
3139           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3140           dw2_asm_output_data_sleb128 (off, NULL);
3141           break;
3142
3143         case DW_CFA_GNU_window_save:
3144           break;
3145
3146         case DW_CFA_def_cfa_expression:
3147         case DW_CFA_expression:
3148           output_cfa_loc (cfi);
3149           break;
3150
3151         case DW_CFA_GNU_negative_offset_extended:
3152           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3153           gcc_unreachable ();
3154
3155         default:
3156           break;
3157         }
3158     }
3159 }
3160
3161 /* Similar, but do it via assembler directives instead.  */
3162
3163 static void
3164 output_cfi_directive (dw_cfi_ref cfi)
3165 {
3166   unsigned long r, r2;
3167
3168   switch (cfi->dw_cfi_opc)
3169     {
3170     case DW_CFA_advance_loc:
3171     case DW_CFA_advance_loc1:
3172     case DW_CFA_advance_loc2:
3173     case DW_CFA_advance_loc4:
3174     case DW_CFA_MIPS_advance_loc8:
3175     case DW_CFA_set_loc:
3176       /* Should only be created by add_fde_cfi in a code path not
3177          followed when emitting via directives.  The assembler is
3178          going to take care of this for us.  */
3179       gcc_unreachable ();
3180
3181     case DW_CFA_offset:
3182     case DW_CFA_offset_extended:
3183     case DW_CFA_offset_extended_sf:
3184       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3185       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3186                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3187       break;
3188
3189     case DW_CFA_restore:
3190     case DW_CFA_restore_extended:
3191       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3192       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3193       break;
3194
3195     case DW_CFA_undefined:
3196       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3197       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3198       break;
3199
3200     case DW_CFA_same_value:
3201       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3202       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3203       break;
3204
3205     case DW_CFA_def_cfa:
3206     case DW_CFA_def_cfa_sf:
3207       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3208       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3209                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3210       break;
3211
3212     case DW_CFA_def_cfa_register:
3213       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3214       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3215       break;
3216
3217     case DW_CFA_register:
3218       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3219       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3220       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3221       break;
3222
3223     case DW_CFA_def_cfa_offset:
3224     case DW_CFA_def_cfa_offset_sf:
3225       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3226                HOST_WIDE_INT_PRINT_DEC"\n",
3227                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3228       break;
3229
3230     case DW_CFA_remember_state:
3231       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3232       break;
3233     case DW_CFA_restore_state:
3234       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3235       break;
3236
3237     case DW_CFA_GNU_args_size:
3238       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3239       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3240       if (flag_debug_asm)
3241         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3242                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3243       fputc ('\n', asm_out_file);
3244       break;
3245
3246     case DW_CFA_GNU_window_save:
3247       fprintf (asm_out_file, "\t.cfi_window_save\n");
3248       break;
3249
3250     case DW_CFA_def_cfa_expression:
3251     case DW_CFA_expression:
3252       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3253       output_cfa_loc_raw (cfi);
3254       fputc ('\n', asm_out_file);
3255       break;
3256
3257     default:
3258       gcc_unreachable ();
3259     }
3260 }
3261
3262 DEF_VEC_P (dw_cfi_ref);
3263 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3264
3265 /* Output CFIs to bring current FDE to the same state as after executing
3266    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3267    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3268    other arguments to pass to output_cfi.  */
3269
3270 static void
3271 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3272 {
3273   struct dw_cfi_struct cfi_buf;
3274   dw_cfi_ref cfi2;
3275   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3276   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3277   unsigned int len, idx;
3278
3279   for (;; cfi = cfi->dw_cfi_next)
3280     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3281       {
3282       case DW_CFA_advance_loc:
3283       case DW_CFA_advance_loc1:
3284       case DW_CFA_advance_loc2:
3285       case DW_CFA_advance_loc4:
3286       case DW_CFA_MIPS_advance_loc8:
3287       case DW_CFA_set_loc:
3288         /* All advances should be ignored.  */
3289         break;
3290       case DW_CFA_remember_state:
3291         {
3292           dw_cfi_ref args_size = cfi_args_size;
3293
3294           /* Skip everything between .cfi_remember_state and
3295              .cfi_restore_state.  */
3296           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3297             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3298               break;
3299             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3300               args_size = cfi2;
3301             else
3302               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3303
3304           if (cfi2 == NULL)
3305             goto flush_all;
3306           else
3307             {
3308               cfi = cfi2;
3309               cfi_args_size = args_size;
3310             }
3311           break;
3312         }
3313       case DW_CFA_GNU_args_size:
3314         cfi_args_size = cfi;
3315         break;
3316       case DW_CFA_GNU_window_save:
3317         goto flush_all;
3318       case DW_CFA_offset:
3319       case DW_CFA_offset_extended:
3320       case DW_CFA_offset_extended_sf:
3321       case DW_CFA_restore:
3322       case DW_CFA_restore_extended:
3323       case DW_CFA_undefined:
3324       case DW_CFA_same_value:
3325       case DW_CFA_register:
3326       case DW_CFA_val_offset:
3327       case DW_CFA_val_offset_sf:
3328       case DW_CFA_expression:
3329       case DW_CFA_val_expression:
3330       case DW_CFA_GNU_negative_offset_extended:
3331         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3332           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3333                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3334         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3335         break;
3336       case DW_CFA_def_cfa:
3337       case DW_CFA_def_cfa_sf:
3338       case DW_CFA_def_cfa_expression:
3339         cfi_cfa = cfi;
3340         cfi_cfa_offset = cfi;
3341         break;
3342       case DW_CFA_def_cfa_register:
3343         cfi_cfa = cfi;
3344         break;
3345       case DW_CFA_def_cfa_offset:
3346       case DW_CFA_def_cfa_offset_sf:
3347         cfi_cfa_offset = cfi;
3348         break;
3349       case DW_CFA_nop:
3350         gcc_assert (cfi == NULL);
3351       flush_all:
3352         len = VEC_length (dw_cfi_ref, regs);
3353         for (idx = 0; idx < len; idx++)
3354           {
3355             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3356             if (cfi2 != NULL
3357                 && cfi2->dw_cfi_opc != DW_CFA_restore
3358                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3359               {
3360                 if (do_cfi_asm)
3361                   output_cfi_directive (cfi2);
3362                 else
3363                   output_cfi (cfi2, fde, for_eh);
3364               }
3365           }
3366         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3367           {
3368             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3369             cfi_buf = *cfi_cfa;
3370             switch (cfi_cfa_offset->dw_cfi_opc)
3371               {
3372               case DW_CFA_def_cfa_offset:
3373                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3374                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3375                 break;
3376               case DW_CFA_def_cfa_offset_sf:
3377                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3378                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3379                 break;
3380               case DW_CFA_def_cfa:
3381               case DW_CFA_def_cfa_sf:
3382                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3383                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3384                 break;
3385               default:
3386                 gcc_unreachable ();
3387               }
3388             cfi_cfa = &cfi_buf;
3389           }
3390         else if (cfi_cfa_offset)
3391           cfi_cfa = cfi_cfa_offset;
3392         if (cfi_cfa)
3393           {
3394             if (do_cfi_asm)
3395               output_cfi_directive (cfi_cfa);
3396             else
3397               output_cfi (cfi_cfa, fde, for_eh);
3398           }
3399         cfi_cfa = NULL;
3400         cfi_cfa_offset = NULL;
3401         if (cfi_args_size
3402             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3403           {
3404             if (do_cfi_asm)
3405               output_cfi_directive (cfi_args_size);
3406             else
3407               output_cfi (cfi_args_size, fde, for_eh);
3408           }
3409         cfi_args_size = NULL;
3410         if (cfi == NULL)
3411           {
3412             VEC_free (dw_cfi_ref, heap, regs);
3413             return;
3414           }
3415         else if (do_cfi_asm)
3416           output_cfi_directive (cfi);
3417         else
3418           output_cfi (cfi, fde, for_eh);
3419         break;
3420       default:
3421         gcc_unreachable ();
3422     }
3423 }
3424
3425 /* Output one FDE.  */
3426
3427 static void
3428 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3429             char *section_start_label, int fde_encoding, char *augmentation,
3430             bool any_lsda_needed, int lsda_encoding)
3431 {
3432   const char *begin, *end;
3433   static unsigned int j;
3434   char l1[20], l2[20];
3435   dw_cfi_ref cfi;
3436
3437   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3438                                 /* empty */ 0);
3439   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3440                                   for_eh + j);
3441   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3442   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3443   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3444     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3445                          " indicating 64-bit DWARF extension");
3446   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3447                         "FDE Length");
3448   ASM_OUTPUT_LABEL (asm_out_file, l1);
3449
3450   if (for_eh)
3451     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3452   else
3453     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3454                            debug_frame_section, "FDE CIE offset");
3455
3456   if (!fde->dw_fde_switched_sections)
3457     {
3458       begin = fde->dw_fde_begin;
3459       end = fde->dw_fde_end;
3460     }
3461   else
3462     {
3463       /* For the first section, prefer dw_fde_begin over
3464          dw_fde_{hot,cold}_section_label, as the latter
3465          might be separated from the real start of the
3466          function by alignment padding.  */
3467       if (!second)
3468         begin = fde->dw_fde_begin;
3469       else if (fde->dw_fde_switched_cold_to_hot)
3470         begin = fde->dw_fde_hot_section_label;
3471       else
3472         begin = fde->dw_fde_unlikely_section_label;
3473       if (second ^ fde->dw_fde_switched_cold_to_hot)
3474         end = fde->dw_fde_unlikely_section_end_label;
3475       else
3476         end = fde->dw_fde_hot_section_end_label;
3477     }
3478
3479   if (for_eh)
3480     {
3481       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3482       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3483       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3484                                        "FDE initial location");
3485       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3486                             end, begin, "FDE address range");
3487     }
3488   else
3489     {
3490       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3491       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3492     }
3493
3494   if (augmentation[0])
3495     {
3496       if (any_lsda_needed)
3497         {
3498           int size = size_of_encoded_value (lsda_encoding);
3499
3500           if (lsda_encoding == DW_EH_PE_aligned)
3501             {
3502               int offset = (  4         /* Length */
3503                             + 4         /* CIE offset */
3504                             + 2 * size_of_encoded_value (fde_encoding)
3505                             + 1         /* Augmentation size */ );
3506               int pad = -offset & (PTR_SIZE - 1);
3507
3508               size += pad;
3509               gcc_assert (size_of_uleb128 (size) == 1);
3510             }
3511
3512           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3513
3514           if (fde->uses_eh_lsda)
3515             {
3516               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3517                                            fde->funcdef_number);
3518               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3519                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3520                                                false,
3521                                                "Language Specific Data Area");
3522             }
3523           else
3524             {
3525               if (lsda_encoding == DW_EH_PE_aligned)
3526                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3527               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3528                                    "Language Specific Data Area (none)");
3529             }
3530         }
3531       else
3532         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3533     }
3534
3535   /* Loop through the Call Frame Instructions associated with
3536      this FDE.  */
3537   fde->dw_fde_current_label = begin;
3538   if (!fde->dw_fde_switched_sections)
3539     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3540       output_cfi (cfi, fde, for_eh);
3541   else if (!second)
3542     {
3543       if (fde->dw_fde_switch_cfi)
3544         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3545           {
3546             output_cfi (cfi, fde, for_eh);
3547             if (cfi == fde->dw_fde_switch_cfi)
3548               break;
3549           }
3550     }
3551   else
3552     {
3553       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3554
3555       if (fde->dw_fde_switch_cfi)
3556         {
3557           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3558           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3559           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3560           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3561         }
3562       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3563         output_cfi (cfi, fde, for_eh);
3564     }
3565
3566   /* If we are to emit a ref/link from function bodies to their frame tables,
3567      do it now.  This is typically performed to make sure that tables
3568      associated with functions are dragged with them and not discarded in
3569      garbage collecting links. We need to do this on a per function basis to
3570      cope with -ffunction-sections.  */
3571
3572 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3573   /* Switch to the function section, emit the ref to the tables, and
3574      switch *back* into the table section.  */
3575   switch_to_section (function_section (fde->decl));
3576   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3577   switch_to_frame_table_section (for_eh, true);
3578 #endif
3579
3580   /* Pad the FDE out to an address sized boundary.  */
3581   ASM_OUTPUT_ALIGN (asm_out_file,
3582                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3583   ASM_OUTPUT_LABEL (asm_out_file, l2);
3584
3585   j += 2;
3586 }
3587
3588 /* Output the call frame information used to record information
3589    that relates to calculating the frame pointer, and records the
3590    location of saved registers.  */
3591
3592 static void
3593 output_call_frame_info (int for_eh)
3594 {
3595   unsigned int i;
3596   dw_fde_ref fde;
3597   dw_cfi_ref cfi;
3598   char l1[20], l2[20], section_start_label[20];
3599   bool any_lsda_needed = false;
3600   char augmentation[6];
3601   int augmentation_size;
3602   int fde_encoding = DW_EH_PE_absptr;
3603   int per_encoding = DW_EH_PE_absptr;
3604   int lsda_encoding = DW_EH_PE_absptr;
3605   int return_reg;
3606   rtx personality = NULL;
3607   int dw_cie_version;
3608
3609   /* Don't emit a CIE if there won't be any FDEs.  */
3610   if (fde_table_in_use == 0)
3611     return;
3612
3613   /* Nothing to do if the assembler's doing it all.  */
3614   if (dwarf2out_do_cfi_asm ())
3615     return;
3616
3617   /* If we make FDEs linkonce, we may have to emit an empty label for
3618      an FDE that wouldn't otherwise be emitted.  We want to avoid
3619      having an FDE kept around when the function it refers to is
3620      discarded.  Example where this matters: a primary function
3621      template in C++ requires EH information, but an explicit
3622      specialization doesn't.  */
3623   if (TARGET_USES_WEAK_UNWIND_INFO
3624       && ! flag_asynchronous_unwind_tables
3625       && flag_exceptions
3626       && for_eh)
3627     for (i = 0; i < fde_table_in_use; i++)
3628       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3629           && !fde_table[i].uses_eh_lsda
3630           && ! DECL_WEAK (fde_table[i].decl))
3631         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3632                                       for_eh, /* empty */ 1);
3633
3634   /* If we don't have any functions we'll want to unwind out of, don't
3635      emit any EH unwind information.  Note that if exceptions aren't
3636      enabled, we won't have collected nothrow information, and if we
3637      asked for asynchronous tables, we always want this info.  */
3638   if (for_eh)
3639     {
3640       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3641
3642       for (i = 0; i < fde_table_in_use; i++)
3643         if (fde_table[i].uses_eh_lsda)
3644           any_eh_needed = any_lsda_needed = true;
3645         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3646           any_eh_needed = true;
3647         else if (! fde_table[i].nothrow
3648                  && ! fde_table[i].all_throwers_are_sibcalls)
3649           any_eh_needed = true;
3650
3651       if (! any_eh_needed)
3652         return;
3653     }
3654
3655   /* We're going to be generating comments, so turn on app.  */
3656   if (flag_debug_asm)
3657     app_enable ();
3658
3659   /* Switch to the proper frame section, first time.  */
3660   switch_to_frame_table_section (for_eh, false);
3661
3662   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3663   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3664
3665   /* Output the CIE.  */
3666   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3667   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3668   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3669     dw2_asm_output_data (4, 0xffffffff,
3670       "Initial length escape value indicating 64-bit DWARF extension");
3671   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3672                         "Length of Common Information Entry");
3673   ASM_OUTPUT_LABEL (asm_out_file, l1);
3674
3675   /* Now that the CIE pointer is PC-relative for EH,
3676      use 0 to identify the CIE.  */
3677   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3678                        (for_eh ? 0 : DWARF_CIE_ID),
3679                        "CIE Identifier Tag");
3680
3681   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3682      use CIE version 1, unless that would produce incorrect results
3683      due to overflowing the return register column.  */
3684   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3685   dw_cie_version = 1;
3686   if (return_reg >= 256 || dwarf_version > 2)
3687     dw_cie_version = 3;
3688   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3689
3690   augmentation[0] = 0;
3691   augmentation_size = 0;
3692
3693   personality = current_unit_personality;
3694   if (for_eh)
3695     {
3696       char *p;
3697
3698       /* Augmentation:
3699          z      Indicates that a uleb128 is present to size the
3700                 augmentation section.
3701          L      Indicates the encoding (and thus presence) of
3702                 an LSDA pointer in the FDE augmentation.
3703          R      Indicates a non-default pointer encoding for
3704                 FDE code pointers.
3705          P      Indicates the presence of an encoding + language
3706                 personality routine in the CIE augmentation.  */
3707
3708       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3709       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3710       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3711
3712       p = augmentation + 1;
3713       if (personality)
3714         {
3715           *p++ = 'P';
3716           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3717           assemble_external_libcall (personality);
3718         }
3719       if (any_lsda_needed)
3720         {
3721           *p++ = 'L';
3722           augmentation_size += 1;
3723         }
3724       if (fde_encoding != DW_EH_PE_absptr)
3725         {
3726           *p++ = 'R';
3727           augmentation_size += 1;
3728         }
3729       if (p > augmentation + 1)
3730         {
3731           augmentation[0] = 'z';
3732           *p = '\0';
3733         }
3734
3735       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3736       if (personality && per_encoding == DW_EH_PE_aligned)
3737         {
3738           int offset = (  4             /* Length */
3739                         + 4             /* CIE Id */
3740                         + 1             /* CIE version */
3741                         + strlen (augmentation) + 1     /* Augmentation */
3742                         + size_of_uleb128 (1)           /* Code alignment */
3743                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3744                         + 1             /* RA column */
3745                         + 1             /* Augmentation size */
3746                         + 1             /* Personality encoding */ );
3747           int pad = -offset & (PTR_SIZE - 1);
3748
3749           augmentation_size += pad;
3750
3751           /* Augmentations should be small, so there's scarce need to
3752              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3753           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3754         }
3755     }
3756
3757   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3758   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3759   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3760                                "CIE Data Alignment Factor");
3761
3762   if (dw_cie_version == 1)
3763     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3764   else
3765     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3766
3767   if (augmentation[0])
3768     {
3769       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3770       if (personality)
3771         {
3772           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3773                                eh_data_format_name (per_encoding));
3774           dw2_asm_output_encoded_addr_rtx (per_encoding,
3775                                            personality,
3776                                            true, NULL);
3777         }
3778
3779       if (any_lsda_needed)
3780         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3781                              eh_data_format_name (lsda_encoding));
3782
3783       if (fde_encoding != DW_EH_PE_absptr)
3784         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3785                              eh_data_format_name (fde_encoding));
3786     }
3787
3788   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3789     output_cfi (cfi, NULL, for_eh);
3790
3791   /* Pad the CIE out to an address sized boundary.  */
3792   ASM_OUTPUT_ALIGN (asm_out_file,
3793                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3794   ASM_OUTPUT_LABEL (asm_out_file, l2);
3795
3796   /* Loop through all of the FDE's.  */
3797   for (i = 0; i < fde_table_in_use; i++)
3798     {
3799       unsigned int k;
3800       fde = &fde_table[i];
3801
3802       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3803       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3804           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3805           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3806           && !fde->uses_eh_lsda)
3807         continue;
3808
3809       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3810         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3811                     augmentation, any_lsda_needed, lsda_encoding);
3812     }
3813
3814   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3815     dw2_asm_output_data (4, 0, "End of Table");
3816 #ifdef MIPS_DEBUGGING_INFO
3817   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3818      get a value of 0.  Putting .align 0 after the label fixes it.  */
3819   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3820 #endif
3821
3822   /* Turn off app to make assembly quicker.  */
3823   if (flag_debug_asm)
3824     app_disable ();
3825 }
3826
3827 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3828
3829 static void
3830 dwarf2out_do_cfi_startproc (bool second)
3831 {
3832   int enc;
3833   rtx ref;
3834   rtx personality = get_personality_function (current_function_decl);
3835
3836   fprintf (asm_out_file, "\t.cfi_startproc\n");
3837
3838   if (personality)
3839     {
3840       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3841       ref = personality;
3842
3843       /* ??? The GAS support isn't entirely consistent.  We have to
3844          handle indirect support ourselves, but PC-relative is done
3845          in the assembler.  Further, the assembler can't handle any
3846          of the weirder relocation types.  */
3847       if (enc & DW_EH_PE_indirect)
3848         ref = dw2_force_const_mem (ref, true);
3849
3850       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3851       output_addr_const (asm_out_file, ref);
3852       fputc ('\n', asm_out_file);
3853     }
3854
3855   if (crtl->uses_eh_lsda)
3856     {
3857       char lab[20];
3858
3859       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3860       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3861                                    current_function_funcdef_no);
3862       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3863       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3864
3865       if (enc & DW_EH_PE_indirect)
3866         ref = dw2_force_const_mem (ref, true);
3867
3868       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3869       output_addr_const (asm_out_file, ref);
3870       fputc ('\n', asm_out_file);
3871     }
3872 }
3873
3874 /* Output a marker (i.e. a label) for the beginning of a function, before
3875    the prologue.  */
3876
3877 void
3878 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3879                           const char *file ATTRIBUTE_UNUSED)
3880 {
3881   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3882   char * dup_label;
3883   dw_fde_ref fde;
3884   rtx personality;
3885   section *fnsec;
3886
3887   current_function_func_begin_label = NULL;
3888
3889 #ifdef TARGET_UNWIND_INFO
3890   /* ??? current_function_func_begin_label is also used by except.c
3891      for call-site information.  We must emit this label if it might
3892      be used.  */
3893   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3894       && ! dwarf2out_do_frame ())
3895     return;
3896 #else
3897   if (! dwarf2out_do_frame ())
3898     return;
3899 #endif
3900
3901   fnsec = function_section (current_function_decl);
3902   switch_to_section (fnsec);
3903   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3904                                current_function_funcdef_no);
3905   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3906                           current_function_funcdef_no);
3907   dup_label = xstrdup (label);
3908   current_function_func_begin_label = dup_label;
3909
3910 #ifdef TARGET_UNWIND_INFO
3911   /* We can elide the fde allocation if we're not emitting debug info.  */
3912   if (! dwarf2out_do_frame ())
3913     return;
3914 #endif
3915
3916   /* Expand the fde table if necessary.  */
3917   if (fde_table_in_use == fde_table_allocated)
3918     {
3919       fde_table_allocated += FDE_TABLE_INCREMENT;
3920       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3921       memset (fde_table + fde_table_in_use, 0,
3922               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3923     }
3924
3925   /* Record the FDE associated with this function.  */
3926   current_funcdef_fde = fde_table_in_use;
3927
3928   /* Add the new FDE at the end of the fde_table.  */
3929   fde = &fde_table[fde_table_in_use++];
3930   fde->decl = current_function_decl;
3931   fde->dw_fde_begin = dup_label;
3932   fde->dw_fde_current_label = dup_label;
3933   fde->dw_fde_hot_section_label = NULL;
3934   fde->dw_fde_hot_section_end_label = NULL;
3935   fde->dw_fde_unlikely_section_label = NULL;
3936   fde->dw_fde_unlikely_section_end_label = NULL;
3937   fde->dw_fde_switched_sections = 0;
3938   fde->dw_fde_switched_cold_to_hot = 0;
3939   fde->dw_fde_end = NULL;
3940   fde->dw_fde_cfi = NULL;
3941   fde->dw_fde_switch_cfi = NULL;
3942   fde->funcdef_number = current_function_funcdef_no;
3943   fde->nothrow = crtl->nothrow;
3944   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3945   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3946   fde->drap_reg = INVALID_REGNUM;
3947   fde->vdrap_reg = INVALID_REGNUM;
3948   if (flag_reorder_blocks_and_partition)
3949     {
3950       section *unlikelysec;
3951       if (first_function_block_is_cold)
3952         fde->in_std_section = 1;
3953       else
3954         fde->in_std_section
3955           = (fnsec == text_section
3956              || (cold_text_section && fnsec == cold_text_section));
3957       unlikelysec = unlikely_text_section ();
3958       fde->cold_in_std_section
3959         = (unlikelysec == text_section
3960            || (cold_text_section && unlikelysec == cold_text_section));
3961     }
3962   else
3963     {
3964       fde->in_std_section
3965         = (fnsec == text_section
3966            || (cold_text_section && fnsec == cold_text_section));
3967       fde->cold_in_std_section = 0;
3968     }
3969
3970   args_size = old_args_size = 0;
3971
3972   /* We only want to output line number information for the genuine dwarf2
3973      prologue case, not the eh frame case.  */
3974 #ifdef DWARF2_DEBUGGING_INFO
3975   if (file)
3976     dwarf2out_source_line (line, file, 0, true);
3977 #endif
3978
3979   personality = get_personality_function (current_function_decl);
3980   if (dwarf2out_do_cfi_asm ())
3981     dwarf2out_do_cfi_startproc (false);
3982   else
3983     {
3984       if (!current_unit_personality || current_unit_personality == personality)
3985         current_unit_personality = personality;
3986       else
3987         sorry ("Multiple EH personalities are supported only with assemblers "
3988                "supporting .cfi.personality directive.");
3989     }
3990 }
3991
3992 /* Output a marker (i.e. a label) for the absolute end of the generated code
3993    for a function definition.  This gets called *after* the epilogue code has
3994    been generated.  */
3995
3996 void
3997 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3998                         const char *file ATTRIBUTE_UNUSED)
3999 {
4000   dw_fde_ref fde;
4001   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4002
4003 #ifdef DWARF2_DEBUGGING_INFO
4004   last_var_location_insn = NULL_RTX;
4005 #endif
4006
4007   if (dwarf2out_do_cfi_asm ())
4008     fprintf (asm_out_file, "\t.cfi_endproc\n");
4009
4010   /* Output a label to mark the endpoint of the code generated for this
4011      function.  */
4012   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4013                                current_function_funcdef_no);
4014   ASM_OUTPUT_LABEL (asm_out_file, label);
4015   fde = current_fde ();
4016   gcc_assert (fde != NULL);
4017   fde->dw_fde_end = xstrdup (label);
4018 }
4019
4020 void
4021 dwarf2out_frame_init (void)
4022 {
4023   /* Allocate the initial hunk of the fde_table.  */
4024   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4025   fde_table_allocated = FDE_TABLE_INCREMENT;
4026   fde_table_in_use = 0;
4027
4028   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4029      sake of lookup_cfa.  */
4030
4031   /* On entry, the Canonical Frame Address is at SP.  */
4032   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4033
4034 #ifdef DWARF2_UNWIND_INFO
4035   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4036     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4037 #endif
4038 }
4039
4040 void
4041 dwarf2out_frame_finish (void)
4042 {
4043   /* Output call frame information.  */
4044   if (DWARF2_FRAME_INFO)
4045     output_call_frame_info (0);
4046
4047 #ifndef TARGET_UNWIND_INFO
4048   /* Output another copy for the unwinder.  */
4049   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4050     output_call_frame_info (1);
4051 #endif
4052 }
4053
4054 /* Note that the current function section is being used for code.  */
4055
4056 static void
4057 dwarf2out_note_section_used (void)
4058 {
4059   section *sec = current_function_section ();
4060   if (sec == text_section)
4061     text_section_used = true;
4062   else if (sec == cold_text_section)
4063     cold_text_section_used = true;
4064 }
4065
4066 void
4067 dwarf2out_switch_text_section (void)
4068 {
4069   dw_fde_ref fde = current_fde ();
4070
4071   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4072
4073   fde->dw_fde_switched_sections = 1;
4074   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4075
4076   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4077   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4078   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4079   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4080   have_multiple_function_sections = true;
4081
4082   /* Reset the current label on switching text sections, so that we
4083      don't attempt to advance_loc4 between labels in different sections.  */
4084   fde->dw_fde_current_label = NULL;
4085
4086   /* There is no need to mark used sections when not debugging.  */
4087   if (cold_text_section != NULL)
4088     dwarf2out_note_section_used ();
4089
4090   if (dwarf2out_do_cfi_asm ())
4091     fprintf (asm_out_file, "\t.cfi_endproc\n");
4092
4093   /* Now do the real section switch.  */
4094   switch_to_section (current_function_section ());
4095
4096   if (dwarf2out_do_cfi_asm ())
4097     {
4098       dwarf2out_do_cfi_startproc (true);
4099       /* As this is a different FDE, insert all current CFI instructions
4100          again.  */
4101       output_cfis (fde->dw_fde_cfi, true, fde, true);
4102     }
4103   else
4104     {
4105       dw_cfi_ref cfi = fde->dw_fde_cfi;
4106
4107       cfi = fde->dw_fde_cfi;
4108       if (cfi)
4109         while (cfi->dw_cfi_next != NULL)
4110           cfi = cfi->dw_cfi_next;
4111       fde->dw_fde_switch_cfi = cfi;
4112     }
4113 }
4114 #endif
4115 \f
4116 /* And now, the subset of the debugging information support code necessary
4117    for emitting location expressions.  */
4118
4119 /* Data about a single source file.  */
4120 struct GTY(()) dwarf_file_data {
4121   const char * filename;
4122   int emitted_number;
4123 };
4124
4125 typedef struct dw_val_struct *dw_val_ref;
4126 typedef struct die_struct *dw_die_ref;
4127 typedef const struct die_struct *const_dw_die_ref;
4128 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4129 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4130
4131 typedef struct GTY(()) deferred_locations_struct
4132 {
4133   tree variable;
4134   dw_die_ref die;
4135 } deferred_locations;
4136
4137 DEF_VEC_O(deferred_locations);
4138 DEF_VEC_ALLOC_O(deferred_locations,gc);
4139
4140 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4141
4142 /* Each DIE may have a series of attribute/value pairs.  Values
4143    can take on several forms.  The forms that are used in this
4144    implementation are listed below.  */
4145
4146 enum dw_val_class
4147 {
4148   dw_val_class_addr,
4149   dw_val_class_offset,
4150   dw_val_class_loc,
4151   dw_val_class_loc_list,
4152   dw_val_class_range_list,
4153   dw_val_class_const,
4154   dw_val_class_unsigned_const,
4155   dw_val_class_long_long,
4156   dw_val_class_vec,
4157   dw_val_class_flag,
4158   dw_val_class_die_ref,
4159   dw_val_class_fde_ref,
4160   dw_val_class_lbl_id,
4161   dw_val_class_lineptr,
4162   dw_val_class_str,
4163   dw_val_class_macptr,
4164   dw_val_class_file
4165 };
4166
4167 /* Describe a floating point constant value, or a vector constant value.  */
4168
4169 typedef struct GTY(()) dw_vec_struct {
4170   unsigned char * GTY((length ("%h.length"))) array;
4171   unsigned length;
4172   unsigned elt_size;
4173 }
4174 dw_vec_const;
4175
4176 /* The dw_val_node describes an attribute's value, as it is
4177    represented internally.  */
4178
4179 typedef struct GTY(()) dw_val_struct {
4180   enum dw_val_class val_class;
4181   union dw_val_struct_union
4182     {
4183       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4184       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4185       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4186       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4187       HOST_WIDE_INT GTY ((default)) val_int;
4188       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4189       rtx GTY ((tag ("dw_val_class_long_long"))) val_long_long;
4190       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4191       struct dw_val_die_union
4192         {
4193           dw_die_ref die;
4194           int external;
4195         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4196       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4197       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4198       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4199       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4200       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4201     }
4202   GTY ((desc ("%1.val_class"))) v;
4203 }
4204 dw_val_node;
4205
4206 /* Locations in memory are described using a sequence of stack machine
4207    operations.  */
4208
4209 typedef struct GTY(()) dw_loc_descr_struct {
4210   dw_loc_descr_ref dw_loc_next;
4211   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4212   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4213      from DW_OP_addr with a dtp-relative symbol relocation.  */
4214   unsigned int dtprel : 1;
4215   int dw_loc_addr;
4216   dw_val_node dw_loc_oprnd1;
4217   dw_val_node dw_loc_oprnd2;
4218 }
4219 dw_loc_descr_node;
4220
4221 /* Location lists are ranges + location descriptions for that range,
4222    so you can track variables that are in different places over
4223    their entire life.  */
4224 typedef struct GTY(()) dw_loc_list_struct {
4225   dw_loc_list_ref dw_loc_next;
4226   const char *begin; /* Label for begin address of range */
4227   const char *end;  /* Label for end address of range */
4228   char *ll_symbol; /* Label for beginning of location list.
4229                       Only on head of list */
4230   const char *section; /* Section this loclist is relative to */
4231   dw_loc_descr_ref expr;
4232 } dw_loc_list_node;
4233
4234 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4235
4236 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4237
4238 /* Convert a DWARF stack opcode into its string name.  */
4239
4240 static const char *
4241 dwarf_stack_op_name (unsigned int op)
4242 {
4243   switch (op)
4244     {
4245     case DW_OP_addr:
4246       return "DW_OP_addr";
4247     case DW_OP_deref:
4248       return "DW_OP_deref";
4249     case DW_OP_const1u:
4250       return "DW_OP_const1u";
4251     case DW_OP_const1s:
4252       return "DW_OP_const1s";
4253     case DW_OP_const2u:
4254       return "DW_OP_const2u";
4255     case DW_OP_const2s:
4256       return "DW_OP_const2s";
4257     case DW_OP_const4u:
4258       return "DW_OP_const4u";
4259     case DW_OP_const4s:
4260       return "DW_OP_const4s";
4261     case DW_OP_const8u:
4262       return "DW_OP_const8u";
4263     case DW_OP_const8s:
4264       return "DW_OP_const8s";
4265     case DW_OP_constu:
4266       return "DW_OP_constu";
4267     case DW_OP_consts:
4268       return "DW_OP_consts";
4269     case DW_OP_dup:
4270       return "DW_OP_dup";
4271     case DW_OP_drop:
4272       return "DW_OP_drop";
4273     case DW_OP_over:
4274       return "DW_OP_over";
4275     case DW_OP_pick:
4276       return "DW_OP_pick";
4277     case DW_OP_swap:
4278       return "DW_OP_swap";
4279     case DW_OP_rot:
4280       return "DW_OP_rot";
4281     case DW_OP_xderef:
4282       return "DW_OP_xderef";
4283     case DW_OP_abs:
4284       return "DW_OP_abs";
4285     case DW_OP_and:
4286       return "DW_OP_and";
4287     case DW_OP_div:
4288       return "DW_OP_div";
4289     case DW_OP_minus:
4290       return "DW_OP_minus";
4291     case DW_OP_mod:
4292       return "DW_OP_mod";
4293     case DW_OP_mul:
4294       return "DW_OP_mul";
4295     case DW_OP_neg:
4296       return "DW_OP_neg";
4297     case DW_OP_not:
4298       return "DW_OP_not";
4299     case DW_OP_or:
4300       return "DW_OP_or";
4301     case DW_OP_plus:
4302       return "DW_OP_plus";
4303     case DW_OP_plus_uconst:
4304       return "DW_OP_plus_uconst";
4305     case DW_OP_shl:
4306       return "DW_OP_shl";
4307     case DW_OP_shr:
4308       return "DW_OP_shr";
4309     case DW_OP_shra:
4310       return "DW_OP_shra";
4311     case DW_OP_xor:
4312       return "DW_OP_xor";
4313     case DW_OP_bra:
4314       return "DW_OP_bra";
4315     case DW_OP_eq:
4316       return "DW_OP_eq";
4317     case DW_OP_ge:
4318       return "DW_OP_ge";
4319     case DW_OP_gt:
4320       return "DW_OP_gt";
4321     case DW_OP_le:
4322       return "DW_OP_le";
4323     case DW_OP_lt:
4324       return "DW_OP_lt";
4325     case DW_OP_ne:
4326       return "DW_OP_ne";
4327     case DW_OP_skip:
4328       return "DW_OP_skip";
4329     case DW_OP_lit0:
4330       return "DW_OP_lit0";
4331     case DW_OP_lit1:
4332       return "DW_OP_lit1";
4333     case DW_OP_lit2:
4334       return "DW_OP_lit2";
4335     case DW_OP_lit3:
4336       return "DW_OP_lit3";
4337     case DW_OP_lit4:
4338       return "DW_OP_lit4";
4339     case DW_OP_lit5:
4340       return "DW_OP_lit5";
4341     case DW_OP_lit6:
4342       return "DW_OP_lit6";
4343     case DW_OP_lit7:
4344       return "DW_OP_lit7";
4345     case DW_OP_lit8:
4346       return "DW_OP_lit8";
4347     case DW_OP_lit9:
4348       return "DW_OP_lit9";
4349     case DW_OP_lit10:
4350       return "DW_OP_lit10";
4351     case DW_OP_lit11:
4352       return "DW_OP_lit11";
4353     case DW_OP_lit12:
4354       return "DW_OP_lit12";
4355     case DW_OP_lit13:
4356       return "DW_OP_lit13";
4357     case DW_OP_lit14:
4358       return "DW_OP_lit14";
4359     case DW_OP_lit15:
4360       return "DW_OP_lit15";
4361     case DW_OP_lit16:
4362       return "DW_OP_lit16";
4363     case DW_OP_lit17:
4364       return "DW_OP_lit17";
4365     case DW_OP_lit18:
4366       return "DW_OP_lit18";
4367     case DW_OP_lit19:
4368       return "DW_OP_lit19";
4369     case DW_OP_lit20:
4370       return "DW_OP_lit20";
4371     case DW_OP_lit21:
4372       return "DW_OP_lit21";
4373     case DW_OP_lit22:
4374       return "DW_OP_lit22";
4375     case DW_OP_lit23:
4376       return "DW_OP_lit23";
4377     case DW_OP_lit24:
4378       return "DW_OP_lit24";
4379     case DW_OP_lit25:
4380       return "DW_OP_lit25";
4381     case DW_OP_lit26:
4382       return "DW_OP_lit26";
4383     case DW_OP_lit27:
4384       return "DW_OP_lit27";
4385     case DW_OP_lit28:
4386       return "DW_OP_lit28";
4387     case DW_OP_lit29:
4388       return "DW_OP_lit29";
4389     case DW_OP_lit30:
4390       return "DW_OP_lit30";
4391     case DW_OP_lit31:
4392       return "DW_OP_lit31";
4393     case DW_OP_reg0:
4394       return "DW_OP_reg0";
4395     case DW_OP_reg1:
4396       return "DW_OP_reg1";
4397     case DW_OP_reg2:
4398       return "DW_OP_reg2";
4399     case DW_OP_reg3:
4400       return "DW_OP_reg3";
4401     case DW_OP_reg4:
4402       return "DW_OP_reg4";
4403     case DW_OP_reg5:
4404       return "DW_OP_reg5";
4405     case DW_OP_reg6:
4406       return "DW_OP_reg6";
4407     case DW_OP_reg7:
4408       return "DW_OP_reg7";
4409     case DW_OP_reg8:
4410       return "DW_OP_reg8";
4411     case DW_OP_reg9:
4412       return "DW_OP_reg9";
4413     case DW_OP_reg10:
4414       return "DW_OP_reg10";
4415     case DW_OP_reg11:
4416       return "DW_OP_reg11";
4417     case DW_OP_reg12:
4418       return "DW_OP_reg12";
4419     case DW_OP_reg13:
4420       return "DW_OP_reg13";
4421     case DW_OP_reg14:
4422       return "DW_OP_reg14";
4423     case DW_OP_reg15:
4424       return "DW_OP_reg15";
4425     case DW_OP_reg16:
4426       return "DW_OP_reg16";
4427     case DW_OP_reg17:
4428       return "DW_OP_reg17";
4429     case DW_OP_reg18:
4430       return "DW_OP_reg18";
4431     case DW_OP_reg19:
4432       return "DW_OP_reg19";
4433     case DW_OP_reg20:
4434       return "DW_OP_reg20";
4435     case DW_OP_reg21:
4436       return "DW_OP_reg21";
4437     case DW_OP_reg22:
4438       return "DW_OP_reg22";
4439     case DW_OP_reg23:
4440       return "DW_OP_reg23";
4441     case DW_OP_reg24:
4442       return "DW_OP_reg24";
4443     case DW_OP_reg25:
4444       return "DW_OP_reg25";
4445     case DW_OP_reg26:
4446       return "DW_OP_reg26";
4447     case DW_OP_reg27:
4448       return "DW_OP_reg27";
4449     case DW_OP_reg28:
4450       return "DW_OP_reg28";
4451     case DW_OP_reg29:
4452       return "DW_OP_reg29";
4453     case DW_OP_reg30:
4454       return "DW_OP_reg30";
4455     case DW_OP_reg31:
4456       return "DW_OP_reg31";
4457     case DW_OP_breg0:
4458       return "DW_OP_breg0";
4459     case DW_OP_breg1:
4460       return "DW_OP_breg1";
4461     case DW_OP_breg2:
4462       return "DW_OP_breg2";
4463     case DW_OP_breg3:
4464       return "DW_OP_breg3";
4465     case DW_OP_breg4:
4466       return "DW_OP_breg4";
4467     case DW_OP_breg5:
4468       return "DW_OP_breg5";
4469     case DW_OP_breg6:
4470       return "DW_OP_breg6";
4471     case DW_OP_breg7:
4472       return "DW_OP_breg7";
4473     case DW_OP_breg8:
4474       return "DW_OP_breg8";
4475     case DW_OP_breg9:
4476       return "DW_OP_breg9";
4477     case DW_OP_breg10:
4478       return "DW_OP_breg10";
4479     case DW_OP_breg11:
4480       return "DW_OP_breg11";
4481     case DW_OP_breg12:
4482       return "DW_OP_breg12";
4483     case DW_OP_breg13:
4484       return "DW_OP_breg13";
4485     case DW_OP_breg14:
4486       return "DW_OP_breg14";
4487     case DW_OP_breg15:
4488       return "DW_OP_breg15";
4489     case DW_OP_breg16:
4490       return "DW_OP_breg16";
4491     case DW_OP_breg17:
4492       return "DW_OP_breg17";
4493     case DW_OP_breg18:
4494       return "DW_OP_breg18";
4495     case DW_OP_breg19:
4496       return "DW_OP_breg19";
4497     case DW_OP_breg20:
4498       return "DW_OP_breg20";
4499     case DW_OP_breg21:
4500       return "DW_OP_breg21";
4501     case DW_OP_breg22:
4502       return "DW_OP_breg22";
4503     case DW_OP_breg23:
4504       return "DW_OP_breg23";
4505     case DW_OP_breg24:
4506       return "DW_OP_breg24";
4507     case DW_OP_breg25:
4508       return "DW_OP_breg25";
4509     case DW_OP_breg26:
4510       return "DW_OP_breg26";
4511     case DW_OP_breg27:
4512       return "DW_OP_breg27";
4513     case DW_OP_breg28:
4514       return "DW_OP_breg28";
4515     case DW_OP_breg29:
4516       return "DW_OP_breg29";
4517     case DW_OP_breg30:
4518       return "DW_OP_breg30";
4519     case DW_OP_breg31:
4520       return "DW_OP_breg31";
4521     case DW_OP_regx:
4522       return "DW_OP_regx";
4523     case DW_OP_fbreg:
4524       return "DW_OP_fbreg";
4525     case DW_OP_bregx:
4526       return "DW_OP_bregx";
4527     case DW_OP_piece:
4528       return "DW_OP_piece";
4529     case DW_OP_deref_size:
4530       return "DW_OP_deref_size";
4531     case DW_OP_xderef_size:
4532       return "DW_OP_xderef_size";
4533     case DW_OP_nop:
4534       return "DW_OP_nop";
4535
4536     case DW_OP_push_object_address:
4537       return "DW_OP_push_object_address";
4538     case DW_OP_call2:
4539       return "DW_OP_call2";
4540     case DW_OP_call4:
4541       return "DW_OP_call4";
4542     case DW_OP_call_ref:
4543       return "DW_OP_call_ref";
4544     case DW_OP_implicit_value:
4545       return "DW_OP_implicit_value";
4546     case DW_OP_stack_value:
4547       return "DW_OP_stack_value";
4548     case DW_OP_form_tls_address:
4549       return "DW_OP_form_tls_address";
4550     case DW_OP_call_frame_cfa:
4551       return "DW_OP_call_frame_cfa";
4552     case DW_OP_bit_piece:
4553       return "DW_OP_bit_piece";
4554
4555     case DW_OP_GNU_push_tls_address:
4556       return "DW_OP_GNU_push_tls_address";
4557     case DW_OP_GNU_uninit:
4558       return "DW_OP_GNU_uninit";
4559     case DW_OP_GNU_encoded_addr:
4560       return "DW_OP_GNU_encoded_addr";
4561
4562     default:
4563       return "OP_<unknown>";
4564     }
4565 }
4566
4567 /* Return a pointer to a newly allocated location description.  Location
4568    descriptions are simple expression terms that can be strung
4569    together to form more complicated location (address) descriptions.  */
4570
4571 static inline dw_loc_descr_ref
4572 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4573                unsigned HOST_WIDE_INT oprnd2)
4574 {
4575   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4576
4577   descr->dw_loc_opc = op;
4578   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4579   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4580   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4581   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4582
4583   return descr;
4584 }
4585
4586 /* Return a pointer to a newly allocated location description for
4587    REG and OFFSET.  */
4588
4589 static inline dw_loc_descr_ref
4590 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4591 {
4592   if (reg <= 31)
4593     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4594                           offset, 0);
4595   else
4596     return new_loc_descr (DW_OP_bregx, reg, offset);
4597 }
4598
4599 /* Add a location description term to a location description expression.  */
4600
4601 static inline void
4602 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4603 {
4604   dw_loc_descr_ref *d;
4605
4606   /* Find the end of the chain.  */
4607   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4608     ;
4609
4610   *d = descr;
4611 }
4612
4613 /* Add a constant OFFSET to a location expression.  */
4614
4615 static void
4616 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4617 {
4618   dw_loc_descr_ref loc;
4619   HOST_WIDE_INT *p;
4620
4621   gcc_assert (*list_head != NULL);
4622
4623   if (!offset)
4624     return;
4625
4626   /* Find the end of the chain.  */
4627   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4628     ;
4629
4630   p = NULL;
4631   if (loc->dw_loc_opc == DW_OP_fbreg
4632       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4633     p = &loc->dw_loc_oprnd1.v.val_int;
4634   else if (loc->dw_loc_opc == DW_OP_bregx)
4635     p = &loc->dw_loc_oprnd2.v.val_int;
4636
4637   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4638      offset.  Don't optimize if an signed integer overflow would happen.  */
4639   if (p != NULL
4640       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4641           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4642     *p += offset;
4643
4644   else if (offset > 0)
4645     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4646
4647   else
4648     {
4649       loc->dw_loc_next = int_loc_descriptor (offset);
4650       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4651     }
4652 }
4653
4654 /* Return the size of a location descriptor.  */
4655
4656 static unsigned long
4657 size_of_loc_descr (dw_loc_descr_ref loc)
4658 {
4659   unsigned long size = 1;
4660
4661   switch (loc->dw_loc_opc)
4662     {
4663     case DW_OP_addr:
4664       size += DWARF2_ADDR_SIZE;
4665       break;
4666     case DW_OP_const1u:
4667     case DW_OP_const1s:
4668       size += 1;
4669       break;
4670     case DW_OP_const2u:
4671     case DW_OP_const2s:
4672       size += 2;
4673       break;
4674     case DW_OP_const4u:
4675     case DW_OP_const4s:
4676       size += 4;
4677       break;
4678     case DW_OP_const8u:
4679     case DW_OP_const8s:
4680       size += 8;
4681       break;
4682     case DW_OP_constu:
4683       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4684       break;
4685     case DW_OP_consts:
4686       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4687       break;
4688     case DW_OP_pick:
4689       size += 1;
4690       break;
4691     case DW_OP_plus_uconst:
4692       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4693       break;
4694     case DW_OP_skip:
4695     case DW_OP_bra:
4696       size += 2;
4697       break;
4698     case DW_OP_breg0:
4699     case DW_OP_breg1:
4700     case DW_OP_breg2:
4701     case DW_OP_breg3:
4702     case DW_OP_breg4:
4703     case DW_OP_breg5:
4704     case DW_OP_breg6:
4705     case DW_OP_breg7:
4706     case DW_OP_breg8:
4707     case DW_OP_breg9:
4708     case DW_OP_breg10:
4709     case DW_OP_breg11:
4710     case DW_OP_breg12:
4711     case DW_OP_breg13:
4712     case DW_OP_breg14:
4713     case DW_OP_breg15:
4714     case DW_OP_breg16:
4715     case DW_OP_breg17:
4716     case DW_OP_breg18:
4717     case DW_OP_breg19:
4718     case DW_OP_breg20:
4719     case DW_OP_breg21:
4720     case DW_OP_breg22:
4721     case DW_OP_breg23:
4722     case DW_OP_breg24:
4723     case DW_OP_breg25:
4724     case DW_OP_breg26:
4725     case DW_OP_breg27:
4726     case DW_OP_breg28:
4727     case DW_OP_breg29:
4728     case DW_OP_breg30:
4729     case DW_OP_breg31:
4730       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4731       break;
4732     case DW_OP_regx:
4733       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4734       break;
4735     case DW_OP_fbreg:
4736       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4737       break;
4738     case DW_OP_bregx:
4739       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4740       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4741       break;
4742     case DW_OP_piece:
4743       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4744       break;
4745     case DW_OP_deref_size:
4746     case DW_OP_xderef_size:
4747       size += 1;
4748       break;
4749     case DW_OP_call2:
4750       size += 2;
4751       break;
4752     case DW_OP_call4:
4753       size += 4;
4754       break;
4755     case DW_OP_call_ref:
4756       size += DWARF2_ADDR_SIZE;
4757       break;
4758     case DW_OP_implicit_value:
4759       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4760               + loc->dw_loc_oprnd1.v.val_unsigned;
4761       break;
4762     default:
4763       break;
4764     }
4765
4766   return size;
4767 }
4768
4769 /* Return the size of a series of location descriptors.  */
4770
4771 static unsigned long
4772 size_of_locs (dw_loc_descr_ref loc)
4773 {
4774   dw_loc_descr_ref l;
4775   unsigned long size;
4776
4777   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4778      field, to avoid writing to a PCH file.  */
4779   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4780     {
4781       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4782         break;
4783       size += size_of_loc_descr (l);
4784     }
4785   if (! l)
4786     return size;
4787
4788   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4789     {
4790       l->dw_loc_addr = size;
4791       size += size_of_loc_descr (l);
4792     }
4793
4794   return size;
4795 }
4796
4797 #ifdef DWARF2_DEBUGGING_INFO
4798 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4799 #endif
4800
4801 /* Output location description stack opcode's operands (if any).  */
4802
4803 static void
4804 output_loc_operands (dw_loc_descr_ref loc)
4805 {
4806   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4807   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4808
4809   switch (loc->dw_loc_opc)
4810     {
4811 #ifdef DWARF2_DEBUGGING_INFO
4812     case DW_OP_const2u:
4813     case DW_OP_const2s:
4814       dw2_asm_output_data (2, val1->v.val_int, NULL);
4815       break;
4816     case DW_OP_const4u:
4817     case DW_OP_const4s:
4818       dw2_asm_output_data (4, val1->v.val_int, NULL);
4819       break;
4820     case DW_OP_const8u:
4821     case DW_OP_const8s:
4822       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4823       dw2_asm_output_data (8, val1->v.val_int, NULL);
4824       break;
4825     case DW_OP_skip:
4826     case DW_OP_bra:
4827       {
4828         int offset;
4829
4830         gcc_assert (val1->val_class == dw_val_class_loc);
4831         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4832
4833         dw2_asm_output_data (2, offset, NULL);
4834       }
4835       break;
4836     case DW_OP_implicit_value:
4837       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4838       switch (val2->val_class)
4839         {
4840         case dw_val_class_const:
4841           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4842           break;
4843         case dw_val_class_vec:
4844           {
4845             unsigned int elt_size = val2->v.val_vec.elt_size;
4846             unsigned int len = val2->v.val_vec.length;
4847             unsigned int i;
4848             unsigned char *p;
4849
4850             if (elt_size > sizeof (HOST_WIDE_INT))
4851               {
4852                 elt_size /= 2;
4853                 len *= 2;
4854               }
4855             for (i = 0, p = val2->v.val_vec.array;
4856                  i < len;
4857                  i++, p += elt_size)
4858               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4859                                    "fp or vector constant word %u", i);
4860           }
4861           break;
4862         case dw_val_class_long_long:
4863           {
4864             unsigned HOST_WIDE_INT first, second;
4865
4866             if (WORDS_BIG_ENDIAN)
4867               {
4868                 first = CONST_DOUBLE_HIGH (val2->v.val_long_long);
4869                 second = CONST_DOUBLE_LOW (val2->v.val_long_long);
4870               }
4871             else
4872               {
4873                 first = CONST_DOUBLE_LOW (val2->v.val_long_long);
4874                 second = CONST_DOUBLE_HIGH (val2->v.val_long_long);
4875               }
4876             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4877                                  first, "long long constant");
4878             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4879                                  second, NULL);
4880           }
4881           break;
4882         case dw_val_class_addr:
4883           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4884           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4885           break;
4886         default:
4887           gcc_unreachable ();
4888         }
4889       break;
4890 #else
4891     case DW_OP_const2u:
4892     case DW_OP_const2s:
4893     case DW_OP_const4u:
4894     case DW_OP_const4s:
4895     case DW_OP_const8u:
4896     case DW_OP_const8s:
4897     case DW_OP_skip:
4898     case DW_OP_bra:
4899     case DW_OP_implicit_value:
4900       /* We currently don't make any attempt to make sure these are
4901          aligned properly like we do for the main unwind info, so
4902          don't support emitting things larger than a byte if we're
4903          only doing unwinding.  */
4904       gcc_unreachable ();
4905 #endif
4906     case DW_OP_const1u:
4907     case DW_OP_const1s:
4908       dw2_asm_output_data (1, val1->v.val_int, NULL);
4909       break;
4910     case DW_OP_constu:
4911       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4912       break;
4913     case DW_OP_consts:
4914       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4915       break;
4916     case DW_OP_pick:
4917       dw2_asm_output_data (1, val1->v.val_int, NULL);
4918       break;
4919     case DW_OP_plus_uconst:
4920       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4921       break;
4922     case DW_OP_breg0:
4923     case DW_OP_breg1:
4924     case DW_OP_breg2:
4925     case DW_OP_breg3:
4926     case DW_OP_breg4:
4927     case DW_OP_breg5:
4928     case DW_OP_breg6:
4929     case DW_OP_breg7:
4930     case DW_OP_breg8:
4931     case DW_OP_breg9:
4932     case DW_OP_breg10:
4933     case DW_OP_breg11:
4934     case DW_OP_breg12:
4935     case DW_OP_breg13:
4936     case DW_OP_breg14:
4937     case DW_OP_breg15:
4938     case DW_OP_breg16:
4939     case DW_OP_breg17:
4940     case DW_OP_breg18:
4941     case DW_OP_breg19:
4942     case DW_OP_breg20:
4943     case DW_OP_breg21:
4944     case DW_OP_breg22:
4945     case DW_OP_breg23:
4946     case DW_OP_breg24:
4947     case DW_OP_breg25:
4948     case DW_OP_breg26:
4949     case DW_OP_breg27:
4950     case DW_OP_breg28:
4951     case DW_OP_breg29:
4952     case DW_OP_breg30:
4953     case DW_OP_breg31:
4954       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4955       break;
4956     case DW_OP_regx:
4957       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4958       break;
4959     case DW_OP_fbreg:
4960       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4961       break;
4962     case DW_OP_bregx:
4963       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4964       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4965       break;
4966     case DW_OP_piece:
4967       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4968       break;
4969     case DW_OP_deref_size:
4970     case DW_OP_xderef_size:
4971       dw2_asm_output_data (1, val1->v.val_int, NULL);
4972       break;
4973
4974     case DW_OP_addr:
4975       if (loc->dtprel)
4976         {
4977           if (targetm.asm_out.output_dwarf_dtprel)
4978             {
4979               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4980                                                    DWARF2_ADDR_SIZE,
4981                                                    val1->v.val_addr);
4982               fputc ('\n', asm_out_file);
4983             }
4984           else
4985             gcc_unreachable ();
4986         }
4987       else
4988         {
4989 #ifdef DWARF2_DEBUGGING_INFO
4990           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4991 #else
4992           gcc_unreachable ();
4993 #endif
4994         }
4995       break;
4996
4997     default:
4998       /* Other codes have no operands.  */
4999       break;
5000     }
5001 }
5002
5003 /* Output a sequence of location operations.  */
5004
5005 static void
5006 output_loc_sequence (dw_loc_descr_ref loc)
5007 {
5008   for (; loc != NULL; loc = loc->dw_loc_next)
5009     {
5010       /* Output the opcode.  */
5011       dw2_asm_output_data (1, loc->dw_loc_opc,
5012                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5013
5014       /* Output the operand(s) (if any).  */
5015       output_loc_operands (loc);
5016     }
5017 }
5018
5019 /* Output location description stack opcode's operands (if any).
5020    The output is single bytes on a line, suitable for .cfi_escape.  */
5021
5022 static void
5023 output_loc_operands_raw (dw_loc_descr_ref loc)
5024 {
5025   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5026   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5027
5028   switch (loc->dw_loc_opc)
5029     {
5030     case DW_OP_addr:
5031     case DW_OP_implicit_value:
5032       /* We cannot output addresses in .cfi_escape, only bytes.  */
5033       gcc_unreachable ();
5034
5035     case DW_OP_const1u:
5036     case DW_OP_const1s:
5037     case DW_OP_pick:
5038     case DW_OP_deref_size:
5039     case DW_OP_xderef_size:
5040       fputc (',', asm_out_file);
5041       dw2_asm_output_data_raw (1, val1->v.val_int);
5042       break;
5043
5044     case DW_OP_const2u:
5045     case DW_OP_const2s:
5046       fputc (',', asm_out_file);
5047       dw2_asm_output_data_raw (2, val1->v.val_int);
5048       break;
5049
5050     case DW_OP_const4u:
5051     case DW_OP_const4s:
5052       fputc (',', asm_out_file);
5053       dw2_asm_output_data_raw (4, val1->v.val_int);
5054       break;
5055
5056     case DW_OP_const8u:
5057     case DW_OP_const8s:
5058       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5059       fputc (',', asm_out_file);
5060       dw2_asm_output_data_raw (8, val1->v.val_int);
5061       break;
5062
5063     case DW_OP_skip:
5064     case DW_OP_bra:
5065       {
5066         int offset;
5067
5068         gcc_assert (val1->val_class == dw_val_class_loc);
5069         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5070
5071         fputc (',', asm_out_file);
5072         dw2_asm_output_data_raw (2, offset);
5073       }
5074       break;
5075
5076     case DW_OP_constu:
5077     case DW_OP_plus_uconst:
5078     case DW_OP_regx:
5079     case DW_OP_piece:
5080       fputc (',', asm_out_file);
5081       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5082       break;
5083
5084     case DW_OP_consts:
5085     case DW_OP_breg0:
5086     case DW_OP_breg1:
5087     case DW_OP_breg2:
5088     case DW_OP_breg3:
5089     case DW_OP_breg4:
5090     case DW_OP_breg5:
5091     case DW_OP_breg6:
5092     case DW_OP_breg7:
5093     case DW_OP_breg8:
5094     case DW_OP_breg9:
5095     case DW_OP_breg10:
5096     case DW_OP_breg11:
5097     case DW_OP_breg12:
5098     case DW_OP_breg13:
5099     case DW_OP_breg14:
5100     case DW_OP_breg15:
5101     case DW_OP_breg16:
5102     case DW_OP_breg17:
5103     case DW_OP_breg18:
5104     case DW_OP_breg19:
5105     case DW_OP_breg20:
5106     case DW_OP_breg21:
5107     case DW_OP_breg22:
5108     case DW_OP_breg23:
5109     case DW_OP_breg24:
5110     case DW_OP_breg25:
5111     case DW_OP_breg26:
5112     case DW_OP_breg27:
5113     case DW_OP_breg28:
5114     case DW_OP_breg29:
5115     case DW_OP_breg30:
5116     case DW_OP_breg31:
5117     case DW_OP_fbreg:
5118       fputc (',', asm_out_file);
5119       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5120       break;
5121
5122     case DW_OP_bregx:
5123       fputc (',', asm_out_file);
5124       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5125       fputc (',', asm_out_file);
5126       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5127       break;
5128
5129     default:
5130       /* Other codes have no operands.  */
5131       break;
5132     }
5133 }
5134
5135 static void
5136 output_loc_sequence_raw (dw_loc_descr_ref loc)
5137 {
5138   while (1)
5139     {
5140       /* Output the opcode.  */
5141       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5142       output_loc_operands_raw (loc);
5143
5144       if (!loc->dw_loc_next)
5145         break;
5146       loc = loc->dw_loc_next;
5147
5148       fputc (',', asm_out_file);
5149     }
5150 }
5151
5152 /* This routine will generate the correct assembly data for a location
5153    description based on a cfi entry with a complex address.  */
5154
5155 static void
5156 output_cfa_loc (dw_cfi_ref cfi)
5157 {
5158   dw_loc_descr_ref loc;
5159   unsigned long size;
5160
5161   if (cfi->dw_cfi_opc == DW_CFA_expression)
5162     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5163
5164   /* Output the size of the block.  */
5165   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5166   size = size_of_locs (loc);
5167   dw2_asm_output_data_uleb128 (size, NULL);
5168
5169   /* Now output the operations themselves.  */
5170   output_loc_sequence (loc);
5171 }
5172
5173 /* Similar, but used for .cfi_escape.  */
5174
5175 static void
5176 output_cfa_loc_raw (dw_cfi_ref cfi)
5177 {
5178   dw_loc_descr_ref loc;
5179   unsigned long size;
5180
5181   if (cfi->dw_cfi_opc == DW_CFA_expression)
5182     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5183
5184   /* Output the size of the block.  */
5185   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5186   size = size_of_locs (loc);
5187   dw2_asm_output_data_uleb128_raw (size);
5188   fputc (',', asm_out_file);
5189
5190   /* Now output the operations themselves.  */
5191   output_loc_sequence_raw (loc);
5192 }
5193
5194 /* This function builds a dwarf location descriptor sequence from a
5195    dw_cfa_location, adding the given OFFSET to the result of the
5196    expression.  */
5197
5198 static struct dw_loc_descr_struct *
5199 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5200 {
5201   struct dw_loc_descr_struct *head, *tmp;
5202
5203   offset += cfa->offset;
5204
5205   if (cfa->indirect)
5206     {
5207       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5208       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5209       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5210       add_loc_descr (&head, tmp);
5211       if (offset != 0)
5212         {
5213           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5214           add_loc_descr (&head, tmp);
5215         }
5216     }
5217   else
5218     head = new_reg_loc_descr (cfa->reg, offset);
5219
5220   return head;
5221 }
5222
5223 /* This function builds a dwarf location descriptor sequence for
5224    the address at OFFSET from the CFA when stack is aligned to
5225    ALIGNMENT byte.  */
5226
5227 static struct dw_loc_descr_struct *
5228 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5229 {
5230   struct dw_loc_descr_struct *head;
5231   unsigned int dwarf_fp
5232     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5233
5234  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5235   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5236     {
5237       head = new_reg_loc_descr (dwarf_fp, 0);
5238       add_loc_descr (&head, int_loc_descriptor (alignment));
5239       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5240       loc_descr_plus_const (&head, offset);
5241     }
5242   else
5243     head = new_reg_loc_descr (dwarf_fp, offset);
5244   return head;
5245 }
5246
5247 /* This function fills in aa dw_cfa_location structure from a dwarf location
5248    descriptor sequence.  */
5249
5250 static void
5251 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5252 {
5253   struct dw_loc_descr_struct *ptr;
5254   cfa->offset = 0;
5255   cfa->base_offset = 0;
5256   cfa->indirect = 0;
5257   cfa->reg = -1;
5258
5259   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5260     {
5261       enum dwarf_location_atom op = ptr->dw_loc_opc;
5262
5263       switch (op)
5264         {
5265         case DW_OP_reg0:
5266         case DW_OP_reg1:
5267         case DW_OP_reg2:
5268         case DW_OP_reg3:
5269         case DW_OP_reg4:
5270         case DW_OP_reg5:
5271         case DW_OP_reg6:
5272         case DW_OP_reg7:
5273         case DW_OP_reg8:
5274         case DW_OP_reg9:
5275         case DW_OP_reg10:
5276         case DW_OP_reg11:
5277         case DW_OP_reg12:
5278         case DW_OP_reg13:
5279         case DW_OP_reg14:
5280         case DW_OP_reg15:
5281         case DW_OP_reg16:
5282         case DW_OP_reg17:
5283         case DW_OP_reg18:
5284         case DW_OP_reg19:
5285         case DW_OP_reg20:
5286         case DW_OP_reg21:
5287         case DW_OP_reg22:
5288         case DW_OP_reg23:
5289         case DW_OP_reg24:
5290         case DW_OP_reg25:
5291         case DW_OP_reg26:
5292         case DW_OP_reg27:
5293         case DW_OP_reg28:
5294         case DW_OP_reg29:
5295         case DW_OP_reg30:
5296         case DW_OP_reg31:
5297           cfa->reg = op - DW_OP_reg0;
5298           break;
5299         case DW_OP_regx:
5300           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5301           break;
5302         case DW_OP_breg0:
5303         case DW_OP_breg1:
5304         case DW_OP_breg2:
5305         case DW_OP_breg3:
5306         case DW_OP_breg4:
5307         case DW_OP_breg5:
5308         case DW_OP_breg6:
5309         case DW_OP_breg7:
5310         case DW_OP_breg8:
5311         case DW_OP_breg9:
5312         case DW_OP_breg10:
5313         case DW_OP_breg11:
5314         case DW_OP_breg12:
5315         case DW_OP_breg13:
5316         case DW_OP_breg14:
5317         case DW_OP_breg15:
5318         case DW_OP_breg16:
5319         case DW_OP_breg17:
5320         case DW_OP_breg18:
5321         case DW_OP_breg19:
5322         case DW_OP_breg20:
5323         case DW_OP_breg21:
5324         case DW_OP_breg22:
5325         case DW_OP_breg23:
5326         case DW_OP_breg24:
5327         case DW_OP_breg25:
5328         case DW_OP_breg26:
5329         case DW_OP_breg27:
5330         case DW_OP_breg28:
5331         case DW_OP_breg29:
5332         case DW_OP_breg30:
5333         case DW_OP_breg31:
5334           cfa->reg = op - DW_OP_breg0;
5335           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5336           break;
5337         case DW_OP_bregx:
5338           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5339           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5340           break;
5341         case DW_OP_deref:
5342           cfa->indirect = 1;
5343           break;
5344         case DW_OP_plus_uconst:
5345           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5346           break;
5347         default:
5348           internal_error ("DW_LOC_OP %s not implemented",
5349                           dwarf_stack_op_name (ptr->dw_loc_opc));
5350         }
5351     }
5352 }
5353 #endif /* .debug_frame support */
5354 \f
5355 /* And now, the support for symbolic debugging information.  */
5356 #ifdef DWARF2_DEBUGGING_INFO
5357
5358 /* .debug_str support.  */
5359 static int output_indirect_string (void **, void *);
5360
5361 static void dwarf2out_init (const char *);
5362 static void dwarf2out_finish (const char *);
5363 static void dwarf2out_define (unsigned int, const char *);
5364 static void dwarf2out_undef (unsigned int, const char *);
5365 static void dwarf2out_start_source_file (unsigned, const char *);
5366 static void dwarf2out_end_source_file (unsigned);
5367 static void dwarf2out_begin_block (unsigned, unsigned);
5368 static void dwarf2out_end_block (unsigned, unsigned);
5369 static bool dwarf2out_ignore_block (const_tree);
5370 static void dwarf2out_global_decl (tree);
5371 static void dwarf2out_type_decl (tree, int);
5372 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5373 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5374                                                  dw_die_ref);
5375 static void dwarf2out_abstract_function (tree);
5376 static void dwarf2out_var_location (rtx);
5377 static void dwarf2out_begin_function (tree);
5378 static void dwarf2out_set_name (tree, tree);
5379
5380 /* The debug hooks structure.  */
5381
5382 const struct gcc_debug_hooks dwarf2_debug_hooks =
5383 {
5384   dwarf2out_init,
5385   dwarf2out_finish,
5386   dwarf2out_define,
5387   dwarf2out_undef,
5388   dwarf2out_start_source_file,
5389   dwarf2out_end_source_file,
5390   dwarf2out_begin_block,
5391   dwarf2out_end_block,
5392   dwarf2out_ignore_block,
5393   dwarf2out_source_line,
5394   dwarf2out_begin_prologue,
5395   debug_nothing_int_charstar,   /* end_prologue */
5396   dwarf2out_end_epilogue,
5397   dwarf2out_begin_function,
5398   debug_nothing_int,            /* end_function */
5399   dwarf2out_decl,               /* function_decl */
5400   dwarf2out_global_decl,
5401   dwarf2out_type_decl,          /* type_decl */
5402   dwarf2out_imported_module_or_decl,
5403   debug_nothing_tree,           /* deferred_inline_function */
5404   /* The DWARF 2 backend tries to reduce debugging bloat by not
5405      emitting the abstract description of inline functions until
5406      something tries to reference them.  */
5407   dwarf2out_abstract_function,  /* outlining_inline_function */
5408   debug_nothing_rtx,            /* label */
5409   debug_nothing_int,            /* handle_pch */
5410   dwarf2out_var_location,
5411   dwarf2out_switch_text_section,
5412   dwarf2out_set_name,
5413   1                             /* start_end_main_source_file */
5414 };
5415 #endif
5416 \f
5417 /* NOTE: In the comments in this file, many references are made to
5418    "Debugging Information Entries".  This term is abbreviated as `DIE'
5419    throughout the remainder of this file.  */
5420
5421 /* An internal representation of the DWARF output is built, and then
5422    walked to generate the DWARF debugging info.  The walk of the internal
5423    representation is done after the entire program has been compiled.
5424    The types below are used to describe the internal representation.  */
5425
5426 /* Various DIE's use offsets relative to the beginning of the
5427    .debug_info section to refer to each other.  */
5428
5429 typedef long int dw_offset;
5430
5431 /* Define typedefs here to avoid circular dependencies.  */
5432
5433 typedef struct dw_attr_struct *dw_attr_ref;
5434 typedef struct dw_line_info_struct *dw_line_info_ref;
5435 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5436 typedef struct pubname_struct *pubname_ref;
5437 typedef struct dw_ranges_struct *dw_ranges_ref;
5438 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5439
5440 /* Each entry in the line_info_table maintains the file and
5441    line number associated with the label generated for that
5442    entry.  The label gives the PC value associated with
5443    the line number entry.  */
5444
5445 typedef struct GTY(()) dw_line_info_struct {
5446   unsigned long dw_file_num;
5447   unsigned long dw_line_num;
5448 }
5449 dw_line_info_entry;
5450
5451 /* Line information for functions in separate sections; each one gets its
5452    own sequence.  */
5453 typedef struct GTY(()) dw_separate_line_info_struct {
5454   unsigned long dw_file_num;
5455   unsigned long dw_line_num;
5456   unsigned long function;
5457 }
5458 dw_separate_line_info_entry;
5459
5460 /* Each DIE attribute has a field specifying the attribute kind,
5461    a link to the next attribute in the chain, and an attribute value.
5462    Attributes are typically linked below the DIE they modify.  */
5463
5464 typedef struct GTY(()) dw_attr_struct {
5465   enum dwarf_attribute dw_attr;
5466   dw_val_node dw_attr_val;
5467 }
5468 dw_attr_node;
5469
5470 DEF_VEC_O(dw_attr_node);
5471 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5472
5473 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5474    The children of each node form a circular list linked by
5475    die_sib.  die_child points to the node *before* the "first" child node.  */
5476
5477 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5478   enum dwarf_tag die_tag;
5479   char *die_symbol;
5480   VEC(dw_attr_node,gc) * die_attr;
5481   dw_die_ref die_parent;
5482   dw_die_ref die_child;
5483   dw_die_ref die_sib;
5484   dw_die_ref die_definition; /* ref from a specification to its definition */
5485   dw_offset die_offset;
5486   unsigned long die_abbrev;
5487   int die_mark;
5488   /* Die is used and must not be pruned as unused.  */
5489   int die_perennial_p;
5490   unsigned int decl_id;
5491 }
5492 die_node;
5493
5494 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5495 #define FOR_EACH_CHILD(die, c, expr) do {       \
5496   c = die->die_child;                           \
5497   if (c) do {                                   \
5498     c = c->die_sib;                             \
5499     expr;                                       \
5500   } while (c != die->die_child);                \
5501 } while (0)
5502
5503 /* The pubname structure */
5504
5505 typedef struct GTY(()) pubname_struct {
5506   dw_die_ref die;
5507   const char *name;
5508 }
5509 pubname_entry;
5510
5511 DEF_VEC_O(pubname_entry);
5512 DEF_VEC_ALLOC_O(pubname_entry, gc);
5513
5514 struct GTY(()) dw_ranges_struct {
5515   /* If this is positive, it's a block number, otherwise it's a
5516      bitwise-negated index into dw_ranges_by_label.  */
5517   int num;
5518 };
5519
5520 struct GTY(()) dw_ranges_by_label_struct {
5521   const char *begin;
5522   const char *end;
5523 };
5524
5525 /* The limbo die list structure.  */
5526 typedef struct GTY(()) limbo_die_struct {
5527   dw_die_ref die;
5528   tree created_for;
5529   struct limbo_die_struct *next;
5530 }
5531 limbo_die_node;
5532
5533 /* How to start an assembler comment.  */
5534 #ifndef ASM_COMMENT_START
5535 #define ASM_COMMENT_START ";#"
5536 #endif
5537
5538 /* Define a macro which returns nonzero for a TYPE_DECL which was
5539    implicitly generated for a tagged type.
5540
5541    Note that unlike the gcc front end (which generates a NULL named
5542    TYPE_DECL node for each complete tagged type, each array type, and
5543    each function type node created) the g++ front end generates a
5544    _named_ TYPE_DECL node for each tagged type node created.
5545    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5546    generate a DW_TAG_typedef DIE for them.  */
5547
5548 #define TYPE_DECL_IS_STUB(decl)                         \
5549   (DECL_NAME (decl) == NULL_TREE                        \
5550    || (DECL_ARTIFICIAL (decl)                           \
5551        && is_tagged_type (TREE_TYPE (decl))             \
5552        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5553            /* This is necessary for stub decls that     \
5554               appear in nested inline functions.  */    \
5555            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5556                && (decl_ultimate_origin (decl)          \
5557                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5558
5559 /* Information concerning the compilation unit's programming
5560    language, and compiler version.  */
5561
5562 /* Fixed size portion of the DWARF compilation unit header.  */
5563 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5564   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5565
5566 /* Fixed size portion of public names info.  */
5567 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5568
5569 /* Fixed size portion of the address range info.  */
5570 #define DWARF_ARANGES_HEADER_SIZE                                       \
5571   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5572                 DWARF2_ADDR_SIZE * 2)                                   \
5573    - DWARF_INITIAL_LENGTH_SIZE)
5574
5575 /* Size of padding portion in the address range info.  It must be
5576    aligned to twice the pointer size.  */
5577 #define DWARF_ARANGES_PAD_SIZE \
5578   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5579                 DWARF2_ADDR_SIZE * 2)                              \
5580    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5581
5582 /* Use assembler line directives if available.  */
5583 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5584 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5585 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5586 #else
5587 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5588 #endif
5589 #endif
5590
5591 /* Minimum line offset in a special line info. opcode.
5592    This value was chosen to give a reasonable range of values.  */
5593 #define DWARF_LINE_BASE  -10
5594
5595 /* First special line opcode - leave room for the standard opcodes.  */
5596 #define DWARF_LINE_OPCODE_BASE  10
5597
5598 /* Range of line offsets in a special line info. opcode.  */
5599 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5600
5601 /* Flag that indicates the initial value of the is_stmt_start flag.
5602    In the present implementation, we do not mark any lines as
5603    the beginning of a source statement, because that information
5604    is not made available by the GCC front-end.  */
5605 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5606
5607 #ifdef DWARF2_DEBUGGING_INFO
5608 /* This location is used by calc_die_sizes() to keep track
5609    the offset of each DIE within the .debug_info section.  */
5610 static unsigned long next_die_offset;
5611 #endif
5612
5613 /* Record the root of the DIE's built for the current compilation unit.  */
5614 static GTY(()) dw_die_ref comp_unit_die;
5615
5616 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5617 static GTY(()) limbo_die_node *limbo_die_list;
5618
5619 /* A list of DIEs for which we may have to generate
5620    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5621    set.  */
5622 static GTY(()) limbo_die_node *deferred_asm_name;
5623
5624 /* Filenames referenced by this compilation unit.  */
5625 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5626
5627 /* A hash table of references to DIE's that describe declarations.
5628    The key is a DECL_UID() which is a unique number identifying each decl.  */
5629 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5630
5631 /* A hash table of references to DIE's that describe COMMON blocks.
5632    The key is DECL_UID() ^ die_parent.  */
5633 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5634
5635 typedef struct GTY(()) die_arg_entry_struct {
5636     dw_die_ref die;
5637     tree arg;
5638 } die_arg_entry;
5639
5640 DEF_VEC_O(die_arg_entry);
5641 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5642
5643 /* Node of the variable location list.  */
5644 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5645   rtx GTY (()) var_loc_note;
5646   const char * GTY (()) label;
5647   const char * GTY (()) section_label;
5648   struct var_loc_node * GTY (()) next;
5649 };
5650
5651 /* Variable location list.  */
5652 struct GTY (()) var_loc_list_def {
5653   struct var_loc_node * GTY (()) first;
5654
5655   /* Do not mark the last element of the chained list because
5656      it is marked through the chain.  */
5657   struct var_loc_node * GTY ((skip ("%h"))) last;
5658
5659   /* DECL_UID of the variable decl.  */
5660   unsigned int decl_id;
5661 };
5662 typedef struct var_loc_list_def var_loc_list;
5663
5664
5665 /* Table of decl location linked lists.  */
5666 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5667
5668 /* A pointer to the base of a list of references to DIE's that
5669    are uniquely identified by their tag, presence/absence of
5670    children DIE's, and list of attribute/value pairs.  */
5671 static GTY((length ("abbrev_die_table_allocated")))
5672   dw_die_ref *abbrev_die_table;
5673
5674 /* Number of elements currently allocated for abbrev_die_table.  */
5675 static GTY(()) unsigned abbrev_die_table_allocated;
5676
5677 /* Number of elements in type_die_table currently in use.  */
5678 static GTY(()) unsigned abbrev_die_table_in_use;
5679
5680 /* Size (in elements) of increments by which we may expand the
5681    abbrev_die_table.  */
5682 #define ABBREV_DIE_TABLE_INCREMENT 256
5683
5684 /* A pointer to the base of a table that contains line information
5685    for each source code line in .text in the compilation unit.  */
5686 static GTY((length ("line_info_table_allocated")))
5687      dw_line_info_ref line_info_table;
5688
5689 /* Number of elements currently allocated for line_info_table.  */
5690 static GTY(()) unsigned line_info_table_allocated;
5691
5692 /* Number of elements in line_info_table currently in use.  */
5693 static GTY(()) unsigned line_info_table_in_use;
5694
5695 /* A pointer to the base of a table that contains line information
5696    for each source code line outside of .text in the compilation unit.  */
5697 static GTY ((length ("separate_line_info_table_allocated")))
5698      dw_separate_line_info_ref separate_line_info_table;
5699
5700 /* Number of elements currently allocated for separate_line_info_table.  */
5701 static GTY(()) unsigned separate_line_info_table_allocated;
5702
5703 /* Number of elements in separate_line_info_table currently in use.  */
5704 static GTY(()) unsigned separate_line_info_table_in_use;
5705
5706 /* Size (in elements) of increments by which we may expand the
5707    line_info_table.  */
5708 #define LINE_INFO_TABLE_INCREMENT 1024
5709
5710 /* A pointer to the base of a table that contains a list of publicly
5711    accessible names.  */
5712 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5713
5714 /* A pointer to the base of a table that contains a list of publicly
5715    accessible types.  */
5716 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5717
5718 /* Array of dies for which we should generate .debug_arange info.  */
5719 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5720
5721 /* Number of elements currently allocated for arange_table.  */
5722 static GTY(()) unsigned arange_table_allocated;
5723
5724 /* Number of elements in arange_table currently in use.  */
5725 static GTY(()) unsigned arange_table_in_use;
5726
5727 /* Size (in elements) of increments by which we may expand the
5728    arange_table.  */
5729 #define ARANGE_TABLE_INCREMENT 64
5730
5731 /* Array of dies for which we should generate .debug_ranges info.  */
5732 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5733
5734 /* Number of elements currently allocated for ranges_table.  */
5735 static GTY(()) unsigned ranges_table_allocated;
5736
5737 /* Number of elements in ranges_table currently in use.  */
5738 static GTY(()) unsigned ranges_table_in_use;
5739
5740 /* Array of pairs of labels referenced in ranges_table.  */
5741 static GTY ((length ("ranges_by_label_allocated")))
5742      dw_ranges_by_label_ref ranges_by_label;
5743
5744 /* Number of elements currently allocated for ranges_by_label.  */
5745 static GTY(()) unsigned ranges_by_label_allocated;
5746
5747 /* Number of elements in ranges_by_label currently in use.  */
5748 static GTY(()) unsigned ranges_by_label_in_use;
5749
5750 /* Size (in elements) of increments by which we may expand the
5751    ranges_table.  */
5752 #define RANGES_TABLE_INCREMENT 64
5753
5754 /* Whether we have location lists that need outputting */
5755 static GTY(()) bool have_location_lists;
5756
5757 /* Unique label counter.  */
5758 static GTY(()) unsigned int loclabel_num;
5759
5760 #ifdef DWARF2_DEBUGGING_INFO
5761 /* Record whether the function being analyzed contains inlined functions.  */
5762 static int current_function_has_inlines;
5763 #endif
5764 #if 0 && defined (MIPS_DEBUGGING_INFO)
5765 static int comp_unit_has_inlines;
5766 #endif
5767
5768 /* The last file entry emitted by maybe_emit_file().  */
5769 static GTY(()) struct dwarf_file_data * last_emitted_file;
5770
5771 /* Number of internal labels generated by gen_internal_sym().  */
5772 static GTY(()) int label_num;
5773
5774 /* Cached result of previous call to lookup_filename.  */
5775 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5776
5777 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5778
5779 #ifdef DWARF2_DEBUGGING_INFO
5780
5781 /* Offset from the "steady-state frame pointer" to the frame base,
5782    within the current function.  */
5783 static HOST_WIDE_INT frame_pointer_fb_offset;
5784
5785 /* Forward declarations for functions defined in this file.  */
5786
5787 static int is_pseudo_reg (const_rtx);
5788 static tree type_main_variant (tree);
5789 static int is_tagged_type (const_tree);
5790 static const char *dwarf_tag_name (unsigned);
5791 static const char *dwarf_attr_name (unsigned);
5792 static const char *dwarf_form_name (unsigned);
5793 static tree decl_ultimate_origin (const_tree);
5794 static tree decl_class_context (tree);
5795 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5796 static inline enum dw_val_class AT_class (dw_attr_ref);
5797 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5798 static inline unsigned AT_flag (dw_attr_ref);
5799 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5800 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5801 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5802 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5803 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, rtx);
5804 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5805                                unsigned int, unsigned char *);
5806 static hashval_t debug_str_do_hash (const void *);
5807 static int debug_str_eq (const void *, const void *);
5808 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5809 static inline const char *AT_string (dw_attr_ref);
5810 static enum dwarf_form AT_string_form (dw_attr_ref);
5811 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5812 static void add_AT_specification (dw_die_ref, dw_die_ref);
5813 static inline dw_die_ref AT_ref (dw_attr_ref);
5814 static inline int AT_ref_external (dw_attr_ref);
5815 static inline void set_AT_ref_external (dw_attr_ref, int);
5816 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5817 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5818 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5819 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5820                              dw_loc_list_ref);
5821 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5822 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5823 static inline rtx AT_addr (dw_attr_ref);
5824 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5825 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5826 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5827 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5828                            unsigned HOST_WIDE_INT);
5829 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5830                                unsigned long);
5831 static inline const char *AT_lbl (dw_attr_ref);
5832 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5833 static const char *get_AT_low_pc (dw_die_ref);
5834 static const char *get_AT_hi_pc (dw_die_ref);
5835 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5836 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5837 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5838 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5839 static bool is_c_family (void);
5840 static bool is_cxx (void);
5841 static bool is_java (void);
5842 static bool is_fortran (void);
5843 static bool is_ada (void);
5844 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5845 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5846 static void add_child_die (dw_die_ref, dw_die_ref);
5847 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5848 static dw_die_ref lookup_type_die (tree);
5849 static void equate_type_number_to_die (tree, dw_die_ref);
5850 static hashval_t decl_die_table_hash (const void *);
5851 static int decl_die_table_eq (const void *, const void *);
5852 static dw_die_ref lookup_decl_die (tree);
5853 static hashval_t common_block_die_table_hash (const void *);
5854 static int common_block_die_table_eq (const void *, const void *);
5855 static hashval_t decl_loc_table_hash (const void *);
5856 static int decl_loc_table_eq (const void *, const void *);
5857 static var_loc_list *lookup_decl_loc (const_tree);
5858 static void equate_decl_number_to_die (tree, dw_die_ref);
5859 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5860 static void print_spaces (FILE *);
5861 static void print_die (dw_die_ref, FILE *);
5862 static void print_dwarf_line_table (FILE *);
5863 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5864 static dw_die_ref pop_compile_unit (dw_die_ref);
5865 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5866 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5867 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5868 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5869 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5870 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5871 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5872 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5873 static void compute_section_prefix (dw_die_ref);
5874 static int is_type_die (dw_die_ref);
5875 static int is_comdat_die (dw_die_ref);
5876 static int is_symbol_die (dw_die_ref);
5877 static void assign_symbol_names (dw_die_ref);
5878 static void break_out_includes (dw_die_ref);
5879 static hashval_t htab_cu_hash (const void *);
5880 static int htab_cu_eq (const void *, const void *);
5881 static void htab_cu_del (void *);
5882 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5883 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5884 static void add_sibling_attributes (dw_die_ref);
5885 static void build_abbrev_table (dw_die_ref);
5886 static void output_location_lists (dw_die_ref);
5887 static int constant_size (unsigned HOST_WIDE_INT);
5888 static unsigned long size_of_die (dw_die_ref);
5889 static void calc_die_sizes (dw_die_ref);
5890 static void mark_dies (dw_die_ref);
5891 static void unmark_dies (dw_die_ref);
5892 static void unmark_all_dies (dw_die_ref);
5893 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5894 static unsigned long size_of_aranges (void);
5895 static enum dwarf_form value_format (dw_attr_ref);
5896 static void output_value_format (dw_attr_ref);
5897 static void output_abbrev_section (void);
5898 static void output_die_symbol (dw_die_ref);
5899 static void output_die (dw_die_ref);
5900 static void output_compilation_unit_header (void);
5901 static void output_comp_unit (dw_die_ref, int);
5902 static const char *dwarf2_name (tree, int);
5903 static void add_pubname (tree, dw_die_ref);
5904 static void add_pubname_string (const char *, dw_die_ref);
5905 static void add_pubtype (tree, dw_die_ref);
5906 static void output_pubnames (VEC (pubname_entry,gc) *);
5907 static void add_arange (tree, dw_die_ref);
5908 static void output_aranges (void);
5909 static unsigned int add_ranges_num (int);
5910 static unsigned int add_ranges (const_tree);
5911 static unsigned int add_ranges_by_labels (const char *, const char *);
5912 static void output_ranges (void);
5913 static void output_line_info (void);
5914 static void output_file_names (void);
5915 static dw_die_ref base_type_die (tree);
5916 static int is_base_type (tree);
5917 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5918 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5919 static dw_die_ref generic_parameter_die (tree, tree, dw_die_ref, int);
5920 static int type_is_enum (const_tree);
5921 static unsigned int dbx_reg_number (const_rtx);
5922 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5923 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5924 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5925                                                 enum var_init_status);
5926 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5927                                                      enum var_init_status);
5928 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5929                                          enum var_init_status);
5930 static int is_based_loc (const_rtx);
5931 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5932                                             enum var_init_status);
5933 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5934                                                enum var_init_status);
5935 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
5936                                         enum var_init_status);
5937 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5938 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5939 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5940 static tree field_type (const_tree);
5941 static unsigned int simple_type_align_in_bits (const_tree);
5942 static unsigned int simple_decl_align_in_bits (const_tree);
5943 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5944 static HOST_WIDE_INT field_byte_offset (const_tree);
5945 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5946                                          dw_loc_descr_ref);
5947 static void add_data_member_location_attribute (dw_die_ref, tree);
5948 static void add_const_value_attribute (dw_die_ref, rtx);
5949 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5950 static void insert_float (const_rtx, unsigned char *);
5951 static rtx rtl_for_decl_location (tree);
5952 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5953                                                    enum dwarf_attribute);
5954 static void tree_add_const_value_attribute (dw_die_ref, tree);
5955 static void tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
5956 static void add_name_attribute (dw_die_ref, const char *);
5957 static void add_comp_dir_attribute (dw_die_ref);
5958 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5959 static void add_subscript_info (dw_die_ref, tree, bool);
5960 static void add_byte_size_attribute (dw_die_ref, tree);
5961 static void add_bit_offset_attribute (dw_die_ref, tree);
5962 static void add_bit_size_attribute (dw_die_ref, tree);
5963 static void add_prototyped_attribute (dw_die_ref, tree);
5964 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5965 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5966 static void add_src_coords_attributes (dw_die_ref, tree);
5967 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5968 static void push_decl_scope (tree);
5969 static void pop_decl_scope (void);
5970 static dw_die_ref scope_die_for (tree, dw_die_ref);
5971 static inline int local_scope_p (dw_die_ref);
5972 static inline int class_scope_p (dw_die_ref);
5973 static inline int class_or_namespace_scope_p (dw_die_ref);
5974 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5975 static void add_calling_convention_attribute (dw_die_ref, tree);
5976 static const char *type_tag (const_tree);
5977 static tree member_declared_type (const_tree);
5978 #if 0
5979 static const char *decl_start_label (tree);
5980 #endif
5981 static void gen_array_type_die (tree, dw_die_ref);
5982 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5983 #if 0
5984 static void gen_entry_point_die (tree, dw_die_ref);
5985 #endif
5986 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5987 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5988 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5989 static void gen_formal_types_die (tree, dw_die_ref);
5990 static void gen_subprogram_die (tree, dw_die_ref);
5991 static void gen_variable_die (tree, tree, dw_die_ref);
5992 static void gen_const_die (tree, dw_die_ref);
5993 static void gen_label_die (tree, dw_die_ref);
5994 static void gen_lexical_block_die (tree, dw_die_ref, int);
5995 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5996 static void gen_field_die (tree, dw_die_ref);
5997 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5998 static dw_die_ref gen_compile_unit_die (const char *);
5999 static void gen_inheritance_die (tree, tree, dw_die_ref);
6000 static void gen_member_die (tree, dw_die_ref);
6001 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6002                                                 enum debug_info_usage);
6003 static void gen_subroutine_type_die (tree, dw_die_ref);
6004 static void gen_typedef_die (tree, dw_die_ref);
6005 static void gen_type_die (tree, dw_die_ref);
6006 static void gen_block_die (tree, dw_die_ref, int);
6007 static void decls_for_scope (tree, dw_die_ref, int);
6008 static int is_redundant_typedef (const_tree);
6009 static inline dw_die_ref get_context_die (tree);
6010 static void gen_namespace_die (tree, dw_die_ref);
6011 static void gen_decl_die (tree, tree, dw_die_ref);
6012 static dw_die_ref force_decl_die (tree);
6013 static dw_die_ref force_type_die (tree);
6014 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6015 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6016 static struct dwarf_file_data * lookup_filename (const char *);
6017 static void retry_incomplete_types (void);
6018 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6019 static tree make_ith_pack_parameter_name (tree, int);
6020 static void gen_generic_params_dies (tree);
6021 static void splice_child_die (dw_die_ref, dw_die_ref);
6022 static int file_info_cmp (const void *, const void *);
6023 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6024                                      const char *, const char *, unsigned);
6025 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
6026                                        const char *, const char *,
6027                                        const char *);
6028 static void output_loc_list (dw_loc_list_ref);
6029 static char *gen_internal_sym (const char *);
6030
6031 static void prune_unmark_dies (dw_die_ref);
6032 static void prune_unused_types_mark (dw_die_ref, int);
6033 static void prune_unused_types_walk (dw_die_ref);
6034 static void prune_unused_types_walk_attribs (dw_die_ref);
6035 static void prune_unused_types_prune (dw_die_ref);
6036 static void prune_unused_types (void);
6037 static int maybe_emit_file (struct dwarf_file_data *fd);
6038 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6039 static void gen_remaining_tmpl_value_param_die_attribute (void);
6040
6041 /* Section names used to hold DWARF debugging information.  */
6042 #ifndef DEBUG_INFO_SECTION
6043 #define DEBUG_INFO_SECTION      ".debug_info"
6044 #endif
6045 #ifndef DEBUG_ABBREV_SECTION
6046 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6047 #endif
6048 #ifndef DEBUG_ARANGES_SECTION
6049 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6050 #endif
6051 #ifndef DEBUG_MACINFO_SECTION
6052 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6053 #endif
6054 #ifndef DEBUG_LINE_SECTION
6055 #define DEBUG_LINE_SECTION      ".debug_line"
6056 #endif
6057 #ifndef DEBUG_LOC_SECTION
6058 #define DEBUG_LOC_SECTION       ".debug_loc"
6059 #endif
6060 #ifndef DEBUG_PUBNAMES_SECTION
6061 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6062 #endif
6063 #ifndef DEBUG_PUBTYPES_SECTION
6064 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6065 #endif
6066 #ifndef DEBUG_STR_SECTION
6067 #define DEBUG_STR_SECTION       ".debug_str"
6068 #endif
6069 #ifndef DEBUG_RANGES_SECTION
6070 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6071 #endif
6072
6073 /* Standard ELF section names for compiled code and data.  */
6074 #ifndef TEXT_SECTION_NAME
6075 #define TEXT_SECTION_NAME       ".text"
6076 #endif
6077
6078 /* Section flags for .debug_str section.  */
6079 #define DEBUG_STR_SECTION_FLAGS \
6080   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6081    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6082    : SECTION_DEBUG)
6083
6084 /* Labels we insert at beginning sections we can reference instead of
6085    the section names themselves.  */
6086
6087 #ifndef TEXT_SECTION_LABEL
6088 #define TEXT_SECTION_LABEL              "Ltext"
6089 #endif
6090 #ifndef COLD_TEXT_SECTION_LABEL
6091 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6092 #endif
6093 #ifndef DEBUG_LINE_SECTION_LABEL
6094 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6095 #endif
6096 #ifndef DEBUG_INFO_SECTION_LABEL
6097 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6098 #endif
6099 #ifndef DEBUG_ABBREV_SECTION_LABEL
6100 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6101 #endif
6102 #ifndef DEBUG_LOC_SECTION_LABEL
6103 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6104 #endif
6105 #ifndef DEBUG_RANGES_SECTION_LABEL
6106 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6107 #endif
6108 #ifndef DEBUG_MACINFO_SECTION_LABEL
6109 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6110 #endif
6111
6112 /* Definitions of defaults for formats and names of various special
6113    (artificial) labels which may be generated within this file (when the -g
6114    options is used and DWARF2_DEBUGGING_INFO is in effect.
6115    If necessary, these may be overridden from within the tm.h file, but
6116    typically, overriding these defaults is unnecessary.  */
6117
6118 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6119 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6120 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6121 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6122 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6123 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6124 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6125 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6126 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6127 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6128
6129 #ifndef TEXT_END_LABEL
6130 #define TEXT_END_LABEL          "Letext"
6131 #endif
6132 #ifndef COLD_END_LABEL
6133 #define COLD_END_LABEL          "Letext_cold"
6134 #endif
6135 #ifndef BLOCK_BEGIN_LABEL
6136 #define BLOCK_BEGIN_LABEL       "LBB"
6137 #endif
6138 #ifndef BLOCK_END_LABEL
6139 #define BLOCK_END_LABEL         "LBE"
6140 #endif
6141 #ifndef LINE_CODE_LABEL
6142 #define LINE_CODE_LABEL         "LM"
6143 #endif
6144 #ifndef SEPARATE_LINE_CODE_LABEL
6145 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6146 #endif
6147
6148 \f
6149 /* We allow a language front-end to designate a function that is to be
6150    called to "demangle" any name before it is put into a DIE.  */
6151
6152 static const char *(*demangle_name_func) (const char *);
6153
6154 void
6155 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6156 {
6157   demangle_name_func = func;
6158 }
6159
6160 /* Test if rtl node points to a pseudo register.  */
6161
6162 static inline int
6163 is_pseudo_reg (const_rtx rtl)
6164 {
6165   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6166           || (GET_CODE (rtl) == SUBREG
6167               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6168 }
6169
6170 /* Return a reference to a type, with its const and volatile qualifiers
6171    removed.  */
6172
6173 static inline tree
6174 type_main_variant (tree type)
6175 {
6176   type = TYPE_MAIN_VARIANT (type);
6177
6178   /* ??? There really should be only one main variant among any group of
6179      variants of a given type (and all of the MAIN_VARIANT values for all
6180      members of the group should point to that one type) but sometimes the C
6181      front-end messes this up for array types, so we work around that bug
6182      here.  */
6183   if (TREE_CODE (type) == ARRAY_TYPE)
6184     while (type != TYPE_MAIN_VARIANT (type))
6185       type = TYPE_MAIN_VARIANT (type);
6186
6187   return type;
6188 }
6189
6190 /* Return nonzero if the given type node represents a tagged type.  */
6191
6192 static inline int
6193 is_tagged_type (const_tree type)
6194 {
6195   enum tree_code code = TREE_CODE (type);
6196
6197   return (code == RECORD_TYPE || code == UNION_TYPE
6198           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6199 }
6200
6201 /* Convert a DIE tag into its string name.  */
6202
6203 static const char *
6204 dwarf_tag_name (unsigned int tag)
6205 {
6206   switch (tag)
6207     {
6208     case DW_TAG_padding:
6209       return "DW_TAG_padding";
6210     case DW_TAG_array_type:
6211       return "DW_TAG_array_type";
6212     case DW_TAG_class_type:
6213       return "DW_TAG_class_type";
6214     case DW_TAG_entry_point:
6215       return "DW_TAG_entry_point";
6216     case DW_TAG_enumeration_type:
6217       return "DW_TAG_enumeration_type";
6218     case DW_TAG_formal_parameter:
6219       return "DW_TAG_formal_parameter";
6220     case DW_TAG_imported_declaration:
6221       return "DW_TAG_imported_declaration";
6222     case DW_TAG_label:
6223       return "DW_TAG_label";
6224     case DW_TAG_lexical_block:
6225       return "DW_TAG_lexical_block";
6226     case DW_TAG_member:
6227       return "DW_TAG_member";
6228     case DW_TAG_pointer_type:
6229       return "DW_TAG_pointer_type";
6230     case DW_TAG_reference_type:
6231       return "DW_TAG_reference_type";
6232     case DW_TAG_compile_unit:
6233       return "DW_TAG_compile_unit";
6234     case DW_TAG_string_type:
6235       return "DW_TAG_string_type";
6236     case DW_TAG_structure_type:
6237       return "DW_TAG_structure_type";
6238     case DW_TAG_subroutine_type:
6239       return "DW_TAG_subroutine_type";
6240     case DW_TAG_typedef:
6241       return "DW_TAG_typedef";
6242     case DW_TAG_union_type:
6243       return "DW_TAG_union_type";
6244     case DW_TAG_unspecified_parameters:
6245       return "DW_TAG_unspecified_parameters";
6246     case DW_TAG_variant:
6247       return "DW_TAG_variant";
6248     case DW_TAG_common_block:
6249       return "DW_TAG_common_block";
6250     case DW_TAG_common_inclusion:
6251       return "DW_TAG_common_inclusion";
6252     case DW_TAG_inheritance:
6253       return "DW_TAG_inheritance";
6254     case DW_TAG_inlined_subroutine:
6255       return "DW_TAG_inlined_subroutine";
6256     case DW_TAG_module:
6257       return "DW_TAG_module";
6258     case DW_TAG_ptr_to_member_type:
6259       return "DW_TAG_ptr_to_member_type";
6260     case DW_TAG_set_type:
6261       return "DW_TAG_set_type";
6262     case DW_TAG_subrange_type:
6263       return "DW_TAG_subrange_type";
6264     case DW_TAG_with_stmt:
6265       return "DW_TAG_with_stmt";
6266     case DW_TAG_access_declaration:
6267       return "DW_TAG_access_declaration";
6268     case DW_TAG_base_type:
6269       return "DW_TAG_base_type";
6270     case DW_TAG_catch_block:
6271       return "DW_TAG_catch_block";
6272     case DW_TAG_const_type:
6273       return "DW_TAG_const_type";
6274     case DW_TAG_constant:
6275       return "DW_TAG_constant";
6276     case DW_TAG_enumerator:
6277       return "DW_TAG_enumerator";
6278     case DW_TAG_file_type:
6279       return "DW_TAG_file_type";
6280     case DW_TAG_friend:
6281       return "DW_TAG_friend";
6282     case DW_TAG_namelist:
6283       return "DW_TAG_namelist";
6284     case DW_TAG_namelist_item:
6285       return "DW_TAG_namelist_item";
6286     case DW_TAG_packed_type:
6287       return "DW_TAG_packed_type";
6288     case DW_TAG_subprogram:
6289       return "DW_TAG_subprogram";
6290     case DW_TAG_template_type_param:
6291       return "DW_TAG_template_type_param";
6292     case DW_TAG_template_value_param:
6293       return "DW_TAG_template_value_param";
6294     case DW_TAG_thrown_type:
6295       return "DW_TAG_thrown_type";
6296     case DW_TAG_try_block:
6297       return "DW_TAG_try_block";
6298     case DW_TAG_variant_part:
6299       return "DW_TAG_variant_part";
6300     case DW_TAG_variable:
6301       return "DW_TAG_variable";
6302     case DW_TAG_volatile_type:
6303       return "DW_TAG_volatile_type";
6304     case DW_TAG_dwarf_procedure:
6305       return "DW_TAG_dwarf_procedure";
6306     case DW_TAG_restrict_type:
6307       return "DW_TAG_restrict_type";
6308     case DW_TAG_interface_type:
6309       return "DW_TAG_interface_type";
6310     case DW_TAG_namespace:
6311       return "DW_TAG_namespace";
6312     case DW_TAG_imported_module:
6313       return "DW_TAG_imported_module";
6314     case DW_TAG_unspecified_type:
6315       return "DW_TAG_unspecified_type";
6316     case DW_TAG_partial_unit:
6317       return "DW_TAG_partial_unit";
6318     case DW_TAG_imported_unit:
6319       return "DW_TAG_imported_unit";
6320     case DW_TAG_condition:
6321       return "DW_TAG_condition";
6322     case DW_TAG_shared_type:
6323       return "DW_TAG_shared_type";
6324     case DW_TAG_MIPS_loop:
6325       return "DW_TAG_MIPS_loop";
6326     case DW_TAG_format_label:
6327       return "DW_TAG_format_label";
6328     case DW_TAG_function_template:
6329       return "DW_TAG_function_template";
6330     case DW_TAG_class_template:
6331       return "DW_TAG_class_template";
6332     case DW_TAG_GNU_BINCL:
6333       return "DW_TAG_GNU_BINCL";
6334     case DW_TAG_GNU_EINCL:
6335       return "DW_TAG_GNU_EINCL";
6336     case DW_TAG_GNU_template_template_param:
6337       return "DW_TAG_GNU_template_template_param";
6338     default:
6339       return "DW_TAG_<unknown>";
6340     }
6341 }
6342
6343 /* Convert a DWARF attribute code into its string name.  */
6344
6345 static const char *
6346 dwarf_attr_name (unsigned int attr)
6347 {
6348   switch (attr)
6349     {
6350     case DW_AT_sibling:
6351       return "DW_AT_sibling";
6352     case DW_AT_location:
6353       return "DW_AT_location";
6354     case DW_AT_name:
6355       return "DW_AT_name";
6356     case DW_AT_ordering:
6357       return "DW_AT_ordering";
6358     case DW_AT_subscr_data:
6359       return "DW_AT_subscr_data";
6360     case DW_AT_byte_size:
6361       return "DW_AT_byte_size";
6362     case DW_AT_bit_offset:
6363       return "DW_AT_bit_offset";
6364     case DW_AT_bit_size:
6365       return "DW_AT_bit_size";
6366     case DW_AT_element_list:
6367       return "DW_AT_element_list";
6368     case DW_AT_stmt_list:
6369       return "DW_AT_stmt_list";
6370     case DW_AT_low_pc:
6371       return "DW_AT_low_pc";
6372     case DW_AT_high_pc:
6373       return "DW_AT_high_pc";
6374     case DW_AT_language:
6375       return "DW_AT_language";
6376     case DW_AT_member:
6377       return "DW_AT_member";
6378     case DW_AT_discr:
6379       return "DW_AT_discr";
6380     case DW_AT_discr_value:
6381       return "DW_AT_discr_value";
6382     case DW_AT_visibility:
6383       return "DW_AT_visibility";
6384     case DW_AT_import:
6385       return "DW_AT_import";
6386     case DW_AT_string_length:
6387       return "DW_AT_string_length";
6388     case DW_AT_common_reference:
6389       return "DW_AT_common_reference";
6390     case DW_AT_comp_dir:
6391       return "DW_AT_comp_dir";
6392     case DW_AT_const_value:
6393       return "DW_AT_const_value";
6394     case DW_AT_containing_type:
6395       return "DW_AT_containing_type";
6396     case DW_AT_default_value:
6397       return "DW_AT_default_value";
6398     case DW_AT_inline:
6399       return "DW_AT_inline";
6400     case DW_AT_is_optional:
6401       return "DW_AT_is_optional";
6402     case DW_AT_lower_bound:
6403       return "DW_AT_lower_bound";
6404     case DW_AT_producer:
6405       return "DW_AT_producer";
6406     case DW_AT_prototyped:
6407       return "DW_AT_prototyped";
6408     case DW_AT_return_addr:
6409       return "DW_AT_return_addr";
6410     case DW_AT_start_scope:
6411       return "DW_AT_start_scope";
6412     case DW_AT_bit_stride:
6413       return "DW_AT_bit_stride";
6414     case DW_AT_upper_bound:
6415       return "DW_AT_upper_bound";
6416     case DW_AT_abstract_origin:
6417       return "DW_AT_abstract_origin";
6418     case DW_AT_accessibility:
6419       return "DW_AT_accessibility";
6420     case DW_AT_address_class:
6421       return "DW_AT_address_class";
6422     case DW_AT_artificial:
6423       return "DW_AT_artificial";
6424     case DW_AT_base_types:
6425       return "DW_AT_base_types";
6426     case DW_AT_calling_convention:
6427       return "DW_AT_calling_convention";
6428     case DW_AT_count:
6429       return "DW_AT_count";
6430     case DW_AT_data_member_location:
6431       return "DW_AT_data_member_location";
6432     case DW_AT_decl_column:
6433       return "DW_AT_decl_column";
6434     case DW_AT_decl_file:
6435       return "DW_AT_decl_file";
6436     case DW_AT_decl_line:
6437       return "DW_AT_decl_line";
6438     case DW_AT_declaration:
6439       return "DW_AT_declaration";
6440     case DW_AT_discr_list:
6441       return "DW_AT_discr_list";
6442     case DW_AT_encoding:
6443       return "DW_AT_encoding";
6444     case DW_AT_external:
6445       return "DW_AT_external";
6446     case DW_AT_explicit:
6447       return "DW_AT_explicit";
6448     case DW_AT_frame_base:
6449       return "DW_AT_frame_base";
6450     case DW_AT_friend:
6451       return "DW_AT_friend";
6452     case DW_AT_identifier_case:
6453       return "DW_AT_identifier_case";
6454     case DW_AT_macro_info:
6455       return "DW_AT_macro_info";
6456     case DW_AT_namelist_items:
6457       return "DW_AT_namelist_items";
6458     case DW_AT_priority:
6459       return "DW_AT_priority";
6460     case DW_AT_segment:
6461       return "DW_AT_segment";
6462     case DW_AT_specification:
6463       return "DW_AT_specification";
6464     case DW_AT_static_link:
6465       return "DW_AT_static_link";
6466     case DW_AT_type:
6467       return "DW_AT_type";
6468     case DW_AT_use_location:
6469       return "DW_AT_use_location";
6470     case DW_AT_variable_parameter:
6471       return "DW_AT_variable_parameter";
6472     case DW_AT_virtuality:
6473       return "DW_AT_virtuality";
6474     case DW_AT_vtable_elem_location:
6475       return "DW_AT_vtable_elem_location";
6476
6477     case DW_AT_allocated:
6478       return "DW_AT_allocated";
6479     case DW_AT_associated:
6480       return "DW_AT_associated";
6481     case DW_AT_data_location:
6482       return "DW_AT_data_location";
6483     case DW_AT_byte_stride:
6484       return "DW_AT_byte_stride";
6485     case DW_AT_entry_pc:
6486       return "DW_AT_entry_pc";
6487     case DW_AT_use_UTF8:
6488       return "DW_AT_use_UTF8";
6489     case DW_AT_extension:
6490       return "DW_AT_extension";
6491     case DW_AT_ranges:
6492       return "DW_AT_ranges";
6493     case DW_AT_trampoline:
6494       return "DW_AT_trampoline";
6495     case DW_AT_call_column:
6496       return "DW_AT_call_column";
6497     case DW_AT_call_file:
6498       return "DW_AT_call_file";
6499     case DW_AT_call_line:
6500       return "DW_AT_call_line";
6501
6502     case DW_AT_MIPS_fde:
6503       return "DW_AT_MIPS_fde";
6504     case DW_AT_MIPS_loop_begin:
6505       return "DW_AT_MIPS_loop_begin";
6506     case DW_AT_MIPS_tail_loop_begin:
6507       return "DW_AT_MIPS_tail_loop_begin";
6508     case DW_AT_MIPS_epilog_begin:
6509       return "DW_AT_MIPS_epilog_begin";
6510     case DW_AT_MIPS_loop_unroll_factor:
6511       return "DW_AT_MIPS_loop_unroll_factor";
6512     case DW_AT_MIPS_software_pipeline_depth:
6513       return "DW_AT_MIPS_software_pipeline_depth";
6514     case DW_AT_MIPS_linkage_name:
6515       return "DW_AT_MIPS_linkage_name";
6516     case DW_AT_MIPS_stride:
6517       return "DW_AT_MIPS_stride";
6518     case DW_AT_MIPS_abstract_name:
6519       return "DW_AT_MIPS_abstract_name";
6520     case DW_AT_MIPS_clone_origin:
6521       return "DW_AT_MIPS_clone_origin";
6522     case DW_AT_MIPS_has_inlines:
6523       return "DW_AT_MIPS_has_inlines";
6524
6525     case DW_AT_sf_names:
6526       return "DW_AT_sf_names";
6527     case DW_AT_src_info:
6528       return "DW_AT_src_info";
6529     case DW_AT_mac_info:
6530       return "DW_AT_mac_info";
6531     case DW_AT_src_coords:
6532       return "DW_AT_src_coords";
6533     case DW_AT_body_begin:
6534       return "DW_AT_body_begin";
6535     case DW_AT_body_end:
6536       return "DW_AT_body_end";
6537     case DW_AT_GNU_vector:
6538       return "DW_AT_GNU_vector";
6539     case DW_AT_GNU_template_name:
6540       return "DW_AT_GNU_template_name";
6541
6542     case DW_AT_VMS_rtnbeg_pd_address:
6543       return "DW_AT_VMS_rtnbeg_pd_address";
6544
6545     default:
6546       return "DW_AT_<unknown>";
6547     }
6548 }
6549
6550 /* Convert a DWARF value form code into its string name.  */
6551
6552 static const char *
6553 dwarf_form_name (unsigned int form)
6554 {
6555   switch (form)
6556     {
6557     case DW_FORM_addr:
6558       return "DW_FORM_addr";
6559     case DW_FORM_block2:
6560       return "DW_FORM_block2";
6561     case DW_FORM_block4:
6562       return "DW_FORM_block4";
6563     case DW_FORM_data2:
6564       return "DW_FORM_data2";
6565     case DW_FORM_data4:
6566       return "DW_FORM_data4";
6567     case DW_FORM_data8:
6568       return "DW_FORM_data8";
6569     case DW_FORM_string:
6570       return "DW_FORM_string";
6571     case DW_FORM_block:
6572       return "DW_FORM_block";
6573     case DW_FORM_block1:
6574       return "DW_FORM_block1";
6575     case DW_FORM_data1:
6576       return "DW_FORM_data1";
6577     case DW_FORM_flag:
6578       return "DW_FORM_flag";
6579     case DW_FORM_sdata:
6580       return "DW_FORM_sdata";
6581     case DW_FORM_strp:
6582       return "DW_FORM_strp";
6583     case DW_FORM_udata:
6584       return "DW_FORM_udata";
6585     case DW_FORM_ref_addr:
6586       return "DW_FORM_ref_addr";
6587     case DW_FORM_ref1:
6588       return "DW_FORM_ref1";
6589     case DW_FORM_ref2:
6590       return "DW_FORM_ref2";
6591     case DW_FORM_ref4:
6592       return "DW_FORM_ref4";
6593     case DW_FORM_ref8:
6594       return "DW_FORM_ref8";
6595     case DW_FORM_ref_udata:
6596       return "DW_FORM_ref_udata";
6597     case DW_FORM_indirect:
6598       return "DW_FORM_indirect";
6599     default:
6600       return "DW_FORM_<unknown>";
6601     }
6602 }
6603 \f
6604 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6605    instance of an inlined instance of a decl which is local to an inline
6606    function, so we have to trace all of the way back through the origin chain
6607    to find out what sort of node actually served as the original seed for the
6608    given block.  */
6609
6610 static tree
6611 decl_ultimate_origin (const_tree decl)
6612 {
6613   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6614     return NULL_TREE;
6615
6616   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6617      nodes in the function to point to themselves; ignore that if
6618      we're trying to output the abstract instance of this function.  */
6619   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6620     return NULL_TREE;
6621
6622   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6623      most distant ancestor, this should never happen.  */
6624   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6625
6626   return DECL_ABSTRACT_ORIGIN (decl);
6627 }
6628
6629 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6630    of a virtual function may refer to a base class, so we check the 'this'
6631    parameter.  */
6632
6633 static tree
6634 decl_class_context (tree decl)
6635 {
6636   tree context = NULL_TREE;
6637
6638   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6639     context = DECL_CONTEXT (decl);
6640   else
6641     context = TYPE_MAIN_VARIANT
6642       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6643
6644   if (context && !TYPE_P (context))
6645     context = NULL_TREE;
6646
6647   return context;
6648 }
6649 \f
6650 /* Add an attribute/value pair to a DIE.  */
6651
6652 static inline void
6653 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6654 {
6655   /* Maybe this should be an assert?  */
6656   if (die == NULL)
6657     return;
6658
6659   if (die->die_attr == NULL)
6660     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6661   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6662 }
6663
6664 static inline enum dw_val_class
6665 AT_class (dw_attr_ref a)
6666 {
6667   return a->dw_attr_val.val_class;
6668 }
6669
6670 /* Add a flag value attribute to a DIE.  */
6671
6672 static inline void
6673 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6674 {
6675   dw_attr_node attr;
6676
6677   attr.dw_attr = attr_kind;
6678   attr.dw_attr_val.val_class = dw_val_class_flag;
6679   attr.dw_attr_val.v.val_flag = flag;
6680   add_dwarf_attr (die, &attr);
6681 }
6682
6683 static inline unsigned
6684 AT_flag (dw_attr_ref a)
6685 {
6686   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6687   return a->dw_attr_val.v.val_flag;
6688 }
6689
6690 /* Add a signed integer attribute value to a DIE.  */
6691
6692 static inline void
6693 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6694 {
6695   dw_attr_node attr;
6696
6697   attr.dw_attr = attr_kind;
6698   attr.dw_attr_val.val_class = dw_val_class_const;
6699   attr.dw_attr_val.v.val_int = int_val;
6700   add_dwarf_attr (die, &attr);
6701 }
6702
6703 static inline HOST_WIDE_INT
6704 AT_int (dw_attr_ref a)
6705 {
6706   gcc_assert (a && AT_class (a) == dw_val_class_const);
6707   return a->dw_attr_val.v.val_int;
6708 }
6709
6710 /* Add an unsigned integer attribute value to a DIE.  */
6711
6712 static inline void
6713 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6714                  unsigned HOST_WIDE_INT unsigned_val)
6715 {
6716   dw_attr_node attr;
6717
6718   attr.dw_attr = attr_kind;
6719   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6720   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6721   add_dwarf_attr (die, &attr);
6722 }
6723
6724 static inline unsigned HOST_WIDE_INT
6725 AT_unsigned (dw_attr_ref a)
6726 {
6727   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6728   return a->dw_attr_val.v.val_unsigned;
6729 }
6730
6731 /* Add an unsigned double integer attribute value to a DIE.  */
6732
6733 static inline void
6734 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6735                   rtx val_const_double)
6736 {
6737   dw_attr_node attr;
6738
6739   attr.dw_attr = attr_kind;
6740   attr.dw_attr_val.val_class = dw_val_class_long_long;
6741   attr.dw_attr_val.v.val_long_long = val_const_double;
6742   add_dwarf_attr (die, &attr);
6743 }
6744
6745 /* Add a floating point attribute value to a DIE and return it.  */
6746
6747 static inline void
6748 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6749             unsigned int length, unsigned int elt_size, unsigned char *array)
6750 {
6751   dw_attr_node attr;
6752
6753   attr.dw_attr = attr_kind;
6754   attr.dw_attr_val.val_class = dw_val_class_vec;
6755   attr.dw_attr_val.v.val_vec.length = length;
6756   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6757   attr.dw_attr_val.v.val_vec.array = array;
6758   add_dwarf_attr (die, &attr);
6759 }
6760
6761 /* Hash and equality functions for debug_str_hash.  */
6762
6763 static hashval_t
6764 debug_str_do_hash (const void *x)
6765 {
6766   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6767 }
6768
6769 static int
6770 debug_str_eq (const void *x1, const void *x2)
6771 {
6772   return strcmp ((((const struct indirect_string_node *)x1)->str),
6773                  (const char *)x2) == 0;
6774 }
6775
6776 /* Add STR to the indirect string hash table.  */
6777
6778 static struct indirect_string_node *
6779 find_AT_string (const char *str)
6780 {
6781   struct indirect_string_node *node;
6782   void **slot;
6783
6784   if (! debug_str_hash)
6785     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6786                                       debug_str_eq, NULL);
6787
6788   slot = htab_find_slot_with_hash (debug_str_hash, str,
6789                                    htab_hash_string (str), INSERT);
6790   if (*slot == NULL)
6791     {
6792       node = (struct indirect_string_node *)
6793                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6794       node->str = ggc_strdup (str);
6795       *slot = node;
6796     }
6797   else
6798     node = (struct indirect_string_node *) *slot;
6799
6800   node->refcount++;
6801   return node;
6802 }
6803
6804 /* Add a string attribute value to a DIE.  */
6805
6806 static inline void
6807 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6808 {
6809   dw_attr_node attr;
6810   struct indirect_string_node *node;
6811
6812   node = find_AT_string (str);
6813
6814   attr.dw_attr = attr_kind;
6815   attr.dw_attr_val.val_class = dw_val_class_str;
6816   attr.dw_attr_val.v.val_str = node;
6817   add_dwarf_attr (die, &attr);
6818 }
6819
6820 /* Create a label for an indirect string node, ensuring it is going to
6821    be output, unless its reference count goes down to zero.  */
6822
6823 static inline void
6824 gen_label_for_indirect_string (struct indirect_string_node *node)
6825 {
6826   char label[32];
6827
6828   if (node->label)
6829     return;
6830
6831   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6832   ++dw2_string_counter;
6833   node->label = xstrdup (label);
6834 }
6835
6836 /* Create a SYMBOL_REF rtx whose value is the initial address of a
6837    debug string STR.  */
6838
6839 static inline rtx
6840 get_debug_string_label (const char *str)
6841 {
6842   struct indirect_string_node *node = find_AT_string (str);
6843
6844   debug_str_hash_forced = true;
6845
6846   gen_label_for_indirect_string (node);
6847
6848   return gen_rtx_SYMBOL_REF (Pmode, node->label);
6849 }
6850
6851 static inline const char *
6852 AT_string (dw_attr_ref a)
6853 {
6854   gcc_assert (a && AT_class (a) == dw_val_class_str);
6855   return a->dw_attr_val.v.val_str->str;
6856 }
6857
6858 /* Find out whether a string should be output inline in DIE
6859    or out-of-line in .debug_str section.  */
6860
6861 static enum dwarf_form
6862 AT_string_form (dw_attr_ref a)
6863 {
6864   struct indirect_string_node *node;
6865   unsigned int len;
6866
6867   gcc_assert (a && AT_class (a) == dw_val_class_str);
6868
6869   node = a->dw_attr_val.v.val_str;
6870   if (node->form)
6871     return node->form;
6872
6873   len = strlen (node->str) + 1;
6874
6875   /* If the string is shorter or equal to the size of the reference, it is
6876      always better to put it inline.  */
6877   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6878     return node->form = DW_FORM_string;
6879
6880   /* If we cannot expect the linker to merge strings in .debug_str
6881      section, only put it into .debug_str if it is worth even in this
6882      single module.  */
6883   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
6884       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
6885       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
6886     return node->form = DW_FORM_string;
6887
6888   gen_label_for_indirect_string (node);
6889
6890   return node->form = DW_FORM_strp;
6891 }
6892
6893 /* Add a DIE reference attribute value to a DIE.  */
6894
6895 static inline void
6896 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6897 {
6898   dw_attr_node attr;
6899
6900   attr.dw_attr = attr_kind;
6901   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6902   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6903   attr.dw_attr_val.v.val_die_ref.external = 0;
6904   add_dwarf_attr (die, &attr);
6905 }
6906
6907 /* Add an AT_specification attribute to a DIE, and also make the back
6908    pointer from the specification to the definition.  */
6909
6910 static inline void
6911 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6912 {
6913   add_AT_die_ref (die, DW_AT_specification, targ_die);
6914   gcc_assert (!targ_die->die_definition);
6915   targ_die->die_definition = die;
6916 }
6917
6918 static inline dw_die_ref
6919 AT_ref (dw_attr_ref a)
6920 {
6921   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6922   return a->dw_attr_val.v.val_die_ref.die;
6923 }
6924
6925 static inline int
6926 AT_ref_external (dw_attr_ref a)
6927 {
6928   if (a && AT_class (a) == dw_val_class_die_ref)
6929     return a->dw_attr_val.v.val_die_ref.external;
6930
6931   return 0;
6932 }
6933
6934 static inline void
6935 set_AT_ref_external (dw_attr_ref a, int i)
6936 {
6937   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6938   a->dw_attr_val.v.val_die_ref.external = i;
6939 }
6940
6941 /* Add an FDE reference attribute value to a DIE.  */
6942
6943 static inline void
6944 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6945 {
6946   dw_attr_node attr;
6947
6948   attr.dw_attr = attr_kind;
6949   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6950   attr.dw_attr_val.v.val_fde_index = targ_fde;
6951   add_dwarf_attr (die, &attr);
6952 }
6953
6954 /* Add a location description attribute value to a DIE.  */
6955
6956 static inline void
6957 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6958 {
6959   dw_attr_node attr;
6960
6961   attr.dw_attr = attr_kind;
6962   attr.dw_attr_val.val_class = dw_val_class_loc;
6963   attr.dw_attr_val.v.val_loc = loc;
6964   add_dwarf_attr (die, &attr);
6965 }
6966
6967 static inline dw_loc_descr_ref
6968 AT_loc (dw_attr_ref a)
6969 {
6970   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6971   return a->dw_attr_val.v.val_loc;
6972 }
6973
6974 static inline void
6975 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6976 {
6977   dw_attr_node attr;
6978
6979   attr.dw_attr = attr_kind;
6980   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6981   attr.dw_attr_val.v.val_loc_list = loc_list;
6982   add_dwarf_attr (die, &attr);
6983   have_location_lists = true;
6984 }
6985
6986 static inline dw_loc_list_ref
6987 AT_loc_list (dw_attr_ref a)
6988 {
6989   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6990   return a->dw_attr_val.v.val_loc_list;
6991 }
6992
6993 /* Add an address constant attribute value to a DIE.  */
6994
6995 static inline void
6996 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6997 {
6998   dw_attr_node attr;
6999
7000   attr.dw_attr = attr_kind;
7001   attr.dw_attr_val.val_class = dw_val_class_addr;
7002   attr.dw_attr_val.v.val_addr = addr;
7003   add_dwarf_attr (die, &attr);
7004 }
7005
7006 /* Get the RTX from to an address DIE attribute.  */
7007
7008 static inline rtx
7009 AT_addr (dw_attr_ref a)
7010 {
7011   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7012   return a->dw_attr_val.v.val_addr;
7013 }
7014
7015 /* Add a file attribute value to a DIE.  */
7016
7017 static inline void
7018 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7019              struct dwarf_file_data *fd)
7020 {
7021   dw_attr_node attr;
7022
7023   attr.dw_attr = attr_kind;
7024   attr.dw_attr_val.val_class = dw_val_class_file;
7025   attr.dw_attr_val.v.val_file = fd;
7026   add_dwarf_attr (die, &attr);
7027 }
7028
7029 /* Get the dwarf_file_data from a file DIE attribute.  */
7030
7031 static inline struct dwarf_file_data *
7032 AT_file (dw_attr_ref a)
7033 {
7034   gcc_assert (a && AT_class (a) == dw_val_class_file);
7035   return a->dw_attr_val.v.val_file;
7036 }
7037
7038 /* Add a label identifier attribute value to a DIE.  */
7039
7040 static inline void
7041 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7042 {
7043   dw_attr_node attr;
7044
7045   attr.dw_attr = attr_kind;
7046   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7047   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7048   add_dwarf_attr (die, &attr);
7049 }
7050
7051 /* Add a section offset attribute value to a DIE, an offset into the
7052    debug_line section.  */
7053
7054 static inline void
7055 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7056                 const char *label)
7057 {
7058   dw_attr_node attr;
7059
7060   attr.dw_attr = attr_kind;
7061   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7062   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7063   add_dwarf_attr (die, &attr);
7064 }
7065
7066 /* Add a section offset attribute value to a DIE, an offset into the
7067    debug_macinfo section.  */
7068
7069 static inline void
7070 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7071                const char *label)
7072 {
7073   dw_attr_node attr;
7074
7075   attr.dw_attr = attr_kind;
7076   attr.dw_attr_val.val_class = dw_val_class_macptr;
7077   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7078   add_dwarf_attr (die, &attr);
7079 }
7080
7081 /* Add an offset attribute value to a DIE.  */
7082
7083 static inline void
7084 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7085                unsigned HOST_WIDE_INT offset)
7086 {
7087   dw_attr_node attr;
7088
7089   attr.dw_attr = attr_kind;
7090   attr.dw_attr_val.val_class = dw_val_class_offset;
7091   attr.dw_attr_val.v.val_offset = offset;
7092   add_dwarf_attr (die, &attr);
7093 }
7094
7095 /* Add an range_list attribute value to a DIE.  */
7096
7097 static void
7098 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7099                    long unsigned int offset)
7100 {
7101   dw_attr_node attr;
7102
7103   attr.dw_attr = attr_kind;
7104   attr.dw_attr_val.val_class = dw_val_class_range_list;
7105   attr.dw_attr_val.v.val_offset = offset;
7106   add_dwarf_attr (die, &attr);
7107 }
7108
7109 static inline const char *
7110 AT_lbl (dw_attr_ref a)
7111 {
7112   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7113                     || AT_class (a) == dw_val_class_lineptr
7114                     || AT_class (a) == dw_val_class_macptr));
7115   return a->dw_attr_val.v.val_lbl_id;
7116 }
7117
7118 /* Get the attribute of type attr_kind.  */
7119
7120 static dw_attr_ref
7121 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7122 {
7123   dw_attr_ref a;
7124   unsigned ix;
7125   dw_die_ref spec = NULL;
7126
7127   if (! die)
7128     return NULL;
7129
7130   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7131     if (a->dw_attr == attr_kind)
7132       return a;
7133     else if (a->dw_attr == DW_AT_specification
7134              || a->dw_attr == DW_AT_abstract_origin)
7135       spec = AT_ref (a);
7136
7137   if (spec)
7138     return get_AT (spec, attr_kind);
7139
7140   return NULL;
7141 }
7142
7143 /* Return the "low pc" attribute value, typically associated with a subprogram
7144    DIE.  Return null if the "low pc" attribute is either not present, or if it
7145    cannot be represented as an assembler label identifier.  */
7146
7147 static inline const char *
7148 get_AT_low_pc (dw_die_ref die)
7149 {
7150   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7151
7152   return a ? AT_lbl (a) : NULL;
7153 }
7154
7155 /* Return the "high pc" attribute value, typically associated with a subprogram
7156    DIE.  Return null if the "high pc" attribute is either not present, or if it
7157    cannot be represented as an assembler label identifier.  */
7158
7159 static inline const char *
7160 get_AT_hi_pc (dw_die_ref die)
7161 {
7162   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7163
7164   return a ? AT_lbl (a) : NULL;
7165 }
7166
7167 /* Return the value of the string attribute designated by ATTR_KIND, or
7168    NULL if it is not present.  */
7169
7170 static inline const char *
7171 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7172 {
7173   dw_attr_ref a = get_AT (die, attr_kind);
7174
7175   return a ? AT_string (a) : NULL;
7176 }
7177
7178 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7179    if it is not present.  */
7180
7181 static inline int
7182 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7183 {
7184   dw_attr_ref a = get_AT (die, attr_kind);
7185
7186   return a ? AT_flag (a) : 0;
7187 }
7188
7189 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7190    if it is not present.  */
7191
7192 static inline unsigned
7193 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7194 {
7195   dw_attr_ref a = get_AT (die, attr_kind);
7196
7197   return a ? AT_unsigned (a) : 0;
7198 }
7199
7200 static inline dw_die_ref
7201 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7202 {
7203   dw_attr_ref a = get_AT (die, attr_kind);
7204
7205   return a ? AT_ref (a) : NULL;
7206 }
7207
7208 static inline struct dwarf_file_data *
7209 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7210 {
7211   dw_attr_ref a = get_AT (die, attr_kind);
7212
7213   return a ? AT_file (a) : NULL;
7214 }
7215
7216 /* Return TRUE if the language is C or C++.  */
7217
7218 static inline bool
7219 is_c_family (void)
7220 {
7221   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7222
7223   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7224           || lang == DW_LANG_C99
7225           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7226 }
7227
7228 /* Return TRUE if the language is C++.  */
7229
7230 static inline bool
7231 is_cxx (void)
7232 {
7233   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7234
7235   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7236 }
7237
7238 /* Return TRUE if the language is Fortran.  */
7239
7240 static inline bool
7241 is_fortran (void)
7242 {
7243   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7244
7245   return (lang == DW_LANG_Fortran77
7246           || lang == DW_LANG_Fortran90
7247           || lang == DW_LANG_Fortran95);
7248 }
7249
7250 /* Return TRUE if the language is Java.  */
7251
7252 static inline bool
7253 is_java (void)
7254 {
7255   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7256
7257   return lang == DW_LANG_Java;
7258 }
7259
7260 /* Return TRUE if the language is Ada.  */
7261
7262 static inline bool
7263 is_ada (void)
7264 {
7265   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7266
7267   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7268 }
7269
7270 /* Remove the specified attribute if present.  */
7271
7272 static void
7273 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7274 {
7275   dw_attr_ref a;
7276   unsigned ix;
7277
7278   if (! die)
7279     return;
7280
7281   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7282     if (a->dw_attr == attr_kind)
7283       {
7284         if (AT_class (a) == dw_val_class_str)
7285           if (a->dw_attr_val.v.val_str->refcount)
7286             a->dw_attr_val.v.val_str->refcount--;
7287
7288         /* VEC_ordered_remove should help reduce the number of abbrevs
7289            that are needed.  */
7290         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7291         return;
7292       }
7293 }
7294
7295 /* Remove CHILD from its parent.  PREV must have the property that
7296    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7297
7298 static void
7299 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7300 {
7301   gcc_assert (child->die_parent == prev->die_parent);
7302   gcc_assert (prev->die_sib == child);
7303   if (prev == child)
7304     {
7305       gcc_assert (child->die_parent->die_child == child);
7306       prev = NULL;
7307     }
7308   else
7309     prev->die_sib = child->die_sib;
7310   if (child->die_parent->die_child == child)
7311     child->die_parent->die_child = prev;
7312 }
7313
7314 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7315    matches TAG.  */
7316
7317 static void
7318 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7319 {
7320   dw_die_ref c;
7321
7322   c = die->die_child;
7323   if (c) do {
7324     dw_die_ref prev = c;
7325     c = c->die_sib;
7326     while (c->die_tag == tag)
7327       {
7328         remove_child_with_prev (c, prev);
7329         /* Might have removed every child.  */
7330         if (c == c->die_sib)
7331           return;
7332         c = c->die_sib;
7333       }
7334   } while (c != die->die_child);
7335 }
7336
7337 /* Add a CHILD_DIE as the last child of DIE.  */
7338
7339 static void
7340 add_child_die (dw_die_ref die, dw_die_ref child_die)
7341 {
7342   /* FIXME this should probably be an assert.  */
7343   if (! die || ! child_die)
7344     return;
7345   gcc_assert (die != child_die);
7346
7347   child_die->die_parent = die;
7348   if (die->die_child)
7349     {
7350       child_die->die_sib = die->die_child->die_sib;
7351       die->die_child->die_sib = child_die;
7352     }
7353   else
7354     child_die->die_sib = child_die;
7355   die->die_child = child_die;
7356 }
7357
7358 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7359    is the specification, to the end of PARENT's list of children.
7360    This is done by removing and re-adding it.  */
7361
7362 static void
7363 splice_child_die (dw_die_ref parent, dw_die_ref child)
7364 {
7365   dw_die_ref p;
7366
7367   /* We want the declaration DIE from inside the class, not the
7368      specification DIE at toplevel.  */
7369   if (child->die_parent != parent)
7370     {
7371       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7372
7373       if (tmp)
7374         child = tmp;
7375     }
7376
7377   gcc_assert (child->die_parent == parent
7378               || (child->die_parent
7379                   == get_AT_ref (parent, DW_AT_specification)));
7380
7381   for (p = child->die_parent->die_child; ; p = p->die_sib)
7382     if (p->die_sib == child)
7383       {
7384         remove_child_with_prev (child, p);
7385         break;
7386       }
7387
7388   add_child_die (parent, child);
7389 }
7390
7391 /* Return a pointer to a newly created DIE node.  */
7392
7393 static inline dw_die_ref
7394 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7395 {
7396   dw_die_ref die = GGC_CNEW (die_node);
7397
7398   die->die_tag = tag_value;
7399
7400   if (parent_die != NULL)
7401     add_child_die (parent_die, die);
7402   else
7403     {
7404       limbo_die_node *limbo_node;
7405
7406       limbo_node = GGC_CNEW (limbo_die_node);
7407       limbo_node->die = die;
7408       limbo_node->created_for = t;
7409       limbo_node->next = limbo_die_list;
7410       limbo_die_list = limbo_node;
7411     }
7412
7413   return die;
7414 }
7415
7416 /* Return the DIE associated with the given type specifier.  */
7417
7418 static inline dw_die_ref
7419 lookup_type_die (tree type)
7420 {
7421   return TYPE_SYMTAB_DIE (type);
7422 }
7423
7424 /* Equate a DIE to a given type specifier.  */
7425
7426 static inline void
7427 equate_type_number_to_die (tree type, dw_die_ref type_die)
7428 {
7429   TYPE_SYMTAB_DIE (type) = type_die;
7430 }
7431
7432 /* Returns a hash value for X (which really is a die_struct).  */
7433
7434 static hashval_t
7435 decl_die_table_hash (const void *x)
7436 {
7437   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7438 }
7439
7440 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7441
7442 static int
7443 decl_die_table_eq (const void *x, const void *y)
7444 {
7445   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7446 }
7447
7448 /* Return the DIE associated with a given declaration.  */
7449
7450 static inline dw_die_ref
7451 lookup_decl_die (tree decl)
7452 {
7453   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7454 }
7455
7456 /* Returns a hash value for X (which really is a var_loc_list).  */
7457
7458 static hashval_t
7459 decl_loc_table_hash (const void *x)
7460 {
7461   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7462 }
7463
7464 /* Return nonzero if decl_id of var_loc_list X is the same as
7465    UID of decl *Y.  */
7466
7467 static int
7468 decl_loc_table_eq (const void *x, const void *y)
7469 {
7470   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7471 }
7472
7473 /* Return the var_loc list associated with a given declaration.  */
7474
7475 static inline var_loc_list *
7476 lookup_decl_loc (const_tree decl)
7477 {
7478   return (var_loc_list *)
7479     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7480 }
7481
7482 /* Equate a DIE to a particular declaration.  */
7483
7484 static void
7485 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7486 {
7487   unsigned int decl_id = DECL_UID (decl);
7488   void **slot;
7489
7490   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7491   *slot = decl_die;
7492   decl_die->decl_id = decl_id;
7493 }
7494
7495 /* Add a variable location node to the linked list for DECL.  */
7496
7497 static void
7498 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7499 {
7500   unsigned int decl_id = DECL_UID (decl);
7501   var_loc_list *temp;
7502   void **slot;
7503
7504   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7505   if (*slot == NULL)
7506     {
7507       temp = GGC_CNEW (var_loc_list);
7508       temp->decl_id = decl_id;
7509       *slot = temp;
7510     }
7511   else
7512     temp = (var_loc_list *) *slot;
7513
7514   if (temp->last)
7515     {
7516       /* If the current location is the same as the end of the list,
7517          and either both or neither of the locations is uninitialized,
7518          we have nothing to do.  */
7519       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7520                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7521           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7522                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7523               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7524                    == VAR_INIT_STATUS_UNINITIALIZED)
7525                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7526                       == VAR_INIT_STATUS_UNINITIALIZED))))
7527         {
7528           /* Add LOC to the end of list and update LAST.  */
7529           temp->last->next = loc;
7530           temp->last = loc;
7531         }
7532     }
7533   /* Do not add empty location to the beginning of the list.  */
7534   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7535     {
7536       temp->first = loc;
7537       temp->last = loc;
7538     }
7539 }
7540 \f
7541 /* Keep track of the number of spaces used to indent the
7542    output of the debugging routines that print the structure of
7543    the DIE internal representation.  */
7544 static int print_indent;
7545
7546 /* Indent the line the number of spaces given by print_indent.  */
7547
7548 static inline void
7549 print_spaces (FILE *outfile)
7550 {
7551   fprintf (outfile, "%*s", print_indent, "");
7552 }
7553
7554 /* Print the information associated with a given DIE, and its children.
7555    This routine is a debugging aid only.  */
7556
7557 static void
7558 print_die (dw_die_ref die, FILE *outfile)
7559 {
7560   dw_attr_ref a;
7561   dw_die_ref c;
7562   unsigned ix;
7563
7564   print_spaces (outfile);
7565   fprintf (outfile, "DIE %4ld: %s\n",
7566            die->die_offset, dwarf_tag_name (die->die_tag));
7567   print_spaces (outfile);
7568   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7569   fprintf (outfile, " offset: %ld\n", die->die_offset);
7570
7571   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7572     {
7573       print_spaces (outfile);
7574       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7575
7576       switch (AT_class (a))
7577         {
7578         case dw_val_class_addr:
7579           fprintf (outfile, "address");
7580           break;
7581         case dw_val_class_offset:
7582           fprintf (outfile, "offset");
7583           break;
7584         case dw_val_class_loc:
7585           fprintf (outfile, "location descriptor");
7586           break;
7587         case dw_val_class_loc_list:
7588           fprintf (outfile, "location list -> label:%s",
7589                    AT_loc_list (a)->ll_symbol);
7590           break;
7591         case dw_val_class_range_list:
7592           fprintf (outfile, "range list");
7593           break;
7594         case dw_val_class_const:
7595           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7596           break;
7597         case dw_val_class_unsigned_const:
7598           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7599           break;
7600         case dw_val_class_long_long:
7601           fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_UNSIGNED
7602                             "," HOST_WIDE_INT_PRINT_UNSIGNED ")",
7603                    CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long),
7604                    CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long));
7605           break;
7606         case dw_val_class_vec:
7607           fprintf (outfile, "floating-point or vector constant");
7608           break;
7609         case dw_val_class_flag:
7610           fprintf (outfile, "%u", AT_flag (a));
7611           break;
7612         case dw_val_class_die_ref:
7613           if (AT_ref (a) != NULL)
7614             {
7615               if (AT_ref (a)->die_symbol)
7616                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7617               else
7618                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7619             }
7620           else
7621             fprintf (outfile, "die -> <null>");
7622           break;
7623         case dw_val_class_lbl_id:
7624         case dw_val_class_lineptr:
7625         case dw_val_class_macptr:
7626           fprintf (outfile, "label: %s", AT_lbl (a));
7627           break;
7628         case dw_val_class_str:
7629           if (AT_string (a) != NULL)
7630             fprintf (outfile, "\"%s\"", AT_string (a));
7631           else
7632             fprintf (outfile, "<null>");
7633           break;
7634         case dw_val_class_file:
7635           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7636                    AT_file (a)->emitted_number);
7637           break;
7638         default:
7639           break;
7640         }
7641
7642       fprintf (outfile, "\n");
7643     }
7644
7645   if (die->die_child != NULL)
7646     {
7647       print_indent += 4;
7648       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7649       print_indent -= 4;
7650     }
7651   if (print_indent == 0)
7652     fprintf (outfile, "\n");
7653 }
7654
7655 /* Print the contents of the source code line number correspondence table.
7656    This routine is a debugging aid only.  */
7657
7658 static void
7659 print_dwarf_line_table (FILE *outfile)
7660 {
7661   unsigned i;
7662   dw_line_info_ref line_info;
7663
7664   fprintf (outfile, "\n\nDWARF source line information\n");
7665   for (i = 1; i < line_info_table_in_use; i++)
7666     {
7667       line_info = &line_info_table[i];
7668       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7669                line_info->dw_file_num,
7670                line_info->dw_line_num);
7671     }
7672
7673   fprintf (outfile, "\n\n");
7674 }
7675
7676 /* Print the information collected for a given DIE.  */
7677
7678 void
7679 debug_dwarf_die (dw_die_ref die)
7680 {
7681   print_die (die, stderr);
7682 }
7683
7684 /* Print all DWARF information collected for the compilation unit.
7685    This routine is a debugging aid only.  */
7686
7687 void
7688 debug_dwarf (void)
7689 {
7690   print_indent = 0;
7691   print_die (comp_unit_die, stderr);
7692   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7693     print_dwarf_line_table (stderr);
7694 }
7695 \f
7696 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7697    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7698    DIE that marks the start of the DIEs for this include file.  */
7699
7700 static dw_die_ref
7701 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7702 {
7703   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7704   dw_die_ref new_unit = gen_compile_unit_die (filename);
7705
7706   new_unit->die_sib = old_unit;
7707   return new_unit;
7708 }
7709
7710 /* Close an include-file CU and reopen the enclosing one.  */
7711
7712 static dw_die_ref
7713 pop_compile_unit (dw_die_ref old_unit)
7714 {
7715   dw_die_ref new_unit = old_unit->die_sib;
7716
7717   old_unit->die_sib = NULL;
7718   return new_unit;
7719 }
7720
7721 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7722 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7723
7724 /* Calculate the checksum of a location expression.  */
7725
7726 static inline void
7727 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7728 {
7729   int tem;
7730
7731   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7732   CHECKSUM (tem);
7733   CHECKSUM (loc->dw_loc_oprnd1);
7734   CHECKSUM (loc->dw_loc_oprnd2);
7735 }
7736
7737 /* Calculate the checksum of an attribute.  */
7738
7739 static void
7740 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7741 {
7742   dw_loc_descr_ref loc;
7743   rtx r;
7744
7745   CHECKSUM (at->dw_attr);
7746
7747   /* We don't care that this was compiled with a different compiler
7748      snapshot; if the output is the same, that's what matters.  */
7749   if (at->dw_attr == DW_AT_producer)
7750     return;
7751
7752   switch (AT_class (at))
7753     {
7754     case dw_val_class_const:
7755       CHECKSUM (at->dw_attr_val.v.val_int);
7756       break;
7757     case dw_val_class_unsigned_const:
7758       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7759       break;
7760     case dw_val_class_long_long:
7761       CHECKSUM (CONST_DOUBLE_HIGH (at->dw_attr_val.v.val_long_long));
7762       CHECKSUM (CONST_DOUBLE_LOW (at->dw_attr_val.v.val_long_long));
7763       break;
7764     case dw_val_class_vec:
7765       CHECKSUM (at->dw_attr_val.v.val_vec);
7766       break;
7767     case dw_val_class_flag:
7768       CHECKSUM (at->dw_attr_val.v.val_flag);
7769       break;
7770     case dw_val_class_str:
7771       CHECKSUM_STRING (AT_string (at));
7772       break;
7773
7774     case dw_val_class_addr:
7775       r = AT_addr (at);
7776       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7777       CHECKSUM_STRING (XSTR (r, 0));
7778       break;
7779
7780     case dw_val_class_offset:
7781       CHECKSUM (at->dw_attr_val.v.val_offset);
7782       break;
7783
7784     case dw_val_class_loc:
7785       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7786         loc_checksum (loc, ctx);
7787       break;
7788
7789     case dw_val_class_die_ref:
7790       die_checksum (AT_ref (at), ctx, mark);
7791       break;
7792
7793     case dw_val_class_fde_ref:
7794     case dw_val_class_lbl_id:
7795     case dw_val_class_lineptr:
7796     case dw_val_class_macptr:
7797       break;
7798
7799     case dw_val_class_file:
7800       CHECKSUM_STRING (AT_file (at)->filename);
7801       break;
7802
7803     default:
7804       break;
7805     }
7806 }
7807
7808 /* Calculate the checksum of a DIE.  */
7809
7810 static void
7811 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7812 {
7813   dw_die_ref c;
7814   dw_attr_ref a;
7815   unsigned ix;
7816
7817   /* To avoid infinite recursion.  */
7818   if (die->die_mark)
7819     {
7820       CHECKSUM (die->die_mark);
7821       return;
7822     }
7823   die->die_mark = ++(*mark);
7824
7825   CHECKSUM (die->die_tag);
7826
7827   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7828     attr_checksum (a, ctx, mark);
7829
7830   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7831 }
7832
7833 #undef CHECKSUM
7834 #undef CHECKSUM_STRING
7835
7836 /* Do the location expressions look same?  */
7837 static inline int
7838 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7839 {
7840   return loc1->dw_loc_opc == loc2->dw_loc_opc
7841          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7842          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7843 }
7844
7845 /* Do the values look the same?  */
7846 static int
7847 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7848 {
7849   dw_loc_descr_ref loc1, loc2;
7850   rtx r1, r2;
7851
7852   if (v1->val_class != v2->val_class)
7853     return 0;
7854
7855   switch (v1->val_class)
7856     {
7857     case dw_val_class_const:
7858       return v1->v.val_int == v2->v.val_int;
7859     case dw_val_class_unsigned_const:
7860       return v1->v.val_unsigned == v2->v.val_unsigned;
7861     case dw_val_class_long_long:
7862       return CONST_DOUBLE_HIGH (v1->v.val_long_long)
7863              == CONST_DOUBLE_HIGH (v2->v.val_long_long)
7864              && CONST_DOUBLE_LOW (v1->v.val_long_long)
7865                 == CONST_DOUBLE_LOW (v2->v.val_long_long);
7866     case dw_val_class_vec:
7867       if (v1->v.val_vec.length != v2->v.val_vec.length
7868           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7869         return 0;
7870       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7871                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7872         return 0;
7873       return 1;
7874     case dw_val_class_flag:
7875       return v1->v.val_flag == v2->v.val_flag;
7876     case dw_val_class_str:
7877       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7878
7879     case dw_val_class_addr:
7880       r1 = v1->v.val_addr;
7881       r2 = v2->v.val_addr;
7882       if (GET_CODE (r1) != GET_CODE (r2))
7883         return 0;
7884       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7885       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7886
7887     case dw_val_class_offset:
7888       return v1->v.val_offset == v2->v.val_offset;
7889
7890     case dw_val_class_loc:
7891       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7892            loc1 && loc2;
7893            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7894         if (!same_loc_p (loc1, loc2, mark))
7895           return 0;
7896       return !loc1 && !loc2;
7897
7898     case dw_val_class_die_ref:
7899       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7900
7901     case dw_val_class_fde_ref:
7902     case dw_val_class_lbl_id:
7903     case dw_val_class_lineptr:
7904     case dw_val_class_macptr:
7905       return 1;
7906
7907     case dw_val_class_file:
7908       return v1->v.val_file == v2->v.val_file;
7909
7910     default:
7911       return 1;
7912     }
7913 }
7914
7915 /* Do the attributes look the same?  */
7916
7917 static int
7918 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7919 {
7920   if (at1->dw_attr != at2->dw_attr)
7921     return 0;
7922
7923   /* We don't care that this was compiled with a different compiler
7924      snapshot; if the output is the same, that's what matters. */
7925   if (at1->dw_attr == DW_AT_producer)
7926     return 1;
7927
7928   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7929 }
7930
7931 /* Do the dies look the same?  */
7932
7933 static int
7934 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7935 {
7936   dw_die_ref c1, c2;
7937   dw_attr_ref a1;
7938   unsigned ix;
7939
7940   /* To avoid infinite recursion.  */
7941   if (die1->die_mark)
7942     return die1->die_mark == die2->die_mark;
7943   die1->die_mark = die2->die_mark = ++(*mark);
7944
7945   if (die1->die_tag != die2->die_tag)
7946     return 0;
7947
7948   if (VEC_length (dw_attr_node, die1->die_attr)
7949       != VEC_length (dw_attr_node, die2->die_attr))
7950     return 0;
7951
7952   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7953     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7954       return 0;
7955
7956   c1 = die1->die_child;
7957   c2 = die2->die_child;
7958   if (! c1)
7959     {
7960       if (c2)
7961         return 0;
7962     }
7963   else
7964     for (;;)
7965       {
7966         if (!same_die_p (c1, c2, mark))
7967           return 0;
7968         c1 = c1->die_sib;
7969         c2 = c2->die_sib;
7970         if (c1 == die1->die_child)
7971           {
7972             if (c2 == die2->die_child)
7973               break;
7974             else
7975               return 0;
7976           }
7977     }
7978
7979   return 1;
7980 }
7981
7982 /* Do the dies look the same?  Wrapper around same_die_p.  */
7983
7984 static int
7985 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7986 {
7987   int mark = 0;
7988   int ret = same_die_p (die1, die2, &mark);
7989
7990   unmark_all_dies (die1);
7991   unmark_all_dies (die2);
7992
7993   return ret;
7994 }
7995
7996 /* The prefix to attach to symbols on DIEs in the current comdat debug
7997    info section.  */
7998 static char *comdat_symbol_id;
7999
8000 /* The index of the current symbol within the current comdat CU.  */
8001 static unsigned int comdat_symbol_number;
8002
8003 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8004    children, and set comdat_symbol_id accordingly.  */
8005
8006 static void
8007 compute_section_prefix (dw_die_ref unit_die)
8008 {
8009   const char *die_name = get_AT_string (unit_die, DW_AT_name);
8010   const char *base = die_name ? lbasename (die_name) : "anonymous";
8011   char *name = XALLOCAVEC (char, strlen (base) + 64);
8012   char *p;
8013   int i, mark;
8014   unsigned char checksum[16];
8015   struct md5_ctx ctx;
8016
8017   /* Compute the checksum of the DIE, then append part of it as hex digits to
8018      the name filename of the unit.  */
8019
8020   md5_init_ctx (&ctx);
8021   mark = 0;
8022   die_checksum (unit_die, &ctx, &mark);
8023   unmark_all_dies (unit_die);
8024   md5_finish_ctx (&ctx, checksum);
8025
8026   sprintf (name, "%s.", base);
8027   clean_symbol_name (name);
8028
8029   p = name + strlen (name);
8030   for (i = 0; i < 4; i++)
8031     {
8032       sprintf (p, "%.2x", checksum[i]);
8033       p += 2;
8034     }
8035
8036   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
8037   comdat_symbol_number = 0;
8038 }
8039
8040 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
8041
8042 static int
8043 is_type_die (dw_die_ref die)
8044 {
8045   switch (die->die_tag)
8046     {
8047     case DW_TAG_array_type:
8048     case DW_TAG_class_type:
8049     case DW_TAG_interface_type:
8050     case DW_TAG_enumeration_type:
8051     case DW_TAG_pointer_type:
8052     case DW_TAG_reference_type:
8053     case DW_TAG_string_type:
8054     case DW_TAG_structure_type:
8055     case DW_TAG_subroutine_type:
8056     case DW_TAG_union_type:
8057     case DW_TAG_ptr_to_member_type:
8058     case DW_TAG_set_type:
8059     case DW_TAG_subrange_type:
8060     case DW_TAG_base_type:
8061     case DW_TAG_const_type:
8062     case DW_TAG_file_type:
8063     case DW_TAG_packed_type:
8064     case DW_TAG_volatile_type:
8065     case DW_TAG_typedef:
8066       return 1;
8067     default:
8068       return 0;
8069     }
8070 }
8071
8072 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
8073    Basically, we want to choose the bits that are likely to be shared between
8074    compilations (types) and leave out the bits that are specific to individual
8075    compilations (functions).  */
8076
8077 static int
8078 is_comdat_die (dw_die_ref c)
8079 {
8080   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
8081      we do for stabs.  The advantage is a greater likelihood of sharing between
8082      objects that don't include headers in the same order (and therefore would
8083      put the base types in a different comdat).  jason 8/28/00 */
8084
8085   if (c->die_tag == DW_TAG_base_type)
8086     return 0;
8087
8088   if (c->die_tag == DW_TAG_pointer_type
8089       || c->die_tag == DW_TAG_reference_type
8090       || c->die_tag == DW_TAG_const_type
8091       || c->die_tag == DW_TAG_volatile_type)
8092     {
8093       dw_die_ref t = get_AT_ref (c, DW_AT_type);
8094
8095       return t ? is_comdat_die (t) : 0;
8096     }
8097
8098   return is_type_die (c);
8099 }
8100
8101 /* Returns 1 iff C is the sort of DIE that might be referred to from another
8102    compilation unit.  */
8103
8104 static int
8105 is_symbol_die (dw_die_ref c)
8106 {
8107   return (is_type_die (c)
8108           || (get_AT (c, DW_AT_declaration)
8109               && !get_AT (c, DW_AT_specification))
8110           || c->die_tag == DW_TAG_namespace
8111           || c->die_tag == DW_TAG_module);
8112 }
8113
8114 static char *
8115 gen_internal_sym (const char *prefix)
8116 {
8117   char buf[256];
8118
8119   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8120   return xstrdup (buf);
8121 }
8122
8123 /* Assign symbols to all worthy DIEs under DIE.  */
8124
8125 static void
8126 assign_symbol_names (dw_die_ref die)
8127 {
8128   dw_die_ref c;
8129
8130   if (is_symbol_die (die))
8131     {
8132       if (comdat_symbol_id)
8133         {
8134           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
8135
8136           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
8137                    comdat_symbol_id, comdat_symbol_number++);
8138           die->die_symbol = xstrdup (p);
8139         }
8140       else
8141         die->die_symbol = gen_internal_sym ("LDIE");
8142     }
8143
8144   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
8145 }
8146
8147 struct cu_hash_table_entry
8148 {
8149   dw_die_ref cu;
8150   unsigned min_comdat_num, max_comdat_num;
8151   struct cu_hash_table_entry *next;
8152 };
8153
8154 /* Routines to manipulate hash table of CUs.  */
8155 static hashval_t
8156 htab_cu_hash (const void *of)
8157 {
8158   const struct cu_hash_table_entry *const entry =
8159     (const struct cu_hash_table_entry *) of;
8160
8161   return htab_hash_string (entry->cu->die_symbol);
8162 }
8163
8164 static int
8165 htab_cu_eq (const void *of1, const void *of2)
8166 {
8167   const struct cu_hash_table_entry *const entry1 =
8168     (const struct cu_hash_table_entry *) of1;
8169   const struct die_struct *const entry2 = (const struct die_struct *) of2;
8170
8171   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
8172 }
8173
8174 static void
8175 htab_cu_del (void *what)
8176 {
8177   struct cu_hash_table_entry *next,
8178     *entry = (struct cu_hash_table_entry *) what;
8179
8180   while (entry)
8181     {
8182       next = entry->next;
8183       free (entry);
8184       entry = next;
8185     }
8186 }
8187
8188 /* Check whether we have already seen this CU and set up SYM_NUM
8189    accordingly.  */
8190 static int
8191 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
8192 {
8193   struct cu_hash_table_entry dummy;
8194   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
8195
8196   dummy.max_comdat_num = 0;
8197
8198   slot = (struct cu_hash_table_entry **)
8199     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8200         INSERT);
8201   entry = *slot;
8202
8203   for (; entry; last = entry, entry = entry->next)
8204     {
8205       if (same_die_p_wrap (cu, entry->cu))
8206         break;
8207     }
8208
8209   if (entry)
8210     {
8211       *sym_num = entry->min_comdat_num;
8212       return 1;
8213     }
8214
8215   entry = XCNEW (struct cu_hash_table_entry);
8216   entry->cu = cu;
8217   entry->min_comdat_num = *sym_num = last->max_comdat_num;
8218   entry->next = *slot;
8219   *slot = entry;
8220
8221   return 0;
8222 }
8223
8224 /* Record SYM_NUM to record of CU in HTABLE.  */
8225 static void
8226 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
8227 {
8228   struct cu_hash_table_entry **slot, *entry;
8229
8230   slot = (struct cu_hash_table_entry **)
8231     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8232         NO_INSERT);
8233   entry = *slot;
8234
8235   entry->max_comdat_num = sym_num;
8236 }
8237
8238 /* Traverse the DIE (which is always comp_unit_die), and set up
8239    additional compilation units for each of the include files we see
8240    bracketed by BINCL/EINCL.  */
8241
8242 static void
8243 break_out_includes (dw_die_ref die)
8244 {
8245   dw_die_ref c;
8246   dw_die_ref unit = NULL;
8247   limbo_die_node *node, **pnode;
8248   htab_t cu_hash_table;
8249
8250   c = die->die_child;
8251   if (c) do {
8252     dw_die_ref prev = c;
8253     c = c->die_sib;
8254     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
8255            || (unit && is_comdat_die (c)))
8256       {
8257         dw_die_ref next = c->die_sib;
8258
8259         /* This DIE is for a secondary CU; remove it from the main one.  */
8260         remove_child_with_prev (c, prev);
8261
8262         if (c->die_tag == DW_TAG_GNU_BINCL)
8263           unit = push_new_compile_unit (unit, c);
8264         else if (c->die_tag == DW_TAG_GNU_EINCL)
8265           unit = pop_compile_unit (unit);
8266         else
8267           add_child_die (unit, c);
8268         c = next;
8269         if (c == die->die_child)
8270           break;
8271       }
8272   } while (c != die->die_child);
8273
8274 #if 0
8275   /* We can only use this in debugging, since the frontend doesn't check
8276      to make sure that we leave every include file we enter.  */
8277   gcc_assert (!unit);
8278 #endif
8279
8280   assign_symbol_names (die);
8281   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
8282   for (node = limbo_die_list, pnode = &limbo_die_list;
8283        node;
8284        node = node->next)
8285     {
8286       int is_dupl;
8287
8288       compute_section_prefix (node->die);
8289       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
8290                         &comdat_symbol_number);
8291       assign_symbol_names (node->die);
8292       if (is_dupl)
8293         *pnode = node->next;
8294       else
8295         {
8296           pnode = &node->next;
8297           record_comdat_symbol_number (node->die, cu_hash_table,
8298                 comdat_symbol_number);
8299         }
8300     }
8301   htab_delete (cu_hash_table);
8302 }
8303
8304 /* Traverse the DIE and add a sibling attribute if it may have the
8305    effect of speeding up access to siblings.  To save some space,
8306    avoid generating sibling attributes for DIE's without children.  */
8307
8308 static void
8309 add_sibling_attributes (dw_die_ref die)
8310 {
8311   dw_die_ref c;
8312
8313   if (! die->die_child)
8314     return;
8315
8316   if (die->die_parent && die != die->die_parent->die_child)
8317     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8318
8319   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8320 }
8321
8322 /* Output all location lists for the DIE and its children.  */
8323
8324 static void
8325 output_location_lists (dw_die_ref die)
8326 {
8327   dw_die_ref c;
8328   dw_attr_ref a;
8329   unsigned ix;
8330
8331   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8332     if (AT_class (a) == dw_val_class_loc_list)
8333       output_loc_list (AT_loc_list (a));
8334
8335   FOR_EACH_CHILD (die, c, output_location_lists (c));
8336 }
8337
8338 /* The format of each DIE (and its attribute value pairs) is encoded in an
8339    abbreviation table.  This routine builds the abbreviation table and assigns
8340    a unique abbreviation id for each abbreviation entry.  The children of each
8341    die are visited recursively.  */
8342
8343 static void
8344 build_abbrev_table (dw_die_ref die)
8345 {
8346   unsigned long abbrev_id;
8347   unsigned int n_alloc;
8348   dw_die_ref c;
8349   dw_attr_ref a;
8350   unsigned ix;
8351
8352   /* Scan the DIE references, and mark as external any that refer to
8353      DIEs from other CUs (i.e. those which are not marked).  */
8354   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8355     if (AT_class (a) == dw_val_class_die_ref
8356         && AT_ref (a)->die_mark == 0)
8357       {
8358         gcc_assert (AT_ref (a)->die_symbol);
8359         set_AT_ref_external (a, 1);
8360       }
8361
8362   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8363     {
8364       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8365       dw_attr_ref die_a, abbrev_a;
8366       unsigned ix;
8367       bool ok = true;
8368
8369       if (abbrev->die_tag != die->die_tag)
8370         continue;
8371       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8372         continue;
8373
8374       if (VEC_length (dw_attr_node, abbrev->die_attr)
8375           != VEC_length (dw_attr_node, die->die_attr))
8376         continue;
8377
8378       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
8379         {
8380           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8381           if ((abbrev_a->dw_attr != die_a->dw_attr)
8382               || (value_format (abbrev_a) != value_format (die_a)))
8383             {
8384               ok = false;
8385               break;
8386             }
8387         }
8388       if (ok)
8389         break;
8390     }
8391
8392   if (abbrev_id >= abbrev_die_table_in_use)
8393     {
8394       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8395         {
8396           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8397           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8398                                             n_alloc);
8399
8400           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8401                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8402           abbrev_die_table_allocated = n_alloc;
8403         }
8404
8405       ++abbrev_die_table_in_use;
8406       abbrev_die_table[abbrev_id] = die;
8407     }
8408
8409   die->die_abbrev = abbrev_id;
8410   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8411 }
8412 \f
8413 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8414
8415 static int
8416 constant_size (unsigned HOST_WIDE_INT value)
8417 {
8418   int log;
8419
8420   if (value == 0)
8421     log = 0;
8422   else
8423     log = floor_log2 (value);
8424
8425   log = log / 8;
8426   log = 1 << (floor_log2 (log) + 1);
8427
8428   return log;
8429 }
8430
8431 /* Return the size of a DIE as it is represented in the
8432    .debug_info section.  */
8433
8434 static unsigned long
8435 size_of_die (dw_die_ref die)
8436 {
8437   unsigned long size = 0;
8438   dw_attr_ref a;
8439   unsigned ix;
8440
8441   size += size_of_uleb128 (die->die_abbrev);
8442   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8443     {
8444       switch (AT_class (a))
8445         {
8446         case dw_val_class_addr:
8447           size += DWARF2_ADDR_SIZE;
8448           break;
8449         case dw_val_class_offset:
8450           size += DWARF_OFFSET_SIZE;
8451           break;
8452         case dw_val_class_loc:
8453           {
8454             unsigned long lsize = size_of_locs (AT_loc (a));
8455
8456             /* Block length.  */
8457             size += constant_size (lsize);
8458             size += lsize;
8459           }
8460           break;
8461         case dw_val_class_loc_list:
8462           size += DWARF_OFFSET_SIZE;
8463           break;
8464         case dw_val_class_range_list:
8465           size += DWARF_OFFSET_SIZE;
8466           break;
8467         case dw_val_class_const:
8468           size += size_of_sleb128 (AT_int (a));
8469           break;
8470         case dw_val_class_unsigned_const:
8471           size += constant_size (AT_unsigned (a));
8472           break;
8473         case dw_val_class_long_long:
8474           size += 1 + 2*HOST_BITS_PER_WIDE_INT/HOST_BITS_PER_CHAR; /* block */
8475           break;
8476         case dw_val_class_vec:
8477           size += constant_size (a->dw_attr_val.v.val_vec.length
8478                                  * a->dw_attr_val.v.val_vec.elt_size)
8479                   + a->dw_attr_val.v.val_vec.length
8480                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8481           break;
8482         case dw_val_class_flag:
8483           size += 1;
8484           break;
8485         case dw_val_class_die_ref:
8486           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8487              whereas in DWARF3 it's always sized as an offset.  */
8488           if (AT_ref_external (a) && dwarf_version == 2)
8489             size += DWARF2_ADDR_SIZE;
8490           else
8491             size += DWARF_OFFSET_SIZE;
8492           break;
8493         case dw_val_class_fde_ref:
8494           size += DWARF_OFFSET_SIZE;
8495           break;
8496         case dw_val_class_lbl_id:
8497           size += DWARF2_ADDR_SIZE;
8498           break;
8499         case dw_val_class_lineptr:
8500         case dw_val_class_macptr:
8501           size += DWARF_OFFSET_SIZE;
8502           break;
8503         case dw_val_class_str:
8504           if (AT_string_form (a) == DW_FORM_strp)
8505             size += DWARF_OFFSET_SIZE;
8506           else
8507             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8508           break;
8509         case dw_val_class_file:
8510           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8511           break;
8512         default:
8513           gcc_unreachable ();
8514         }
8515     }
8516
8517   return size;
8518 }
8519
8520 /* Size the debugging information associated with a given DIE.  Visits the
8521    DIE's children recursively.  Updates the global variable next_die_offset, on
8522    each time through.  Uses the current value of next_die_offset to update the
8523    die_offset field in each DIE.  */
8524
8525 static void
8526 calc_die_sizes (dw_die_ref die)
8527 {
8528   dw_die_ref c;
8529
8530   die->die_offset = next_die_offset;
8531   next_die_offset += size_of_die (die);
8532
8533   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8534
8535   if (die->die_child != NULL)
8536     /* Count the null byte used to terminate sibling lists.  */
8537     next_die_offset += 1;
8538 }
8539
8540 /* Set the marks for a die and its children.  We do this so
8541    that we know whether or not a reference needs to use FORM_ref_addr; only
8542    DIEs in the same CU will be marked.  We used to clear out the offset
8543    and use that as the flag, but ran into ordering problems.  */
8544
8545 static void
8546 mark_dies (dw_die_ref die)
8547 {
8548   dw_die_ref c;
8549
8550   gcc_assert (!die->die_mark);
8551
8552   die->die_mark = 1;
8553   FOR_EACH_CHILD (die, c, mark_dies (c));
8554 }
8555
8556 /* Clear the marks for a die and its children.  */
8557
8558 static void
8559 unmark_dies (dw_die_ref die)
8560 {
8561   dw_die_ref c;
8562
8563   gcc_assert (die->die_mark);
8564
8565   die->die_mark = 0;
8566   FOR_EACH_CHILD (die, c, unmark_dies (c));
8567 }
8568
8569 /* Clear the marks for a die, its children and referred dies.  */
8570
8571 static void
8572 unmark_all_dies (dw_die_ref die)
8573 {
8574   dw_die_ref c;
8575   dw_attr_ref a;
8576   unsigned ix;
8577
8578   if (!die->die_mark)
8579     return;
8580   die->die_mark = 0;
8581
8582   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8583
8584   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8585     if (AT_class (a) == dw_val_class_die_ref)
8586       unmark_all_dies (AT_ref (a));
8587 }
8588
8589 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8590    generated for the compilation unit.  */
8591
8592 static unsigned long
8593 size_of_pubnames (VEC (pubname_entry, gc) * names)
8594 {
8595   unsigned long size;
8596   unsigned i;
8597   pubname_ref p;
8598
8599   size = DWARF_PUBNAMES_HEADER_SIZE;
8600   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8601     if (names != pubtype_table
8602         || p->die->die_offset != 0
8603         || !flag_eliminate_unused_debug_types)
8604       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8605
8606   size += DWARF_OFFSET_SIZE;
8607   return size;
8608 }
8609
8610 /* Return the size of the information in the .debug_aranges section.  */
8611
8612 static unsigned long
8613 size_of_aranges (void)
8614 {
8615   unsigned long size;
8616
8617   size = DWARF_ARANGES_HEADER_SIZE;
8618
8619   /* Count the address/length pair for this compilation unit.  */
8620   if (text_section_used)
8621     size += 2 * DWARF2_ADDR_SIZE;
8622   if (cold_text_section_used)
8623     size += 2 * DWARF2_ADDR_SIZE;
8624   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8625
8626   /* Count the two zero words used to terminated the address range table.  */
8627   size += 2 * DWARF2_ADDR_SIZE;
8628   return size;
8629 }
8630 \f
8631 /* Select the encoding of an attribute value.  */
8632
8633 static enum dwarf_form
8634 value_format (dw_attr_ref a)
8635 {
8636   switch (a->dw_attr_val.val_class)
8637     {
8638     case dw_val_class_addr:
8639       return DW_FORM_addr;
8640     case dw_val_class_range_list:
8641     case dw_val_class_offset:
8642     case dw_val_class_loc_list:
8643       switch (DWARF_OFFSET_SIZE)
8644         {
8645         case 4:
8646           return DW_FORM_data4;
8647         case 8:
8648           return DW_FORM_data8;
8649         default:
8650           gcc_unreachable ();
8651         }
8652     case dw_val_class_loc:
8653       switch (constant_size (size_of_locs (AT_loc (a))))
8654         {
8655         case 1:
8656           return DW_FORM_block1;
8657         case 2:
8658           return DW_FORM_block2;
8659         default:
8660           gcc_unreachable ();
8661         }
8662     case dw_val_class_const:
8663       return DW_FORM_sdata;
8664     case dw_val_class_unsigned_const:
8665       switch (constant_size (AT_unsigned (a)))
8666         {
8667         case 1:
8668           return DW_FORM_data1;
8669         case 2:
8670           return DW_FORM_data2;
8671         case 4:
8672           return DW_FORM_data4;
8673         case 8:
8674           return DW_FORM_data8;
8675         default:
8676           gcc_unreachable ();
8677         }
8678     case dw_val_class_long_long:
8679       return DW_FORM_block1;
8680     case dw_val_class_vec:
8681       switch (constant_size (a->dw_attr_val.v.val_vec.length
8682                              * a->dw_attr_val.v.val_vec.elt_size))
8683         {
8684         case 1:
8685           return DW_FORM_block1;
8686         case 2:
8687           return DW_FORM_block2;
8688         case 4:
8689           return DW_FORM_block4;
8690         default:
8691           gcc_unreachable ();
8692         }
8693     case dw_val_class_flag:
8694       return DW_FORM_flag;
8695     case dw_val_class_die_ref:
8696       if (AT_ref_external (a))
8697         return DW_FORM_ref_addr;
8698       else
8699         return DW_FORM_ref;
8700     case dw_val_class_fde_ref:
8701       return DW_FORM_data;
8702     case dw_val_class_lbl_id:
8703       return DW_FORM_addr;
8704     case dw_val_class_lineptr:
8705     case dw_val_class_macptr:
8706       return DW_FORM_data;
8707     case dw_val_class_str:
8708       return AT_string_form (a);
8709     case dw_val_class_file:
8710       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8711         {
8712         case 1:
8713           return DW_FORM_data1;
8714         case 2:
8715           return DW_FORM_data2;
8716         case 4:
8717           return DW_FORM_data4;
8718         default:
8719           gcc_unreachable ();
8720         }
8721
8722     default:
8723       gcc_unreachable ();
8724     }
8725 }
8726
8727 /* Output the encoding of an attribute value.  */
8728
8729 static void
8730 output_value_format (dw_attr_ref a)
8731 {
8732   enum dwarf_form form = value_format (a);
8733
8734   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8735 }
8736
8737 /* Output the .debug_abbrev section which defines the DIE abbreviation
8738    table.  */
8739
8740 static void
8741 output_abbrev_section (void)
8742 {
8743   unsigned long abbrev_id;
8744
8745   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8746     {
8747       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8748       unsigned ix;
8749       dw_attr_ref a_attr;
8750
8751       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8752       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8753                                    dwarf_tag_name (abbrev->die_tag));
8754
8755       if (abbrev->die_child != NULL)
8756         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8757       else
8758         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8759
8760       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8761            ix++)
8762         {
8763           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8764                                        dwarf_attr_name (a_attr->dw_attr));
8765           output_value_format (a_attr);
8766         }
8767
8768       dw2_asm_output_data (1, 0, NULL);
8769       dw2_asm_output_data (1, 0, NULL);
8770     }
8771
8772   /* Terminate the table.  */
8773   dw2_asm_output_data (1, 0, NULL);
8774 }
8775
8776 /* Output a symbol we can use to refer to this DIE from another CU.  */
8777
8778 static inline void
8779 output_die_symbol (dw_die_ref die)
8780 {
8781   char *sym = die->die_symbol;
8782
8783   if (sym == 0)
8784     return;
8785
8786   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8787     /* We make these global, not weak; if the target doesn't support
8788        .linkonce, it doesn't support combining the sections, so debugging
8789        will break.  */
8790     targetm.asm_out.globalize_label (asm_out_file, sym);
8791
8792   ASM_OUTPUT_LABEL (asm_out_file, sym);
8793 }
8794
8795 /* Return a new location list, given the begin and end range, and the
8796    expression. gensym tells us whether to generate a new internal symbol for
8797    this location list node, which is done for the head of the list only.  */
8798
8799 static inline dw_loc_list_ref
8800 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8801               const char *section, unsigned int gensym)
8802 {
8803   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8804
8805   retlist->begin = begin;
8806   retlist->end = end;
8807   retlist->expr = expr;
8808   retlist->section = section;
8809   if (gensym)
8810     retlist->ll_symbol = gen_internal_sym ("LLST");
8811
8812   return retlist;
8813 }
8814
8815 /* Add a location description expression to a location list.  */
8816
8817 static inline void
8818 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8819                            const char *begin, const char *end,
8820                            const char *section)
8821 {
8822   dw_loc_list_ref *d;
8823
8824   /* Find the end of the chain.  */
8825   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8826     ;
8827
8828   /* Add a new location list node to the list.  */
8829   *d = new_loc_list (descr, begin, end, section, 0);
8830 }
8831
8832 /* Output the location list given to us.  */
8833
8834 static void
8835 output_loc_list (dw_loc_list_ref list_head)
8836 {
8837   dw_loc_list_ref curr = list_head;
8838
8839   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8840
8841   /* Walk the location list, and output each range + expression.  */
8842   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8843     {
8844       unsigned long size;
8845       /* Don't output an entry that starts and ends at the same address.  */
8846       if (strcmp (curr->begin, curr->end) == 0)
8847         continue;
8848       if (!have_multiple_function_sections)
8849         {
8850           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8851                                 "Location list begin address (%s)",
8852                                 list_head->ll_symbol);
8853           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8854                                 "Location list end address (%s)",
8855                                 list_head->ll_symbol);
8856         }
8857       else
8858         {
8859           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8860                                "Location list begin address (%s)",
8861                                list_head->ll_symbol);
8862           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8863                                "Location list end address (%s)",
8864                                list_head->ll_symbol);
8865         }
8866       size = size_of_locs (curr->expr);
8867
8868       /* Output the block length for this list of location operations.  */
8869       gcc_assert (size <= 0xffff);
8870       dw2_asm_output_data (2, size, "%s", "Location expression size");
8871
8872       output_loc_sequence (curr->expr);
8873     }
8874
8875   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8876                        "Location list terminator begin (%s)",
8877                        list_head->ll_symbol);
8878   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8879                        "Location list terminator end (%s)",
8880                        list_head->ll_symbol);
8881 }
8882
8883 /* Output the DIE and its attributes.  Called recursively to generate
8884    the definitions of each child DIE.  */
8885
8886 static void
8887 output_die (dw_die_ref die)
8888 {
8889   dw_attr_ref a;
8890   dw_die_ref c;
8891   unsigned long size;
8892   unsigned ix;
8893
8894   /* If someone in another CU might refer to us, set up a symbol for
8895      them to point to.  */
8896   if (die->die_symbol)
8897     output_die_symbol (die);
8898
8899   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8900                                (unsigned long)die->die_offset,
8901                                dwarf_tag_name (die->die_tag));
8902
8903   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8904     {
8905       const char *name = dwarf_attr_name (a->dw_attr);
8906
8907       switch (AT_class (a))
8908         {
8909         case dw_val_class_addr:
8910           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8911           break;
8912
8913         case dw_val_class_offset:
8914           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8915                                "%s", name);
8916           break;
8917
8918         case dw_val_class_range_list:
8919           {
8920             char *p = strchr (ranges_section_label, '\0');
8921
8922             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8923                      a->dw_attr_val.v.val_offset);
8924             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8925                                    debug_ranges_section, "%s", name);
8926             *p = '\0';
8927           }
8928           break;
8929
8930         case dw_val_class_loc:
8931           size = size_of_locs (AT_loc (a));
8932
8933           /* Output the block length for this list of location operations.  */
8934           dw2_asm_output_data (constant_size (size), size, "%s", name);
8935
8936           output_loc_sequence (AT_loc (a));
8937           break;
8938
8939         case dw_val_class_const:
8940           /* ??? It would be slightly more efficient to use a scheme like is
8941              used for unsigned constants below, but gdb 4.x does not sign
8942              extend.  Gdb 5.x does sign extend.  */
8943           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8944           break;
8945
8946         case dw_val_class_unsigned_const:
8947           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8948                                AT_unsigned (a), "%s", name);
8949           break;
8950
8951         case dw_val_class_long_long:
8952           {
8953             unsigned HOST_WIDE_INT first, second;
8954
8955             dw2_asm_output_data (1,
8956                                  2 * HOST_BITS_PER_WIDE_INT
8957                                  / HOST_BITS_PER_CHAR,
8958                                  "%s", name);
8959
8960             if (WORDS_BIG_ENDIAN)
8961               {
8962                 first = CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long);
8963                 second = CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long);
8964               }
8965             else
8966               {
8967                 first = CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long);
8968                 second = CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long);
8969               }
8970
8971             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8972                                  first, "long long constant");
8973             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8974                                  second, NULL);
8975           }
8976           break;
8977
8978         case dw_val_class_vec:
8979           {
8980             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8981             unsigned int len = a->dw_attr_val.v.val_vec.length;
8982             unsigned int i;
8983             unsigned char *p;
8984
8985             dw2_asm_output_data (constant_size (len * elt_size),
8986                                  len * elt_size, "%s", name);
8987             if (elt_size > sizeof (HOST_WIDE_INT))
8988               {
8989                 elt_size /= 2;
8990                 len *= 2;
8991               }
8992             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8993                  i < len;
8994                  i++, p += elt_size)
8995               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8996                                    "fp or vector constant word %u", i);
8997             break;
8998           }
8999
9000         case dw_val_class_flag:
9001           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9002           break;
9003
9004         case dw_val_class_loc_list:
9005           {
9006             char *sym = AT_loc_list (a)->ll_symbol;
9007
9008             gcc_assert (sym);
9009             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9010                                    "%s", name);
9011           }
9012           break;
9013
9014         case dw_val_class_die_ref:
9015           if (AT_ref_external (a))
9016             {
9017               char *sym = AT_ref (a)->die_symbol;
9018               int size;
9019
9020               gcc_assert (sym);
9021
9022               /* In DWARF2, DW_FORM_ref_addr is sized by target address
9023                  length, whereas in DWARF3 it's always sized as an offset.  */
9024               if (dwarf_version == 2)
9025                 size = DWARF2_ADDR_SIZE;
9026               else
9027                 size = DWARF_OFFSET_SIZE;
9028               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
9029             }
9030           else
9031             {
9032               gcc_assert (AT_ref (a)->die_offset);
9033               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9034                                    "%s", name);
9035             }
9036           break;
9037
9038         case dw_val_class_fde_ref:
9039           {
9040             char l1[20];
9041
9042             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9043                                          a->dw_attr_val.v.val_fde_index * 2);
9044             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9045                                    "%s", name);
9046           }
9047           break;
9048
9049         case dw_val_class_lbl_id:
9050           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9051           break;
9052
9053         case dw_val_class_lineptr:
9054           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9055                                  debug_line_section, "%s", name);
9056           break;
9057
9058         case dw_val_class_macptr:
9059           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9060                                  debug_macinfo_section, "%s", name);
9061           break;
9062
9063         case dw_val_class_str:
9064           if (AT_string_form (a) == DW_FORM_strp)
9065             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9066                                    a->dw_attr_val.v.val_str->label,
9067                                    debug_str_section,
9068                                    "%s: \"%s\"", name, AT_string (a));
9069           else
9070             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9071           break;
9072
9073         case dw_val_class_file:
9074           {
9075             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9076
9077             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9078                                  a->dw_attr_val.v.val_file->filename);
9079             break;
9080           }
9081
9082         default:
9083           gcc_unreachable ();
9084         }
9085     }
9086
9087   FOR_EACH_CHILD (die, c, output_die (c));
9088
9089   /* Add null byte to terminate sibling list.  */
9090   if (die->die_child != NULL)
9091     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
9092                          (unsigned long) die->die_offset);
9093 }
9094
9095 /* Output the compilation unit that appears at the beginning of the
9096    .debug_info section, and precedes the DIE descriptions.  */
9097
9098 static void
9099 output_compilation_unit_header (void)
9100 {
9101   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9102     dw2_asm_output_data (4, 0xffffffff,
9103       "Initial length escape value indicating 64-bit DWARF extension");
9104   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9105                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9106                        "Length of Compilation Unit Info");
9107   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9108   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9109                          debug_abbrev_section,
9110                          "Offset Into Abbrev. Section");
9111   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9112 }
9113
9114 /* Output the compilation unit DIE and its children.  */
9115
9116 static void
9117 output_comp_unit (dw_die_ref die, int output_if_empty)
9118 {
9119   const char *secname;
9120   char *oldsym, *tmp;
9121
9122   /* Unless we are outputting main CU, we may throw away empty ones.  */
9123   if (!output_if_empty && die->die_child == NULL)
9124     return;
9125
9126   /* Even if there are no children of this DIE, we must output the information
9127      about the compilation unit.  Otherwise, on an empty translation unit, we
9128      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9129      will then complain when examining the file.  First mark all the DIEs in
9130      this CU so we know which get local refs.  */
9131   mark_dies (die);
9132
9133   build_abbrev_table (die);
9134
9135   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9136   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9137   calc_die_sizes (die);
9138
9139   oldsym = die->die_symbol;
9140   if (oldsym)
9141     {
9142       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9143
9144       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9145       secname = tmp;
9146       die->die_symbol = NULL;
9147       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9148     }
9149   else
9150     switch_to_section (debug_info_section);
9151
9152   /* Output debugging information.  */
9153   output_compilation_unit_header ();
9154   output_die (die);
9155
9156   /* Leave the marks on the main CU, so we can check them in
9157      output_pubnames.  */
9158   if (oldsym)
9159     {
9160       unmark_dies (die);
9161       die->die_symbol = oldsym;
9162     }
9163 }
9164
9165 /* Return the DWARF2/3 pubname associated with a decl.  */
9166
9167 static const char *
9168 dwarf2_name (tree decl, int scope)
9169 {
9170   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9171 }
9172
9173 /* Add a new entry to .debug_pubnames if appropriate.  */
9174
9175 static void
9176 add_pubname_string (const char *str, dw_die_ref die)
9177 {
9178   pubname_entry e;
9179
9180   e.die = die;
9181   e.name = xstrdup (str);
9182   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9183 }
9184
9185 static void
9186 add_pubname (tree decl, dw_die_ref die)
9187 {
9188   if (TREE_PUBLIC (decl))
9189     add_pubname_string (dwarf2_name (decl, 1), die);
9190 }
9191
9192 /* Add a new entry to .debug_pubtypes if appropriate.  */
9193
9194 static void
9195 add_pubtype (tree decl, dw_die_ref die)
9196 {
9197   pubname_entry e;
9198
9199   e.name = NULL;
9200   if ((TREE_PUBLIC (decl)
9201        || die->die_parent == comp_unit_die)
9202       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9203     {
9204       e.die = die;
9205       if (TYPE_P (decl))
9206         {
9207           if (TYPE_NAME (decl))
9208             {
9209               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9210                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9211               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9212                        && DECL_NAME (TYPE_NAME (decl)))
9213                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9214               else
9215                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9216             }
9217         }
9218       else
9219         e.name = xstrdup (dwarf2_name (decl, 1));
9220
9221       /* If we don't have a name for the type, there's no point in adding
9222          it to the table.  */
9223       if (e.name && e.name[0] != '\0')
9224         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9225     }
9226 }
9227
9228 /* Output the public names table used to speed up access to externally
9229    visible names; or the public types table used to find type definitions.  */
9230
9231 static void
9232 output_pubnames (VEC (pubname_entry, gc) * names)
9233 {
9234   unsigned i;
9235   unsigned long pubnames_length = size_of_pubnames (names);
9236   pubname_ref pub;
9237
9238   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9239     dw2_asm_output_data (4, 0xffffffff,
9240       "Initial length escape value indicating 64-bit DWARF extension");
9241   if (names == pubname_table)
9242     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9243                          "Length of Public Names Info");
9244   else
9245     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9246                          "Length of Public Type Names Info");
9247   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
9248   dw2_asm_output_data (2, 2, "DWARF Version");
9249   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9250                          debug_info_section,
9251                          "Offset of Compilation Unit Info");
9252   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9253                        "Compilation Unit Length");
9254
9255   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
9256     {
9257       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9258       if (names == pubname_table)
9259         gcc_assert (pub->die->die_mark);
9260
9261       if (names != pubtype_table
9262           || pub->die->die_offset != 0
9263           || !flag_eliminate_unused_debug_types)
9264         {
9265           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9266                                "DIE offset");
9267
9268           dw2_asm_output_nstring (pub->name, -1, "external name");
9269         }
9270     }
9271
9272   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9273 }
9274
9275 /* Add a new entry to .debug_aranges if appropriate.  */
9276
9277 static void
9278 add_arange (tree decl, dw_die_ref die)
9279 {
9280   if (! DECL_SECTION_NAME (decl))
9281     return;
9282
9283   if (arange_table_in_use == arange_table_allocated)
9284     {
9285       arange_table_allocated += ARANGE_TABLE_INCREMENT;
9286       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
9287                                     arange_table_allocated);
9288       memset (arange_table + arange_table_in_use, 0,
9289               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
9290     }
9291
9292   arange_table[arange_table_in_use++] = die;
9293 }
9294
9295 /* Output the information that goes into the .debug_aranges table.
9296    Namely, define the beginning and ending address range of the
9297    text section generated for this compilation unit.  */
9298
9299 static void
9300 output_aranges (void)
9301 {
9302   unsigned i;
9303   unsigned long aranges_length = size_of_aranges ();
9304
9305   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9306     dw2_asm_output_data (4, 0xffffffff,
9307       "Initial length escape value indicating 64-bit DWARF extension");
9308   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9309                        "Length of Address Ranges Info");
9310   /* Version number for aranges is still 2, even in DWARF3.  */
9311   dw2_asm_output_data (2, 2, "DWARF Version");
9312   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9313                          debug_info_section,
9314                          "Offset of Compilation Unit Info");
9315   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9316   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9317
9318   /* We need to align to twice the pointer size here.  */
9319   if (DWARF_ARANGES_PAD_SIZE)
9320     {
9321       /* Pad using a 2 byte words so that padding is correct for any
9322          pointer size.  */
9323       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9324                            2 * DWARF2_ADDR_SIZE);
9325       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9326         dw2_asm_output_data (2, 0, NULL);
9327     }
9328
9329   /* It is necessary not to output these entries if the sections were
9330      not used; if the sections were not used, the length will be 0 and
9331      the address may end up as 0 if the section is discarded by ld
9332      --gc-sections, leaving an invalid (0, 0) entry that can be
9333      confused with the terminator.  */
9334   if (text_section_used)
9335     {
9336       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9337       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9338                             text_section_label, "Length");
9339     }
9340   if (cold_text_section_used)
9341     {
9342       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9343                            "Address");
9344       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9345                             cold_text_section_label, "Length");
9346     }
9347
9348   for (i = 0; i < arange_table_in_use; i++)
9349     {
9350       dw_die_ref die = arange_table[i];
9351
9352       /* We shouldn't see aranges for DIEs outside of the main CU.  */
9353       gcc_assert (die->die_mark);
9354
9355       if (die->die_tag == DW_TAG_subprogram)
9356         {
9357           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
9358                                "Address");
9359           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
9360                                 get_AT_low_pc (die), "Length");
9361         }
9362       else
9363         {
9364           /* A static variable; extract the symbol from DW_AT_location.
9365              Note that this code isn't currently hit, as we only emit
9366              aranges for functions (jason 9/23/99).  */
9367           dw_attr_ref a = get_AT (die, DW_AT_location);
9368           dw_loc_descr_ref loc;
9369
9370           gcc_assert (a && AT_class (a) == dw_val_class_loc);
9371
9372           loc = AT_loc (a);
9373           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
9374
9375           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
9376                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
9377           dw2_asm_output_data (DWARF2_ADDR_SIZE,
9378                                get_AT_unsigned (die, DW_AT_byte_size),
9379                                "Length");
9380         }
9381     }
9382
9383   /* Output the terminator words.  */
9384   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9385   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9386 }
9387
9388 /* Add a new entry to .debug_ranges.  Return the offset at which it
9389    was placed.  */
9390
9391 static unsigned int
9392 add_ranges_num (int num)
9393 {
9394   unsigned int in_use = ranges_table_in_use;
9395
9396   if (in_use == ranges_table_allocated)
9397     {
9398       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9399       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9400                                     ranges_table_allocated);
9401       memset (ranges_table + ranges_table_in_use, 0,
9402               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9403     }
9404
9405   ranges_table[in_use].num = num;
9406   ranges_table_in_use = in_use + 1;
9407
9408   return in_use * 2 * DWARF2_ADDR_SIZE;
9409 }
9410
9411 /* Add a new entry to .debug_ranges corresponding to a block, or a
9412    range terminator if BLOCK is NULL.  */
9413
9414 static unsigned int
9415 add_ranges (const_tree block)
9416 {
9417   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9418 }
9419
9420 /* Add a new entry to .debug_ranges corresponding to a pair of
9421    labels.  */
9422
9423 static unsigned int
9424 add_ranges_by_labels (const char *begin, const char *end)
9425 {
9426   unsigned int in_use = ranges_by_label_in_use;
9427
9428   if (in_use == ranges_by_label_allocated)
9429     {
9430       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9431       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9432                                        ranges_by_label,
9433                                        ranges_by_label_allocated);
9434       memset (ranges_by_label + ranges_by_label_in_use, 0,
9435               RANGES_TABLE_INCREMENT
9436               * sizeof (struct dw_ranges_by_label_struct));
9437     }
9438
9439   ranges_by_label[in_use].begin = begin;
9440   ranges_by_label[in_use].end = end;
9441   ranges_by_label_in_use = in_use + 1;
9442
9443   return add_ranges_num (-(int)in_use - 1);
9444 }
9445
9446 static void
9447 output_ranges (void)
9448 {
9449   unsigned i;
9450   static const char *const start_fmt = "Offset 0x%x";
9451   const char *fmt = start_fmt;
9452
9453   for (i = 0; i < ranges_table_in_use; i++)
9454     {
9455       int block_num = ranges_table[i].num;
9456
9457       if (block_num > 0)
9458         {
9459           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9460           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9461
9462           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9463           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9464
9465           /* If all code is in the text section, then the compilation
9466              unit base address defaults to DW_AT_low_pc, which is the
9467              base of the text section.  */
9468           if (!have_multiple_function_sections)
9469             {
9470               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9471                                     text_section_label,
9472                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9473               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9474                                     text_section_label, NULL);
9475             }
9476
9477           /* Otherwise, the compilation unit base address is zero,
9478              which allows us to use absolute addresses, and not worry
9479              about whether the target supports cross-section
9480              arithmetic.  */
9481           else
9482             {
9483               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9484                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9485               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9486             }
9487
9488           fmt = NULL;
9489         }
9490
9491       /* Negative block_num stands for an index into ranges_by_label.  */
9492       else if (block_num < 0)
9493         {
9494           int lab_idx = - block_num - 1;
9495
9496           if (!have_multiple_function_sections)
9497             {
9498               gcc_unreachable ();
9499 #if 0
9500               /* If we ever use add_ranges_by_labels () for a single
9501                  function section, all we have to do is to take out
9502                  the #if 0 above.  */
9503               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9504                                     ranges_by_label[lab_idx].begin,
9505                                     text_section_label,
9506                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9507               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9508                                     ranges_by_label[lab_idx].end,
9509                                     text_section_label, NULL);
9510 #endif
9511             }
9512           else
9513             {
9514               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9515                                    ranges_by_label[lab_idx].begin,
9516                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9517               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9518                                    ranges_by_label[lab_idx].end,
9519                                    NULL);
9520             }
9521         }
9522       else
9523         {
9524           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9525           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9526           fmt = start_fmt;
9527         }
9528     }
9529 }
9530
9531 /* Data structure containing information about input files.  */
9532 struct file_info
9533 {
9534   const char *path;     /* Complete file name.  */
9535   const char *fname;    /* File name part.  */
9536   int length;           /* Length of entire string.  */
9537   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9538   int dir_idx;          /* Index in directory table.  */
9539 };
9540
9541 /* Data structure containing information about directories with source
9542    files.  */
9543 struct dir_info
9544 {
9545   const char *path;     /* Path including directory name.  */
9546   int length;           /* Path length.  */
9547   int prefix;           /* Index of directory entry which is a prefix.  */
9548   int count;            /* Number of files in this directory.  */
9549   int dir_idx;          /* Index of directory used as base.  */
9550 };
9551
9552 /* Callback function for file_info comparison.  We sort by looking at
9553    the directories in the path.  */
9554
9555 static int
9556 file_info_cmp (const void *p1, const void *p2)
9557 {
9558   const struct file_info *const s1 = (const struct file_info *) p1;
9559   const struct file_info *const s2 = (const struct file_info *) p2;
9560   const unsigned char *cp1;
9561   const unsigned char *cp2;
9562
9563   /* Take care of file names without directories.  We need to make sure that
9564      we return consistent values to qsort since some will get confused if
9565      we return the same value when identical operands are passed in opposite
9566      orders.  So if neither has a directory, return 0 and otherwise return
9567      1 or -1 depending on which one has the directory.  */
9568   if ((s1->path == s1->fname || s2->path == s2->fname))
9569     return (s2->path == s2->fname) - (s1->path == s1->fname);
9570
9571   cp1 = (const unsigned char *) s1->path;
9572   cp2 = (const unsigned char *) s2->path;
9573
9574   while (1)
9575     {
9576       ++cp1;
9577       ++cp2;
9578       /* Reached the end of the first path?  If so, handle like above.  */
9579       if ((cp1 == (const unsigned char *) s1->fname)
9580           || (cp2 == (const unsigned char *) s2->fname))
9581         return ((cp2 == (const unsigned char *) s2->fname)
9582                 - (cp1 == (const unsigned char *) s1->fname));
9583
9584       /* Character of current path component the same?  */
9585       else if (*cp1 != *cp2)
9586         return *cp1 - *cp2;
9587     }
9588 }
9589
9590 struct file_name_acquire_data
9591 {
9592   struct file_info *files;
9593   int used_files;
9594   int max_files;
9595 };
9596
9597 /* Traversal function for the hash table.  */
9598
9599 static int
9600 file_name_acquire (void ** slot, void *data)
9601 {
9602   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9603   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9604   struct file_info *fi;
9605   const char *f;
9606
9607   gcc_assert (fnad->max_files >= d->emitted_number);
9608
9609   if (! d->emitted_number)
9610     return 1;
9611
9612   gcc_assert (fnad->max_files != fnad->used_files);
9613
9614   fi = fnad->files + fnad->used_files++;
9615
9616   /* Skip all leading "./".  */
9617   f = d->filename;
9618   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9619     f += 2;
9620
9621   /* Create a new array entry.  */
9622   fi->path = f;
9623   fi->length = strlen (f);
9624   fi->file_idx = d;
9625
9626   /* Search for the file name part.  */
9627   f = strrchr (f, DIR_SEPARATOR);
9628 #if defined (DIR_SEPARATOR_2)
9629   {
9630     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9631
9632     if (g != NULL)
9633       {
9634         if (f == NULL || f < g)
9635           f = g;
9636       }
9637   }
9638 #endif
9639
9640   fi->fname = f == NULL ? fi->path : f + 1;
9641   return 1;
9642 }
9643
9644 /* Output the directory table and the file name table.  We try to minimize
9645    the total amount of memory needed.  A heuristic is used to avoid large
9646    slowdowns with many input files.  */
9647
9648 static void
9649 output_file_names (void)
9650 {
9651   struct file_name_acquire_data fnad;
9652   int numfiles;
9653   struct file_info *files;
9654   struct dir_info *dirs;
9655   int *saved;
9656   int *savehere;
9657   int *backmap;
9658   int ndirs;
9659   int idx_offset;
9660   int i;
9661   int idx;
9662
9663   if (!last_emitted_file)
9664     {
9665       dw2_asm_output_data (1, 0, "End directory table");
9666       dw2_asm_output_data (1, 0, "End file name table");
9667       return;
9668     }
9669
9670   numfiles = last_emitted_file->emitted_number;
9671
9672   /* Allocate the various arrays we need.  */
9673   files = XALLOCAVEC (struct file_info, numfiles);
9674   dirs = XALLOCAVEC (struct dir_info, numfiles);
9675
9676   fnad.files = files;
9677   fnad.used_files = 0;
9678   fnad.max_files = numfiles;
9679   htab_traverse (file_table, file_name_acquire, &fnad);
9680   gcc_assert (fnad.used_files == fnad.max_files);
9681
9682   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9683
9684   /* Find all the different directories used.  */
9685   dirs[0].path = files[0].path;
9686   dirs[0].length = files[0].fname - files[0].path;
9687   dirs[0].prefix = -1;
9688   dirs[0].count = 1;
9689   dirs[0].dir_idx = 0;
9690   files[0].dir_idx = 0;
9691   ndirs = 1;
9692
9693   for (i = 1; i < numfiles; i++)
9694     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9695         && memcmp (dirs[ndirs - 1].path, files[i].path,
9696                    dirs[ndirs - 1].length) == 0)
9697       {
9698         /* Same directory as last entry.  */
9699         files[i].dir_idx = ndirs - 1;
9700         ++dirs[ndirs - 1].count;
9701       }
9702     else
9703       {
9704         int j;
9705
9706         /* This is a new directory.  */
9707         dirs[ndirs].path = files[i].path;
9708         dirs[ndirs].length = files[i].fname - files[i].path;
9709         dirs[ndirs].count = 1;
9710         dirs[ndirs].dir_idx = ndirs;
9711         files[i].dir_idx = ndirs;
9712
9713         /* Search for a prefix.  */
9714         dirs[ndirs].prefix = -1;
9715         for (j = 0; j < ndirs; j++)
9716           if (dirs[j].length < dirs[ndirs].length
9717               && dirs[j].length > 1
9718               && (dirs[ndirs].prefix == -1
9719                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9720               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9721             dirs[ndirs].prefix = j;
9722
9723         ++ndirs;
9724       }
9725
9726   /* Now to the actual work.  We have to find a subset of the directories which
9727      allow expressing the file name using references to the directory table
9728      with the least amount of characters.  We do not do an exhaustive search
9729      where we would have to check out every combination of every single
9730      possible prefix.  Instead we use a heuristic which provides nearly optimal
9731      results in most cases and never is much off.  */
9732   saved = XALLOCAVEC (int, ndirs);
9733   savehere = XALLOCAVEC (int, ndirs);
9734
9735   memset (saved, '\0', ndirs * sizeof (saved[0]));
9736   for (i = 0; i < ndirs; i++)
9737     {
9738       int j;
9739       int total;
9740
9741       /* We can always save some space for the current directory.  But this
9742          does not mean it will be enough to justify adding the directory.  */
9743       savehere[i] = dirs[i].length;
9744       total = (savehere[i] - saved[i]) * dirs[i].count;
9745
9746       for (j = i + 1; j < ndirs; j++)
9747         {
9748           savehere[j] = 0;
9749           if (saved[j] < dirs[i].length)
9750             {
9751               /* Determine whether the dirs[i] path is a prefix of the
9752                  dirs[j] path.  */
9753               int k;
9754
9755               k = dirs[j].prefix;
9756               while (k != -1 && k != (int) i)
9757                 k = dirs[k].prefix;
9758
9759               if (k == (int) i)
9760                 {
9761                   /* Yes it is.  We can possibly save some memory by
9762                      writing the filenames in dirs[j] relative to
9763                      dirs[i].  */
9764                   savehere[j] = dirs[i].length;
9765                   total += (savehere[j] - saved[j]) * dirs[j].count;
9766                 }
9767             }
9768         }
9769
9770       /* Check whether we can save enough to justify adding the dirs[i]
9771          directory.  */
9772       if (total > dirs[i].length + 1)
9773         {
9774           /* It's worthwhile adding.  */
9775           for (j = i; j < ndirs; j++)
9776             if (savehere[j] > 0)
9777               {
9778                 /* Remember how much we saved for this directory so far.  */
9779                 saved[j] = savehere[j];
9780
9781                 /* Remember the prefix directory.  */
9782                 dirs[j].dir_idx = i;
9783               }
9784         }
9785     }
9786
9787   /* Emit the directory name table.  */
9788   idx = 1;
9789   idx_offset = dirs[0].length > 0 ? 1 : 0;
9790   for (i = 1 - idx_offset; i < ndirs; i++)
9791     dw2_asm_output_nstring (dirs[i].path,
9792                             dirs[i].length
9793                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9794                             "Directory Entry: 0x%x", i + idx_offset);
9795
9796   dw2_asm_output_data (1, 0, "End directory table");
9797
9798   /* We have to emit them in the order of emitted_number since that's
9799      used in the debug info generation.  To do this efficiently we
9800      generate a back-mapping of the indices first.  */
9801   backmap = XALLOCAVEC (int, numfiles);
9802   for (i = 0; i < numfiles; i++)
9803     backmap[files[i].file_idx->emitted_number - 1] = i;
9804
9805   /* Now write all the file names.  */
9806   for (i = 0; i < numfiles; i++)
9807     {
9808       int file_idx = backmap[i];
9809       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9810
9811 #ifdef VMS_DEBUGGING_INFO
9812 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9813
9814       /* Setting these fields can lead to debugger miscomparisons,
9815          but VMS Debug requires them to be set correctly.  */
9816
9817       int ver;
9818       long long cdt;
9819       long siz;
9820       int maxfilelen = strlen (files[file_idx].path)
9821                                + dirs[dir_idx].length
9822                                + MAX_VMS_VERSION_LEN + 1;
9823       char *filebuf = XALLOCAVEC (char, maxfilelen);
9824
9825       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9826       snprintf (filebuf, maxfilelen, "%s;%d",
9827                 files[file_idx].path + dirs[dir_idx].length, ver);
9828
9829       dw2_asm_output_nstring
9830         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
9831
9832       /* Include directory index.  */
9833       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9834
9835       /* Modification time.  */
9836       dw2_asm_output_data_uleb128
9837         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9838           ? cdt : 0,
9839          NULL);
9840
9841       /* File length in bytes.  */
9842       dw2_asm_output_data_uleb128
9843         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9844           ? siz : 0,
9845          NULL);
9846 #else
9847       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9848                               "File Entry: 0x%x", (unsigned) i + 1);
9849
9850       /* Include directory index.  */
9851       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9852
9853       /* Modification time.  */
9854       dw2_asm_output_data_uleb128 (0, NULL);
9855
9856       /* File length in bytes.  */
9857       dw2_asm_output_data_uleb128 (0, NULL);
9858 #endif
9859     }
9860
9861   dw2_asm_output_data (1, 0, "End file name table");
9862 }
9863
9864
9865 /* Output the source line number correspondence information.  This
9866    information goes into the .debug_line section.  */
9867
9868 static void
9869 output_line_info (void)
9870 {
9871   char l1[20], l2[20], p1[20], p2[20];
9872   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9873   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9874   unsigned opc;
9875   unsigned n_op_args;
9876   unsigned long lt_index;
9877   unsigned long current_line;
9878   long line_offset;
9879   long line_delta;
9880   unsigned long current_file;
9881   unsigned long function;
9882
9883   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9884   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9885   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9886   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9887
9888   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9889     dw2_asm_output_data (4, 0xffffffff,
9890       "Initial length escape value indicating 64-bit DWARF extension");
9891   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9892                         "Length of Source Line Info");
9893   ASM_OUTPUT_LABEL (asm_out_file, l1);
9894
9895   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9896   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9897   ASM_OUTPUT_LABEL (asm_out_file, p1);
9898
9899   /* Define the architecture-dependent minimum instruction length (in
9900    bytes).  In this implementation of DWARF, this field is used for
9901    information purposes only.  Since GCC generates assembly language,
9902    we have no a priori knowledge of how many instruction bytes are
9903    generated for each source line, and therefore can use only the
9904    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9905    commands.  Accordingly, we fix this as `1', which is "correct
9906    enough" for all architectures, and don't let the target override.  */
9907   dw2_asm_output_data (1, 1,
9908                        "Minimum Instruction Length");
9909
9910   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9911                        "Default is_stmt_start flag");
9912   dw2_asm_output_data (1, DWARF_LINE_BASE,
9913                        "Line Base Value (Special Opcodes)");
9914   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9915                        "Line Range Value (Special Opcodes)");
9916   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9917                        "Special Opcode Base");
9918
9919   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9920     {
9921       switch (opc)
9922         {
9923         case DW_LNS_advance_pc:
9924         case DW_LNS_advance_line:
9925         case DW_LNS_set_file:
9926         case DW_LNS_set_column:
9927         case DW_LNS_fixed_advance_pc:
9928           n_op_args = 1;
9929           break;
9930         default:
9931           n_op_args = 0;
9932           break;
9933         }
9934
9935       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9936                            opc, n_op_args);
9937     }
9938
9939   /* Write out the information about the files we use.  */
9940   output_file_names ();
9941   ASM_OUTPUT_LABEL (asm_out_file, p2);
9942
9943   /* We used to set the address register to the first location in the text
9944      section here, but that didn't accomplish anything since we already
9945      have a line note for the opening brace of the first function.  */
9946
9947   /* Generate the line number to PC correspondence table, encoded as
9948      a series of state machine operations.  */
9949   current_file = 1;
9950   current_line = 1;
9951
9952   if (cfun && in_cold_section_p)
9953     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9954   else
9955     strcpy (prev_line_label, text_section_label);
9956   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9957     {
9958       dw_line_info_ref line_info = &line_info_table[lt_index];
9959
9960 #if 0
9961       /* Disable this optimization for now; GDB wants to see two line notes
9962          at the beginning of a function so it can find the end of the
9963          prologue.  */
9964
9965       /* Don't emit anything for redundant notes.  Just updating the
9966          address doesn't accomplish anything, because we already assume
9967          that anything after the last address is this line.  */
9968       if (line_info->dw_line_num == current_line
9969           && line_info->dw_file_num == current_file)
9970         continue;
9971 #endif
9972
9973       /* Emit debug info for the address of the current line.
9974
9975          Unfortunately, we have little choice here currently, and must always
9976          use the most general form.  GCC does not know the address delta
9977          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9978          attributes which will give an upper bound on the address range.  We
9979          could perhaps use length attributes to determine when it is safe to
9980          use DW_LNS_fixed_advance_pc.  */
9981
9982       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9983       if (0)
9984         {
9985           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9986           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9987                                "DW_LNS_fixed_advance_pc");
9988           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9989         }
9990       else
9991         {
9992           /* This can handle any delta.  This takes
9993              4+DWARF2_ADDR_SIZE bytes.  */
9994           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9995           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9996           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9997           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9998         }
9999
10000       strcpy (prev_line_label, line_label);
10001
10002       /* Emit debug info for the source file of the current line, if
10003          different from the previous line.  */
10004       if (line_info->dw_file_num != current_file)
10005         {
10006           current_file = line_info->dw_file_num;
10007           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10008           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10009         }
10010
10011       /* Emit debug info for the current line number, choosing the encoding
10012          that uses the least amount of space.  */
10013       if (line_info->dw_line_num != current_line)
10014         {
10015           line_offset = line_info->dw_line_num - current_line;
10016           line_delta = line_offset - DWARF_LINE_BASE;
10017           current_line = line_info->dw_line_num;
10018           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10019             /* This can handle deltas from -10 to 234, using the current
10020                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
10021                takes 1 byte.  */
10022             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10023                                  "line %lu", current_line);
10024           else
10025             {
10026               /* This can handle any delta.  This takes at least 4 bytes,
10027                  depending on the value being encoded.  */
10028               dw2_asm_output_data (1, DW_LNS_advance_line,
10029                                    "advance to line %lu", current_line);
10030               dw2_asm_output_data_sleb128 (line_offset, NULL);
10031               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10032             }
10033         }
10034       else
10035         /* We still need to start a new row, so output a copy insn.  */
10036         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10037     }
10038
10039   /* Emit debug info for the address of the end of the function.  */
10040   if (0)
10041     {
10042       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10043                            "DW_LNS_fixed_advance_pc");
10044       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
10045     }
10046   else
10047     {
10048       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10049       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10050       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10051       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
10052     }
10053
10054   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10055   dw2_asm_output_data_uleb128 (1, NULL);
10056   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10057
10058   function = 0;
10059   current_file = 1;
10060   current_line = 1;
10061   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
10062     {
10063       dw_separate_line_info_ref line_info
10064         = &separate_line_info_table[lt_index];
10065
10066 #if 0
10067       /* Don't emit anything for redundant notes.  */
10068       if (line_info->dw_line_num == current_line
10069           && line_info->dw_file_num == current_file
10070           && line_info->function == function)
10071         goto cont;
10072 #endif
10073
10074       /* Emit debug info for the address of the current line.  If this is
10075          a new function, or the first line of a function, then we need
10076          to handle it differently.  */
10077       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
10078                                    lt_index);
10079       if (function != line_info->function)
10080         {
10081           function = line_info->function;
10082
10083           /* Set the address register to the first line in the function.  */
10084           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10085           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10086           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10087           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10088         }
10089       else
10090         {
10091           /* ??? See the DW_LNS_advance_pc comment above.  */
10092           if (0)
10093             {
10094               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10095                                    "DW_LNS_fixed_advance_pc");
10096               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10097             }
10098           else
10099             {
10100               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10101               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10102               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10103               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10104             }
10105         }
10106
10107       strcpy (prev_line_label, line_label);
10108
10109       /* Emit debug info for the source file of the current line, if
10110          different from the previous line.  */
10111       if (line_info->dw_file_num != current_file)
10112         {
10113           current_file = line_info->dw_file_num;
10114           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10115           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10116         }
10117
10118       /* Emit debug info for the current line number, choosing the encoding
10119          that uses the least amount of space.  */
10120       if (line_info->dw_line_num != current_line)
10121         {
10122           line_offset = line_info->dw_line_num - current_line;
10123           line_delta = line_offset - DWARF_LINE_BASE;
10124           current_line = line_info->dw_line_num;
10125           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10126             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10127                                  "line %lu", current_line);
10128           else
10129             {
10130               dw2_asm_output_data (1, DW_LNS_advance_line,
10131                                    "advance to line %lu", current_line);
10132               dw2_asm_output_data_sleb128 (line_offset, NULL);
10133               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10134             }
10135         }
10136       else
10137         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10138
10139 #if 0
10140     cont:
10141 #endif
10142
10143       lt_index++;
10144
10145       /* If we're done with a function, end its sequence.  */
10146       if (lt_index == separate_line_info_table_in_use
10147           || separate_line_info_table[lt_index].function != function)
10148         {
10149           current_file = 1;
10150           current_line = 1;
10151
10152           /* Emit debug info for the address of the end of the function.  */
10153           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
10154           if (0)
10155             {
10156               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10157                                    "DW_LNS_fixed_advance_pc");
10158               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10159             }
10160           else
10161             {
10162               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10163               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10164               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10165               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10166             }
10167
10168           /* Output the marker for the end of this sequence.  */
10169           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10170           dw2_asm_output_data_uleb128 (1, NULL);
10171           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10172         }
10173     }
10174
10175   /* Output the marker for the end of the line number info.  */
10176   ASM_OUTPUT_LABEL (asm_out_file, l2);
10177 }
10178 \f
10179 /* Given a pointer to a tree node for some base type, return a pointer to
10180    a DIE that describes the given type.
10181
10182    This routine must only be called for GCC type nodes that correspond to
10183    Dwarf base (fundamental) types.  */
10184
10185 static dw_die_ref
10186 base_type_die (tree type)
10187 {
10188   dw_die_ref base_type_result;
10189   enum dwarf_type encoding;
10190
10191   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10192     return 0;
10193
10194   /* If this is a subtype that should not be emitted as a subrange type,
10195      use the base type.  See subrange_type_for_debug_p.  */
10196   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10197     type = TREE_TYPE (type);
10198
10199   switch (TREE_CODE (type))
10200     {
10201     case INTEGER_TYPE:
10202       if (TYPE_STRING_FLAG (type))
10203         {
10204           if (TYPE_UNSIGNED (type))
10205             encoding = DW_ATE_unsigned_char;
10206           else
10207             encoding = DW_ATE_signed_char;
10208         }
10209       else if (TYPE_UNSIGNED (type))
10210         encoding = DW_ATE_unsigned;
10211       else
10212         encoding = DW_ATE_signed;
10213       break;
10214
10215     case REAL_TYPE:
10216       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10217         encoding = DW_ATE_decimal_float;
10218       else
10219         encoding = DW_ATE_float;
10220       break;
10221
10222     case FIXED_POINT_TYPE:
10223       if (TYPE_UNSIGNED (type))
10224         encoding = DW_ATE_unsigned_fixed;
10225       else
10226         encoding = DW_ATE_signed_fixed;
10227       break;
10228
10229       /* Dwarf2 doesn't know anything about complex ints, so use
10230          a user defined type for it.  */
10231     case COMPLEX_TYPE:
10232       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10233         encoding = DW_ATE_complex_float;
10234       else
10235         encoding = DW_ATE_lo_user;
10236       break;
10237
10238     case BOOLEAN_TYPE:
10239       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10240       encoding = DW_ATE_boolean;
10241       break;
10242
10243     default:
10244       /* No other TREE_CODEs are Dwarf fundamental types.  */
10245       gcc_unreachable ();
10246     }
10247
10248   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
10249
10250   /* This probably indicates a bug.  */
10251   if (! TYPE_NAME (type))
10252     add_name_attribute (base_type_result, "__unknown__");
10253
10254   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10255                    int_size_in_bytes (type));
10256   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10257
10258   return base_type_result;
10259 }
10260
10261 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10262    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10263
10264 static inline int
10265 is_base_type (tree type)
10266 {
10267   switch (TREE_CODE (type))
10268     {
10269     case ERROR_MARK:
10270     case VOID_TYPE:
10271     case INTEGER_TYPE:
10272     case REAL_TYPE:
10273     case FIXED_POINT_TYPE:
10274     case COMPLEX_TYPE:
10275     case BOOLEAN_TYPE:
10276       return 1;
10277
10278     case ARRAY_TYPE:
10279     case RECORD_TYPE:
10280     case UNION_TYPE:
10281     case QUAL_UNION_TYPE:
10282     case ENUMERAL_TYPE:
10283     case FUNCTION_TYPE:
10284     case METHOD_TYPE:
10285     case POINTER_TYPE:
10286     case REFERENCE_TYPE:
10287     case OFFSET_TYPE:
10288     case LANG_TYPE:
10289     case VECTOR_TYPE:
10290       return 0;
10291
10292     default:
10293       gcc_unreachable ();
10294     }
10295
10296   return 0;
10297 }
10298
10299 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10300    node, return the size in bits for the type if it is a constant, or else
10301    return the alignment for the type if the type's size is not constant, or
10302    else return BITS_PER_WORD if the type actually turns out to be an
10303    ERROR_MARK node.  */
10304
10305 static inline unsigned HOST_WIDE_INT
10306 simple_type_size_in_bits (const_tree type)
10307 {
10308   if (TREE_CODE (type) == ERROR_MARK)
10309     return BITS_PER_WORD;
10310   else if (TYPE_SIZE (type) == NULL_TREE)
10311     return 0;
10312   else if (host_integerp (TYPE_SIZE (type), 1))
10313     return tree_low_cst (TYPE_SIZE (type), 1);
10314   else
10315     return TYPE_ALIGN (type);
10316 }
10317
10318 /*  Given a pointer to a tree node for a subrange type, return a pointer
10319     to a DIE that describes the given type.  */
10320
10321 static dw_die_ref
10322 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10323 {
10324   dw_die_ref subrange_die;
10325   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10326
10327   if (context_die == NULL)
10328     context_die = comp_unit_die;
10329
10330   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10331
10332   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10333     {
10334       /* The size of the subrange type and its base type do not match,
10335          so we need to generate a size attribute for the subrange type.  */
10336       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10337     }
10338
10339   if (low)
10340     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10341   if (high)
10342     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10343
10344   return subrange_die;
10345 }
10346
10347 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10348    entry that chains various modifiers in front of the given type.  */
10349
10350 static dw_die_ref
10351 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10352                    dw_die_ref context_die)
10353 {
10354   enum tree_code code = TREE_CODE (type);
10355   dw_die_ref mod_type_die;
10356   dw_die_ref sub_die = NULL;
10357   tree item_type = NULL;
10358   tree qualified_type;
10359   tree name, low, high;
10360
10361   if (code == ERROR_MARK)
10362     return NULL;
10363
10364   /* See if we already have the appropriately qualified variant of
10365      this type.  */
10366   qualified_type
10367     = get_qualified_type (type,
10368                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10369                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10370
10371   /* If we do, then we can just use its DIE, if it exists.  */
10372   if (qualified_type)
10373     {
10374       mod_type_die = lookup_type_die (qualified_type);
10375       if (mod_type_die)
10376         return mod_type_die;
10377     }
10378
10379   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10380
10381   /* Handle C typedef types.  */
10382   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
10383     {
10384       tree dtype = TREE_TYPE (name);
10385
10386       if (qualified_type == dtype)
10387         {
10388           /* For a named type, use the typedef.  */
10389           gen_type_die (qualified_type, context_die);
10390           return lookup_type_die (qualified_type);
10391         }
10392       else if (is_const_type < TYPE_READONLY (dtype)
10393                || is_volatile_type < TYPE_VOLATILE (dtype)
10394                || (is_const_type <= TYPE_READONLY (dtype)
10395                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10396                    && DECL_ORIGINAL_TYPE (name) != type))
10397         /* cv-unqualified version of named type.  Just use the unnamed
10398            type to which it refers.  */
10399         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10400                                   is_const_type, is_volatile_type,
10401                                   context_die);
10402       /* Else cv-qualified version of named type; fall through.  */
10403     }
10404
10405   if (is_const_type)
10406     {
10407       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
10408       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10409     }
10410   else if (is_volatile_type)
10411     {
10412       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
10413       sub_die = modified_type_die (type, 0, 0, context_die);
10414     }
10415   else if (code == POINTER_TYPE)
10416     {
10417       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
10418       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10419                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10420       item_type = TREE_TYPE (type);
10421     }
10422   else if (code == REFERENCE_TYPE)
10423     {
10424       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
10425       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10426                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10427       item_type = TREE_TYPE (type);
10428     }
10429   else if (code == INTEGER_TYPE
10430            && TREE_TYPE (type) != NULL_TREE
10431            && subrange_type_for_debug_p (type, &low, &high))
10432     {
10433       mod_type_die = subrange_type_die (type, low, high, context_die);
10434       item_type = TREE_TYPE (type);
10435     }
10436   else if (is_base_type (type))
10437     mod_type_die = base_type_die (type);
10438   else
10439     {
10440       gen_type_die (type, context_die);
10441
10442       /* We have to get the type_main_variant here (and pass that to the
10443          `lookup_type_die' routine) because the ..._TYPE node we have
10444          might simply be a *copy* of some original type node (where the
10445          copy was created to help us keep track of typedef names) and
10446          that copy might have a different TYPE_UID from the original
10447          ..._TYPE node.  */
10448       if (TREE_CODE (type) != VECTOR_TYPE)
10449         return lookup_type_die (type_main_variant (type));
10450       else
10451         /* Vectors have the debugging information in the type,
10452            not the main variant.  */
10453         return lookup_type_die (type);
10454     }
10455
10456   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10457      don't output a DW_TAG_typedef, since there isn't one in the
10458      user's program; just attach a DW_AT_name to the type.  */
10459   if (name
10460       && (TREE_CODE (name) != TYPE_DECL
10461           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
10462     {
10463       if (TREE_CODE (name) == TYPE_DECL)
10464         /* Could just call add_name_and_src_coords_attributes here,
10465            but since this is a builtin type it doesn't have any
10466            useful source coordinates anyway.  */
10467         name = DECL_NAME (name);
10468       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10469     }
10470
10471   if (qualified_type)
10472     equate_type_number_to_die (qualified_type, mod_type_die);
10473
10474   if (item_type)
10475     /* We must do this after the equate_type_number_to_die call, in case
10476        this is a recursive type.  This ensures that the modified_type_die
10477        recursion will terminate even if the type is recursive.  Recursive
10478        types are possible in Ada.  */
10479     sub_die = modified_type_die (item_type,
10480                                  TYPE_READONLY (item_type),
10481                                  TYPE_VOLATILE (item_type),
10482                                  context_die);
10483
10484   if (sub_die != NULL)
10485     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10486
10487   return mod_type_die;
10488 }
10489
10490 /* Generate a new name for the parameter pack name NAME (an
10491    IDENTIFIER_NODE) that incorporates its */
10492
10493 static tree
10494 make_ith_pack_parameter_name (tree name, int i)
10495 {
10496   /* Munge the name to include the parameter index.  */
10497 #define NUMBUF_LEN 128
10498   char numbuf[NUMBUF_LEN];
10499   char* newname;
10500   int newname_len;
10501
10502   snprintf (numbuf, NUMBUF_LEN, "%i", i);
10503   newname_len = IDENTIFIER_LENGTH (name)
10504                 + strlen (numbuf) + 2;
10505   newname = (char*) alloca (newname_len);
10506   snprintf (newname, newname_len,
10507             "%s#%i", IDENTIFIER_POINTER (name), i);
10508   return get_identifier (newname);
10509 }
10510
10511 /* Generate DIEs for the generic parameters of T.
10512    T must be either a generic type or a generic function.
10513    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10514
10515 static void
10516 gen_generic_params_dies (tree t)
10517 {
10518   tree parms, args;
10519   int parms_num, i;
10520   dw_die_ref die = NULL;
10521
10522   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10523     return;
10524
10525   if (TYPE_P (t))
10526     die = lookup_type_die (t);
10527   else if (DECL_P (t))
10528     die = lookup_decl_die (t);
10529
10530   gcc_assert (die);
10531
10532   parms = lang_hooks.get_innermost_generic_parms (t);
10533   if (!parms)
10534     /* T has no generic parameter. It means T is neither a generic type
10535        or function. End of story.  */
10536     return;
10537
10538   parms_num = TREE_VEC_LENGTH (parms);
10539   args = lang_hooks.get_innermost_generic_args (t);
10540   for (i = 0; i < parms_num; i++)
10541     {
10542       tree parm, arg;
10543
10544       parm = TREE_VEC_ELT (parms, i);
10545       arg = TREE_VEC_ELT (args, i);
10546       if (parm && TREE_VALUE (parm) && arg)
10547         {
10548           tree pack_elems =
10549             lang_hooks.types.get_argument_pack_elems (arg);
10550           if (pack_elems)
10551             {
10552               /* So ARG is an argument pack and the elements of that pack
10553                  are stored in PACK_ELEMS.  */
10554               int i, len;
10555
10556               len = TREE_VEC_LENGTH (pack_elems);
10557               for (i = 0; i < len; i++)
10558                 generic_parameter_die (TREE_VALUE (parm),
10559                                        TREE_VEC_ELT (pack_elems, i),
10560                                        die, i);
10561             }
10562           else /* Arg is not an argument pack.  */
10563             generic_parameter_die (TREE_VALUE (parm),
10564                                    arg, die,
10565                                    -1/* Not a param pack.  */);
10566         }
10567     }
10568 }
10569
10570 /* Create and return a DIE for PARM which should be
10571    the representation of a generic type parameter.
10572    For instance, in the C++ front end, PARM would be a template parameter.
10573    ARG is the argument to PARM.
10574    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10575    as a child node.
10576    PACK_ELEM_INDEX is >= 0 if PARM is a generic parameter pack, and if ARG
10577    is one of the unpacked elements of the parameter PACK. In that case,
10578    PACK_ELEM_INDEX is the index of ARG in the parameter pack.  */
10579
10580 static dw_die_ref
10581 generic_parameter_die (tree parm, tree arg, dw_die_ref parent_die,
10582                        int pack_elem_index)
10583 {
10584   dw_die_ref tmpl_die = NULL;
10585   const char *name = NULL;
10586
10587   if (!parm || !DECL_NAME (parm) || !arg)
10588     return NULL;
10589
10590   /* We support non-type generic parameters and arguments,
10591      type generic parameters and arguments, as well as
10592      generic generic parameters (a.k.a. template template parameters in C++)
10593      and arguments.  */
10594   if (TREE_CODE (parm) == PARM_DECL)
10595     /* PARM is a nontype generic parameter  */
10596     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10597   else if (TREE_CODE (parm) == TYPE_DECL)
10598     /* PARM is a type generic parameter.  */
10599     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10600   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10601     /* PARM is a generic generic parameter.
10602        Its DIE is a GNU extension. It shall have a
10603        DW_AT_name attribute to represent the name of the template template
10604        parameter, and a DW_AT_GNU_template_name attribute to represent the
10605        name of the template template argument.  */
10606     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10607                         parent_die, parm);
10608   else
10609     gcc_unreachable ();
10610
10611   if (tmpl_die)
10612     {
10613       tree tmpl_type;
10614
10615       if (pack_elem_index >= 0)
10616         {
10617           /* PARM is an element of a parameter pack.
10618              Generate a name for it.  */
10619           tree identifier = make_ith_pack_parameter_name (DECL_NAME (parm),
10620                                                           pack_elem_index);
10621           if (identifier)
10622             name = IDENTIFIER_POINTER (identifier);
10623         }
10624       else
10625         name = IDENTIFIER_POINTER (DECL_NAME (parm));
10626
10627       gcc_assert (name);
10628       add_AT_string (tmpl_die, DW_AT_name, name);
10629
10630       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10631         {
10632           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10633              TMPL_DIE should have a child DW_AT_type attribute that is set
10634              to the type of the argument to PARM, which is ARG.
10635              If PARM is a type generic parameter, TMPL_DIE should have a
10636              child DW_AT_type that is set to ARG.  */
10637           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10638           add_type_attribute (tmpl_die, tmpl_type, 0,
10639                               TREE_THIS_VOLATILE (tmpl_type),
10640                               parent_die);
10641         }
10642       else
10643         {
10644           /* So TMPL_DIE is a DIE representing a
10645              a generic generic template parameter, a.k.a template template
10646              parameter in C++ and arg is a template.  */
10647
10648           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10649              to the name of the argument.  */
10650           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10651           add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10652         }
10653
10654       if (TREE_CODE (parm) == PARM_DECL)
10655         /* So PARM is a non-type generic parameter.
10656            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10657            attribute of TMPL_DIE which value represents the value
10658            of ARG.
10659            We must be careful here:
10660            The value of ARG might reference some function decls.
10661            We might currently be emitting debug info for a generic
10662            type and types are emitted before function decls, we don't
10663            know if the function decls referenced by ARG will actually be
10664            emitted after cgraph computations.
10665            So must defer the generation of the DW_AT_const_value to
10666            after cgraph is ready.  */
10667         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10668     }
10669
10670   return tmpl_die;
10671 }
10672
10673 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10674    an enumerated type.  */
10675
10676 static inline int
10677 type_is_enum (const_tree type)
10678 {
10679   return TREE_CODE (type) == ENUMERAL_TYPE;
10680 }
10681
10682 /* Return the DBX register number described by a given RTL node.  */
10683
10684 static unsigned int
10685 dbx_reg_number (const_rtx rtl)
10686 {
10687   unsigned regno = REGNO (rtl);
10688
10689   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10690
10691 #ifdef LEAF_REG_REMAP
10692   if (current_function_uses_only_leaf_regs)
10693     {
10694       int leaf_reg = LEAF_REG_REMAP (regno);
10695       if (leaf_reg != -1)
10696         regno = (unsigned) leaf_reg;
10697     }
10698 #endif
10699
10700   return DBX_REGISTER_NUMBER (regno);
10701 }
10702
10703 /* Optionally add a DW_OP_piece term to a location description expression.
10704    DW_OP_piece is only added if the location description expression already
10705    doesn't end with DW_OP_piece.  */
10706
10707 static void
10708 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10709 {
10710   dw_loc_descr_ref loc;
10711
10712   if (*list_head != NULL)
10713     {
10714       /* Find the end of the chain.  */
10715       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10716         ;
10717
10718       if (loc->dw_loc_opc != DW_OP_piece)
10719         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10720     }
10721 }
10722
10723 /* Return a location descriptor that designates a machine register or
10724    zero if there is none.  */
10725
10726 static dw_loc_descr_ref
10727 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10728 {
10729   rtx regs;
10730
10731   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10732     return 0;
10733
10734   regs = targetm.dwarf_register_span (rtl);
10735
10736   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10737     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10738   else
10739     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10740 }
10741
10742 /* Return a location descriptor that designates a machine register for
10743    a given hard register number.  */
10744
10745 static dw_loc_descr_ref
10746 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10747 {
10748   dw_loc_descr_ref reg_loc_descr;
10749
10750   if (regno <= 31)
10751     reg_loc_descr
10752       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10753   else
10754     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10755
10756   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10757     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10758
10759   return reg_loc_descr;
10760 }
10761
10762 /* Given an RTL of a register, return a location descriptor that
10763    designates a value that spans more than one register.  */
10764
10765 static dw_loc_descr_ref
10766 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10767                              enum var_init_status initialized)
10768 {
10769   int nregs, size, i;
10770   unsigned reg;
10771   dw_loc_descr_ref loc_result = NULL;
10772
10773   reg = REGNO (rtl);
10774 #ifdef LEAF_REG_REMAP
10775   if (current_function_uses_only_leaf_regs)
10776     {
10777       int leaf_reg = LEAF_REG_REMAP (reg);
10778       if (leaf_reg != -1)
10779         reg = (unsigned) leaf_reg;
10780     }
10781 #endif
10782   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10783   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10784
10785   /* Simple, contiguous registers.  */
10786   if (regs == NULL_RTX)
10787     {
10788       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10789
10790       loc_result = NULL;
10791       while (nregs--)
10792         {
10793           dw_loc_descr_ref t;
10794
10795           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10796                                       VAR_INIT_STATUS_INITIALIZED);
10797           add_loc_descr (&loc_result, t);
10798           add_loc_descr_op_piece (&loc_result, size);
10799           ++reg;
10800         }
10801       return loc_result;
10802     }
10803
10804   /* Now onto stupid register sets in non contiguous locations.  */
10805
10806   gcc_assert (GET_CODE (regs) == PARALLEL);
10807
10808   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10809   loc_result = NULL;
10810
10811   for (i = 0; i < XVECLEN (regs, 0); ++i)
10812     {
10813       dw_loc_descr_ref t;
10814
10815       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10816                                   VAR_INIT_STATUS_INITIALIZED);
10817       add_loc_descr (&loc_result, t);
10818       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10819       add_loc_descr_op_piece (&loc_result, size);
10820     }
10821
10822   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10823     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10824   return loc_result;
10825 }
10826
10827 #endif /* DWARF2_DEBUGGING_INFO */
10828
10829 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10830
10831 /* Return a location descriptor that designates a constant.  */
10832
10833 static dw_loc_descr_ref
10834 int_loc_descriptor (HOST_WIDE_INT i)
10835 {
10836   enum dwarf_location_atom op;
10837
10838   /* Pick the smallest representation of a constant, rather than just
10839      defaulting to the LEB encoding.  */
10840   if (i >= 0)
10841     {
10842       if (i <= 31)
10843         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10844       else if (i <= 0xff)
10845         op = DW_OP_const1u;
10846       else if (i <= 0xffff)
10847         op = DW_OP_const2u;
10848       else if (HOST_BITS_PER_WIDE_INT == 32
10849                || i <= 0xffffffff)
10850         op = DW_OP_const4u;
10851       else
10852         op = DW_OP_constu;
10853     }
10854   else
10855     {
10856       if (i >= -0x80)
10857         op = DW_OP_const1s;
10858       else if (i >= -0x8000)
10859         op = DW_OP_const2s;
10860       else if (HOST_BITS_PER_WIDE_INT == 32
10861                || i >= -0x80000000)
10862         op = DW_OP_const4s;
10863       else
10864         op = DW_OP_consts;
10865     }
10866
10867   return new_loc_descr (op, i, 0);
10868 }
10869 #endif
10870
10871 #ifdef DWARF2_DEBUGGING_INFO
10872
10873 /* Return a location descriptor that designates a base+offset location.  */
10874
10875 static dw_loc_descr_ref
10876 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10877                  enum var_init_status initialized)
10878 {
10879   unsigned int regno;
10880   dw_loc_descr_ref result;
10881   dw_fde_ref fde = current_fde ();
10882
10883   /* We only use "frame base" when we're sure we're talking about the
10884      post-prologue local stack frame.  We do this by *not* running
10885      register elimination until this point, and recognizing the special
10886      argument pointer and soft frame pointer rtx's.  */
10887   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10888     {
10889       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10890
10891       if (elim != reg)
10892         {
10893           if (GET_CODE (elim) == PLUS)
10894             {
10895               offset += INTVAL (XEXP (elim, 1));
10896               elim = XEXP (elim, 0);
10897             }
10898           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10899                        && (elim == hard_frame_pointer_rtx
10900                            || elim == stack_pointer_rtx))
10901                       || elim == (frame_pointer_needed
10902                                   ? hard_frame_pointer_rtx
10903                                   : stack_pointer_rtx));
10904
10905           /* If drap register is used to align stack, use frame
10906              pointer + offset to access stack variables.  If stack
10907              is aligned without drap, use stack pointer + offset to
10908              access stack variables.  */
10909           if (crtl->stack_realign_tried
10910               && reg == frame_pointer_rtx)
10911             {
10912               int base_reg
10913                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10914                                       ? HARD_FRAME_POINTER_REGNUM
10915                                       : STACK_POINTER_REGNUM);
10916               return new_reg_loc_descr (base_reg, offset);
10917             }
10918
10919           offset += frame_pointer_fb_offset;
10920           return new_loc_descr (DW_OP_fbreg, offset, 0);
10921         }
10922     }
10923   else if (fde
10924            && fde->drap_reg != INVALID_REGNUM
10925            && (fde->drap_reg == REGNO (reg)
10926                || fde->vdrap_reg == REGNO (reg)))
10927     {
10928       /* Use cfa+offset to represent the location of arguments passed
10929          on stack when drap is used to align stack.  */
10930       return new_loc_descr (DW_OP_fbreg, offset, 0);
10931     }
10932
10933   regno = dbx_reg_number (reg);
10934   if (regno <= 31)
10935     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10936                             offset, 0);
10937   else
10938     result = new_loc_descr (DW_OP_bregx, regno, offset);
10939
10940   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10941     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10942
10943   return result;
10944 }
10945
10946 /* Return true if this RTL expression describes a base+offset calculation.  */
10947
10948 static inline int
10949 is_based_loc (const_rtx rtl)
10950 {
10951   return (GET_CODE (rtl) == PLUS
10952           && ((REG_P (XEXP (rtl, 0))
10953                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10954                && CONST_INT_P (XEXP (rtl, 1)))));
10955 }
10956
10957 /* Return a descriptor that describes the concatenation of N locations
10958    used to form the address of a memory location.  */
10959
10960 static dw_loc_descr_ref
10961 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10962                             enum var_init_status initialized)
10963 {
10964   unsigned int i;
10965   dw_loc_descr_ref cc_loc_result = NULL;
10966   unsigned int n = XVECLEN (concatn, 0);
10967
10968   for (i = 0; i < n; ++i)
10969     {
10970       dw_loc_descr_ref ref;
10971       rtx x = XVECEXP (concatn, 0, i);
10972
10973       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10974       if (ref == NULL)
10975         return NULL;
10976
10977       add_loc_descr (&cc_loc_result, ref);
10978       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10979     }
10980
10981   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10982     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10983
10984   return cc_loc_result;
10985 }
10986
10987 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10988    failed.  */
10989
10990 static dw_loc_descr_ref
10991 tls_mem_loc_descriptor (rtx mem)
10992 {
10993   tree base;
10994   dw_loc_descr_ref loc_result;
10995
10996   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10997     return NULL;
10998
10999   base = get_base_address (MEM_EXPR (mem));
11000   if (base == NULL
11001       || TREE_CODE (base) != VAR_DECL
11002       || !DECL_THREAD_LOCAL_P (base))
11003     return NULL;
11004
11005   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
11006   if (loc_result == NULL)
11007     return NULL;
11008
11009   if (INTVAL (MEM_OFFSET (mem)))
11010     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
11011
11012   return loc_result;
11013 }
11014
11015 /* The following routine converts the RTL for a variable or parameter
11016    (resident in memory) into an equivalent Dwarf representation of a
11017    mechanism for getting the address of that same variable onto the top of a
11018    hypothetical "address evaluation" stack.
11019
11020    When creating memory location descriptors, we are effectively transforming
11021    the RTL for a memory-resident object into its Dwarf postfix expression
11022    equivalent.  This routine recursively descends an RTL tree, turning
11023    it into Dwarf postfix code as it goes.
11024
11025    MODE is the mode of the memory reference, needed to handle some
11026    autoincrement addressing modes.
11027
11028    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
11029    location list for RTL.
11030
11031    Return 0 if we can't represent the location.  */
11032
11033 static dw_loc_descr_ref
11034 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11035                     enum var_init_status initialized)
11036 {
11037   dw_loc_descr_ref mem_loc_result = NULL;
11038   enum dwarf_location_atom op;
11039   dw_loc_descr_ref op0, op1;
11040
11041   /* Note that for a dynamically sized array, the location we will generate a
11042      description of here will be the lowest numbered location which is
11043      actually within the array.  That's *not* necessarily the same as the
11044      zeroth element of the array.  */
11045
11046   rtl = targetm.delegitimize_address (rtl);
11047
11048   switch (GET_CODE (rtl))
11049     {
11050     case POST_INC:
11051     case POST_DEC:
11052     case POST_MODIFY:
11053       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
11054          just fall into the SUBREG code.  */
11055
11056       /* ... fall through ...  */
11057
11058     case SUBREG:
11059       /* The case of a subreg may arise when we have a local (register)
11060          variable or a formal (register) parameter which doesn't quite fill
11061          up an entire register.  For now, just assume that it is
11062          legitimate to make the Dwarf info refer to the whole register which
11063          contains the given subreg.  */
11064       rtl = XEXP (rtl, 0);
11065       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
11066         break;
11067
11068       /* ... fall through ...  */
11069
11070     case REG:
11071       /* Whenever a register number forms a part of the description of the
11072          method for calculating the (dynamic) address of a memory resident
11073          object, DWARF rules require the register number be referred to as
11074          a "base register".  This distinction is not based in any way upon
11075          what category of register the hardware believes the given register
11076          belongs to.  This is strictly DWARF terminology we're dealing with
11077          here. Note that in cases where the location of a memory-resident
11078          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11079          OP_CONST (0)) the actual DWARF location descriptor that we generate
11080          may just be OP_BASEREG (basereg).  This may look deceptively like
11081          the object in question was allocated to a register (rather than in
11082          memory) so DWARF consumers need to be aware of the subtle
11083          distinction between OP_REG and OP_BASEREG.  */
11084       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11085         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11086       else if (stack_realign_drap
11087                && crtl->drap_reg
11088                && crtl->args.internal_arg_pointer == rtl
11089                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11090         {
11091           /* If RTL is internal_arg_pointer, which has been optimized
11092              out, use DRAP instead.  */
11093           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11094                                             VAR_INIT_STATUS_INITIALIZED);
11095         }
11096       break;
11097
11098     case SIGN_EXTEND:
11099     case ZERO_EXTEND:
11100       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11101                                 VAR_INIT_STATUS_INITIALIZED);
11102       if (op0 == 0)
11103         break;
11104       else
11105         {
11106           int shift = DWARF2_ADDR_SIZE
11107                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11108           shift *= BITS_PER_UNIT;
11109           if (GET_CODE (rtl) == SIGN_EXTEND)
11110             op = DW_OP_shra;
11111           else
11112             op = DW_OP_shr;
11113           mem_loc_result = op0;
11114           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11115           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11116           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11117           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11118         }
11119       break;
11120
11121     case MEM:
11122       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11123                                            VAR_INIT_STATUS_INITIALIZED);
11124       if (mem_loc_result == NULL)
11125         mem_loc_result = tls_mem_loc_descriptor (rtl);
11126       if (mem_loc_result != 0)
11127         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11128       break;
11129
11130     case LO_SUM:
11131          rtl = XEXP (rtl, 1);
11132
11133       /* ... fall through ...  */
11134
11135     case LABEL_REF:
11136       /* Some ports can transform a symbol ref into a label ref, because
11137          the symbol ref is too far away and has to be dumped into a constant
11138          pool.  */
11139     case CONST:
11140     case SYMBOL_REF:
11141       /* Alternatively, the symbol in the constant pool might be referenced
11142          by a different symbol.  */
11143       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
11144         {
11145           bool marked;
11146           rtx tmp = get_pool_constant_mark (rtl, &marked);
11147
11148           if (GET_CODE (tmp) == SYMBOL_REF)
11149             {
11150               rtl = tmp;
11151               if (CONSTANT_POOL_ADDRESS_P (tmp))
11152                 get_pool_constant_mark (tmp, &marked);
11153               else
11154                 marked = true;
11155             }
11156
11157           /* If all references to this pool constant were optimized away,
11158              it was not output and thus we can't represent it.
11159              FIXME: might try to use DW_OP_const_value here, though
11160              DW_OP_piece complicates it.  */
11161           if (!marked)
11162             return 0;
11163         }
11164
11165       if (GET_CODE (rtl) == SYMBOL_REF
11166           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11167         {
11168           dw_loc_descr_ref temp;
11169
11170           /* If this is not defined, we have no way to emit the data.  */
11171           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11172             break;
11173
11174           temp = new_loc_descr (DW_OP_addr, 0, 0);
11175           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11176           temp->dw_loc_oprnd1.v.val_addr = rtl;
11177           temp->dtprel = true;
11178
11179           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11180           add_loc_descr (&mem_loc_result, temp);
11181
11182           break;
11183         }
11184
11185     symref:
11186       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11187       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11188       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11189       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11190       break;
11191
11192     case PRE_MODIFY:
11193       /* Extract the PLUS expression nested inside and fall into
11194          PLUS code below.  */
11195       rtl = XEXP (rtl, 1);
11196       goto plus;
11197
11198     case PRE_INC:
11199     case PRE_DEC:
11200       /* Turn these into a PLUS expression and fall into the PLUS code
11201          below.  */
11202       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
11203                           GEN_INT (GET_CODE (rtl) == PRE_INC
11204                                    ? GET_MODE_UNIT_SIZE (mode)
11205                                    : -GET_MODE_UNIT_SIZE (mode)));
11206
11207       /* ... fall through ...  */
11208
11209     case PLUS:
11210     plus:
11211       if (is_based_loc (rtl))
11212         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11213                                           INTVAL (XEXP (rtl, 1)),
11214                                           VAR_INIT_STATUS_INITIALIZED);
11215       else
11216         {
11217           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
11218                                                VAR_INIT_STATUS_INITIALIZED);
11219           if (mem_loc_result == 0)
11220             break;
11221
11222           if (CONST_INT_P (XEXP (rtl, 1)))
11223             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11224           else
11225             {
11226               dw_loc_descr_ref mem_loc_result2
11227                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11228                                       VAR_INIT_STATUS_INITIALIZED);
11229               if (mem_loc_result2 == 0)
11230                 break;
11231               add_loc_descr (&mem_loc_result, mem_loc_result2);
11232               add_loc_descr (&mem_loc_result,
11233                              new_loc_descr (DW_OP_plus, 0, 0));
11234             }
11235         }
11236       break;
11237
11238     /* If a pseudo-reg is optimized away, it is possible for it to
11239        be replaced with a MEM containing a multiply or shift.  */
11240     case MINUS:
11241       op = DW_OP_minus;
11242       goto do_binop;
11243
11244     case MULT:
11245       op = DW_OP_mul;
11246       goto do_binop;
11247
11248     case DIV:
11249       op = DW_OP_div;
11250       goto do_binop;
11251
11252     case MOD:
11253       op = DW_OP_mod;
11254       goto do_binop;
11255
11256     case ASHIFT:
11257       op = DW_OP_shl;
11258       goto do_binop;
11259
11260     case ASHIFTRT:
11261       op = DW_OP_shra;
11262       goto do_binop;
11263
11264     case LSHIFTRT:
11265       op = DW_OP_shr;
11266       goto do_binop;
11267
11268     case AND:
11269       op = DW_OP_and;
11270       goto do_binop;
11271
11272     case IOR:
11273       op = DW_OP_or;
11274       goto do_binop;
11275
11276     case XOR:
11277       op = DW_OP_xor;
11278       goto do_binop;
11279
11280     do_binop:
11281       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11282                                 VAR_INIT_STATUS_INITIALIZED);
11283       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11284                                 VAR_INIT_STATUS_INITIALIZED);
11285
11286       if (op0 == 0 || op1 == 0)
11287         break;
11288
11289       mem_loc_result = op0;
11290       add_loc_descr (&mem_loc_result, op1);
11291       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11292       break;
11293
11294     case NOT:
11295       op = DW_OP_not;
11296       goto do_unop;
11297
11298     case ABS:
11299       op = DW_OP_abs;
11300       goto do_unop;
11301
11302     case NEG:
11303       op = DW_OP_neg;
11304       goto do_unop;
11305
11306     do_unop:
11307       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11308                                 VAR_INIT_STATUS_INITIALIZED);
11309
11310       if (op0 == 0)
11311         break;
11312
11313       mem_loc_result = op0;
11314       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11315       break;
11316
11317     case CONST_INT:
11318       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11319       break;
11320
11321     case CONCATN:
11322       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
11323                                                    VAR_INIT_STATUS_INITIALIZED);
11324       break;
11325
11326     case EQ:
11327       op = DW_OP_eq;
11328       goto do_scompare;
11329
11330     case GE:
11331       op = DW_OP_ge;
11332       goto do_scompare;
11333
11334     case GT:
11335       op = DW_OP_gt;
11336       goto do_scompare;
11337
11338     case LE:
11339       op = DW_OP_le;
11340       goto do_scompare;
11341
11342     case LT:
11343       op = DW_OP_lt;
11344       goto do_scompare;
11345
11346     case NE:
11347       op = DW_OP_ne;
11348       goto do_scompare;
11349
11350     do_scompare:
11351       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11352           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11353           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11354         break;
11355
11356       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11357                                 VAR_INIT_STATUS_INITIALIZED);
11358       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11359                                 VAR_INIT_STATUS_INITIALIZED);
11360
11361       if (op0 == 0 || op1 == 0)
11362         break;
11363
11364       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11365         {
11366           int shift = DWARF2_ADDR_SIZE
11367                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11368           shift *= BITS_PER_UNIT;
11369           add_loc_descr (&op0, int_loc_descriptor (shift));
11370           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11371           if (CONST_INT_P (XEXP (rtl, 1)))
11372             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11373           else
11374             {
11375               add_loc_descr (&op1, int_loc_descriptor (shift));
11376               add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11377             }
11378         }
11379
11380     do_compare:
11381       mem_loc_result = op0;
11382       add_loc_descr (&mem_loc_result, op1);
11383       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11384       if (STORE_FLAG_VALUE != 1)
11385         {
11386           add_loc_descr (&mem_loc_result,
11387                          int_loc_descriptor (STORE_FLAG_VALUE));
11388           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11389         }
11390       break;
11391
11392     case GEU:
11393       op = DW_OP_ge;
11394       goto do_ucompare;
11395
11396     case GTU:
11397       op = DW_OP_gt;
11398       goto do_ucompare;
11399
11400     case LEU:
11401       op = DW_OP_le;
11402       goto do_ucompare;
11403
11404     case LTU:
11405       op = DW_OP_lt;
11406       goto do_ucompare;
11407
11408     do_ucompare:
11409       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11410           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11411           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11412         break;
11413
11414       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11415                                 VAR_INIT_STATUS_INITIALIZED);
11416       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11417                                 VAR_INIT_STATUS_INITIALIZED);
11418
11419       if (op0 == 0 || op1 == 0)
11420         break;
11421
11422       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11423         {
11424           HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11425           add_loc_descr (&op0, int_loc_descriptor (mask));
11426           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11427           if (CONST_INT_P (XEXP (rtl, 1)))
11428             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11429           else
11430             {
11431               add_loc_descr (&op1, int_loc_descriptor (mask));
11432               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11433             }
11434         }
11435       else
11436         {
11437           HOST_WIDE_INT bias = 1;
11438           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11439           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11440           if (CONST_INT_P (XEXP (rtl, 1)))
11441             op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11442                                       + INTVAL (XEXP (rtl, 1)));
11443           else
11444             add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11445         }
11446       goto do_compare;
11447
11448     case SMIN:
11449     case SMAX:
11450     case UMIN:
11451     case UMAX:
11452       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11453           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11454           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11455         break;
11456
11457       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11458                                 VAR_INIT_STATUS_INITIALIZED);
11459       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11460                                 VAR_INIT_STATUS_INITIALIZED);
11461
11462       if (op0 == 0 || op1 == 0)
11463         break;
11464
11465       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11466       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11467       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11468       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11469         {
11470           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11471             {
11472               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11473               add_loc_descr (&op0, int_loc_descriptor (mask));
11474               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11475               add_loc_descr (&op1, int_loc_descriptor (mask));
11476               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11477             }
11478           else
11479             {
11480               HOST_WIDE_INT bias = 1;
11481               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11482               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11483               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11484             }
11485         }
11486       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11487         {
11488           int shift = DWARF2_ADDR_SIZE
11489                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11490           shift *= BITS_PER_UNIT;
11491           add_loc_descr (&op0, int_loc_descriptor (shift));
11492           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11493           add_loc_descr (&op1, int_loc_descriptor (shift));
11494           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11495         }
11496
11497       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11498         op = DW_OP_lt;
11499       else
11500         op = DW_OP_gt;
11501       mem_loc_result = op0;
11502       add_loc_descr (&mem_loc_result, op1);
11503       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11504       {
11505         dw_loc_descr_ref bra_node, drop_node;
11506
11507         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11508         add_loc_descr (&mem_loc_result, bra_node);
11509         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11510         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11511         add_loc_descr (&mem_loc_result, drop_node);
11512         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11513         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11514       }
11515       break;
11516
11517     case ZERO_EXTRACT:
11518     case SIGN_EXTRACT:
11519       if (CONST_INT_P (XEXP (rtl, 1))
11520           && CONST_INT_P (XEXP (rtl, 2))
11521           && ((unsigned) INTVAL (XEXP (rtl, 1))
11522               + (unsigned) INTVAL (XEXP (rtl, 2))
11523               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
11524           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
11525           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11526         {
11527           int shift, size;
11528           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11529                                     VAR_INIT_STATUS_INITIALIZED);
11530           if (op0 == 0)
11531             break;
11532           if (GET_CODE (rtl) == SIGN_EXTRACT)
11533             op = DW_OP_shra;
11534           else
11535             op = DW_OP_shr;
11536           mem_loc_result = op0;
11537           size = INTVAL (XEXP (rtl, 1));
11538           shift = INTVAL (XEXP (rtl, 2));
11539           if (BITS_BIG_ENDIAN)
11540             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11541                     - shift - size;
11542           add_loc_descr (&mem_loc_result,
11543                          int_loc_descriptor (DWARF2_ADDR_SIZE - shift - size));
11544           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11545           add_loc_descr (&mem_loc_result,
11546                          int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11547           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11548         }
11549       break;
11550
11551     case COMPARE:
11552     case IF_THEN_ELSE:
11553     case ROTATE:
11554     case ROTATERT:
11555     case TRUNCATE:
11556       /* In theory, we could implement the above.  */
11557       /* DWARF cannot represent the unsigned compare operations
11558          natively.  */
11559     case SS_MULT:
11560     case US_MULT:
11561     case SS_DIV:
11562     case US_DIV:
11563     case UDIV:
11564     case UMOD:
11565     case UNORDERED:
11566     case ORDERED:
11567     case UNEQ:
11568     case UNGE:
11569     case UNLE:
11570     case UNLT:
11571     case LTGT:
11572     case FLOAT_EXTEND:
11573     case FLOAT_TRUNCATE:
11574     case FLOAT:
11575     case UNSIGNED_FLOAT:
11576     case FIX:
11577     case UNSIGNED_FIX:
11578     case FRACT_CONVERT:
11579     case UNSIGNED_FRACT_CONVERT:
11580     case SAT_FRACT:
11581     case UNSIGNED_SAT_FRACT:
11582     case SQRT:
11583     case BSWAP:
11584     case FFS:
11585     case CLZ:
11586     case CTZ:
11587     case POPCOUNT:
11588     case PARITY:
11589     case ASM_OPERANDS:
11590     case UNSPEC:
11591       /* If delegitimize_address couldn't do anything with the UNSPEC, we
11592          can't express it in the debug info.  This can happen e.g. with some
11593          TLS UNSPECs.  */
11594       break;
11595
11596     case CONST_STRING:
11597       rtl = get_debug_string_label (XSTR (rtl, 0));
11598       goto symref;
11599
11600     default:
11601 #ifdef ENABLE_CHECKING
11602       print_rtl (stderr, rtl);
11603       gcc_unreachable ();
11604 #else
11605       break;
11606 #endif
11607     }
11608
11609   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11610     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11611
11612   return mem_loc_result;
11613 }
11614
11615 /* Return a descriptor that describes the concatenation of two locations.
11616    This is typically a complex variable.  */
11617
11618 static dw_loc_descr_ref
11619 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
11620 {
11621   dw_loc_descr_ref cc_loc_result = NULL;
11622   dw_loc_descr_ref x0_ref
11623     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11624   dw_loc_descr_ref x1_ref
11625     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11626
11627   if (x0_ref == 0 || x1_ref == 0)
11628     return 0;
11629
11630   cc_loc_result = x0_ref;
11631   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
11632
11633   add_loc_descr (&cc_loc_result, x1_ref);
11634   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
11635
11636   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11637     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11638
11639   return cc_loc_result;
11640 }
11641
11642 /* Return a descriptor that describes the concatenation of N
11643    locations.  */
11644
11645 static dw_loc_descr_ref
11646 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
11647 {
11648   unsigned int i;
11649   dw_loc_descr_ref cc_loc_result = NULL;
11650   unsigned int n = XVECLEN (concatn, 0);
11651
11652   for (i = 0; i < n; ++i)
11653     {
11654       dw_loc_descr_ref ref;
11655       rtx x = XVECEXP (concatn, 0, i);
11656
11657       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11658       if (ref == NULL)
11659         return NULL;
11660
11661       add_loc_descr (&cc_loc_result, ref);
11662       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
11663     }
11664
11665   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11666     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11667
11668   return cc_loc_result;
11669 }
11670
11671 /* Output a proper Dwarf location descriptor for a variable or parameter
11672    which is either allocated in a register or in a memory location.  For a
11673    register, we just generate an OP_REG and the register number.  For a
11674    memory location we provide a Dwarf postfix expression describing how to
11675    generate the (dynamic) address of the object onto the address stack.
11676
11677    MODE is mode of the decl if this loc_descriptor is going to be used in
11678    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
11679    allowed, VOIDmode otherwise.
11680
11681    If we don't know how to describe it, return 0.  */
11682
11683 static dw_loc_descr_ref
11684 loc_descriptor (rtx rtl, enum machine_mode mode,
11685                 enum var_init_status initialized)
11686 {
11687   dw_loc_descr_ref loc_result = NULL;
11688
11689   switch (GET_CODE (rtl))
11690     {
11691     case SUBREG:
11692       /* The case of a subreg may arise when we have a local (register)
11693          variable or a formal (register) parameter which doesn't quite fill
11694          up an entire register.  For now, just assume that it is
11695          legitimate to make the Dwarf info refer to the whole register which
11696          contains the given subreg.  */
11697       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
11698       break;
11699
11700     case REG:
11701       loc_result = reg_loc_descriptor (rtl, initialized);
11702       break;
11703
11704     case SIGN_EXTEND:
11705     case ZERO_EXTEND:
11706       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
11707       break;
11708
11709     case MEM:
11710       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11711                                        initialized);
11712       if (loc_result == NULL)
11713         loc_result = tls_mem_loc_descriptor (rtl);
11714       break;
11715
11716     case CONCAT:
11717       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
11718                                           initialized);
11719       break;
11720
11721     case CONCATN:
11722       loc_result = concatn_loc_descriptor (rtl, initialized);
11723       break;
11724
11725     case VAR_LOCATION:
11726       /* Single part.  */
11727       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
11728         {
11729           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), mode,
11730                                        initialized);
11731           break;
11732         }
11733
11734       rtl = XEXP (rtl, 1);
11735       /* FALLTHRU */
11736
11737     case PARALLEL:
11738       {
11739         rtvec par_elems = XVEC (rtl, 0);
11740         int num_elem = GET_NUM_ELEM (par_elems);
11741         enum machine_mode mode;
11742         int i;
11743
11744         /* Create the first one, so we have something to add to.  */
11745         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
11746                                      VOIDmode, initialized);
11747         if (loc_result == NULL)
11748           return NULL;
11749         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
11750         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11751         for (i = 1; i < num_elem; i++)
11752           {
11753             dw_loc_descr_ref temp;
11754
11755             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
11756                                    VOIDmode, initialized);
11757             if (temp == NULL)
11758               return NULL;
11759             add_loc_descr (&loc_result, temp);
11760             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
11761             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11762           }
11763       }
11764       break;
11765
11766     case CONST_INT:
11767       if (mode != VOIDmode && mode != BLKmode && dwarf_version >= 4)
11768         {
11769           HOST_WIDE_INT i = INTVAL (rtl);
11770           int litsize;
11771           if (i >= 0)
11772             {
11773               if (i <= 31)
11774                 litsize = 1;
11775               else if (i <= 0xff)
11776                 litsize = 2;
11777               else if (i <= 0xffff)
11778                 litsize = 3;
11779               else if (HOST_BITS_PER_WIDE_INT == 32
11780                        || i <= 0xffffffff)
11781                 litsize = 5;
11782               else
11783                 litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11784             }
11785           else
11786             {
11787               if (i >= -0x80)
11788                 litsize = 2;
11789               else if (i >= -0x8000)
11790                 litsize = 3;
11791               else if (HOST_BITS_PER_WIDE_INT == 32
11792                        || i >= -0x80000000)
11793                 litsize = 5;
11794               else
11795                 litsize = 1 + size_of_sleb128 (i);
11796             }
11797           /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11798              is more compact.  For DW_OP_stack_value we need:
11799              litsize + 1 (DW_OP_stack_value) + 1 (DW_OP_bit_size)
11800              + 1 (mode size)
11801              and for DW_OP_implicit_value:
11802              1 (DW_OP_implicit_value) + 1 (length) + mode_size.  */
11803           if (DWARF2_ADDR_SIZE >= GET_MODE_SIZE (mode)
11804               && litsize + 1 + 1 + 1 < 1 + 1 + GET_MODE_SIZE (mode))
11805             {
11806               loc_result = int_loc_descriptor (i);
11807               add_loc_descr (&loc_result,
11808                              new_loc_descr (DW_OP_stack_value, 0, 0));
11809               add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11810               return loc_result;
11811             }
11812
11813           loc_result = new_loc_descr (DW_OP_implicit_value,
11814                                       GET_MODE_SIZE (mode), 0);
11815           loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11816           loc_result->dw_loc_oprnd2.v.val_int = i;
11817         }
11818       break;
11819
11820     case CONST_DOUBLE:
11821       if (mode != VOIDmode && dwarf_version >= 4)
11822         {
11823           /* Note that a CONST_DOUBLE rtx could represent either an integer
11824              or a floating-point constant.  A CONST_DOUBLE is used whenever
11825              the constant requires more than one word in order to be
11826              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11827           if (GET_MODE (rtl) != VOIDmode)
11828             mode = GET_MODE (rtl);
11829
11830           loc_result = new_loc_descr (DW_OP_implicit_value,
11831                                       GET_MODE_SIZE (mode), 0);
11832           if (SCALAR_FLOAT_MODE_P (mode))
11833             {
11834               unsigned int length = GET_MODE_SIZE (mode);
11835               unsigned char *array = GGC_NEWVEC (unsigned char, length);
11836
11837               insert_float (rtl, array);
11838               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11839               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11840               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11841               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11842             }
11843           else
11844             {
11845               loc_result->dw_loc_oprnd2.val_class = dw_val_class_long_long;
11846               loc_result->dw_loc_oprnd2.v.val_long_long = rtl;
11847             }
11848         }
11849       break;
11850
11851     case CONST_VECTOR:
11852       if (mode != VOIDmode && dwarf_version >= 4)
11853         {
11854           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
11855           unsigned int length = CONST_VECTOR_NUNITS (rtl);
11856           unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11857           unsigned int i;
11858           unsigned char *p;
11859
11860           mode = GET_MODE (rtl);
11861           switch (GET_MODE_CLASS (mode))
11862             {
11863             case MODE_VECTOR_INT:
11864               for (i = 0, p = array; i < length; i++, p += elt_size)
11865                 {
11866                   rtx elt = CONST_VECTOR_ELT (rtl, i);
11867                   HOST_WIDE_INT lo, hi;
11868
11869                   switch (GET_CODE (elt))
11870                     {
11871                     case CONST_INT:
11872                       lo = INTVAL (elt);
11873                       hi = -(lo < 0);
11874                       break;
11875
11876                     case CONST_DOUBLE:
11877                       lo = CONST_DOUBLE_LOW (elt);
11878                       hi = CONST_DOUBLE_HIGH (elt);
11879                       break;
11880
11881                     default:
11882                       gcc_unreachable ();
11883                     }
11884
11885                   if (elt_size <= sizeof (HOST_WIDE_INT))
11886                     insert_int (lo, elt_size, p);
11887                   else
11888                     {
11889                       unsigned char *p0 = p;
11890                       unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11891
11892                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11893                       if (WORDS_BIG_ENDIAN)
11894                         {
11895                           p0 = p1;
11896                           p1 = p;
11897                         }
11898                       insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11899                       insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11900                     }
11901                 }
11902               break;
11903
11904             case MODE_VECTOR_FLOAT:
11905               for (i = 0, p = array; i < length; i++, p += elt_size)
11906                 {
11907                   rtx elt = CONST_VECTOR_ELT (rtl, i);
11908                   insert_float (elt, p);
11909                 }
11910               break;
11911
11912             default:
11913               gcc_unreachable ();
11914             }
11915
11916           loc_result = new_loc_descr (DW_OP_implicit_value,
11917                                       length * elt_size, 0);
11918           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11919           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
11920           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
11921           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11922         }
11923       break;
11924
11925     case CONST:
11926       if (mode == VOIDmode
11927           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
11928           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
11929           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
11930         {
11931           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
11932           break;
11933         }
11934       /* FALLTHROUGH */
11935     case SYMBOL_REF:
11936       if (GET_CODE (rtl) == SYMBOL_REF
11937           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11938         break;
11939     case LABEL_REF:
11940       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
11941           && dwarf_version >= 4)
11942         {
11943           loc_result = new_loc_descr (DW_OP_implicit_value,
11944                                       DWARF2_ADDR_SIZE, 0);
11945           loc_result->dw_loc_oprnd2.val_class = dw_val_class_addr;
11946           loc_result->dw_loc_oprnd2.v.val_addr = rtl;
11947           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11948         }
11949       break;
11950
11951     default:
11952       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
11953           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
11954           && dwarf_version >= 4)
11955         {
11956           /* Value expression.  */
11957           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
11958           if (loc_result)
11959             {
11960               add_loc_descr (&loc_result,
11961                              new_loc_descr (DW_OP_stack_value, 0, 0));
11962               add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11963             }
11964         }
11965       break;
11966     }
11967
11968   return loc_result;
11969 }
11970
11971 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
11972    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
11973    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
11974    top-level invocation, and we require the address of LOC; is 0 if we require
11975    the value of LOC.  */
11976
11977 static dw_loc_descr_ref
11978 loc_descriptor_from_tree_1 (tree loc, int want_address)
11979 {
11980   dw_loc_descr_ref ret, ret1;
11981   int have_address = 0;
11982   enum dwarf_location_atom op;
11983
11984   /* ??? Most of the time we do not take proper care for sign/zero
11985      extending the values properly.  Hopefully this won't be a real
11986      problem...  */
11987
11988   switch (TREE_CODE (loc))
11989     {
11990     case ERROR_MARK:
11991       return 0;
11992
11993     case PLACEHOLDER_EXPR:
11994       /* This case involves extracting fields from an object to determine the
11995          position of other fields.  We don't try to encode this here.  The
11996          only user of this is Ada, which encodes the needed information using
11997          the names of types.  */
11998       return 0;
11999
12000     case CALL_EXPR:
12001       return 0;
12002
12003     case PREINCREMENT_EXPR:
12004     case PREDECREMENT_EXPR:
12005     case POSTINCREMENT_EXPR:
12006     case POSTDECREMENT_EXPR:
12007       /* There are no opcodes for these operations.  */
12008       return 0;
12009
12010     case ADDR_EXPR:
12011       /* If we already want an address, there's nothing we can do.  */
12012       if (want_address)
12013         return 0;
12014
12015       /* Otherwise, process the argument and look for the address.  */
12016       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
12017
12018     case VAR_DECL:
12019       if (DECL_THREAD_LOCAL_P (loc))
12020         {
12021           rtx rtl;
12022           enum dwarf_location_atom first_op;
12023           enum dwarf_location_atom second_op;
12024           bool dtprel = false;
12025
12026           if (targetm.have_tls)
12027             {
12028               /* If this is not defined, we have no way to emit the
12029                  data.  */
12030               if (!targetm.asm_out.output_dwarf_dtprel)
12031                 return 0;
12032
12033                /* The way DW_OP_GNU_push_tls_address is specified, we
12034                   can only look up addresses of objects in the current
12035                   module.  */
12036               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
12037                 return 0;
12038               first_op = DW_OP_addr;
12039               dtprel = true;
12040               second_op = DW_OP_GNU_push_tls_address;
12041             }
12042           else
12043             {
12044               if (!targetm.emutls.debug_form_tls_address)
12045                 return 0;
12046               loc = emutls_decl (loc);
12047               first_op = DW_OP_addr;
12048               second_op = DW_OP_form_tls_address;
12049             }
12050
12051           rtl = rtl_for_decl_location (loc);
12052           if (rtl == NULL_RTX)
12053             return 0;
12054
12055           if (!MEM_P (rtl))
12056             return 0;
12057           rtl = XEXP (rtl, 0);
12058           if (! CONSTANT_P (rtl))
12059             return 0;
12060
12061           ret = new_loc_descr (first_op, 0, 0);
12062           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12063           ret->dw_loc_oprnd1.v.val_addr = rtl;
12064           ret->dtprel = dtprel;
12065
12066           ret1 = new_loc_descr (second_op, 0, 0);
12067           add_loc_descr (&ret, ret1);
12068
12069           have_address = 1;
12070           break;
12071         }
12072       /* FALLTHRU */
12073
12074     case PARM_DECL:
12075       if (DECL_HAS_VALUE_EXPR_P (loc))
12076         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
12077                                            want_address);
12078       /* FALLTHRU */
12079
12080     case RESULT_DECL:
12081     case FUNCTION_DECL:
12082       {
12083         rtx rtl = rtl_for_decl_location (loc);
12084
12085         if (rtl == NULL_RTX)
12086           return 0;
12087         else if (CONST_INT_P (rtl))
12088           {
12089             HOST_WIDE_INT val = INTVAL (rtl);
12090             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
12091               val &= GET_MODE_MASK (DECL_MODE (loc));
12092             ret = int_loc_descriptor (val);
12093           }
12094         else if (GET_CODE (rtl) == CONST_STRING)
12095           return 0;
12096         else if (CONSTANT_P (rtl))
12097           {
12098             ret = new_loc_descr (DW_OP_addr, 0, 0);
12099             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12100             ret->dw_loc_oprnd1.v.val_addr = rtl;
12101           }
12102         else
12103           {
12104             enum machine_mode mode;
12105
12106             /* Certain constructs can only be represented at top-level.  */
12107             if (want_address == 2)
12108               return loc_descriptor (rtl, VOIDmode,
12109                                      VAR_INIT_STATUS_INITIALIZED);
12110
12111             mode = GET_MODE (rtl);
12112             if (MEM_P (rtl))
12113               {
12114                 rtl = XEXP (rtl, 0);
12115                 have_address = 1;
12116               }
12117             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12118           }
12119       }
12120       break;
12121
12122     case INDIRECT_REF:
12123       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12124       have_address = 1;
12125       break;
12126
12127     case COMPOUND_EXPR:
12128       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
12129
12130     CASE_CONVERT:
12131     case VIEW_CONVERT_EXPR:
12132     case SAVE_EXPR:
12133     case MODIFY_EXPR:
12134       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
12135
12136     case COMPONENT_REF:
12137     case BIT_FIELD_REF:
12138     case ARRAY_REF:
12139     case ARRAY_RANGE_REF:
12140       {
12141         tree obj, offset;
12142         HOST_WIDE_INT bitsize, bitpos, bytepos;
12143         enum machine_mode mode;
12144         int volatilep;
12145         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
12146
12147         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
12148                                    &unsignedp, &volatilep, false);
12149
12150         if (obj == loc)
12151           return 0;
12152
12153         ret = loc_descriptor_from_tree_1 (obj, 1);
12154         if (ret == 0
12155             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
12156           return 0;
12157
12158         if (offset != NULL_TREE)
12159           {
12160             /* Variable offset.  */
12161             ret1 = loc_descriptor_from_tree_1 (offset, 0);
12162             if (ret1 == 0)
12163               return 0;
12164             add_loc_descr (&ret, ret1);
12165             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
12166           }
12167
12168         bytepos = bitpos / BITS_PER_UNIT;
12169         loc_descr_plus_const (&ret, bytepos);
12170
12171         have_address = 1;
12172         break;
12173       }
12174
12175     case INTEGER_CST:
12176       if (host_integerp (loc, 0))
12177         ret = int_loc_descriptor (tree_low_cst (loc, 0));
12178       else
12179         return 0;
12180       break;
12181
12182     case CONSTRUCTOR:
12183       {
12184         /* Get an RTL for this, if something has been emitted.  */
12185         rtx rtl = lookup_constant_def (loc);
12186         enum machine_mode mode;
12187
12188         if (!rtl || !MEM_P (rtl))
12189           return 0;
12190         mode = GET_MODE (rtl);
12191         rtl = XEXP (rtl, 0);
12192         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12193         have_address = 1;
12194         break;
12195       }
12196
12197     case TRUTH_AND_EXPR:
12198     case TRUTH_ANDIF_EXPR:
12199     case BIT_AND_EXPR:
12200       op = DW_OP_and;
12201       goto do_binop;
12202
12203     case TRUTH_XOR_EXPR:
12204     case BIT_XOR_EXPR:
12205       op = DW_OP_xor;
12206       goto do_binop;
12207
12208     case TRUTH_OR_EXPR:
12209     case TRUTH_ORIF_EXPR:
12210     case BIT_IOR_EXPR:
12211       op = DW_OP_or;
12212       goto do_binop;
12213
12214     case FLOOR_DIV_EXPR:
12215     case CEIL_DIV_EXPR:
12216     case ROUND_DIV_EXPR:
12217     case TRUNC_DIV_EXPR:
12218       op = DW_OP_div;
12219       goto do_binop;
12220
12221     case MINUS_EXPR:
12222       op = DW_OP_minus;
12223       goto do_binop;
12224
12225     case FLOOR_MOD_EXPR:
12226     case CEIL_MOD_EXPR:
12227     case ROUND_MOD_EXPR:
12228     case TRUNC_MOD_EXPR:
12229       op = DW_OP_mod;
12230       goto do_binop;
12231
12232     case MULT_EXPR:
12233       op = DW_OP_mul;
12234       goto do_binop;
12235
12236     case LSHIFT_EXPR:
12237       op = DW_OP_shl;
12238       goto do_binop;
12239
12240     case RSHIFT_EXPR:
12241       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
12242       goto do_binop;
12243
12244     case POINTER_PLUS_EXPR:
12245     case PLUS_EXPR:
12246       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
12247           && host_integerp (TREE_OPERAND (loc, 1), 0))
12248         {
12249           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12250           if (ret == 0)
12251             return 0;
12252
12253           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
12254           break;
12255         }
12256
12257       op = DW_OP_plus;
12258       goto do_binop;
12259
12260     case LE_EXPR:
12261       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12262         return 0;
12263
12264       op = DW_OP_le;
12265       goto do_binop;
12266
12267     case GE_EXPR:
12268       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12269         return 0;
12270
12271       op = DW_OP_ge;
12272       goto do_binop;
12273
12274     case LT_EXPR:
12275       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12276         return 0;
12277
12278       op = DW_OP_lt;
12279       goto do_binop;
12280
12281     case GT_EXPR:
12282       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12283         return 0;
12284
12285       op = DW_OP_gt;
12286       goto do_binop;
12287
12288     case EQ_EXPR:
12289       op = DW_OP_eq;
12290       goto do_binop;
12291
12292     case NE_EXPR:
12293       op = DW_OP_ne;
12294       goto do_binop;
12295
12296     do_binop:
12297       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12298       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
12299       if (ret == 0 || ret1 == 0)
12300         return 0;
12301
12302       add_loc_descr (&ret, ret1);
12303       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12304       break;
12305
12306     case TRUTH_NOT_EXPR:
12307     case BIT_NOT_EXPR:
12308       op = DW_OP_not;
12309       goto do_unop;
12310
12311     case ABS_EXPR:
12312       op = DW_OP_abs;
12313       goto do_unop;
12314
12315     case NEGATE_EXPR:
12316       op = DW_OP_neg;
12317       goto do_unop;
12318
12319     do_unop:
12320       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12321       if (ret == 0)
12322         return 0;
12323
12324       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12325       break;
12326
12327     case MIN_EXPR:
12328     case MAX_EXPR:
12329       {
12330         const enum tree_code code =
12331           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
12332
12333         loc = build3 (COND_EXPR, TREE_TYPE (loc),
12334                       build2 (code, integer_type_node,
12335                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
12336                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
12337       }
12338
12339       /* ... fall through ...  */
12340
12341     case COND_EXPR:
12342       {
12343         dw_loc_descr_ref lhs
12344           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
12345         dw_loc_descr_ref rhs
12346           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
12347         dw_loc_descr_ref bra_node, jump_node, tmp;
12348
12349         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12350         if (ret == 0 || lhs == 0 || rhs == 0)
12351           return 0;
12352
12353         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12354         add_loc_descr (&ret, bra_node);
12355
12356         add_loc_descr (&ret, rhs);
12357         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
12358         add_loc_descr (&ret, jump_node);
12359
12360         add_loc_descr (&ret, lhs);
12361         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12362         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
12363
12364         /* ??? Need a node to point the skip at.  Use a nop.  */
12365         tmp = new_loc_descr (DW_OP_nop, 0, 0);
12366         add_loc_descr (&ret, tmp);
12367         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12368         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
12369       }
12370       break;
12371
12372     case FIX_TRUNC_EXPR:
12373       return 0;
12374
12375     default:
12376       /* Leave front-end specific codes as simply unknown.  This comes
12377          up, for instance, with the C STMT_EXPR.  */
12378       if ((unsigned int) TREE_CODE (loc)
12379           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
12380         return 0;
12381
12382 #ifdef ENABLE_CHECKING
12383       /* Otherwise this is a generic code; we should just lists all of
12384          these explicitly.  We forgot one.  */
12385       gcc_unreachable ();
12386 #else
12387       /* In a release build, we want to degrade gracefully: better to
12388          generate incomplete debugging information than to crash.  */
12389       return NULL;
12390 #endif
12391     }
12392
12393   /* Show if we can't fill the request for an address.  */
12394   if (want_address && !have_address)
12395     return 0;
12396
12397   /* If we've got an address and don't want one, dereference.  */
12398   if (!want_address && have_address && ret)
12399     {
12400       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12401
12402       if (size > DWARF2_ADDR_SIZE || size == -1)
12403         return 0;
12404       else if (size == DWARF2_ADDR_SIZE)
12405         op = DW_OP_deref;
12406       else
12407         op = DW_OP_deref_size;
12408
12409       add_loc_descr (&ret, new_loc_descr (op, size, 0));
12410     }
12411
12412   return ret;
12413 }
12414
12415 static inline dw_loc_descr_ref
12416 loc_descriptor_from_tree (tree loc)
12417 {
12418   return loc_descriptor_from_tree_1 (loc, 2);
12419 }
12420
12421 /* Given a value, round it up to the lowest multiple of `boundary'
12422    which is not less than the value itself.  */
12423
12424 static inline HOST_WIDE_INT
12425 ceiling (HOST_WIDE_INT value, unsigned int boundary)
12426 {
12427   return (((value + boundary - 1) / boundary) * boundary);
12428 }
12429
12430 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
12431    pointer to the declared type for the relevant field variable, or return
12432    `integer_type_node' if the given node turns out to be an
12433    ERROR_MARK node.  */
12434
12435 static inline tree
12436 field_type (const_tree decl)
12437 {
12438   tree type;
12439
12440   if (TREE_CODE (decl) == ERROR_MARK)
12441     return integer_type_node;
12442
12443   type = DECL_BIT_FIELD_TYPE (decl);
12444   if (type == NULL_TREE)
12445     type = TREE_TYPE (decl);
12446
12447   return type;
12448 }
12449
12450 /* Given a pointer to a tree node, return the alignment in bits for
12451    it, or else return BITS_PER_WORD if the node actually turns out to
12452    be an ERROR_MARK node.  */
12453
12454 static inline unsigned
12455 simple_type_align_in_bits (const_tree type)
12456 {
12457   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
12458 }
12459
12460 static inline unsigned
12461 simple_decl_align_in_bits (const_tree decl)
12462 {
12463   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
12464 }
12465
12466 /* Return the result of rounding T up to ALIGN.  */
12467
12468 static inline HOST_WIDE_INT
12469 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
12470 {
12471   /* We must be careful if T is negative because HOST_WIDE_INT can be
12472      either "above" or "below" unsigned int as per the C promotion
12473      rules, depending on the host, thus making the signedness of the
12474      direct multiplication and division unpredictable.  */
12475   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
12476
12477   u += align - 1;
12478   u /= align;
12479   u *= align;
12480
12481   return (HOST_WIDE_INT) u;
12482 }
12483
12484 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
12485    lowest addressed byte of the "containing object" for the given FIELD_DECL,
12486    or return 0 if we are unable to determine what that offset is, either
12487    because the argument turns out to be a pointer to an ERROR_MARK node, or
12488    because the offset is actually variable.  (We can't handle the latter case
12489    just yet).  */
12490
12491 static HOST_WIDE_INT
12492 field_byte_offset (const_tree decl)
12493 {
12494   HOST_WIDE_INT object_offset_in_bits;
12495   HOST_WIDE_INT bitpos_int;
12496
12497   if (TREE_CODE (decl) == ERROR_MARK)
12498     return 0;
12499
12500   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
12501
12502   /* We cannot yet cope with fields whose positions are variable, so
12503      for now, when we see such things, we simply return 0.  Someday, we may
12504      be able to handle such cases, but it will be damn difficult.  */
12505   if (! host_integerp (bit_position (decl), 0))
12506     return 0;
12507
12508   bitpos_int = int_bit_position (decl);
12509
12510 #ifdef PCC_BITFIELD_TYPE_MATTERS
12511   if (PCC_BITFIELD_TYPE_MATTERS)
12512     {
12513       tree type;
12514       tree field_size_tree;
12515       HOST_WIDE_INT deepest_bitpos;
12516       unsigned HOST_WIDE_INT field_size_in_bits;
12517       unsigned int type_align_in_bits;
12518       unsigned int decl_align_in_bits;
12519       unsigned HOST_WIDE_INT type_size_in_bits;
12520
12521       type = field_type (decl);
12522       type_size_in_bits = simple_type_size_in_bits (type);
12523       type_align_in_bits = simple_type_align_in_bits (type);
12524
12525       field_size_tree = DECL_SIZE (decl);
12526
12527       /* The size could be unspecified if there was an error, or for
12528          a flexible array member.  */
12529       if (!field_size_tree)
12530         field_size_tree = bitsize_zero_node;
12531
12532       /* If the size of the field is not constant, use the type size.  */
12533       if (host_integerp (field_size_tree, 1))
12534         field_size_in_bits = tree_low_cst (field_size_tree, 1);
12535       else
12536         field_size_in_bits = type_size_in_bits;
12537
12538       decl_align_in_bits = simple_decl_align_in_bits (decl);
12539
12540       /* The GCC front-end doesn't make any attempt to keep track of the
12541          starting bit offset (relative to the start of the containing
12542          structure type) of the hypothetical "containing object" for a
12543          bit-field.  Thus, when computing the byte offset value for the
12544          start of the "containing object" of a bit-field, we must deduce
12545          this information on our own. This can be rather tricky to do in
12546          some cases.  For example, handling the following structure type
12547          definition when compiling for an i386/i486 target (which only
12548          aligns long long's to 32-bit boundaries) can be very tricky:
12549
12550          struct S { int field1; long long field2:31; };
12551
12552          Fortunately, there is a simple rule-of-thumb which can be used
12553          in such cases.  When compiling for an i386/i486, GCC will
12554          allocate 8 bytes for the structure shown above.  It decides to
12555          do this based upon one simple rule for bit-field allocation.
12556          GCC allocates each "containing object" for each bit-field at
12557          the first (i.e. lowest addressed) legitimate alignment boundary
12558          (based upon the required minimum alignment for the declared
12559          type of the field) which it can possibly use, subject to the
12560          condition that there is still enough available space remaining
12561          in the containing object (when allocated at the selected point)
12562          to fully accommodate all of the bits of the bit-field itself.
12563
12564          This simple rule makes it obvious why GCC allocates 8 bytes for
12565          each object of the structure type shown above.  When looking
12566          for a place to allocate the "containing object" for `field2',
12567          the compiler simply tries to allocate a 64-bit "containing
12568          object" at each successive 32-bit boundary (starting at zero)
12569          until it finds a place to allocate that 64- bit field such that
12570          at least 31 contiguous (and previously unallocated) bits remain
12571          within that selected 64 bit field.  (As it turns out, for the
12572          example above, the compiler finds it is OK to allocate the
12573          "containing object" 64-bit field at bit-offset zero within the
12574          structure type.)
12575
12576          Here we attempt to work backwards from the limited set of facts
12577          we're given, and we try to deduce from those facts, where GCC
12578          must have believed that the containing object started (within
12579          the structure type). The value we deduce is then used (by the
12580          callers of this routine) to generate DW_AT_location and
12581          DW_AT_bit_offset attributes for fields (both bit-fields and, in
12582          the case of DW_AT_location, regular fields as well).  */
12583
12584       /* Figure out the bit-distance from the start of the structure to
12585          the "deepest" bit of the bit-field.  */
12586       deepest_bitpos = bitpos_int + field_size_in_bits;
12587
12588       /* This is the tricky part.  Use some fancy footwork to deduce
12589          where the lowest addressed bit of the containing object must
12590          be.  */
12591       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
12592
12593       /* Round up to type_align by default.  This works best for
12594          bitfields.  */
12595       object_offset_in_bits
12596         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
12597
12598       if (object_offset_in_bits > bitpos_int)
12599         {
12600           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
12601
12602           /* Round up to decl_align instead.  */
12603           object_offset_in_bits
12604             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
12605         }
12606     }
12607   else
12608 #endif
12609     object_offset_in_bits = bitpos_int;
12610
12611   return object_offset_in_bits / BITS_PER_UNIT;
12612 }
12613 \f
12614 /* The following routines define various Dwarf attributes and any data
12615    associated with them.  */
12616
12617 /* Add a location description attribute value to a DIE.
12618
12619    This emits location attributes suitable for whole variables and
12620    whole parameters.  Note that the location attributes for struct fields are
12621    generated by the routine `data_member_location_attribute' below.  */
12622
12623 static inline void
12624 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
12625                              dw_loc_descr_ref descr)
12626 {
12627   if (descr != 0)
12628     add_AT_loc (die, attr_kind, descr);
12629 }
12630
12631 /* Attach the specialized form of location attribute used for data members of
12632    struct and union types.  In the special case of a FIELD_DECL node which
12633    represents a bit-field, the "offset" part of this special location
12634    descriptor must indicate the distance in bytes from the lowest-addressed
12635    byte of the containing struct or union type to the lowest-addressed byte of
12636    the "containing object" for the bit-field.  (See the `field_byte_offset'
12637    function above).
12638
12639    For any given bit-field, the "containing object" is a hypothetical object
12640    (of some integral or enum type) within which the given bit-field lives.  The
12641    type of this hypothetical "containing object" is always the same as the
12642    declared type of the individual bit-field itself (for GCC anyway... the
12643    DWARF spec doesn't actually mandate this).  Note that it is the size (in
12644    bytes) of the hypothetical "containing object" which will be given in the
12645    DW_AT_byte_size attribute for this bit-field.  (See the
12646    `byte_size_attribute' function below.)  It is also used when calculating the
12647    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
12648    function below.)  */
12649
12650 static void
12651 add_data_member_location_attribute (dw_die_ref die, tree decl)
12652 {
12653   HOST_WIDE_INT offset;
12654   dw_loc_descr_ref loc_descr = 0;
12655
12656   if (TREE_CODE (decl) == TREE_BINFO)
12657     {
12658       /* We're working on the TAG_inheritance for a base class.  */
12659       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
12660         {
12661           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
12662              aren't at a fixed offset from all (sub)objects of the same
12663              type.  We need to extract the appropriate offset from our
12664              vtable.  The following dwarf expression means
12665
12666                BaseAddr = ObAddr + *((*ObAddr) - Offset)
12667
12668              This is specific to the V3 ABI, of course.  */
12669
12670           dw_loc_descr_ref tmp;
12671
12672           /* Make a copy of the object address.  */
12673           tmp = new_loc_descr (DW_OP_dup, 0, 0);
12674           add_loc_descr (&loc_descr, tmp);
12675
12676           /* Extract the vtable address.  */
12677           tmp = new_loc_descr (DW_OP_deref, 0, 0);
12678           add_loc_descr (&loc_descr, tmp);
12679
12680           /* Calculate the address of the offset.  */
12681           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
12682           gcc_assert (offset < 0);
12683
12684           tmp = int_loc_descriptor (-offset);
12685           add_loc_descr (&loc_descr, tmp);
12686           tmp = new_loc_descr (DW_OP_minus, 0, 0);
12687           add_loc_descr (&loc_descr, tmp);
12688
12689           /* Extract the offset.  */
12690           tmp = new_loc_descr (DW_OP_deref, 0, 0);
12691           add_loc_descr (&loc_descr, tmp);
12692
12693           /* Add it to the object address.  */
12694           tmp = new_loc_descr (DW_OP_plus, 0, 0);
12695           add_loc_descr (&loc_descr, tmp);
12696         }
12697       else
12698         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
12699     }
12700   else
12701     offset = field_byte_offset (decl);
12702
12703   if (! loc_descr)
12704     {
12705       if (dwarf_version > 2)
12706         {
12707           /* Don't need to output a location expression, just the constant. */
12708           add_AT_int (die, DW_AT_data_member_location, offset);
12709           return;
12710         }
12711       else
12712         {
12713           enum dwarf_location_atom op;
12714           
12715           /* The DWARF2 standard says that we should assume that the structure
12716              address is already on the stack, so we can specify a structure
12717              field address by using DW_OP_plus_uconst.  */
12718           
12719 #ifdef MIPS_DEBUGGING_INFO
12720           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
12721              operator correctly.  It works only if we leave the offset on the
12722              stack.  */
12723           op = DW_OP_constu;
12724 #else
12725           op = DW_OP_plus_uconst;
12726 #endif
12727           
12728           loc_descr = new_loc_descr (op, offset, 0);
12729         }
12730     }
12731
12732   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
12733 }
12734
12735 /* Writes integer values to dw_vec_const array.  */
12736
12737 static void
12738 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
12739 {
12740   while (size != 0)
12741     {
12742       *dest++ = val & 0xff;
12743       val >>= 8;
12744       --size;
12745     }
12746 }
12747
12748 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
12749
12750 static HOST_WIDE_INT
12751 extract_int (const unsigned char *src, unsigned int size)
12752 {
12753   HOST_WIDE_INT val = 0;
12754
12755   src += size;
12756   while (size != 0)
12757     {
12758       val <<= 8;
12759       val |= *--src & 0xff;
12760       --size;
12761     }
12762   return val;
12763 }
12764
12765 /* Writes floating point values to dw_vec_const array.  */
12766
12767 static void
12768 insert_float (const_rtx rtl, unsigned char *array)
12769 {
12770   REAL_VALUE_TYPE rv;
12771   long val[4];
12772   int i;
12773
12774   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
12775   real_to_target (val, &rv, GET_MODE (rtl));
12776
12777   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
12778   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
12779     {
12780       insert_int (val[i], 4, array);
12781       array += 4;
12782     }
12783 }
12784
12785 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
12786    does not have a "location" either in memory or in a register.  These
12787    things can arise in GNU C when a constant is passed as an actual parameter
12788    to an inlined function.  They can also arise in C++ where declared
12789    constants do not necessarily get memory "homes".  */
12790
12791 static void
12792 add_const_value_attribute (dw_die_ref die, rtx rtl)
12793 {
12794   switch (GET_CODE (rtl))
12795     {
12796     case CONST_INT:
12797       {
12798         HOST_WIDE_INT val = INTVAL (rtl);
12799
12800         if (val < 0)
12801           add_AT_int (die, DW_AT_const_value, val);
12802         else
12803           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
12804       }
12805       break;
12806
12807     case CONST_DOUBLE:
12808       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
12809          floating-point constant.  A CONST_DOUBLE is used whenever the
12810          constant requires more than one word in order to be adequately
12811          represented.  We output CONST_DOUBLEs as blocks.  */
12812       {
12813         enum machine_mode mode = GET_MODE (rtl);
12814
12815         if (SCALAR_FLOAT_MODE_P (mode))
12816           {
12817             unsigned int length = GET_MODE_SIZE (mode);
12818             unsigned char *array = GGC_NEWVEC (unsigned char, length);
12819
12820             insert_float (rtl, array);
12821             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
12822           }
12823         else
12824           add_AT_long_long (die, DW_AT_const_value, rtl);
12825       }
12826       break;
12827
12828     case CONST_VECTOR:
12829       {
12830         enum machine_mode mode = GET_MODE (rtl);
12831         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
12832         unsigned int length = CONST_VECTOR_NUNITS (rtl);
12833         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
12834         unsigned int i;
12835         unsigned char *p;
12836
12837         switch (GET_MODE_CLASS (mode))
12838           {
12839           case MODE_VECTOR_INT:
12840             for (i = 0, p = array; i < length; i++, p += elt_size)
12841               {
12842                 rtx elt = CONST_VECTOR_ELT (rtl, i);
12843                 HOST_WIDE_INT lo, hi;
12844
12845                 switch (GET_CODE (elt))
12846                   {
12847                   case CONST_INT:
12848                     lo = INTVAL (elt);
12849                     hi = -(lo < 0);
12850                     break;
12851
12852                   case CONST_DOUBLE:
12853                     lo = CONST_DOUBLE_LOW (elt);
12854                     hi = CONST_DOUBLE_HIGH (elt);
12855                     break;
12856
12857                   default:
12858                     gcc_unreachable ();
12859                   }
12860
12861                 if (elt_size <= sizeof (HOST_WIDE_INT))
12862                   insert_int (lo, elt_size, p);
12863                 else
12864                   {
12865                     unsigned char *p0 = p;
12866                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
12867
12868                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12869                     if (WORDS_BIG_ENDIAN)
12870                       {
12871                         p0 = p1;
12872                         p1 = p;
12873                       }
12874                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
12875                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
12876                   }
12877               }
12878             break;
12879
12880           case MODE_VECTOR_FLOAT:
12881             for (i = 0, p = array; i < length; i++, p += elt_size)
12882               {
12883                 rtx elt = CONST_VECTOR_ELT (rtl, i);
12884                 insert_float (elt, p);
12885               }
12886             break;
12887
12888           default:
12889             gcc_unreachable ();
12890           }
12891
12892         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
12893       }
12894       break;
12895
12896     case CONST_STRING:
12897       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
12898       break;
12899
12900     case CONST:
12901       if (CONSTANT_P (XEXP (rtl, 0)))
12902         {
12903           add_const_value_attribute (die, XEXP (rtl, 0));
12904           return;
12905         }
12906       /* FALLTHROUGH */
12907     case SYMBOL_REF:
12908       if (GET_CODE (rtl) == SYMBOL_REF
12909           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12910         break;
12911     case LABEL_REF:
12912       add_AT_addr (die, DW_AT_const_value, rtl);
12913       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12914       break;
12915
12916     case PLUS:
12917       /* In cases where an inlined instance of an inline function is passed
12918          the address of an `auto' variable (which is local to the caller) we
12919          can get a situation where the DECL_RTL of the artificial local
12920          variable (for the inlining) which acts as a stand-in for the
12921          corresponding formal parameter (of the inline function) will look
12922          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
12923          exactly a compile-time constant expression, but it isn't the address
12924          of the (artificial) local variable either.  Rather, it represents the
12925          *value* which the artificial local variable always has during its
12926          lifetime.  We currently have no way to represent such quasi-constant
12927          values in Dwarf, so for now we just punt and generate nothing.  */
12928       break;
12929
12930     default:
12931       /* No other kinds of rtx should be possible here.  */
12932       gcc_unreachable ();
12933     }
12934
12935 }
12936
12937 /* Determine whether the evaluation of EXPR references any variables
12938    or functions which aren't otherwise used (and therefore may not be
12939    output).  */
12940 static tree
12941 reference_to_unused (tree * tp, int * walk_subtrees,
12942                      void * data ATTRIBUTE_UNUSED)
12943 {
12944   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
12945     *walk_subtrees = 0;
12946
12947   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
12948       && ! TREE_ASM_WRITTEN (*tp))
12949     return *tp;
12950   /* ???  The C++ FE emits debug information for using decls, so
12951      putting gcc_unreachable here falls over.  See PR31899.  For now
12952      be conservative.  */
12953   else if (!cgraph_global_info_ready
12954            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
12955     return *tp;
12956   else if (TREE_CODE (*tp) == VAR_DECL)
12957     {
12958       struct varpool_node *node = varpool_node (*tp);
12959       if (!node->needed)
12960         return *tp;
12961     }
12962   else if (TREE_CODE (*tp) == FUNCTION_DECL
12963            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
12964     {
12965       /* The call graph machinery must have finished analyzing,
12966          optimizing and gimplifying the CU by now.
12967          So if *TP has no call graph node associated
12968          to it, it means *TP will not be emitted.  */
12969       if (!cgraph_get_node (*tp))
12970         return *tp;
12971     }
12972   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
12973     return *tp;
12974
12975   return NULL_TREE;
12976 }
12977
12978 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
12979    for use in a later add_const_value_attribute call.  */
12980
12981 static rtx
12982 rtl_for_decl_init (tree init, tree type)
12983 {
12984   rtx rtl = NULL_RTX;
12985
12986   /* If a variable is initialized with a string constant without embedded
12987      zeros, build CONST_STRING.  */
12988   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
12989     {
12990       tree enttype = TREE_TYPE (type);
12991       tree domain = TYPE_DOMAIN (type);
12992       enum machine_mode mode = TYPE_MODE (enttype);
12993
12994       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
12995           && domain
12996           && integer_zerop (TYPE_MIN_VALUE (domain))
12997           && compare_tree_int (TYPE_MAX_VALUE (domain),
12998                                TREE_STRING_LENGTH (init) - 1) == 0
12999           && ((size_t) TREE_STRING_LENGTH (init)
13000               == strlen (TREE_STRING_POINTER (init)) + 1))
13001         rtl = gen_rtx_CONST_STRING (VOIDmode,
13002                                     ggc_strdup (TREE_STRING_POINTER (init)));
13003     }
13004   /* Other aggregates, and complex values, could be represented using
13005      CONCAT: FIXME!  */
13006   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
13007     ;
13008   /* Vectors only work if their mode is supported by the target.
13009      FIXME: generic vectors ought to work too.  */
13010   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
13011     ;
13012   /* If the initializer is something that we know will expand into an
13013      immediate RTL constant, expand it now.  We must be careful not to
13014      reference variables which won't be output.  */
13015   else if (initializer_constant_valid_p (init, type)
13016            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
13017     {
13018       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
13019          possible.  */
13020       if (TREE_CODE (type) == VECTOR_TYPE)
13021         switch (TREE_CODE (init))
13022           {
13023           case VECTOR_CST:
13024             break;
13025           case CONSTRUCTOR:
13026             if (TREE_CONSTANT (init))
13027               {
13028                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
13029                 bool constant_p = true;
13030                 tree value;
13031                 unsigned HOST_WIDE_INT ix;
13032
13033                 /* Even when ctor is constant, it might contain non-*_CST
13034                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
13035                    belong into VECTOR_CST nodes.  */
13036                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
13037                   if (!CONSTANT_CLASS_P (value))
13038                     {
13039                       constant_p = false;
13040                       break;
13041                     }
13042
13043                 if (constant_p)
13044                   {
13045                     init = build_vector_from_ctor (type, elts);
13046                     break;
13047                   }
13048               }
13049             /* FALLTHRU */
13050
13051           default:
13052             return NULL;
13053           }
13054
13055       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
13056
13057       /* If expand_expr returns a MEM, it wasn't immediate.  */
13058       gcc_assert (!rtl || !MEM_P (rtl));
13059     }
13060
13061   return rtl;
13062 }
13063
13064 /* Generate RTL for the variable DECL to represent its location.  */
13065
13066 static rtx
13067 rtl_for_decl_location (tree decl)
13068 {
13069   rtx rtl;
13070
13071   /* Here we have to decide where we are going to say the parameter "lives"
13072      (as far as the debugger is concerned).  We only have a couple of
13073      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
13074
13075      DECL_RTL normally indicates where the parameter lives during most of the
13076      activation of the function.  If optimization is enabled however, this
13077      could be either NULL or else a pseudo-reg.  Both of those cases indicate
13078      that the parameter doesn't really live anywhere (as far as the code
13079      generation parts of GCC are concerned) during most of the function's
13080      activation.  That will happen (for example) if the parameter is never
13081      referenced within the function.
13082
13083      We could just generate a location descriptor here for all non-NULL
13084      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
13085      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
13086      where DECL_RTL is NULL or is a pseudo-reg.
13087
13088      Note however that we can only get away with using DECL_INCOMING_RTL as
13089      a backup substitute for DECL_RTL in certain limited cases.  In cases
13090      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
13091      we can be sure that the parameter was passed using the same type as it is
13092      declared to have within the function, and that its DECL_INCOMING_RTL
13093      points us to a place where a value of that type is passed.
13094
13095      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
13096      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
13097      because in these cases DECL_INCOMING_RTL points us to a value of some
13098      type which is *different* from the type of the parameter itself.  Thus,
13099      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
13100      such cases, the debugger would end up (for example) trying to fetch a
13101      `float' from a place which actually contains the first part of a
13102      `double'.  That would lead to really incorrect and confusing
13103      output at debug-time.
13104
13105      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
13106      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
13107      are a couple of exceptions however.  On little-endian machines we can
13108      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
13109      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
13110      an integral type that is smaller than TREE_TYPE (decl). These cases arise
13111      when (on a little-endian machine) a non-prototyped function has a
13112      parameter declared to be of type `short' or `char'.  In such cases,
13113      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
13114      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
13115      passed `int' value.  If the debugger then uses that address to fetch
13116      a `short' or a `char' (on a little-endian machine) the result will be
13117      the correct data, so we allow for such exceptional cases below.
13118
13119      Note that our goal here is to describe the place where the given formal
13120      parameter lives during most of the function's activation (i.e. between the
13121      end of the prologue and the start of the epilogue).  We'll do that as best
13122      as we can. Note however that if the given formal parameter is modified
13123      sometime during the execution of the function, then a stack backtrace (at
13124      debug-time) will show the function as having been called with the *new*
13125      value rather than the value which was originally passed in.  This happens
13126      rarely enough that it is not a major problem, but it *is* a problem, and
13127      I'd like to fix it.
13128
13129      A future version of dwarf2out.c may generate two additional attributes for
13130      any given DW_TAG_formal_parameter DIE which will describe the "passed
13131      type" and the "passed location" for the given formal parameter in addition
13132      to the attributes we now generate to indicate the "declared type" and the
13133      "active location" for each parameter.  This additional set of attributes
13134      could be used by debuggers for stack backtraces. Separately, note that
13135      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
13136      This happens (for example) for inlined-instances of inline function formal
13137      parameters which are never referenced.  This really shouldn't be
13138      happening.  All PARM_DECL nodes should get valid non-NULL
13139      DECL_INCOMING_RTL values.  FIXME.  */
13140
13141   /* Use DECL_RTL as the "location" unless we find something better.  */
13142   rtl = DECL_RTL_IF_SET (decl);
13143
13144   /* When generating abstract instances, ignore everything except
13145      constants, symbols living in memory, and symbols living in
13146      fixed registers.  */
13147   if (! reload_completed)
13148     {
13149       if (rtl
13150           && (CONSTANT_P (rtl)
13151               || (MEM_P (rtl)
13152                   && CONSTANT_P (XEXP (rtl, 0)))
13153               || (REG_P (rtl)
13154                   && TREE_CODE (decl) == VAR_DECL
13155                   && TREE_STATIC (decl))))
13156         {
13157           rtl = targetm.delegitimize_address (rtl);
13158           return rtl;
13159         }
13160       rtl = NULL_RTX;
13161     }
13162   else if (TREE_CODE (decl) == PARM_DECL)
13163     {
13164       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
13165         {
13166           tree declared_type = TREE_TYPE (decl);
13167           tree passed_type = DECL_ARG_TYPE (decl);
13168           enum machine_mode dmode = TYPE_MODE (declared_type);
13169           enum machine_mode pmode = TYPE_MODE (passed_type);
13170
13171           /* This decl represents a formal parameter which was optimized out.
13172              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
13173              all cases where (rtl == NULL_RTX) just below.  */
13174           if (dmode == pmode)
13175             rtl = DECL_INCOMING_RTL (decl);
13176           else if (SCALAR_INT_MODE_P (dmode)
13177                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
13178                    && DECL_INCOMING_RTL (decl))
13179             {
13180               rtx inc = DECL_INCOMING_RTL (decl);
13181               if (REG_P (inc))
13182                 rtl = inc;
13183               else if (MEM_P (inc))
13184                 {
13185                   if (BYTES_BIG_ENDIAN)
13186                     rtl = adjust_address_nv (inc, dmode,
13187                                              GET_MODE_SIZE (pmode)
13188                                              - GET_MODE_SIZE (dmode));
13189                   else
13190                     rtl = inc;
13191                 }
13192             }
13193         }
13194
13195       /* If the parm was passed in registers, but lives on the stack, then
13196          make a big endian correction if the mode of the type of the
13197          parameter is not the same as the mode of the rtl.  */
13198       /* ??? This is the same series of checks that are made in dbxout.c before
13199          we reach the big endian correction code there.  It isn't clear if all
13200          of these checks are necessary here, but keeping them all is the safe
13201          thing to do.  */
13202       else if (MEM_P (rtl)
13203                && XEXP (rtl, 0) != const0_rtx
13204                && ! CONSTANT_P (XEXP (rtl, 0))
13205                /* Not passed in memory.  */
13206                && !MEM_P (DECL_INCOMING_RTL (decl))
13207                /* Not passed by invisible reference.  */
13208                && (!REG_P (XEXP (rtl, 0))
13209                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
13210                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
13211 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
13212                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
13213 #endif
13214                      )
13215                /* Big endian correction check.  */
13216                && BYTES_BIG_ENDIAN
13217                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
13218                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
13219                    < UNITS_PER_WORD))
13220         {
13221           int offset = (UNITS_PER_WORD
13222                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
13223
13224           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13225                              plus_constant (XEXP (rtl, 0), offset));
13226         }
13227     }
13228   else if (TREE_CODE (decl) == VAR_DECL
13229            && rtl
13230            && MEM_P (rtl)
13231            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
13232            && BYTES_BIG_ENDIAN)
13233     {
13234       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
13235       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
13236
13237       /* If a variable is declared "register" yet is smaller than
13238          a register, then if we store the variable to memory, it
13239          looks like we're storing a register-sized value, when in
13240          fact we are not.  We need to adjust the offset of the
13241          storage location to reflect the actual value's bytes,
13242          else gdb will not be able to display it.  */
13243       if (rsize > dsize)
13244         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13245                            plus_constant (XEXP (rtl, 0), rsize-dsize));
13246     }
13247
13248   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
13249      and will have been substituted directly into all expressions that use it.
13250      C does not have such a concept, but C++ and other languages do.  */
13251   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
13252     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
13253
13254   if (rtl)
13255     rtl = targetm.delegitimize_address (rtl);
13256
13257   /* If we don't look past the constant pool, we risk emitting a
13258      reference to a constant pool entry that isn't referenced from
13259      code, and thus is not emitted.  */
13260   if (rtl)
13261     rtl = avoid_constant_pool_reference (rtl);
13262
13263   return rtl;
13264 }
13265
13266 /* We need to figure out what section we should use as the base for the
13267    address ranges where a given location is valid.
13268    1. If this particular DECL has a section associated with it, use that.
13269    2. If this function has a section associated with it, use that.
13270    3. Otherwise, use the text section.
13271    XXX: If you split a variable across multiple sections, we won't notice.  */
13272
13273 static const char *
13274 secname_for_decl (const_tree decl)
13275 {
13276   const char *secname;
13277
13278   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13279     {
13280       tree sectree = DECL_SECTION_NAME (decl);
13281       secname = TREE_STRING_POINTER (sectree);
13282     }
13283   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13284     {
13285       tree sectree = DECL_SECTION_NAME (current_function_decl);
13286       secname = TREE_STRING_POINTER (sectree);
13287     }
13288   else if (cfun && in_cold_section_p)
13289     secname = crtl->subsections.cold_section_label;
13290   else
13291     secname = text_section_label;
13292
13293   return secname;
13294 }
13295
13296 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
13297    returned.  If so, the decl for the COMMON block is returned, and the
13298    value is the offset into the common block for the symbol.  */
13299
13300 static tree
13301 fortran_common (tree decl, HOST_WIDE_INT *value)
13302 {
13303   tree val_expr, cvar;
13304   enum machine_mode mode;
13305   HOST_WIDE_INT bitsize, bitpos;
13306   tree offset;
13307   int volatilep = 0, unsignedp = 0;
13308
13309   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
13310      it does not have a value (the offset into the common area), or if it
13311      is thread local (as opposed to global) then it isn't common, and shouldn't
13312      be handled as such.  */
13313   if (TREE_CODE (decl) != VAR_DECL
13314       || !TREE_PUBLIC (decl)
13315       || !TREE_STATIC (decl)
13316       || !DECL_HAS_VALUE_EXPR_P (decl)
13317       || !is_fortran ())
13318     return NULL_TREE;
13319
13320   val_expr = DECL_VALUE_EXPR (decl);
13321   if (TREE_CODE (val_expr) != COMPONENT_REF)
13322     return NULL_TREE;
13323
13324   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
13325                               &mode, &unsignedp, &volatilep, true);
13326
13327   if (cvar == NULL_TREE
13328       || TREE_CODE (cvar) != VAR_DECL
13329       || DECL_ARTIFICIAL (cvar)
13330       || !TREE_PUBLIC (cvar))
13331     return NULL_TREE;
13332
13333   *value = 0;
13334   if (offset != NULL)
13335     {
13336       if (!host_integerp (offset, 0))
13337         return NULL_TREE;
13338       *value = tree_low_cst (offset, 0);
13339     }
13340   if (bitpos != 0)
13341     *value += bitpos / BITS_PER_UNIT;
13342
13343   return cvar;
13344 }
13345
13346 /* Dereference a location expression LOC if DECL is passed by invisible
13347    reference.  */
13348
13349 static dw_loc_descr_ref
13350 loc_by_reference (dw_loc_descr_ref loc, tree decl)
13351 {
13352   HOST_WIDE_INT size;
13353   enum dwarf_location_atom op;
13354
13355   if (loc == NULL)
13356     return NULL;
13357
13358   if ((TREE_CODE (decl) != PARM_DECL
13359        && TREE_CODE (decl) != RESULT_DECL
13360        && TREE_CODE (decl) != VAR_DECL)
13361       || !DECL_BY_REFERENCE (decl))
13362     return loc;
13363
13364   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
13365      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
13366      location expression is considered to be address of a memory location,
13367      rather than the register itself.  */
13368   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
13369        || loc->dw_loc_opc == DW_OP_regx)
13370       && (loc->dw_loc_next == NULL
13371           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
13372               && loc->dw_loc_next->dw_loc_next == NULL)))
13373     {
13374       if (loc->dw_loc_opc == DW_OP_regx)
13375         {
13376           loc->dw_loc_opc = DW_OP_bregx;
13377           loc->dw_loc_oprnd2.v.val_int = 0;
13378         }
13379       else
13380         {
13381           loc->dw_loc_opc
13382             = (enum dwarf_location_atom)
13383               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
13384           loc->dw_loc_oprnd1.v.val_int = 0;
13385         }
13386       return loc;
13387     }
13388
13389   size = int_size_in_bytes (TREE_TYPE (decl));
13390   if (size > DWARF2_ADDR_SIZE || size == -1)
13391     return 0;
13392   else if (size == DWARF2_ADDR_SIZE)
13393     op = DW_OP_deref;
13394   else
13395     op = DW_OP_deref_size;
13396   add_loc_descr (&loc, new_loc_descr (op, size, 0));
13397   return loc;
13398 }
13399
13400 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
13401    data attribute for a variable or a parameter.  We generate the
13402    DW_AT_const_value attribute only in those cases where the given variable
13403    or parameter does not have a true "location" either in memory or in a
13404    register.  This can happen (for example) when a constant is passed as an
13405    actual argument in a call to an inline function.  (It's possible that
13406    these things can crop up in other ways also.)  Note that one type of
13407    constant value which can be passed into an inlined function is a constant
13408    pointer.  This can happen for example if an actual argument in an inlined
13409    function call evaluates to a compile-time constant address.  */
13410
13411 static void
13412 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
13413                                        enum dwarf_attribute attr)
13414 {
13415   rtx rtl;
13416   dw_loc_descr_ref descr;
13417   var_loc_list *loc_list;
13418   struct var_loc_node *node;
13419   if (TREE_CODE (decl) == ERROR_MARK)
13420     return;
13421
13422   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
13423               || TREE_CODE (decl) == RESULT_DECL);
13424
13425   /* See if we possibly have multiple locations for this variable.  */
13426   loc_list = lookup_decl_loc (decl);
13427
13428   /* If it truly has multiple locations, the first and last node will
13429      differ.  */
13430   if (loc_list && loc_list->first != loc_list->last)
13431     {
13432       const char *endname, *secname;
13433       dw_loc_list_ref list;
13434       rtx varloc;
13435       enum var_init_status initialized;
13436
13437       /* Now that we know what section we are using for a base,
13438          actually construct the list of locations.
13439          The first location information is what is passed to the
13440          function that creates the location list, and the remaining
13441          locations just get added on to that list.
13442          Note that we only know the start address for a location
13443          (IE location changes), so to build the range, we use
13444          the range [current location start, next location start].
13445          This means we have to special case the last node, and generate
13446          a range of [last location start, end of function label].  */
13447
13448       node = loc_list->first;
13449       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13450       secname = secname_for_decl (decl);
13451
13452       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
13453         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13454       else
13455         initialized = VAR_INIT_STATUS_INITIALIZED;
13456
13457       descr = loc_by_reference (loc_descriptor (varloc, DECL_MODE (decl),
13458                                                 initialized), decl);
13459       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
13460       node = node->next;
13461
13462       for (; node->next; node = node->next)
13463         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13464           {
13465             /* The variable has a location between NODE->LABEL and
13466                NODE->NEXT->LABEL.  */
13467             enum var_init_status initialized =
13468               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13469             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13470             descr = loc_by_reference (loc_descriptor (varloc, DECL_MODE (decl),
13471                                       initialized), decl);
13472             add_loc_descr_to_loc_list (&list, descr,
13473                                        node->label, node->next->label, secname);
13474           }
13475
13476       /* If the variable has a location at the last label
13477          it keeps its location until the end of function.  */
13478       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13479         {
13480           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13481           enum var_init_status initialized =
13482             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13483
13484           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13485           if (!current_function_decl)
13486             endname = text_end_label;
13487           else
13488             {
13489               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13490                                            current_function_funcdef_no);
13491               endname = ggc_strdup (label_id);
13492             }
13493           descr = loc_by_reference (loc_descriptor (varloc,
13494                                                     DECL_MODE (decl),
13495                                                     initialized),
13496                                     decl);
13497           add_loc_descr_to_loc_list (&list, descr,
13498                                      node->label, endname, secname);
13499         }
13500
13501       /* Finally, add the location list to the DIE, and we are done.  */
13502       add_AT_loc_list (die, attr, list);
13503       return;
13504     }
13505
13506   /* Try to get some constant RTL for this decl, and use that as the value of
13507      the location.  */
13508
13509   rtl = rtl_for_decl_location (decl);
13510   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
13511     {
13512       add_const_value_attribute (die, rtl);
13513       return;
13514     }
13515
13516   /* If we have tried to generate the location otherwise, and it
13517      didn't work out (we wouldn't be here if we did), and we have a one entry
13518      location list, try generating a location from that.  */
13519   if (loc_list && loc_list->first)
13520     {
13521       enum var_init_status status;
13522       node = loc_list->first;
13523       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13524       rtl = NOTE_VAR_LOCATION (node->var_loc_note);
13525       if (GET_CODE (rtl) == VAR_LOCATION
13526           && GET_CODE (XEXP (rtl, 1)) != PARALLEL)
13527         rtl = XEXP (XEXP (rtl, 1), 0);
13528       if (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
13529         {
13530           add_const_value_attribute (die, rtl);
13531           return;
13532         }
13533       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note),
13534                               DECL_MODE (decl), status);
13535       if (descr)
13536         {
13537           descr = loc_by_reference (descr, decl);
13538           add_AT_location_description (die, attr, descr);
13539           return;
13540         }
13541     }
13542
13543   /* We couldn't get any rtl, so try directly generating the location
13544      description from the tree.  */
13545   descr = loc_descriptor_from_tree (decl);
13546   if (descr)
13547     {
13548       descr = loc_by_reference (descr, decl);
13549       add_AT_location_description (die, attr, descr);
13550       return;
13551     }
13552   /* None of that worked, so it must not really have a location;
13553      try adding a constant value attribute from the DECL_INITIAL.  */
13554   tree_add_const_value_attribute_for_decl (die, decl);
13555 }
13556
13557 /* Add VARIABLE and DIE into deferred locations list.  */
13558
13559 static void
13560 defer_location (tree variable, dw_die_ref die)
13561 {
13562   deferred_locations entry;
13563   entry.variable = variable;
13564   entry.die = die;
13565   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
13566 }
13567
13568 /* Helper function for tree_add_const_value_attribute.  Natively encode
13569    initializer INIT into an array.  Return true if successful.  */
13570
13571 static bool
13572 native_encode_initializer (tree init, unsigned char *array, int size)
13573 {
13574   tree type;
13575
13576   if (init == NULL_TREE)
13577     return false;
13578
13579   STRIP_NOPS (init);
13580   switch (TREE_CODE (init))
13581     {
13582     case STRING_CST:
13583       type = TREE_TYPE (init);
13584       if (TREE_CODE (type) == ARRAY_TYPE)
13585         {
13586           tree enttype = TREE_TYPE (type);
13587           enum machine_mode mode = TYPE_MODE (enttype);
13588
13589           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
13590             return false;
13591           if (int_size_in_bytes (type) != size)
13592             return false;
13593           if (size > TREE_STRING_LENGTH (init))
13594             {
13595               memcpy (array, TREE_STRING_POINTER (init),
13596                       TREE_STRING_LENGTH (init));
13597               memset (array + TREE_STRING_LENGTH (init),
13598                       '\0', size - TREE_STRING_LENGTH (init));
13599             }
13600           else
13601             memcpy (array, TREE_STRING_POINTER (init), size);
13602           return true;
13603         }
13604       return false;
13605     case CONSTRUCTOR:
13606       type = TREE_TYPE (init);
13607       if (int_size_in_bytes (type) != size)
13608         return false;
13609       if (TREE_CODE (type) == ARRAY_TYPE)
13610         {
13611           HOST_WIDE_INT min_index;
13612           unsigned HOST_WIDE_INT cnt;
13613           int curpos = 0, fieldsize;
13614           constructor_elt *ce;
13615
13616           if (TYPE_DOMAIN (type) == NULL_TREE
13617               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
13618             return false;
13619
13620           fieldsize = int_size_in_bytes (TREE_TYPE (type));
13621           if (fieldsize <= 0)
13622             return false;
13623
13624           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
13625           memset (array, '\0', size);
13626           for (cnt = 0;
13627                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
13628                cnt++)
13629             {
13630               tree val = ce->value;
13631               tree index = ce->index;
13632               int pos = curpos;
13633               if (index && TREE_CODE (index) == RANGE_EXPR)
13634                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
13635                       * fieldsize;
13636               else if (index)
13637                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
13638
13639               if (val)
13640                 {
13641                   STRIP_NOPS (val);
13642                   if (!native_encode_initializer (val, array + pos, fieldsize))
13643                     return false;
13644                 }
13645               curpos = pos + fieldsize;
13646               if (index && TREE_CODE (index) == RANGE_EXPR)
13647                 {
13648                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
13649                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
13650                   while (count > 0)
13651                     {
13652                       if (val)
13653                         memcpy (array + curpos, array + pos, fieldsize);
13654                       curpos += fieldsize;
13655                     }
13656                 }
13657               gcc_assert (curpos <= size);
13658             }
13659           return true;
13660         }
13661       else if (TREE_CODE (type) == RECORD_TYPE
13662                || TREE_CODE (type) == UNION_TYPE)
13663         {
13664           tree field = NULL_TREE;
13665           unsigned HOST_WIDE_INT cnt;
13666           constructor_elt *ce;
13667
13668           if (int_size_in_bytes (type) != size)
13669             return false;
13670
13671           if (TREE_CODE (type) == RECORD_TYPE)
13672             field = TYPE_FIELDS (type);
13673
13674           for (cnt = 0;
13675                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
13676                cnt++, field = field ? TREE_CHAIN (field) : 0)
13677             {
13678               tree val = ce->value;
13679               int pos, fieldsize;
13680
13681               if (ce->index != 0)
13682                 field = ce->index;
13683
13684               if (val)
13685                 STRIP_NOPS (val);
13686
13687               if (field == NULL_TREE || DECL_BIT_FIELD (field))
13688                 return false;
13689
13690               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
13691                   && TYPE_DOMAIN (TREE_TYPE (field))
13692                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
13693                 return false;
13694               else if (DECL_SIZE_UNIT (field) == NULL_TREE
13695                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
13696                 return false;
13697               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
13698               pos = int_byte_position (field);
13699               gcc_assert (pos + fieldsize <= size);
13700               if (val
13701                   && !native_encode_initializer (val, array + pos, fieldsize))
13702                 return false;
13703             }
13704           return true;
13705         }
13706       return false;
13707     case VIEW_CONVERT_EXPR:
13708     case NON_LVALUE_EXPR:
13709       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
13710     default:
13711       return native_encode_expr (init, array, size) == size;
13712     }
13713 }
13714
13715 /* Attach a DW_AT_const_value attribute to DIE. The value of the
13716    attribute is the const value T.  */
13717
13718 static void
13719 tree_add_const_value_attribute (dw_die_ref die, tree t)
13720 {
13721   tree init;
13722   tree type = TREE_TYPE (t);
13723   rtx rtl;
13724
13725   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
13726     return;
13727
13728   init = t;
13729   gcc_assert (!DECL_P (init));
13730
13731   rtl = rtl_for_decl_init (init, type);
13732   if (rtl)
13733     add_const_value_attribute (die, rtl);
13734   /* If the host and target are sane, try harder.  */
13735   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
13736            && initializer_constant_valid_p (init, type))
13737     {
13738       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
13739       if (size > 0 && (int) size == size)
13740         {
13741           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
13742
13743           if (native_encode_initializer (init, array, size))
13744             add_AT_vec (die, DW_AT_const_value, size, 1, array);
13745         }
13746     }
13747 }
13748
13749 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
13750    attribute is the const value of T, where T is an integral constant
13751    variable with static storage duration
13752    (so it can't be a PARM_DECL or a RESULT_DECL).  */
13753
13754 static void
13755 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
13756 {
13757
13758   if (!decl
13759       || (TREE_CODE (decl) != VAR_DECL
13760           && TREE_CODE (decl) != CONST_DECL))
13761     return;
13762
13763     if (TREE_READONLY (decl)
13764         && ! TREE_THIS_VOLATILE (decl)
13765         && DECL_INITIAL (decl))
13766       /* OK */;
13767     else
13768       return;
13769
13770     tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
13771 }
13772
13773 /* Convert the CFI instructions for the current function into a
13774    location list.  This is used for DW_AT_frame_base when we targeting
13775    a dwarf2 consumer that does not support the dwarf3
13776    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
13777    expressions.  */
13778
13779 static dw_loc_list_ref
13780 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
13781 {
13782   dw_fde_ref fde;
13783   dw_loc_list_ref list, *list_tail;
13784   dw_cfi_ref cfi;
13785   dw_cfa_location last_cfa, next_cfa;
13786   const char *start_label, *last_label, *section;
13787   dw_cfa_location remember;
13788
13789   fde = current_fde ();
13790   gcc_assert (fde != NULL);
13791
13792   section = secname_for_decl (current_function_decl);
13793   list_tail = &list;
13794   list = NULL;
13795
13796   memset (&next_cfa, 0, sizeof (next_cfa));
13797   next_cfa.reg = INVALID_REGNUM;
13798   remember = next_cfa;
13799
13800   start_label = fde->dw_fde_begin;
13801
13802   /* ??? Bald assumption that the CIE opcode list does not contain
13803      advance opcodes.  */
13804   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
13805     lookup_cfa_1 (cfi, &next_cfa, &remember);
13806
13807   last_cfa = next_cfa;
13808   last_label = start_label;
13809
13810   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
13811     switch (cfi->dw_cfi_opc)
13812       {
13813       case DW_CFA_set_loc:
13814       case DW_CFA_advance_loc1:
13815       case DW_CFA_advance_loc2:
13816       case DW_CFA_advance_loc4:
13817         if (!cfa_equal_p (&last_cfa, &next_cfa))
13818           {
13819             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
13820                                        start_label, last_label, section,
13821                                        list == NULL);
13822
13823             list_tail = &(*list_tail)->dw_loc_next;
13824             last_cfa = next_cfa;
13825             start_label = last_label;
13826           }
13827         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
13828         break;
13829
13830       case DW_CFA_advance_loc:
13831         /* The encoding is complex enough that we should never emit this.  */
13832         gcc_unreachable ();
13833
13834       default:
13835         lookup_cfa_1 (cfi, &next_cfa, &remember);
13836         break;
13837       }
13838
13839   if (!cfa_equal_p (&last_cfa, &next_cfa))
13840     {
13841       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
13842                                  start_label, last_label, section,
13843                                  list == NULL);
13844       list_tail = &(*list_tail)->dw_loc_next;
13845       start_label = last_label;
13846     }
13847   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
13848                              start_label, fde->dw_fde_end, section,
13849                              list == NULL);
13850
13851   return list;
13852 }
13853
13854 /* Compute a displacement from the "steady-state frame pointer" to the
13855    frame base (often the same as the CFA), and store it in
13856    frame_pointer_fb_offset.  OFFSET is added to the displacement
13857    before the latter is negated.  */
13858
13859 static void
13860 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
13861 {
13862   rtx reg, elim;
13863
13864 #ifdef FRAME_POINTER_CFA_OFFSET
13865   reg = frame_pointer_rtx;
13866   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
13867 #else
13868   reg = arg_pointer_rtx;
13869   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
13870 #endif
13871
13872   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13873   if (GET_CODE (elim) == PLUS)
13874     {
13875       offset += INTVAL (XEXP (elim, 1));
13876       elim = XEXP (elim, 0);
13877     }
13878
13879   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13880                && (elim == hard_frame_pointer_rtx
13881                    || elim == stack_pointer_rtx))
13882               || elim == (frame_pointer_needed
13883                           ? hard_frame_pointer_rtx
13884                           : stack_pointer_rtx));
13885
13886   frame_pointer_fb_offset = -offset;
13887 }
13888
13889 /* Generate a DW_AT_name attribute given some string value to be included as
13890    the value of the attribute.  */
13891
13892 static void
13893 add_name_attribute (dw_die_ref die, const char *name_string)
13894 {
13895   if (name_string != NULL && *name_string != 0)
13896     {
13897       if (demangle_name_func)
13898         name_string = (*demangle_name_func) (name_string);
13899
13900       add_AT_string (die, DW_AT_name, name_string);
13901     }
13902 }
13903
13904 /* Generate a DW_AT_comp_dir attribute for DIE.  */
13905
13906 static void
13907 add_comp_dir_attribute (dw_die_ref die)
13908 {
13909   const char *wd = get_src_pwd ();
13910   char *wd1;
13911
13912   if (wd == NULL)
13913     return;
13914
13915   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
13916     {
13917       int wdlen;
13918
13919       wdlen = strlen (wd);
13920       wd1 = GGC_NEWVEC (char, wdlen + 2);
13921       strcpy (wd1, wd);
13922       wd1 [wdlen] = DIR_SEPARATOR;
13923       wd1 [wdlen + 1] = 0;
13924       wd = wd1;
13925     }
13926
13927     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
13928 }
13929
13930 /* Given a tree node describing an array bound (either lower or upper) output
13931    a representation for that bound.  */
13932
13933 static void
13934 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
13935 {
13936   switch (TREE_CODE (bound))
13937     {
13938     case ERROR_MARK:
13939       return;
13940
13941     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
13942     case INTEGER_CST:
13943       if (! host_integerp (bound, 0)
13944           || (bound_attr == DW_AT_lower_bound
13945               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
13946                   || (is_fortran () && integer_onep (bound)))))
13947         /* Use the default.  */
13948         ;
13949       else
13950         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
13951       break;
13952
13953     CASE_CONVERT:
13954     case VIEW_CONVERT_EXPR:
13955       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
13956       break;
13957
13958     case SAVE_EXPR:
13959       break;
13960
13961     case VAR_DECL:
13962     case PARM_DECL:
13963     case RESULT_DECL:
13964       {
13965         dw_die_ref decl_die = lookup_decl_die (bound);
13966         dw_loc_descr_ref loc;
13967
13968         /* ??? Can this happen, or should the variable have been bound
13969            first?  Probably it can, since I imagine that we try to create
13970            the types of parameters in the order in which they exist in
13971            the list, and won't have created a forward reference to a
13972            later parameter.  */
13973         if (decl_die != NULL)
13974           add_AT_die_ref (subrange_die, bound_attr, decl_die);
13975         else
13976           {
13977             loc = loc_descriptor_from_tree_1 (bound, 0);
13978             add_AT_location_description (subrange_die, bound_attr, loc);
13979           }
13980         break;
13981       }
13982
13983     default:
13984       {
13985         /* Otherwise try to create a stack operation procedure to
13986            evaluate the value of the array bound.  */
13987
13988         dw_die_ref ctx, decl_die;
13989         dw_loc_descr_ref loc;
13990
13991         loc = loc_descriptor_from_tree (bound);
13992         if (loc == NULL)
13993           break;
13994
13995         if (current_function_decl == 0)
13996           ctx = comp_unit_die;
13997         else
13998           ctx = lookup_decl_die (current_function_decl);
13999
14000         decl_die = new_die (DW_TAG_variable, ctx, bound);
14001         add_AT_flag (decl_die, DW_AT_artificial, 1);
14002         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
14003         add_AT_loc (decl_die, DW_AT_location, loc);
14004
14005         add_AT_die_ref (subrange_die, bound_attr, decl_die);
14006         break;
14007       }
14008     }
14009 }
14010
14011 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
14012    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
14013    Note that the block of subscript information for an array type also
14014    includes information about the element type of the given array type.  */
14015
14016 static void
14017 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
14018 {
14019   unsigned dimension_number;
14020   tree lower, upper;
14021   dw_die_ref subrange_die;
14022
14023   for (dimension_number = 0;
14024        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
14025        type = TREE_TYPE (type), dimension_number++)
14026     {
14027       tree domain = TYPE_DOMAIN (type);
14028
14029       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
14030         break;
14031
14032       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
14033          and (in GNU C only) variable bounds.  Handle all three forms
14034          here.  */
14035       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
14036       if (domain)
14037         {
14038           /* We have an array type with specified bounds.  */
14039           lower = TYPE_MIN_VALUE (domain);
14040           upper = TYPE_MAX_VALUE (domain);
14041
14042           /* Define the index type.  */
14043           if (TREE_TYPE (domain))
14044             {
14045               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
14046                  TREE_TYPE field.  We can't emit debug info for this
14047                  because it is an unnamed integral type.  */
14048               if (TREE_CODE (domain) == INTEGER_TYPE
14049                   && TYPE_NAME (domain) == NULL_TREE
14050                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
14051                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
14052                 ;
14053               else
14054                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
14055                                     type_die);
14056             }
14057
14058           /* ??? If upper is NULL, the array has unspecified length,
14059              but it does have a lower bound.  This happens with Fortran
14060                dimension arr(N:*)
14061              Since the debugger is definitely going to need to know N
14062              to produce useful results, go ahead and output the lower
14063              bound solo, and hope the debugger can cope.  */
14064
14065           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
14066           if (upper)
14067             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
14068         }
14069
14070       /* Otherwise we have an array type with an unspecified length.  The
14071          DWARF-2 spec does not say how to handle this; let's just leave out the
14072          bounds.  */
14073     }
14074 }
14075
14076 static void
14077 add_byte_size_attribute (dw_die_ref die, tree tree_node)
14078 {
14079   unsigned size;
14080
14081   switch (TREE_CODE (tree_node))
14082     {
14083     case ERROR_MARK:
14084       size = 0;
14085       break;
14086     case ENUMERAL_TYPE:
14087     case RECORD_TYPE:
14088     case UNION_TYPE:
14089     case QUAL_UNION_TYPE:
14090       size = int_size_in_bytes (tree_node);
14091       break;
14092     case FIELD_DECL:
14093       /* For a data member of a struct or union, the DW_AT_byte_size is
14094          generally given as the number of bytes normally allocated for an
14095          object of the *declared* type of the member itself.  This is true
14096          even for bit-fields.  */
14097       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
14098       break;
14099     default:
14100       gcc_unreachable ();
14101     }
14102
14103   /* Note that `size' might be -1 when we get to this point.  If it is, that
14104      indicates that the byte size of the entity in question is variable.  We
14105      have no good way of expressing this fact in Dwarf at the present time,
14106      so just let the -1 pass on through.  */
14107   add_AT_unsigned (die, DW_AT_byte_size, size);
14108 }
14109
14110 /* For a FIELD_DECL node which represents a bit-field, output an attribute
14111    which specifies the distance in bits from the highest order bit of the
14112    "containing object" for the bit-field to the highest order bit of the
14113    bit-field itself.
14114
14115    For any given bit-field, the "containing object" is a hypothetical object
14116    (of some integral or enum type) within which the given bit-field lives.  The
14117    type of this hypothetical "containing object" is always the same as the
14118    declared type of the individual bit-field itself.  The determination of the
14119    exact location of the "containing object" for a bit-field is rather
14120    complicated.  It's handled by the `field_byte_offset' function (above).
14121
14122    Note that it is the size (in bytes) of the hypothetical "containing object"
14123    which will be given in the DW_AT_byte_size attribute for this bit-field.
14124    (See `byte_size_attribute' above).  */
14125
14126 static inline void
14127 add_bit_offset_attribute (dw_die_ref die, tree decl)
14128 {
14129   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
14130   tree type = DECL_BIT_FIELD_TYPE (decl);
14131   HOST_WIDE_INT bitpos_int;
14132   HOST_WIDE_INT highest_order_object_bit_offset;
14133   HOST_WIDE_INT highest_order_field_bit_offset;
14134   HOST_WIDE_INT unsigned bit_offset;
14135
14136   /* Must be a field and a bit field.  */
14137   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
14138
14139   /* We can't yet handle bit-fields whose offsets are variable, so if we
14140      encounter such things, just return without generating any attribute
14141      whatsoever.  Likewise for variable or too large size.  */
14142   if (! host_integerp (bit_position (decl), 0)
14143       || ! host_integerp (DECL_SIZE (decl), 1))
14144     return;
14145
14146   bitpos_int = int_bit_position (decl);
14147
14148   /* Note that the bit offset is always the distance (in bits) from the
14149      highest-order bit of the "containing object" to the highest-order bit of
14150      the bit-field itself.  Since the "high-order end" of any object or field
14151      is different on big-endian and little-endian machines, the computation
14152      below must take account of these differences.  */
14153   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
14154   highest_order_field_bit_offset = bitpos_int;
14155
14156   if (! BYTES_BIG_ENDIAN)
14157     {
14158       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
14159       highest_order_object_bit_offset += simple_type_size_in_bits (type);
14160     }
14161
14162   bit_offset
14163     = (! BYTES_BIG_ENDIAN
14164        ? highest_order_object_bit_offset - highest_order_field_bit_offset
14165        : highest_order_field_bit_offset - highest_order_object_bit_offset);
14166
14167   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
14168 }
14169
14170 /* For a FIELD_DECL node which represents a bit field, output an attribute
14171    which specifies the length in bits of the given field.  */
14172
14173 static inline void
14174 add_bit_size_attribute (dw_die_ref die, tree decl)
14175 {
14176   /* Must be a field and a bit field.  */
14177   gcc_assert (TREE_CODE (decl) == FIELD_DECL
14178               && DECL_BIT_FIELD_TYPE (decl));
14179
14180   if (host_integerp (DECL_SIZE (decl), 1))
14181     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
14182 }
14183
14184 /* If the compiled language is ANSI C, then add a 'prototyped'
14185    attribute, if arg types are given for the parameters of a function.  */
14186
14187 static inline void
14188 add_prototyped_attribute (dw_die_ref die, tree func_type)
14189 {
14190   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
14191       && TYPE_ARG_TYPES (func_type) != NULL)
14192     add_AT_flag (die, DW_AT_prototyped, 1);
14193 }
14194
14195 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
14196    by looking in either the type declaration or object declaration
14197    equate table.  */
14198
14199 static inline dw_die_ref
14200 add_abstract_origin_attribute (dw_die_ref die, tree origin)
14201 {
14202   dw_die_ref origin_die = NULL;
14203
14204   if (TREE_CODE (origin) != FUNCTION_DECL)
14205     {
14206       /* We may have gotten separated from the block for the inlined
14207          function, if we're in an exception handler or some such; make
14208          sure that the abstract function has been written out.
14209
14210          Doing this for nested functions is wrong, however; functions are
14211          distinct units, and our context might not even be inline.  */
14212       tree fn = origin;
14213
14214       if (TYPE_P (fn))
14215         fn = TYPE_STUB_DECL (fn);
14216
14217       fn = decl_function_context (fn);
14218       if (fn)
14219         dwarf2out_abstract_function (fn);
14220     }
14221
14222   if (DECL_P (origin))
14223     origin_die = lookup_decl_die (origin);
14224   else if (TYPE_P (origin))
14225     origin_die = lookup_type_die (origin);
14226
14227   /* XXX: Functions that are never lowered don't always have correct block
14228      trees (in the case of java, they simply have no block tree, in some other
14229      languages).  For these functions, there is nothing we can really do to
14230      output correct debug info for inlined functions in all cases.  Rather
14231      than die, we'll just produce deficient debug info now, in that we will
14232      have variables without a proper abstract origin.  In the future, when all
14233      functions are lowered, we should re-add a gcc_assert (origin_die)
14234      here.  */
14235
14236   if (origin_die)
14237     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
14238   return origin_die;
14239 }
14240
14241 /* We do not currently support the pure_virtual attribute.  */
14242
14243 static inline void
14244 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
14245 {
14246   if (DECL_VINDEX (func_decl))
14247     {
14248       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14249
14250       if (host_integerp (DECL_VINDEX (func_decl), 0))
14251         add_AT_loc (die, DW_AT_vtable_elem_location,
14252                     new_loc_descr (DW_OP_constu,
14253                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
14254                                    0));
14255
14256       /* GNU extension: Record what type this method came from originally.  */
14257       if (debug_info_level > DINFO_LEVEL_TERSE)
14258         add_AT_die_ref (die, DW_AT_containing_type,
14259                         lookup_type_die (DECL_CONTEXT (func_decl)));
14260     }
14261 }
14262 \f
14263 /* Add source coordinate attributes for the given decl.  */
14264
14265 static void
14266 add_src_coords_attributes (dw_die_ref die, tree decl)
14267 {
14268   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14269
14270   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
14271   add_AT_unsigned (die, DW_AT_decl_line, s.line);
14272 }
14273
14274 /* Add a DW_AT_name attribute and source coordinate attribute for the
14275    given decl, but only if it actually has a name.  */
14276
14277 static void
14278 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
14279 {
14280   tree decl_name;
14281
14282   decl_name = DECL_NAME (decl);
14283   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
14284     {
14285       add_name_attribute (die, dwarf2_name (decl, 0));
14286       if (! DECL_ARTIFICIAL (decl))
14287         add_src_coords_attributes (die, decl);
14288
14289       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
14290           && TREE_PUBLIC (decl)
14291           && !DECL_ABSTRACT (decl)
14292           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
14293           && !is_fortran ())
14294         {
14295           /* Defer until we have an assembler name set.  */
14296           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
14297             {
14298               limbo_die_node *asm_name;
14299
14300               asm_name = GGC_CNEW (limbo_die_node);
14301               asm_name->die = die;
14302               asm_name->created_for = decl;
14303               asm_name->next = deferred_asm_name;
14304               deferred_asm_name = asm_name;
14305             }
14306           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
14307             add_AT_string (die, DW_AT_MIPS_linkage_name,
14308                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
14309         }
14310     }
14311
14312 #ifdef VMS_DEBUGGING_INFO
14313   /* Get the function's name, as described by its RTL.  This may be different
14314      from the DECL_NAME name used in the source file.  */
14315   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
14316     {
14317       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
14318                    XEXP (DECL_RTL (decl), 0));
14319       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
14320     }
14321 #endif
14322 }
14323
14324 /* Push a new declaration scope.  */
14325
14326 static void
14327 push_decl_scope (tree scope)
14328 {
14329   VEC_safe_push (tree, gc, decl_scope_table, scope);
14330 }
14331
14332 /* Pop a declaration scope.  */
14333
14334 static inline void
14335 pop_decl_scope (void)
14336 {
14337   VEC_pop (tree, decl_scope_table);
14338 }
14339
14340 /* Return the DIE for the scope that immediately contains this type.
14341    Non-named types get global scope.  Named types nested in other
14342    types get their containing scope if it's open, or global scope
14343    otherwise.  All other types (i.e. function-local named types) get
14344    the current active scope.  */
14345
14346 static dw_die_ref
14347 scope_die_for (tree t, dw_die_ref context_die)
14348 {
14349   dw_die_ref scope_die = NULL;
14350   tree containing_scope;
14351   int i;
14352
14353   /* Non-types always go in the current scope.  */
14354   gcc_assert (TYPE_P (t));
14355
14356   containing_scope = TYPE_CONTEXT (t);
14357
14358   /* Use the containing namespace if it was passed in (for a declaration).  */
14359   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
14360     {
14361       if (context_die == lookup_decl_die (containing_scope))
14362         /* OK */;
14363       else
14364         containing_scope = NULL_TREE;
14365     }
14366
14367   /* Ignore function type "scopes" from the C frontend.  They mean that
14368      a tagged type is local to a parmlist of a function declarator, but
14369      that isn't useful to DWARF.  */
14370   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
14371     containing_scope = NULL_TREE;
14372
14373   if (containing_scope == NULL_TREE)
14374     scope_die = comp_unit_die;
14375   else if (TYPE_P (containing_scope))
14376     {
14377       /* For types, we can just look up the appropriate DIE.  But
14378          first we check to see if we're in the middle of emitting it
14379          so we know where the new DIE should go.  */
14380       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
14381         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
14382           break;
14383
14384       if (i < 0)
14385         {
14386           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
14387                       || TREE_ASM_WRITTEN (containing_scope));
14388
14389           /* If none of the current dies are suitable, we get file scope.  */
14390           scope_die = comp_unit_die;
14391         }
14392       else
14393         scope_die = lookup_type_die (containing_scope);
14394     }
14395   else
14396     scope_die = context_die;
14397
14398   return scope_die;
14399 }
14400
14401 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
14402
14403 static inline int
14404 local_scope_p (dw_die_ref context_die)
14405 {
14406   for (; context_die; context_die = context_die->die_parent)
14407     if (context_die->die_tag == DW_TAG_inlined_subroutine
14408         || context_die->die_tag == DW_TAG_subprogram)
14409       return 1;
14410
14411   return 0;
14412 }
14413
14414 /* Returns nonzero if CONTEXT_DIE is a class.  */
14415
14416 static inline int
14417 class_scope_p (dw_die_ref context_die)
14418 {
14419   return (context_die
14420           && (context_die->die_tag == DW_TAG_structure_type
14421               || context_die->die_tag == DW_TAG_class_type
14422               || context_die->die_tag == DW_TAG_interface_type
14423               || context_die->die_tag == DW_TAG_union_type));
14424 }
14425
14426 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
14427    whether or not to treat a DIE in this context as a declaration.  */
14428
14429 static inline int
14430 class_or_namespace_scope_p (dw_die_ref context_die)
14431 {
14432   return (class_scope_p (context_die)
14433           || (context_die && context_die->die_tag == DW_TAG_namespace));
14434 }
14435
14436 /* Many forms of DIEs require a "type description" attribute.  This
14437    routine locates the proper "type descriptor" die for the type given
14438    by 'type', and adds a DW_AT_type attribute below the given die.  */
14439
14440 static void
14441 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
14442                     int decl_volatile, dw_die_ref context_die)
14443 {
14444   enum tree_code code  = TREE_CODE (type);
14445   dw_die_ref type_die  = NULL;
14446
14447   /* ??? If this type is an unnamed subrange type of an integral, floating-point
14448      or fixed-point type, use the inner type.  This is because we have no
14449      support for unnamed types in base_type_die.  This can happen if this is
14450      an Ada subrange type.  Correct solution is emit a subrange type die.  */
14451   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
14452       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
14453     type = TREE_TYPE (type), code = TREE_CODE (type);
14454
14455   if (code == ERROR_MARK
14456       /* Handle a special case.  For functions whose return type is void, we
14457          generate *no* type attribute.  (Note that no object may have type
14458          `void', so this only applies to function return types).  */
14459       || code == VOID_TYPE)
14460     return;
14461
14462   type_die = modified_type_die (type,
14463                                 decl_const || TYPE_READONLY (type),
14464                                 decl_volatile || TYPE_VOLATILE (type),
14465                                 context_die);
14466
14467   if (type_die != NULL)
14468     add_AT_die_ref (object_die, DW_AT_type, type_die);
14469 }
14470
14471 /* Given an object die, add the calling convention attribute for the
14472    function call type.  */
14473 static void
14474 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
14475 {
14476   enum dwarf_calling_convention value = DW_CC_normal;
14477
14478   value = ((enum dwarf_calling_convention)
14479            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
14480
14481   /* DWARF doesn't provide a way to identify a program's source-level
14482      entry point.  DW_AT_calling_convention attributes are only meant
14483      to describe functions' calling conventions.  However, lacking a
14484      better way to signal the Fortran main program, we use this for the
14485      time being, following existing custom.  */
14486   if (is_fortran ()
14487       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
14488     value = DW_CC_program;
14489
14490   /* Only add the attribute if the backend requests it, and
14491      is not DW_CC_normal.  */
14492   if (value && (value != DW_CC_normal))
14493     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
14494 }
14495
14496 /* Given a tree pointer to a struct, class, union, or enum type node, return
14497    a pointer to the (string) tag name for the given type, or zero if the type
14498    was declared without a tag.  */
14499
14500 static const char *
14501 type_tag (const_tree type)
14502 {
14503   const char *name = 0;
14504
14505   if (TYPE_NAME (type) != 0)
14506     {
14507       tree t = 0;
14508
14509       /* Find the IDENTIFIER_NODE for the type name.  */
14510       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
14511         t = TYPE_NAME (type);
14512
14513       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
14514          a TYPE_DECL node, regardless of whether or not a `typedef' was
14515          involved.  */
14516       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14517                && ! DECL_IGNORED_P (TYPE_NAME (type)))
14518         {
14519           /* We want to be extra verbose.  Don't call dwarf_name if
14520              DECL_NAME isn't set.  The default hook for decl_printable_name
14521              doesn't like that, and in this context it's correct to return
14522              0, instead of "<anonymous>" or the like.  */
14523           if (DECL_NAME (TYPE_NAME (type)))
14524             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
14525         }
14526
14527       /* Now get the name as a string, or invent one.  */
14528       if (!name && t != 0)
14529         name = IDENTIFIER_POINTER (t);
14530     }
14531
14532   return (name == 0 || *name == '\0') ? 0 : name;
14533 }
14534
14535 /* Return the type associated with a data member, make a special check
14536    for bit field types.  */
14537
14538 static inline tree
14539 member_declared_type (const_tree member)
14540 {
14541   return (DECL_BIT_FIELD_TYPE (member)
14542           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
14543 }
14544
14545 /* Get the decl's label, as described by its RTL. This may be different
14546    from the DECL_NAME name used in the source file.  */
14547
14548 #if 0
14549 static const char *
14550 decl_start_label (tree decl)
14551 {
14552   rtx x;
14553   const char *fnname;
14554
14555   x = DECL_RTL (decl);
14556   gcc_assert (MEM_P (x));
14557
14558   x = XEXP (x, 0);
14559   gcc_assert (GET_CODE (x) == SYMBOL_REF);
14560
14561   fnname = XSTR (x, 0);
14562   return fnname;
14563 }
14564 #endif
14565 \f
14566 /* These routines generate the internal representation of the DIE's for
14567    the compilation unit.  Debugging information is collected by walking
14568    the declaration trees passed in from dwarf2out_decl().  */
14569
14570 static void
14571 gen_array_type_die (tree type, dw_die_ref context_die)
14572 {
14573   dw_die_ref scope_die = scope_die_for (type, context_die);
14574   dw_die_ref array_die;
14575
14576   /* GNU compilers represent multidimensional array types as sequences of one
14577      dimensional array types whose element types are themselves array types.
14578      We sometimes squish that down to a single array_type DIE with multiple
14579      subscripts in the Dwarf debugging info.  The draft Dwarf specification
14580      say that we are allowed to do this kind of compression in C, because
14581      there is no difference between an array of arrays and a multidimensional
14582      array.  We don't do this for Ada to remain as close as possible to the
14583      actual representation, which is especially important against the language
14584      flexibilty wrt arrays of variable size.  */
14585
14586   bool collapse_nested_arrays = !is_ada ();
14587   tree element_type;
14588
14589   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
14590      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
14591   if (TYPE_STRING_FLAG (type)
14592       && TREE_CODE (type) == ARRAY_TYPE
14593       && is_fortran ()
14594       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
14595     {
14596       HOST_WIDE_INT size;
14597
14598       array_die = new_die (DW_TAG_string_type, scope_die, type);
14599       add_name_attribute (array_die, type_tag (type));
14600       equate_type_number_to_die (type, array_die);
14601       size = int_size_in_bytes (type);
14602       if (size >= 0)
14603         add_AT_unsigned (array_die, DW_AT_byte_size, size);
14604       else if (TYPE_DOMAIN (type) != NULL_TREE
14605                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
14606                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
14607         {
14608           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
14609           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
14610
14611           size = int_size_in_bytes (TREE_TYPE (szdecl));
14612           if (loc && size > 0)
14613             {
14614               add_AT_loc (array_die, DW_AT_string_length, loc);
14615               if (size != DWARF2_ADDR_SIZE)
14616                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
14617             }
14618         }
14619       return;
14620     }
14621
14622   /* ??? The SGI dwarf reader fails for array of array of enum types
14623      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
14624      array type comes before the outer array type.  We thus call gen_type_die
14625      before we new_die and must prevent nested array types collapsing for this
14626      target.  */
14627
14628 #ifdef MIPS_DEBUGGING_INFO
14629   gen_type_die (TREE_TYPE (type), context_die);
14630   collapse_nested_arrays = false;
14631 #endif
14632
14633   array_die = new_die (DW_TAG_array_type, scope_die, type);
14634   add_name_attribute (array_die, type_tag (type));
14635   equate_type_number_to_die (type, array_die);
14636
14637   if (TREE_CODE (type) == VECTOR_TYPE)
14638     {
14639       /* The frontend feeds us a representation for the vector as a struct
14640          containing an array.  Pull out the array type.  */
14641       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
14642       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
14643     }
14644
14645   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
14646   if (is_fortran ()
14647       && TREE_CODE (type) == ARRAY_TYPE
14648       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
14649       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
14650     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
14651
14652 #if 0
14653   /* We default the array ordering.  SDB will probably do
14654      the right things even if DW_AT_ordering is not present.  It's not even
14655      an issue until we start to get into multidimensional arrays anyway.  If
14656      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
14657      then we'll have to put the DW_AT_ordering attribute back in.  (But if
14658      and when we find out that we need to put these in, we will only do so
14659      for multidimensional arrays.  */
14660   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
14661 #endif
14662
14663 #ifdef MIPS_DEBUGGING_INFO
14664   /* The SGI compilers handle arrays of unknown bound by setting
14665      AT_declaration and not emitting any subrange DIEs.  */
14666   if (! TYPE_DOMAIN (type))
14667     add_AT_flag (array_die, DW_AT_declaration, 1);
14668   else
14669 #endif
14670     add_subscript_info (array_die, type, collapse_nested_arrays);
14671
14672   /* Add representation of the type of the elements of this array type and
14673      emit the corresponding DIE if we haven't done it already.  */  
14674   element_type = TREE_TYPE (type);
14675   if (collapse_nested_arrays)
14676     while (TREE_CODE (element_type) == ARRAY_TYPE)
14677       {
14678         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
14679           break;
14680         element_type = TREE_TYPE (element_type);
14681       }
14682
14683 #ifndef MIPS_DEBUGGING_INFO
14684   gen_type_die (element_type, context_die);
14685 #endif
14686
14687   add_type_attribute (array_die, element_type, 0, 0, context_die);
14688
14689   if (get_AT (array_die, DW_AT_name))
14690     add_pubtype (type, array_die);
14691 }
14692
14693 static dw_loc_descr_ref
14694 descr_info_loc (tree val, tree base_decl)
14695 {
14696   HOST_WIDE_INT size;
14697   dw_loc_descr_ref loc, loc2;
14698   enum dwarf_location_atom op;
14699
14700   if (val == base_decl)
14701     return new_loc_descr (DW_OP_push_object_address, 0, 0);
14702
14703   switch (TREE_CODE (val))
14704     {
14705     CASE_CONVERT:
14706       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14707     case VAR_DECL:
14708       return loc_descriptor_from_tree_1 (val, 0);
14709     case INTEGER_CST:
14710       if (host_integerp (val, 0))
14711         return int_loc_descriptor (tree_low_cst (val, 0));
14712       break;
14713     case INDIRECT_REF:
14714       size = int_size_in_bytes (TREE_TYPE (val));
14715       if (size < 0)
14716         break;
14717       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14718       if (!loc)
14719         break;
14720       if (size == DWARF2_ADDR_SIZE)
14721         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
14722       else
14723         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
14724       return loc;
14725     case POINTER_PLUS_EXPR:
14726     case PLUS_EXPR:
14727       if (host_integerp (TREE_OPERAND (val, 1), 1)
14728           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
14729              < 16384)
14730         {
14731           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14732           if (!loc)
14733             break;
14734           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
14735         }
14736       else
14737         {
14738           op = DW_OP_plus;
14739         do_binop:
14740           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14741           if (!loc)
14742             break;
14743           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
14744           if (!loc2)
14745             break;
14746           add_loc_descr (&loc, loc2);
14747           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
14748         }
14749       return loc;
14750     case MINUS_EXPR:
14751       op = DW_OP_minus;
14752       goto do_binop;
14753     case MULT_EXPR:
14754       op = DW_OP_mul;
14755       goto do_binop;
14756     case EQ_EXPR:
14757       op = DW_OP_eq;
14758       goto do_binop;
14759     case NE_EXPR:
14760       op = DW_OP_ne;
14761       goto do_binop;
14762     default:
14763       break;
14764     }
14765   return NULL;
14766 }
14767
14768 static void
14769 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
14770                       tree val, tree base_decl)
14771 {
14772   dw_loc_descr_ref loc;
14773
14774   if (host_integerp (val, 0))
14775     {
14776       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
14777       return;
14778     }
14779
14780   loc = descr_info_loc (val, base_decl);
14781   if (!loc)
14782     return;
14783
14784   add_AT_loc (die, attr, loc);
14785 }
14786
14787 /* This routine generates DIE for array with hidden descriptor, details
14788    are filled into *info by a langhook.  */
14789
14790 static void
14791 gen_descr_array_type_die (tree type, struct array_descr_info *info,
14792                           dw_die_ref context_die)
14793 {
14794   dw_die_ref scope_die = scope_die_for (type, context_die);
14795   dw_die_ref array_die;
14796   int dim;
14797
14798   array_die = new_die (DW_TAG_array_type, scope_die, type);
14799   add_name_attribute (array_die, type_tag (type));
14800   equate_type_number_to_die (type, array_die);
14801
14802   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
14803   if (is_fortran ()
14804       && info->ndimensions >= 2)
14805     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
14806
14807   if (info->data_location)
14808     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
14809                           info->base_decl);
14810   if (info->associated)
14811     add_descr_info_field (array_die, DW_AT_associated, info->associated,
14812                           info->base_decl);
14813   if (info->allocated)
14814     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
14815                           info->base_decl);
14816
14817   for (dim = 0; dim < info->ndimensions; dim++)
14818     {
14819       dw_die_ref subrange_die
14820         = new_die (DW_TAG_subrange_type, array_die, NULL);
14821
14822       if (info->dimen[dim].lower_bound)
14823         {
14824           /* If it is the default value, omit it.  */
14825           if ((is_c_family () || is_java ())
14826               && integer_zerop (info->dimen[dim].lower_bound))
14827             ;
14828           else if (is_fortran ()
14829                    && integer_onep (info->dimen[dim].lower_bound))
14830             ;
14831           else
14832             add_descr_info_field (subrange_die, DW_AT_lower_bound,
14833                                   info->dimen[dim].lower_bound,
14834                                   info->base_decl);
14835         }
14836       if (info->dimen[dim].upper_bound)
14837         add_descr_info_field (subrange_die, DW_AT_upper_bound,
14838                               info->dimen[dim].upper_bound,
14839                               info->base_decl);
14840       if (info->dimen[dim].stride)
14841         add_descr_info_field (subrange_die, DW_AT_byte_stride,
14842                               info->dimen[dim].stride,
14843                               info->base_decl);
14844     }
14845
14846   gen_type_die (info->element_type, context_die);
14847   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
14848
14849   if (get_AT (array_die, DW_AT_name))
14850     add_pubtype (type, array_die);
14851 }
14852
14853 #if 0
14854 static void
14855 gen_entry_point_die (tree decl, dw_die_ref context_die)
14856 {
14857   tree origin = decl_ultimate_origin (decl);
14858   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
14859
14860   if (origin != NULL)
14861     add_abstract_origin_attribute (decl_die, origin);
14862   else
14863     {
14864       add_name_and_src_coords_attributes (decl_die, decl);
14865       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
14866                           0, 0, context_die);
14867     }
14868
14869   if (DECL_ABSTRACT (decl))
14870     equate_decl_number_to_die (decl, decl_die);
14871   else
14872     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
14873 }
14874 #endif
14875
14876 /* Walk through the list of incomplete types again, trying once more to
14877    emit full debugging info for them.  */
14878
14879 static void
14880 retry_incomplete_types (void)
14881 {
14882   int i;
14883
14884   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
14885     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
14886 }
14887
14888 /* Determine what tag to use for a record type.  */
14889
14890 static enum dwarf_tag
14891 record_type_tag (tree type)
14892 {
14893   if (! lang_hooks.types.classify_record)
14894     return DW_TAG_structure_type;
14895
14896   switch (lang_hooks.types.classify_record (type))
14897     {
14898     case RECORD_IS_STRUCT:
14899       return DW_TAG_structure_type;
14900
14901     case RECORD_IS_CLASS:
14902       return DW_TAG_class_type;
14903
14904     case RECORD_IS_INTERFACE:
14905       return DW_TAG_interface_type;
14906
14907     default:
14908       gcc_unreachable ();
14909     }
14910 }
14911
14912 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
14913    include all of the information about the enumeration values also. Each
14914    enumerated type name/value is listed as a child of the enumerated type
14915    DIE.  */
14916
14917 static dw_die_ref
14918 gen_enumeration_type_die (tree type, dw_die_ref context_die)
14919 {
14920   dw_die_ref type_die = lookup_type_die (type);
14921
14922   if (type_die == NULL)
14923     {
14924       type_die = new_die (DW_TAG_enumeration_type,
14925                           scope_die_for (type, context_die), type);
14926       equate_type_number_to_die (type, type_die);
14927       add_name_attribute (type_die, type_tag (type));
14928     }
14929   else if (! TYPE_SIZE (type))
14930     return type_die;
14931   else
14932     remove_AT (type_die, DW_AT_declaration);
14933
14934   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
14935      given enum type is incomplete, do not generate the DW_AT_byte_size
14936      attribute or the DW_AT_element_list attribute.  */
14937   if (TYPE_SIZE (type))
14938     {
14939       tree link;
14940
14941       TREE_ASM_WRITTEN (type) = 1;
14942       add_byte_size_attribute (type_die, type);
14943       if (TYPE_STUB_DECL (type) != NULL_TREE)
14944         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14945
14946       /* If the first reference to this type was as the return type of an
14947          inline function, then it may not have a parent.  Fix this now.  */
14948       if (type_die->die_parent == NULL)
14949         add_child_die (scope_die_for (type, context_die), type_die);
14950
14951       for (link = TYPE_VALUES (type);
14952            link != NULL; link = TREE_CHAIN (link))
14953         {
14954           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
14955           tree value = TREE_VALUE (link);
14956
14957           add_name_attribute (enum_die,
14958                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
14959
14960           if (TREE_CODE (value) == CONST_DECL)
14961             value = DECL_INITIAL (value);
14962
14963           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
14964             /* DWARF2 does not provide a way of indicating whether or
14965                not enumeration constants are signed or unsigned.  GDB
14966                always assumes the values are signed, so we output all
14967                values as if they were signed.  That means that
14968                enumeration constants with very large unsigned values
14969                will appear to have negative values in the debugger.  */
14970             add_AT_int (enum_die, DW_AT_const_value,
14971                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
14972         }
14973     }
14974   else
14975     add_AT_flag (type_die, DW_AT_declaration, 1);
14976
14977   if (get_AT (type_die, DW_AT_name))
14978     add_pubtype (type, type_die);
14979
14980   return type_die;
14981 }
14982
14983 /* Generate a DIE to represent either a real live formal parameter decl or to
14984    represent just the type of some formal parameter position in some function
14985    type.
14986
14987    Note that this routine is a bit unusual because its argument may be a
14988    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
14989    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
14990    node.  If it's the former then this function is being called to output a
14991    DIE to represent a formal parameter object (or some inlining thereof).  If
14992    it's the latter, then this function is only being called to output a
14993    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
14994    argument type of some subprogram type.  */
14995
14996 static dw_die_ref
14997 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
14998 {
14999   tree node_or_origin = node ? node : origin;
15000   dw_die_ref parm_die
15001     = new_die (DW_TAG_formal_parameter, context_die, node);
15002
15003   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
15004     {
15005     case tcc_declaration:
15006       if (!origin)
15007         origin = decl_ultimate_origin (node);
15008       if (origin != NULL)
15009         add_abstract_origin_attribute (parm_die, origin);
15010       else
15011         {
15012           tree type = TREE_TYPE (node);
15013           add_name_and_src_coords_attributes (parm_die, node);
15014           if (DECL_BY_REFERENCE (node))
15015             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
15016                                 context_die);
15017           else
15018             add_type_attribute (parm_die, type,
15019                                 TREE_READONLY (node),
15020                                 TREE_THIS_VOLATILE (node),
15021                                 context_die);
15022           if (DECL_ARTIFICIAL (node))
15023             add_AT_flag (parm_die, DW_AT_artificial, 1);
15024         }
15025
15026       if (node && node != origin)
15027         equate_decl_number_to_die (node, parm_die);
15028       if (! DECL_ABSTRACT (node_or_origin))
15029         add_location_or_const_value_attribute (parm_die, node_or_origin,
15030                                                DW_AT_location);
15031
15032       break;
15033
15034     case tcc_type:
15035       /* We were called with some kind of a ..._TYPE node.  */
15036       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
15037       break;
15038
15039     default:
15040       gcc_unreachable ();
15041     }
15042
15043   return parm_die;
15044 }
15045
15046 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
15047    at the end of an (ANSI prototyped) formal parameters list.  */
15048
15049 static void
15050 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
15051 {
15052   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
15053 }
15054
15055 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
15056    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
15057    parameters as specified in some function type specification (except for
15058    those which appear as part of a function *definition*).  */
15059
15060 static void
15061 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
15062 {
15063   tree link;
15064   tree formal_type = NULL;
15065   tree first_parm_type;
15066   tree arg;
15067
15068   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
15069     {
15070       arg = DECL_ARGUMENTS (function_or_method_type);
15071       function_or_method_type = TREE_TYPE (function_or_method_type);
15072     }
15073   else
15074     arg = NULL_TREE;
15075
15076   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
15077
15078   /* Make our first pass over the list of formal parameter types and output a
15079      DW_TAG_formal_parameter DIE for each one.  */
15080   for (link = first_parm_type; link; )
15081     {
15082       dw_die_ref parm_die;
15083
15084       formal_type = TREE_VALUE (link);
15085       if (formal_type == void_type_node)
15086         break;
15087
15088       /* Output a (nameless) DIE to represent the formal parameter itself.  */
15089       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
15090       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
15091            && link == first_parm_type)
15092           || (arg && DECL_ARTIFICIAL (arg)))
15093         add_AT_flag (parm_die, DW_AT_artificial, 1);
15094
15095       link = TREE_CHAIN (link);
15096       if (arg)
15097         arg = TREE_CHAIN (arg);
15098     }
15099
15100   /* If this function type has an ellipsis, add a
15101      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
15102   if (formal_type != void_type_node)
15103     gen_unspecified_parameters_die (function_or_method_type, context_die);
15104
15105   /* Make our second (and final) pass over the list of formal parameter types
15106      and output DIEs to represent those types (as necessary).  */
15107   for (link = TYPE_ARG_TYPES (function_or_method_type);
15108        link && TREE_VALUE (link);
15109        link = TREE_CHAIN (link))
15110     gen_type_die (TREE_VALUE (link), context_die);
15111 }
15112
15113 /* We want to generate the DIE for TYPE so that we can generate the
15114    die for MEMBER, which has been defined; we will need to refer back
15115    to the member declaration nested within TYPE.  If we're trying to
15116    generate minimal debug info for TYPE, processing TYPE won't do the
15117    trick; we need to attach the member declaration by hand.  */
15118
15119 static void
15120 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
15121 {
15122   gen_type_die (type, context_die);
15123
15124   /* If we're trying to avoid duplicate debug info, we may not have
15125      emitted the member decl for this function.  Emit it now.  */
15126   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
15127       && ! lookup_decl_die (member))
15128     {
15129       dw_die_ref type_die;
15130       gcc_assert (!decl_ultimate_origin (member));
15131
15132       push_decl_scope (type);
15133       type_die = lookup_type_die (type);
15134       if (TREE_CODE (member) == FUNCTION_DECL)
15135         gen_subprogram_die (member, type_die);
15136       else if (TREE_CODE (member) == FIELD_DECL)
15137         {
15138           /* Ignore the nameless fields that are used to skip bits but handle
15139              C++ anonymous unions and structs.  */
15140           if (DECL_NAME (member) != NULL_TREE
15141               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
15142               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
15143             {
15144               gen_type_die (member_declared_type (member), type_die);
15145               gen_field_die (member, type_die);
15146             }
15147         }
15148       else
15149         gen_variable_die (member, NULL_TREE, type_die);
15150
15151       pop_decl_scope ();
15152     }
15153 }
15154
15155 /* Generate the DWARF2 info for the "abstract" instance of a function which we
15156    may later generate inlined and/or out-of-line instances of.  */
15157
15158 static void
15159 dwarf2out_abstract_function (tree decl)
15160 {
15161   dw_die_ref old_die;
15162   tree save_fn;
15163   tree context;
15164   int was_abstract = DECL_ABSTRACT (decl);
15165
15166   /* Make sure we have the actual abstract inline, not a clone.  */
15167   decl = DECL_ORIGIN (decl);
15168   htab_empty (decl_loc_table);
15169
15170   old_die = lookup_decl_die (decl);
15171   if (old_die && get_AT (old_die, DW_AT_inline))
15172     /* We've already generated the abstract instance.  */
15173     return;
15174
15175   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
15176      we don't get confused by DECL_ABSTRACT.  */
15177   if (debug_info_level > DINFO_LEVEL_TERSE)
15178     {
15179       context = decl_class_context (decl);
15180       if (context)
15181         gen_type_die_for_member
15182           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
15183     }
15184
15185   /* Pretend we've just finished compiling this function.  */
15186   save_fn = current_function_decl;
15187   current_function_decl = decl;
15188   push_cfun (DECL_STRUCT_FUNCTION (decl));
15189
15190   set_decl_abstract_flags (decl, 1);
15191   dwarf2out_decl (decl);
15192   if (! was_abstract)
15193     set_decl_abstract_flags (decl, 0);
15194
15195   current_function_decl = save_fn;
15196   pop_cfun ();
15197 }
15198
15199 /* Helper function of premark_used_types() which gets called through
15200    htab_traverse_resize().
15201
15202    Marks the DIE of a given type in *SLOT as perennial, so it never gets
15203    marked as unused by prune_unused_types.  */
15204 static int
15205 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
15206 {
15207   tree type;
15208   dw_die_ref die;
15209
15210   type = (tree) *slot;
15211   die = lookup_type_die (type);
15212   if (die != NULL)
15213     die->die_perennial_p = 1;
15214   return 1;
15215 }
15216
15217 /* Mark all members of used_types_hash as perennial.  */
15218 static void
15219 premark_used_types (void)
15220 {
15221   if (cfun && cfun->used_types_hash)
15222     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
15223 }
15224
15225 /* Generate a DIE to represent a declared function (either file-scope or
15226    block-local).  */
15227
15228 static void
15229 gen_subprogram_die (tree decl, dw_die_ref context_die)
15230 {
15231   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15232   tree origin = decl_ultimate_origin (decl);
15233   dw_die_ref subr_die;
15234   tree fn_arg_types;
15235   tree outer_scope;
15236   dw_die_ref old_die = lookup_decl_die (decl);
15237   int declaration = (current_function_decl != decl
15238                      || class_or_namespace_scope_p (context_die));
15239
15240   premark_used_types ();
15241
15242   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
15243      started to generate the abstract instance of an inline, decided to output
15244      its containing class, and proceeded to emit the declaration of the inline
15245      from the member list for the class.  If so, DECLARATION takes priority;
15246      we'll get back to the abstract instance when done with the class.  */
15247
15248   /* The class-scope declaration DIE must be the primary DIE.  */
15249   if (origin && declaration && class_or_namespace_scope_p (context_die))
15250     {
15251       origin = NULL;
15252       gcc_assert (!old_die);
15253     }
15254
15255   /* Now that the C++ front end lazily declares artificial member fns, we
15256      might need to retrofit the declaration into its class.  */
15257   if (!declaration && !origin && !old_die
15258       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
15259       && !class_or_namespace_scope_p (context_die)
15260       && debug_info_level > DINFO_LEVEL_TERSE)
15261     old_die = force_decl_die (decl);
15262
15263   if (origin != NULL)
15264     {
15265       gcc_assert (!declaration || local_scope_p (context_die));
15266
15267       /* Fixup die_parent for the abstract instance of a nested
15268          inline function.  */
15269       if (old_die && old_die->die_parent == NULL)
15270         add_child_die (context_die, old_die);
15271
15272       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15273       add_abstract_origin_attribute (subr_die, origin);
15274     }
15275   else if (old_die)
15276     {
15277       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
15278       struct dwarf_file_data * file_index = lookup_filename (s.file);
15279
15280       if (!get_AT_flag (old_die, DW_AT_declaration)
15281           /* We can have a normal definition following an inline one in the
15282              case of redefinition of GNU C extern inlines.
15283              It seems reasonable to use AT_specification in this case.  */
15284           && !get_AT (old_die, DW_AT_inline))
15285         {
15286           /* Detect and ignore this case, where we are trying to output
15287              something we have already output.  */
15288           return;
15289         }
15290
15291       /* If the definition comes from the same place as the declaration,
15292          maybe use the old DIE.  We always want the DIE for this function
15293          that has the *_pc attributes to be under comp_unit_die so the
15294          debugger can find it.  We also need to do this for abstract
15295          instances of inlines, since the spec requires the out-of-line copy
15296          to have the same parent.  For local class methods, this doesn't
15297          apply; we just use the old DIE.  */
15298       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
15299           && (DECL_ARTIFICIAL (decl)
15300               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
15301                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
15302                       == (unsigned) s.line))))
15303         {
15304           subr_die = old_die;
15305
15306           /* Clear out the declaration attribute and the formal parameters.
15307              Do not remove all children, because it is possible that this
15308              declaration die was forced using force_decl_die(). In such
15309              cases die that forced declaration die (e.g. TAG_imported_module)
15310              is one of the children that we do not want to remove.  */
15311           remove_AT (subr_die, DW_AT_declaration);
15312           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
15313         }
15314       else
15315         {
15316           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15317           add_AT_specification (subr_die, old_die);
15318           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
15319             add_AT_file (subr_die, DW_AT_decl_file, file_index);
15320           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
15321             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
15322         }
15323     }
15324   else
15325     {
15326       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15327
15328       if (TREE_PUBLIC (decl))
15329         add_AT_flag (subr_die, DW_AT_external, 1);
15330
15331       add_name_and_src_coords_attributes (subr_die, decl);
15332       if (debug_info_level > DINFO_LEVEL_TERSE)
15333         {
15334           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
15335           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
15336                               0, 0, context_die);
15337         }
15338
15339       add_pure_or_virtual_attribute (subr_die, decl);
15340       if (DECL_ARTIFICIAL (decl))
15341         add_AT_flag (subr_die, DW_AT_artificial, 1);
15342
15343       if (TREE_PROTECTED (decl))
15344         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
15345       else if (TREE_PRIVATE (decl))
15346         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
15347     }
15348
15349   if (declaration)
15350     {
15351       if (!old_die || !get_AT (old_die, DW_AT_inline))
15352         {
15353           add_AT_flag (subr_die, DW_AT_declaration, 1);
15354
15355           /* If this is an explicit function declaration then generate
15356              a DW_AT_explicit attribute.  */
15357           if (lang_hooks.decls.function_decl_explicit_p (decl))
15358             add_AT_flag (subr_die, DW_AT_explicit, 1);
15359
15360           /* The first time we see a member function, it is in the context of
15361              the class to which it belongs.  We make sure of this by emitting
15362              the class first.  The next time is the definition, which is
15363              handled above.  The two may come from the same source text.
15364
15365              Note that force_decl_die() forces function declaration die. It is
15366              later reused to represent definition.  */
15367           equate_decl_number_to_die (decl, subr_die);
15368         }
15369     }
15370   else if (DECL_ABSTRACT (decl))
15371     {
15372       if (DECL_DECLARED_INLINE_P (decl))
15373         {
15374           if (cgraph_function_possibly_inlined_p (decl))
15375             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
15376           else
15377             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
15378         }
15379       else
15380         {
15381           if (cgraph_function_possibly_inlined_p (decl))
15382             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
15383           else
15384             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
15385         }
15386
15387       if (DECL_DECLARED_INLINE_P (decl)
15388           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
15389         add_AT_flag (subr_die, DW_AT_artificial, 1);
15390
15391       equate_decl_number_to_die (decl, subr_die);
15392     }
15393   else if (!DECL_EXTERNAL (decl))
15394     {
15395       HOST_WIDE_INT cfa_fb_offset;
15396
15397       if (!old_die || !get_AT (old_die, DW_AT_inline))
15398         equate_decl_number_to_die (decl, subr_die);
15399
15400       if (!flag_reorder_blocks_and_partition)
15401         {
15402           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
15403                                        current_function_funcdef_no);
15404           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
15405           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15406                                        current_function_funcdef_no);
15407           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
15408
15409           add_pubname (decl, subr_die);
15410           add_arange (decl, subr_die);
15411         }
15412       else
15413         {  /* Do nothing for now; maybe need to duplicate die, one for
15414               hot section and one for cold section, then use the hot/cold
15415               section begin/end labels to generate the aranges...  */
15416           /*
15417             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
15418             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
15419             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
15420             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
15421
15422             add_pubname (decl, subr_die);
15423             add_arange (decl, subr_die);
15424             add_arange (decl, subr_die);
15425            */
15426         }
15427
15428 #ifdef MIPS_DEBUGGING_INFO
15429       /* Add a reference to the FDE for this routine.  */
15430       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
15431 #endif
15432
15433       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
15434
15435       /* We define the "frame base" as the function's CFA.  This is more
15436          convenient for several reasons: (1) It's stable across the prologue
15437          and epilogue, which makes it better than just a frame pointer,
15438          (2) With dwarf3, there exists a one-byte encoding that allows us
15439          to reference the .debug_frame data by proxy, but failing that,
15440          (3) We can at least reuse the code inspection and interpretation
15441          code that determines the CFA position at various points in the
15442          function.  */
15443       if (dwarf_version >= 3)
15444         {
15445           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
15446           add_AT_loc (subr_die, DW_AT_frame_base, op);
15447         }
15448       else
15449         {
15450           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
15451           if (list->dw_loc_next)
15452             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
15453           else
15454             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
15455         }
15456
15457       /* Compute a displacement from the "steady-state frame pointer" to
15458          the CFA.  The former is what all stack slots and argument slots
15459          will reference in the rtl; the later is what we've told the
15460          debugger about.  We'll need to adjust all frame_base references
15461          by this displacement.  */
15462       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
15463
15464       if (cfun->static_chain_decl)
15465         add_AT_location_description (subr_die, DW_AT_static_link,
15466                  loc_descriptor_from_tree (cfun->static_chain_decl));
15467     }
15468
15469   /* Generate child dies for template paramaters.  */
15470   if (debug_info_level > DINFO_LEVEL_TERSE)
15471     gen_generic_params_dies (decl);
15472
15473   /* Now output descriptions of the arguments for this function. This gets
15474      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
15475      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
15476      `...' at the end of the formal parameter list.  In order to find out if
15477      there was a trailing ellipsis or not, we must instead look at the type
15478      associated with the FUNCTION_DECL.  This will be a node of type
15479      FUNCTION_TYPE. If the chain of type nodes hanging off of this
15480      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
15481      an ellipsis at the end.  */
15482
15483   /* In the case where we are describing a mere function declaration, all we
15484      need to do here (and all we *can* do here) is to describe the *types* of
15485      its formal parameters.  */
15486   if (debug_info_level <= DINFO_LEVEL_TERSE)
15487     ;
15488   else if (declaration)
15489     gen_formal_types_die (decl, subr_die);
15490   else
15491     {
15492       /* Generate DIEs to represent all known formal parameters.  */
15493       tree arg_decls = DECL_ARGUMENTS (decl);
15494       tree parm;
15495
15496       /* When generating DIEs, generate the unspecified_parameters DIE
15497          instead if we come across the arg "__builtin_va_alist" */
15498       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
15499         if (TREE_CODE (parm) == PARM_DECL)
15500           {
15501             if (DECL_NAME (parm)
15502                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
15503                             "__builtin_va_alist"))
15504               gen_unspecified_parameters_die (parm, subr_die);
15505             else
15506               gen_decl_die (parm, NULL, subr_die);
15507           }
15508
15509       /* Decide whether we need an unspecified_parameters DIE at the end.
15510          There are 2 more cases to do this for: 1) the ansi ... declaration -
15511          this is detectable when the end of the arg list is not a
15512          void_type_node 2) an unprototyped function declaration (not a
15513          definition).  This just means that we have no info about the
15514          parameters at all.  */
15515       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
15516       if (fn_arg_types != NULL)
15517         {
15518           /* This is the prototyped case, check for....  */
15519           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
15520             gen_unspecified_parameters_die (decl, subr_die);
15521         }
15522       else if (DECL_INITIAL (decl) == NULL_TREE)
15523         gen_unspecified_parameters_die (decl, subr_die);
15524     }
15525
15526   /* Output Dwarf info for all of the stuff within the body of the function
15527      (if it has one - it may be just a declaration).  */
15528   outer_scope = DECL_INITIAL (decl);
15529
15530   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
15531      a function.  This BLOCK actually represents the outermost binding contour
15532      for the function, i.e. the contour in which the function's formal
15533      parameters and labels get declared. Curiously, it appears that the front
15534      end doesn't actually put the PARM_DECL nodes for the current function onto
15535      the BLOCK_VARS list for this outer scope, but are strung off of the
15536      DECL_ARGUMENTS list for the function instead.
15537
15538      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
15539      the LABEL_DECL nodes for the function however, and we output DWARF info
15540      for those in decls_for_scope.  Just within the `outer_scope' there will be
15541      a BLOCK node representing the function's outermost pair of curly braces,
15542      and any blocks used for the base and member initializers of a C++
15543      constructor function.  */
15544   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
15545     {
15546       /* Emit a DW_TAG_variable DIE for a named return value.  */
15547       if (DECL_NAME (DECL_RESULT (decl)))
15548         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
15549
15550       current_function_has_inlines = 0;
15551       decls_for_scope (outer_scope, subr_die, 0);
15552
15553 #if 0 && defined (MIPS_DEBUGGING_INFO)
15554       if (current_function_has_inlines)
15555         {
15556           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
15557           if (! comp_unit_has_inlines)
15558             {
15559               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
15560               comp_unit_has_inlines = 1;
15561             }
15562         }
15563 #endif
15564     }
15565   /* Add the calling convention attribute if requested.  */
15566   add_calling_convention_attribute (subr_die, decl);
15567
15568 }
15569
15570 /* Returns a hash value for X (which really is a die_struct).  */
15571
15572 static hashval_t
15573 common_block_die_table_hash (const void *x)
15574 {
15575   const_dw_die_ref d = (const_dw_die_ref) x;
15576   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
15577 }
15578
15579 /* Return nonzero if decl_id and die_parent of die_struct X is the same
15580    as decl_id and die_parent of die_struct Y.  */
15581
15582 static int
15583 common_block_die_table_eq (const void *x, const void *y)
15584 {
15585   const_dw_die_ref d = (const_dw_die_ref) x;
15586   const_dw_die_ref e = (const_dw_die_ref) y;
15587   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
15588 }
15589
15590 /* Generate a DIE to represent a declared data object.
15591    Either DECL or ORIGIN must be non-null.  */
15592
15593 static void
15594 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
15595 {
15596   HOST_WIDE_INT off;
15597   tree com_decl;
15598   tree decl_or_origin = decl ? decl : origin;
15599   dw_die_ref var_die;
15600   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
15601   dw_die_ref origin_die;
15602   int declaration = (DECL_EXTERNAL (decl_or_origin)
15603                      /* If DECL is COMDAT and has not actually been
15604                         emitted, we cannot take its address; there
15605                         might end up being no definition anywhere in
15606                         the program.  For example, consider the C++
15607                         test case:
15608
15609                           template <class T>
15610                           struct S { static const int i = 7; };
15611
15612                           template <class T>
15613                           const int S<T>::i;
15614
15615                           int f() { return S<int>::i; }
15616
15617                         Here, S<int>::i is not DECL_EXTERNAL, but no
15618                         definition is required, so the compiler will
15619                         not emit a definition.  */
15620                      || (TREE_CODE (decl_or_origin) == VAR_DECL
15621                          && DECL_COMDAT (decl_or_origin)
15622                          && !TREE_ASM_WRITTEN (decl_or_origin))
15623                      || class_or_namespace_scope_p (context_die));
15624
15625   if (!origin)
15626     origin = decl_ultimate_origin (decl);
15627
15628   com_decl = fortran_common (decl_or_origin, &off);
15629
15630   /* Symbol in common gets emitted as a child of the common block, in the form
15631      of a data member.  */
15632   if (com_decl)
15633     {
15634       tree field;
15635       dw_die_ref com_die;
15636       dw_loc_descr_ref loc;
15637       die_node com_die_arg;
15638
15639       var_die = lookup_decl_die (decl_or_origin);
15640       if (var_die)
15641         {
15642           if (get_AT (var_die, DW_AT_location) == NULL)
15643             {
15644               loc = loc_descriptor_from_tree (com_decl);
15645               if (loc)
15646                 {
15647                   if (off)
15648                     {
15649                       /* Optimize the common case.  */
15650                       if (loc->dw_loc_opc == DW_OP_addr
15651                           && loc->dw_loc_next == NULL
15652                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
15653                              == SYMBOL_REF)
15654                         loc->dw_loc_oprnd1.v.val_addr
15655                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
15656                         else
15657                           loc_descr_plus_const (&loc, off);
15658                     }
15659                   add_AT_loc (var_die, DW_AT_location, loc);
15660                   remove_AT (var_die, DW_AT_declaration);
15661                 }
15662             }
15663           return;
15664         }
15665
15666       if (common_block_die_table == NULL)
15667         common_block_die_table
15668           = htab_create_ggc (10, common_block_die_table_hash,
15669                              common_block_die_table_eq, NULL);
15670
15671       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
15672       com_die_arg.decl_id = DECL_UID (com_decl);
15673       com_die_arg.die_parent = context_die;
15674       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
15675       loc = loc_descriptor_from_tree (com_decl);
15676       if (com_die == NULL)
15677         {
15678           const char *cnam
15679             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
15680           void **slot;
15681
15682           com_die = new_die (DW_TAG_common_block, context_die, decl);
15683           add_name_and_src_coords_attributes (com_die, com_decl);
15684           if (loc)
15685             {
15686               add_AT_loc (com_die, DW_AT_location, loc);
15687               /* Avoid sharing the same loc descriptor between
15688                  DW_TAG_common_block and DW_TAG_variable.  */
15689               loc = loc_descriptor_from_tree (com_decl);
15690             }
15691           else if (DECL_EXTERNAL (decl))
15692             add_AT_flag (com_die, DW_AT_declaration, 1);
15693           add_pubname_string (cnam, com_die); /* ??? needed? */
15694           com_die->decl_id = DECL_UID (com_decl);
15695           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
15696           *slot = (void *) com_die;
15697         }
15698       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
15699         {
15700           add_AT_loc (com_die, DW_AT_location, loc);
15701           loc = loc_descriptor_from_tree (com_decl);
15702           remove_AT (com_die, DW_AT_declaration);
15703         }
15704       var_die = new_die (DW_TAG_variable, com_die, decl);
15705       add_name_and_src_coords_attributes (var_die, decl);
15706       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
15707                           TREE_THIS_VOLATILE (decl), context_die);
15708       add_AT_flag (var_die, DW_AT_external, 1);
15709       if (loc)
15710         {
15711           if (off)
15712             {
15713               /* Optimize the common case.  */
15714               if (loc->dw_loc_opc == DW_OP_addr
15715                   && loc->dw_loc_next == NULL
15716                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
15717                 loc->dw_loc_oprnd1.v.val_addr
15718                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
15719               else
15720                 loc_descr_plus_const (&loc, off);
15721             }
15722           add_AT_loc (var_die, DW_AT_location, loc);
15723         }
15724       else if (DECL_EXTERNAL (decl))
15725         add_AT_flag (var_die, DW_AT_declaration, 1);
15726       equate_decl_number_to_die (decl, var_die);
15727       return;
15728     }
15729
15730   /* If the compiler emitted a definition for the DECL declaration
15731      and if we already emitted a DIE for it, don't emit a second
15732      DIE for it again.  */
15733   if (old_die
15734       && declaration
15735       && old_die->die_parent == context_die)
15736     return;
15737
15738   /* For static data members, the declaration in the class is supposed
15739      to have DW_TAG_member tag; the specification should still be
15740      DW_TAG_variable referencing the DW_TAG_member DIE.  */
15741   if (declaration && class_scope_p (context_die))
15742     var_die = new_die (DW_TAG_member, context_die, decl);
15743   else
15744     var_die = new_die (DW_TAG_variable, context_die, decl);
15745
15746   origin_die = NULL;
15747   if (origin != NULL)
15748     origin_die = add_abstract_origin_attribute (var_die, origin);
15749
15750   /* Loop unrolling can create multiple blocks that refer to the same
15751      static variable, so we must test for the DW_AT_declaration flag.
15752
15753      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
15754      copy decls and set the DECL_ABSTRACT flag on them instead of
15755      sharing them.
15756
15757      ??? Duplicated blocks have been rewritten to use .debug_ranges.
15758
15759      ??? The declare_in_namespace support causes us to get two DIEs for one
15760      variable, both of which are declarations.  We want to avoid considering
15761      one to be a specification, so we must test that this DIE is not a
15762      declaration.  */
15763   else if (old_die && TREE_STATIC (decl) && ! declaration
15764            && get_AT_flag (old_die, DW_AT_declaration) == 1)
15765     {
15766       /* This is a definition of a C++ class level static.  */
15767       add_AT_specification (var_die, old_die);
15768       if (DECL_NAME (decl))
15769         {
15770           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
15771           struct dwarf_file_data * file_index = lookup_filename (s.file);
15772
15773           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
15774             add_AT_file (var_die, DW_AT_decl_file, file_index);
15775
15776           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
15777             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
15778         }
15779     }
15780   else
15781     {
15782       tree type = TREE_TYPE (decl);
15783
15784       add_name_and_src_coords_attributes (var_die, decl);
15785       if ((TREE_CODE (decl) == PARM_DECL
15786            || TREE_CODE (decl) == RESULT_DECL
15787            || TREE_CODE (decl) == VAR_DECL)
15788           && DECL_BY_REFERENCE (decl))
15789         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
15790       else
15791         add_type_attribute (var_die, type, TREE_READONLY (decl),
15792                             TREE_THIS_VOLATILE (decl), context_die);
15793
15794       if (TREE_PUBLIC (decl))
15795         add_AT_flag (var_die, DW_AT_external, 1);
15796
15797       if (DECL_ARTIFICIAL (decl))
15798         add_AT_flag (var_die, DW_AT_artificial, 1);
15799
15800       if (TREE_PROTECTED (decl))
15801         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
15802       else if (TREE_PRIVATE (decl))
15803         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
15804     }
15805
15806   if (declaration)
15807     add_AT_flag (var_die, DW_AT_declaration, 1);
15808
15809   if (decl && (DECL_ABSTRACT (decl) || declaration))
15810     equate_decl_number_to_die (decl, var_die);
15811
15812   if (! declaration
15813       && (! DECL_ABSTRACT (decl_or_origin)
15814           /* Local static vars are shared between all clones/inlines,
15815              so emit DW_AT_location on the abstract DIE if DECL_RTL is
15816              already set.  */
15817           || (TREE_CODE (decl_or_origin) == VAR_DECL
15818               && TREE_STATIC (decl_or_origin)
15819               && DECL_RTL_SET_P (decl_or_origin)))
15820       /* When abstract origin already has DW_AT_location attribute, no need
15821          to add it again.  */
15822       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
15823     {
15824       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
15825           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
15826         defer_location (decl_or_origin, var_die);
15827       else
15828         add_location_or_const_value_attribute (var_die,
15829                                                decl_or_origin,
15830                                                DW_AT_location);
15831       add_pubname (decl_or_origin, var_die);
15832     }
15833   else
15834     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
15835 }
15836
15837 /* Generate a DIE to represent a named constant.  */
15838
15839 static void
15840 gen_const_die (tree decl, dw_die_ref context_die)
15841 {
15842   dw_die_ref const_die;
15843   tree type = TREE_TYPE (decl);
15844
15845   const_die = new_die (DW_TAG_constant, context_die, decl);
15846   add_name_and_src_coords_attributes (const_die, decl);
15847   add_type_attribute (const_die, type, 1, 0, context_die);
15848   if (TREE_PUBLIC (decl))
15849     add_AT_flag (const_die, DW_AT_external, 1);
15850   if (DECL_ARTIFICIAL (decl))
15851     add_AT_flag (const_die, DW_AT_artificial, 1);
15852   tree_add_const_value_attribute_for_decl (const_die, decl);
15853 }
15854
15855 /* Generate a DIE to represent a label identifier.  */
15856
15857 static void
15858 gen_label_die (tree decl, dw_die_ref context_die)
15859 {
15860   tree origin = decl_ultimate_origin (decl);
15861   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
15862   rtx insn;
15863   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15864
15865   if (origin != NULL)
15866     add_abstract_origin_attribute (lbl_die, origin);
15867   else
15868     add_name_and_src_coords_attributes (lbl_die, decl);
15869
15870   if (DECL_ABSTRACT (decl))
15871     equate_decl_number_to_die (decl, lbl_die);
15872   else
15873     {
15874       insn = DECL_RTL_IF_SET (decl);
15875
15876       /* Deleted labels are programmer specified labels which have been
15877          eliminated because of various optimizations.  We still emit them
15878          here so that it is possible to put breakpoints on them.  */
15879       if (insn
15880           && (LABEL_P (insn)
15881               || ((NOTE_P (insn)
15882                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
15883         {
15884           /* When optimization is enabled (via -O) some parts of the compiler
15885              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
15886              represent source-level labels which were explicitly declared by
15887              the user.  This really shouldn't be happening though, so catch
15888              it if it ever does happen.  */
15889           gcc_assert (!INSN_DELETED_P (insn));
15890
15891           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
15892           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
15893         }
15894     }
15895 }
15896
15897 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
15898    attributes to the DIE for a block STMT, to describe where the inlined
15899    function was called from.  This is similar to add_src_coords_attributes.  */
15900
15901 static inline void
15902 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
15903 {
15904   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
15905
15906   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
15907   add_AT_unsigned (die, DW_AT_call_line, s.line);
15908 }
15909
15910
15911 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
15912    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
15913
15914 static inline void
15915 add_high_low_attributes (tree stmt, dw_die_ref die)
15916 {
15917   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15918
15919   if (BLOCK_FRAGMENT_CHAIN (stmt))
15920     {
15921       tree chain;
15922
15923       if (inlined_function_outer_scope_p (stmt))
15924         {
15925           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
15926                                        BLOCK_NUMBER (stmt));
15927           add_AT_lbl_id (die, DW_AT_entry_pc, label);
15928         }
15929
15930       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
15931
15932       chain = BLOCK_FRAGMENT_CHAIN (stmt);
15933       do
15934         {
15935           add_ranges (chain);
15936           chain = BLOCK_FRAGMENT_CHAIN (chain);
15937         }
15938       while (chain);
15939       add_ranges (NULL);
15940     }
15941   else
15942     {
15943       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
15944                                    BLOCK_NUMBER (stmt));
15945       add_AT_lbl_id (die, DW_AT_low_pc, label);
15946       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
15947                                    BLOCK_NUMBER (stmt));
15948       add_AT_lbl_id (die, DW_AT_high_pc, label);
15949     }
15950 }
15951
15952 /* Generate a DIE for a lexical block.  */
15953
15954 static void
15955 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
15956 {
15957   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
15958
15959   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
15960     add_high_low_attributes (stmt, stmt_die);
15961
15962   decls_for_scope (stmt, stmt_die, depth);
15963 }
15964
15965 /* Generate a DIE for an inlined subprogram.  */
15966
15967 static void
15968 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
15969 {
15970   tree decl;
15971
15972   /* The instance of function that is effectively being inlined shall not
15973      be abstract.  */
15974   gcc_assert (! BLOCK_ABSTRACT (stmt));
15975
15976   decl = block_ultimate_origin (stmt);
15977
15978   /* Emit info for the abstract instance first, if we haven't yet.  We
15979      must emit this even if the block is abstract, otherwise when we
15980      emit the block below (or elsewhere), we may end up trying to emit
15981      a die whose origin die hasn't been emitted, and crashing.  */
15982   dwarf2out_abstract_function (decl);
15983
15984   if (! BLOCK_ABSTRACT (stmt))
15985     {
15986       dw_die_ref subr_die
15987         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
15988
15989       add_abstract_origin_attribute (subr_die, decl);
15990       if (TREE_ASM_WRITTEN (stmt))
15991         add_high_low_attributes (stmt, subr_die);
15992       add_call_src_coords_attributes (stmt, subr_die);
15993
15994       decls_for_scope (stmt, subr_die, depth);
15995       current_function_has_inlines = 1;
15996     }
15997 }
15998
15999 /* Generate a DIE for a field in a record, or structure.  */
16000
16001 static void
16002 gen_field_die (tree decl, dw_die_ref context_die)
16003 {
16004   dw_die_ref decl_die;
16005
16006   if (TREE_TYPE (decl) == error_mark_node)
16007     return;
16008
16009   decl_die = new_die (DW_TAG_member, context_die, decl);
16010   add_name_and_src_coords_attributes (decl_die, decl);
16011   add_type_attribute (decl_die, member_declared_type (decl),
16012                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
16013                       context_die);
16014
16015   if (DECL_BIT_FIELD_TYPE (decl))
16016     {
16017       add_byte_size_attribute (decl_die, decl);
16018       add_bit_size_attribute (decl_die, decl);
16019       add_bit_offset_attribute (decl_die, decl);
16020     }
16021
16022   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
16023     add_data_member_location_attribute (decl_die, decl);
16024
16025   if (DECL_ARTIFICIAL (decl))
16026     add_AT_flag (decl_die, DW_AT_artificial, 1);
16027
16028   if (TREE_PROTECTED (decl))
16029     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
16030   else if (TREE_PRIVATE (decl))
16031     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
16032
16033   /* Equate decl number to die, so that we can look up this decl later on.  */
16034   equate_decl_number_to_die (decl, decl_die);
16035 }
16036
16037 #if 0
16038 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16039    Use modified_type_die instead.
16040    We keep this code here just in case these types of DIEs may be needed to
16041    represent certain things in other languages (e.g. Pascal) someday.  */
16042
16043 static void
16044 gen_pointer_type_die (tree type, dw_die_ref context_die)
16045 {
16046   dw_die_ref ptr_die
16047     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
16048
16049   equate_type_number_to_die (type, ptr_die);
16050   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16051   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16052 }
16053
16054 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16055    Use modified_type_die instead.
16056    We keep this code here just in case these types of DIEs may be needed to
16057    represent certain things in other languages (e.g. Pascal) someday.  */
16058
16059 static void
16060 gen_reference_type_die (tree type, dw_die_ref context_die)
16061 {
16062   dw_die_ref ref_die
16063     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
16064
16065   equate_type_number_to_die (type, ref_die);
16066   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
16067   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16068 }
16069 #endif
16070
16071 /* Generate a DIE for a pointer to a member type.  */
16072
16073 static void
16074 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
16075 {
16076   dw_die_ref ptr_die
16077     = new_die (DW_TAG_ptr_to_member_type,
16078                scope_die_for (type, context_die), type);
16079
16080   equate_type_number_to_die (type, ptr_die);
16081   add_AT_die_ref (ptr_die, DW_AT_containing_type,
16082                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
16083   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16084 }
16085
16086 /* Generate the DIE for the compilation unit.  */
16087
16088 static dw_die_ref
16089 gen_compile_unit_die (const char *filename)
16090 {
16091   dw_die_ref die;
16092   char producer[250];
16093   const char *language_string = lang_hooks.name;
16094   int language;
16095
16096   die = new_die (DW_TAG_compile_unit, NULL, NULL);
16097
16098   if (filename)
16099     {
16100       add_name_attribute (die, filename);
16101       /* Don't add cwd for <built-in>.  */
16102       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
16103         add_comp_dir_attribute (die);
16104     }
16105
16106   sprintf (producer, "%s %s", language_string, version_string);
16107
16108 #ifdef MIPS_DEBUGGING_INFO
16109   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
16110      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
16111      not appear in the producer string, the debugger reaches the conclusion
16112      that the object file is stripped and has no debugging information.
16113      To get the MIPS/SGI debugger to believe that there is debugging
16114      information in the object file, we add a -g to the producer string.  */
16115   if (debug_info_level > DINFO_LEVEL_TERSE)
16116     strcat (producer, " -g");
16117 #endif
16118
16119   add_AT_string (die, DW_AT_producer, producer);
16120
16121   if (strcmp (language_string, "GNU C++") == 0)
16122     language = DW_LANG_C_plus_plus;
16123   else if (strcmp (language_string, "GNU Ada") == 0)
16124     language = DW_LANG_Ada95;
16125   else if (strcmp (language_string, "GNU F77") == 0)
16126     language = DW_LANG_Fortran77;
16127   else if (strcmp (language_string, "GNU Fortran") == 0)
16128     language = DW_LANG_Fortran95;
16129   else if (strcmp (language_string, "GNU Pascal") == 0)
16130     language = DW_LANG_Pascal83;
16131   else if (strcmp (language_string, "GNU Java") == 0)
16132     language = DW_LANG_Java;
16133   else if (strcmp (language_string, "GNU Objective-C") == 0)
16134     language = DW_LANG_ObjC;
16135   else if (strcmp (language_string, "GNU Objective-C++") == 0)
16136     language = DW_LANG_ObjC_plus_plus;
16137   else
16138     language = DW_LANG_C89;
16139
16140   add_AT_unsigned (die, DW_AT_language, language);
16141   return die;
16142 }
16143
16144 /* Generate the DIE for a base class.  */
16145
16146 static void
16147 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
16148 {
16149   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
16150
16151   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
16152   add_data_member_location_attribute (die, binfo);
16153
16154   if (BINFO_VIRTUAL_P (binfo))
16155     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16156
16157   if (access == access_public_node)
16158     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16159   else if (access == access_protected_node)
16160     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16161 }
16162
16163 /* Generate a DIE for a class member.  */
16164
16165 static void
16166 gen_member_die (tree type, dw_die_ref context_die)
16167 {
16168   tree member;
16169   tree binfo = TYPE_BINFO (type);
16170   dw_die_ref child;
16171
16172   /* If this is not an incomplete type, output descriptions of each of its
16173      members. Note that as we output the DIEs necessary to represent the
16174      members of this record or union type, we will also be trying to output
16175      DIEs to represent the *types* of those members. However the `type'
16176      function (above) will specifically avoid generating type DIEs for member
16177      types *within* the list of member DIEs for this (containing) type except
16178      for those types (of members) which are explicitly marked as also being
16179      members of this (containing) type themselves.  The g++ front- end can
16180      force any given type to be treated as a member of some other (containing)
16181      type by setting the TYPE_CONTEXT of the given (member) type to point to
16182      the TREE node representing the appropriate (containing) type.  */
16183
16184   /* First output info about the base classes.  */
16185   if (binfo)
16186     {
16187       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
16188       int i;
16189       tree base;
16190
16191       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
16192         gen_inheritance_die (base,
16193                              (accesses ? VEC_index (tree, accesses, i)
16194                               : access_public_node), context_die);
16195     }
16196
16197   /* Now output info about the data members and type members.  */
16198   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
16199     {
16200       /* If we thought we were generating minimal debug info for TYPE
16201          and then changed our minds, some of the member declarations
16202          may have already been defined.  Don't define them again, but
16203          do put them in the right order.  */
16204
16205       child = lookup_decl_die (member);
16206       if (child)
16207         splice_child_die (context_die, child);
16208       else
16209         gen_decl_die (member, NULL, context_die);
16210     }
16211
16212   /* Now output info about the function members (if any).  */
16213   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
16214     {
16215       /* Don't include clones in the member list.  */
16216       if (DECL_ABSTRACT_ORIGIN (member))
16217         continue;
16218
16219       child = lookup_decl_die (member);
16220       if (child)
16221         splice_child_die (context_die, child);
16222       else
16223         gen_decl_die (member, NULL, context_die);
16224     }
16225 }
16226
16227 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
16228    is set, we pretend that the type was never defined, so we only get the
16229    member DIEs needed by later specification DIEs.  */
16230
16231 static void
16232 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
16233                                 enum debug_info_usage usage)
16234 {
16235   dw_die_ref type_die = lookup_type_die (type);
16236   dw_die_ref scope_die = 0;
16237   int nested = 0;
16238   int complete = (TYPE_SIZE (type)
16239                   && (! TYPE_STUB_DECL (type)
16240                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
16241   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
16242   complete = complete && should_emit_struct_debug (type, usage);
16243
16244   if (type_die && ! complete)
16245     return;
16246
16247   if (TYPE_CONTEXT (type) != NULL_TREE
16248       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
16249           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
16250     nested = 1;
16251
16252   scope_die = scope_die_for (type, context_die);
16253
16254   if (! type_die || (nested && scope_die == comp_unit_die))
16255     /* First occurrence of type or toplevel definition of nested class.  */
16256     {
16257       dw_die_ref old_die = type_die;
16258
16259       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
16260                           ? record_type_tag (type) : DW_TAG_union_type,
16261                           scope_die, type);
16262       equate_type_number_to_die (type, type_die);
16263       if (old_die)
16264         add_AT_specification (type_die, old_die);
16265       else
16266         add_name_attribute (type_die, type_tag (type));
16267     }
16268   else
16269     remove_AT (type_die, DW_AT_declaration);
16270
16271   /* Generate child dies for template paramaters.  */
16272   if (debug_info_level > DINFO_LEVEL_TERSE
16273       && COMPLETE_TYPE_P (type))
16274     gen_generic_params_dies (type);
16275
16276   /* If this type has been completed, then give it a byte_size attribute and
16277      then give a list of members.  */
16278   if (complete && !ns_decl)
16279     {
16280       /* Prevent infinite recursion in cases where the type of some member of
16281          this type is expressed in terms of this type itself.  */
16282       TREE_ASM_WRITTEN (type) = 1;
16283       add_byte_size_attribute (type_die, type);
16284       if (TYPE_STUB_DECL (type) != NULL_TREE)
16285         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16286
16287       /* If the first reference to this type was as the return type of an
16288          inline function, then it may not have a parent.  Fix this now.  */
16289       if (type_die->die_parent == NULL)
16290         add_child_die (scope_die, type_die);
16291
16292       push_decl_scope (type);
16293       gen_member_die (type, type_die);
16294       pop_decl_scope ();
16295
16296       /* GNU extension: Record what type our vtable lives in.  */
16297       if (TYPE_VFIELD (type))
16298         {
16299           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
16300
16301           gen_type_die (vtype, context_die);
16302           add_AT_die_ref (type_die, DW_AT_containing_type,
16303                           lookup_type_die (vtype));
16304         }
16305     }
16306   else
16307     {
16308       add_AT_flag (type_die, DW_AT_declaration, 1);
16309
16310       /* We don't need to do this for function-local types.  */
16311       if (TYPE_STUB_DECL (type)
16312           && ! decl_function_context (TYPE_STUB_DECL (type)))
16313         VEC_safe_push (tree, gc, incomplete_types, type);
16314     }
16315
16316   if (get_AT (type_die, DW_AT_name))
16317     add_pubtype (type, type_die);
16318 }
16319
16320 /* Generate a DIE for a subroutine _type_.  */
16321
16322 static void
16323 gen_subroutine_type_die (tree type, dw_die_ref context_die)
16324 {
16325   tree return_type = TREE_TYPE (type);
16326   dw_die_ref subr_die
16327     = new_die (DW_TAG_subroutine_type,
16328                scope_die_for (type, context_die), type);
16329
16330   equate_type_number_to_die (type, subr_die);
16331   add_prototyped_attribute (subr_die, type);
16332   add_type_attribute (subr_die, return_type, 0, 0, context_die);
16333   gen_formal_types_die (type, subr_die);
16334
16335   if (get_AT (subr_die, DW_AT_name))
16336     add_pubtype (type, subr_die);
16337 }
16338
16339 /* Generate a DIE for a type definition.  */
16340
16341 static void
16342 gen_typedef_die (tree decl, dw_die_ref context_die)
16343 {
16344   dw_die_ref type_die;
16345   tree origin;
16346
16347   if (TREE_ASM_WRITTEN (decl))
16348     return;
16349
16350   TREE_ASM_WRITTEN (decl) = 1;
16351   type_die = new_die (DW_TAG_typedef, context_die, decl);
16352   origin = decl_ultimate_origin (decl);
16353   if (origin != NULL)
16354     add_abstract_origin_attribute (type_die, origin);
16355   else
16356     {
16357       tree type;
16358
16359       add_name_and_src_coords_attributes (type_die, decl);
16360       if (DECL_ORIGINAL_TYPE (decl))
16361         {
16362           type = DECL_ORIGINAL_TYPE (decl);
16363
16364           gcc_assert (type != TREE_TYPE (decl));
16365           equate_type_number_to_die (TREE_TYPE (decl), type_die);
16366         }
16367       else
16368         type = TREE_TYPE (decl);
16369
16370       add_type_attribute (type_die, type, TREE_READONLY (decl),
16371                           TREE_THIS_VOLATILE (decl), context_die);
16372     }
16373
16374   if (DECL_ABSTRACT (decl))
16375     equate_decl_number_to_die (decl, type_die);
16376
16377   if (get_AT (type_die, DW_AT_name))
16378     add_pubtype (decl, type_die);
16379 }
16380
16381 /* Generate a type description DIE.  */
16382
16383 static void
16384 gen_type_die_with_usage (tree type, dw_die_ref context_die,
16385                                 enum debug_info_usage usage)
16386 {
16387   int need_pop;
16388   struct array_descr_info info;
16389
16390   if (type == NULL_TREE || type == error_mark_node)
16391     return;
16392
16393   /* If TYPE is a typedef type variant, let's generate debug info
16394      for the parent typedef which TYPE is a type of.  */
16395   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16396       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
16397     {
16398       if (TREE_ASM_WRITTEN (type))
16399         return;
16400
16401       /* Prevent broken recursion; we can't hand off to the same type.  */
16402       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
16403
16404       /* Use the DIE of the containing namespace as the parent DIE of
16405          the type description DIE we want to generate.  */
16406       if (DECL_CONTEXT (TYPE_NAME (type))
16407           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
16408         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
16409
16410       TREE_ASM_WRITTEN (type) = 1;
16411       gen_decl_die (TYPE_NAME (type), NULL, context_die);
16412       return;
16413     }
16414
16415   /* If this is an array type with hidden descriptor, handle it first.  */
16416   if (!TREE_ASM_WRITTEN (type)
16417       && lang_hooks.types.get_array_descr_info
16418       && lang_hooks.types.get_array_descr_info (type, &info))
16419     {
16420       gen_descr_array_type_die (type, &info, context_die);
16421       TREE_ASM_WRITTEN (type) = 1;
16422       return;
16423     }
16424
16425   /* We are going to output a DIE to represent the unqualified version
16426      of this type (i.e. without any const or volatile qualifiers) so
16427      get the main variant (i.e. the unqualified version) of this type
16428      now.  (Vectors are special because the debugging info is in the
16429      cloned type itself).  */
16430   if (TREE_CODE (type) != VECTOR_TYPE)
16431     type = type_main_variant (type);
16432
16433   if (TREE_ASM_WRITTEN (type))
16434     return;
16435
16436   switch (TREE_CODE (type))
16437     {
16438     case ERROR_MARK:
16439       break;
16440
16441     case POINTER_TYPE:
16442     case REFERENCE_TYPE:
16443       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
16444          ensures that the gen_type_die recursion will terminate even if the
16445          type is recursive.  Recursive types are possible in Ada.  */
16446       /* ??? We could perhaps do this for all types before the switch
16447          statement.  */
16448       TREE_ASM_WRITTEN (type) = 1;
16449
16450       /* For these types, all that is required is that we output a DIE (or a
16451          set of DIEs) to represent the "basis" type.  */
16452       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16453                                 DINFO_USAGE_IND_USE);
16454       break;
16455
16456     case OFFSET_TYPE:
16457       /* This code is used for C++ pointer-to-data-member types.
16458          Output a description of the relevant class type.  */
16459       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
16460                                         DINFO_USAGE_IND_USE);
16461
16462       /* Output a description of the type of the object pointed to.  */
16463       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16464                                         DINFO_USAGE_IND_USE);
16465
16466       /* Now output a DIE to represent this pointer-to-data-member type
16467          itself.  */
16468       gen_ptr_to_mbr_type_die (type, context_die);
16469       break;
16470
16471     case FUNCTION_TYPE:
16472       /* Force out return type (in case it wasn't forced out already).  */
16473       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16474                                         DINFO_USAGE_DIR_USE);
16475       gen_subroutine_type_die (type, context_die);
16476       break;
16477
16478     case METHOD_TYPE:
16479       /* Force out return type (in case it wasn't forced out already).  */
16480       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16481                                         DINFO_USAGE_DIR_USE);
16482       gen_subroutine_type_die (type, context_die);
16483       break;
16484
16485     case ARRAY_TYPE:
16486       gen_array_type_die (type, context_die);
16487       break;
16488
16489     case VECTOR_TYPE:
16490       gen_array_type_die (type, context_die);
16491       break;
16492
16493     case ENUMERAL_TYPE:
16494     case RECORD_TYPE:
16495     case UNION_TYPE:
16496     case QUAL_UNION_TYPE:
16497       /* If this is a nested type whose containing class hasn't been written
16498          out yet, writing it out will cover this one, too.  This does not apply
16499          to instantiations of member class templates; they need to be added to
16500          the containing class as they are generated.  FIXME: This hurts the
16501          idea of combining type decls from multiple TUs, since we can't predict
16502          what set of template instantiations we'll get.  */
16503       if (TYPE_CONTEXT (type)
16504           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
16505           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
16506         {
16507           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
16508
16509           if (TREE_ASM_WRITTEN (type))
16510             return;
16511
16512           /* If that failed, attach ourselves to the stub.  */
16513           push_decl_scope (TYPE_CONTEXT (type));
16514           context_die = lookup_type_die (TYPE_CONTEXT (type));
16515           need_pop = 1;
16516         }
16517       else if (TYPE_CONTEXT (type) != NULL_TREE
16518                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
16519         {
16520           /* If this type is local to a function that hasn't been written
16521              out yet, use a NULL context for now; it will be fixed up in
16522              decls_for_scope.  */
16523           context_die = lookup_decl_die (TYPE_CONTEXT (type));
16524           need_pop = 0;
16525         }
16526       else
16527         {
16528           context_die = declare_in_namespace (type, context_die);
16529           need_pop = 0;
16530         }
16531
16532       if (TREE_CODE (type) == ENUMERAL_TYPE)
16533         {
16534           /* This might have been written out by the call to
16535              declare_in_namespace.  */
16536           if (!TREE_ASM_WRITTEN (type))
16537             gen_enumeration_type_die (type, context_die);
16538         }
16539       else
16540         gen_struct_or_union_type_die (type, context_die, usage);
16541
16542       if (need_pop)
16543         pop_decl_scope ();
16544
16545       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
16546          it up if it is ever completed.  gen_*_type_die will set it for us
16547          when appropriate.  */
16548       return;
16549
16550     case VOID_TYPE:
16551     case INTEGER_TYPE:
16552     case REAL_TYPE:
16553     case FIXED_POINT_TYPE:
16554     case COMPLEX_TYPE:
16555     case BOOLEAN_TYPE:
16556       /* No DIEs needed for fundamental types.  */
16557       break;
16558
16559     case LANG_TYPE:
16560       /* No Dwarf representation currently defined.  */
16561       break;
16562
16563     default:
16564       gcc_unreachable ();
16565     }
16566
16567   TREE_ASM_WRITTEN (type) = 1;
16568 }
16569
16570 static void
16571 gen_type_die (tree type, dw_die_ref context_die)
16572 {
16573   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
16574 }
16575
16576 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
16577    things which are local to the given block.  */
16578
16579 static void
16580 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
16581 {
16582   int must_output_die = 0;
16583   bool inlined_func;
16584
16585   /* Ignore blocks that are NULL.  */
16586   if (stmt == NULL_TREE)
16587     return;
16588
16589   inlined_func = inlined_function_outer_scope_p (stmt);
16590
16591   /* If the block is one fragment of a non-contiguous block, do not
16592      process the variables, since they will have been done by the
16593      origin block.  Do process subblocks.  */
16594   if (BLOCK_FRAGMENT_ORIGIN (stmt))
16595     {
16596       tree sub;
16597
16598       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
16599         gen_block_die (sub, context_die, depth + 1);
16600
16601       return;
16602     }
16603
16604   /* Determine if we need to output any Dwarf DIEs at all to represent this
16605      block.  */
16606   if (inlined_func)
16607     /* The outer scopes for inlinings *must* always be represented.  We
16608        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
16609     must_output_die = 1;
16610   else
16611     {
16612       /* Determine if this block directly contains any "significant"
16613          local declarations which we will need to output DIEs for.  */
16614       if (debug_info_level > DINFO_LEVEL_TERSE)
16615         /* We are not in terse mode so *any* local declaration counts
16616            as being a "significant" one.  */
16617         must_output_die = ((BLOCK_VARS (stmt) != NULL
16618                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
16619                            && (TREE_USED (stmt)
16620                                || TREE_ASM_WRITTEN (stmt)
16621                                || BLOCK_ABSTRACT (stmt)));
16622       else if ((TREE_USED (stmt)
16623                 || TREE_ASM_WRITTEN (stmt)
16624                 || BLOCK_ABSTRACT (stmt))
16625                && !dwarf2out_ignore_block (stmt))
16626         must_output_die = 1;
16627     }
16628
16629   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
16630      DIE for any block which contains no significant local declarations at
16631      all.  Rather, in such cases we just call `decls_for_scope' so that any
16632      needed Dwarf info for any sub-blocks will get properly generated. Note
16633      that in terse mode, our definition of what constitutes a "significant"
16634      local declaration gets restricted to include only inlined function
16635      instances and local (nested) function definitions.  */
16636   if (must_output_die)
16637     {
16638       if (inlined_func)
16639         {
16640           /* If STMT block is abstract, that means we have been called
16641              indirectly from dwarf2out_abstract_function.
16642              That function rightfully marks the descendent blocks (of
16643              the abstract function it is dealing with) as being abstract,
16644              precisely to prevent us from emitting any
16645              DW_TAG_inlined_subroutine DIE as a descendent
16646              of an abstract function instance. So in that case, we should
16647              not call gen_inlined_subroutine_die.
16648
16649              Later though, when cgraph asks dwarf2out to emit info
16650              for the concrete instance of the function decl into which
16651              the concrete instance of STMT got inlined, the later will lead
16652              to the generation of a DW_TAG_inlined_subroutine DIE.  */
16653           if (! BLOCK_ABSTRACT (stmt))
16654             gen_inlined_subroutine_die (stmt, context_die, depth);
16655         }
16656       else
16657         gen_lexical_block_die (stmt, context_die, depth);
16658     }
16659   else
16660     decls_for_scope (stmt, context_die, depth);
16661 }
16662
16663 /* Process variable DECL (or variable with origin ORIGIN) within
16664    block STMT and add it to CONTEXT_DIE.  */
16665 static void
16666 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
16667 {
16668   dw_die_ref die;
16669   tree decl_or_origin = decl ? decl : origin;
16670   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
16671
16672   if (ultimate_origin)
16673     origin = ultimate_origin;
16674
16675   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
16676     die = lookup_decl_die (decl_or_origin);
16677   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
16678            && TYPE_DECL_IS_STUB (decl_or_origin))
16679     die = lookup_type_die (TREE_TYPE (decl_or_origin));
16680   else
16681     die = NULL;
16682
16683   if (die != NULL && die->die_parent == NULL)
16684     add_child_die (context_die, die);
16685   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
16686     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
16687                                          stmt, context_die);
16688   else
16689     gen_decl_die (decl, origin, context_die);
16690 }
16691
16692 /* Generate all of the decls declared within a given scope and (recursively)
16693    all of its sub-blocks.  */
16694
16695 static void
16696 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
16697 {
16698   tree decl;
16699   unsigned int i;
16700   tree subblocks;
16701
16702   /* Ignore NULL blocks.  */
16703   if (stmt == NULL_TREE)
16704     return;
16705
16706   /* Output the DIEs to represent all of the data objects and typedefs
16707      declared directly within this block but not within any nested
16708      sub-blocks.  Also, nested function and tag DIEs have been
16709      generated with a parent of NULL; fix that up now.  */
16710   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
16711     process_scope_var (stmt, decl, NULL_TREE, context_die);
16712   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
16713     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
16714                        context_die);
16715
16716   /* If we're at -g1, we're not interested in subblocks.  */
16717   if (debug_info_level <= DINFO_LEVEL_TERSE)
16718     return;
16719
16720   /* Output the DIEs to represent all sub-blocks (and the items declared
16721      therein) of this block.  */
16722   for (subblocks = BLOCK_SUBBLOCKS (stmt);
16723        subblocks != NULL;
16724        subblocks = BLOCK_CHAIN (subblocks))
16725     gen_block_die (subblocks, context_die, depth + 1);
16726 }
16727
16728 /* Is this a typedef we can avoid emitting?  */
16729
16730 static inline int
16731 is_redundant_typedef (const_tree decl)
16732 {
16733   if (TYPE_DECL_IS_STUB (decl))
16734     return 1;
16735
16736   if (DECL_ARTIFICIAL (decl)
16737       && DECL_CONTEXT (decl)
16738       && is_tagged_type (DECL_CONTEXT (decl))
16739       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
16740       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
16741     /* Also ignore the artificial member typedef for the class name.  */
16742     return 1;
16743
16744   return 0;
16745 }
16746
16747 /* Returns the DIE for a context.  */
16748
16749 static inline dw_die_ref
16750 get_context_die (tree context)
16751 {
16752   if (context)
16753     {
16754       /* Find die that represents this context.  */
16755       if (TYPE_P (context))
16756         return force_type_die (context);
16757       else
16758         return force_decl_die (context);
16759     }
16760   return comp_unit_die;
16761 }
16762
16763 /* Returns the DIE for decl.  A DIE will always be returned.  */
16764
16765 static dw_die_ref
16766 force_decl_die (tree decl)
16767 {
16768   dw_die_ref decl_die;
16769   unsigned saved_external_flag;
16770   tree save_fn = NULL_TREE;
16771   decl_die = lookup_decl_die (decl);
16772   if (!decl_die)
16773     {
16774       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
16775
16776       decl_die = lookup_decl_die (decl);
16777       if (decl_die)
16778         return decl_die;
16779
16780       switch (TREE_CODE (decl))
16781         {
16782         case FUNCTION_DECL:
16783           /* Clear current_function_decl, so that gen_subprogram_die thinks
16784              that this is a declaration. At this point, we just want to force
16785              declaration die.  */
16786           save_fn = current_function_decl;
16787           current_function_decl = NULL_TREE;
16788           gen_subprogram_die (decl, context_die);
16789           current_function_decl = save_fn;
16790           break;
16791
16792         case VAR_DECL:
16793           /* Set external flag to force declaration die. Restore it after
16794            gen_decl_die() call.  */
16795           saved_external_flag = DECL_EXTERNAL (decl);
16796           DECL_EXTERNAL (decl) = 1;
16797           gen_decl_die (decl, NULL, context_die);
16798           DECL_EXTERNAL (decl) = saved_external_flag;
16799           break;
16800
16801         case NAMESPACE_DECL:
16802           dwarf2out_decl (decl);
16803           break;
16804
16805         default:
16806           gcc_unreachable ();
16807         }
16808
16809       /* We should be able to find the DIE now.  */
16810       if (!decl_die)
16811         decl_die = lookup_decl_die (decl);
16812       gcc_assert (decl_die);
16813     }
16814
16815   return decl_die;
16816 }
16817
16818 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
16819    always returned.  */
16820
16821 static dw_die_ref
16822 force_type_die (tree type)
16823 {
16824   dw_die_ref type_die;
16825
16826   type_die = lookup_type_die (type);
16827   if (!type_die)
16828     {
16829       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
16830
16831       type_die = modified_type_die (type, TYPE_READONLY (type),
16832                                     TYPE_VOLATILE (type), context_die);
16833       gcc_assert (type_die);
16834     }
16835   return type_die;
16836 }
16837
16838 /* Force out any required namespaces to be able to output DECL,
16839    and return the new context_die for it, if it's changed.  */
16840
16841 static dw_die_ref
16842 setup_namespace_context (tree thing, dw_die_ref context_die)
16843 {
16844   tree context = (DECL_P (thing)
16845                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
16846   if (context && TREE_CODE (context) == NAMESPACE_DECL)
16847     /* Force out the namespace.  */
16848     context_die = force_decl_die (context);
16849
16850   return context_die;
16851 }
16852
16853 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
16854    type) within its namespace, if appropriate.
16855
16856    For compatibility with older debuggers, namespace DIEs only contain
16857    declarations; all definitions are emitted at CU scope.  */
16858
16859 static dw_die_ref
16860 declare_in_namespace (tree thing, dw_die_ref context_die)
16861 {
16862   dw_die_ref ns_context;
16863
16864   if (debug_info_level <= DINFO_LEVEL_TERSE)
16865     return context_die;
16866
16867   /* If this decl is from an inlined function, then don't try to emit it in its
16868      namespace, as we will get confused.  It would have already been emitted
16869      when the abstract instance of the inline function was emitted anyways.  */
16870   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
16871     return context_die;
16872
16873   ns_context = setup_namespace_context (thing, context_die);
16874
16875   if (ns_context != context_die)
16876     {
16877       if (is_fortran ())
16878         return ns_context;
16879       if (DECL_P (thing))
16880         gen_decl_die (thing, NULL, ns_context);
16881       else
16882         gen_type_die (thing, ns_context);
16883     }
16884   return context_die;
16885 }
16886
16887 /* Generate a DIE for a namespace or namespace alias.  */
16888
16889 static void
16890 gen_namespace_die (tree decl, dw_die_ref context_die)
16891 {
16892   dw_die_ref namespace_die;
16893
16894   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
16895      they are an alias of.  */
16896   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
16897     {
16898       /* Output a real namespace or module.  */
16899       context_die = setup_namespace_context (decl, comp_unit_die);
16900       namespace_die = new_die (is_fortran ()
16901                                ? DW_TAG_module : DW_TAG_namespace,
16902                                context_die, decl);
16903       /* For Fortran modules defined in different CU don't add src coords.  */
16904       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
16905         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
16906       else
16907         add_name_and_src_coords_attributes (namespace_die, decl);
16908       if (DECL_EXTERNAL (decl))
16909         add_AT_flag (namespace_die, DW_AT_declaration, 1);
16910       equate_decl_number_to_die (decl, namespace_die);
16911     }
16912   else
16913     {
16914       /* Output a namespace alias.  */
16915
16916       /* Force out the namespace we are an alias of, if necessary.  */
16917       dw_die_ref origin_die
16918         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
16919
16920       if (DECL_CONTEXT (decl) == NULL_TREE
16921           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
16922         context_die = setup_namespace_context (decl, comp_unit_die);
16923       /* Now create the namespace alias DIE.  */
16924       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
16925       add_name_and_src_coords_attributes (namespace_die, decl);
16926       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
16927       equate_decl_number_to_die (decl, namespace_die);
16928     }
16929 }
16930
16931 /* Generate Dwarf debug information for a decl described by DECL.  */
16932
16933 static void
16934 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
16935 {
16936   tree decl_or_origin = decl ? decl : origin;
16937   tree class_origin = NULL;
16938
16939   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
16940     return;
16941
16942   switch (TREE_CODE (decl_or_origin))
16943     {
16944     case ERROR_MARK:
16945       break;
16946
16947     case CONST_DECL:
16948       if (!is_fortran ())
16949         {
16950           /* The individual enumerators of an enum type get output when we output
16951              the Dwarf representation of the relevant enum type itself.  */
16952           break;
16953         }
16954
16955       /* Emit its type.  */
16956       gen_type_die (TREE_TYPE (decl), context_die);
16957
16958       /* And its containing namespace.  */
16959       context_die = declare_in_namespace (decl, context_die);
16960
16961       gen_const_die (decl, context_die);
16962       break;
16963
16964     case FUNCTION_DECL:
16965       /* Don't output any DIEs to represent mere function declarations,
16966          unless they are class members or explicit block externs.  */
16967       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
16968           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
16969           && (current_function_decl == NULL_TREE
16970               || DECL_ARTIFICIAL (decl_or_origin)))
16971         break;
16972
16973 #if 0
16974       /* FIXME */
16975       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
16976          on local redeclarations of global functions.  That seems broken.  */
16977       if (current_function_decl != decl)
16978         /* This is only a declaration.  */;
16979 #endif
16980
16981       /* If we're emitting a clone, emit info for the abstract instance.  */
16982       if (origin || DECL_ORIGIN (decl) != decl)
16983         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
16984
16985       /* If we're emitting an out-of-line copy of an inline function,
16986          emit info for the abstract instance and set up to refer to it.  */
16987       else if (cgraph_function_possibly_inlined_p (decl)
16988                && ! DECL_ABSTRACT (decl)
16989                && ! class_or_namespace_scope_p (context_die)
16990                /* dwarf2out_abstract_function won't emit a die if this is just
16991                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
16992                   that case, because that works only if we have a die.  */
16993                && DECL_INITIAL (decl) != NULL_TREE)
16994         {
16995           dwarf2out_abstract_function (decl);
16996           set_decl_origin_self (decl);
16997         }
16998
16999       /* Otherwise we're emitting the primary DIE for this decl.  */
17000       else if (debug_info_level > DINFO_LEVEL_TERSE)
17001         {
17002           /* Before we describe the FUNCTION_DECL itself, make sure that we
17003              have described its return type.  */
17004           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
17005
17006           /* And its virtual context.  */
17007           if (DECL_VINDEX (decl) != NULL_TREE)
17008             gen_type_die (DECL_CONTEXT (decl), context_die);
17009
17010           /* And its containing type.  */
17011           if (!origin)
17012             origin = decl_class_context (decl);
17013           if (origin != NULL_TREE)
17014             gen_type_die_for_member (origin, decl, context_die);
17015
17016           /* And its containing namespace.  */
17017           context_die = declare_in_namespace (decl, context_die);
17018         }
17019
17020       /* Now output a DIE to represent the function itself.  */
17021       if (decl)
17022         gen_subprogram_die (decl, context_die);
17023       break;
17024
17025     case TYPE_DECL:
17026       /* If we are in terse mode, don't generate any DIEs to represent any
17027          actual typedefs.  */
17028       if (debug_info_level <= DINFO_LEVEL_TERSE)
17029         break;
17030
17031       /* In the special case of a TYPE_DECL node representing the declaration
17032          of some type tag, if the given TYPE_DECL is marked as having been
17033          instantiated from some other (original) TYPE_DECL node (e.g. one which
17034          was generated within the original definition of an inline function) we
17035          used to generate a special (abbreviated) DW_TAG_structure_type,
17036          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
17037          should be actually referencing those DIEs, as variable DIEs with that
17038          type would be emitted already in the abstract origin, so it was always
17039          removed during unused type prunning.  Don't add anything in this
17040          case.  */
17041       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
17042         break;
17043
17044       if (is_redundant_typedef (decl))
17045         gen_type_die (TREE_TYPE (decl), context_die);
17046       else
17047         /* Output a DIE to represent the typedef itself.  */
17048         gen_typedef_die (decl, context_die);
17049       break;
17050
17051     case LABEL_DECL:
17052       if (debug_info_level >= DINFO_LEVEL_NORMAL)
17053         gen_label_die (decl, context_die);
17054       break;
17055
17056     case VAR_DECL:
17057     case RESULT_DECL:
17058       /* If we are in terse mode, don't generate any DIEs to represent any
17059          variable declarations or definitions.  */
17060       if (debug_info_level <= DINFO_LEVEL_TERSE)
17061         break;
17062
17063       /* Output any DIEs that are needed to specify the type of this data
17064          object.  */
17065       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
17066            || TREE_CODE (decl_or_origin) == VAR_DECL)
17067           && DECL_BY_REFERENCE (decl_or_origin))
17068         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17069       else
17070         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17071
17072       /* And its containing type.  */
17073       class_origin = decl_class_context (decl_or_origin);
17074       if (class_origin != NULL_TREE)
17075         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
17076
17077       /* And its containing namespace.  */
17078       context_die = declare_in_namespace (decl_or_origin, context_die);
17079
17080       /* Now output the DIE to represent the data object itself.  This gets
17081          complicated because of the possibility that the VAR_DECL really
17082          represents an inlined instance of a formal parameter for an inline
17083          function.  */
17084       if (!origin)
17085         origin = decl_ultimate_origin (decl);
17086       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
17087         gen_formal_parameter_die (decl, origin, context_die);
17088       else
17089         gen_variable_die (decl, origin, context_die);
17090       break;
17091
17092     case FIELD_DECL:
17093       /* Ignore the nameless fields that are used to skip bits but handle C++
17094          anonymous unions and structs.  */
17095       if (DECL_NAME (decl) != NULL_TREE
17096           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
17097           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
17098         {
17099           gen_type_die (member_declared_type (decl), context_die);
17100           gen_field_die (decl, context_die);
17101         }
17102       break;
17103
17104     case PARM_DECL:
17105       if (DECL_BY_REFERENCE (decl_or_origin))
17106         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17107       else
17108         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17109       gen_formal_parameter_die (decl, origin, context_die);
17110       break;
17111
17112     case NAMESPACE_DECL:
17113     case IMPORTED_DECL:
17114       gen_namespace_die (decl, context_die);
17115       break;
17116
17117     default:
17118       /* Probably some frontend-internal decl.  Assume we don't care.  */
17119       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
17120       break;
17121     }
17122 }
17123 \f
17124 /* Output debug information for global decl DECL.  Called from toplev.c after
17125    compilation proper has finished.  */
17126
17127 static void
17128 dwarf2out_global_decl (tree decl)
17129 {
17130   /* Output DWARF2 information for file-scope tentative data object
17131      declarations, file-scope (extern) function declarations (which
17132      had no corresponding body) and file-scope tagged type declarations
17133      and definitions which have not yet been forced out.  */
17134   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
17135     dwarf2out_decl (decl);
17136 }
17137
17138 /* Output debug information for type decl DECL.  Called from toplev.c
17139    and from language front ends (to record built-in types).  */
17140 static void
17141 dwarf2out_type_decl (tree decl, int local)
17142 {
17143   if (!local)
17144     dwarf2out_decl (decl);
17145 }
17146
17147 /* Output debug information for imported module or decl DECL.
17148    NAME is non-NULL name in the lexical block if the decl has been renamed.
17149    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
17150    that DECL belongs to.
17151    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
17152 static void
17153 dwarf2out_imported_module_or_decl_1 (tree decl,
17154                                      tree name,
17155                                      tree lexical_block,
17156                                      dw_die_ref lexical_block_die)
17157 {
17158   expanded_location xloc;
17159   dw_die_ref imported_die = NULL;
17160   dw_die_ref at_import_die;
17161
17162   if (TREE_CODE (decl) == IMPORTED_DECL)
17163     {
17164       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
17165       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
17166       gcc_assert (decl);
17167     }
17168   else
17169     xloc = expand_location (input_location);
17170
17171   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
17172     {
17173       if (is_base_type (TREE_TYPE (decl)))
17174         at_import_die = base_type_die (TREE_TYPE (decl));
17175       else
17176         at_import_die = force_type_die (TREE_TYPE (decl));
17177       /* For namespace N { typedef void T; } using N::T; base_type_die
17178          returns NULL, but DW_TAG_imported_declaration requires
17179          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
17180       if (!at_import_die)
17181         {
17182           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
17183           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
17184           at_import_die = lookup_type_die (TREE_TYPE (decl));
17185           gcc_assert (at_import_die);
17186         }
17187     }
17188   else
17189     {
17190       at_import_die = lookup_decl_die (decl);
17191       if (!at_import_die)
17192         {
17193           /* If we're trying to avoid duplicate debug info, we may not have
17194              emitted the member decl for this field.  Emit it now.  */
17195           if (TREE_CODE (decl) == FIELD_DECL)
17196             {
17197               tree type = DECL_CONTEXT (decl);
17198
17199               if (TYPE_CONTEXT (type)
17200                   && TYPE_P (TYPE_CONTEXT (type))
17201                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
17202                                                 DINFO_USAGE_DIR_USE))
17203                 return;
17204               gen_type_die_for_member (type, decl,
17205                                        get_context_die (TYPE_CONTEXT (type)));
17206             }
17207           at_import_die = force_decl_die (decl);
17208         }
17209     }
17210
17211   if (TREE_CODE (decl) == NAMESPACE_DECL)
17212     imported_die = new_die (DW_TAG_imported_module,
17213                             lexical_block_die,
17214                             lexical_block);
17215   else
17216     imported_die = new_die (DW_TAG_imported_declaration,
17217                             lexical_block_die,
17218                             lexical_block);
17219
17220   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
17221   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
17222   if (name)
17223     add_AT_string (imported_die, DW_AT_name,
17224                    IDENTIFIER_POINTER (name));
17225   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
17226 }
17227
17228 /* Output debug information for imported module or decl DECL.
17229    NAME is non-NULL name in context if the decl has been renamed.
17230    CHILD is true if decl is one of the renamed decls as part of
17231    importing whole module.  */
17232
17233 static void
17234 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
17235                                    bool child)
17236 {
17237   /* dw_die_ref at_import_die;  */
17238   dw_die_ref scope_die;
17239
17240   if (debug_info_level <= DINFO_LEVEL_TERSE)
17241     return;
17242
17243   gcc_assert (decl);
17244
17245   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
17246      We need decl DIE for reference and scope die. First, get DIE for the decl
17247      itself.  */
17248
17249   /* Get the scope die for decl context. Use comp_unit_die for global module
17250      or decl. If die is not found for non globals, force new die.  */
17251   if (context
17252       && TYPE_P (context)
17253       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
17254     return;
17255   scope_die = get_context_die (context);
17256
17257   if (child)
17258     {
17259       gcc_assert (scope_die->die_child);
17260       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
17261       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
17262       scope_die = scope_die->die_child;
17263     }
17264
17265   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
17266   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
17267
17268 }
17269
17270 /* Write the debugging output for DECL.  */
17271
17272 void
17273 dwarf2out_decl (tree decl)
17274 {
17275   dw_die_ref context_die = comp_unit_die;
17276
17277   switch (TREE_CODE (decl))
17278     {
17279     case ERROR_MARK:
17280       return;
17281
17282     case FUNCTION_DECL:
17283       /* What we would really like to do here is to filter out all mere
17284          file-scope declarations of file-scope functions which are never
17285          referenced later within this translation unit (and keep all of ones
17286          that *are* referenced later on) but we aren't clairvoyant, so we have
17287          no idea which functions will be referenced in the future (i.e. later
17288          on within the current translation unit). So here we just ignore all
17289          file-scope function declarations which are not also definitions.  If
17290          and when the debugger needs to know something about these functions,
17291          it will have to hunt around and find the DWARF information associated
17292          with the definition of the function.
17293
17294          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
17295          nodes represent definitions and which ones represent mere
17296          declarations.  We have to check DECL_INITIAL instead. That's because
17297          the C front-end supports some weird semantics for "extern inline"
17298          function definitions.  These can get inlined within the current
17299          translation unit (and thus, we need to generate Dwarf info for their
17300          abstract instances so that the Dwarf info for the concrete inlined
17301          instances can have something to refer to) but the compiler never
17302          generates any out-of-lines instances of such things (despite the fact
17303          that they *are* definitions).
17304
17305          The important point is that the C front-end marks these "extern
17306          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
17307          them anyway. Note that the C++ front-end also plays some similar games
17308          for inline function definitions appearing within include files which
17309          also contain `#pragma interface' pragmas.  */
17310       if (DECL_INITIAL (decl) == NULL_TREE)
17311         return;
17312
17313       /* If we're a nested function, initially use a parent of NULL; if we're
17314          a plain function, this will be fixed up in decls_for_scope.  If
17315          we're a method, it will be ignored, since we already have a DIE.  */
17316       if (decl_function_context (decl)
17317           /* But if we're in terse mode, we don't care about scope.  */
17318           && debug_info_level > DINFO_LEVEL_TERSE)
17319         context_die = NULL;
17320       break;
17321
17322     case VAR_DECL:
17323       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
17324          declaration and if the declaration was never even referenced from
17325          within this entire compilation unit.  We suppress these DIEs in
17326          order to save space in the .debug section (by eliminating entries
17327          which are probably useless).  Note that we must not suppress
17328          block-local extern declarations (whether used or not) because that
17329          would screw-up the debugger's name lookup mechanism and cause it to
17330          miss things which really ought to be in scope at a given point.  */
17331       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
17332         return;
17333
17334       /* For local statics lookup proper context die.  */
17335       if (TREE_STATIC (decl) && decl_function_context (decl))
17336         context_die = lookup_decl_die (DECL_CONTEXT (decl));
17337
17338       /* If we are in terse mode, don't generate any DIEs to represent any
17339          variable declarations or definitions.  */
17340       if (debug_info_level <= DINFO_LEVEL_TERSE)
17341         return;
17342       break;
17343
17344     case CONST_DECL:
17345       if (debug_info_level <= DINFO_LEVEL_TERSE)
17346         return;
17347       if (!is_fortran ())
17348         return;
17349       if (TREE_STATIC (decl) && decl_function_context (decl))
17350         context_die = lookup_decl_die (DECL_CONTEXT (decl));
17351       break;
17352
17353     case NAMESPACE_DECL:
17354     case IMPORTED_DECL:
17355       if (debug_info_level <= DINFO_LEVEL_TERSE)
17356         return;
17357       if (lookup_decl_die (decl) != NULL)
17358         return;
17359       break;
17360
17361     case TYPE_DECL:
17362       /* Don't emit stubs for types unless they are needed by other DIEs.  */
17363       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
17364         return;
17365
17366       /* Don't bother trying to generate any DIEs to represent any of the
17367          normal built-in types for the language we are compiling.  */
17368       if (DECL_IS_BUILTIN (decl))
17369         {
17370           /* OK, we need to generate one for `bool' so GDB knows what type
17371              comparisons have.  */
17372           if (is_cxx ()
17373               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
17374               && ! DECL_IGNORED_P (decl))
17375             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
17376
17377           return;
17378         }
17379
17380       /* If we are in terse mode, don't generate any DIEs for types.  */
17381       if (debug_info_level <= DINFO_LEVEL_TERSE)
17382         return;
17383
17384       /* If we're a function-scope tag, initially use a parent of NULL;
17385          this will be fixed up in decls_for_scope.  */
17386       if (decl_function_context (decl))
17387         context_die = NULL;
17388
17389       break;
17390
17391     default:
17392       return;
17393     }
17394
17395   gen_decl_die (decl, NULL, context_die);
17396 }
17397
17398 /* Output a marker (i.e. a label) for the beginning of the generated code for
17399    a lexical block.  */
17400
17401 static void
17402 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
17403                        unsigned int blocknum)
17404 {
17405   switch_to_section (current_function_section ());
17406   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
17407 }
17408
17409 /* Output a marker (i.e. a label) for the end of the generated code for a
17410    lexical block.  */
17411
17412 static void
17413 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
17414 {
17415   switch_to_section (current_function_section ());
17416   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
17417 }
17418
17419 /* Returns nonzero if it is appropriate not to emit any debugging
17420    information for BLOCK, because it doesn't contain any instructions.
17421
17422    Don't allow this for blocks with nested functions or local classes
17423    as we would end up with orphans, and in the presence of scheduling
17424    we may end up calling them anyway.  */
17425
17426 static bool
17427 dwarf2out_ignore_block (const_tree block)
17428 {
17429   tree decl;
17430   unsigned int i;
17431
17432   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
17433     if (TREE_CODE (decl) == FUNCTION_DECL
17434         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
17435       return 0;
17436   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
17437     {
17438       decl = BLOCK_NONLOCALIZED_VAR (block, i);
17439       if (TREE_CODE (decl) == FUNCTION_DECL
17440           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
17441       return 0;
17442     }
17443
17444   return 1;
17445 }
17446
17447 /* Hash table routines for file_hash.  */
17448
17449 static int
17450 file_table_eq (const void *p1_p, const void *p2_p)
17451 {
17452   const struct dwarf_file_data *const p1 =
17453     (const struct dwarf_file_data *) p1_p;
17454   const char *const p2 = (const char *) p2_p;
17455   return strcmp (p1->filename, p2) == 0;
17456 }
17457
17458 static hashval_t
17459 file_table_hash (const void *p_p)
17460 {
17461   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
17462   return htab_hash_string (p->filename);
17463 }
17464
17465 /* Lookup FILE_NAME (in the list of filenames that we know about here in
17466    dwarf2out.c) and return its "index".  The index of each (known) filename is
17467    just a unique number which is associated with only that one filename.  We
17468    need such numbers for the sake of generating labels (in the .debug_sfnames
17469    section) and references to those files numbers (in the .debug_srcinfo
17470    and.debug_macinfo sections).  If the filename given as an argument is not
17471    found in our current list, add it to the list and assign it the next
17472    available unique index number.  In order to speed up searches, we remember
17473    the index of the filename was looked up last.  This handles the majority of
17474    all searches.  */
17475
17476 static struct dwarf_file_data *
17477 lookup_filename (const char *file_name)
17478 {
17479   void ** slot;
17480   struct dwarf_file_data * created;
17481
17482   /* Check to see if the file name that was searched on the previous
17483      call matches this file name.  If so, return the index.  */
17484   if (file_table_last_lookup
17485       && (file_name == file_table_last_lookup->filename
17486           || strcmp (file_table_last_lookup->filename, file_name) == 0))
17487     return file_table_last_lookup;
17488
17489   /* Didn't match the previous lookup, search the table.  */
17490   slot = htab_find_slot_with_hash (file_table, file_name,
17491                                    htab_hash_string (file_name), INSERT);
17492   if (*slot)
17493     return (struct dwarf_file_data *) *slot;
17494
17495   created = GGC_NEW (struct dwarf_file_data);
17496   created->filename = file_name;
17497   created->emitted_number = 0;
17498   *slot = created;
17499   return created;
17500 }
17501
17502 /* If the assembler will construct the file table, then translate the compiler
17503    internal file table number into the assembler file table number, and emit
17504    a .file directive if we haven't already emitted one yet.  The file table
17505    numbers are different because we prune debug info for unused variables and
17506    types, which may include filenames.  */
17507
17508 static int
17509 maybe_emit_file (struct dwarf_file_data * fd)
17510 {
17511   if (! fd->emitted_number)
17512     {
17513       if (last_emitted_file)
17514         fd->emitted_number = last_emitted_file->emitted_number + 1;
17515       else
17516         fd->emitted_number = 1;
17517       last_emitted_file = fd;
17518
17519       if (DWARF2_ASM_LINE_DEBUG_INFO)
17520         {
17521           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
17522           output_quoted_string (asm_out_file,
17523                                 remap_debug_filename (fd->filename));
17524           fputc ('\n', asm_out_file);
17525         }
17526     }
17527
17528   return fd->emitted_number;
17529 }
17530
17531 /* Schedule generation of a DW_AT_const_value attribute to DIE.
17532    That generation should happen after function debug info has been
17533    generated. The value of the attribute is the constant value of ARG.  */
17534
17535 static void
17536 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
17537 {
17538   die_arg_entry entry;
17539
17540   if (!die || !arg)
17541     return;
17542
17543   if (!tmpl_value_parm_die_table)
17544     tmpl_value_parm_die_table
17545       = VEC_alloc (die_arg_entry, gc, 32);
17546
17547   entry.die = die;
17548   entry.arg = arg;
17549   VEC_safe_push (die_arg_entry, gc,
17550                  tmpl_value_parm_die_table,
17551                  &entry);
17552 }
17553
17554 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
17555    by append_entry_to_tmpl_value_parm_die_table. This function must
17556    be called after function DIEs have been generated.  */
17557
17558 static void
17559 gen_remaining_tmpl_value_param_die_attribute (void)
17560 {
17561   if (tmpl_value_parm_die_table)
17562     {
17563       unsigned i;
17564       die_arg_entry *e;
17565
17566       for (i = 0;
17567            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
17568            i++)
17569         tree_add_const_value_attribute (e->die, e->arg);
17570     }
17571 }
17572
17573
17574 /* Replace DW_AT_name for the decl with name.  */
17575  
17576 static void
17577 dwarf2out_set_name (tree decl, tree name)
17578 {
17579   dw_die_ref die;
17580   dw_attr_ref attr;
17581
17582   die = TYPE_SYMTAB_DIE (decl);
17583   if (!die)
17584     return;
17585
17586   attr = get_AT (die, DW_AT_name);
17587   if (attr)
17588     {
17589       struct indirect_string_node *node;
17590
17591       node = find_AT_string (dwarf2_name (name, 0));
17592       /* replace the string.  */
17593       attr->dw_attr_val.v.val_str = node;
17594     }
17595
17596   else
17597     add_name_attribute (die, dwarf2_name (name, 0));
17598 }
17599
17600 /* Called by the final INSN scan whenever we see a var location.  We
17601    use it to drop labels in the right places, and throw the location in
17602    our lookup table.  */
17603
17604 static void
17605 dwarf2out_var_location (rtx loc_note)
17606 {
17607   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
17608   struct var_loc_node *newloc;
17609   rtx next_real;
17610   static const char *last_label;
17611   static const char *last_postcall_label;
17612   static bool last_in_cold_section_p;
17613   tree decl;
17614
17615   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
17616     return;
17617
17618   next_real = next_real_insn (loc_note);
17619   /* If there are no instructions which would be affected by this note,
17620      don't do anything.  */
17621   if (next_real == NULL_RTX)
17622     return;
17623
17624   newloc = GGC_CNEW (struct var_loc_node);
17625   /* If there were no real insns between note we processed last time
17626      and this note, use the label we emitted last time.  */
17627   if (last_var_location_insn == NULL_RTX
17628       || last_var_location_insn != next_real
17629       || last_in_cold_section_p != in_cold_section_p)
17630     {
17631       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
17632       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
17633       loclabel_num++;
17634       last_label = ggc_strdup (loclabel);
17635       if (!NOTE_DURING_CALL_P (loc_note))
17636         last_postcall_label = NULL;
17637     }
17638   newloc->var_loc_note = loc_note;
17639   newloc->next = NULL;
17640
17641   if (!NOTE_DURING_CALL_P (loc_note))
17642     newloc->label = last_label;
17643   else
17644     {
17645       if (!last_postcall_label)
17646         {
17647           sprintf (loclabel, "%s-1", last_label);
17648           last_postcall_label = ggc_strdup (loclabel);
17649         }
17650       newloc->label = last_postcall_label;
17651     }
17652
17653   if (cfun && in_cold_section_p)
17654     newloc->section_label = crtl->subsections.cold_section_label;
17655   else
17656     newloc->section_label = text_section_label;
17657
17658   last_var_location_insn = next_real;
17659   last_in_cold_section_p = in_cold_section_p;
17660   decl = NOTE_VAR_LOCATION_DECL (loc_note);
17661   add_var_loc_to_decl (decl, newloc);
17662 }
17663
17664 /* We need to reset the locations at the beginning of each
17665    function. We can't do this in the end_function hook, because the
17666    declarations that use the locations won't have been output when
17667    that hook is called.  Also compute have_multiple_function_sections here.  */
17668
17669 static void
17670 dwarf2out_begin_function (tree fun)
17671 {
17672   htab_empty (decl_loc_table);
17673
17674   if (function_section (fun) != text_section)
17675     have_multiple_function_sections = true;
17676
17677   dwarf2out_note_section_used ();
17678 }
17679
17680 /* Output a label to mark the beginning of a source code line entry
17681    and record information relating to this source line, in
17682    'line_info_table' for later output of the .debug_line section.  */
17683
17684 static void
17685 dwarf2out_source_line (unsigned int line, const char *filename,
17686                        int discriminator, bool is_stmt)
17687 {
17688   static bool last_is_stmt = true;
17689
17690   if (debug_info_level >= DINFO_LEVEL_NORMAL
17691       && line != 0)
17692     {
17693       int file_num = maybe_emit_file (lookup_filename (filename));
17694
17695       switch_to_section (current_function_section ());
17696
17697       /* If requested, emit something human-readable.  */
17698       if (flag_debug_asm)
17699         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
17700                  filename, line);
17701
17702       if (DWARF2_ASM_LINE_DEBUG_INFO)
17703         {
17704           /* Emit the .loc directive understood by GNU as.  */
17705           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
17706           if (is_stmt != last_is_stmt)
17707             {
17708               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
17709               last_is_stmt = is_stmt;
17710             }
17711           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
17712             fprintf (asm_out_file, " discriminator %d", discriminator);
17713           fputc ('\n', asm_out_file);
17714
17715           /* Indicate that line number info exists.  */
17716           line_info_table_in_use++;
17717         }
17718       else if (function_section (current_function_decl) != text_section)
17719         {
17720           dw_separate_line_info_ref line_info;
17721           targetm.asm_out.internal_label (asm_out_file,
17722                                           SEPARATE_LINE_CODE_LABEL,
17723                                           separate_line_info_table_in_use);
17724
17725           /* Expand the line info table if necessary.  */
17726           if (separate_line_info_table_in_use
17727               == separate_line_info_table_allocated)
17728             {
17729               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
17730               separate_line_info_table
17731                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
17732                                  separate_line_info_table,
17733                                  separate_line_info_table_allocated);
17734               memset (separate_line_info_table
17735                        + separate_line_info_table_in_use,
17736                       0,
17737                       (LINE_INFO_TABLE_INCREMENT
17738                        * sizeof (dw_separate_line_info_entry)));
17739             }
17740
17741           /* Add the new entry at the end of the line_info_table.  */
17742           line_info
17743             = &separate_line_info_table[separate_line_info_table_in_use++];
17744           line_info->dw_file_num = file_num;
17745           line_info->dw_line_num = line;
17746           line_info->function = current_function_funcdef_no;
17747         }
17748       else
17749         {
17750           dw_line_info_ref line_info;
17751
17752           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
17753                                      line_info_table_in_use);
17754
17755           /* Expand the line info table if necessary.  */
17756           if (line_info_table_in_use == line_info_table_allocated)
17757             {
17758               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
17759               line_info_table
17760                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
17761                                  line_info_table_allocated);
17762               memset (line_info_table + line_info_table_in_use, 0,
17763                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
17764             }
17765
17766           /* Add the new entry at the end of the line_info_table.  */
17767           line_info = &line_info_table[line_info_table_in_use++];
17768           line_info->dw_file_num = file_num;
17769           line_info->dw_line_num = line;
17770         }
17771     }
17772 }
17773
17774 /* Record the beginning of a new source file.  */
17775
17776 static void
17777 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
17778 {
17779   if (flag_eliminate_dwarf2_dups)
17780     {
17781       /* Record the beginning of the file for break_out_includes.  */
17782       dw_die_ref bincl_die;
17783
17784       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
17785       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
17786     }
17787
17788   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17789     {
17790       int file_num = maybe_emit_file (lookup_filename (filename));
17791
17792       switch_to_section (debug_macinfo_section);
17793       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
17794       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
17795                                    lineno);
17796
17797       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
17798     }
17799 }
17800
17801 /* Record the end of a source file.  */
17802
17803 static void
17804 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
17805 {
17806   if (flag_eliminate_dwarf2_dups)
17807     /* Record the end of the file for break_out_includes.  */
17808     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
17809
17810   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17811     {
17812       switch_to_section (debug_macinfo_section);
17813       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
17814     }
17815 }
17816
17817 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
17818    the tail part of the directive line, i.e. the part which is past the
17819    initial whitespace, #, whitespace, directive-name, whitespace part.  */
17820
17821 static void
17822 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
17823                   const char *buffer ATTRIBUTE_UNUSED)
17824 {
17825   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17826     {
17827       switch_to_section (debug_macinfo_section);
17828       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
17829       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
17830       dw2_asm_output_nstring (buffer, -1, "The macro");
17831     }
17832 }
17833
17834 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
17835    the tail part of the directive line, i.e. the part which is past the
17836    initial whitespace, #, whitespace, directive-name, whitespace part.  */
17837
17838 static void
17839 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
17840                  const char *buffer ATTRIBUTE_UNUSED)
17841 {
17842   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17843     {
17844       switch_to_section (debug_macinfo_section);
17845       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
17846       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
17847       dw2_asm_output_nstring (buffer, -1, "The macro");
17848     }
17849 }
17850
17851 /* Set up for Dwarf output at the start of compilation.  */
17852
17853 static void
17854 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
17855 {
17856   /* Allocate the file_table.  */
17857   file_table = htab_create_ggc (50, file_table_hash,
17858                                 file_table_eq, NULL);
17859
17860   /* Allocate the decl_die_table.  */
17861   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
17862                                     decl_die_table_eq, NULL);
17863
17864   /* Allocate the decl_loc_table.  */
17865   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
17866                                     decl_loc_table_eq, NULL);
17867
17868   /* Allocate the initial hunk of the decl_scope_table.  */
17869   decl_scope_table = VEC_alloc (tree, gc, 256);
17870
17871   /* Allocate the initial hunk of the abbrev_die_table.  */
17872   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
17873   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
17874   /* Zero-th entry is allocated, but unused.  */
17875   abbrev_die_table_in_use = 1;
17876
17877   /* Allocate the initial hunk of the line_info_table.  */
17878   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
17879   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
17880
17881   /* Zero-th entry is allocated, but unused.  */
17882   line_info_table_in_use = 1;
17883
17884   /* Allocate the pubtypes and pubnames vectors.  */
17885   pubname_table = VEC_alloc (pubname_entry, gc, 32);
17886   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
17887
17888   /* Generate the initial DIE for the .debug section.  Note that the (string)
17889      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
17890      will (typically) be a relative pathname and that this pathname should be
17891      taken as being relative to the directory from which the compiler was
17892      invoked when the given (base) source file was compiled.  We will fill
17893      in this value in dwarf2out_finish.  */
17894   comp_unit_die = gen_compile_unit_die (NULL);
17895
17896   incomplete_types = VEC_alloc (tree, gc, 64);
17897
17898   used_rtx_array = VEC_alloc (rtx, gc, 32);
17899
17900   debug_info_section = get_section (DEBUG_INFO_SECTION,
17901                                     SECTION_DEBUG, NULL);
17902   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
17903                                       SECTION_DEBUG, NULL);
17904   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
17905                                        SECTION_DEBUG, NULL);
17906   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
17907                                        SECTION_DEBUG, NULL);
17908   debug_line_section = get_section (DEBUG_LINE_SECTION,
17909                                     SECTION_DEBUG, NULL);
17910   debug_loc_section = get_section (DEBUG_LOC_SECTION,
17911                                    SECTION_DEBUG, NULL);
17912   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
17913                                         SECTION_DEBUG, NULL);
17914   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
17915                                         SECTION_DEBUG, NULL);
17916   debug_str_section = get_section (DEBUG_STR_SECTION,
17917                                    DEBUG_STR_SECTION_FLAGS, NULL);
17918   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
17919                                       SECTION_DEBUG, NULL);
17920   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
17921                                      SECTION_DEBUG, NULL);
17922
17923   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
17924   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
17925                                DEBUG_ABBREV_SECTION_LABEL, 0);
17926   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
17927   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
17928                                COLD_TEXT_SECTION_LABEL, 0);
17929   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
17930
17931   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
17932                                DEBUG_INFO_SECTION_LABEL, 0);
17933   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
17934                                DEBUG_LINE_SECTION_LABEL, 0);
17935   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
17936                                DEBUG_RANGES_SECTION_LABEL, 0);
17937   switch_to_section (debug_abbrev_section);
17938   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
17939   switch_to_section (debug_info_section);
17940   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
17941   switch_to_section (debug_line_section);
17942   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
17943
17944   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17945     {
17946       switch_to_section (debug_macinfo_section);
17947       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
17948                                    DEBUG_MACINFO_SECTION_LABEL, 0);
17949       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
17950     }
17951
17952   switch_to_section (text_section);
17953   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
17954   if (flag_reorder_blocks_and_partition)
17955     {
17956       cold_text_section = unlikely_text_section ();
17957       switch_to_section (cold_text_section);
17958       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
17959     }
17960 }
17961
17962 /* A helper function for dwarf2out_finish called through
17963    htab_traverse.  Emit one queued .debug_str string.  */
17964
17965 static int
17966 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
17967 {
17968   struct indirect_string_node *node = (struct indirect_string_node *) *h;
17969
17970   if (node->label && node->refcount)
17971     {
17972       switch_to_section (debug_str_section);
17973       ASM_OUTPUT_LABEL (asm_out_file, node->label);
17974       assemble_string (node->str, strlen (node->str) + 1);
17975     }
17976
17977   return 1;
17978 }
17979
17980 #if ENABLE_ASSERT_CHECKING
17981 /* Verify that all marks are clear.  */
17982
17983 static void
17984 verify_marks_clear (dw_die_ref die)
17985 {
17986   dw_die_ref c;
17987
17988   gcc_assert (! die->die_mark);
17989   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
17990 }
17991 #endif /* ENABLE_ASSERT_CHECKING */
17992
17993 /* Clear the marks for a die and its children.
17994    Be cool if the mark isn't set.  */
17995
17996 static void
17997 prune_unmark_dies (dw_die_ref die)
17998 {
17999   dw_die_ref c;
18000
18001   if (die->die_mark)
18002     die->die_mark = 0;
18003   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
18004 }
18005
18006 /* Given DIE that we're marking as used, find any other dies
18007    it references as attributes and mark them as used.  */
18008
18009 static void
18010 prune_unused_types_walk_attribs (dw_die_ref die)
18011 {
18012   dw_attr_ref a;
18013   unsigned ix;
18014
18015   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18016     {
18017       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
18018         {
18019           /* A reference to another DIE.
18020              Make sure that it will get emitted.  */
18021           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
18022         }
18023       /* Set the string's refcount to 0 so that prune_unused_types_mark
18024          accounts properly for it.  */
18025       if (AT_class (a) == dw_val_class_str)
18026         a->dw_attr_val.v.val_str->refcount = 0;
18027     }
18028 }
18029
18030
18031 /* Mark DIE as being used.  If DOKIDS is true, then walk down
18032    to DIE's children.  */
18033
18034 static void
18035 prune_unused_types_mark (dw_die_ref die, int dokids)
18036 {
18037   dw_die_ref c;
18038
18039   if (die->die_mark == 0)
18040     {
18041       /* We haven't done this node yet.  Mark it as used.  */
18042       die->die_mark = 1;
18043
18044       /* We also have to mark its parents as used.
18045          (But we don't want to mark our parents' kids due to this.)  */
18046       if (die->die_parent)
18047         prune_unused_types_mark (die->die_parent, 0);
18048
18049       /* Mark any referenced nodes.  */
18050       prune_unused_types_walk_attribs (die);
18051
18052       /* If this node is a specification,
18053          also mark the definition, if it exists.  */
18054       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
18055         prune_unused_types_mark (die->die_definition, 1);
18056     }
18057
18058   if (dokids && die->die_mark != 2)
18059     {
18060       /* We need to walk the children, but haven't done so yet.
18061          Remember that we've walked the kids.  */
18062       die->die_mark = 2;
18063
18064       /* If this is an array type, we need to make sure our
18065          kids get marked, even if they're types.  */
18066       if (die->die_tag == DW_TAG_array_type)
18067         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
18068       else
18069         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18070     }
18071 }
18072
18073 /* For local classes, look if any static member functions were emitted
18074    and if so, mark them.  */
18075
18076 static void
18077 prune_unused_types_walk_local_classes (dw_die_ref die)
18078 {
18079   dw_die_ref c;
18080
18081   if (die->die_mark == 2)
18082     return;
18083
18084   switch (die->die_tag)
18085     {
18086     case DW_TAG_structure_type:
18087     case DW_TAG_union_type:
18088     case DW_TAG_class_type:
18089       break;
18090
18091     case DW_TAG_subprogram:
18092       if (!get_AT_flag (die, DW_AT_declaration)
18093           || die->die_definition != NULL)
18094         prune_unused_types_mark (die, 1);
18095       return;
18096
18097     default:
18098       return;
18099     }
18100
18101   /* Mark children.  */
18102   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
18103 }
18104
18105 /* Walk the tree DIE and mark types that we actually use.  */
18106
18107 static void
18108 prune_unused_types_walk (dw_die_ref die)
18109 {
18110   dw_die_ref c;
18111
18112   /* Don't do anything if this node is already marked and
18113      children have been marked as well.  */
18114   if (die->die_mark == 2)
18115     return;
18116
18117   switch (die->die_tag)
18118     {
18119     case DW_TAG_structure_type:
18120     case DW_TAG_union_type:
18121     case DW_TAG_class_type:
18122       if (die->die_perennial_p)
18123         break;
18124
18125       for (c = die->die_parent; c; c = c->die_parent)
18126         if (c->die_tag == DW_TAG_subprogram)
18127           break;
18128
18129       /* Finding used static member functions inside of classes
18130          is needed just for local classes, because for other classes
18131          static member function DIEs with DW_AT_specification
18132          are emitted outside of the DW_TAG_*_type.  If we ever change
18133          it, we'd need to call this even for non-local classes.  */
18134       if (c)
18135         prune_unused_types_walk_local_classes (die);
18136
18137       /* It's a type node --- don't mark it.  */
18138       return;
18139
18140     case DW_TAG_const_type:
18141     case DW_TAG_packed_type:
18142     case DW_TAG_pointer_type:
18143     case DW_TAG_reference_type:
18144     case DW_TAG_volatile_type:
18145     case DW_TAG_typedef:
18146     case DW_TAG_array_type:
18147     case DW_TAG_interface_type:
18148     case DW_TAG_friend:
18149     case DW_TAG_variant_part:
18150     case DW_TAG_enumeration_type:
18151     case DW_TAG_subroutine_type:
18152     case DW_TAG_string_type:
18153     case DW_TAG_set_type:
18154     case DW_TAG_subrange_type:
18155     case DW_TAG_ptr_to_member_type:
18156     case DW_TAG_file_type:
18157       if (die->die_perennial_p)
18158         break;
18159
18160       /* It's a type node --- don't mark it.  */
18161       return;
18162
18163     default:
18164       /* Mark everything else.  */
18165       break;
18166   }
18167
18168   if (die->die_mark == 0)
18169     {
18170       die->die_mark = 1;
18171
18172       /* Now, mark any dies referenced from here.  */
18173       prune_unused_types_walk_attribs (die);
18174     }
18175
18176   die->die_mark = 2;
18177
18178   /* Mark children.  */
18179   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18180 }
18181
18182 /* Increment the string counts on strings referred to from DIE's
18183    attributes.  */
18184
18185 static void
18186 prune_unused_types_update_strings (dw_die_ref die)
18187 {
18188   dw_attr_ref a;
18189   unsigned ix;
18190
18191   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18192     if (AT_class (a) == dw_val_class_str)
18193       {
18194         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
18195         s->refcount++;
18196         /* Avoid unnecessarily putting strings that are used less than
18197            twice in the hash table.  */
18198         if (s->refcount
18199             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
18200           {
18201             void ** slot;
18202             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
18203                                              htab_hash_string (s->str),
18204                                              INSERT);
18205             gcc_assert (*slot == NULL);
18206             *slot = s;
18207           }
18208       }
18209 }
18210
18211 /* Remove from the tree DIE any dies that aren't marked.  */
18212
18213 static void
18214 prune_unused_types_prune (dw_die_ref die)
18215 {
18216   dw_die_ref c;
18217
18218   gcc_assert (die->die_mark);
18219   prune_unused_types_update_strings (die);
18220
18221   if (! die->die_child)
18222     return;
18223
18224   c = die->die_child;
18225   do {
18226     dw_die_ref prev = c;
18227     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
18228       if (c == die->die_child)
18229         {
18230           /* No marked children between 'prev' and the end of the list.  */
18231           if (prev == c)
18232             /* No marked children at all.  */
18233             die->die_child = NULL;
18234           else
18235             {
18236               prev->die_sib = c->die_sib;
18237               die->die_child = prev;
18238             }
18239           return;
18240         }
18241
18242     if (c != prev->die_sib)
18243       prev->die_sib = c;
18244     prune_unused_types_prune (c);
18245   } while (c != die->die_child);
18246 }
18247
18248 /* A helper function for dwarf2out_finish called through
18249    htab_traverse.  Clear .debug_str strings that we haven't already
18250    decided to emit.  */
18251
18252 static int
18253 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
18254 {
18255   struct indirect_string_node *node = (struct indirect_string_node *) *h;
18256
18257   if (!node->label || !node->refcount)
18258     htab_clear_slot (debug_str_hash, h);
18259
18260   return 1;
18261 }
18262
18263 /* Remove dies representing declarations that we never use.  */
18264
18265 static void
18266 prune_unused_types (void)
18267 {
18268   unsigned int i;
18269   limbo_die_node *node;
18270   pubname_ref pub;
18271
18272 #if ENABLE_ASSERT_CHECKING
18273   /* All the marks should already be clear.  */
18274   verify_marks_clear (comp_unit_die);
18275   for (node = limbo_die_list; node; node = node->next)
18276     verify_marks_clear (node->die);
18277 #endif /* ENABLE_ASSERT_CHECKING */
18278
18279   /* Set the mark on nodes that are actually used.  */
18280   prune_unused_types_walk (comp_unit_die);
18281   for (node = limbo_die_list; node; node = node->next)
18282     prune_unused_types_walk (node->die);
18283
18284   /* Also set the mark on nodes referenced from the
18285      pubname_table or arange_table.  */
18286   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
18287     prune_unused_types_mark (pub->die, 1);
18288   for (i = 0; i < arange_table_in_use; i++)
18289     prune_unused_types_mark (arange_table[i], 1);
18290
18291   /* Get rid of nodes that aren't marked; and update the string counts.  */
18292   if (debug_str_hash && debug_str_hash_forced)
18293     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
18294   else if (debug_str_hash)
18295     htab_empty (debug_str_hash);
18296   prune_unused_types_prune (comp_unit_die);
18297   for (node = limbo_die_list; node; node = node->next)
18298     prune_unused_types_prune (node->die);
18299
18300   /* Leave the marks clear.  */
18301   prune_unmark_dies (comp_unit_die);
18302   for (node = limbo_die_list; node; node = node->next)
18303     prune_unmark_dies (node->die);
18304 }
18305
18306 /* Set the parameter to true if there are any relative pathnames in
18307    the file table.  */
18308 static int
18309 file_table_relative_p (void ** slot, void *param)
18310 {
18311   bool *p = (bool *) param;
18312   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
18313   if (!IS_ABSOLUTE_PATH (d->filename))
18314     {
18315       *p = true;
18316       return 0;
18317     }
18318   return 1;
18319 }
18320
18321 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
18322    to the location it would have been added, should we know its
18323    DECL_ASSEMBLER_NAME when we added other attributes.  This will
18324    probably improve compactness of debug info, removing equivalent
18325    abbrevs, and hide any differences caused by deferring the
18326    computation of the assembler name, triggered by e.g. PCH.  */
18327
18328 static inline void
18329 move_linkage_attr (dw_die_ref die)
18330 {
18331   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
18332   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
18333
18334   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
18335
18336   while (--ix > 0)
18337     {
18338       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
18339
18340       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
18341         break;
18342     }
18343
18344   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
18345     {
18346       VEC_pop (dw_attr_node, die->die_attr);
18347       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
18348     }
18349 }
18350
18351 /* Output stuff that dwarf requires at the end of every file,
18352    and generate the DWARF-2 debugging info.  */
18353
18354 static void
18355 dwarf2out_finish (const char *filename)
18356 {
18357   limbo_die_node *node, *next_node;
18358   dw_die_ref die = 0;
18359   unsigned int i;
18360
18361   gen_remaining_tmpl_value_param_die_attribute ();
18362
18363   /* Add the name for the main input file now.  We delayed this from
18364      dwarf2out_init to avoid complications with PCH.  */
18365   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
18366   if (!IS_ABSOLUTE_PATH (filename))
18367     add_comp_dir_attribute (comp_unit_die);
18368   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
18369     {
18370       bool p = false;
18371       htab_traverse (file_table, file_table_relative_p, &p);
18372       if (p)
18373         add_comp_dir_attribute (comp_unit_die);
18374     }
18375
18376   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
18377     {
18378       add_location_or_const_value_attribute (
18379         VEC_index (deferred_locations, deferred_locations_list, i)->die,
18380         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
18381         DW_AT_location);
18382     }
18383
18384   /* Traverse the limbo die list, and add parent/child links.  The only
18385      dies without parents that should be here are concrete instances of
18386      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
18387      For concrete instances, we can get the parent die from the abstract
18388      instance.  */
18389   for (node = limbo_die_list; node; node = next_node)
18390     {
18391       next_node = node->next;
18392       die = node->die;
18393
18394       if (die->die_parent == NULL)
18395         {
18396           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
18397
18398           if (origin)
18399             add_child_die (origin->die_parent, die);
18400           else if (die == comp_unit_die)
18401             ;
18402           else if (errorcount > 0 || sorrycount > 0)
18403             /* It's OK to be confused by errors in the input.  */
18404             add_child_die (comp_unit_die, die);
18405           else
18406             {
18407               /* In certain situations, the lexical block containing a
18408                  nested function can be optimized away, which results
18409                  in the nested function die being orphaned.  Likewise
18410                  with the return type of that nested function.  Force
18411                  this to be a child of the containing function.
18412
18413                  It may happen that even the containing function got fully
18414                  inlined and optimized out.  In that case we are lost and
18415                  assign the empty child.  This should not be big issue as
18416                  the function is likely unreachable too.  */
18417               tree context = NULL_TREE;
18418
18419               gcc_assert (node->created_for);
18420
18421               if (DECL_P (node->created_for))
18422                 context = DECL_CONTEXT (node->created_for);
18423               else if (TYPE_P (node->created_for))
18424                 context = TYPE_CONTEXT (node->created_for);
18425
18426               gcc_assert (context
18427                           && (TREE_CODE (context) == FUNCTION_DECL
18428                               || TREE_CODE (context) == NAMESPACE_DECL));
18429
18430               origin = lookup_decl_die (context);
18431               if (origin)
18432                 add_child_die (origin, die);
18433               else
18434                 add_child_die (comp_unit_die, die);
18435             }
18436         }
18437     }
18438
18439   limbo_die_list = NULL;
18440
18441   for (node = deferred_asm_name; node; node = node->next)
18442     {
18443       tree decl = node->created_for;
18444       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18445         {
18446           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
18447                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
18448           move_linkage_attr (node->die);
18449         }
18450     }
18451
18452   deferred_asm_name = NULL;
18453
18454   /* Walk through the list of incomplete types again, trying once more to
18455      emit full debugging info for them.  */
18456   retry_incomplete_types ();
18457
18458   if (flag_eliminate_unused_debug_types)
18459     prune_unused_types ();
18460
18461   /* Generate separate CUs for each of the include files we've seen.
18462      They will go into limbo_die_list.  */
18463   if (flag_eliminate_dwarf2_dups)
18464     break_out_includes (comp_unit_die);
18465
18466   /* Traverse the DIE's and add add sibling attributes to those DIE's
18467      that have children.  */
18468   add_sibling_attributes (comp_unit_die);
18469   for (node = limbo_die_list; node; node = node->next)
18470     add_sibling_attributes (node->die);
18471
18472   /* Output a terminator label for the .text section.  */
18473   switch_to_section (text_section);
18474   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
18475   if (flag_reorder_blocks_and_partition)
18476     {
18477       switch_to_section (unlikely_text_section ());
18478       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
18479     }
18480
18481   /* We can only use the low/high_pc attributes if all of the code was
18482      in .text.  */
18483   if (!have_multiple_function_sections)
18484     {
18485       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
18486       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
18487     }
18488
18489   else
18490     {
18491       unsigned fde_idx = 0;
18492
18493       /* We need to give .debug_loc and .debug_ranges an appropriate
18494          "base address".  Use zero so that these addresses become
18495          absolute.  Historically, we've emitted the unexpected
18496          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
18497          Emit both to give time for other tools to adapt.  */
18498       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
18499       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
18500
18501       add_AT_range_list (comp_unit_die, DW_AT_ranges,
18502                          add_ranges_by_labels (text_section_label,
18503                                                text_end_label));
18504       if (flag_reorder_blocks_and_partition)
18505         add_ranges_by_labels (cold_text_section_label,
18506                               cold_end_label);
18507
18508       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
18509         {
18510           dw_fde_ref fde = &fde_table[fde_idx];
18511
18512           if (fde->dw_fde_switched_sections)
18513             {
18514               if (!fde->in_std_section)
18515                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
18516                                       fde->dw_fde_hot_section_end_label);
18517               if (!fde->cold_in_std_section)
18518                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
18519                                       fde->dw_fde_unlikely_section_end_label);
18520             }
18521           else if (!fde->in_std_section)
18522             add_ranges_by_labels (fde->dw_fde_begin,
18523                                   fde->dw_fde_end);
18524         }
18525
18526       add_ranges (NULL);
18527     }
18528
18529   /* Output location list section if necessary.  */
18530   if (have_location_lists)
18531     {
18532       /* Output the location lists info.  */
18533       switch_to_section (debug_loc_section);
18534       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
18535                                    DEBUG_LOC_SECTION_LABEL, 0);
18536       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
18537       output_location_lists (die);
18538     }
18539
18540   if (debug_info_level >= DINFO_LEVEL_NORMAL)
18541     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
18542                     debug_line_section_label);
18543
18544   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18545     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
18546
18547   /* Output all of the compilation units.  We put the main one last so that
18548      the offsets are available to output_pubnames.  */
18549   for (node = limbo_die_list; node; node = node->next)
18550     output_comp_unit (node->die, 0);
18551
18552   /* Output the main compilation unit if non-empty or if .debug_macinfo
18553      has been emitted.  */
18554   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
18555
18556   /* Output the abbreviation table.  */
18557   switch_to_section (debug_abbrev_section);
18558   output_abbrev_section ();
18559
18560   /* Output public names table if necessary.  */
18561   if (!VEC_empty (pubname_entry, pubname_table))
18562     {
18563       switch_to_section (debug_pubnames_section);
18564       output_pubnames (pubname_table);
18565     }
18566
18567   /* Output public types table if necessary.  */
18568   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
18569      It shouldn't hurt to emit it always, since pure DWARF2 consumers
18570      simply won't look for the section.  */
18571   if (!VEC_empty (pubname_entry, pubtype_table))
18572     {
18573       switch_to_section (debug_pubtypes_section);
18574       output_pubnames (pubtype_table);
18575     }
18576
18577   /* Output the address range information.  We only put functions in the arange
18578      table, so don't write it out if we don't have any.  */
18579   if (fde_table_in_use)
18580     {
18581       switch_to_section (debug_aranges_section);
18582       output_aranges ();
18583     }
18584
18585   /* Output ranges section if necessary.  */
18586   if (ranges_table_in_use)
18587     {
18588       switch_to_section (debug_ranges_section);
18589       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
18590       output_ranges ();
18591     }
18592
18593   /* Output the source line correspondence table.  We must do this
18594      even if there is no line information.  Otherwise, on an empty
18595      translation unit, we will generate a present, but empty,
18596      .debug_info section.  IRIX 6.5 `nm' will then complain when
18597      examining the file.  This is done late so that any filenames
18598      used by the debug_info section are marked as 'used'.  */
18599   if (! DWARF2_ASM_LINE_DEBUG_INFO)
18600     {
18601       switch_to_section (debug_line_section);
18602       output_line_info ();
18603     }
18604
18605   /* Have to end the macro section.  */
18606   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18607     {
18608       switch_to_section (debug_macinfo_section);
18609       dw2_asm_output_data (1, 0, "End compilation unit");
18610     }
18611
18612   /* If we emitted any DW_FORM_strp form attribute, output the string
18613      table too.  */
18614   if (debug_str_hash)
18615     htab_traverse (debug_str_hash, output_indirect_string, NULL);
18616 }
18617 #else
18618
18619 /* This should never be used, but its address is needed for comparisons.  */
18620 const struct gcc_debug_hooks dwarf2_debug_hooks =
18621 {
18622   0,            /* init */
18623   0,            /* finish */
18624   0,            /* define */
18625   0,            /* undef */
18626   0,            /* start_source_file */
18627   0,            /* end_source_file */
18628   0,            /* begin_block */
18629   0,            /* end_block */
18630   0,            /* ignore_block */
18631   0,            /* source_line */
18632   0,            /* begin_prologue */
18633   0,            /* end_prologue */
18634   0,            /* end_epilogue */
18635   0,            /* begin_function */
18636   0,            /* end_function */
18637   0,            /* function_decl */
18638   0,            /* global_decl */
18639   0,            /* type_decl */
18640   0,            /* imported_module_or_decl */
18641   0,            /* deferred_inline_function */
18642   0,            /* outlining_inline_function */
18643   0,            /* label */
18644   0,            /* handle_pch */
18645   0,            /* var_location */
18646   0,            /* switch_text_section */
18647   0,            /* set_name */
18648   0             /* start_end_main_source_file */
18649 };
18650
18651 #endif /* DWARF2_DEBUGGING_INFO */
18652
18653 #include "gt-dwarf2out.h"