OSDN Git Service

2009-09-14 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   section *fnsec;
3885
3886   current_function_func_begin_label = NULL;
3887
3888 #ifdef TARGET_UNWIND_INFO
3889   /* ??? current_function_func_begin_label is also used by except.c
3890      for call-site information.  We must emit this label if it might
3891      be used.  */
3892   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3893       && ! dwarf2out_do_frame ())
3894     return;
3895 #else
3896   if (! dwarf2out_do_frame ())
3897     return;
3898 #endif
3899
3900   fnsec = function_section (current_function_decl);
3901   switch_to_section (fnsec);
3902   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3903                                current_function_funcdef_no);
3904   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3905                           current_function_funcdef_no);
3906   dup_label = xstrdup (label);
3907   current_function_func_begin_label = dup_label;
3908
3909 #ifdef TARGET_UNWIND_INFO
3910   /* We can elide the fde allocation if we're not emitting debug info.  */
3911   if (! dwarf2out_do_frame ())
3912     return;
3913 #endif
3914
3915   /* Expand the fde table if necessary.  */
3916   if (fde_table_in_use == fde_table_allocated)
3917     {
3918       fde_table_allocated += FDE_TABLE_INCREMENT;
3919       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3920       memset (fde_table + fde_table_in_use, 0,
3921               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3922     }
3923
3924   /* Record the FDE associated with this function.  */
3925   current_funcdef_fde = fde_table_in_use;
3926
3927   /* Add the new FDE at the end of the fde_table.  */
3928   fde = &fde_table[fde_table_in_use++];
3929   fde->decl = current_function_decl;
3930   fde->dw_fde_begin = dup_label;
3931   fde->dw_fde_current_label = dup_label;
3932   fde->dw_fde_hot_section_label = NULL;
3933   fde->dw_fde_hot_section_end_label = NULL;
3934   fde->dw_fde_unlikely_section_label = NULL;
3935   fde->dw_fde_unlikely_section_end_label = NULL;
3936   fde->dw_fde_switched_sections = 0;
3937   fde->dw_fde_switched_cold_to_hot = 0;
3938   fde->dw_fde_end = NULL;
3939   fde->dw_fde_cfi = NULL;
3940   fde->dw_fde_switch_cfi = NULL;
3941   fde->funcdef_number = current_function_funcdef_no;
3942   fde->nothrow = crtl->nothrow;
3943   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3944   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3945   fde->drap_reg = INVALID_REGNUM;
3946   fde->vdrap_reg = INVALID_REGNUM;
3947   if (flag_reorder_blocks_and_partition)
3948     {
3949       section *unlikelysec;
3950       if (first_function_block_is_cold)
3951         fde->in_std_section = 1;
3952       else
3953         fde->in_std_section
3954           = (fnsec == text_section
3955              || (cold_text_section && fnsec == cold_text_section));
3956       unlikelysec = unlikely_text_section ();
3957       fde->cold_in_std_section
3958         = (unlikelysec == text_section
3959            || (cold_text_section && unlikelysec == cold_text_section));
3960     }
3961   else
3962     {
3963       fde->in_std_section
3964         = (fnsec == text_section
3965            || (cold_text_section && fnsec == cold_text_section));
3966       fde->cold_in_std_section = 0;
3967     }
3968
3969   args_size = old_args_size = 0;
3970
3971   /* We only want to output line number information for the genuine dwarf2
3972      prologue case, not the eh frame case.  */
3973 #ifdef DWARF2_DEBUGGING_INFO
3974   if (file)
3975     dwarf2out_source_line (line, file, 0, true);
3976 #endif
3977
3978   if (dwarf2out_do_cfi_asm ())
3979     dwarf2out_do_cfi_startproc (false);
3980   else
3981     {
3982       rtx personality = get_personality_function (current_function_decl);
3983       if (!current_unit_personality)
3984         current_unit_personality = personality;
3985
3986       /* We cannot keep a current personality per function as without CFI
3987          asm at the point where we emit the CFI data there is no current
3988          function anymore.  */
3989       if (personality
3990           && current_unit_personality != personality)
3991         sorry ("Multiple EH personalities are supported only with assemblers "
3992                "supporting .cfi.personality directive.");
3993     }
3994 }
3995
3996 /* Output a marker (i.e. a label) for the absolute end of the generated code
3997    for a function definition.  This gets called *after* the epilogue code has
3998    been generated.  */
3999
4000 void
4001 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4002                         const char *file ATTRIBUTE_UNUSED)
4003 {
4004   dw_fde_ref fde;
4005   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4006
4007 #ifdef DWARF2_DEBUGGING_INFO
4008   last_var_location_insn = NULL_RTX;
4009 #endif
4010
4011   if (dwarf2out_do_cfi_asm ())
4012     fprintf (asm_out_file, "\t.cfi_endproc\n");
4013
4014   /* Output a label to mark the endpoint of the code generated for this
4015      function.  */
4016   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4017                                current_function_funcdef_no);
4018   ASM_OUTPUT_LABEL (asm_out_file, label);
4019   fde = current_fde ();
4020   gcc_assert (fde != NULL);
4021   fde->dw_fde_end = xstrdup (label);
4022 }
4023
4024 void
4025 dwarf2out_frame_init (void)
4026 {
4027   /* Allocate the initial hunk of the fde_table.  */
4028   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4029   fde_table_allocated = FDE_TABLE_INCREMENT;
4030   fde_table_in_use = 0;
4031
4032   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4033      sake of lookup_cfa.  */
4034
4035   /* On entry, the Canonical Frame Address is at SP.  */
4036   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4037
4038 #ifdef DWARF2_UNWIND_INFO
4039   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4040     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4041 #endif
4042 }
4043
4044 void
4045 dwarf2out_frame_finish (void)
4046 {
4047   /* Output call frame information.  */
4048   if (DWARF2_FRAME_INFO)
4049     output_call_frame_info (0);
4050
4051 #ifndef TARGET_UNWIND_INFO
4052   /* Output another copy for the unwinder.  */
4053   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4054     output_call_frame_info (1);
4055 #endif
4056 }
4057
4058 /* Note that the current function section is being used for code.  */
4059
4060 static void
4061 dwarf2out_note_section_used (void)
4062 {
4063   section *sec = current_function_section ();
4064   if (sec == text_section)
4065     text_section_used = true;
4066   else if (sec == cold_text_section)
4067     cold_text_section_used = true;
4068 }
4069
4070 void
4071 dwarf2out_switch_text_section (void)
4072 {
4073   dw_fde_ref fde = current_fde ();
4074
4075   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4076
4077   fde->dw_fde_switched_sections = 1;
4078   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4079
4080   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4081   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4082   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4083   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4084   have_multiple_function_sections = true;
4085
4086   /* Reset the current label on switching text sections, so that we
4087      don't attempt to advance_loc4 between labels in different sections.  */
4088   fde->dw_fde_current_label = NULL;
4089
4090   /* There is no need to mark used sections when not debugging.  */
4091   if (cold_text_section != NULL)
4092     dwarf2out_note_section_used ();
4093
4094   if (dwarf2out_do_cfi_asm ())
4095     fprintf (asm_out_file, "\t.cfi_endproc\n");
4096
4097   /* Now do the real section switch.  */
4098   switch_to_section (current_function_section ());
4099
4100   if (dwarf2out_do_cfi_asm ())
4101     {
4102       dwarf2out_do_cfi_startproc (true);
4103       /* As this is a different FDE, insert all current CFI instructions
4104          again.  */
4105       output_cfis (fde->dw_fde_cfi, true, fde, true);
4106     }
4107   else
4108     {
4109       dw_cfi_ref cfi = fde->dw_fde_cfi;
4110
4111       cfi = fde->dw_fde_cfi;
4112       if (cfi)
4113         while (cfi->dw_cfi_next != NULL)
4114           cfi = cfi->dw_cfi_next;
4115       fde->dw_fde_switch_cfi = cfi;
4116     }
4117 }
4118 #endif
4119 \f
4120 /* And now, the subset of the debugging information support code necessary
4121    for emitting location expressions.  */
4122
4123 /* Data about a single source file.  */
4124 struct GTY(()) dwarf_file_data {
4125   const char * filename;
4126   int emitted_number;
4127 };
4128
4129 typedef struct dw_val_struct *dw_val_ref;
4130 typedef struct die_struct *dw_die_ref;
4131 typedef const struct die_struct *const_dw_die_ref;
4132 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4133 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4134
4135 typedef struct GTY(()) deferred_locations_struct
4136 {
4137   tree variable;
4138   dw_die_ref die;
4139 } deferred_locations;
4140
4141 DEF_VEC_O(deferred_locations);
4142 DEF_VEC_ALLOC_O(deferred_locations,gc);
4143
4144 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4145
4146 /* Each DIE may have a series of attribute/value pairs.  Values
4147    can take on several forms.  The forms that are used in this
4148    implementation are listed below.  */
4149
4150 enum dw_val_class
4151 {
4152   dw_val_class_addr,
4153   dw_val_class_offset,
4154   dw_val_class_loc,
4155   dw_val_class_loc_list,
4156   dw_val_class_range_list,
4157   dw_val_class_const,
4158   dw_val_class_unsigned_const,
4159   dw_val_class_long_long,
4160   dw_val_class_vec,
4161   dw_val_class_flag,
4162   dw_val_class_die_ref,
4163   dw_val_class_fde_ref,
4164   dw_val_class_lbl_id,
4165   dw_val_class_lineptr,
4166   dw_val_class_str,
4167   dw_val_class_macptr,
4168   dw_val_class_file
4169 };
4170
4171 /* Describe a floating point constant value, or a vector constant value.  */
4172
4173 typedef struct GTY(()) dw_vec_struct {
4174   unsigned char * GTY((length ("%h.length"))) array;
4175   unsigned length;
4176   unsigned elt_size;
4177 }
4178 dw_vec_const;
4179
4180 /* The dw_val_node describes an attribute's value, as it is
4181    represented internally.  */
4182
4183 typedef struct GTY(()) dw_val_struct {
4184   enum dw_val_class val_class;
4185   union dw_val_struct_union
4186     {
4187       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4188       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4189       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4190       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4191       HOST_WIDE_INT GTY ((default)) val_int;
4192       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4193       rtx GTY ((tag ("dw_val_class_long_long"))) val_long_long;
4194       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4195       struct dw_val_die_union
4196         {
4197           dw_die_ref die;
4198           int external;
4199         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4200       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4201       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4202       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4203       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4204       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4205     }
4206   GTY ((desc ("%1.val_class"))) v;
4207 }
4208 dw_val_node;
4209
4210 /* Locations in memory are described using a sequence of stack machine
4211    operations.  */
4212
4213 typedef struct GTY(()) dw_loc_descr_struct {
4214   dw_loc_descr_ref dw_loc_next;
4215   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4216   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4217      from DW_OP_addr with a dtp-relative symbol relocation.  */
4218   unsigned int dtprel : 1;
4219   int dw_loc_addr;
4220   dw_val_node dw_loc_oprnd1;
4221   dw_val_node dw_loc_oprnd2;
4222 }
4223 dw_loc_descr_node;
4224
4225 /* Location lists are ranges + location descriptions for that range,
4226    so you can track variables that are in different places over
4227    their entire life.  */
4228 typedef struct GTY(()) dw_loc_list_struct {
4229   dw_loc_list_ref dw_loc_next;
4230   const char *begin; /* Label for begin address of range */
4231   const char *end;  /* Label for end address of range */
4232   char *ll_symbol; /* Label for beginning of location list.
4233                       Only on head of list */
4234   const char *section; /* Section this loclist is relative to */
4235   dw_loc_descr_ref expr;
4236 } dw_loc_list_node;
4237
4238 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4239
4240 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4241
4242 /* Convert a DWARF stack opcode into its string name.  */
4243
4244 static const char *
4245 dwarf_stack_op_name (unsigned int op)
4246 {
4247   switch (op)
4248     {
4249     case DW_OP_addr:
4250       return "DW_OP_addr";
4251     case DW_OP_deref:
4252       return "DW_OP_deref";
4253     case DW_OP_const1u:
4254       return "DW_OP_const1u";
4255     case DW_OP_const1s:
4256       return "DW_OP_const1s";
4257     case DW_OP_const2u:
4258       return "DW_OP_const2u";
4259     case DW_OP_const2s:
4260       return "DW_OP_const2s";
4261     case DW_OP_const4u:
4262       return "DW_OP_const4u";
4263     case DW_OP_const4s:
4264       return "DW_OP_const4s";
4265     case DW_OP_const8u:
4266       return "DW_OP_const8u";
4267     case DW_OP_const8s:
4268       return "DW_OP_const8s";
4269     case DW_OP_constu:
4270       return "DW_OP_constu";
4271     case DW_OP_consts:
4272       return "DW_OP_consts";
4273     case DW_OP_dup:
4274       return "DW_OP_dup";
4275     case DW_OP_drop:
4276       return "DW_OP_drop";
4277     case DW_OP_over:
4278       return "DW_OP_over";
4279     case DW_OP_pick:
4280       return "DW_OP_pick";
4281     case DW_OP_swap:
4282       return "DW_OP_swap";
4283     case DW_OP_rot:
4284       return "DW_OP_rot";
4285     case DW_OP_xderef:
4286       return "DW_OP_xderef";
4287     case DW_OP_abs:
4288       return "DW_OP_abs";
4289     case DW_OP_and:
4290       return "DW_OP_and";
4291     case DW_OP_div:
4292       return "DW_OP_div";
4293     case DW_OP_minus:
4294       return "DW_OP_minus";
4295     case DW_OP_mod:
4296       return "DW_OP_mod";
4297     case DW_OP_mul:
4298       return "DW_OP_mul";
4299     case DW_OP_neg:
4300       return "DW_OP_neg";
4301     case DW_OP_not:
4302       return "DW_OP_not";
4303     case DW_OP_or:
4304       return "DW_OP_or";
4305     case DW_OP_plus:
4306       return "DW_OP_plus";
4307     case DW_OP_plus_uconst:
4308       return "DW_OP_plus_uconst";
4309     case DW_OP_shl:
4310       return "DW_OP_shl";
4311     case DW_OP_shr:
4312       return "DW_OP_shr";
4313     case DW_OP_shra:
4314       return "DW_OP_shra";
4315     case DW_OP_xor:
4316       return "DW_OP_xor";
4317     case DW_OP_bra:
4318       return "DW_OP_bra";
4319     case DW_OP_eq:
4320       return "DW_OP_eq";
4321     case DW_OP_ge:
4322       return "DW_OP_ge";
4323     case DW_OP_gt:
4324       return "DW_OP_gt";
4325     case DW_OP_le:
4326       return "DW_OP_le";
4327     case DW_OP_lt:
4328       return "DW_OP_lt";
4329     case DW_OP_ne:
4330       return "DW_OP_ne";
4331     case DW_OP_skip:
4332       return "DW_OP_skip";
4333     case DW_OP_lit0:
4334       return "DW_OP_lit0";
4335     case DW_OP_lit1:
4336       return "DW_OP_lit1";
4337     case DW_OP_lit2:
4338       return "DW_OP_lit2";
4339     case DW_OP_lit3:
4340       return "DW_OP_lit3";
4341     case DW_OP_lit4:
4342       return "DW_OP_lit4";
4343     case DW_OP_lit5:
4344       return "DW_OP_lit5";
4345     case DW_OP_lit6:
4346       return "DW_OP_lit6";
4347     case DW_OP_lit7:
4348       return "DW_OP_lit7";
4349     case DW_OP_lit8:
4350       return "DW_OP_lit8";
4351     case DW_OP_lit9:
4352       return "DW_OP_lit9";
4353     case DW_OP_lit10:
4354       return "DW_OP_lit10";
4355     case DW_OP_lit11:
4356       return "DW_OP_lit11";
4357     case DW_OP_lit12:
4358       return "DW_OP_lit12";
4359     case DW_OP_lit13:
4360       return "DW_OP_lit13";
4361     case DW_OP_lit14:
4362       return "DW_OP_lit14";
4363     case DW_OP_lit15:
4364       return "DW_OP_lit15";
4365     case DW_OP_lit16:
4366       return "DW_OP_lit16";
4367     case DW_OP_lit17:
4368       return "DW_OP_lit17";
4369     case DW_OP_lit18:
4370       return "DW_OP_lit18";
4371     case DW_OP_lit19:
4372       return "DW_OP_lit19";
4373     case DW_OP_lit20:
4374       return "DW_OP_lit20";
4375     case DW_OP_lit21:
4376       return "DW_OP_lit21";
4377     case DW_OP_lit22:
4378       return "DW_OP_lit22";
4379     case DW_OP_lit23:
4380       return "DW_OP_lit23";
4381     case DW_OP_lit24:
4382       return "DW_OP_lit24";
4383     case DW_OP_lit25:
4384       return "DW_OP_lit25";
4385     case DW_OP_lit26:
4386       return "DW_OP_lit26";
4387     case DW_OP_lit27:
4388       return "DW_OP_lit27";
4389     case DW_OP_lit28:
4390       return "DW_OP_lit28";
4391     case DW_OP_lit29:
4392       return "DW_OP_lit29";
4393     case DW_OP_lit30:
4394       return "DW_OP_lit30";
4395     case DW_OP_lit31:
4396       return "DW_OP_lit31";
4397     case DW_OP_reg0:
4398       return "DW_OP_reg0";
4399     case DW_OP_reg1:
4400       return "DW_OP_reg1";
4401     case DW_OP_reg2:
4402       return "DW_OP_reg2";
4403     case DW_OP_reg3:
4404       return "DW_OP_reg3";
4405     case DW_OP_reg4:
4406       return "DW_OP_reg4";
4407     case DW_OP_reg5:
4408       return "DW_OP_reg5";
4409     case DW_OP_reg6:
4410       return "DW_OP_reg6";
4411     case DW_OP_reg7:
4412       return "DW_OP_reg7";
4413     case DW_OP_reg8:
4414       return "DW_OP_reg8";
4415     case DW_OP_reg9:
4416       return "DW_OP_reg9";
4417     case DW_OP_reg10:
4418       return "DW_OP_reg10";
4419     case DW_OP_reg11:
4420       return "DW_OP_reg11";
4421     case DW_OP_reg12:
4422       return "DW_OP_reg12";
4423     case DW_OP_reg13:
4424       return "DW_OP_reg13";
4425     case DW_OP_reg14:
4426       return "DW_OP_reg14";
4427     case DW_OP_reg15:
4428       return "DW_OP_reg15";
4429     case DW_OP_reg16:
4430       return "DW_OP_reg16";
4431     case DW_OP_reg17:
4432       return "DW_OP_reg17";
4433     case DW_OP_reg18:
4434       return "DW_OP_reg18";
4435     case DW_OP_reg19:
4436       return "DW_OP_reg19";
4437     case DW_OP_reg20:
4438       return "DW_OP_reg20";
4439     case DW_OP_reg21:
4440       return "DW_OP_reg21";
4441     case DW_OP_reg22:
4442       return "DW_OP_reg22";
4443     case DW_OP_reg23:
4444       return "DW_OP_reg23";
4445     case DW_OP_reg24:
4446       return "DW_OP_reg24";
4447     case DW_OP_reg25:
4448       return "DW_OP_reg25";
4449     case DW_OP_reg26:
4450       return "DW_OP_reg26";
4451     case DW_OP_reg27:
4452       return "DW_OP_reg27";
4453     case DW_OP_reg28:
4454       return "DW_OP_reg28";
4455     case DW_OP_reg29:
4456       return "DW_OP_reg29";
4457     case DW_OP_reg30:
4458       return "DW_OP_reg30";
4459     case DW_OP_reg31:
4460       return "DW_OP_reg31";
4461     case DW_OP_breg0:
4462       return "DW_OP_breg0";
4463     case DW_OP_breg1:
4464       return "DW_OP_breg1";
4465     case DW_OP_breg2:
4466       return "DW_OP_breg2";
4467     case DW_OP_breg3:
4468       return "DW_OP_breg3";
4469     case DW_OP_breg4:
4470       return "DW_OP_breg4";
4471     case DW_OP_breg5:
4472       return "DW_OP_breg5";
4473     case DW_OP_breg6:
4474       return "DW_OP_breg6";
4475     case DW_OP_breg7:
4476       return "DW_OP_breg7";
4477     case DW_OP_breg8:
4478       return "DW_OP_breg8";
4479     case DW_OP_breg9:
4480       return "DW_OP_breg9";
4481     case DW_OP_breg10:
4482       return "DW_OP_breg10";
4483     case DW_OP_breg11:
4484       return "DW_OP_breg11";
4485     case DW_OP_breg12:
4486       return "DW_OP_breg12";
4487     case DW_OP_breg13:
4488       return "DW_OP_breg13";
4489     case DW_OP_breg14:
4490       return "DW_OP_breg14";
4491     case DW_OP_breg15:
4492       return "DW_OP_breg15";
4493     case DW_OP_breg16:
4494       return "DW_OP_breg16";
4495     case DW_OP_breg17:
4496       return "DW_OP_breg17";
4497     case DW_OP_breg18:
4498       return "DW_OP_breg18";
4499     case DW_OP_breg19:
4500       return "DW_OP_breg19";
4501     case DW_OP_breg20:
4502       return "DW_OP_breg20";
4503     case DW_OP_breg21:
4504       return "DW_OP_breg21";
4505     case DW_OP_breg22:
4506       return "DW_OP_breg22";
4507     case DW_OP_breg23:
4508       return "DW_OP_breg23";
4509     case DW_OP_breg24:
4510       return "DW_OP_breg24";
4511     case DW_OP_breg25:
4512       return "DW_OP_breg25";
4513     case DW_OP_breg26:
4514       return "DW_OP_breg26";
4515     case DW_OP_breg27:
4516       return "DW_OP_breg27";
4517     case DW_OP_breg28:
4518       return "DW_OP_breg28";
4519     case DW_OP_breg29:
4520       return "DW_OP_breg29";
4521     case DW_OP_breg30:
4522       return "DW_OP_breg30";
4523     case DW_OP_breg31:
4524       return "DW_OP_breg31";
4525     case DW_OP_regx:
4526       return "DW_OP_regx";
4527     case DW_OP_fbreg:
4528       return "DW_OP_fbreg";
4529     case DW_OP_bregx:
4530       return "DW_OP_bregx";
4531     case DW_OP_piece:
4532       return "DW_OP_piece";
4533     case DW_OP_deref_size:
4534       return "DW_OP_deref_size";
4535     case DW_OP_xderef_size:
4536       return "DW_OP_xderef_size";
4537     case DW_OP_nop:
4538       return "DW_OP_nop";
4539
4540     case DW_OP_push_object_address:
4541       return "DW_OP_push_object_address";
4542     case DW_OP_call2:
4543       return "DW_OP_call2";
4544     case DW_OP_call4:
4545       return "DW_OP_call4";
4546     case DW_OP_call_ref:
4547       return "DW_OP_call_ref";
4548     case DW_OP_implicit_value:
4549       return "DW_OP_implicit_value";
4550     case DW_OP_stack_value:
4551       return "DW_OP_stack_value";
4552     case DW_OP_form_tls_address:
4553       return "DW_OP_form_tls_address";
4554     case DW_OP_call_frame_cfa:
4555       return "DW_OP_call_frame_cfa";
4556     case DW_OP_bit_piece:
4557       return "DW_OP_bit_piece";
4558
4559     case DW_OP_GNU_push_tls_address:
4560       return "DW_OP_GNU_push_tls_address";
4561     case DW_OP_GNU_uninit:
4562       return "DW_OP_GNU_uninit";
4563     case DW_OP_GNU_encoded_addr:
4564       return "DW_OP_GNU_encoded_addr";
4565
4566     default:
4567       return "OP_<unknown>";
4568     }
4569 }
4570
4571 /* Return a pointer to a newly allocated location description.  Location
4572    descriptions are simple expression terms that can be strung
4573    together to form more complicated location (address) descriptions.  */
4574
4575 static inline dw_loc_descr_ref
4576 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4577                unsigned HOST_WIDE_INT oprnd2)
4578 {
4579   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4580
4581   descr->dw_loc_opc = op;
4582   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4583   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4584   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4585   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4586
4587   return descr;
4588 }
4589
4590 /* Return a pointer to a newly allocated location description for
4591    REG and OFFSET.  */
4592
4593 static inline dw_loc_descr_ref
4594 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4595 {
4596   if (reg <= 31)
4597     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4598                           offset, 0);
4599   else
4600     return new_loc_descr (DW_OP_bregx, reg, offset);
4601 }
4602
4603 /* Add a location description term to a location description expression.  */
4604
4605 static inline void
4606 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4607 {
4608   dw_loc_descr_ref *d;
4609
4610   /* Find the end of the chain.  */
4611   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4612     ;
4613
4614   *d = descr;
4615 }
4616
4617 /* Add a constant OFFSET to a location expression.  */
4618
4619 static void
4620 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4621 {
4622   dw_loc_descr_ref loc;
4623   HOST_WIDE_INT *p;
4624
4625   gcc_assert (*list_head != NULL);
4626
4627   if (!offset)
4628     return;
4629
4630   /* Find the end of the chain.  */
4631   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4632     ;
4633
4634   p = NULL;
4635   if (loc->dw_loc_opc == DW_OP_fbreg
4636       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4637     p = &loc->dw_loc_oprnd1.v.val_int;
4638   else if (loc->dw_loc_opc == DW_OP_bregx)
4639     p = &loc->dw_loc_oprnd2.v.val_int;
4640
4641   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4642      offset.  Don't optimize if an signed integer overflow would happen.  */
4643   if (p != NULL
4644       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4645           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4646     *p += offset;
4647
4648   else if (offset > 0)
4649     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4650
4651   else
4652     {
4653       loc->dw_loc_next = int_loc_descriptor (offset);
4654       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4655     }
4656 }
4657
4658 /* Return the size of a location descriptor.  */
4659
4660 static unsigned long
4661 size_of_loc_descr (dw_loc_descr_ref loc)
4662 {
4663   unsigned long size = 1;
4664
4665   switch (loc->dw_loc_opc)
4666     {
4667     case DW_OP_addr:
4668       size += DWARF2_ADDR_SIZE;
4669       break;
4670     case DW_OP_const1u:
4671     case DW_OP_const1s:
4672       size += 1;
4673       break;
4674     case DW_OP_const2u:
4675     case DW_OP_const2s:
4676       size += 2;
4677       break;
4678     case DW_OP_const4u:
4679     case DW_OP_const4s:
4680       size += 4;
4681       break;
4682     case DW_OP_const8u:
4683     case DW_OP_const8s:
4684       size += 8;
4685       break;
4686     case DW_OP_constu:
4687       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4688       break;
4689     case DW_OP_consts:
4690       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4691       break;
4692     case DW_OP_pick:
4693       size += 1;
4694       break;
4695     case DW_OP_plus_uconst:
4696       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4697       break;
4698     case DW_OP_skip:
4699     case DW_OP_bra:
4700       size += 2;
4701       break;
4702     case DW_OP_breg0:
4703     case DW_OP_breg1:
4704     case DW_OP_breg2:
4705     case DW_OP_breg3:
4706     case DW_OP_breg4:
4707     case DW_OP_breg5:
4708     case DW_OP_breg6:
4709     case DW_OP_breg7:
4710     case DW_OP_breg8:
4711     case DW_OP_breg9:
4712     case DW_OP_breg10:
4713     case DW_OP_breg11:
4714     case DW_OP_breg12:
4715     case DW_OP_breg13:
4716     case DW_OP_breg14:
4717     case DW_OP_breg15:
4718     case DW_OP_breg16:
4719     case DW_OP_breg17:
4720     case DW_OP_breg18:
4721     case DW_OP_breg19:
4722     case DW_OP_breg20:
4723     case DW_OP_breg21:
4724     case DW_OP_breg22:
4725     case DW_OP_breg23:
4726     case DW_OP_breg24:
4727     case DW_OP_breg25:
4728     case DW_OP_breg26:
4729     case DW_OP_breg27:
4730     case DW_OP_breg28:
4731     case DW_OP_breg29:
4732     case DW_OP_breg30:
4733     case DW_OP_breg31:
4734       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4735       break;
4736     case DW_OP_regx:
4737       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4738       break;
4739     case DW_OP_fbreg:
4740       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4741       break;
4742     case DW_OP_bregx:
4743       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4744       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4745       break;
4746     case DW_OP_piece:
4747       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4748       break;
4749     case DW_OP_deref_size:
4750     case DW_OP_xderef_size:
4751       size += 1;
4752       break;
4753     case DW_OP_call2:
4754       size += 2;
4755       break;
4756     case DW_OP_call4:
4757       size += 4;
4758       break;
4759     case DW_OP_call_ref:
4760       size += DWARF2_ADDR_SIZE;
4761       break;
4762     case DW_OP_implicit_value:
4763       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4764               + loc->dw_loc_oprnd1.v.val_unsigned;
4765       break;
4766     default:
4767       break;
4768     }
4769
4770   return size;
4771 }
4772
4773 /* Return the size of a series of location descriptors.  */
4774
4775 static unsigned long
4776 size_of_locs (dw_loc_descr_ref loc)
4777 {
4778   dw_loc_descr_ref l;
4779   unsigned long size;
4780
4781   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4782      field, to avoid writing to a PCH file.  */
4783   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4784     {
4785       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4786         break;
4787       size += size_of_loc_descr (l);
4788     }
4789   if (! l)
4790     return size;
4791
4792   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4793     {
4794       l->dw_loc_addr = size;
4795       size += size_of_loc_descr (l);
4796     }
4797
4798   return size;
4799 }
4800
4801 #ifdef DWARF2_DEBUGGING_INFO
4802 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4803 #endif
4804
4805 /* Output location description stack opcode's operands (if any).  */
4806
4807 static void
4808 output_loc_operands (dw_loc_descr_ref loc)
4809 {
4810   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4811   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4812
4813   switch (loc->dw_loc_opc)
4814     {
4815 #ifdef DWARF2_DEBUGGING_INFO
4816     case DW_OP_const2u:
4817     case DW_OP_const2s:
4818       dw2_asm_output_data (2, val1->v.val_int, NULL);
4819       break;
4820     case DW_OP_const4u:
4821     case DW_OP_const4s:
4822       dw2_asm_output_data (4, val1->v.val_int, NULL);
4823       break;
4824     case DW_OP_const8u:
4825     case DW_OP_const8s:
4826       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4827       dw2_asm_output_data (8, val1->v.val_int, NULL);
4828       break;
4829     case DW_OP_skip:
4830     case DW_OP_bra:
4831       {
4832         int offset;
4833
4834         gcc_assert (val1->val_class == dw_val_class_loc);
4835         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4836
4837         dw2_asm_output_data (2, offset, NULL);
4838       }
4839       break;
4840     case DW_OP_implicit_value:
4841       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4842       switch (val2->val_class)
4843         {
4844         case dw_val_class_const:
4845           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4846           break;
4847         case dw_val_class_vec:
4848           {
4849             unsigned int elt_size = val2->v.val_vec.elt_size;
4850             unsigned int len = val2->v.val_vec.length;
4851             unsigned int i;
4852             unsigned char *p;
4853
4854             if (elt_size > sizeof (HOST_WIDE_INT))
4855               {
4856                 elt_size /= 2;
4857                 len *= 2;
4858               }
4859             for (i = 0, p = val2->v.val_vec.array;
4860                  i < len;
4861                  i++, p += elt_size)
4862               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4863                                    "fp or vector constant word %u", i);
4864           }
4865           break;
4866         case dw_val_class_long_long:
4867           {
4868             unsigned HOST_WIDE_INT first, second;
4869
4870             if (WORDS_BIG_ENDIAN)
4871               {
4872                 first = CONST_DOUBLE_HIGH (val2->v.val_long_long);
4873                 second = CONST_DOUBLE_LOW (val2->v.val_long_long);
4874               }
4875             else
4876               {
4877                 first = CONST_DOUBLE_LOW (val2->v.val_long_long);
4878                 second = CONST_DOUBLE_HIGH (val2->v.val_long_long);
4879               }
4880             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4881                                  first, "long long constant");
4882             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4883                                  second, NULL);
4884           }
4885           break;
4886         case dw_val_class_addr:
4887           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4888           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4889           break;
4890         default:
4891           gcc_unreachable ();
4892         }
4893       break;
4894 #else
4895     case DW_OP_const2u:
4896     case DW_OP_const2s:
4897     case DW_OP_const4u:
4898     case DW_OP_const4s:
4899     case DW_OP_const8u:
4900     case DW_OP_const8s:
4901     case DW_OP_skip:
4902     case DW_OP_bra:
4903     case DW_OP_implicit_value:
4904       /* We currently don't make any attempt to make sure these are
4905          aligned properly like we do for the main unwind info, so
4906          don't support emitting things larger than a byte if we're
4907          only doing unwinding.  */
4908       gcc_unreachable ();
4909 #endif
4910     case DW_OP_const1u:
4911     case DW_OP_const1s:
4912       dw2_asm_output_data (1, val1->v.val_int, NULL);
4913       break;
4914     case DW_OP_constu:
4915       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4916       break;
4917     case DW_OP_consts:
4918       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4919       break;
4920     case DW_OP_pick:
4921       dw2_asm_output_data (1, val1->v.val_int, NULL);
4922       break;
4923     case DW_OP_plus_uconst:
4924       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4925       break;
4926     case DW_OP_breg0:
4927     case DW_OP_breg1:
4928     case DW_OP_breg2:
4929     case DW_OP_breg3:
4930     case DW_OP_breg4:
4931     case DW_OP_breg5:
4932     case DW_OP_breg6:
4933     case DW_OP_breg7:
4934     case DW_OP_breg8:
4935     case DW_OP_breg9:
4936     case DW_OP_breg10:
4937     case DW_OP_breg11:
4938     case DW_OP_breg12:
4939     case DW_OP_breg13:
4940     case DW_OP_breg14:
4941     case DW_OP_breg15:
4942     case DW_OP_breg16:
4943     case DW_OP_breg17:
4944     case DW_OP_breg18:
4945     case DW_OP_breg19:
4946     case DW_OP_breg20:
4947     case DW_OP_breg21:
4948     case DW_OP_breg22:
4949     case DW_OP_breg23:
4950     case DW_OP_breg24:
4951     case DW_OP_breg25:
4952     case DW_OP_breg26:
4953     case DW_OP_breg27:
4954     case DW_OP_breg28:
4955     case DW_OP_breg29:
4956     case DW_OP_breg30:
4957     case DW_OP_breg31:
4958       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4959       break;
4960     case DW_OP_regx:
4961       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4962       break;
4963     case DW_OP_fbreg:
4964       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4965       break;
4966     case DW_OP_bregx:
4967       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4968       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4969       break;
4970     case DW_OP_piece:
4971       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4972       break;
4973     case DW_OP_deref_size:
4974     case DW_OP_xderef_size:
4975       dw2_asm_output_data (1, val1->v.val_int, NULL);
4976       break;
4977
4978     case DW_OP_addr:
4979       if (loc->dtprel)
4980         {
4981           if (targetm.asm_out.output_dwarf_dtprel)
4982             {
4983               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4984                                                    DWARF2_ADDR_SIZE,
4985                                                    val1->v.val_addr);
4986               fputc ('\n', asm_out_file);
4987             }
4988           else
4989             gcc_unreachable ();
4990         }
4991       else
4992         {
4993 #ifdef DWARF2_DEBUGGING_INFO
4994           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4995 #else
4996           gcc_unreachable ();
4997 #endif
4998         }
4999       break;
5000
5001     default:
5002       /* Other codes have no operands.  */
5003       break;
5004     }
5005 }
5006
5007 /* Output a sequence of location operations.  */
5008
5009 static void
5010 output_loc_sequence (dw_loc_descr_ref loc)
5011 {
5012   for (; loc != NULL; loc = loc->dw_loc_next)
5013     {
5014       /* Output the opcode.  */
5015       dw2_asm_output_data (1, loc->dw_loc_opc,
5016                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5017
5018       /* Output the operand(s) (if any).  */
5019       output_loc_operands (loc);
5020     }
5021 }
5022
5023 /* Output location description stack opcode's operands (if any).
5024    The output is single bytes on a line, suitable for .cfi_escape.  */
5025
5026 static void
5027 output_loc_operands_raw (dw_loc_descr_ref loc)
5028 {
5029   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5030   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5031
5032   switch (loc->dw_loc_opc)
5033     {
5034     case DW_OP_addr:
5035     case DW_OP_implicit_value:
5036       /* We cannot output addresses in .cfi_escape, only bytes.  */
5037       gcc_unreachable ();
5038
5039     case DW_OP_const1u:
5040     case DW_OP_const1s:
5041     case DW_OP_pick:
5042     case DW_OP_deref_size:
5043     case DW_OP_xderef_size:
5044       fputc (',', asm_out_file);
5045       dw2_asm_output_data_raw (1, val1->v.val_int);
5046       break;
5047
5048     case DW_OP_const2u:
5049     case DW_OP_const2s:
5050       fputc (',', asm_out_file);
5051       dw2_asm_output_data_raw (2, val1->v.val_int);
5052       break;
5053
5054     case DW_OP_const4u:
5055     case DW_OP_const4s:
5056       fputc (',', asm_out_file);
5057       dw2_asm_output_data_raw (4, val1->v.val_int);
5058       break;
5059
5060     case DW_OP_const8u:
5061     case DW_OP_const8s:
5062       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5063       fputc (',', asm_out_file);
5064       dw2_asm_output_data_raw (8, val1->v.val_int);
5065       break;
5066
5067     case DW_OP_skip:
5068     case DW_OP_bra:
5069       {
5070         int offset;
5071
5072         gcc_assert (val1->val_class == dw_val_class_loc);
5073         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5074
5075         fputc (',', asm_out_file);
5076         dw2_asm_output_data_raw (2, offset);
5077       }
5078       break;
5079
5080     case DW_OP_constu:
5081     case DW_OP_plus_uconst:
5082     case DW_OP_regx:
5083     case DW_OP_piece:
5084       fputc (',', asm_out_file);
5085       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5086       break;
5087
5088     case DW_OP_consts:
5089     case DW_OP_breg0:
5090     case DW_OP_breg1:
5091     case DW_OP_breg2:
5092     case DW_OP_breg3:
5093     case DW_OP_breg4:
5094     case DW_OP_breg5:
5095     case DW_OP_breg6:
5096     case DW_OP_breg7:
5097     case DW_OP_breg8:
5098     case DW_OP_breg9:
5099     case DW_OP_breg10:
5100     case DW_OP_breg11:
5101     case DW_OP_breg12:
5102     case DW_OP_breg13:
5103     case DW_OP_breg14:
5104     case DW_OP_breg15:
5105     case DW_OP_breg16:
5106     case DW_OP_breg17:
5107     case DW_OP_breg18:
5108     case DW_OP_breg19:
5109     case DW_OP_breg20:
5110     case DW_OP_breg21:
5111     case DW_OP_breg22:
5112     case DW_OP_breg23:
5113     case DW_OP_breg24:
5114     case DW_OP_breg25:
5115     case DW_OP_breg26:
5116     case DW_OP_breg27:
5117     case DW_OP_breg28:
5118     case DW_OP_breg29:
5119     case DW_OP_breg30:
5120     case DW_OP_breg31:
5121     case DW_OP_fbreg:
5122       fputc (',', asm_out_file);
5123       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5124       break;
5125
5126     case DW_OP_bregx:
5127       fputc (',', asm_out_file);
5128       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5129       fputc (',', asm_out_file);
5130       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5131       break;
5132
5133     default:
5134       /* Other codes have no operands.  */
5135       break;
5136     }
5137 }
5138
5139 static void
5140 output_loc_sequence_raw (dw_loc_descr_ref loc)
5141 {
5142   while (1)
5143     {
5144       /* Output the opcode.  */
5145       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5146       output_loc_operands_raw (loc);
5147
5148       if (!loc->dw_loc_next)
5149         break;
5150       loc = loc->dw_loc_next;
5151
5152       fputc (',', asm_out_file);
5153     }
5154 }
5155
5156 /* This routine will generate the correct assembly data for a location
5157    description based on a cfi entry with a complex address.  */
5158
5159 static void
5160 output_cfa_loc (dw_cfi_ref cfi)
5161 {
5162   dw_loc_descr_ref loc;
5163   unsigned long size;
5164
5165   if (cfi->dw_cfi_opc == DW_CFA_expression)
5166     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5167
5168   /* Output the size of the block.  */
5169   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5170   size = size_of_locs (loc);
5171   dw2_asm_output_data_uleb128 (size, NULL);
5172
5173   /* Now output the operations themselves.  */
5174   output_loc_sequence (loc);
5175 }
5176
5177 /* Similar, but used for .cfi_escape.  */
5178
5179 static void
5180 output_cfa_loc_raw (dw_cfi_ref cfi)
5181 {
5182   dw_loc_descr_ref loc;
5183   unsigned long size;
5184
5185   if (cfi->dw_cfi_opc == DW_CFA_expression)
5186     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5187
5188   /* Output the size of the block.  */
5189   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5190   size = size_of_locs (loc);
5191   dw2_asm_output_data_uleb128_raw (size);
5192   fputc (',', asm_out_file);
5193
5194   /* Now output the operations themselves.  */
5195   output_loc_sequence_raw (loc);
5196 }
5197
5198 /* This function builds a dwarf location descriptor sequence from a
5199    dw_cfa_location, adding the given OFFSET to the result of the
5200    expression.  */
5201
5202 static struct dw_loc_descr_struct *
5203 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5204 {
5205   struct dw_loc_descr_struct *head, *tmp;
5206
5207   offset += cfa->offset;
5208
5209   if (cfa->indirect)
5210     {
5211       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5212       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5213       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5214       add_loc_descr (&head, tmp);
5215       if (offset != 0)
5216         {
5217           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5218           add_loc_descr (&head, tmp);
5219         }
5220     }
5221   else
5222     head = new_reg_loc_descr (cfa->reg, offset);
5223
5224   return head;
5225 }
5226
5227 /* This function builds a dwarf location descriptor sequence for
5228    the address at OFFSET from the CFA when stack is aligned to
5229    ALIGNMENT byte.  */
5230
5231 static struct dw_loc_descr_struct *
5232 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5233 {
5234   struct dw_loc_descr_struct *head;
5235   unsigned int dwarf_fp
5236     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5237
5238  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5239   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5240     {
5241       head = new_reg_loc_descr (dwarf_fp, 0);
5242       add_loc_descr (&head, int_loc_descriptor (alignment));
5243       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5244       loc_descr_plus_const (&head, offset);
5245     }
5246   else
5247     head = new_reg_loc_descr (dwarf_fp, offset);
5248   return head;
5249 }
5250
5251 /* This function fills in aa dw_cfa_location structure from a dwarf location
5252    descriptor sequence.  */
5253
5254 static void
5255 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5256 {
5257   struct dw_loc_descr_struct *ptr;
5258   cfa->offset = 0;
5259   cfa->base_offset = 0;
5260   cfa->indirect = 0;
5261   cfa->reg = -1;
5262
5263   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5264     {
5265       enum dwarf_location_atom op = ptr->dw_loc_opc;
5266
5267       switch (op)
5268         {
5269         case DW_OP_reg0:
5270         case DW_OP_reg1:
5271         case DW_OP_reg2:
5272         case DW_OP_reg3:
5273         case DW_OP_reg4:
5274         case DW_OP_reg5:
5275         case DW_OP_reg6:
5276         case DW_OP_reg7:
5277         case DW_OP_reg8:
5278         case DW_OP_reg9:
5279         case DW_OP_reg10:
5280         case DW_OP_reg11:
5281         case DW_OP_reg12:
5282         case DW_OP_reg13:
5283         case DW_OP_reg14:
5284         case DW_OP_reg15:
5285         case DW_OP_reg16:
5286         case DW_OP_reg17:
5287         case DW_OP_reg18:
5288         case DW_OP_reg19:
5289         case DW_OP_reg20:
5290         case DW_OP_reg21:
5291         case DW_OP_reg22:
5292         case DW_OP_reg23:
5293         case DW_OP_reg24:
5294         case DW_OP_reg25:
5295         case DW_OP_reg26:
5296         case DW_OP_reg27:
5297         case DW_OP_reg28:
5298         case DW_OP_reg29:
5299         case DW_OP_reg30:
5300         case DW_OP_reg31:
5301           cfa->reg = op - DW_OP_reg0;
5302           break;
5303         case DW_OP_regx:
5304           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5305           break;
5306         case DW_OP_breg0:
5307         case DW_OP_breg1:
5308         case DW_OP_breg2:
5309         case DW_OP_breg3:
5310         case DW_OP_breg4:
5311         case DW_OP_breg5:
5312         case DW_OP_breg6:
5313         case DW_OP_breg7:
5314         case DW_OP_breg8:
5315         case DW_OP_breg9:
5316         case DW_OP_breg10:
5317         case DW_OP_breg11:
5318         case DW_OP_breg12:
5319         case DW_OP_breg13:
5320         case DW_OP_breg14:
5321         case DW_OP_breg15:
5322         case DW_OP_breg16:
5323         case DW_OP_breg17:
5324         case DW_OP_breg18:
5325         case DW_OP_breg19:
5326         case DW_OP_breg20:
5327         case DW_OP_breg21:
5328         case DW_OP_breg22:
5329         case DW_OP_breg23:
5330         case DW_OP_breg24:
5331         case DW_OP_breg25:
5332         case DW_OP_breg26:
5333         case DW_OP_breg27:
5334         case DW_OP_breg28:
5335         case DW_OP_breg29:
5336         case DW_OP_breg30:
5337         case DW_OP_breg31:
5338           cfa->reg = op - DW_OP_breg0;
5339           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5340           break;
5341         case DW_OP_bregx:
5342           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5343           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5344           break;
5345         case DW_OP_deref:
5346           cfa->indirect = 1;
5347           break;
5348         case DW_OP_plus_uconst:
5349           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5350           break;
5351         default:
5352           internal_error ("DW_LOC_OP %s not implemented",
5353                           dwarf_stack_op_name (ptr->dw_loc_opc));
5354         }
5355     }
5356 }
5357 #endif /* .debug_frame support */
5358 \f
5359 /* And now, the support for symbolic debugging information.  */
5360 #ifdef DWARF2_DEBUGGING_INFO
5361
5362 /* .debug_str support.  */
5363 static int output_indirect_string (void **, void *);
5364
5365 static void dwarf2out_init (const char *);
5366 static void dwarf2out_finish (const char *);
5367 static void dwarf2out_define (unsigned int, const char *);
5368 static void dwarf2out_undef (unsigned int, const char *);
5369 static void dwarf2out_start_source_file (unsigned, const char *);
5370 static void dwarf2out_end_source_file (unsigned);
5371 static void dwarf2out_begin_block (unsigned, unsigned);
5372 static void dwarf2out_end_block (unsigned, unsigned);
5373 static bool dwarf2out_ignore_block (const_tree);
5374 static void dwarf2out_global_decl (tree);
5375 static void dwarf2out_type_decl (tree, int);
5376 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5377 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5378                                                  dw_die_ref);
5379 static void dwarf2out_abstract_function (tree);
5380 static void dwarf2out_var_location (rtx);
5381 static void dwarf2out_begin_function (tree);
5382 static void dwarf2out_set_name (tree, tree);
5383
5384 /* The debug hooks structure.  */
5385
5386 const struct gcc_debug_hooks dwarf2_debug_hooks =
5387 {
5388   dwarf2out_init,
5389   dwarf2out_finish,
5390   dwarf2out_define,
5391   dwarf2out_undef,
5392   dwarf2out_start_source_file,
5393   dwarf2out_end_source_file,
5394   dwarf2out_begin_block,
5395   dwarf2out_end_block,
5396   dwarf2out_ignore_block,
5397   dwarf2out_source_line,
5398   dwarf2out_begin_prologue,
5399   debug_nothing_int_charstar,   /* end_prologue */
5400   dwarf2out_end_epilogue,
5401   dwarf2out_begin_function,
5402   debug_nothing_int,            /* end_function */
5403   dwarf2out_decl,               /* function_decl */
5404   dwarf2out_global_decl,
5405   dwarf2out_type_decl,          /* type_decl */
5406   dwarf2out_imported_module_or_decl,
5407   debug_nothing_tree,           /* deferred_inline_function */
5408   /* The DWARF 2 backend tries to reduce debugging bloat by not
5409      emitting the abstract description of inline functions until
5410      something tries to reference them.  */
5411   dwarf2out_abstract_function,  /* outlining_inline_function */
5412   debug_nothing_rtx,            /* label */
5413   debug_nothing_int,            /* handle_pch */
5414   dwarf2out_var_location,
5415   dwarf2out_switch_text_section,
5416   dwarf2out_set_name,
5417   1                             /* start_end_main_source_file */
5418 };
5419 #endif
5420 \f
5421 /* NOTE: In the comments in this file, many references are made to
5422    "Debugging Information Entries".  This term is abbreviated as `DIE'
5423    throughout the remainder of this file.  */
5424
5425 /* An internal representation of the DWARF output is built, and then
5426    walked to generate the DWARF debugging info.  The walk of the internal
5427    representation is done after the entire program has been compiled.
5428    The types below are used to describe the internal representation.  */
5429
5430 /* Various DIE's use offsets relative to the beginning of the
5431    .debug_info section to refer to each other.  */
5432
5433 typedef long int dw_offset;
5434
5435 /* Define typedefs here to avoid circular dependencies.  */
5436
5437 typedef struct dw_attr_struct *dw_attr_ref;
5438 typedef struct dw_line_info_struct *dw_line_info_ref;
5439 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5440 typedef struct pubname_struct *pubname_ref;
5441 typedef struct dw_ranges_struct *dw_ranges_ref;
5442 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5443
5444 /* Each entry in the line_info_table maintains the file and
5445    line number associated with the label generated for that
5446    entry.  The label gives the PC value associated with
5447    the line number entry.  */
5448
5449 typedef struct GTY(()) dw_line_info_struct {
5450   unsigned long dw_file_num;
5451   unsigned long dw_line_num;
5452 }
5453 dw_line_info_entry;
5454
5455 /* Line information for functions in separate sections; each one gets its
5456    own sequence.  */
5457 typedef struct GTY(()) dw_separate_line_info_struct {
5458   unsigned long dw_file_num;
5459   unsigned long dw_line_num;
5460   unsigned long function;
5461 }
5462 dw_separate_line_info_entry;
5463
5464 /* Each DIE attribute has a field specifying the attribute kind,
5465    a link to the next attribute in the chain, and an attribute value.
5466    Attributes are typically linked below the DIE they modify.  */
5467
5468 typedef struct GTY(()) dw_attr_struct {
5469   enum dwarf_attribute dw_attr;
5470   dw_val_node dw_attr_val;
5471 }
5472 dw_attr_node;
5473
5474 DEF_VEC_O(dw_attr_node);
5475 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5476
5477 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5478    The children of each node form a circular list linked by
5479    die_sib.  die_child points to the node *before* the "first" child node.  */
5480
5481 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5482   enum dwarf_tag die_tag;
5483   char *die_symbol;
5484   VEC(dw_attr_node,gc) * die_attr;
5485   dw_die_ref die_parent;
5486   dw_die_ref die_child;
5487   dw_die_ref die_sib;
5488   dw_die_ref die_definition; /* ref from a specification to its definition */
5489   dw_offset die_offset;
5490   unsigned long die_abbrev;
5491   int die_mark;
5492   /* Die is used and must not be pruned as unused.  */
5493   int die_perennial_p;
5494   unsigned int decl_id;
5495 }
5496 die_node;
5497
5498 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5499 #define FOR_EACH_CHILD(die, c, expr) do {       \
5500   c = die->die_child;                           \
5501   if (c) do {                                   \
5502     c = c->die_sib;                             \
5503     expr;                                       \
5504   } while (c != die->die_child);                \
5505 } while (0)
5506
5507 /* The pubname structure */
5508
5509 typedef struct GTY(()) pubname_struct {
5510   dw_die_ref die;
5511   const char *name;
5512 }
5513 pubname_entry;
5514
5515 DEF_VEC_O(pubname_entry);
5516 DEF_VEC_ALLOC_O(pubname_entry, gc);
5517
5518 struct GTY(()) dw_ranges_struct {
5519   /* If this is positive, it's a block number, otherwise it's a
5520      bitwise-negated index into dw_ranges_by_label.  */
5521   int num;
5522 };
5523
5524 struct GTY(()) dw_ranges_by_label_struct {
5525   const char *begin;
5526   const char *end;
5527 };
5528
5529 /* The limbo die list structure.  */
5530 typedef struct GTY(()) limbo_die_struct {
5531   dw_die_ref die;
5532   tree created_for;
5533   struct limbo_die_struct *next;
5534 }
5535 limbo_die_node;
5536
5537 /* How to start an assembler comment.  */
5538 #ifndef ASM_COMMENT_START
5539 #define ASM_COMMENT_START ";#"
5540 #endif
5541
5542 /* Define a macro which returns nonzero for a TYPE_DECL which was
5543    implicitly generated for a tagged type.
5544
5545    Note that unlike the gcc front end (which generates a NULL named
5546    TYPE_DECL node for each complete tagged type, each array type, and
5547    each function type node created) the g++ front end generates a
5548    _named_ TYPE_DECL node for each tagged type node created.
5549    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5550    generate a DW_TAG_typedef DIE for them.  */
5551
5552 #define TYPE_DECL_IS_STUB(decl)                         \
5553   (DECL_NAME (decl) == NULL_TREE                        \
5554    || (DECL_ARTIFICIAL (decl)                           \
5555        && is_tagged_type (TREE_TYPE (decl))             \
5556        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5557            /* This is necessary for stub decls that     \
5558               appear in nested inline functions.  */    \
5559            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5560                && (decl_ultimate_origin (decl)          \
5561                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5562
5563 /* Information concerning the compilation unit's programming
5564    language, and compiler version.  */
5565
5566 /* Fixed size portion of the DWARF compilation unit header.  */
5567 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5568   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5569
5570 /* Fixed size portion of public names info.  */
5571 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5572
5573 /* Fixed size portion of the address range info.  */
5574 #define DWARF_ARANGES_HEADER_SIZE                                       \
5575   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5576                 DWARF2_ADDR_SIZE * 2)                                   \
5577    - DWARF_INITIAL_LENGTH_SIZE)
5578
5579 /* Size of padding portion in the address range info.  It must be
5580    aligned to twice the pointer size.  */
5581 #define DWARF_ARANGES_PAD_SIZE \
5582   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5583                 DWARF2_ADDR_SIZE * 2)                              \
5584    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5585
5586 /* Use assembler line directives if available.  */
5587 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5588 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5589 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5590 #else
5591 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5592 #endif
5593 #endif
5594
5595 /* Minimum line offset in a special line info. opcode.
5596    This value was chosen to give a reasonable range of values.  */
5597 #define DWARF_LINE_BASE  -10
5598
5599 /* First special line opcode - leave room for the standard opcodes.  */
5600 #define DWARF_LINE_OPCODE_BASE  10
5601
5602 /* Range of line offsets in a special line info. opcode.  */
5603 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5604
5605 /* Flag that indicates the initial value of the is_stmt_start flag.
5606    In the present implementation, we do not mark any lines as
5607    the beginning of a source statement, because that information
5608    is not made available by the GCC front-end.  */
5609 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5610
5611 #ifdef DWARF2_DEBUGGING_INFO
5612 /* This location is used by calc_die_sizes() to keep track
5613    the offset of each DIE within the .debug_info section.  */
5614 static unsigned long next_die_offset;
5615 #endif
5616
5617 /* Record the root of the DIE's built for the current compilation unit.  */
5618 static GTY(()) dw_die_ref comp_unit_die;
5619
5620 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5621 static GTY(()) limbo_die_node *limbo_die_list;
5622
5623 /* A list of DIEs for which we may have to generate
5624    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5625    set.  */
5626 static GTY(()) limbo_die_node *deferred_asm_name;
5627
5628 /* Filenames referenced by this compilation unit.  */
5629 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5630
5631 /* A hash table of references to DIE's that describe declarations.
5632    The key is a DECL_UID() which is a unique number identifying each decl.  */
5633 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5634
5635 /* A hash table of references to DIE's that describe COMMON blocks.
5636    The key is DECL_UID() ^ die_parent.  */
5637 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5638
5639 typedef struct GTY(()) die_arg_entry_struct {
5640     dw_die_ref die;
5641     tree arg;
5642 } die_arg_entry;
5643
5644 DEF_VEC_O(die_arg_entry);
5645 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5646
5647 /* Node of the variable location list.  */
5648 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5649   rtx GTY (()) var_loc_note;
5650   const char * GTY (()) label;
5651   const char * GTY (()) section_label;
5652   struct var_loc_node * GTY (()) next;
5653 };
5654
5655 /* Variable location list.  */
5656 struct GTY (()) var_loc_list_def {
5657   struct var_loc_node * GTY (()) first;
5658
5659   /* Do not mark the last element of the chained list because
5660      it is marked through the chain.  */
5661   struct var_loc_node * GTY ((skip ("%h"))) last;
5662
5663   /* DECL_UID of the variable decl.  */
5664   unsigned int decl_id;
5665 };
5666 typedef struct var_loc_list_def var_loc_list;
5667
5668
5669 /* Table of decl location linked lists.  */
5670 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5671
5672 /* A pointer to the base of a list of references to DIE's that
5673    are uniquely identified by their tag, presence/absence of
5674    children DIE's, and list of attribute/value pairs.  */
5675 static GTY((length ("abbrev_die_table_allocated")))
5676   dw_die_ref *abbrev_die_table;
5677
5678 /* Number of elements currently allocated for abbrev_die_table.  */
5679 static GTY(()) unsigned abbrev_die_table_allocated;
5680
5681 /* Number of elements in type_die_table currently in use.  */
5682 static GTY(()) unsigned abbrev_die_table_in_use;
5683
5684 /* Size (in elements) of increments by which we may expand the
5685    abbrev_die_table.  */
5686 #define ABBREV_DIE_TABLE_INCREMENT 256
5687
5688 /* A pointer to the base of a table that contains line information
5689    for each source code line in .text in the compilation unit.  */
5690 static GTY((length ("line_info_table_allocated")))
5691      dw_line_info_ref line_info_table;
5692
5693 /* Number of elements currently allocated for line_info_table.  */
5694 static GTY(()) unsigned line_info_table_allocated;
5695
5696 /* Number of elements in line_info_table currently in use.  */
5697 static GTY(()) unsigned line_info_table_in_use;
5698
5699 /* A pointer to the base of a table that contains line information
5700    for each source code line outside of .text in the compilation unit.  */
5701 static GTY ((length ("separate_line_info_table_allocated")))
5702      dw_separate_line_info_ref separate_line_info_table;
5703
5704 /* Number of elements currently allocated for separate_line_info_table.  */
5705 static GTY(()) unsigned separate_line_info_table_allocated;
5706
5707 /* Number of elements in separate_line_info_table currently in use.  */
5708 static GTY(()) unsigned separate_line_info_table_in_use;
5709
5710 /* Size (in elements) of increments by which we may expand the
5711    line_info_table.  */
5712 #define LINE_INFO_TABLE_INCREMENT 1024
5713
5714 /* A pointer to the base of a table that contains a list of publicly
5715    accessible names.  */
5716 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5717
5718 /* A pointer to the base of a table that contains a list of publicly
5719    accessible types.  */
5720 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5721
5722 /* Array of dies for which we should generate .debug_arange info.  */
5723 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5724
5725 /* Number of elements currently allocated for arange_table.  */
5726 static GTY(()) unsigned arange_table_allocated;
5727
5728 /* Number of elements in arange_table currently in use.  */
5729 static GTY(()) unsigned arange_table_in_use;
5730
5731 /* Size (in elements) of increments by which we may expand the
5732    arange_table.  */
5733 #define ARANGE_TABLE_INCREMENT 64
5734
5735 /* Array of dies for which we should generate .debug_ranges info.  */
5736 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5737
5738 /* Number of elements currently allocated for ranges_table.  */
5739 static GTY(()) unsigned ranges_table_allocated;
5740
5741 /* Number of elements in ranges_table currently in use.  */
5742 static GTY(()) unsigned ranges_table_in_use;
5743
5744 /* Array of pairs of labels referenced in ranges_table.  */
5745 static GTY ((length ("ranges_by_label_allocated")))
5746      dw_ranges_by_label_ref ranges_by_label;
5747
5748 /* Number of elements currently allocated for ranges_by_label.  */
5749 static GTY(()) unsigned ranges_by_label_allocated;
5750
5751 /* Number of elements in ranges_by_label currently in use.  */
5752 static GTY(()) unsigned ranges_by_label_in_use;
5753
5754 /* Size (in elements) of increments by which we may expand the
5755    ranges_table.  */
5756 #define RANGES_TABLE_INCREMENT 64
5757
5758 /* Whether we have location lists that need outputting */
5759 static GTY(()) bool have_location_lists;
5760
5761 /* Unique label counter.  */
5762 static GTY(()) unsigned int loclabel_num;
5763
5764 #ifdef DWARF2_DEBUGGING_INFO
5765 /* Record whether the function being analyzed contains inlined functions.  */
5766 static int current_function_has_inlines;
5767 #endif
5768 #if 0 && defined (MIPS_DEBUGGING_INFO)
5769 static int comp_unit_has_inlines;
5770 #endif
5771
5772 /* The last file entry emitted by maybe_emit_file().  */
5773 static GTY(()) struct dwarf_file_data * last_emitted_file;
5774
5775 /* Number of internal labels generated by gen_internal_sym().  */
5776 static GTY(()) int label_num;
5777
5778 /* Cached result of previous call to lookup_filename.  */
5779 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5780
5781 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5782
5783 #ifdef DWARF2_DEBUGGING_INFO
5784
5785 /* Offset from the "steady-state frame pointer" to the frame base,
5786    within the current function.  */
5787 static HOST_WIDE_INT frame_pointer_fb_offset;
5788
5789 /* Forward declarations for functions defined in this file.  */
5790
5791 static int is_pseudo_reg (const_rtx);
5792 static tree type_main_variant (tree);
5793 static int is_tagged_type (const_tree);
5794 static const char *dwarf_tag_name (unsigned);
5795 static const char *dwarf_attr_name (unsigned);
5796 static const char *dwarf_form_name (unsigned);
5797 static tree decl_ultimate_origin (const_tree);
5798 static tree decl_class_context (tree);
5799 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5800 static inline enum dw_val_class AT_class (dw_attr_ref);
5801 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5802 static inline unsigned AT_flag (dw_attr_ref);
5803 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5804 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5805 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5806 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5807 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, rtx);
5808 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5809                                unsigned int, unsigned char *);
5810 static hashval_t debug_str_do_hash (const void *);
5811 static int debug_str_eq (const void *, const void *);
5812 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5813 static inline const char *AT_string (dw_attr_ref);
5814 static enum dwarf_form AT_string_form (dw_attr_ref);
5815 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5816 static void add_AT_specification (dw_die_ref, dw_die_ref);
5817 static inline dw_die_ref AT_ref (dw_attr_ref);
5818 static inline int AT_ref_external (dw_attr_ref);
5819 static inline void set_AT_ref_external (dw_attr_ref, int);
5820 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5821 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5822 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5823 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5824                              dw_loc_list_ref);
5825 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5826 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5827 static inline rtx AT_addr (dw_attr_ref);
5828 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5829 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5830 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5831 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5832                            unsigned HOST_WIDE_INT);
5833 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5834                                unsigned long);
5835 static inline const char *AT_lbl (dw_attr_ref);
5836 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5837 static const char *get_AT_low_pc (dw_die_ref);
5838 static const char *get_AT_hi_pc (dw_die_ref);
5839 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5840 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5841 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5842 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5843 static bool is_c_family (void);
5844 static bool is_cxx (void);
5845 static bool is_java (void);
5846 static bool is_fortran (void);
5847 static bool is_ada (void);
5848 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5849 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5850 static void add_child_die (dw_die_ref, dw_die_ref);
5851 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5852 static dw_die_ref lookup_type_die (tree);
5853 static void equate_type_number_to_die (tree, dw_die_ref);
5854 static hashval_t decl_die_table_hash (const void *);
5855 static int decl_die_table_eq (const void *, const void *);
5856 static dw_die_ref lookup_decl_die (tree);
5857 static hashval_t common_block_die_table_hash (const void *);
5858 static int common_block_die_table_eq (const void *, const void *);
5859 static hashval_t decl_loc_table_hash (const void *);
5860 static int decl_loc_table_eq (const void *, const void *);
5861 static var_loc_list *lookup_decl_loc (const_tree);
5862 static void equate_decl_number_to_die (tree, dw_die_ref);
5863 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5864 static void print_spaces (FILE *);
5865 static void print_die (dw_die_ref, FILE *);
5866 static void print_dwarf_line_table (FILE *);
5867 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5868 static dw_die_ref pop_compile_unit (dw_die_ref);
5869 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5870 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5871 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5872 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5873 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5874 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5875 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5876 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5877 static void compute_section_prefix (dw_die_ref);
5878 static int is_type_die (dw_die_ref);
5879 static int is_comdat_die (dw_die_ref);
5880 static int is_symbol_die (dw_die_ref);
5881 static void assign_symbol_names (dw_die_ref);
5882 static void break_out_includes (dw_die_ref);
5883 static hashval_t htab_cu_hash (const void *);
5884 static int htab_cu_eq (const void *, const void *);
5885 static void htab_cu_del (void *);
5886 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5887 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5888 static void add_sibling_attributes (dw_die_ref);
5889 static void build_abbrev_table (dw_die_ref);
5890 static void output_location_lists (dw_die_ref);
5891 static int constant_size (unsigned HOST_WIDE_INT);
5892 static unsigned long size_of_die (dw_die_ref);
5893 static void calc_die_sizes (dw_die_ref);
5894 static void mark_dies (dw_die_ref);
5895 static void unmark_dies (dw_die_ref);
5896 static void unmark_all_dies (dw_die_ref);
5897 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5898 static unsigned long size_of_aranges (void);
5899 static enum dwarf_form value_format (dw_attr_ref);
5900 static void output_value_format (dw_attr_ref);
5901 static void output_abbrev_section (void);
5902 static void output_die_symbol (dw_die_ref);
5903 static void output_die (dw_die_ref);
5904 static void output_compilation_unit_header (void);
5905 static void output_comp_unit (dw_die_ref, int);
5906 static const char *dwarf2_name (tree, int);
5907 static void add_pubname (tree, dw_die_ref);
5908 static void add_pubname_string (const char *, dw_die_ref);
5909 static void add_pubtype (tree, dw_die_ref);
5910 static void output_pubnames (VEC (pubname_entry,gc) *);
5911 static void add_arange (tree, dw_die_ref);
5912 static void output_aranges (void);
5913 static unsigned int add_ranges_num (int);
5914 static unsigned int add_ranges (const_tree);
5915 static unsigned int add_ranges_by_labels (const char *, const char *);
5916 static void output_ranges (void);
5917 static void output_line_info (void);
5918 static void output_file_names (void);
5919 static dw_die_ref base_type_die (tree);
5920 static int is_base_type (tree);
5921 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5922 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5923 static dw_die_ref generic_parameter_die (tree, tree, dw_die_ref, int);
5924 static int type_is_enum (const_tree);
5925 static unsigned int dbx_reg_number (const_rtx);
5926 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5927 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5928 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5929                                                 enum var_init_status);
5930 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5931                                                      enum var_init_status);
5932 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5933                                          enum var_init_status);
5934 static int is_based_loc (const_rtx);
5935 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5936                                             enum var_init_status);
5937 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5938                                                enum var_init_status);
5939 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
5940                                         enum var_init_status);
5941 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5942 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5943 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5944 static tree field_type (const_tree);
5945 static unsigned int simple_type_align_in_bits (const_tree);
5946 static unsigned int simple_decl_align_in_bits (const_tree);
5947 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5948 static HOST_WIDE_INT field_byte_offset (const_tree);
5949 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5950                                          dw_loc_descr_ref);
5951 static void add_data_member_location_attribute (dw_die_ref, tree);
5952 static void add_const_value_attribute (dw_die_ref, rtx);
5953 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5954 static void insert_float (const_rtx, unsigned char *);
5955 static rtx rtl_for_decl_location (tree);
5956 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5957                                                    enum dwarf_attribute);
5958 static void tree_add_const_value_attribute (dw_die_ref, tree);
5959 static void tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
5960 static void add_name_attribute (dw_die_ref, const char *);
5961 static void add_comp_dir_attribute (dw_die_ref);
5962 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5963 static void add_subscript_info (dw_die_ref, tree, bool);
5964 static void add_byte_size_attribute (dw_die_ref, tree);
5965 static void add_bit_offset_attribute (dw_die_ref, tree);
5966 static void add_bit_size_attribute (dw_die_ref, tree);
5967 static void add_prototyped_attribute (dw_die_ref, tree);
5968 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5969 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5970 static void add_src_coords_attributes (dw_die_ref, tree);
5971 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5972 static void push_decl_scope (tree);
5973 static void pop_decl_scope (void);
5974 static dw_die_ref scope_die_for (tree, dw_die_ref);
5975 static inline int local_scope_p (dw_die_ref);
5976 static inline int class_scope_p (dw_die_ref);
5977 static inline int class_or_namespace_scope_p (dw_die_ref);
5978 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5979 static void add_calling_convention_attribute (dw_die_ref, tree);
5980 static const char *type_tag (const_tree);
5981 static tree member_declared_type (const_tree);
5982 #if 0
5983 static const char *decl_start_label (tree);
5984 #endif
5985 static void gen_array_type_die (tree, dw_die_ref);
5986 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5987 #if 0
5988 static void gen_entry_point_die (tree, dw_die_ref);
5989 #endif
5990 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5991 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5992 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5993 static void gen_formal_types_die (tree, dw_die_ref);
5994 static void gen_subprogram_die (tree, dw_die_ref);
5995 static void gen_variable_die (tree, tree, dw_die_ref);
5996 static void gen_const_die (tree, dw_die_ref);
5997 static void gen_label_die (tree, dw_die_ref);
5998 static void gen_lexical_block_die (tree, dw_die_ref, int);
5999 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6000 static void gen_field_die (tree, dw_die_ref);
6001 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6002 static dw_die_ref gen_compile_unit_die (const char *);
6003 static void gen_inheritance_die (tree, tree, dw_die_ref);
6004 static void gen_member_die (tree, dw_die_ref);
6005 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6006                                                 enum debug_info_usage);
6007 static void gen_subroutine_type_die (tree, dw_die_ref);
6008 static void gen_typedef_die (tree, dw_die_ref);
6009 static void gen_type_die (tree, dw_die_ref);
6010 static void gen_block_die (tree, dw_die_ref, int);
6011 static void decls_for_scope (tree, dw_die_ref, int);
6012 static int is_redundant_typedef (const_tree);
6013 static inline dw_die_ref get_context_die (tree);
6014 static void gen_namespace_die (tree, dw_die_ref);
6015 static void gen_decl_die (tree, tree, dw_die_ref);
6016 static dw_die_ref force_decl_die (tree);
6017 static dw_die_ref force_type_die (tree);
6018 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6019 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6020 static struct dwarf_file_data * lookup_filename (const char *);
6021 static void retry_incomplete_types (void);
6022 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6023 static tree make_ith_pack_parameter_name (tree, int);
6024 static void gen_generic_params_dies (tree);
6025 static void splice_child_die (dw_die_ref, dw_die_ref);
6026 static int file_info_cmp (const void *, const void *);
6027 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6028                                      const char *, const char *, unsigned);
6029 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
6030                                        const char *, const char *,
6031                                        const char *);
6032 static void output_loc_list (dw_loc_list_ref);
6033 static char *gen_internal_sym (const char *);
6034
6035 static void prune_unmark_dies (dw_die_ref);
6036 static void prune_unused_types_mark (dw_die_ref, int);
6037 static void prune_unused_types_walk (dw_die_ref);
6038 static void prune_unused_types_walk_attribs (dw_die_ref);
6039 static void prune_unused_types_prune (dw_die_ref);
6040 static void prune_unused_types (void);
6041 static int maybe_emit_file (struct dwarf_file_data *fd);
6042 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6043 static void gen_remaining_tmpl_value_param_die_attribute (void);
6044
6045 /* Section names used to hold DWARF debugging information.  */
6046 #ifndef DEBUG_INFO_SECTION
6047 #define DEBUG_INFO_SECTION      ".debug_info"
6048 #endif
6049 #ifndef DEBUG_ABBREV_SECTION
6050 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6051 #endif
6052 #ifndef DEBUG_ARANGES_SECTION
6053 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6054 #endif
6055 #ifndef DEBUG_MACINFO_SECTION
6056 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6057 #endif
6058 #ifndef DEBUG_LINE_SECTION
6059 #define DEBUG_LINE_SECTION      ".debug_line"
6060 #endif
6061 #ifndef DEBUG_LOC_SECTION
6062 #define DEBUG_LOC_SECTION       ".debug_loc"
6063 #endif
6064 #ifndef DEBUG_PUBNAMES_SECTION
6065 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6066 #endif
6067 #ifndef DEBUG_PUBTYPES_SECTION
6068 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6069 #endif
6070 #ifndef DEBUG_STR_SECTION
6071 #define DEBUG_STR_SECTION       ".debug_str"
6072 #endif
6073 #ifndef DEBUG_RANGES_SECTION
6074 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6075 #endif
6076
6077 /* Standard ELF section names for compiled code and data.  */
6078 #ifndef TEXT_SECTION_NAME
6079 #define TEXT_SECTION_NAME       ".text"
6080 #endif
6081
6082 /* Section flags for .debug_str section.  */
6083 #define DEBUG_STR_SECTION_FLAGS \
6084   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6085    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6086    : SECTION_DEBUG)
6087
6088 /* Labels we insert at beginning sections we can reference instead of
6089    the section names themselves.  */
6090
6091 #ifndef TEXT_SECTION_LABEL
6092 #define TEXT_SECTION_LABEL              "Ltext"
6093 #endif
6094 #ifndef COLD_TEXT_SECTION_LABEL
6095 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6096 #endif
6097 #ifndef DEBUG_LINE_SECTION_LABEL
6098 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6099 #endif
6100 #ifndef DEBUG_INFO_SECTION_LABEL
6101 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6102 #endif
6103 #ifndef DEBUG_ABBREV_SECTION_LABEL
6104 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6105 #endif
6106 #ifndef DEBUG_LOC_SECTION_LABEL
6107 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6108 #endif
6109 #ifndef DEBUG_RANGES_SECTION_LABEL
6110 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6111 #endif
6112 #ifndef DEBUG_MACINFO_SECTION_LABEL
6113 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6114 #endif
6115
6116 /* Definitions of defaults for formats and names of various special
6117    (artificial) labels which may be generated within this file (when the -g
6118    options is used and DWARF2_DEBUGGING_INFO is in effect.
6119    If necessary, these may be overridden from within the tm.h file, but
6120    typically, overriding these defaults is unnecessary.  */
6121
6122 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6123 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6124 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6125 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6126 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6127 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6128 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6129 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6130 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6131 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6132
6133 #ifndef TEXT_END_LABEL
6134 #define TEXT_END_LABEL          "Letext"
6135 #endif
6136 #ifndef COLD_END_LABEL
6137 #define COLD_END_LABEL          "Letext_cold"
6138 #endif
6139 #ifndef BLOCK_BEGIN_LABEL
6140 #define BLOCK_BEGIN_LABEL       "LBB"
6141 #endif
6142 #ifndef BLOCK_END_LABEL
6143 #define BLOCK_END_LABEL         "LBE"
6144 #endif
6145 #ifndef LINE_CODE_LABEL
6146 #define LINE_CODE_LABEL         "LM"
6147 #endif
6148 #ifndef SEPARATE_LINE_CODE_LABEL
6149 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6150 #endif
6151
6152 \f
6153 /* We allow a language front-end to designate a function that is to be
6154    called to "demangle" any name before it is put into a DIE.  */
6155
6156 static const char *(*demangle_name_func) (const char *);
6157
6158 void
6159 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6160 {
6161   demangle_name_func = func;
6162 }
6163
6164 /* Test if rtl node points to a pseudo register.  */
6165
6166 static inline int
6167 is_pseudo_reg (const_rtx rtl)
6168 {
6169   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6170           || (GET_CODE (rtl) == SUBREG
6171               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6172 }
6173
6174 /* Return a reference to a type, with its const and volatile qualifiers
6175    removed.  */
6176
6177 static inline tree
6178 type_main_variant (tree type)
6179 {
6180   type = TYPE_MAIN_VARIANT (type);
6181
6182   /* ??? There really should be only one main variant among any group of
6183      variants of a given type (and all of the MAIN_VARIANT values for all
6184      members of the group should point to that one type) but sometimes the C
6185      front-end messes this up for array types, so we work around that bug
6186      here.  */
6187   if (TREE_CODE (type) == ARRAY_TYPE)
6188     while (type != TYPE_MAIN_VARIANT (type))
6189       type = TYPE_MAIN_VARIANT (type);
6190
6191   return type;
6192 }
6193
6194 /* Return nonzero if the given type node represents a tagged type.  */
6195
6196 static inline int
6197 is_tagged_type (const_tree type)
6198 {
6199   enum tree_code code = TREE_CODE (type);
6200
6201   return (code == RECORD_TYPE || code == UNION_TYPE
6202           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6203 }
6204
6205 /* Convert a DIE tag into its string name.  */
6206
6207 static const char *
6208 dwarf_tag_name (unsigned int tag)
6209 {
6210   switch (tag)
6211     {
6212     case DW_TAG_padding:
6213       return "DW_TAG_padding";
6214     case DW_TAG_array_type:
6215       return "DW_TAG_array_type";
6216     case DW_TAG_class_type:
6217       return "DW_TAG_class_type";
6218     case DW_TAG_entry_point:
6219       return "DW_TAG_entry_point";
6220     case DW_TAG_enumeration_type:
6221       return "DW_TAG_enumeration_type";
6222     case DW_TAG_formal_parameter:
6223       return "DW_TAG_formal_parameter";
6224     case DW_TAG_imported_declaration:
6225       return "DW_TAG_imported_declaration";
6226     case DW_TAG_label:
6227       return "DW_TAG_label";
6228     case DW_TAG_lexical_block:
6229       return "DW_TAG_lexical_block";
6230     case DW_TAG_member:
6231       return "DW_TAG_member";
6232     case DW_TAG_pointer_type:
6233       return "DW_TAG_pointer_type";
6234     case DW_TAG_reference_type:
6235       return "DW_TAG_reference_type";
6236     case DW_TAG_compile_unit:
6237       return "DW_TAG_compile_unit";
6238     case DW_TAG_string_type:
6239       return "DW_TAG_string_type";
6240     case DW_TAG_structure_type:
6241       return "DW_TAG_structure_type";
6242     case DW_TAG_subroutine_type:
6243       return "DW_TAG_subroutine_type";
6244     case DW_TAG_typedef:
6245       return "DW_TAG_typedef";
6246     case DW_TAG_union_type:
6247       return "DW_TAG_union_type";
6248     case DW_TAG_unspecified_parameters:
6249       return "DW_TAG_unspecified_parameters";
6250     case DW_TAG_variant:
6251       return "DW_TAG_variant";
6252     case DW_TAG_common_block:
6253       return "DW_TAG_common_block";
6254     case DW_TAG_common_inclusion:
6255       return "DW_TAG_common_inclusion";
6256     case DW_TAG_inheritance:
6257       return "DW_TAG_inheritance";
6258     case DW_TAG_inlined_subroutine:
6259       return "DW_TAG_inlined_subroutine";
6260     case DW_TAG_module:
6261       return "DW_TAG_module";
6262     case DW_TAG_ptr_to_member_type:
6263       return "DW_TAG_ptr_to_member_type";
6264     case DW_TAG_set_type:
6265       return "DW_TAG_set_type";
6266     case DW_TAG_subrange_type:
6267       return "DW_TAG_subrange_type";
6268     case DW_TAG_with_stmt:
6269       return "DW_TAG_with_stmt";
6270     case DW_TAG_access_declaration:
6271       return "DW_TAG_access_declaration";
6272     case DW_TAG_base_type:
6273       return "DW_TAG_base_type";
6274     case DW_TAG_catch_block:
6275       return "DW_TAG_catch_block";
6276     case DW_TAG_const_type:
6277       return "DW_TAG_const_type";
6278     case DW_TAG_constant:
6279       return "DW_TAG_constant";
6280     case DW_TAG_enumerator:
6281       return "DW_TAG_enumerator";
6282     case DW_TAG_file_type:
6283       return "DW_TAG_file_type";
6284     case DW_TAG_friend:
6285       return "DW_TAG_friend";
6286     case DW_TAG_namelist:
6287       return "DW_TAG_namelist";
6288     case DW_TAG_namelist_item:
6289       return "DW_TAG_namelist_item";
6290     case DW_TAG_packed_type:
6291       return "DW_TAG_packed_type";
6292     case DW_TAG_subprogram:
6293       return "DW_TAG_subprogram";
6294     case DW_TAG_template_type_param:
6295       return "DW_TAG_template_type_param";
6296     case DW_TAG_template_value_param:
6297       return "DW_TAG_template_value_param";
6298     case DW_TAG_thrown_type:
6299       return "DW_TAG_thrown_type";
6300     case DW_TAG_try_block:
6301       return "DW_TAG_try_block";
6302     case DW_TAG_variant_part:
6303       return "DW_TAG_variant_part";
6304     case DW_TAG_variable:
6305       return "DW_TAG_variable";
6306     case DW_TAG_volatile_type:
6307       return "DW_TAG_volatile_type";
6308     case DW_TAG_dwarf_procedure:
6309       return "DW_TAG_dwarf_procedure";
6310     case DW_TAG_restrict_type:
6311       return "DW_TAG_restrict_type";
6312     case DW_TAG_interface_type:
6313       return "DW_TAG_interface_type";
6314     case DW_TAG_namespace:
6315       return "DW_TAG_namespace";
6316     case DW_TAG_imported_module:
6317       return "DW_TAG_imported_module";
6318     case DW_TAG_unspecified_type:
6319       return "DW_TAG_unspecified_type";
6320     case DW_TAG_partial_unit:
6321       return "DW_TAG_partial_unit";
6322     case DW_TAG_imported_unit:
6323       return "DW_TAG_imported_unit";
6324     case DW_TAG_condition:
6325       return "DW_TAG_condition";
6326     case DW_TAG_shared_type:
6327       return "DW_TAG_shared_type";
6328     case DW_TAG_MIPS_loop:
6329       return "DW_TAG_MIPS_loop";
6330     case DW_TAG_format_label:
6331       return "DW_TAG_format_label";
6332     case DW_TAG_function_template:
6333       return "DW_TAG_function_template";
6334     case DW_TAG_class_template:
6335       return "DW_TAG_class_template";
6336     case DW_TAG_GNU_BINCL:
6337       return "DW_TAG_GNU_BINCL";
6338     case DW_TAG_GNU_EINCL:
6339       return "DW_TAG_GNU_EINCL";
6340     case DW_TAG_GNU_template_template_param:
6341       return "DW_TAG_GNU_template_template_param";
6342     default:
6343       return "DW_TAG_<unknown>";
6344     }
6345 }
6346
6347 /* Convert a DWARF attribute code into its string name.  */
6348
6349 static const char *
6350 dwarf_attr_name (unsigned int attr)
6351 {
6352   switch (attr)
6353     {
6354     case DW_AT_sibling:
6355       return "DW_AT_sibling";
6356     case DW_AT_location:
6357       return "DW_AT_location";
6358     case DW_AT_name:
6359       return "DW_AT_name";
6360     case DW_AT_ordering:
6361       return "DW_AT_ordering";
6362     case DW_AT_subscr_data:
6363       return "DW_AT_subscr_data";
6364     case DW_AT_byte_size:
6365       return "DW_AT_byte_size";
6366     case DW_AT_bit_offset:
6367       return "DW_AT_bit_offset";
6368     case DW_AT_bit_size:
6369       return "DW_AT_bit_size";
6370     case DW_AT_element_list:
6371       return "DW_AT_element_list";
6372     case DW_AT_stmt_list:
6373       return "DW_AT_stmt_list";
6374     case DW_AT_low_pc:
6375       return "DW_AT_low_pc";
6376     case DW_AT_high_pc:
6377       return "DW_AT_high_pc";
6378     case DW_AT_language:
6379       return "DW_AT_language";
6380     case DW_AT_member:
6381       return "DW_AT_member";
6382     case DW_AT_discr:
6383       return "DW_AT_discr";
6384     case DW_AT_discr_value:
6385       return "DW_AT_discr_value";
6386     case DW_AT_visibility:
6387       return "DW_AT_visibility";
6388     case DW_AT_import:
6389       return "DW_AT_import";
6390     case DW_AT_string_length:
6391       return "DW_AT_string_length";
6392     case DW_AT_common_reference:
6393       return "DW_AT_common_reference";
6394     case DW_AT_comp_dir:
6395       return "DW_AT_comp_dir";
6396     case DW_AT_const_value:
6397       return "DW_AT_const_value";
6398     case DW_AT_containing_type:
6399       return "DW_AT_containing_type";
6400     case DW_AT_default_value:
6401       return "DW_AT_default_value";
6402     case DW_AT_inline:
6403       return "DW_AT_inline";
6404     case DW_AT_is_optional:
6405       return "DW_AT_is_optional";
6406     case DW_AT_lower_bound:
6407       return "DW_AT_lower_bound";
6408     case DW_AT_producer:
6409       return "DW_AT_producer";
6410     case DW_AT_prototyped:
6411       return "DW_AT_prototyped";
6412     case DW_AT_return_addr:
6413       return "DW_AT_return_addr";
6414     case DW_AT_start_scope:
6415       return "DW_AT_start_scope";
6416     case DW_AT_bit_stride:
6417       return "DW_AT_bit_stride";
6418     case DW_AT_upper_bound:
6419       return "DW_AT_upper_bound";
6420     case DW_AT_abstract_origin:
6421       return "DW_AT_abstract_origin";
6422     case DW_AT_accessibility:
6423       return "DW_AT_accessibility";
6424     case DW_AT_address_class:
6425       return "DW_AT_address_class";
6426     case DW_AT_artificial:
6427       return "DW_AT_artificial";
6428     case DW_AT_base_types:
6429       return "DW_AT_base_types";
6430     case DW_AT_calling_convention:
6431       return "DW_AT_calling_convention";
6432     case DW_AT_count:
6433       return "DW_AT_count";
6434     case DW_AT_data_member_location:
6435       return "DW_AT_data_member_location";
6436     case DW_AT_decl_column:
6437       return "DW_AT_decl_column";
6438     case DW_AT_decl_file:
6439       return "DW_AT_decl_file";
6440     case DW_AT_decl_line:
6441       return "DW_AT_decl_line";
6442     case DW_AT_declaration:
6443       return "DW_AT_declaration";
6444     case DW_AT_discr_list:
6445       return "DW_AT_discr_list";
6446     case DW_AT_encoding:
6447       return "DW_AT_encoding";
6448     case DW_AT_external:
6449       return "DW_AT_external";
6450     case DW_AT_explicit:
6451       return "DW_AT_explicit";
6452     case DW_AT_frame_base:
6453       return "DW_AT_frame_base";
6454     case DW_AT_friend:
6455       return "DW_AT_friend";
6456     case DW_AT_identifier_case:
6457       return "DW_AT_identifier_case";
6458     case DW_AT_macro_info:
6459       return "DW_AT_macro_info";
6460     case DW_AT_namelist_items:
6461       return "DW_AT_namelist_items";
6462     case DW_AT_priority:
6463       return "DW_AT_priority";
6464     case DW_AT_segment:
6465       return "DW_AT_segment";
6466     case DW_AT_specification:
6467       return "DW_AT_specification";
6468     case DW_AT_static_link:
6469       return "DW_AT_static_link";
6470     case DW_AT_type:
6471       return "DW_AT_type";
6472     case DW_AT_use_location:
6473       return "DW_AT_use_location";
6474     case DW_AT_variable_parameter:
6475       return "DW_AT_variable_parameter";
6476     case DW_AT_virtuality:
6477       return "DW_AT_virtuality";
6478     case DW_AT_vtable_elem_location:
6479       return "DW_AT_vtable_elem_location";
6480
6481     case DW_AT_allocated:
6482       return "DW_AT_allocated";
6483     case DW_AT_associated:
6484       return "DW_AT_associated";
6485     case DW_AT_data_location:
6486       return "DW_AT_data_location";
6487     case DW_AT_byte_stride:
6488       return "DW_AT_byte_stride";
6489     case DW_AT_entry_pc:
6490       return "DW_AT_entry_pc";
6491     case DW_AT_use_UTF8:
6492       return "DW_AT_use_UTF8";
6493     case DW_AT_extension:
6494       return "DW_AT_extension";
6495     case DW_AT_ranges:
6496       return "DW_AT_ranges";
6497     case DW_AT_trampoline:
6498       return "DW_AT_trampoline";
6499     case DW_AT_call_column:
6500       return "DW_AT_call_column";
6501     case DW_AT_call_file:
6502       return "DW_AT_call_file";
6503     case DW_AT_call_line:
6504       return "DW_AT_call_line";
6505
6506     case DW_AT_MIPS_fde:
6507       return "DW_AT_MIPS_fde";
6508     case DW_AT_MIPS_loop_begin:
6509       return "DW_AT_MIPS_loop_begin";
6510     case DW_AT_MIPS_tail_loop_begin:
6511       return "DW_AT_MIPS_tail_loop_begin";
6512     case DW_AT_MIPS_epilog_begin:
6513       return "DW_AT_MIPS_epilog_begin";
6514     case DW_AT_MIPS_loop_unroll_factor:
6515       return "DW_AT_MIPS_loop_unroll_factor";
6516     case DW_AT_MIPS_software_pipeline_depth:
6517       return "DW_AT_MIPS_software_pipeline_depth";
6518     case DW_AT_MIPS_linkage_name:
6519       return "DW_AT_MIPS_linkage_name";
6520     case DW_AT_MIPS_stride:
6521       return "DW_AT_MIPS_stride";
6522     case DW_AT_MIPS_abstract_name:
6523       return "DW_AT_MIPS_abstract_name";
6524     case DW_AT_MIPS_clone_origin:
6525       return "DW_AT_MIPS_clone_origin";
6526     case DW_AT_MIPS_has_inlines:
6527       return "DW_AT_MIPS_has_inlines";
6528
6529     case DW_AT_sf_names:
6530       return "DW_AT_sf_names";
6531     case DW_AT_src_info:
6532       return "DW_AT_src_info";
6533     case DW_AT_mac_info:
6534       return "DW_AT_mac_info";
6535     case DW_AT_src_coords:
6536       return "DW_AT_src_coords";
6537     case DW_AT_body_begin:
6538       return "DW_AT_body_begin";
6539     case DW_AT_body_end:
6540       return "DW_AT_body_end";
6541     case DW_AT_GNU_vector:
6542       return "DW_AT_GNU_vector";
6543     case DW_AT_GNU_template_name:
6544       return "DW_AT_GNU_template_name";
6545
6546     case DW_AT_VMS_rtnbeg_pd_address:
6547       return "DW_AT_VMS_rtnbeg_pd_address";
6548
6549     default:
6550       return "DW_AT_<unknown>";
6551     }
6552 }
6553
6554 /* Convert a DWARF value form code into its string name.  */
6555
6556 static const char *
6557 dwarf_form_name (unsigned int form)
6558 {
6559   switch (form)
6560     {
6561     case DW_FORM_addr:
6562       return "DW_FORM_addr";
6563     case DW_FORM_block2:
6564       return "DW_FORM_block2";
6565     case DW_FORM_block4:
6566       return "DW_FORM_block4";
6567     case DW_FORM_data2:
6568       return "DW_FORM_data2";
6569     case DW_FORM_data4:
6570       return "DW_FORM_data4";
6571     case DW_FORM_data8:
6572       return "DW_FORM_data8";
6573     case DW_FORM_string:
6574       return "DW_FORM_string";
6575     case DW_FORM_block:
6576       return "DW_FORM_block";
6577     case DW_FORM_block1:
6578       return "DW_FORM_block1";
6579     case DW_FORM_data1:
6580       return "DW_FORM_data1";
6581     case DW_FORM_flag:
6582       return "DW_FORM_flag";
6583     case DW_FORM_sdata:
6584       return "DW_FORM_sdata";
6585     case DW_FORM_strp:
6586       return "DW_FORM_strp";
6587     case DW_FORM_udata:
6588       return "DW_FORM_udata";
6589     case DW_FORM_ref_addr:
6590       return "DW_FORM_ref_addr";
6591     case DW_FORM_ref1:
6592       return "DW_FORM_ref1";
6593     case DW_FORM_ref2:
6594       return "DW_FORM_ref2";
6595     case DW_FORM_ref4:
6596       return "DW_FORM_ref4";
6597     case DW_FORM_ref8:
6598       return "DW_FORM_ref8";
6599     case DW_FORM_ref_udata:
6600       return "DW_FORM_ref_udata";
6601     case DW_FORM_indirect:
6602       return "DW_FORM_indirect";
6603     default:
6604       return "DW_FORM_<unknown>";
6605     }
6606 }
6607 \f
6608 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6609    instance of an inlined instance of a decl which is local to an inline
6610    function, so we have to trace all of the way back through the origin chain
6611    to find out what sort of node actually served as the original seed for the
6612    given block.  */
6613
6614 static tree
6615 decl_ultimate_origin (const_tree decl)
6616 {
6617   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6618     return NULL_TREE;
6619
6620   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6621      nodes in the function to point to themselves; ignore that if
6622      we're trying to output the abstract instance of this function.  */
6623   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6624     return NULL_TREE;
6625
6626   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6627      most distant ancestor, this should never happen.  */
6628   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6629
6630   return DECL_ABSTRACT_ORIGIN (decl);
6631 }
6632
6633 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6634    of a virtual function may refer to a base class, so we check the 'this'
6635    parameter.  */
6636
6637 static tree
6638 decl_class_context (tree decl)
6639 {
6640   tree context = NULL_TREE;
6641
6642   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6643     context = DECL_CONTEXT (decl);
6644   else
6645     context = TYPE_MAIN_VARIANT
6646       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6647
6648   if (context && !TYPE_P (context))
6649     context = NULL_TREE;
6650
6651   return context;
6652 }
6653 \f
6654 /* Add an attribute/value pair to a DIE.  */
6655
6656 static inline void
6657 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6658 {
6659   /* Maybe this should be an assert?  */
6660   if (die == NULL)
6661     return;
6662
6663   if (die->die_attr == NULL)
6664     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6665   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6666 }
6667
6668 static inline enum dw_val_class
6669 AT_class (dw_attr_ref a)
6670 {
6671   return a->dw_attr_val.val_class;
6672 }
6673
6674 /* Add a flag value attribute to a DIE.  */
6675
6676 static inline void
6677 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6678 {
6679   dw_attr_node attr;
6680
6681   attr.dw_attr = attr_kind;
6682   attr.dw_attr_val.val_class = dw_val_class_flag;
6683   attr.dw_attr_val.v.val_flag = flag;
6684   add_dwarf_attr (die, &attr);
6685 }
6686
6687 static inline unsigned
6688 AT_flag (dw_attr_ref a)
6689 {
6690   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6691   return a->dw_attr_val.v.val_flag;
6692 }
6693
6694 /* Add a signed integer attribute value to a DIE.  */
6695
6696 static inline void
6697 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6698 {
6699   dw_attr_node attr;
6700
6701   attr.dw_attr = attr_kind;
6702   attr.dw_attr_val.val_class = dw_val_class_const;
6703   attr.dw_attr_val.v.val_int = int_val;
6704   add_dwarf_attr (die, &attr);
6705 }
6706
6707 static inline HOST_WIDE_INT
6708 AT_int (dw_attr_ref a)
6709 {
6710   gcc_assert (a && AT_class (a) == dw_val_class_const);
6711   return a->dw_attr_val.v.val_int;
6712 }
6713
6714 /* Add an unsigned integer attribute value to a DIE.  */
6715
6716 static inline void
6717 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6718                  unsigned HOST_WIDE_INT unsigned_val)
6719 {
6720   dw_attr_node attr;
6721
6722   attr.dw_attr = attr_kind;
6723   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6724   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6725   add_dwarf_attr (die, &attr);
6726 }
6727
6728 static inline unsigned HOST_WIDE_INT
6729 AT_unsigned (dw_attr_ref a)
6730 {
6731   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6732   return a->dw_attr_val.v.val_unsigned;
6733 }
6734
6735 /* Add an unsigned double integer attribute value to a DIE.  */
6736
6737 static inline void
6738 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6739                   rtx val_const_double)
6740 {
6741   dw_attr_node attr;
6742
6743   attr.dw_attr = attr_kind;
6744   attr.dw_attr_val.val_class = dw_val_class_long_long;
6745   attr.dw_attr_val.v.val_long_long = val_const_double;
6746   add_dwarf_attr (die, &attr);
6747 }
6748
6749 /* Add a floating point attribute value to a DIE and return it.  */
6750
6751 static inline void
6752 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6753             unsigned int length, unsigned int elt_size, unsigned char *array)
6754 {
6755   dw_attr_node attr;
6756
6757   attr.dw_attr = attr_kind;
6758   attr.dw_attr_val.val_class = dw_val_class_vec;
6759   attr.dw_attr_val.v.val_vec.length = length;
6760   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6761   attr.dw_attr_val.v.val_vec.array = array;
6762   add_dwarf_attr (die, &attr);
6763 }
6764
6765 /* Hash and equality functions for debug_str_hash.  */
6766
6767 static hashval_t
6768 debug_str_do_hash (const void *x)
6769 {
6770   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6771 }
6772
6773 static int
6774 debug_str_eq (const void *x1, const void *x2)
6775 {
6776   return strcmp ((((const struct indirect_string_node *)x1)->str),
6777                  (const char *)x2) == 0;
6778 }
6779
6780 /* Add STR to the indirect string hash table.  */
6781
6782 static struct indirect_string_node *
6783 find_AT_string (const char *str)
6784 {
6785   struct indirect_string_node *node;
6786   void **slot;
6787
6788   if (! debug_str_hash)
6789     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6790                                       debug_str_eq, NULL);
6791
6792   slot = htab_find_slot_with_hash (debug_str_hash, str,
6793                                    htab_hash_string (str), INSERT);
6794   if (*slot == NULL)
6795     {
6796       node = (struct indirect_string_node *)
6797                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6798       node->str = ggc_strdup (str);
6799       *slot = node;
6800     }
6801   else
6802     node = (struct indirect_string_node *) *slot;
6803
6804   node->refcount++;
6805   return node;
6806 }
6807
6808 /* Add a string attribute value to a DIE.  */
6809
6810 static inline void
6811 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6812 {
6813   dw_attr_node attr;
6814   struct indirect_string_node *node;
6815
6816   node = find_AT_string (str);
6817
6818   attr.dw_attr = attr_kind;
6819   attr.dw_attr_val.val_class = dw_val_class_str;
6820   attr.dw_attr_val.v.val_str = node;
6821   add_dwarf_attr (die, &attr);
6822 }
6823
6824 /* Create a label for an indirect string node, ensuring it is going to
6825    be output, unless its reference count goes down to zero.  */
6826
6827 static inline void
6828 gen_label_for_indirect_string (struct indirect_string_node *node)
6829 {
6830   char label[32];
6831
6832   if (node->label)
6833     return;
6834
6835   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6836   ++dw2_string_counter;
6837   node->label = xstrdup (label);
6838 }
6839
6840 /* Create a SYMBOL_REF rtx whose value is the initial address of a
6841    debug string STR.  */
6842
6843 static inline rtx
6844 get_debug_string_label (const char *str)
6845 {
6846   struct indirect_string_node *node = find_AT_string (str);
6847
6848   debug_str_hash_forced = true;
6849
6850   gen_label_for_indirect_string (node);
6851
6852   return gen_rtx_SYMBOL_REF (Pmode, node->label);
6853 }
6854
6855 static inline const char *
6856 AT_string (dw_attr_ref a)
6857 {
6858   gcc_assert (a && AT_class (a) == dw_val_class_str);
6859   return a->dw_attr_val.v.val_str->str;
6860 }
6861
6862 /* Find out whether a string should be output inline in DIE
6863    or out-of-line in .debug_str section.  */
6864
6865 static enum dwarf_form
6866 AT_string_form (dw_attr_ref a)
6867 {
6868   struct indirect_string_node *node;
6869   unsigned int len;
6870
6871   gcc_assert (a && AT_class (a) == dw_val_class_str);
6872
6873   node = a->dw_attr_val.v.val_str;
6874   if (node->form)
6875     return node->form;
6876
6877   len = strlen (node->str) + 1;
6878
6879   /* If the string is shorter or equal to the size of the reference, it is
6880      always better to put it inline.  */
6881   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6882     return node->form = DW_FORM_string;
6883
6884   /* If we cannot expect the linker to merge strings in .debug_str
6885      section, only put it into .debug_str if it is worth even in this
6886      single module.  */
6887   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
6888       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
6889       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
6890     return node->form = DW_FORM_string;
6891
6892   gen_label_for_indirect_string (node);
6893
6894   return node->form = DW_FORM_strp;
6895 }
6896
6897 /* Add a DIE reference attribute value to a DIE.  */
6898
6899 static inline void
6900 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6901 {
6902   dw_attr_node attr;
6903
6904   attr.dw_attr = attr_kind;
6905   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6906   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6907   attr.dw_attr_val.v.val_die_ref.external = 0;
6908   add_dwarf_attr (die, &attr);
6909 }
6910
6911 /* Add an AT_specification attribute to a DIE, and also make the back
6912    pointer from the specification to the definition.  */
6913
6914 static inline void
6915 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6916 {
6917   add_AT_die_ref (die, DW_AT_specification, targ_die);
6918   gcc_assert (!targ_die->die_definition);
6919   targ_die->die_definition = die;
6920 }
6921
6922 static inline dw_die_ref
6923 AT_ref (dw_attr_ref a)
6924 {
6925   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6926   return a->dw_attr_val.v.val_die_ref.die;
6927 }
6928
6929 static inline int
6930 AT_ref_external (dw_attr_ref a)
6931 {
6932   if (a && AT_class (a) == dw_val_class_die_ref)
6933     return a->dw_attr_val.v.val_die_ref.external;
6934
6935   return 0;
6936 }
6937
6938 static inline void
6939 set_AT_ref_external (dw_attr_ref a, int i)
6940 {
6941   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6942   a->dw_attr_val.v.val_die_ref.external = i;
6943 }
6944
6945 /* Add an FDE reference attribute value to a DIE.  */
6946
6947 static inline void
6948 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6949 {
6950   dw_attr_node attr;
6951
6952   attr.dw_attr = attr_kind;
6953   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6954   attr.dw_attr_val.v.val_fde_index = targ_fde;
6955   add_dwarf_attr (die, &attr);
6956 }
6957
6958 /* Add a location description attribute value to a DIE.  */
6959
6960 static inline void
6961 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6962 {
6963   dw_attr_node attr;
6964
6965   attr.dw_attr = attr_kind;
6966   attr.dw_attr_val.val_class = dw_val_class_loc;
6967   attr.dw_attr_val.v.val_loc = loc;
6968   add_dwarf_attr (die, &attr);
6969 }
6970
6971 static inline dw_loc_descr_ref
6972 AT_loc (dw_attr_ref a)
6973 {
6974   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6975   return a->dw_attr_val.v.val_loc;
6976 }
6977
6978 static inline void
6979 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6980 {
6981   dw_attr_node attr;
6982
6983   attr.dw_attr = attr_kind;
6984   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6985   attr.dw_attr_val.v.val_loc_list = loc_list;
6986   add_dwarf_attr (die, &attr);
6987   have_location_lists = true;
6988 }
6989
6990 static inline dw_loc_list_ref
6991 AT_loc_list (dw_attr_ref a)
6992 {
6993   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6994   return a->dw_attr_val.v.val_loc_list;
6995 }
6996
6997 /* Add an address constant attribute value to a DIE.  */
6998
6999 static inline void
7000 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7001 {
7002   dw_attr_node attr;
7003
7004   attr.dw_attr = attr_kind;
7005   attr.dw_attr_val.val_class = dw_val_class_addr;
7006   attr.dw_attr_val.v.val_addr = addr;
7007   add_dwarf_attr (die, &attr);
7008 }
7009
7010 /* Get the RTX from to an address DIE attribute.  */
7011
7012 static inline rtx
7013 AT_addr (dw_attr_ref a)
7014 {
7015   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7016   return a->dw_attr_val.v.val_addr;
7017 }
7018
7019 /* Add a file attribute value to a DIE.  */
7020
7021 static inline void
7022 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7023              struct dwarf_file_data *fd)
7024 {
7025   dw_attr_node attr;
7026
7027   attr.dw_attr = attr_kind;
7028   attr.dw_attr_val.val_class = dw_val_class_file;
7029   attr.dw_attr_val.v.val_file = fd;
7030   add_dwarf_attr (die, &attr);
7031 }
7032
7033 /* Get the dwarf_file_data from a file DIE attribute.  */
7034
7035 static inline struct dwarf_file_data *
7036 AT_file (dw_attr_ref a)
7037 {
7038   gcc_assert (a && AT_class (a) == dw_val_class_file);
7039   return a->dw_attr_val.v.val_file;
7040 }
7041
7042 /* Add a label identifier attribute value to a DIE.  */
7043
7044 static inline void
7045 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7046 {
7047   dw_attr_node attr;
7048
7049   attr.dw_attr = attr_kind;
7050   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7051   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7052   add_dwarf_attr (die, &attr);
7053 }
7054
7055 /* Add a section offset attribute value to a DIE, an offset into the
7056    debug_line section.  */
7057
7058 static inline void
7059 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7060                 const char *label)
7061 {
7062   dw_attr_node attr;
7063
7064   attr.dw_attr = attr_kind;
7065   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7066   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7067   add_dwarf_attr (die, &attr);
7068 }
7069
7070 /* Add a section offset attribute value to a DIE, an offset into the
7071    debug_macinfo section.  */
7072
7073 static inline void
7074 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7075                const char *label)
7076 {
7077   dw_attr_node attr;
7078
7079   attr.dw_attr = attr_kind;
7080   attr.dw_attr_val.val_class = dw_val_class_macptr;
7081   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7082   add_dwarf_attr (die, &attr);
7083 }
7084
7085 /* Add an offset attribute value to a DIE.  */
7086
7087 static inline void
7088 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7089                unsigned HOST_WIDE_INT offset)
7090 {
7091   dw_attr_node attr;
7092
7093   attr.dw_attr = attr_kind;
7094   attr.dw_attr_val.val_class = dw_val_class_offset;
7095   attr.dw_attr_val.v.val_offset = offset;
7096   add_dwarf_attr (die, &attr);
7097 }
7098
7099 /* Add an range_list attribute value to a DIE.  */
7100
7101 static void
7102 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7103                    long unsigned int offset)
7104 {
7105   dw_attr_node attr;
7106
7107   attr.dw_attr = attr_kind;
7108   attr.dw_attr_val.val_class = dw_val_class_range_list;
7109   attr.dw_attr_val.v.val_offset = offset;
7110   add_dwarf_attr (die, &attr);
7111 }
7112
7113 static inline const char *
7114 AT_lbl (dw_attr_ref a)
7115 {
7116   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7117                     || AT_class (a) == dw_val_class_lineptr
7118                     || AT_class (a) == dw_val_class_macptr));
7119   return a->dw_attr_val.v.val_lbl_id;
7120 }
7121
7122 /* Get the attribute of type attr_kind.  */
7123
7124 static dw_attr_ref
7125 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7126 {
7127   dw_attr_ref a;
7128   unsigned ix;
7129   dw_die_ref spec = NULL;
7130
7131   if (! die)
7132     return NULL;
7133
7134   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7135     if (a->dw_attr == attr_kind)
7136       return a;
7137     else if (a->dw_attr == DW_AT_specification
7138              || a->dw_attr == DW_AT_abstract_origin)
7139       spec = AT_ref (a);
7140
7141   if (spec)
7142     return get_AT (spec, attr_kind);
7143
7144   return NULL;
7145 }
7146
7147 /* Return the "low pc" attribute value, typically associated with a subprogram
7148    DIE.  Return null if the "low pc" attribute is either not present, or if it
7149    cannot be represented as an assembler label identifier.  */
7150
7151 static inline const char *
7152 get_AT_low_pc (dw_die_ref die)
7153 {
7154   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7155
7156   return a ? AT_lbl (a) : NULL;
7157 }
7158
7159 /* Return the "high pc" attribute value, typically associated with a subprogram
7160    DIE.  Return null if the "high pc" attribute is either not present, or if it
7161    cannot be represented as an assembler label identifier.  */
7162
7163 static inline const char *
7164 get_AT_hi_pc (dw_die_ref die)
7165 {
7166   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7167
7168   return a ? AT_lbl (a) : NULL;
7169 }
7170
7171 /* Return the value of the string attribute designated by ATTR_KIND, or
7172    NULL if it is not present.  */
7173
7174 static inline const char *
7175 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7176 {
7177   dw_attr_ref a = get_AT (die, attr_kind);
7178
7179   return a ? AT_string (a) : NULL;
7180 }
7181
7182 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7183    if it is not present.  */
7184
7185 static inline int
7186 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7187 {
7188   dw_attr_ref a = get_AT (die, attr_kind);
7189
7190   return a ? AT_flag (a) : 0;
7191 }
7192
7193 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7194    if it is not present.  */
7195
7196 static inline unsigned
7197 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7198 {
7199   dw_attr_ref a = get_AT (die, attr_kind);
7200
7201   return a ? AT_unsigned (a) : 0;
7202 }
7203
7204 static inline dw_die_ref
7205 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7206 {
7207   dw_attr_ref a = get_AT (die, attr_kind);
7208
7209   return a ? AT_ref (a) : NULL;
7210 }
7211
7212 static inline struct dwarf_file_data *
7213 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7214 {
7215   dw_attr_ref a = get_AT (die, attr_kind);
7216
7217   return a ? AT_file (a) : NULL;
7218 }
7219
7220 /* Return TRUE if the language is C or C++.  */
7221
7222 static inline bool
7223 is_c_family (void)
7224 {
7225   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7226
7227   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7228           || lang == DW_LANG_C99
7229           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7230 }
7231
7232 /* Return TRUE if the language is C++.  */
7233
7234 static inline bool
7235 is_cxx (void)
7236 {
7237   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7238
7239   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7240 }
7241
7242 /* Return TRUE if the language is Fortran.  */
7243
7244 static inline bool
7245 is_fortran (void)
7246 {
7247   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7248
7249   return (lang == DW_LANG_Fortran77
7250           || lang == DW_LANG_Fortran90
7251           || lang == DW_LANG_Fortran95);
7252 }
7253
7254 /* Return TRUE if the language is Java.  */
7255
7256 static inline bool
7257 is_java (void)
7258 {
7259   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7260
7261   return lang == DW_LANG_Java;
7262 }
7263
7264 /* Return TRUE if the language is Ada.  */
7265
7266 static inline bool
7267 is_ada (void)
7268 {
7269   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7270
7271   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7272 }
7273
7274 /* Remove the specified attribute if present.  */
7275
7276 static void
7277 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7278 {
7279   dw_attr_ref a;
7280   unsigned ix;
7281
7282   if (! die)
7283     return;
7284
7285   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7286     if (a->dw_attr == attr_kind)
7287       {
7288         if (AT_class (a) == dw_val_class_str)
7289           if (a->dw_attr_val.v.val_str->refcount)
7290             a->dw_attr_val.v.val_str->refcount--;
7291
7292         /* VEC_ordered_remove should help reduce the number of abbrevs
7293            that are needed.  */
7294         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7295         return;
7296       }
7297 }
7298
7299 /* Remove CHILD from its parent.  PREV must have the property that
7300    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7301
7302 static void
7303 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7304 {
7305   gcc_assert (child->die_parent == prev->die_parent);
7306   gcc_assert (prev->die_sib == child);
7307   if (prev == child)
7308     {
7309       gcc_assert (child->die_parent->die_child == child);
7310       prev = NULL;
7311     }
7312   else
7313     prev->die_sib = child->die_sib;
7314   if (child->die_parent->die_child == child)
7315     child->die_parent->die_child = prev;
7316 }
7317
7318 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7319    matches TAG.  */
7320
7321 static void
7322 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7323 {
7324   dw_die_ref c;
7325
7326   c = die->die_child;
7327   if (c) do {
7328     dw_die_ref prev = c;
7329     c = c->die_sib;
7330     while (c->die_tag == tag)
7331       {
7332         remove_child_with_prev (c, prev);
7333         /* Might have removed every child.  */
7334         if (c == c->die_sib)
7335           return;
7336         c = c->die_sib;
7337       }
7338   } while (c != die->die_child);
7339 }
7340
7341 /* Add a CHILD_DIE as the last child of DIE.  */
7342
7343 static void
7344 add_child_die (dw_die_ref die, dw_die_ref child_die)
7345 {
7346   /* FIXME this should probably be an assert.  */
7347   if (! die || ! child_die)
7348     return;
7349   gcc_assert (die != child_die);
7350
7351   child_die->die_parent = die;
7352   if (die->die_child)
7353     {
7354       child_die->die_sib = die->die_child->die_sib;
7355       die->die_child->die_sib = child_die;
7356     }
7357   else
7358     child_die->die_sib = child_die;
7359   die->die_child = child_die;
7360 }
7361
7362 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7363    is the specification, to the end of PARENT's list of children.
7364    This is done by removing and re-adding it.  */
7365
7366 static void
7367 splice_child_die (dw_die_ref parent, dw_die_ref child)
7368 {
7369   dw_die_ref p;
7370
7371   /* We want the declaration DIE from inside the class, not the
7372      specification DIE at toplevel.  */
7373   if (child->die_parent != parent)
7374     {
7375       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7376
7377       if (tmp)
7378         child = tmp;
7379     }
7380
7381   gcc_assert (child->die_parent == parent
7382               || (child->die_parent
7383                   == get_AT_ref (parent, DW_AT_specification)));
7384
7385   for (p = child->die_parent->die_child; ; p = p->die_sib)
7386     if (p->die_sib == child)
7387       {
7388         remove_child_with_prev (child, p);
7389         break;
7390       }
7391
7392   add_child_die (parent, child);
7393 }
7394
7395 /* Return a pointer to a newly created DIE node.  */
7396
7397 static inline dw_die_ref
7398 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7399 {
7400   dw_die_ref die = GGC_CNEW (die_node);
7401
7402   die->die_tag = tag_value;
7403
7404   if (parent_die != NULL)
7405     add_child_die (parent_die, die);
7406   else
7407     {
7408       limbo_die_node *limbo_node;
7409
7410       limbo_node = GGC_CNEW (limbo_die_node);
7411       limbo_node->die = die;
7412       limbo_node->created_for = t;
7413       limbo_node->next = limbo_die_list;
7414       limbo_die_list = limbo_node;
7415     }
7416
7417   return die;
7418 }
7419
7420 /* Return the DIE associated with the given type specifier.  */
7421
7422 static inline dw_die_ref
7423 lookup_type_die (tree type)
7424 {
7425   return TYPE_SYMTAB_DIE (type);
7426 }
7427
7428 /* Equate a DIE to a given type specifier.  */
7429
7430 static inline void
7431 equate_type_number_to_die (tree type, dw_die_ref type_die)
7432 {
7433   TYPE_SYMTAB_DIE (type) = type_die;
7434 }
7435
7436 /* Returns a hash value for X (which really is a die_struct).  */
7437
7438 static hashval_t
7439 decl_die_table_hash (const void *x)
7440 {
7441   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7442 }
7443
7444 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7445
7446 static int
7447 decl_die_table_eq (const void *x, const void *y)
7448 {
7449   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7450 }
7451
7452 /* Return the DIE associated with a given declaration.  */
7453
7454 static inline dw_die_ref
7455 lookup_decl_die (tree decl)
7456 {
7457   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7458 }
7459
7460 /* Returns a hash value for X (which really is a var_loc_list).  */
7461
7462 static hashval_t
7463 decl_loc_table_hash (const void *x)
7464 {
7465   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7466 }
7467
7468 /* Return nonzero if decl_id of var_loc_list X is the same as
7469    UID of decl *Y.  */
7470
7471 static int
7472 decl_loc_table_eq (const void *x, const void *y)
7473 {
7474   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7475 }
7476
7477 /* Return the var_loc list associated with a given declaration.  */
7478
7479 static inline var_loc_list *
7480 lookup_decl_loc (const_tree decl)
7481 {
7482   return (var_loc_list *)
7483     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7484 }
7485
7486 /* Equate a DIE to a particular declaration.  */
7487
7488 static void
7489 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7490 {
7491   unsigned int decl_id = DECL_UID (decl);
7492   void **slot;
7493
7494   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7495   *slot = decl_die;
7496   decl_die->decl_id = decl_id;
7497 }
7498
7499 /* Add a variable location node to the linked list for DECL.  */
7500
7501 static void
7502 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7503 {
7504   unsigned int decl_id = DECL_UID (decl);
7505   var_loc_list *temp;
7506   void **slot;
7507
7508   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7509   if (*slot == NULL)
7510     {
7511       temp = GGC_CNEW (var_loc_list);
7512       temp->decl_id = decl_id;
7513       *slot = temp;
7514     }
7515   else
7516     temp = (var_loc_list *) *slot;
7517
7518   if (temp->last)
7519     {
7520       /* If the current location is the same as the end of the list,
7521          and either both or neither of the locations is uninitialized,
7522          we have nothing to do.  */
7523       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7524                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7525           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7526                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7527               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7528                    == VAR_INIT_STATUS_UNINITIALIZED)
7529                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7530                       == VAR_INIT_STATUS_UNINITIALIZED))))
7531         {
7532           /* Add LOC to the end of list and update LAST.  */
7533           temp->last->next = loc;
7534           temp->last = loc;
7535         }
7536     }
7537   /* Do not add empty location to the beginning of the list.  */
7538   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7539     {
7540       temp->first = loc;
7541       temp->last = loc;
7542     }
7543 }
7544 \f
7545 /* Keep track of the number of spaces used to indent the
7546    output of the debugging routines that print the structure of
7547    the DIE internal representation.  */
7548 static int print_indent;
7549
7550 /* Indent the line the number of spaces given by print_indent.  */
7551
7552 static inline void
7553 print_spaces (FILE *outfile)
7554 {
7555   fprintf (outfile, "%*s", print_indent, "");
7556 }
7557
7558 /* Print the information associated with a given DIE, and its children.
7559    This routine is a debugging aid only.  */
7560
7561 static void
7562 print_die (dw_die_ref die, FILE *outfile)
7563 {
7564   dw_attr_ref a;
7565   dw_die_ref c;
7566   unsigned ix;
7567
7568   print_spaces (outfile);
7569   fprintf (outfile, "DIE %4ld: %s\n",
7570            die->die_offset, dwarf_tag_name (die->die_tag));
7571   print_spaces (outfile);
7572   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7573   fprintf (outfile, " offset: %ld\n", die->die_offset);
7574
7575   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7576     {
7577       print_spaces (outfile);
7578       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7579
7580       switch (AT_class (a))
7581         {
7582         case dw_val_class_addr:
7583           fprintf (outfile, "address");
7584           break;
7585         case dw_val_class_offset:
7586           fprintf (outfile, "offset");
7587           break;
7588         case dw_val_class_loc:
7589           fprintf (outfile, "location descriptor");
7590           break;
7591         case dw_val_class_loc_list:
7592           fprintf (outfile, "location list -> label:%s",
7593                    AT_loc_list (a)->ll_symbol);
7594           break;
7595         case dw_val_class_range_list:
7596           fprintf (outfile, "range list");
7597           break;
7598         case dw_val_class_const:
7599           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7600           break;
7601         case dw_val_class_unsigned_const:
7602           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7603           break;
7604         case dw_val_class_long_long:
7605           fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_UNSIGNED
7606                             "," HOST_WIDE_INT_PRINT_UNSIGNED ")",
7607                    CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long),
7608                    CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long));
7609           break;
7610         case dw_val_class_vec:
7611           fprintf (outfile, "floating-point or vector constant");
7612           break;
7613         case dw_val_class_flag:
7614           fprintf (outfile, "%u", AT_flag (a));
7615           break;
7616         case dw_val_class_die_ref:
7617           if (AT_ref (a) != NULL)
7618             {
7619               if (AT_ref (a)->die_symbol)
7620                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7621               else
7622                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7623             }
7624           else
7625             fprintf (outfile, "die -> <null>");
7626           break;
7627         case dw_val_class_lbl_id:
7628         case dw_val_class_lineptr:
7629         case dw_val_class_macptr:
7630           fprintf (outfile, "label: %s", AT_lbl (a));
7631           break;
7632         case dw_val_class_str:
7633           if (AT_string (a) != NULL)
7634             fprintf (outfile, "\"%s\"", AT_string (a));
7635           else
7636             fprintf (outfile, "<null>");
7637           break;
7638         case dw_val_class_file:
7639           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7640                    AT_file (a)->emitted_number);
7641           break;
7642         default:
7643           break;
7644         }
7645
7646       fprintf (outfile, "\n");
7647     }
7648
7649   if (die->die_child != NULL)
7650     {
7651       print_indent += 4;
7652       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7653       print_indent -= 4;
7654     }
7655   if (print_indent == 0)
7656     fprintf (outfile, "\n");
7657 }
7658
7659 /* Print the contents of the source code line number correspondence table.
7660    This routine is a debugging aid only.  */
7661
7662 static void
7663 print_dwarf_line_table (FILE *outfile)
7664 {
7665   unsigned i;
7666   dw_line_info_ref line_info;
7667
7668   fprintf (outfile, "\n\nDWARF source line information\n");
7669   for (i = 1; i < line_info_table_in_use; i++)
7670     {
7671       line_info = &line_info_table[i];
7672       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7673                line_info->dw_file_num,
7674                line_info->dw_line_num);
7675     }
7676
7677   fprintf (outfile, "\n\n");
7678 }
7679
7680 /* Print the information collected for a given DIE.  */
7681
7682 void
7683 debug_dwarf_die (dw_die_ref die)
7684 {
7685   print_die (die, stderr);
7686 }
7687
7688 /* Print all DWARF information collected for the compilation unit.
7689    This routine is a debugging aid only.  */
7690
7691 void
7692 debug_dwarf (void)
7693 {
7694   print_indent = 0;
7695   print_die (comp_unit_die, stderr);
7696   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7697     print_dwarf_line_table (stderr);
7698 }
7699 \f
7700 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7701    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7702    DIE that marks the start of the DIEs for this include file.  */
7703
7704 static dw_die_ref
7705 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7706 {
7707   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7708   dw_die_ref new_unit = gen_compile_unit_die (filename);
7709
7710   new_unit->die_sib = old_unit;
7711   return new_unit;
7712 }
7713
7714 /* Close an include-file CU and reopen the enclosing one.  */
7715
7716 static dw_die_ref
7717 pop_compile_unit (dw_die_ref old_unit)
7718 {
7719   dw_die_ref new_unit = old_unit->die_sib;
7720
7721   old_unit->die_sib = NULL;
7722   return new_unit;
7723 }
7724
7725 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7726 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7727
7728 /* Calculate the checksum of a location expression.  */
7729
7730 static inline void
7731 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7732 {
7733   int tem;
7734
7735   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7736   CHECKSUM (tem);
7737   CHECKSUM (loc->dw_loc_oprnd1);
7738   CHECKSUM (loc->dw_loc_oprnd2);
7739 }
7740
7741 /* Calculate the checksum of an attribute.  */
7742
7743 static void
7744 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7745 {
7746   dw_loc_descr_ref loc;
7747   rtx r;
7748
7749   CHECKSUM (at->dw_attr);
7750
7751   /* We don't care that this was compiled with a different compiler
7752      snapshot; if the output is the same, that's what matters.  */
7753   if (at->dw_attr == DW_AT_producer)
7754     return;
7755
7756   switch (AT_class (at))
7757     {
7758     case dw_val_class_const:
7759       CHECKSUM (at->dw_attr_val.v.val_int);
7760       break;
7761     case dw_val_class_unsigned_const:
7762       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7763       break;
7764     case dw_val_class_long_long:
7765       CHECKSUM (CONST_DOUBLE_HIGH (at->dw_attr_val.v.val_long_long));
7766       CHECKSUM (CONST_DOUBLE_LOW (at->dw_attr_val.v.val_long_long));
7767       break;
7768     case dw_val_class_vec:
7769       CHECKSUM (at->dw_attr_val.v.val_vec);
7770       break;
7771     case dw_val_class_flag:
7772       CHECKSUM (at->dw_attr_val.v.val_flag);
7773       break;
7774     case dw_val_class_str:
7775       CHECKSUM_STRING (AT_string (at));
7776       break;
7777
7778     case dw_val_class_addr:
7779       r = AT_addr (at);
7780       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7781       CHECKSUM_STRING (XSTR (r, 0));
7782       break;
7783
7784     case dw_val_class_offset:
7785       CHECKSUM (at->dw_attr_val.v.val_offset);
7786       break;
7787
7788     case dw_val_class_loc:
7789       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7790         loc_checksum (loc, ctx);
7791       break;
7792
7793     case dw_val_class_die_ref:
7794       die_checksum (AT_ref (at), ctx, mark);
7795       break;
7796
7797     case dw_val_class_fde_ref:
7798     case dw_val_class_lbl_id:
7799     case dw_val_class_lineptr:
7800     case dw_val_class_macptr:
7801       break;
7802
7803     case dw_val_class_file:
7804       CHECKSUM_STRING (AT_file (at)->filename);
7805       break;
7806
7807     default:
7808       break;
7809     }
7810 }
7811
7812 /* Calculate the checksum of a DIE.  */
7813
7814 static void
7815 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7816 {
7817   dw_die_ref c;
7818   dw_attr_ref a;
7819   unsigned ix;
7820
7821   /* To avoid infinite recursion.  */
7822   if (die->die_mark)
7823     {
7824       CHECKSUM (die->die_mark);
7825       return;
7826     }
7827   die->die_mark = ++(*mark);
7828
7829   CHECKSUM (die->die_tag);
7830
7831   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7832     attr_checksum (a, ctx, mark);
7833
7834   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7835 }
7836
7837 #undef CHECKSUM
7838 #undef CHECKSUM_STRING
7839
7840 /* Do the location expressions look same?  */
7841 static inline int
7842 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7843 {
7844   return loc1->dw_loc_opc == loc2->dw_loc_opc
7845          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7846          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7847 }
7848
7849 /* Do the values look the same?  */
7850 static int
7851 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7852 {
7853   dw_loc_descr_ref loc1, loc2;
7854   rtx r1, r2;
7855
7856   if (v1->val_class != v2->val_class)
7857     return 0;
7858
7859   switch (v1->val_class)
7860     {
7861     case dw_val_class_const:
7862       return v1->v.val_int == v2->v.val_int;
7863     case dw_val_class_unsigned_const:
7864       return v1->v.val_unsigned == v2->v.val_unsigned;
7865     case dw_val_class_long_long:
7866       return CONST_DOUBLE_HIGH (v1->v.val_long_long)
7867              == CONST_DOUBLE_HIGH (v2->v.val_long_long)
7868              && CONST_DOUBLE_LOW (v1->v.val_long_long)
7869                 == CONST_DOUBLE_LOW (v2->v.val_long_long);
7870     case dw_val_class_vec:
7871       if (v1->v.val_vec.length != v2->v.val_vec.length
7872           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7873         return 0;
7874       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7875                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7876         return 0;
7877       return 1;
7878     case dw_val_class_flag:
7879       return v1->v.val_flag == v2->v.val_flag;
7880     case dw_val_class_str:
7881       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7882
7883     case dw_val_class_addr:
7884       r1 = v1->v.val_addr;
7885       r2 = v2->v.val_addr;
7886       if (GET_CODE (r1) != GET_CODE (r2))
7887         return 0;
7888       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7889       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7890
7891     case dw_val_class_offset:
7892       return v1->v.val_offset == v2->v.val_offset;
7893
7894     case dw_val_class_loc:
7895       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7896            loc1 && loc2;
7897            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7898         if (!same_loc_p (loc1, loc2, mark))
7899           return 0;
7900       return !loc1 && !loc2;
7901
7902     case dw_val_class_die_ref:
7903       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7904
7905     case dw_val_class_fde_ref:
7906     case dw_val_class_lbl_id:
7907     case dw_val_class_lineptr:
7908     case dw_val_class_macptr:
7909       return 1;
7910
7911     case dw_val_class_file:
7912       return v1->v.val_file == v2->v.val_file;
7913
7914     default:
7915       return 1;
7916     }
7917 }
7918
7919 /* Do the attributes look the same?  */
7920
7921 static int
7922 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7923 {
7924   if (at1->dw_attr != at2->dw_attr)
7925     return 0;
7926
7927   /* We don't care that this was compiled with a different compiler
7928      snapshot; if the output is the same, that's what matters. */
7929   if (at1->dw_attr == DW_AT_producer)
7930     return 1;
7931
7932   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7933 }
7934
7935 /* Do the dies look the same?  */
7936
7937 static int
7938 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7939 {
7940   dw_die_ref c1, c2;
7941   dw_attr_ref a1;
7942   unsigned ix;
7943
7944   /* To avoid infinite recursion.  */
7945   if (die1->die_mark)
7946     return die1->die_mark == die2->die_mark;
7947   die1->die_mark = die2->die_mark = ++(*mark);
7948
7949   if (die1->die_tag != die2->die_tag)
7950     return 0;
7951
7952   if (VEC_length (dw_attr_node, die1->die_attr)
7953       != VEC_length (dw_attr_node, die2->die_attr))
7954     return 0;
7955
7956   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7957     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7958       return 0;
7959
7960   c1 = die1->die_child;
7961   c2 = die2->die_child;
7962   if (! c1)
7963     {
7964       if (c2)
7965         return 0;
7966     }
7967   else
7968     for (;;)
7969       {
7970         if (!same_die_p (c1, c2, mark))
7971           return 0;
7972         c1 = c1->die_sib;
7973         c2 = c2->die_sib;
7974         if (c1 == die1->die_child)
7975           {
7976             if (c2 == die2->die_child)
7977               break;
7978             else
7979               return 0;
7980           }
7981     }
7982
7983   return 1;
7984 }
7985
7986 /* Do the dies look the same?  Wrapper around same_die_p.  */
7987
7988 static int
7989 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7990 {
7991   int mark = 0;
7992   int ret = same_die_p (die1, die2, &mark);
7993
7994   unmark_all_dies (die1);
7995   unmark_all_dies (die2);
7996
7997   return ret;
7998 }
7999
8000 /* The prefix to attach to symbols on DIEs in the current comdat debug
8001    info section.  */
8002 static char *comdat_symbol_id;
8003
8004 /* The index of the current symbol within the current comdat CU.  */
8005 static unsigned int comdat_symbol_number;
8006
8007 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8008    children, and set comdat_symbol_id accordingly.  */
8009
8010 static void
8011 compute_section_prefix (dw_die_ref unit_die)
8012 {
8013   const char *die_name = get_AT_string (unit_die, DW_AT_name);
8014   const char *base = die_name ? lbasename (die_name) : "anonymous";
8015   char *name = XALLOCAVEC (char, strlen (base) + 64);
8016   char *p;
8017   int i, mark;
8018   unsigned char checksum[16];
8019   struct md5_ctx ctx;
8020
8021   /* Compute the checksum of the DIE, then append part of it as hex digits to
8022      the name filename of the unit.  */
8023
8024   md5_init_ctx (&ctx);
8025   mark = 0;
8026   die_checksum (unit_die, &ctx, &mark);
8027   unmark_all_dies (unit_die);
8028   md5_finish_ctx (&ctx, checksum);
8029
8030   sprintf (name, "%s.", base);
8031   clean_symbol_name (name);
8032
8033   p = name + strlen (name);
8034   for (i = 0; i < 4; i++)
8035     {
8036       sprintf (p, "%.2x", checksum[i]);
8037       p += 2;
8038     }
8039
8040   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
8041   comdat_symbol_number = 0;
8042 }
8043
8044 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
8045
8046 static int
8047 is_type_die (dw_die_ref die)
8048 {
8049   switch (die->die_tag)
8050     {
8051     case DW_TAG_array_type:
8052     case DW_TAG_class_type:
8053     case DW_TAG_interface_type:
8054     case DW_TAG_enumeration_type:
8055     case DW_TAG_pointer_type:
8056     case DW_TAG_reference_type:
8057     case DW_TAG_string_type:
8058     case DW_TAG_structure_type:
8059     case DW_TAG_subroutine_type:
8060     case DW_TAG_union_type:
8061     case DW_TAG_ptr_to_member_type:
8062     case DW_TAG_set_type:
8063     case DW_TAG_subrange_type:
8064     case DW_TAG_base_type:
8065     case DW_TAG_const_type:
8066     case DW_TAG_file_type:
8067     case DW_TAG_packed_type:
8068     case DW_TAG_volatile_type:
8069     case DW_TAG_typedef:
8070       return 1;
8071     default:
8072       return 0;
8073     }
8074 }
8075
8076 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
8077    Basically, we want to choose the bits that are likely to be shared between
8078    compilations (types) and leave out the bits that are specific to individual
8079    compilations (functions).  */
8080
8081 static int
8082 is_comdat_die (dw_die_ref c)
8083 {
8084   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
8085      we do for stabs.  The advantage is a greater likelihood of sharing between
8086      objects that don't include headers in the same order (and therefore would
8087      put the base types in a different comdat).  jason 8/28/00 */
8088
8089   if (c->die_tag == DW_TAG_base_type)
8090     return 0;
8091
8092   if (c->die_tag == DW_TAG_pointer_type
8093       || c->die_tag == DW_TAG_reference_type
8094       || c->die_tag == DW_TAG_const_type
8095       || c->die_tag == DW_TAG_volatile_type)
8096     {
8097       dw_die_ref t = get_AT_ref (c, DW_AT_type);
8098
8099       return t ? is_comdat_die (t) : 0;
8100     }
8101
8102   return is_type_die (c);
8103 }
8104
8105 /* Returns 1 iff C is the sort of DIE that might be referred to from another
8106    compilation unit.  */
8107
8108 static int
8109 is_symbol_die (dw_die_ref c)
8110 {
8111   return (is_type_die (c)
8112           || (get_AT (c, DW_AT_declaration)
8113               && !get_AT (c, DW_AT_specification))
8114           || c->die_tag == DW_TAG_namespace
8115           || c->die_tag == DW_TAG_module);
8116 }
8117
8118 static char *
8119 gen_internal_sym (const char *prefix)
8120 {
8121   char buf[256];
8122
8123   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8124   return xstrdup (buf);
8125 }
8126
8127 /* Assign symbols to all worthy DIEs under DIE.  */
8128
8129 static void
8130 assign_symbol_names (dw_die_ref die)
8131 {
8132   dw_die_ref c;
8133
8134   if (is_symbol_die (die))
8135     {
8136       if (comdat_symbol_id)
8137         {
8138           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
8139
8140           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
8141                    comdat_symbol_id, comdat_symbol_number++);
8142           die->die_symbol = xstrdup (p);
8143         }
8144       else
8145         die->die_symbol = gen_internal_sym ("LDIE");
8146     }
8147
8148   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
8149 }
8150
8151 struct cu_hash_table_entry
8152 {
8153   dw_die_ref cu;
8154   unsigned min_comdat_num, max_comdat_num;
8155   struct cu_hash_table_entry *next;
8156 };
8157
8158 /* Routines to manipulate hash table of CUs.  */
8159 static hashval_t
8160 htab_cu_hash (const void *of)
8161 {
8162   const struct cu_hash_table_entry *const entry =
8163     (const struct cu_hash_table_entry *) of;
8164
8165   return htab_hash_string (entry->cu->die_symbol);
8166 }
8167
8168 static int
8169 htab_cu_eq (const void *of1, const void *of2)
8170 {
8171   const struct cu_hash_table_entry *const entry1 =
8172     (const struct cu_hash_table_entry *) of1;
8173   const struct die_struct *const entry2 = (const struct die_struct *) of2;
8174
8175   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
8176 }
8177
8178 static void
8179 htab_cu_del (void *what)
8180 {
8181   struct cu_hash_table_entry *next,
8182     *entry = (struct cu_hash_table_entry *) what;
8183
8184   while (entry)
8185     {
8186       next = entry->next;
8187       free (entry);
8188       entry = next;
8189     }
8190 }
8191
8192 /* Check whether we have already seen this CU and set up SYM_NUM
8193    accordingly.  */
8194 static int
8195 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
8196 {
8197   struct cu_hash_table_entry dummy;
8198   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
8199
8200   dummy.max_comdat_num = 0;
8201
8202   slot = (struct cu_hash_table_entry **)
8203     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8204         INSERT);
8205   entry = *slot;
8206
8207   for (; entry; last = entry, entry = entry->next)
8208     {
8209       if (same_die_p_wrap (cu, entry->cu))
8210         break;
8211     }
8212
8213   if (entry)
8214     {
8215       *sym_num = entry->min_comdat_num;
8216       return 1;
8217     }
8218
8219   entry = XCNEW (struct cu_hash_table_entry);
8220   entry->cu = cu;
8221   entry->min_comdat_num = *sym_num = last->max_comdat_num;
8222   entry->next = *slot;
8223   *slot = entry;
8224
8225   return 0;
8226 }
8227
8228 /* Record SYM_NUM to record of CU in HTABLE.  */
8229 static void
8230 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
8231 {
8232   struct cu_hash_table_entry **slot, *entry;
8233
8234   slot = (struct cu_hash_table_entry **)
8235     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8236         NO_INSERT);
8237   entry = *slot;
8238
8239   entry->max_comdat_num = sym_num;
8240 }
8241
8242 /* Traverse the DIE (which is always comp_unit_die), and set up
8243    additional compilation units for each of the include files we see
8244    bracketed by BINCL/EINCL.  */
8245
8246 static void
8247 break_out_includes (dw_die_ref die)
8248 {
8249   dw_die_ref c;
8250   dw_die_ref unit = NULL;
8251   limbo_die_node *node, **pnode;
8252   htab_t cu_hash_table;
8253
8254   c = die->die_child;
8255   if (c) do {
8256     dw_die_ref prev = c;
8257     c = c->die_sib;
8258     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
8259            || (unit && is_comdat_die (c)))
8260       {
8261         dw_die_ref next = c->die_sib;
8262
8263         /* This DIE is for a secondary CU; remove it from the main one.  */
8264         remove_child_with_prev (c, prev);
8265
8266         if (c->die_tag == DW_TAG_GNU_BINCL)
8267           unit = push_new_compile_unit (unit, c);
8268         else if (c->die_tag == DW_TAG_GNU_EINCL)
8269           unit = pop_compile_unit (unit);
8270         else
8271           add_child_die (unit, c);
8272         c = next;
8273         if (c == die->die_child)
8274           break;
8275       }
8276   } while (c != die->die_child);
8277
8278 #if 0
8279   /* We can only use this in debugging, since the frontend doesn't check
8280      to make sure that we leave every include file we enter.  */
8281   gcc_assert (!unit);
8282 #endif
8283
8284   assign_symbol_names (die);
8285   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
8286   for (node = limbo_die_list, pnode = &limbo_die_list;
8287        node;
8288        node = node->next)
8289     {
8290       int is_dupl;
8291
8292       compute_section_prefix (node->die);
8293       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
8294                         &comdat_symbol_number);
8295       assign_symbol_names (node->die);
8296       if (is_dupl)
8297         *pnode = node->next;
8298       else
8299         {
8300           pnode = &node->next;
8301           record_comdat_symbol_number (node->die, cu_hash_table,
8302                 comdat_symbol_number);
8303         }
8304     }
8305   htab_delete (cu_hash_table);
8306 }
8307
8308 /* Traverse the DIE and add a sibling attribute if it may have the
8309    effect of speeding up access to siblings.  To save some space,
8310    avoid generating sibling attributes for DIE's without children.  */
8311
8312 static void
8313 add_sibling_attributes (dw_die_ref die)
8314 {
8315   dw_die_ref c;
8316
8317   if (! die->die_child)
8318     return;
8319
8320   if (die->die_parent && die != die->die_parent->die_child)
8321     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8322
8323   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8324 }
8325
8326 /* Output all location lists for the DIE and its children.  */
8327
8328 static void
8329 output_location_lists (dw_die_ref die)
8330 {
8331   dw_die_ref c;
8332   dw_attr_ref a;
8333   unsigned ix;
8334
8335   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8336     if (AT_class (a) == dw_val_class_loc_list)
8337       output_loc_list (AT_loc_list (a));
8338
8339   FOR_EACH_CHILD (die, c, output_location_lists (c));
8340 }
8341
8342 /* The format of each DIE (and its attribute value pairs) is encoded in an
8343    abbreviation table.  This routine builds the abbreviation table and assigns
8344    a unique abbreviation id for each abbreviation entry.  The children of each
8345    die are visited recursively.  */
8346
8347 static void
8348 build_abbrev_table (dw_die_ref die)
8349 {
8350   unsigned long abbrev_id;
8351   unsigned int n_alloc;
8352   dw_die_ref c;
8353   dw_attr_ref a;
8354   unsigned ix;
8355
8356   /* Scan the DIE references, and mark as external any that refer to
8357      DIEs from other CUs (i.e. those which are not marked).  */
8358   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8359     if (AT_class (a) == dw_val_class_die_ref
8360         && AT_ref (a)->die_mark == 0)
8361       {
8362         gcc_assert (AT_ref (a)->die_symbol);
8363         set_AT_ref_external (a, 1);
8364       }
8365
8366   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8367     {
8368       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8369       dw_attr_ref die_a, abbrev_a;
8370       unsigned ix;
8371       bool ok = true;
8372
8373       if (abbrev->die_tag != die->die_tag)
8374         continue;
8375       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8376         continue;
8377
8378       if (VEC_length (dw_attr_node, abbrev->die_attr)
8379           != VEC_length (dw_attr_node, die->die_attr))
8380         continue;
8381
8382       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
8383         {
8384           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8385           if ((abbrev_a->dw_attr != die_a->dw_attr)
8386               || (value_format (abbrev_a) != value_format (die_a)))
8387             {
8388               ok = false;
8389               break;
8390             }
8391         }
8392       if (ok)
8393         break;
8394     }
8395
8396   if (abbrev_id >= abbrev_die_table_in_use)
8397     {
8398       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8399         {
8400           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8401           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8402                                             n_alloc);
8403
8404           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8405                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8406           abbrev_die_table_allocated = n_alloc;
8407         }
8408
8409       ++abbrev_die_table_in_use;
8410       abbrev_die_table[abbrev_id] = die;
8411     }
8412
8413   die->die_abbrev = abbrev_id;
8414   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8415 }
8416 \f
8417 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8418
8419 static int
8420 constant_size (unsigned HOST_WIDE_INT value)
8421 {
8422   int log;
8423
8424   if (value == 0)
8425     log = 0;
8426   else
8427     log = floor_log2 (value);
8428
8429   log = log / 8;
8430   log = 1 << (floor_log2 (log) + 1);
8431
8432   return log;
8433 }
8434
8435 /* Return the size of a DIE as it is represented in the
8436    .debug_info section.  */
8437
8438 static unsigned long
8439 size_of_die (dw_die_ref die)
8440 {
8441   unsigned long size = 0;
8442   dw_attr_ref a;
8443   unsigned ix;
8444
8445   size += size_of_uleb128 (die->die_abbrev);
8446   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8447     {
8448       switch (AT_class (a))
8449         {
8450         case dw_val_class_addr:
8451           size += DWARF2_ADDR_SIZE;
8452           break;
8453         case dw_val_class_offset:
8454           size += DWARF_OFFSET_SIZE;
8455           break;
8456         case dw_val_class_loc:
8457           {
8458             unsigned long lsize = size_of_locs (AT_loc (a));
8459
8460             /* Block length.  */
8461             size += constant_size (lsize);
8462             size += lsize;
8463           }
8464           break;
8465         case dw_val_class_loc_list:
8466           size += DWARF_OFFSET_SIZE;
8467           break;
8468         case dw_val_class_range_list:
8469           size += DWARF_OFFSET_SIZE;
8470           break;
8471         case dw_val_class_const:
8472           size += size_of_sleb128 (AT_int (a));
8473           break;
8474         case dw_val_class_unsigned_const:
8475           size += constant_size (AT_unsigned (a));
8476           break;
8477         case dw_val_class_long_long:
8478           size += 1 + 2*HOST_BITS_PER_WIDE_INT/HOST_BITS_PER_CHAR; /* block */
8479           break;
8480         case dw_val_class_vec:
8481           size += constant_size (a->dw_attr_val.v.val_vec.length
8482                                  * a->dw_attr_val.v.val_vec.elt_size)
8483                   + a->dw_attr_val.v.val_vec.length
8484                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8485           break;
8486         case dw_val_class_flag:
8487           size += 1;
8488           break;
8489         case dw_val_class_die_ref:
8490           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8491              whereas in DWARF3 it's always sized as an offset.  */
8492           if (AT_ref_external (a) && dwarf_version == 2)
8493             size += DWARF2_ADDR_SIZE;
8494           else
8495             size += DWARF_OFFSET_SIZE;
8496           break;
8497         case dw_val_class_fde_ref:
8498           size += DWARF_OFFSET_SIZE;
8499           break;
8500         case dw_val_class_lbl_id:
8501           size += DWARF2_ADDR_SIZE;
8502           break;
8503         case dw_val_class_lineptr:
8504         case dw_val_class_macptr:
8505           size += DWARF_OFFSET_SIZE;
8506           break;
8507         case dw_val_class_str:
8508           if (AT_string_form (a) == DW_FORM_strp)
8509             size += DWARF_OFFSET_SIZE;
8510           else
8511             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8512           break;
8513         case dw_val_class_file:
8514           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8515           break;
8516         default:
8517           gcc_unreachable ();
8518         }
8519     }
8520
8521   return size;
8522 }
8523
8524 /* Size the debugging information associated with a given DIE.  Visits the
8525    DIE's children recursively.  Updates the global variable next_die_offset, on
8526    each time through.  Uses the current value of next_die_offset to update the
8527    die_offset field in each DIE.  */
8528
8529 static void
8530 calc_die_sizes (dw_die_ref die)
8531 {
8532   dw_die_ref c;
8533
8534   die->die_offset = next_die_offset;
8535   next_die_offset += size_of_die (die);
8536
8537   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8538
8539   if (die->die_child != NULL)
8540     /* Count the null byte used to terminate sibling lists.  */
8541     next_die_offset += 1;
8542 }
8543
8544 /* Set the marks for a die and its children.  We do this so
8545    that we know whether or not a reference needs to use FORM_ref_addr; only
8546    DIEs in the same CU will be marked.  We used to clear out the offset
8547    and use that as the flag, but ran into ordering problems.  */
8548
8549 static void
8550 mark_dies (dw_die_ref die)
8551 {
8552   dw_die_ref c;
8553
8554   gcc_assert (!die->die_mark);
8555
8556   die->die_mark = 1;
8557   FOR_EACH_CHILD (die, c, mark_dies (c));
8558 }
8559
8560 /* Clear the marks for a die and its children.  */
8561
8562 static void
8563 unmark_dies (dw_die_ref die)
8564 {
8565   dw_die_ref c;
8566
8567   gcc_assert (die->die_mark);
8568
8569   die->die_mark = 0;
8570   FOR_EACH_CHILD (die, c, unmark_dies (c));
8571 }
8572
8573 /* Clear the marks for a die, its children and referred dies.  */
8574
8575 static void
8576 unmark_all_dies (dw_die_ref die)
8577 {
8578   dw_die_ref c;
8579   dw_attr_ref a;
8580   unsigned ix;
8581
8582   if (!die->die_mark)
8583     return;
8584   die->die_mark = 0;
8585
8586   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8587
8588   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8589     if (AT_class (a) == dw_val_class_die_ref)
8590       unmark_all_dies (AT_ref (a));
8591 }
8592
8593 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8594    generated for the compilation unit.  */
8595
8596 static unsigned long
8597 size_of_pubnames (VEC (pubname_entry, gc) * names)
8598 {
8599   unsigned long size;
8600   unsigned i;
8601   pubname_ref p;
8602
8603   size = DWARF_PUBNAMES_HEADER_SIZE;
8604   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8605     if (names != pubtype_table
8606         || p->die->die_offset != 0
8607         || !flag_eliminate_unused_debug_types)
8608       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8609
8610   size += DWARF_OFFSET_SIZE;
8611   return size;
8612 }
8613
8614 /* Return the size of the information in the .debug_aranges section.  */
8615
8616 static unsigned long
8617 size_of_aranges (void)
8618 {
8619   unsigned long size;
8620
8621   size = DWARF_ARANGES_HEADER_SIZE;
8622
8623   /* Count the address/length pair for this compilation unit.  */
8624   if (text_section_used)
8625     size += 2 * DWARF2_ADDR_SIZE;
8626   if (cold_text_section_used)
8627     size += 2 * DWARF2_ADDR_SIZE;
8628   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8629
8630   /* Count the two zero words used to terminated the address range table.  */
8631   size += 2 * DWARF2_ADDR_SIZE;
8632   return size;
8633 }
8634 \f
8635 /* Select the encoding of an attribute value.  */
8636
8637 static enum dwarf_form
8638 value_format (dw_attr_ref a)
8639 {
8640   switch (a->dw_attr_val.val_class)
8641     {
8642     case dw_val_class_addr:
8643       return DW_FORM_addr;
8644     case dw_val_class_range_list:
8645     case dw_val_class_offset:
8646     case dw_val_class_loc_list:
8647       switch (DWARF_OFFSET_SIZE)
8648         {
8649         case 4:
8650           return DW_FORM_data4;
8651         case 8:
8652           return DW_FORM_data8;
8653         default:
8654           gcc_unreachable ();
8655         }
8656     case dw_val_class_loc:
8657       switch (constant_size (size_of_locs (AT_loc (a))))
8658         {
8659         case 1:
8660           return DW_FORM_block1;
8661         case 2:
8662           return DW_FORM_block2;
8663         default:
8664           gcc_unreachable ();
8665         }
8666     case dw_val_class_const:
8667       return DW_FORM_sdata;
8668     case dw_val_class_unsigned_const:
8669       switch (constant_size (AT_unsigned (a)))
8670         {
8671         case 1:
8672           return DW_FORM_data1;
8673         case 2:
8674           return DW_FORM_data2;
8675         case 4:
8676           return DW_FORM_data4;
8677         case 8:
8678           return DW_FORM_data8;
8679         default:
8680           gcc_unreachable ();
8681         }
8682     case dw_val_class_long_long:
8683       return DW_FORM_block1;
8684     case dw_val_class_vec:
8685       switch (constant_size (a->dw_attr_val.v.val_vec.length
8686                              * a->dw_attr_val.v.val_vec.elt_size))
8687         {
8688         case 1:
8689           return DW_FORM_block1;
8690         case 2:
8691           return DW_FORM_block2;
8692         case 4:
8693           return DW_FORM_block4;
8694         default:
8695           gcc_unreachable ();
8696         }
8697     case dw_val_class_flag:
8698       return DW_FORM_flag;
8699     case dw_val_class_die_ref:
8700       if (AT_ref_external (a))
8701         return DW_FORM_ref_addr;
8702       else
8703         return DW_FORM_ref;
8704     case dw_val_class_fde_ref:
8705       return DW_FORM_data;
8706     case dw_val_class_lbl_id:
8707       return DW_FORM_addr;
8708     case dw_val_class_lineptr:
8709     case dw_val_class_macptr:
8710       return DW_FORM_data;
8711     case dw_val_class_str:
8712       return AT_string_form (a);
8713     case dw_val_class_file:
8714       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8715         {
8716         case 1:
8717           return DW_FORM_data1;
8718         case 2:
8719           return DW_FORM_data2;
8720         case 4:
8721           return DW_FORM_data4;
8722         default:
8723           gcc_unreachable ();
8724         }
8725
8726     default:
8727       gcc_unreachable ();
8728     }
8729 }
8730
8731 /* Output the encoding of an attribute value.  */
8732
8733 static void
8734 output_value_format (dw_attr_ref a)
8735 {
8736   enum dwarf_form form = value_format (a);
8737
8738   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8739 }
8740
8741 /* Output the .debug_abbrev section which defines the DIE abbreviation
8742    table.  */
8743
8744 static void
8745 output_abbrev_section (void)
8746 {
8747   unsigned long abbrev_id;
8748
8749   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8750     {
8751       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8752       unsigned ix;
8753       dw_attr_ref a_attr;
8754
8755       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8756       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8757                                    dwarf_tag_name (abbrev->die_tag));
8758
8759       if (abbrev->die_child != NULL)
8760         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8761       else
8762         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8763
8764       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8765            ix++)
8766         {
8767           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8768                                        dwarf_attr_name (a_attr->dw_attr));
8769           output_value_format (a_attr);
8770         }
8771
8772       dw2_asm_output_data (1, 0, NULL);
8773       dw2_asm_output_data (1, 0, NULL);
8774     }
8775
8776   /* Terminate the table.  */
8777   dw2_asm_output_data (1, 0, NULL);
8778 }
8779
8780 /* Output a symbol we can use to refer to this DIE from another CU.  */
8781
8782 static inline void
8783 output_die_symbol (dw_die_ref die)
8784 {
8785   char *sym = die->die_symbol;
8786
8787   if (sym == 0)
8788     return;
8789
8790   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8791     /* We make these global, not weak; if the target doesn't support
8792        .linkonce, it doesn't support combining the sections, so debugging
8793        will break.  */
8794     targetm.asm_out.globalize_label (asm_out_file, sym);
8795
8796   ASM_OUTPUT_LABEL (asm_out_file, sym);
8797 }
8798
8799 /* Return a new location list, given the begin and end range, and the
8800    expression. gensym tells us whether to generate a new internal symbol for
8801    this location list node, which is done for the head of the list only.  */
8802
8803 static inline dw_loc_list_ref
8804 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8805               const char *section, unsigned int gensym)
8806 {
8807   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8808
8809   retlist->begin = begin;
8810   retlist->end = end;
8811   retlist->expr = expr;
8812   retlist->section = section;
8813   if (gensym)
8814     retlist->ll_symbol = gen_internal_sym ("LLST");
8815
8816   return retlist;
8817 }
8818
8819 /* Add a location description expression to a location list.  */
8820
8821 static inline void
8822 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8823                            const char *begin, const char *end,
8824                            const char *section)
8825 {
8826   dw_loc_list_ref *d;
8827
8828   /* Find the end of the chain.  */
8829   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8830     ;
8831
8832   /* Add a new location list node to the list.  */
8833   *d = new_loc_list (descr, begin, end, section, 0);
8834 }
8835
8836 /* Output the location list given to us.  */
8837
8838 static void
8839 output_loc_list (dw_loc_list_ref list_head)
8840 {
8841   dw_loc_list_ref curr = list_head;
8842
8843   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8844
8845   /* Walk the location list, and output each range + expression.  */
8846   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8847     {
8848       unsigned long size;
8849       /* Don't output an entry that starts and ends at the same address.  */
8850       if (strcmp (curr->begin, curr->end) == 0)
8851         continue;
8852       if (!have_multiple_function_sections)
8853         {
8854           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8855                                 "Location list begin address (%s)",
8856                                 list_head->ll_symbol);
8857           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8858                                 "Location list end address (%s)",
8859                                 list_head->ll_symbol);
8860         }
8861       else
8862         {
8863           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8864                                "Location list begin address (%s)",
8865                                list_head->ll_symbol);
8866           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8867                                "Location list end address (%s)",
8868                                list_head->ll_symbol);
8869         }
8870       size = size_of_locs (curr->expr);
8871
8872       /* Output the block length for this list of location operations.  */
8873       gcc_assert (size <= 0xffff);
8874       dw2_asm_output_data (2, size, "%s", "Location expression size");
8875
8876       output_loc_sequence (curr->expr);
8877     }
8878
8879   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8880                        "Location list terminator begin (%s)",
8881                        list_head->ll_symbol);
8882   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8883                        "Location list terminator end (%s)",
8884                        list_head->ll_symbol);
8885 }
8886
8887 /* Output the DIE and its attributes.  Called recursively to generate
8888    the definitions of each child DIE.  */
8889
8890 static void
8891 output_die (dw_die_ref die)
8892 {
8893   dw_attr_ref a;
8894   dw_die_ref c;
8895   unsigned long size;
8896   unsigned ix;
8897
8898   /* If someone in another CU might refer to us, set up a symbol for
8899      them to point to.  */
8900   if (die->die_symbol)
8901     output_die_symbol (die);
8902
8903   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8904                                (unsigned long)die->die_offset,
8905                                dwarf_tag_name (die->die_tag));
8906
8907   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8908     {
8909       const char *name = dwarf_attr_name (a->dw_attr);
8910
8911       switch (AT_class (a))
8912         {
8913         case dw_val_class_addr:
8914           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8915           break;
8916
8917         case dw_val_class_offset:
8918           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8919                                "%s", name);
8920           break;
8921
8922         case dw_val_class_range_list:
8923           {
8924             char *p = strchr (ranges_section_label, '\0');
8925
8926             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8927                      a->dw_attr_val.v.val_offset);
8928             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8929                                    debug_ranges_section, "%s", name);
8930             *p = '\0';
8931           }
8932           break;
8933
8934         case dw_val_class_loc:
8935           size = size_of_locs (AT_loc (a));
8936
8937           /* Output the block length for this list of location operations.  */
8938           dw2_asm_output_data (constant_size (size), size, "%s", name);
8939
8940           output_loc_sequence (AT_loc (a));
8941           break;
8942
8943         case dw_val_class_const:
8944           /* ??? It would be slightly more efficient to use a scheme like is
8945              used for unsigned constants below, but gdb 4.x does not sign
8946              extend.  Gdb 5.x does sign extend.  */
8947           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8948           break;
8949
8950         case dw_val_class_unsigned_const:
8951           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8952                                AT_unsigned (a), "%s", name);
8953           break;
8954
8955         case dw_val_class_long_long:
8956           {
8957             unsigned HOST_WIDE_INT first, second;
8958
8959             dw2_asm_output_data (1,
8960                                  2 * HOST_BITS_PER_WIDE_INT
8961                                  / HOST_BITS_PER_CHAR,
8962                                  "%s", name);
8963
8964             if (WORDS_BIG_ENDIAN)
8965               {
8966                 first = CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long);
8967                 second = CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long);
8968               }
8969             else
8970               {
8971                 first = CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long);
8972                 second = CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long);
8973               }
8974
8975             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8976                                  first, "long long constant");
8977             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8978                                  second, NULL);
8979           }
8980           break;
8981
8982         case dw_val_class_vec:
8983           {
8984             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8985             unsigned int len = a->dw_attr_val.v.val_vec.length;
8986             unsigned int i;
8987             unsigned char *p;
8988
8989             dw2_asm_output_data (constant_size (len * elt_size),
8990                                  len * elt_size, "%s", name);
8991             if (elt_size > sizeof (HOST_WIDE_INT))
8992               {
8993                 elt_size /= 2;
8994                 len *= 2;
8995               }
8996             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8997                  i < len;
8998                  i++, p += elt_size)
8999               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9000                                    "fp or vector constant word %u", i);
9001             break;
9002           }
9003
9004         case dw_val_class_flag:
9005           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9006           break;
9007
9008         case dw_val_class_loc_list:
9009           {
9010             char *sym = AT_loc_list (a)->ll_symbol;
9011
9012             gcc_assert (sym);
9013             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9014                                    "%s", name);
9015           }
9016           break;
9017
9018         case dw_val_class_die_ref:
9019           if (AT_ref_external (a))
9020             {
9021               char *sym = AT_ref (a)->die_symbol;
9022               int size;
9023
9024               gcc_assert (sym);
9025
9026               /* In DWARF2, DW_FORM_ref_addr is sized by target address
9027                  length, whereas in DWARF3 it's always sized as an offset.  */
9028               if (dwarf_version == 2)
9029                 size = DWARF2_ADDR_SIZE;
9030               else
9031                 size = DWARF_OFFSET_SIZE;
9032               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
9033             }
9034           else
9035             {
9036               gcc_assert (AT_ref (a)->die_offset);
9037               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9038                                    "%s", name);
9039             }
9040           break;
9041
9042         case dw_val_class_fde_ref:
9043           {
9044             char l1[20];
9045
9046             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9047                                          a->dw_attr_val.v.val_fde_index * 2);
9048             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9049                                    "%s", name);
9050           }
9051           break;
9052
9053         case dw_val_class_lbl_id:
9054           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9055           break;
9056
9057         case dw_val_class_lineptr:
9058           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9059                                  debug_line_section, "%s", name);
9060           break;
9061
9062         case dw_val_class_macptr:
9063           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9064                                  debug_macinfo_section, "%s", name);
9065           break;
9066
9067         case dw_val_class_str:
9068           if (AT_string_form (a) == DW_FORM_strp)
9069             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9070                                    a->dw_attr_val.v.val_str->label,
9071                                    debug_str_section,
9072                                    "%s: \"%s\"", name, AT_string (a));
9073           else
9074             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9075           break;
9076
9077         case dw_val_class_file:
9078           {
9079             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9080
9081             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9082                                  a->dw_attr_val.v.val_file->filename);
9083             break;
9084           }
9085
9086         default:
9087           gcc_unreachable ();
9088         }
9089     }
9090
9091   FOR_EACH_CHILD (die, c, output_die (c));
9092
9093   /* Add null byte to terminate sibling list.  */
9094   if (die->die_child != NULL)
9095     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
9096                          (unsigned long) die->die_offset);
9097 }
9098
9099 /* Output the compilation unit that appears at the beginning of the
9100    .debug_info section, and precedes the DIE descriptions.  */
9101
9102 static void
9103 output_compilation_unit_header (void)
9104 {
9105   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9106     dw2_asm_output_data (4, 0xffffffff,
9107       "Initial length escape value indicating 64-bit DWARF extension");
9108   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9109                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9110                        "Length of Compilation Unit Info");
9111   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9112   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9113                          debug_abbrev_section,
9114                          "Offset Into Abbrev. Section");
9115   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9116 }
9117
9118 /* Output the compilation unit DIE and its children.  */
9119
9120 static void
9121 output_comp_unit (dw_die_ref die, int output_if_empty)
9122 {
9123   const char *secname;
9124   char *oldsym, *tmp;
9125
9126   /* Unless we are outputting main CU, we may throw away empty ones.  */
9127   if (!output_if_empty && die->die_child == NULL)
9128     return;
9129
9130   /* Even if there are no children of this DIE, we must output the information
9131      about the compilation unit.  Otherwise, on an empty translation unit, we
9132      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9133      will then complain when examining the file.  First mark all the DIEs in
9134      this CU so we know which get local refs.  */
9135   mark_dies (die);
9136
9137   build_abbrev_table (die);
9138
9139   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9140   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9141   calc_die_sizes (die);
9142
9143   oldsym = die->die_symbol;
9144   if (oldsym)
9145     {
9146       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9147
9148       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9149       secname = tmp;
9150       die->die_symbol = NULL;
9151       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9152     }
9153   else
9154     switch_to_section (debug_info_section);
9155
9156   /* Output debugging information.  */
9157   output_compilation_unit_header ();
9158   output_die (die);
9159
9160   /* Leave the marks on the main CU, so we can check them in
9161      output_pubnames.  */
9162   if (oldsym)
9163     {
9164       unmark_dies (die);
9165       die->die_symbol = oldsym;
9166     }
9167 }
9168
9169 /* Return the DWARF2/3 pubname associated with a decl.  */
9170
9171 static const char *
9172 dwarf2_name (tree decl, int scope)
9173 {
9174   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9175 }
9176
9177 /* Add a new entry to .debug_pubnames if appropriate.  */
9178
9179 static void
9180 add_pubname_string (const char *str, dw_die_ref die)
9181 {
9182   pubname_entry e;
9183
9184   e.die = die;
9185   e.name = xstrdup (str);
9186   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9187 }
9188
9189 static void
9190 add_pubname (tree decl, dw_die_ref die)
9191 {
9192   if (TREE_PUBLIC (decl))
9193     add_pubname_string (dwarf2_name (decl, 1), die);
9194 }
9195
9196 /* Add a new entry to .debug_pubtypes if appropriate.  */
9197
9198 static void
9199 add_pubtype (tree decl, dw_die_ref die)
9200 {
9201   pubname_entry e;
9202
9203   e.name = NULL;
9204   if ((TREE_PUBLIC (decl)
9205        || die->die_parent == comp_unit_die)
9206       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9207     {
9208       e.die = die;
9209       if (TYPE_P (decl))
9210         {
9211           if (TYPE_NAME (decl))
9212             {
9213               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9214                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9215               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9216                        && DECL_NAME (TYPE_NAME (decl)))
9217                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9218               else
9219                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9220             }
9221         }
9222       else
9223         e.name = xstrdup (dwarf2_name (decl, 1));
9224
9225       /* If we don't have a name for the type, there's no point in adding
9226          it to the table.  */
9227       if (e.name && e.name[0] != '\0')
9228         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9229     }
9230 }
9231
9232 /* Output the public names table used to speed up access to externally
9233    visible names; or the public types table used to find type definitions.  */
9234
9235 static void
9236 output_pubnames (VEC (pubname_entry, gc) * names)
9237 {
9238   unsigned i;
9239   unsigned long pubnames_length = size_of_pubnames (names);
9240   pubname_ref pub;
9241
9242   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9243     dw2_asm_output_data (4, 0xffffffff,
9244       "Initial length escape value indicating 64-bit DWARF extension");
9245   if (names == pubname_table)
9246     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9247                          "Length of Public Names Info");
9248   else
9249     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9250                          "Length of Public Type Names Info");
9251   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
9252   dw2_asm_output_data (2, 2, "DWARF Version");
9253   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9254                          debug_info_section,
9255                          "Offset of Compilation Unit Info");
9256   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9257                        "Compilation Unit Length");
9258
9259   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
9260     {
9261       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9262       if (names == pubname_table)
9263         gcc_assert (pub->die->die_mark);
9264
9265       if (names != pubtype_table
9266           || pub->die->die_offset != 0
9267           || !flag_eliminate_unused_debug_types)
9268         {
9269           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9270                                "DIE offset");
9271
9272           dw2_asm_output_nstring (pub->name, -1, "external name");
9273         }
9274     }
9275
9276   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9277 }
9278
9279 /* Add a new entry to .debug_aranges if appropriate.  */
9280
9281 static void
9282 add_arange (tree decl, dw_die_ref die)
9283 {
9284   if (! DECL_SECTION_NAME (decl))
9285     return;
9286
9287   if (arange_table_in_use == arange_table_allocated)
9288     {
9289       arange_table_allocated += ARANGE_TABLE_INCREMENT;
9290       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
9291                                     arange_table_allocated);
9292       memset (arange_table + arange_table_in_use, 0,
9293               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
9294     }
9295
9296   arange_table[arange_table_in_use++] = die;
9297 }
9298
9299 /* Output the information that goes into the .debug_aranges table.
9300    Namely, define the beginning and ending address range of the
9301    text section generated for this compilation unit.  */
9302
9303 static void
9304 output_aranges (void)
9305 {
9306   unsigned i;
9307   unsigned long aranges_length = size_of_aranges ();
9308
9309   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9310     dw2_asm_output_data (4, 0xffffffff,
9311       "Initial length escape value indicating 64-bit DWARF extension");
9312   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9313                        "Length of Address Ranges Info");
9314   /* Version number for aranges is still 2, even in DWARF3.  */
9315   dw2_asm_output_data (2, 2, "DWARF Version");
9316   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9317                          debug_info_section,
9318                          "Offset of Compilation Unit Info");
9319   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9320   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9321
9322   /* We need to align to twice the pointer size here.  */
9323   if (DWARF_ARANGES_PAD_SIZE)
9324     {
9325       /* Pad using a 2 byte words so that padding is correct for any
9326          pointer size.  */
9327       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9328                            2 * DWARF2_ADDR_SIZE);
9329       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9330         dw2_asm_output_data (2, 0, NULL);
9331     }
9332
9333   /* It is necessary not to output these entries if the sections were
9334      not used; if the sections were not used, the length will be 0 and
9335      the address may end up as 0 if the section is discarded by ld
9336      --gc-sections, leaving an invalid (0, 0) entry that can be
9337      confused with the terminator.  */
9338   if (text_section_used)
9339     {
9340       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9341       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9342                             text_section_label, "Length");
9343     }
9344   if (cold_text_section_used)
9345     {
9346       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9347                            "Address");
9348       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9349                             cold_text_section_label, "Length");
9350     }
9351
9352   for (i = 0; i < arange_table_in_use; i++)
9353     {
9354       dw_die_ref die = arange_table[i];
9355
9356       /* We shouldn't see aranges for DIEs outside of the main CU.  */
9357       gcc_assert (die->die_mark);
9358
9359       if (die->die_tag == DW_TAG_subprogram)
9360         {
9361           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
9362                                "Address");
9363           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
9364                                 get_AT_low_pc (die), "Length");
9365         }
9366       else
9367         {
9368           /* A static variable; extract the symbol from DW_AT_location.
9369              Note that this code isn't currently hit, as we only emit
9370              aranges for functions (jason 9/23/99).  */
9371           dw_attr_ref a = get_AT (die, DW_AT_location);
9372           dw_loc_descr_ref loc;
9373
9374           gcc_assert (a && AT_class (a) == dw_val_class_loc);
9375
9376           loc = AT_loc (a);
9377           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
9378
9379           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
9380                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
9381           dw2_asm_output_data (DWARF2_ADDR_SIZE,
9382                                get_AT_unsigned (die, DW_AT_byte_size),
9383                                "Length");
9384         }
9385     }
9386
9387   /* Output the terminator words.  */
9388   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9389   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9390 }
9391
9392 /* Add a new entry to .debug_ranges.  Return the offset at which it
9393    was placed.  */
9394
9395 static unsigned int
9396 add_ranges_num (int num)
9397 {
9398   unsigned int in_use = ranges_table_in_use;
9399
9400   if (in_use == ranges_table_allocated)
9401     {
9402       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9403       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9404                                     ranges_table_allocated);
9405       memset (ranges_table + ranges_table_in_use, 0,
9406               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9407     }
9408
9409   ranges_table[in_use].num = num;
9410   ranges_table_in_use = in_use + 1;
9411
9412   return in_use * 2 * DWARF2_ADDR_SIZE;
9413 }
9414
9415 /* Add a new entry to .debug_ranges corresponding to a block, or a
9416    range terminator if BLOCK is NULL.  */
9417
9418 static unsigned int
9419 add_ranges (const_tree block)
9420 {
9421   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9422 }
9423
9424 /* Add a new entry to .debug_ranges corresponding to a pair of
9425    labels.  */
9426
9427 static unsigned int
9428 add_ranges_by_labels (const char *begin, const char *end)
9429 {
9430   unsigned int in_use = ranges_by_label_in_use;
9431
9432   if (in_use == ranges_by_label_allocated)
9433     {
9434       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9435       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9436                                        ranges_by_label,
9437                                        ranges_by_label_allocated);
9438       memset (ranges_by_label + ranges_by_label_in_use, 0,
9439               RANGES_TABLE_INCREMENT
9440               * sizeof (struct dw_ranges_by_label_struct));
9441     }
9442
9443   ranges_by_label[in_use].begin = begin;
9444   ranges_by_label[in_use].end = end;
9445   ranges_by_label_in_use = in_use + 1;
9446
9447   return add_ranges_num (-(int)in_use - 1);
9448 }
9449
9450 static void
9451 output_ranges (void)
9452 {
9453   unsigned i;
9454   static const char *const start_fmt = "Offset 0x%x";
9455   const char *fmt = start_fmt;
9456
9457   for (i = 0; i < ranges_table_in_use; i++)
9458     {
9459       int block_num = ranges_table[i].num;
9460
9461       if (block_num > 0)
9462         {
9463           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9464           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9465
9466           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9467           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9468
9469           /* If all code is in the text section, then the compilation
9470              unit base address defaults to DW_AT_low_pc, which is the
9471              base of the text section.  */
9472           if (!have_multiple_function_sections)
9473             {
9474               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9475                                     text_section_label,
9476                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9477               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9478                                     text_section_label, NULL);
9479             }
9480
9481           /* Otherwise, the compilation unit base address is zero,
9482              which allows us to use absolute addresses, and not worry
9483              about whether the target supports cross-section
9484              arithmetic.  */
9485           else
9486             {
9487               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9488                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9489               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9490             }
9491
9492           fmt = NULL;
9493         }
9494
9495       /* Negative block_num stands for an index into ranges_by_label.  */
9496       else if (block_num < 0)
9497         {
9498           int lab_idx = - block_num - 1;
9499
9500           if (!have_multiple_function_sections)
9501             {
9502               gcc_unreachable ();
9503 #if 0
9504               /* If we ever use add_ranges_by_labels () for a single
9505                  function section, all we have to do is to take out
9506                  the #if 0 above.  */
9507               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9508                                     ranges_by_label[lab_idx].begin,
9509                                     text_section_label,
9510                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9511               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9512                                     ranges_by_label[lab_idx].end,
9513                                     text_section_label, NULL);
9514 #endif
9515             }
9516           else
9517             {
9518               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9519                                    ranges_by_label[lab_idx].begin,
9520                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9521               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9522                                    ranges_by_label[lab_idx].end,
9523                                    NULL);
9524             }
9525         }
9526       else
9527         {
9528           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9529           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9530           fmt = start_fmt;
9531         }
9532     }
9533 }
9534
9535 /* Data structure containing information about input files.  */
9536 struct file_info
9537 {
9538   const char *path;     /* Complete file name.  */
9539   const char *fname;    /* File name part.  */
9540   int length;           /* Length of entire string.  */
9541   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9542   int dir_idx;          /* Index in directory table.  */
9543 };
9544
9545 /* Data structure containing information about directories with source
9546    files.  */
9547 struct dir_info
9548 {
9549   const char *path;     /* Path including directory name.  */
9550   int length;           /* Path length.  */
9551   int prefix;           /* Index of directory entry which is a prefix.  */
9552   int count;            /* Number of files in this directory.  */
9553   int dir_idx;          /* Index of directory used as base.  */
9554 };
9555
9556 /* Callback function for file_info comparison.  We sort by looking at
9557    the directories in the path.  */
9558
9559 static int
9560 file_info_cmp (const void *p1, const void *p2)
9561 {
9562   const struct file_info *const s1 = (const struct file_info *) p1;
9563   const struct file_info *const s2 = (const struct file_info *) p2;
9564   const unsigned char *cp1;
9565   const unsigned char *cp2;
9566
9567   /* Take care of file names without directories.  We need to make sure that
9568      we return consistent values to qsort since some will get confused if
9569      we return the same value when identical operands are passed in opposite
9570      orders.  So if neither has a directory, return 0 and otherwise return
9571      1 or -1 depending on which one has the directory.  */
9572   if ((s1->path == s1->fname || s2->path == s2->fname))
9573     return (s2->path == s2->fname) - (s1->path == s1->fname);
9574
9575   cp1 = (const unsigned char *) s1->path;
9576   cp2 = (const unsigned char *) s2->path;
9577
9578   while (1)
9579     {
9580       ++cp1;
9581       ++cp2;
9582       /* Reached the end of the first path?  If so, handle like above.  */
9583       if ((cp1 == (const unsigned char *) s1->fname)
9584           || (cp2 == (const unsigned char *) s2->fname))
9585         return ((cp2 == (const unsigned char *) s2->fname)
9586                 - (cp1 == (const unsigned char *) s1->fname));
9587
9588       /* Character of current path component the same?  */
9589       else if (*cp1 != *cp2)
9590         return *cp1 - *cp2;
9591     }
9592 }
9593
9594 struct file_name_acquire_data
9595 {
9596   struct file_info *files;
9597   int used_files;
9598   int max_files;
9599 };
9600
9601 /* Traversal function for the hash table.  */
9602
9603 static int
9604 file_name_acquire (void ** slot, void *data)
9605 {
9606   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9607   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9608   struct file_info *fi;
9609   const char *f;
9610
9611   gcc_assert (fnad->max_files >= d->emitted_number);
9612
9613   if (! d->emitted_number)
9614     return 1;
9615
9616   gcc_assert (fnad->max_files != fnad->used_files);
9617
9618   fi = fnad->files + fnad->used_files++;
9619
9620   /* Skip all leading "./".  */
9621   f = d->filename;
9622   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9623     f += 2;
9624
9625   /* Create a new array entry.  */
9626   fi->path = f;
9627   fi->length = strlen (f);
9628   fi->file_idx = d;
9629
9630   /* Search for the file name part.  */
9631   f = strrchr (f, DIR_SEPARATOR);
9632 #if defined (DIR_SEPARATOR_2)
9633   {
9634     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9635
9636     if (g != NULL)
9637       {
9638         if (f == NULL || f < g)
9639           f = g;
9640       }
9641   }
9642 #endif
9643
9644   fi->fname = f == NULL ? fi->path : f + 1;
9645   return 1;
9646 }
9647
9648 /* Output the directory table and the file name table.  We try to minimize
9649    the total amount of memory needed.  A heuristic is used to avoid large
9650    slowdowns with many input files.  */
9651
9652 static void
9653 output_file_names (void)
9654 {
9655   struct file_name_acquire_data fnad;
9656   int numfiles;
9657   struct file_info *files;
9658   struct dir_info *dirs;
9659   int *saved;
9660   int *savehere;
9661   int *backmap;
9662   int ndirs;
9663   int idx_offset;
9664   int i;
9665   int idx;
9666
9667   if (!last_emitted_file)
9668     {
9669       dw2_asm_output_data (1, 0, "End directory table");
9670       dw2_asm_output_data (1, 0, "End file name table");
9671       return;
9672     }
9673
9674   numfiles = last_emitted_file->emitted_number;
9675
9676   /* Allocate the various arrays we need.  */
9677   files = XALLOCAVEC (struct file_info, numfiles);
9678   dirs = XALLOCAVEC (struct dir_info, numfiles);
9679
9680   fnad.files = files;
9681   fnad.used_files = 0;
9682   fnad.max_files = numfiles;
9683   htab_traverse (file_table, file_name_acquire, &fnad);
9684   gcc_assert (fnad.used_files == fnad.max_files);
9685
9686   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9687
9688   /* Find all the different directories used.  */
9689   dirs[0].path = files[0].path;
9690   dirs[0].length = files[0].fname - files[0].path;
9691   dirs[0].prefix = -1;
9692   dirs[0].count = 1;
9693   dirs[0].dir_idx = 0;
9694   files[0].dir_idx = 0;
9695   ndirs = 1;
9696
9697   for (i = 1; i < numfiles; i++)
9698     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9699         && memcmp (dirs[ndirs - 1].path, files[i].path,
9700                    dirs[ndirs - 1].length) == 0)
9701       {
9702         /* Same directory as last entry.  */
9703         files[i].dir_idx = ndirs - 1;
9704         ++dirs[ndirs - 1].count;
9705       }
9706     else
9707       {
9708         int j;
9709
9710         /* This is a new directory.  */
9711         dirs[ndirs].path = files[i].path;
9712         dirs[ndirs].length = files[i].fname - files[i].path;
9713         dirs[ndirs].count = 1;
9714         dirs[ndirs].dir_idx = ndirs;
9715         files[i].dir_idx = ndirs;
9716
9717         /* Search for a prefix.  */
9718         dirs[ndirs].prefix = -1;
9719         for (j = 0; j < ndirs; j++)
9720           if (dirs[j].length < dirs[ndirs].length
9721               && dirs[j].length > 1
9722               && (dirs[ndirs].prefix == -1
9723                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9724               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9725             dirs[ndirs].prefix = j;
9726
9727         ++ndirs;
9728       }
9729
9730   /* Now to the actual work.  We have to find a subset of the directories which
9731      allow expressing the file name using references to the directory table
9732      with the least amount of characters.  We do not do an exhaustive search
9733      where we would have to check out every combination of every single
9734      possible prefix.  Instead we use a heuristic which provides nearly optimal
9735      results in most cases and never is much off.  */
9736   saved = XALLOCAVEC (int, ndirs);
9737   savehere = XALLOCAVEC (int, ndirs);
9738
9739   memset (saved, '\0', ndirs * sizeof (saved[0]));
9740   for (i = 0; i < ndirs; i++)
9741     {
9742       int j;
9743       int total;
9744
9745       /* We can always save some space for the current directory.  But this
9746          does not mean it will be enough to justify adding the directory.  */
9747       savehere[i] = dirs[i].length;
9748       total = (savehere[i] - saved[i]) * dirs[i].count;
9749
9750       for (j = i + 1; j < ndirs; j++)
9751         {
9752           savehere[j] = 0;
9753           if (saved[j] < dirs[i].length)
9754             {
9755               /* Determine whether the dirs[i] path is a prefix of the
9756                  dirs[j] path.  */
9757               int k;
9758
9759               k = dirs[j].prefix;
9760               while (k != -1 && k != (int) i)
9761                 k = dirs[k].prefix;
9762
9763               if (k == (int) i)
9764                 {
9765                   /* Yes it is.  We can possibly save some memory by
9766                      writing the filenames in dirs[j] relative to
9767                      dirs[i].  */
9768                   savehere[j] = dirs[i].length;
9769                   total += (savehere[j] - saved[j]) * dirs[j].count;
9770                 }
9771             }
9772         }
9773
9774       /* Check whether we can save enough to justify adding the dirs[i]
9775          directory.  */
9776       if (total > dirs[i].length + 1)
9777         {
9778           /* It's worthwhile adding.  */
9779           for (j = i; j < ndirs; j++)
9780             if (savehere[j] > 0)
9781               {
9782                 /* Remember how much we saved for this directory so far.  */
9783                 saved[j] = savehere[j];
9784
9785                 /* Remember the prefix directory.  */
9786                 dirs[j].dir_idx = i;
9787               }
9788         }
9789     }
9790
9791   /* Emit the directory name table.  */
9792   idx = 1;
9793   idx_offset = dirs[0].length > 0 ? 1 : 0;
9794   for (i = 1 - idx_offset; i < ndirs; i++)
9795     dw2_asm_output_nstring (dirs[i].path,
9796                             dirs[i].length
9797                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9798                             "Directory Entry: 0x%x", i + idx_offset);
9799
9800   dw2_asm_output_data (1, 0, "End directory table");
9801
9802   /* We have to emit them in the order of emitted_number since that's
9803      used in the debug info generation.  To do this efficiently we
9804      generate a back-mapping of the indices first.  */
9805   backmap = XALLOCAVEC (int, numfiles);
9806   for (i = 0; i < numfiles; i++)
9807     backmap[files[i].file_idx->emitted_number - 1] = i;
9808
9809   /* Now write all the file names.  */
9810   for (i = 0; i < numfiles; i++)
9811     {
9812       int file_idx = backmap[i];
9813       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9814
9815 #ifdef VMS_DEBUGGING_INFO
9816 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9817
9818       /* Setting these fields can lead to debugger miscomparisons,
9819          but VMS Debug requires them to be set correctly.  */
9820
9821       int ver;
9822       long long cdt;
9823       long siz;
9824       int maxfilelen = strlen (files[file_idx].path)
9825                                + dirs[dir_idx].length
9826                                + MAX_VMS_VERSION_LEN + 1;
9827       char *filebuf = XALLOCAVEC (char, maxfilelen);
9828
9829       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9830       snprintf (filebuf, maxfilelen, "%s;%d",
9831                 files[file_idx].path + dirs[dir_idx].length, ver);
9832
9833       dw2_asm_output_nstring
9834         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
9835
9836       /* Include directory index.  */
9837       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9838
9839       /* Modification time.  */
9840       dw2_asm_output_data_uleb128
9841         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9842           ? cdt : 0,
9843          NULL);
9844
9845       /* File length in bytes.  */
9846       dw2_asm_output_data_uleb128
9847         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9848           ? siz : 0,
9849          NULL);
9850 #else
9851       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9852                               "File Entry: 0x%x", (unsigned) i + 1);
9853
9854       /* Include directory index.  */
9855       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9856
9857       /* Modification time.  */
9858       dw2_asm_output_data_uleb128 (0, NULL);
9859
9860       /* File length in bytes.  */
9861       dw2_asm_output_data_uleb128 (0, NULL);
9862 #endif
9863     }
9864
9865   dw2_asm_output_data (1, 0, "End file name table");
9866 }
9867
9868
9869 /* Output the source line number correspondence information.  This
9870    information goes into the .debug_line section.  */
9871
9872 static void
9873 output_line_info (void)
9874 {
9875   char l1[20], l2[20], p1[20], p2[20];
9876   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9877   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9878   unsigned opc;
9879   unsigned n_op_args;
9880   unsigned long lt_index;
9881   unsigned long current_line;
9882   long line_offset;
9883   long line_delta;
9884   unsigned long current_file;
9885   unsigned long function;
9886
9887   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9888   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9889   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9890   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9891
9892   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9893     dw2_asm_output_data (4, 0xffffffff,
9894       "Initial length escape value indicating 64-bit DWARF extension");
9895   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9896                         "Length of Source Line Info");
9897   ASM_OUTPUT_LABEL (asm_out_file, l1);
9898
9899   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9900   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9901   ASM_OUTPUT_LABEL (asm_out_file, p1);
9902
9903   /* Define the architecture-dependent minimum instruction length (in
9904    bytes).  In this implementation of DWARF, this field is used for
9905    information purposes only.  Since GCC generates assembly language,
9906    we have no a priori knowledge of how many instruction bytes are
9907    generated for each source line, and therefore can use only the
9908    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9909    commands.  Accordingly, we fix this as `1', which is "correct
9910    enough" for all architectures, and don't let the target override.  */
9911   dw2_asm_output_data (1, 1,
9912                        "Minimum Instruction Length");
9913
9914   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9915                        "Default is_stmt_start flag");
9916   dw2_asm_output_data (1, DWARF_LINE_BASE,
9917                        "Line Base Value (Special Opcodes)");
9918   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9919                        "Line Range Value (Special Opcodes)");
9920   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9921                        "Special Opcode Base");
9922
9923   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9924     {
9925       switch (opc)
9926         {
9927         case DW_LNS_advance_pc:
9928         case DW_LNS_advance_line:
9929         case DW_LNS_set_file:
9930         case DW_LNS_set_column:
9931         case DW_LNS_fixed_advance_pc:
9932           n_op_args = 1;
9933           break;
9934         default:
9935           n_op_args = 0;
9936           break;
9937         }
9938
9939       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9940                            opc, n_op_args);
9941     }
9942
9943   /* Write out the information about the files we use.  */
9944   output_file_names ();
9945   ASM_OUTPUT_LABEL (asm_out_file, p2);
9946
9947   /* We used to set the address register to the first location in the text
9948      section here, but that didn't accomplish anything since we already
9949      have a line note for the opening brace of the first function.  */
9950
9951   /* Generate the line number to PC correspondence table, encoded as
9952      a series of state machine operations.  */
9953   current_file = 1;
9954   current_line = 1;
9955
9956   if (cfun && in_cold_section_p)
9957     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9958   else
9959     strcpy (prev_line_label, text_section_label);
9960   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9961     {
9962       dw_line_info_ref line_info = &line_info_table[lt_index];
9963
9964 #if 0
9965       /* Disable this optimization for now; GDB wants to see two line notes
9966          at the beginning of a function so it can find the end of the
9967          prologue.  */
9968
9969       /* Don't emit anything for redundant notes.  Just updating the
9970          address doesn't accomplish anything, because we already assume
9971          that anything after the last address is this line.  */
9972       if (line_info->dw_line_num == current_line
9973           && line_info->dw_file_num == current_file)
9974         continue;
9975 #endif
9976
9977       /* Emit debug info for the address of the current line.
9978
9979          Unfortunately, we have little choice here currently, and must always
9980          use the most general form.  GCC does not know the address delta
9981          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9982          attributes which will give an upper bound on the address range.  We
9983          could perhaps use length attributes to determine when it is safe to
9984          use DW_LNS_fixed_advance_pc.  */
9985
9986       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9987       if (0)
9988         {
9989           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9990           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9991                                "DW_LNS_fixed_advance_pc");
9992           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9993         }
9994       else
9995         {
9996           /* This can handle any delta.  This takes
9997              4+DWARF2_ADDR_SIZE bytes.  */
9998           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9999           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10000           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10001           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10002         }
10003
10004       strcpy (prev_line_label, line_label);
10005
10006       /* Emit debug info for the source file of the current line, if
10007          different from the previous line.  */
10008       if (line_info->dw_file_num != current_file)
10009         {
10010           current_file = line_info->dw_file_num;
10011           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10012           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10013         }
10014
10015       /* Emit debug info for the current line number, choosing the encoding
10016          that uses the least amount of space.  */
10017       if (line_info->dw_line_num != current_line)
10018         {
10019           line_offset = line_info->dw_line_num - current_line;
10020           line_delta = line_offset - DWARF_LINE_BASE;
10021           current_line = line_info->dw_line_num;
10022           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10023             /* This can handle deltas from -10 to 234, using the current
10024                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
10025                takes 1 byte.  */
10026             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10027                                  "line %lu", current_line);
10028           else
10029             {
10030               /* This can handle any delta.  This takes at least 4 bytes,
10031                  depending on the value being encoded.  */
10032               dw2_asm_output_data (1, DW_LNS_advance_line,
10033                                    "advance to line %lu", current_line);
10034               dw2_asm_output_data_sleb128 (line_offset, NULL);
10035               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10036             }
10037         }
10038       else
10039         /* We still need to start a new row, so output a copy insn.  */
10040         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10041     }
10042
10043   /* Emit debug info for the address of the end of the function.  */
10044   if (0)
10045     {
10046       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10047                            "DW_LNS_fixed_advance_pc");
10048       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
10049     }
10050   else
10051     {
10052       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10053       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10054       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10055       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
10056     }
10057
10058   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10059   dw2_asm_output_data_uleb128 (1, NULL);
10060   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10061
10062   function = 0;
10063   current_file = 1;
10064   current_line = 1;
10065   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
10066     {
10067       dw_separate_line_info_ref line_info
10068         = &separate_line_info_table[lt_index];
10069
10070 #if 0
10071       /* Don't emit anything for redundant notes.  */
10072       if (line_info->dw_line_num == current_line
10073           && line_info->dw_file_num == current_file
10074           && line_info->function == function)
10075         goto cont;
10076 #endif
10077
10078       /* Emit debug info for the address of the current line.  If this is
10079          a new function, or the first line of a function, then we need
10080          to handle it differently.  */
10081       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
10082                                    lt_index);
10083       if (function != line_info->function)
10084         {
10085           function = line_info->function;
10086
10087           /* Set the address register to the first line in the function.  */
10088           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10089           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10090           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10091           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10092         }
10093       else
10094         {
10095           /* ??? See the DW_LNS_advance_pc comment above.  */
10096           if (0)
10097             {
10098               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10099                                    "DW_LNS_fixed_advance_pc");
10100               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10101             }
10102           else
10103             {
10104               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10105               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10106               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10107               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10108             }
10109         }
10110
10111       strcpy (prev_line_label, line_label);
10112
10113       /* Emit debug info for the source file of the current line, if
10114          different from the previous line.  */
10115       if (line_info->dw_file_num != current_file)
10116         {
10117           current_file = line_info->dw_file_num;
10118           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10119           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10120         }
10121
10122       /* Emit debug info for the current line number, choosing the encoding
10123          that uses the least amount of space.  */
10124       if (line_info->dw_line_num != current_line)
10125         {
10126           line_offset = line_info->dw_line_num - current_line;
10127           line_delta = line_offset - DWARF_LINE_BASE;
10128           current_line = line_info->dw_line_num;
10129           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10130             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10131                                  "line %lu", current_line);
10132           else
10133             {
10134               dw2_asm_output_data (1, DW_LNS_advance_line,
10135                                    "advance to line %lu", current_line);
10136               dw2_asm_output_data_sleb128 (line_offset, NULL);
10137               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10138             }
10139         }
10140       else
10141         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10142
10143 #if 0
10144     cont:
10145 #endif
10146
10147       lt_index++;
10148
10149       /* If we're done with a function, end its sequence.  */
10150       if (lt_index == separate_line_info_table_in_use
10151           || separate_line_info_table[lt_index].function != function)
10152         {
10153           current_file = 1;
10154           current_line = 1;
10155
10156           /* Emit debug info for the address of the end of the function.  */
10157           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
10158           if (0)
10159             {
10160               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10161                                    "DW_LNS_fixed_advance_pc");
10162               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10163             }
10164           else
10165             {
10166               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10167               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10168               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10169               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10170             }
10171
10172           /* Output the marker for the end of this sequence.  */
10173           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10174           dw2_asm_output_data_uleb128 (1, NULL);
10175           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10176         }
10177     }
10178
10179   /* Output the marker for the end of the line number info.  */
10180   ASM_OUTPUT_LABEL (asm_out_file, l2);
10181 }
10182 \f
10183 /* Given a pointer to a tree node for some base type, return a pointer to
10184    a DIE that describes the given type.
10185
10186    This routine must only be called for GCC type nodes that correspond to
10187    Dwarf base (fundamental) types.  */
10188
10189 static dw_die_ref
10190 base_type_die (tree type)
10191 {
10192   dw_die_ref base_type_result;
10193   enum dwarf_type encoding;
10194
10195   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10196     return 0;
10197
10198   /* If this is a subtype that should not be emitted as a subrange type,
10199      use the base type.  See subrange_type_for_debug_p.  */
10200   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10201     type = TREE_TYPE (type);
10202
10203   switch (TREE_CODE (type))
10204     {
10205     case INTEGER_TYPE:
10206       if (TYPE_STRING_FLAG (type))
10207         {
10208           if (TYPE_UNSIGNED (type))
10209             encoding = DW_ATE_unsigned_char;
10210           else
10211             encoding = DW_ATE_signed_char;
10212         }
10213       else if (TYPE_UNSIGNED (type))
10214         encoding = DW_ATE_unsigned;
10215       else
10216         encoding = DW_ATE_signed;
10217       break;
10218
10219     case REAL_TYPE:
10220       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10221         encoding = DW_ATE_decimal_float;
10222       else
10223         encoding = DW_ATE_float;
10224       break;
10225
10226     case FIXED_POINT_TYPE:
10227       if (TYPE_UNSIGNED (type))
10228         encoding = DW_ATE_unsigned_fixed;
10229       else
10230         encoding = DW_ATE_signed_fixed;
10231       break;
10232
10233       /* Dwarf2 doesn't know anything about complex ints, so use
10234          a user defined type for it.  */
10235     case COMPLEX_TYPE:
10236       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10237         encoding = DW_ATE_complex_float;
10238       else
10239         encoding = DW_ATE_lo_user;
10240       break;
10241
10242     case BOOLEAN_TYPE:
10243       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10244       encoding = DW_ATE_boolean;
10245       break;
10246
10247     default:
10248       /* No other TREE_CODEs are Dwarf fundamental types.  */
10249       gcc_unreachable ();
10250     }
10251
10252   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
10253
10254   /* This probably indicates a bug.  */
10255   if (! TYPE_NAME (type))
10256     add_name_attribute (base_type_result, "__unknown__");
10257
10258   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10259                    int_size_in_bytes (type));
10260   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10261
10262   return base_type_result;
10263 }
10264
10265 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10266    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10267
10268 static inline int
10269 is_base_type (tree type)
10270 {
10271   switch (TREE_CODE (type))
10272     {
10273     case ERROR_MARK:
10274     case VOID_TYPE:
10275     case INTEGER_TYPE:
10276     case REAL_TYPE:
10277     case FIXED_POINT_TYPE:
10278     case COMPLEX_TYPE:
10279     case BOOLEAN_TYPE:
10280       return 1;
10281
10282     case ARRAY_TYPE:
10283     case RECORD_TYPE:
10284     case UNION_TYPE:
10285     case QUAL_UNION_TYPE:
10286     case ENUMERAL_TYPE:
10287     case FUNCTION_TYPE:
10288     case METHOD_TYPE:
10289     case POINTER_TYPE:
10290     case REFERENCE_TYPE:
10291     case OFFSET_TYPE:
10292     case LANG_TYPE:
10293     case VECTOR_TYPE:
10294       return 0;
10295
10296     default:
10297       gcc_unreachable ();
10298     }
10299
10300   return 0;
10301 }
10302
10303 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10304    node, return the size in bits for the type if it is a constant, or else
10305    return the alignment for the type if the type's size is not constant, or
10306    else return BITS_PER_WORD if the type actually turns out to be an
10307    ERROR_MARK node.  */
10308
10309 static inline unsigned HOST_WIDE_INT
10310 simple_type_size_in_bits (const_tree type)
10311 {
10312   if (TREE_CODE (type) == ERROR_MARK)
10313     return BITS_PER_WORD;
10314   else if (TYPE_SIZE (type) == NULL_TREE)
10315     return 0;
10316   else if (host_integerp (TYPE_SIZE (type), 1))
10317     return tree_low_cst (TYPE_SIZE (type), 1);
10318   else
10319     return TYPE_ALIGN (type);
10320 }
10321
10322 /*  Given a pointer to a tree node for a subrange type, return a pointer
10323     to a DIE that describes the given type.  */
10324
10325 static dw_die_ref
10326 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10327 {
10328   dw_die_ref subrange_die;
10329   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10330
10331   if (context_die == NULL)
10332     context_die = comp_unit_die;
10333
10334   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10335
10336   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10337     {
10338       /* The size of the subrange type and its base type do not match,
10339          so we need to generate a size attribute for the subrange type.  */
10340       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10341     }
10342
10343   if (low)
10344     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10345   if (high)
10346     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10347
10348   return subrange_die;
10349 }
10350
10351 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10352    entry that chains various modifiers in front of the given type.  */
10353
10354 static dw_die_ref
10355 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10356                    dw_die_ref context_die)
10357 {
10358   enum tree_code code = TREE_CODE (type);
10359   dw_die_ref mod_type_die;
10360   dw_die_ref sub_die = NULL;
10361   tree item_type = NULL;
10362   tree qualified_type;
10363   tree name, low, high;
10364
10365   if (code == ERROR_MARK)
10366     return NULL;
10367
10368   /* See if we already have the appropriately qualified variant of
10369      this type.  */
10370   qualified_type
10371     = get_qualified_type (type,
10372                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10373                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10374
10375   /* If we do, then we can just use its DIE, if it exists.  */
10376   if (qualified_type)
10377     {
10378       mod_type_die = lookup_type_die (qualified_type);
10379       if (mod_type_die)
10380         return mod_type_die;
10381     }
10382
10383   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10384
10385   /* Handle C typedef types.  */
10386   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
10387     {
10388       tree dtype = TREE_TYPE (name);
10389
10390       if (qualified_type == dtype)
10391         {
10392           /* For a named type, use the typedef.  */
10393           gen_type_die (qualified_type, context_die);
10394           return lookup_type_die (qualified_type);
10395         }
10396       else if (is_const_type < TYPE_READONLY (dtype)
10397                || is_volatile_type < TYPE_VOLATILE (dtype)
10398                || (is_const_type <= TYPE_READONLY (dtype)
10399                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10400                    && DECL_ORIGINAL_TYPE (name) != type))
10401         /* cv-unqualified version of named type.  Just use the unnamed
10402            type to which it refers.  */
10403         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10404                                   is_const_type, is_volatile_type,
10405                                   context_die);
10406       /* Else cv-qualified version of named type; fall through.  */
10407     }
10408
10409   if (is_const_type)
10410     {
10411       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
10412       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10413     }
10414   else if (is_volatile_type)
10415     {
10416       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
10417       sub_die = modified_type_die (type, 0, 0, context_die);
10418     }
10419   else if (code == POINTER_TYPE)
10420     {
10421       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
10422       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10423                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10424       item_type = TREE_TYPE (type);
10425     }
10426   else if (code == REFERENCE_TYPE)
10427     {
10428       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
10429       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10430                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10431       item_type = TREE_TYPE (type);
10432     }
10433   else if (code == INTEGER_TYPE
10434            && TREE_TYPE (type) != NULL_TREE
10435            && subrange_type_for_debug_p (type, &low, &high))
10436     {
10437       mod_type_die = subrange_type_die (type, low, high, context_die);
10438       item_type = TREE_TYPE (type);
10439     }
10440   else if (is_base_type (type))
10441     mod_type_die = base_type_die (type);
10442   else
10443     {
10444       gen_type_die (type, context_die);
10445
10446       /* We have to get the type_main_variant here (and pass that to the
10447          `lookup_type_die' routine) because the ..._TYPE node we have
10448          might simply be a *copy* of some original type node (where the
10449          copy was created to help us keep track of typedef names) and
10450          that copy might have a different TYPE_UID from the original
10451          ..._TYPE node.  */
10452       if (TREE_CODE (type) != VECTOR_TYPE)
10453         return lookup_type_die (type_main_variant (type));
10454       else
10455         /* Vectors have the debugging information in the type,
10456            not the main variant.  */
10457         return lookup_type_die (type);
10458     }
10459
10460   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10461      don't output a DW_TAG_typedef, since there isn't one in the
10462      user's program; just attach a DW_AT_name to the type.  */
10463   if (name
10464       && (TREE_CODE (name) != TYPE_DECL
10465           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
10466     {
10467       if (TREE_CODE (name) == TYPE_DECL)
10468         /* Could just call add_name_and_src_coords_attributes here,
10469            but since this is a builtin type it doesn't have any
10470            useful source coordinates anyway.  */
10471         name = DECL_NAME (name);
10472       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10473     }
10474
10475   if (qualified_type)
10476     equate_type_number_to_die (qualified_type, mod_type_die);
10477
10478   if (item_type)
10479     /* We must do this after the equate_type_number_to_die call, in case
10480        this is a recursive type.  This ensures that the modified_type_die
10481        recursion will terminate even if the type is recursive.  Recursive
10482        types are possible in Ada.  */
10483     sub_die = modified_type_die (item_type,
10484                                  TYPE_READONLY (item_type),
10485                                  TYPE_VOLATILE (item_type),
10486                                  context_die);
10487
10488   if (sub_die != NULL)
10489     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10490
10491   return mod_type_die;
10492 }
10493
10494 /* Generate a new name for the parameter pack name NAME (an
10495    IDENTIFIER_NODE) that incorporates its */
10496
10497 static tree
10498 make_ith_pack_parameter_name (tree name, int i)
10499 {
10500   /* Munge the name to include the parameter index.  */
10501 #define NUMBUF_LEN 128
10502   char numbuf[NUMBUF_LEN];
10503   char* newname;
10504   int newname_len;
10505
10506   snprintf (numbuf, NUMBUF_LEN, "%i", i);
10507   newname_len = IDENTIFIER_LENGTH (name)
10508                 + strlen (numbuf) + 2;
10509   newname = (char*) alloca (newname_len);
10510   snprintf (newname, newname_len,
10511             "%s#%i", IDENTIFIER_POINTER (name), i);
10512   return get_identifier (newname);
10513 }
10514
10515 /* Generate DIEs for the generic parameters of T.
10516    T must be either a generic type or a generic function.
10517    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10518
10519 static void
10520 gen_generic_params_dies (tree t)
10521 {
10522   tree parms, args;
10523   int parms_num, i;
10524   dw_die_ref die = NULL;
10525
10526   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10527     return;
10528
10529   if (TYPE_P (t))
10530     die = lookup_type_die (t);
10531   else if (DECL_P (t))
10532     die = lookup_decl_die (t);
10533
10534   gcc_assert (die);
10535
10536   parms = lang_hooks.get_innermost_generic_parms (t);
10537   if (!parms)
10538     /* T has no generic parameter. It means T is neither a generic type
10539        or function. End of story.  */
10540     return;
10541
10542   parms_num = TREE_VEC_LENGTH (parms);
10543   args = lang_hooks.get_innermost_generic_args (t);
10544   for (i = 0; i < parms_num; i++)
10545     {
10546       tree parm, arg;
10547
10548       parm = TREE_VEC_ELT (parms, i);
10549       arg = TREE_VEC_ELT (args, i);
10550       if (parm && TREE_VALUE (parm) && arg)
10551         {
10552           tree pack_elems =
10553             lang_hooks.types.get_argument_pack_elems (arg);
10554           if (pack_elems)
10555             {
10556               /* So ARG is an argument pack and the elements of that pack
10557                  are stored in PACK_ELEMS.  */
10558               int i, len;
10559
10560               len = TREE_VEC_LENGTH (pack_elems);
10561               for (i = 0; i < len; i++)
10562                 generic_parameter_die (TREE_VALUE (parm),
10563                                        TREE_VEC_ELT (pack_elems, i),
10564                                        die, i);
10565             }
10566           else /* Arg is not an argument pack.  */
10567             generic_parameter_die (TREE_VALUE (parm),
10568                                    arg, die,
10569                                    -1/* Not a param pack.  */);
10570         }
10571     }
10572 }
10573
10574 /* Create and return a DIE for PARM which should be
10575    the representation of a generic type parameter.
10576    For instance, in the C++ front end, PARM would be a template parameter.
10577    ARG is the argument to PARM.
10578    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10579    as a child node.
10580    PACK_ELEM_INDEX is >= 0 if PARM is a generic parameter pack, and if ARG
10581    is one of the unpacked elements of the parameter PACK. In that case,
10582    PACK_ELEM_INDEX is the index of ARG in the parameter pack.  */
10583
10584 static dw_die_ref
10585 generic_parameter_die (tree parm, tree arg, dw_die_ref parent_die,
10586                        int pack_elem_index)
10587 {
10588   dw_die_ref tmpl_die = NULL;
10589   const char *name = NULL;
10590
10591   if (!parm || !DECL_NAME (parm) || !arg)
10592     return NULL;
10593
10594   /* We support non-type generic parameters and arguments,
10595      type generic parameters and arguments, as well as
10596      generic generic parameters (a.k.a. template template parameters in C++)
10597      and arguments.  */
10598   if (TREE_CODE (parm) == PARM_DECL)
10599     /* PARM is a nontype generic parameter  */
10600     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10601   else if (TREE_CODE (parm) == TYPE_DECL)
10602     /* PARM is a type generic parameter.  */
10603     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10604   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10605     /* PARM is a generic generic parameter.
10606        Its DIE is a GNU extension. It shall have a
10607        DW_AT_name attribute to represent the name of the template template
10608        parameter, and a DW_AT_GNU_template_name attribute to represent the
10609        name of the template template argument.  */
10610     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10611                         parent_die, parm);
10612   else
10613     gcc_unreachable ();
10614
10615   if (tmpl_die)
10616     {
10617       tree tmpl_type;
10618
10619       if (pack_elem_index >= 0)
10620         {
10621           /* PARM is an element of a parameter pack.
10622              Generate a name for it.  */
10623           tree identifier = make_ith_pack_parameter_name (DECL_NAME (parm),
10624                                                           pack_elem_index);
10625           if (identifier)
10626             name = IDENTIFIER_POINTER (identifier);
10627         }
10628       else
10629         name = IDENTIFIER_POINTER (DECL_NAME (parm));
10630
10631       gcc_assert (name);
10632       add_AT_string (tmpl_die, DW_AT_name, name);
10633
10634       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10635         {
10636           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10637              TMPL_DIE should have a child DW_AT_type attribute that is set
10638              to the type of the argument to PARM, which is ARG.
10639              If PARM is a type generic parameter, TMPL_DIE should have a
10640              child DW_AT_type that is set to ARG.  */
10641           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10642           add_type_attribute (tmpl_die, tmpl_type, 0,
10643                               TREE_THIS_VOLATILE (tmpl_type),
10644                               parent_die);
10645         }
10646       else
10647         {
10648           /* So TMPL_DIE is a DIE representing a
10649              a generic generic template parameter, a.k.a template template
10650              parameter in C++ and arg is a template.  */
10651
10652           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10653              to the name of the argument.  */
10654           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10655           add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10656         }
10657
10658       if (TREE_CODE (parm) == PARM_DECL)
10659         /* So PARM is a non-type generic parameter.
10660            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10661            attribute of TMPL_DIE which value represents the value
10662            of ARG.
10663            We must be careful here:
10664            The value of ARG might reference some function decls.
10665            We might currently be emitting debug info for a generic
10666            type and types are emitted before function decls, we don't
10667            know if the function decls referenced by ARG will actually be
10668            emitted after cgraph computations.
10669            So must defer the generation of the DW_AT_const_value to
10670            after cgraph is ready.  */
10671         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10672     }
10673
10674   return tmpl_die;
10675 }
10676
10677 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10678    an enumerated type.  */
10679
10680 static inline int
10681 type_is_enum (const_tree type)
10682 {
10683   return TREE_CODE (type) == ENUMERAL_TYPE;
10684 }
10685
10686 /* Return the DBX register number described by a given RTL node.  */
10687
10688 static unsigned int
10689 dbx_reg_number (const_rtx rtl)
10690 {
10691   unsigned regno = REGNO (rtl);
10692
10693   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10694
10695 #ifdef LEAF_REG_REMAP
10696   if (current_function_uses_only_leaf_regs)
10697     {
10698       int leaf_reg = LEAF_REG_REMAP (regno);
10699       if (leaf_reg != -1)
10700         regno = (unsigned) leaf_reg;
10701     }
10702 #endif
10703
10704   return DBX_REGISTER_NUMBER (regno);
10705 }
10706
10707 /* Optionally add a DW_OP_piece term to a location description expression.
10708    DW_OP_piece is only added if the location description expression already
10709    doesn't end with DW_OP_piece.  */
10710
10711 static void
10712 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10713 {
10714   dw_loc_descr_ref loc;
10715
10716   if (*list_head != NULL)
10717     {
10718       /* Find the end of the chain.  */
10719       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10720         ;
10721
10722       if (loc->dw_loc_opc != DW_OP_piece)
10723         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10724     }
10725 }
10726
10727 /* Return a location descriptor that designates a machine register or
10728    zero if there is none.  */
10729
10730 static dw_loc_descr_ref
10731 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10732 {
10733   rtx regs;
10734
10735   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10736     return 0;
10737
10738   regs = targetm.dwarf_register_span (rtl);
10739
10740   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10741     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10742   else
10743     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10744 }
10745
10746 /* Return a location descriptor that designates a machine register for
10747    a given hard register number.  */
10748
10749 static dw_loc_descr_ref
10750 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10751 {
10752   dw_loc_descr_ref reg_loc_descr;
10753
10754   if (regno <= 31)
10755     reg_loc_descr
10756       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10757   else
10758     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10759
10760   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10761     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10762
10763   return reg_loc_descr;
10764 }
10765
10766 /* Given an RTL of a register, return a location descriptor that
10767    designates a value that spans more than one register.  */
10768
10769 static dw_loc_descr_ref
10770 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10771                              enum var_init_status initialized)
10772 {
10773   int nregs, size, i;
10774   unsigned reg;
10775   dw_loc_descr_ref loc_result = NULL;
10776
10777   reg = REGNO (rtl);
10778 #ifdef LEAF_REG_REMAP
10779   if (current_function_uses_only_leaf_regs)
10780     {
10781       int leaf_reg = LEAF_REG_REMAP (reg);
10782       if (leaf_reg != -1)
10783         reg = (unsigned) leaf_reg;
10784     }
10785 #endif
10786   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10787   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10788
10789   /* Simple, contiguous registers.  */
10790   if (regs == NULL_RTX)
10791     {
10792       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10793
10794       loc_result = NULL;
10795       while (nregs--)
10796         {
10797           dw_loc_descr_ref t;
10798
10799           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10800                                       VAR_INIT_STATUS_INITIALIZED);
10801           add_loc_descr (&loc_result, t);
10802           add_loc_descr_op_piece (&loc_result, size);
10803           ++reg;
10804         }
10805       return loc_result;
10806     }
10807
10808   /* Now onto stupid register sets in non contiguous locations.  */
10809
10810   gcc_assert (GET_CODE (regs) == PARALLEL);
10811
10812   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10813   loc_result = NULL;
10814
10815   for (i = 0; i < XVECLEN (regs, 0); ++i)
10816     {
10817       dw_loc_descr_ref t;
10818
10819       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10820                                   VAR_INIT_STATUS_INITIALIZED);
10821       add_loc_descr (&loc_result, t);
10822       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10823       add_loc_descr_op_piece (&loc_result, size);
10824     }
10825
10826   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10827     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10828   return loc_result;
10829 }
10830
10831 #endif /* DWARF2_DEBUGGING_INFO */
10832
10833 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10834
10835 /* Return a location descriptor that designates a constant.  */
10836
10837 static dw_loc_descr_ref
10838 int_loc_descriptor (HOST_WIDE_INT i)
10839 {
10840   enum dwarf_location_atom op;
10841
10842   /* Pick the smallest representation of a constant, rather than just
10843      defaulting to the LEB encoding.  */
10844   if (i >= 0)
10845     {
10846       if (i <= 31)
10847         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10848       else if (i <= 0xff)
10849         op = DW_OP_const1u;
10850       else if (i <= 0xffff)
10851         op = DW_OP_const2u;
10852       else if (HOST_BITS_PER_WIDE_INT == 32
10853                || i <= 0xffffffff)
10854         op = DW_OP_const4u;
10855       else
10856         op = DW_OP_constu;
10857     }
10858   else
10859     {
10860       if (i >= -0x80)
10861         op = DW_OP_const1s;
10862       else if (i >= -0x8000)
10863         op = DW_OP_const2s;
10864       else if (HOST_BITS_PER_WIDE_INT == 32
10865                || i >= -0x80000000)
10866         op = DW_OP_const4s;
10867       else
10868         op = DW_OP_consts;
10869     }
10870
10871   return new_loc_descr (op, i, 0);
10872 }
10873 #endif
10874
10875 #ifdef DWARF2_DEBUGGING_INFO
10876
10877 /* Return a location descriptor that designates a base+offset location.  */
10878
10879 static dw_loc_descr_ref
10880 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10881                  enum var_init_status initialized)
10882 {
10883   unsigned int regno;
10884   dw_loc_descr_ref result;
10885   dw_fde_ref fde = current_fde ();
10886
10887   /* We only use "frame base" when we're sure we're talking about the
10888      post-prologue local stack frame.  We do this by *not* running
10889      register elimination until this point, and recognizing the special
10890      argument pointer and soft frame pointer rtx's.  */
10891   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10892     {
10893       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10894
10895       if (elim != reg)
10896         {
10897           if (GET_CODE (elim) == PLUS)
10898             {
10899               offset += INTVAL (XEXP (elim, 1));
10900               elim = XEXP (elim, 0);
10901             }
10902           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10903                        && (elim == hard_frame_pointer_rtx
10904                            || elim == stack_pointer_rtx))
10905                       || elim == (frame_pointer_needed
10906                                   ? hard_frame_pointer_rtx
10907                                   : stack_pointer_rtx));
10908
10909           /* If drap register is used to align stack, use frame
10910              pointer + offset to access stack variables.  If stack
10911              is aligned without drap, use stack pointer + offset to
10912              access stack variables.  */
10913           if (crtl->stack_realign_tried
10914               && reg == frame_pointer_rtx)
10915             {
10916               int base_reg
10917                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10918                                       ? HARD_FRAME_POINTER_REGNUM
10919                                       : STACK_POINTER_REGNUM);
10920               return new_reg_loc_descr (base_reg, offset);
10921             }
10922
10923           offset += frame_pointer_fb_offset;
10924           return new_loc_descr (DW_OP_fbreg, offset, 0);
10925         }
10926     }
10927   else if (fde
10928            && fde->drap_reg != INVALID_REGNUM
10929            && (fde->drap_reg == REGNO (reg)
10930                || fde->vdrap_reg == REGNO (reg)))
10931     {
10932       /* Use cfa+offset to represent the location of arguments passed
10933          on stack when drap is used to align stack.  */
10934       return new_loc_descr (DW_OP_fbreg, offset, 0);
10935     }
10936
10937   regno = dbx_reg_number (reg);
10938   if (regno <= 31)
10939     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10940                             offset, 0);
10941   else
10942     result = new_loc_descr (DW_OP_bregx, regno, offset);
10943
10944   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10945     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10946
10947   return result;
10948 }
10949
10950 /* Return true if this RTL expression describes a base+offset calculation.  */
10951
10952 static inline int
10953 is_based_loc (const_rtx rtl)
10954 {
10955   return (GET_CODE (rtl) == PLUS
10956           && ((REG_P (XEXP (rtl, 0))
10957                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10958                && CONST_INT_P (XEXP (rtl, 1)))));
10959 }
10960
10961 /* Return a descriptor that describes the concatenation of N locations
10962    used to form the address of a memory location.  */
10963
10964 static dw_loc_descr_ref
10965 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10966                             enum var_init_status initialized)
10967 {
10968   unsigned int i;
10969   dw_loc_descr_ref cc_loc_result = NULL;
10970   unsigned int n = XVECLEN (concatn, 0);
10971
10972   for (i = 0; i < n; ++i)
10973     {
10974       dw_loc_descr_ref ref;
10975       rtx x = XVECEXP (concatn, 0, i);
10976
10977       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10978       if (ref == NULL)
10979         return NULL;
10980
10981       add_loc_descr (&cc_loc_result, ref);
10982       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10983     }
10984
10985   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10986     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10987
10988   return cc_loc_result;
10989 }
10990
10991 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10992    failed.  */
10993
10994 static dw_loc_descr_ref
10995 tls_mem_loc_descriptor (rtx mem)
10996 {
10997   tree base;
10998   dw_loc_descr_ref loc_result;
10999
11000   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
11001     return NULL;
11002
11003   base = get_base_address (MEM_EXPR (mem));
11004   if (base == NULL
11005       || TREE_CODE (base) != VAR_DECL
11006       || !DECL_THREAD_LOCAL_P (base))
11007     return NULL;
11008
11009   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
11010   if (loc_result == NULL)
11011     return NULL;
11012
11013   if (INTVAL (MEM_OFFSET (mem)))
11014     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
11015
11016   return loc_result;
11017 }
11018
11019 /* The following routine converts the RTL for a variable or parameter
11020    (resident in memory) into an equivalent Dwarf representation of a
11021    mechanism for getting the address of that same variable onto the top of a
11022    hypothetical "address evaluation" stack.
11023
11024    When creating memory location descriptors, we are effectively transforming
11025    the RTL for a memory-resident object into its Dwarf postfix expression
11026    equivalent.  This routine recursively descends an RTL tree, turning
11027    it into Dwarf postfix code as it goes.
11028
11029    MODE is the mode of the memory reference, needed to handle some
11030    autoincrement addressing modes.
11031
11032    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
11033    location list for RTL.
11034
11035    Return 0 if we can't represent the location.  */
11036
11037 static dw_loc_descr_ref
11038 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11039                     enum var_init_status initialized)
11040 {
11041   dw_loc_descr_ref mem_loc_result = NULL;
11042   enum dwarf_location_atom op;
11043   dw_loc_descr_ref op0, op1;
11044
11045   /* Note that for a dynamically sized array, the location we will generate a
11046      description of here will be the lowest numbered location which is
11047      actually within the array.  That's *not* necessarily the same as the
11048      zeroth element of the array.  */
11049
11050   rtl = targetm.delegitimize_address (rtl);
11051
11052   switch (GET_CODE (rtl))
11053     {
11054     case POST_INC:
11055     case POST_DEC:
11056     case POST_MODIFY:
11057       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
11058          just fall into the SUBREG code.  */
11059
11060       /* ... fall through ...  */
11061
11062     case SUBREG:
11063       /* The case of a subreg may arise when we have a local (register)
11064          variable or a formal (register) parameter which doesn't quite fill
11065          up an entire register.  For now, just assume that it is
11066          legitimate to make the Dwarf info refer to the whole register which
11067          contains the given subreg.  */
11068       rtl = XEXP (rtl, 0);
11069       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
11070         break;
11071
11072       /* ... fall through ...  */
11073
11074     case REG:
11075       /* Whenever a register number forms a part of the description of the
11076          method for calculating the (dynamic) address of a memory resident
11077          object, DWARF rules require the register number be referred to as
11078          a "base register".  This distinction is not based in any way upon
11079          what category of register the hardware believes the given register
11080          belongs to.  This is strictly DWARF terminology we're dealing with
11081          here. Note that in cases where the location of a memory-resident
11082          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11083          OP_CONST (0)) the actual DWARF location descriptor that we generate
11084          may just be OP_BASEREG (basereg).  This may look deceptively like
11085          the object in question was allocated to a register (rather than in
11086          memory) so DWARF consumers need to be aware of the subtle
11087          distinction between OP_REG and OP_BASEREG.  */
11088       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11089         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11090       else if (stack_realign_drap
11091                && crtl->drap_reg
11092                && crtl->args.internal_arg_pointer == rtl
11093                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11094         {
11095           /* If RTL is internal_arg_pointer, which has been optimized
11096              out, use DRAP instead.  */
11097           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11098                                             VAR_INIT_STATUS_INITIALIZED);
11099         }
11100       break;
11101
11102     case SIGN_EXTEND:
11103     case ZERO_EXTEND:
11104       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11105                                 VAR_INIT_STATUS_INITIALIZED);
11106       if (op0 == 0)
11107         break;
11108       else
11109         {
11110           int shift = DWARF2_ADDR_SIZE
11111                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11112           shift *= BITS_PER_UNIT;
11113           if (GET_CODE (rtl) == SIGN_EXTEND)
11114             op = DW_OP_shra;
11115           else
11116             op = DW_OP_shr;
11117           mem_loc_result = op0;
11118           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11119           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11120           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11121           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11122         }
11123       break;
11124
11125     case MEM:
11126       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11127                                            VAR_INIT_STATUS_INITIALIZED);
11128       if (mem_loc_result == NULL)
11129         mem_loc_result = tls_mem_loc_descriptor (rtl);
11130       if (mem_loc_result != 0)
11131         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11132       break;
11133
11134     case LO_SUM:
11135          rtl = XEXP (rtl, 1);
11136
11137       /* ... fall through ...  */
11138
11139     case LABEL_REF:
11140       /* Some ports can transform a symbol ref into a label ref, because
11141          the symbol ref is too far away and has to be dumped into a constant
11142          pool.  */
11143     case CONST:
11144     case SYMBOL_REF:
11145       /* Alternatively, the symbol in the constant pool might be referenced
11146          by a different symbol.  */
11147       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
11148         {
11149           bool marked;
11150           rtx tmp = get_pool_constant_mark (rtl, &marked);
11151
11152           if (GET_CODE (tmp) == SYMBOL_REF)
11153             {
11154               rtl = tmp;
11155               if (CONSTANT_POOL_ADDRESS_P (tmp))
11156                 get_pool_constant_mark (tmp, &marked);
11157               else
11158                 marked = true;
11159             }
11160
11161           /* If all references to this pool constant were optimized away,
11162              it was not output and thus we can't represent it.
11163              FIXME: might try to use DW_OP_const_value here, though
11164              DW_OP_piece complicates it.  */
11165           if (!marked)
11166             return 0;
11167         }
11168
11169       if (GET_CODE (rtl) == SYMBOL_REF
11170           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11171         {
11172           dw_loc_descr_ref temp;
11173
11174           /* If this is not defined, we have no way to emit the data.  */
11175           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11176             break;
11177
11178           temp = new_loc_descr (DW_OP_addr, 0, 0);
11179           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11180           temp->dw_loc_oprnd1.v.val_addr = rtl;
11181           temp->dtprel = true;
11182
11183           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11184           add_loc_descr (&mem_loc_result, temp);
11185
11186           break;
11187         }
11188
11189     symref:
11190       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11191       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11192       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11193       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11194       break;
11195
11196     case PRE_MODIFY:
11197       /* Extract the PLUS expression nested inside and fall into
11198          PLUS code below.  */
11199       rtl = XEXP (rtl, 1);
11200       goto plus;
11201
11202     case PRE_INC:
11203     case PRE_DEC:
11204       /* Turn these into a PLUS expression and fall into the PLUS code
11205          below.  */
11206       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
11207                           GEN_INT (GET_CODE (rtl) == PRE_INC
11208                                    ? GET_MODE_UNIT_SIZE (mode)
11209                                    : -GET_MODE_UNIT_SIZE (mode)));
11210
11211       /* ... fall through ...  */
11212
11213     case PLUS:
11214     plus:
11215       if (is_based_loc (rtl))
11216         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11217                                           INTVAL (XEXP (rtl, 1)),
11218                                           VAR_INIT_STATUS_INITIALIZED);
11219       else
11220         {
11221           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
11222                                                VAR_INIT_STATUS_INITIALIZED);
11223           if (mem_loc_result == 0)
11224             break;
11225
11226           if (CONST_INT_P (XEXP (rtl, 1)))
11227             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11228           else
11229             {
11230               dw_loc_descr_ref mem_loc_result2
11231                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11232                                       VAR_INIT_STATUS_INITIALIZED);
11233               if (mem_loc_result2 == 0)
11234                 break;
11235               add_loc_descr (&mem_loc_result, mem_loc_result2);
11236               add_loc_descr (&mem_loc_result,
11237                              new_loc_descr (DW_OP_plus, 0, 0));
11238             }
11239         }
11240       break;
11241
11242     /* If a pseudo-reg is optimized away, it is possible for it to
11243        be replaced with a MEM containing a multiply or shift.  */
11244     case MINUS:
11245       op = DW_OP_minus;
11246       goto do_binop;
11247
11248     case MULT:
11249       op = DW_OP_mul;
11250       goto do_binop;
11251
11252     case DIV:
11253       op = DW_OP_div;
11254       goto do_binop;
11255
11256     case MOD:
11257       op = DW_OP_mod;
11258       goto do_binop;
11259
11260     case ASHIFT:
11261       op = DW_OP_shl;
11262       goto do_binop;
11263
11264     case ASHIFTRT:
11265       op = DW_OP_shra;
11266       goto do_binop;
11267
11268     case LSHIFTRT:
11269       op = DW_OP_shr;
11270       goto do_binop;
11271
11272     case AND:
11273       op = DW_OP_and;
11274       goto do_binop;
11275
11276     case IOR:
11277       op = DW_OP_or;
11278       goto do_binop;
11279
11280     case XOR:
11281       op = DW_OP_xor;
11282       goto do_binop;
11283
11284     do_binop:
11285       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11286                                 VAR_INIT_STATUS_INITIALIZED);
11287       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11288                                 VAR_INIT_STATUS_INITIALIZED);
11289
11290       if (op0 == 0 || op1 == 0)
11291         break;
11292
11293       mem_loc_result = op0;
11294       add_loc_descr (&mem_loc_result, op1);
11295       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11296       break;
11297
11298     case NOT:
11299       op = DW_OP_not;
11300       goto do_unop;
11301
11302     case ABS:
11303       op = DW_OP_abs;
11304       goto do_unop;
11305
11306     case NEG:
11307       op = DW_OP_neg;
11308       goto do_unop;
11309
11310     do_unop:
11311       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11312                                 VAR_INIT_STATUS_INITIALIZED);
11313
11314       if (op0 == 0)
11315         break;
11316
11317       mem_loc_result = op0;
11318       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11319       break;
11320
11321     case CONST_INT:
11322       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11323       break;
11324
11325     case CONCATN:
11326       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
11327                                                    VAR_INIT_STATUS_INITIALIZED);
11328       break;
11329
11330     case EQ:
11331       op = DW_OP_eq;
11332       goto do_scompare;
11333
11334     case GE:
11335       op = DW_OP_ge;
11336       goto do_scompare;
11337
11338     case GT:
11339       op = DW_OP_gt;
11340       goto do_scompare;
11341
11342     case LE:
11343       op = DW_OP_le;
11344       goto do_scompare;
11345
11346     case LT:
11347       op = DW_OP_lt;
11348       goto do_scompare;
11349
11350     case NE:
11351       op = DW_OP_ne;
11352       goto do_scompare;
11353
11354     do_scompare:
11355       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11356           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11357           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11358         break;
11359
11360       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11361                                 VAR_INIT_STATUS_INITIALIZED);
11362       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11363                                 VAR_INIT_STATUS_INITIALIZED);
11364
11365       if (op0 == 0 || op1 == 0)
11366         break;
11367
11368       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11369         {
11370           int shift = DWARF2_ADDR_SIZE
11371                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11372           shift *= BITS_PER_UNIT;
11373           add_loc_descr (&op0, int_loc_descriptor (shift));
11374           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11375           if (CONST_INT_P (XEXP (rtl, 1)))
11376             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11377           else
11378             {
11379               add_loc_descr (&op1, int_loc_descriptor (shift));
11380               add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11381             }
11382         }
11383
11384     do_compare:
11385       mem_loc_result = op0;
11386       add_loc_descr (&mem_loc_result, op1);
11387       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11388       if (STORE_FLAG_VALUE != 1)
11389         {
11390           add_loc_descr (&mem_loc_result,
11391                          int_loc_descriptor (STORE_FLAG_VALUE));
11392           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11393         }
11394       break;
11395
11396     case GEU:
11397       op = DW_OP_ge;
11398       goto do_ucompare;
11399
11400     case GTU:
11401       op = DW_OP_gt;
11402       goto do_ucompare;
11403
11404     case LEU:
11405       op = DW_OP_le;
11406       goto do_ucompare;
11407
11408     case LTU:
11409       op = DW_OP_lt;
11410       goto do_ucompare;
11411
11412     do_ucompare:
11413       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11414           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11415           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11416         break;
11417
11418       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11419                                 VAR_INIT_STATUS_INITIALIZED);
11420       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11421                                 VAR_INIT_STATUS_INITIALIZED);
11422
11423       if (op0 == 0 || op1 == 0)
11424         break;
11425
11426       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11427         {
11428           HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11429           add_loc_descr (&op0, int_loc_descriptor (mask));
11430           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11431           if (CONST_INT_P (XEXP (rtl, 1)))
11432             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11433           else
11434             {
11435               add_loc_descr (&op1, int_loc_descriptor (mask));
11436               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11437             }
11438         }
11439       else
11440         {
11441           HOST_WIDE_INT bias = 1;
11442           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11443           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11444           if (CONST_INT_P (XEXP (rtl, 1)))
11445             op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11446                                       + INTVAL (XEXP (rtl, 1)));
11447           else
11448             add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11449         }
11450       goto do_compare;
11451
11452     case SMIN:
11453     case SMAX:
11454     case UMIN:
11455     case UMAX:
11456       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11457           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11458           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11459         break;
11460
11461       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11462                                 VAR_INIT_STATUS_INITIALIZED);
11463       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11464                                 VAR_INIT_STATUS_INITIALIZED);
11465
11466       if (op0 == 0 || op1 == 0)
11467         break;
11468
11469       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11470       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11471       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11472       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11473         {
11474           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11475             {
11476               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11477               add_loc_descr (&op0, int_loc_descriptor (mask));
11478               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11479               add_loc_descr (&op1, int_loc_descriptor (mask));
11480               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11481             }
11482           else
11483             {
11484               HOST_WIDE_INT bias = 1;
11485               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11486               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11487               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11488             }
11489         }
11490       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11491         {
11492           int shift = DWARF2_ADDR_SIZE
11493                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11494           shift *= BITS_PER_UNIT;
11495           add_loc_descr (&op0, int_loc_descriptor (shift));
11496           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11497           add_loc_descr (&op1, int_loc_descriptor (shift));
11498           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11499         }
11500
11501       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11502         op = DW_OP_lt;
11503       else
11504         op = DW_OP_gt;
11505       mem_loc_result = op0;
11506       add_loc_descr (&mem_loc_result, op1);
11507       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11508       {
11509         dw_loc_descr_ref bra_node, drop_node;
11510
11511         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11512         add_loc_descr (&mem_loc_result, bra_node);
11513         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11514         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11515         add_loc_descr (&mem_loc_result, drop_node);
11516         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11517         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11518       }
11519       break;
11520
11521     case ZERO_EXTRACT:
11522     case SIGN_EXTRACT:
11523       if (CONST_INT_P (XEXP (rtl, 1))
11524           && CONST_INT_P (XEXP (rtl, 2))
11525           && ((unsigned) INTVAL (XEXP (rtl, 1))
11526               + (unsigned) INTVAL (XEXP (rtl, 2))
11527               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
11528           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
11529           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11530         {
11531           int shift, size;
11532           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11533                                     VAR_INIT_STATUS_INITIALIZED);
11534           if (op0 == 0)
11535             break;
11536           if (GET_CODE (rtl) == SIGN_EXTRACT)
11537             op = DW_OP_shra;
11538           else
11539             op = DW_OP_shr;
11540           mem_loc_result = op0;
11541           size = INTVAL (XEXP (rtl, 1));
11542           shift = INTVAL (XEXP (rtl, 2));
11543           if (BITS_BIG_ENDIAN)
11544             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11545                     - shift - size;
11546           add_loc_descr (&mem_loc_result,
11547                          int_loc_descriptor (DWARF2_ADDR_SIZE - shift - size));
11548           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11549           add_loc_descr (&mem_loc_result,
11550                          int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11551           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11552         }
11553       break;
11554
11555     case COMPARE:
11556     case IF_THEN_ELSE:
11557     case ROTATE:
11558     case ROTATERT:
11559     case TRUNCATE:
11560       /* In theory, we could implement the above.  */
11561       /* DWARF cannot represent the unsigned compare operations
11562          natively.  */
11563     case SS_MULT:
11564     case US_MULT:
11565     case SS_DIV:
11566     case US_DIV:
11567     case UDIV:
11568     case UMOD:
11569     case UNORDERED:
11570     case ORDERED:
11571     case UNEQ:
11572     case UNGE:
11573     case UNLE:
11574     case UNLT:
11575     case LTGT:
11576     case FLOAT_EXTEND:
11577     case FLOAT_TRUNCATE:
11578     case FLOAT:
11579     case UNSIGNED_FLOAT:
11580     case FIX:
11581     case UNSIGNED_FIX:
11582     case FRACT_CONVERT:
11583     case UNSIGNED_FRACT_CONVERT:
11584     case SAT_FRACT:
11585     case UNSIGNED_SAT_FRACT:
11586     case SQRT:
11587     case BSWAP:
11588     case FFS:
11589     case CLZ:
11590     case CTZ:
11591     case POPCOUNT:
11592     case PARITY:
11593     case ASM_OPERANDS:
11594     case UNSPEC:
11595       /* If delegitimize_address couldn't do anything with the UNSPEC, we
11596          can't express it in the debug info.  This can happen e.g. with some
11597          TLS UNSPECs.  */
11598       break;
11599
11600     case CONST_STRING:
11601       rtl = get_debug_string_label (XSTR (rtl, 0));
11602       goto symref;
11603
11604     default:
11605 #ifdef ENABLE_CHECKING
11606       print_rtl (stderr, rtl);
11607       gcc_unreachable ();
11608 #else
11609       break;
11610 #endif
11611     }
11612
11613   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11614     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11615
11616   return mem_loc_result;
11617 }
11618
11619 /* Return a descriptor that describes the concatenation of two locations.
11620    This is typically a complex variable.  */
11621
11622 static dw_loc_descr_ref
11623 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
11624 {
11625   dw_loc_descr_ref cc_loc_result = NULL;
11626   dw_loc_descr_ref x0_ref
11627     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11628   dw_loc_descr_ref x1_ref
11629     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11630
11631   if (x0_ref == 0 || x1_ref == 0)
11632     return 0;
11633
11634   cc_loc_result = x0_ref;
11635   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
11636
11637   add_loc_descr (&cc_loc_result, x1_ref);
11638   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
11639
11640   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11641     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11642
11643   return cc_loc_result;
11644 }
11645
11646 /* Return a descriptor that describes the concatenation of N
11647    locations.  */
11648
11649 static dw_loc_descr_ref
11650 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
11651 {
11652   unsigned int i;
11653   dw_loc_descr_ref cc_loc_result = NULL;
11654   unsigned int n = XVECLEN (concatn, 0);
11655
11656   for (i = 0; i < n; ++i)
11657     {
11658       dw_loc_descr_ref ref;
11659       rtx x = XVECEXP (concatn, 0, i);
11660
11661       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11662       if (ref == NULL)
11663         return NULL;
11664
11665       add_loc_descr (&cc_loc_result, ref);
11666       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
11667     }
11668
11669   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11670     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11671
11672   return cc_loc_result;
11673 }
11674
11675 /* Output a proper Dwarf location descriptor for a variable or parameter
11676    which is either allocated in a register or in a memory location.  For a
11677    register, we just generate an OP_REG and the register number.  For a
11678    memory location we provide a Dwarf postfix expression describing how to
11679    generate the (dynamic) address of the object onto the address stack.
11680
11681    MODE is mode of the decl if this loc_descriptor is going to be used in
11682    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
11683    allowed, VOIDmode otherwise.
11684
11685    If we don't know how to describe it, return 0.  */
11686
11687 static dw_loc_descr_ref
11688 loc_descriptor (rtx rtl, enum machine_mode mode,
11689                 enum var_init_status initialized)
11690 {
11691   dw_loc_descr_ref loc_result = NULL;
11692
11693   switch (GET_CODE (rtl))
11694     {
11695     case SUBREG:
11696       /* The case of a subreg may arise when we have a local (register)
11697          variable or a formal (register) parameter which doesn't quite fill
11698          up an entire register.  For now, just assume that it is
11699          legitimate to make the Dwarf info refer to the whole register which
11700          contains the given subreg.  */
11701       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
11702       break;
11703
11704     case REG:
11705       loc_result = reg_loc_descriptor (rtl, initialized);
11706       break;
11707
11708     case SIGN_EXTEND:
11709     case ZERO_EXTEND:
11710       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
11711       break;
11712
11713     case MEM:
11714       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11715                                        initialized);
11716       if (loc_result == NULL)
11717         loc_result = tls_mem_loc_descriptor (rtl);
11718       break;
11719
11720     case CONCAT:
11721       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
11722                                           initialized);
11723       break;
11724
11725     case CONCATN:
11726       loc_result = concatn_loc_descriptor (rtl, initialized);
11727       break;
11728
11729     case VAR_LOCATION:
11730       /* Single part.  */
11731       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
11732         {
11733           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), mode,
11734                                        initialized);
11735           break;
11736         }
11737
11738       rtl = XEXP (rtl, 1);
11739       /* FALLTHRU */
11740
11741     case PARALLEL:
11742       {
11743         rtvec par_elems = XVEC (rtl, 0);
11744         int num_elem = GET_NUM_ELEM (par_elems);
11745         enum machine_mode mode;
11746         int i;
11747
11748         /* Create the first one, so we have something to add to.  */
11749         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
11750                                      VOIDmode, initialized);
11751         if (loc_result == NULL)
11752           return NULL;
11753         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
11754         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11755         for (i = 1; i < num_elem; i++)
11756           {
11757             dw_loc_descr_ref temp;
11758
11759             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
11760                                    VOIDmode, initialized);
11761             if (temp == NULL)
11762               return NULL;
11763             add_loc_descr (&loc_result, temp);
11764             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
11765             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11766           }
11767       }
11768       break;
11769
11770     case CONST_INT:
11771       if (mode != VOIDmode && mode != BLKmode && dwarf_version >= 4)
11772         {
11773           HOST_WIDE_INT i = INTVAL (rtl);
11774           int litsize;
11775           if (i >= 0)
11776             {
11777               if (i <= 31)
11778                 litsize = 1;
11779               else if (i <= 0xff)
11780                 litsize = 2;
11781               else if (i <= 0xffff)
11782                 litsize = 3;
11783               else if (HOST_BITS_PER_WIDE_INT == 32
11784                        || i <= 0xffffffff)
11785                 litsize = 5;
11786               else
11787                 litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11788             }
11789           else
11790             {
11791               if (i >= -0x80)
11792                 litsize = 2;
11793               else if (i >= -0x8000)
11794                 litsize = 3;
11795               else if (HOST_BITS_PER_WIDE_INT == 32
11796                        || i >= -0x80000000)
11797                 litsize = 5;
11798               else
11799                 litsize = 1 + size_of_sleb128 (i);
11800             }
11801           /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11802              is more compact.  For DW_OP_stack_value we need:
11803              litsize + 1 (DW_OP_stack_value) + 1 (DW_OP_bit_size)
11804              + 1 (mode size)
11805              and for DW_OP_implicit_value:
11806              1 (DW_OP_implicit_value) + 1 (length) + mode_size.  */
11807           if (DWARF2_ADDR_SIZE >= GET_MODE_SIZE (mode)
11808               && litsize + 1 + 1 + 1 < 1 + 1 + GET_MODE_SIZE (mode))
11809             {
11810               loc_result = int_loc_descriptor (i);
11811               add_loc_descr (&loc_result,
11812                              new_loc_descr (DW_OP_stack_value, 0, 0));
11813               add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11814               return loc_result;
11815             }
11816
11817           loc_result = new_loc_descr (DW_OP_implicit_value,
11818                                       GET_MODE_SIZE (mode), 0);
11819           loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11820           loc_result->dw_loc_oprnd2.v.val_int = i;
11821         }
11822       break;
11823
11824     case CONST_DOUBLE:
11825       if (mode != VOIDmode && dwarf_version >= 4)
11826         {
11827           /* Note that a CONST_DOUBLE rtx could represent either an integer
11828              or a floating-point constant.  A CONST_DOUBLE is used whenever
11829              the constant requires more than one word in order to be
11830              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11831           if (GET_MODE (rtl) != VOIDmode)
11832             mode = GET_MODE (rtl);
11833
11834           loc_result = new_loc_descr (DW_OP_implicit_value,
11835                                       GET_MODE_SIZE (mode), 0);
11836           if (SCALAR_FLOAT_MODE_P (mode))
11837             {
11838               unsigned int length = GET_MODE_SIZE (mode);
11839               unsigned char *array = GGC_NEWVEC (unsigned char, length);
11840
11841               insert_float (rtl, array);
11842               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11843               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11844               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11845               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11846             }
11847           else
11848             {
11849               loc_result->dw_loc_oprnd2.val_class = dw_val_class_long_long;
11850               loc_result->dw_loc_oprnd2.v.val_long_long = rtl;
11851             }
11852         }
11853       break;
11854
11855     case CONST_VECTOR:
11856       if (mode != VOIDmode && dwarf_version >= 4)
11857         {
11858           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
11859           unsigned int length = CONST_VECTOR_NUNITS (rtl);
11860           unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11861           unsigned int i;
11862           unsigned char *p;
11863
11864           mode = GET_MODE (rtl);
11865           switch (GET_MODE_CLASS (mode))
11866             {
11867             case MODE_VECTOR_INT:
11868               for (i = 0, p = array; i < length; i++, p += elt_size)
11869                 {
11870                   rtx elt = CONST_VECTOR_ELT (rtl, i);
11871                   HOST_WIDE_INT lo, hi;
11872
11873                   switch (GET_CODE (elt))
11874                     {
11875                     case CONST_INT:
11876                       lo = INTVAL (elt);
11877                       hi = -(lo < 0);
11878                       break;
11879
11880                     case CONST_DOUBLE:
11881                       lo = CONST_DOUBLE_LOW (elt);
11882                       hi = CONST_DOUBLE_HIGH (elt);
11883                       break;
11884
11885                     default:
11886                       gcc_unreachable ();
11887                     }
11888
11889                   if (elt_size <= sizeof (HOST_WIDE_INT))
11890                     insert_int (lo, elt_size, p);
11891                   else
11892                     {
11893                       unsigned char *p0 = p;
11894                       unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11895
11896                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11897                       if (WORDS_BIG_ENDIAN)
11898                         {
11899                           p0 = p1;
11900                           p1 = p;
11901                         }
11902                       insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11903                       insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11904                     }
11905                 }
11906               break;
11907
11908             case MODE_VECTOR_FLOAT:
11909               for (i = 0, p = array; i < length; i++, p += elt_size)
11910                 {
11911                   rtx elt = CONST_VECTOR_ELT (rtl, i);
11912                   insert_float (elt, p);
11913                 }
11914               break;
11915
11916             default:
11917               gcc_unreachable ();
11918             }
11919
11920           loc_result = new_loc_descr (DW_OP_implicit_value,
11921                                       length * elt_size, 0);
11922           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11923           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
11924           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
11925           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11926         }
11927       break;
11928
11929     case CONST:
11930       if (mode == VOIDmode
11931           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
11932           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
11933           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
11934         {
11935           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
11936           break;
11937         }
11938       /* FALLTHROUGH */
11939     case SYMBOL_REF:
11940       if (GET_CODE (rtl) == SYMBOL_REF
11941           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11942         break;
11943     case LABEL_REF:
11944       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
11945           && dwarf_version >= 4)
11946         {
11947           loc_result = new_loc_descr (DW_OP_implicit_value,
11948                                       DWARF2_ADDR_SIZE, 0);
11949           loc_result->dw_loc_oprnd2.val_class = dw_val_class_addr;
11950           loc_result->dw_loc_oprnd2.v.val_addr = rtl;
11951           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11952         }
11953       break;
11954
11955     default:
11956       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
11957           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
11958           && dwarf_version >= 4)
11959         {
11960           /* Value expression.  */
11961           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
11962           if (loc_result)
11963             {
11964               add_loc_descr (&loc_result,
11965                              new_loc_descr (DW_OP_stack_value, 0, 0));
11966               add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11967             }
11968         }
11969       break;
11970     }
11971
11972   return loc_result;
11973 }
11974
11975 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
11976    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
11977    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
11978    top-level invocation, and we require the address of LOC; is 0 if we require
11979    the value of LOC.  */
11980
11981 static dw_loc_descr_ref
11982 loc_descriptor_from_tree_1 (tree loc, int want_address)
11983 {
11984   dw_loc_descr_ref ret, ret1;
11985   int have_address = 0;
11986   enum dwarf_location_atom op;
11987
11988   /* ??? Most of the time we do not take proper care for sign/zero
11989      extending the values properly.  Hopefully this won't be a real
11990      problem...  */
11991
11992   switch (TREE_CODE (loc))
11993     {
11994     case ERROR_MARK:
11995       return 0;
11996
11997     case PLACEHOLDER_EXPR:
11998       /* This case involves extracting fields from an object to determine the
11999          position of other fields.  We don't try to encode this here.  The
12000          only user of this is Ada, which encodes the needed information using
12001          the names of types.  */
12002       return 0;
12003
12004     case CALL_EXPR:
12005       return 0;
12006
12007     case PREINCREMENT_EXPR:
12008     case PREDECREMENT_EXPR:
12009     case POSTINCREMENT_EXPR:
12010     case POSTDECREMENT_EXPR:
12011       /* There are no opcodes for these operations.  */
12012       return 0;
12013
12014     case ADDR_EXPR:
12015       /* If we already want an address, there's nothing we can do.  */
12016       if (want_address)
12017         return 0;
12018
12019       /* Otherwise, process the argument and look for the address.  */
12020       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
12021
12022     case VAR_DECL:
12023       if (DECL_THREAD_LOCAL_P (loc))
12024         {
12025           rtx rtl;
12026           enum dwarf_location_atom first_op;
12027           enum dwarf_location_atom second_op;
12028           bool dtprel = false;
12029
12030           if (targetm.have_tls)
12031             {
12032               /* If this is not defined, we have no way to emit the
12033                  data.  */
12034               if (!targetm.asm_out.output_dwarf_dtprel)
12035                 return 0;
12036
12037                /* The way DW_OP_GNU_push_tls_address is specified, we
12038                   can only look up addresses of objects in the current
12039                   module.  */
12040               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
12041                 return 0;
12042               first_op = DW_OP_addr;
12043               dtprel = true;
12044               second_op = DW_OP_GNU_push_tls_address;
12045             }
12046           else
12047             {
12048               if (!targetm.emutls.debug_form_tls_address)
12049                 return 0;
12050               loc = emutls_decl (loc);
12051               first_op = DW_OP_addr;
12052               second_op = DW_OP_form_tls_address;
12053             }
12054
12055           rtl = rtl_for_decl_location (loc);
12056           if (rtl == NULL_RTX)
12057             return 0;
12058
12059           if (!MEM_P (rtl))
12060             return 0;
12061           rtl = XEXP (rtl, 0);
12062           if (! CONSTANT_P (rtl))
12063             return 0;
12064
12065           ret = new_loc_descr (first_op, 0, 0);
12066           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12067           ret->dw_loc_oprnd1.v.val_addr = rtl;
12068           ret->dtprel = dtprel;
12069
12070           ret1 = new_loc_descr (second_op, 0, 0);
12071           add_loc_descr (&ret, ret1);
12072
12073           have_address = 1;
12074           break;
12075         }
12076       /* FALLTHRU */
12077
12078     case PARM_DECL:
12079       if (DECL_HAS_VALUE_EXPR_P (loc))
12080         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
12081                                            want_address);
12082       /* FALLTHRU */
12083
12084     case RESULT_DECL:
12085     case FUNCTION_DECL:
12086       {
12087         rtx rtl = rtl_for_decl_location (loc);
12088
12089         if (rtl == NULL_RTX)
12090           return 0;
12091         else if (CONST_INT_P (rtl))
12092           {
12093             HOST_WIDE_INT val = INTVAL (rtl);
12094             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
12095               val &= GET_MODE_MASK (DECL_MODE (loc));
12096             ret = int_loc_descriptor (val);
12097           }
12098         else if (GET_CODE (rtl) == CONST_STRING)
12099           return 0;
12100         else if (CONSTANT_P (rtl))
12101           {
12102             ret = new_loc_descr (DW_OP_addr, 0, 0);
12103             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12104             ret->dw_loc_oprnd1.v.val_addr = rtl;
12105           }
12106         else
12107           {
12108             enum machine_mode mode;
12109
12110             /* Certain constructs can only be represented at top-level.  */
12111             if (want_address == 2)
12112               return loc_descriptor (rtl, VOIDmode,
12113                                      VAR_INIT_STATUS_INITIALIZED);
12114
12115             mode = GET_MODE (rtl);
12116             if (MEM_P (rtl))
12117               {
12118                 rtl = XEXP (rtl, 0);
12119                 have_address = 1;
12120               }
12121             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12122           }
12123       }
12124       break;
12125
12126     case INDIRECT_REF:
12127       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12128       have_address = 1;
12129       break;
12130
12131     case COMPOUND_EXPR:
12132       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
12133
12134     CASE_CONVERT:
12135     case VIEW_CONVERT_EXPR:
12136     case SAVE_EXPR:
12137     case MODIFY_EXPR:
12138       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
12139
12140     case COMPONENT_REF:
12141     case BIT_FIELD_REF:
12142     case ARRAY_REF:
12143     case ARRAY_RANGE_REF:
12144       {
12145         tree obj, offset;
12146         HOST_WIDE_INT bitsize, bitpos, bytepos;
12147         enum machine_mode mode;
12148         int volatilep;
12149         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
12150
12151         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
12152                                    &unsignedp, &volatilep, false);
12153
12154         if (obj == loc)
12155           return 0;
12156
12157         ret = loc_descriptor_from_tree_1 (obj, 1);
12158         if (ret == 0
12159             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
12160           return 0;
12161
12162         if (offset != NULL_TREE)
12163           {
12164             /* Variable offset.  */
12165             ret1 = loc_descriptor_from_tree_1 (offset, 0);
12166             if (ret1 == 0)
12167               return 0;
12168             add_loc_descr (&ret, ret1);
12169             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
12170           }
12171
12172         bytepos = bitpos / BITS_PER_UNIT;
12173         loc_descr_plus_const (&ret, bytepos);
12174
12175         have_address = 1;
12176         break;
12177       }
12178
12179     case INTEGER_CST:
12180       if (host_integerp (loc, 0))
12181         ret = int_loc_descriptor (tree_low_cst (loc, 0));
12182       else
12183         return 0;
12184       break;
12185
12186     case CONSTRUCTOR:
12187       {
12188         /* Get an RTL for this, if something has been emitted.  */
12189         rtx rtl = lookup_constant_def (loc);
12190         enum machine_mode mode;
12191
12192         if (!rtl || !MEM_P (rtl))
12193           return 0;
12194         mode = GET_MODE (rtl);
12195         rtl = XEXP (rtl, 0);
12196         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12197         have_address = 1;
12198         break;
12199       }
12200
12201     case TRUTH_AND_EXPR:
12202     case TRUTH_ANDIF_EXPR:
12203     case BIT_AND_EXPR:
12204       op = DW_OP_and;
12205       goto do_binop;
12206
12207     case TRUTH_XOR_EXPR:
12208     case BIT_XOR_EXPR:
12209       op = DW_OP_xor;
12210       goto do_binop;
12211
12212     case TRUTH_OR_EXPR:
12213     case TRUTH_ORIF_EXPR:
12214     case BIT_IOR_EXPR:
12215       op = DW_OP_or;
12216       goto do_binop;
12217
12218     case FLOOR_DIV_EXPR:
12219     case CEIL_DIV_EXPR:
12220     case ROUND_DIV_EXPR:
12221     case TRUNC_DIV_EXPR:
12222       op = DW_OP_div;
12223       goto do_binop;
12224
12225     case MINUS_EXPR:
12226       op = DW_OP_minus;
12227       goto do_binop;
12228
12229     case FLOOR_MOD_EXPR:
12230     case CEIL_MOD_EXPR:
12231     case ROUND_MOD_EXPR:
12232     case TRUNC_MOD_EXPR:
12233       op = DW_OP_mod;
12234       goto do_binop;
12235
12236     case MULT_EXPR:
12237       op = DW_OP_mul;
12238       goto do_binop;
12239
12240     case LSHIFT_EXPR:
12241       op = DW_OP_shl;
12242       goto do_binop;
12243
12244     case RSHIFT_EXPR:
12245       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
12246       goto do_binop;
12247
12248     case POINTER_PLUS_EXPR:
12249     case PLUS_EXPR:
12250       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
12251           && host_integerp (TREE_OPERAND (loc, 1), 0))
12252         {
12253           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12254           if (ret == 0)
12255             return 0;
12256
12257           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
12258           break;
12259         }
12260
12261       op = DW_OP_plus;
12262       goto do_binop;
12263
12264     case LE_EXPR:
12265       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12266         return 0;
12267
12268       op = DW_OP_le;
12269       goto do_binop;
12270
12271     case GE_EXPR:
12272       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12273         return 0;
12274
12275       op = DW_OP_ge;
12276       goto do_binop;
12277
12278     case LT_EXPR:
12279       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12280         return 0;
12281
12282       op = DW_OP_lt;
12283       goto do_binop;
12284
12285     case GT_EXPR:
12286       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12287         return 0;
12288
12289       op = DW_OP_gt;
12290       goto do_binop;
12291
12292     case EQ_EXPR:
12293       op = DW_OP_eq;
12294       goto do_binop;
12295
12296     case NE_EXPR:
12297       op = DW_OP_ne;
12298       goto do_binop;
12299
12300     do_binop:
12301       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12302       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
12303       if (ret == 0 || ret1 == 0)
12304         return 0;
12305
12306       add_loc_descr (&ret, ret1);
12307       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12308       break;
12309
12310     case TRUTH_NOT_EXPR:
12311     case BIT_NOT_EXPR:
12312       op = DW_OP_not;
12313       goto do_unop;
12314
12315     case ABS_EXPR:
12316       op = DW_OP_abs;
12317       goto do_unop;
12318
12319     case NEGATE_EXPR:
12320       op = DW_OP_neg;
12321       goto do_unop;
12322
12323     do_unop:
12324       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12325       if (ret == 0)
12326         return 0;
12327
12328       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12329       break;
12330
12331     case MIN_EXPR:
12332     case MAX_EXPR:
12333       {
12334         const enum tree_code code =
12335           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
12336
12337         loc = build3 (COND_EXPR, TREE_TYPE (loc),
12338                       build2 (code, integer_type_node,
12339                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
12340                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
12341       }
12342
12343       /* ... fall through ...  */
12344
12345     case COND_EXPR:
12346       {
12347         dw_loc_descr_ref lhs
12348           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
12349         dw_loc_descr_ref rhs
12350           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
12351         dw_loc_descr_ref bra_node, jump_node, tmp;
12352
12353         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12354         if (ret == 0 || lhs == 0 || rhs == 0)
12355           return 0;
12356
12357         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12358         add_loc_descr (&ret, bra_node);
12359
12360         add_loc_descr (&ret, rhs);
12361         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
12362         add_loc_descr (&ret, jump_node);
12363
12364         add_loc_descr (&ret, lhs);
12365         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12366         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
12367
12368         /* ??? Need a node to point the skip at.  Use a nop.  */
12369         tmp = new_loc_descr (DW_OP_nop, 0, 0);
12370         add_loc_descr (&ret, tmp);
12371         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12372         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
12373       }
12374       break;
12375
12376     case FIX_TRUNC_EXPR:
12377       return 0;
12378
12379     default:
12380       /* Leave front-end specific codes as simply unknown.  This comes
12381          up, for instance, with the C STMT_EXPR.  */
12382       if ((unsigned int) TREE_CODE (loc)
12383           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
12384         return 0;
12385
12386 #ifdef ENABLE_CHECKING
12387       /* Otherwise this is a generic code; we should just lists all of
12388          these explicitly.  We forgot one.  */
12389       gcc_unreachable ();
12390 #else
12391       /* In a release build, we want to degrade gracefully: better to
12392          generate incomplete debugging information than to crash.  */
12393       return NULL;
12394 #endif
12395     }
12396
12397   /* Show if we can't fill the request for an address.  */
12398   if (want_address && !have_address)
12399     return 0;
12400
12401   /* If we've got an address and don't want one, dereference.  */
12402   if (!want_address && have_address && ret)
12403     {
12404       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12405
12406       if (size > DWARF2_ADDR_SIZE || size == -1)
12407         return 0;
12408       else if (size == DWARF2_ADDR_SIZE)
12409         op = DW_OP_deref;
12410       else
12411         op = DW_OP_deref_size;
12412
12413       add_loc_descr (&ret, new_loc_descr (op, size, 0));
12414     }
12415
12416   return ret;
12417 }
12418
12419 static inline dw_loc_descr_ref
12420 loc_descriptor_from_tree (tree loc)
12421 {
12422   return loc_descriptor_from_tree_1 (loc, 2);
12423 }
12424
12425 /* Given a value, round it up to the lowest multiple of `boundary'
12426    which is not less than the value itself.  */
12427
12428 static inline HOST_WIDE_INT
12429 ceiling (HOST_WIDE_INT value, unsigned int boundary)
12430 {
12431   return (((value + boundary - 1) / boundary) * boundary);
12432 }
12433
12434 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
12435    pointer to the declared type for the relevant field variable, or return
12436    `integer_type_node' if the given node turns out to be an
12437    ERROR_MARK node.  */
12438
12439 static inline tree
12440 field_type (const_tree decl)
12441 {
12442   tree type;
12443
12444   if (TREE_CODE (decl) == ERROR_MARK)
12445     return integer_type_node;
12446
12447   type = DECL_BIT_FIELD_TYPE (decl);
12448   if (type == NULL_TREE)
12449     type = TREE_TYPE (decl);
12450
12451   return type;
12452 }
12453
12454 /* Given a pointer to a tree node, return the alignment in bits for
12455    it, or else return BITS_PER_WORD if the node actually turns out to
12456    be an ERROR_MARK node.  */
12457
12458 static inline unsigned
12459 simple_type_align_in_bits (const_tree type)
12460 {
12461   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
12462 }
12463
12464 static inline unsigned
12465 simple_decl_align_in_bits (const_tree decl)
12466 {
12467   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
12468 }
12469
12470 /* Return the result of rounding T up to ALIGN.  */
12471
12472 static inline HOST_WIDE_INT
12473 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
12474 {
12475   /* We must be careful if T is negative because HOST_WIDE_INT can be
12476      either "above" or "below" unsigned int as per the C promotion
12477      rules, depending on the host, thus making the signedness of the
12478      direct multiplication and division unpredictable.  */
12479   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
12480
12481   u += align - 1;
12482   u /= align;
12483   u *= align;
12484
12485   return (HOST_WIDE_INT) u;
12486 }
12487
12488 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
12489    lowest addressed byte of the "containing object" for the given FIELD_DECL,
12490    or return 0 if we are unable to determine what that offset is, either
12491    because the argument turns out to be a pointer to an ERROR_MARK node, or
12492    because the offset is actually variable.  (We can't handle the latter case
12493    just yet).  */
12494
12495 static HOST_WIDE_INT
12496 field_byte_offset (const_tree decl)
12497 {
12498   HOST_WIDE_INT object_offset_in_bits;
12499   HOST_WIDE_INT bitpos_int;
12500
12501   if (TREE_CODE (decl) == ERROR_MARK)
12502     return 0;
12503
12504   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
12505
12506   /* We cannot yet cope with fields whose positions are variable, so
12507      for now, when we see such things, we simply return 0.  Someday, we may
12508      be able to handle such cases, but it will be damn difficult.  */
12509   if (! host_integerp (bit_position (decl), 0))
12510     return 0;
12511
12512   bitpos_int = int_bit_position (decl);
12513
12514 #ifdef PCC_BITFIELD_TYPE_MATTERS
12515   if (PCC_BITFIELD_TYPE_MATTERS)
12516     {
12517       tree type;
12518       tree field_size_tree;
12519       HOST_WIDE_INT deepest_bitpos;
12520       unsigned HOST_WIDE_INT field_size_in_bits;
12521       unsigned int type_align_in_bits;
12522       unsigned int decl_align_in_bits;
12523       unsigned HOST_WIDE_INT type_size_in_bits;
12524
12525       type = field_type (decl);
12526       type_size_in_bits = simple_type_size_in_bits (type);
12527       type_align_in_bits = simple_type_align_in_bits (type);
12528
12529       field_size_tree = DECL_SIZE (decl);
12530
12531       /* The size could be unspecified if there was an error, or for
12532          a flexible array member.  */
12533       if (!field_size_tree)
12534         field_size_tree = bitsize_zero_node;
12535
12536       /* If the size of the field is not constant, use the type size.  */
12537       if (host_integerp (field_size_tree, 1))
12538         field_size_in_bits = tree_low_cst (field_size_tree, 1);
12539       else
12540         field_size_in_bits = type_size_in_bits;
12541
12542       decl_align_in_bits = simple_decl_align_in_bits (decl);
12543
12544       /* The GCC front-end doesn't make any attempt to keep track of the
12545          starting bit offset (relative to the start of the containing
12546          structure type) of the hypothetical "containing object" for a
12547          bit-field.  Thus, when computing the byte offset value for the
12548          start of the "containing object" of a bit-field, we must deduce
12549          this information on our own. This can be rather tricky to do in
12550          some cases.  For example, handling the following structure type
12551          definition when compiling for an i386/i486 target (which only
12552          aligns long long's to 32-bit boundaries) can be very tricky:
12553
12554          struct S { int field1; long long field2:31; };
12555
12556          Fortunately, there is a simple rule-of-thumb which can be used
12557          in such cases.  When compiling for an i386/i486, GCC will
12558          allocate 8 bytes for the structure shown above.  It decides to
12559          do this based upon one simple rule for bit-field allocation.
12560          GCC allocates each "containing object" for each bit-field at
12561          the first (i.e. lowest addressed) legitimate alignment boundary
12562          (based upon the required minimum alignment for the declared
12563          type of the field) which it can possibly use, subject to the
12564          condition that there is still enough available space remaining
12565          in the containing object (when allocated at the selected point)
12566          to fully accommodate all of the bits of the bit-field itself.
12567
12568          This simple rule makes it obvious why GCC allocates 8 bytes for
12569          each object of the structure type shown above.  When looking
12570          for a place to allocate the "containing object" for `field2',
12571          the compiler simply tries to allocate a 64-bit "containing
12572          object" at each successive 32-bit boundary (starting at zero)
12573          until it finds a place to allocate that 64- bit field such that
12574          at least 31 contiguous (and previously unallocated) bits remain
12575          within that selected 64 bit field.  (As it turns out, for the
12576          example above, the compiler finds it is OK to allocate the
12577          "containing object" 64-bit field at bit-offset zero within the
12578          structure type.)
12579
12580          Here we attempt to work backwards from the limited set of facts
12581          we're given, and we try to deduce from those facts, where GCC
12582          must have believed that the containing object started (within
12583          the structure type). The value we deduce is then used (by the
12584          callers of this routine) to generate DW_AT_location and
12585          DW_AT_bit_offset attributes for fields (both bit-fields and, in
12586          the case of DW_AT_location, regular fields as well).  */
12587
12588       /* Figure out the bit-distance from the start of the structure to
12589          the "deepest" bit of the bit-field.  */
12590       deepest_bitpos = bitpos_int + field_size_in_bits;
12591
12592       /* This is the tricky part.  Use some fancy footwork to deduce
12593          where the lowest addressed bit of the containing object must
12594          be.  */
12595       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
12596
12597       /* Round up to type_align by default.  This works best for
12598          bitfields.  */
12599       object_offset_in_bits
12600         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
12601
12602       if (object_offset_in_bits > bitpos_int)
12603         {
12604           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
12605
12606           /* Round up to decl_align instead.  */
12607           object_offset_in_bits
12608             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
12609         }
12610     }
12611   else
12612 #endif
12613     object_offset_in_bits = bitpos_int;
12614
12615   return object_offset_in_bits / BITS_PER_UNIT;
12616 }
12617 \f
12618 /* The following routines define various Dwarf attributes and any data
12619    associated with them.  */
12620
12621 /* Add a location description attribute value to a DIE.
12622
12623    This emits location attributes suitable for whole variables and
12624    whole parameters.  Note that the location attributes for struct fields are
12625    generated by the routine `data_member_location_attribute' below.  */
12626
12627 static inline void
12628 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
12629                              dw_loc_descr_ref descr)
12630 {
12631   if (descr != 0)
12632     add_AT_loc (die, attr_kind, descr);
12633 }
12634
12635 /* Attach the specialized form of location attribute used for data members of
12636    struct and union types.  In the special case of a FIELD_DECL node which
12637    represents a bit-field, the "offset" part of this special location
12638    descriptor must indicate the distance in bytes from the lowest-addressed
12639    byte of the containing struct or union type to the lowest-addressed byte of
12640    the "containing object" for the bit-field.  (See the `field_byte_offset'
12641    function above).
12642
12643    For any given bit-field, the "containing object" is a hypothetical object
12644    (of some integral or enum type) within which the given bit-field lives.  The
12645    type of this hypothetical "containing object" is always the same as the
12646    declared type of the individual bit-field itself (for GCC anyway... the
12647    DWARF spec doesn't actually mandate this).  Note that it is the size (in
12648    bytes) of the hypothetical "containing object" which will be given in the
12649    DW_AT_byte_size attribute for this bit-field.  (See the
12650    `byte_size_attribute' function below.)  It is also used when calculating the
12651    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
12652    function below.)  */
12653
12654 static void
12655 add_data_member_location_attribute (dw_die_ref die, tree decl)
12656 {
12657   HOST_WIDE_INT offset;
12658   dw_loc_descr_ref loc_descr = 0;
12659
12660   if (TREE_CODE (decl) == TREE_BINFO)
12661     {
12662       /* We're working on the TAG_inheritance for a base class.  */
12663       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
12664         {
12665           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
12666              aren't at a fixed offset from all (sub)objects of the same
12667              type.  We need to extract the appropriate offset from our
12668              vtable.  The following dwarf expression means
12669
12670                BaseAddr = ObAddr + *((*ObAddr) - Offset)
12671
12672              This is specific to the V3 ABI, of course.  */
12673
12674           dw_loc_descr_ref tmp;
12675
12676           /* Make a copy of the object address.  */
12677           tmp = new_loc_descr (DW_OP_dup, 0, 0);
12678           add_loc_descr (&loc_descr, tmp);
12679
12680           /* Extract the vtable address.  */
12681           tmp = new_loc_descr (DW_OP_deref, 0, 0);
12682           add_loc_descr (&loc_descr, tmp);
12683
12684           /* Calculate the address of the offset.  */
12685           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
12686           gcc_assert (offset < 0);
12687
12688           tmp = int_loc_descriptor (-offset);
12689           add_loc_descr (&loc_descr, tmp);
12690           tmp = new_loc_descr (DW_OP_minus, 0, 0);
12691           add_loc_descr (&loc_descr, tmp);
12692
12693           /* Extract the offset.  */
12694           tmp = new_loc_descr (DW_OP_deref, 0, 0);
12695           add_loc_descr (&loc_descr, tmp);
12696
12697           /* Add it to the object address.  */
12698           tmp = new_loc_descr (DW_OP_plus, 0, 0);
12699           add_loc_descr (&loc_descr, tmp);
12700         }
12701       else
12702         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
12703     }
12704   else
12705     offset = field_byte_offset (decl);
12706
12707   if (! loc_descr)
12708     {
12709       if (dwarf_version > 2)
12710         {
12711           /* Don't need to output a location expression, just the constant. */
12712           add_AT_int (die, DW_AT_data_member_location, offset);
12713           return;
12714         }
12715       else
12716         {
12717           enum dwarf_location_atom op;
12718           
12719           /* The DWARF2 standard says that we should assume that the structure
12720              address is already on the stack, so we can specify a structure
12721              field address by using DW_OP_plus_uconst.  */
12722           
12723 #ifdef MIPS_DEBUGGING_INFO
12724           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
12725              operator correctly.  It works only if we leave the offset on the
12726              stack.  */
12727           op = DW_OP_constu;
12728 #else
12729           op = DW_OP_plus_uconst;
12730 #endif
12731           
12732           loc_descr = new_loc_descr (op, offset, 0);
12733         }
12734     }
12735
12736   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
12737 }
12738
12739 /* Writes integer values to dw_vec_const array.  */
12740
12741 static void
12742 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
12743 {
12744   while (size != 0)
12745     {
12746       *dest++ = val & 0xff;
12747       val >>= 8;
12748       --size;
12749     }
12750 }
12751
12752 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
12753
12754 static HOST_WIDE_INT
12755 extract_int (const unsigned char *src, unsigned int size)
12756 {
12757   HOST_WIDE_INT val = 0;
12758
12759   src += size;
12760   while (size != 0)
12761     {
12762       val <<= 8;
12763       val |= *--src & 0xff;
12764       --size;
12765     }
12766   return val;
12767 }
12768
12769 /* Writes floating point values to dw_vec_const array.  */
12770
12771 static void
12772 insert_float (const_rtx rtl, unsigned char *array)
12773 {
12774   REAL_VALUE_TYPE rv;
12775   long val[4];
12776   int i;
12777
12778   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
12779   real_to_target (val, &rv, GET_MODE (rtl));
12780
12781   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
12782   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
12783     {
12784       insert_int (val[i], 4, array);
12785       array += 4;
12786     }
12787 }
12788
12789 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
12790    does not have a "location" either in memory or in a register.  These
12791    things can arise in GNU C when a constant is passed as an actual parameter
12792    to an inlined function.  They can also arise in C++ where declared
12793    constants do not necessarily get memory "homes".  */
12794
12795 static void
12796 add_const_value_attribute (dw_die_ref die, rtx rtl)
12797 {
12798   switch (GET_CODE (rtl))
12799     {
12800     case CONST_INT:
12801       {
12802         HOST_WIDE_INT val = INTVAL (rtl);
12803
12804         if (val < 0)
12805           add_AT_int (die, DW_AT_const_value, val);
12806         else
12807           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
12808       }
12809       break;
12810
12811     case CONST_DOUBLE:
12812       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
12813          floating-point constant.  A CONST_DOUBLE is used whenever the
12814          constant requires more than one word in order to be adequately
12815          represented.  We output CONST_DOUBLEs as blocks.  */
12816       {
12817         enum machine_mode mode = GET_MODE (rtl);
12818
12819         if (SCALAR_FLOAT_MODE_P (mode))
12820           {
12821             unsigned int length = GET_MODE_SIZE (mode);
12822             unsigned char *array = GGC_NEWVEC (unsigned char, length);
12823
12824             insert_float (rtl, array);
12825             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
12826           }
12827         else
12828           add_AT_long_long (die, DW_AT_const_value, rtl);
12829       }
12830       break;
12831
12832     case CONST_VECTOR:
12833       {
12834         enum machine_mode mode = GET_MODE (rtl);
12835         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
12836         unsigned int length = CONST_VECTOR_NUNITS (rtl);
12837         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
12838         unsigned int i;
12839         unsigned char *p;
12840
12841         switch (GET_MODE_CLASS (mode))
12842           {
12843           case MODE_VECTOR_INT:
12844             for (i = 0, p = array; i < length; i++, p += elt_size)
12845               {
12846                 rtx elt = CONST_VECTOR_ELT (rtl, i);
12847                 HOST_WIDE_INT lo, hi;
12848
12849                 switch (GET_CODE (elt))
12850                   {
12851                   case CONST_INT:
12852                     lo = INTVAL (elt);
12853                     hi = -(lo < 0);
12854                     break;
12855
12856                   case CONST_DOUBLE:
12857                     lo = CONST_DOUBLE_LOW (elt);
12858                     hi = CONST_DOUBLE_HIGH (elt);
12859                     break;
12860
12861                   default:
12862                     gcc_unreachable ();
12863                   }
12864
12865                 if (elt_size <= sizeof (HOST_WIDE_INT))
12866                   insert_int (lo, elt_size, p);
12867                 else
12868                   {
12869                     unsigned char *p0 = p;
12870                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
12871
12872                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12873                     if (WORDS_BIG_ENDIAN)
12874                       {
12875                         p0 = p1;
12876                         p1 = p;
12877                       }
12878                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
12879                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
12880                   }
12881               }
12882             break;
12883
12884           case MODE_VECTOR_FLOAT:
12885             for (i = 0, p = array; i < length; i++, p += elt_size)
12886               {
12887                 rtx elt = CONST_VECTOR_ELT (rtl, i);
12888                 insert_float (elt, p);
12889               }
12890             break;
12891
12892           default:
12893             gcc_unreachable ();
12894           }
12895
12896         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
12897       }
12898       break;
12899
12900     case CONST_STRING:
12901       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
12902       break;
12903
12904     case CONST:
12905       if (CONSTANT_P (XEXP (rtl, 0)))
12906         {
12907           add_const_value_attribute (die, XEXP (rtl, 0));
12908           return;
12909         }
12910       /* FALLTHROUGH */
12911     case SYMBOL_REF:
12912       if (GET_CODE (rtl) == SYMBOL_REF
12913           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12914         break;
12915     case LABEL_REF:
12916       add_AT_addr (die, DW_AT_const_value, rtl);
12917       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12918       break;
12919
12920     case PLUS:
12921       /* In cases where an inlined instance of an inline function is passed
12922          the address of an `auto' variable (which is local to the caller) we
12923          can get a situation where the DECL_RTL of the artificial local
12924          variable (for the inlining) which acts as a stand-in for the
12925          corresponding formal parameter (of the inline function) will look
12926          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
12927          exactly a compile-time constant expression, but it isn't the address
12928          of the (artificial) local variable either.  Rather, it represents the
12929          *value* which the artificial local variable always has during its
12930          lifetime.  We currently have no way to represent such quasi-constant
12931          values in Dwarf, so for now we just punt and generate nothing.  */
12932       break;
12933
12934     default:
12935       /* No other kinds of rtx should be possible here.  */
12936       gcc_unreachable ();
12937     }
12938
12939 }
12940
12941 /* Determine whether the evaluation of EXPR references any variables
12942    or functions which aren't otherwise used (and therefore may not be
12943    output).  */
12944 static tree
12945 reference_to_unused (tree * tp, int * walk_subtrees,
12946                      void * data ATTRIBUTE_UNUSED)
12947 {
12948   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
12949     *walk_subtrees = 0;
12950
12951   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
12952       && ! TREE_ASM_WRITTEN (*tp))
12953     return *tp;
12954   /* ???  The C++ FE emits debug information for using decls, so
12955      putting gcc_unreachable here falls over.  See PR31899.  For now
12956      be conservative.  */
12957   else if (!cgraph_global_info_ready
12958            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
12959     return *tp;
12960   else if (TREE_CODE (*tp) == VAR_DECL)
12961     {
12962       struct varpool_node *node = varpool_node (*tp);
12963       if (!node->needed)
12964         return *tp;
12965     }
12966   else if (TREE_CODE (*tp) == FUNCTION_DECL
12967            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
12968     {
12969       /* The call graph machinery must have finished analyzing,
12970          optimizing and gimplifying the CU by now.
12971          So if *TP has no call graph node associated
12972          to it, it means *TP will not be emitted.  */
12973       if (!cgraph_get_node (*tp))
12974         return *tp;
12975     }
12976   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
12977     return *tp;
12978
12979   return NULL_TREE;
12980 }
12981
12982 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
12983    for use in a later add_const_value_attribute call.  */
12984
12985 static rtx
12986 rtl_for_decl_init (tree init, tree type)
12987 {
12988   rtx rtl = NULL_RTX;
12989
12990   /* If a variable is initialized with a string constant without embedded
12991      zeros, build CONST_STRING.  */
12992   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
12993     {
12994       tree enttype = TREE_TYPE (type);
12995       tree domain = TYPE_DOMAIN (type);
12996       enum machine_mode mode = TYPE_MODE (enttype);
12997
12998       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
12999           && domain
13000           && integer_zerop (TYPE_MIN_VALUE (domain))
13001           && compare_tree_int (TYPE_MAX_VALUE (domain),
13002                                TREE_STRING_LENGTH (init) - 1) == 0
13003           && ((size_t) TREE_STRING_LENGTH (init)
13004               == strlen (TREE_STRING_POINTER (init)) + 1))
13005         rtl = gen_rtx_CONST_STRING (VOIDmode,
13006                                     ggc_strdup (TREE_STRING_POINTER (init)));
13007     }
13008   /* Other aggregates, and complex values, could be represented using
13009      CONCAT: FIXME!  */
13010   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
13011     ;
13012   /* Vectors only work if their mode is supported by the target.
13013      FIXME: generic vectors ought to work too.  */
13014   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
13015     ;
13016   /* If the initializer is something that we know will expand into an
13017      immediate RTL constant, expand it now.  We must be careful not to
13018      reference variables which won't be output.  */
13019   else if (initializer_constant_valid_p (init, type)
13020            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
13021     {
13022       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
13023          possible.  */
13024       if (TREE_CODE (type) == VECTOR_TYPE)
13025         switch (TREE_CODE (init))
13026           {
13027           case VECTOR_CST:
13028             break;
13029           case CONSTRUCTOR:
13030             if (TREE_CONSTANT (init))
13031               {
13032                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
13033                 bool constant_p = true;
13034                 tree value;
13035                 unsigned HOST_WIDE_INT ix;
13036
13037                 /* Even when ctor is constant, it might contain non-*_CST
13038                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
13039                    belong into VECTOR_CST nodes.  */
13040                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
13041                   if (!CONSTANT_CLASS_P (value))
13042                     {
13043                       constant_p = false;
13044                       break;
13045                     }
13046
13047                 if (constant_p)
13048                   {
13049                     init = build_vector_from_ctor (type, elts);
13050                     break;
13051                   }
13052               }
13053             /* FALLTHRU */
13054
13055           default:
13056             return NULL;
13057           }
13058
13059       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
13060
13061       /* If expand_expr returns a MEM, it wasn't immediate.  */
13062       gcc_assert (!rtl || !MEM_P (rtl));
13063     }
13064
13065   return rtl;
13066 }
13067
13068 /* Generate RTL for the variable DECL to represent its location.  */
13069
13070 static rtx
13071 rtl_for_decl_location (tree decl)
13072 {
13073   rtx rtl;
13074
13075   /* Here we have to decide where we are going to say the parameter "lives"
13076      (as far as the debugger is concerned).  We only have a couple of
13077      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
13078
13079      DECL_RTL normally indicates where the parameter lives during most of the
13080      activation of the function.  If optimization is enabled however, this
13081      could be either NULL or else a pseudo-reg.  Both of those cases indicate
13082      that the parameter doesn't really live anywhere (as far as the code
13083      generation parts of GCC are concerned) during most of the function's
13084      activation.  That will happen (for example) if the parameter is never
13085      referenced within the function.
13086
13087      We could just generate a location descriptor here for all non-NULL
13088      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
13089      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
13090      where DECL_RTL is NULL or is a pseudo-reg.
13091
13092      Note however that we can only get away with using DECL_INCOMING_RTL as
13093      a backup substitute for DECL_RTL in certain limited cases.  In cases
13094      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
13095      we can be sure that the parameter was passed using the same type as it is
13096      declared to have within the function, and that its DECL_INCOMING_RTL
13097      points us to a place where a value of that type is passed.
13098
13099      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
13100      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
13101      because in these cases DECL_INCOMING_RTL points us to a value of some
13102      type which is *different* from the type of the parameter itself.  Thus,
13103      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
13104      such cases, the debugger would end up (for example) trying to fetch a
13105      `float' from a place which actually contains the first part of a
13106      `double'.  That would lead to really incorrect and confusing
13107      output at debug-time.
13108
13109      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
13110      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
13111      are a couple of exceptions however.  On little-endian machines we can
13112      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
13113      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
13114      an integral type that is smaller than TREE_TYPE (decl). These cases arise
13115      when (on a little-endian machine) a non-prototyped function has a
13116      parameter declared to be of type `short' or `char'.  In such cases,
13117      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
13118      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
13119      passed `int' value.  If the debugger then uses that address to fetch
13120      a `short' or a `char' (on a little-endian machine) the result will be
13121      the correct data, so we allow for such exceptional cases below.
13122
13123      Note that our goal here is to describe the place where the given formal
13124      parameter lives during most of the function's activation (i.e. between the
13125      end of the prologue and the start of the epilogue).  We'll do that as best
13126      as we can. Note however that if the given formal parameter is modified
13127      sometime during the execution of the function, then a stack backtrace (at
13128      debug-time) will show the function as having been called with the *new*
13129      value rather than the value which was originally passed in.  This happens
13130      rarely enough that it is not a major problem, but it *is* a problem, and
13131      I'd like to fix it.
13132
13133      A future version of dwarf2out.c may generate two additional attributes for
13134      any given DW_TAG_formal_parameter DIE which will describe the "passed
13135      type" and the "passed location" for the given formal parameter in addition
13136      to the attributes we now generate to indicate the "declared type" and the
13137      "active location" for each parameter.  This additional set of attributes
13138      could be used by debuggers for stack backtraces. Separately, note that
13139      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
13140      This happens (for example) for inlined-instances of inline function formal
13141      parameters which are never referenced.  This really shouldn't be
13142      happening.  All PARM_DECL nodes should get valid non-NULL
13143      DECL_INCOMING_RTL values.  FIXME.  */
13144
13145   /* Use DECL_RTL as the "location" unless we find something better.  */
13146   rtl = DECL_RTL_IF_SET (decl);
13147
13148   /* When generating abstract instances, ignore everything except
13149      constants, symbols living in memory, and symbols living in
13150      fixed registers.  */
13151   if (! reload_completed)
13152     {
13153       if (rtl
13154           && (CONSTANT_P (rtl)
13155               || (MEM_P (rtl)
13156                   && CONSTANT_P (XEXP (rtl, 0)))
13157               || (REG_P (rtl)
13158                   && TREE_CODE (decl) == VAR_DECL
13159                   && TREE_STATIC (decl))))
13160         {
13161           rtl = targetm.delegitimize_address (rtl);
13162           return rtl;
13163         }
13164       rtl = NULL_RTX;
13165     }
13166   else if (TREE_CODE (decl) == PARM_DECL)
13167     {
13168       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
13169         {
13170           tree declared_type = TREE_TYPE (decl);
13171           tree passed_type = DECL_ARG_TYPE (decl);
13172           enum machine_mode dmode = TYPE_MODE (declared_type);
13173           enum machine_mode pmode = TYPE_MODE (passed_type);
13174
13175           /* This decl represents a formal parameter which was optimized out.
13176              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
13177              all cases where (rtl == NULL_RTX) just below.  */
13178           if (dmode == pmode)
13179             rtl = DECL_INCOMING_RTL (decl);
13180           else if (SCALAR_INT_MODE_P (dmode)
13181                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
13182                    && DECL_INCOMING_RTL (decl))
13183             {
13184               rtx inc = DECL_INCOMING_RTL (decl);
13185               if (REG_P (inc))
13186                 rtl = inc;
13187               else if (MEM_P (inc))
13188                 {
13189                   if (BYTES_BIG_ENDIAN)
13190                     rtl = adjust_address_nv (inc, dmode,
13191                                              GET_MODE_SIZE (pmode)
13192                                              - GET_MODE_SIZE (dmode));
13193                   else
13194                     rtl = inc;
13195                 }
13196             }
13197         }
13198
13199       /* If the parm was passed in registers, but lives on the stack, then
13200          make a big endian correction if the mode of the type of the
13201          parameter is not the same as the mode of the rtl.  */
13202       /* ??? This is the same series of checks that are made in dbxout.c before
13203          we reach the big endian correction code there.  It isn't clear if all
13204          of these checks are necessary here, but keeping them all is the safe
13205          thing to do.  */
13206       else if (MEM_P (rtl)
13207                && XEXP (rtl, 0) != const0_rtx
13208                && ! CONSTANT_P (XEXP (rtl, 0))
13209                /* Not passed in memory.  */
13210                && !MEM_P (DECL_INCOMING_RTL (decl))
13211                /* Not passed by invisible reference.  */
13212                && (!REG_P (XEXP (rtl, 0))
13213                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
13214                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
13215 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
13216                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
13217 #endif
13218                      )
13219                /* Big endian correction check.  */
13220                && BYTES_BIG_ENDIAN
13221                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
13222                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
13223                    < UNITS_PER_WORD))
13224         {
13225           int offset = (UNITS_PER_WORD
13226                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
13227
13228           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13229                              plus_constant (XEXP (rtl, 0), offset));
13230         }
13231     }
13232   else if (TREE_CODE (decl) == VAR_DECL
13233            && rtl
13234            && MEM_P (rtl)
13235            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
13236            && BYTES_BIG_ENDIAN)
13237     {
13238       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
13239       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
13240
13241       /* If a variable is declared "register" yet is smaller than
13242          a register, then if we store the variable to memory, it
13243          looks like we're storing a register-sized value, when in
13244          fact we are not.  We need to adjust the offset of the
13245          storage location to reflect the actual value's bytes,
13246          else gdb will not be able to display it.  */
13247       if (rsize > dsize)
13248         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13249                            plus_constant (XEXP (rtl, 0), rsize-dsize));
13250     }
13251
13252   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
13253      and will have been substituted directly into all expressions that use it.
13254      C does not have such a concept, but C++ and other languages do.  */
13255   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
13256     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
13257
13258   if (rtl)
13259     rtl = targetm.delegitimize_address (rtl);
13260
13261   /* If we don't look past the constant pool, we risk emitting a
13262      reference to a constant pool entry that isn't referenced from
13263      code, and thus is not emitted.  */
13264   if (rtl)
13265     rtl = avoid_constant_pool_reference (rtl);
13266
13267   return rtl;
13268 }
13269
13270 /* We need to figure out what section we should use as the base for the
13271    address ranges where a given location is valid.
13272    1. If this particular DECL has a section associated with it, use that.
13273    2. If this function has a section associated with it, use that.
13274    3. Otherwise, use the text section.
13275    XXX: If you split a variable across multiple sections, we won't notice.  */
13276
13277 static const char *
13278 secname_for_decl (const_tree decl)
13279 {
13280   const char *secname;
13281
13282   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13283     {
13284       tree sectree = DECL_SECTION_NAME (decl);
13285       secname = TREE_STRING_POINTER (sectree);
13286     }
13287   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13288     {
13289       tree sectree = DECL_SECTION_NAME (current_function_decl);
13290       secname = TREE_STRING_POINTER (sectree);
13291     }
13292   else if (cfun && in_cold_section_p)
13293     secname = crtl->subsections.cold_section_label;
13294   else
13295     secname = text_section_label;
13296
13297   return secname;
13298 }
13299
13300 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
13301    returned.  If so, the decl for the COMMON block is returned, and the
13302    value is the offset into the common block for the symbol.  */
13303
13304 static tree
13305 fortran_common (tree decl, HOST_WIDE_INT *value)
13306 {
13307   tree val_expr, cvar;
13308   enum machine_mode mode;
13309   HOST_WIDE_INT bitsize, bitpos;
13310   tree offset;
13311   int volatilep = 0, unsignedp = 0;
13312
13313   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
13314      it does not have a value (the offset into the common area), or if it
13315      is thread local (as opposed to global) then it isn't common, and shouldn't
13316      be handled as such.  */
13317   if (TREE_CODE (decl) != VAR_DECL
13318       || !TREE_PUBLIC (decl)
13319       || !TREE_STATIC (decl)
13320       || !DECL_HAS_VALUE_EXPR_P (decl)
13321       || !is_fortran ())
13322     return NULL_TREE;
13323
13324   val_expr = DECL_VALUE_EXPR (decl);
13325   if (TREE_CODE (val_expr) != COMPONENT_REF)
13326     return NULL_TREE;
13327
13328   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
13329                               &mode, &unsignedp, &volatilep, true);
13330
13331   if (cvar == NULL_TREE
13332       || TREE_CODE (cvar) != VAR_DECL
13333       || DECL_ARTIFICIAL (cvar)
13334       || !TREE_PUBLIC (cvar))
13335     return NULL_TREE;
13336
13337   *value = 0;
13338   if (offset != NULL)
13339     {
13340       if (!host_integerp (offset, 0))
13341         return NULL_TREE;
13342       *value = tree_low_cst (offset, 0);
13343     }
13344   if (bitpos != 0)
13345     *value += bitpos / BITS_PER_UNIT;
13346
13347   return cvar;
13348 }
13349
13350 /* Dereference a location expression LOC if DECL is passed by invisible
13351    reference.  */
13352
13353 static dw_loc_descr_ref
13354 loc_by_reference (dw_loc_descr_ref loc, tree decl)
13355 {
13356   HOST_WIDE_INT size;
13357   enum dwarf_location_atom op;
13358
13359   if (loc == NULL)
13360     return NULL;
13361
13362   if ((TREE_CODE (decl) != PARM_DECL
13363        && TREE_CODE (decl) != RESULT_DECL
13364        && TREE_CODE (decl) != VAR_DECL)
13365       || !DECL_BY_REFERENCE (decl))
13366     return loc;
13367
13368   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
13369      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
13370      location expression is considered to be address of a memory location,
13371      rather than the register itself.  */
13372   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
13373        || loc->dw_loc_opc == DW_OP_regx)
13374       && (loc->dw_loc_next == NULL
13375           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
13376               && loc->dw_loc_next->dw_loc_next == NULL)))
13377     {
13378       if (loc->dw_loc_opc == DW_OP_regx)
13379         {
13380           loc->dw_loc_opc = DW_OP_bregx;
13381           loc->dw_loc_oprnd2.v.val_int = 0;
13382         }
13383       else
13384         {
13385           loc->dw_loc_opc
13386             = (enum dwarf_location_atom)
13387               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
13388           loc->dw_loc_oprnd1.v.val_int = 0;
13389         }
13390       return loc;
13391     }
13392
13393   size = int_size_in_bytes (TREE_TYPE (decl));
13394   if (size > DWARF2_ADDR_SIZE || size == -1)
13395     return 0;
13396   else if (size == DWARF2_ADDR_SIZE)
13397     op = DW_OP_deref;
13398   else
13399     op = DW_OP_deref_size;
13400   add_loc_descr (&loc, new_loc_descr (op, size, 0));
13401   return loc;
13402 }
13403
13404 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
13405    data attribute for a variable or a parameter.  We generate the
13406    DW_AT_const_value attribute only in those cases where the given variable
13407    or parameter does not have a true "location" either in memory or in a
13408    register.  This can happen (for example) when a constant is passed as an
13409    actual argument in a call to an inline function.  (It's possible that
13410    these things can crop up in other ways also.)  Note that one type of
13411    constant value which can be passed into an inlined function is a constant
13412    pointer.  This can happen for example if an actual argument in an inlined
13413    function call evaluates to a compile-time constant address.  */
13414
13415 static void
13416 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
13417                                        enum dwarf_attribute attr)
13418 {
13419   rtx rtl;
13420   dw_loc_descr_ref descr;
13421   var_loc_list *loc_list;
13422   struct var_loc_node *node;
13423   if (TREE_CODE (decl) == ERROR_MARK)
13424     return;
13425
13426   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
13427               || TREE_CODE (decl) == RESULT_DECL);
13428
13429   /* See if we possibly have multiple locations for this variable.  */
13430   loc_list = lookup_decl_loc (decl);
13431
13432   /* If it truly has multiple locations, the first and last node will
13433      differ.  */
13434   if (loc_list && loc_list->first != loc_list->last)
13435     {
13436       const char *endname, *secname;
13437       dw_loc_list_ref list;
13438       rtx varloc;
13439       enum var_init_status initialized;
13440
13441       /* Now that we know what section we are using for a base,
13442          actually construct the list of locations.
13443          The first location information is what is passed to the
13444          function that creates the location list, and the remaining
13445          locations just get added on to that list.
13446          Note that we only know the start address for a location
13447          (IE location changes), so to build the range, we use
13448          the range [current location start, next location start].
13449          This means we have to special case the last node, and generate
13450          a range of [last location start, end of function label].  */
13451
13452       node = loc_list->first;
13453       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13454       secname = secname_for_decl (decl);
13455
13456       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
13457         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13458       else
13459         initialized = VAR_INIT_STATUS_INITIALIZED;
13460
13461       descr = loc_by_reference (loc_descriptor (varloc, DECL_MODE (decl),
13462                                                 initialized), decl);
13463       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
13464       node = node->next;
13465
13466       for (; node->next; node = node->next)
13467         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13468           {
13469             /* The variable has a location between NODE->LABEL and
13470                NODE->NEXT->LABEL.  */
13471             enum var_init_status initialized =
13472               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13473             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13474             descr = loc_by_reference (loc_descriptor (varloc, DECL_MODE (decl),
13475                                       initialized), decl);
13476             add_loc_descr_to_loc_list (&list, descr,
13477                                        node->label, node->next->label, secname);
13478           }
13479
13480       /* If the variable has a location at the last label
13481          it keeps its location until the end of function.  */
13482       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13483         {
13484           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13485           enum var_init_status initialized =
13486             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13487
13488           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13489           if (!current_function_decl)
13490             endname = text_end_label;
13491           else
13492             {
13493               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13494                                            current_function_funcdef_no);
13495               endname = ggc_strdup (label_id);
13496             }
13497           descr = loc_by_reference (loc_descriptor (varloc,
13498                                                     DECL_MODE (decl),
13499                                                     initialized),
13500                                     decl);
13501           add_loc_descr_to_loc_list (&list, descr,
13502                                      node->label, endname, secname);
13503         }
13504
13505       /* Finally, add the location list to the DIE, and we are done.  */
13506       add_AT_loc_list (die, attr, list);
13507       return;
13508     }
13509
13510   /* Try to get some constant RTL for this decl, and use that as the value of
13511      the location.  */
13512
13513   rtl = rtl_for_decl_location (decl);
13514   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
13515     {
13516       add_const_value_attribute (die, rtl);
13517       return;
13518     }
13519
13520   /* If we have tried to generate the location otherwise, and it
13521      didn't work out (we wouldn't be here if we did), and we have a one entry
13522      location list, try generating a location from that.  */
13523   if (loc_list && loc_list->first)
13524     {
13525       enum var_init_status status;
13526       node = loc_list->first;
13527       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13528       rtl = NOTE_VAR_LOCATION (node->var_loc_note);
13529       if (GET_CODE (rtl) == VAR_LOCATION
13530           && GET_CODE (XEXP (rtl, 1)) != PARALLEL)
13531         rtl = XEXP (XEXP (rtl, 1), 0);
13532       if (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
13533         {
13534           add_const_value_attribute (die, rtl);
13535           return;
13536         }
13537       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note),
13538                               DECL_MODE (decl), status);
13539       if (descr)
13540         {
13541           descr = loc_by_reference (descr, decl);
13542           add_AT_location_description (die, attr, descr);
13543           return;
13544         }
13545     }
13546
13547   /* We couldn't get any rtl, so try directly generating the location
13548      description from the tree.  */
13549   descr = loc_descriptor_from_tree (decl);
13550   if (descr)
13551     {
13552       descr = loc_by_reference (descr, decl);
13553       add_AT_location_description (die, attr, descr);
13554       return;
13555     }
13556   /* None of that worked, so it must not really have a location;
13557      try adding a constant value attribute from the DECL_INITIAL.  */
13558   tree_add_const_value_attribute_for_decl (die, decl);
13559 }
13560
13561 /* Add VARIABLE and DIE into deferred locations list.  */
13562
13563 static void
13564 defer_location (tree variable, dw_die_ref die)
13565 {
13566   deferred_locations entry;
13567   entry.variable = variable;
13568   entry.die = die;
13569   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
13570 }
13571
13572 /* Helper function for tree_add_const_value_attribute.  Natively encode
13573    initializer INIT into an array.  Return true if successful.  */
13574
13575 static bool
13576 native_encode_initializer (tree init, unsigned char *array, int size)
13577 {
13578   tree type;
13579
13580   if (init == NULL_TREE)
13581     return false;
13582
13583   STRIP_NOPS (init);
13584   switch (TREE_CODE (init))
13585     {
13586     case STRING_CST:
13587       type = TREE_TYPE (init);
13588       if (TREE_CODE (type) == ARRAY_TYPE)
13589         {
13590           tree enttype = TREE_TYPE (type);
13591           enum machine_mode mode = TYPE_MODE (enttype);
13592
13593           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
13594             return false;
13595           if (int_size_in_bytes (type) != size)
13596             return false;
13597           if (size > TREE_STRING_LENGTH (init))
13598             {
13599               memcpy (array, TREE_STRING_POINTER (init),
13600                       TREE_STRING_LENGTH (init));
13601               memset (array + TREE_STRING_LENGTH (init),
13602                       '\0', size - TREE_STRING_LENGTH (init));
13603             }
13604           else
13605             memcpy (array, TREE_STRING_POINTER (init), size);
13606           return true;
13607         }
13608       return false;
13609     case CONSTRUCTOR:
13610       type = TREE_TYPE (init);
13611       if (int_size_in_bytes (type) != size)
13612         return false;
13613       if (TREE_CODE (type) == ARRAY_TYPE)
13614         {
13615           HOST_WIDE_INT min_index;
13616           unsigned HOST_WIDE_INT cnt;
13617           int curpos = 0, fieldsize;
13618           constructor_elt *ce;
13619
13620           if (TYPE_DOMAIN (type) == NULL_TREE
13621               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
13622             return false;
13623
13624           fieldsize = int_size_in_bytes (TREE_TYPE (type));
13625           if (fieldsize <= 0)
13626             return false;
13627
13628           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
13629           memset (array, '\0', size);
13630           for (cnt = 0;
13631                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
13632                cnt++)
13633             {
13634               tree val = ce->value;
13635               tree index = ce->index;
13636               int pos = curpos;
13637               if (index && TREE_CODE (index) == RANGE_EXPR)
13638                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
13639                       * fieldsize;
13640               else if (index)
13641                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
13642
13643               if (val)
13644                 {
13645                   STRIP_NOPS (val);
13646                   if (!native_encode_initializer (val, array + pos, fieldsize))
13647                     return false;
13648                 }
13649               curpos = pos + fieldsize;
13650               if (index && TREE_CODE (index) == RANGE_EXPR)
13651                 {
13652                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
13653                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
13654                   while (count > 0)
13655                     {
13656                       if (val)
13657                         memcpy (array + curpos, array + pos, fieldsize);
13658                       curpos += fieldsize;
13659                     }
13660                 }
13661               gcc_assert (curpos <= size);
13662             }
13663           return true;
13664         }
13665       else if (TREE_CODE (type) == RECORD_TYPE
13666                || TREE_CODE (type) == UNION_TYPE)
13667         {
13668           tree field = NULL_TREE;
13669           unsigned HOST_WIDE_INT cnt;
13670           constructor_elt *ce;
13671
13672           if (int_size_in_bytes (type) != size)
13673             return false;
13674
13675           if (TREE_CODE (type) == RECORD_TYPE)
13676             field = TYPE_FIELDS (type);
13677
13678           for (cnt = 0;
13679                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
13680                cnt++, field = field ? TREE_CHAIN (field) : 0)
13681             {
13682               tree val = ce->value;
13683               int pos, fieldsize;
13684
13685               if (ce->index != 0)
13686                 field = ce->index;
13687
13688               if (val)
13689                 STRIP_NOPS (val);
13690
13691               if (field == NULL_TREE || DECL_BIT_FIELD (field))
13692                 return false;
13693
13694               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
13695                   && TYPE_DOMAIN (TREE_TYPE (field))
13696                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
13697                 return false;
13698               else if (DECL_SIZE_UNIT (field) == NULL_TREE
13699                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
13700                 return false;
13701               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
13702               pos = int_byte_position (field);
13703               gcc_assert (pos + fieldsize <= size);
13704               if (val
13705                   && !native_encode_initializer (val, array + pos, fieldsize))
13706                 return false;
13707             }
13708           return true;
13709         }
13710       return false;
13711     case VIEW_CONVERT_EXPR:
13712     case NON_LVALUE_EXPR:
13713       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
13714     default:
13715       return native_encode_expr (init, array, size) == size;
13716     }
13717 }
13718
13719 /* Attach a DW_AT_const_value attribute to DIE. The value of the
13720    attribute is the const value T.  */
13721
13722 static void
13723 tree_add_const_value_attribute (dw_die_ref die, tree t)
13724 {
13725   tree init;
13726   tree type = TREE_TYPE (t);
13727   rtx rtl;
13728
13729   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
13730     return;
13731
13732   init = t;
13733   gcc_assert (!DECL_P (init));
13734
13735   rtl = rtl_for_decl_init (init, type);
13736   if (rtl)
13737     add_const_value_attribute (die, rtl);
13738   /* If the host and target are sane, try harder.  */
13739   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
13740            && initializer_constant_valid_p (init, type))
13741     {
13742       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
13743       if (size > 0 && (int) size == size)
13744         {
13745           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
13746
13747           if (native_encode_initializer (init, array, size))
13748             add_AT_vec (die, DW_AT_const_value, size, 1, array);
13749         }
13750     }
13751 }
13752
13753 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
13754    attribute is the const value of T, where T is an integral constant
13755    variable with static storage duration
13756    (so it can't be a PARM_DECL or a RESULT_DECL).  */
13757
13758 static void
13759 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
13760 {
13761
13762   if (!decl
13763       || (TREE_CODE (decl) != VAR_DECL
13764           && TREE_CODE (decl) != CONST_DECL))
13765     return;
13766
13767     if (TREE_READONLY (decl)
13768         && ! TREE_THIS_VOLATILE (decl)
13769         && DECL_INITIAL (decl))
13770       /* OK */;
13771     else
13772       return;
13773
13774     tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
13775 }
13776
13777 /* Convert the CFI instructions for the current function into a
13778    location list.  This is used for DW_AT_frame_base when we targeting
13779    a dwarf2 consumer that does not support the dwarf3
13780    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
13781    expressions.  */
13782
13783 static dw_loc_list_ref
13784 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
13785 {
13786   dw_fde_ref fde;
13787   dw_loc_list_ref list, *list_tail;
13788   dw_cfi_ref cfi;
13789   dw_cfa_location last_cfa, next_cfa;
13790   const char *start_label, *last_label, *section;
13791   dw_cfa_location remember;
13792
13793   fde = current_fde ();
13794   gcc_assert (fde != NULL);
13795
13796   section = secname_for_decl (current_function_decl);
13797   list_tail = &list;
13798   list = NULL;
13799
13800   memset (&next_cfa, 0, sizeof (next_cfa));
13801   next_cfa.reg = INVALID_REGNUM;
13802   remember = next_cfa;
13803
13804   start_label = fde->dw_fde_begin;
13805
13806   /* ??? Bald assumption that the CIE opcode list does not contain
13807      advance opcodes.  */
13808   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
13809     lookup_cfa_1 (cfi, &next_cfa, &remember);
13810
13811   last_cfa = next_cfa;
13812   last_label = start_label;
13813
13814   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
13815     switch (cfi->dw_cfi_opc)
13816       {
13817       case DW_CFA_set_loc:
13818       case DW_CFA_advance_loc1:
13819       case DW_CFA_advance_loc2:
13820       case DW_CFA_advance_loc4:
13821         if (!cfa_equal_p (&last_cfa, &next_cfa))
13822           {
13823             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
13824                                        start_label, last_label, section,
13825                                        list == NULL);
13826
13827             list_tail = &(*list_tail)->dw_loc_next;
13828             last_cfa = next_cfa;
13829             start_label = last_label;
13830           }
13831         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
13832         break;
13833
13834       case DW_CFA_advance_loc:
13835         /* The encoding is complex enough that we should never emit this.  */
13836         gcc_unreachable ();
13837
13838       default:
13839         lookup_cfa_1 (cfi, &next_cfa, &remember);
13840         break;
13841       }
13842
13843   if (!cfa_equal_p (&last_cfa, &next_cfa))
13844     {
13845       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
13846                                  start_label, last_label, section,
13847                                  list == NULL);
13848       list_tail = &(*list_tail)->dw_loc_next;
13849       start_label = last_label;
13850     }
13851   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
13852                              start_label, fde->dw_fde_end, section,
13853                              list == NULL);
13854
13855   return list;
13856 }
13857
13858 /* Compute a displacement from the "steady-state frame pointer" to the
13859    frame base (often the same as the CFA), and store it in
13860    frame_pointer_fb_offset.  OFFSET is added to the displacement
13861    before the latter is negated.  */
13862
13863 static void
13864 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
13865 {
13866   rtx reg, elim;
13867
13868 #ifdef FRAME_POINTER_CFA_OFFSET
13869   reg = frame_pointer_rtx;
13870   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
13871 #else
13872   reg = arg_pointer_rtx;
13873   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
13874 #endif
13875
13876   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13877   if (GET_CODE (elim) == PLUS)
13878     {
13879       offset += INTVAL (XEXP (elim, 1));
13880       elim = XEXP (elim, 0);
13881     }
13882
13883   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13884                && (elim == hard_frame_pointer_rtx
13885                    || elim == stack_pointer_rtx))
13886               || elim == (frame_pointer_needed
13887                           ? hard_frame_pointer_rtx
13888                           : stack_pointer_rtx));
13889
13890   frame_pointer_fb_offset = -offset;
13891 }
13892
13893 /* Generate a DW_AT_name attribute given some string value to be included as
13894    the value of the attribute.  */
13895
13896 static void
13897 add_name_attribute (dw_die_ref die, const char *name_string)
13898 {
13899   if (name_string != NULL && *name_string != 0)
13900     {
13901       if (demangle_name_func)
13902         name_string = (*demangle_name_func) (name_string);
13903
13904       add_AT_string (die, DW_AT_name, name_string);
13905     }
13906 }
13907
13908 /* Generate a DW_AT_comp_dir attribute for DIE.  */
13909
13910 static void
13911 add_comp_dir_attribute (dw_die_ref die)
13912 {
13913   const char *wd = get_src_pwd ();
13914   char *wd1;
13915
13916   if (wd == NULL)
13917     return;
13918
13919   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
13920     {
13921       int wdlen;
13922
13923       wdlen = strlen (wd);
13924       wd1 = GGC_NEWVEC (char, wdlen + 2);
13925       strcpy (wd1, wd);
13926       wd1 [wdlen] = DIR_SEPARATOR;
13927       wd1 [wdlen + 1] = 0;
13928       wd = wd1;
13929     }
13930
13931     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
13932 }
13933
13934 /* Given a tree node describing an array bound (either lower or upper) output
13935    a representation for that bound.  */
13936
13937 static void
13938 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
13939 {
13940   switch (TREE_CODE (bound))
13941     {
13942     case ERROR_MARK:
13943       return;
13944
13945     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
13946     case INTEGER_CST:
13947       if (! host_integerp (bound, 0)
13948           || (bound_attr == DW_AT_lower_bound
13949               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
13950                   || (is_fortran () && integer_onep (bound)))))
13951         /* Use the default.  */
13952         ;
13953       else
13954         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
13955       break;
13956
13957     CASE_CONVERT:
13958     case VIEW_CONVERT_EXPR:
13959       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
13960       break;
13961
13962     case SAVE_EXPR:
13963       break;
13964
13965     case VAR_DECL:
13966     case PARM_DECL:
13967     case RESULT_DECL:
13968       {
13969         dw_die_ref decl_die = lookup_decl_die (bound);
13970         dw_loc_descr_ref loc;
13971
13972         /* ??? Can this happen, or should the variable have been bound
13973            first?  Probably it can, since I imagine that we try to create
13974            the types of parameters in the order in which they exist in
13975            the list, and won't have created a forward reference to a
13976            later parameter.  */
13977         if (decl_die != NULL)
13978           add_AT_die_ref (subrange_die, bound_attr, decl_die);
13979         else
13980           {
13981             loc = loc_descriptor_from_tree_1 (bound, 0);
13982             add_AT_location_description (subrange_die, bound_attr, loc);
13983           }
13984         break;
13985       }
13986
13987     default:
13988       {
13989         /* Otherwise try to create a stack operation procedure to
13990            evaluate the value of the array bound.  */
13991
13992         dw_die_ref ctx, decl_die;
13993         dw_loc_descr_ref loc;
13994
13995         loc = loc_descriptor_from_tree (bound);
13996         if (loc == NULL)
13997           break;
13998
13999         if (current_function_decl == 0)
14000           ctx = comp_unit_die;
14001         else
14002           ctx = lookup_decl_die (current_function_decl);
14003
14004         decl_die = new_die (DW_TAG_variable, ctx, bound);
14005         add_AT_flag (decl_die, DW_AT_artificial, 1);
14006         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
14007         add_AT_loc (decl_die, DW_AT_location, loc);
14008
14009         add_AT_die_ref (subrange_die, bound_attr, decl_die);
14010         break;
14011       }
14012     }
14013 }
14014
14015 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
14016    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
14017    Note that the block of subscript information for an array type also
14018    includes information about the element type of the given array type.  */
14019
14020 static void
14021 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
14022 {
14023   unsigned dimension_number;
14024   tree lower, upper;
14025   dw_die_ref subrange_die;
14026
14027   for (dimension_number = 0;
14028        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
14029        type = TREE_TYPE (type), dimension_number++)
14030     {
14031       tree domain = TYPE_DOMAIN (type);
14032
14033       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
14034         break;
14035
14036       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
14037          and (in GNU C only) variable bounds.  Handle all three forms
14038          here.  */
14039       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
14040       if (domain)
14041         {
14042           /* We have an array type with specified bounds.  */
14043           lower = TYPE_MIN_VALUE (domain);
14044           upper = TYPE_MAX_VALUE (domain);
14045
14046           /* Define the index type.  */
14047           if (TREE_TYPE (domain))
14048             {
14049               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
14050                  TREE_TYPE field.  We can't emit debug info for this
14051                  because it is an unnamed integral type.  */
14052               if (TREE_CODE (domain) == INTEGER_TYPE
14053                   && TYPE_NAME (domain) == NULL_TREE
14054                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
14055                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
14056                 ;
14057               else
14058                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
14059                                     type_die);
14060             }
14061
14062           /* ??? If upper is NULL, the array has unspecified length,
14063              but it does have a lower bound.  This happens with Fortran
14064                dimension arr(N:*)
14065              Since the debugger is definitely going to need to know N
14066              to produce useful results, go ahead and output the lower
14067              bound solo, and hope the debugger can cope.  */
14068
14069           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
14070           if (upper)
14071             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
14072         }
14073
14074       /* Otherwise we have an array type with an unspecified length.  The
14075          DWARF-2 spec does not say how to handle this; let's just leave out the
14076          bounds.  */
14077     }
14078 }
14079
14080 static void
14081 add_byte_size_attribute (dw_die_ref die, tree tree_node)
14082 {
14083   unsigned size;
14084
14085   switch (TREE_CODE (tree_node))
14086     {
14087     case ERROR_MARK:
14088       size = 0;
14089       break;
14090     case ENUMERAL_TYPE:
14091     case RECORD_TYPE:
14092     case UNION_TYPE:
14093     case QUAL_UNION_TYPE:
14094       size = int_size_in_bytes (tree_node);
14095       break;
14096     case FIELD_DECL:
14097       /* For a data member of a struct or union, the DW_AT_byte_size is
14098          generally given as the number of bytes normally allocated for an
14099          object of the *declared* type of the member itself.  This is true
14100          even for bit-fields.  */
14101       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
14102       break;
14103     default:
14104       gcc_unreachable ();
14105     }
14106
14107   /* Note that `size' might be -1 when we get to this point.  If it is, that
14108      indicates that the byte size of the entity in question is variable.  We
14109      have no good way of expressing this fact in Dwarf at the present time,
14110      so just let the -1 pass on through.  */
14111   add_AT_unsigned (die, DW_AT_byte_size, size);
14112 }
14113
14114 /* For a FIELD_DECL node which represents a bit-field, output an attribute
14115    which specifies the distance in bits from the highest order bit of the
14116    "containing object" for the bit-field to the highest order bit of the
14117    bit-field itself.
14118
14119    For any given bit-field, the "containing object" is a hypothetical object
14120    (of some integral or enum type) within which the given bit-field lives.  The
14121    type of this hypothetical "containing object" is always the same as the
14122    declared type of the individual bit-field itself.  The determination of the
14123    exact location of the "containing object" for a bit-field is rather
14124    complicated.  It's handled by the `field_byte_offset' function (above).
14125
14126    Note that it is the size (in bytes) of the hypothetical "containing object"
14127    which will be given in the DW_AT_byte_size attribute for this bit-field.
14128    (See `byte_size_attribute' above).  */
14129
14130 static inline void
14131 add_bit_offset_attribute (dw_die_ref die, tree decl)
14132 {
14133   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
14134   tree type = DECL_BIT_FIELD_TYPE (decl);
14135   HOST_WIDE_INT bitpos_int;
14136   HOST_WIDE_INT highest_order_object_bit_offset;
14137   HOST_WIDE_INT highest_order_field_bit_offset;
14138   HOST_WIDE_INT unsigned bit_offset;
14139
14140   /* Must be a field and a bit field.  */
14141   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
14142
14143   /* We can't yet handle bit-fields whose offsets are variable, so if we
14144      encounter such things, just return without generating any attribute
14145      whatsoever.  Likewise for variable or too large size.  */
14146   if (! host_integerp (bit_position (decl), 0)
14147       || ! host_integerp (DECL_SIZE (decl), 1))
14148     return;
14149
14150   bitpos_int = int_bit_position (decl);
14151
14152   /* Note that the bit offset is always the distance (in bits) from the
14153      highest-order bit of the "containing object" to the highest-order bit of
14154      the bit-field itself.  Since the "high-order end" of any object or field
14155      is different on big-endian and little-endian machines, the computation
14156      below must take account of these differences.  */
14157   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
14158   highest_order_field_bit_offset = bitpos_int;
14159
14160   if (! BYTES_BIG_ENDIAN)
14161     {
14162       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
14163       highest_order_object_bit_offset += simple_type_size_in_bits (type);
14164     }
14165
14166   bit_offset
14167     = (! BYTES_BIG_ENDIAN
14168        ? highest_order_object_bit_offset - highest_order_field_bit_offset
14169        : highest_order_field_bit_offset - highest_order_object_bit_offset);
14170
14171   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
14172 }
14173
14174 /* For a FIELD_DECL node which represents a bit field, output an attribute
14175    which specifies the length in bits of the given field.  */
14176
14177 static inline void
14178 add_bit_size_attribute (dw_die_ref die, tree decl)
14179 {
14180   /* Must be a field and a bit field.  */
14181   gcc_assert (TREE_CODE (decl) == FIELD_DECL
14182               && DECL_BIT_FIELD_TYPE (decl));
14183
14184   if (host_integerp (DECL_SIZE (decl), 1))
14185     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
14186 }
14187
14188 /* If the compiled language is ANSI C, then add a 'prototyped'
14189    attribute, if arg types are given for the parameters of a function.  */
14190
14191 static inline void
14192 add_prototyped_attribute (dw_die_ref die, tree func_type)
14193 {
14194   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
14195       && TYPE_ARG_TYPES (func_type) != NULL)
14196     add_AT_flag (die, DW_AT_prototyped, 1);
14197 }
14198
14199 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
14200    by looking in either the type declaration or object declaration
14201    equate table.  */
14202
14203 static inline dw_die_ref
14204 add_abstract_origin_attribute (dw_die_ref die, tree origin)
14205 {
14206   dw_die_ref origin_die = NULL;
14207
14208   if (TREE_CODE (origin) != FUNCTION_DECL)
14209     {
14210       /* We may have gotten separated from the block for the inlined
14211          function, if we're in an exception handler or some such; make
14212          sure that the abstract function has been written out.
14213
14214          Doing this for nested functions is wrong, however; functions are
14215          distinct units, and our context might not even be inline.  */
14216       tree fn = origin;
14217
14218       if (TYPE_P (fn))
14219         fn = TYPE_STUB_DECL (fn);
14220
14221       fn = decl_function_context (fn);
14222       if (fn)
14223         dwarf2out_abstract_function (fn);
14224     }
14225
14226   if (DECL_P (origin))
14227     origin_die = lookup_decl_die (origin);
14228   else if (TYPE_P (origin))
14229     origin_die = lookup_type_die (origin);
14230
14231   /* XXX: Functions that are never lowered don't always have correct block
14232      trees (in the case of java, they simply have no block tree, in some other
14233      languages).  For these functions, there is nothing we can really do to
14234      output correct debug info for inlined functions in all cases.  Rather
14235      than die, we'll just produce deficient debug info now, in that we will
14236      have variables without a proper abstract origin.  In the future, when all
14237      functions are lowered, we should re-add a gcc_assert (origin_die)
14238      here.  */
14239
14240   if (origin_die)
14241     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
14242   return origin_die;
14243 }
14244
14245 /* We do not currently support the pure_virtual attribute.  */
14246
14247 static inline void
14248 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
14249 {
14250   if (DECL_VINDEX (func_decl))
14251     {
14252       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14253
14254       if (host_integerp (DECL_VINDEX (func_decl), 0))
14255         add_AT_loc (die, DW_AT_vtable_elem_location,
14256                     new_loc_descr (DW_OP_constu,
14257                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
14258                                    0));
14259
14260       /* GNU extension: Record what type this method came from originally.  */
14261       if (debug_info_level > DINFO_LEVEL_TERSE)
14262         add_AT_die_ref (die, DW_AT_containing_type,
14263                         lookup_type_die (DECL_CONTEXT (func_decl)));
14264     }
14265 }
14266 \f
14267 /* Add source coordinate attributes for the given decl.  */
14268
14269 static void
14270 add_src_coords_attributes (dw_die_ref die, tree decl)
14271 {
14272   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14273
14274   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
14275   add_AT_unsigned (die, DW_AT_decl_line, s.line);
14276 }
14277
14278 /* Add a DW_AT_name attribute and source coordinate attribute for the
14279    given decl, but only if it actually has a name.  */
14280
14281 static void
14282 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
14283 {
14284   tree decl_name;
14285
14286   decl_name = DECL_NAME (decl);
14287   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
14288     {
14289       add_name_attribute (die, dwarf2_name (decl, 0));
14290       if (! DECL_ARTIFICIAL (decl))
14291         add_src_coords_attributes (die, decl);
14292
14293       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
14294           && TREE_PUBLIC (decl)
14295           && !DECL_ABSTRACT (decl)
14296           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
14297           && !is_fortran ())
14298         {
14299           /* Defer until we have an assembler name set.  */
14300           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
14301             {
14302               limbo_die_node *asm_name;
14303
14304               asm_name = GGC_CNEW (limbo_die_node);
14305               asm_name->die = die;
14306               asm_name->created_for = decl;
14307               asm_name->next = deferred_asm_name;
14308               deferred_asm_name = asm_name;
14309             }
14310           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
14311             add_AT_string (die, DW_AT_MIPS_linkage_name,
14312                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
14313         }
14314     }
14315
14316 #ifdef VMS_DEBUGGING_INFO
14317   /* Get the function's name, as described by its RTL.  This may be different
14318      from the DECL_NAME name used in the source file.  */
14319   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
14320     {
14321       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
14322                    XEXP (DECL_RTL (decl), 0));
14323       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
14324     }
14325 #endif
14326 }
14327
14328 /* Push a new declaration scope.  */
14329
14330 static void
14331 push_decl_scope (tree scope)
14332 {
14333   VEC_safe_push (tree, gc, decl_scope_table, scope);
14334 }
14335
14336 /* Pop a declaration scope.  */
14337
14338 static inline void
14339 pop_decl_scope (void)
14340 {
14341   VEC_pop (tree, decl_scope_table);
14342 }
14343
14344 /* Return the DIE for the scope that immediately contains this type.
14345    Non-named types get global scope.  Named types nested in other
14346    types get their containing scope if it's open, or global scope
14347    otherwise.  All other types (i.e. function-local named types) get
14348    the current active scope.  */
14349
14350 static dw_die_ref
14351 scope_die_for (tree t, dw_die_ref context_die)
14352 {
14353   dw_die_ref scope_die = NULL;
14354   tree containing_scope;
14355   int i;
14356
14357   /* Non-types always go in the current scope.  */
14358   gcc_assert (TYPE_P (t));
14359
14360   containing_scope = TYPE_CONTEXT (t);
14361
14362   /* Use the containing namespace if it was passed in (for a declaration).  */
14363   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
14364     {
14365       if (context_die == lookup_decl_die (containing_scope))
14366         /* OK */;
14367       else
14368         containing_scope = NULL_TREE;
14369     }
14370
14371   /* Ignore function type "scopes" from the C frontend.  They mean that
14372      a tagged type is local to a parmlist of a function declarator, but
14373      that isn't useful to DWARF.  */
14374   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
14375     containing_scope = NULL_TREE;
14376
14377   if (containing_scope == NULL_TREE)
14378     scope_die = comp_unit_die;
14379   else if (TYPE_P (containing_scope))
14380     {
14381       /* For types, we can just look up the appropriate DIE.  But
14382          first we check to see if we're in the middle of emitting it
14383          so we know where the new DIE should go.  */
14384       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
14385         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
14386           break;
14387
14388       if (i < 0)
14389         {
14390           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
14391                       || TREE_ASM_WRITTEN (containing_scope));
14392
14393           /* If none of the current dies are suitable, we get file scope.  */
14394           scope_die = comp_unit_die;
14395         }
14396       else
14397         scope_die = lookup_type_die (containing_scope);
14398     }
14399   else
14400     scope_die = context_die;
14401
14402   return scope_die;
14403 }
14404
14405 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
14406
14407 static inline int
14408 local_scope_p (dw_die_ref context_die)
14409 {
14410   for (; context_die; context_die = context_die->die_parent)
14411     if (context_die->die_tag == DW_TAG_inlined_subroutine
14412         || context_die->die_tag == DW_TAG_subprogram)
14413       return 1;
14414
14415   return 0;
14416 }
14417
14418 /* Returns nonzero if CONTEXT_DIE is a class.  */
14419
14420 static inline int
14421 class_scope_p (dw_die_ref context_die)
14422 {
14423   return (context_die
14424           && (context_die->die_tag == DW_TAG_structure_type
14425               || context_die->die_tag == DW_TAG_class_type
14426               || context_die->die_tag == DW_TAG_interface_type
14427               || context_die->die_tag == DW_TAG_union_type));
14428 }
14429
14430 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
14431    whether or not to treat a DIE in this context as a declaration.  */
14432
14433 static inline int
14434 class_or_namespace_scope_p (dw_die_ref context_die)
14435 {
14436   return (class_scope_p (context_die)
14437           || (context_die && context_die->die_tag == DW_TAG_namespace));
14438 }
14439
14440 /* Many forms of DIEs require a "type description" attribute.  This
14441    routine locates the proper "type descriptor" die for the type given
14442    by 'type', and adds a DW_AT_type attribute below the given die.  */
14443
14444 static void
14445 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
14446                     int decl_volatile, dw_die_ref context_die)
14447 {
14448   enum tree_code code  = TREE_CODE (type);
14449   dw_die_ref type_die  = NULL;
14450
14451   /* ??? If this type is an unnamed subrange type of an integral, floating-point
14452      or fixed-point type, use the inner type.  This is because we have no
14453      support for unnamed types in base_type_die.  This can happen if this is
14454      an Ada subrange type.  Correct solution is emit a subrange type die.  */
14455   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
14456       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
14457     type = TREE_TYPE (type), code = TREE_CODE (type);
14458
14459   if (code == ERROR_MARK
14460       /* Handle a special case.  For functions whose return type is void, we
14461          generate *no* type attribute.  (Note that no object may have type
14462          `void', so this only applies to function return types).  */
14463       || code == VOID_TYPE)
14464     return;
14465
14466   type_die = modified_type_die (type,
14467                                 decl_const || TYPE_READONLY (type),
14468                                 decl_volatile || TYPE_VOLATILE (type),
14469                                 context_die);
14470
14471   if (type_die != NULL)
14472     add_AT_die_ref (object_die, DW_AT_type, type_die);
14473 }
14474
14475 /* Given an object die, add the calling convention attribute for the
14476    function call type.  */
14477 static void
14478 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
14479 {
14480   enum dwarf_calling_convention value = DW_CC_normal;
14481
14482   value = ((enum dwarf_calling_convention)
14483            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
14484
14485   /* DWARF doesn't provide a way to identify a program's source-level
14486      entry point.  DW_AT_calling_convention attributes are only meant
14487      to describe functions' calling conventions.  However, lacking a
14488      better way to signal the Fortran main program, we use this for the
14489      time being, following existing custom.  */
14490   if (is_fortran ()
14491       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
14492     value = DW_CC_program;
14493
14494   /* Only add the attribute if the backend requests it, and
14495      is not DW_CC_normal.  */
14496   if (value && (value != DW_CC_normal))
14497     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
14498 }
14499
14500 /* Given a tree pointer to a struct, class, union, or enum type node, return
14501    a pointer to the (string) tag name for the given type, or zero if the type
14502    was declared without a tag.  */
14503
14504 static const char *
14505 type_tag (const_tree type)
14506 {
14507   const char *name = 0;
14508
14509   if (TYPE_NAME (type) != 0)
14510     {
14511       tree t = 0;
14512
14513       /* Find the IDENTIFIER_NODE for the type name.  */
14514       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
14515         t = TYPE_NAME (type);
14516
14517       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
14518          a TYPE_DECL node, regardless of whether or not a `typedef' was
14519          involved.  */
14520       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14521                && ! DECL_IGNORED_P (TYPE_NAME (type)))
14522         {
14523           /* We want to be extra verbose.  Don't call dwarf_name if
14524              DECL_NAME isn't set.  The default hook for decl_printable_name
14525              doesn't like that, and in this context it's correct to return
14526              0, instead of "<anonymous>" or the like.  */
14527           if (DECL_NAME (TYPE_NAME (type)))
14528             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
14529         }
14530
14531       /* Now get the name as a string, or invent one.  */
14532       if (!name && t != 0)
14533         name = IDENTIFIER_POINTER (t);
14534     }
14535
14536   return (name == 0 || *name == '\0') ? 0 : name;
14537 }
14538
14539 /* Return the type associated with a data member, make a special check
14540    for bit field types.  */
14541
14542 static inline tree
14543 member_declared_type (const_tree member)
14544 {
14545   return (DECL_BIT_FIELD_TYPE (member)
14546           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
14547 }
14548
14549 /* Get the decl's label, as described by its RTL. This may be different
14550    from the DECL_NAME name used in the source file.  */
14551
14552 #if 0
14553 static const char *
14554 decl_start_label (tree decl)
14555 {
14556   rtx x;
14557   const char *fnname;
14558
14559   x = DECL_RTL (decl);
14560   gcc_assert (MEM_P (x));
14561
14562   x = XEXP (x, 0);
14563   gcc_assert (GET_CODE (x) == SYMBOL_REF);
14564
14565   fnname = XSTR (x, 0);
14566   return fnname;
14567 }
14568 #endif
14569 \f
14570 /* These routines generate the internal representation of the DIE's for
14571    the compilation unit.  Debugging information is collected by walking
14572    the declaration trees passed in from dwarf2out_decl().  */
14573
14574 static void
14575 gen_array_type_die (tree type, dw_die_ref context_die)
14576 {
14577   dw_die_ref scope_die = scope_die_for (type, context_die);
14578   dw_die_ref array_die;
14579
14580   /* GNU compilers represent multidimensional array types as sequences of one
14581      dimensional array types whose element types are themselves array types.
14582      We sometimes squish that down to a single array_type DIE with multiple
14583      subscripts in the Dwarf debugging info.  The draft Dwarf specification
14584      say that we are allowed to do this kind of compression in C, because
14585      there is no difference between an array of arrays and a multidimensional
14586      array.  We don't do this for Ada to remain as close as possible to the
14587      actual representation, which is especially important against the language
14588      flexibilty wrt arrays of variable size.  */
14589
14590   bool collapse_nested_arrays = !is_ada ();
14591   tree element_type;
14592
14593   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
14594      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
14595   if (TYPE_STRING_FLAG (type)
14596       && TREE_CODE (type) == ARRAY_TYPE
14597       && is_fortran ()
14598       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
14599     {
14600       HOST_WIDE_INT size;
14601
14602       array_die = new_die (DW_TAG_string_type, scope_die, type);
14603       add_name_attribute (array_die, type_tag (type));
14604       equate_type_number_to_die (type, array_die);
14605       size = int_size_in_bytes (type);
14606       if (size >= 0)
14607         add_AT_unsigned (array_die, DW_AT_byte_size, size);
14608       else if (TYPE_DOMAIN (type) != NULL_TREE
14609                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
14610                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
14611         {
14612           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
14613           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
14614
14615           size = int_size_in_bytes (TREE_TYPE (szdecl));
14616           if (loc && size > 0)
14617             {
14618               add_AT_loc (array_die, DW_AT_string_length, loc);
14619               if (size != DWARF2_ADDR_SIZE)
14620                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
14621             }
14622         }
14623       return;
14624     }
14625
14626   /* ??? The SGI dwarf reader fails for array of array of enum types
14627      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
14628      array type comes before the outer array type.  We thus call gen_type_die
14629      before we new_die and must prevent nested array types collapsing for this
14630      target.  */
14631
14632 #ifdef MIPS_DEBUGGING_INFO
14633   gen_type_die (TREE_TYPE (type), context_die);
14634   collapse_nested_arrays = false;
14635 #endif
14636
14637   array_die = new_die (DW_TAG_array_type, scope_die, type);
14638   add_name_attribute (array_die, type_tag (type));
14639   equate_type_number_to_die (type, array_die);
14640
14641   if (TREE_CODE (type) == VECTOR_TYPE)
14642     {
14643       /* The frontend feeds us a representation for the vector as a struct
14644          containing an array.  Pull out the array type.  */
14645       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
14646       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
14647     }
14648
14649   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
14650   if (is_fortran ()
14651       && TREE_CODE (type) == ARRAY_TYPE
14652       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
14653       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
14654     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
14655
14656 #if 0
14657   /* We default the array ordering.  SDB will probably do
14658      the right things even if DW_AT_ordering is not present.  It's not even
14659      an issue until we start to get into multidimensional arrays anyway.  If
14660      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
14661      then we'll have to put the DW_AT_ordering attribute back in.  (But if
14662      and when we find out that we need to put these in, we will only do so
14663      for multidimensional arrays.  */
14664   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
14665 #endif
14666
14667 #ifdef MIPS_DEBUGGING_INFO
14668   /* The SGI compilers handle arrays of unknown bound by setting
14669      AT_declaration and not emitting any subrange DIEs.  */
14670   if (! TYPE_DOMAIN (type))
14671     add_AT_flag (array_die, DW_AT_declaration, 1);
14672   else
14673 #endif
14674     add_subscript_info (array_die, type, collapse_nested_arrays);
14675
14676   /* Add representation of the type of the elements of this array type and
14677      emit the corresponding DIE if we haven't done it already.  */  
14678   element_type = TREE_TYPE (type);
14679   if (collapse_nested_arrays)
14680     while (TREE_CODE (element_type) == ARRAY_TYPE)
14681       {
14682         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
14683           break;
14684         element_type = TREE_TYPE (element_type);
14685       }
14686
14687 #ifndef MIPS_DEBUGGING_INFO
14688   gen_type_die (element_type, context_die);
14689 #endif
14690
14691   add_type_attribute (array_die, element_type, 0, 0, context_die);
14692
14693   if (get_AT (array_die, DW_AT_name))
14694     add_pubtype (type, array_die);
14695 }
14696
14697 static dw_loc_descr_ref
14698 descr_info_loc (tree val, tree base_decl)
14699 {
14700   HOST_WIDE_INT size;
14701   dw_loc_descr_ref loc, loc2;
14702   enum dwarf_location_atom op;
14703
14704   if (val == base_decl)
14705     return new_loc_descr (DW_OP_push_object_address, 0, 0);
14706
14707   switch (TREE_CODE (val))
14708     {
14709     CASE_CONVERT:
14710       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14711     case VAR_DECL:
14712       return loc_descriptor_from_tree_1 (val, 0);
14713     case INTEGER_CST:
14714       if (host_integerp (val, 0))
14715         return int_loc_descriptor (tree_low_cst (val, 0));
14716       break;
14717     case INDIRECT_REF:
14718       size = int_size_in_bytes (TREE_TYPE (val));
14719       if (size < 0)
14720         break;
14721       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14722       if (!loc)
14723         break;
14724       if (size == DWARF2_ADDR_SIZE)
14725         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
14726       else
14727         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
14728       return loc;
14729     case POINTER_PLUS_EXPR:
14730     case PLUS_EXPR:
14731       if (host_integerp (TREE_OPERAND (val, 1), 1)
14732           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
14733              < 16384)
14734         {
14735           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14736           if (!loc)
14737             break;
14738           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
14739         }
14740       else
14741         {
14742           op = DW_OP_plus;
14743         do_binop:
14744           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14745           if (!loc)
14746             break;
14747           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
14748           if (!loc2)
14749             break;
14750           add_loc_descr (&loc, loc2);
14751           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
14752         }
14753       return loc;
14754     case MINUS_EXPR:
14755       op = DW_OP_minus;
14756       goto do_binop;
14757     case MULT_EXPR:
14758       op = DW_OP_mul;
14759       goto do_binop;
14760     case EQ_EXPR:
14761       op = DW_OP_eq;
14762       goto do_binop;
14763     case NE_EXPR:
14764       op = DW_OP_ne;
14765       goto do_binop;
14766     default:
14767       break;
14768     }
14769   return NULL;
14770 }
14771
14772 static void
14773 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
14774                       tree val, tree base_decl)
14775 {
14776   dw_loc_descr_ref loc;
14777
14778   if (host_integerp (val, 0))
14779     {
14780       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
14781       return;
14782     }
14783
14784   loc = descr_info_loc (val, base_decl);
14785   if (!loc)
14786     return;
14787
14788   add_AT_loc (die, attr, loc);
14789 }
14790
14791 /* This routine generates DIE for array with hidden descriptor, details
14792    are filled into *info by a langhook.  */
14793
14794 static void
14795 gen_descr_array_type_die (tree type, struct array_descr_info *info,
14796                           dw_die_ref context_die)
14797 {
14798   dw_die_ref scope_die = scope_die_for (type, context_die);
14799   dw_die_ref array_die;
14800   int dim;
14801
14802   array_die = new_die (DW_TAG_array_type, scope_die, type);
14803   add_name_attribute (array_die, type_tag (type));
14804   equate_type_number_to_die (type, array_die);
14805
14806   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
14807   if (is_fortran ()
14808       && info->ndimensions >= 2)
14809     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
14810
14811   if (info->data_location)
14812     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
14813                           info->base_decl);
14814   if (info->associated)
14815     add_descr_info_field (array_die, DW_AT_associated, info->associated,
14816                           info->base_decl);
14817   if (info->allocated)
14818     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
14819                           info->base_decl);
14820
14821   for (dim = 0; dim < info->ndimensions; dim++)
14822     {
14823       dw_die_ref subrange_die
14824         = new_die (DW_TAG_subrange_type, array_die, NULL);
14825
14826       if (info->dimen[dim].lower_bound)
14827         {
14828           /* If it is the default value, omit it.  */
14829           if ((is_c_family () || is_java ())
14830               && integer_zerop (info->dimen[dim].lower_bound))
14831             ;
14832           else if (is_fortran ()
14833                    && integer_onep (info->dimen[dim].lower_bound))
14834             ;
14835           else
14836             add_descr_info_field (subrange_die, DW_AT_lower_bound,
14837                                   info->dimen[dim].lower_bound,
14838                                   info->base_decl);
14839         }
14840       if (info->dimen[dim].upper_bound)
14841         add_descr_info_field (subrange_die, DW_AT_upper_bound,
14842                               info->dimen[dim].upper_bound,
14843                               info->base_decl);
14844       if (info->dimen[dim].stride)
14845         add_descr_info_field (subrange_die, DW_AT_byte_stride,
14846                               info->dimen[dim].stride,
14847                               info->base_decl);
14848     }
14849
14850   gen_type_die (info->element_type, context_die);
14851   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
14852
14853   if (get_AT (array_die, DW_AT_name))
14854     add_pubtype (type, array_die);
14855 }
14856
14857 #if 0
14858 static void
14859 gen_entry_point_die (tree decl, dw_die_ref context_die)
14860 {
14861   tree origin = decl_ultimate_origin (decl);
14862   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
14863
14864   if (origin != NULL)
14865     add_abstract_origin_attribute (decl_die, origin);
14866   else
14867     {
14868       add_name_and_src_coords_attributes (decl_die, decl);
14869       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
14870                           0, 0, context_die);
14871     }
14872
14873   if (DECL_ABSTRACT (decl))
14874     equate_decl_number_to_die (decl, decl_die);
14875   else
14876     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
14877 }
14878 #endif
14879
14880 /* Walk through the list of incomplete types again, trying once more to
14881    emit full debugging info for them.  */
14882
14883 static void
14884 retry_incomplete_types (void)
14885 {
14886   int i;
14887
14888   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
14889     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
14890 }
14891
14892 /* Determine what tag to use for a record type.  */
14893
14894 static enum dwarf_tag
14895 record_type_tag (tree type)
14896 {
14897   if (! lang_hooks.types.classify_record)
14898     return DW_TAG_structure_type;
14899
14900   switch (lang_hooks.types.classify_record (type))
14901     {
14902     case RECORD_IS_STRUCT:
14903       return DW_TAG_structure_type;
14904
14905     case RECORD_IS_CLASS:
14906       return DW_TAG_class_type;
14907
14908     case RECORD_IS_INTERFACE:
14909       return DW_TAG_interface_type;
14910
14911     default:
14912       gcc_unreachable ();
14913     }
14914 }
14915
14916 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
14917    include all of the information about the enumeration values also. Each
14918    enumerated type name/value is listed as a child of the enumerated type
14919    DIE.  */
14920
14921 static dw_die_ref
14922 gen_enumeration_type_die (tree type, dw_die_ref context_die)
14923 {
14924   dw_die_ref type_die = lookup_type_die (type);
14925
14926   if (type_die == NULL)
14927     {
14928       type_die = new_die (DW_TAG_enumeration_type,
14929                           scope_die_for (type, context_die), type);
14930       equate_type_number_to_die (type, type_die);
14931       add_name_attribute (type_die, type_tag (type));
14932     }
14933   else if (! TYPE_SIZE (type))
14934     return type_die;
14935   else
14936     remove_AT (type_die, DW_AT_declaration);
14937
14938   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
14939      given enum type is incomplete, do not generate the DW_AT_byte_size
14940      attribute or the DW_AT_element_list attribute.  */
14941   if (TYPE_SIZE (type))
14942     {
14943       tree link;
14944
14945       TREE_ASM_WRITTEN (type) = 1;
14946       add_byte_size_attribute (type_die, type);
14947       if (TYPE_STUB_DECL (type) != NULL_TREE)
14948         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14949
14950       /* If the first reference to this type was as the return type of an
14951          inline function, then it may not have a parent.  Fix this now.  */
14952       if (type_die->die_parent == NULL)
14953         add_child_die (scope_die_for (type, context_die), type_die);
14954
14955       for (link = TYPE_VALUES (type);
14956            link != NULL; link = TREE_CHAIN (link))
14957         {
14958           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
14959           tree value = TREE_VALUE (link);
14960
14961           add_name_attribute (enum_die,
14962                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
14963
14964           if (TREE_CODE (value) == CONST_DECL)
14965             value = DECL_INITIAL (value);
14966
14967           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
14968             /* DWARF2 does not provide a way of indicating whether or
14969                not enumeration constants are signed or unsigned.  GDB
14970                always assumes the values are signed, so we output all
14971                values as if they were signed.  That means that
14972                enumeration constants with very large unsigned values
14973                will appear to have negative values in the debugger.  */
14974             add_AT_int (enum_die, DW_AT_const_value,
14975                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
14976         }
14977     }
14978   else
14979     add_AT_flag (type_die, DW_AT_declaration, 1);
14980
14981   if (get_AT (type_die, DW_AT_name))
14982     add_pubtype (type, type_die);
14983
14984   return type_die;
14985 }
14986
14987 /* Generate a DIE to represent either a real live formal parameter decl or to
14988    represent just the type of some formal parameter position in some function
14989    type.
14990
14991    Note that this routine is a bit unusual because its argument may be a
14992    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
14993    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
14994    node.  If it's the former then this function is being called to output a
14995    DIE to represent a formal parameter object (or some inlining thereof).  If
14996    it's the latter, then this function is only being called to output a
14997    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
14998    argument type of some subprogram type.  */
14999
15000 static dw_die_ref
15001 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
15002 {
15003   tree node_or_origin = node ? node : origin;
15004   dw_die_ref parm_die
15005     = new_die (DW_TAG_formal_parameter, context_die, node);
15006
15007   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
15008     {
15009     case tcc_declaration:
15010       if (!origin)
15011         origin = decl_ultimate_origin (node);
15012       if (origin != NULL)
15013         add_abstract_origin_attribute (parm_die, origin);
15014       else
15015         {
15016           tree type = TREE_TYPE (node);
15017           add_name_and_src_coords_attributes (parm_die, node);
15018           if (DECL_BY_REFERENCE (node))
15019             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
15020                                 context_die);
15021           else
15022             add_type_attribute (parm_die, type,
15023                                 TREE_READONLY (node),
15024                                 TREE_THIS_VOLATILE (node),
15025                                 context_die);
15026           if (DECL_ARTIFICIAL (node))
15027             add_AT_flag (parm_die, DW_AT_artificial, 1);
15028         }
15029
15030       if (node && node != origin)
15031         equate_decl_number_to_die (node, parm_die);
15032       if (! DECL_ABSTRACT (node_or_origin))
15033         add_location_or_const_value_attribute (parm_die, node_or_origin,
15034                                                DW_AT_location);
15035
15036       break;
15037
15038     case tcc_type:
15039       /* We were called with some kind of a ..._TYPE node.  */
15040       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
15041       break;
15042
15043     default:
15044       gcc_unreachable ();
15045     }
15046
15047   return parm_die;
15048 }
15049
15050 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
15051    at the end of an (ANSI prototyped) formal parameters list.  */
15052
15053 static void
15054 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
15055 {
15056   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
15057 }
15058
15059 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
15060    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
15061    parameters as specified in some function type specification (except for
15062    those which appear as part of a function *definition*).  */
15063
15064 static void
15065 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
15066 {
15067   tree link;
15068   tree formal_type = NULL;
15069   tree first_parm_type;
15070   tree arg;
15071
15072   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
15073     {
15074       arg = DECL_ARGUMENTS (function_or_method_type);
15075       function_or_method_type = TREE_TYPE (function_or_method_type);
15076     }
15077   else
15078     arg = NULL_TREE;
15079
15080   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
15081
15082   /* Make our first pass over the list of formal parameter types and output a
15083      DW_TAG_formal_parameter DIE for each one.  */
15084   for (link = first_parm_type; link; )
15085     {
15086       dw_die_ref parm_die;
15087
15088       formal_type = TREE_VALUE (link);
15089       if (formal_type == void_type_node)
15090         break;
15091
15092       /* Output a (nameless) DIE to represent the formal parameter itself.  */
15093       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
15094       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
15095            && link == first_parm_type)
15096           || (arg && DECL_ARTIFICIAL (arg)))
15097         add_AT_flag (parm_die, DW_AT_artificial, 1);
15098
15099       link = TREE_CHAIN (link);
15100       if (arg)
15101         arg = TREE_CHAIN (arg);
15102     }
15103
15104   /* If this function type has an ellipsis, add a
15105      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
15106   if (formal_type != void_type_node)
15107     gen_unspecified_parameters_die (function_or_method_type, context_die);
15108
15109   /* Make our second (and final) pass over the list of formal parameter types
15110      and output DIEs to represent those types (as necessary).  */
15111   for (link = TYPE_ARG_TYPES (function_or_method_type);
15112        link && TREE_VALUE (link);
15113        link = TREE_CHAIN (link))
15114     gen_type_die (TREE_VALUE (link), context_die);
15115 }
15116
15117 /* We want to generate the DIE for TYPE so that we can generate the
15118    die for MEMBER, which has been defined; we will need to refer back
15119    to the member declaration nested within TYPE.  If we're trying to
15120    generate minimal debug info for TYPE, processing TYPE won't do the
15121    trick; we need to attach the member declaration by hand.  */
15122
15123 static void
15124 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
15125 {
15126   gen_type_die (type, context_die);
15127
15128   /* If we're trying to avoid duplicate debug info, we may not have
15129      emitted the member decl for this function.  Emit it now.  */
15130   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
15131       && ! lookup_decl_die (member))
15132     {
15133       dw_die_ref type_die;
15134       gcc_assert (!decl_ultimate_origin (member));
15135
15136       push_decl_scope (type);
15137       type_die = lookup_type_die (type);
15138       if (TREE_CODE (member) == FUNCTION_DECL)
15139         gen_subprogram_die (member, type_die);
15140       else if (TREE_CODE (member) == FIELD_DECL)
15141         {
15142           /* Ignore the nameless fields that are used to skip bits but handle
15143              C++ anonymous unions and structs.  */
15144           if (DECL_NAME (member) != NULL_TREE
15145               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
15146               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
15147             {
15148               gen_type_die (member_declared_type (member), type_die);
15149               gen_field_die (member, type_die);
15150             }
15151         }
15152       else
15153         gen_variable_die (member, NULL_TREE, type_die);
15154
15155       pop_decl_scope ();
15156     }
15157 }
15158
15159 /* Generate the DWARF2 info for the "abstract" instance of a function which we
15160    may later generate inlined and/or out-of-line instances of.  */
15161
15162 static void
15163 dwarf2out_abstract_function (tree decl)
15164 {
15165   dw_die_ref old_die;
15166   tree save_fn;
15167   tree context;
15168   int was_abstract = DECL_ABSTRACT (decl);
15169
15170   /* Make sure we have the actual abstract inline, not a clone.  */
15171   decl = DECL_ORIGIN (decl);
15172   htab_empty (decl_loc_table);
15173
15174   old_die = lookup_decl_die (decl);
15175   if (old_die && get_AT (old_die, DW_AT_inline))
15176     /* We've already generated the abstract instance.  */
15177     return;
15178
15179   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
15180      we don't get confused by DECL_ABSTRACT.  */
15181   if (debug_info_level > DINFO_LEVEL_TERSE)
15182     {
15183       context = decl_class_context (decl);
15184       if (context)
15185         gen_type_die_for_member
15186           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
15187     }
15188
15189   /* Pretend we've just finished compiling this function.  */
15190   save_fn = current_function_decl;
15191   current_function_decl = decl;
15192   push_cfun (DECL_STRUCT_FUNCTION (decl));
15193
15194   set_decl_abstract_flags (decl, 1);
15195   dwarf2out_decl (decl);
15196   if (! was_abstract)
15197     set_decl_abstract_flags (decl, 0);
15198
15199   current_function_decl = save_fn;
15200   pop_cfun ();
15201 }
15202
15203 /* Helper function of premark_used_types() which gets called through
15204    htab_traverse_resize().
15205
15206    Marks the DIE of a given type in *SLOT as perennial, so it never gets
15207    marked as unused by prune_unused_types.  */
15208 static int
15209 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
15210 {
15211   tree type;
15212   dw_die_ref die;
15213
15214   type = (tree) *slot;
15215   die = lookup_type_die (type);
15216   if (die != NULL)
15217     die->die_perennial_p = 1;
15218   return 1;
15219 }
15220
15221 /* Mark all members of used_types_hash as perennial.  */
15222 static void
15223 premark_used_types (void)
15224 {
15225   if (cfun && cfun->used_types_hash)
15226     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
15227 }
15228
15229 /* Generate a DIE to represent a declared function (either file-scope or
15230    block-local).  */
15231
15232 static void
15233 gen_subprogram_die (tree decl, dw_die_ref context_die)
15234 {
15235   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15236   tree origin = decl_ultimate_origin (decl);
15237   dw_die_ref subr_die;
15238   tree fn_arg_types;
15239   tree outer_scope;
15240   dw_die_ref old_die = lookup_decl_die (decl);
15241   int declaration = (current_function_decl != decl
15242                      || class_or_namespace_scope_p (context_die));
15243
15244   premark_used_types ();
15245
15246   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
15247      started to generate the abstract instance of an inline, decided to output
15248      its containing class, and proceeded to emit the declaration of the inline
15249      from the member list for the class.  If so, DECLARATION takes priority;
15250      we'll get back to the abstract instance when done with the class.  */
15251
15252   /* The class-scope declaration DIE must be the primary DIE.  */
15253   if (origin && declaration && class_or_namespace_scope_p (context_die))
15254     {
15255       origin = NULL;
15256       gcc_assert (!old_die);
15257     }
15258
15259   /* Now that the C++ front end lazily declares artificial member fns, we
15260      might need to retrofit the declaration into its class.  */
15261   if (!declaration && !origin && !old_die
15262       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
15263       && !class_or_namespace_scope_p (context_die)
15264       && debug_info_level > DINFO_LEVEL_TERSE)
15265     old_die = force_decl_die (decl);
15266
15267   if (origin != NULL)
15268     {
15269       gcc_assert (!declaration || local_scope_p (context_die));
15270
15271       /* Fixup die_parent for the abstract instance of a nested
15272          inline function.  */
15273       if (old_die && old_die->die_parent == NULL)
15274         add_child_die (context_die, old_die);
15275
15276       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15277       add_abstract_origin_attribute (subr_die, origin);
15278     }
15279   else if (old_die)
15280     {
15281       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
15282       struct dwarf_file_data * file_index = lookup_filename (s.file);
15283
15284       if (!get_AT_flag (old_die, DW_AT_declaration)
15285           /* We can have a normal definition following an inline one in the
15286              case of redefinition of GNU C extern inlines.
15287              It seems reasonable to use AT_specification in this case.  */
15288           && !get_AT (old_die, DW_AT_inline))
15289         {
15290           /* Detect and ignore this case, where we are trying to output
15291              something we have already output.  */
15292           return;
15293         }
15294
15295       /* If the definition comes from the same place as the declaration,
15296          maybe use the old DIE.  We always want the DIE for this function
15297          that has the *_pc attributes to be under comp_unit_die so the
15298          debugger can find it.  We also need to do this for abstract
15299          instances of inlines, since the spec requires the out-of-line copy
15300          to have the same parent.  For local class methods, this doesn't
15301          apply; we just use the old DIE.  */
15302       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
15303           && (DECL_ARTIFICIAL (decl)
15304               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
15305                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
15306                       == (unsigned) s.line))))
15307         {
15308           subr_die = old_die;
15309
15310           /* Clear out the declaration attribute and the formal parameters.
15311              Do not remove all children, because it is possible that this
15312              declaration die was forced using force_decl_die(). In such
15313              cases die that forced declaration die (e.g. TAG_imported_module)
15314              is one of the children that we do not want to remove.  */
15315           remove_AT (subr_die, DW_AT_declaration);
15316           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
15317         }
15318       else
15319         {
15320           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15321           add_AT_specification (subr_die, old_die);
15322           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
15323             add_AT_file (subr_die, DW_AT_decl_file, file_index);
15324           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
15325             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
15326         }
15327     }
15328   else
15329     {
15330       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15331
15332       if (TREE_PUBLIC (decl))
15333         add_AT_flag (subr_die, DW_AT_external, 1);
15334
15335       add_name_and_src_coords_attributes (subr_die, decl);
15336       if (debug_info_level > DINFO_LEVEL_TERSE)
15337         {
15338           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
15339           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
15340                               0, 0, context_die);
15341         }
15342
15343       add_pure_or_virtual_attribute (subr_die, decl);
15344       if (DECL_ARTIFICIAL (decl))
15345         add_AT_flag (subr_die, DW_AT_artificial, 1);
15346
15347       if (TREE_PROTECTED (decl))
15348         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
15349       else if (TREE_PRIVATE (decl))
15350         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
15351     }
15352
15353   if (declaration)
15354     {
15355       if (!old_die || !get_AT (old_die, DW_AT_inline))
15356         {
15357           add_AT_flag (subr_die, DW_AT_declaration, 1);
15358
15359           /* If this is an explicit function declaration then generate
15360              a DW_AT_explicit attribute.  */
15361           if (lang_hooks.decls.function_decl_explicit_p (decl))
15362             add_AT_flag (subr_die, DW_AT_explicit, 1);
15363
15364           /* The first time we see a member function, it is in the context of
15365              the class to which it belongs.  We make sure of this by emitting
15366              the class first.  The next time is the definition, which is
15367              handled above.  The two may come from the same source text.
15368
15369              Note that force_decl_die() forces function declaration die. It is
15370              later reused to represent definition.  */
15371           equate_decl_number_to_die (decl, subr_die);
15372         }
15373     }
15374   else if (DECL_ABSTRACT (decl))
15375     {
15376       if (DECL_DECLARED_INLINE_P (decl))
15377         {
15378           if (cgraph_function_possibly_inlined_p (decl))
15379             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
15380           else
15381             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
15382         }
15383       else
15384         {
15385           if (cgraph_function_possibly_inlined_p (decl))
15386             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
15387           else
15388             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
15389         }
15390
15391       if (DECL_DECLARED_INLINE_P (decl)
15392           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
15393         add_AT_flag (subr_die, DW_AT_artificial, 1);
15394
15395       equate_decl_number_to_die (decl, subr_die);
15396     }
15397   else if (!DECL_EXTERNAL (decl))
15398     {
15399       HOST_WIDE_INT cfa_fb_offset;
15400
15401       if (!old_die || !get_AT (old_die, DW_AT_inline))
15402         equate_decl_number_to_die (decl, subr_die);
15403
15404       if (!flag_reorder_blocks_and_partition)
15405         {
15406           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
15407                                        current_function_funcdef_no);
15408           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
15409           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15410                                        current_function_funcdef_no);
15411           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
15412
15413           add_pubname (decl, subr_die);
15414           add_arange (decl, subr_die);
15415         }
15416       else
15417         {  /* Do nothing for now; maybe need to duplicate die, one for
15418               hot section and one for cold section, then use the hot/cold
15419               section begin/end labels to generate the aranges...  */
15420           /*
15421             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
15422             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
15423             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
15424             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
15425
15426             add_pubname (decl, subr_die);
15427             add_arange (decl, subr_die);
15428             add_arange (decl, subr_die);
15429            */
15430         }
15431
15432 #ifdef MIPS_DEBUGGING_INFO
15433       /* Add a reference to the FDE for this routine.  */
15434       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
15435 #endif
15436
15437       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
15438
15439       /* We define the "frame base" as the function's CFA.  This is more
15440          convenient for several reasons: (1) It's stable across the prologue
15441          and epilogue, which makes it better than just a frame pointer,
15442          (2) With dwarf3, there exists a one-byte encoding that allows us
15443          to reference the .debug_frame data by proxy, but failing that,
15444          (3) We can at least reuse the code inspection and interpretation
15445          code that determines the CFA position at various points in the
15446          function.  */
15447       if (dwarf_version >= 3)
15448         {
15449           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
15450           add_AT_loc (subr_die, DW_AT_frame_base, op);
15451         }
15452       else
15453         {
15454           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
15455           if (list->dw_loc_next)
15456             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
15457           else
15458             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
15459         }
15460
15461       /* Compute a displacement from the "steady-state frame pointer" to
15462          the CFA.  The former is what all stack slots and argument slots
15463          will reference in the rtl; the later is what we've told the
15464          debugger about.  We'll need to adjust all frame_base references
15465          by this displacement.  */
15466       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
15467
15468       if (cfun->static_chain_decl)
15469         add_AT_location_description (subr_die, DW_AT_static_link,
15470                  loc_descriptor_from_tree (cfun->static_chain_decl));
15471     }
15472
15473   /* Generate child dies for template paramaters.  */
15474   if (debug_info_level > DINFO_LEVEL_TERSE)
15475     gen_generic_params_dies (decl);
15476
15477   /* Now output descriptions of the arguments for this function. This gets
15478      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
15479      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
15480      `...' at the end of the formal parameter list.  In order to find out if
15481      there was a trailing ellipsis or not, we must instead look at the type
15482      associated with the FUNCTION_DECL.  This will be a node of type
15483      FUNCTION_TYPE. If the chain of type nodes hanging off of this
15484      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
15485      an ellipsis at the end.  */
15486
15487   /* In the case where we are describing a mere function declaration, all we
15488      need to do here (and all we *can* do here) is to describe the *types* of
15489      its formal parameters.  */
15490   if (debug_info_level <= DINFO_LEVEL_TERSE)
15491     ;
15492   else if (declaration)
15493     gen_formal_types_die (decl, subr_die);
15494   else
15495     {
15496       /* Generate DIEs to represent all known formal parameters.  */
15497       tree arg_decls = DECL_ARGUMENTS (decl);
15498       tree parm;
15499
15500       /* When generating DIEs, generate the unspecified_parameters DIE
15501          instead if we come across the arg "__builtin_va_alist" */
15502       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
15503         if (TREE_CODE (parm) == PARM_DECL)
15504           {
15505             if (DECL_NAME (parm)
15506                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
15507                             "__builtin_va_alist"))
15508               gen_unspecified_parameters_die (parm, subr_die);
15509             else
15510               gen_decl_die (parm, NULL, subr_die);
15511           }
15512
15513       /* Decide whether we need an unspecified_parameters DIE at the end.
15514          There are 2 more cases to do this for: 1) the ansi ... declaration -
15515          this is detectable when the end of the arg list is not a
15516          void_type_node 2) an unprototyped function declaration (not a
15517          definition).  This just means that we have no info about the
15518          parameters at all.  */
15519       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
15520       if (fn_arg_types != NULL)
15521         {
15522           /* This is the prototyped case, check for....  */
15523           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
15524             gen_unspecified_parameters_die (decl, subr_die);
15525         }
15526       else if (DECL_INITIAL (decl) == NULL_TREE)
15527         gen_unspecified_parameters_die (decl, subr_die);
15528     }
15529
15530   /* Output Dwarf info for all of the stuff within the body of the function
15531      (if it has one - it may be just a declaration).  */
15532   outer_scope = DECL_INITIAL (decl);
15533
15534   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
15535      a function.  This BLOCK actually represents the outermost binding contour
15536      for the function, i.e. the contour in which the function's formal
15537      parameters and labels get declared. Curiously, it appears that the front
15538      end doesn't actually put the PARM_DECL nodes for the current function onto
15539      the BLOCK_VARS list for this outer scope, but are strung off of the
15540      DECL_ARGUMENTS list for the function instead.
15541
15542      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
15543      the LABEL_DECL nodes for the function however, and we output DWARF info
15544      for those in decls_for_scope.  Just within the `outer_scope' there will be
15545      a BLOCK node representing the function's outermost pair of curly braces,
15546      and any blocks used for the base and member initializers of a C++
15547      constructor function.  */
15548   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
15549     {
15550       /* Emit a DW_TAG_variable DIE for a named return value.  */
15551       if (DECL_NAME (DECL_RESULT (decl)))
15552         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
15553
15554       current_function_has_inlines = 0;
15555       decls_for_scope (outer_scope, subr_die, 0);
15556
15557 #if 0 && defined (MIPS_DEBUGGING_INFO)
15558       if (current_function_has_inlines)
15559         {
15560           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
15561           if (! comp_unit_has_inlines)
15562             {
15563               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
15564               comp_unit_has_inlines = 1;
15565             }
15566         }
15567 #endif
15568     }
15569   /* Add the calling convention attribute if requested.  */
15570   add_calling_convention_attribute (subr_die, decl);
15571
15572 }
15573
15574 /* Returns a hash value for X (which really is a die_struct).  */
15575
15576 static hashval_t
15577 common_block_die_table_hash (const void *x)
15578 {
15579   const_dw_die_ref d = (const_dw_die_ref) x;
15580   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
15581 }
15582
15583 /* Return nonzero if decl_id and die_parent of die_struct X is the same
15584    as decl_id and die_parent of die_struct Y.  */
15585
15586 static int
15587 common_block_die_table_eq (const void *x, const void *y)
15588 {
15589   const_dw_die_ref d = (const_dw_die_ref) x;
15590   const_dw_die_ref e = (const_dw_die_ref) y;
15591   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
15592 }
15593
15594 /* Generate a DIE to represent a declared data object.
15595    Either DECL or ORIGIN must be non-null.  */
15596
15597 static void
15598 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
15599 {
15600   HOST_WIDE_INT off;
15601   tree com_decl;
15602   tree decl_or_origin = decl ? decl : origin;
15603   dw_die_ref var_die;
15604   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
15605   dw_die_ref origin_die;
15606   int declaration = (DECL_EXTERNAL (decl_or_origin)
15607                      /* If DECL is COMDAT and has not actually been
15608                         emitted, we cannot take its address; there
15609                         might end up being no definition anywhere in
15610                         the program.  For example, consider the C++
15611                         test case:
15612
15613                           template <class T>
15614                           struct S { static const int i = 7; };
15615
15616                           template <class T>
15617                           const int S<T>::i;
15618
15619                           int f() { return S<int>::i; }
15620
15621                         Here, S<int>::i is not DECL_EXTERNAL, but no
15622                         definition is required, so the compiler will
15623                         not emit a definition.  */
15624                      || (TREE_CODE (decl_or_origin) == VAR_DECL
15625                          && DECL_COMDAT (decl_or_origin)
15626                          && !TREE_ASM_WRITTEN (decl_or_origin))
15627                      || class_or_namespace_scope_p (context_die));
15628
15629   if (!origin)
15630     origin = decl_ultimate_origin (decl);
15631
15632   com_decl = fortran_common (decl_or_origin, &off);
15633
15634   /* Symbol in common gets emitted as a child of the common block, in the form
15635      of a data member.  */
15636   if (com_decl)
15637     {
15638       tree field;
15639       dw_die_ref com_die;
15640       dw_loc_descr_ref loc;
15641       die_node com_die_arg;
15642
15643       var_die = lookup_decl_die (decl_or_origin);
15644       if (var_die)
15645         {
15646           if (get_AT (var_die, DW_AT_location) == NULL)
15647             {
15648               loc = loc_descriptor_from_tree (com_decl);
15649               if (loc)
15650                 {
15651                   if (off)
15652                     {
15653                       /* Optimize the common case.  */
15654                       if (loc->dw_loc_opc == DW_OP_addr
15655                           && loc->dw_loc_next == NULL
15656                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
15657                              == SYMBOL_REF)
15658                         loc->dw_loc_oprnd1.v.val_addr
15659                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
15660                         else
15661                           loc_descr_plus_const (&loc, off);
15662                     }
15663                   add_AT_loc (var_die, DW_AT_location, loc);
15664                   remove_AT (var_die, DW_AT_declaration);
15665                 }
15666             }
15667           return;
15668         }
15669
15670       if (common_block_die_table == NULL)
15671         common_block_die_table
15672           = htab_create_ggc (10, common_block_die_table_hash,
15673                              common_block_die_table_eq, NULL);
15674
15675       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
15676       com_die_arg.decl_id = DECL_UID (com_decl);
15677       com_die_arg.die_parent = context_die;
15678       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
15679       loc = loc_descriptor_from_tree (com_decl);
15680       if (com_die == NULL)
15681         {
15682           const char *cnam
15683             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
15684           void **slot;
15685
15686           com_die = new_die (DW_TAG_common_block, context_die, decl);
15687           add_name_and_src_coords_attributes (com_die, com_decl);
15688           if (loc)
15689             {
15690               add_AT_loc (com_die, DW_AT_location, loc);
15691               /* Avoid sharing the same loc descriptor between
15692                  DW_TAG_common_block and DW_TAG_variable.  */
15693               loc = loc_descriptor_from_tree (com_decl);
15694             }
15695           else if (DECL_EXTERNAL (decl))
15696             add_AT_flag (com_die, DW_AT_declaration, 1);
15697           add_pubname_string (cnam, com_die); /* ??? needed? */
15698           com_die->decl_id = DECL_UID (com_decl);
15699           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
15700           *slot = (void *) com_die;
15701         }
15702       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
15703         {
15704           add_AT_loc (com_die, DW_AT_location, loc);
15705           loc = loc_descriptor_from_tree (com_decl);
15706           remove_AT (com_die, DW_AT_declaration);
15707         }
15708       var_die = new_die (DW_TAG_variable, com_die, decl);
15709       add_name_and_src_coords_attributes (var_die, decl);
15710       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
15711                           TREE_THIS_VOLATILE (decl), context_die);
15712       add_AT_flag (var_die, DW_AT_external, 1);
15713       if (loc)
15714         {
15715           if (off)
15716             {
15717               /* Optimize the common case.  */
15718               if (loc->dw_loc_opc == DW_OP_addr
15719                   && loc->dw_loc_next == NULL
15720                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
15721                 loc->dw_loc_oprnd1.v.val_addr
15722                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
15723               else
15724                 loc_descr_plus_const (&loc, off);
15725             }
15726           add_AT_loc (var_die, DW_AT_location, loc);
15727         }
15728       else if (DECL_EXTERNAL (decl))
15729         add_AT_flag (var_die, DW_AT_declaration, 1);
15730       equate_decl_number_to_die (decl, var_die);
15731       return;
15732     }
15733
15734   /* If the compiler emitted a definition for the DECL declaration
15735      and if we already emitted a DIE for it, don't emit a second
15736      DIE for it again.  */
15737   if (old_die
15738       && declaration
15739       && old_die->die_parent == context_die)
15740     return;
15741
15742   /* For static data members, the declaration in the class is supposed
15743      to have DW_TAG_member tag; the specification should still be
15744      DW_TAG_variable referencing the DW_TAG_member DIE.  */
15745   if (declaration && class_scope_p (context_die))
15746     var_die = new_die (DW_TAG_member, context_die, decl);
15747   else
15748     var_die = new_die (DW_TAG_variable, context_die, decl);
15749
15750   origin_die = NULL;
15751   if (origin != NULL)
15752     origin_die = add_abstract_origin_attribute (var_die, origin);
15753
15754   /* Loop unrolling can create multiple blocks that refer to the same
15755      static variable, so we must test for the DW_AT_declaration flag.
15756
15757      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
15758      copy decls and set the DECL_ABSTRACT flag on them instead of
15759      sharing them.
15760
15761      ??? Duplicated blocks have been rewritten to use .debug_ranges.
15762
15763      ??? The declare_in_namespace support causes us to get two DIEs for one
15764      variable, both of which are declarations.  We want to avoid considering
15765      one to be a specification, so we must test that this DIE is not a
15766      declaration.  */
15767   else if (old_die && TREE_STATIC (decl) && ! declaration
15768            && get_AT_flag (old_die, DW_AT_declaration) == 1)
15769     {
15770       /* This is a definition of a C++ class level static.  */
15771       add_AT_specification (var_die, old_die);
15772       if (DECL_NAME (decl))
15773         {
15774           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
15775           struct dwarf_file_data * file_index = lookup_filename (s.file);
15776
15777           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
15778             add_AT_file (var_die, DW_AT_decl_file, file_index);
15779
15780           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
15781             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
15782         }
15783     }
15784   else
15785     {
15786       tree type = TREE_TYPE (decl);
15787
15788       add_name_and_src_coords_attributes (var_die, decl);
15789       if ((TREE_CODE (decl) == PARM_DECL
15790            || TREE_CODE (decl) == RESULT_DECL
15791            || TREE_CODE (decl) == VAR_DECL)
15792           && DECL_BY_REFERENCE (decl))
15793         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
15794       else
15795         add_type_attribute (var_die, type, TREE_READONLY (decl),
15796                             TREE_THIS_VOLATILE (decl), context_die);
15797
15798       if (TREE_PUBLIC (decl))
15799         add_AT_flag (var_die, DW_AT_external, 1);
15800
15801       if (DECL_ARTIFICIAL (decl))
15802         add_AT_flag (var_die, DW_AT_artificial, 1);
15803
15804       if (TREE_PROTECTED (decl))
15805         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
15806       else if (TREE_PRIVATE (decl))
15807         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
15808     }
15809
15810   if (declaration)
15811     add_AT_flag (var_die, DW_AT_declaration, 1);
15812
15813   if (decl && (DECL_ABSTRACT (decl) || declaration))
15814     equate_decl_number_to_die (decl, var_die);
15815
15816   if (! declaration
15817       && (! DECL_ABSTRACT (decl_or_origin)
15818           /* Local static vars are shared between all clones/inlines,
15819              so emit DW_AT_location on the abstract DIE if DECL_RTL is
15820              already set.  */
15821           || (TREE_CODE (decl_or_origin) == VAR_DECL
15822               && TREE_STATIC (decl_or_origin)
15823               && DECL_RTL_SET_P (decl_or_origin)))
15824       /* When abstract origin already has DW_AT_location attribute, no need
15825          to add it again.  */
15826       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
15827     {
15828       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
15829           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
15830         defer_location (decl_or_origin, var_die);
15831       else
15832         add_location_or_const_value_attribute (var_die,
15833                                                decl_or_origin,
15834                                                DW_AT_location);
15835       add_pubname (decl_or_origin, var_die);
15836     }
15837   else
15838     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
15839 }
15840
15841 /* Generate a DIE to represent a named constant.  */
15842
15843 static void
15844 gen_const_die (tree decl, dw_die_ref context_die)
15845 {
15846   dw_die_ref const_die;
15847   tree type = TREE_TYPE (decl);
15848
15849   const_die = new_die (DW_TAG_constant, context_die, decl);
15850   add_name_and_src_coords_attributes (const_die, decl);
15851   add_type_attribute (const_die, type, 1, 0, context_die);
15852   if (TREE_PUBLIC (decl))
15853     add_AT_flag (const_die, DW_AT_external, 1);
15854   if (DECL_ARTIFICIAL (decl))
15855     add_AT_flag (const_die, DW_AT_artificial, 1);
15856   tree_add_const_value_attribute_for_decl (const_die, decl);
15857 }
15858
15859 /* Generate a DIE to represent a label identifier.  */
15860
15861 static void
15862 gen_label_die (tree decl, dw_die_ref context_die)
15863 {
15864   tree origin = decl_ultimate_origin (decl);
15865   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
15866   rtx insn;
15867   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15868
15869   if (origin != NULL)
15870     add_abstract_origin_attribute (lbl_die, origin);
15871   else
15872     add_name_and_src_coords_attributes (lbl_die, decl);
15873
15874   if (DECL_ABSTRACT (decl))
15875     equate_decl_number_to_die (decl, lbl_die);
15876   else
15877     {
15878       insn = DECL_RTL_IF_SET (decl);
15879
15880       /* Deleted labels are programmer specified labels which have been
15881          eliminated because of various optimizations.  We still emit them
15882          here so that it is possible to put breakpoints on them.  */
15883       if (insn
15884           && (LABEL_P (insn)
15885               || ((NOTE_P (insn)
15886                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
15887         {
15888           /* When optimization is enabled (via -O) some parts of the compiler
15889              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
15890              represent source-level labels which were explicitly declared by
15891              the user.  This really shouldn't be happening though, so catch
15892              it if it ever does happen.  */
15893           gcc_assert (!INSN_DELETED_P (insn));
15894
15895           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
15896           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
15897         }
15898     }
15899 }
15900
15901 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
15902    attributes to the DIE for a block STMT, to describe where the inlined
15903    function was called from.  This is similar to add_src_coords_attributes.  */
15904
15905 static inline void
15906 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
15907 {
15908   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
15909
15910   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
15911   add_AT_unsigned (die, DW_AT_call_line, s.line);
15912 }
15913
15914
15915 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
15916    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
15917
15918 static inline void
15919 add_high_low_attributes (tree stmt, dw_die_ref die)
15920 {
15921   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15922
15923   if (BLOCK_FRAGMENT_CHAIN (stmt))
15924     {
15925       tree chain;
15926
15927       if (inlined_function_outer_scope_p (stmt))
15928         {
15929           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
15930                                        BLOCK_NUMBER (stmt));
15931           add_AT_lbl_id (die, DW_AT_entry_pc, label);
15932         }
15933
15934       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
15935
15936       chain = BLOCK_FRAGMENT_CHAIN (stmt);
15937       do
15938         {
15939           add_ranges (chain);
15940           chain = BLOCK_FRAGMENT_CHAIN (chain);
15941         }
15942       while (chain);
15943       add_ranges (NULL);
15944     }
15945   else
15946     {
15947       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
15948                                    BLOCK_NUMBER (stmt));
15949       add_AT_lbl_id (die, DW_AT_low_pc, label);
15950       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
15951                                    BLOCK_NUMBER (stmt));
15952       add_AT_lbl_id (die, DW_AT_high_pc, label);
15953     }
15954 }
15955
15956 /* Generate a DIE for a lexical block.  */
15957
15958 static void
15959 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
15960 {
15961   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
15962
15963   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
15964     add_high_low_attributes (stmt, stmt_die);
15965
15966   decls_for_scope (stmt, stmt_die, depth);
15967 }
15968
15969 /* Generate a DIE for an inlined subprogram.  */
15970
15971 static void
15972 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
15973 {
15974   tree decl;
15975
15976   /* The instance of function that is effectively being inlined shall not
15977      be abstract.  */
15978   gcc_assert (! BLOCK_ABSTRACT (stmt));
15979
15980   decl = block_ultimate_origin (stmt);
15981
15982   /* Emit info for the abstract instance first, if we haven't yet.  We
15983      must emit this even if the block is abstract, otherwise when we
15984      emit the block below (or elsewhere), we may end up trying to emit
15985      a die whose origin die hasn't been emitted, and crashing.  */
15986   dwarf2out_abstract_function (decl);
15987
15988   if (! BLOCK_ABSTRACT (stmt))
15989     {
15990       dw_die_ref subr_die
15991         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
15992
15993       add_abstract_origin_attribute (subr_die, decl);
15994       if (TREE_ASM_WRITTEN (stmt))
15995         add_high_low_attributes (stmt, subr_die);
15996       add_call_src_coords_attributes (stmt, subr_die);
15997
15998       decls_for_scope (stmt, subr_die, depth);
15999       current_function_has_inlines = 1;
16000     }
16001 }
16002
16003 /* Generate a DIE for a field in a record, or structure.  */
16004
16005 static void
16006 gen_field_die (tree decl, dw_die_ref context_die)
16007 {
16008   dw_die_ref decl_die;
16009
16010   if (TREE_TYPE (decl) == error_mark_node)
16011     return;
16012
16013   decl_die = new_die (DW_TAG_member, context_die, decl);
16014   add_name_and_src_coords_attributes (decl_die, decl);
16015   add_type_attribute (decl_die, member_declared_type (decl),
16016                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
16017                       context_die);
16018
16019   if (DECL_BIT_FIELD_TYPE (decl))
16020     {
16021       add_byte_size_attribute (decl_die, decl);
16022       add_bit_size_attribute (decl_die, decl);
16023       add_bit_offset_attribute (decl_die, decl);
16024     }
16025
16026   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
16027     add_data_member_location_attribute (decl_die, decl);
16028
16029   if (DECL_ARTIFICIAL (decl))
16030     add_AT_flag (decl_die, DW_AT_artificial, 1);
16031
16032   if (TREE_PROTECTED (decl))
16033     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
16034   else if (TREE_PRIVATE (decl))
16035     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
16036
16037   /* Equate decl number to die, so that we can look up this decl later on.  */
16038   equate_decl_number_to_die (decl, decl_die);
16039 }
16040
16041 #if 0
16042 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16043    Use modified_type_die instead.
16044    We keep this code here just in case these types of DIEs may be needed to
16045    represent certain things in other languages (e.g. Pascal) someday.  */
16046
16047 static void
16048 gen_pointer_type_die (tree type, dw_die_ref context_die)
16049 {
16050   dw_die_ref ptr_die
16051     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
16052
16053   equate_type_number_to_die (type, ptr_die);
16054   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16055   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16056 }
16057
16058 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16059    Use modified_type_die instead.
16060    We keep this code here just in case these types of DIEs may be needed to
16061    represent certain things in other languages (e.g. Pascal) someday.  */
16062
16063 static void
16064 gen_reference_type_die (tree type, dw_die_ref context_die)
16065 {
16066   dw_die_ref ref_die
16067     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
16068
16069   equate_type_number_to_die (type, ref_die);
16070   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
16071   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16072 }
16073 #endif
16074
16075 /* Generate a DIE for a pointer to a member type.  */
16076
16077 static void
16078 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
16079 {
16080   dw_die_ref ptr_die
16081     = new_die (DW_TAG_ptr_to_member_type,
16082                scope_die_for (type, context_die), type);
16083
16084   equate_type_number_to_die (type, ptr_die);
16085   add_AT_die_ref (ptr_die, DW_AT_containing_type,
16086                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
16087   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16088 }
16089
16090 /* Generate the DIE for the compilation unit.  */
16091
16092 static dw_die_ref
16093 gen_compile_unit_die (const char *filename)
16094 {
16095   dw_die_ref die;
16096   char producer[250];
16097   const char *language_string = lang_hooks.name;
16098   int language;
16099
16100   die = new_die (DW_TAG_compile_unit, NULL, NULL);
16101
16102   if (filename)
16103     {
16104       add_name_attribute (die, filename);
16105       /* Don't add cwd for <built-in>.  */
16106       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
16107         add_comp_dir_attribute (die);
16108     }
16109
16110   sprintf (producer, "%s %s", language_string, version_string);
16111
16112 #ifdef MIPS_DEBUGGING_INFO
16113   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
16114      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
16115      not appear in the producer string, the debugger reaches the conclusion
16116      that the object file is stripped and has no debugging information.
16117      To get the MIPS/SGI debugger to believe that there is debugging
16118      information in the object file, we add a -g to the producer string.  */
16119   if (debug_info_level > DINFO_LEVEL_TERSE)
16120     strcat (producer, " -g");
16121 #endif
16122
16123   add_AT_string (die, DW_AT_producer, producer);
16124
16125   if (strcmp (language_string, "GNU C++") == 0)
16126     language = DW_LANG_C_plus_plus;
16127   else if (strcmp (language_string, "GNU Ada") == 0)
16128     language = DW_LANG_Ada95;
16129   else if (strcmp (language_string, "GNU F77") == 0)
16130     language = DW_LANG_Fortran77;
16131   else if (strcmp (language_string, "GNU Fortran") == 0)
16132     language = DW_LANG_Fortran95;
16133   else if (strcmp (language_string, "GNU Pascal") == 0)
16134     language = DW_LANG_Pascal83;
16135   else if (strcmp (language_string, "GNU Java") == 0)
16136     language = DW_LANG_Java;
16137   else if (strcmp (language_string, "GNU Objective-C") == 0)
16138     language = DW_LANG_ObjC;
16139   else if (strcmp (language_string, "GNU Objective-C++") == 0)
16140     language = DW_LANG_ObjC_plus_plus;
16141   else
16142     language = DW_LANG_C89;
16143
16144   add_AT_unsigned (die, DW_AT_language, language);
16145   return die;
16146 }
16147
16148 /* Generate the DIE for a base class.  */
16149
16150 static void
16151 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
16152 {
16153   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
16154
16155   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
16156   add_data_member_location_attribute (die, binfo);
16157
16158   if (BINFO_VIRTUAL_P (binfo))
16159     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16160
16161   if (access == access_public_node)
16162     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16163   else if (access == access_protected_node)
16164     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16165 }
16166
16167 /* Generate a DIE for a class member.  */
16168
16169 static void
16170 gen_member_die (tree type, dw_die_ref context_die)
16171 {
16172   tree member;
16173   tree binfo = TYPE_BINFO (type);
16174   dw_die_ref child;
16175
16176   /* If this is not an incomplete type, output descriptions of each of its
16177      members. Note that as we output the DIEs necessary to represent the
16178      members of this record or union type, we will also be trying to output
16179      DIEs to represent the *types* of those members. However the `type'
16180      function (above) will specifically avoid generating type DIEs for member
16181      types *within* the list of member DIEs for this (containing) type except
16182      for those types (of members) which are explicitly marked as also being
16183      members of this (containing) type themselves.  The g++ front- end can
16184      force any given type to be treated as a member of some other (containing)
16185      type by setting the TYPE_CONTEXT of the given (member) type to point to
16186      the TREE node representing the appropriate (containing) type.  */
16187
16188   /* First output info about the base classes.  */
16189   if (binfo)
16190     {
16191       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
16192       int i;
16193       tree base;
16194
16195       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
16196         gen_inheritance_die (base,
16197                              (accesses ? VEC_index (tree, accesses, i)
16198                               : access_public_node), context_die);
16199     }
16200
16201   /* Now output info about the data members and type members.  */
16202   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
16203     {
16204       /* If we thought we were generating minimal debug info for TYPE
16205          and then changed our minds, some of the member declarations
16206          may have already been defined.  Don't define them again, but
16207          do put them in the right order.  */
16208
16209       child = lookup_decl_die (member);
16210       if (child)
16211         splice_child_die (context_die, child);
16212       else
16213         gen_decl_die (member, NULL, context_die);
16214     }
16215
16216   /* Now output info about the function members (if any).  */
16217   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
16218     {
16219       /* Don't include clones in the member list.  */
16220       if (DECL_ABSTRACT_ORIGIN (member))
16221         continue;
16222
16223       child = lookup_decl_die (member);
16224       if (child)
16225         splice_child_die (context_die, child);
16226       else
16227         gen_decl_die (member, NULL, context_die);
16228     }
16229 }
16230
16231 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
16232    is set, we pretend that the type was never defined, so we only get the
16233    member DIEs needed by later specification DIEs.  */
16234
16235 static void
16236 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
16237                                 enum debug_info_usage usage)
16238 {
16239   dw_die_ref type_die = lookup_type_die (type);
16240   dw_die_ref scope_die = 0;
16241   int nested = 0;
16242   int complete = (TYPE_SIZE (type)
16243                   && (! TYPE_STUB_DECL (type)
16244                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
16245   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
16246   complete = complete && should_emit_struct_debug (type, usage);
16247
16248   if (type_die && ! complete)
16249     return;
16250
16251   if (TYPE_CONTEXT (type) != NULL_TREE
16252       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
16253           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
16254     nested = 1;
16255
16256   scope_die = scope_die_for (type, context_die);
16257
16258   if (! type_die || (nested && scope_die == comp_unit_die))
16259     /* First occurrence of type or toplevel definition of nested class.  */
16260     {
16261       dw_die_ref old_die = type_die;
16262
16263       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
16264                           ? record_type_tag (type) : DW_TAG_union_type,
16265                           scope_die, type);
16266       equate_type_number_to_die (type, type_die);
16267       if (old_die)
16268         add_AT_specification (type_die, old_die);
16269       else
16270         add_name_attribute (type_die, type_tag (type));
16271     }
16272   else
16273     remove_AT (type_die, DW_AT_declaration);
16274
16275   /* Generate child dies for template paramaters.  */
16276   if (debug_info_level > DINFO_LEVEL_TERSE
16277       && COMPLETE_TYPE_P (type))
16278     gen_generic_params_dies (type);
16279
16280   /* If this type has been completed, then give it a byte_size attribute and
16281      then give a list of members.  */
16282   if (complete && !ns_decl)
16283     {
16284       /* Prevent infinite recursion in cases where the type of some member of
16285          this type is expressed in terms of this type itself.  */
16286       TREE_ASM_WRITTEN (type) = 1;
16287       add_byte_size_attribute (type_die, type);
16288       if (TYPE_STUB_DECL (type) != NULL_TREE)
16289         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16290
16291       /* If the first reference to this type was as the return type of an
16292          inline function, then it may not have a parent.  Fix this now.  */
16293       if (type_die->die_parent == NULL)
16294         add_child_die (scope_die, type_die);
16295
16296       push_decl_scope (type);
16297       gen_member_die (type, type_die);
16298       pop_decl_scope ();
16299
16300       /* GNU extension: Record what type our vtable lives in.  */
16301       if (TYPE_VFIELD (type))
16302         {
16303           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
16304
16305           gen_type_die (vtype, context_die);
16306           add_AT_die_ref (type_die, DW_AT_containing_type,
16307                           lookup_type_die (vtype));
16308         }
16309     }
16310   else
16311     {
16312       add_AT_flag (type_die, DW_AT_declaration, 1);
16313
16314       /* We don't need to do this for function-local types.  */
16315       if (TYPE_STUB_DECL (type)
16316           && ! decl_function_context (TYPE_STUB_DECL (type)))
16317         VEC_safe_push (tree, gc, incomplete_types, type);
16318     }
16319
16320   if (get_AT (type_die, DW_AT_name))
16321     add_pubtype (type, type_die);
16322 }
16323
16324 /* Generate a DIE for a subroutine _type_.  */
16325
16326 static void
16327 gen_subroutine_type_die (tree type, dw_die_ref context_die)
16328 {
16329   tree return_type = TREE_TYPE (type);
16330   dw_die_ref subr_die
16331     = new_die (DW_TAG_subroutine_type,
16332                scope_die_for (type, context_die), type);
16333
16334   equate_type_number_to_die (type, subr_die);
16335   add_prototyped_attribute (subr_die, type);
16336   add_type_attribute (subr_die, return_type, 0, 0, context_die);
16337   gen_formal_types_die (type, subr_die);
16338
16339   if (get_AT (subr_die, DW_AT_name))
16340     add_pubtype (type, subr_die);
16341 }
16342
16343 /* Generate a DIE for a type definition.  */
16344
16345 static void
16346 gen_typedef_die (tree decl, dw_die_ref context_die)
16347 {
16348   dw_die_ref type_die;
16349   tree origin;
16350
16351   if (TREE_ASM_WRITTEN (decl))
16352     return;
16353
16354   TREE_ASM_WRITTEN (decl) = 1;
16355   type_die = new_die (DW_TAG_typedef, context_die, decl);
16356   origin = decl_ultimate_origin (decl);
16357   if (origin != NULL)
16358     add_abstract_origin_attribute (type_die, origin);
16359   else
16360     {
16361       tree type;
16362
16363       add_name_and_src_coords_attributes (type_die, decl);
16364       if (DECL_ORIGINAL_TYPE (decl))
16365         {
16366           type = DECL_ORIGINAL_TYPE (decl);
16367
16368           gcc_assert (type != TREE_TYPE (decl));
16369           equate_type_number_to_die (TREE_TYPE (decl), type_die);
16370         }
16371       else
16372         type = TREE_TYPE (decl);
16373
16374       add_type_attribute (type_die, type, TREE_READONLY (decl),
16375                           TREE_THIS_VOLATILE (decl), context_die);
16376     }
16377
16378   if (DECL_ABSTRACT (decl))
16379     equate_decl_number_to_die (decl, type_die);
16380
16381   if (get_AT (type_die, DW_AT_name))
16382     add_pubtype (decl, type_die);
16383 }
16384
16385 /* Generate a type description DIE.  */
16386
16387 static void
16388 gen_type_die_with_usage (tree type, dw_die_ref context_die,
16389                                 enum debug_info_usage usage)
16390 {
16391   int need_pop;
16392   struct array_descr_info info;
16393
16394   if (type == NULL_TREE || type == error_mark_node)
16395     return;
16396
16397   /* If TYPE is a typedef type variant, let's generate debug info
16398      for the parent typedef which TYPE is a type of.  */
16399   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16400       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
16401     {
16402       if (TREE_ASM_WRITTEN (type))
16403         return;
16404
16405       /* Prevent broken recursion; we can't hand off to the same type.  */
16406       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
16407
16408       /* Use the DIE of the containing namespace as the parent DIE of
16409          the type description DIE we want to generate.  */
16410       if (DECL_CONTEXT (TYPE_NAME (type))
16411           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
16412         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
16413
16414       TREE_ASM_WRITTEN (type) = 1;
16415       gen_decl_die (TYPE_NAME (type), NULL, context_die);
16416       return;
16417     }
16418
16419   /* If this is an array type with hidden descriptor, handle it first.  */
16420   if (!TREE_ASM_WRITTEN (type)
16421       && lang_hooks.types.get_array_descr_info
16422       && lang_hooks.types.get_array_descr_info (type, &info))
16423     {
16424       gen_descr_array_type_die (type, &info, context_die);
16425       TREE_ASM_WRITTEN (type) = 1;
16426       return;
16427     }
16428
16429   /* We are going to output a DIE to represent the unqualified version
16430      of this type (i.e. without any const or volatile qualifiers) so
16431      get the main variant (i.e. the unqualified version) of this type
16432      now.  (Vectors are special because the debugging info is in the
16433      cloned type itself).  */
16434   if (TREE_CODE (type) != VECTOR_TYPE)
16435     type = type_main_variant (type);
16436
16437   if (TREE_ASM_WRITTEN (type))
16438     return;
16439
16440   switch (TREE_CODE (type))
16441     {
16442     case ERROR_MARK:
16443       break;
16444
16445     case POINTER_TYPE:
16446     case REFERENCE_TYPE:
16447       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
16448          ensures that the gen_type_die recursion will terminate even if the
16449          type is recursive.  Recursive types are possible in Ada.  */
16450       /* ??? We could perhaps do this for all types before the switch
16451          statement.  */
16452       TREE_ASM_WRITTEN (type) = 1;
16453
16454       /* For these types, all that is required is that we output a DIE (or a
16455          set of DIEs) to represent the "basis" type.  */
16456       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16457                                 DINFO_USAGE_IND_USE);
16458       break;
16459
16460     case OFFSET_TYPE:
16461       /* This code is used for C++ pointer-to-data-member types.
16462          Output a description of the relevant class type.  */
16463       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
16464                                         DINFO_USAGE_IND_USE);
16465
16466       /* Output a description of the type of the object pointed to.  */
16467       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16468                                         DINFO_USAGE_IND_USE);
16469
16470       /* Now output a DIE to represent this pointer-to-data-member type
16471          itself.  */
16472       gen_ptr_to_mbr_type_die (type, context_die);
16473       break;
16474
16475     case FUNCTION_TYPE:
16476       /* Force out return type (in case it wasn't forced out already).  */
16477       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16478                                         DINFO_USAGE_DIR_USE);
16479       gen_subroutine_type_die (type, context_die);
16480       break;
16481
16482     case METHOD_TYPE:
16483       /* Force out return type (in case it wasn't forced out already).  */
16484       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16485                                         DINFO_USAGE_DIR_USE);
16486       gen_subroutine_type_die (type, context_die);
16487       break;
16488
16489     case ARRAY_TYPE:
16490       gen_array_type_die (type, context_die);
16491       break;
16492
16493     case VECTOR_TYPE:
16494       gen_array_type_die (type, context_die);
16495       break;
16496
16497     case ENUMERAL_TYPE:
16498     case RECORD_TYPE:
16499     case UNION_TYPE:
16500     case QUAL_UNION_TYPE:
16501       /* If this is a nested type whose containing class hasn't been written
16502          out yet, writing it out will cover this one, too.  This does not apply
16503          to instantiations of member class templates; they need to be added to
16504          the containing class as they are generated.  FIXME: This hurts the
16505          idea of combining type decls from multiple TUs, since we can't predict
16506          what set of template instantiations we'll get.  */
16507       if (TYPE_CONTEXT (type)
16508           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
16509           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
16510         {
16511           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
16512
16513           if (TREE_ASM_WRITTEN (type))
16514             return;
16515
16516           /* If that failed, attach ourselves to the stub.  */
16517           push_decl_scope (TYPE_CONTEXT (type));
16518           context_die = lookup_type_die (TYPE_CONTEXT (type));
16519           need_pop = 1;
16520         }
16521       else if (TYPE_CONTEXT (type) != NULL_TREE
16522                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
16523         {
16524           /* If this type is local to a function that hasn't been written
16525              out yet, use a NULL context for now; it will be fixed up in
16526              decls_for_scope.  */
16527           context_die = lookup_decl_die (TYPE_CONTEXT (type));
16528           need_pop = 0;
16529         }
16530       else
16531         {
16532           context_die = declare_in_namespace (type, context_die);
16533           need_pop = 0;
16534         }
16535
16536       if (TREE_CODE (type) == ENUMERAL_TYPE)
16537         {
16538           /* This might have been written out by the call to
16539              declare_in_namespace.  */
16540           if (!TREE_ASM_WRITTEN (type))
16541             gen_enumeration_type_die (type, context_die);
16542         }
16543       else
16544         gen_struct_or_union_type_die (type, context_die, usage);
16545
16546       if (need_pop)
16547         pop_decl_scope ();
16548
16549       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
16550          it up if it is ever completed.  gen_*_type_die will set it for us
16551          when appropriate.  */
16552       return;
16553
16554     case VOID_TYPE:
16555     case INTEGER_TYPE:
16556     case REAL_TYPE:
16557     case FIXED_POINT_TYPE:
16558     case COMPLEX_TYPE:
16559     case BOOLEAN_TYPE:
16560       /* No DIEs needed for fundamental types.  */
16561       break;
16562
16563     case LANG_TYPE:
16564       /* No Dwarf representation currently defined.  */
16565       break;
16566
16567     default:
16568       gcc_unreachable ();
16569     }
16570
16571   TREE_ASM_WRITTEN (type) = 1;
16572 }
16573
16574 static void
16575 gen_type_die (tree type, dw_die_ref context_die)
16576 {
16577   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
16578 }
16579
16580 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
16581    things which are local to the given block.  */
16582
16583 static void
16584 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
16585 {
16586   int must_output_die = 0;
16587   bool inlined_func;
16588
16589   /* Ignore blocks that are NULL.  */
16590   if (stmt == NULL_TREE)
16591     return;
16592
16593   inlined_func = inlined_function_outer_scope_p (stmt);
16594
16595   /* If the block is one fragment of a non-contiguous block, do not
16596      process the variables, since they will have been done by the
16597      origin block.  Do process subblocks.  */
16598   if (BLOCK_FRAGMENT_ORIGIN (stmt))
16599     {
16600       tree sub;
16601
16602       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
16603         gen_block_die (sub, context_die, depth + 1);
16604
16605       return;
16606     }
16607
16608   /* Determine if we need to output any Dwarf DIEs at all to represent this
16609      block.  */
16610   if (inlined_func)
16611     /* The outer scopes for inlinings *must* always be represented.  We
16612        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
16613     must_output_die = 1;
16614   else
16615     {
16616       /* Determine if this block directly contains any "significant"
16617          local declarations which we will need to output DIEs for.  */
16618       if (debug_info_level > DINFO_LEVEL_TERSE)
16619         /* We are not in terse mode so *any* local declaration counts
16620            as being a "significant" one.  */
16621         must_output_die = ((BLOCK_VARS (stmt) != NULL
16622                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
16623                            && (TREE_USED (stmt)
16624                                || TREE_ASM_WRITTEN (stmt)
16625                                || BLOCK_ABSTRACT (stmt)));
16626       else if ((TREE_USED (stmt)
16627                 || TREE_ASM_WRITTEN (stmt)
16628                 || BLOCK_ABSTRACT (stmt))
16629                && !dwarf2out_ignore_block (stmt))
16630         must_output_die = 1;
16631     }
16632
16633   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
16634      DIE for any block which contains no significant local declarations at
16635      all.  Rather, in such cases we just call `decls_for_scope' so that any
16636      needed Dwarf info for any sub-blocks will get properly generated. Note
16637      that in terse mode, our definition of what constitutes a "significant"
16638      local declaration gets restricted to include only inlined function
16639      instances and local (nested) function definitions.  */
16640   if (must_output_die)
16641     {
16642       if (inlined_func)
16643         {
16644           /* If STMT block is abstract, that means we have been called
16645              indirectly from dwarf2out_abstract_function.
16646              That function rightfully marks the descendent blocks (of
16647              the abstract function it is dealing with) as being abstract,
16648              precisely to prevent us from emitting any
16649              DW_TAG_inlined_subroutine DIE as a descendent
16650              of an abstract function instance. So in that case, we should
16651              not call gen_inlined_subroutine_die.
16652
16653              Later though, when cgraph asks dwarf2out to emit info
16654              for the concrete instance of the function decl into which
16655              the concrete instance of STMT got inlined, the later will lead
16656              to the generation of a DW_TAG_inlined_subroutine DIE.  */
16657           if (! BLOCK_ABSTRACT (stmt))
16658             gen_inlined_subroutine_die (stmt, context_die, depth);
16659         }
16660       else
16661         gen_lexical_block_die (stmt, context_die, depth);
16662     }
16663   else
16664     decls_for_scope (stmt, context_die, depth);
16665 }
16666
16667 /* Process variable DECL (or variable with origin ORIGIN) within
16668    block STMT and add it to CONTEXT_DIE.  */
16669 static void
16670 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
16671 {
16672   dw_die_ref die;
16673   tree decl_or_origin = decl ? decl : origin;
16674   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
16675
16676   if (ultimate_origin)
16677     origin = ultimate_origin;
16678
16679   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
16680     die = lookup_decl_die (decl_or_origin);
16681   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
16682            && TYPE_DECL_IS_STUB (decl_or_origin))
16683     die = lookup_type_die (TREE_TYPE (decl_or_origin));
16684   else
16685     die = NULL;
16686
16687   if (die != NULL && die->die_parent == NULL)
16688     add_child_die (context_die, die);
16689   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
16690     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
16691                                          stmt, context_die);
16692   else
16693     gen_decl_die (decl, origin, context_die);
16694 }
16695
16696 /* Generate all of the decls declared within a given scope and (recursively)
16697    all of its sub-blocks.  */
16698
16699 static void
16700 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
16701 {
16702   tree decl;
16703   unsigned int i;
16704   tree subblocks;
16705
16706   /* Ignore NULL blocks.  */
16707   if (stmt == NULL_TREE)
16708     return;
16709
16710   /* Output the DIEs to represent all of the data objects and typedefs
16711      declared directly within this block but not within any nested
16712      sub-blocks.  Also, nested function and tag DIEs have been
16713      generated with a parent of NULL; fix that up now.  */
16714   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
16715     process_scope_var (stmt, decl, NULL_TREE, context_die);
16716   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
16717     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
16718                        context_die);
16719
16720   /* If we're at -g1, we're not interested in subblocks.  */
16721   if (debug_info_level <= DINFO_LEVEL_TERSE)
16722     return;
16723
16724   /* Output the DIEs to represent all sub-blocks (and the items declared
16725      therein) of this block.  */
16726   for (subblocks = BLOCK_SUBBLOCKS (stmt);
16727        subblocks != NULL;
16728        subblocks = BLOCK_CHAIN (subblocks))
16729     gen_block_die (subblocks, context_die, depth + 1);
16730 }
16731
16732 /* Is this a typedef we can avoid emitting?  */
16733
16734 static inline int
16735 is_redundant_typedef (const_tree decl)
16736 {
16737   if (TYPE_DECL_IS_STUB (decl))
16738     return 1;
16739
16740   if (DECL_ARTIFICIAL (decl)
16741       && DECL_CONTEXT (decl)
16742       && is_tagged_type (DECL_CONTEXT (decl))
16743       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
16744       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
16745     /* Also ignore the artificial member typedef for the class name.  */
16746     return 1;
16747
16748   return 0;
16749 }
16750
16751 /* Returns the DIE for a context.  */
16752
16753 static inline dw_die_ref
16754 get_context_die (tree context)
16755 {
16756   if (context)
16757     {
16758       /* Find die that represents this context.  */
16759       if (TYPE_P (context))
16760         return force_type_die (context);
16761       else
16762         return force_decl_die (context);
16763     }
16764   return comp_unit_die;
16765 }
16766
16767 /* Returns the DIE for decl.  A DIE will always be returned.  */
16768
16769 static dw_die_ref
16770 force_decl_die (tree decl)
16771 {
16772   dw_die_ref decl_die;
16773   unsigned saved_external_flag;
16774   tree save_fn = NULL_TREE;
16775   decl_die = lookup_decl_die (decl);
16776   if (!decl_die)
16777     {
16778       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
16779
16780       decl_die = lookup_decl_die (decl);
16781       if (decl_die)
16782         return decl_die;
16783
16784       switch (TREE_CODE (decl))
16785         {
16786         case FUNCTION_DECL:
16787           /* Clear current_function_decl, so that gen_subprogram_die thinks
16788              that this is a declaration. At this point, we just want to force
16789              declaration die.  */
16790           save_fn = current_function_decl;
16791           current_function_decl = NULL_TREE;
16792           gen_subprogram_die (decl, context_die);
16793           current_function_decl = save_fn;
16794           break;
16795
16796         case VAR_DECL:
16797           /* Set external flag to force declaration die. Restore it after
16798            gen_decl_die() call.  */
16799           saved_external_flag = DECL_EXTERNAL (decl);
16800           DECL_EXTERNAL (decl) = 1;
16801           gen_decl_die (decl, NULL, context_die);
16802           DECL_EXTERNAL (decl) = saved_external_flag;
16803           break;
16804
16805         case NAMESPACE_DECL:
16806           dwarf2out_decl (decl);
16807           break;
16808
16809         default:
16810           gcc_unreachable ();
16811         }
16812
16813       /* We should be able to find the DIE now.  */
16814       if (!decl_die)
16815         decl_die = lookup_decl_die (decl);
16816       gcc_assert (decl_die);
16817     }
16818
16819   return decl_die;
16820 }
16821
16822 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
16823    always returned.  */
16824
16825 static dw_die_ref
16826 force_type_die (tree type)
16827 {
16828   dw_die_ref type_die;
16829
16830   type_die = lookup_type_die (type);
16831   if (!type_die)
16832     {
16833       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
16834
16835       type_die = modified_type_die (type, TYPE_READONLY (type),
16836                                     TYPE_VOLATILE (type), context_die);
16837       gcc_assert (type_die);
16838     }
16839   return type_die;
16840 }
16841
16842 /* Force out any required namespaces to be able to output DECL,
16843    and return the new context_die for it, if it's changed.  */
16844
16845 static dw_die_ref
16846 setup_namespace_context (tree thing, dw_die_ref context_die)
16847 {
16848   tree context = (DECL_P (thing)
16849                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
16850   if (context && TREE_CODE (context) == NAMESPACE_DECL)
16851     /* Force out the namespace.  */
16852     context_die = force_decl_die (context);
16853
16854   return context_die;
16855 }
16856
16857 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
16858    type) within its namespace, if appropriate.
16859
16860    For compatibility with older debuggers, namespace DIEs only contain
16861    declarations; all definitions are emitted at CU scope.  */
16862
16863 static dw_die_ref
16864 declare_in_namespace (tree thing, dw_die_ref context_die)
16865 {
16866   dw_die_ref ns_context;
16867
16868   if (debug_info_level <= DINFO_LEVEL_TERSE)
16869     return context_die;
16870
16871   /* If this decl is from an inlined function, then don't try to emit it in its
16872      namespace, as we will get confused.  It would have already been emitted
16873      when the abstract instance of the inline function was emitted anyways.  */
16874   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
16875     return context_die;
16876
16877   ns_context = setup_namespace_context (thing, context_die);
16878
16879   if (ns_context != context_die)
16880     {
16881       if (is_fortran ())
16882         return ns_context;
16883       if (DECL_P (thing))
16884         gen_decl_die (thing, NULL, ns_context);
16885       else
16886         gen_type_die (thing, ns_context);
16887     }
16888   return context_die;
16889 }
16890
16891 /* Generate a DIE for a namespace or namespace alias.  */
16892
16893 static void
16894 gen_namespace_die (tree decl, dw_die_ref context_die)
16895 {
16896   dw_die_ref namespace_die;
16897
16898   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
16899      they are an alias of.  */
16900   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
16901     {
16902       /* Output a real namespace or module.  */
16903       context_die = setup_namespace_context (decl, comp_unit_die);
16904       namespace_die = new_die (is_fortran ()
16905                                ? DW_TAG_module : DW_TAG_namespace,
16906                                context_die, decl);
16907       /* For Fortran modules defined in different CU don't add src coords.  */
16908       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
16909         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
16910       else
16911         add_name_and_src_coords_attributes (namespace_die, decl);
16912       if (DECL_EXTERNAL (decl))
16913         add_AT_flag (namespace_die, DW_AT_declaration, 1);
16914       equate_decl_number_to_die (decl, namespace_die);
16915     }
16916   else
16917     {
16918       /* Output a namespace alias.  */
16919
16920       /* Force out the namespace we are an alias of, if necessary.  */
16921       dw_die_ref origin_die
16922         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
16923
16924       if (DECL_CONTEXT (decl) == NULL_TREE
16925           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
16926         context_die = setup_namespace_context (decl, comp_unit_die);
16927       /* Now create the namespace alias DIE.  */
16928       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
16929       add_name_and_src_coords_attributes (namespace_die, decl);
16930       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
16931       equate_decl_number_to_die (decl, namespace_die);
16932     }
16933 }
16934
16935 /* Generate Dwarf debug information for a decl described by DECL.  */
16936
16937 static void
16938 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
16939 {
16940   tree decl_or_origin = decl ? decl : origin;
16941   tree class_origin = NULL;
16942
16943   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
16944     return;
16945
16946   switch (TREE_CODE (decl_or_origin))
16947     {
16948     case ERROR_MARK:
16949       break;
16950
16951     case CONST_DECL:
16952       if (!is_fortran ())
16953         {
16954           /* The individual enumerators of an enum type get output when we output
16955              the Dwarf representation of the relevant enum type itself.  */
16956           break;
16957         }
16958
16959       /* Emit its type.  */
16960       gen_type_die (TREE_TYPE (decl), context_die);
16961
16962       /* And its containing namespace.  */
16963       context_die = declare_in_namespace (decl, context_die);
16964
16965       gen_const_die (decl, context_die);
16966       break;
16967
16968     case FUNCTION_DECL:
16969       /* Don't output any DIEs to represent mere function declarations,
16970          unless they are class members or explicit block externs.  */
16971       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
16972           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
16973           && (current_function_decl == NULL_TREE
16974               || DECL_ARTIFICIAL (decl_or_origin)))
16975         break;
16976
16977 #if 0
16978       /* FIXME */
16979       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
16980          on local redeclarations of global functions.  That seems broken.  */
16981       if (current_function_decl != decl)
16982         /* This is only a declaration.  */;
16983 #endif
16984
16985       /* If we're emitting a clone, emit info for the abstract instance.  */
16986       if (origin || DECL_ORIGIN (decl) != decl)
16987         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
16988
16989       /* If we're emitting an out-of-line copy of an inline function,
16990          emit info for the abstract instance and set up to refer to it.  */
16991       else if (cgraph_function_possibly_inlined_p (decl)
16992                && ! DECL_ABSTRACT (decl)
16993                && ! class_or_namespace_scope_p (context_die)
16994                /* dwarf2out_abstract_function won't emit a die if this is just
16995                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
16996                   that case, because that works only if we have a die.  */
16997                && DECL_INITIAL (decl) != NULL_TREE)
16998         {
16999           dwarf2out_abstract_function (decl);
17000           set_decl_origin_self (decl);
17001         }
17002
17003       /* Otherwise we're emitting the primary DIE for this decl.  */
17004       else if (debug_info_level > DINFO_LEVEL_TERSE)
17005         {
17006           /* Before we describe the FUNCTION_DECL itself, make sure that we
17007              have described its return type.  */
17008           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
17009
17010           /* And its virtual context.  */
17011           if (DECL_VINDEX (decl) != NULL_TREE)
17012             gen_type_die (DECL_CONTEXT (decl), context_die);
17013
17014           /* And its containing type.  */
17015           if (!origin)
17016             origin = decl_class_context (decl);
17017           if (origin != NULL_TREE)
17018             gen_type_die_for_member (origin, decl, context_die);
17019
17020           /* And its containing namespace.  */
17021           context_die = declare_in_namespace (decl, context_die);
17022         }
17023
17024       /* Now output a DIE to represent the function itself.  */
17025       if (decl)
17026         gen_subprogram_die (decl, context_die);
17027       break;
17028
17029     case TYPE_DECL:
17030       /* If we are in terse mode, don't generate any DIEs to represent any
17031          actual typedefs.  */
17032       if (debug_info_level <= DINFO_LEVEL_TERSE)
17033         break;
17034
17035       /* In the special case of a TYPE_DECL node representing the declaration
17036          of some type tag, if the given TYPE_DECL is marked as having been
17037          instantiated from some other (original) TYPE_DECL node (e.g. one which
17038          was generated within the original definition of an inline function) we
17039          used to generate a special (abbreviated) DW_TAG_structure_type,
17040          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
17041          should be actually referencing those DIEs, as variable DIEs with that
17042          type would be emitted already in the abstract origin, so it was always
17043          removed during unused type prunning.  Don't add anything in this
17044          case.  */
17045       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
17046         break;
17047
17048       if (is_redundant_typedef (decl))
17049         gen_type_die (TREE_TYPE (decl), context_die);
17050       else
17051         /* Output a DIE to represent the typedef itself.  */
17052         gen_typedef_die (decl, context_die);
17053       break;
17054
17055     case LABEL_DECL:
17056       if (debug_info_level >= DINFO_LEVEL_NORMAL)
17057         gen_label_die (decl, context_die);
17058       break;
17059
17060     case VAR_DECL:
17061     case RESULT_DECL:
17062       /* If we are in terse mode, don't generate any DIEs to represent any
17063          variable declarations or definitions.  */
17064       if (debug_info_level <= DINFO_LEVEL_TERSE)
17065         break;
17066
17067       /* Output any DIEs that are needed to specify the type of this data
17068          object.  */
17069       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
17070            || TREE_CODE (decl_or_origin) == VAR_DECL)
17071           && DECL_BY_REFERENCE (decl_or_origin))
17072         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17073       else
17074         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17075
17076       /* And its containing type.  */
17077       class_origin = decl_class_context (decl_or_origin);
17078       if (class_origin != NULL_TREE)
17079         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
17080
17081       /* And its containing namespace.  */
17082       context_die = declare_in_namespace (decl_or_origin, context_die);
17083
17084       /* Now output the DIE to represent the data object itself.  This gets
17085          complicated because of the possibility that the VAR_DECL really
17086          represents an inlined instance of a formal parameter for an inline
17087          function.  */
17088       if (!origin)
17089         origin = decl_ultimate_origin (decl);
17090       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
17091         gen_formal_parameter_die (decl, origin, context_die);
17092       else
17093         gen_variable_die (decl, origin, context_die);
17094       break;
17095
17096     case FIELD_DECL:
17097       /* Ignore the nameless fields that are used to skip bits but handle C++
17098          anonymous unions and structs.  */
17099       if (DECL_NAME (decl) != NULL_TREE
17100           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
17101           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
17102         {
17103           gen_type_die (member_declared_type (decl), context_die);
17104           gen_field_die (decl, context_die);
17105         }
17106       break;
17107
17108     case PARM_DECL:
17109       if (DECL_BY_REFERENCE (decl_or_origin))
17110         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17111       else
17112         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17113       gen_formal_parameter_die (decl, origin, context_die);
17114       break;
17115
17116     case NAMESPACE_DECL:
17117     case IMPORTED_DECL:
17118       gen_namespace_die (decl, context_die);
17119       break;
17120
17121     default:
17122       /* Probably some frontend-internal decl.  Assume we don't care.  */
17123       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
17124       break;
17125     }
17126 }
17127 \f
17128 /* Output debug information for global decl DECL.  Called from toplev.c after
17129    compilation proper has finished.  */
17130
17131 static void
17132 dwarf2out_global_decl (tree decl)
17133 {
17134   /* Output DWARF2 information for file-scope tentative data object
17135      declarations, file-scope (extern) function declarations (which
17136      had no corresponding body) and file-scope tagged type declarations
17137      and definitions which have not yet been forced out.  */
17138   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
17139     dwarf2out_decl (decl);
17140 }
17141
17142 /* Output debug information for type decl DECL.  Called from toplev.c
17143    and from language front ends (to record built-in types).  */
17144 static void
17145 dwarf2out_type_decl (tree decl, int local)
17146 {
17147   if (!local)
17148     dwarf2out_decl (decl);
17149 }
17150
17151 /* Output debug information for imported module or decl DECL.
17152    NAME is non-NULL name in the lexical block if the decl has been renamed.
17153    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
17154    that DECL belongs to.
17155    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
17156 static void
17157 dwarf2out_imported_module_or_decl_1 (tree decl,
17158                                      tree name,
17159                                      tree lexical_block,
17160                                      dw_die_ref lexical_block_die)
17161 {
17162   expanded_location xloc;
17163   dw_die_ref imported_die = NULL;
17164   dw_die_ref at_import_die;
17165
17166   if (TREE_CODE (decl) == IMPORTED_DECL)
17167     {
17168       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
17169       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
17170       gcc_assert (decl);
17171     }
17172   else
17173     xloc = expand_location (input_location);
17174
17175   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
17176     {
17177       if (is_base_type (TREE_TYPE (decl)))
17178         at_import_die = base_type_die (TREE_TYPE (decl));
17179       else
17180         at_import_die = force_type_die (TREE_TYPE (decl));
17181       /* For namespace N { typedef void T; } using N::T; base_type_die
17182          returns NULL, but DW_TAG_imported_declaration requires
17183          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
17184       if (!at_import_die)
17185         {
17186           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
17187           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
17188           at_import_die = lookup_type_die (TREE_TYPE (decl));
17189           gcc_assert (at_import_die);
17190         }
17191     }
17192   else
17193     {
17194       at_import_die = lookup_decl_die (decl);
17195       if (!at_import_die)
17196         {
17197           /* If we're trying to avoid duplicate debug info, we may not have
17198              emitted the member decl for this field.  Emit it now.  */
17199           if (TREE_CODE (decl) == FIELD_DECL)
17200             {
17201               tree type = DECL_CONTEXT (decl);
17202
17203               if (TYPE_CONTEXT (type)
17204                   && TYPE_P (TYPE_CONTEXT (type))
17205                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
17206                                                 DINFO_USAGE_DIR_USE))
17207                 return;
17208               gen_type_die_for_member (type, decl,
17209                                        get_context_die (TYPE_CONTEXT (type)));
17210             }
17211           at_import_die = force_decl_die (decl);
17212         }
17213     }
17214
17215   if (TREE_CODE (decl) == NAMESPACE_DECL)
17216     imported_die = new_die (DW_TAG_imported_module,
17217                             lexical_block_die,
17218                             lexical_block);
17219   else
17220     imported_die = new_die (DW_TAG_imported_declaration,
17221                             lexical_block_die,
17222                             lexical_block);
17223
17224   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
17225   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
17226   if (name)
17227     add_AT_string (imported_die, DW_AT_name,
17228                    IDENTIFIER_POINTER (name));
17229   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
17230 }
17231
17232 /* Output debug information for imported module or decl DECL.
17233    NAME is non-NULL name in context if the decl has been renamed.
17234    CHILD is true if decl is one of the renamed decls as part of
17235    importing whole module.  */
17236
17237 static void
17238 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
17239                                    bool child)
17240 {
17241   /* dw_die_ref at_import_die;  */
17242   dw_die_ref scope_die;
17243
17244   if (debug_info_level <= DINFO_LEVEL_TERSE)
17245     return;
17246
17247   gcc_assert (decl);
17248
17249   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
17250      We need decl DIE for reference and scope die. First, get DIE for the decl
17251      itself.  */
17252
17253   /* Get the scope die for decl context. Use comp_unit_die for global module
17254      or decl. If die is not found for non globals, force new die.  */
17255   if (context
17256       && TYPE_P (context)
17257       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
17258     return;
17259   scope_die = get_context_die (context);
17260
17261   if (child)
17262     {
17263       gcc_assert (scope_die->die_child);
17264       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
17265       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
17266       scope_die = scope_die->die_child;
17267     }
17268
17269   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
17270   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
17271
17272 }
17273
17274 /* Write the debugging output for DECL.  */
17275
17276 void
17277 dwarf2out_decl (tree decl)
17278 {
17279   dw_die_ref context_die = comp_unit_die;
17280
17281   switch (TREE_CODE (decl))
17282     {
17283     case ERROR_MARK:
17284       return;
17285
17286     case FUNCTION_DECL:
17287       /* What we would really like to do here is to filter out all mere
17288          file-scope declarations of file-scope functions which are never
17289          referenced later within this translation unit (and keep all of ones
17290          that *are* referenced later on) but we aren't clairvoyant, so we have
17291          no idea which functions will be referenced in the future (i.e. later
17292          on within the current translation unit). So here we just ignore all
17293          file-scope function declarations which are not also definitions.  If
17294          and when the debugger needs to know something about these functions,
17295          it will have to hunt around and find the DWARF information associated
17296          with the definition of the function.
17297
17298          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
17299          nodes represent definitions and which ones represent mere
17300          declarations.  We have to check DECL_INITIAL instead. That's because
17301          the C front-end supports some weird semantics for "extern inline"
17302          function definitions.  These can get inlined within the current
17303          translation unit (and thus, we need to generate Dwarf info for their
17304          abstract instances so that the Dwarf info for the concrete inlined
17305          instances can have something to refer to) but the compiler never
17306          generates any out-of-lines instances of such things (despite the fact
17307          that they *are* definitions).
17308
17309          The important point is that the C front-end marks these "extern
17310          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
17311          them anyway. Note that the C++ front-end also plays some similar games
17312          for inline function definitions appearing within include files which
17313          also contain `#pragma interface' pragmas.  */
17314       if (DECL_INITIAL (decl) == NULL_TREE)
17315         return;
17316
17317       /* If we're a nested function, initially use a parent of NULL; if we're
17318          a plain function, this will be fixed up in decls_for_scope.  If
17319          we're a method, it will be ignored, since we already have a DIE.  */
17320       if (decl_function_context (decl)
17321           /* But if we're in terse mode, we don't care about scope.  */
17322           && debug_info_level > DINFO_LEVEL_TERSE)
17323         context_die = NULL;
17324       break;
17325
17326     case VAR_DECL:
17327       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
17328          declaration and if the declaration was never even referenced from
17329          within this entire compilation unit.  We suppress these DIEs in
17330          order to save space in the .debug section (by eliminating entries
17331          which are probably useless).  Note that we must not suppress
17332          block-local extern declarations (whether used or not) because that
17333          would screw-up the debugger's name lookup mechanism and cause it to
17334          miss things which really ought to be in scope at a given point.  */
17335       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
17336         return;
17337
17338       /* For local statics lookup proper context die.  */
17339       if (TREE_STATIC (decl) && decl_function_context (decl))
17340         context_die = lookup_decl_die (DECL_CONTEXT (decl));
17341
17342       /* If we are in terse mode, don't generate any DIEs to represent any
17343          variable declarations or definitions.  */
17344       if (debug_info_level <= DINFO_LEVEL_TERSE)
17345         return;
17346       break;
17347
17348     case CONST_DECL:
17349       if (debug_info_level <= DINFO_LEVEL_TERSE)
17350         return;
17351       if (!is_fortran ())
17352         return;
17353       if (TREE_STATIC (decl) && decl_function_context (decl))
17354         context_die = lookup_decl_die (DECL_CONTEXT (decl));
17355       break;
17356
17357     case NAMESPACE_DECL:
17358     case IMPORTED_DECL:
17359       if (debug_info_level <= DINFO_LEVEL_TERSE)
17360         return;
17361       if (lookup_decl_die (decl) != NULL)
17362         return;
17363       break;
17364
17365     case TYPE_DECL:
17366       /* Don't emit stubs for types unless they are needed by other DIEs.  */
17367       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
17368         return;
17369
17370       /* Don't bother trying to generate any DIEs to represent any of the
17371          normal built-in types for the language we are compiling.  */
17372       if (DECL_IS_BUILTIN (decl))
17373         {
17374           /* OK, we need to generate one for `bool' so GDB knows what type
17375              comparisons have.  */
17376           if (is_cxx ()
17377               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
17378               && ! DECL_IGNORED_P (decl))
17379             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
17380
17381           return;
17382         }
17383
17384       /* If we are in terse mode, don't generate any DIEs for types.  */
17385       if (debug_info_level <= DINFO_LEVEL_TERSE)
17386         return;
17387
17388       /* If we're a function-scope tag, initially use a parent of NULL;
17389          this will be fixed up in decls_for_scope.  */
17390       if (decl_function_context (decl))
17391         context_die = NULL;
17392
17393       break;
17394
17395     default:
17396       return;
17397     }
17398
17399   gen_decl_die (decl, NULL, context_die);
17400 }
17401
17402 /* Output a marker (i.e. a label) for the beginning of the generated code for
17403    a lexical block.  */
17404
17405 static void
17406 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
17407                        unsigned int blocknum)
17408 {
17409   switch_to_section (current_function_section ());
17410   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
17411 }
17412
17413 /* Output a marker (i.e. a label) for the end of the generated code for a
17414    lexical block.  */
17415
17416 static void
17417 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
17418 {
17419   switch_to_section (current_function_section ());
17420   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
17421 }
17422
17423 /* Returns nonzero if it is appropriate not to emit any debugging
17424    information for BLOCK, because it doesn't contain any instructions.
17425
17426    Don't allow this for blocks with nested functions or local classes
17427    as we would end up with orphans, and in the presence of scheduling
17428    we may end up calling them anyway.  */
17429
17430 static bool
17431 dwarf2out_ignore_block (const_tree block)
17432 {
17433   tree decl;
17434   unsigned int i;
17435
17436   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
17437     if (TREE_CODE (decl) == FUNCTION_DECL
17438         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
17439       return 0;
17440   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
17441     {
17442       decl = BLOCK_NONLOCALIZED_VAR (block, i);
17443       if (TREE_CODE (decl) == FUNCTION_DECL
17444           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
17445       return 0;
17446     }
17447
17448   return 1;
17449 }
17450
17451 /* Hash table routines for file_hash.  */
17452
17453 static int
17454 file_table_eq (const void *p1_p, const void *p2_p)
17455 {
17456   const struct dwarf_file_data *const p1 =
17457     (const struct dwarf_file_data *) p1_p;
17458   const char *const p2 = (const char *) p2_p;
17459   return strcmp (p1->filename, p2) == 0;
17460 }
17461
17462 static hashval_t
17463 file_table_hash (const void *p_p)
17464 {
17465   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
17466   return htab_hash_string (p->filename);
17467 }
17468
17469 /* Lookup FILE_NAME (in the list of filenames that we know about here in
17470    dwarf2out.c) and return its "index".  The index of each (known) filename is
17471    just a unique number which is associated with only that one filename.  We
17472    need such numbers for the sake of generating labels (in the .debug_sfnames
17473    section) and references to those files numbers (in the .debug_srcinfo
17474    and.debug_macinfo sections).  If the filename given as an argument is not
17475    found in our current list, add it to the list and assign it the next
17476    available unique index number.  In order to speed up searches, we remember
17477    the index of the filename was looked up last.  This handles the majority of
17478    all searches.  */
17479
17480 static struct dwarf_file_data *
17481 lookup_filename (const char *file_name)
17482 {
17483   void ** slot;
17484   struct dwarf_file_data * created;
17485
17486   /* Check to see if the file name that was searched on the previous
17487      call matches this file name.  If so, return the index.  */
17488   if (file_table_last_lookup
17489       && (file_name == file_table_last_lookup->filename
17490           || strcmp (file_table_last_lookup->filename, file_name) == 0))
17491     return file_table_last_lookup;
17492
17493   /* Didn't match the previous lookup, search the table.  */
17494   slot = htab_find_slot_with_hash (file_table, file_name,
17495                                    htab_hash_string (file_name), INSERT);
17496   if (*slot)
17497     return (struct dwarf_file_data *) *slot;
17498
17499   created = GGC_NEW (struct dwarf_file_data);
17500   created->filename = file_name;
17501   created->emitted_number = 0;
17502   *slot = created;
17503   return created;
17504 }
17505
17506 /* If the assembler will construct the file table, then translate the compiler
17507    internal file table number into the assembler file table number, and emit
17508    a .file directive if we haven't already emitted one yet.  The file table
17509    numbers are different because we prune debug info for unused variables and
17510    types, which may include filenames.  */
17511
17512 static int
17513 maybe_emit_file (struct dwarf_file_data * fd)
17514 {
17515   if (! fd->emitted_number)
17516     {
17517       if (last_emitted_file)
17518         fd->emitted_number = last_emitted_file->emitted_number + 1;
17519       else
17520         fd->emitted_number = 1;
17521       last_emitted_file = fd;
17522
17523       if (DWARF2_ASM_LINE_DEBUG_INFO)
17524         {
17525           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
17526           output_quoted_string (asm_out_file,
17527                                 remap_debug_filename (fd->filename));
17528           fputc ('\n', asm_out_file);
17529         }
17530     }
17531
17532   return fd->emitted_number;
17533 }
17534
17535 /* Schedule generation of a DW_AT_const_value attribute to DIE.
17536    That generation should happen after function debug info has been
17537    generated. The value of the attribute is the constant value of ARG.  */
17538
17539 static void
17540 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
17541 {
17542   die_arg_entry entry;
17543
17544   if (!die || !arg)
17545     return;
17546
17547   if (!tmpl_value_parm_die_table)
17548     tmpl_value_parm_die_table
17549       = VEC_alloc (die_arg_entry, gc, 32);
17550
17551   entry.die = die;
17552   entry.arg = arg;
17553   VEC_safe_push (die_arg_entry, gc,
17554                  tmpl_value_parm_die_table,
17555                  &entry);
17556 }
17557
17558 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
17559    by append_entry_to_tmpl_value_parm_die_table. This function must
17560    be called after function DIEs have been generated.  */
17561
17562 static void
17563 gen_remaining_tmpl_value_param_die_attribute (void)
17564 {
17565   if (tmpl_value_parm_die_table)
17566     {
17567       unsigned i;
17568       die_arg_entry *e;
17569
17570       for (i = 0;
17571            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
17572            i++)
17573         tree_add_const_value_attribute (e->die, e->arg);
17574     }
17575 }
17576
17577
17578 /* Replace DW_AT_name for the decl with name.  */
17579  
17580 static void
17581 dwarf2out_set_name (tree decl, tree name)
17582 {
17583   dw_die_ref die;
17584   dw_attr_ref attr;
17585
17586   die = TYPE_SYMTAB_DIE (decl);
17587   if (!die)
17588     return;
17589
17590   attr = get_AT (die, DW_AT_name);
17591   if (attr)
17592     {
17593       struct indirect_string_node *node;
17594
17595       node = find_AT_string (dwarf2_name (name, 0));
17596       /* replace the string.  */
17597       attr->dw_attr_val.v.val_str = node;
17598     }
17599
17600   else
17601     add_name_attribute (die, dwarf2_name (name, 0));
17602 }
17603
17604 /* Called by the final INSN scan whenever we see a var location.  We
17605    use it to drop labels in the right places, and throw the location in
17606    our lookup table.  */
17607
17608 static void
17609 dwarf2out_var_location (rtx loc_note)
17610 {
17611   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
17612   struct var_loc_node *newloc;
17613   rtx next_real;
17614   static const char *last_label;
17615   static const char *last_postcall_label;
17616   static bool last_in_cold_section_p;
17617   tree decl;
17618
17619   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
17620     return;
17621
17622   next_real = next_real_insn (loc_note);
17623   /* If there are no instructions which would be affected by this note,
17624      don't do anything.  */
17625   if (next_real == NULL_RTX)
17626     return;
17627
17628   newloc = GGC_CNEW (struct var_loc_node);
17629   /* If there were no real insns between note we processed last time
17630      and this note, use the label we emitted last time.  */
17631   if (last_var_location_insn == NULL_RTX
17632       || last_var_location_insn != next_real
17633       || last_in_cold_section_p != in_cold_section_p)
17634     {
17635       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
17636       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
17637       loclabel_num++;
17638       last_label = ggc_strdup (loclabel);
17639       if (!NOTE_DURING_CALL_P (loc_note))
17640         last_postcall_label = NULL;
17641     }
17642   newloc->var_loc_note = loc_note;
17643   newloc->next = NULL;
17644
17645   if (!NOTE_DURING_CALL_P (loc_note))
17646     newloc->label = last_label;
17647   else
17648     {
17649       if (!last_postcall_label)
17650         {
17651           sprintf (loclabel, "%s-1", last_label);
17652           last_postcall_label = ggc_strdup (loclabel);
17653         }
17654       newloc->label = last_postcall_label;
17655     }
17656
17657   if (cfun && in_cold_section_p)
17658     newloc->section_label = crtl->subsections.cold_section_label;
17659   else
17660     newloc->section_label = text_section_label;
17661
17662   last_var_location_insn = next_real;
17663   last_in_cold_section_p = in_cold_section_p;
17664   decl = NOTE_VAR_LOCATION_DECL (loc_note);
17665   add_var_loc_to_decl (decl, newloc);
17666 }
17667
17668 /* We need to reset the locations at the beginning of each
17669    function. We can't do this in the end_function hook, because the
17670    declarations that use the locations won't have been output when
17671    that hook is called.  Also compute have_multiple_function_sections here.  */
17672
17673 static void
17674 dwarf2out_begin_function (tree fun)
17675 {
17676   htab_empty (decl_loc_table);
17677
17678   if (function_section (fun) != text_section)
17679     have_multiple_function_sections = true;
17680
17681   dwarf2out_note_section_used ();
17682 }
17683
17684 /* Output a label to mark the beginning of a source code line entry
17685    and record information relating to this source line, in
17686    'line_info_table' for later output of the .debug_line section.  */
17687
17688 static void
17689 dwarf2out_source_line (unsigned int line, const char *filename,
17690                        int discriminator, bool is_stmt)
17691 {
17692   static bool last_is_stmt = true;
17693
17694   if (debug_info_level >= DINFO_LEVEL_NORMAL
17695       && line != 0)
17696     {
17697       int file_num = maybe_emit_file (lookup_filename (filename));
17698
17699       switch_to_section (current_function_section ());
17700
17701       /* If requested, emit something human-readable.  */
17702       if (flag_debug_asm)
17703         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
17704                  filename, line);
17705
17706       if (DWARF2_ASM_LINE_DEBUG_INFO)
17707         {
17708           /* Emit the .loc directive understood by GNU as.  */
17709           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
17710           if (is_stmt != last_is_stmt)
17711             {
17712               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
17713               last_is_stmt = is_stmt;
17714             }
17715           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
17716             fprintf (asm_out_file, " discriminator %d", discriminator);
17717           fputc ('\n', asm_out_file);
17718
17719           /* Indicate that line number info exists.  */
17720           line_info_table_in_use++;
17721         }
17722       else if (function_section (current_function_decl) != text_section)
17723         {
17724           dw_separate_line_info_ref line_info;
17725           targetm.asm_out.internal_label (asm_out_file,
17726                                           SEPARATE_LINE_CODE_LABEL,
17727                                           separate_line_info_table_in_use);
17728
17729           /* Expand the line info table if necessary.  */
17730           if (separate_line_info_table_in_use
17731               == separate_line_info_table_allocated)
17732             {
17733               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
17734               separate_line_info_table
17735                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
17736                                  separate_line_info_table,
17737                                  separate_line_info_table_allocated);
17738               memset (separate_line_info_table
17739                        + separate_line_info_table_in_use,
17740                       0,
17741                       (LINE_INFO_TABLE_INCREMENT
17742                        * sizeof (dw_separate_line_info_entry)));
17743             }
17744
17745           /* Add the new entry at the end of the line_info_table.  */
17746           line_info
17747             = &separate_line_info_table[separate_line_info_table_in_use++];
17748           line_info->dw_file_num = file_num;
17749           line_info->dw_line_num = line;
17750           line_info->function = current_function_funcdef_no;
17751         }
17752       else
17753         {
17754           dw_line_info_ref line_info;
17755
17756           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
17757                                      line_info_table_in_use);
17758
17759           /* Expand the line info table if necessary.  */
17760           if (line_info_table_in_use == line_info_table_allocated)
17761             {
17762               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
17763               line_info_table
17764                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
17765                                  line_info_table_allocated);
17766               memset (line_info_table + line_info_table_in_use, 0,
17767                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
17768             }
17769
17770           /* Add the new entry at the end of the line_info_table.  */
17771           line_info = &line_info_table[line_info_table_in_use++];
17772           line_info->dw_file_num = file_num;
17773           line_info->dw_line_num = line;
17774         }
17775     }
17776 }
17777
17778 /* Record the beginning of a new source file.  */
17779
17780 static void
17781 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
17782 {
17783   if (flag_eliminate_dwarf2_dups)
17784     {
17785       /* Record the beginning of the file for break_out_includes.  */
17786       dw_die_ref bincl_die;
17787
17788       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
17789       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
17790     }
17791
17792   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17793     {
17794       int file_num = maybe_emit_file (lookup_filename (filename));
17795
17796       switch_to_section (debug_macinfo_section);
17797       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
17798       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
17799                                    lineno);
17800
17801       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
17802     }
17803 }
17804
17805 /* Record the end of a source file.  */
17806
17807 static void
17808 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
17809 {
17810   if (flag_eliminate_dwarf2_dups)
17811     /* Record the end of the file for break_out_includes.  */
17812     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
17813
17814   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17815     {
17816       switch_to_section (debug_macinfo_section);
17817       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
17818     }
17819 }
17820
17821 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
17822    the tail part of the directive line, i.e. the part which is past the
17823    initial whitespace, #, whitespace, directive-name, whitespace part.  */
17824
17825 static void
17826 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
17827                   const char *buffer ATTRIBUTE_UNUSED)
17828 {
17829   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17830     {
17831       switch_to_section (debug_macinfo_section);
17832       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
17833       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
17834       dw2_asm_output_nstring (buffer, -1, "The macro");
17835     }
17836 }
17837
17838 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
17839    the tail part of the directive line, i.e. the part which is past the
17840    initial whitespace, #, whitespace, directive-name, whitespace part.  */
17841
17842 static void
17843 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
17844                  const char *buffer ATTRIBUTE_UNUSED)
17845 {
17846   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17847     {
17848       switch_to_section (debug_macinfo_section);
17849       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
17850       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
17851       dw2_asm_output_nstring (buffer, -1, "The macro");
17852     }
17853 }
17854
17855 /* Set up for Dwarf output at the start of compilation.  */
17856
17857 static void
17858 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
17859 {
17860   /* Allocate the file_table.  */
17861   file_table = htab_create_ggc (50, file_table_hash,
17862                                 file_table_eq, NULL);
17863
17864   /* Allocate the decl_die_table.  */
17865   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
17866                                     decl_die_table_eq, NULL);
17867
17868   /* Allocate the decl_loc_table.  */
17869   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
17870                                     decl_loc_table_eq, NULL);
17871
17872   /* Allocate the initial hunk of the decl_scope_table.  */
17873   decl_scope_table = VEC_alloc (tree, gc, 256);
17874
17875   /* Allocate the initial hunk of the abbrev_die_table.  */
17876   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
17877   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
17878   /* Zero-th entry is allocated, but unused.  */
17879   abbrev_die_table_in_use = 1;
17880
17881   /* Allocate the initial hunk of the line_info_table.  */
17882   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
17883   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
17884
17885   /* Zero-th entry is allocated, but unused.  */
17886   line_info_table_in_use = 1;
17887
17888   /* Allocate the pubtypes and pubnames vectors.  */
17889   pubname_table = VEC_alloc (pubname_entry, gc, 32);
17890   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
17891
17892   /* Generate the initial DIE for the .debug section.  Note that the (string)
17893      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
17894      will (typically) be a relative pathname and that this pathname should be
17895      taken as being relative to the directory from which the compiler was
17896      invoked when the given (base) source file was compiled.  We will fill
17897      in this value in dwarf2out_finish.  */
17898   comp_unit_die = gen_compile_unit_die (NULL);
17899
17900   incomplete_types = VEC_alloc (tree, gc, 64);
17901
17902   used_rtx_array = VEC_alloc (rtx, gc, 32);
17903
17904   debug_info_section = get_section (DEBUG_INFO_SECTION,
17905                                     SECTION_DEBUG, NULL);
17906   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
17907                                       SECTION_DEBUG, NULL);
17908   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
17909                                        SECTION_DEBUG, NULL);
17910   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
17911                                        SECTION_DEBUG, NULL);
17912   debug_line_section = get_section (DEBUG_LINE_SECTION,
17913                                     SECTION_DEBUG, NULL);
17914   debug_loc_section = get_section (DEBUG_LOC_SECTION,
17915                                    SECTION_DEBUG, NULL);
17916   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
17917                                         SECTION_DEBUG, NULL);
17918   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
17919                                         SECTION_DEBUG, NULL);
17920   debug_str_section = get_section (DEBUG_STR_SECTION,
17921                                    DEBUG_STR_SECTION_FLAGS, NULL);
17922   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
17923                                       SECTION_DEBUG, NULL);
17924   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
17925                                      SECTION_DEBUG, NULL);
17926
17927   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
17928   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
17929                                DEBUG_ABBREV_SECTION_LABEL, 0);
17930   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
17931   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
17932                                COLD_TEXT_SECTION_LABEL, 0);
17933   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
17934
17935   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
17936                                DEBUG_INFO_SECTION_LABEL, 0);
17937   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
17938                                DEBUG_LINE_SECTION_LABEL, 0);
17939   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
17940                                DEBUG_RANGES_SECTION_LABEL, 0);
17941   switch_to_section (debug_abbrev_section);
17942   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
17943   switch_to_section (debug_info_section);
17944   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
17945   switch_to_section (debug_line_section);
17946   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
17947
17948   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17949     {
17950       switch_to_section (debug_macinfo_section);
17951       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
17952                                    DEBUG_MACINFO_SECTION_LABEL, 0);
17953       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
17954     }
17955
17956   switch_to_section (text_section);
17957   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
17958   if (flag_reorder_blocks_and_partition)
17959     {
17960       cold_text_section = unlikely_text_section ();
17961       switch_to_section (cold_text_section);
17962       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
17963     }
17964 }
17965
17966 /* A helper function for dwarf2out_finish called through
17967    htab_traverse.  Emit one queued .debug_str string.  */
17968
17969 static int
17970 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
17971 {
17972   struct indirect_string_node *node = (struct indirect_string_node *) *h;
17973
17974   if (node->label && node->refcount)
17975     {
17976       switch_to_section (debug_str_section);
17977       ASM_OUTPUT_LABEL (asm_out_file, node->label);
17978       assemble_string (node->str, strlen (node->str) + 1);
17979     }
17980
17981   return 1;
17982 }
17983
17984 #if ENABLE_ASSERT_CHECKING
17985 /* Verify that all marks are clear.  */
17986
17987 static void
17988 verify_marks_clear (dw_die_ref die)
17989 {
17990   dw_die_ref c;
17991
17992   gcc_assert (! die->die_mark);
17993   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
17994 }
17995 #endif /* ENABLE_ASSERT_CHECKING */
17996
17997 /* Clear the marks for a die and its children.
17998    Be cool if the mark isn't set.  */
17999
18000 static void
18001 prune_unmark_dies (dw_die_ref die)
18002 {
18003   dw_die_ref c;
18004
18005   if (die->die_mark)
18006     die->die_mark = 0;
18007   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
18008 }
18009
18010 /* Given DIE that we're marking as used, find any other dies
18011    it references as attributes and mark them as used.  */
18012
18013 static void
18014 prune_unused_types_walk_attribs (dw_die_ref die)
18015 {
18016   dw_attr_ref a;
18017   unsigned ix;
18018
18019   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18020     {
18021       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
18022         {
18023           /* A reference to another DIE.
18024              Make sure that it will get emitted.  */
18025           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
18026         }
18027       /* Set the string's refcount to 0 so that prune_unused_types_mark
18028          accounts properly for it.  */
18029       if (AT_class (a) == dw_val_class_str)
18030         a->dw_attr_val.v.val_str->refcount = 0;
18031     }
18032 }
18033
18034
18035 /* Mark DIE as being used.  If DOKIDS is true, then walk down
18036    to DIE's children.  */
18037
18038 static void
18039 prune_unused_types_mark (dw_die_ref die, int dokids)
18040 {
18041   dw_die_ref c;
18042
18043   if (die->die_mark == 0)
18044     {
18045       /* We haven't done this node yet.  Mark it as used.  */
18046       die->die_mark = 1;
18047
18048       /* We also have to mark its parents as used.
18049          (But we don't want to mark our parents' kids due to this.)  */
18050       if (die->die_parent)
18051         prune_unused_types_mark (die->die_parent, 0);
18052
18053       /* Mark any referenced nodes.  */
18054       prune_unused_types_walk_attribs (die);
18055
18056       /* If this node is a specification,
18057          also mark the definition, if it exists.  */
18058       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
18059         prune_unused_types_mark (die->die_definition, 1);
18060     }
18061
18062   if (dokids && die->die_mark != 2)
18063     {
18064       /* We need to walk the children, but haven't done so yet.
18065          Remember that we've walked the kids.  */
18066       die->die_mark = 2;
18067
18068       /* If this is an array type, we need to make sure our
18069          kids get marked, even if they're types.  */
18070       if (die->die_tag == DW_TAG_array_type)
18071         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
18072       else
18073         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18074     }
18075 }
18076
18077 /* For local classes, look if any static member functions were emitted
18078    and if so, mark them.  */
18079
18080 static void
18081 prune_unused_types_walk_local_classes (dw_die_ref die)
18082 {
18083   dw_die_ref c;
18084
18085   if (die->die_mark == 2)
18086     return;
18087
18088   switch (die->die_tag)
18089     {
18090     case DW_TAG_structure_type:
18091     case DW_TAG_union_type:
18092     case DW_TAG_class_type:
18093       break;
18094
18095     case DW_TAG_subprogram:
18096       if (!get_AT_flag (die, DW_AT_declaration)
18097           || die->die_definition != NULL)
18098         prune_unused_types_mark (die, 1);
18099       return;
18100
18101     default:
18102       return;
18103     }
18104
18105   /* Mark children.  */
18106   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
18107 }
18108
18109 /* Walk the tree DIE and mark types that we actually use.  */
18110
18111 static void
18112 prune_unused_types_walk (dw_die_ref die)
18113 {
18114   dw_die_ref c;
18115
18116   /* Don't do anything if this node is already marked and
18117      children have been marked as well.  */
18118   if (die->die_mark == 2)
18119     return;
18120
18121   switch (die->die_tag)
18122     {
18123     case DW_TAG_structure_type:
18124     case DW_TAG_union_type:
18125     case DW_TAG_class_type:
18126       if (die->die_perennial_p)
18127         break;
18128
18129       for (c = die->die_parent; c; c = c->die_parent)
18130         if (c->die_tag == DW_TAG_subprogram)
18131           break;
18132
18133       /* Finding used static member functions inside of classes
18134          is needed just for local classes, because for other classes
18135          static member function DIEs with DW_AT_specification
18136          are emitted outside of the DW_TAG_*_type.  If we ever change
18137          it, we'd need to call this even for non-local classes.  */
18138       if (c)
18139         prune_unused_types_walk_local_classes (die);
18140
18141       /* It's a type node --- don't mark it.  */
18142       return;
18143
18144     case DW_TAG_const_type:
18145     case DW_TAG_packed_type:
18146     case DW_TAG_pointer_type:
18147     case DW_TAG_reference_type:
18148     case DW_TAG_volatile_type:
18149     case DW_TAG_typedef:
18150     case DW_TAG_array_type:
18151     case DW_TAG_interface_type:
18152     case DW_TAG_friend:
18153     case DW_TAG_variant_part:
18154     case DW_TAG_enumeration_type:
18155     case DW_TAG_subroutine_type:
18156     case DW_TAG_string_type:
18157     case DW_TAG_set_type:
18158     case DW_TAG_subrange_type:
18159     case DW_TAG_ptr_to_member_type:
18160     case DW_TAG_file_type:
18161       if (die->die_perennial_p)
18162         break;
18163
18164       /* It's a type node --- don't mark it.  */
18165       return;
18166
18167     default:
18168       /* Mark everything else.  */
18169       break;
18170   }
18171
18172   if (die->die_mark == 0)
18173     {
18174       die->die_mark = 1;
18175
18176       /* Now, mark any dies referenced from here.  */
18177       prune_unused_types_walk_attribs (die);
18178     }
18179
18180   die->die_mark = 2;
18181
18182   /* Mark children.  */
18183   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18184 }
18185
18186 /* Increment the string counts on strings referred to from DIE's
18187    attributes.  */
18188
18189 static void
18190 prune_unused_types_update_strings (dw_die_ref die)
18191 {
18192   dw_attr_ref a;
18193   unsigned ix;
18194
18195   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18196     if (AT_class (a) == dw_val_class_str)
18197       {
18198         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
18199         s->refcount++;
18200         /* Avoid unnecessarily putting strings that are used less than
18201            twice in the hash table.  */
18202         if (s->refcount
18203             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
18204           {
18205             void ** slot;
18206             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
18207                                              htab_hash_string (s->str),
18208                                              INSERT);
18209             gcc_assert (*slot == NULL);
18210             *slot = s;
18211           }
18212       }
18213 }
18214
18215 /* Remove from the tree DIE any dies that aren't marked.  */
18216
18217 static void
18218 prune_unused_types_prune (dw_die_ref die)
18219 {
18220   dw_die_ref c;
18221
18222   gcc_assert (die->die_mark);
18223   prune_unused_types_update_strings (die);
18224
18225   if (! die->die_child)
18226     return;
18227
18228   c = die->die_child;
18229   do {
18230     dw_die_ref prev = c;
18231     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
18232       if (c == die->die_child)
18233         {
18234           /* No marked children between 'prev' and the end of the list.  */
18235           if (prev == c)
18236             /* No marked children at all.  */
18237             die->die_child = NULL;
18238           else
18239             {
18240               prev->die_sib = c->die_sib;
18241               die->die_child = prev;
18242             }
18243           return;
18244         }
18245
18246     if (c != prev->die_sib)
18247       prev->die_sib = c;
18248     prune_unused_types_prune (c);
18249   } while (c != die->die_child);
18250 }
18251
18252 /* A helper function for dwarf2out_finish called through
18253    htab_traverse.  Clear .debug_str strings that we haven't already
18254    decided to emit.  */
18255
18256 static int
18257 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
18258 {
18259   struct indirect_string_node *node = (struct indirect_string_node *) *h;
18260
18261   if (!node->label || !node->refcount)
18262     htab_clear_slot (debug_str_hash, h);
18263
18264   return 1;
18265 }
18266
18267 /* Remove dies representing declarations that we never use.  */
18268
18269 static void
18270 prune_unused_types (void)
18271 {
18272   unsigned int i;
18273   limbo_die_node *node;
18274   pubname_ref pub;
18275
18276 #if ENABLE_ASSERT_CHECKING
18277   /* All the marks should already be clear.  */
18278   verify_marks_clear (comp_unit_die);
18279   for (node = limbo_die_list; node; node = node->next)
18280     verify_marks_clear (node->die);
18281 #endif /* ENABLE_ASSERT_CHECKING */
18282
18283   /* Set the mark on nodes that are actually used.  */
18284   prune_unused_types_walk (comp_unit_die);
18285   for (node = limbo_die_list; node; node = node->next)
18286     prune_unused_types_walk (node->die);
18287
18288   /* Also set the mark on nodes referenced from the
18289      pubname_table or arange_table.  */
18290   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
18291     prune_unused_types_mark (pub->die, 1);
18292   for (i = 0; i < arange_table_in_use; i++)
18293     prune_unused_types_mark (arange_table[i], 1);
18294
18295   /* Get rid of nodes that aren't marked; and update the string counts.  */
18296   if (debug_str_hash && debug_str_hash_forced)
18297     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
18298   else if (debug_str_hash)
18299     htab_empty (debug_str_hash);
18300   prune_unused_types_prune (comp_unit_die);
18301   for (node = limbo_die_list; node; node = node->next)
18302     prune_unused_types_prune (node->die);
18303
18304   /* Leave the marks clear.  */
18305   prune_unmark_dies (comp_unit_die);
18306   for (node = limbo_die_list; node; node = node->next)
18307     prune_unmark_dies (node->die);
18308 }
18309
18310 /* Set the parameter to true if there are any relative pathnames in
18311    the file table.  */
18312 static int
18313 file_table_relative_p (void ** slot, void *param)
18314 {
18315   bool *p = (bool *) param;
18316   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
18317   if (!IS_ABSOLUTE_PATH (d->filename))
18318     {
18319       *p = true;
18320       return 0;
18321     }
18322   return 1;
18323 }
18324
18325 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
18326    to the location it would have been added, should we know its
18327    DECL_ASSEMBLER_NAME when we added other attributes.  This will
18328    probably improve compactness of debug info, removing equivalent
18329    abbrevs, and hide any differences caused by deferring the
18330    computation of the assembler name, triggered by e.g. PCH.  */
18331
18332 static inline void
18333 move_linkage_attr (dw_die_ref die)
18334 {
18335   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
18336   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
18337
18338   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
18339
18340   while (--ix > 0)
18341     {
18342       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
18343
18344       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
18345         break;
18346     }
18347
18348   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
18349     {
18350       VEC_pop (dw_attr_node, die->die_attr);
18351       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
18352     }
18353 }
18354
18355 /* Output stuff that dwarf requires at the end of every file,
18356    and generate the DWARF-2 debugging info.  */
18357
18358 static void
18359 dwarf2out_finish (const char *filename)
18360 {
18361   limbo_die_node *node, *next_node;
18362   dw_die_ref die = 0;
18363   unsigned int i;
18364
18365   gen_remaining_tmpl_value_param_die_attribute ();
18366
18367   /* Add the name for the main input file now.  We delayed this from
18368      dwarf2out_init to avoid complications with PCH.  */
18369   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
18370   if (!IS_ABSOLUTE_PATH (filename))
18371     add_comp_dir_attribute (comp_unit_die);
18372   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
18373     {
18374       bool p = false;
18375       htab_traverse (file_table, file_table_relative_p, &p);
18376       if (p)
18377         add_comp_dir_attribute (comp_unit_die);
18378     }
18379
18380   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
18381     {
18382       add_location_or_const_value_attribute (
18383         VEC_index (deferred_locations, deferred_locations_list, i)->die,
18384         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
18385         DW_AT_location);
18386     }
18387
18388   /* Traverse the limbo die list, and add parent/child links.  The only
18389      dies without parents that should be here are concrete instances of
18390      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
18391      For concrete instances, we can get the parent die from the abstract
18392      instance.  */
18393   for (node = limbo_die_list; node; node = next_node)
18394     {
18395       next_node = node->next;
18396       die = node->die;
18397
18398       if (die->die_parent == NULL)
18399         {
18400           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
18401
18402           if (origin)
18403             add_child_die (origin->die_parent, die);
18404           else if (die == comp_unit_die)
18405             ;
18406           else if (errorcount > 0 || sorrycount > 0)
18407             /* It's OK to be confused by errors in the input.  */
18408             add_child_die (comp_unit_die, die);
18409           else
18410             {
18411               /* In certain situations, the lexical block containing a
18412                  nested function can be optimized away, which results
18413                  in the nested function die being orphaned.  Likewise
18414                  with the return type of that nested function.  Force
18415                  this to be a child of the containing function.
18416
18417                  It may happen that even the containing function got fully
18418                  inlined and optimized out.  In that case we are lost and
18419                  assign the empty child.  This should not be big issue as
18420                  the function is likely unreachable too.  */
18421               tree context = NULL_TREE;
18422
18423               gcc_assert (node->created_for);
18424
18425               if (DECL_P (node->created_for))
18426                 context = DECL_CONTEXT (node->created_for);
18427               else if (TYPE_P (node->created_for))
18428                 context = TYPE_CONTEXT (node->created_for);
18429
18430               gcc_assert (context
18431                           && (TREE_CODE (context) == FUNCTION_DECL
18432                               || TREE_CODE (context) == NAMESPACE_DECL));
18433
18434               origin = lookup_decl_die (context);
18435               if (origin)
18436                 add_child_die (origin, die);
18437               else
18438                 add_child_die (comp_unit_die, die);
18439             }
18440         }
18441     }
18442
18443   limbo_die_list = NULL;
18444
18445   for (node = deferred_asm_name; node; node = node->next)
18446     {
18447       tree decl = node->created_for;
18448       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18449         {
18450           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
18451                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
18452           move_linkage_attr (node->die);
18453         }
18454     }
18455
18456   deferred_asm_name = NULL;
18457
18458   /* Walk through the list of incomplete types again, trying once more to
18459      emit full debugging info for them.  */
18460   retry_incomplete_types ();
18461
18462   if (flag_eliminate_unused_debug_types)
18463     prune_unused_types ();
18464
18465   /* Generate separate CUs for each of the include files we've seen.
18466      They will go into limbo_die_list.  */
18467   if (flag_eliminate_dwarf2_dups)
18468     break_out_includes (comp_unit_die);
18469
18470   /* Traverse the DIE's and add add sibling attributes to those DIE's
18471      that have children.  */
18472   add_sibling_attributes (comp_unit_die);
18473   for (node = limbo_die_list; node; node = node->next)
18474     add_sibling_attributes (node->die);
18475
18476   /* Output a terminator label for the .text section.  */
18477   switch_to_section (text_section);
18478   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
18479   if (flag_reorder_blocks_and_partition)
18480     {
18481       switch_to_section (unlikely_text_section ());
18482       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
18483     }
18484
18485   /* We can only use the low/high_pc attributes if all of the code was
18486      in .text.  */
18487   if (!have_multiple_function_sections)
18488     {
18489       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
18490       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
18491     }
18492
18493   else
18494     {
18495       unsigned fde_idx = 0;
18496
18497       /* We need to give .debug_loc and .debug_ranges an appropriate
18498          "base address".  Use zero so that these addresses become
18499          absolute.  Historically, we've emitted the unexpected
18500          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
18501          Emit both to give time for other tools to adapt.  */
18502       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
18503       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
18504
18505       add_AT_range_list (comp_unit_die, DW_AT_ranges,
18506                          add_ranges_by_labels (text_section_label,
18507                                                text_end_label));
18508       if (flag_reorder_blocks_and_partition)
18509         add_ranges_by_labels (cold_text_section_label,
18510                               cold_end_label);
18511
18512       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
18513         {
18514           dw_fde_ref fde = &fde_table[fde_idx];
18515
18516           if (fde->dw_fde_switched_sections)
18517             {
18518               if (!fde->in_std_section)
18519                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
18520                                       fde->dw_fde_hot_section_end_label);
18521               if (!fde->cold_in_std_section)
18522                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
18523                                       fde->dw_fde_unlikely_section_end_label);
18524             }
18525           else if (!fde->in_std_section)
18526             add_ranges_by_labels (fde->dw_fde_begin,
18527                                   fde->dw_fde_end);
18528         }
18529
18530       add_ranges (NULL);
18531     }
18532
18533   /* Output location list section if necessary.  */
18534   if (have_location_lists)
18535     {
18536       /* Output the location lists info.  */
18537       switch_to_section (debug_loc_section);
18538       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
18539                                    DEBUG_LOC_SECTION_LABEL, 0);
18540       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
18541       output_location_lists (die);
18542     }
18543
18544   if (debug_info_level >= DINFO_LEVEL_NORMAL)
18545     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
18546                     debug_line_section_label);
18547
18548   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18549     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
18550
18551   /* Output all of the compilation units.  We put the main one last so that
18552      the offsets are available to output_pubnames.  */
18553   for (node = limbo_die_list; node; node = node->next)
18554     output_comp_unit (node->die, 0);
18555
18556   /* Output the main compilation unit if non-empty or if .debug_macinfo
18557      has been emitted.  */
18558   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
18559
18560   /* Output the abbreviation table.  */
18561   switch_to_section (debug_abbrev_section);
18562   output_abbrev_section ();
18563
18564   /* Output public names table if necessary.  */
18565   if (!VEC_empty (pubname_entry, pubname_table))
18566     {
18567       switch_to_section (debug_pubnames_section);
18568       output_pubnames (pubname_table);
18569     }
18570
18571   /* Output public types table if necessary.  */
18572   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
18573      It shouldn't hurt to emit it always, since pure DWARF2 consumers
18574      simply won't look for the section.  */
18575   if (!VEC_empty (pubname_entry, pubtype_table))
18576     {
18577       switch_to_section (debug_pubtypes_section);
18578       output_pubnames (pubtype_table);
18579     }
18580
18581   /* Output the address range information.  We only put functions in the arange
18582      table, so don't write it out if we don't have any.  */
18583   if (fde_table_in_use)
18584     {
18585       switch_to_section (debug_aranges_section);
18586       output_aranges ();
18587     }
18588
18589   /* Output ranges section if necessary.  */
18590   if (ranges_table_in_use)
18591     {
18592       switch_to_section (debug_ranges_section);
18593       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
18594       output_ranges ();
18595     }
18596
18597   /* Output the source line correspondence table.  We must do this
18598      even if there is no line information.  Otherwise, on an empty
18599      translation unit, we will generate a present, but empty,
18600      .debug_info section.  IRIX 6.5 `nm' will then complain when
18601      examining the file.  This is done late so that any filenames
18602      used by the debug_info section are marked as 'used'.  */
18603   if (! DWARF2_ASM_LINE_DEBUG_INFO)
18604     {
18605       switch_to_section (debug_line_section);
18606       output_line_info ();
18607     }
18608
18609   /* Have to end the macro section.  */
18610   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18611     {
18612       switch_to_section (debug_macinfo_section);
18613       dw2_asm_output_data (1, 0, "End compilation unit");
18614     }
18615
18616   /* If we emitted any DW_FORM_strp form attribute, output the string
18617      table too.  */
18618   if (debug_str_hash)
18619     htab_traverse (debug_str_hash, output_indirect_string, NULL);
18620 }
18621 #else
18622
18623 /* This should never be used, but its address is needed for comparisons.  */
18624 const struct gcc_debug_hooks dwarf2_debug_hooks =
18625 {
18626   0,            /* init */
18627   0,            /* finish */
18628   0,            /* define */
18629   0,            /* undef */
18630   0,            /* start_source_file */
18631   0,            /* end_source_file */
18632   0,            /* begin_block */
18633   0,            /* end_block */
18634   0,            /* ignore_block */
18635   0,            /* source_line */
18636   0,            /* begin_prologue */
18637   0,            /* end_prologue */
18638   0,            /* end_epilogue */
18639   0,            /* begin_function */
18640   0,            /* end_function */
18641   0,            /* function_decl */
18642   0,            /* global_decl */
18643   0,            /* type_decl */
18644   0,            /* imported_module_or_decl */
18645   0,            /* deferred_inline_function */
18646   0,            /* outlining_inline_function */
18647   0,            /* label */
18648   0,            /* handle_pch */
18649   0,            /* var_location */
18650   0,            /* switch_text_section */
18651   0,            /* set_name */
18652   0             /* start_end_main_source_file */
18653 };
18654
18655 #endif /* DWARF2_DEBUGGING_INFO */
18656
18657 #include "gt-dwarf2out.h"