OSDN Git Service

gcc/ChangeLog
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94
95 #ifdef DWARF2_DEBUGGING_INFO
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97
98 static rtx last_var_location_insn;
99 #endif
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 #ifndef DWARF2_FRAME_INFO
118 # ifdef DWARF2_DEBUGGING_INFO
119 #  define DWARF2_FRAME_INFO \
120   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
121 # else
122 #  define DWARF2_FRAME_INFO 0
123 # endif
124 #endif
125
126 /* Map register numbers held in the call frame info that gcc has
127    collected using DWARF_FRAME_REGNUM to those that should be output in
128    .debug_frame and .eh_frame.  */
129 #ifndef DWARF2_FRAME_REG_OUT
130 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
131 #endif
132
133 /* Save the result of dwarf2out_do_frame across PCH.  */
134 static GTY(()) bool saved_do_cfi_asm = 0;
135
136 /* Decide whether we want to emit frame unwind information for the current
137    translation unit.  */
138
139 int
140 dwarf2out_do_frame (void)
141 {
142   /* We want to emit correct CFA location expressions or lists, so we
143      have to return true if we're going to output debug info, even if
144      we're not going to output frame or unwind info.  */
145   return (write_symbols == DWARF2_DEBUG
146           || write_symbols == VMS_AND_DWARF2_DEBUG
147           || DWARF2_FRAME_INFO || saved_do_cfi_asm
148 #ifdef DWARF2_UNWIND_INFO
149           || (DWARF2_UNWIND_INFO
150               && (flag_unwind_tables
151                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
152 #endif
153           );
154 }
155
156 /* Decide whether to emit frame unwind via assembler directives.  */
157
158 int
159 dwarf2out_do_cfi_asm (void)
160 {
161   int enc;
162
163 #ifdef MIPS_DEBUGGING_INFO
164   return false;
165 #endif
166   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
167     return false;
168   if (saved_do_cfi_asm)
169     return true;
170   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
171     return false;
172
173   /* Make sure the personality encoding is one the assembler can support.
174      In particular, aligned addresses can't be handled.  */
175   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
176   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
177     return false;
178   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
179   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
180     return false;
181
182   saved_do_cfi_asm = true;
183   return true;
184 }
185
186 /* The size of the target's pointer type.  */
187 #ifndef PTR_SIZE
188 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
189 #endif
190
191 /* Array of RTXes referenced by the debugging information, which therefore
192    must be kept around forever.  */
193 static GTY(()) VEC(rtx,gc) *used_rtx_array;
194
195 /* A pointer to the base of a list of incomplete types which might be
196    completed at some later time.  incomplete_types_list needs to be a
197    VEC(tree,gc) because we want to tell the garbage collector about
198    it.  */
199 static GTY(()) VEC(tree,gc) *incomplete_types;
200
201 /* A pointer to the base of a table of references to declaration
202    scopes.  This table is a display which tracks the nesting
203    of declaration scopes at the current scope and containing
204    scopes.  This table is used to find the proper place to
205    define type declaration DIE's.  */
206 static GTY(()) VEC(tree,gc) *decl_scope_table;
207
208 /* Pointers to various DWARF2 sections.  */
209 static GTY(()) section *debug_info_section;
210 static GTY(()) section *debug_abbrev_section;
211 static GTY(()) section *debug_aranges_section;
212 static GTY(()) section *debug_macinfo_section;
213 static GTY(()) section *debug_line_section;
214 static GTY(()) section *debug_loc_section;
215 static GTY(()) section *debug_pubnames_section;
216 static GTY(()) section *debug_pubtypes_section;
217 static GTY(()) section *debug_str_section;
218 static GTY(()) section *debug_ranges_section;
219 static GTY(()) section *debug_frame_section;
220
221 /* Personality decl of current unit.  Used only when assembler does not support
222    personality CFI.  */
223 static GTY(()) rtx current_unit_personality;
224
225 /* How to start an assembler comment.  */
226 #ifndef ASM_COMMENT_START
227 #define ASM_COMMENT_START ";#"
228 #endif
229
230 typedef struct dw_cfi_struct *dw_cfi_ref;
231 typedef struct dw_fde_struct *dw_fde_ref;
232 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
233
234 /* Call frames are described using a sequence of Call Frame
235    Information instructions.  The register number, offset
236    and address fields are provided as possible operands;
237    their use is selected by the opcode field.  */
238
239 enum dw_cfi_oprnd_type {
240   dw_cfi_oprnd_unused,
241   dw_cfi_oprnd_reg_num,
242   dw_cfi_oprnd_offset,
243   dw_cfi_oprnd_addr,
244   dw_cfi_oprnd_loc
245 };
246
247 typedef union GTY(()) dw_cfi_oprnd_struct {
248   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
249   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
250   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
251   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
252 }
253 dw_cfi_oprnd;
254
255 typedef struct GTY(()) dw_cfi_struct {
256   dw_cfi_ref dw_cfi_next;
257   enum dwarf_call_frame_info dw_cfi_opc;
258   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
259     dw_cfi_oprnd1;
260   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
261     dw_cfi_oprnd2;
262 }
263 dw_cfi_node;
264
265 /* This is how we define the location of the CFA. We use to handle it
266    as REG + OFFSET all the time,  but now it can be more complex.
267    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
268    Instead of passing around REG and OFFSET, we pass a copy
269    of this structure.  */
270 typedef struct GTY(()) cfa_loc {
271   HOST_WIDE_INT offset;
272   HOST_WIDE_INT base_offset;
273   unsigned int reg;
274   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
275   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
276 } dw_cfa_location;
277
278 /* All call frame descriptions (FDE's) in the GCC generated DWARF
279    refer to a single Common Information Entry (CIE), defined at
280    the beginning of the .debug_frame section.  This use of a single
281    CIE obviates the need to keep track of multiple CIE's
282    in the DWARF generation routines below.  */
283
284 typedef struct GTY(()) dw_fde_struct {
285   tree decl;
286   const char *dw_fde_begin;
287   const char *dw_fde_current_label;
288   const char *dw_fde_end;
289   const char *dw_fde_hot_section_label;
290   const char *dw_fde_hot_section_end_label;
291   const char *dw_fde_unlikely_section_label;
292   const char *dw_fde_unlikely_section_end_label;
293   dw_cfi_ref dw_fde_cfi;
294   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
295   unsigned funcdef_number;
296   HOST_WIDE_INT stack_realignment;
297   /* Dynamic realign argument pointer register.  */
298   unsigned int drap_reg;
299   /* Virtual dynamic realign argument pointer register.  */
300   unsigned int vdrap_reg;
301   unsigned all_throwers_are_sibcalls : 1;
302   unsigned nothrow : 1;
303   unsigned uses_eh_lsda : 1;
304   /* Whether we did stack realign in this call frame.  */
305   unsigned stack_realign : 1;
306   /* Whether dynamic realign argument pointer register has been saved.  */
307   unsigned drap_reg_saved: 1;
308   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
309   unsigned in_std_section : 1;
310   /* True iff dw_fde_unlikely_section_label is in text_section or
311      cold_text_section.  */
312   unsigned cold_in_std_section : 1;
313   /* True iff switched sections.  */
314   unsigned dw_fde_switched_sections : 1;
315   /* True iff switching from cold to hot section.  */
316   unsigned dw_fde_switched_cold_to_hot : 1;
317 }
318 dw_fde_node;
319
320 /* Maximum size (in bytes) of an artificially generated label.  */
321 #define MAX_ARTIFICIAL_LABEL_BYTES      30
322
323 /* The size of addresses as they appear in the Dwarf 2 data.
324    Some architectures use word addresses to refer to code locations,
325    but Dwarf 2 info always uses byte addresses.  On such machines,
326    Dwarf 2 addresses need to be larger than the architecture's
327    pointers.  */
328 #ifndef DWARF2_ADDR_SIZE
329 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
330 #endif
331
332 /* The size in bytes of a DWARF field indicating an offset or length
333    relative to a debug info section, specified to be 4 bytes in the
334    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
335    as PTR_SIZE.  */
336
337 #ifndef DWARF_OFFSET_SIZE
338 #define DWARF_OFFSET_SIZE 4
339 #endif
340
341 /* According to the (draft) DWARF 3 specification, the initial length
342    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
343    bytes are 0xffffffff, followed by the length stored in the next 8
344    bytes.
345
346    However, the SGI/MIPS ABI uses an initial length which is equal to
347    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
348
349 #ifndef DWARF_INITIAL_LENGTH_SIZE
350 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
351 #endif
352
353 /* Round SIZE up to the nearest BOUNDARY.  */
354 #define DWARF_ROUND(SIZE,BOUNDARY) \
355   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
356
357 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
358 #ifndef DWARF_CIE_DATA_ALIGNMENT
359 #ifdef STACK_GROWS_DOWNWARD
360 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
361 #else
362 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
363 #endif
364 #endif
365
366 /* CIE identifier.  */
367 #if HOST_BITS_PER_WIDE_INT >= 64
368 #define DWARF_CIE_ID \
369   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
370 #else
371 #define DWARF_CIE_ID DW_CIE_ID
372 #endif
373
374 /* A pointer to the base of a table that contains frame description
375    information for each routine.  */
376 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
377
378 /* Number of elements currently allocated for fde_table.  */
379 static GTY(()) unsigned fde_table_allocated;
380
381 /* Number of elements in fde_table currently in use.  */
382 static GTY(()) unsigned fde_table_in_use;
383
384 /* Size (in elements) of increments by which we may expand the
385    fde_table.  */
386 #define FDE_TABLE_INCREMENT 256
387
388 /* Get the current fde_table entry we should use.  */
389
390 static inline dw_fde_ref
391 current_fde (void)
392 {
393   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
394 }
395
396 /* A list of call frame insns for the CIE.  */
397 static GTY(()) dw_cfi_ref cie_cfi_head;
398
399 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
400 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
401    attribute that accelerates the lookup of the FDE associated
402    with the subprogram.  This variable holds the table index of the FDE
403    associated with the current function (body) definition.  */
404 static unsigned current_funcdef_fde;
405 #endif
406
407 struct GTY(()) indirect_string_node {
408   const char *str;
409   unsigned int refcount;
410   enum dwarf_form form;
411   char *label;
412 };
413
414 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
415
416 /* True if the compilation unit has location entries that reference
417    debug strings.  */
418 static GTY(()) bool debug_str_hash_forced = false;
419
420 static GTY(()) int dw2_string_counter;
421 static GTY(()) unsigned long dwarf2out_cfi_label_num;
422
423 /* True if the compilation unit places functions in more than one section.  */
424 static GTY(()) bool have_multiple_function_sections = false;
425
426 /* Whether the default text and cold text sections have been used at all.  */
427
428 static GTY(()) bool text_section_used = false;
429 static GTY(()) bool cold_text_section_used = false;
430
431 /* The default cold text section.  */
432 static GTY(()) section *cold_text_section;
433
434 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
435
436 /* Forward declarations for functions defined in this file.  */
437
438 static char *stripattributes (const char *);
439 static const char *dwarf_cfi_name (unsigned);
440 static dw_cfi_ref new_cfi (void);
441 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
442 static void add_fde_cfi (const char *, dw_cfi_ref);
443 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
444 static void lookup_cfa (dw_cfa_location *);
445 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
446 #ifdef DWARF2_UNWIND_INFO
447 static void initial_return_save (rtx);
448 #endif
449 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
450                                           HOST_WIDE_INT);
451 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
452 static void output_cfi_directive (dw_cfi_ref);
453 static void output_call_frame_info (int);
454 static void dwarf2out_note_section_used (void);
455 static void dwarf2out_stack_adjust (rtx, bool);
456 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
457 static void flush_queued_reg_saves (void);
458 static bool clobbers_queued_reg_save (const_rtx);
459 static void dwarf2out_frame_debug_expr (rtx, const char *);
460
461 /* Support for complex CFA locations.  */
462 static void output_cfa_loc (dw_cfi_ref);
463 static void output_cfa_loc_raw (dw_cfi_ref);
464 static void get_cfa_from_loc_descr (dw_cfa_location *,
465                                     struct dw_loc_descr_struct *);
466 static struct dw_loc_descr_struct *build_cfa_loc
467   (dw_cfa_location *, HOST_WIDE_INT);
468 static struct dw_loc_descr_struct *build_cfa_aligned_loc
469   (HOST_WIDE_INT, HOST_WIDE_INT);
470 static void def_cfa_1 (const char *, dw_cfa_location *);
471
472 /* How to start an assembler comment.  */
473 #ifndef ASM_COMMENT_START
474 #define ASM_COMMENT_START ";#"
475 #endif
476
477 /* Data and reference forms for relocatable data.  */
478 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
479 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
480
481 #ifndef DEBUG_FRAME_SECTION
482 #define DEBUG_FRAME_SECTION     ".debug_frame"
483 #endif
484
485 #ifndef FUNC_BEGIN_LABEL
486 #define FUNC_BEGIN_LABEL        "LFB"
487 #endif
488
489 #ifndef FUNC_END_LABEL
490 #define FUNC_END_LABEL          "LFE"
491 #endif
492
493 #ifndef FRAME_BEGIN_LABEL
494 #define FRAME_BEGIN_LABEL       "Lframe"
495 #endif
496 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
497 #define CIE_END_LABEL           "LECIE"
498 #define FDE_LABEL               "LSFDE"
499 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
500 #define FDE_END_LABEL           "LEFDE"
501 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
502 #define LINE_NUMBER_END_LABEL   "LELT"
503 #define LN_PROLOG_AS_LABEL      "LASLTP"
504 #define LN_PROLOG_END_LABEL     "LELTP"
505 #define DIE_LABEL_PREFIX        "DW"
506
507 /* The DWARF 2 CFA column which tracks the return address.  Normally this
508    is the column for PC, or the first column after all of the hard
509    registers.  */
510 #ifndef DWARF_FRAME_RETURN_COLUMN
511 #ifdef PC_REGNUM
512 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
513 #else
514 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
515 #endif
516 #endif
517
518 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
519    default, we just provide columns for all registers.  */
520 #ifndef DWARF_FRAME_REGNUM
521 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
522 #endif
523 \f
524 /* Hook used by __throw.  */
525
526 rtx
527 expand_builtin_dwarf_sp_column (void)
528 {
529   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
530   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
531 }
532
533 /* Return a pointer to a copy of the section string name S with all
534    attributes stripped off, and an asterisk prepended (for assemble_name).  */
535
536 static inline char *
537 stripattributes (const char *s)
538 {
539   char *stripped = XNEWVEC (char, strlen (s) + 2);
540   char *p = stripped;
541
542   *p++ = '*';
543
544   while (*s && *s != ',')
545     *p++ = *s++;
546
547   *p = '\0';
548   return stripped;
549 }
550
551 /* MEM is a memory reference for the register size table, each element of
552    which has mode MODE.  Initialize column C as a return address column.  */
553
554 static void
555 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
556 {
557   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
558   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
559   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
560 }
561
562 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
563
564 static inline HOST_WIDE_INT
565 div_data_align (HOST_WIDE_INT off)
566 {
567   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
568   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
569   return r;
570 }
571
572 /* Return true if we need a signed version of a given opcode
573    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
574
575 static inline bool
576 need_data_align_sf_opcode (HOST_WIDE_INT off)
577 {
578   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
579 }
580
581 /* Generate code to initialize the register size table.  */
582
583 void
584 expand_builtin_init_dwarf_reg_sizes (tree address)
585 {
586   unsigned int i;
587   enum machine_mode mode = TYPE_MODE (char_type_node);
588   rtx addr = expand_normal (address);
589   rtx mem = gen_rtx_MEM (BLKmode, addr);
590   bool wrote_return_column = false;
591
592   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
593     {
594       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
595
596       if (rnum < DWARF_FRAME_REGISTERS)
597         {
598           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
599           enum machine_mode save_mode = reg_raw_mode[i];
600           HOST_WIDE_INT size;
601
602           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
603             save_mode = choose_hard_reg_mode (i, 1, true);
604           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
605             {
606               if (save_mode == VOIDmode)
607                 continue;
608               wrote_return_column = true;
609             }
610           size = GET_MODE_SIZE (save_mode);
611           if (offset < 0)
612             continue;
613
614           emit_move_insn (adjust_address (mem, mode, offset),
615                           gen_int_mode (size, mode));
616         }
617     }
618
619   if (!wrote_return_column)
620     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
621
622 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
623   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
624 #endif
625
626   targetm.init_dwarf_reg_sizes_extra (address);
627 }
628
629 /* Convert a DWARF call frame info. operation to its string name */
630
631 static const char *
632 dwarf_cfi_name (unsigned int cfi_opc)
633 {
634   switch (cfi_opc)
635     {
636     case DW_CFA_advance_loc:
637       return "DW_CFA_advance_loc";
638     case DW_CFA_offset:
639       return "DW_CFA_offset";
640     case DW_CFA_restore:
641       return "DW_CFA_restore";
642     case DW_CFA_nop:
643       return "DW_CFA_nop";
644     case DW_CFA_set_loc:
645       return "DW_CFA_set_loc";
646     case DW_CFA_advance_loc1:
647       return "DW_CFA_advance_loc1";
648     case DW_CFA_advance_loc2:
649       return "DW_CFA_advance_loc2";
650     case DW_CFA_advance_loc4:
651       return "DW_CFA_advance_loc4";
652     case DW_CFA_offset_extended:
653       return "DW_CFA_offset_extended";
654     case DW_CFA_restore_extended:
655       return "DW_CFA_restore_extended";
656     case DW_CFA_undefined:
657       return "DW_CFA_undefined";
658     case DW_CFA_same_value:
659       return "DW_CFA_same_value";
660     case DW_CFA_register:
661       return "DW_CFA_register";
662     case DW_CFA_remember_state:
663       return "DW_CFA_remember_state";
664     case DW_CFA_restore_state:
665       return "DW_CFA_restore_state";
666     case DW_CFA_def_cfa:
667       return "DW_CFA_def_cfa";
668     case DW_CFA_def_cfa_register:
669       return "DW_CFA_def_cfa_register";
670     case DW_CFA_def_cfa_offset:
671       return "DW_CFA_def_cfa_offset";
672
673     /* DWARF 3 */
674     case DW_CFA_def_cfa_expression:
675       return "DW_CFA_def_cfa_expression";
676     case DW_CFA_expression:
677       return "DW_CFA_expression";
678     case DW_CFA_offset_extended_sf:
679       return "DW_CFA_offset_extended_sf";
680     case DW_CFA_def_cfa_sf:
681       return "DW_CFA_def_cfa_sf";
682     case DW_CFA_def_cfa_offset_sf:
683       return "DW_CFA_def_cfa_offset_sf";
684
685     /* SGI/MIPS specific */
686     case DW_CFA_MIPS_advance_loc8:
687       return "DW_CFA_MIPS_advance_loc8";
688
689     /* GNU extensions */
690     case DW_CFA_GNU_window_save:
691       return "DW_CFA_GNU_window_save";
692     case DW_CFA_GNU_args_size:
693       return "DW_CFA_GNU_args_size";
694     case DW_CFA_GNU_negative_offset_extended:
695       return "DW_CFA_GNU_negative_offset_extended";
696
697     default:
698       return "DW_CFA_<unknown>";
699     }
700 }
701
702 /* Return a pointer to a newly allocated Call Frame Instruction.  */
703
704 static inline dw_cfi_ref
705 new_cfi (void)
706 {
707   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
708
709   cfi->dw_cfi_next = NULL;
710   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
711   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
712
713   return cfi;
714 }
715
716 /* Add a Call Frame Instruction to list of instructions.  */
717
718 static inline void
719 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
720 {
721   dw_cfi_ref *p;
722   dw_fde_ref fde = current_fde ();
723
724   /* When DRAP is used, CFA is defined with an expression.  Redefine
725      CFA may lead to a different CFA value.   */
726   /* ??? Of course, this heuristic fails when we're annotating epilogues,
727      because of course we'll always want to redefine the CFA back to the
728      stack pointer on the way out.  Where should we move this check?  */
729   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
730     switch (cfi->dw_cfi_opc)
731       {
732         case DW_CFA_def_cfa_register:
733         case DW_CFA_def_cfa_offset:
734         case DW_CFA_def_cfa_offset_sf:
735         case DW_CFA_def_cfa:
736         case DW_CFA_def_cfa_sf:
737           gcc_unreachable ();
738
739         default:
740           break;
741       }
742
743   /* Find the end of the chain.  */
744   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
745     ;
746
747   *p = cfi;
748 }
749
750 /* Generate a new label for the CFI info to refer to.  FORCE is true
751    if a label needs to be output even when using .cfi_* directives.  */
752
753 char *
754 dwarf2out_cfi_label (bool force)
755 {
756   static char label[20];
757
758   if (!force && dwarf2out_do_cfi_asm ())
759     {
760       /* In this case, we will be emitting the asm directive instead of
761          the label, so just return a placeholder to keep the rest of the
762          interfaces happy.  */
763       strcpy (label, "<do not output>");
764     }
765   else
766     {
767       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
768       ASM_OUTPUT_LABEL (asm_out_file, label);
769     }
770
771   return label;
772 }
773
774 /* True if remember_state should be emitted before following CFI directive.  */
775 static bool emit_cfa_remember;
776
777 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
778    or to the CIE if LABEL is NULL.  */
779
780 static void
781 add_fde_cfi (const char *label, dw_cfi_ref cfi)
782 {
783   dw_cfi_ref *list_head;
784
785   if (emit_cfa_remember)
786     {
787       dw_cfi_ref cfi_remember;
788
789       /* Emit the state save.  */
790       emit_cfa_remember = false;
791       cfi_remember = new_cfi (); 
792       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
793       add_fde_cfi (label, cfi_remember);
794     }
795
796   list_head = &cie_cfi_head;
797
798   if (dwarf2out_do_cfi_asm ())
799     {
800       if (label)
801         {
802           dw_fde_ref fde = current_fde ();
803
804           gcc_assert (fde != NULL);
805
806           /* We still have to add the cfi to the list so that lookup_cfa
807              works later on.  When -g2 and above we even need to force
808              emitting of CFI labels and add to list a DW_CFA_set_loc for
809              convert_cfa_to_fb_loc_list purposes.  If we're generating
810              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
811              convert_cfa_to_fb_loc_list.  */
812           if (dwarf_version == 2
813               && debug_info_level > DINFO_LEVEL_TERSE
814               && (write_symbols == DWARF2_DEBUG
815                   || write_symbols == VMS_AND_DWARF2_DEBUG))
816             {
817               switch (cfi->dw_cfi_opc)
818                 {
819                 case DW_CFA_def_cfa_offset:
820                 case DW_CFA_def_cfa_offset_sf:
821                 case DW_CFA_def_cfa_register:
822                 case DW_CFA_def_cfa:
823                 case DW_CFA_def_cfa_sf:
824                 case DW_CFA_def_cfa_expression:
825                 case DW_CFA_restore_state:
826                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
827                     label = dwarf2out_cfi_label (true);
828
829                   if (fde->dw_fde_current_label == NULL
830                       || strcmp (label, fde->dw_fde_current_label) != 0)
831                     {
832                       dw_cfi_ref xcfi;
833
834                       label = xstrdup (label);
835
836                       /* Set the location counter to the new label.  */
837                       xcfi = new_cfi ();
838                       /* It doesn't metter whether DW_CFA_set_loc
839                          or DW_CFA_advance_loc4 is added here, those aren't
840                          emitted into assembly, only looked up by
841                          convert_cfa_to_fb_loc_list.  */
842                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
843                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
844                       add_cfi (&fde->dw_fde_cfi, xcfi);
845                       fde->dw_fde_current_label = label;
846                     }
847                   break;
848                 default:
849                   break;
850                 }
851             }
852
853           output_cfi_directive (cfi);
854
855           list_head = &fde->dw_fde_cfi;
856         }
857       /* ??? If this is a CFI for the CIE, we don't emit.  This
858          assumes that the standard CIE contents that the assembler
859          uses matches the standard CIE contents that the compiler
860          uses.  This is probably a bad assumption.  I'm not quite
861          sure how to address this for now.  */
862     }
863   else if (label)
864     {
865       dw_fde_ref fde = current_fde ();
866
867       gcc_assert (fde != NULL);
868
869       if (*label == 0)
870         label = dwarf2out_cfi_label (false);
871
872       if (fde->dw_fde_current_label == NULL
873           || strcmp (label, fde->dw_fde_current_label) != 0)
874         {
875           dw_cfi_ref xcfi;
876
877           label = xstrdup (label);
878
879           /* Set the location counter to the new label.  */
880           xcfi = new_cfi ();
881           /* If we have a current label, advance from there, otherwise
882              set the location directly using set_loc.  */
883           xcfi->dw_cfi_opc = fde->dw_fde_current_label
884                              ? DW_CFA_advance_loc4
885                              : DW_CFA_set_loc;
886           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
887           add_cfi (&fde->dw_fde_cfi, xcfi);
888
889           fde->dw_fde_current_label = label;
890         }
891
892       list_head = &fde->dw_fde_cfi;
893     }
894
895   add_cfi (list_head, cfi);
896 }
897
898 /* Subroutine of lookup_cfa.  */
899
900 static void
901 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
902 {
903   switch (cfi->dw_cfi_opc)
904     {
905     case DW_CFA_def_cfa_offset:
906     case DW_CFA_def_cfa_offset_sf:
907       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
908       break;
909     case DW_CFA_def_cfa_register:
910       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
911       break;
912     case DW_CFA_def_cfa:
913     case DW_CFA_def_cfa_sf:
914       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
915       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
916       break;
917     case DW_CFA_def_cfa_expression:
918       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
919       break;
920
921     case DW_CFA_remember_state:
922       gcc_assert (!remember->in_use);
923       *remember = *loc;
924       remember->in_use = 1;
925       break;
926     case DW_CFA_restore_state:
927       gcc_assert (remember->in_use);
928       *loc = *remember;
929       remember->in_use = 0;
930       break;
931
932     default:
933       break;
934     }
935 }
936
937 /* Find the previous value for the CFA.  */
938
939 static void
940 lookup_cfa (dw_cfa_location *loc)
941 {
942   dw_cfi_ref cfi;
943   dw_fde_ref fde;
944   dw_cfa_location remember;
945
946   memset (loc, 0, sizeof (*loc));
947   loc->reg = INVALID_REGNUM;
948   remember = *loc;
949
950   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
951     lookup_cfa_1 (cfi, loc, &remember);
952
953   fde = current_fde ();
954   if (fde)
955     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
956       lookup_cfa_1 (cfi, loc, &remember);
957 }
958
959 /* The current rule for calculating the DWARF2 canonical frame address.  */
960 static dw_cfa_location cfa;
961
962 /* The register used for saving registers to the stack, and its offset
963    from the CFA.  */
964 static dw_cfa_location cfa_store;
965
966 /* The current save location around an epilogue.  */
967 static dw_cfa_location cfa_remember;
968
969 /* The running total of the size of arguments pushed onto the stack.  */
970 static HOST_WIDE_INT args_size;
971
972 /* The last args_size we actually output.  */
973 static HOST_WIDE_INT old_args_size;
974
975 /* Entry point to update the canonical frame address (CFA).
976    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
977    calculated from REG+OFFSET.  */
978
979 void
980 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
981 {
982   dw_cfa_location loc;
983   loc.indirect = 0;
984   loc.base_offset = 0;
985   loc.reg = reg;
986   loc.offset = offset;
987   def_cfa_1 (label, &loc);
988 }
989
990 /* Determine if two dw_cfa_location structures define the same data.  */
991
992 static bool
993 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
994 {
995   return (loc1->reg == loc2->reg
996           && loc1->offset == loc2->offset
997           && loc1->indirect == loc2->indirect
998           && (loc1->indirect == 0
999               || loc1->base_offset == loc2->base_offset));
1000 }
1001
1002 /* This routine does the actual work.  The CFA is now calculated from
1003    the dw_cfa_location structure.  */
1004
1005 static void
1006 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1007 {
1008   dw_cfi_ref cfi;
1009   dw_cfa_location old_cfa, loc;
1010
1011   cfa = *loc_p;
1012   loc = *loc_p;
1013
1014   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1015     cfa_store.offset = loc.offset;
1016
1017   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1018   lookup_cfa (&old_cfa);
1019
1020   /* If nothing changed, no need to issue any call frame instructions.  */
1021   if (cfa_equal_p (&loc, &old_cfa))
1022     return;
1023
1024   cfi = new_cfi ();
1025
1026   if (loc.reg == old_cfa.reg && !loc.indirect)
1027     {
1028       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1029          the CFA register did not change but the offset did.  The data 
1030          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1031          in the assembler via the .cfi_def_cfa_offset directive.  */
1032       if (loc.offset < 0)
1033         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1034       else
1035         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1036       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1037     }
1038
1039 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1040   else if (loc.offset == old_cfa.offset
1041            && old_cfa.reg != INVALID_REGNUM
1042            && !loc.indirect)
1043     {
1044       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1045          indicating the CFA register has changed to <register> but the
1046          offset has not changed.  */
1047       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1048       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1049     }
1050 #endif
1051
1052   else if (loc.indirect == 0)
1053     {
1054       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1055          indicating the CFA register has changed to <register> with
1056          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1057          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1058          directive.  */
1059       if (loc.offset < 0)
1060         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1061       else
1062         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1063       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1064       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1065     }
1066   else
1067     {
1068       /* Construct a DW_CFA_def_cfa_expression instruction to
1069          calculate the CFA using a full location expression since no
1070          register-offset pair is available.  */
1071       struct dw_loc_descr_struct *loc_list;
1072
1073       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1074       loc_list = build_cfa_loc (&loc, 0);
1075       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1076     }
1077
1078   add_fde_cfi (label, cfi);
1079 }
1080
1081 /* Add the CFI for saving a register.  REG is the CFA column number.
1082    LABEL is passed to add_fde_cfi.
1083    If SREG is -1, the register is saved at OFFSET from the CFA;
1084    otherwise it is saved in SREG.  */
1085
1086 static void
1087 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1088 {
1089   dw_cfi_ref cfi = new_cfi ();
1090   dw_fde_ref fde = current_fde ();
1091
1092   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1093
1094   /* When stack is aligned, store REG using DW_CFA_expression with
1095      FP.  */
1096   if (fde
1097       && fde->stack_realign
1098       && sreg == INVALID_REGNUM)
1099     {
1100       cfi->dw_cfi_opc = DW_CFA_expression;
1101       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1102       cfi->dw_cfi_oprnd1.dw_cfi_loc
1103         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1104     }
1105   else if (sreg == INVALID_REGNUM)
1106     {
1107       if (need_data_align_sf_opcode (offset))
1108         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1109       else if (reg & ~0x3f)
1110         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1111       else
1112         cfi->dw_cfi_opc = DW_CFA_offset;
1113       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1114     }
1115   else if (sreg == reg)
1116     cfi->dw_cfi_opc = DW_CFA_same_value;
1117   else
1118     {
1119       cfi->dw_cfi_opc = DW_CFA_register;
1120       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1121     }
1122
1123   add_fde_cfi (label, cfi);
1124 }
1125
1126 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1127    This CFI tells the unwinder that it needs to restore the window registers
1128    from the previous frame's window save area.
1129
1130    ??? Perhaps we should note in the CIE where windows are saved (instead of
1131    assuming 0(cfa)) and what registers are in the window.  */
1132
1133 void
1134 dwarf2out_window_save (const char *label)
1135 {
1136   dw_cfi_ref cfi = new_cfi ();
1137
1138   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1139   add_fde_cfi (label, cfi);
1140 }
1141
1142 /* Add a CFI to update the running total of the size of arguments
1143    pushed onto the stack.  */
1144
1145 void
1146 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1147 {
1148   dw_cfi_ref cfi;
1149
1150   if (size == old_args_size)
1151     return;
1152
1153   old_args_size = size;
1154
1155   cfi = new_cfi ();
1156   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1157   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1158   add_fde_cfi (label, cfi);
1159 }
1160
1161 /* Entry point for saving a register to the stack.  REG is the GCC register
1162    number.  LABEL and OFFSET are passed to reg_save.  */
1163
1164 void
1165 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1166 {
1167   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1168 }
1169
1170 /* Entry point for saving the return address in the stack.
1171    LABEL and OFFSET are passed to reg_save.  */
1172
1173 void
1174 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1175 {
1176   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1177 }
1178
1179 /* Entry point for saving the return address in a register.
1180    LABEL and SREG are passed to reg_save.  */
1181
1182 void
1183 dwarf2out_return_reg (const char *label, unsigned int sreg)
1184 {
1185   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1186 }
1187
1188 #ifdef DWARF2_UNWIND_INFO
1189 /* Record the initial position of the return address.  RTL is
1190    INCOMING_RETURN_ADDR_RTX.  */
1191
1192 static void
1193 initial_return_save (rtx rtl)
1194 {
1195   unsigned int reg = INVALID_REGNUM;
1196   HOST_WIDE_INT offset = 0;
1197
1198   switch (GET_CODE (rtl))
1199     {
1200     case REG:
1201       /* RA is in a register.  */
1202       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1203       break;
1204
1205     case MEM:
1206       /* RA is on the stack.  */
1207       rtl = XEXP (rtl, 0);
1208       switch (GET_CODE (rtl))
1209         {
1210         case REG:
1211           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1212           offset = 0;
1213           break;
1214
1215         case PLUS:
1216           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1217           offset = INTVAL (XEXP (rtl, 1));
1218           break;
1219
1220         case MINUS:
1221           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1222           offset = -INTVAL (XEXP (rtl, 1));
1223           break;
1224
1225         default:
1226           gcc_unreachable ();
1227         }
1228
1229       break;
1230
1231     case PLUS:
1232       /* The return address is at some offset from any value we can
1233          actually load.  For instance, on the SPARC it is in %i7+8. Just
1234          ignore the offset for now; it doesn't matter for unwinding frames.  */
1235       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1236       initial_return_save (XEXP (rtl, 0));
1237       return;
1238
1239     default:
1240       gcc_unreachable ();
1241     }
1242
1243   if (reg != DWARF_FRAME_RETURN_COLUMN)
1244     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1245 }
1246 #endif
1247
1248 /* Given a SET, calculate the amount of stack adjustment it
1249    contains.  */
1250
1251 static HOST_WIDE_INT
1252 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1253                      HOST_WIDE_INT cur_offset)
1254 {
1255   const_rtx src = SET_SRC (pattern);
1256   const_rtx dest = SET_DEST (pattern);
1257   HOST_WIDE_INT offset = 0;
1258   enum rtx_code code;
1259
1260   if (dest == stack_pointer_rtx)
1261     {
1262       code = GET_CODE (src);
1263
1264       /* Assume (set (reg sp) (reg whatever)) sets args_size
1265          level to 0.  */
1266       if (code == REG && src != stack_pointer_rtx)
1267         {
1268           offset = -cur_args_size;
1269 #ifndef STACK_GROWS_DOWNWARD
1270           offset = -offset;
1271 #endif
1272           return offset - cur_offset;
1273         }
1274
1275       if (! (code == PLUS || code == MINUS)
1276           || XEXP (src, 0) != stack_pointer_rtx
1277           || !CONST_INT_P (XEXP (src, 1)))
1278         return 0;
1279
1280       /* (set (reg sp) (plus (reg sp) (const_int))) */
1281       offset = INTVAL (XEXP (src, 1));
1282       if (code == PLUS)
1283         offset = -offset;
1284       return offset;
1285     }
1286
1287   if (MEM_P (src) && !MEM_P (dest))
1288     dest = src;
1289   if (MEM_P (dest))
1290     {
1291       /* (set (mem (pre_dec (reg sp))) (foo)) */
1292       src = XEXP (dest, 0);
1293       code = GET_CODE (src);
1294
1295       switch (code)
1296         {
1297         case PRE_MODIFY:
1298         case POST_MODIFY:
1299           if (XEXP (src, 0) == stack_pointer_rtx)
1300             {
1301               rtx val = XEXP (XEXP (src, 1), 1);
1302               /* We handle only adjustments by constant amount.  */
1303               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1304                           && CONST_INT_P (val));
1305               offset = -INTVAL (val);
1306               break;
1307             }
1308           return 0;
1309
1310         case PRE_DEC:
1311         case POST_DEC:
1312           if (XEXP (src, 0) == stack_pointer_rtx)
1313             {
1314               offset = GET_MODE_SIZE (GET_MODE (dest));
1315               break;
1316             }
1317           return 0;
1318
1319         case PRE_INC:
1320         case POST_INC:
1321           if (XEXP (src, 0) == stack_pointer_rtx)
1322             {
1323               offset = -GET_MODE_SIZE (GET_MODE (dest));
1324               break;
1325             }
1326           return 0;
1327
1328         default:
1329           return 0;
1330         }
1331     }
1332   else
1333     return 0;
1334
1335   return offset;
1336 }
1337
1338 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1339    indexed by INSN_UID.  */
1340
1341 static HOST_WIDE_INT *barrier_args_size;
1342
1343 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1344
1345 static HOST_WIDE_INT
1346 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1347                              VEC (rtx, heap) **next)
1348 {
1349   HOST_WIDE_INT offset = 0;
1350   int i;
1351
1352   if (! RTX_FRAME_RELATED_P (insn))
1353     {
1354       if (prologue_epilogue_contains (insn))
1355         /* Nothing */;
1356       else if (GET_CODE (PATTERN (insn)) == SET)
1357         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1358       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1359                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1360         {
1361           /* There may be stack adjustments inside compound insns.  Search
1362              for them.  */
1363           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1364             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1365               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1366                                              cur_args_size, offset);
1367         }
1368     }
1369   else
1370     {
1371       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1372
1373       if (expr)
1374         {
1375           expr = XEXP (expr, 0);
1376           if (GET_CODE (expr) == PARALLEL
1377               || GET_CODE (expr) == SEQUENCE)
1378             for (i = 1; i < XVECLEN (expr, 0); i++)
1379               {
1380                 rtx elem = XVECEXP (expr, 0, i);
1381
1382                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1383                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1384               }
1385         }
1386     }
1387
1388 #ifndef STACK_GROWS_DOWNWARD
1389   offset = -offset;
1390 #endif
1391
1392   cur_args_size += offset;
1393   if (cur_args_size < 0)
1394     cur_args_size = 0;
1395
1396   if (JUMP_P (insn))
1397     {
1398       rtx dest = JUMP_LABEL (insn);
1399
1400       if (dest)
1401         {
1402           if (barrier_args_size [INSN_UID (dest)] < 0)
1403             {
1404               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1405               VEC_safe_push (rtx, heap, *next, dest);
1406             }
1407         }
1408     }
1409
1410   return cur_args_size;
1411 }
1412
1413 /* Walk the whole function and compute args_size on BARRIERs.  */
1414
1415 static void
1416 compute_barrier_args_size (void)
1417 {
1418   int max_uid = get_max_uid (), i;
1419   rtx insn;
1420   VEC (rtx, heap) *worklist, *next, *tmp;
1421
1422   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1423   for (i = 0; i < max_uid; i++)
1424     barrier_args_size[i] = -1;
1425
1426   worklist = VEC_alloc (rtx, heap, 20);
1427   next = VEC_alloc (rtx, heap, 20);
1428   insn = get_insns ();
1429   barrier_args_size[INSN_UID (insn)] = 0;
1430   VEC_quick_push (rtx, worklist, insn);
1431   for (;;)
1432     {
1433       while (!VEC_empty (rtx, worklist))
1434         {
1435           rtx prev, body, first_insn;
1436           HOST_WIDE_INT cur_args_size;
1437
1438           first_insn = insn = VEC_pop (rtx, worklist);
1439           cur_args_size = barrier_args_size[INSN_UID (insn)];
1440           prev = prev_nonnote_insn (insn);
1441           if (prev && BARRIER_P (prev))
1442             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1443
1444           for (; insn; insn = NEXT_INSN (insn))
1445             {
1446               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1447                 continue;
1448               if (BARRIER_P (insn))
1449                 break;
1450
1451               if (LABEL_P (insn))
1452                 {
1453                   if (insn == first_insn)
1454                     continue;
1455                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1456                     {
1457                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1458                       continue;
1459                     }
1460                   else
1461                     {
1462                       /* The insns starting with this label have been
1463                          already scanned or are in the worklist.  */
1464                       break;
1465                     }
1466                 }
1467
1468               body = PATTERN (insn);
1469               if (GET_CODE (body) == SEQUENCE)
1470                 {
1471                   HOST_WIDE_INT dest_args_size = cur_args_size;
1472                   for (i = 1; i < XVECLEN (body, 0); i++)
1473                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1474                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1475                       dest_args_size
1476                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1477                                                        dest_args_size, &next);
1478                     else
1479                       cur_args_size
1480                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1481                                                        cur_args_size, &next);
1482
1483                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1484                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1485                                                  dest_args_size, &next);
1486                   else
1487                     cur_args_size
1488                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1489                                                      cur_args_size, &next);
1490                 }
1491               else
1492                 cur_args_size
1493                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1494             }
1495         }
1496
1497       if (VEC_empty (rtx, next))
1498         break;
1499
1500       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1501       tmp = next;
1502       next = worklist;
1503       worklist = tmp;
1504       VEC_truncate (rtx, next, 0);
1505     }
1506
1507   VEC_free (rtx, heap, worklist);
1508   VEC_free (rtx, heap, next);
1509 }
1510
1511
1512 /* Check INSN to see if it looks like a push or a stack adjustment, and
1513    make a note of it if it does.  EH uses this information to find out how
1514    much extra space it needs to pop off the stack.  */
1515
1516 static void
1517 dwarf2out_stack_adjust (rtx insn, bool after_p)
1518 {
1519   HOST_WIDE_INT offset;
1520   const char *label;
1521   int i;
1522
1523   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1524      with this function.  Proper support would require all frame-related
1525      insns to be marked, and to be able to handle saving state around
1526      epilogues textually in the middle of the function.  */
1527   if (prologue_epilogue_contains (insn))
1528     return;
1529
1530   /* If INSN is an instruction from target of an annulled branch, the
1531      effects are for the target only and so current argument size
1532      shouldn't change at all.  */
1533   if (final_sequence
1534       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1535       && INSN_FROM_TARGET_P (insn))
1536     return;
1537
1538   /* If only calls can throw, and we have a frame pointer,
1539      save up adjustments until we see the CALL_INSN.  */
1540   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1541     {
1542       if (CALL_P (insn) && !after_p)
1543         {
1544           /* Extract the size of the args from the CALL rtx itself.  */
1545           insn = PATTERN (insn);
1546           if (GET_CODE (insn) == PARALLEL)
1547             insn = XVECEXP (insn, 0, 0);
1548           if (GET_CODE (insn) == SET)
1549             insn = SET_SRC (insn);
1550           gcc_assert (GET_CODE (insn) == CALL);
1551           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1552         }
1553       return;
1554     }
1555
1556   if (CALL_P (insn) && !after_p)
1557     {
1558       if (!flag_asynchronous_unwind_tables)
1559         dwarf2out_args_size ("", args_size);
1560       return;
1561     }
1562   else if (BARRIER_P (insn))
1563     {
1564       /* Don't call compute_barrier_args_size () if the only
1565          BARRIER is at the end of function.  */
1566       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1567         compute_barrier_args_size ();
1568       if (barrier_args_size == NULL)
1569         offset = 0;
1570       else
1571         {
1572           offset = barrier_args_size[INSN_UID (insn)];
1573           if (offset < 0)
1574             offset = 0;
1575         }
1576
1577       offset -= args_size;
1578 #ifndef STACK_GROWS_DOWNWARD
1579       offset = -offset;
1580 #endif
1581     }
1582   else if (GET_CODE (PATTERN (insn)) == SET)
1583     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1584   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1585            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1586     {
1587       /* There may be stack adjustments inside compound insns.  Search
1588          for them.  */
1589       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1590         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1591           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1592                                          args_size, offset);
1593     }
1594   else
1595     return;
1596
1597   if (offset == 0)
1598     return;
1599
1600   label = dwarf2out_cfi_label (false);
1601   dwarf2out_args_size_adjust (offset, label);
1602 }
1603
1604 /* Adjust args_size based on stack adjustment OFFSET.  */
1605
1606 static void
1607 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1608 {
1609   if (cfa.reg == STACK_POINTER_REGNUM)
1610     cfa.offset += offset;
1611
1612   if (cfa_store.reg == STACK_POINTER_REGNUM)
1613     cfa_store.offset += offset;
1614
1615 #ifndef STACK_GROWS_DOWNWARD
1616   offset = -offset;
1617 #endif
1618
1619   args_size += offset;
1620   if (args_size < 0)
1621     args_size = 0;
1622
1623   def_cfa_1 (label, &cfa);
1624   if (flag_asynchronous_unwind_tables)
1625     dwarf2out_args_size (label, args_size);
1626 }
1627
1628 #endif
1629
1630 /* We delay emitting a register save until either (a) we reach the end
1631    of the prologue or (b) the register is clobbered.  This clusters
1632    register saves so that there are fewer pc advances.  */
1633
1634 struct GTY(()) queued_reg_save {
1635   struct queued_reg_save *next;
1636   rtx reg;
1637   HOST_WIDE_INT cfa_offset;
1638   rtx saved_reg;
1639 };
1640
1641 static GTY(()) struct queued_reg_save *queued_reg_saves;
1642
1643 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1644 struct GTY(()) reg_saved_in_data {
1645   rtx orig_reg;
1646   rtx saved_in_reg;
1647 };
1648
1649 /* A list of registers saved in other registers.
1650    The list intentionally has a small maximum capacity of 4; if your
1651    port needs more than that, you might consider implementing a
1652    more efficient data structure.  */
1653 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1654 static GTY(()) size_t num_regs_saved_in_regs;
1655
1656 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1657 static const char *last_reg_save_label;
1658
1659 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1660    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1661
1662 static void
1663 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1664 {
1665   struct queued_reg_save *q;
1666
1667   /* Duplicates waste space, but it's also necessary to remove them
1668      for correctness, since the queue gets output in reverse
1669      order.  */
1670   for (q = queued_reg_saves; q != NULL; q = q->next)
1671     if (REGNO (q->reg) == REGNO (reg))
1672       break;
1673
1674   if (q == NULL)
1675     {
1676       q = GGC_NEW (struct queued_reg_save);
1677       q->next = queued_reg_saves;
1678       queued_reg_saves = q;
1679     }
1680
1681   q->reg = reg;
1682   q->cfa_offset = offset;
1683   q->saved_reg = sreg;
1684
1685   last_reg_save_label = label;
1686 }
1687
1688 /* Output all the entries in QUEUED_REG_SAVES.  */
1689
1690 static void
1691 flush_queued_reg_saves (void)
1692 {
1693   struct queued_reg_save *q;
1694
1695   for (q = queued_reg_saves; q; q = q->next)
1696     {
1697       size_t i;
1698       unsigned int reg, sreg;
1699
1700       for (i = 0; i < num_regs_saved_in_regs; i++)
1701         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1702           break;
1703       if (q->saved_reg && i == num_regs_saved_in_regs)
1704         {
1705           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1706           num_regs_saved_in_regs++;
1707         }
1708       if (i != num_regs_saved_in_regs)
1709         {
1710           regs_saved_in_regs[i].orig_reg = q->reg;
1711           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1712         }
1713
1714       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1715       if (q->saved_reg)
1716         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1717       else
1718         sreg = INVALID_REGNUM;
1719       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1720     }
1721
1722   queued_reg_saves = NULL;
1723   last_reg_save_label = NULL;
1724 }
1725
1726 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1727    location for?  Or, does it clobber a register which we've previously
1728    said that some other register is saved in, and for which we now
1729    have a new location for?  */
1730
1731 static bool
1732 clobbers_queued_reg_save (const_rtx insn)
1733 {
1734   struct queued_reg_save *q;
1735
1736   for (q = queued_reg_saves; q; q = q->next)
1737     {
1738       size_t i;
1739       if (modified_in_p (q->reg, insn))
1740         return true;
1741       for (i = 0; i < num_regs_saved_in_regs; i++)
1742         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1743             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1744           return true;
1745     }
1746
1747   return false;
1748 }
1749
1750 /* Entry point for saving the first register into the second.  */
1751
1752 void
1753 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1754 {
1755   size_t i;
1756   unsigned int regno, sregno;
1757
1758   for (i = 0; i < num_regs_saved_in_regs; i++)
1759     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1760       break;
1761   if (i == num_regs_saved_in_regs)
1762     {
1763       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1764       num_regs_saved_in_regs++;
1765     }
1766   regs_saved_in_regs[i].orig_reg = reg;
1767   regs_saved_in_regs[i].saved_in_reg = sreg;
1768
1769   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1770   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1771   reg_save (label, regno, sregno, 0);
1772 }
1773
1774 /* What register, if any, is currently saved in REG?  */
1775
1776 static rtx
1777 reg_saved_in (rtx reg)
1778 {
1779   unsigned int regn = REGNO (reg);
1780   size_t i;
1781   struct queued_reg_save *q;
1782
1783   for (q = queued_reg_saves; q; q = q->next)
1784     if (q->saved_reg && regn == REGNO (q->saved_reg))
1785       return q->reg;
1786
1787   for (i = 0; i < num_regs_saved_in_regs; i++)
1788     if (regs_saved_in_regs[i].saved_in_reg
1789         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1790       return regs_saved_in_regs[i].orig_reg;
1791
1792   return NULL_RTX;
1793 }
1794
1795
1796 /* A temporary register holding an integral value used in adjusting SP
1797    or setting up the store_reg.  The "offset" field holds the integer
1798    value, not an offset.  */
1799 static dw_cfa_location cfa_temp;
1800
1801 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1802
1803 static void
1804 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1805 {
1806   memset (&cfa, 0, sizeof (cfa));
1807
1808   switch (GET_CODE (pat))
1809     {
1810     case PLUS:
1811       cfa.reg = REGNO (XEXP (pat, 0));
1812       cfa.offset = INTVAL (XEXP (pat, 1));
1813       break;
1814
1815     case REG:
1816       cfa.reg = REGNO (pat);
1817       break;
1818
1819     default:
1820       /* Recurse and define an expression.  */
1821       gcc_unreachable ();
1822     }
1823
1824   def_cfa_1 (label, &cfa);
1825 }
1826
1827 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1828
1829 static void
1830 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1831 {
1832   rtx src, dest;
1833
1834   gcc_assert (GET_CODE (pat) == SET);
1835   dest = XEXP (pat, 0);
1836   src = XEXP (pat, 1);
1837
1838   switch (GET_CODE (src))
1839     {
1840     case PLUS:
1841       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1842       cfa.offset -= INTVAL (XEXP (src, 1));
1843       break;
1844
1845     case REG:
1846         break;
1847
1848     default:
1849         gcc_unreachable ();
1850     }
1851
1852   cfa.reg = REGNO (dest);
1853   gcc_assert (cfa.indirect == 0);
1854
1855   def_cfa_1 (label, &cfa);
1856 }
1857
1858 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1859
1860 static void
1861 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1862 {
1863   HOST_WIDE_INT offset;
1864   rtx src, addr, span;
1865
1866   src = XEXP (set, 1);
1867   addr = XEXP (set, 0);
1868   gcc_assert (MEM_P (addr));
1869   addr = XEXP (addr, 0);
1870   
1871   /* As documented, only consider extremely simple addresses.  */
1872   switch (GET_CODE (addr))
1873     {
1874     case REG:
1875       gcc_assert (REGNO (addr) == cfa.reg);
1876       offset = -cfa.offset;
1877       break;
1878     case PLUS:
1879       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1880       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1881       break;
1882     default:
1883       gcc_unreachable ();
1884     }
1885
1886   span = targetm.dwarf_register_span (src);
1887
1888   /* ??? We'd like to use queue_reg_save, but we need to come up with
1889      a different flushing heuristic for epilogues.  */
1890   if (!span)
1891     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1892   else
1893     {
1894       /* We have a PARALLEL describing where the contents of SRC live.
1895          Queue register saves for each piece of the PARALLEL.  */
1896       int par_index;
1897       int limit;
1898       HOST_WIDE_INT span_offset = offset;
1899
1900       gcc_assert (GET_CODE (span) == PARALLEL);
1901
1902       limit = XVECLEN (span, 0);
1903       for (par_index = 0; par_index < limit; par_index++)
1904         {
1905           rtx elem = XVECEXP (span, 0, par_index);
1906
1907           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1908                     INVALID_REGNUM, span_offset);
1909           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1910         }
1911     }
1912 }
1913
1914 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1915
1916 static void
1917 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1918 {
1919   rtx src, dest;
1920   unsigned sregno, dregno;
1921
1922   src = XEXP (set, 1);
1923   dest = XEXP (set, 0);
1924
1925   if (src == pc_rtx)
1926     sregno = DWARF_FRAME_RETURN_COLUMN;
1927   else
1928     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1929
1930   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1931
1932   /* ??? We'd like to use queue_reg_save, but we need to come up with
1933      a different flushing heuristic for epilogues.  */
1934   reg_save (label, sregno, dregno, 0);
1935 }
1936
1937 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1938
1939 static void
1940 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1941 {
1942   dw_cfi_ref cfi = new_cfi ();
1943   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1944
1945   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1946   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1947
1948   add_fde_cfi (label, cfi);
1949 }
1950
1951 /* Record call frame debugging information for an expression EXPR,
1952    which either sets SP or FP (adjusting how we calculate the frame
1953    address) or saves a register to the stack or another register.
1954    LABEL indicates the address of EXPR.
1955
1956    This function encodes a state machine mapping rtxes to actions on
1957    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1958    users need not read the source code.
1959
1960   The High-Level Picture
1961
1962   Changes in the register we use to calculate the CFA: Currently we
1963   assume that if you copy the CFA register into another register, we
1964   should take the other one as the new CFA register; this seems to
1965   work pretty well.  If it's wrong for some target, it's simple
1966   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1967
1968   Changes in the register we use for saving registers to the stack:
1969   This is usually SP, but not always.  Again, we deduce that if you
1970   copy SP into another register (and SP is not the CFA register),
1971   then the new register is the one we will be using for register
1972   saves.  This also seems to work.
1973
1974   Register saves: There's not much guesswork about this one; if
1975   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1976   register save, and the register used to calculate the destination
1977   had better be the one we think we're using for this purpose.
1978   It's also assumed that a copy from a call-saved register to another
1979   register is saving that register if RTX_FRAME_RELATED_P is set on
1980   that instruction.  If the copy is from a call-saved register to
1981   the *same* register, that means that the register is now the same
1982   value as in the caller.
1983
1984   Except: If the register being saved is the CFA register, and the
1985   offset is nonzero, we are saving the CFA, so we assume we have to
1986   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1987   the intent is to save the value of SP from the previous frame.
1988
1989   In addition, if a register has previously been saved to a different
1990   register,
1991
1992   Invariants / Summaries of Rules
1993
1994   cfa          current rule for calculating the CFA.  It usually
1995                consists of a register and an offset.
1996   cfa_store    register used by prologue code to save things to the stack
1997                cfa_store.offset is the offset from the value of
1998                cfa_store.reg to the actual CFA
1999   cfa_temp     register holding an integral value.  cfa_temp.offset
2000                stores the value, which will be used to adjust the
2001                stack pointer.  cfa_temp is also used like cfa_store,
2002                to track stores to the stack via fp or a temp reg.
2003
2004   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2005                with cfa.reg as the first operand changes the cfa.reg and its
2006                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2007                cfa_temp.offset.
2008
2009   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2010                expression yielding a constant.  This sets cfa_temp.reg
2011                and cfa_temp.offset.
2012
2013   Rule 5:      Create a new register cfa_store used to save items to the
2014                stack.
2015
2016   Rules 10-14: Save a register to the stack.  Define offset as the
2017                difference of the original location and cfa_store's
2018                location (or cfa_temp's location if cfa_temp is used).
2019
2020   Rules 16-20: If AND operation happens on sp in prologue, we assume
2021                stack is realigned.  We will use a group of DW_OP_XXX
2022                expressions to represent the location of the stored
2023                register instead of CFA+offset.
2024
2025   The Rules
2026
2027   "{a,b}" indicates a choice of a xor b.
2028   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2029
2030   Rule 1:
2031   (set <reg1> <reg2>:cfa.reg)
2032   effects: cfa.reg = <reg1>
2033            cfa.offset unchanged
2034            cfa_temp.reg = <reg1>
2035            cfa_temp.offset = cfa.offset
2036
2037   Rule 2:
2038   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2039                               {<const_int>,<reg>:cfa_temp.reg}))
2040   effects: cfa.reg = sp if fp used
2041            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2042            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2043              if cfa_store.reg==sp
2044
2045   Rule 3:
2046   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2047   effects: cfa.reg = fp
2048            cfa_offset += +/- <const_int>
2049
2050   Rule 4:
2051   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2052   constraints: <reg1> != fp
2053                <reg1> != sp
2054   effects: cfa.reg = <reg1>
2055            cfa_temp.reg = <reg1>
2056            cfa_temp.offset = cfa.offset
2057
2058   Rule 5:
2059   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2060   constraints: <reg1> != fp
2061                <reg1> != sp
2062   effects: cfa_store.reg = <reg1>
2063            cfa_store.offset = cfa.offset - cfa_temp.offset
2064
2065   Rule 6:
2066   (set <reg> <const_int>)
2067   effects: cfa_temp.reg = <reg>
2068            cfa_temp.offset = <const_int>
2069
2070   Rule 7:
2071   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2072   effects: cfa_temp.reg = <reg1>
2073            cfa_temp.offset |= <const_int>
2074
2075   Rule 8:
2076   (set <reg> (high <exp>))
2077   effects: none
2078
2079   Rule 9:
2080   (set <reg> (lo_sum <exp> <const_int>))
2081   effects: cfa_temp.reg = <reg>
2082            cfa_temp.offset = <const_int>
2083
2084   Rule 10:
2085   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2086   effects: cfa_store.offset -= <const_int>
2087            cfa.offset = cfa_store.offset if cfa.reg == sp
2088            cfa.reg = sp
2089            cfa.base_offset = -cfa_store.offset
2090
2091   Rule 11:
2092   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2093   effects: cfa_store.offset += -/+ mode_size(mem)
2094            cfa.offset = cfa_store.offset if cfa.reg == sp
2095            cfa.reg = sp
2096            cfa.base_offset = -cfa_store.offset
2097
2098   Rule 12:
2099   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2100
2101        <reg2>)
2102   effects: cfa.reg = <reg1>
2103            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2104
2105   Rule 13:
2106   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2107   effects: cfa.reg = <reg1>
2108            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2109
2110   Rule 14:
2111   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2112   effects: cfa.reg = <reg1>
2113            cfa.base_offset = -cfa_temp.offset
2114            cfa_temp.offset -= mode_size(mem)
2115
2116   Rule 15:
2117   (set <reg> {unspec, unspec_volatile})
2118   effects: target-dependent
2119
2120   Rule 16:
2121   (set sp (and: sp <const_int>))
2122   constraints: cfa_store.reg == sp
2123   effects: current_fde.stack_realign = 1
2124            cfa_store.offset = 0
2125            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2126
2127   Rule 17:
2128   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2129   effects: cfa_store.offset += -/+ mode_size(mem)
2130
2131   Rule 18:
2132   (set (mem ({pre_inc, pre_dec} sp)) fp)
2133   constraints: fde->stack_realign == 1
2134   effects: cfa_store.offset = 0
2135            cfa.reg != HARD_FRAME_POINTER_REGNUM
2136
2137   Rule 19:
2138   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2139   constraints: fde->stack_realign == 1
2140                && cfa.offset == 0
2141                && cfa.indirect == 0
2142                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2143   effects: Use DW_CFA_def_cfa_expression to define cfa
2144            cfa.reg == fde->drap_reg
2145
2146   Rule 20:
2147   (set reg fde->drap_reg)
2148   constraints: fde->vdrap_reg == INVALID_REGNUM
2149   effects: fde->vdrap_reg = reg.
2150   (set mem fde->drap_reg)
2151   constraints: fde->drap_reg_saved == 1
2152   effects: none.  */
2153
2154 static void
2155 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2156 {
2157   rtx src, dest, span;
2158   HOST_WIDE_INT offset;
2159   dw_fde_ref fde;
2160
2161   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2162      the PARALLEL independently. The first element is always processed if
2163      it is a SET. This is for backward compatibility.   Other elements
2164      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2165      flag is set in them.  */
2166   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2167     {
2168       int par_index;
2169       int limit = XVECLEN (expr, 0);
2170       rtx elem;
2171
2172       /* PARALLELs have strict read-modify-write semantics, so we
2173          ought to evaluate every rvalue before changing any lvalue.
2174          It's cumbersome to do that in general, but there's an
2175          easy approximation that is enough for all current users:
2176          handle register saves before register assignments.  */
2177       if (GET_CODE (expr) == PARALLEL)
2178         for (par_index = 0; par_index < limit; par_index++)
2179           {
2180             elem = XVECEXP (expr, 0, par_index);
2181             if (GET_CODE (elem) == SET
2182                 && MEM_P (SET_DEST (elem))
2183                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2184               dwarf2out_frame_debug_expr (elem, label);
2185           }
2186
2187       for (par_index = 0; par_index < limit; par_index++)
2188         {
2189           elem = XVECEXP (expr, 0, par_index);
2190           if (GET_CODE (elem) == SET
2191               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2192               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2193             dwarf2out_frame_debug_expr (elem, label);
2194           else if (GET_CODE (elem) == SET
2195                    && par_index != 0
2196                    && !RTX_FRAME_RELATED_P (elem))
2197             {
2198               /* Stack adjustment combining might combine some post-prologue
2199                  stack adjustment into a prologue stack adjustment.  */
2200               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2201
2202               if (offset != 0)
2203                 dwarf2out_args_size_adjust (offset, label);
2204             }
2205         }
2206       return;
2207     }
2208
2209   gcc_assert (GET_CODE (expr) == SET);
2210
2211   src = SET_SRC (expr);
2212   dest = SET_DEST (expr);
2213
2214   if (REG_P (src))
2215     {
2216       rtx rsi = reg_saved_in (src);
2217       if (rsi)
2218         src = rsi;
2219     }
2220
2221   fde = current_fde ();
2222
2223   if (REG_P (src)
2224       && fde
2225       && fde->drap_reg == REGNO (src)
2226       && (fde->drap_reg_saved
2227           || REG_P (dest)))
2228     {
2229       /* Rule 20 */
2230       /* If we are saving dynamic realign argument pointer to a
2231          register, the destination is virtual dynamic realign
2232          argument pointer.  It may be used to access argument.  */
2233       if (REG_P (dest))
2234         {
2235           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2236           fde->vdrap_reg = REGNO (dest);
2237         }
2238       return;
2239     }
2240
2241   switch (GET_CODE (dest))
2242     {
2243     case REG:
2244       switch (GET_CODE (src))
2245         {
2246           /* Setting FP from SP.  */
2247         case REG:
2248           if (cfa.reg == (unsigned) REGNO (src))
2249             {
2250               /* Rule 1 */
2251               /* Update the CFA rule wrt SP or FP.  Make sure src is
2252                  relative to the current CFA register.
2253
2254                  We used to require that dest be either SP or FP, but the
2255                  ARM copies SP to a temporary register, and from there to
2256                  FP.  So we just rely on the backends to only set
2257                  RTX_FRAME_RELATED_P on appropriate insns.  */
2258               cfa.reg = REGNO (dest);
2259               cfa_temp.reg = cfa.reg;
2260               cfa_temp.offset = cfa.offset;
2261             }
2262           else
2263             {
2264               /* Saving a register in a register.  */
2265               gcc_assert (!fixed_regs [REGNO (dest)]
2266                           /* For the SPARC and its register window.  */
2267                           || (DWARF_FRAME_REGNUM (REGNO (src))
2268                               == DWARF_FRAME_RETURN_COLUMN));
2269
2270               /* After stack is aligned, we can only save SP in FP
2271                  if drap register is used.  In this case, we have
2272                  to restore stack pointer with the CFA value and we
2273                  don't generate this DWARF information.  */
2274               if (fde
2275                   && fde->stack_realign
2276                   && REGNO (src) == STACK_POINTER_REGNUM)
2277                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2278                             && fde->drap_reg != INVALID_REGNUM
2279                             && cfa.reg != REGNO (src));
2280               else
2281                 queue_reg_save (label, src, dest, 0);
2282             }
2283           break;
2284
2285         case PLUS:
2286         case MINUS:
2287         case LO_SUM:
2288           if (dest == stack_pointer_rtx)
2289             {
2290               /* Rule 2 */
2291               /* Adjusting SP.  */
2292               switch (GET_CODE (XEXP (src, 1)))
2293                 {
2294                 case CONST_INT:
2295                   offset = INTVAL (XEXP (src, 1));
2296                   break;
2297                 case REG:
2298                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2299                               == cfa_temp.reg);
2300                   offset = cfa_temp.offset;
2301                   break;
2302                 default:
2303                   gcc_unreachable ();
2304                 }
2305
2306               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2307                 {
2308                   /* Restoring SP from FP in the epilogue.  */
2309                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2310                   cfa.reg = STACK_POINTER_REGNUM;
2311                 }
2312               else if (GET_CODE (src) == LO_SUM)
2313                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2314                 ;
2315               else
2316                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2317
2318               if (GET_CODE (src) != MINUS)
2319                 offset = -offset;
2320               if (cfa.reg == STACK_POINTER_REGNUM)
2321                 cfa.offset += offset;
2322               if (cfa_store.reg == STACK_POINTER_REGNUM)
2323                 cfa_store.offset += offset;
2324             }
2325           else if (dest == hard_frame_pointer_rtx)
2326             {
2327               /* Rule 3 */
2328               /* Either setting the FP from an offset of the SP,
2329                  or adjusting the FP */
2330               gcc_assert (frame_pointer_needed);
2331
2332               gcc_assert (REG_P (XEXP (src, 0))
2333                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2334                           && CONST_INT_P (XEXP (src, 1)));
2335               offset = INTVAL (XEXP (src, 1));
2336               if (GET_CODE (src) != MINUS)
2337                 offset = -offset;
2338               cfa.offset += offset;
2339               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2340             }
2341           else
2342             {
2343               gcc_assert (GET_CODE (src) != MINUS);
2344
2345               /* Rule 4 */
2346               if (REG_P (XEXP (src, 0))
2347                   && REGNO (XEXP (src, 0)) == cfa.reg
2348                   && CONST_INT_P (XEXP (src, 1)))
2349                 {
2350                   /* Setting a temporary CFA register that will be copied
2351                      into the FP later on.  */
2352                   offset = - INTVAL (XEXP (src, 1));
2353                   cfa.offset += offset;
2354                   cfa.reg = REGNO (dest);
2355                   /* Or used to save regs to the stack.  */
2356                   cfa_temp.reg = cfa.reg;
2357                   cfa_temp.offset = cfa.offset;
2358                 }
2359
2360               /* Rule 5 */
2361               else if (REG_P (XEXP (src, 0))
2362                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2363                        && XEXP (src, 1) == stack_pointer_rtx)
2364                 {
2365                   /* Setting a scratch register that we will use instead
2366                      of SP for saving registers to the stack.  */
2367                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2368                   cfa_store.reg = REGNO (dest);
2369                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2370                 }
2371
2372               /* Rule 9 */
2373               else if (GET_CODE (src) == LO_SUM
2374                        && CONST_INT_P (XEXP (src, 1)))
2375                 {
2376                   cfa_temp.reg = REGNO (dest);
2377                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2378                 }
2379               else
2380                 gcc_unreachable ();
2381             }
2382           break;
2383
2384           /* Rule 6 */
2385         case CONST_INT:
2386           cfa_temp.reg = REGNO (dest);
2387           cfa_temp.offset = INTVAL (src);
2388           break;
2389
2390           /* Rule 7 */
2391         case IOR:
2392           gcc_assert (REG_P (XEXP (src, 0))
2393                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2394                       && CONST_INT_P (XEXP (src, 1)));
2395
2396           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2397             cfa_temp.reg = REGNO (dest);
2398           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2399           break;
2400
2401           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2402              which will fill in all of the bits.  */
2403           /* Rule 8 */
2404         case HIGH:
2405           break;
2406
2407           /* Rule 15 */
2408         case UNSPEC:
2409         case UNSPEC_VOLATILE:
2410           gcc_assert (targetm.dwarf_handle_frame_unspec);
2411           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2412           return;
2413
2414           /* Rule 16 */
2415         case AND:
2416           /* If this AND operation happens on stack pointer in prologue,
2417              we assume the stack is realigned and we extract the
2418              alignment.  */
2419           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2420             {
2421               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2422               fde->stack_realign = 1;
2423               fde->stack_realignment = INTVAL (XEXP (src, 1));
2424               cfa_store.offset = 0;
2425
2426               if (cfa.reg != STACK_POINTER_REGNUM
2427                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2428                 fde->drap_reg = cfa.reg;
2429             }
2430           return;
2431
2432         default:
2433           gcc_unreachable ();
2434         }
2435
2436       def_cfa_1 (label, &cfa);
2437       break;
2438
2439     case MEM:
2440
2441       /* Saving a register to the stack.  Make sure dest is relative to the
2442          CFA register.  */
2443       switch (GET_CODE (XEXP (dest, 0)))
2444         {
2445           /* Rule 10 */
2446           /* With a push.  */
2447         case PRE_MODIFY:
2448           /* We can't handle variable size modifications.  */
2449           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2450                       == CONST_INT);
2451           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2452
2453           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2454                       && cfa_store.reg == STACK_POINTER_REGNUM);
2455
2456           cfa_store.offset += offset;
2457           if (cfa.reg == STACK_POINTER_REGNUM)
2458             cfa.offset = cfa_store.offset;
2459
2460           offset = -cfa_store.offset;
2461           break;
2462
2463           /* Rule 11 */
2464         case PRE_INC:
2465         case PRE_DEC:
2466           offset = GET_MODE_SIZE (GET_MODE (dest));
2467           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2468             offset = -offset;
2469
2470           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2471                        == STACK_POINTER_REGNUM)
2472                       && cfa_store.reg == STACK_POINTER_REGNUM);
2473
2474           cfa_store.offset += offset;
2475
2476           /* Rule 18: If stack is aligned, we will use FP as a
2477              reference to represent the address of the stored
2478              regiser.  */
2479           if (fde
2480               && fde->stack_realign
2481               && src == hard_frame_pointer_rtx)
2482             {
2483               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2484               cfa_store.offset = 0;
2485             }
2486
2487           if (cfa.reg == STACK_POINTER_REGNUM)
2488             cfa.offset = cfa_store.offset;
2489
2490           offset = -cfa_store.offset;
2491           break;
2492
2493           /* Rule 12 */
2494           /* With an offset.  */
2495         case PLUS:
2496         case MINUS:
2497         case LO_SUM:
2498           {
2499             int regno;
2500
2501             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2502                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2503             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2504             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2505               offset = -offset;
2506
2507             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2508
2509             if (cfa_store.reg == (unsigned) regno)
2510               offset -= cfa_store.offset;
2511             else
2512               {
2513                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2514                 offset -= cfa_temp.offset;
2515               }
2516           }
2517           break;
2518
2519           /* Rule 13 */
2520           /* Without an offset.  */
2521         case REG:
2522           {
2523             int regno = REGNO (XEXP (dest, 0));
2524
2525             if (cfa_store.reg == (unsigned) regno)
2526               offset = -cfa_store.offset;
2527             else
2528               {
2529                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2530                 offset = -cfa_temp.offset;
2531               }
2532           }
2533           break;
2534
2535           /* Rule 14 */
2536         case POST_INC:
2537           gcc_assert (cfa_temp.reg
2538                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2539           offset = -cfa_temp.offset;
2540           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2541           break;
2542
2543         default:
2544           gcc_unreachable ();
2545         }
2546
2547         /* Rule 17 */
2548         /* If the source operand of this MEM operation is not a
2549            register, basically the source is return address.  Here
2550            we only care how much stack grew and we don't save it.  */
2551       if (!REG_P (src))
2552         break;
2553
2554       if (REGNO (src) != STACK_POINTER_REGNUM
2555           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2556           && (unsigned) REGNO (src) == cfa.reg)
2557         {
2558           /* We're storing the current CFA reg into the stack.  */
2559
2560           if (cfa.offset == 0)
2561             {
2562               /* Rule 19 */
2563               /* If stack is aligned, putting CFA reg into stack means
2564                  we can no longer use reg + offset to represent CFA.
2565                  Here we use DW_CFA_def_cfa_expression instead.  The
2566                  result of this expression equals to the original CFA
2567                  value.  */
2568               if (fde
2569                   && fde->stack_realign
2570                   && cfa.indirect == 0
2571                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2572                 {
2573                   dw_cfa_location cfa_exp;
2574
2575                   gcc_assert (fde->drap_reg == cfa.reg);
2576
2577                   cfa_exp.indirect = 1;
2578                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2579                   cfa_exp.base_offset = offset;
2580                   cfa_exp.offset = 0;
2581
2582                   fde->drap_reg_saved = 1;
2583
2584                   def_cfa_1 (label, &cfa_exp);
2585                   break;
2586                 }
2587
2588               /* If the source register is exactly the CFA, assume
2589                  we're saving SP like any other register; this happens
2590                  on the ARM.  */
2591               def_cfa_1 (label, &cfa);
2592               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2593               break;
2594             }
2595           else
2596             {
2597               /* Otherwise, we'll need to look in the stack to
2598                  calculate the CFA.  */
2599               rtx x = XEXP (dest, 0);
2600
2601               if (!REG_P (x))
2602                 x = XEXP (x, 0);
2603               gcc_assert (REG_P (x));
2604
2605               cfa.reg = REGNO (x);
2606               cfa.base_offset = offset;
2607               cfa.indirect = 1;
2608               def_cfa_1 (label, &cfa);
2609               break;
2610             }
2611         }
2612
2613       def_cfa_1 (label, &cfa);
2614       {
2615         span = targetm.dwarf_register_span (src);
2616
2617         if (!span)
2618           queue_reg_save (label, src, NULL_RTX, offset);
2619         else
2620           {
2621             /* We have a PARALLEL describing where the contents of SRC
2622                live.  Queue register saves for each piece of the
2623                PARALLEL.  */
2624             int par_index;
2625             int limit;
2626             HOST_WIDE_INT span_offset = offset;
2627
2628             gcc_assert (GET_CODE (span) == PARALLEL);
2629
2630             limit = XVECLEN (span, 0);
2631             for (par_index = 0; par_index < limit; par_index++)
2632               {
2633                 rtx elem = XVECEXP (span, 0, par_index);
2634
2635                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2636                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2637               }
2638           }
2639       }
2640       break;
2641
2642     default:
2643       gcc_unreachable ();
2644     }
2645 }
2646
2647 /* Record call frame debugging information for INSN, which either
2648    sets SP or FP (adjusting how we calculate the frame address) or saves a
2649    register to the stack.  If INSN is NULL_RTX, initialize our state.
2650
2651    If AFTER_P is false, we're being called before the insn is emitted,
2652    otherwise after.  Call instructions get invoked twice.  */
2653
2654 void
2655 dwarf2out_frame_debug (rtx insn, bool after_p)
2656 {
2657   const char *label;
2658   rtx note, n;
2659   bool handled_one = false;
2660
2661   if (insn == NULL_RTX)
2662     {
2663       size_t i;
2664
2665       /* Flush any queued register saves.  */
2666       flush_queued_reg_saves ();
2667
2668       /* Set up state for generating call frame debug info.  */
2669       lookup_cfa (&cfa);
2670       gcc_assert (cfa.reg
2671                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2672
2673       cfa.reg = STACK_POINTER_REGNUM;
2674       cfa_store = cfa;
2675       cfa_temp.reg = -1;
2676       cfa_temp.offset = 0;
2677
2678       for (i = 0; i < num_regs_saved_in_regs; i++)
2679         {
2680           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2681           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2682         }
2683       num_regs_saved_in_regs = 0;
2684
2685       if (barrier_args_size)
2686         {
2687           XDELETEVEC (barrier_args_size);
2688           barrier_args_size = NULL;
2689         }
2690       return;
2691     }
2692
2693   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2694     flush_queued_reg_saves ();
2695
2696   if (! RTX_FRAME_RELATED_P (insn))
2697     {
2698       if (!ACCUMULATE_OUTGOING_ARGS)
2699         dwarf2out_stack_adjust (insn, after_p);
2700       return;
2701     }
2702
2703   label = dwarf2out_cfi_label (false);
2704
2705   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2706     switch (REG_NOTE_KIND (note))
2707       {
2708       case REG_FRAME_RELATED_EXPR:
2709         insn = XEXP (note, 0);
2710         goto found;
2711
2712       case REG_CFA_DEF_CFA:
2713         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2714         handled_one = true;
2715         break;
2716
2717       case REG_CFA_ADJUST_CFA:
2718         n = XEXP (note, 0);
2719         if (n == NULL)
2720           {
2721             n = PATTERN (insn);
2722             if (GET_CODE (n) == PARALLEL)
2723               n = XVECEXP (n, 0, 0);
2724           }
2725         dwarf2out_frame_debug_adjust_cfa (n, label);
2726         handled_one = true;
2727         break;
2728
2729       case REG_CFA_OFFSET:
2730         n = XEXP (note, 0);
2731         if (n == NULL)
2732           n = single_set (insn);
2733         dwarf2out_frame_debug_cfa_offset (n, label);
2734         handled_one = true;
2735         break;
2736
2737       case REG_CFA_REGISTER:
2738         n = XEXP (note, 0);
2739         if (n == NULL)
2740           {
2741             n = PATTERN (insn);
2742             if (GET_CODE (n) == PARALLEL)
2743               n = XVECEXP (n, 0, 0);
2744           }
2745         dwarf2out_frame_debug_cfa_register (n, label);
2746         handled_one = true;
2747         break;
2748
2749       case REG_CFA_RESTORE:
2750         n = XEXP (note, 0);
2751         if (n == NULL)
2752           {
2753             n = PATTERN (insn);
2754             if (GET_CODE (n) == PARALLEL)
2755               n = XVECEXP (n, 0, 0);
2756             n = XEXP (n, 0);
2757           }
2758         dwarf2out_frame_debug_cfa_restore (n, label);
2759         handled_one = true;
2760         break;
2761
2762       default:
2763         break;
2764       }
2765   if (handled_one)
2766     return;
2767
2768   insn = PATTERN (insn);
2769  found:
2770   dwarf2out_frame_debug_expr (insn, label);
2771 }
2772
2773 /* Determine if we need to save and restore CFI information around this
2774    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2775    we do need to save/restore, then emit the save now, and insert a
2776    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2777
2778 void
2779 dwarf2out_begin_epilogue (rtx insn)
2780 {
2781   bool saw_frp = false;
2782   rtx i;
2783
2784   /* Scan forward to the return insn, noticing if there are possible
2785      frame related insns.  */
2786   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2787     {
2788       if (!INSN_P (i))
2789         continue;
2790
2791       /* Look for both regular and sibcalls to end the block.  */
2792       if (returnjump_p (i))
2793         break;
2794       if (CALL_P (i) && SIBLING_CALL_P (i))
2795         break;
2796
2797       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2798         {
2799           int idx;
2800           rtx seq = PATTERN (i);
2801
2802           if (returnjump_p (XVECEXP (seq, 0, 0)))
2803             break;
2804           if (CALL_P (XVECEXP (seq, 0, 0))
2805               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2806             break;
2807
2808           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2809             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2810               saw_frp = true;
2811         }
2812
2813       if (RTX_FRAME_RELATED_P (i))
2814         saw_frp = true;
2815     }
2816
2817   /* If the port doesn't emit epilogue unwind info, we don't need a
2818      save/restore pair.  */
2819   if (!saw_frp)
2820     return;
2821
2822   /* Otherwise, search forward to see if the return insn was the last
2823      basic block of the function.  If so, we don't need save/restore.  */
2824   gcc_assert (i != NULL);
2825   i = next_real_insn (i);
2826   if (i == NULL)
2827     return;
2828
2829   /* Insert the restore before that next real insn in the stream, and before
2830      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2831      properly nested.  This should be after any label or alignment.  This
2832      will be pushed into the CFI stream by the function below.  */
2833   while (1)
2834     {
2835       rtx p = PREV_INSN (i);
2836       if (!NOTE_P (p))
2837         break;
2838       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2839         break;
2840       i = p;
2841     }
2842   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2843
2844   emit_cfa_remember = true;
2845
2846   /* And emulate the state save.  */
2847   gcc_assert (!cfa_remember.in_use);
2848   cfa_remember = cfa;
2849   cfa_remember.in_use = 1;
2850 }
2851
2852 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2853
2854 void
2855 dwarf2out_frame_debug_restore_state (void)
2856 {
2857   dw_cfi_ref cfi = new_cfi (); 
2858   const char *label = dwarf2out_cfi_label (false);
2859
2860   cfi->dw_cfi_opc = DW_CFA_restore_state;
2861   add_fde_cfi (label, cfi);
2862
2863   gcc_assert (cfa_remember.in_use);
2864   cfa = cfa_remember;
2865   cfa_remember.in_use = 0;
2866 }
2867
2868 #endif
2869
2870 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2871 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2872  (enum dwarf_call_frame_info cfi);
2873
2874 static enum dw_cfi_oprnd_type
2875 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2876 {
2877   switch (cfi)
2878     {
2879     case DW_CFA_nop:
2880     case DW_CFA_GNU_window_save:
2881     case DW_CFA_remember_state:
2882     case DW_CFA_restore_state:
2883       return dw_cfi_oprnd_unused;
2884
2885     case DW_CFA_set_loc:
2886     case DW_CFA_advance_loc1:
2887     case DW_CFA_advance_loc2:
2888     case DW_CFA_advance_loc4:
2889     case DW_CFA_MIPS_advance_loc8:
2890       return dw_cfi_oprnd_addr;
2891
2892     case DW_CFA_offset:
2893     case DW_CFA_offset_extended:
2894     case DW_CFA_def_cfa:
2895     case DW_CFA_offset_extended_sf:
2896     case DW_CFA_def_cfa_sf:
2897     case DW_CFA_restore:
2898     case DW_CFA_restore_extended:
2899     case DW_CFA_undefined:
2900     case DW_CFA_same_value:
2901     case DW_CFA_def_cfa_register:
2902     case DW_CFA_register:
2903       return dw_cfi_oprnd_reg_num;
2904
2905     case DW_CFA_def_cfa_offset:
2906     case DW_CFA_GNU_args_size:
2907     case DW_CFA_def_cfa_offset_sf:
2908       return dw_cfi_oprnd_offset;
2909
2910     case DW_CFA_def_cfa_expression:
2911     case DW_CFA_expression:
2912       return dw_cfi_oprnd_loc;
2913
2914     default:
2915       gcc_unreachable ();
2916     }
2917 }
2918
2919 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2920 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2921  (enum dwarf_call_frame_info cfi);
2922
2923 static enum dw_cfi_oprnd_type
2924 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2925 {
2926   switch (cfi)
2927     {
2928     case DW_CFA_def_cfa:
2929     case DW_CFA_def_cfa_sf:
2930     case DW_CFA_offset:
2931     case DW_CFA_offset_extended_sf:
2932     case DW_CFA_offset_extended:
2933       return dw_cfi_oprnd_offset;
2934
2935     case DW_CFA_register:
2936       return dw_cfi_oprnd_reg_num;
2937
2938     default:
2939       return dw_cfi_oprnd_unused;
2940     }
2941 }
2942
2943 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2944
2945 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2946    switch to the data section instead, and write out a synthetic start label
2947    for collect2 the first time around.  */
2948
2949 static void
2950 switch_to_eh_frame_section (bool back)
2951 {
2952   tree label;
2953
2954 #ifdef EH_FRAME_SECTION_NAME
2955   if (eh_frame_section == 0)
2956     {
2957       int flags;
2958
2959       if (EH_TABLES_CAN_BE_READ_ONLY)
2960         {
2961           int fde_encoding;
2962           int per_encoding;
2963           int lsda_encoding;
2964
2965           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2966                                                        /*global=*/0);
2967           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2968                                                        /*global=*/1);
2969           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2970                                                         /*global=*/0);
2971           flags = ((! flag_pic
2972                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2973                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2974                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2975                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2976                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2977                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2978                    ? 0 : SECTION_WRITE);
2979         }
2980       else
2981         flags = SECTION_WRITE;
2982       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2983     }
2984 #endif
2985
2986   if (eh_frame_section)
2987     switch_to_section (eh_frame_section);
2988   else
2989     {
2990       /* We have no special eh_frame section.  Put the information in
2991          the data section and emit special labels to guide collect2.  */
2992       switch_to_section (data_section);
2993
2994       if (!back)
2995         {
2996           label = get_file_function_name ("F");
2997           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2998           targetm.asm_out.globalize_label (asm_out_file,
2999                                            IDENTIFIER_POINTER (label));
3000           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3001         }
3002     }
3003 }
3004
3005 /* Switch [BACK] to the eh or debug frame table section, depending on
3006    FOR_EH.  */
3007
3008 static void
3009 switch_to_frame_table_section (int for_eh, bool back)
3010 {
3011   if (for_eh)
3012     switch_to_eh_frame_section (back);
3013   else
3014     {
3015       if (!debug_frame_section)
3016         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3017                                            SECTION_DEBUG, NULL);
3018       switch_to_section (debug_frame_section);
3019     }
3020 }
3021
3022 /* Output a Call Frame Information opcode and its operand(s).  */
3023
3024 static void
3025 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3026 {
3027   unsigned long r;
3028   HOST_WIDE_INT off;
3029
3030   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3031     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3032                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3033                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3034                          ((unsigned HOST_WIDE_INT)
3035                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3036   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3037     {
3038       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3039       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3040                            "DW_CFA_offset, column 0x%lx", r);
3041       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3042       dw2_asm_output_data_uleb128 (off, NULL);
3043     }
3044   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3045     {
3046       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3047       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3048                            "DW_CFA_restore, column 0x%lx", r);
3049     }
3050   else
3051     {
3052       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3053                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3054
3055       switch (cfi->dw_cfi_opc)
3056         {
3057         case DW_CFA_set_loc:
3058           if (for_eh)
3059             dw2_asm_output_encoded_addr_rtx (
3060                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3061                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3062                 false, NULL);
3063           else
3064             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3065                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3066           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3067           break;
3068
3069         case DW_CFA_advance_loc1:
3070           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3071                                 fde->dw_fde_current_label, NULL);
3072           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3073           break;
3074
3075         case DW_CFA_advance_loc2:
3076           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3077                                 fde->dw_fde_current_label, NULL);
3078           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3079           break;
3080
3081         case DW_CFA_advance_loc4:
3082           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3083                                 fde->dw_fde_current_label, NULL);
3084           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3085           break;
3086
3087         case DW_CFA_MIPS_advance_loc8:
3088           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3089                                 fde->dw_fde_current_label, NULL);
3090           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3091           break;
3092
3093         case DW_CFA_offset_extended:
3094           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3095           dw2_asm_output_data_uleb128 (r, NULL);
3096           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3097           dw2_asm_output_data_uleb128 (off, NULL);
3098           break;
3099
3100         case DW_CFA_def_cfa:
3101           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3102           dw2_asm_output_data_uleb128 (r, NULL);
3103           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3104           break;
3105
3106         case DW_CFA_offset_extended_sf:
3107           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3108           dw2_asm_output_data_uleb128 (r, NULL);
3109           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3110           dw2_asm_output_data_sleb128 (off, NULL);
3111           break;
3112
3113         case DW_CFA_def_cfa_sf:
3114           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3115           dw2_asm_output_data_uleb128 (r, NULL);
3116           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3117           dw2_asm_output_data_sleb128 (off, NULL);
3118           break;
3119
3120         case DW_CFA_restore_extended:
3121         case DW_CFA_undefined:
3122         case DW_CFA_same_value:
3123         case DW_CFA_def_cfa_register:
3124           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3125           dw2_asm_output_data_uleb128 (r, NULL);
3126           break;
3127
3128         case DW_CFA_register:
3129           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3130           dw2_asm_output_data_uleb128 (r, NULL);
3131           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3132           dw2_asm_output_data_uleb128 (r, NULL);
3133           break;
3134
3135         case DW_CFA_def_cfa_offset:
3136         case DW_CFA_GNU_args_size:
3137           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3138           break;
3139
3140         case DW_CFA_def_cfa_offset_sf:
3141           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3142           dw2_asm_output_data_sleb128 (off, NULL);
3143           break;
3144
3145         case DW_CFA_GNU_window_save:
3146           break;
3147
3148         case DW_CFA_def_cfa_expression:
3149         case DW_CFA_expression:
3150           output_cfa_loc (cfi);
3151           break;
3152
3153         case DW_CFA_GNU_negative_offset_extended:
3154           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3155           gcc_unreachable ();
3156
3157         default:
3158           break;
3159         }
3160     }
3161 }
3162
3163 /* Similar, but do it via assembler directives instead.  */
3164
3165 static void
3166 output_cfi_directive (dw_cfi_ref cfi)
3167 {
3168   unsigned long r, r2;
3169
3170   switch (cfi->dw_cfi_opc)
3171     {
3172     case DW_CFA_advance_loc:
3173     case DW_CFA_advance_loc1:
3174     case DW_CFA_advance_loc2:
3175     case DW_CFA_advance_loc4:
3176     case DW_CFA_MIPS_advance_loc8:
3177     case DW_CFA_set_loc:
3178       /* Should only be created by add_fde_cfi in a code path not
3179          followed when emitting via directives.  The assembler is
3180          going to take care of this for us.  */
3181       gcc_unreachable ();
3182
3183     case DW_CFA_offset:
3184     case DW_CFA_offset_extended:
3185     case DW_CFA_offset_extended_sf:
3186       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3187       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3188                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3189       break;
3190
3191     case DW_CFA_restore:
3192     case DW_CFA_restore_extended:
3193       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3194       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3195       break;
3196
3197     case DW_CFA_undefined:
3198       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3199       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3200       break;
3201
3202     case DW_CFA_same_value:
3203       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3204       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3205       break;
3206
3207     case DW_CFA_def_cfa:
3208     case DW_CFA_def_cfa_sf:
3209       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3210       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3211                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3212       break;
3213
3214     case DW_CFA_def_cfa_register:
3215       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3216       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3217       break;
3218
3219     case DW_CFA_register:
3220       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3221       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3222       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3223       break;
3224
3225     case DW_CFA_def_cfa_offset:
3226     case DW_CFA_def_cfa_offset_sf:
3227       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3228                HOST_WIDE_INT_PRINT_DEC"\n",
3229                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3230       break;
3231
3232     case DW_CFA_remember_state:
3233       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3234       break;
3235     case DW_CFA_restore_state:
3236       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3237       break;
3238
3239     case DW_CFA_GNU_args_size:
3240       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3241       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3242       if (flag_debug_asm)
3243         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3244                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3245       fputc ('\n', asm_out_file);
3246       break;
3247
3248     case DW_CFA_GNU_window_save:
3249       fprintf (asm_out_file, "\t.cfi_window_save\n");
3250       break;
3251
3252     case DW_CFA_def_cfa_expression:
3253     case DW_CFA_expression:
3254       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3255       output_cfa_loc_raw (cfi);
3256       fputc ('\n', asm_out_file);
3257       break;
3258
3259     default:
3260       gcc_unreachable ();
3261     }
3262 }
3263
3264 DEF_VEC_P (dw_cfi_ref);
3265 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3266
3267 /* Output CFIs to bring current FDE to the same state as after executing
3268    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3269    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3270    other arguments to pass to output_cfi.  */
3271
3272 static void
3273 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3274 {
3275   struct dw_cfi_struct cfi_buf;
3276   dw_cfi_ref cfi2;
3277   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3278   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3279   unsigned int len, idx;
3280
3281   for (;; cfi = cfi->dw_cfi_next)
3282     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3283       {
3284       case DW_CFA_advance_loc:
3285       case DW_CFA_advance_loc1:
3286       case DW_CFA_advance_loc2:
3287       case DW_CFA_advance_loc4:
3288       case DW_CFA_MIPS_advance_loc8:
3289       case DW_CFA_set_loc:
3290         /* All advances should be ignored.  */
3291         break;
3292       case DW_CFA_remember_state:
3293         {
3294           dw_cfi_ref args_size = cfi_args_size;
3295
3296           /* Skip everything between .cfi_remember_state and
3297              .cfi_restore_state.  */
3298           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3299             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3300               break;
3301             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3302               args_size = cfi2;
3303             else
3304               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3305
3306           if (cfi2 == NULL)
3307             goto flush_all;
3308           else
3309             {
3310               cfi = cfi2;
3311               cfi_args_size = args_size;
3312             }
3313           break;
3314         }
3315       case DW_CFA_GNU_args_size:
3316         cfi_args_size = cfi;
3317         break;
3318       case DW_CFA_GNU_window_save:
3319         goto flush_all;
3320       case DW_CFA_offset:
3321       case DW_CFA_offset_extended:
3322       case DW_CFA_offset_extended_sf:
3323       case DW_CFA_restore:
3324       case DW_CFA_restore_extended:
3325       case DW_CFA_undefined:
3326       case DW_CFA_same_value:
3327       case DW_CFA_register:
3328       case DW_CFA_val_offset:
3329       case DW_CFA_val_offset_sf:
3330       case DW_CFA_expression:
3331       case DW_CFA_val_expression:
3332       case DW_CFA_GNU_negative_offset_extended:
3333         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3334           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3335                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3336         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3337         break;
3338       case DW_CFA_def_cfa:
3339       case DW_CFA_def_cfa_sf:
3340       case DW_CFA_def_cfa_expression:
3341         cfi_cfa = cfi;
3342         cfi_cfa_offset = cfi;
3343         break;
3344       case DW_CFA_def_cfa_register:
3345         cfi_cfa = cfi;
3346         break;
3347       case DW_CFA_def_cfa_offset:
3348       case DW_CFA_def_cfa_offset_sf:
3349         cfi_cfa_offset = cfi;
3350         break;
3351       case DW_CFA_nop:
3352         gcc_assert (cfi == NULL);
3353       flush_all:
3354         len = VEC_length (dw_cfi_ref, regs);
3355         for (idx = 0; idx < len; idx++)
3356           {
3357             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3358             if (cfi2 != NULL
3359                 && cfi2->dw_cfi_opc != DW_CFA_restore
3360                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3361               {
3362                 if (do_cfi_asm)
3363                   output_cfi_directive (cfi2);
3364                 else
3365                   output_cfi (cfi2, fde, for_eh);
3366               }
3367           }
3368         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3369           {
3370             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3371             cfi_buf = *cfi_cfa;
3372             switch (cfi_cfa_offset->dw_cfi_opc)
3373               {
3374               case DW_CFA_def_cfa_offset:
3375                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3376                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3377                 break;
3378               case DW_CFA_def_cfa_offset_sf:
3379                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3380                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3381                 break;
3382               case DW_CFA_def_cfa:
3383               case DW_CFA_def_cfa_sf:
3384                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3385                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3386                 break;
3387               default:
3388                 gcc_unreachable ();
3389               }
3390             cfi_cfa = &cfi_buf;
3391           }
3392         else if (cfi_cfa_offset)
3393           cfi_cfa = cfi_cfa_offset;
3394         if (cfi_cfa)
3395           {
3396             if (do_cfi_asm)
3397               output_cfi_directive (cfi_cfa);
3398             else
3399               output_cfi (cfi_cfa, fde, for_eh);
3400           }
3401         cfi_cfa = NULL;
3402         cfi_cfa_offset = NULL;
3403         if (cfi_args_size
3404             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3405           {
3406             if (do_cfi_asm)
3407               output_cfi_directive (cfi_args_size);
3408             else
3409               output_cfi (cfi_args_size, fde, for_eh);
3410           }
3411         cfi_args_size = NULL;
3412         if (cfi == NULL)
3413           {
3414             VEC_free (dw_cfi_ref, heap, regs);
3415             return;
3416           }
3417         else if (do_cfi_asm)
3418           output_cfi_directive (cfi);
3419         else
3420           output_cfi (cfi, fde, for_eh);
3421         break;
3422       default:
3423         gcc_unreachable ();
3424     }
3425 }
3426
3427 /* Output one FDE.  */
3428
3429 static void
3430 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3431             char *section_start_label, int fde_encoding, char *augmentation,
3432             bool any_lsda_needed, int lsda_encoding)
3433 {
3434   const char *begin, *end;
3435   static unsigned int j;
3436   char l1[20], l2[20];
3437   dw_cfi_ref cfi;
3438
3439   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3440                                 /* empty */ 0);
3441   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3442                                   for_eh + j);
3443   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3444   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3445   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3446     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3447                          " indicating 64-bit DWARF extension");
3448   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3449                         "FDE Length");
3450   ASM_OUTPUT_LABEL (asm_out_file, l1);
3451
3452   if (for_eh)
3453     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3454   else
3455     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3456                            debug_frame_section, "FDE CIE offset");
3457
3458   if (!fde->dw_fde_switched_sections)
3459     {
3460       begin = fde->dw_fde_begin;
3461       end = fde->dw_fde_end;
3462     }
3463   else
3464     {
3465       /* For the first section, prefer dw_fde_begin over
3466          dw_fde_{hot,cold}_section_label, as the latter
3467          might be separated from the real start of the
3468          function by alignment padding.  */
3469       if (!second)
3470         begin = fde->dw_fde_begin;
3471       else if (fde->dw_fde_switched_cold_to_hot)
3472         begin = fde->dw_fde_hot_section_label;
3473       else
3474         begin = fde->dw_fde_unlikely_section_label;
3475       if (second ^ fde->dw_fde_switched_cold_to_hot)
3476         end = fde->dw_fde_unlikely_section_end_label;
3477       else
3478         end = fde->dw_fde_hot_section_end_label;
3479     }
3480
3481   if (for_eh)
3482     {
3483       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3484       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3485       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3486                                        "FDE initial location");
3487       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3488                             end, begin, "FDE address range");
3489     }
3490   else
3491     {
3492       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3493       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3494     }
3495
3496   if (augmentation[0])
3497     {
3498       if (any_lsda_needed)
3499         {
3500           int size = size_of_encoded_value (lsda_encoding);
3501
3502           if (lsda_encoding == DW_EH_PE_aligned)
3503             {
3504               int offset = (  4         /* Length */
3505                             + 4         /* CIE offset */
3506                             + 2 * size_of_encoded_value (fde_encoding)
3507                             + 1         /* Augmentation size */ );
3508               int pad = -offset & (PTR_SIZE - 1);
3509
3510               size += pad;
3511               gcc_assert (size_of_uleb128 (size) == 1);
3512             }
3513
3514           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3515
3516           if (fde->uses_eh_lsda)
3517             {
3518               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3519                                            fde->funcdef_number);
3520               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3521                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3522                                                false,
3523                                                "Language Specific Data Area");
3524             }
3525           else
3526             {
3527               if (lsda_encoding == DW_EH_PE_aligned)
3528                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3529               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3530                                    "Language Specific Data Area (none)");
3531             }
3532         }
3533       else
3534         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3535     }
3536
3537   /* Loop through the Call Frame Instructions associated with
3538      this FDE.  */
3539   fde->dw_fde_current_label = begin;
3540   if (!fde->dw_fde_switched_sections)
3541     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3542       output_cfi (cfi, fde, for_eh);
3543   else if (!second)
3544     {
3545       if (fde->dw_fde_switch_cfi)
3546         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3547           {
3548             output_cfi (cfi, fde, for_eh);
3549             if (cfi == fde->dw_fde_switch_cfi)
3550               break;
3551           }
3552     }
3553   else
3554     {
3555       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3556
3557       if (fde->dw_fde_switch_cfi)
3558         {
3559           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3560           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3561           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3562           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3563         }
3564       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3565         output_cfi (cfi, fde, for_eh);
3566     }
3567
3568   /* If we are to emit a ref/link from function bodies to their frame tables,
3569      do it now.  This is typically performed to make sure that tables
3570      associated with functions are dragged with them and not discarded in
3571      garbage collecting links. We need to do this on a per function basis to
3572      cope with -ffunction-sections.  */
3573
3574 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3575   /* Switch to the function section, emit the ref to the tables, and
3576      switch *back* into the table section.  */
3577   switch_to_section (function_section (fde->decl));
3578   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3579   switch_to_frame_table_section (for_eh, true);
3580 #endif
3581
3582   /* Pad the FDE out to an address sized boundary.  */
3583   ASM_OUTPUT_ALIGN (asm_out_file,
3584                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3585   ASM_OUTPUT_LABEL (asm_out_file, l2);
3586
3587   j += 2;
3588 }
3589
3590 /* Output the call frame information used to record information
3591    that relates to calculating the frame pointer, and records the
3592    location of saved registers.  */
3593
3594 static void
3595 output_call_frame_info (int for_eh)
3596 {
3597   unsigned int i;
3598   dw_fde_ref fde;
3599   dw_cfi_ref cfi;
3600   char l1[20], l2[20], section_start_label[20];
3601   bool any_lsda_needed = false;
3602   char augmentation[6];
3603   int augmentation_size;
3604   int fde_encoding = DW_EH_PE_absptr;
3605   int per_encoding = DW_EH_PE_absptr;
3606   int lsda_encoding = DW_EH_PE_absptr;
3607   int return_reg;
3608   rtx personality = NULL;
3609   int dw_cie_version;
3610
3611   /* Don't emit a CIE if there won't be any FDEs.  */
3612   if (fde_table_in_use == 0)
3613     return;
3614
3615   /* Nothing to do if the assembler's doing it all.  */
3616   if (dwarf2out_do_cfi_asm ())
3617     return;
3618
3619   /* If we make FDEs linkonce, we may have to emit an empty label for
3620      an FDE that wouldn't otherwise be emitted.  We want to avoid
3621      having an FDE kept around when the function it refers to is
3622      discarded.  Example where this matters: a primary function
3623      template in C++ requires EH information, but an explicit
3624      specialization doesn't.  */
3625   if (TARGET_USES_WEAK_UNWIND_INFO
3626       && ! flag_asynchronous_unwind_tables
3627       && flag_exceptions
3628       && for_eh)
3629     for (i = 0; i < fde_table_in_use; i++)
3630       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3631           && !fde_table[i].uses_eh_lsda
3632           && ! DECL_WEAK (fde_table[i].decl))
3633         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3634                                       for_eh, /* empty */ 1);
3635
3636   /* If we don't have any functions we'll want to unwind out of, don't
3637      emit any EH unwind information.  Note that if exceptions aren't
3638      enabled, we won't have collected nothrow information, and if we
3639      asked for asynchronous tables, we always want this info.  */
3640   if (for_eh)
3641     {
3642       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3643
3644       for (i = 0; i < fde_table_in_use; i++)
3645         if (fde_table[i].uses_eh_lsda)
3646           any_eh_needed = any_lsda_needed = true;
3647         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3648           any_eh_needed = true;
3649         else if (! fde_table[i].nothrow
3650                  && ! fde_table[i].all_throwers_are_sibcalls)
3651           any_eh_needed = true;
3652
3653       if (! any_eh_needed)
3654         return;
3655     }
3656
3657   /* We're going to be generating comments, so turn on app.  */
3658   if (flag_debug_asm)
3659     app_enable ();
3660
3661   /* Switch to the proper frame section, first time.  */
3662   switch_to_frame_table_section (for_eh, false);
3663
3664   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3665   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3666
3667   /* Output the CIE.  */
3668   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3669   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3670   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3671     dw2_asm_output_data (4, 0xffffffff,
3672       "Initial length escape value indicating 64-bit DWARF extension");
3673   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3674                         "Length of Common Information Entry");
3675   ASM_OUTPUT_LABEL (asm_out_file, l1);
3676
3677   /* Now that the CIE pointer is PC-relative for EH,
3678      use 0 to identify the CIE.  */
3679   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3680                        (for_eh ? 0 : DWARF_CIE_ID),
3681                        "CIE Identifier Tag");
3682
3683   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3684      use CIE version 1, unless that would produce incorrect results
3685      due to overflowing the return register column.  */
3686   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3687   dw_cie_version = 1;
3688   if (return_reg >= 256 || dwarf_version > 2)
3689     dw_cie_version = 3;
3690   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3691
3692   augmentation[0] = 0;
3693   augmentation_size = 0;
3694
3695   personality = current_unit_personality;
3696   if (for_eh)
3697     {
3698       char *p;
3699
3700       /* Augmentation:
3701          z      Indicates that a uleb128 is present to size the
3702                 augmentation section.
3703          L      Indicates the encoding (and thus presence) of
3704                 an LSDA pointer in the FDE augmentation.
3705          R      Indicates a non-default pointer encoding for
3706                 FDE code pointers.
3707          P      Indicates the presence of an encoding + language
3708                 personality routine in the CIE augmentation.  */
3709
3710       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3711       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3712       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3713
3714       p = augmentation + 1;
3715       if (personality)
3716         {
3717           *p++ = 'P';
3718           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3719           assemble_external_libcall (personality);
3720         }
3721       if (any_lsda_needed)
3722         {
3723           *p++ = 'L';
3724           augmentation_size += 1;
3725         }
3726       if (fde_encoding != DW_EH_PE_absptr)
3727         {
3728           *p++ = 'R';
3729           augmentation_size += 1;
3730         }
3731       if (p > augmentation + 1)
3732         {
3733           augmentation[0] = 'z';
3734           *p = '\0';
3735         }
3736
3737       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3738       if (personality && per_encoding == DW_EH_PE_aligned)
3739         {
3740           int offset = (  4             /* Length */
3741                         + 4             /* CIE Id */
3742                         + 1             /* CIE version */
3743                         + strlen (augmentation) + 1     /* Augmentation */
3744                         + size_of_uleb128 (1)           /* Code alignment */
3745                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3746                         + 1             /* RA column */
3747                         + 1             /* Augmentation size */
3748                         + 1             /* Personality encoding */ );
3749           int pad = -offset & (PTR_SIZE - 1);
3750
3751           augmentation_size += pad;
3752
3753           /* Augmentations should be small, so there's scarce need to
3754              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3755           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3756         }
3757     }
3758
3759   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3760   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3761   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3762                                "CIE Data Alignment Factor");
3763
3764   if (dw_cie_version == 1)
3765     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3766   else
3767     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3768
3769   if (augmentation[0])
3770     {
3771       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3772       if (personality)
3773         {
3774           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3775                                eh_data_format_name (per_encoding));
3776           dw2_asm_output_encoded_addr_rtx (per_encoding,
3777                                            personality,
3778                                            true, NULL);
3779         }
3780
3781       if (any_lsda_needed)
3782         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3783                              eh_data_format_name (lsda_encoding));
3784
3785       if (fde_encoding != DW_EH_PE_absptr)
3786         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3787                              eh_data_format_name (fde_encoding));
3788     }
3789
3790   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3791     output_cfi (cfi, NULL, for_eh);
3792
3793   /* Pad the CIE out to an address sized boundary.  */
3794   ASM_OUTPUT_ALIGN (asm_out_file,
3795                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3796   ASM_OUTPUT_LABEL (asm_out_file, l2);
3797
3798   /* Loop through all of the FDE's.  */
3799   for (i = 0; i < fde_table_in_use; i++)
3800     {
3801       unsigned int k;
3802       fde = &fde_table[i];
3803
3804       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3805       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3806           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3807           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3808           && !fde->uses_eh_lsda)
3809         continue;
3810
3811       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3812         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3813                     augmentation, any_lsda_needed, lsda_encoding);
3814     }
3815
3816   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3817     dw2_asm_output_data (4, 0, "End of Table");
3818 #ifdef MIPS_DEBUGGING_INFO
3819   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3820      get a value of 0.  Putting .align 0 after the label fixes it.  */
3821   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3822 #endif
3823
3824   /* Turn off app to make assembly quicker.  */
3825   if (flag_debug_asm)
3826     app_disable ();
3827 }
3828
3829 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3830
3831 static void
3832 dwarf2out_do_cfi_startproc (bool second)
3833 {
3834   int enc;
3835   rtx ref;
3836   rtx personality = get_personality_function (current_function_decl);
3837
3838   fprintf (asm_out_file, "\t.cfi_startproc\n");
3839
3840   if (personality)
3841     {
3842       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3843       ref = personality;
3844
3845       /* ??? The GAS support isn't entirely consistent.  We have to
3846          handle indirect support ourselves, but PC-relative is done
3847          in the assembler.  Further, the assembler can't handle any
3848          of the weirder relocation types.  */
3849       if (enc & DW_EH_PE_indirect)
3850         ref = dw2_force_const_mem (ref, true);
3851
3852       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3853       output_addr_const (asm_out_file, ref);
3854       fputc ('\n', asm_out_file);
3855     }
3856
3857   if (crtl->uses_eh_lsda)
3858     {
3859       char lab[20];
3860
3861       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3862       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3863                                    current_function_funcdef_no);
3864       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3865       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3866
3867       if (enc & DW_EH_PE_indirect)
3868         ref = dw2_force_const_mem (ref, true);
3869
3870       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3871       output_addr_const (asm_out_file, ref);
3872       fputc ('\n', asm_out_file);
3873     }
3874 }
3875
3876 /* Output a marker (i.e. a label) for the beginning of a function, before
3877    the prologue.  */
3878
3879 void
3880 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3881                           const char *file ATTRIBUTE_UNUSED)
3882 {
3883   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3884   char * dup_label;
3885   dw_fde_ref fde;
3886   section *fnsec;
3887
3888   current_function_func_begin_label = NULL;
3889
3890 #ifdef TARGET_UNWIND_INFO
3891   /* ??? current_function_func_begin_label is also used by except.c
3892      for call-site information.  We must emit this label if it might
3893      be used.  */
3894   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3895       && ! dwarf2out_do_frame ())
3896     return;
3897 #else
3898   if (! dwarf2out_do_frame ())
3899     return;
3900 #endif
3901
3902   fnsec = function_section (current_function_decl);
3903   switch_to_section (fnsec);
3904   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3905                                current_function_funcdef_no);
3906   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3907                           current_function_funcdef_no);
3908   dup_label = xstrdup (label);
3909   current_function_func_begin_label = dup_label;
3910
3911 #ifdef TARGET_UNWIND_INFO
3912   /* We can elide the fde allocation if we're not emitting debug info.  */
3913   if (! dwarf2out_do_frame ())
3914     return;
3915 #endif
3916
3917   /* Expand the fde table if necessary.  */
3918   if (fde_table_in_use == fde_table_allocated)
3919     {
3920       fde_table_allocated += FDE_TABLE_INCREMENT;
3921       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3922       memset (fde_table + fde_table_in_use, 0,
3923               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3924     }
3925
3926   /* Record the FDE associated with this function.  */
3927   current_funcdef_fde = fde_table_in_use;
3928
3929   /* Add the new FDE at the end of the fde_table.  */
3930   fde = &fde_table[fde_table_in_use++];
3931   fde->decl = current_function_decl;
3932   fde->dw_fde_begin = dup_label;
3933   fde->dw_fde_current_label = dup_label;
3934   fde->dw_fde_hot_section_label = NULL;
3935   fde->dw_fde_hot_section_end_label = NULL;
3936   fde->dw_fde_unlikely_section_label = NULL;
3937   fde->dw_fde_unlikely_section_end_label = NULL;
3938   fde->dw_fde_switched_sections = 0;
3939   fde->dw_fde_switched_cold_to_hot = 0;
3940   fde->dw_fde_end = NULL;
3941   fde->dw_fde_cfi = NULL;
3942   fde->dw_fde_switch_cfi = NULL;
3943   fde->funcdef_number = current_function_funcdef_no;
3944   fde->nothrow = crtl->nothrow;
3945   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3946   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3947   fde->drap_reg = INVALID_REGNUM;
3948   fde->vdrap_reg = INVALID_REGNUM;
3949   if (flag_reorder_blocks_and_partition)
3950     {
3951       section *unlikelysec;
3952       if (first_function_block_is_cold)
3953         fde->in_std_section = 1;
3954       else
3955         fde->in_std_section
3956           = (fnsec == text_section
3957              || (cold_text_section && fnsec == cold_text_section));
3958       unlikelysec = unlikely_text_section ();
3959       fde->cold_in_std_section
3960         = (unlikelysec == text_section
3961            || (cold_text_section && unlikelysec == cold_text_section));
3962     }
3963   else
3964     {
3965       fde->in_std_section
3966         = (fnsec == text_section
3967            || (cold_text_section && fnsec == cold_text_section));
3968       fde->cold_in_std_section = 0;
3969     }
3970
3971   args_size = old_args_size = 0;
3972
3973   /* We only want to output line number information for the genuine dwarf2
3974      prologue case, not the eh frame case.  */
3975 #ifdef DWARF2_DEBUGGING_INFO
3976   if (file)
3977     dwarf2out_source_line (line, file, 0, true);
3978 #endif
3979
3980   if (dwarf2out_do_cfi_asm ())
3981     dwarf2out_do_cfi_startproc (false);
3982   else
3983     {
3984       rtx personality = get_personality_function (current_function_decl);
3985       if (!current_unit_personality)
3986         current_unit_personality = personality;
3987
3988       /* We cannot keep a current personality per function as without CFI
3989          asm at the point where we emit the CFI data there is no current
3990          function anymore.  */
3991       if (personality
3992           && current_unit_personality != personality)
3993         sorry ("Multiple EH personalities are supported only with assemblers "
3994                "supporting .cfi.personality directive.");
3995     }
3996 }
3997
3998 /* Output a marker (i.e. a label) for the absolute end of the generated code
3999    for a function definition.  This gets called *after* the epilogue code has
4000    been generated.  */
4001
4002 void
4003 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4004                         const char *file ATTRIBUTE_UNUSED)
4005 {
4006   dw_fde_ref fde;
4007   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4008
4009 #ifdef DWARF2_DEBUGGING_INFO
4010   last_var_location_insn = NULL_RTX;
4011 #endif
4012
4013   if (dwarf2out_do_cfi_asm ())
4014     fprintf (asm_out_file, "\t.cfi_endproc\n");
4015
4016   /* Output a label to mark the endpoint of the code generated for this
4017      function.  */
4018   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4019                                current_function_funcdef_no);
4020   ASM_OUTPUT_LABEL (asm_out_file, label);
4021   fde = current_fde ();
4022   gcc_assert (fde != NULL);
4023   fde->dw_fde_end = xstrdup (label);
4024 }
4025
4026 void
4027 dwarf2out_frame_init (void)
4028 {
4029   /* Allocate the initial hunk of the fde_table.  */
4030   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4031   fde_table_allocated = FDE_TABLE_INCREMENT;
4032   fde_table_in_use = 0;
4033
4034   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4035      sake of lookup_cfa.  */
4036
4037   /* On entry, the Canonical Frame Address is at SP.  */
4038   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4039
4040 #ifdef DWARF2_UNWIND_INFO
4041   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4042     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4043 #endif
4044 }
4045
4046 void
4047 dwarf2out_frame_finish (void)
4048 {
4049   /* Output call frame information.  */
4050   if (DWARF2_FRAME_INFO)
4051     output_call_frame_info (0);
4052
4053 #ifndef TARGET_UNWIND_INFO
4054   /* Output another copy for the unwinder.  */
4055   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4056     output_call_frame_info (1);
4057 #endif
4058 }
4059
4060 /* Note that the current function section is being used for code.  */
4061
4062 static void
4063 dwarf2out_note_section_used (void)
4064 {
4065   section *sec = current_function_section ();
4066   if (sec == text_section)
4067     text_section_used = true;
4068   else if (sec == cold_text_section)
4069     cold_text_section_used = true;
4070 }
4071
4072 void
4073 dwarf2out_switch_text_section (void)
4074 {
4075   dw_fde_ref fde = current_fde ();
4076
4077   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4078
4079   fde->dw_fde_switched_sections = 1;
4080   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4081
4082   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4083   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4084   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4085   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4086   have_multiple_function_sections = true;
4087
4088   /* Reset the current label on switching text sections, so that we
4089      don't attempt to advance_loc4 between labels in different sections.  */
4090   fde->dw_fde_current_label = NULL;
4091
4092   /* There is no need to mark used sections when not debugging.  */
4093   if (cold_text_section != NULL)
4094     dwarf2out_note_section_used ();
4095
4096   if (dwarf2out_do_cfi_asm ())
4097     fprintf (asm_out_file, "\t.cfi_endproc\n");
4098
4099   /* Now do the real section switch.  */
4100   switch_to_section (current_function_section ());
4101
4102   if (dwarf2out_do_cfi_asm ())
4103     {
4104       dwarf2out_do_cfi_startproc (true);
4105       /* As this is a different FDE, insert all current CFI instructions
4106          again.  */
4107       output_cfis (fde->dw_fde_cfi, true, fde, true);
4108     }
4109   else
4110     {
4111       dw_cfi_ref cfi = fde->dw_fde_cfi;
4112
4113       cfi = fde->dw_fde_cfi;
4114       if (cfi)
4115         while (cfi->dw_cfi_next != NULL)
4116           cfi = cfi->dw_cfi_next;
4117       fde->dw_fde_switch_cfi = cfi;
4118     }
4119 }
4120 #endif
4121 \f
4122 /* And now, the subset of the debugging information support code necessary
4123    for emitting location expressions.  */
4124
4125 /* Data about a single source file.  */
4126 struct GTY(()) dwarf_file_data {
4127   const char * filename;
4128   int emitted_number;
4129 };
4130
4131 typedef struct dw_val_struct *dw_val_ref;
4132 typedef struct die_struct *dw_die_ref;
4133 typedef const struct die_struct *const_dw_die_ref;
4134 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4135 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4136
4137 typedef struct GTY(()) deferred_locations_struct
4138 {
4139   tree variable;
4140   dw_die_ref die;
4141 } deferred_locations;
4142
4143 DEF_VEC_O(deferred_locations);
4144 DEF_VEC_ALLOC_O(deferred_locations,gc);
4145
4146 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4147
4148 /* Each DIE may have a series of attribute/value pairs.  Values
4149    can take on several forms.  The forms that are used in this
4150    implementation are listed below.  */
4151
4152 enum dw_val_class
4153 {
4154   dw_val_class_addr,
4155   dw_val_class_offset,
4156   dw_val_class_loc,
4157   dw_val_class_loc_list,
4158   dw_val_class_range_list,
4159   dw_val_class_const,
4160   dw_val_class_unsigned_const,
4161   dw_val_class_long_long,
4162   dw_val_class_vec,
4163   dw_val_class_flag,
4164   dw_val_class_die_ref,
4165   dw_val_class_fde_ref,
4166   dw_val_class_lbl_id,
4167   dw_val_class_lineptr,
4168   dw_val_class_str,
4169   dw_val_class_macptr,
4170   dw_val_class_file
4171 };
4172
4173 /* Describe a floating point constant value, or a vector constant value.  */
4174
4175 typedef struct GTY(()) dw_vec_struct {
4176   unsigned char * GTY((length ("%h.length"))) array;
4177   unsigned length;
4178   unsigned elt_size;
4179 }
4180 dw_vec_const;
4181
4182 /* The dw_val_node describes an attribute's value, as it is
4183    represented internally.  */
4184
4185 typedef struct GTY(()) dw_val_struct {
4186   enum dw_val_class val_class;
4187   union dw_val_struct_union
4188     {
4189       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4190       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4191       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4192       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4193       HOST_WIDE_INT GTY ((default)) val_int;
4194       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4195       rtx GTY ((tag ("dw_val_class_long_long"))) val_long_long;
4196       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4197       struct dw_val_die_union
4198         {
4199           dw_die_ref die;
4200           int external;
4201         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4202       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4203       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4204       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4205       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4206       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4207     }
4208   GTY ((desc ("%1.val_class"))) v;
4209 }
4210 dw_val_node;
4211
4212 /* Locations in memory are described using a sequence of stack machine
4213    operations.  */
4214
4215 typedef struct GTY(()) dw_loc_descr_struct {
4216   dw_loc_descr_ref dw_loc_next;
4217   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4218   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4219      from DW_OP_addr with a dtp-relative symbol relocation.  */
4220   unsigned int dtprel : 1;
4221   int dw_loc_addr;
4222   dw_val_node dw_loc_oprnd1;
4223   dw_val_node dw_loc_oprnd2;
4224 }
4225 dw_loc_descr_node;
4226
4227 /* Location lists are ranges + location descriptions for that range,
4228    so you can track variables that are in different places over
4229    their entire life.  */
4230 typedef struct GTY(()) dw_loc_list_struct {
4231   dw_loc_list_ref dw_loc_next;
4232   const char *begin; /* Label for begin address of range */
4233   const char *end;  /* Label for end address of range */
4234   char *ll_symbol; /* Label for beginning of location list.
4235                       Only on head of list */
4236   const char *section; /* Section this loclist is relative to */
4237   dw_loc_descr_ref expr;
4238 } dw_loc_list_node;
4239
4240 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4241
4242 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4243
4244 /* Convert a DWARF stack opcode into its string name.  */
4245
4246 static const char *
4247 dwarf_stack_op_name (unsigned int op)
4248 {
4249   switch (op)
4250     {
4251     case DW_OP_addr:
4252       return "DW_OP_addr";
4253     case DW_OP_deref:
4254       return "DW_OP_deref";
4255     case DW_OP_const1u:
4256       return "DW_OP_const1u";
4257     case DW_OP_const1s:
4258       return "DW_OP_const1s";
4259     case DW_OP_const2u:
4260       return "DW_OP_const2u";
4261     case DW_OP_const2s:
4262       return "DW_OP_const2s";
4263     case DW_OP_const4u:
4264       return "DW_OP_const4u";
4265     case DW_OP_const4s:
4266       return "DW_OP_const4s";
4267     case DW_OP_const8u:
4268       return "DW_OP_const8u";
4269     case DW_OP_const8s:
4270       return "DW_OP_const8s";
4271     case DW_OP_constu:
4272       return "DW_OP_constu";
4273     case DW_OP_consts:
4274       return "DW_OP_consts";
4275     case DW_OP_dup:
4276       return "DW_OP_dup";
4277     case DW_OP_drop:
4278       return "DW_OP_drop";
4279     case DW_OP_over:
4280       return "DW_OP_over";
4281     case DW_OP_pick:
4282       return "DW_OP_pick";
4283     case DW_OP_swap:
4284       return "DW_OP_swap";
4285     case DW_OP_rot:
4286       return "DW_OP_rot";
4287     case DW_OP_xderef:
4288       return "DW_OP_xderef";
4289     case DW_OP_abs:
4290       return "DW_OP_abs";
4291     case DW_OP_and:
4292       return "DW_OP_and";
4293     case DW_OP_div:
4294       return "DW_OP_div";
4295     case DW_OP_minus:
4296       return "DW_OP_minus";
4297     case DW_OP_mod:
4298       return "DW_OP_mod";
4299     case DW_OP_mul:
4300       return "DW_OP_mul";
4301     case DW_OP_neg:
4302       return "DW_OP_neg";
4303     case DW_OP_not:
4304       return "DW_OP_not";
4305     case DW_OP_or:
4306       return "DW_OP_or";
4307     case DW_OP_plus:
4308       return "DW_OP_plus";
4309     case DW_OP_plus_uconst:
4310       return "DW_OP_plus_uconst";
4311     case DW_OP_shl:
4312       return "DW_OP_shl";
4313     case DW_OP_shr:
4314       return "DW_OP_shr";
4315     case DW_OP_shra:
4316       return "DW_OP_shra";
4317     case DW_OP_xor:
4318       return "DW_OP_xor";
4319     case DW_OP_bra:
4320       return "DW_OP_bra";
4321     case DW_OP_eq:
4322       return "DW_OP_eq";
4323     case DW_OP_ge:
4324       return "DW_OP_ge";
4325     case DW_OP_gt:
4326       return "DW_OP_gt";
4327     case DW_OP_le:
4328       return "DW_OP_le";
4329     case DW_OP_lt:
4330       return "DW_OP_lt";
4331     case DW_OP_ne:
4332       return "DW_OP_ne";
4333     case DW_OP_skip:
4334       return "DW_OP_skip";
4335     case DW_OP_lit0:
4336       return "DW_OP_lit0";
4337     case DW_OP_lit1:
4338       return "DW_OP_lit1";
4339     case DW_OP_lit2:
4340       return "DW_OP_lit2";
4341     case DW_OP_lit3:
4342       return "DW_OP_lit3";
4343     case DW_OP_lit4:
4344       return "DW_OP_lit4";
4345     case DW_OP_lit5:
4346       return "DW_OP_lit5";
4347     case DW_OP_lit6:
4348       return "DW_OP_lit6";
4349     case DW_OP_lit7:
4350       return "DW_OP_lit7";
4351     case DW_OP_lit8:
4352       return "DW_OP_lit8";
4353     case DW_OP_lit9:
4354       return "DW_OP_lit9";
4355     case DW_OP_lit10:
4356       return "DW_OP_lit10";
4357     case DW_OP_lit11:
4358       return "DW_OP_lit11";
4359     case DW_OP_lit12:
4360       return "DW_OP_lit12";
4361     case DW_OP_lit13:
4362       return "DW_OP_lit13";
4363     case DW_OP_lit14:
4364       return "DW_OP_lit14";
4365     case DW_OP_lit15:
4366       return "DW_OP_lit15";
4367     case DW_OP_lit16:
4368       return "DW_OP_lit16";
4369     case DW_OP_lit17:
4370       return "DW_OP_lit17";
4371     case DW_OP_lit18:
4372       return "DW_OP_lit18";
4373     case DW_OP_lit19:
4374       return "DW_OP_lit19";
4375     case DW_OP_lit20:
4376       return "DW_OP_lit20";
4377     case DW_OP_lit21:
4378       return "DW_OP_lit21";
4379     case DW_OP_lit22:
4380       return "DW_OP_lit22";
4381     case DW_OP_lit23:
4382       return "DW_OP_lit23";
4383     case DW_OP_lit24:
4384       return "DW_OP_lit24";
4385     case DW_OP_lit25:
4386       return "DW_OP_lit25";
4387     case DW_OP_lit26:
4388       return "DW_OP_lit26";
4389     case DW_OP_lit27:
4390       return "DW_OP_lit27";
4391     case DW_OP_lit28:
4392       return "DW_OP_lit28";
4393     case DW_OP_lit29:
4394       return "DW_OP_lit29";
4395     case DW_OP_lit30:
4396       return "DW_OP_lit30";
4397     case DW_OP_lit31:
4398       return "DW_OP_lit31";
4399     case DW_OP_reg0:
4400       return "DW_OP_reg0";
4401     case DW_OP_reg1:
4402       return "DW_OP_reg1";
4403     case DW_OP_reg2:
4404       return "DW_OP_reg2";
4405     case DW_OP_reg3:
4406       return "DW_OP_reg3";
4407     case DW_OP_reg4:
4408       return "DW_OP_reg4";
4409     case DW_OP_reg5:
4410       return "DW_OP_reg5";
4411     case DW_OP_reg6:
4412       return "DW_OP_reg6";
4413     case DW_OP_reg7:
4414       return "DW_OP_reg7";
4415     case DW_OP_reg8:
4416       return "DW_OP_reg8";
4417     case DW_OP_reg9:
4418       return "DW_OP_reg9";
4419     case DW_OP_reg10:
4420       return "DW_OP_reg10";
4421     case DW_OP_reg11:
4422       return "DW_OP_reg11";
4423     case DW_OP_reg12:
4424       return "DW_OP_reg12";
4425     case DW_OP_reg13:
4426       return "DW_OP_reg13";
4427     case DW_OP_reg14:
4428       return "DW_OP_reg14";
4429     case DW_OP_reg15:
4430       return "DW_OP_reg15";
4431     case DW_OP_reg16:
4432       return "DW_OP_reg16";
4433     case DW_OP_reg17:
4434       return "DW_OP_reg17";
4435     case DW_OP_reg18:
4436       return "DW_OP_reg18";
4437     case DW_OP_reg19:
4438       return "DW_OP_reg19";
4439     case DW_OP_reg20:
4440       return "DW_OP_reg20";
4441     case DW_OP_reg21:
4442       return "DW_OP_reg21";
4443     case DW_OP_reg22:
4444       return "DW_OP_reg22";
4445     case DW_OP_reg23:
4446       return "DW_OP_reg23";
4447     case DW_OP_reg24:
4448       return "DW_OP_reg24";
4449     case DW_OP_reg25:
4450       return "DW_OP_reg25";
4451     case DW_OP_reg26:
4452       return "DW_OP_reg26";
4453     case DW_OP_reg27:
4454       return "DW_OP_reg27";
4455     case DW_OP_reg28:
4456       return "DW_OP_reg28";
4457     case DW_OP_reg29:
4458       return "DW_OP_reg29";
4459     case DW_OP_reg30:
4460       return "DW_OP_reg30";
4461     case DW_OP_reg31:
4462       return "DW_OP_reg31";
4463     case DW_OP_breg0:
4464       return "DW_OP_breg0";
4465     case DW_OP_breg1:
4466       return "DW_OP_breg1";
4467     case DW_OP_breg2:
4468       return "DW_OP_breg2";
4469     case DW_OP_breg3:
4470       return "DW_OP_breg3";
4471     case DW_OP_breg4:
4472       return "DW_OP_breg4";
4473     case DW_OP_breg5:
4474       return "DW_OP_breg5";
4475     case DW_OP_breg6:
4476       return "DW_OP_breg6";
4477     case DW_OP_breg7:
4478       return "DW_OP_breg7";
4479     case DW_OP_breg8:
4480       return "DW_OP_breg8";
4481     case DW_OP_breg9:
4482       return "DW_OP_breg9";
4483     case DW_OP_breg10:
4484       return "DW_OP_breg10";
4485     case DW_OP_breg11:
4486       return "DW_OP_breg11";
4487     case DW_OP_breg12:
4488       return "DW_OP_breg12";
4489     case DW_OP_breg13:
4490       return "DW_OP_breg13";
4491     case DW_OP_breg14:
4492       return "DW_OP_breg14";
4493     case DW_OP_breg15:
4494       return "DW_OP_breg15";
4495     case DW_OP_breg16:
4496       return "DW_OP_breg16";
4497     case DW_OP_breg17:
4498       return "DW_OP_breg17";
4499     case DW_OP_breg18:
4500       return "DW_OP_breg18";
4501     case DW_OP_breg19:
4502       return "DW_OP_breg19";
4503     case DW_OP_breg20:
4504       return "DW_OP_breg20";
4505     case DW_OP_breg21:
4506       return "DW_OP_breg21";
4507     case DW_OP_breg22:
4508       return "DW_OP_breg22";
4509     case DW_OP_breg23:
4510       return "DW_OP_breg23";
4511     case DW_OP_breg24:
4512       return "DW_OP_breg24";
4513     case DW_OP_breg25:
4514       return "DW_OP_breg25";
4515     case DW_OP_breg26:
4516       return "DW_OP_breg26";
4517     case DW_OP_breg27:
4518       return "DW_OP_breg27";
4519     case DW_OP_breg28:
4520       return "DW_OP_breg28";
4521     case DW_OP_breg29:
4522       return "DW_OP_breg29";
4523     case DW_OP_breg30:
4524       return "DW_OP_breg30";
4525     case DW_OP_breg31:
4526       return "DW_OP_breg31";
4527     case DW_OP_regx:
4528       return "DW_OP_regx";
4529     case DW_OP_fbreg:
4530       return "DW_OP_fbreg";
4531     case DW_OP_bregx:
4532       return "DW_OP_bregx";
4533     case DW_OP_piece:
4534       return "DW_OP_piece";
4535     case DW_OP_deref_size:
4536       return "DW_OP_deref_size";
4537     case DW_OP_xderef_size:
4538       return "DW_OP_xderef_size";
4539     case DW_OP_nop:
4540       return "DW_OP_nop";
4541
4542     case DW_OP_push_object_address:
4543       return "DW_OP_push_object_address";
4544     case DW_OP_call2:
4545       return "DW_OP_call2";
4546     case DW_OP_call4:
4547       return "DW_OP_call4";
4548     case DW_OP_call_ref:
4549       return "DW_OP_call_ref";
4550     case DW_OP_implicit_value:
4551       return "DW_OP_implicit_value";
4552     case DW_OP_stack_value:
4553       return "DW_OP_stack_value";
4554     case DW_OP_form_tls_address:
4555       return "DW_OP_form_tls_address";
4556     case DW_OP_call_frame_cfa:
4557       return "DW_OP_call_frame_cfa";
4558     case DW_OP_bit_piece:
4559       return "DW_OP_bit_piece";
4560
4561     case DW_OP_GNU_push_tls_address:
4562       return "DW_OP_GNU_push_tls_address";
4563     case DW_OP_GNU_uninit:
4564       return "DW_OP_GNU_uninit";
4565     case DW_OP_GNU_encoded_addr:
4566       return "DW_OP_GNU_encoded_addr";
4567
4568     default:
4569       return "OP_<unknown>";
4570     }
4571 }
4572
4573 /* Return a pointer to a newly allocated location description.  Location
4574    descriptions are simple expression terms that can be strung
4575    together to form more complicated location (address) descriptions.  */
4576
4577 static inline dw_loc_descr_ref
4578 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4579                unsigned HOST_WIDE_INT oprnd2)
4580 {
4581   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4582
4583   descr->dw_loc_opc = op;
4584   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4585   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4586   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4587   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4588
4589   return descr;
4590 }
4591
4592 /* Return a pointer to a newly allocated location description for
4593    REG and OFFSET.  */
4594
4595 static inline dw_loc_descr_ref
4596 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4597 {
4598   if (reg <= 31)
4599     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4600                           offset, 0);
4601   else
4602     return new_loc_descr (DW_OP_bregx, reg, offset);
4603 }
4604
4605 /* Add a location description term to a location description expression.  */
4606
4607 static inline void
4608 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4609 {
4610   dw_loc_descr_ref *d;
4611
4612   /* Find the end of the chain.  */
4613   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4614     ;
4615
4616   *d = descr;
4617 }
4618
4619 /* Add a constant OFFSET to a location expression.  */
4620
4621 static void
4622 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4623 {
4624   dw_loc_descr_ref loc;
4625   HOST_WIDE_INT *p;
4626
4627   gcc_assert (*list_head != NULL);
4628
4629   if (!offset)
4630     return;
4631
4632   /* Find the end of the chain.  */
4633   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4634     ;
4635
4636   p = NULL;
4637   if (loc->dw_loc_opc == DW_OP_fbreg
4638       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4639     p = &loc->dw_loc_oprnd1.v.val_int;
4640   else if (loc->dw_loc_opc == DW_OP_bregx)
4641     p = &loc->dw_loc_oprnd2.v.val_int;
4642
4643   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4644      offset.  Don't optimize if an signed integer overflow would happen.  */
4645   if (p != NULL
4646       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4647           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4648     *p += offset;
4649
4650   else if (offset > 0)
4651     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4652
4653   else
4654     {
4655       loc->dw_loc_next = int_loc_descriptor (offset);
4656       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4657     }
4658 }
4659
4660 /* Add a constant OFFSET to a location list.  */
4661
4662 static void
4663 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4664 {
4665   dw_loc_list_ref d;
4666   for (d = list_head; d != NULL; d = d->dw_loc_next)
4667     loc_descr_plus_const (&d->expr, offset);
4668 }
4669
4670 /* Return the size of a location descriptor.  */
4671
4672 static unsigned long
4673 size_of_loc_descr (dw_loc_descr_ref loc)
4674 {
4675   unsigned long size = 1;
4676
4677   switch (loc->dw_loc_opc)
4678     {
4679     case DW_OP_addr:
4680       size += DWARF2_ADDR_SIZE;
4681       break;
4682     case DW_OP_const1u:
4683     case DW_OP_const1s:
4684       size += 1;
4685       break;
4686     case DW_OP_const2u:
4687     case DW_OP_const2s:
4688       size += 2;
4689       break;
4690     case DW_OP_const4u:
4691     case DW_OP_const4s:
4692       size += 4;
4693       break;
4694     case DW_OP_const8u:
4695     case DW_OP_const8s:
4696       size += 8;
4697       break;
4698     case DW_OP_constu:
4699       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4700       break;
4701     case DW_OP_consts:
4702       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4703       break;
4704     case DW_OP_pick:
4705       size += 1;
4706       break;
4707     case DW_OP_plus_uconst:
4708       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4709       break;
4710     case DW_OP_skip:
4711     case DW_OP_bra:
4712       size += 2;
4713       break;
4714     case DW_OP_breg0:
4715     case DW_OP_breg1:
4716     case DW_OP_breg2:
4717     case DW_OP_breg3:
4718     case DW_OP_breg4:
4719     case DW_OP_breg5:
4720     case DW_OP_breg6:
4721     case DW_OP_breg7:
4722     case DW_OP_breg8:
4723     case DW_OP_breg9:
4724     case DW_OP_breg10:
4725     case DW_OP_breg11:
4726     case DW_OP_breg12:
4727     case DW_OP_breg13:
4728     case DW_OP_breg14:
4729     case DW_OP_breg15:
4730     case DW_OP_breg16:
4731     case DW_OP_breg17:
4732     case DW_OP_breg18:
4733     case DW_OP_breg19:
4734     case DW_OP_breg20:
4735     case DW_OP_breg21:
4736     case DW_OP_breg22:
4737     case DW_OP_breg23:
4738     case DW_OP_breg24:
4739     case DW_OP_breg25:
4740     case DW_OP_breg26:
4741     case DW_OP_breg27:
4742     case DW_OP_breg28:
4743     case DW_OP_breg29:
4744     case DW_OP_breg30:
4745     case DW_OP_breg31:
4746       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4747       break;
4748     case DW_OP_regx:
4749       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4750       break;
4751     case DW_OP_fbreg:
4752       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4753       break;
4754     case DW_OP_bregx:
4755       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4756       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4757       break;
4758     case DW_OP_piece:
4759       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4760       break;
4761     case DW_OP_deref_size:
4762     case DW_OP_xderef_size:
4763       size += 1;
4764       break;
4765     case DW_OP_call2:
4766       size += 2;
4767       break;
4768     case DW_OP_call4:
4769       size += 4;
4770       break;
4771     case DW_OP_call_ref:
4772       size += DWARF2_ADDR_SIZE;
4773       break;
4774     case DW_OP_implicit_value:
4775       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4776               + loc->dw_loc_oprnd1.v.val_unsigned;
4777       break;
4778     default:
4779       break;
4780     }
4781
4782   return size;
4783 }
4784
4785 /* Return the size of a series of location descriptors.  */
4786
4787 static unsigned long
4788 size_of_locs (dw_loc_descr_ref loc)
4789 {
4790   dw_loc_descr_ref l;
4791   unsigned long size;
4792
4793   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4794      field, to avoid writing to a PCH file.  */
4795   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4796     {
4797       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4798         break;
4799       size += size_of_loc_descr (l);
4800     }
4801   if (! l)
4802     return size;
4803
4804   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4805     {
4806       l->dw_loc_addr = size;
4807       size += size_of_loc_descr (l);
4808     }
4809
4810   return size;
4811 }
4812
4813 #ifdef DWARF2_DEBUGGING_INFO
4814 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4815 #endif
4816
4817 /* Output location description stack opcode's operands (if any).  */
4818
4819 static void
4820 output_loc_operands (dw_loc_descr_ref loc)
4821 {
4822   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4823   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4824
4825   switch (loc->dw_loc_opc)
4826     {
4827 #ifdef DWARF2_DEBUGGING_INFO
4828     case DW_OP_const2u:
4829     case DW_OP_const2s:
4830       dw2_asm_output_data (2, val1->v.val_int, NULL);
4831       break;
4832     case DW_OP_const4u:
4833     case DW_OP_const4s:
4834       dw2_asm_output_data (4, val1->v.val_int, NULL);
4835       break;
4836     case DW_OP_const8u:
4837     case DW_OP_const8s:
4838       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4839       dw2_asm_output_data (8, val1->v.val_int, NULL);
4840       break;
4841     case DW_OP_skip:
4842     case DW_OP_bra:
4843       {
4844         int offset;
4845
4846         gcc_assert (val1->val_class == dw_val_class_loc);
4847         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4848
4849         dw2_asm_output_data (2, offset, NULL);
4850       }
4851       break;
4852     case DW_OP_implicit_value:
4853       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4854       switch (val2->val_class)
4855         {
4856         case dw_val_class_const:
4857           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4858           break;
4859         case dw_val_class_vec:
4860           {
4861             unsigned int elt_size = val2->v.val_vec.elt_size;
4862             unsigned int len = val2->v.val_vec.length;
4863             unsigned int i;
4864             unsigned char *p;
4865
4866             if (elt_size > sizeof (HOST_WIDE_INT))
4867               {
4868                 elt_size /= 2;
4869                 len *= 2;
4870               }
4871             for (i = 0, p = val2->v.val_vec.array;
4872                  i < len;
4873                  i++, p += elt_size)
4874               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4875                                    "fp or vector constant word %u", i);
4876           }
4877           break;
4878         case dw_val_class_long_long:
4879           {
4880             unsigned HOST_WIDE_INT first, second;
4881
4882             if (WORDS_BIG_ENDIAN)
4883               {
4884                 first = CONST_DOUBLE_HIGH (val2->v.val_long_long);
4885                 second = CONST_DOUBLE_LOW (val2->v.val_long_long);
4886               }
4887             else
4888               {
4889                 first = CONST_DOUBLE_LOW (val2->v.val_long_long);
4890                 second = CONST_DOUBLE_HIGH (val2->v.val_long_long);
4891               }
4892             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4893                                  first, "long long constant");
4894             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4895                                  second, NULL);
4896           }
4897           break;
4898         case dw_val_class_addr:
4899           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4900           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4901           break;
4902         default:
4903           gcc_unreachable ();
4904         }
4905       break;
4906 #else
4907     case DW_OP_const2u:
4908     case DW_OP_const2s:
4909     case DW_OP_const4u:
4910     case DW_OP_const4s:
4911     case DW_OP_const8u:
4912     case DW_OP_const8s:
4913     case DW_OP_skip:
4914     case DW_OP_bra:
4915     case DW_OP_implicit_value:
4916       /* We currently don't make any attempt to make sure these are
4917          aligned properly like we do for the main unwind info, so
4918          don't support emitting things larger than a byte if we're
4919          only doing unwinding.  */
4920       gcc_unreachable ();
4921 #endif
4922     case DW_OP_const1u:
4923     case DW_OP_const1s:
4924       dw2_asm_output_data (1, val1->v.val_int, NULL);
4925       break;
4926     case DW_OP_constu:
4927       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4928       break;
4929     case DW_OP_consts:
4930       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4931       break;
4932     case DW_OP_pick:
4933       dw2_asm_output_data (1, val1->v.val_int, NULL);
4934       break;
4935     case DW_OP_plus_uconst:
4936       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4937       break;
4938     case DW_OP_breg0:
4939     case DW_OP_breg1:
4940     case DW_OP_breg2:
4941     case DW_OP_breg3:
4942     case DW_OP_breg4:
4943     case DW_OP_breg5:
4944     case DW_OP_breg6:
4945     case DW_OP_breg7:
4946     case DW_OP_breg8:
4947     case DW_OP_breg9:
4948     case DW_OP_breg10:
4949     case DW_OP_breg11:
4950     case DW_OP_breg12:
4951     case DW_OP_breg13:
4952     case DW_OP_breg14:
4953     case DW_OP_breg15:
4954     case DW_OP_breg16:
4955     case DW_OP_breg17:
4956     case DW_OP_breg18:
4957     case DW_OP_breg19:
4958     case DW_OP_breg20:
4959     case DW_OP_breg21:
4960     case DW_OP_breg22:
4961     case DW_OP_breg23:
4962     case DW_OP_breg24:
4963     case DW_OP_breg25:
4964     case DW_OP_breg26:
4965     case DW_OP_breg27:
4966     case DW_OP_breg28:
4967     case DW_OP_breg29:
4968     case DW_OP_breg30:
4969     case DW_OP_breg31:
4970       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4971       break;
4972     case DW_OP_regx:
4973       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4974       break;
4975     case DW_OP_fbreg:
4976       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4977       break;
4978     case DW_OP_bregx:
4979       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4980       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4981       break;
4982     case DW_OP_piece:
4983       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4984       break;
4985     case DW_OP_deref_size:
4986     case DW_OP_xderef_size:
4987       dw2_asm_output_data (1, val1->v.val_int, NULL);
4988       break;
4989
4990     case DW_OP_addr:
4991       if (loc->dtprel)
4992         {
4993           if (targetm.asm_out.output_dwarf_dtprel)
4994             {
4995               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4996                                                    DWARF2_ADDR_SIZE,
4997                                                    val1->v.val_addr);
4998               fputc ('\n', asm_out_file);
4999             }
5000           else
5001             gcc_unreachable ();
5002         }
5003       else
5004         {
5005 #ifdef DWARF2_DEBUGGING_INFO
5006           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5007 #else
5008           gcc_unreachable ();
5009 #endif
5010         }
5011       break;
5012
5013     default:
5014       /* Other codes have no operands.  */
5015       break;
5016     }
5017 }
5018
5019 /* Output a sequence of location operations.  */
5020
5021 static void
5022 output_loc_sequence (dw_loc_descr_ref loc)
5023 {
5024   for (; loc != NULL; loc = loc->dw_loc_next)
5025     {
5026       /* Output the opcode.  */
5027       dw2_asm_output_data (1, loc->dw_loc_opc,
5028                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5029
5030       /* Output the operand(s) (if any).  */
5031       output_loc_operands (loc);
5032     }
5033 }
5034
5035 /* Output location description stack opcode's operands (if any).
5036    The output is single bytes on a line, suitable for .cfi_escape.  */
5037
5038 static void
5039 output_loc_operands_raw (dw_loc_descr_ref loc)
5040 {
5041   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5042   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5043
5044   switch (loc->dw_loc_opc)
5045     {
5046     case DW_OP_addr:
5047     case DW_OP_implicit_value:
5048       /* We cannot output addresses in .cfi_escape, only bytes.  */
5049       gcc_unreachable ();
5050
5051     case DW_OP_const1u:
5052     case DW_OP_const1s:
5053     case DW_OP_pick:
5054     case DW_OP_deref_size:
5055     case DW_OP_xderef_size:
5056       fputc (',', asm_out_file);
5057       dw2_asm_output_data_raw (1, val1->v.val_int);
5058       break;
5059
5060     case DW_OP_const2u:
5061     case DW_OP_const2s:
5062       fputc (',', asm_out_file);
5063       dw2_asm_output_data_raw (2, val1->v.val_int);
5064       break;
5065
5066     case DW_OP_const4u:
5067     case DW_OP_const4s:
5068       fputc (',', asm_out_file);
5069       dw2_asm_output_data_raw (4, val1->v.val_int);
5070       break;
5071
5072     case DW_OP_const8u:
5073     case DW_OP_const8s:
5074       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5075       fputc (',', asm_out_file);
5076       dw2_asm_output_data_raw (8, val1->v.val_int);
5077       break;
5078
5079     case DW_OP_skip:
5080     case DW_OP_bra:
5081       {
5082         int offset;
5083
5084         gcc_assert (val1->val_class == dw_val_class_loc);
5085         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5086
5087         fputc (',', asm_out_file);
5088         dw2_asm_output_data_raw (2, offset);
5089       }
5090       break;
5091
5092     case DW_OP_constu:
5093     case DW_OP_plus_uconst:
5094     case DW_OP_regx:
5095     case DW_OP_piece:
5096       fputc (',', asm_out_file);
5097       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5098       break;
5099
5100     case DW_OP_consts:
5101     case DW_OP_breg0:
5102     case DW_OP_breg1:
5103     case DW_OP_breg2:
5104     case DW_OP_breg3:
5105     case DW_OP_breg4:
5106     case DW_OP_breg5:
5107     case DW_OP_breg6:
5108     case DW_OP_breg7:
5109     case DW_OP_breg8:
5110     case DW_OP_breg9:
5111     case DW_OP_breg10:
5112     case DW_OP_breg11:
5113     case DW_OP_breg12:
5114     case DW_OP_breg13:
5115     case DW_OP_breg14:
5116     case DW_OP_breg15:
5117     case DW_OP_breg16:
5118     case DW_OP_breg17:
5119     case DW_OP_breg18:
5120     case DW_OP_breg19:
5121     case DW_OP_breg20:
5122     case DW_OP_breg21:
5123     case DW_OP_breg22:
5124     case DW_OP_breg23:
5125     case DW_OP_breg24:
5126     case DW_OP_breg25:
5127     case DW_OP_breg26:
5128     case DW_OP_breg27:
5129     case DW_OP_breg28:
5130     case DW_OP_breg29:
5131     case DW_OP_breg30:
5132     case DW_OP_breg31:
5133     case DW_OP_fbreg:
5134       fputc (',', asm_out_file);
5135       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5136       break;
5137
5138     case DW_OP_bregx:
5139       fputc (',', asm_out_file);
5140       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5141       fputc (',', asm_out_file);
5142       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5143       break;
5144
5145     default:
5146       /* Other codes have no operands.  */
5147       break;
5148     }
5149 }
5150
5151 static void
5152 output_loc_sequence_raw (dw_loc_descr_ref loc)
5153 {
5154   while (1)
5155     {
5156       /* Output the opcode.  */
5157       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5158       output_loc_operands_raw (loc);
5159
5160       if (!loc->dw_loc_next)
5161         break;
5162       loc = loc->dw_loc_next;
5163
5164       fputc (',', asm_out_file);
5165     }
5166 }
5167
5168 /* This routine will generate the correct assembly data for a location
5169    description based on a cfi entry with a complex address.  */
5170
5171 static void
5172 output_cfa_loc (dw_cfi_ref cfi)
5173 {
5174   dw_loc_descr_ref loc;
5175   unsigned long size;
5176
5177   if (cfi->dw_cfi_opc == DW_CFA_expression)
5178     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5179
5180   /* Output the size of the block.  */
5181   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5182   size = size_of_locs (loc);
5183   dw2_asm_output_data_uleb128 (size, NULL);
5184
5185   /* Now output the operations themselves.  */
5186   output_loc_sequence (loc);
5187 }
5188
5189 /* Similar, but used for .cfi_escape.  */
5190
5191 static void
5192 output_cfa_loc_raw (dw_cfi_ref cfi)
5193 {
5194   dw_loc_descr_ref loc;
5195   unsigned long size;
5196
5197   if (cfi->dw_cfi_opc == DW_CFA_expression)
5198     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5199
5200   /* Output the size of the block.  */
5201   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5202   size = size_of_locs (loc);
5203   dw2_asm_output_data_uleb128_raw (size);
5204   fputc (',', asm_out_file);
5205
5206   /* Now output the operations themselves.  */
5207   output_loc_sequence_raw (loc);
5208 }
5209
5210 /* This function builds a dwarf location descriptor sequence from a
5211    dw_cfa_location, adding the given OFFSET to the result of the
5212    expression.  */
5213
5214 static struct dw_loc_descr_struct *
5215 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5216 {
5217   struct dw_loc_descr_struct *head, *tmp;
5218
5219   offset += cfa->offset;
5220
5221   if (cfa->indirect)
5222     {
5223       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5224       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5225       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5226       add_loc_descr (&head, tmp);
5227       if (offset != 0)
5228         {
5229           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5230           add_loc_descr (&head, tmp);
5231         }
5232     }
5233   else
5234     head = new_reg_loc_descr (cfa->reg, offset);
5235
5236   return head;
5237 }
5238
5239 /* This function builds a dwarf location descriptor sequence for
5240    the address at OFFSET from the CFA when stack is aligned to
5241    ALIGNMENT byte.  */
5242
5243 static struct dw_loc_descr_struct *
5244 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5245 {
5246   struct dw_loc_descr_struct *head;
5247   unsigned int dwarf_fp
5248     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5249
5250  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5251   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5252     {
5253       head = new_reg_loc_descr (dwarf_fp, 0);
5254       add_loc_descr (&head, int_loc_descriptor (alignment));
5255       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5256       loc_descr_plus_const (&head, offset);
5257     }
5258   else
5259     head = new_reg_loc_descr (dwarf_fp, offset);
5260   return head;
5261 }
5262
5263 /* This function fills in aa dw_cfa_location structure from a dwarf location
5264    descriptor sequence.  */
5265
5266 static void
5267 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5268 {
5269   struct dw_loc_descr_struct *ptr;
5270   cfa->offset = 0;
5271   cfa->base_offset = 0;
5272   cfa->indirect = 0;
5273   cfa->reg = -1;
5274
5275   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5276     {
5277       enum dwarf_location_atom op = ptr->dw_loc_opc;
5278
5279       switch (op)
5280         {
5281         case DW_OP_reg0:
5282         case DW_OP_reg1:
5283         case DW_OP_reg2:
5284         case DW_OP_reg3:
5285         case DW_OP_reg4:
5286         case DW_OP_reg5:
5287         case DW_OP_reg6:
5288         case DW_OP_reg7:
5289         case DW_OP_reg8:
5290         case DW_OP_reg9:
5291         case DW_OP_reg10:
5292         case DW_OP_reg11:
5293         case DW_OP_reg12:
5294         case DW_OP_reg13:
5295         case DW_OP_reg14:
5296         case DW_OP_reg15:
5297         case DW_OP_reg16:
5298         case DW_OP_reg17:
5299         case DW_OP_reg18:
5300         case DW_OP_reg19:
5301         case DW_OP_reg20:
5302         case DW_OP_reg21:
5303         case DW_OP_reg22:
5304         case DW_OP_reg23:
5305         case DW_OP_reg24:
5306         case DW_OP_reg25:
5307         case DW_OP_reg26:
5308         case DW_OP_reg27:
5309         case DW_OP_reg28:
5310         case DW_OP_reg29:
5311         case DW_OP_reg30:
5312         case DW_OP_reg31:
5313           cfa->reg = op - DW_OP_reg0;
5314           break;
5315         case DW_OP_regx:
5316           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5317           break;
5318         case DW_OP_breg0:
5319         case DW_OP_breg1:
5320         case DW_OP_breg2:
5321         case DW_OP_breg3:
5322         case DW_OP_breg4:
5323         case DW_OP_breg5:
5324         case DW_OP_breg6:
5325         case DW_OP_breg7:
5326         case DW_OP_breg8:
5327         case DW_OP_breg9:
5328         case DW_OP_breg10:
5329         case DW_OP_breg11:
5330         case DW_OP_breg12:
5331         case DW_OP_breg13:
5332         case DW_OP_breg14:
5333         case DW_OP_breg15:
5334         case DW_OP_breg16:
5335         case DW_OP_breg17:
5336         case DW_OP_breg18:
5337         case DW_OP_breg19:
5338         case DW_OP_breg20:
5339         case DW_OP_breg21:
5340         case DW_OP_breg22:
5341         case DW_OP_breg23:
5342         case DW_OP_breg24:
5343         case DW_OP_breg25:
5344         case DW_OP_breg26:
5345         case DW_OP_breg27:
5346         case DW_OP_breg28:
5347         case DW_OP_breg29:
5348         case DW_OP_breg30:
5349         case DW_OP_breg31:
5350           cfa->reg = op - DW_OP_breg0;
5351           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5352           break;
5353         case DW_OP_bregx:
5354           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5355           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5356           break;
5357         case DW_OP_deref:
5358           cfa->indirect = 1;
5359           break;
5360         case DW_OP_plus_uconst:
5361           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5362           break;
5363         default:
5364           internal_error ("DW_LOC_OP %s not implemented",
5365                           dwarf_stack_op_name (ptr->dw_loc_opc));
5366         }
5367     }
5368 }
5369 #endif /* .debug_frame support */
5370 \f
5371 /* And now, the support for symbolic debugging information.  */
5372 #ifdef DWARF2_DEBUGGING_INFO
5373
5374 /* .debug_str support.  */
5375 static int output_indirect_string (void **, void *);
5376
5377 static void dwarf2out_init (const char *);
5378 static void dwarf2out_finish (const char *);
5379 static void dwarf2out_define (unsigned int, const char *);
5380 static void dwarf2out_undef (unsigned int, const char *);
5381 static void dwarf2out_start_source_file (unsigned, const char *);
5382 static void dwarf2out_end_source_file (unsigned);
5383 static void dwarf2out_begin_block (unsigned, unsigned);
5384 static void dwarf2out_end_block (unsigned, unsigned);
5385 static bool dwarf2out_ignore_block (const_tree);
5386 static void dwarf2out_global_decl (tree);
5387 static void dwarf2out_type_decl (tree, int);
5388 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5389 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5390                                                  dw_die_ref);
5391 static void dwarf2out_abstract_function (tree);
5392 static void dwarf2out_var_location (rtx);
5393 static void dwarf2out_begin_function (tree);
5394 static void dwarf2out_set_name (tree, tree);
5395
5396 /* The debug hooks structure.  */
5397
5398 const struct gcc_debug_hooks dwarf2_debug_hooks =
5399 {
5400   dwarf2out_init,
5401   dwarf2out_finish,
5402   dwarf2out_define,
5403   dwarf2out_undef,
5404   dwarf2out_start_source_file,
5405   dwarf2out_end_source_file,
5406   dwarf2out_begin_block,
5407   dwarf2out_end_block,
5408   dwarf2out_ignore_block,
5409   dwarf2out_source_line,
5410   dwarf2out_begin_prologue,
5411   debug_nothing_int_charstar,   /* end_prologue */
5412   dwarf2out_end_epilogue,
5413   dwarf2out_begin_function,
5414   debug_nothing_int,            /* end_function */
5415   dwarf2out_decl,               /* function_decl */
5416   dwarf2out_global_decl,
5417   dwarf2out_type_decl,          /* type_decl */
5418   dwarf2out_imported_module_or_decl,
5419   debug_nothing_tree,           /* deferred_inline_function */
5420   /* The DWARF 2 backend tries to reduce debugging bloat by not
5421      emitting the abstract description of inline functions until
5422      something tries to reference them.  */
5423   dwarf2out_abstract_function,  /* outlining_inline_function */
5424   debug_nothing_rtx,            /* label */
5425   debug_nothing_int,            /* handle_pch */
5426   dwarf2out_var_location,
5427   dwarf2out_switch_text_section,
5428   dwarf2out_set_name,
5429   1                             /* start_end_main_source_file */
5430 };
5431 #endif
5432 \f
5433 /* NOTE: In the comments in this file, many references are made to
5434    "Debugging Information Entries".  This term is abbreviated as `DIE'
5435    throughout the remainder of this file.  */
5436
5437 /* An internal representation of the DWARF output is built, and then
5438    walked to generate the DWARF debugging info.  The walk of the internal
5439    representation is done after the entire program has been compiled.
5440    The types below are used to describe the internal representation.  */
5441
5442 /* Various DIE's use offsets relative to the beginning of the
5443    .debug_info section to refer to each other.  */
5444
5445 typedef long int dw_offset;
5446
5447 /* Define typedefs here to avoid circular dependencies.  */
5448
5449 typedef struct dw_attr_struct *dw_attr_ref;
5450 typedef struct dw_line_info_struct *dw_line_info_ref;
5451 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5452 typedef struct pubname_struct *pubname_ref;
5453 typedef struct dw_ranges_struct *dw_ranges_ref;
5454 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5455
5456 /* Each entry in the line_info_table maintains the file and
5457    line number associated with the label generated for that
5458    entry.  The label gives the PC value associated with
5459    the line number entry.  */
5460
5461 typedef struct GTY(()) dw_line_info_struct {
5462   unsigned long dw_file_num;
5463   unsigned long dw_line_num;
5464 }
5465 dw_line_info_entry;
5466
5467 /* Line information for functions in separate sections; each one gets its
5468    own sequence.  */
5469 typedef struct GTY(()) dw_separate_line_info_struct {
5470   unsigned long dw_file_num;
5471   unsigned long dw_line_num;
5472   unsigned long function;
5473 }
5474 dw_separate_line_info_entry;
5475
5476 /* Each DIE attribute has a field specifying the attribute kind,
5477    a link to the next attribute in the chain, and an attribute value.
5478    Attributes are typically linked below the DIE they modify.  */
5479
5480 typedef struct GTY(()) dw_attr_struct {
5481   enum dwarf_attribute dw_attr;
5482   dw_val_node dw_attr_val;
5483 }
5484 dw_attr_node;
5485
5486 DEF_VEC_O(dw_attr_node);
5487 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5488
5489 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5490    The children of each node form a circular list linked by
5491    die_sib.  die_child points to the node *before* the "first" child node.  */
5492
5493 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5494   enum dwarf_tag die_tag;
5495   char *die_symbol;
5496   VEC(dw_attr_node,gc) * die_attr;
5497   dw_die_ref die_parent;
5498   dw_die_ref die_child;
5499   dw_die_ref die_sib;
5500   dw_die_ref die_definition; /* ref from a specification to its definition */
5501   dw_offset die_offset;
5502   unsigned long die_abbrev;
5503   int die_mark;
5504   /* Die is used and must not be pruned as unused.  */
5505   int die_perennial_p;
5506   unsigned int decl_id;
5507 }
5508 die_node;
5509
5510 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5511 #define FOR_EACH_CHILD(die, c, expr) do {       \
5512   c = die->die_child;                           \
5513   if (c) do {                                   \
5514     c = c->die_sib;                             \
5515     expr;                                       \
5516   } while (c != die->die_child);                \
5517 } while (0)
5518
5519 /* The pubname structure */
5520
5521 typedef struct GTY(()) pubname_struct {
5522   dw_die_ref die;
5523   const char *name;
5524 }
5525 pubname_entry;
5526
5527 DEF_VEC_O(pubname_entry);
5528 DEF_VEC_ALLOC_O(pubname_entry, gc);
5529
5530 struct GTY(()) dw_ranges_struct {
5531   /* If this is positive, it's a block number, otherwise it's a
5532      bitwise-negated index into dw_ranges_by_label.  */
5533   int num;
5534 };
5535
5536 struct GTY(()) dw_ranges_by_label_struct {
5537   const char *begin;
5538   const char *end;
5539 };
5540
5541 /* The limbo die list structure.  */
5542 typedef struct GTY(()) limbo_die_struct {
5543   dw_die_ref die;
5544   tree created_for;
5545   struct limbo_die_struct *next;
5546 }
5547 limbo_die_node;
5548
5549 /* How to start an assembler comment.  */
5550 #ifndef ASM_COMMENT_START
5551 #define ASM_COMMENT_START ";#"
5552 #endif
5553
5554 /* Define a macro which returns nonzero for a TYPE_DECL which was
5555    implicitly generated for a tagged type.
5556
5557    Note that unlike the gcc front end (which generates a NULL named
5558    TYPE_DECL node for each complete tagged type, each array type, and
5559    each function type node created) the g++ front end generates a
5560    _named_ TYPE_DECL node for each tagged type node created.
5561    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5562    generate a DW_TAG_typedef DIE for them.  */
5563
5564 #define TYPE_DECL_IS_STUB(decl)                         \
5565   (DECL_NAME (decl) == NULL_TREE                        \
5566    || (DECL_ARTIFICIAL (decl)                           \
5567        && is_tagged_type (TREE_TYPE (decl))             \
5568        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5569            /* This is necessary for stub decls that     \
5570               appear in nested inline functions.  */    \
5571            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5572                && (decl_ultimate_origin (decl)          \
5573                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5574
5575 /* Information concerning the compilation unit's programming
5576    language, and compiler version.  */
5577
5578 /* Fixed size portion of the DWARF compilation unit header.  */
5579 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5580   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5581
5582 /* Fixed size portion of public names info.  */
5583 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5584
5585 /* Fixed size portion of the address range info.  */
5586 #define DWARF_ARANGES_HEADER_SIZE                                       \
5587   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5588                 DWARF2_ADDR_SIZE * 2)                                   \
5589    - DWARF_INITIAL_LENGTH_SIZE)
5590
5591 /* Size of padding portion in the address range info.  It must be
5592    aligned to twice the pointer size.  */
5593 #define DWARF_ARANGES_PAD_SIZE \
5594   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5595                 DWARF2_ADDR_SIZE * 2)                              \
5596    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5597
5598 /* Use assembler line directives if available.  */
5599 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5600 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5601 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5602 #else
5603 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5604 #endif
5605 #endif
5606
5607 /* Minimum line offset in a special line info. opcode.
5608    This value was chosen to give a reasonable range of values.  */
5609 #define DWARF_LINE_BASE  -10
5610
5611 /* First special line opcode - leave room for the standard opcodes.  */
5612 #define DWARF_LINE_OPCODE_BASE  10
5613
5614 /* Range of line offsets in a special line info. opcode.  */
5615 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5616
5617 /* Flag that indicates the initial value of the is_stmt_start flag.
5618    In the present implementation, we do not mark any lines as
5619    the beginning of a source statement, because that information
5620    is not made available by the GCC front-end.  */
5621 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5622
5623 #ifdef DWARF2_DEBUGGING_INFO
5624 /* This location is used by calc_die_sizes() to keep track
5625    the offset of each DIE within the .debug_info section.  */
5626 static unsigned long next_die_offset;
5627 #endif
5628
5629 /* Record the root of the DIE's built for the current compilation unit.  */
5630 static GTY(()) dw_die_ref comp_unit_die;
5631
5632 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5633 static GTY(()) limbo_die_node *limbo_die_list;
5634
5635 /* A list of DIEs for which we may have to generate
5636    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5637    set.  */
5638 static GTY(()) limbo_die_node *deferred_asm_name;
5639
5640 /* Filenames referenced by this compilation unit.  */
5641 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5642
5643 /* A hash table of references to DIE's that describe declarations.
5644    The key is a DECL_UID() which is a unique number identifying each decl.  */
5645 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5646
5647 /* A hash table of references to DIE's that describe COMMON blocks.
5648    The key is DECL_UID() ^ die_parent.  */
5649 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5650
5651 typedef struct GTY(()) die_arg_entry_struct {
5652     dw_die_ref die;
5653     tree arg;
5654 } die_arg_entry;
5655
5656 DEF_VEC_O(die_arg_entry);
5657 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5658
5659 /* Node of the variable location list.  */
5660 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5661   rtx GTY (()) var_loc_note;
5662   const char * GTY (()) label;
5663   const char * GTY (()) section_label;
5664   struct var_loc_node * GTY (()) next;
5665 };
5666
5667 /* Variable location list.  */
5668 struct GTY (()) var_loc_list_def {
5669   struct var_loc_node * GTY (()) first;
5670
5671   /* Do not mark the last element of the chained list because
5672      it is marked through the chain.  */
5673   struct var_loc_node * GTY ((skip ("%h"))) last;
5674
5675   /* DECL_UID of the variable decl.  */
5676   unsigned int decl_id;
5677 };
5678 typedef struct var_loc_list_def var_loc_list;
5679
5680
5681 /* Table of decl location linked lists.  */
5682 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5683
5684 /* A pointer to the base of a list of references to DIE's that
5685    are uniquely identified by their tag, presence/absence of
5686    children DIE's, and list of attribute/value pairs.  */
5687 static GTY((length ("abbrev_die_table_allocated")))
5688   dw_die_ref *abbrev_die_table;
5689
5690 /* Number of elements currently allocated for abbrev_die_table.  */
5691 static GTY(()) unsigned abbrev_die_table_allocated;
5692
5693 /* Number of elements in type_die_table currently in use.  */
5694 static GTY(()) unsigned abbrev_die_table_in_use;
5695
5696 /* Size (in elements) of increments by which we may expand the
5697    abbrev_die_table.  */
5698 #define ABBREV_DIE_TABLE_INCREMENT 256
5699
5700 /* A pointer to the base of a table that contains line information
5701    for each source code line in .text in the compilation unit.  */
5702 static GTY((length ("line_info_table_allocated")))
5703      dw_line_info_ref line_info_table;
5704
5705 /* Number of elements currently allocated for line_info_table.  */
5706 static GTY(()) unsigned line_info_table_allocated;
5707
5708 /* Number of elements in line_info_table currently in use.  */
5709 static GTY(()) unsigned line_info_table_in_use;
5710
5711 /* A pointer to the base of a table that contains line information
5712    for each source code line outside of .text in the compilation unit.  */
5713 static GTY ((length ("separate_line_info_table_allocated")))
5714      dw_separate_line_info_ref separate_line_info_table;
5715
5716 /* Number of elements currently allocated for separate_line_info_table.  */
5717 static GTY(()) unsigned separate_line_info_table_allocated;
5718
5719 /* Number of elements in separate_line_info_table currently in use.  */
5720 static GTY(()) unsigned separate_line_info_table_in_use;
5721
5722 /* Size (in elements) of increments by which we may expand the
5723    line_info_table.  */
5724 #define LINE_INFO_TABLE_INCREMENT 1024
5725
5726 /* A pointer to the base of a table that contains a list of publicly
5727    accessible names.  */
5728 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5729
5730 /* A pointer to the base of a table that contains a list of publicly
5731    accessible types.  */
5732 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5733
5734 /* Array of dies for which we should generate .debug_arange info.  */
5735 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5736
5737 /* Number of elements currently allocated for arange_table.  */
5738 static GTY(()) unsigned arange_table_allocated;
5739
5740 /* Number of elements in arange_table currently in use.  */
5741 static GTY(()) unsigned arange_table_in_use;
5742
5743 /* Size (in elements) of increments by which we may expand the
5744    arange_table.  */
5745 #define ARANGE_TABLE_INCREMENT 64
5746
5747 /* Array of dies for which we should generate .debug_ranges info.  */
5748 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5749
5750 /* Number of elements currently allocated for ranges_table.  */
5751 static GTY(()) unsigned ranges_table_allocated;
5752
5753 /* Number of elements in ranges_table currently in use.  */
5754 static GTY(()) unsigned ranges_table_in_use;
5755
5756 /* Array of pairs of labels referenced in ranges_table.  */
5757 static GTY ((length ("ranges_by_label_allocated")))
5758      dw_ranges_by_label_ref ranges_by_label;
5759
5760 /* Number of elements currently allocated for ranges_by_label.  */
5761 static GTY(()) unsigned ranges_by_label_allocated;
5762
5763 /* Number of elements in ranges_by_label currently in use.  */
5764 static GTY(()) unsigned ranges_by_label_in_use;
5765
5766 /* Size (in elements) of increments by which we may expand the
5767    ranges_table.  */
5768 #define RANGES_TABLE_INCREMENT 64
5769
5770 /* Whether we have location lists that need outputting */
5771 static GTY(()) bool have_location_lists;
5772
5773 /* Unique label counter.  */
5774 static GTY(()) unsigned int loclabel_num;
5775
5776 #ifdef DWARF2_DEBUGGING_INFO
5777 /* Record whether the function being analyzed contains inlined functions.  */
5778 static int current_function_has_inlines;
5779 #endif
5780 #if 0 && defined (MIPS_DEBUGGING_INFO)
5781 static int comp_unit_has_inlines;
5782 #endif
5783
5784 /* The last file entry emitted by maybe_emit_file().  */
5785 static GTY(()) struct dwarf_file_data * last_emitted_file;
5786
5787 /* Number of internal labels generated by gen_internal_sym().  */
5788 static GTY(()) int label_num;
5789
5790 /* Cached result of previous call to lookup_filename.  */
5791 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5792
5793 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5794
5795 #ifdef DWARF2_DEBUGGING_INFO
5796
5797 /* Offset from the "steady-state frame pointer" to the frame base,
5798    within the current function.  */
5799 static HOST_WIDE_INT frame_pointer_fb_offset;
5800
5801 /* Forward declarations for functions defined in this file.  */
5802
5803 static int is_pseudo_reg (const_rtx);
5804 static tree type_main_variant (tree);
5805 static int is_tagged_type (const_tree);
5806 static const char *dwarf_tag_name (unsigned);
5807 static const char *dwarf_attr_name (unsigned);
5808 static const char *dwarf_form_name (unsigned);
5809 static tree decl_ultimate_origin (const_tree);
5810 static tree decl_class_context (tree);
5811 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5812 static inline enum dw_val_class AT_class (dw_attr_ref);
5813 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5814 static inline unsigned AT_flag (dw_attr_ref);
5815 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5816 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5817 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5818 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5819 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, rtx);
5820 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5821                                unsigned int, unsigned char *);
5822 static hashval_t debug_str_do_hash (const void *);
5823 static int debug_str_eq (const void *, const void *);
5824 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5825 static inline const char *AT_string (dw_attr_ref);
5826 static enum dwarf_form AT_string_form (dw_attr_ref);
5827 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5828 static void add_AT_specification (dw_die_ref, dw_die_ref);
5829 static inline dw_die_ref AT_ref (dw_attr_ref);
5830 static inline int AT_ref_external (dw_attr_ref);
5831 static inline void set_AT_ref_external (dw_attr_ref, int);
5832 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5833 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5834 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5835 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5836                              dw_loc_list_ref);
5837 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5838 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5839 static inline rtx AT_addr (dw_attr_ref);
5840 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5841 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5842 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5843 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5844                            unsigned HOST_WIDE_INT);
5845 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5846                                unsigned long);
5847 static inline const char *AT_lbl (dw_attr_ref);
5848 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5849 static const char *get_AT_low_pc (dw_die_ref);
5850 static const char *get_AT_hi_pc (dw_die_ref);
5851 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5852 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5853 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5854 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5855 static bool is_c_family (void);
5856 static bool is_cxx (void);
5857 static bool is_java (void);
5858 static bool is_fortran (void);
5859 static bool is_ada (void);
5860 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5861 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5862 static void add_child_die (dw_die_ref, dw_die_ref);
5863 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5864 static dw_die_ref lookup_type_die (tree);
5865 static void equate_type_number_to_die (tree, dw_die_ref);
5866 static hashval_t decl_die_table_hash (const void *);
5867 static int decl_die_table_eq (const void *, const void *);
5868 static dw_die_ref lookup_decl_die (tree);
5869 static hashval_t common_block_die_table_hash (const void *);
5870 static int common_block_die_table_eq (const void *, const void *);
5871 static hashval_t decl_loc_table_hash (const void *);
5872 static int decl_loc_table_eq (const void *, const void *);
5873 static var_loc_list *lookup_decl_loc (const_tree);
5874 static void equate_decl_number_to_die (tree, dw_die_ref);
5875 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5876 static void print_spaces (FILE *);
5877 static void print_die (dw_die_ref, FILE *);
5878 static void print_dwarf_line_table (FILE *);
5879 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5880 static dw_die_ref pop_compile_unit (dw_die_ref);
5881 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5882 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5883 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5884 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5885 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5886 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5887 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5888 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5889 static void compute_section_prefix (dw_die_ref);
5890 static int is_type_die (dw_die_ref);
5891 static int is_comdat_die (dw_die_ref);
5892 static int is_symbol_die (dw_die_ref);
5893 static void assign_symbol_names (dw_die_ref);
5894 static void break_out_includes (dw_die_ref);
5895 static hashval_t htab_cu_hash (const void *);
5896 static int htab_cu_eq (const void *, const void *);
5897 static void htab_cu_del (void *);
5898 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5899 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5900 static void add_sibling_attributes (dw_die_ref);
5901 static void build_abbrev_table (dw_die_ref);
5902 static void output_location_lists (dw_die_ref);
5903 static int constant_size (unsigned HOST_WIDE_INT);
5904 static unsigned long size_of_die (dw_die_ref);
5905 static void calc_die_sizes (dw_die_ref);
5906 static void mark_dies (dw_die_ref);
5907 static void unmark_dies (dw_die_ref);
5908 static void unmark_all_dies (dw_die_ref);
5909 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5910 static unsigned long size_of_aranges (void);
5911 static enum dwarf_form value_format (dw_attr_ref);
5912 static void output_value_format (dw_attr_ref);
5913 static void output_abbrev_section (void);
5914 static void output_die_symbol (dw_die_ref);
5915 static void output_die (dw_die_ref);
5916 static void output_compilation_unit_header (void);
5917 static void output_comp_unit (dw_die_ref, int);
5918 static const char *dwarf2_name (tree, int);
5919 static void add_pubname (tree, dw_die_ref);
5920 static void add_pubname_string (const char *, dw_die_ref);
5921 static void add_pubtype (tree, dw_die_ref);
5922 static void output_pubnames (VEC (pubname_entry,gc) *);
5923 static void add_arange (tree, dw_die_ref);
5924 static void output_aranges (void);
5925 static unsigned int add_ranges_num (int);
5926 static unsigned int add_ranges (const_tree);
5927 static unsigned int add_ranges_by_labels (const char *, const char *);
5928 static void output_ranges (void);
5929 static void output_line_info (void);
5930 static void output_file_names (void);
5931 static dw_die_ref base_type_die (tree);
5932 static int is_base_type (tree);
5933 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5934 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5935 static dw_die_ref generic_parameter_die (tree, tree, dw_die_ref, int);
5936 static int type_is_enum (const_tree);
5937 static unsigned int dbx_reg_number (const_rtx);
5938 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5939 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5940 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5941                                                 enum var_init_status);
5942 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5943                                                      enum var_init_status);
5944 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5945                                          enum var_init_status);
5946 static int is_based_loc (const_rtx);
5947 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5948                                             enum var_init_status);
5949 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5950                                                enum var_init_status);
5951 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
5952                                         enum var_init_status);
5953 static dw_loc_list_ref loc_list_from_tree (tree, int);
5954 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
5955 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5956 static tree field_type (const_tree);
5957 static unsigned int simple_type_align_in_bits (const_tree);
5958 static unsigned int simple_decl_align_in_bits (const_tree);
5959 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5960 static HOST_WIDE_INT field_byte_offset (const_tree);
5961 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5962                                          dw_loc_list_ref);
5963 static void add_data_member_location_attribute (dw_die_ref, tree);
5964 static bool add_const_value_attribute (dw_die_ref, rtx);
5965 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5966 static void insert_float (const_rtx, unsigned char *);
5967 static rtx rtl_for_decl_location (tree);
5968 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
5969                                                    enum dwarf_attribute);
5970 static bool tree_add_const_value_attribute (dw_die_ref, tree);
5971 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
5972 static void add_name_attribute (dw_die_ref, const char *);
5973 static void add_comp_dir_attribute (dw_die_ref);
5974 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5975 static void add_subscript_info (dw_die_ref, tree, bool);
5976 static void add_byte_size_attribute (dw_die_ref, tree);
5977 static void add_bit_offset_attribute (dw_die_ref, tree);
5978 static void add_bit_size_attribute (dw_die_ref, tree);
5979 static void add_prototyped_attribute (dw_die_ref, tree);
5980 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5981 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5982 static void add_src_coords_attributes (dw_die_ref, tree);
5983 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5984 static void push_decl_scope (tree);
5985 static void pop_decl_scope (void);
5986 static dw_die_ref scope_die_for (tree, dw_die_ref);
5987 static inline int local_scope_p (dw_die_ref);
5988 static inline int class_scope_p (dw_die_ref);
5989 static inline int class_or_namespace_scope_p (dw_die_ref);
5990 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5991 static void add_calling_convention_attribute (dw_die_ref, tree);
5992 static const char *type_tag (const_tree);
5993 static tree member_declared_type (const_tree);
5994 #if 0
5995 static const char *decl_start_label (tree);
5996 #endif
5997 static void gen_array_type_die (tree, dw_die_ref);
5998 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5999 #if 0
6000 static void gen_entry_point_die (tree, dw_die_ref);
6001 #endif
6002 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6003 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
6004 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6005 static void gen_formal_types_die (tree, dw_die_ref);
6006 static void gen_subprogram_die (tree, dw_die_ref);
6007 static void gen_variable_die (tree, tree, dw_die_ref);
6008 static void gen_const_die (tree, dw_die_ref);
6009 static void gen_label_die (tree, dw_die_ref);
6010 static void gen_lexical_block_die (tree, dw_die_ref, int);
6011 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6012 static void gen_field_die (tree, dw_die_ref);
6013 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6014 static dw_die_ref gen_compile_unit_die (const char *);
6015 static void gen_inheritance_die (tree, tree, dw_die_ref);
6016 static void gen_member_die (tree, dw_die_ref);
6017 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6018                                                 enum debug_info_usage);
6019 static void gen_subroutine_type_die (tree, dw_die_ref);
6020 static void gen_typedef_die (tree, dw_die_ref);
6021 static void gen_type_die (tree, dw_die_ref);
6022 static void gen_block_die (tree, dw_die_ref, int);
6023 static void decls_for_scope (tree, dw_die_ref, int);
6024 static int is_redundant_typedef (const_tree);
6025 static inline dw_die_ref get_context_die (tree);
6026 static void gen_namespace_die (tree, dw_die_ref);
6027 static void gen_decl_die (tree, tree, dw_die_ref);
6028 static dw_die_ref force_decl_die (tree);
6029 static dw_die_ref force_type_die (tree);
6030 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6031 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6032 static struct dwarf_file_data * lookup_filename (const char *);
6033 static void retry_incomplete_types (void);
6034 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6035 static tree make_ith_pack_parameter_name (tree, int);
6036 static void gen_generic_params_dies (tree);
6037 static void splice_child_die (dw_die_ref, dw_die_ref);
6038 static int file_info_cmp (const void *, const void *);
6039 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6040                                      const char *, const char *, unsigned);
6041 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
6042                                        const char *, const char *,
6043                                        const char *);
6044 static void output_loc_list (dw_loc_list_ref);
6045 static char *gen_internal_sym (const char *);
6046
6047 static void prune_unmark_dies (dw_die_ref);
6048 static void prune_unused_types_mark (dw_die_ref, int);
6049 static void prune_unused_types_walk (dw_die_ref);
6050 static void prune_unused_types_walk_attribs (dw_die_ref);
6051 static void prune_unused_types_prune (dw_die_ref);
6052 static void prune_unused_types (void);
6053 static int maybe_emit_file (struct dwarf_file_data *fd);
6054 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6055 static void gen_remaining_tmpl_value_param_die_attribute (void);
6056
6057 /* Section names used to hold DWARF debugging information.  */
6058 #ifndef DEBUG_INFO_SECTION
6059 #define DEBUG_INFO_SECTION      ".debug_info"
6060 #endif
6061 #ifndef DEBUG_ABBREV_SECTION
6062 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6063 #endif
6064 #ifndef DEBUG_ARANGES_SECTION
6065 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6066 #endif
6067 #ifndef DEBUG_MACINFO_SECTION
6068 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6069 #endif
6070 #ifndef DEBUG_LINE_SECTION
6071 #define DEBUG_LINE_SECTION      ".debug_line"
6072 #endif
6073 #ifndef DEBUG_LOC_SECTION
6074 #define DEBUG_LOC_SECTION       ".debug_loc"
6075 #endif
6076 #ifndef DEBUG_PUBNAMES_SECTION
6077 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6078 #endif
6079 #ifndef DEBUG_PUBTYPES_SECTION
6080 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6081 #endif
6082 #ifndef DEBUG_STR_SECTION
6083 #define DEBUG_STR_SECTION       ".debug_str"
6084 #endif
6085 #ifndef DEBUG_RANGES_SECTION
6086 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6087 #endif
6088
6089 /* Standard ELF section names for compiled code and data.  */
6090 #ifndef TEXT_SECTION_NAME
6091 #define TEXT_SECTION_NAME       ".text"
6092 #endif
6093
6094 /* Section flags for .debug_str section.  */
6095 #define DEBUG_STR_SECTION_FLAGS \
6096   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6097    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6098    : SECTION_DEBUG)
6099
6100 /* Labels we insert at beginning sections we can reference instead of
6101    the section names themselves.  */
6102
6103 #ifndef TEXT_SECTION_LABEL
6104 #define TEXT_SECTION_LABEL              "Ltext"
6105 #endif
6106 #ifndef COLD_TEXT_SECTION_LABEL
6107 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6108 #endif
6109 #ifndef DEBUG_LINE_SECTION_LABEL
6110 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6111 #endif
6112 #ifndef DEBUG_INFO_SECTION_LABEL
6113 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6114 #endif
6115 #ifndef DEBUG_ABBREV_SECTION_LABEL
6116 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6117 #endif
6118 #ifndef DEBUG_LOC_SECTION_LABEL
6119 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6120 #endif
6121 #ifndef DEBUG_RANGES_SECTION_LABEL
6122 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6123 #endif
6124 #ifndef DEBUG_MACINFO_SECTION_LABEL
6125 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6126 #endif
6127
6128 /* Definitions of defaults for formats and names of various special
6129    (artificial) labels which may be generated within this file (when the -g
6130    options is used and DWARF2_DEBUGGING_INFO is in effect.
6131    If necessary, these may be overridden from within the tm.h file, but
6132    typically, overriding these defaults is unnecessary.  */
6133
6134 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6135 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6136 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6137 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6138 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6139 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6140 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6141 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6142 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6143 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6144
6145 #ifndef TEXT_END_LABEL
6146 #define TEXT_END_LABEL          "Letext"
6147 #endif
6148 #ifndef COLD_END_LABEL
6149 #define COLD_END_LABEL          "Letext_cold"
6150 #endif
6151 #ifndef BLOCK_BEGIN_LABEL
6152 #define BLOCK_BEGIN_LABEL       "LBB"
6153 #endif
6154 #ifndef BLOCK_END_LABEL
6155 #define BLOCK_END_LABEL         "LBE"
6156 #endif
6157 #ifndef LINE_CODE_LABEL
6158 #define LINE_CODE_LABEL         "LM"
6159 #endif
6160 #ifndef SEPARATE_LINE_CODE_LABEL
6161 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6162 #endif
6163
6164 \f
6165 /* We allow a language front-end to designate a function that is to be
6166    called to "demangle" any name before it is put into a DIE.  */
6167
6168 static const char *(*demangle_name_func) (const char *);
6169
6170 void
6171 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6172 {
6173   demangle_name_func = func;
6174 }
6175
6176 /* Test if rtl node points to a pseudo register.  */
6177
6178 static inline int
6179 is_pseudo_reg (const_rtx rtl)
6180 {
6181   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6182           || (GET_CODE (rtl) == SUBREG
6183               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6184 }
6185
6186 /* Return a reference to a type, with its const and volatile qualifiers
6187    removed.  */
6188
6189 static inline tree
6190 type_main_variant (tree type)
6191 {
6192   type = TYPE_MAIN_VARIANT (type);
6193
6194   /* ??? There really should be only one main variant among any group of
6195      variants of a given type (and all of the MAIN_VARIANT values for all
6196      members of the group should point to that one type) but sometimes the C
6197      front-end messes this up for array types, so we work around that bug
6198      here.  */
6199   if (TREE_CODE (type) == ARRAY_TYPE)
6200     while (type != TYPE_MAIN_VARIANT (type))
6201       type = TYPE_MAIN_VARIANT (type);
6202
6203   return type;
6204 }
6205
6206 /* Return nonzero if the given type node represents a tagged type.  */
6207
6208 static inline int
6209 is_tagged_type (const_tree type)
6210 {
6211   enum tree_code code = TREE_CODE (type);
6212
6213   return (code == RECORD_TYPE || code == UNION_TYPE
6214           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6215 }
6216
6217 /* Convert a DIE tag into its string name.  */
6218
6219 static const char *
6220 dwarf_tag_name (unsigned int tag)
6221 {
6222   switch (tag)
6223     {
6224     case DW_TAG_padding:
6225       return "DW_TAG_padding";
6226     case DW_TAG_array_type:
6227       return "DW_TAG_array_type";
6228     case DW_TAG_class_type:
6229       return "DW_TAG_class_type";
6230     case DW_TAG_entry_point:
6231       return "DW_TAG_entry_point";
6232     case DW_TAG_enumeration_type:
6233       return "DW_TAG_enumeration_type";
6234     case DW_TAG_formal_parameter:
6235       return "DW_TAG_formal_parameter";
6236     case DW_TAG_imported_declaration:
6237       return "DW_TAG_imported_declaration";
6238     case DW_TAG_label:
6239       return "DW_TAG_label";
6240     case DW_TAG_lexical_block:
6241       return "DW_TAG_lexical_block";
6242     case DW_TAG_member:
6243       return "DW_TAG_member";
6244     case DW_TAG_pointer_type:
6245       return "DW_TAG_pointer_type";
6246     case DW_TAG_reference_type:
6247       return "DW_TAG_reference_type";
6248     case DW_TAG_compile_unit:
6249       return "DW_TAG_compile_unit";
6250     case DW_TAG_string_type:
6251       return "DW_TAG_string_type";
6252     case DW_TAG_structure_type:
6253       return "DW_TAG_structure_type";
6254     case DW_TAG_subroutine_type:
6255       return "DW_TAG_subroutine_type";
6256     case DW_TAG_typedef:
6257       return "DW_TAG_typedef";
6258     case DW_TAG_union_type:
6259       return "DW_TAG_union_type";
6260     case DW_TAG_unspecified_parameters:
6261       return "DW_TAG_unspecified_parameters";
6262     case DW_TAG_variant:
6263       return "DW_TAG_variant";
6264     case DW_TAG_common_block:
6265       return "DW_TAG_common_block";
6266     case DW_TAG_common_inclusion:
6267       return "DW_TAG_common_inclusion";
6268     case DW_TAG_inheritance:
6269       return "DW_TAG_inheritance";
6270     case DW_TAG_inlined_subroutine:
6271       return "DW_TAG_inlined_subroutine";
6272     case DW_TAG_module:
6273       return "DW_TAG_module";
6274     case DW_TAG_ptr_to_member_type:
6275       return "DW_TAG_ptr_to_member_type";
6276     case DW_TAG_set_type:
6277       return "DW_TAG_set_type";
6278     case DW_TAG_subrange_type:
6279       return "DW_TAG_subrange_type";
6280     case DW_TAG_with_stmt:
6281       return "DW_TAG_with_stmt";
6282     case DW_TAG_access_declaration:
6283       return "DW_TAG_access_declaration";
6284     case DW_TAG_base_type:
6285       return "DW_TAG_base_type";
6286     case DW_TAG_catch_block:
6287       return "DW_TAG_catch_block";
6288     case DW_TAG_const_type:
6289       return "DW_TAG_const_type";
6290     case DW_TAG_constant:
6291       return "DW_TAG_constant";
6292     case DW_TAG_enumerator:
6293       return "DW_TAG_enumerator";
6294     case DW_TAG_file_type:
6295       return "DW_TAG_file_type";
6296     case DW_TAG_friend:
6297       return "DW_TAG_friend";
6298     case DW_TAG_namelist:
6299       return "DW_TAG_namelist";
6300     case DW_TAG_namelist_item:
6301       return "DW_TAG_namelist_item";
6302     case DW_TAG_packed_type:
6303       return "DW_TAG_packed_type";
6304     case DW_TAG_subprogram:
6305       return "DW_TAG_subprogram";
6306     case DW_TAG_template_type_param:
6307       return "DW_TAG_template_type_param";
6308     case DW_TAG_template_value_param:
6309       return "DW_TAG_template_value_param";
6310     case DW_TAG_thrown_type:
6311       return "DW_TAG_thrown_type";
6312     case DW_TAG_try_block:
6313       return "DW_TAG_try_block";
6314     case DW_TAG_variant_part:
6315       return "DW_TAG_variant_part";
6316     case DW_TAG_variable:
6317       return "DW_TAG_variable";
6318     case DW_TAG_volatile_type:
6319       return "DW_TAG_volatile_type";
6320     case DW_TAG_dwarf_procedure:
6321       return "DW_TAG_dwarf_procedure";
6322     case DW_TAG_restrict_type:
6323       return "DW_TAG_restrict_type";
6324     case DW_TAG_interface_type:
6325       return "DW_TAG_interface_type";
6326     case DW_TAG_namespace:
6327       return "DW_TAG_namespace";
6328     case DW_TAG_imported_module:
6329       return "DW_TAG_imported_module";
6330     case DW_TAG_unspecified_type:
6331       return "DW_TAG_unspecified_type";
6332     case DW_TAG_partial_unit:
6333       return "DW_TAG_partial_unit";
6334     case DW_TAG_imported_unit:
6335       return "DW_TAG_imported_unit";
6336     case DW_TAG_condition:
6337       return "DW_TAG_condition";
6338     case DW_TAG_shared_type:
6339       return "DW_TAG_shared_type";
6340     case DW_TAG_MIPS_loop:
6341       return "DW_TAG_MIPS_loop";
6342     case DW_TAG_format_label:
6343       return "DW_TAG_format_label";
6344     case DW_TAG_function_template:
6345       return "DW_TAG_function_template";
6346     case DW_TAG_class_template:
6347       return "DW_TAG_class_template";
6348     case DW_TAG_GNU_BINCL:
6349       return "DW_TAG_GNU_BINCL";
6350     case DW_TAG_GNU_EINCL:
6351       return "DW_TAG_GNU_EINCL";
6352     case DW_TAG_GNU_template_template_param:
6353       return "DW_TAG_GNU_template_template_param";
6354     default:
6355       return "DW_TAG_<unknown>";
6356     }
6357 }
6358
6359 /* Convert a DWARF attribute code into its string name.  */
6360
6361 static const char *
6362 dwarf_attr_name (unsigned int attr)
6363 {
6364   switch (attr)
6365     {
6366     case DW_AT_sibling:
6367       return "DW_AT_sibling";
6368     case DW_AT_location:
6369       return "DW_AT_location";
6370     case DW_AT_name:
6371       return "DW_AT_name";
6372     case DW_AT_ordering:
6373       return "DW_AT_ordering";
6374     case DW_AT_subscr_data:
6375       return "DW_AT_subscr_data";
6376     case DW_AT_byte_size:
6377       return "DW_AT_byte_size";
6378     case DW_AT_bit_offset:
6379       return "DW_AT_bit_offset";
6380     case DW_AT_bit_size:
6381       return "DW_AT_bit_size";
6382     case DW_AT_element_list:
6383       return "DW_AT_element_list";
6384     case DW_AT_stmt_list:
6385       return "DW_AT_stmt_list";
6386     case DW_AT_low_pc:
6387       return "DW_AT_low_pc";
6388     case DW_AT_high_pc:
6389       return "DW_AT_high_pc";
6390     case DW_AT_language:
6391       return "DW_AT_language";
6392     case DW_AT_member:
6393       return "DW_AT_member";
6394     case DW_AT_discr:
6395       return "DW_AT_discr";
6396     case DW_AT_discr_value:
6397       return "DW_AT_discr_value";
6398     case DW_AT_visibility:
6399       return "DW_AT_visibility";
6400     case DW_AT_import:
6401       return "DW_AT_import";
6402     case DW_AT_string_length:
6403       return "DW_AT_string_length";
6404     case DW_AT_common_reference:
6405       return "DW_AT_common_reference";
6406     case DW_AT_comp_dir:
6407       return "DW_AT_comp_dir";
6408     case DW_AT_const_value:
6409       return "DW_AT_const_value";
6410     case DW_AT_containing_type:
6411       return "DW_AT_containing_type";
6412     case DW_AT_default_value:
6413       return "DW_AT_default_value";
6414     case DW_AT_inline:
6415       return "DW_AT_inline";
6416     case DW_AT_is_optional:
6417       return "DW_AT_is_optional";
6418     case DW_AT_lower_bound:
6419       return "DW_AT_lower_bound";
6420     case DW_AT_producer:
6421       return "DW_AT_producer";
6422     case DW_AT_prototyped:
6423       return "DW_AT_prototyped";
6424     case DW_AT_return_addr:
6425       return "DW_AT_return_addr";
6426     case DW_AT_start_scope:
6427       return "DW_AT_start_scope";
6428     case DW_AT_bit_stride:
6429       return "DW_AT_bit_stride";
6430     case DW_AT_upper_bound:
6431       return "DW_AT_upper_bound";
6432     case DW_AT_abstract_origin:
6433       return "DW_AT_abstract_origin";
6434     case DW_AT_accessibility:
6435       return "DW_AT_accessibility";
6436     case DW_AT_address_class:
6437       return "DW_AT_address_class";
6438     case DW_AT_artificial:
6439       return "DW_AT_artificial";
6440     case DW_AT_base_types:
6441       return "DW_AT_base_types";
6442     case DW_AT_calling_convention:
6443       return "DW_AT_calling_convention";
6444     case DW_AT_count:
6445       return "DW_AT_count";
6446     case DW_AT_data_member_location:
6447       return "DW_AT_data_member_location";
6448     case DW_AT_decl_column:
6449       return "DW_AT_decl_column";
6450     case DW_AT_decl_file:
6451       return "DW_AT_decl_file";
6452     case DW_AT_decl_line:
6453       return "DW_AT_decl_line";
6454     case DW_AT_declaration:
6455       return "DW_AT_declaration";
6456     case DW_AT_discr_list:
6457       return "DW_AT_discr_list";
6458     case DW_AT_encoding:
6459       return "DW_AT_encoding";
6460     case DW_AT_external:
6461       return "DW_AT_external";
6462     case DW_AT_explicit:
6463       return "DW_AT_explicit";
6464     case DW_AT_frame_base:
6465       return "DW_AT_frame_base";
6466     case DW_AT_friend:
6467       return "DW_AT_friend";
6468     case DW_AT_identifier_case:
6469       return "DW_AT_identifier_case";
6470     case DW_AT_macro_info:
6471       return "DW_AT_macro_info";
6472     case DW_AT_namelist_items:
6473       return "DW_AT_namelist_items";
6474     case DW_AT_priority:
6475       return "DW_AT_priority";
6476     case DW_AT_segment:
6477       return "DW_AT_segment";
6478     case DW_AT_specification:
6479       return "DW_AT_specification";
6480     case DW_AT_static_link:
6481       return "DW_AT_static_link";
6482     case DW_AT_type:
6483       return "DW_AT_type";
6484     case DW_AT_use_location:
6485       return "DW_AT_use_location";
6486     case DW_AT_variable_parameter:
6487       return "DW_AT_variable_parameter";
6488     case DW_AT_virtuality:
6489       return "DW_AT_virtuality";
6490     case DW_AT_vtable_elem_location:
6491       return "DW_AT_vtable_elem_location";
6492
6493     case DW_AT_allocated:
6494       return "DW_AT_allocated";
6495     case DW_AT_associated:
6496       return "DW_AT_associated";
6497     case DW_AT_data_location:
6498       return "DW_AT_data_location";
6499     case DW_AT_byte_stride:
6500       return "DW_AT_byte_stride";
6501     case DW_AT_entry_pc:
6502       return "DW_AT_entry_pc";
6503     case DW_AT_use_UTF8:
6504       return "DW_AT_use_UTF8";
6505     case DW_AT_extension:
6506       return "DW_AT_extension";
6507     case DW_AT_ranges:
6508       return "DW_AT_ranges";
6509     case DW_AT_trampoline:
6510       return "DW_AT_trampoline";
6511     case DW_AT_call_column:
6512       return "DW_AT_call_column";
6513     case DW_AT_call_file:
6514       return "DW_AT_call_file";
6515     case DW_AT_call_line:
6516       return "DW_AT_call_line";
6517
6518     case DW_AT_MIPS_fde:
6519       return "DW_AT_MIPS_fde";
6520     case DW_AT_MIPS_loop_begin:
6521       return "DW_AT_MIPS_loop_begin";
6522     case DW_AT_MIPS_tail_loop_begin:
6523       return "DW_AT_MIPS_tail_loop_begin";
6524     case DW_AT_MIPS_epilog_begin:
6525       return "DW_AT_MIPS_epilog_begin";
6526     case DW_AT_MIPS_loop_unroll_factor:
6527       return "DW_AT_MIPS_loop_unroll_factor";
6528     case DW_AT_MIPS_software_pipeline_depth:
6529       return "DW_AT_MIPS_software_pipeline_depth";
6530     case DW_AT_MIPS_linkage_name:
6531       return "DW_AT_MIPS_linkage_name";
6532     case DW_AT_MIPS_stride:
6533       return "DW_AT_MIPS_stride";
6534     case DW_AT_MIPS_abstract_name:
6535       return "DW_AT_MIPS_abstract_name";
6536     case DW_AT_MIPS_clone_origin:
6537       return "DW_AT_MIPS_clone_origin";
6538     case DW_AT_MIPS_has_inlines:
6539       return "DW_AT_MIPS_has_inlines";
6540
6541     case DW_AT_sf_names:
6542       return "DW_AT_sf_names";
6543     case DW_AT_src_info:
6544       return "DW_AT_src_info";
6545     case DW_AT_mac_info:
6546       return "DW_AT_mac_info";
6547     case DW_AT_src_coords:
6548       return "DW_AT_src_coords";
6549     case DW_AT_body_begin:
6550       return "DW_AT_body_begin";
6551     case DW_AT_body_end:
6552       return "DW_AT_body_end";
6553     case DW_AT_GNU_vector:
6554       return "DW_AT_GNU_vector";
6555     case DW_AT_GNU_template_name:
6556       return "DW_AT_GNU_template_name";
6557
6558     case DW_AT_VMS_rtnbeg_pd_address:
6559       return "DW_AT_VMS_rtnbeg_pd_address";
6560
6561     default:
6562       return "DW_AT_<unknown>";
6563     }
6564 }
6565
6566 /* Convert a DWARF value form code into its string name.  */
6567
6568 static const char *
6569 dwarf_form_name (unsigned int form)
6570 {
6571   switch (form)
6572     {
6573     case DW_FORM_addr:
6574       return "DW_FORM_addr";
6575     case DW_FORM_block2:
6576       return "DW_FORM_block2";
6577     case DW_FORM_block4:
6578       return "DW_FORM_block4";
6579     case DW_FORM_data2:
6580       return "DW_FORM_data2";
6581     case DW_FORM_data4:
6582       return "DW_FORM_data4";
6583     case DW_FORM_data8:
6584       return "DW_FORM_data8";
6585     case DW_FORM_string:
6586       return "DW_FORM_string";
6587     case DW_FORM_block:
6588       return "DW_FORM_block";
6589     case DW_FORM_block1:
6590       return "DW_FORM_block1";
6591     case DW_FORM_data1:
6592       return "DW_FORM_data1";
6593     case DW_FORM_flag:
6594       return "DW_FORM_flag";
6595     case DW_FORM_sdata:
6596       return "DW_FORM_sdata";
6597     case DW_FORM_strp:
6598       return "DW_FORM_strp";
6599     case DW_FORM_udata:
6600       return "DW_FORM_udata";
6601     case DW_FORM_ref_addr:
6602       return "DW_FORM_ref_addr";
6603     case DW_FORM_ref1:
6604       return "DW_FORM_ref1";
6605     case DW_FORM_ref2:
6606       return "DW_FORM_ref2";
6607     case DW_FORM_ref4:
6608       return "DW_FORM_ref4";
6609     case DW_FORM_ref8:
6610       return "DW_FORM_ref8";
6611     case DW_FORM_ref_udata:
6612       return "DW_FORM_ref_udata";
6613     case DW_FORM_indirect:
6614       return "DW_FORM_indirect";
6615     default:
6616       return "DW_FORM_<unknown>";
6617     }
6618 }
6619 \f
6620 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6621    instance of an inlined instance of a decl which is local to an inline
6622    function, so we have to trace all of the way back through the origin chain
6623    to find out what sort of node actually served as the original seed for the
6624    given block.  */
6625
6626 static tree
6627 decl_ultimate_origin (const_tree decl)
6628 {
6629   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6630     return NULL_TREE;
6631
6632   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6633      nodes in the function to point to themselves; ignore that if
6634      we're trying to output the abstract instance of this function.  */
6635   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6636     return NULL_TREE;
6637
6638   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6639      most distant ancestor, this should never happen.  */
6640   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6641
6642   return DECL_ABSTRACT_ORIGIN (decl);
6643 }
6644
6645 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6646    of a virtual function may refer to a base class, so we check the 'this'
6647    parameter.  */
6648
6649 static tree
6650 decl_class_context (tree decl)
6651 {
6652   tree context = NULL_TREE;
6653
6654   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6655     context = DECL_CONTEXT (decl);
6656   else
6657     context = TYPE_MAIN_VARIANT
6658       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6659
6660   if (context && !TYPE_P (context))
6661     context = NULL_TREE;
6662
6663   return context;
6664 }
6665 \f
6666 /* Add an attribute/value pair to a DIE.  */
6667
6668 static inline void
6669 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6670 {
6671   /* Maybe this should be an assert?  */
6672   if (die == NULL)
6673     return;
6674
6675   if (die->die_attr == NULL)
6676     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6677   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6678 }
6679
6680 static inline enum dw_val_class
6681 AT_class (dw_attr_ref a)
6682 {
6683   return a->dw_attr_val.val_class;
6684 }
6685
6686 /* Add a flag value attribute to a DIE.  */
6687
6688 static inline void
6689 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6690 {
6691   dw_attr_node attr;
6692
6693   attr.dw_attr = attr_kind;
6694   attr.dw_attr_val.val_class = dw_val_class_flag;
6695   attr.dw_attr_val.v.val_flag = flag;
6696   add_dwarf_attr (die, &attr);
6697 }
6698
6699 static inline unsigned
6700 AT_flag (dw_attr_ref a)
6701 {
6702   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6703   return a->dw_attr_val.v.val_flag;
6704 }
6705
6706 /* Add a signed integer attribute value to a DIE.  */
6707
6708 static inline void
6709 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6710 {
6711   dw_attr_node attr;
6712
6713   attr.dw_attr = attr_kind;
6714   attr.dw_attr_val.val_class = dw_val_class_const;
6715   attr.dw_attr_val.v.val_int = int_val;
6716   add_dwarf_attr (die, &attr);
6717 }
6718
6719 static inline HOST_WIDE_INT
6720 AT_int (dw_attr_ref a)
6721 {
6722   gcc_assert (a && AT_class (a) == dw_val_class_const);
6723   return a->dw_attr_val.v.val_int;
6724 }
6725
6726 /* Add an unsigned integer attribute value to a DIE.  */
6727
6728 static inline void
6729 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6730                  unsigned HOST_WIDE_INT unsigned_val)
6731 {
6732   dw_attr_node attr;
6733
6734   attr.dw_attr = attr_kind;
6735   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6736   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6737   add_dwarf_attr (die, &attr);
6738 }
6739
6740 static inline unsigned HOST_WIDE_INT
6741 AT_unsigned (dw_attr_ref a)
6742 {
6743   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6744   return a->dw_attr_val.v.val_unsigned;
6745 }
6746
6747 /* Add an unsigned double integer attribute value to a DIE.  */
6748
6749 static inline void
6750 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6751                   rtx val_const_double)
6752 {
6753   dw_attr_node attr;
6754
6755   attr.dw_attr = attr_kind;
6756   attr.dw_attr_val.val_class = dw_val_class_long_long;
6757   attr.dw_attr_val.v.val_long_long = val_const_double;
6758   add_dwarf_attr (die, &attr);
6759 }
6760
6761 /* Add a floating point attribute value to a DIE and return it.  */
6762
6763 static inline void
6764 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6765             unsigned int length, unsigned int elt_size, unsigned char *array)
6766 {
6767   dw_attr_node attr;
6768
6769   attr.dw_attr = attr_kind;
6770   attr.dw_attr_val.val_class = dw_val_class_vec;
6771   attr.dw_attr_val.v.val_vec.length = length;
6772   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6773   attr.dw_attr_val.v.val_vec.array = array;
6774   add_dwarf_attr (die, &attr);
6775 }
6776
6777 /* Hash and equality functions for debug_str_hash.  */
6778
6779 static hashval_t
6780 debug_str_do_hash (const void *x)
6781 {
6782   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6783 }
6784
6785 static int
6786 debug_str_eq (const void *x1, const void *x2)
6787 {
6788   return strcmp ((((const struct indirect_string_node *)x1)->str),
6789                  (const char *)x2) == 0;
6790 }
6791
6792 /* Add STR to the indirect string hash table.  */
6793
6794 static struct indirect_string_node *
6795 find_AT_string (const char *str)
6796 {
6797   struct indirect_string_node *node;
6798   void **slot;
6799
6800   if (! debug_str_hash)
6801     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6802                                       debug_str_eq, NULL);
6803
6804   slot = htab_find_slot_with_hash (debug_str_hash, str,
6805                                    htab_hash_string (str), INSERT);
6806   if (*slot == NULL)
6807     {
6808       node = (struct indirect_string_node *)
6809                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6810       node->str = ggc_strdup (str);
6811       *slot = node;
6812     }
6813   else
6814     node = (struct indirect_string_node *) *slot;
6815
6816   node->refcount++;
6817   return node;
6818 }
6819
6820 /* Add a string attribute value to a DIE.  */
6821
6822 static inline void
6823 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6824 {
6825   dw_attr_node attr;
6826   struct indirect_string_node *node;
6827
6828   node = find_AT_string (str);
6829
6830   attr.dw_attr = attr_kind;
6831   attr.dw_attr_val.val_class = dw_val_class_str;
6832   attr.dw_attr_val.v.val_str = node;
6833   add_dwarf_attr (die, &attr);
6834 }
6835
6836 /* Create a label for an indirect string node, ensuring it is going to
6837    be output, unless its reference count goes down to zero.  */
6838
6839 static inline void
6840 gen_label_for_indirect_string (struct indirect_string_node *node)
6841 {
6842   char label[32];
6843
6844   if (node->label)
6845     return;
6846
6847   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6848   ++dw2_string_counter;
6849   node->label = xstrdup (label);
6850 }
6851
6852 /* Create a SYMBOL_REF rtx whose value is the initial address of a
6853    debug string STR.  */
6854
6855 static inline rtx
6856 get_debug_string_label (const char *str)
6857 {
6858   struct indirect_string_node *node = find_AT_string (str);
6859
6860   debug_str_hash_forced = true;
6861
6862   gen_label_for_indirect_string (node);
6863
6864   return gen_rtx_SYMBOL_REF (Pmode, node->label);
6865 }
6866
6867 static inline const char *
6868 AT_string (dw_attr_ref a)
6869 {
6870   gcc_assert (a && AT_class (a) == dw_val_class_str);
6871   return a->dw_attr_val.v.val_str->str;
6872 }
6873
6874 /* Find out whether a string should be output inline in DIE
6875    or out-of-line in .debug_str section.  */
6876
6877 static enum dwarf_form
6878 AT_string_form (dw_attr_ref a)
6879 {
6880   struct indirect_string_node *node;
6881   unsigned int len;
6882
6883   gcc_assert (a && AT_class (a) == dw_val_class_str);
6884
6885   node = a->dw_attr_val.v.val_str;
6886   if (node->form)
6887     return node->form;
6888
6889   len = strlen (node->str) + 1;
6890
6891   /* If the string is shorter or equal to the size of the reference, it is
6892      always better to put it inline.  */
6893   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6894     return node->form = DW_FORM_string;
6895
6896   /* If we cannot expect the linker to merge strings in .debug_str
6897      section, only put it into .debug_str if it is worth even in this
6898      single module.  */
6899   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
6900       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
6901       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
6902     return node->form = DW_FORM_string;
6903
6904   gen_label_for_indirect_string (node);
6905
6906   return node->form = DW_FORM_strp;
6907 }
6908
6909 /* Add a DIE reference attribute value to a DIE.  */
6910
6911 static inline void
6912 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6913 {
6914   dw_attr_node attr;
6915
6916   attr.dw_attr = attr_kind;
6917   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6918   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6919   attr.dw_attr_val.v.val_die_ref.external = 0;
6920   add_dwarf_attr (die, &attr);
6921 }
6922
6923 /* Add an AT_specification attribute to a DIE, and also make the back
6924    pointer from the specification to the definition.  */
6925
6926 static inline void
6927 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6928 {
6929   add_AT_die_ref (die, DW_AT_specification, targ_die);
6930   gcc_assert (!targ_die->die_definition);
6931   targ_die->die_definition = die;
6932 }
6933
6934 static inline dw_die_ref
6935 AT_ref (dw_attr_ref a)
6936 {
6937   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6938   return a->dw_attr_val.v.val_die_ref.die;
6939 }
6940
6941 static inline int
6942 AT_ref_external (dw_attr_ref a)
6943 {
6944   if (a && AT_class (a) == dw_val_class_die_ref)
6945     return a->dw_attr_val.v.val_die_ref.external;
6946
6947   return 0;
6948 }
6949
6950 static inline void
6951 set_AT_ref_external (dw_attr_ref a, int i)
6952 {
6953   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6954   a->dw_attr_val.v.val_die_ref.external = i;
6955 }
6956
6957 /* Add an FDE reference attribute value to a DIE.  */
6958
6959 static inline void
6960 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6961 {
6962   dw_attr_node attr;
6963
6964   attr.dw_attr = attr_kind;
6965   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6966   attr.dw_attr_val.v.val_fde_index = targ_fde;
6967   add_dwarf_attr (die, &attr);
6968 }
6969
6970 /* Add a location description attribute value to a DIE.  */
6971
6972 static inline void
6973 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6974 {
6975   dw_attr_node attr;
6976
6977   attr.dw_attr = attr_kind;
6978   attr.dw_attr_val.val_class = dw_val_class_loc;
6979   attr.dw_attr_val.v.val_loc = loc;
6980   add_dwarf_attr (die, &attr);
6981 }
6982
6983 static inline dw_loc_descr_ref
6984 AT_loc (dw_attr_ref a)
6985 {
6986   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6987   return a->dw_attr_val.v.val_loc;
6988 }
6989
6990 static inline void
6991 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6992 {
6993   dw_attr_node attr;
6994
6995   attr.dw_attr = attr_kind;
6996   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6997   attr.dw_attr_val.v.val_loc_list = loc_list;
6998   add_dwarf_attr (die, &attr);
6999   have_location_lists = true;
7000 }
7001
7002 static inline dw_loc_list_ref
7003 AT_loc_list (dw_attr_ref a)
7004 {
7005   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7006   return a->dw_attr_val.v.val_loc_list;
7007 }
7008
7009 /* Add an address constant attribute value to a DIE.  */
7010
7011 static inline void
7012 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7013 {
7014   dw_attr_node attr;
7015
7016   attr.dw_attr = attr_kind;
7017   attr.dw_attr_val.val_class = dw_val_class_addr;
7018   attr.dw_attr_val.v.val_addr = addr;
7019   add_dwarf_attr (die, &attr);
7020 }
7021
7022 /* Get the RTX from to an address DIE attribute.  */
7023
7024 static inline rtx
7025 AT_addr (dw_attr_ref a)
7026 {
7027   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7028   return a->dw_attr_val.v.val_addr;
7029 }
7030
7031 /* Add a file attribute value to a DIE.  */
7032
7033 static inline void
7034 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7035              struct dwarf_file_data *fd)
7036 {
7037   dw_attr_node attr;
7038
7039   attr.dw_attr = attr_kind;
7040   attr.dw_attr_val.val_class = dw_val_class_file;
7041   attr.dw_attr_val.v.val_file = fd;
7042   add_dwarf_attr (die, &attr);
7043 }
7044
7045 /* Get the dwarf_file_data from a file DIE attribute.  */
7046
7047 static inline struct dwarf_file_data *
7048 AT_file (dw_attr_ref a)
7049 {
7050   gcc_assert (a && AT_class (a) == dw_val_class_file);
7051   return a->dw_attr_val.v.val_file;
7052 }
7053
7054 /* Add a label identifier attribute value to a DIE.  */
7055
7056 static inline void
7057 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7058 {
7059   dw_attr_node attr;
7060
7061   attr.dw_attr = attr_kind;
7062   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7063   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7064   add_dwarf_attr (die, &attr);
7065 }
7066
7067 /* Add a section offset attribute value to a DIE, an offset into the
7068    debug_line section.  */
7069
7070 static inline void
7071 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7072                 const char *label)
7073 {
7074   dw_attr_node attr;
7075
7076   attr.dw_attr = attr_kind;
7077   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7078   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7079   add_dwarf_attr (die, &attr);
7080 }
7081
7082 /* Add a section offset attribute value to a DIE, an offset into the
7083    debug_macinfo section.  */
7084
7085 static inline void
7086 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7087                const char *label)
7088 {
7089   dw_attr_node attr;
7090
7091   attr.dw_attr = attr_kind;
7092   attr.dw_attr_val.val_class = dw_val_class_macptr;
7093   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7094   add_dwarf_attr (die, &attr);
7095 }
7096
7097 /* Add an offset attribute value to a DIE.  */
7098
7099 static inline void
7100 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7101                unsigned HOST_WIDE_INT offset)
7102 {
7103   dw_attr_node attr;
7104
7105   attr.dw_attr = attr_kind;
7106   attr.dw_attr_val.val_class = dw_val_class_offset;
7107   attr.dw_attr_val.v.val_offset = offset;
7108   add_dwarf_attr (die, &attr);
7109 }
7110
7111 /* Add an range_list attribute value to a DIE.  */
7112
7113 static void
7114 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7115                    long unsigned int offset)
7116 {
7117   dw_attr_node attr;
7118
7119   attr.dw_attr = attr_kind;
7120   attr.dw_attr_val.val_class = dw_val_class_range_list;
7121   attr.dw_attr_val.v.val_offset = offset;
7122   add_dwarf_attr (die, &attr);
7123 }
7124
7125 static inline const char *
7126 AT_lbl (dw_attr_ref a)
7127 {
7128   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7129                     || AT_class (a) == dw_val_class_lineptr
7130                     || AT_class (a) == dw_val_class_macptr));
7131   return a->dw_attr_val.v.val_lbl_id;
7132 }
7133
7134 /* Get the attribute of type attr_kind.  */
7135
7136 static dw_attr_ref
7137 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7138 {
7139   dw_attr_ref a;
7140   unsigned ix;
7141   dw_die_ref spec = NULL;
7142
7143   if (! die)
7144     return NULL;
7145
7146   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7147     if (a->dw_attr == attr_kind)
7148       return a;
7149     else if (a->dw_attr == DW_AT_specification
7150              || a->dw_attr == DW_AT_abstract_origin)
7151       spec = AT_ref (a);
7152
7153   if (spec)
7154     return get_AT (spec, attr_kind);
7155
7156   return NULL;
7157 }
7158
7159 /* Return the "low pc" attribute value, typically associated with a subprogram
7160    DIE.  Return null if the "low 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_low_pc (dw_die_ref die)
7165 {
7166   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7167
7168   return a ? AT_lbl (a) : NULL;
7169 }
7170
7171 /* Return the "high pc" attribute value, typically associated with a subprogram
7172    DIE.  Return null if the "high pc" attribute is either not present, or if it
7173    cannot be represented as an assembler label identifier.  */
7174
7175 static inline const char *
7176 get_AT_hi_pc (dw_die_ref die)
7177 {
7178   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7179
7180   return a ? AT_lbl (a) : NULL;
7181 }
7182
7183 /* Return the value of the string attribute designated by ATTR_KIND, or
7184    NULL if it is not present.  */
7185
7186 static inline const char *
7187 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7188 {
7189   dw_attr_ref a = get_AT (die, attr_kind);
7190
7191   return a ? AT_string (a) : NULL;
7192 }
7193
7194 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7195    if it is not present.  */
7196
7197 static inline int
7198 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7199 {
7200   dw_attr_ref a = get_AT (die, attr_kind);
7201
7202   return a ? AT_flag (a) : 0;
7203 }
7204
7205 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7206    if it is not present.  */
7207
7208 static inline unsigned
7209 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7210 {
7211   dw_attr_ref a = get_AT (die, attr_kind);
7212
7213   return a ? AT_unsigned (a) : 0;
7214 }
7215
7216 static inline dw_die_ref
7217 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7218 {
7219   dw_attr_ref a = get_AT (die, attr_kind);
7220
7221   return a ? AT_ref (a) : NULL;
7222 }
7223
7224 static inline struct dwarf_file_data *
7225 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7226 {
7227   dw_attr_ref a = get_AT (die, attr_kind);
7228
7229   return a ? AT_file (a) : NULL;
7230 }
7231
7232 /* Return TRUE if the language is C or C++.  */
7233
7234 static inline bool
7235 is_c_family (void)
7236 {
7237   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7238
7239   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7240           || lang == DW_LANG_C99
7241           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7242 }
7243
7244 /* Return TRUE if the language is C++.  */
7245
7246 static inline bool
7247 is_cxx (void)
7248 {
7249   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7250
7251   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7252 }
7253
7254 /* Return TRUE if the language is Fortran.  */
7255
7256 static inline bool
7257 is_fortran (void)
7258 {
7259   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7260
7261   return (lang == DW_LANG_Fortran77
7262           || lang == DW_LANG_Fortran90
7263           || lang == DW_LANG_Fortran95);
7264 }
7265
7266 /* Return TRUE if the language is Java.  */
7267
7268 static inline bool
7269 is_java (void)
7270 {
7271   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7272
7273   return lang == DW_LANG_Java;
7274 }
7275
7276 /* Return TRUE if the language is Ada.  */
7277
7278 static inline bool
7279 is_ada (void)
7280 {
7281   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7282
7283   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7284 }
7285
7286 /* Remove the specified attribute if present.  */
7287
7288 static void
7289 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7290 {
7291   dw_attr_ref a;
7292   unsigned ix;
7293
7294   if (! die)
7295     return;
7296
7297   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7298     if (a->dw_attr == attr_kind)
7299       {
7300         if (AT_class (a) == dw_val_class_str)
7301           if (a->dw_attr_val.v.val_str->refcount)
7302             a->dw_attr_val.v.val_str->refcount--;
7303
7304         /* VEC_ordered_remove should help reduce the number of abbrevs
7305            that are needed.  */
7306         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7307         return;
7308       }
7309 }
7310
7311 /* Remove CHILD from its parent.  PREV must have the property that
7312    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7313
7314 static void
7315 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7316 {
7317   gcc_assert (child->die_parent == prev->die_parent);
7318   gcc_assert (prev->die_sib == child);
7319   if (prev == child)
7320     {
7321       gcc_assert (child->die_parent->die_child == child);
7322       prev = NULL;
7323     }
7324   else
7325     prev->die_sib = child->die_sib;
7326   if (child->die_parent->die_child == child)
7327     child->die_parent->die_child = prev;
7328 }
7329
7330 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7331    matches TAG.  */
7332
7333 static void
7334 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7335 {
7336   dw_die_ref c;
7337
7338   c = die->die_child;
7339   if (c) do {
7340     dw_die_ref prev = c;
7341     c = c->die_sib;
7342     while (c->die_tag == tag)
7343       {
7344         remove_child_with_prev (c, prev);
7345         /* Might have removed every child.  */
7346         if (c == c->die_sib)
7347           return;
7348         c = c->die_sib;
7349       }
7350   } while (c != die->die_child);
7351 }
7352
7353 /* Add a CHILD_DIE as the last child of DIE.  */
7354
7355 static void
7356 add_child_die (dw_die_ref die, dw_die_ref child_die)
7357 {
7358   /* FIXME this should probably be an assert.  */
7359   if (! die || ! child_die)
7360     return;
7361   gcc_assert (die != child_die);
7362
7363   child_die->die_parent = die;
7364   if (die->die_child)
7365     {
7366       child_die->die_sib = die->die_child->die_sib;
7367       die->die_child->die_sib = child_die;
7368     }
7369   else
7370     child_die->die_sib = child_die;
7371   die->die_child = child_die;
7372 }
7373
7374 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7375    is the specification, to the end of PARENT's list of children.
7376    This is done by removing and re-adding it.  */
7377
7378 static void
7379 splice_child_die (dw_die_ref parent, dw_die_ref child)
7380 {
7381   dw_die_ref p;
7382
7383   /* We want the declaration DIE from inside the class, not the
7384      specification DIE at toplevel.  */
7385   if (child->die_parent != parent)
7386     {
7387       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7388
7389       if (tmp)
7390         child = tmp;
7391     }
7392
7393   gcc_assert (child->die_parent == parent
7394               || (child->die_parent
7395                   == get_AT_ref (parent, DW_AT_specification)));
7396
7397   for (p = child->die_parent->die_child; ; p = p->die_sib)
7398     if (p->die_sib == child)
7399       {
7400         remove_child_with_prev (child, p);
7401         break;
7402       }
7403
7404   add_child_die (parent, child);
7405 }
7406
7407 /* Return a pointer to a newly created DIE node.  */
7408
7409 static inline dw_die_ref
7410 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7411 {
7412   dw_die_ref die = GGC_CNEW (die_node);
7413
7414   die->die_tag = tag_value;
7415
7416   if (parent_die != NULL)
7417     add_child_die (parent_die, die);
7418   else
7419     {
7420       limbo_die_node *limbo_node;
7421
7422       limbo_node = GGC_CNEW (limbo_die_node);
7423       limbo_node->die = die;
7424       limbo_node->created_for = t;
7425       limbo_node->next = limbo_die_list;
7426       limbo_die_list = limbo_node;
7427     }
7428
7429   return die;
7430 }
7431
7432 /* Return the DIE associated with the given type specifier.  */
7433
7434 static inline dw_die_ref
7435 lookup_type_die (tree type)
7436 {
7437   return TYPE_SYMTAB_DIE (type);
7438 }
7439
7440 /* Equate a DIE to a given type specifier.  */
7441
7442 static inline void
7443 equate_type_number_to_die (tree type, dw_die_ref type_die)
7444 {
7445   TYPE_SYMTAB_DIE (type) = type_die;
7446 }
7447
7448 /* Returns a hash value for X (which really is a die_struct).  */
7449
7450 static hashval_t
7451 decl_die_table_hash (const void *x)
7452 {
7453   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7454 }
7455
7456 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7457
7458 static int
7459 decl_die_table_eq (const void *x, const void *y)
7460 {
7461   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7462 }
7463
7464 /* Return the DIE associated with a given declaration.  */
7465
7466 static inline dw_die_ref
7467 lookup_decl_die (tree decl)
7468 {
7469   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7470 }
7471
7472 /* Returns a hash value for X (which really is a var_loc_list).  */
7473
7474 static hashval_t
7475 decl_loc_table_hash (const void *x)
7476 {
7477   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7478 }
7479
7480 /* Return nonzero if decl_id of var_loc_list X is the same as
7481    UID of decl *Y.  */
7482
7483 static int
7484 decl_loc_table_eq (const void *x, const void *y)
7485 {
7486   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7487 }
7488
7489 /* Return the var_loc list associated with a given declaration.  */
7490
7491 static inline var_loc_list *
7492 lookup_decl_loc (const_tree decl)
7493 {
7494   if (!decl_loc_table)
7495     return NULL;
7496   return (var_loc_list *)
7497     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7498 }
7499
7500 /* Equate a DIE to a particular declaration.  */
7501
7502 static void
7503 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7504 {
7505   unsigned int decl_id = DECL_UID (decl);
7506   void **slot;
7507
7508   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7509   *slot = decl_die;
7510   decl_die->decl_id = decl_id;
7511 }
7512
7513 /* Add a variable location node to the linked list for DECL.  */
7514
7515 static void
7516 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7517 {
7518   unsigned int decl_id = DECL_UID (decl);
7519   var_loc_list *temp;
7520   void **slot;
7521
7522   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7523   if (*slot == NULL)
7524     {
7525       temp = GGC_CNEW (var_loc_list);
7526       temp->decl_id = decl_id;
7527       *slot = temp;
7528     }
7529   else
7530     temp = (var_loc_list *) *slot;
7531
7532   if (temp->last)
7533     {
7534       /* If the current location is the same as the end of the list,
7535          and either both or neither of the locations is uninitialized,
7536          we have nothing to do.  */
7537       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7538                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7539           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7540                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7541               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7542                    == VAR_INIT_STATUS_UNINITIALIZED)
7543                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7544                       == VAR_INIT_STATUS_UNINITIALIZED))))
7545         {
7546           /* Add LOC to the end of list and update LAST.  */
7547           temp->last->next = loc;
7548           temp->last = loc;
7549         }
7550     }
7551   /* Do not add empty location to the beginning of the list.  */
7552   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7553     {
7554       temp->first = loc;
7555       temp->last = loc;
7556     }
7557 }
7558 \f
7559 /* Keep track of the number of spaces used to indent the
7560    output of the debugging routines that print the structure of
7561    the DIE internal representation.  */
7562 static int print_indent;
7563
7564 /* Indent the line the number of spaces given by print_indent.  */
7565
7566 static inline void
7567 print_spaces (FILE *outfile)
7568 {
7569   fprintf (outfile, "%*s", print_indent, "");
7570 }
7571
7572 /* Print the information associated with a given DIE, and its children.
7573    This routine is a debugging aid only.  */
7574
7575 static void
7576 print_die (dw_die_ref die, FILE *outfile)
7577 {
7578   dw_attr_ref a;
7579   dw_die_ref c;
7580   unsigned ix;
7581
7582   print_spaces (outfile);
7583   fprintf (outfile, "DIE %4ld: %s\n",
7584            die->die_offset, dwarf_tag_name (die->die_tag));
7585   print_spaces (outfile);
7586   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7587   fprintf (outfile, " offset: %ld\n", die->die_offset);
7588
7589   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7590     {
7591       print_spaces (outfile);
7592       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7593
7594       switch (AT_class (a))
7595         {
7596         case dw_val_class_addr:
7597           fprintf (outfile, "address");
7598           break;
7599         case dw_val_class_offset:
7600           fprintf (outfile, "offset");
7601           break;
7602         case dw_val_class_loc:
7603           fprintf (outfile, "location descriptor");
7604           break;
7605         case dw_val_class_loc_list:
7606           fprintf (outfile, "location list -> label:%s",
7607                    AT_loc_list (a)->ll_symbol);
7608           break;
7609         case dw_val_class_range_list:
7610           fprintf (outfile, "range list");
7611           break;
7612         case dw_val_class_const:
7613           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7614           break;
7615         case dw_val_class_unsigned_const:
7616           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7617           break;
7618         case dw_val_class_long_long:
7619           fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_UNSIGNED
7620                             "," HOST_WIDE_INT_PRINT_UNSIGNED ")",
7621                    CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long),
7622                    CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long));
7623           break;
7624         case dw_val_class_vec:
7625           fprintf (outfile, "floating-point or vector constant");
7626           break;
7627         case dw_val_class_flag:
7628           fprintf (outfile, "%u", AT_flag (a));
7629           break;
7630         case dw_val_class_die_ref:
7631           if (AT_ref (a) != NULL)
7632             {
7633               if (AT_ref (a)->die_symbol)
7634                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7635               else
7636                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7637             }
7638           else
7639             fprintf (outfile, "die -> <null>");
7640           break;
7641         case dw_val_class_lbl_id:
7642         case dw_val_class_lineptr:
7643         case dw_val_class_macptr:
7644           fprintf (outfile, "label: %s", AT_lbl (a));
7645           break;
7646         case dw_val_class_str:
7647           if (AT_string (a) != NULL)
7648             fprintf (outfile, "\"%s\"", AT_string (a));
7649           else
7650             fprintf (outfile, "<null>");
7651           break;
7652         case dw_val_class_file:
7653           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7654                    AT_file (a)->emitted_number);
7655           break;
7656         default:
7657           break;
7658         }
7659
7660       fprintf (outfile, "\n");
7661     }
7662
7663   if (die->die_child != NULL)
7664     {
7665       print_indent += 4;
7666       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7667       print_indent -= 4;
7668     }
7669   if (print_indent == 0)
7670     fprintf (outfile, "\n");
7671 }
7672
7673 /* Print the contents of the source code line number correspondence table.
7674    This routine is a debugging aid only.  */
7675
7676 static void
7677 print_dwarf_line_table (FILE *outfile)
7678 {
7679   unsigned i;
7680   dw_line_info_ref line_info;
7681
7682   fprintf (outfile, "\n\nDWARF source line information\n");
7683   for (i = 1; i < line_info_table_in_use; i++)
7684     {
7685       line_info = &line_info_table[i];
7686       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7687                line_info->dw_file_num,
7688                line_info->dw_line_num);
7689     }
7690
7691   fprintf (outfile, "\n\n");
7692 }
7693
7694 /* Print the information collected for a given DIE.  */
7695
7696 void
7697 debug_dwarf_die (dw_die_ref die)
7698 {
7699   print_die (die, stderr);
7700 }
7701
7702 /* Print all DWARF information collected for the compilation unit.
7703    This routine is a debugging aid only.  */
7704
7705 void
7706 debug_dwarf (void)
7707 {
7708   print_indent = 0;
7709   print_die (comp_unit_die, stderr);
7710   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7711     print_dwarf_line_table (stderr);
7712 }
7713 \f
7714 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7715    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7716    DIE that marks the start of the DIEs for this include file.  */
7717
7718 static dw_die_ref
7719 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7720 {
7721   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7722   dw_die_ref new_unit = gen_compile_unit_die (filename);
7723
7724   new_unit->die_sib = old_unit;
7725   return new_unit;
7726 }
7727
7728 /* Close an include-file CU and reopen the enclosing one.  */
7729
7730 static dw_die_ref
7731 pop_compile_unit (dw_die_ref old_unit)
7732 {
7733   dw_die_ref new_unit = old_unit->die_sib;
7734
7735   old_unit->die_sib = NULL;
7736   return new_unit;
7737 }
7738
7739 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7740 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7741
7742 /* Calculate the checksum of a location expression.  */
7743
7744 static inline void
7745 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7746 {
7747   int tem;
7748
7749   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7750   CHECKSUM (tem);
7751   CHECKSUM (loc->dw_loc_oprnd1);
7752   CHECKSUM (loc->dw_loc_oprnd2);
7753 }
7754
7755 /* Calculate the checksum of an attribute.  */
7756
7757 static void
7758 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7759 {
7760   dw_loc_descr_ref loc;
7761   rtx r;
7762
7763   CHECKSUM (at->dw_attr);
7764
7765   /* We don't care that this was compiled with a different compiler
7766      snapshot; if the output is the same, that's what matters.  */
7767   if (at->dw_attr == DW_AT_producer)
7768     return;
7769
7770   switch (AT_class (at))
7771     {
7772     case dw_val_class_const:
7773       CHECKSUM (at->dw_attr_val.v.val_int);
7774       break;
7775     case dw_val_class_unsigned_const:
7776       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7777       break;
7778     case dw_val_class_long_long:
7779       CHECKSUM (CONST_DOUBLE_HIGH (at->dw_attr_val.v.val_long_long));
7780       CHECKSUM (CONST_DOUBLE_LOW (at->dw_attr_val.v.val_long_long));
7781       break;
7782     case dw_val_class_vec:
7783       CHECKSUM (at->dw_attr_val.v.val_vec);
7784       break;
7785     case dw_val_class_flag:
7786       CHECKSUM (at->dw_attr_val.v.val_flag);
7787       break;
7788     case dw_val_class_str:
7789       CHECKSUM_STRING (AT_string (at));
7790       break;
7791
7792     case dw_val_class_addr:
7793       r = AT_addr (at);
7794       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7795       CHECKSUM_STRING (XSTR (r, 0));
7796       break;
7797
7798     case dw_val_class_offset:
7799       CHECKSUM (at->dw_attr_val.v.val_offset);
7800       break;
7801
7802     case dw_val_class_loc:
7803       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7804         loc_checksum (loc, ctx);
7805       break;
7806
7807     case dw_val_class_die_ref:
7808       die_checksum (AT_ref (at), ctx, mark);
7809       break;
7810
7811     case dw_val_class_fde_ref:
7812     case dw_val_class_lbl_id:
7813     case dw_val_class_lineptr:
7814     case dw_val_class_macptr:
7815       break;
7816
7817     case dw_val_class_file:
7818       CHECKSUM_STRING (AT_file (at)->filename);
7819       break;
7820
7821     default:
7822       break;
7823     }
7824 }
7825
7826 /* Calculate the checksum of a DIE.  */
7827
7828 static void
7829 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7830 {
7831   dw_die_ref c;
7832   dw_attr_ref a;
7833   unsigned ix;
7834
7835   /* To avoid infinite recursion.  */
7836   if (die->die_mark)
7837     {
7838       CHECKSUM (die->die_mark);
7839       return;
7840     }
7841   die->die_mark = ++(*mark);
7842
7843   CHECKSUM (die->die_tag);
7844
7845   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7846     attr_checksum (a, ctx, mark);
7847
7848   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7849 }
7850
7851 #undef CHECKSUM
7852 #undef CHECKSUM_STRING
7853
7854 /* Do the location expressions look same?  */
7855 static inline int
7856 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7857 {
7858   return loc1->dw_loc_opc == loc2->dw_loc_opc
7859          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7860          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7861 }
7862
7863 /* Do the values look the same?  */
7864 static int
7865 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7866 {
7867   dw_loc_descr_ref loc1, loc2;
7868   rtx r1, r2;
7869
7870   if (v1->val_class != v2->val_class)
7871     return 0;
7872
7873   switch (v1->val_class)
7874     {
7875     case dw_val_class_const:
7876       return v1->v.val_int == v2->v.val_int;
7877     case dw_val_class_unsigned_const:
7878       return v1->v.val_unsigned == v2->v.val_unsigned;
7879     case dw_val_class_long_long:
7880       return CONST_DOUBLE_HIGH (v1->v.val_long_long)
7881              == CONST_DOUBLE_HIGH (v2->v.val_long_long)
7882              && CONST_DOUBLE_LOW (v1->v.val_long_long)
7883                 == CONST_DOUBLE_LOW (v2->v.val_long_long);
7884     case dw_val_class_vec:
7885       if (v1->v.val_vec.length != v2->v.val_vec.length
7886           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7887         return 0;
7888       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7889                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7890         return 0;
7891       return 1;
7892     case dw_val_class_flag:
7893       return v1->v.val_flag == v2->v.val_flag;
7894     case dw_val_class_str:
7895       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7896
7897     case dw_val_class_addr:
7898       r1 = v1->v.val_addr;
7899       r2 = v2->v.val_addr;
7900       if (GET_CODE (r1) != GET_CODE (r2))
7901         return 0;
7902       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7903       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7904
7905     case dw_val_class_offset:
7906       return v1->v.val_offset == v2->v.val_offset;
7907
7908     case dw_val_class_loc:
7909       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7910            loc1 && loc2;
7911            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7912         if (!same_loc_p (loc1, loc2, mark))
7913           return 0;
7914       return !loc1 && !loc2;
7915
7916     case dw_val_class_die_ref:
7917       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7918
7919     case dw_val_class_fde_ref:
7920     case dw_val_class_lbl_id:
7921     case dw_val_class_lineptr:
7922     case dw_val_class_macptr:
7923       return 1;
7924
7925     case dw_val_class_file:
7926       return v1->v.val_file == v2->v.val_file;
7927
7928     default:
7929       return 1;
7930     }
7931 }
7932
7933 /* Do the attributes look the same?  */
7934
7935 static int
7936 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7937 {
7938   if (at1->dw_attr != at2->dw_attr)
7939     return 0;
7940
7941   /* We don't care that this was compiled with a different compiler
7942      snapshot; if the output is the same, that's what matters. */
7943   if (at1->dw_attr == DW_AT_producer)
7944     return 1;
7945
7946   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7947 }
7948
7949 /* Do the dies look the same?  */
7950
7951 static int
7952 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7953 {
7954   dw_die_ref c1, c2;
7955   dw_attr_ref a1;
7956   unsigned ix;
7957
7958   /* To avoid infinite recursion.  */
7959   if (die1->die_mark)
7960     return die1->die_mark == die2->die_mark;
7961   die1->die_mark = die2->die_mark = ++(*mark);
7962
7963   if (die1->die_tag != die2->die_tag)
7964     return 0;
7965
7966   if (VEC_length (dw_attr_node, die1->die_attr)
7967       != VEC_length (dw_attr_node, die2->die_attr))
7968     return 0;
7969
7970   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7971     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7972       return 0;
7973
7974   c1 = die1->die_child;
7975   c2 = die2->die_child;
7976   if (! c1)
7977     {
7978       if (c2)
7979         return 0;
7980     }
7981   else
7982     for (;;)
7983       {
7984         if (!same_die_p (c1, c2, mark))
7985           return 0;
7986         c1 = c1->die_sib;
7987         c2 = c2->die_sib;
7988         if (c1 == die1->die_child)
7989           {
7990             if (c2 == die2->die_child)
7991               break;
7992             else
7993               return 0;
7994           }
7995     }
7996
7997   return 1;
7998 }
7999
8000 /* Do the dies look the same?  Wrapper around same_die_p.  */
8001
8002 static int
8003 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
8004 {
8005   int mark = 0;
8006   int ret = same_die_p (die1, die2, &mark);
8007
8008   unmark_all_dies (die1);
8009   unmark_all_dies (die2);
8010
8011   return ret;
8012 }
8013
8014 /* The prefix to attach to symbols on DIEs in the current comdat debug
8015    info section.  */
8016 static char *comdat_symbol_id;
8017
8018 /* The index of the current symbol within the current comdat CU.  */
8019 static unsigned int comdat_symbol_number;
8020
8021 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8022    children, and set comdat_symbol_id accordingly.  */
8023
8024 static void
8025 compute_section_prefix (dw_die_ref unit_die)
8026 {
8027   const char *die_name = get_AT_string (unit_die, DW_AT_name);
8028   const char *base = die_name ? lbasename (die_name) : "anonymous";
8029   char *name = XALLOCAVEC (char, strlen (base) + 64);
8030   char *p;
8031   int i, mark;
8032   unsigned char checksum[16];
8033   struct md5_ctx ctx;
8034
8035   /* Compute the checksum of the DIE, then append part of it as hex digits to
8036      the name filename of the unit.  */
8037
8038   md5_init_ctx (&ctx);
8039   mark = 0;
8040   die_checksum (unit_die, &ctx, &mark);
8041   unmark_all_dies (unit_die);
8042   md5_finish_ctx (&ctx, checksum);
8043
8044   sprintf (name, "%s.", base);
8045   clean_symbol_name (name);
8046
8047   p = name + strlen (name);
8048   for (i = 0; i < 4; i++)
8049     {
8050       sprintf (p, "%.2x", checksum[i]);
8051       p += 2;
8052     }
8053
8054   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
8055   comdat_symbol_number = 0;
8056 }
8057
8058 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
8059
8060 static int
8061 is_type_die (dw_die_ref die)
8062 {
8063   switch (die->die_tag)
8064     {
8065     case DW_TAG_array_type:
8066     case DW_TAG_class_type:
8067     case DW_TAG_interface_type:
8068     case DW_TAG_enumeration_type:
8069     case DW_TAG_pointer_type:
8070     case DW_TAG_reference_type:
8071     case DW_TAG_string_type:
8072     case DW_TAG_structure_type:
8073     case DW_TAG_subroutine_type:
8074     case DW_TAG_union_type:
8075     case DW_TAG_ptr_to_member_type:
8076     case DW_TAG_set_type:
8077     case DW_TAG_subrange_type:
8078     case DW_TAG_base_type:
8079     case DW_TAG_const_type:
8080     case DW_TAG_file_type:
8081     case DW_TAG_packed_type:
8082     case DW_TAG_volatile_type:
8083     case DW_TAG_typedef:
8084       return 1;
8085     default:
8086       return 0;
8087     }
8088 }
8089
8090 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
8091    Basically, we want to choose the bits that are likely to be shared between
8092    compilations (types) and leave out the bits that are specific to individual
8093    compilations (functions).  */
8094
8095 static int
8096 is_comdat_die (dw_die_ref c)
8097 {
8098   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
8099      we do for stabs.  The advantage is a greater likelihood of sharing between
8100      objects that don't include headers in the same order (and therefore would
8101      put the base types in a different comdat).  jason 8/28/00 */
8102
8103   if (c->die_tag == DW_TAG_base_type)
8104     return 0;
8105
8106   if (c->die_tag == DW_TAG_pointer_type
8107       || c->die_tag == DW_TAG_reference_type
8108       || c->die_tag == DW_TAG_const_type
8109       || c->die_tag == DW_TAG_volatile_type)
8110     {
8111       dw_die_ref t = get_AT_ref (c, DW_AT_type);
8112
8113       return t ? is_comdat_die (t) : 0;
8114     }
8115
8116   return is_type_die (c);
8117 }
8118
8119 /* Returns 1 iff C is the sort of DIE that might be referred to from another
8120    compilation unit.  */
8121
8122 static int
8123 is_symbol_die (dw_die_ref c)
8124 {
8125   return (is_type_die (c)
8126           || (get_AT (c, DW_AT_declaration)
8127               && !get_AT (c, DW_AT_specification))
8128           || c->die_tag == DW_TAG_namespace
8129           || c->die_tag == DW_TAG_module);
8130 }
8131
8132 static char *
8133 gen_internal_sym (const char *prefix)
8134 {
8135   char buf[256];
8136
8137   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8138   return xstrdup (buf);
8139 }
8140
8141 /* Assign symbols to all worthy DIEs under DIE.  */
8142
8143 static void
8144 assign_symbol_names (dw_die_ref die)
8145 {
8146   dw_die_ref c;
8147
8148   if (is_symbol_die (die))
8149     {
8150       if (comdat_symbol_id)
8151         {
8152           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
8153
8154           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
8155                    comdat_symbol_id, comdat_symbol_number++);
8156           die->die_symbol = xstrdup (p);
8157         }
8158       else
8159         die->die_symbol = gen_internal_sym ("LDIE");
8160     }
8161
8162   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
8163 }
8164
8165 struct cu_hash_table_entry
8166 {
8167   dw_die_ref cu;
8168   unsigned min_comdat_num, max_comdat_num;
8169   struct cu_hash_table_entry *next;
8170 };
8171
8172 /* Routines to manipulate hash table of CUs.  */
8173 static hashval_t
8174 htab_cu_hash (const void *of)
8175 {
8176   const struct cu_hash_table_entry *const entry =
8177     (const struct cu_hash_table_entry *) of;
8178
8179   return htab_hash_string (entry->cu->die_symbol);
8180 }
8181
8182 static int
8183 htab_cu_eq (const void *of1, const void *of2)
8184 {
8185   const struct cu_hash_table_entry *const entry1 =
8186     (const struct cu_hash_table_entry *) of1;
8187   const struct die_struct *const entry2 = (const struct die_struct *) of2;
8188
8189   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
8190 }
8191
8192 static void
8193 htab_cu_del (void *what)
8194 {
8195   struct cu_hash_table_entry *next,
8196     *entry = (struct cu_hash_table_entry *) what;
8197
8198   while (entry)
8199     {
8200       next = entry->next;
8201       free (entry);
8202       entry = next;
8203     }
8204 }
8205
8206 /* Check whether we have already seen this CU and set up SYM_NUM
8207    accordingly.  */
8208 static int
8209 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
8210 {
8211   struct cu_hash_table_entry dummy;
8212   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
8213
8214   dummy.max_comdat_num = 0;
8215
8216   slot = (struct cu_hash_table_entry **)
8217     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8218         INSERT);
8219   entry = *slot;
8220
8221   for (; entry; last = entry, entry = entry->next)
8222     {
8223       if (same_die_p_wrap (cu, entry->cu))
8224         break;
8225     }
8226
8227   if (entry)
8228     {
8229       *sym_num = entry->min_comdat_num;
8230       return 1;
8231     }
8232
8233   entry = XCNEW (struct cu_hash_table_entry);
8234   entry->cu = cu;
8235   entry->min_comdat_num = *sym_num = last->max_comdat_num;
8236   entry->next = *slot;
8237   *slot = entry;
8238
8239   return 0;
8240 }
8241
8242 /* Record SYM_NUM to record of CU in HTABLE.  */
8243 static void
8244 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
8245 {
8246   struct cu_hash_table_entry **slot, *entry;
8247
8248   slot = (struct cu_hash_table_entry **)
8249     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8250         NO_INSERT);
8251   entry = *slot;
8252
8253   entry->max_comdat_num = sym_num;
8254 }
8255
8256 /* Traverse the DIE (which is always comp_unit_die), and set up
8257    additional compilation units for each of the include files we see
8258    bracketed by BINCL/EINCL.  */
8259
8260 static void
8261 break_out_includes (dw_die_ref die)
8262 {
8263   dw_die_ref c;
8264   dw_die_ref unit = NULL;
8265   limbo_die_node *node, **pnode;
8266   htab_t cu_hash_table;
8267
8268   c = die->die_child;
8269   if (c) do {
8270     dw_die_ref prev = c;
8271     c = c->die_sib;
8272     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
8273            || (unit && is_comdat_die (c)))
8274       {
8275         dw_die_ref next = c->die_sib;
8276
8277         /* This DIE is for a secondary CU; remove it from the main one.  */
8278         remove_child_with_prev (c, prev);
8279
8280         if (c->die_tag == DW_TAG_GNU_BINCL)
8281           unit = push_new_compile_unit (unit, c);
8282         else if (c->die_tag == DW_TAG_GNU_EINCL)
8283           unit = pop_compile_unit (unit);
8284         else
8285           add_child_die (unit, c);
8286         c = next;
8287         if (c == die->die_child)
8288           break;
8289       }
8290   } while (c != die->die_child);
8291
8292 #if 0
8293   /* We can only use this in debugging, since the frontend doesn't check
8294      to make sure that we leave every include file we enter.  */
8295   gcc_assert (!unit);
8296 #endif
8297
8298   assign_symbol_names (die);
8299   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
8300   for (node = limbo_die_list, pnode = &limbo_die_list;
8301        node;
8302        node = node->next)
8303     {
8304       int is_dupl;
8305
8306       compute_section_prefix (node->die);
8307       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
8308                         &comdat_symbol_number);
8309       assign_symbol_names (node->die);
8310       if (is_dupl)
8311         *pnode = node->next;
8312       else
8313         {
8314           pnode = &node->next;
8315           record_comdat_symbol_number (node->die, cu_hash_table,
8316                 comdat_symbol_number);
8317         }
8318     }
8319   htab_delete (cu_hash_table);
8320 }
8321
8322 /* Traverse the DIE and add a sibling attribute if it may have the
8323    effect of speeding up access to siblings.  To save some space,
8324    avoid generating sibling attributes for DIE's without children.  */
8325
8326 static void
8327 add_sibling_attributes (dw_die_ref die)
8328 {
8329   dw_die_ref c;
8330
8331   if (! die->die_child)
8332     return;
8333
8334   if (die->die_parent && die != die->die_parent->die_child)
8335     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8336
8337   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8338 }
8339
8340 /* Output all location lists for the DIE and its children.  */
8341
8342 static void
8343 output_location_lists (dw_die_ref die)
8344 {
8345   dw_die_ref c;
8346   dw_attr_ref a;
8347   unsigned ix;
8348
8349   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8350     if (AT_class (a) == dw_val_class_loc_list)
8351       output_loc_list (AT_loc_list (a));
8352
8353   FOR_EACH_CHILD (die, c, output_location_lists (c));
8354 }
8355
8356 /* The format of each DIE (and its attribute value pairs) is encoded in an
8357    abbreviation table.  This routine builds the abbreviation table and assigns
8358    a unique abbreviation id for each abbreviation entry.  The children of each
8359    die are visited recursively.  */
8360
8361 static void
8362 build_abbrev_table (dw_die_ref die)
8363 {
8364   unsigned long abbrev_id;
8365   unsigned int n_alloc;
8366   dw_die_ref c;
8367   dw_attr_ref a;
8368   unsigned ix;
8369
8370   /* Scan the DIE references, and mark as external any that refer to
8371      DIEs from other CUs (i.e. those which are not marked).  */
8372   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8373     if (AT_class (a) == dw_val_class_die_ref
8374         && AT_ref (a)->die_mark == 0)
8375       {
8376         gcc_assert (AT_ref (a)->die_symbol);
8377         set_AT_ref_external (a, 1);
8378       }
8379
8380   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8381     {
8382       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8383       dw_attr_ref die_a, abbrev_a;
8384       unsigned ix;
8385       bool ok = true;
8386
8387       if (abbrev->die_tag != die->die_tag)
8388         continue;
8389       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8390         continue;
8391
8392       if (VEC_length (dw_attr_node, abbrev->die_attr)
8393           != VEC_length (dw_attr_node, die->die_attr))
8394         continue;
8395
8396       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
8397         {
8398           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8399           if ((abbrev_a->dw_attr != die_a->dw_attr)
8400               || (value_format (abbrev_a) != value_format (die_a)))
8401             {
8402               ok = false;
8403               break;
8404             }
8405         }
8406       if (ok)
8407         break;
8408     }
8409
8410   if (abbrev_id >= abbrev_die_table_in_use)
8411     {
8412       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8413         {
8414           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8415           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8416                                             n_alloc);
8417
8418           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8419                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8420           abbrev_die_table_allocated = n_alloc;
8421         }
8422
8423       ++abbrev_die_table_in_use;
8424       abbrev_die_table[abbrev_id] = die;
8425     }
8426
8427   die->die_abbrev = abbrev_id;
8428   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8429 }
8430 \f
8431 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8432
8433 static int
8434 constant_size (unsigned HOST_WIDE_INT value)
8435 {
8436   int log;
8437
8438   if (value == 0)
8439     log = 0;
8440   else
8441     log = floor_log2 (value);
8442
8443   log = log / 8;
8444   log = 1 << (floor_log2 (log) + 1);
8445
8446   return log;
8447 }
8448
8449 /* Return the size of a DIE as it is represented in the
8450    .debug_info section.  */
8451
8452 static unsigned long
8453 size_of_die (dw_die_ref die)
8454 {
8455   unsigned long size = 0;
8456   dw_attr_ref a;
8457   unsigned ix;
8458
8459   size += size_of_uleb128 (die->die_abbrev);
8460   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8461     {
8462       switch (AT_class (a))
8463         {
8464         case dw_val_class_addr:
8465           size += DWARF2_ADDR_SIZE;
8466           break;
8467         case dw_val_class_offset:
8468           size += DWARF_OFFSET_SIZE;
8469           break;
8470         case dw_val_class_loc:
8471           {
8472             unsigned long lsize = size_of_locs (AT_loc (a));
8473
8474             /* Block length.  */
8475             size += constant_size (lsize);
8476             size += lsize;
8477           }
8478           break;
8479         case dw_val_class_loc_list:
8480           size += DWARF_OFFSET_SIZE;
8481           break;
8482         case dw_val_class_range_list:
8483           size += DWARF_OFFSET_SIZE;
8484           break;
8485         case dw_val_class_const:
8486           size += size_of_sleb128 (AT_int (a));
8487           break;
8488         case dw_val_class_unsigned_const:
8489           size += constant_size (AT_unsigned (a));
8490           break;
8491         case dw_val_class_long_long:
8492           size += 1 + 2*HOST_BITS_PER_WIDE_INT/HOST_BITS_PER_CHAR; /* block */
8493           break;
8494         case dw_val_class_vec:
8495           size += constant_size (a->dw_attr_val.v.val_vec.length
8496                                  * a->dw_attr_val.v.val_vec.elt_size)
8497                   + a->dw_attr_val.v.val_vec.length
8498                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8499           break;
8500         case dw_val_class_flag:
8501           size += 1;
8502           break;
8503         case dw_val_class_die_ref:
8504           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8505              whereas in DWARF3 it's always sized as an offset.  */
8506           if (AT_ref_external (a) && dwarf_version == 2)
8507             size += DWARF2_ADDR_SIZE;
8508           else
8509             size += DWARF_OFFSET_SIZE;
8510           break;
8511         case dw_val_class_fde_ref:
8512           size += DWARF_OFFSET_SIZE;
8513           break;
8514         case dw_val_class_lbl_id:
8515           size += DWARF2_ADDR_SIZE;
8516           break;
8517         case dw_val_class_lineptr:
8518         case dw_val_class_macptr:
8519           size += DWARF_OFFSET_SIZE;
8520           break;
8521         case dw_val_class_str:
8522           if (AT_string_form (a) == DW_FORM_strp)
8523             size += DWARF_OFFSET_SIZE;
8524           else
8525             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8526           break;
8527         case dw_val_class_file:
8528           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8529           break;
8530         default:
8531           gcc_unreachable ();
8532         }
8533     }
8534
8535   return size;
8536 }
8537
8538 /* Size the debugging information associated with a given DIE.  Visits the
8539    DIE's children recursively.  Updates the global variable next_die_offset, on
8540    each time through.  Uses the current value of next_die_offset to update the
8541    die_offset field in each DIE.  */
8542
8543 static void
8544 calc_die_sizes (dw_die_ref die)
8545 {
8546   dw_die_ref c;
8547
8548   die->die_offset = next_die_offset;
8549   next_die_offset += size_of_die (die);
8550
8551   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8552
8553   if (die->die_child != NULL)
8554     /* Count the null byte used to terminate sibling lists.  */
8555     next_die_offset += 1;
8556 }
8557
8558 /* Set the marks for a die and its children.  We do this so
8559    that we know whether or not a reference needs to use FORM_ref_addr; only
8560    DIEs in the same CU will be marked.  We used to clear out the offset
8561    and use that as the flag, but ran into ordering problems.  */
8562
8563 static void
8564 mark_dies (dw_die_ref die)
8565 {
8566   dw_die_ref c;
8567
8568   gcc_assert (!die->die_mark);
8569
8570   die->die_mark = 1;
8571   FOR_EACH_CHILD (die, c, mark_dies (c));
8572 }
8573
8574 /* Clear the marks for a die and its children.  */
8575
8576 static void
8577 unmark_dies (dw_die_ref die)
8578 {
8579   dw_die_ref c;
8580
8581   gcc_assert (die->die_mark);
8582
8583   die->die_mark = 0;
8584   FOR_EACH_CHILD (die, c, unmark_dies (c));
8585 }
8586
8587 /* Clear the marks for a die, its children and referred dies.  */
8588
8589 static void
8590 unmark_all_dies (dw_die_ref die)
8591 {
8592   dw_die_ref c;
8593   dw_attr_ref a;
8594   unsigned ix;
8595
8596   if (!die->die_mark)
8597     return;
8598   die->die_mark = 0;
8599
8600   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8601
8602   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8603     if (AT_class (a) == dw_val_class_die_ref)
8604       unmark_all_dies (AT_ref (a));
8605 }
8606
8607 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8608    generated for the compilation unit.  */
8609
8610 static unsigned long
8611 size_of_pubnames (VEC (pubname_entry, gc) * names)
8612 {
8613   unsigned long size;
8614   unsigned i;
8615   pubname_ref p;
8616
8617   size = DWARF_PUBNAMES_HEADER_SIZE;
8618   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8619     if (names != pubtype_table
8620         || p->die->die_offset != 0
8621         || !flag_eliminate_unused_debug_types)
8622       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8623
8624   size += DWARF_OFFSET_SIZE;
8625   return size;
8626 }
8627
8628 /* Return the size of the information in the .debug_aranges section.  */
8629
8630 static unsigned long
8631 size_of_aranges (void)
8632 {
8633   unsigned long size;
8634
8635   size = DWARF_ARANGES_HEADER_SIZE;
8636
8637   /* Count the address/length pair for this compilation unit.  */
8638   if (text_section_used)
8639     size += 2 * DWARF2_ADDR_SIZE;
8640   if (cold_text_section_used)
8641     size += 2 * DWARF2_ADDR_SIZE;
8642   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8643
8644   /* Count the two zero words used to terminated the address range table.  */
8645   size += 2 * DWARF2_ADDR_SIZE;
8646   return size;
8647 }
8648 \f
8649 /* Select the encoding of an attribute value.  */
8650
8651 static enum dwarf_form
8652 value_format (dw_attr_ref a)
8653 {
8654   switch (a->dw_attr_val.val_class)
8655     {
8656     case dw_val_class_addr:
8657       return DW_FORM_addr;
8658     case dw_val_class_range_list:
8659     case dw_val_class_offset:
8660     case dw_val_class_loc_list:
8661       switch (DWARF_OFFSET_SIZE)
8662         {
8663         case 4:
8664           return DW_FORM_data4;
8665         case 8:
8666           return DW_FORM_data8;
8667         default:
8668           gcc_unreachable ();
8669         }
8670     case dw_val_class_loc:
8671       switch (constant_size (size_of_locs (AT_loc (a))))
8672         {
8673         case 1:
8674           return DW_FORM_block1;
8675         case 2:
8676           return DW_FORM_block2;
8677         default:
8678           gcc_unreachable ();
8679         }
8680     case dw_val_class_const:
8681       return DW_FORM_sdata;
8682     case dw_val_class_unsigned_const:
8683       switch (constant_size (AT_unsigned (a)))
8684         {
8685         case 1:
8686           return DW_FORM_data1;
8687         case 2:
8688           return DW_FORM_data2;
8689         case 4:
8690           return DW_FORM_data4;
8691         case 8:
8692           return DW_FORM_data8;
8693         default:
8694           gcc_unreachable ();
8695         }
8696     case dw_val_class_long_long:
8697       return DW_FORM_block1;
8698     case dw_val_class_vec:
8699       switch (constant_size (a->dw_attr_val.v.val_vec.length
8700                              * a->dw_attr_val.v.val_vec.elt_size))
8701         {
8702         case 1:
8703           return DW_FORM_block1;
8704         case 2:
8705           return DW_FORM_block2;
8706         case 4:
8707           return DW_FORM_block4;
8708         default:
8709           gcc_unreachable ();
8710         }
8711     case dw_val_class_flag:
8712       return DW_FORM_flag;
8713     case dw_val_class_die_ref:
8714       if (AT_ref_external (a))
8715         return DW_FORM_ref_addr;
8716       else
8717         return DW_FORM_ref;
8718     case dw_val_class_fde_ref:
8719       return DW_FORM_data;
8720     case dw_val_class_lbl_id:
8721       return DW_FORM_addr;
8722     case dw_val_class_lineptr:
8723     case dw_val_class_macptr:
8724       return DW_FORM_data;
8725     case dw_val_class_str:
8726       return AT_string_form (a);
8727     case dw_val_class_file:
8728       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8729         {
8730         case 1:
8731           return DW_FORM_data1;
8732         case 2:
8733           return DW_FORM_data2;
8734         case 4:
8735           return DW_FORM_data4;
8736         default:
8737           gcc_unreachable ();
8738         }
8739
8740     default:
8741       gcc_unreachable ();
8742     }
8743 }
8744
8745 /* Output the encoding of an attribute value.  */
8746
8747 static void
8748 output_value_format (dw_attr_ref a)
8749 {
8750   enum dwarf_form form = value_format (a);
8751
8752   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8753 }
8754
8755 /* Output the .debug_abbrev section which defines the DIE abbreviation
8756    table.  */
8757
8758 static void
8759 output_abbrev_section (void)
8760 {
8761   unsigned long abbrev_id;
8762
8763   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8764     {
8765       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8766       unsigned ix;
8767       dw_attr_ref a_attr;
8768
8769       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8770       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8771                                    dwarf_tag_name (abbrev->die_tag));
8772
8773       if (abbrev->die_child != NULL)
8774         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8775       else
8776         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8777
8778       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8779            ix++)
8780         {
8781           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8782                                        dwarf_attr_name (a_attr->dw_attr));
8783           output_value_format (a_attr);
8784         }
8785
8786       dw2_asm_output_data (1, 0, NULL);
8787       dw2_asm_output_data (1, 0, NULL);
8788     }
8789
8790   /* Terminate the table.  */
8791   dw2_asm_output_data (1, 0, NULL);
8792 }
8793
8794 /* Output a symbol we can use to refer to this DIE from another CU.  */
8795
8796 static inline void
8797 output_die_symbol (dw_die_ref die)
8798 {
8799   char *sym = die->die_symbol;
8800
8801   if (sym == 0)
8802     return;
8803
8804   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8805     /* We make these global, not weak; if the target doesn't support
8806        .linkonce, it doesn't support combining the sections, so debugging
8807        will break.  */
8808     targetm.asm_out.globalize_label (asm_out_file, sym);
8809
8810   ASM_OUTPUT_LABEL (asm_out_file, sym);
8811 }
8812
8813 /* Return a new location list, given the begin and end range, and the
8814    expression. gensym tells us whether to generate a new internal symbol for
8815    this location list node, which is done for the head of the list only.  */
8816
8817 static inline dw_loc_list_ref
8818 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8819               const char *section, unsigned int gensym)
8820 {
8821   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8822
8823   retlist->begin = begin;
8824   retlist->end = end;
8825   retlist->expr = expr;
8826   retlist->section = section;
8827   if (gensym)
8828     retlist->ll_symbol = gen_internal_sym ("LLST");
8829
8830   return retlist;
8831 }
8832
8833 /* Add a location description expression to a location list.  */
8834
8835 static inline void
8836 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8837                            const char *begin, const char *end,
8838                            const char *section)
8839 {
8840   dw_loc_list_ref *d;
8841
8842   /* Find the end of the chain.  */
8843   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8844     ;
8845
8846   /* Add a new location list node to the list.  */
8847   *d = new_loc_list (descr, begin, end, section, 0);
8848 }
8849
8850 /* Output the location list given to us.  */
8851
8852 static void
8853 output_loc_list (dw_loc_list_ref list_head)
8854 {
8855   dw_loc_list_ref curr = list_head;
8856
8857   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8858
8859   /* Walk the location list, and output each range + expression.  */
8860   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8861     {
8862       unsigned long size;
8863       /* Don't output an entry that starts and ends at the same address.  */
8864       if (strcmp (curr->begin, curr->end) == 0)
8865         continue;
8866       if (!have_multiple_function_sections)
8867         {
8868           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8869                                 "Location list begin address (%s)",
8870                                 list_head->ll_symbol);
8871           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8872                                 "Location list end address (%s)",
8873                                 list_head->ll_symbol);
8874         }
8875       else
8876         {
8877           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8878                                "Location list begin address (%s)",
8879                                list_head->ll_symbol);
8880           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8881                                "Location list end address (%s)",
8882                                list_head->ll_symbol);
8883         }
8884       size = size_of_locs (curr->expr);
8885
8886       /* Output the block length for this list of location operations.  */
8887       gcc_assert (size <= 0xffff);
8888       dw2_asm_output_data (2, size, "%s", "Location expression size");
8889
8890       output_loc_sequence (curr->expr);
8891     }
8892
8893   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8894                        "Location list terminator begin (%s)",
8895                        list_head->ll_symbol);
8896   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8897                        "Location list terminator end (%s)",
8898                        list_head->ll_symbol);
8899 }
8900
8901 /* Output the DIE and its attributes.  Called recursively to generate
8902    the definitions of each child DIE.  */
8903
8904 static void
8905 output_die (dw_die_ref die)
8906 {
8907   dw_attr_ref a;
8908   dw_die_ref c;
8909   unsigned long size;
8910   unsigned ix;
8911
8912   /* If someone in another CU might refer to us, set up a symbol for
8913      them to point to.  */
8914   if (die->die_symbol)
8915     output_die_symbol (die);
8916
8917   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8918                                (unsigned long)die->die_offset,
8919                                dwarf_tag_name (die->die_tag));
8920
8921   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8922     {
8923       const char *name = dwarf_attr_name (a->dw_attr);
8924
8925       switch (AT_class (a))
8926         {
8927         case dw_val_class_addr:
8928           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8929           break;
8930
8931         case dw_val_class_offset:
8932           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8933                                "%s", name);
8934           break;
8935
8936         case dw_val_class_range_list:
8937           {
8938             char *p = strchr (ranges_section_label, '\0');
8939
8940             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8941                      a->dw_attr_val.v.val_offset);
8942             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8943                                    debug_ranges_section, "%s", name);
8944             *p = '\0';
8945           }
8946           break;
8947
8948         case dw_val_class_loc:
8949           size = size_of_locs (AT_loc (a));
8950
8951           /* Output the block length for this list of location operations.  */
8952           dw2_asm_output_data (constant_size (size), size, "%s", name);
8953
8954           output_loc_sequence (AT_loc (a));
8955           break;
8956
8957         case dw_val_class_const:
8958           /* ??? It would be slightly more efficient to use a scheme like is
8959              used for unsigned constants below, but gdb 4.x does not sign
8960              extend.  Gdb 5.x does sign extend.  */
8961           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8962           break;
8963
8964         case dw_val_class_unsigned_const:
8965           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8966                                AT_unsigned (a), "%s", name);
8967           break;
8968
8969         case dw_val_class_long_long:
8970           {
8971             unsigned HOST_WIDE_INT first, second;
8972
8973             dw2_asm_output_data (1,
8974                                  2 * HOST_BITS_PER_WIDE_INT
8975                                  / HOST_BITS_PER_CHAR,
8976                                  "%s", name);
8977
8978             if (WORDS_BIG_ENDIAN)
8979               {
8980                 first = CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long);
8981                 second = CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long);
8982               }
8983             else
8984               {
8985                 first = CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long);
8986                 second = CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long);
8987               }
8988
8989             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8990                                  first, "long long constant");
8991             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8992                                  second, NULL);
8993           }
8994           break;
8995
8996         case dw_val_class_vec:
8997           {
8998             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8999             unsigned int len = a->dw_attr_val.v.val_vec.length;
9000             unsigned int i;
9001             unsigned char *p;
9002
9003             dw2_asm_output_data (constant_size (len * elt_size),
9004                                  len * elt_size, "%s", name);
9005             if (elt_size > sizeof (HOST_WIDE_INT))
9006               {
9007                 elt_size /= 2;
9008                 len *= 2;
9009               }
9010             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9011                  i < len;
9012                  i++, p += elt_size)
9013               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9014                                    "fp or vector constant word %u", i);
9015             break;
9016           }
9017
9018         case dw_val_class_flag:
9019           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9020           break;
9021
9022         case dw_val_class_loc_list:
9023           {
9024             char *sym = AT_loc_list (a)->ll_symbol;
9025
9026             gcc_assert (sym);
9027             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9028                                    "%s", name);
9029           }
9030           break;
9031
9032         case dw_val_class_die_ref:
9033           if (AT_ref_external (a))
9034             {
9035               char *sym = AT_ref (a)->die_symbol;
9036               int size;
9037
9038               gcc_assert (sym);
9039
9040               /* In DWARF2, DW_FORM_ref_addr is sized by target address
9041                  length, whereas in DWARF3 it's always sized as an offset.  */
9042               if (dwarf_version == 2)
9043                 size = DWARF2_ADDR_SIZE;
9044               else
9045                 size = DWARF_OFFSET_SIZE;
9046               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
9047             }
9048           else
9049             {
9050               gcc_assert (AT_ref (a)->die_offset);
9051               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9052                                    "%s", name);
9053             }
9054           break;
9055
9056         case dw_val_class_fde_ref:
9057           {
9058             char l1[20];
9059
9060             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9061                                          a->dw_attr_val.v.val_fde_index * 2);
9062             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9063                                    "%s", name);
9064           }
9065           break;
9066
9067         case dw_val_class_lbl_id:
9068           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9069           break;
9070
9071         case dw_val_class_lineptr:
9072           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9073                                  debug_line_section, "%s", name);
9074           break;
9075
9076         case dw_val_class_macptr:
9077           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9078                                  debug_macinfo_section, "%s", name);
9079           break;
9080
9081         case dw_val_class_str:
9082           if (AT_string_form (a) == DW_FORM_strp)
9083             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9084                                    a->dw_attr_val.v.val_str->label,
9085                                    debug_str_section,
9086                                    "%s: \"%s\"", name, AT_string (a));
9087           else
9088             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9089           break;
9090
9091         case dw_val_class_file:
9092           {
9093             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9094
9095             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9096                                  a->dw_attr_val.v.val_file->filename);
9097             break;
9098           }
9099
9100         default:
9101           gcc_unreachable ();
9102         }
9103     }
9104
9105   FOR_EACH_CHILD (die, c, output_die (c));
9106
9107   /* Add null byte to terminate sibling list.  */
9108   if (die->die_child != NULL)
9109     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
9110                          (unsigned long) die->die_offset);
9111 }
9112
9113 /* Output the compilation unit that appears at the beginning of the
9114    .debug_info section, and precedes the DIE descriptions.  */
9115
9116 static void
9117 output_compilation_unit_header (void)
9118 {
9119   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9120     dw2_asm_output_data (4, 0xffffffff,
9121       "Initial length escape value indicating 64-bit DWARF extension");
9122   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9123                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9124                        "Length of Compilation Unit Info");
9125   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9126   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9127                          debug_abbrev_section,
9128                          "Offset Into Abbrev. Section");
9129   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9130 }
9131
9132 /* Output the compilation unit DIE and its children.  */
9133
9134 static void
9135 output_comp_unit (dw_die_ref die, int output_if_empty)
9136 {
9137   const char *secname;
9138   char *oldsym, *tmp;
9139
9140   /* Unless we are outputting main CU, we may throw away empty ones.  */
9141   if (!output_if_empty && die->die_child == NULL)
9142     return;
9143
9144   /* Even if there are no children of this DIE, we must output the information
9145      about the compilation unit.  Otherwise, on an empty translation unit, we
9146      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9147      will then complain when examining the file.  First mark all the DIEs in
9148      this CU so we know which get local refs.  */
9149   mark_dies (die);
9150
9151   build_abbrev_table (die);
9152
9153   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9154   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9155   calc_die_sizes (die);
9156
9157   oldsym = die->die_symbol;
9158   if (oldsym)
9159     {
9160       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9161
9162       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9163       secname = tmp;
9164       die->die_symbol = NULL;
9165       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9166     }
9167   else
9168     switch_to_section (debug_info_section);
9169
9170   /* Output debugging information.  */
9171   output_compilation_unit_header ();
9172   output_die (die);
9173
9174   /* Leave the marks on the main CU, so we can check them in
9175      output_pubnames.  */
9176   if (oldsym)
9177     {
9178       unmark_dies (die);
9179       die->die_symbol = oldsym;
9180     }
9181 }
9182
9183 /* Return the DWARF2/3 pubname associated with a decl.  */
9184
9185 static const char *
9186 dwarf2_name (tree decl, int scope)
9187 {
9188   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9189 }
9190
9191 /* Add a new entry to .debug_pubnames if appropriate.  */
9192
9193 static void
9194 add_pubname_string (const char *str, dw_die_ref die)
9195 {
9196   pubname_entry e;
9197
9198   e.die = die;
9199   e.name = xstrdup (str);
9200   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9201 }
9202
9203 static void
9204 add_pubname (tree decl, dw_die_ref die)
9205 {
9206   if (TREE_PUBLIC (decl))
9207     add_pubname_string (dwarf2_name (decl, 1), die);
9208 }
9209
9210 /* Add a new entry to .debug_pubtypes if appropriate.  */
9211
9212 static void
9213 add_pubtype (tree decl, dw_die_ref die)
9214 {
9215   pubname_entry e;
9216
9217   e.name = NULL;
9218   if ((TREE_PUBLIC (decl)
9219        || die->die_parent == comp_unit_die)
9220       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9221     {
9222       e.die = die;
9223       if (TYPE_P (decl))
9224         {
9225           if (TYPE_NAME (decl))
9226             {
9227               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9228                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9229               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9230                        && DECL_NAME (TYPE_NAME (decl)))
9231                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9232               else
9233                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9234             }
9235         }
9236       else
9237         e.name = xstrdup (dwarf2_name (decl, 1));
9238
9239       /* If we don't have a name for the type, there's no point in adding
9240          it to the table.  */
9241       if (e.name && e.name[0] != '\0')
9242         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9243     }
9244 }
9245
9246 /* Output the public names table used to speed up access to externally
9247    visible names; or the public types table used to find type definitions.  */
9248
9249 static void
9250 output_pubnames (VEC (pubname_entry, gc) * names)
9251 {
9252   unsigned i;
9253   unsigned long pubnames_length = size_of_pubnames (names);
9254   pubname_ref pub;
9255
9256   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9257     dw2_asm_output_data (4, 0xffffffff,
9258       "Initial length escape value indicating 64-bit DWARF extension");
9259   if (names == pubname_table)
9260     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9261                          "Length of Public Names Info");
9262   else
9263     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9264                          "Length of Public Type Names Info");
9265   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
9266   dw2_asm_output_data (2, 2, "DWARF Version");
9267   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9268                          debug_info_section,
9269                          "Offset of Compilation Unit Info");
9270   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9271                        "Compilation Unit Length");
9272
9273   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
9274     {
9275       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9276       if (names == pubname_table)
9277         gcc_assert (pub->die->die_mark);
9278
9279       if (names != pubtype_table
9280           || pub->die->die_offset != 0
9281           || !flag_eliminate_unused_debug_types)
9282         {
9283           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9284                                "DIE offset");
9285
9286           dw2_asm_output_nstring (pub->name, -1, "external name");
9287         }
9288     }
9289
9290   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9291 }
9292
9293 /* Add a new entry to .debug_aranges if appropriate.  */
9294
9295 static void
9296 add_arange (tree decl, dw_die_ref die)
9297 {
9298   if (! DECL_SECTION_NAME (decl))
9299     return;
9300
9301   if (arange_table_in_use == arange_table_allocated)
9302     {
9303       arange_table_allocated += ARANGE_TABLE_INCREMENT;
9304       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
9305                                     arange_table_allocated);
9306       memset (arange_table + arange_table_in_use, 0,
9307               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
9308     }
9309
9310   arange_table[arange_table_in_use++] = die;
9311 }
9312
9313 /* Output the information that goes into the .debug_aranges table.
9314    Namely, define the beginning and ending address range of the
9315    text section generated for this compilation unit.  */
9316
9317 static void
9318 output_aranges (void)
9319 {
9320   unsigned i;
9321   unsigned long aranges_length = size_of_aranges ();
9322
9323   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9324     dw2_asm_output_data (4, 0xffffffff,
9325       "Initial length escape value indicating 64-bit DWARF extension");
9326   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9327                        "Length of Address Ranges Info");
9328   /* Version number for aranges is still 2, even in DWARF3.  */
9329   dw2_asm_output_data (2, 2, "DWARF Version");
9330   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9331                          debug_info_section,
9332                          "Offset of Compilation Unit Info");
9333   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9334   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9335
9336   /* We need to align to twice the pointer size here.  */
9337   if (DWARF_ARANGES_PAD_SIZE)
9338     {
9339       /* Pad using a 2 byte words so that padding is correct for any
9340          pointer size.  */
9341       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9342                            2 * DWARF2_ADDR_SIZE);
9343       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9344         dw2_asm_output_data (2, 0, NULL);
9345     }
9346
9347   /* It is necessary not to output these entries if the sections were
9348      not used; if the sections were not used, the length will be 0 and
9349      the address may end up as 0 if the section is discarded by ld
9350      --gc-sections, leaving an invalid (0, 0) entry that can be
9351      confused with the terminator.  */
9352   if (text_section_used)
9353     {
9354       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9355       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9356                             text_section_label, "Length");
9357     }
9358   if (cold_text_section_used)
9359     {
9360       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9361                            "Address");
9362       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9363                             cold_text_section_label, "Length");
9364     }
9365
9366   for (i = 0; i < arange_table_in_use; i++)
9367     {
9368       dw_die_ref die = arange_table[i];
9369
9370       /* We shouldn't see aranges for DIEs outside of the main CU.  */
9371       gcc_assert (die->die_mark);
9372
9373       if (die->die_tag == DW_TAG_subprogram)
9374         {
9375           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
9376                                "Address");
9377           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
9378                                 get_AT_low_pc (die), "Length");
9379         }
9380       else
9381         {
9382           /* A static variable; extract the symbol from DW_AT_location.
9383              Note that this code isn't currently hit, as we only emit
9384              aranges for functions (jason 9/23/99).  */
9385           dw_attr_ref a = get_AT (die, DW_AT_location);
9386           dw_loc_descr_ref loc;
9387
9388           gcc_assert (a && AT_class (a) == dw_val_class_loc);
9389
9390           loc = AT_loc (a);
9391           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
9392
9393           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
9394                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
9395           dw2_asm_output_data (DWARF2_ADDR_SIZE,
9396                                get_AT_unsigned (die, DW_AT_byte_size),
9397                                "Length");
9398         }
9399     }
9400
9401   /* Output the terminator words.  */
9402   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9403   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9404 }
9405
9406 /* Add a new entry to .debug_ranges.  Return the offset at which it
9407    was placed.  */
9408
9409 static unsigned int
9410 add_ranges_num (int num)
9411 {
9412   unsigned int in_use = ranges_table_in_use;
9413
9414   if (in_use == ranges_table_allocated)
9415     {
9416       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9417       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9418                                     ranges_table_allocated);
9419       memset (ranges_table + ranges_table_in_use, 0,
9420               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9421     }
9422
9423   ranges_table[in_use].num = num;
9424   ranges_table_in_use = in_use + 1;
9425
9426   return in_use * 2 * DWARF2_ADDR_SIZE;
9427 }
9428
9429 /* Add a new entry to .debug_ranges corresponding to a block, or a
9430    range terminator if BLOCK is NULL.  */
9431
9432 static unsigned int
9433 add_ranges (const_tree block)
9434 {
9435   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9436 }
9437
9438 /* Add a new entry to .debug_ranges corresponding to a pair of
9439    labels.  */
9440
9441 static unsigned int
9442 add_ranges_by_labels (const char *begin, const char *end)
9443 {
9444   unsigned int in_use = ranges_by_label_in_use;
9445
9446   if (in_use == ranges_by_label_allocated)
9447     {
9448       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9449       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9450                                        ranges_by_label,
9451                                        ranges_by_label_allocated);
9452       memset (ranges_by_label + ranges_by_label_in_use, 0,
9453               RANGES_TABLE_INCREMENT
9454               * sizeof (struct dw_ranges_by_label_struct));
9455     }
9456
9457   ranges_by_label[in_use].begin = begin;
9458   ranges_by_label[in_use].end = end;
9459   ranges_by_label_in_use = in_use + 1;
9460
9461   return add_ranges_num (-(int)in_use - 1);
9462 }
9463
9464 static void
9465 output_ranges (void)
9466 {
9467   unsigned i;
9468   static const char *const start_fmt = "Offset 0x%x";
9469   const char *fmt = start_fmt;
9470
9471   for (i = 0; i < ranges_table_in_use; i++)
9472     {
9473       int block_num = ranges_table[i].num;
9474
9475       if (block_num > 0)
9476         {
9477           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9478           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9479
9480           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9481           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9482
9483           /* If all code is in the text section, then the compilation
9484              unit base address defaults to DW_AT_low_pc, which is the
9485              base of the text section.  */
9486           if (!have_multiple_function_sections)
9487             {
9488               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9489                                     text_section_label,
9490                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9491               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9492                                     text_section_label, NULL);
9493             }
9494
9495           /* Otherwise, the compilation unit base address is zero,
9496              which allows us to use absolute addresses, and not worry
9497              about whether the target supports cross-section
9498              arithmetic.  */
9499           else
9500             {
9501               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9502                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9503               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9504             }
9505
9506           fmt = NULL;
9507         }
9508
9509       /* Negative block_num stands for an index into ranges_by_label.  */
9510       else if (block_num < 0)
9511         {
9512           int lab_idx = - block_num - 1;
9513
9514           if (!have_multiple_function_sections)
9515             {
9516               gcc_unreachable ();
9517 #if 0
9518               /* If we ever use add_ranges_by_labels () for a single
9519                  function section, all we have to do is to take out
9520                  the #if 0 above.  */
9521               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9522                                     ranges_by_label[lab_idx].begin,
9523                                     text_section_label,
9524                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9525               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9526                                     ranges_by_label[lab_idx].end,
9527                                     text_section_label, NULL);
9528 #endif
9529             }
9530           else
9531             {
9532               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9533                                    ranges_by_label[lab_idx].begin,
9534                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9535               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9536                                    ranges_by_label[lab_idx].end,
9537                                    NULL);
9538             }
9539         }
9540       else
9541         {
9542           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9543           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9544           fmt = start_fmt;
9545         }
9546     }
9547 }
9548
9549 /* Data structure containing information about input files.  */
9550 struct file_info
9551 {
9552   const char *path;     /* Complete file name.  */
9553   const char *fname;    /* File name part.  */
9554   int length;           /* Length of entire string.  */
9555   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9556   int dir_idx;          /* Index in directory table.  */
9557 };
9558
9559 /* Data structure containing information about directories with source
9560    files.  */
9561 struct dir_info
9562 {
9563   const char *path;     /* Path including directory name.  */
9564   int length;           /* Path length.  */
9565   int prefix;           /* Index of directory entry which is a prefix.  */
9566   int count;            /* Number of files in this directory.  */
9567   int dir_idx;          /* Index of directory used as base.  */
9568 };
9569
9570 /* Callback function for file_info comparison.  We sort by looking at
9571    the directories in the path.  */
9572
9573 static int
9574 file_info_cmp (const void *p1, const void *p2)
9575 {
9576   const struct file_info *const s1 = (const struct file_info *) p1;
9577   const struct file_info *const s2 = (const struct file_info *) p2;
9578   const unsigned char *cp1;
9579   const unsigned char *cp2;
9580
9581   /* Take care of file names without directories.  We need to make sure that
9582      we return consistent values to qsort since some will get confused if
9583      we return the same value when identical operands are passed in opposite
9584      orders.  So if neither has a directory, return 0 and otherwise return
9585      1 or -1 depending on which one has the directory.  */
9586   if ((s1->path == s1->fname || s2->path == s2->fname))
9587     return (s2->path == s2->fname) - (s1->path == s1->fname);
9588
9589   cp1 = (const unsigned char *) s1->path;
9590   cp2 = (const unsigned char *) s2->path;
9591
9592   while (1)
9593     {
9594       ++cp1;
9595       ++cp2;
9596       /* Reached the end of the first path?  If so, handle like above.  */
9597       if ((cp1 == (const unsigned char *) s1->fname)
9598           || (cp2 == (const unsigned char *) s2->fname))
9599         return ((cp2 == (const unsigned char *) s2->fname)
9600                 - (cp1 == (const unsigned char *) s1->fname));
9601
9602       /* Character of current path component the same?  */
9603       else if (*cp1 != *cp2)
9604         return *cp1 - *cp2;
9605     }
9606 }
9607
9608 struct file_name_acquire_data
9609 {
9610   struct file_info *files;
9611   int used_files;
9612   int max_files;
9613 };
9614
9615 /* Traversal function for the hash table.  */
9616
9617 static int
9618 file_name_acquire (void ** slot, void *data)
9619 {
9620   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9621   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9622   struct file_info *fi;
9623   const char *f;
9624
9625   gcc_assert (fnad->max_files >= d->emitted_number);
9626
9627   if (! d->emitted_number)
9628     return 1;
9629
9630   gcc_assert (fnad->max_files != fnad->used_files);
9631
9632   fi = fnad->files + fnad->used_files++;
9633
9634   /* Skip all leading "./".  */
9635   f = d->filename;
9636   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9637     f += 2;
9638
9639   /* Create a new array entry.  */
9640   fi->path = f;
9641   fi->length = strlen (f);
9642   fi->file_idx = d;
9643
9644   /* Search for the file name part.  */
9645   f = strrchr (f, DIR_SEPARATOR);
9646 #if defined (DIR_SEPARATOR_2)
9647   {
9648     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9649
9650     if (g != NULL)
9651       {
9652         if (f == NULL || f < g)
9653           f = g;
9654       }
9655   }
9656 #endif
9657
9658   fi->fname = f == NULL ? fi->path : f + 1;
9659   return 1;
9660 }
9661
9662 /* Output the directory table and the file name table.  We try to minimize
9663    the total amount of memory needed.  A heuristic is used to avoid large
9664    slowdowns with many input files.  */
9665
9666 static void
9667 output_file_names (void)
9668 {
9669   struct file_name_acquire_data fnad;
9670   int numfiles;
9671   struct file_info *files;
9672   struct dir_info *dirs;
9673   int *saved;
9674   int *savehere;
9675   int *backmap;
9676   int ndirs;
9677   int idx_offset;
9678   int i;
9679   int idx;
9680
9681   if (!last_emitted_file)
9682     {
9683       dw2_asm_output_data (1, 0, "End directory table");
9684       dw2_asm_output_data (1, 0, "End file name table");
9685       return;
9686     }
9687
9688   numfiles = last_emitted_file->emitted_number;
9689
9690   /* Allocate the various arrays we need.  */
9691   files = XALLOCAVEC (struct file_info, numfiles);
9692   dirs = XALLOCAVEC (struct dir_info, numfiles);
9693
9694   fnad.files = files;
9695   fnad.used_files = 0;
9696   fnad.max_files = numfiles;
9697   htab_traverse (file_table, file_name_acquire, &fnad);
9698   gcc_assert (fnad.used_files == fnad.max_files);
9699
9700   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9701
9702   /* Find all the different directories used.  */
9703   dirs[0].path = files[0].path;
9704   dirs[0].length = files[0].fname - files[0].path;
9705   dirs[0].prefix = -1;
9706   dirs[0].count = 1;
9707   dirs[0].dir_idx = 0;
9708   files[0].dir_idx = 0;
9709   ndirs = 1;
9710
9711   for (i = 1; i < numfiles; i++)
9712     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9713         && memcmp (dirs[ndirs - 1].path, files[i].path,
9714                    dirs[ndirs - 1].length) == 0)
9715       {
9716         /* Same directory as last entry.  */
9717         files[i].dir_idx = ndirs - 1;
9718         ++dirs[ndirs - 1].count;
9719       }
9720     else
9721       {
9722         int j;
9723
9724         /* This is a new directory.  */
9725         dirs[ndirs].path = files[i].path;
9726         dirs[ndirs].length = files[i].fname - files[i].path;
9727         dirs[ndirs].count = 1;
9728         dirs[ndirs].dir_idx = ndirs;
9729         files[i].dir_idx = ndirs;
9730
9731         /* Search for a prefix.  */
9732         dirs[ndirs].prefix = -1;
9733         for (j = 0; j < ndirs; j++)
9734           if (dirs[j].length < dirs[ndirs].length
9735               && dirs[j].length > 1
9736               && (dirs[ndirs].prefix == -1
9737                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9738               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9739             dirs[ndirs].prefix = j;
9740
9741         ++ndirs;
9742       }
9743
9744   /* Now to the actual work.  We have to find a subset of the directories which
9745      allow expressing the file name using references to the directory table
9746      with the least amount of characters.  We do not do an exhaustive search
9747      where we would have to check out every combination of every single
9748      possible prefix.  Instead we use a heuristic which provides nearly optimal
9749      results in most cases and never is much off.  */
9750   saved = XALLOCAVEC (int, ndirs);
9751   savehere = XALLOCAVEC (int, ndirs);
9752
9753   memset (saved, '\0', ndirs * sizeof (saved[0]));
9754   for (i = 0; i < ndirs; i++)
9755     {
9756       int j;
9757       int total;
9758
9759       /* We can always save some space for the current directory.  But this
9760          does not mean it will be enough to justify adding the directory.  */
9761       savehere[i] = dirs[i].length;
9762       total = (savehere[i] - saved[i]) * dirs[i].count;
9763
9764       for (j = i + 1; j < ndirs; j++)
9765         {
9766           savehere[j] = 0;
9767           if (saved[j] < dirs[i].length)
9768             {
9769               /* Determine whether the dirs[i] path is a prefix of the
9770                  dirs[j] path.  */
9771               int k;
9772
9773               k = dirs[j].prefix;
9774               while (k != -1 && k != (int) i)
9775                 k = dirs[k].prefix;
9776
9777               if (k == (int) i)
9778                 {
9779                   /* Yes it is.  We can possibly save some memory by
9780                      writing the filenames in dirs[j] relative to
9781                      dirs[i].  */
9782                   savehere[j] = dirs[i].length;
9783                   total += (savehere[j] - saved[j]) * dirs[j].count;
9784                 }
9785             }
9786         }
9787
9788       /* Check whether we can save enough to justify adding the dirs[i]
9789          directory.  */
9790       if (total > dirs[i].length + 1)
9791         {
9792           /* It's worthwhile adding.  */
9793           for (j = i; j < ndirs; j++)
9794             if (savehere[j] > 0)
9795               {
9796                 /* Remember how much we saved for this directory so far.  */
9797                 saved[j] = savehere[j];
9798
9799                 /* Remember the prefix directory.  */
9800                 dirs[j].dir_idx = i;
9801               }
9802         }
9803     }
9804
9805   /* Emit the directory name table.  */
9806   idx = 1;
9807   idx_offset = dirs[0].length > 0 ? 1 : 0;
9808   for (i = 1 - idx_offset; i < ndirs; i++)
9809     dw2_asm_output_nstring (dirs[i].path,
9810                             dirs[i].length
9811                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9812                             "Directory Entry: 0x%x", i + idx_offset);
9813
9814   dw2_asm_output_data (1, 0, "End directory table");
9815
9816   /* We have to emit them in the order of emitted_number since that's
9817      used in the debug info generation.  To do this efficiently we
9818      generate a back-mapping of the indices first.  */
9819   backmap = XALLOCAVEC (int, numfiles);
9820   for (i = 0; i < numfiles; i++)
9821     backmap[files[i].file_idx->emitted_number - 1] = i;
9822
9823   /* Now write all the file names.  */
9824   for (i = 0; i < numfiles; i++)
9825     {
9826       int file_idx = backmap[i];
9827       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9828
9829 #ifdef VMS_DEBUGGING_INFO
9830 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9831
9832       /* Setting these fields can lead to debugger miscomparisons,
9833          but VMS Debug requires them to be set correctly.  */
9834
9835       int ver;
9836       long long cdt;
9837       long siz;
9838       int maxfilelen = strlen (files[file_idx].path)
9839                                + dirs[dir_idx].length
9840                                + MAX_VMS_VERSION_LEN + 1;
9841       char *filebuf = XALLOCAVEC (char, maxfilelen);
9842
9843       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9844       snprintf (filebuf, maxfilelen, "%s;%d",
9845                 files[file_idx].path + dirs[dir_idx].length, ver);
9846
9847       dw2_asm_output_nstring
9848         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
9849
9850       /* Include directory index.  */
9851       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9852
9853       /* Modification time.  */
9854       dw2_asm_output_data_uleb128
9855         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9856           ? cdt : 0,
9857          NULL);
9858
9859       /* File length in bytes.  */
9860       dw2_asm_output_data_uleb128
9861         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9862           ? siz : 0,
9863          NULL);
9864 #else
9865       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9866                               "File Entry: 0x%x", (unsigned) i + 1);
9867
9868       /* Include directory index.  */
9869       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9870
9871       /* Modification time.  */
9872       dw2_asm_output_data_uleb128 (0, NULL);
9873
9874       /* File length in bytes.  */
9875       dw2_asm_output_data_uleb128 (0, NULL);
9876 #endif
9877     }
9878
9879   dw2_asm_output_data (1, 0, "End file name table");
9880 }
9881
9882
9883 /* Output the source line number correspondence information.  This
9884    information goes into the .debug_line section.  */
9885
9886 static void
9887 output_line_info (void)
9888 {
9889   char l1[20], l2[20], p1[20], p2[20];
9890   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9891   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9892   unsigned opc;
9893   unsigned n_op_args;
9894   unsigned long lt_index;
9895   unsigned long current_line;
9896   long line_offset;
9897   long line_delta;
9898   unsigned long current_file;
9899   unsigned long function;
9900
9901   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9902   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9903   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9904   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9905
9906   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9907     dw2_asm_output_data (4, 0xffffffff,
9908       "Initial length escape value indicating 64-bit DWARF extension");
9909   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9910                         "Length of Source Line Info");
9911   ASM_OUTPUT_LABEL (asm_out_file, l1);
9912
9913   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9914   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9915   ASM_OUTPUT_LABEL (asm_out_file, p1);
9916
9917   /* Define the architecture-dependent minimum instruction length (in
9918    bytes).  In this implementation of DWARF, this field is used for
9919    information purposes only.  Since GCC generates assembly language,
9920    we have no a priori knowledge of how many instruction bytes are
9921    generated for each source line, and therefore can use only the
9922    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9923    commands.  Accordingly, we fix this as `1', which is "correct
9924    enough" for all architectures, and don't let the target override.  */
9925   dw2_asm_output_data (1, 1,
9926                        "Minimum Instruction Length");
9927
9928   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9929                        "Default is_stmt_start flag");
9930   dw2_asm_output_data (1, DWARF_LINE_BASE,
9931                        "Line Base Value (Special Opcodes)");
9932   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9933                        "Line Range Value (Special Opcodes)");
9934   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9935                        "Special Opcode Base");
9936
9937   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9938     {
9939       switch (opc)
9940         {
9941         case DW_LNS_advance_pc:
9942         case DW_LNS_advance_line:
9943         case DW_LNS_set_file:
9944         case DW_LNS_set_column:
9945         case DW_LNS_fixed_advance_pc:
9946           n_op_args = 1;
9947           break;
9948         default:
9949           n_op_args = 0;
9950           break;
9951         }
9952
9953       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9954                            opc, n_op_args);
9955     }
9956
9957   /* Write out the information about the files we use.  */
9958   output_file_names ();
9959   ASM_OUTPUT_LABEL (asm_out_file, p2);
9960
9961   /* We used to set the address register to the first location in the text
9962      section here, but that didn't accomplish anything since we already
9963      have a line note for the opening brace of the first function.  */
9964
9965   /* Generate the line number to PC correspondence table, encoded as
9966      a series of state machine operations.  */
9967   current_file = 1;
9968   current_line = 1;
9969
9970   if (cfun && in_cold_section_p)
9971     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9972   else
9973     strcpy (prev_line_label, text_section_label);
9974   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9975     {
9976       dw_line_info_ref line_info = &line_info_table[lt_index];
9977
9978 #if 0
9979       /* Disable this optimization for now; GDB wants to see two line notes
9980          at the beginning of a function so it can find the end of the
9981          prologue.  */
9982
9983       /* Don't emit anything for redundant notes.  Just updating the
9984          address doesn't accomplish anything, because we already assume
9985          that anything after the last address is this line.  */
9986       if (line_info->dw_line_num == current_line
9987           && line_info->dw_file_num == current_file)
9988         continue;
9989 #endif
9990
9991       /* Emit debug info for the address of the current line.
9992
9993          Unfortunately, we have little choice here currently, and must always
9994          use the most general form.  GCC does not know the address delta
9995          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9996          attributes which will give an upper bound on the address range.  We
9997          could perhaps use length attributes to determine when it is safe to
9998          use DW_LNS_fixed_advance_pc.  */
9999
10000       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
10001       if (0)
10002         {
10003           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
10004           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10005                                "DW_LNS_fixed_advance_pc");
10006           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10007         }
10008       else
10009         {
10010           /* This can handle any delta.  This takes
10011              4+DWARF2_ADDR_SIZE bytes.  */
10012           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10013           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10014           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10015           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10016         }
10017
10018       strcpy (prev_line_label, line_label);
10019
10020       /* Emit debug info for the source file of the current line, if
10021          different from the previous line.  */
10022       if (line_info->dw_file_num != current_file)
10023         {
10024           current_file = line_info->dw_file_num;
10025           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10026           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10027         }
10028
10029       /* Emit debug info for the current line number, choosing the encoding
10030          that uses the least amount of space.  */
10031       if (line_info->dw_line_num != current_line)
10032         {
10033           line_offset = line_info->dw_line_num - current_line;
10034           line_delta = line_offset - DWARF_LINE_BASE;
10035           current_line = line_info->dw_line_num;
10036           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10037             /* This can handle deltas from -10 to 234, using the current
10038                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
10039                takes 1 byte.  */
10040             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10041                                  "line %lu", current_line);
10042           else
10043             {
10044               /* This can handle any delta.  This takes at least 4 bytes,
10045                  depending on the value being encoded.  */
10046               dw2_asm_output_data (1, DW_LNS_advance_line,
10047                                    "advance to line %lu", current_line);
10048               dw2_asm_output_data_sleb128 (line_offset, NULL);
10049               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10050             }
10051         }
10052       else
10053         /* We still need to start a new row, so output a copy insn.  */
10054         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10055     }
10056
10057   /* Emit debug info for the address of the end of the function.  */
10058   if (0)
10059     {
10060       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10061                            "DW_LNS_fixed_advance_pc");
10062       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
10063     }
10064   else
10065     {
10066       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10067       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10068       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10069       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
10070     }
10071
10072   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10073   dw2_asm_output_data_uleb128 (1, NULL);
10074   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10075
10076   function = 0;
10077   current_file = 1;
10078   current_line = 1;
10079   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
10080     {
10081       dw_separate_line_info_ref line_info
10082         = &separate_line_info_table[lt_index];
10083
10084 #if 0
10085       /* Don't emit anything for redundant notes.  */
10086       if (line_info->dw_line_num == current_line
10087           && line_info->dw_file_num == current_file
10088           && line_info->function == function)
10089         goto cont;
10090 #endif
10091
10092       /* Emit debug info for the address of the current line.  If this is
10093          a new function, or the first line of a function, then we need
10094          to handle it differently.  */
10095       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
10096                                    lt_index);
10097       if (function != line_info->function)
10098         {
10099           function = line_info->function;
10100
10101           /* Set the address register to the first line in the function.  */
10102           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10103           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10104           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10105           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10106         }
10107       else
10108         {
10109           /* ??? See the DW_LNS_advance_pc comment above.  */
10110           if (0)
10111             {
10112               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10113                                    "DW_LNS_fixed_advance_pc");
10114               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10115             }
10116           else
10117             {
10118               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10119               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10120               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10121               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10122             }
10123         }
10124
10125       strcpy (prev_line_label, line_label);
10126
10127       /* Emit debug info for the source file of the current line, if
10128          different from the previous line.  */
10129       if (line_info->dw_file_num != current_file)
10130         {
10131           current_file = line_info->dw_file_num;
10132           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10133           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10134         }
10135
10136       /* Emit debug info for the current line number, choosing the encoding
10137          that uses the least amount of space.  */
10138       if (line_info->dw_line_num != current_line)
10139         {
10140           line_offset = line_info->dw_line_num - current_line;
10141           line_delta = line_offset - DWARF_LINE_BASE;
10142           current_line = line_info->dw_line_num;
10143           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10144             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10145                                  "line %lu", current_line);
10146           else
10147             {
10148               dw2_asm_output_data (1, DW_LNS_advance_line,
10149                                    "advance to line %lu", current_line);
10150               dw2_asm_output_data_sleb128 (line_offset, NULL);
10151               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10152             }
10153         }
10154       else
10155         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10156
10157 #if 0
10158     cont:
10159 #endif
10160
10161       lt_index++;
10162
10163       /* If we're done with a function, end its sequence.  */
10164       if (lt_index == separate_line_info_table_in_use
10165           || separate_line_info_table[lt_index].function != function)
10166         {
10167           current_file = 1;
10168           current_line = 1;
10169
10170           /* Emit debug info for the address of the end of the function.  */
10171           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
10172           if (0)
10173             {
10174               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10175                                    "DW_LNS_fixed_advance_pc");
10176               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10177             }
10178           else
10179             {
10180               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10181               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10182               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10183               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10184             }
10185
10186           /* Output the marker for the end of this sequence.  */
10187           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10188           dw2_asm_output_data_uleb128 (1, NULL);
10189           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10190         }
10191     }
10192
10193   /* Output the marker for the end of the line number info.  */
10194   ASM_OUTPUT_LABEL (asm_out_file, l2);
10195 }
10196 \f
10197 /* Given a pointer to a tree node for some base type, return a pointer to
10198    a DIE that describes the given type.
10199
10200    This routine must only be called for GCC type nodes that correspond to
10201    Dwarf base (fundamental) types.  */
10202
10203 static dw_die_ref
10204 base_type_die (tree type)
10205 {
10206   dw_die_ref base_type_result;
10207   enum dwarf_type encoding;
10208
10209   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10210     return 0;
10211
10212   /* If this is a subtype that should not be emitted as a subrange type,
10213      use the base type.  See subrange_type_for_debug_p.  */
10214   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10215     type = TREE_TYPE (type);
10216
10217   switch (TREE_CODE (type))
10218     {
10219     case INTEGER_TYPE:
10220       if (TYPE_STRING_FLAG (type))
10221         {
10222           if (TYPE_UNSIGNED (type))
10223             encoding = DW_ATE_unsigned_char;
10224           else
10225             encoding = DW_ATE_signed_char;
10226         }
10227       else if (TYPE_UNSIGNED (type))
10228         encoding = DW_ATE_unsigned;
10229       else
10230         encoding = DW_ATE_signed;
10231       break;
10232
10233     case REAL_TYPE:
10234       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10235         {
10236           if (dwarf_version >= 3 || !dwarf_strict)
10237             encoding = DW_ATE_decimal_float;
10238           else
10239             encoding = DW_ATE_lo_user;
10240         }
10241       else
10242         encoding = DW_ATE_float;
10243       break;
10244
10245     case FIXED_POINT_TYPE:
10246       if (!(dwarf_version >= 3 || !dwarf_strict))
10247         encoding = DW_ATE_lo_user;
10248       else if (TYPE_UNSIGNED (type))
10249         encoding = DW_ATE_unsigned_fixed;
10250       else
10251         encoding = DW_ATE_signed_fixed;
10252       break;
10253
10254       /* Dwarf2 doesn't know anything about complex ints, so use
10255          a user defined type for it.  */
10256     case COMPLEX_TYPE:
10257       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10258         encoding = DW_ATE_complex_float;
10259       else
10260         encoding = DW_ATE_lo_user;
10261       break;
10262
10263     case BOOLEAN_TYPE:
10264       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10265       encoding = DW_ATE_boolean;
10266       break;
10267
10268     default:
10269       /* No other TREE_CODEs are Dwarf fundamental types.  */
10270       gcc_unreachable ();
10271     }
10272
10273   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
10274
10275   /* This probably indicates a bug.  */
10276   if (! TYPE_NAME (type))
10277     add_name_attribute (base_type_result, "__unknown__");
10278
10279   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10280                    int_size_in_bytes (type));
10281   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10282
10283   return base_type_result;
10284 }
10285
10286 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10287    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10288
10289 static inline int
10290 is_base_type (tree type)
10291 {
10292   switch (TREE_CODE (type))
10293     {
10294     case ERROR_MARK:
10295     case VOID_TYPE:
10296     case INTEGER_TYPE:
10297     case REAL_TYPE:
10298     case FIXED_POINT_TYPE:
10299     case COMPLEX_TYPE:
10300     case BOOLEAN_TYPE:
10301       return 1;
10302
10303     case ARRAY_TYPE:
10304     case RECORD_TYPE:
10305     case UNION_TYPE:
10306     case QUAL_UNION_TYPE:
10307     case ENUMERAL_TYPE:
10308     case FUNCTION_TYPE:
10309     case METHOD_TYPE:
10310     case POINTER_TYPE:
10311     case REFERENCE_TYPE:
10312     case OFFSET_TYPE:
10313     case LANG_TYPE:
10314     case VECTOR_TYPE:
10315       return 0;
10316
10317     default:
10318       gcc_unreachable ();
10319     }
10320
10321   return 0;
10322 }
10323
10324 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10325    node, return the size in bits for the type if it is a constant, or else
10326    return the alignment for the type if the type's size is not constant, or
10327    else return BITS_PER_WORD if the type actually turns out to be an
10328    ERROR_MARK node.  */
10329
10330 static inline unsigned HOST_WIDE_INT
10331 simple_type_size_in_bits (const_tree type)
10332 {
10333   if (TREE_CODE (type) == ERROR_MARK)
10334     return BITS_PER_WORD;
10335   else if (TYPE_SIZE (type) == NULL_TREE)
10336     return 0;
10337   else if (host_integerp (TYPE_SIZE (type), 1))
10338     return tree_low_cst (TYPE_SIZE (type), 1);
10339   else
10340     return TYPE_ALIGN (type);
10341 }
10342
10343 /*  Given a pointer to a tree node for a subrange type, return a pointer
10344     to a DIE that describes the given type.  */
10345
10346 static dw_die_ref
10347 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10348 {
10349   dw_die_ref subrange_die;
10350   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10351
10352   if (context_die == NULL)
10353     context_die = comp_unit_die;
10354
10355   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10356
10357   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10358     {
10359       /* The size of the subrange type and its base type do not match,
10360          so we need to generate a size attribute for the subrange type.  */
10361       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10362     }
10363
10364   if (low)
10365     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10366   if (high)
10367     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10368
10369   return subrange_die;
10370 }
10371
10372 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10373    entry that chains various modifiers in front of the given type.  */
10374
10375 static dw_die_ref
10376 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10377                    dw_die_ref context_die)
10378 {
10379   enum tree_code code = TREE_CODE (type);
10380   dw_die_ref mod_type_die;
10381   dw_die_ref sub_die = NULL;
10382   tree item_type = NULL;
10383   tree qualified_type;
10384   tree name, low, high;
10385
10386   if (code == ERROR_MARK)
10387     return NULL;
10388
10389   /* See if we already have the appropriately qualified variant of
10390      this type.  */
10391   qualified_type
10392     = get_qualified_type (type,
10393                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10394                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10395
10396   /* If we do, then we can just use its DIE, if it exists.  */
10397   if (qualified_type)
10398     {
10399       mod_type_die = lookup_type_die (qualified_type);
10400       if (mod_type_die)
10401         return mod_type_die;
10402     }
10403
10404   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10405
10406   /* Handle C typedef types.  */
10407   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
10408     {
10409       tree dtype = TREE_TYPE (name);
10410
10411       if (qualified_type == dtype)
10412         {
10413           /* For a named type, use the typedef.  */
10414           gen_type_die (qualified_type, context_die);
10415           return lookup_type_die (qualified_type);
10416         }
10417       else if (is_const_type < TYPE_READONLY (dtype)
10418                || is_volatile_type < TYPE_VOLATILE (dtype)
10419                || (is_const_type <= TYPE_READONLY (dtype)
10420                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10421                    && DECL_ORIGINAL_TYPE (name) != type))
10422         /* cv-unqualified version of named type.  Just use the unnamed
10423            type to which it refers.  */
10424         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10425                                   is_const_type, is_volatile_type,
10426                                   context_die);
10427       /* Else cv-qualified version of named type; fall through.  */
10428     }
10429
10430   if (is_const_type)
10431     {
10432       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
10433       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10434     }
10435   else if (is_volatile_type)
10436     {
10437       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
10438       sub_die = modified_type_die (type, 0, 0, context_die);
10439     }
10440   else if (code == POINTER_TYPE)
10441     {
10442       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
10443       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10444                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10445       item_type = TREE_TYPE (type);
10446     }
10447   else if (code == REFERENCE_TYPE)
10448     {
10449       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
10450       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10451                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10452       item_type = TREE_TYPE (type);
10453     }
10454   else if (code == INTEGER_TYPE
10455            && TREE_TYPE (type) != NULL_TREE
10456            && subrange_type_for_debug_p (type, &low, &high))
10457     {
10458       mod_type_die = subrange_type_die (type, low, high, context_die);
10459       item_type = TREE_TYPE (type);
10460     }
10461   else if (is_base_type (type))
10462     mod_type_die = base_type_die (type);
10463   else
10464     {
10465       gen_type_die (type, context_die);
10466
10467       /* We have to get the type_main_variant here (and pass that to the
10468          `lookup_type_die' routine) because the ..._TYPE node we have
10469          might simply be a *copy* of some original type node (where the
10470          copy was created to help us keep track of typedef names) and
10471          that copy might have a different TYPE_UID from the original
10472          ..._TYPE node.  */
10473       if (TREE_CODE (type) != VECTOR_TYPE)
10474         return lookup_type_die (type_main_variant (type));
10475       else
10476         /* Vectors have the debugging information in the type,
10477            not the main variant.  */
10478         return lookup_type_die (type);
10479     }
10480
10481   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10482      don't output a DW_TAG_typedef, since there isn't one in the
10483      user's program; just attach a DW_AT_name to the type.  */
10484   if (name
10485       && (TREE_CODE (name) != TYPE_DECL
10486           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
10487     {
10488       if (TREE_CODE (name) == TYPE_DECL)
10489         /* Could just call add_name_and_src_coords_attributes here,
10490            but since this is a builtin type it doesn't have any
10491            useful source coordinates anyway.  */
10492         name = DECL_NAME (name);
10493       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10494     }
10495
10496   if (qualified_type)
10497     equate_type_number_to_die (qualified_type, mod_type_die);
10498
10499   if (item_type)
10500     /* We must do this after the equate_type_number_to_die call, in case
10501        this is a recursive type.  This ensures that the modified_type_die
10502        recursion will terminate even if the type is recursive.  Recursive
10503        types are possible in Ada.  */
10504     sub_die = modified_type_die (item_type,
10505                                  TYPE_READONLY (item_type),
10506                                  TYPE_VOLATILE (item_type),
10507                                  context_die);
10508
10509   if (sub_die != NULL)
10510     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10511
10512   return mod_type_die;
10513 }
10514
10515 /* Generate a new name for the parameter pack name NAME (an
10516    IDENTIFIER_NODE) that incorporates its */
10517
10518 static tree
10519 make_ith_pack_parameter_name (tree name, int i)
10520 {
10521   /* Munge the name to include the parameter index.  */
10522 #define NUMBUF_LEN 128
10523   char numbuf[NUMBUF_LEN];
10524   char* newname;
10525   int newname_len;
10526
10527   snprintf (numbuf, NUMBUF_LEN, "%i", i);
10528   newname_len = IDENTIFIER_LENGTH (name)
10529                 + strlen (numbuf) + 2;
10530   newname = (char*) alloca (newname_len);
10531   snprintf (newname, newname_len,
10532             "%s#%i", IDENTIFIER_POINTER (name), i);
10533   return get_identifier (newname);
10534 }
10535
10536 /* Generate DIEs for the generic parameters of T.
10537    T must be either a generic type or a generic function.
10538    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10539
10540 static void
10541 gen_generic_params_dies (tree t)
10542 {
10543   tree parms, args;
10544   int parms_num, i;
10545   dw_die_ref die = NULL;
10546
10547   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10548     return;
10549
10550   if (TYPE_P (t))
10551     die = lookup_type_die (t);
10552   else if (DECL_P (t))
10553     die = lookup_decl_die (t);
10554
10555   gcc_assert (die);
10556
10557   parms = lang_hooks.get_innermost_generic_parms (t);
10558   if (!parms)
10559     /* T has no generic parameter. It means T is neither a generic type
10560        or function. End of story.  */
10561     return;
10562
10563   parms_num = TREE_VEC_LENGTH (parms);
10564   args = lang_hooks.get_innermost_generic_args (t);
10565   for (i = 0; i < parms_num; i++)
10566     {
10567       tree parm, arg;
10568
10569       parm = TREE_VEC_ELT (parms, i);
10570       arg = TREE_VEC_ELT (args, i);
10571       if (parm && TREE_VALUE (parm) && arg)
10572         {
10573           tree pack_elems =
10574             lang_hooks.types.get_argument_pack_elems (arg);
10575           if (pack_elems)
10576             {
10577               /* So ARG is an argument pack and the elements of that pack
10578                  are stored in PACK_ELEMS.  */
10579               int i, len;
10580
10581               len = TREE_VEC_LENGTH (pack_elems);
10582               for (i = 0; i < len; i++)
10583                 generic_parameter_die (TREE_VALUE (parm),
10584                                        TREE_VEC_ELT (pack_elems, i),
10585                                        die, i);
10586             }
10587           else /* Arg is not an argument pack.  */
10588             generic_parameter_die (TREE_VALUE (parm),
10589                                    arg, die,
10590                                    -1/* Not a param pack.  */);
10591         }
10592     }
10593 }
10594
10595 /* Create and return a DIE for PARM which should be
10596    the representation of a generic type parameter.
10597    For instance, in the C++ front end, PARM would be a template parameter.
10598    ARG is the argument to PARM.
10599    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10600    as a child node.
10601    PACK_ELEM_INDEX is >= 0 if PARM is a generic parameter pack, and if ARG
10602    is one of the unpacked elements of the parameter PACK. In that case,
10603    PACK_ELEM_INDEX is the index of ARG in the parameter pack.  */
10604
10605 static dw_die_ref
10606 generic_parameter_die (tree parm, tree arg, dw_die_ref parent_die,
10607                        int pack_elem_index)
10608 {
10609   dw_die_ref tmpl_die = NULL;
10610   const char *name = NULL;
10611
10612   if (!parm || !DECL_NAME (parm) || !arg)
10613     return NULL;
10614
10615   /* We support non-type generic parameters and arguments,
10616      type generic parameters and arguments, as well as
10617      generic generic parameters (a.k.a. template template parameters in C++)
10618      and arguments.  */
10619   if (TREE_CODE (parm) == PARM_DECL)
10620     /* PARM is a nontype generic parameter  */
10621     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10622   else if (TREE_CODE (parm) == TYPE_DECL)
10623     /* PARM is a type generic parameter.  */
10624     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10625   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10626     /* PARM is a generic generic parameter.
10627        Its DIE is a GNU extension. It shall have a
10628        DW_AT_name attribute to represent the name of the template template
10629        parameter, and a DW_AT_GNU_template_name attribute to represent the
10630        name of the template template argument.  */
10631     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10632                         parent_die, parm);
10633   else
10634     gcc_unreachable ();
10635
10636   if (tmpl_die)
10637     {
10638       tree tmpl_type;
10639
10640       if (pack_elem_index >= 0)
10641         {
10642           /* PARM is an element of a parameter pack.
10643              Generate a name for it.  */
10644           tree identifier = make_ith_pack_parameter_name (DECL_NAME (parm),
10645                                                           pack_elem_index);
10646           if (identifier)
10647             name = IDENTIFIER_POINTER (identifier);
10648         }
10649       else
10650         name = IDENTIFIER_POINTER (DECL_NAME (parm));
10651
10652       gcc_assert (name);
10653       add_AT_string (tmpl_die, DW_AT_name, name);
10654
10655       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10656         {
10657           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10658              TMPL_DIE should have a child DW_AT_type attribute that is set
10659              to the type of the argument to PARM, which is ARG.
10660              If PARM is a type generic parameter, TMPL_DIE should have a
10661              child DW_AT_type that is set to ARG.  */
10662           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10663           add_type_attribute (tmpl_die, tmpl_type, 0,
10664                               TREE_THIS_VOLATILE (tmpl_type),
10665                               parent_die);
10666         }
10667       else
10668         {
10669           /* So TMPL_DIE is a DIE representing a
10670              a generic generic template parameter, a.k.a template template
10671              parameter in C++ and arg is a template.  */
10672
10673           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10674              to the name of the argument.  */
10675           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10676           add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10677         }
10678
10679       if (TREE_CODE (parm) == PARM_DECL)
10680         /* So PARM is a non-type generic parameter.
10681            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10682            attribute of TMPL_DIE which value represents the value
10683            of ARG.
10684            We must be careful here:
10685            The value of ARG might reference some function decls.
10686            We might currently be emitting debug info for a generic
10687            type and types are emitted before function decls, we don't
10688            know if the function decls referenced by ARG will actually be
10689            emitted after cgraph computations.
10690            So must defer the generation of the DW_AT_const_value to
10691            after cgraph is ready.  */
10692         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10693     }
10694
10695   return tmpl_die;
10696 }
10697
10698 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10699    an enumerated type.  */
10700
10701 static inline int
10702 type_is_enum (const_tree type)
10703 {
10704   return TREE_CODE (type) == ENUMERAL_TYPE;
10705 }
10706
10707 /* Return the DBX register number described by a given RTL node.  */
10708
10709 static unsigned int
10710 dbx_reg_number (const_rtx rtl)
10711 {
10712   unsigned regno = REGNO (rtl);
10713
10714   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10715
10716 #ifdef LEAF_REG_REMAP
10717   if (current_function_uses_only_leaf_regs)
10718     {
10719       int leaf_reg = LEAF_REG_REMAP (regno);
10720       if (leaf_reg != -1)
10721         regno = (unsigned) leaf_reg;
10722     }
10723 #endif
10724
10725   return DBX_REGISTER_NUMBER (regno);
10726 }
10727
10728 /* Optionally add a DW_OP_piece term to a location description expression.
10729    DW_OP_piece is only added if the location description expression already
10730    doesn't end with DW_OP_piece.  */
10731
10732 static void
10733 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10734 {
10735   dw_loc_descr_ref loc;
10736
10737   if (*list_head != NULL)
10738     {
10739       /* Find the end of the chain.  */
10740       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10741         ;
10742
10743       if (loc->dw_loc_opc != DW_OP_piece)
10744         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10745     }
10746 }
10747
10748 /* Return a location descriptor that designates a machine register or
10749    zero if there is none.  */
10750
10751 static dw_loc_descr_ref
10752 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10753 {
10754   rtx regs;
10755
10756   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10757     return 0;
10758
10759   regs = targetm.dwarf_register_span (rtl);
10760
10761   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10762     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10763   else
10764     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10765 }
10766
10767 /* Return a location descriptor that designates a machine register for
10768    a given hard register number.  */
10769
10770 static dw_loc_descr_ref
10771 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10772 {
10773   dw_loc_descr_ref reg_loc_descr;
10774
10775   if (regno <= 31)
10776     reg_loc_descr
10777       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10778   else
10779     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10780
10781   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10782     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10783
10784   return reg_loc_descr;
10785 }
10786
10787 /* Given an RTL of a register, return a location descriptor that
10788    designates a value that spans more than one register.  */
10789
10790 static dw_loc_descr_ref
10791 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10792                              enum var_init_status initialized)
10793 {
10794   int nregs, size, i;
10795   unsigned reg;
10796   dw_loc_descr_ref loc_result = NULL;
10797
10798   reg = REGNO (rtl);
10799 #ifdef LEAF_REG_REMAP
10800   if (current_function_uses_only_leaf_regs)
10801     {
10802       int leaf_reg = LEAF_REG_REMAP (reg);
10803       if (leaf_reg != -1)
10804         reg = (unsigned) leaf_reg;
10805     }
10806 #endif
10807   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10808   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10809
10810   /* Simple, contiguous registers.  */
10811   if (regs == NULL_RTX)
10812     {
10813       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10814
10815       loc_result = NULL;
10816       while (nregs--)
10817         {
10818           dw_loc_descr_ref t;
10819
10820           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10821                                       VAR_INIT_STATUS_INITIALIZED);
10822           add_loc_descr (&loc_result, t);
10823           add_loc_descr_op_piece (&loc_result, size);
10824           ++reg;
10825         }
10826       return loc_result;
10827     }
10828
10829   /* Now onto stupid register sets in non contiguous locations.  */
10830
10831   gcc_assert (GET_CODE (regs) == PARALLEL);
10832
10833   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10834   loc_result = NULL;
10835
10836   for (i = 0; i < XVECLEN (regs, 0); ++i)
10837     {
10838       dw_loc_descr_ref t;
10839
10840       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10841                                   VAR_INIT_STATUS_INITIALIZED);
10842       add_loc_descr (&loc_result, t);
10843       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10844       add_loc_descr_op_piece (&loc_result, size);
10845     }
10846
10847   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10848     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10849   return loc_result;
10850 }
10851
10852 #endif /* DWARF2_DEBUGGING_INFO */
10853
10854 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10855
10856 /* Return a location descriptor that designates a constant.  */
10857
10858 static dw_loc_descr_ref
10859 int_loc_descriptor (HOST_WIDE_INT i)
10860 {
10861   enum dwarf_location_atom op;
10862
10863   /* Pick the smallest representation of a constant, rather than just
10864      defaulting to the LEB encoding.  */
10865   if (i >= 0)
10866     {
10867       if (i <= 31)
10868         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10869       else if (i <= 0xff)
10870         op = DW_OP_const1u;
10871       else if (i <= 0xffff)
10872         op = DW_OP_const2u;
10873       else if (HOST_BITS_PER_WIDE_INT == 32
10874                || i <= 0xffffffff)
10875         op = DW_OP_const4u;
10876       else
10877         op = DW_OP_constu;
10878     }
10879   else
10880     {
10881       if (i >= -0x80)
10882         op = DW_OP_const1s;
10883       else if (i >= -0x8000)
10884         op = DW_OP_const2s;
10885       else if (HOST_BITS_PER_WIDE_INT == 32
10886                || i >= -0x80000000)
10887         op = DW_OP_const4s;
10888       else
10889         op = DW_OP_consts;
10890     }
10891
10892   return new_loc_descr (op, i, 0);
10893 }
10894
10895 /* Return loc description representing "address" of integer value.
10896    This can appear only as toplevel expression.  */
10897
10898 static dw_loc_descr_ref
10899 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10900 {
10901   int litsize;
10902   dw_loc_descr_ref loc_result = NULL;
10903
10904   if (!(dwarf_version >= 4 || !dwarf_strict))
10905     return NULL;
10906
10907   if (i >= 0)
10908     {
10909       if (i <= 31)
10910         litsize = 1;
10911       else if (i <= 0xff)
10912         litsize = 2;
10913       else if (i <= 0xffff)
10914         litsize = 3;
10915       else if (HOST_BITS_PER_WIDE_INT == 32
10916                || i <= 0xffffffff)
10917         litsize = 5;
10918       else
10919         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10920     }
10921   else
10922     {
10923       if (i >= -0x80)
10924         litsize = 2;
10925       else if (i >= -0x8000)
10926         litsize = 3;
10927       else if (HOST_BITS_PER_WIDE_INT == 32
10928                || i >= -0x80000000)
10929         litsize = 5;
10930       else
10931         litsize = 1 + size_of_sleb128 (i);
10932     }
10933   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10934      is more compact.  For DW_OP_stack_value we need:
10935      litsize + 1 (DW_OP_stack_value) + 1 (DW_OP_bit_size)
10936      + 1 (mode size)
10937      and for DW_OP_implicit_value:
10938      1 (DW_OP_implicit_value) + 1 (length) + mode_size.  */
10939   if ((int) DWARF2_ADDR_SIZE >= size
10940       && litsize + 1 + 1 + 1 < 1 + 1 + size)
10941     {
10942       loc_result = int_loc_descriptor (i);
10943       add_loc_descr (&loc_result,
10944                      new_loc_descr (DW_OP_stack_value, 0, 0));
10945       add_loc_descr_op_piece (&loc_result, size);
10946       return loc_result;
10947     }
10948
10949   loc_result = new_loc_descr (DW_OP_implicit_value,
10950                               size, 0);
10951   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10952   loc_result->dw_loc_oprnd2.v.val_int = i;
10953   return loc_result;
10954 }
10955 #endif
10956
10957 #ifdef DWARF2_DEBUGGING_INFO
10958
10959 /* Return a location descriptor that designates a base+offset location.  */
10960
10961 static dw_loc_descr_ref
10962 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10963                  enum var_init_status initialized)
10964 {
10965   unsigned int regno;
10966   dw_loc_descr_ref result;
10967   dw_fde_ref fde = current_fde ();
10968
10969   /* We only use "frame base" when we're sure we're talking about the
10970      post-prologue local stack frame.  We do this by *not* running
10971      register elimination until this point, and recognizing the special
10972      argument pointer and soft frame pointer rtx's.  */
10973   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10974     {
10975       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10976
10977       if (elim != reg)
10978         {
10979           if (GET_CODE (elim) == PLUS)
10980             {
10981               offset += INTVAL (XEXP (elim, 1));
10982               elim = XEXP (elim, 0);
10983             }
10984           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10985                        && (elim == hard_frame_pointer_rtx
10986                            || elim == stack_pointer_rtx))
10987                       || elim == (frame_pointer_needed
10988                                   ? hard_frame_pointer_rtx
10989                                   : stack_pointer_rtx));
10990
10991           /* If drap register is used to align stack, use frame
10992              pointer + offset to access stack variables.  If stack
10993              is aligned without drap, use stack pointer + offset to
10994              access stack variables.  */
10995           if (crtl->stack_realign_tried
10996               && reg == frame_pointer_rtx)
10997             {
10998               int base_reg
10999                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11000                                       ? HARD_FRAME_POINTER_REGNUM
11001                                       : STACK_POINTER_REGNUM);
11002               return new_reg_loc_descr (base_reg, offset);
11003             }
11004
11005           offset += frame_pointer_fb_offset;
11006           return new_loc_descr (DW_OP_fbreg, offset, 0);
11007         }
11008     }
11009   else if (fde
11010            && fde->drap_reg != INVALID_REGNUM
11011            && (fde->drap_reg == REGNO (reg)
11012                || fde->vdrap_reg == REGNO (reg)))
11013     {
11014       /* Use cfa+offset to represent the location of arguments passed
11015          on stack when drap is used to align stack.  */
11016       return new_loc_descr (DW_OP_fbreg, offset, 0);
11017     }
11018
11019   regno = dbx_reg_number (reg);
11020   if (regno <= 31)
11021     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11022                             offset, 0);
11023   else
11024     result = new_loc_descr (DW_OP_bregx, regno, offset);
11025
11026   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11027     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11028
11029   return result;
11030 }
11031
11032 /* Return true if this RTL expression describes a base+offset calculation.  */
11033
11034 static inline int
11035 is_based_loc (const_rtx rtl)
11036 {
11037   return (GET_CODE (rtl) == PLUS
11038           && ((REG_P (XEXP (rtl, 0))
11039                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11040                && CONST_INT_P (XEXP (rtl, 1)))));
11041 }
11042
11043 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11044    failed.  */
11045
11046 static dw_loc_descr_ref
11047 tls_mem_loc_descriptor (rtx mem)
11048 {
11049   tree base;
11050   dw_loc_descr_ref loc_result;
11051
11052   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
11053     return NULL;
11054
11055   base = get_base_address (MEM_EXPR (mem));
11056   if (base == NULL
11057       || TREE_CODE (base) != VAR_DECL
11058       || !DECL_THREAD_LOCAL_P (base))
11059     return NULL;
11060
11061   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 2);
11062   if (loc_result == NULL)
11063     return NULL;
11064
11065   if (INTVAL (MEM_OFFSET (mem)))
11066     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
11067
11068   return loc_result;
11069 }
11070
11071 /* Output debug info about reason why we failed to expand expression as dwarf
11072    expression.  */
11073
11074 static void
11075 expansion_failed (tree expr, rtx rtl, char const *reason)
11076 {
11077   if (dump_file && (dump_flags & TDF_DETAILS))
11078     {
11079       fprintf (dump_file, "Failed to expand as dwarf: ");
11080       if (expr)
11081         print_generic_expr (dump_file, expr, dump_flags);
11082       if (rtl)
11083         {
11084           fprintf (dump_file, "\n");
11085           print_rtl (dump_file, rtl);
11086         }
11087       fprintf (dump_file, "\nReason: %s\n", reason);
11088     }
11089 }
11090
11091 /* The following routine converts the RTL for a variable or parameter
11092    (resident in memory) into an equivalent Dwarf representation of a
11093    mechanism for getting the address of that same variable onto the top of a
11094    hypothetical "address evaluation" stack.
11095
11096    When creating memory location descriptors, we are effectively transforming
11097    the RTL for a memory-resident object into its Dwarf postfix expression
11098    equivalent.  This routine recursively descends an RTL tree, turning
11099    it into Dwarf postfix code as it goes.
11100
11101    MODE is the mode of the memory reference, needed to handle some
11102    autoincrement addressing modes.
11103
11104    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
11105    location list for RTL.
11106
11107    Return 0 if we can't represent the location.  */
11108
11109 static dw_loc_descr_ref
11110 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11111                     enum var_init_status initialized)
11112 {
11113   dw_loc_descr_ref mem_loc_result = NULL;
11114   enum dwarf_location_atom op;
11115   dw_loc_descr_ref op0, op1;
11116
11117   /* Note that for a dynamically sized array, the location we will generate a
11118      description of here will be the lowest numbered location which is
11119      actually within the array.  That's *not* necessarily the same as the
11120      zeroth element of the array.  */
11121
11122   rtl = targetm.delegitimize_address (rtl);
11123
11124   switch (GET_CODE (rtl))
11125     {
11126     case POST_INC:
11127     case POST_DEC:
11128     case POST_MODIFY:
11129       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
11130          just fall into the SUBREG code.  */
11131
11132       /* ... fall through ...  */
11133
11134     case SUBREG:
11135       /* The case of a subreg may arise when we have a local (register)
11136          variable or a formal (register) parameter which doesn't quite fill
11137          up an entire register.  For now, just assume that it is
11138          legitimate to make the Dwarf info refer to the whole register which
11139          contains the given subreg.  */
11140       rtl = XEXP (rtl, 0);
11141       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
11142         break;
11143       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
11144       break;
11145
11146     case REG:
11147       /* Whenever a register number forms a part of the description of the
11148          method for calculating the (dynamic) address of a memory resident
11149          object, DWARF rules require the register number be referred to as
11150          a "base register".  This distinction is not based in any way upon
11151          what category of register the hardware believes the given register
11152          belongs to.  This is strictly DWARF terminology we're dealing with
11153          here. Note that in cases where the location of a memory-resident
11154          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11155          OP_CONST (0)) the actual DWARF location descriptor that we generate
11156          may just be OP_BASEREG (basereg).  This may look deceptively like
11157          the object in question was allocated to a register (rather than in
11158          memory) so DWARF consumers need to be aware of the subtle
11159          distinction between OP_REG and OP_BASEREG.  */
11160       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11161         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11162       else if (stack_realign_drap
11163                && crtl->drap_reg
11164                && crtl->args.internal_arg_pointer == rtl
11165                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11166         {
11167           /* If RTL is internal_arg_pointer, which has been optimized
11168              out, use DRAP instead.  */
11169           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11170                                             VAR_INIT_STATUS_INITIALIZED);
11171         }
11172       break;
11173
11174     case SIGN_EXTEND:
11175     case ZERO_EXTEND:
11176       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11177                                 VAR_INIT_STATUS_INITIALIZED);
11178       if (op0 == 0)
11179         break;
11180       else
11181         {
11182           int shift = DWARF2_ADDR_SIZE
11183                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11184           shift *= BITS_PER_UNIT;
11185           if (GET_CODE (rtl) == SIGN_EXTEND)
11186             op = DW_OP_shra;
11187           else
11188             op = DW_OP_shr;
11189           mem_loc_result = op0;
11190           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11191           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11192           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11193           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11194         }
11195       break;
11196
11197     case MEM:
11198       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11199                                            VAR_INIT_STATUS_INITIALIZED);
11200       if (mem_loc_result == NULL)
11201         mem_loc_result = tls_mem_loc_descriptor (rtl);
11202       if (mem_loc_result != 0)
11203         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11204       break;
11205
11206     case LO_SUM:
11207          rtl = XEXP (rtl, 1);
11208
11209       /* ... fall through ...  */
11210
11211     case LABEL_REF:
11212       /* Some ports can transform a symbol ref into a label ref, because
11213          the symbol ref is too far away and has to be dumped into a constant
11214          pool.  */
11215     case CONST:
11216     case SYMBOL_REF:
11217       /* Alternatively, the symbol in the constant pool might be referenced
11218          by a different symbol.  */
11219       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
11220         {
11221           bool marked;
11222           rtx tmp = get_pool_constant_mark (rtl, &marked);
11223
11224           if (GET_CODE (tmp) == SYMBOL_REF)
11225             {
11226               rtl = tmp;
11227               if (CONSTANT_POOL_ADDRESS_P (tmp))
11228                 get_pool_constant_mark (tmp, &marked);
11229               else
11230                 marked = true;
11231             }
11232
11233           /* If all references to this pool constant were optimized away,
11234              it was not output and thus we can't represent it.
11235              FIXME: might try to use DW_OP_const_value here, though
11236              DW_OP_piece complicates it.  */
11237           if (!marked)
11238             {
11239               expansion_failed (NULL_TREE, rtl,
11240                                 "Constant was removed from constant pool.\n");
11241               return 0;
11242             }
11243         }
11244
11245       if (GET_CODE (rtl) == SYMBOL_REF
11246           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11247         {
11248           dw_loc_descr_ref temp;
11249
11250           /* If this is not defined, we have no way to emit the data.  */
11251           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11252             break;
11253
11254           temp = new_loc_descr (DW_OP_addr, 0, 0);
11255           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11256           temp->dw_loc_oprnd1.v.val_addr = rtl;
11257           temp->dtprel = true;
11258
11259           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11260           add_loc_descr (&mem_loc_result, temp);
11261
11262           break;
11263         }
11264
11265       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11266       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11267       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11268       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11269       break;
11270
11271     case CONCAT:
11272     case CONCATN:
11273     case VAR_LOCATION:
11274       expansion_failed (NULL_TREE, rtl,
11275                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11276       gcc_unreachable ();
11277       return 0;
11278
11279     case PRE_MODIFY:
11280       /* Extract the PLUS expression nested inside and fall into
11281          PLUS code below.  */
11282       rtl = XEXP (rtl, 1);
11283       goto plus;
11284
11285     case PRE_INC:
11286     case PRE_DEC:
11287       /* Turn these into a PLUS expression and fall into the PLUS code
11288          below.  */
11289       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
11290                           GEN_INT (GET_CODE (rtl) == PRE_INC
11291                                    ? GET_MODE_UNIT_SIZE (mode)
11292                                    : -GET_MODE_UNIT_SIZE (mode)));
11293
11294       /* ... fall through ...  */
11295
11296     case PLUS:
11297     plus:
11298       if (is_based_loc (rtl))
11299         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11300                                           INTVAL (XEXP (rtl, 1)),
11301                                           VAR_INIT_STATUS_INITIALIZED);
11302       else
11303         {
11304           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
11305                                                VAR_INIT_STATUS_INITIALIZED);
11306           if (mem_loc_result == 0)
11307             break;
11308
11309           if (CONST_INT_P (XEXP (rtl, 1)))
11310             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11311           else
11312             {
11313               dw_loc_descr_ref mem_loc_result2
11314                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11315                                       VAR_INIT_STATUS_INITIALIZED);
11316               if (mem_loc_result2 == 0)
11317                 break;
11318               add_loc_descr (&mem_loc_result, mem_loc_result2);
11319               add_loc_descr (&mem_loc_result,
11320                              new_loc_descr (DW_OP_plus, 0, 0));
11321             }
11322         }
11323       break;
11324
11325     /* If a pseudo-reg is optimized away, it is possible for it to
11326        be replaced with a MEM containing a multiply or shift.  */
11327     case MINUS:
11328       op = DW_OP_minus;
11329       goto do_binop;
11330
11331     case MULT:
11332       op = DW_OP_mul;
11333       goto do_binop;
11334
11335     case DIV:
11336       op = DW_OP_div;
11337       goto do_binop;
11338
11339     case MOD:
11340       op = DW_OP_mod;
11341       goto do_binop;
11342
11343     case ASHIFT:
11344       op = DW_OP_shl;
11345       goto do_binop;
11346
11347     case ASHIFTRT:
11348       op = DW_OP_shra;
11349       goto do_binop;
11350
11351     case LSHIFTRT:
11352       op = DW_OP_shr;
11353       goto do_binop;
11354
11355     case AND:
11356       op = DW_OP_and;
11357       goto do_binop;
11358
11359     case IOR:
11360       op = DW_OP_or;
11361       goto do_binop;
11362
11363     case XOR:
11364       op = DW_OP_xor;
11365       goto do_binop;
11366
11367     do_binop:
11368       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11369                                 VAR_INIT_STATUS_INITIALIZED);
11370       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11371                                 VAR_INIT_STATUS_INITIALIZED);
11372
11373       if (op0 == 0 || op1 == 0)
11374         break;
11375
11376       mem_loc_result = op0;
11377       add_loc_descr (&mem_loc_result, op1);
11378       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11379       break;
11380
11381     case NOT:
11382       op = DW_OP_not;
11383       goto do_unop;
11384
11385     case ABS:
11386       op = DW_OP_abs;
11387       goto do_unop;
11388
11389     case NEG:
11390       op = DW_OP_neg;
11391       goto do_unop;
11392
11393     do_unop:
11394       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11395                                 VAR_INIT_STATUS_INITIALIZED);
11396
11397       if (op0 == 0)
11398         break;
11399
11400       mem_loc_result = op0;
11401       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11402       break;
11403
11404     case CONST_INT:
11405       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11406       break;
11407
11408     case EQ:
11409       op = DW_OP_eq;
11410       goto do_scompare;
11411
11412     case GE:
11413       op = DW_OP_ge;
11414       goto do_scompare;
11415
11416     case GT:
11417       op = DW_OP_gt;
11418       goto do_scompare;
11419
11420     case LE:
11421       op = DW_OP_le;
11422       goto do_scompare;
11423
11424     case LT:
11425       op = DW_OP_lt;
11426       goto do_scompare;
11427
11428     case NE:
11429       op = DW_OP_ne;
11430       goto do_scompare;
11431
11432     do_scompare:
11433       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11434           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11435           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11436         break;
11437
11438       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11439                                 VAR_INIT_STATUS_INITIALIZED);
11440       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11441                                 VAR_INIT_STATUS_INITIALIZED);
11442
11443       if (op0 == 0 || op1 == 0)
11444         break;
11445
11446       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11447         {
11448           int shift = DWARF2_ADDR_SIZE
11449                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11450           shift *= BITS_PER_UNIT;
11451           add_loc_descr (&op0, int_loc_descriptor (shift));
11452           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11453           if (CONST_INT_P (XEXP (rtl, 1)))
11454             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11455           else
11456             {
11457               add_loc_descr (&op1, int_loc_descriptor (shift));
11458               add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11459             }
11460         }
11461
11462     do_compare:
11463       mem_loc_result = op0;
11464       add_loc_descr (&mem_loc_result, op1);
11465       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11466       if (STORE_FLAG_VALUE != 1)
11467         {
11468           add_loc_descr (&mem_loc_result,
11469                          int_loc_descriptor (STORE_FLAG_VALUE));
11470           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11471         }
11472       break;
11473
11474     case GEU:
11475       op = DW_OP_ge;
11476       goto do_ucompare;
11477
11478     case GTU:
11479       op = DW_OP_gt;
11480       goto do_ucompare;
11481
11482     case LEU:
11483       op = DW_OP_le;
11484       goto do_ucompare;
11485
11486     case LTU:
11487       op = DW_OP_lt;
11488       goto do_ucompare;
11489
11490     do_ucompare:
11491       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11492           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11493           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11494         break;
11495
11496       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11497                                 VAR_INIT_STATUS_INITIALIZED);
11498       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11499                                 VAR_INIT_STATUS_INITIALIZED);
11500
11501       if (op0 == 0 || op1 == 0)
11502         break;
11503
11504       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11505         {
11506           HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11507           add_loc_descr (&op0, int_loc_descriptor (mask));
11508           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11509           if (CONST_INT_P (XEXP (rtl, 1)))
11510             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11511           else
11512             {
11513               add_loc_descr (&op1, int_loc_descriptor (mask));
11514               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11515             }
11516         }
11517       else
11518         {
11519           HOST_WIDE_INT bias = 1;
11520           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11521           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11522           if (CONST_INT_P (XEXP (rtl, 1)))
11523             op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11524                                       + INTVAL (XEXP (rtl, 1)));
11525           else
11526             add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11527         }
11528       goto do_compare;
11529
11530     case SMIN:
11531     case SMAX:
11532     case UMIN:
11533     case UMAX:
11534       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11535           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11536           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11537         break;
11538
11539       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11540                                 VAR_INIT_STATUS_INITIALIZED);
11541       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11542                                 VAR_INIT_STATUS_INITIALIZED);
11543
11544       if (op0 == 0 || op1 == 0)
11545         break;
11546
11547       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11548       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11549       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11550       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11551         {
11552           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11553             {
11554               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11555               add_loc_descr (&op0, int_loc_descriptor (mask));
11556               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11557               add_loc_descr (&op1, int_loc_descriptor (mask));
11558               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11559             }
11560           else
11561             {
11562               HOST_WIDE_INT bias = 1;
11563               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11564               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11565               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11566             }
11567         }
11568       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11569         {
11570           int shift = DWARF2_ADDR_SIZE
11571                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11572           shift *= BITS_PER_UNIT;
11573           add_loc_descr (&op0, int_loc_descriptor (shift));
11574           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11575           add_loc_descr (&op1, int_loc_descriptor (shift));
11576           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11577         }
11578
11579       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11580         op = DW_OP_lt;
11581       else
11582         op = DW_OP_gt;
11583       mem_loc_result = op0;
11584       add_loc_descr (&mem_loc_result, op1);
11585       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11586       {
11587         dw_loc_descr_ref bra_node, drop_node;
11588
11589         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11590         add_loc_descr (&mem_loc_result, bra_node);
11591         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11592         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11593         add_loc_descr (&mem_loc_result, drop_node);
11594         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11595         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11596       }
11597       break;
11598
11599     case ZERO_EXTRACT:
11600     case SIGN_EXTRACT:
11601       if (CONST_INT_P (XEXP (rtl, 1))
11602           && CONST_INT_P (XEXP (rtl, 2))
11603           && ((unsigned) INTVAL (XEXP (rtl, 1))
11604               + (unsigned) INTVAL (XEXP (rtl, 2))
11605               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
11606           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
11607           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11608         {
11609           int shift, size;
11610           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11611                                     VAR_INIT_STATUS_INITIALIZED);
11612           if (op0 == 0)
11613             break;
11614           if (GET_CODE (rtl) == SIGN_EXTRACT)
11615             op = DW_OP_shra;
11616           else
11617             op = DW_OP_shr;
11618           mem_loc_result = op0;
11619           size = INTVAL (XEXP (rtl, 1));
11620           shift = INTVAL (XEXP (rtl, 2));
11621           if (BITS_BIG_ENDIAN)
11622             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11623                     - shift - size;
11624           add_loc_descr (&mem_loc_result,
11625                          int_loc_descriptor (DWARF2_ADDR_SIZE - shift - size));
11626           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11627           add_loc_descr (&mem_loc_result,
11628                          int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11629           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11630         }
11631       break;
11632
11633     case COMPARE:
11634     case IF_THEN_ELSE:
11635     case ROTATE:
11636     case ROTATERT:
11637     case TRUNCATE:
11638       /* In theory, we could implement the above.  */
11639       /* DWARF cannot represent the unsigned compare operations
11640          natively.  */
11641     case SS_MULT:
11642     case US_MULT:
11643     case SS_DIV:
11644     case US_DIV:
11645     case UDIV:
11646     case UMOD:
11647     case UNORDERED:
11648     case ORDERED:
11649     case UNEQ:
11650     case UNGE:
11651     case UNLE:
11652     case UNLT:
11653     case LTGT:
11654     case FLOAT_EXTEND:
11655     case FLOAT_TRUNCATE:
11656     case FLOAT:
11657     case UNSIGNED_FLOAT:
11658     case FIX:
11659     case UNSIGNED_FIX:
11660     case FRACT_CONVERT:
11661     case UNSIGNED_FRACT_CONVERT:
11662     case SAT_FRACT:
11663     case UNSIGNED_SAT_FRACT:
11664     case SQRT:
11665     case BSWAP:
11666     case FFS:
11667     case CLZ:
11668     case CTZ:
11669     case POPCOUNT:
11670     case PARITY:
11671     case ASM_OPERANDS:
11672     case UNSPEC:
11673       /* If delegitimize_address couldn't do anything with the UNSPEC, we
11674          can't express it in the debug info.  This can happen e.g. with some
11675          TLS UNSPECs.  */
11676       break;
11677
11678     case CONST_STRING:
11679       /* These can't easily be tracked, see PR41404.  */
11680       break;
11681
11682     default:
11683 #ifdef ENABLE_CHECKING
11684       print_rtl (stderr, rtl);
11685       gcc_unreachable ();
11686 #else
11687       break;
11688 #endif
11689     }
11690
11691   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11692     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11693
11694   return mem_loc_result;
11695 }
11696
11697 /* Return a descriptor that describes the concatenation of two locations.
11698    This is typically a complex variable.  */
11699
11700 static dw_loc_descr_ref
11701 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
11702 {
11703   dw_loc_descr_ref cc_loc_result = NULL;
11704   dw_loc_descr_ref x0_ref
11705     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11706   dw_loc_descr_ref x1_ref
11707     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11708
11709   if (x0_ref == 0 || x1_ref == 0)
11710     return 0;
11711
11712   cc_loc_result = x0_ref;
11713   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
11714
11715   add_loc_descr (&cc_loc_result, x1_ref);
11716   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
11717
11718   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11719     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11720
11721   return cc_loc_result;
11722 }
11723
11724 /* Return a descriptor that describes the concatenation of N
11725    locations.  */
11726
11727 static dw_loc_descr_ref
11728 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
11729 {
11730   unsigned int i;
11731   dw_loc_descr_ref cc_loc_result = NULL;
11732   unsigned int n = XVECLEN (concatn, 0);
11733
11734   for (i = 0; i < n; ++i)
11735     {
11736       dw_loc_descr_ref ref;
11737       rtx x = XVECEXP (concatn, 0, i);
11738
11739       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11740       if (ref == NULL)
11741         return NULL;
11742
11743       add_loc_descr (&cc_loc_result, ref);
11744       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
11745     }
11746
11747   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11748     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11749
11750   return cc_loc_result;
11751 }
11752
11753 /* Output a proper Dwarf location descriptor for a variable or parameter
11754    which is either allocated in a register or in a memory location.  For a
11755    register, we just generate an OP_REG and the register number.  For a
11756    memory location we provide a Dwarf postfix expression describing how to
11757    generate the (dynamic) address of the object onto the address stack.
11758
11759    MODE is mode of the decl if this loc_descriptor is going to be used in
11760    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
11761    allowed, VOIDmode otherwise.
11762
11763    If we don't know how to describe it, return 0.  */
11764
11765 static dw_loc_descr_ref
11766 loc_descriptor (rtx rtl, enum machine_mode mode,
11767                 enum var_init_status initialized)
11768 {
11769   dw_loc_descr_ref loc_result = NULL;
11770
11771   switch (GET_CODE (rtl))
11772     {
11773     case SUBREG:
11774       /* The case of a subreg may arise when we have a local (register)
11775          variable or a formal (register) parameter which doesn't quite fill
11776          up an entire register.  For now, just assume that it is
11777          legitimate to make the Dwarf info refer to the whole register which
11778          contains the given subreg.  */
11779       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
11780       break;
11781
11782     case REG:
11783       loc_result = reg_loc_descriptor (rtl, initialized);
11784       break;
11785
11786     case SIGN_EXTEND:
11787     case ZERO_EXTEND:
11788       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
11789       break;
11790
11791     case MEM:
11792       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11793                                        initialized);
11794       if (loc_result == NULL)
11795         loc_result = tls_mem_loc_descriptor (rtl);
11796       break;
11797
11798     case CONCAT:
11799       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
11800                                           initialized);
11801       break;
11802
11803     case CONCATN:
11804       loc_result = concatn_loc_descriptor (rtl, initialized);
11805       break;
11806
11807     case VAR_LOCATION:
11808       /* Single part.  */
11809       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
11810         {
11811           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), mode,
11812                                        initialized);
11813           break;
11814         }
11815
11816       rtl = XEXP (rtl, 1);
11817       /* FALLTHRU */
11818
11819     case PARALLEL:
11820       {
11821         rtvec par_elems = XVEC (rtl, 0);
11822         int num_elem = GET_NUM_ELEM (par_elems);
11823         enum machine_mode mode;
11824         int i;
11825
11826         /* Create the first one, so we have something to add to.  */
11827         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
11828                                      VOIDmode, initialized);
11829         if (loc_result == NULL)
11830           return NULL;
11831         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
11832         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11833         for (i = 1; i < num_elem; i++)
11834           {
11835             dw_loc_descr_ref temp;
11836
11837             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
11838                                    VOIDmode, initialized);
11839             if (temp == NULL)
11840               return NULL;
11841             add_loc_descr (&loc_result, temp);
11842             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
11843             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11844           }
11845       }
11846       break;
11847
11848     case CONST_INT:
11849       if (mode != VOIDmode && mode != BLKmode)
11850         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
11851                                                     INTVAL (rtl));
11852       break;
11853
11854     case CONST_DOUBLE:
11855       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
11856         {
11857           /* Note that a CONST_DOUBLE rtx could represent either an integer
11858              or a floating-point constant.  A CONST_DOUBLE is used whenever
11859              the constant requires more than one word in order to be
11860              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11861           if (GET_MODE (rtl) != VOIDmode)
11862             mode = GET_MODE (rtl);
11863
11864           loc_result = new_loc_descr (DW_OP_implicit_value,
11865                                       GET_MODE_SIZE (mode), 0);
11866           if (SCALAR_FLOAT_MODE_P (mode))
11867             {
11868               unsigned int length = GET_MODE_SIZE (mode);
11869               unsigned char *array = GGC_NEWVEC (unsigned char, length);
11870
11871               insert_float (rtl, array);
11872               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11873               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11874               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11875               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11876             }
11877           else
11878             {
11879               loc_result->dw_loc_oprnd2.val_class = dw_val_class_long_long;
11880               loc_result->dw_loc_oprnd2.v.val_long_long = rtl;
11881             }
11882         }
11883       break;
11884
11885     case CONST_VECTOR:
11886       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
11887         {
11888           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
11889           unsigned int length = CONST_VECTOR_NUNITS (rtl);
11890           unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11891           unsigned int i;
11892           unsigned char *p;
11893
11894           mode = GET_MODE (rtl);
11895           switch (GET_MODE_CLASS (mode))
11896             {
11897             case MODE_VECTOR_INT:
11898               for (i = 0, p = array; i < length; i++, p += elt_size)
11899                 {
11900                   rtx elt = CONST_VECTOR_ELT (rtl, i);
11901                   HOST_WIDE_INT lo, hi;
11902
11903                   switch (GET_CODE (elt))
11904                     {
11905                     case CONST_INT:
11906                       lo = INTVAL (elt);
11907                       hi = -(lo < 0);
11908                       break;
11909
11910                     case CONST_DOUBLE:
11911                       lo = CONST_DOUBLE_LOW (elt);
11912                       hi = CONST_DOUBLE_HIGH (elt);
11913                       break;
11914
11915                     default:
11916                       gcc_unreachable ();
11917                     }
11918
11919                   if (elt_size <= sizeof (HOST_WIDE_INT))
11920                     insert_int (lo, elt_size, p);
11921                   else
11922                     {
11923                       unsigned char *p0 = p;
11924                       unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11925
11926                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11927                       if (WORDS_BIG_ENDIAN)
11928                         {
11929                           p0 = p1;
11930                           p1 = p;
11931                         }
11932                       insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11933                       insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11934                     }
11935                 }
11936               break;
11937
11938             case MODE_VECTOR_FLOAT:
11939               for (i = 0, p = array; i < length; i++, p += elt_size)
11940                 {
11941                   rtx elt = CONST_VECTOR_ELT (rtl, i);
11942                   insert_float (elt, p);
11943                 }
11944               break;
11945
11946             default:
11947               gcc_unreachable ();
11948             }
11949
11950           loc_result = new_loc_descr (DW_OP_implicit_value,
11951                                       length * elt_size, 0);
11952           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11953           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
11954           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
11955           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11956         }
11957       break;
11958
11959     case CONST:
11960       if (mode == VOIDmode
11961           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
11962           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
11963           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
11964         {
11965           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
11966           break;
11967         }
11968       /* FALLTHROUGH */
11969     case SYMBOL_REF:
11970       if (GET_CODE (rtl) == SYMBOL_REF
11971           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11972         break;
11973     case LABEL_REF:
11974       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
11975           && (dwarf_version >= 4 || !dwarf_strict))
11976         {
11977           loc_result = new_loc_descr (DW_OP_implicit_value,
11978                                       DWARF2_ADDR_SIZE, 0);
11979           loc_result->dw_loc_oprnd2.val_class = dw_val_class_addr;
11980           loc_result->dw_loc_oprnd2.v.val_addr = rtl;
11981           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11982         }
11983       break;
11984
11985     default:
11986       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
11987           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
11988           && (dwarf_version >= 4 || !dwarf_strict))
11989         {
11990           /* Value expression.  */
11991           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
11992           if (loc_result)
11993             {
11994               add_loc_descr (&loc_result,
11995                              new_loc_descr (DW_OP_stack_value, 0, 0));
11996               add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11997             }
11998         }
11999       break;
12000     }
12001
12002   return loc_result;
12003 }
12004
12005 /* We need to figure out what section we should use as the base for the
12006    address ranges where a given location is valid.
12007    1. If this particular DECL has a section associated with it, use that.
12008    2. If this function has a section associated with it, use that.
12009    3. Otherwise, use the text section.
12010    XXX: If you split a variable across multiple sections, we won't notice.  */
12011
12012 static const char *
12013 secname_for_decl (const_tree decl)
12014 {
12015   const char *secname;
12016
12017   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12018     {
12019       tree sectree = DECL_SECTION_NAME (decl);
12020       secname = TREE_STRING_POINTER (sectree);
12021     }
12022   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12023     {
12024       tree sectree = DECL_SECTION_NAME (current_function_decl);
12025       secname = TREE_STRING_POINTER (sectree);
12026     }
12027   else if (cfun && in_cold_section_p)
12028     secname = crtl->subsections.cold_section_label;
12029   else
12030     secname = text_section_label;
12031
12032   return secname;
12033 }
12034
12035 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12036
12037 static bool
12038 decl_by_reference_p (tree decl)
12039 {
12040   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12041            || TREE_CODE (decl) == VAR_DECL)
12042           && DECL_BY_REFERENCE (decl));
12043 }
12044
12045
12046 /* Dereference a location expression LOC if DECL is passed by invisible
12047    reference.  */
12048
12049 static dw_loc_descr_ref
12050 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12051 {
12052   HOST_WIDE_INT size;
12053   enum dwarf_location_atom op;
12054
12055   if (loc == NULL)
12056     return NULL;
12057
12058   if (!decl_by_reference_p (decl))
12059     return loc;
12060
12061   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12062      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
12063      location expression is considered to be address of a memory location,
12064      rather than the register itself.  */
12065   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12066        || loc->dw_loc_opc == DW_OP_regx)
12067       && (loc->dw_loc_next == NULL
12068           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12069               && loc->dw_loc_next->dw_loc_next == NULL)))
12070     {
12071       if (loc->dw_loc_opc == DW_OP_regx)
12072         {
12073           loc->dw_loc_opc = DW_OP_bregx;
12074           loc->dw_loc_oprnd2.v.val_int = 0;
12075         }
12076       else
12077         {
12078           loc->dw_loc_opc
12079             = (enum dwarf_location_atom)
12080               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12081           loc->dw_loc_oprnd1.v.val_int = 0;
12082         }
12083       return loc;
12084     }
12085
12086   size = int_size_in_bytes (TREE_TYPE (decl));
12087   if (size > DWARF2_ADDR_SIZE || size == -1)
12088     return 0;
12089   else if (size == DWARF2_ADDR_SIZE)
12090     op = DW_OP_deref;
12091   else
12092     op = DW_OP_deref_size;
12093   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12094   return loc;
12095 }
12096
12097 /* Return single element location list containing loc descr REF.  */
12098
12099 static dw_loc_list_ref
12100 single_element_loc_list (dw_loc_descr_ref ref)
12101 {
12102   return new_loc_list (ref, NULL, NULL, NULL, 0);
12103 }
12104
12105 /* Return dwarf representation of location list representing for
12106    LOC_LIST of DECL.  */
12107
12108 static dw_loc_list_ref
12109 dw_loc_list (var_loc_list * loc_list, tree decl, bool toplevel)
12110 {
12111   const char *endname, *secname;
12112   dw_loc_list_ref list;
12113   rtx varloc;
12114   enum var_init_status initialized;
12115   struct var_loc_node *node;
12116   dw_loc_descr_ref descr;
12117   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12118
12119   bool by_reference = decl_by_reference_p (decl);
12120
12121   /* Now that we know what section we are using for a base,
12122      actually construct the list of locations.
12123      The first location information is what is passed to the
12124      function that creates the location list, and the remaining
12125      locations just get added on to that list.
12126      Note that we only know the start address for a location
12127      (IE location changes), so to build the range, we use
12128      the range [current location start, next location start].
12129      This means we have to special case the last node, and generate
12130      a range of [last location start, end of function label].  */
12131
12132   node = loc_list->first;
12133   varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12134   secname = secname_for_decl (decl);
12135
12136   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12137     initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12138   else
12139     initialized = VAR_INIT_STATUS_INITIALIZED;
12140
12141   if (!toplevel || by_reference)
12142     {
12143       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12144       /* Single part.  */
12145       if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
12146         descr = loc_by_reference (mem_loc_descriptor (XEXP (XEXP (varloc, 1), 0),
12147                                                       TYPE_MODE (TREE_TYPE (decl)),
12148                                                       initialized),
12149                                   decl);
12150       else
12151         descr = NULL;
12152     }
12153   else
12154     descr = loc_descriptor (varloc, DECL_MODE (decl), initialized);
12155
12156   if (loc_list && loc_list->first != loc_list->last)
12157     list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12158   else
12159     return single_element_loc_list (descr);
12160   node = node->next;
12161
12162   if (!node)
12163     return NULL;
12164
12165   for (; node->next; node = node->next)
12166     if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12167       {
12168         /* The variable has a location between NODE->LABEL and
12169            NODE->NEXT->LABEL.  */
12170         enum var_init_status initialized =
12171           NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12172         varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12173         if (!toplevel || by_reference)
12174           {
12175             gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12176             /* Single part.  */
12177             if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
12178               descr = mem_loc_descriptor (XEXP (XEXP (varloc, 1), 0),
12179                                           TYPE_MODE (TREE_TYPE (decl)), initialized);
12180             else
12181               descr = NULL;
12182             descr = loc_by_reference (descr, decl);
12183           }
12184         else
12185           descr = loc_descriptor (varloc, DECL_MODE (decl), initialized);
12186         add_loc_descr_to_loc_list (&list, descr,
12187                                    node->label, node->next->label, secname);
12188       }
12189
12190   /* If the variable has a location at the last label
12191      it keeps its location until the end of function.  */
12192   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12193     {
12194       enum var_init_status initialized =
12195         NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12196
12197       if (!current_function_decl)
12198         endname = text_end_label;
12199       else
12200         {
12201           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12202                                        current_function_funcdef_no);
12203           endname = ggc_strdup (label_id);
12204         }
12205
12206       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12207       if (!toplevel || by_reference)
12208         {
12209           gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12210           /* Single part.  */
12211           if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
12212             descr = mem_loc_descriptor (XEXP (XEXP (varloc, 1), 0),
12213                                         TYPE_MODE (TREE_TYPE (decl)), initialized);
12214           else
12215             descr = NULL;
12216           descr = loc_by_reference (descr, decl);
12217         }
12218       else
12219         descr = loc_descriptor (varloc, DECL_MODE (decl), initialized);
12220       add_loc_descr_to_loc_list (&list, descr, node->label, endname, secname);
12221     }
12222   return list;
12223 }
12224
12225 /* Return if the loc_list has only single element and thus can be represented
12226    as location description.   */
12227
12228 static bool
12229 single_element_loc_list_p (dw_loc_list_ref list)
12230 {
12231   return (!list->dw_loc_next && !list->begin && !list->end);
12232 }
12233
12234 /* To each location in list LIST add loc descr REF.  */
12235
12236 static void
12237 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
12238 {
12239   dw_loc_descr_ref copy;
12240   add_loc_descr (&list->expr, ref);
12241   list = list->dw_loc_next;
12242   while (list)
12243     {
12244       copy = GGC_CNEW (dw_loc_descr_node);
12245       memcpy (copy, ref, sizeof (dw_loc_descr_node));
12246       add_loc_descr (&list->expr, copy);
12247       while (copy->dw_loc_next)
12248         {
12249           dw_loc_descr_ref new_copy = GGC_CNEW (dw_loc_descr_node);
12250           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
12251           copy->dw_loc_next = new_copy;
12252           copy = new_copy;
12253         }
12254       list = list->dw_loc_next;
12255     }
12256 }
12257
12258 /* Given two lists RET and LIST
12259    produce location list that is result of adding expression in LIST
12260    to expression in RET on each possition in program.
12261    Might be destructive on both RET and LIST.
12262
12263    TODO: We handle only simple cases of RET or LIST having at most one
12264    element. General case would inolve sorting the lists in program order
12265    and merging them that will need some additional work.  
12266    Adding that will improve quality of debug info especially for SRA-ed
12267    structures.  */
12268
12269 static void
12270 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
12271 {
12272   if (!list)
12273     return;
12274   if (!*ret)
12275     {
12276       *ret = list;
12277       return;
12278     }
12279   if (!list->dw_loc_next)
12280     {
12281       add_loc_descr_to_each (*ret, list->expr);
12282       return;
12283     }
12284   if (!(*ret)->dw_loc_next)
12285     {
12286       add_loc_descr_to_each (list, (*ret)->expr);
12287       *ret = list;
12288       return;
12289     }
12290   expansion_failed (NULL_TREE, NULL_RTX,
12291                     "Don't know how to merge two non-trivial"
12292                     " location lists.\n");
12293   *ret = NULL;
12294   return;
12295 }
12296
12297 /* LOC is constant expression.  Try a luck, look it up in constant
12298    pool and return its loc_descr of its address.  */
12299
12300 static dw_loc_descr_ref
12301 cst_pool_loc_descr (tree loc)
12302 {
12303   /* Get an RTL for this, if something has been emitted.  */
12304   rtx rtl = lookup_constant_def (loc);
12305   enum machine_mode mode;
12306
12307   if (!rtl || !MEM_P (rtl))
12308     {
12309       gcc_assert (!rtl);
12310       return 0;
12311     }
12312   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
12313
12314   /* TODO: We might get more coverage if we was actually delaying expansion
12315      of all expressions till end of compilation when constant pools are fully
12316      populated.  */
12317   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
12318     {
12319       expansion_failed (loc, NULL_RTX,
12320                         "CST value in contant pool but not marked.");
12321       return 0;
12322     }
12323   mode = GET_MODE (rtl);
12324   rtl = XEXP (rtl, 0);
12325   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12326 }
12327
12328 /* Return dw_loc_list representing address of addr_expr LOC
12329    by looking for innder INDIRECT_REF expression and turing it
12330    into simple arithmetics.  */
12331
12332 static dw_loc_list_ref
12333 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
12334 {
12335   tree obj, offset;
12336   HOST_WIDE_INT bitsize, bitpos, bytepos;
12337   enum machine_mode mode;
12338   int volatilep;
12339   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
12340   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12341
12342   obj = get_inner_reference (TREE_OPERAND (loc, 0),
12343                              &bitsize, &bitpos, &offset, &mode,
12344                              &unsignedp, &volatilep, false);
12345   STRIP_NOPS (obj);
12346   if (bitpos % BITS_PER_UNIT)
12347     {
12348       expansion_failed (loc, NULL_RTX, "bitfield access");
12349       return 0;
12350     }
12351   if (!INDIRECT_REF_P (obj))
12352     {
12353       expansion_failed (obj,
12354                         NULL_RTX, "no indirect ref in inner refrence");
12355       return 0;
12356     }
12357   if (!offset && !bitpos)
12358     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
12359   else if (toplev
12360            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
12361            && (dwarf_version >= 4 || !dwarf_strict))
12362     {
12363       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
12364       if (!list_ret)
12365         return 0;
12366       if (offset)
12367         {
12368           /* Variable offset.  */
12369           list_ret1 = loc_list_from_tree (offset, 0);
12370           if (list_ret1 == 0)
12371             return 0;
12372           add_loc_list (&list_ret, list_ret1);
12373           if (!list_ret)
12374             return 0;
12375           add_loc_descr_to_each (list_ret,
12376                                  new_loc_descr (DW_OP_plus, 0, 0));
12377         }
12378       bytepos = bitpos / BITS_PER_UNIT;
12379       if (bytepos > 0)
12380         add_loc_descr_to_each (list_ret,
12381                                new_loc_descr (DW_OP_plus_uconst,
12382                                               bytepos, 0));
12383       else if (bytepos < 0)
12384         loc_list_plus_const (list_ret, bytepos);
12385       add_loc_descr_to_each (list_ret,
12386                              new_loc_descr (DW_OP_stack_value, 0, 0));
12387       add_loc_descr_to_each (list_ret,
12388                              new_loc_descr (DW_OP_piece,
12389                                             int_size_in_bytes (TREE_TYPE
12390                                                                (loc)),
12391                                             0));
12392     }
12393   return list_ret;
12394 }
12395
12396
12397 /* Generate Dwarf location list representing LOC.
12398    If WANT_ADDRESS is false, expression computing LOC will be computed
12399    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
12400    if WANT_ADDRESS is 2, expression computing address useable in location
12401      will be returned (i.e. DW_OP_reg can be used
12402      to refer to register values) 
12403    TODO: Dwarf4 adds types to the stack machine that ought to be used here
12404    DW_OP_stack_value will help in cases where we fail to find address of the
12405    expression.
12406  */
12407
12408 static dw_loc_list_ref
12409 loc_list_from_tree (tree loc, int want_address)
12410 {
12411   dw_loc_descr_ref ret = NULL, ret1 = NULL;
12412   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12413   int have_address = 0;
12414   enum dwarf_location_atom op;
12415
12416   /* ??? Most of the time we do not take proper care for sign/zero
12417      extending the values properly.  Hopefully this won't be a real
12418      problem...  */
12419
12420   switch (TREE_CODE (loc))
12421     {
12422     case ERROR_MARK:
12423       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
12424       return 0;
12425
12426     case PLACEHOLDER_EXPR:
12427       /* This case involves extracting fields from an object to determine the
12428          position of other fields.  We don't try to encode this here.  The
12429          only user of this is Ada, which encodes the needed information using
12430          the names of types.  */
12431       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
12432       return 0;
12433
12434     case CALL_EXPR:
12435       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
12436       /* There are no opcodes for these operations.  */
12437       return 0;
12438
12439     case PREINCREMENT_EXPR:
12440     case PREDECREMENT_EXPR:
12441     case POSTINCREMENT_EXPR:
12442     case POSTDECREMENT_EXPR:
12443       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
12444       /* There are no opcodes for these operations.  */
12445       return 0;
12446
12447     case ADDR_EXPR:
12448       /* If we already want an address, see if there is INDIRECT_REF inside
12449          e.g. for &this->field.  */
12450       if (want_address)
12451         {
12452           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
12453                        (loc, want_address == 2);
12454           if (list_ret)
12455             have_address = 1;
12456           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
12457                    && (ret = cst_pool_loc_descr (loc)))
12458             have_address = 1;
12459         }
12460         /* Otherwise, process the argument and look for the address.  */
12461       if (!list_ret && !ret)
12462         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
12463       else
12464         {
12465           if (want_address)
12466             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
12467           return NULL;
12468         }
12469       break;
12470
12471     case VAR_DECL:
12472       if (DECL_THREAD_LOCAL_P (loc))
12473         {
12474           rtx rtl;
12475           enum dwarf_location_atom first_op;
12476           enum dwarf_location_atom second_op;
12477           bool dtprel = false;
12478
12479           if (targetm.have_tls)
12480             {
12481               /* If this is not defined, we have no way to emit the
12482                  data.  */
12483               if (!targetm.asm_out.output_dwarf_dtprel)
12484                 return 0;
12485
12486                /* The way DW_OP_GNU_push_tls_address is specified, we
12487                   can only look up addresses of objects in the current
12488                   module.  */
12489               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
12490                 return 0;
12491               first_op = DW_OP_addr;
12492               dtprel = true;
12493               second_op = DW_OP_GNU_push_tls_address;
12494             }
12495           else
12496             {
12497               if (!targetm.emutls.debug_form_tls_address
12498                   || !(dwarf_version >= 3 || !dwarf_strict))
12499                 return 0;
12500               loc = emutls_decl (loc);
12501               first_op = DW_OP_addr;
12502               second_op = DW_OP_form_tls_address;
12503             }
12504
12505           rtl = rtl_for_decl_location (loc);
12506           if (rtl == NULL_RTX)
12507             return 0;
12508
12509           if (!MEM_P (rtl))
12510             return 0;
12511           rtl = XEXP (rtl, 0);
12512           if (! CONSTANT_P (rtl))
12513             return 0;
12514
12515           ret = new_loc_descr (first_op, 0, 0);
12516           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12517           ret->dw_loc_oprnd1.v.val_addr = rtl;
12518           ret->dtprel = dtprel;
12519
12520           ret1 = new_loc_descr (second_op, 0, 0);
12521           add_loc_descr (&ret, ret1);
12522
12523           have_address = 1;
12524           break;
12525         }
12526       /* FALLTHRU */
12527
12528     case PARM_DECL:
12529       if (DECL_HAS_VALUE_EXPR_P (loc))
12530         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
12531                                    want_address);
12532       /* FALLTHRU */
12533
12534     case RESULT_DECL:
12535     case FUNCTION_DECL:
12536       {
12537         rtx rtl = rtl_for_decl_location (loc);
12538         var_loc_list *loc_list = lookup_decl_loc (loc);
12539
12540         if (loc_list && loc_list->first
12541             && (list_ret = dw_loc_list (loc_list, loc, want_address == 2)))
12542           have_address = 1;
12543         else if (rtl == NULL_RTX)
12544           {
12545             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
12546             return 0;
12547           }
12548         else if (CONST_INT_P (rtl))
12549           {
12550             HOST_WIDE_INT val = INTVAL (rtl);
12551             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
12552               val &= GET_MODE_MASK (DECL_MODE (loc));
12553             ret = int_loc_descriptor (val);
12554           }
12555         else if (GET_CODE (rtl) == CONST_STRING)
12556           {
12557             expansion_failed (loc, NULL_RTX, "CONST_STRING");
12558             return 0;
12559           }
12560         else if (CONSTANT_P (rtl))
12561           {
12562             ret = new_loc_descr (DW_OP_addr, 0, 0);
12563             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12564             ret->dw_loc_oprnd1.v.val_addr = rtl;
12565           }
12566         else
12567           {
12568             enum machine_mode mode;
12569
12570             /* Certain constructs can only be represented at top-level.  */
12571             if (want_address == 2)
12572               {
12573                 ret = loc_descriptor (rtl, VOIDmode,
12574                                       VAR_INIT_STATUS_INITIALIZED);
12575                 have_address = 1;
12576               }
12577             else
12578               {
12579                 mode = GET_MODE (rtl);
12580                 if (MEM_P (rtl))
12581                   {
12582                     rtl = XEXP (rtl, 0);
12583                     have_address = 1;
12584                   }
12585                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12586               }
12587             if (!ret)
12588               expansion_failed (loc, rtl,
12589                                 "failed to produce loc descriptor for rtl");
12590           }
12591       }
12592       break;
12593
12594     case INDIRECT_REF:
12595     case ALIGN_INDIRECT_REF:
12596     case MISALIGNED_INDIRECT_REF:
12597       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12598       have_address = 1;
12599       break;
12600
12601     case COMPOUND_EXPR:
12602       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
12603
12604     CASE_CONVERT:
12605     case VIEW_CONVERT_EXPR:
12606     case SAVE_EXPR:
12607     case MODIFY_EXPR:
12608       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
12609
12610     case COMPONENT_REF:
12611     case BIT_FIELD_REF:
12612     case ARRAY_REF:
12613     case ARRAY_RANGE_REF:
12614     case REALPART_EXPR:
12615     case IMAGPART_EXPR:
12616       {
12617         tree obj, offset;
12618         HOST_WIDE_INT bitsize, bitpos, bytepos;
12619         enum machine_mode mode;
12620         int volatilep;
12621         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
12622
12623         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
12624                                    &unsignedp, &volatilep, false);
12625
12626         gcc_assert (obj != loc);
12627
12628         list_ret = loc_list_from_tree (obj,
12629                                        want_address == 2
12630                                        && !bitpos && !offset ? 2 : 1);
12631         /* TODO: We can extract value of the small expression via shifting even
12632            for nonzero bitpos.  */
12633         if (list_ret == 0)
12634           return 0;
12635         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
12636           {
12637             expansion_failed (loc, NULL_RTX,
12638                               "bitfield access");
12639             return 0;
12640           }
12641
12642         if (offset != NULL_TREE)
12643           {
12644             /* Variable offset.  */
12645             list_ret1 = loc_list_from_tree (offset, 0);
12646             if (list_ret1 == 0)
12647               return 0;
12648             add_loc_list (&list_ret, list_ret1);
12649             if (!list_ret)
12650               return 0;
12651             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
12652           }
12653
12654         bytepos = bitpos / BITS_PER_UNIT;
12655         if (bytepos > 0)
12656           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
12657         else if (bytepos < 0)
12658           loc_list_plus_const (list_ret, bytepos); 
12659
12660         have_address = 1;
12661         break;
12662       }
12663
12664     case INTEGER_CST:
12665       if ((want_address || !host_integerp (loc, 0))
12666           && (ret = cst_pool_loc_descr (loc)))
12667         have_address = 1;
12668       else if (want_address == 2
12669                && host_integerp (loc, 0)
12670                && (ret = address_of_int_loc_descriptor
12671                            (int_size_in_bytes (TREE_TYPE (loc)),
12672                             tree_low_cst (loc, 0))))
12673         have_address = 1;
12674       else if (host_integerp (loc, 0))
12675         ret = int_loc_descriptor (tree_low_cst (loc, 0));
12676       else
12677         {
12678           expansion_failed (loc, NULL_RTX,
12679                             "Integer operand is not host integer");
12680           return 0;
12681         }
12682       break;
12683
12684     case CONSTRUCTOR:
12685     case REAL_CST:
12686     case STRING_CST:
12687     case COMPLEX_CST:
12688       if ((ret = cst_pool_loc_descr (loc)))
12689         have_address = 1;
12690       else
12691       /* We can construct small constants here using int_loc_descriptor.  */
12692         expansion_failed (loc, NULL_RTX,
12693                           "constructor or constant not in constant pool");
12694       break;
12695
12696     case TRUTH_AND_EXPR:
12697     case TRUTH_ANDIF_EXPR:
12698     case BIT_AND_EXPR:
12699       op = DW_OP_and;
12700       goto do_binop;
12701
12702     case TRUTH_XOR_EXPR:
12703     case BIT_XOR_EXPR:
12704       op = DW_OP_xor;
12705       goto do_binop;
12706
12707     case TRUTH_OR_EXPR:
12708     case TRUTH_ORIF_EXPR:
12709     case BIT_IOR_EXPR:
12710       op = DW_OP_or;
12711       goto do_binop;
12712
12713     case FLOOR_DIV_EXPR:
12714     case CEIL_DIV_EXPR:
12715     case ROUND_DIV_EXPR:
12716     case TRUNC_DIV_EXPR:
12717       op = DW_OP_div;
12718       goto do_binop;
12719
12720     case MINUS_EXPR:
12721       op = DW_OP_minus;
12722       goto do_binop;
12723
12724     case FLOOR_MOD_EXPR:
12725     case CEIL_MOD_EXPR:
12726     case ROUND_MOD_EXPR:
12727     case TRUNC_MOD_EXPR:
12728       op = DW_OP_mod;
12729       goto do_binop;
12730
12731     case MULT_EXPR:
12732       op = DW_OP_mul;
12733       goto do_binop;
12734
12735     case LSHIFT_EXPR:
12736       op = DW_OP_shl;
12737       goto do_binop;
12738
12739     case RSHIFT_EXPR:
12740       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
12741       goto do_binop;
12742
12743     case POINTER_PLUS_EXPR:
12744     case PLUS_EXPR:
12745       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
12746           && host_integerp (TREE_OPERAND (loc, 1), 0))
12747         {
12748           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12749           if (list_ret == 0)
12750             return 0;
12751
12752           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
12753           break;
12754         }
12755
12756       op = DW_OP_plus;
12757       goto do_binop;
12758
12759     case LE_EXPR:
12760       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12761         return 0;
12762
12763       op = DW_OP_le;
12764       goto do_binop;
12765
12766     case GE_EXPR:
12767       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12768         return 0;
12769
12770       op = DW_OP_ge;
12771       goto do_binop;
12772
12773     case LT_EXPR:
12774       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12775         return 0;
12776
12777       op = DW_OP_lt;
12778       goto do_binop;
12779
12780     case GT_EXPR:
12781       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12782         return 0;
12783
12784       op = DW_OP_gt;
12785       goto do_binop;
12786
12787     case EQ_EXPR:
12788       op = DW_OP_eq;
12789       goto do_binop;
12790
12791     case NE_EXPR:
12792       op = DW_OP_ne;
12793       goto do_binop;
12794
12795     do_binop:
12796       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12797       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
12798       if (list_ret == 0 || list_ret1 == 0)
12799         return 0;
12800
12801       add_loc_list (&list_ret, list_ret1);
12802       if (list_ret == 0)
12803         return 0;
12804       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
12805       break;
12806
12807     case TRUTH_NOT_EXPR:
12808     case BIT_NOT_EXPR:
12809       op = DW_OP_not;
12810       goto do_unop;
12811
12812     case ABS_EXPR:
12813       op = DW_OP_abs;
12814       goto do_unop;
12815
12816     case NEGATE_EXPR:
12817       op = DW_OP_neg;
12818       goto do_unop;
12819
12820     do_unop:
12821       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12822       if (list_ret == 0)
12823         return 0;
12824
12825       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
12826       break;
12827
12828     case MIN_EXPR:
12829     case MAX_EXPR:
12830       {
12831         const enum tree_code code =
12832           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
12833
12834         loc = build3 (COND_EXPR, TREE_TYPE (loc),
12835                       build2 (code, integer_type_node,
12836                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
12837                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
12838       }
12839
12840       /* ... fall through ...  */
12841
12842     case COND_EXPR:
12843       {
12844         dw_loc_descr_ref lhs
12845           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
12846         dw_loc_list_ref rhs
12847           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
12848         dw_loc_descr_ref bra_node, jump_node, tmp;
12849
12850         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12851         if (list_ret == 0 || lhs == 0 || rhs == 0)
12852           return 0;
12853
12854         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12855         add_loc_descr_to_each (list_ret, bra_node);
12856
12857         add_loc_list (&list_ret, rhs);
12858         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
12859         add_loc_descr_to_each (list_ret, jump_node);
12860
12861         add_loc_descr_to_each (list_ret, lhs);
12862         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12863         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
12864
12865         /* ??? Need a node to point the skip at.  Use a nop.  */
12866         tmp = new_loc_descr (DW_OP_nop, 0, 0);
12867         add_loc_descr_to_each (list_ret, tmp);
12868         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12869         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
12870       }
12871       break;
12872
12873     case FIX_TRUNC_EXPR:
12874       return 0;
12875
12876     default:
12877       /* Leave front-end specific codes as simply unknown.  This comes
12878          up, for instance, with the C STMT_EXPR.  */
12879       if ((unsigned int) TREE_CODE (loc)
12880           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
12881         {
12882           expansion_failed (loc, NULL_RTX,
12883                             "language specific tree node");
12884           return 0;
12885         }
12886
12887 #ifdef ENABLE_CHECKING
12888       /* Otherwise this is a generic code; we should just lists all of
12889          these explicitly.  We forgot one.  */
12890       gcc_unreachable ();
12891 #else
12892       /* In a release build, we want to degrade gracefully: better to
12893          generate incomplete debugging information than to crash.  */
12894       return NULL;
12895 #endif
12896     }
12897
12898   if (!ret && !list_ret)
12899     return 0;
12900
12901   if (want_address == 2 && !have_address
12902       && (dwarf_version >= 4 || !dwarf_strict))
12903     {
12904       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12905         {
12906           expansion_failed (loc, NULL_RTX,
12907                             "DWARF address size mismatch");
12908           return 0;
12909         }
12910       add_loc_descr_to_each (list_ret,
12911                              new_loc_descr (DW_OP_stack_value, 0, 0));
12912       add_loc_descr_to_each (list_ret,
12913                              new_loc_descr (DW_OP_piece,
12914                                             int_size_in_bytes (TREE_TYPE
12915                                                                (loc)),
12916                                             0));
12917       have_address = 1;
12918     }
12919   /* Show if we can't fill the request for an address.  */
12920   if (want_address && !have_address)
12921     {
12922       expansion_failed (loc, NULL_RTX,
12923                         "Want address and only have value");
12924       return 0;
12925     }
12926
12927   gcc_assert (!ret || !list_ret);
12928
12929   /* If we've got an address and don't want one, dereference.  */
12930   if (!want_address && have_address)
12931     {
12932       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12933
12934       if (size > DWARF2_ADDR_SIZE || size == -1)
12935         {
12936           expansion_failed (loc, NULL_RTX,
12937                             "DWARF address size mismatch");
12938           return 0;
12939         }
12940       else if (size == DWARF2_ADDR_SIZE)
12941         op = DW_OP_deref;
12942       else
12943         op = DW_OP_deref_size;
12944
12945       if (ret)
12946         add_loc_descr (&ret, new_loc_descr (op, size, 0));
12947       else
12948         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
12949     }
12950   if (ret)
12951     list_ret = single_element_loc_list (ret);
12952
12953   return list_ret;
12954 }
12955
12956 /* Same as above but return only single location expression.  */
12957 static dw_loc_descr_ref
12958 loc_descriptor_from_tree (tree loc, int want_address)
12959 {
12960   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
12961   if (!ret)
12962     return NULL;
12963   if (ret->dw_loc_next)
12964     {
12965       expansion_failed (loc, NULL_RTX,
12966                         "Location list where only loc descriptor needed");
12967       return NULL;
12968     }
12969   return ret->expr;
12970 }
12971
12972 /* Given a value, round it up to the lowest multiple of `boundary'
12973    which is not less than the value itself.  */
12974
12975 static inline HOST_WIDE_INT
12976 ceiling (HOST_WIDE_INT value, unsigned int boundary)
12977 {
12978   return (((value + boundary - 1) / boundary) * boundary);
12979 }
12980
12981 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
12982    pointer to the declared type for the relevant field variable, or return
12983    `integer_type_node' if the given node turns out to be an
12984    ERROR_MARK node.  */
12985
12986 static inline tree
12987 field_type (const_tree decl)
12988 {
12989   tree type;
12990
12991   if (TREE_CODE (decl) == ERROR_MARK)
12992     return integer_type_node;
12993
12994   type = DECL_BIT_FIELD_TYPE (decl);
12995   if (type == NULL_TREE)
12996     type = TREE_TYPE (decl);
12997
12998   return type;
12999 }
13000
13001 /* Given a pointer to a tree node, return the alignment in bits for
13002    it, or else return BITS_PER_WORD if the node actually turns out to
13003    be an ERROR_MARK node.  */
13004
13005 static inline unsigned
13006 simple_type_align_in_bits (const_tree type)
13007 {
13008   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13009 }
13010
13011 static inline unsigned
13012 simple_decl_align_in_bits (const_tree decl)
13013 {
13014   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13015 }
13016
13017 /* Return the result of rounding T up to ALIGN.  */
13018
13019 static inline HOST_WIDE_INT
13020 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
13021 {
13022   /* We must be careful if T is negative because HOST_WIDE_INT can be
13023      either "above" or "below" unsigned int as per the C promotion
13024      rules, depending on the host, thus making the signedness of the
13025      direct multiplication and division unpredictable.  */
13026   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
13027
13028   u += align - 1;
13029   u /= align;
13030   u *= align;
13031
13032   return (HOST_WIDE_INT) u;
13033 }
13034
13035 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13036    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13037    or return 0 if we are unable to determine what that offset is, either
13038    because the argument turns out to be a pointer to an ERROR_MARK node, or
13039    because the offset is actually variable.  (We can't handle the latter case
13040    just yet).  */
13041
13042 static HOST_WIDE_INT
13043 field_byte_offset (const_tree decl)
13044 {
13045   HOST_WIDE_INT object_offset_in_bits;
13046   HOST_WIDE_INT bitpos_int;
13047
13048   if (TREE_CODE (decl) == ERROR_MARK)
13049     return 0;
13050
13051   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13052
13053   /* We cannot yet cope with fields whose positions are variable, so
13054      for now, when we see such things, we simply return 0.  Someday, we may
13055      be able to handle such cases, but it will be damn difficult.  */
13056   if (! host_integerp (bit_position (decl), 0))
13057     return 0;
13058
13059   bitpos_int = int_bit_position (decl);
13060
13061 #ifdef PCC_BITFIELD_TYPE_MATTERS
13062   if (PCC_BITFIELD_TYPE_MATTERS)
13063     {
13064       tree type;
13065       tree field_size_tree;
13066       HOST_WIDE_INT deepest_bitpos;
13067       unsigned HOST_WIDE_INT field_size_in_bits;
13068       unsigned int type_align_in_bits;
13069       unsigned int decl_align_in_bits;
13070       unsigned HOST_WIDE_INT type_size_in_bits;
13071
13072       type = field_type (decl);
13073       type_size_in_bits = simple_type_size_in_bits (type);
13074       type_align_in_bits = simple_type_align_in_bits (type);
13075
13076       field_size_tree = DECL_SIZE (decl);
13077
13078       /* The size could be unspecified if there was an error, or for
13079          a flexible array member.  */
13080       if (!field_size_tree)
13081         field_size_tree = bitsize_zero_node;
13082
13083       /* If the size of the field is not constant, use the type size.  */
13084       if (host_integerp (field_size_tree, 1))
13085         field_size_in_bits = tree_low_cst (field_size_tree, 1);
13086       else
13087         field_size_in_bits = type_size_in_bits;
13088
13089       decl_align_in_bits = simple_decl_align_in_bits (decl);
13090
13091       /* The GCC front-end doesn't make any attempt to keep track of the
13092          starting bit offset (relative to the start of the containing
13093          structure type) of the hypothetical "containing object" for a
13094          bit-field.  Thus, when computing the byte offset value for the
13095          start of the "containing object" of a bit-field, we must deduce
13096          this information on our own. This can be rather tricky to do in
13097          some cases.  For example, handling the following structure type
13098          definition when compiling for an i386/i486 target (which only
13099          aligns long long's to 32-bit boundaries) can be very tricky:
13100
13101          struct S { int field1; long long field2:31; };
13102
13103          Fortunately, there is a simple rule-of-thumb which can be used
13104          in such cases.  When compiling for an i386/i486, GCC will
13105          allocate 8 bytes for the structure shown above.  It decides to
13106          do this based upon one simple rule for bit-field allocation.
13107          GCC allocates each "containing object" for each bit-field at
13108          the first (i.e. lowest addressed) legitimate alignment boundary
13109          (based upon the required minimum alignment for the declared
13110          type of the field) which it can possibly use, subject to the
13111          condition that there is still enough available space remaining
13112          in the containing object (when allocated at the selected point)
13113          to fully accommodate all of the bits of the bit-field itself.
13114
13115          This simple rule makes it obvious why GCC allocates 8 bytes for
13116          each object of the structure type shown above.  When looking
13117          for a place to allocate the "containing object" for `field2',
13118          the compiler simply tries to allocate a 64-bit "containing
13119          object" at each successive 32-bit boundary (starting at zero)
13120          until it finds a place to allocate that 64- bit field such that
13121          at least 31 contiguous (and previously unallocated) bits remain
13122          within that selected 64 bit field.  (As it turns out, for the
13123          example above, the compiler finds it is OK to allocate the
13124          "containing object" 64-bit field at bit-offset zero within the
13125          structure type.)
13126
13127          Here we attempt to work backwards from the limited set of facts
13128          we're given, and we try to deduce from those facts, where GCC
13129          must have believed that the containing object started (within
13130          the structure type). The value we deduce is then used (by the
13131          callers of this routine) to generate DW_AT_location and
13132          DW_AT_bit_offset attributes for fields (both bit-fields and, in
13133          the case of DW_AT_location, regular fields as well).  */
13134
13135       /* Figure out the bit-distance from the start of the structure to
13136          the "deepest" bit of the bit-field.  */
13137       deepest_bitpos = bitpos_int + field_size_in_bits;
13138
13139       /* This is the tricky part.  Use some fancy footwork to deduce
13140          where the lowest addressed bit of the containing object must
13141          be.  */
13142       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13143
13144       /* Round up to type_align by default.  This works best for
13145          bitfields.  */
13146       object_offset_in_bits
13147         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13148
13149       if (object_offset_in_bits > bitpos_int)
13150         {
13151           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13152
13153           /* Round up to decl_align instead.  */
13154           object_offset_in_bits
13155             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13156         }
13157     }
13158   else
13159 #endif
13160     object_offset_in_bits = bitpos_int;
13161
13162   return object_offset_in_bits / BITS_PER_UNIT;
13163 }
13164 \f
13165 /* The following routines define various Dwarf attributes and any data
13166    associated with them.  */
13167
13168 /* Add a location description attribute value to a DIE.
13169
13170    This emits location attributes suitable for whole variables and
13171    whole parameters.  Note that the location attributes for struct fields are
13172    generated by the routine `data_member_location_attribute' below.  */
13173
13174 static inline void
13175 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
13176                              dw_loc_list_ref descr)
13177 {
13178   if (descr == 0)
13179     return;
13180   if (single_element_loc_list_p (descr))
13181     add_AT_loc (die, attr_kind, descr->expr);
13182   else
13183     add_AT_loc_list (die, attr_kind, descr);
13184 }
13185
13186 /* Attach the specialized form of location attribute used for data members of
13187    struct and union types.  In the special case of a FIELD_DECL node which
13188    represents a bit-field, the "offset" part of this special location
13189    descriptor must indicate the distance in bytes from the lowest-addressed
13190    byte of the containing struct or union type to the lowest-addressed byte of
13191    the "containing object" for the bit-field.  (See the `field_byte_offset'
13192    function above).
13193
13194    For any given bit-field, the "containing object" is a hypothetical object
13195    (of some integral or enum type) within which the given bit-field lives.  The
13196    type of this hypothetical "containing object" is always the same as the
13197    declared type of the individual bit-field itself (for GCC anyway... the
13198    DWARF spec doesn't actually mandate this).  Note that it is the size (in
13199    bytes) of the hypothetical "containing object" which will be given in the
13200    DW_AT_byte_size attribute for this bit-field.  (See the
13201    `byte_size_attribute' function below.)  It is also used when calculating the
13202    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
13203    function below.)  */
13204
13205 static void
13206 add_data_member_location_attribute (dw_die_ref die, tree decl)
13207 {
13208   HOST_WIDE_INT offset;
13209   dw_loc_descr_ref loc_descr = 0;
13210
13211   if (TREE_CODE (decl) == TREE_BINFO)
13212     {
13213       /* We're working on the TAG_inheritance for a base class.  */
13214       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
13215         {
13216           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
13217              aren't at a fixed offset from all (sub)objects of the same
13218              type.  We need to extract the appropriate offset from our
13219              vtable.  The following dwarf expression means
13220
13221                BaseAddr = ObAddr + *((*ObAddr) - Offset)
13222
13223              This is specific to the V3 ABI, of course.  */
13224
13225           dw_loc_descr_ref tmp;
13226
13227           /* Make a copy of the object address.  */
13228           tmp = new_loc_descr (DW_OP_dup, 0, 0);
13229           add_loc_descr (&loc_descr, tmp);
13230
13231           /* Extract the vtable address.  */
13232           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13233           add_loc_descr (&loc_descr, tmp);
13234
13235           /* Calculate the address of the offset.  */
13236           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
13237           gcc_assert (offset < 0);
13238
13239           tmp = int_loc_descriptor (-offset);
13240           add_loc_descr (&loc_descr, tmp);
13241           tmp = new_loc_descr (DW_OP_minus, 0, 0);
13242           add_loc_descr (&loc_descr, tmp);
13243
13244           /* Extract the offset.  */
13245           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13246           add_loc_descr (&loc_descr, tmp);
13247
13248           /* Add it to the object address.  */
13249           tmp = new_loc_descr (DW_OP_plus, 0, 0);
13250           add_loc_descr (&loc_descr, tmp);
13251         }
13252       else
13253         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
13254     }
13255   else
13256     offset = field_byte_offset (decl);
13257
13258   if (! loc_descr)
13259     {
13260       if (dwarf_version > 2)
13261         {
13262           /* Don't need to output a location expression, just the constant. */
13263           add_AT_int (die, DW_AT_data_member_location, offset);
13264           return;
13265         }
13266       else
13267         {
13268           enum dwarf_location_atom op;
13269           
13270           /* The DWARF2 standard says that we should assume that the structure
13271              address is already on the stack, so we can specify a structure
13272              field address by using DW_OP_plus_uconst.  */
13273           
13274 #ifdef MIPS_DEBUGGING_INFO
13275           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
13276              operator correctly.  It works only if we leave the offset on the
13277              stack.  */
13278           op = DW_OP_constu;
13279 #else
13280           op = DW_OP_plus_uconst;
13281 #endif
13282           
13283           loc_descr = new_loc_descr (op, offset, 0);
13284         }
13285     }
13286
13287   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
13288 }
13289
13290 /* Writes integer values to dw_vec_const array.  */
13291
13292 static void
13293 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
13294 {
13295   while (size != 0)
13296     {
13297       *dest++ = val & 0xff;
13298       val >>= 8;
13299       --size;
13300     }
13301 }
13302
13303 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
13304
13305 static HOST_WIDE_INT
13306 extract_int (const unsigned char *src, unsigned int size)
13307 {
13308   HOST_WIDE_INT val = 0;
13309
13310   src += size;
13311   while (size != 0)
13312     {
13313       val <<= 8;
13314       val |= *--src & 0xff;
13315       --size;
13316     }
13317   return val;
13318 }
13319
13320 /* Writes floating point values to dw_vec_const array.  */
13321
13322 static void
13323 insert_float (const_rtx rtl, unsigned char *array)
13324 {
13325   REAL_VALUE_TYPE rv;
13326   long val[4];
13327   int i;
13328
13329   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
13330   real_to_target (val, &rv, GET_MODE (rtl));
13331
13332   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
13333   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
13334     {
13335       insert_int (val[i], 4, array);
13336       array += 4;
13337     }
13338 }
13339
13340 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
13341    does not have a "location" either in memory or in a register.  These
13342    things can arise in GNU C when a constant is passed as an actual parameter
13343    to an inlined function.  They can also arise in C++ where declared
13344    constants do not necessarily get memory "homes".  */
13345
13346 static bool
13347 add_const_value_attribute (dw_die_ref die, rtx rtl)
13348 {
13349   switch (GET_CODE (rtl))
13350     {
13351     case CONST_INT:
13352       {
13353         HOST_WIDE_INT val = INTVAL (rtl);
13354
13355         if (val < 0)
13356           add_AT_int (die, DW_AT_const_value, val);
13357         else
13358           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
13359       }
13360       return true;
13361
13362     case CONST_DOUBLE:
13363       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
13364          floating-point constant.  A CONST_DOUBLE is used whenever the
13365          constant requires more than one word in order to be adequately
13366          represented.  We output CONST_DOUBLEs as blocks.  */
13367       {
13368         enum machine_mode mode = GET_MODE (rtl);
13369
13370         if (SCALAR_FLOAT_MODE_P (mode))
13371           {
13372             unsigned int length = GET_MODE_SIZE (mode);
13373             unsigned char *array = GGC_NEWVEC (unsigned char, length);
13374
13375             insert_float (rtl, array);
13376             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
13377           }
13378         else
13379           add_AT_long_long (die, DW_AT_const_value, rtl);
13380       }
13381       return true;
13382
13383     case CONST_VECTOR:
13384       {
13385         enum machine_mode mode = GET_MODE (rtl);
13386         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
13387         unsigned int length = CONST_VECTOR_NUNITS (rtl);
13388         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
13389         unsigned int i;
13390         unsigned char *p;
13391
13392         switch (GET_MODE_CLASS (mode))
13393           {
13394           case MODE_VECTOR_INT:
13395             for (i = 0, p = array; i < length; i++, p += elt_size)
13396               {
13397                 rtx elt = CONST_VECTOR_ELT (rtl, i);
13398                 HOST_WIDE_INT lo, hi;
13399
13400                 switch (GET_CODE (elt))
13401                   {
13402                   case CONST_INT:
13403                     lo = INTVAL (elt);
13404                     hi = -(lo < 0);
13405                     break;
13406
13407                   case CONST_DOUBLE:
13408                     lo = CONST_DOUBLE_LOW (elt);
13409                     hi = CONST_DOUBLE_HIGH (elt);
13410                     break;
13411
13412                   default:
13413                     gcc_unreachable ();
13414                   }
13415
13416                 if (elt_size <= sizeof (HOST_WIDE_INT))
13417                   insert_int (lo, elt_size, p);
13418                 else
13419                   {
13420                     unsigned char *p0 = p;
13421                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
13422
13423                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13424                     if (WORDS_BIG_ENDIAN)
13425                       {
13426                         p0 = p1;
13427                         p1 = p;
13428                       }
13429                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
13430                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
13431                   }
13432               }
13433             break;
13434
13435           case MODE_VECTOR_FLOAT:
13436             for (i = 0, p = array; i < length; i++, p += elt_size)
13437               {
13438                 rtx elt = CONST_VECTOR_ELT (rtl, i);
13439                 insert_float (elt, p);
13440               }
13441             break;
13442
13443           default:
13444             gcc_unreachable ();
13445           }
13446
13447         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
13448       }
13449       return true;
13450
13451     case CONST_STRING:
13452       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
13453       return true;
13454
13455     case CONST:
13456       if (CONSTANT_P (XEXP (rtl, 0)))
13457         {
13458           add_const_value_attribute (die, XEXP (rtl, 0));
13459           return true;
13460         }
13461       /* FALLTHROUGH */
13462     case SYMBOL_REF:
13463       if (GET_CODE (rtl) == SYMBOL_REF
13464           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13465         return false;
13466     case LABEL_REF:
13467       add_AT_addr (die, DW_AT_const_value, rtl);
13468       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13469       return true;
13470
13471     case PLUS:
13472       /* In cases where an inlined instance of an inline function is passed
13473          the address of an `auto' variable (which is local to the caller) we
13474          can get a situation where the DECL_RTL of the artificial local
13475          variable (for the inlining) which acts as a stand-in for the
13476          corresponding formal parameter (of the inline function) will look
13477          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
13478          exactly a compile-time constant expression, but it isn't the address
13479          of the (artificial) local variable either.  Rather, it represents the
13480          *value* which the artificial local variable always has during its
13481          lifetime.  We currently have no way to represent such quasi-constant
13482          values in Dwarf, so for now we just punt and generate nothing.  */
13483       return false;
13484
13485     default:
13486       /* No other kinds of rtx should be possible here.  */
13487       gcc_unreachable ();
13488     }
13489   return false;
13490 }
13491
13492 /* Determine whether the evaluation of EXPR references any variables
13493    or functions which aren't otherwise used (and therefore may not be
13494    output).  */
13495 static tree
13496 reference_to_unused (tree * tp, int * walk_subtrees,
13497                      void * data ATTRIBUTE_UNUSED)
13498 {
13499   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
13500     *walk_subtrees = 0;
13501
13502   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
13503       && ! TREE_ASM_WRITTEN (*tp))
13504     return *tp;
13505   /* ???  The C++ FE emits debug information for using decls, so
13506      putting gcc_unreachable here falls over.  See PR31899.  For now
13507      be conservative.  */
13508   else if (!cgraph_global_info_ready
13509            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
13510     return *tp;
13511   else if (TREE_CODE (*tp) == VAR_DECL)
13512     {
13513       struct varpool_node *node = varpool_node (*tp);
13514       if (!node->needed)
13515         return *tp;
13516     }
13517   else if (TREE_CODE (*tp) == FUNCTION_DECL
13518            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
13519     {
13520       /* The call graph machinery must have finished analyzing,
13521          optimizing and gimplifying the CU by now.
13522          So if *TP has no call graph node associated
13523          to it, it means *TP will not be emitted.  */
13524       if (!cgraph_get_node (*tp))
13525         return *tp;
13526     }
13527   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
13528     return *tp;
13529
13530   return NULL_TREE;
13531 }
13532
13533 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
13534    for use in a later add_const_value_attribute call.  */
13535
13536 static rtx
13537 rtl_for_decl_init (tree init, tree type)
13538 {
13539   rtx rtl = NULL_RTX;
13540
13541   /* If a variable is initialized with a string constant without embedded
13542      zeros, build CONST_STRING.  */
13543   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
13544     {
13545       tree enttype = TREE_TYPE (type);
13546       tree domain = TYPE_DOMAIN (type);
13547       enum machine_mode mode = TYPE_MODE (enttype);
13548
13549       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
13550           && domain
13551           && integer_zerop (TYPE_MIN_VALUE (domain))
13552           && compare_tree_int (TYPE_MAX_VALUE (domain),
13553                                TREE_STRING_LENGTH (init) - 1) == 0
13554           && ((size_t) TREE_STRING_LENGTH (init)
13555               == strlen (TREE_STRING_POINTER (init)) + 1))
13556         rtl = gen_rtx_CONST_STRING (VOIDmode,
13557                                     ggc_strdup (TREE_STRING_POINTER (init)));
13558     }
13559   /* Other aggregates, and complex values, could be represented using
13560      CONCAT: FIXME!  */
13561   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
13562     ;
13563   /* Vectors only work if their mode is supported by the target.
13564      FIXME: generic vectors ought to work too.  */
13565   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
13566     ;
13567   /* If the initializer is something that we know will expand into an
13568      immediate RTL constant, expand it now.  We must be careful not to
13569      reference variables which won't be output.  */
13570   else if (initializer_constant_valid_p (init, type)
13571            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
13572     {
13573       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
13574          possible.  */
13575       if (TREE_CODE (type) == VECTOR_TYPE)
13576         switch (TREE_CODE (init))
13577           {
13578           case VECTOR_CST:
13579             break;
13580           case CONSTRUCTOR:
13581             if (TREE_CONSTANT (init))
13582               {
13583                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
13584                 bool constant_p = true;
13585                 tree value;
13586                 unsigned HOST_WIDE_INT ix;
13587
13588                 /* Even when ctor is constant, it might contain non-*_CST
13589                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
13590                    belong into VECTOR_CST nodes.  */
13591                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
13592                   if (!CONSTANT_CLASS_P (value))
13593                     {
13594                       constant_p = false;
13595                       break;
13596                     }
13597
13598                 if (constant_p)
13599                   {
13600                     init = build_vector_from_ctor (type, elts);
13601                     break;
13602                   }
13603               }
13604             /* FALLTHRU */
13605
13606           default:
13607             return NULL;
13608           }
13609
13610       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
13611
13612       /* If expand_expr returns a MEM, it wasn't immediate.  */
13613       gcc_assert (!rtl || !MEM_P (rtl));
13614     }
13615
13616   return rtl;
13617 }
13618
13619 /* Generate RTL for the variable DECL to represent its location.  */
13620
13621 static rtx
13622 rtl_for_decl_location (tree decl)
13623 {
13624   rtx rtl;
13625
13626   /* Here we have to decide where we are going to say the parameter "lives"
13627      (as far as the debugger is concerned).  We only have a couple of
13628      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
13629
13630      DECL_RTL normally indicates where the parameter lives during most of the
13631      activation of the function.  If optimization is enabled however, this
13632      could be either NULL or else a pseudo-reg.  Both of those cases indicate
13633      that the parameter doesn't really live anywhere (as far as the code
13634      generation parts of GCC are concerned) during most of the function's
13635      activation.  That will happen (for example) if the parameter is never
13636      referenced within the function.
13637
13638      We could just generate a location descriptor here for all non-NULL
13639      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
13640      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
13641      where DECL_RTL is NULL or is a pseudo-reg.
13642
13643      Note however that we can only get away with using DECL_INCOMING_RTL as
13644      a backup substitute for DECL_RTL in certain limited cases.  In cases
13645      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
13646      we can be sure that the parameter was passed using the same type as it is
13647      declared to have within the function, and that its DECL_INCOMING_RTL
13648      points us to a place where a value of that type is passed.
13649
13650      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
13651      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
13652      because in these cases DECL_INCOMING_RTL points us to a value of some
13653      type which is *different* from the type of the parameter itself.  Thus,
13654      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
13655      such cases, the debugger would end up (for example) trying to fetch a
13656      `float' from a place which actually contains the first part of a
13657      `double'.  That would lead to really incorrect and confusing
13658      output at debug-time.
13659
13660      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
13661      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
13662      are a couple of exceptions however.  On little-endian machines we can
13663      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
13664      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
13665      an integral type that is smaller than TREE_TYPE (decl). These cases arise
13666      when (on a little-endian machine) a non-prototyped function has a
13667      parameter declared to be of type `short' or `char'.  In such cases,
13668      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
13669      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
13670      passed `int' value.  If the debugger then uses that address to fetch
13671      a `short' or a `char' (on a little-endian machine) the result will be
13672      the correct data, so we allow for such exceptional cases below.
13673
13674      Note that our goal here is to describe the place where the given formal
13675      parameter lives during most of the function's activation (i.e. between the
13676      end of the prologue and the start of the epilogue).  We'll do that as best
13677      as we can. Note however that if the given formal parameter is modified
13678      sometime during the execution of the function, then a stack backtrace (at
13679      debug-time) will show the function as having been called with the *new*
13680      value rather than the value which was originally passed in.  This happens
13681      rarely enough that it is not a major problem, but it *is* a problem, and
13682      I'd like to fix it.
13683
13684      A future version of dwarf2out.c may generate two additional attributes for
13685      any given DW_TAG_formal_parameter DIE which will describe the "passed
13686      type" and the "passed location" for the given formal parameter in addition
13687      to the attributes we now generate to indicate the "declared type" and the
13688      "active location" for each parameter.  This additional set of attributes
13689      could be used by debuggers for stack backtraces. Separately, note that
13690      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
13691      This happens (for example) for inlined-instances of inline function formal
13692      parameters which are never referenced.  This really shouldn't be
13693      happening.  All PARM_DECL nodes should get valid non-NULL
13694      DECL_INCOMING_RTL values.  FIXME.  */
13695
13696   /* Use DECL_RTL as the "location" unless we find something better.  */
13697   rtl = DECL_RTL_IF_SET (decl);
13698
13699   /* When generating abstract instances, ignore everything except
13700      constants, symbols living in memory, and symbols living in
13701      fixed registers.  */
13702   if (! reload_completed)
13703     {
13704       if (rtl
13705           && (CONSTANT_P (rtl)
13706               || (MEM_P (rtl)
13707                   && CONSTANT_P (XEXP (rtl, 0)))
13708               || (REG_P (rtl)
13709                   && TREE_CODE (decl) == VAR_DECL
13710                   && TREE_STATIC (decl))))
13711         {
13712           rtl = targetm.delegitimize_address (rtl);
13713           return rtl;
13714         }
13715       rtl = NULL_RTX;
13716     }
13717   else if (TREE_CODE (decl) == PARM_DECL)
13718     {
13719       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
13720         {
13721           tree declared_type = TREE_TYPE (decl);
13722           tree passed_type = DECL_ARG_TYPE (decl);
13723           enum machine_mode dmode = TYPE_MODE (declared_type);
13724           enum machine_mode pmode = TYPE_MODE (passed_type);
13725
13726           /* This decl represents a formal parameter which was optimized out.
13727              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
13728              all cases where (rtl == NULL_RTX) just below.  */
13729           if (dmode == pmode)
13730             rtl = DECL_INCOMING_RTL (decl);
13731           else if (SCALAR_INT_MODE_P (dmode)
13732                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
13733                    && DECL_INCOMING_RTL (decl))
13734             {
13735               rtx inc = DECL_INCOMING_RTL (decl);
13736               if (REG_P (inc))
13737                 rtl = inc;
13738               else if (MEM_P (inc))
13739                 {
13740                   if (BYTES_BIG_ENDIAN)
13741                     rtl = adjust_address_nv (inc, dmode,
13742                                              GET_MODE_SIZE (pmode)
13743                                              - GET_MODE_SIZE (dmode));
13744                   else
13745                     rtl = inc;
13746                 }
13747             }
13748         }
13749
13750       /* If the parm was passed in registers, but lives on the stack, then
13751          make a big endian correction if the mode of the type of the
13752          parameter is not the same as the mode of the rtl.  */
13753       /* ??? This is the same series of checks that are made in dbxout.c before
13754          we reach the big endian correction code there.  It isn't clear if all
13755          of these checks are necessary here, but keeping them all is the safe
13756          thing to do.  */
13757       else if (MEM_P (rtl)
13758                && XEXP (rtl, 0) != const0_rtx
13759                && ! CONSTANT_P (XEXP (rtl, 0))
13760                /* Not passed in memory.  */
13761                && !MEM_P (DECL_INCOMING_RTL (decl))
13762                /* Not passed by invisible reference.  */
13763                && (!REG_P (XEXP (rtl, 0))
13764                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
13765                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
13766 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
13767                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
13768 #endif
13769                      )
13770                /* Big endian correction check.  */
13771                && BYTES_BIG_ENDIAN
13772                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
13773                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
13774                    < UNITS_PER_WORD))
13775         {
13776           int offset = (UNITS_PER_WORD
13777                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
13778
13779           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13780                              plus_constant (XEXP (rtl, 0), offset));
13781         }
13782     }
13783   else if (TREE_CODE (decl) == VAR_DECL
13784            && rtl
13785            && MEM_P (rtl)
13786            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
13787            && BYTES_BIG_ENDIAN)
13788     {
13789       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
13790       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
13791
13792       /* If a variable is declared "register" yet is smaller than
13793          a register, then if we store the variable to memory, it
13794          looks like we're storing a register-sized value, when in
13795          fact we are not.  We need to adjust the offset of the
13796          storage location to reflect the actual value's bytes,
13797          else gdb will not be able to display it.  */
13798       if (rsize > dsize)
13799         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13800                            plus_constant (XEXP (rtl, 0), rsize-dsize));
13801     }
13802
13803   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
13804      and will have been substituted directly into all expressions that use it.
13805      C does not have such a concept, but C++ and other languages do.  */
13806   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
13807     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
13808
13809   if (rtl)
13810     rtl = targetm.delegitimize_address (rtl);
13811
13812   /* If we don't look past the constant pool, we risk emitting a
13813      reference to a constant pool entry that isn't referenced from
13814      code, and thus is not emitted.  */
13815   if (rtl)
13816     rtl = avoid_constant_pool_reference (rtl);
13817
13818   return rtl;
13819 }
13820
13821 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
13822    returned.  If so, the decl for the COMMON block is returned, and the
13823    value is the offset into the common block for the symbol.  */
13824
13825 static tree
13826 fortran_common (tree decl, HOST_WIDE_INT *value)
13827 {
13828   tree val_expr, cvar;
13829   enum machine_mode mode;
13830   HOST_WIDE_INT bitsize, bitpos;
13831   tree offset;
13832   int volatilep = 0, unsignedp = 0;
13833
13834   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
13835      it does not have a value (the offset into the common area), or if it
13836      is thread local (as opposed to global) then it isn't common, and shouldn't
13837      be handled as such.  */
13838   if (TREE_CODE (decl) != VAR_DECL
13839       || !TREE_PUBLIC (decl)
13840       || !TREE_STATIC (decl)
13841       || !DECL_HAS_VALUE_EXPR_P (decl)
13842       || !is_fortran ())
13843     return NULL_TREE;
13844
13845   val_expr = DECL_VALUE_EXPR (decl);
13846   if (TREE_CODE (val_expr) != COMPONENT_REF)
13847     return NULL_TREE;
13848
13849   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
13850                               &mode, &unsignedp, &volatilep, true);
13851
13852   if (cvar == NULL_TREE
13853       || TREE_CODE (cvar) != VAR_DECL
13854       || DECL_ARTIFICIAL (cvar)
13855       || !TREE_PUBLIC (cvar))
13856     return NULL_TREE;
13857
13858   *value = 0;
13859   if (offset != NULL)
13860     {
13861       if (!host_integerp (offset, 0))
13862         return NULL_TREE;
13863       *value = tree_low_cst (offset, 0);
13864     }
13865   if (bitpos != 0)
13866     *value += bitpos / BITS_PER_UNIT;
13867
13868   return cvar;
13869 }
13870
13871 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
13872    data attribute for a variable or a parameter.  We generate the
13873    DW_AT_const_value attribute only in those cases where the given variable
13874    or parameter does not have a true "location" either in memory or in a
13875    register.  This can happen (for example) when a constant is passed as an
13876    actual argument in a call to an inline function.  (It's possible that
13877    these things can crop up in other ways also.)  Note that one type of
13878    constant value which can be passed into an inlined function is a constant
13879    pointer.  This can happen for example if an actual argument in an inlined
13880    function call evaluates to a compile-time constant address.  */
13881
13882 static bool
13883 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
13884                                        enum dwarf_attribute attr)
13885 {
13886   rtx rtl;
13887   dw_loc_list_ref list;
13888   var_loc_list *loc_list;
13889
13890   if (TREE_CODE (decl) == ERROR_MARK)
13891     return false;
13892
13893   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
13894               || TREE_CODE (decl) == RESULT_DECL);
13895
13896   /* Try to get some constant RTL for this decl, and use that as the value of
13897      the location.  */
13898
13899   rtl = rtl_for_decl_location (decl);
13900   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
13901       && add_const_value_attribute (die, rtl))
13902     return true;
13903
13904   /* See if we have single element location list that is equivalent to
13905      a constant value.  That way we are better to use add_const_value_attribute
13906      rather than expanding constant value equivalent.  */
13907   loc_list = lookup_decl_loc (decl);
13908   if (loc_list && loc_list->first && loc_list->first == loc_list->last)
13909     {
13910       enum var_init_status status;
13911       struct var_loc_node *node;
13912
13913       node = loc_list->first;
13914       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13915       rtl = NOTE_VAR_LOCATION (node->var_loc_note);
13916       if (GET_CODE (rtl) == VAR_LOCATION
13917           && GET_CODE (XEXP (rtl, 1)) != PARALLEL)
13918         rtl = XEXP (XEXP (rtl, 1), 0);
13919       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
13920           && add_const_value_attribute (die, rtl))
13921          return true;
13922     }
13923   list = loc_list_from_tree (decl, 2);
13924   if (list)
13925     {
13926       add_AT_location_description (die, attr, list);
13927       return true;
13928     }
13929   /* None of that worked, so it must not really have a location;
13930      try adding a constant value attribute from the DECL_INITIAL.  */
13931   return tree_add_const_value_attribute_for_decl (die, decl);
13932 }
13933
13934 /* Add VARIABLE and DIE into deferred locations list.  */
13935
13936 static void
13937 defer_location (tree variable, dw_die_ref die)
13938 {
13939   deferred_locations entry;
13940   entry.variable = variable;
13941   entry.die = die;
13942   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
13943 }
13944
13945 /* Helper function for tree_add_const_value_attribute.  Natively encode
13946    initializer INIT into an array.  Return true if successful.  */
13947
13948 static bool
13949 native_encode_initializer (tree init, unsigned char *array, int size)
13950 {
13951   tree type;
13952
13953   if (init == NULL_TREE)
13954     return false;
13955
13956   STRIP_NOPS (init);
13957   switch (TREE_CODE (init))
13958     {
13959     case STRING_CST:
13960       type = TREE_TYPE (init);
13961       if (TREE_CODE (type) == ARRAY_TYPE)
13962         {
13963           tree enttype = TREE_TYPE (type);
13964           enum machine_mode mode = TYPE_MODE (enttype);
13965
13966           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
13967             return false;
13968           if (int_size_in_bytes (type) != size)
13969             return false;
13970           if (size > TREE_STRING_LENGTH (init))
13971             {
13972               memcpy (array, TREE_STRING_POINTER (init),
13973                       TREE_STRING_LENGTH (init));
13974               memset (array + TREE_STRING_LENGTH (init),
13975                       '\0', size - TREE_STRING_LENGTH (init));
13976             }
13977           else
13978             memcpy (array, TREE_STRING_POINTER (init), size);
13979           return true;
13980         }
13981       return false;
13982     case CONSTRUCTOR:
13983       type = TREE_TYPE (init);
13984       if (int_size_in_bytes (type) != size)
13985         return false;
13986       if (TREE_CODE (type) == ARRAY_TYPE)
13987         {
13988           HOST_WIDE_INT min_index;
13989           unsigned HOST_WIDE_INT cnt;
13990           int curpos = 0, fieldsize;
13991           constructor_elt *ce;
13992
13993           if (TYPE_DOMAIN (type) == NULL_TREE
13994               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
13995             return false;
13996
13997           fieldsize = int_size_in_bytes (TREE_TYPE (type));
13998           if (fieldsize <= 0)
13999             return false;
14000
14001           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14002           memset (array, '\0', size);
14003           for (cnt = 0;
14004                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
14005                cnt++)
14006             {
14007               tree val = ce->value;
14008               tree index = ce->index;
14009               int pos = curpos;
14010               if (index && TREE_CODE (index) == RANGE_EXPR)
14011                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14012                       * fieldsize;
14013               else if (index)
14014                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14015
14016               if (val)
14017                 {
14018                   STRIP_NOPS (val);
14019                   if (!native_encode_initializer (val, array + pos, fieldsize))
14020                     return false;
14021                 }
14022               curpos = pos + fieldsize;
14023               if (index && TREE_CODE (index) == RANGE_EXPR)
14024                 {
14025                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14026                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
14027                   while (count > 0)
14028                     {
14029                       if (val)
14030                         memcpy (array + curpos, array + pos, fieldsize);
14031                       curpos += fieldsize;
14032                     }
14033                 }
14034               gcc_assert (curpos <= size);
14035             }
14036           return true;
14037         }
14038       else if (TREE_CODE (type) == RECORD_TYPE
14039                || TREE_CODE (type) == UNION_TYPE)
14040         {
14041           tree field = NULL_TREE;
14042           unsigned HOST_WIDE_INT cnt;
14043           constructor_elt *ce;
14044
14045           if (int_size_in_bytes (type) != size)
14046             return false;
14047
14048           if (TREE_CODE (type) == RECORD_TYPE)
14049             field = TYPE_FIELDS (type);
14050
14051           for (cnt = 0;
14052                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
14053                cnt++, field = field ? TREE_CHAIN (field) : 0)
14054             {
14055               tree val = ce->value;
14056               int pos, fieldsize;
14057
14058               if (ce->index != 0)
14059                 field = ce->index;
14060
14061               if (val)
14062                 STRIP_NOPS (val);
14063
14064               if (field == NULL_TREE || DECL_BIT_FIELD (field))
14065                 return false;
14066
14067               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
14068                   && TYPE_DOMAIN (TREE_TYPE (field))
14069                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
14070                 return false;
14071               else if (DECL_SIZE_UNIT (field) == NULL_TREE
14072                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
14073                 return false;
14074               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
14075               pos = int_byte_position (field);
14076               gcc_assert (pos + fieldsize <= size);
14077               if (val
14078                   && !native_encode_initializer (val, array + pos, fieldsize))
14079                 return false;
14080             }
14081           return true;
14082         }
14083       return false;
14084     case VIEW_CONVERT_EXPR:
14085     case NON_LVALUE_EXPR:
14086       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
14087     default:
14088       return native_encode_expr (init, array, size) == size;
14089     }
14090 }
14091
14092 /* Attach a DW_AT_const_value attribute to DIE. The value of the
14093    attribute is the const value T.  */
14094
14095 static bool
14096 tree_add_const_value_attribute (dw_die_ref die, tree t)
14097 {
14098   tree init;
14099   tree type = TREE_TYPE (t);
14100   rtx rtl;
14101
14102   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
14103     return false;
14104
14105   init = t;
14106   gcc_assert (!DECL_P (init));
14107
14108   rtl = rtl_for_decl_init (init, type);
14109   if (rtl)
14110     {
14111       add_const_value_attribute (die, rtl);
14112       return true;
14113     }
14114   /* If the host and target are sane, try harder.  */
14115   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
14116            && initializer_constant_valid_p (init, type))
14117     {
14118       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
14119       if (size > 0 && (int) size == size)
14120         {
14121           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
14122
14123           if (native_encode_initializer (init, array, size))
14124             {
14125               add_AT_vec (die, DW_AT_const_value, size, 1, array);
14126               return true;
14127             }
14128         }
14129     }
14130   return false;
14131 }
14132
14133 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
14134    attribute is the const value of T, where T is an integral constant
14135    variable with static storage duration
14136    (so it can't be a PARM_DECL or a RESULT_DECL).  */
14137
14138 static bool
14139 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
14140 {
14141
14142   if (!decl
14143       || (TREE_CODE (decl) != VAR_DECL
14144           && TREE_CODE (decl) != CONST_DECL))
14145     return false;
14146
14147     if (TREE_READONLY (decl)
14148         && ! TREE_THIS_VOLATILE (decl)
14149         && DECL_INITIAL (decl))
14150       /* OK */;
14151     else
14152       return false;
14153
14154   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
14155 }
14156
14157 /* Convert the CFI instructions for the current function into a
14158    location list.  This is used for DW_AT_frame_base when we targeting
14159    a dwarf2 consumer that does not support the dwarf3
14160    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
14161    expressions.  */
14162
14163 static dw_loc_list_ref
14164 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
14165 {
14166   dw_fde_ref fde;
14167   dw_loc_list_ref list, *list_tail;
14168   dw_cfi_ref cfi;
14169   dw_cfa_location last_cfa, next_cfa;
14170   const char *start_label, *last_label, *section;
14171   dw_cfa_location remember;
14172
14173   fde = current_fde ();
14174   gcc_assert (fde != NULL);
14175
14176   section = secname_for_decl (current_function_decl);
14177   list_tail = &list;
14178   list = NULL;
14179
14180   memset (&next_cfa, 0, sizeof (next_cfa));
14181   next_cfa.reg = INVALID_REGNUM;
14182   remember = next_cfa;
14183
14184   start_label = fde->dw_fde_begin;
14185
14186   /* ??? Bald assumption that the CIE opcode list does not contain
14187      advance opcodes.  */
14188   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
14189     lookup_cfa_1 (cfi, &next_cfa, &remember);
14190
14191   last_cfa = next_cfa;
14192   last_label = start_label;
14193
14194   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
14195     switch (cfi->dw_cfi_opc)
14196       {
14197       case DW_CFA_set_loc:
14198       case DW_CFA_advance_loc1:
14199       case DW_CFA_advance_loc2:
14200       case DW_CFA_advance_loc4:
14201         if (!cfa_equal_p (&last_cfa, &next_cfa))
14202           {
14203             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14204                                        start_label, last_label, section,
14205                                        list == NULL);
14206
14207             list_tail = &(*list_tail)->dw_loc_next;
14208             last_cfa = next_cfa;
14209             start_label = last_label;
14210           }
14211         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
14212         break;
14213
14214       case DW_CFA_advance_loc:
14215         /* The encoding is complex enough that we should never emit this.  */
14216         gcc_unreachable ();
14217
14218       default:
14219         lookup_cfa_1 (cfi, &next_cfa, &remember);
14220         break;
14221       }
14222
14223   if (!cfa_equal_p (&last_cfa, &next_cfa))
14224     {
14225       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14226                                  start_label, last_label, section,
14227                                  list == NULL);
14228       list_tail = &(*list_tail)->dw_loc_next;
14229       start_label = last_label;
14230     }
14231   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
14232                              start_label, fde->dw_fde_end, section,
14233                              list == NULL);
14234
14235   return list;
14236 }
14237
14238 /* Compute a displacement from the "steady-state frame pointer" to the
14239    frame base (often the same as the CFA), and store it in
14240    frame_pointer_fb_offset.  OFFSET is added to the displacement
14241    before the latter is negated.  */
14242
14243 static void
14244 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
14245 {
14246   rtx reg, elim;
14247
14248 #ifdef FRAME_POINTER_CFA_OFFSET
14249   reg = frame_pointer_rtx;
14250   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
14251 #else
14252   reg = arg_pointer_rtx;
14253   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
14254 #endif
14255
14256   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
14257   if (GET_CODE (elim) == PLUS)
14258     {
14259       offset += INTVAL (XEXP (elim, 1));
14260       elim = XEXP (elim, 0);
14261     }
14262
14263   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
14264                && (elim == hard_frame_pointer_rtx
14265                    || elim == stack_pointer_rtx))
14266               || elim == (frame_pointer_needed
14267                           ? hard_frame_pointer_rtx
14268                           : stack_pointer_rtx));
14269
14270   frame_pointer_fb_offset = -offset;
14271 }
14272
14273 /* Generate a DW_AT_name attribute given some string value to be included as
14274    the value of the attribute.  */
14275
14276 static void
14277 add_name_attribute (dw_die_ref die, const char *name_string)
14278 {
14279   if (name_string != NULL && *name_string != 0)
14280     {
14281       if (demangle_name_func)
14282         name_string = (*demangle_name_func) (name_string);
14283
14284       add_AT_string (die, DW_AT_name, name_string);
14285     }
14286 }
14287
14288 /* Generate a DW_AT_comp_dir attribute for DIE.  */
14289
14290 static void
14291 add_comp_dir_attribute (dw_die_ref die)
14292 {
14293   const char *wd = get_src_pwd ();
14294   char *wd1;
14295
14296   if (wd == NULL)
14297     return;
14298
14299   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
14300     {
14301       int wdlen;
14302
14303       wdlen = strlen (wd);
14304       wd1 = GGC_NEWVEC (char, wdlen + 2);
14305       strcpy (wd1, wd);
14306       wd1 [wdlen] = DIR_SEPARATOR;
14307       wd1 [wdlen + 1] = 0;
14308       wd = wd1;
14309     }
14310
14311     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
14312 }
14313
14314 /* Given a tree node describing an array bound (either lower or upper) output
14315    a representation for that bound.  */
14316
14317 static void
14318 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
14319 {
14320   switch (TREE_CODE (bound))
14321     {
14322     case ERROR_MARK:
14323       return;
14324
14325     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
14326     case INTEGER_CST:
14327       if (! host_integerp (bound, 0)
14328           || (bound_attr == DW_AT_lower_bound
14329               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
14330                   || (is_fortran () && integer_onep (bound)))))
14331         /* Use the default.  */
14332         ;
14333       else
14334         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
14335       break;
14336
14337     CASE_CONVERT:
14338     case VIEW_CONVERT_EXPR:
14339       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
14340       break;
14341
14342     case SAVE_EXPR:
14343       break;
14344
14345     case VAR_DECL:
14346     case PARM_DECL:
14347     case RESULT_DECL:
14348       {
14349         dw_die_ref decl_die = lookup_decl_die (bound);
14350         dw_loc_list_ref loc;
14351
14352         /* ??? Can this happen, or should the variable have been bound
14353            first?  Probably it can, since I imagine that we try to create
14354            the types of parameters in the order in which they exist in
14355            the list, and won't have created a forward reference to a
14356            later parameter.  */
14357         if (decl_die != NULL)
14358           add_AT_die_ref (subrange_die, bound_attr, decl_die);
14359         else
14360           {
14361             loc = loc_list_from_tree (bound, 0);
14362             add_AT_location_description (subrange_die, bound_attr, loc);
14363           }
14364         break;
14365       }
14366
14367     default:
14368       {
14369         /* Otherwise try to create a stack operation procedure to
14370            evaluate the value of the array bound.  */
14371
14372         dw_die_ref ctx, decl_die;
14373         dw_loc_list_ref list;
14374
14375         list = loc_list_from_tree (bound, 2);
14376         if (list == NULL)
14377           break;
14378
14379         if (current_function_decl == 0)
14380           ctx = comp_unit_die;
14381         else
14382           ctx = lookup_decl_die (current_function_decl);
14383
14384         decl_die = new_die (DW_TAG_variable, ctx, bound);
14385         add_AT_flag (decl_die, DW_AT_artificial, 1);
14386         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
14387         if (list->dw_loc_next)
14388           add_AT_loc_list (decl_die, DW_AT_location, list);
14389         else
14390           add_AT_loc (decl_die, DW_AT_location, list->expr);
14391
14392         add_AT_die_ref (subrange_die, bound_attr, decl_die);
14393         break;
14394       }
14395     }
14396 }
14397
14398 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
14399    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
14400    Note that the block of subscript information for an array type also
14401    includes information about the element type of the given array type.  */
14402
14403 static void
14404 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
14405 {
14406   unsigned dimension_number;
14407   tree lower, upper;
14408   dw_die_ref subrange_die;
14409
14410   for (dimension_number = 0;
14411        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
14412        type = TREE_TYPE (type), dimension_number++)
14413     {
14414       tree domain = TYPE_DOMAIN (type);
14415
14416       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
14417         break;
14418
14419       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
14420          and (in GNU C only) variable bounds.  Handle all three forms
14421          here.  */
14422       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
14423       if (domain)
14424         {
14425           /* We have an array type with specified bounds.  */
14426           lower = TYPE_MIN_VALUE (domain);
14427           upper = TYPE_MAX_VALUE (domain);
14428
14429           /* Define the index type.  */
14430           if (TREE_TYPE (domain))
14431             {
14432               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
14433                  TREE_TYPE field.  We can't emit debug info for this
14434                  because it is an unnamed integral type.  */
14435               if (TREE_CODE (domain) == INTEGER_TYPE
14436                   && TYPE_NAME (domain) == NULL_TREE
14437                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
14438                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
14439                 ;
14440               else
14441                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
14442                                     type_die);
14443             }
14444
14445           /* ??? If upper is NULL, the array has unspecified length,
14446              but it does have a lower bound.  This happens with Fortran
14447                dimension arr(N:*)
14448              Since the debugger is definitely going to need to know N
14449              to produce useful results, go ahead and output the lower
14450              bound solo, and hope the debugger can cope.  */
14451
14452           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
14453           if (upper)
14454             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
14455         }
14456
14457       /* Otherwise we have an array type with an unspecified length.  The
14458          DWARF-2 spec does not say how to handle this; let's just leave out the
14459          bounds.  */
14460     }
14461 }
14462
14463 static void
14464 add_byte_size_attribute (dw_die_ref die, tree tree_node)
14465 {
14466   unsigned size;
14467
14468   switch (TREE_CODE (tree_node))
14469     {
14470     case ERROR_MARK:
14471       size = 0;
14472       break;
14473     case ENUMERAL_TYPE:
14474     case RECORD_TYPE:
14475     case UNION_TYPE:
14476     case QUAL_UNION_TYPE:
14477       size = int_size_in_bytes (tree_node);
14478       break;
14479     case FIELD_DECL:
14480       /* For a data member of a struct or union, the DW_AT_byte_size is
14481          generally given as the number of bytes normally allocated for an
14482          object of the *declared* type of the member itself.  This is true
14483          even for bit-fields.  */
14484       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
14485       break;
14486     default:
14487       gcc_unreachable ();
14488     }
14489
14490   /* Note that `size' might be -1 when we get to this point.  If it is, that
14491      indicates that the byte size of the entity in question is variable.  We
14492      have no good way of expressing this fact in Dwarf at the present time,
14493      so just let the -1 pass on through.  */
14494   add_AT_unsigned (die, DW_AT_byte_size, size);
14495 }
14496
14497 /* For a FIELD_DECL node which represents a bit-field, output an attribute
14498    which specifies the distance in bits from the highest order bit of the
14499    "containing object" for the bit-field to the highest order bit of the
14500    bit-field itself.
14501
14502    For any given bit-field, the "containing object" is a hypothetical object
14503    (of some integral or enum type) within which the given bit-field lives.  The
14504    type of this hypothetical "containing object" is always the same as the
14505    declared type of the individual bit-field itself.  The determination of the
14506    exact location of the "containing object" for a bit-field is rather
14507    complicated.  It's handled by the `field_byte_offset' function (above).
14508
14509    Note that it is the size (in bytes) of the hypothetical "containing object"
14510    which will be given in the DW_AT_byte_size attribute for this bit-field.
14511    (See `byte_size_attribute' above).  */
14512
14513 static inline void
14514 add_bit_offset_attribute (dw_die_ref die, tree decl)
14515 {
14516   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
14517   tree type = DECL_BIT_FIELD_TYPE (decl);
14518   HOST_WIDE_INT bitpos_int;
14519   HOST_WIDE_INT highest_order_object_bit_offset;
14520   HOST_WIDE_INT highest_order_field_bit_offset;
14521   HOST_WIDE_INT unsigned bit_offset;
14522
14523   /* Must be a field and a bit field.  */
14524   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
14525
14526   /* We can't yet handle bit-fields whose offsets are variable, so if we
14527      encounter such things, just return without generating any attribute
14528      whatsoever.  Likewise for variable or too large size.  */
14529   if (! host_integerp (bit_position (decl), 0)
14530       || ! host_integerp (DECL_SIZE (decl), 1))
14531     return;
14532
14533   bitpos_int = int_bit_position (decl);
14534
14535   /* Note that the bit offset is always the distance (in bits) from the
14536      highest-order bit of the "containing object" to the highest-order bit of
14537      the bit-field itself.  Since the "high-order end" of any object or field
14538      is different on big-endian and little-endian machines, the computation
14539      below must take account of these differences.  */
14540   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
14541   highest_order_field_bit_offset = bitpos_int;
14542
14543   if (! BYTES_BIG_ENDIAN)
14544     {
14545       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
14546       highest_order_object_bit_offset += simple_type_size_in_bits (type);
14547     }
14548
14549   bit_offset
14550     = (! BYTES_BIG_ENDIAN
14551        ? highest_order_object_bit_offset - highest_order_field_bit_offset
14552        : highest_order_field_bit_offset - highest_order_object_bit_offset);
14553
14554   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
14555 }
14556
14557 /* For a FIELD_DECL node which represents a bit field, output an attribute
14558    which specifies the length in bits of the given field.  */
14559
14560 static inline void
14561 add_bit_size_attribute (dw_die_ref die, tree decl)
14562 {
14563   /* Must be a field and a bit field.  */
14564   gcc_assert (TREE_CODE (decl) == FIELD_DECL
14565               && DECL_BIT_FIELD_TYPE (decl));
14566
14567   if (host_integerp (DECL_SIZE (decl), 1))
14568     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
14569 }
14570
14571 /* If the compiled language is ANSI C, then add a 'prototyped'
14572    attribute, if arg types are given for the parameters of a function.  */
14573
14574 static inline void
14575 add_prototyped_attribute (dw_die_ref die, tree func_type)
14576 {
14577   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
14578       && TYPE_ARG_TYPES (func_type) != NULL)
14579     add_AT_flag (die, DW_AT_prototyped, 1);
14580 }
14581
14582 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
14583    by looking in either the type declaration or object declaration
14584    equate table.  */
14585
14586 static inline dw_die_ref
14587 add_abstract_origin_attribute (dw_die_ref die, tree origin)
14588 {
14589   dw_die_ref origin_die = NULL;
14590
14591   if (TREE_CODE (origin) != FUNCTION_DECL)
14592     {
14593       /* We may have gotten separated from the block for the inlined
14594          function, if we're in an exception handler or some such; make
14595          sure that the abstract function has been written out.
14596
14597          Doing this for nested functions is wrong, however; functions are
14598          distinct units, and our context might not even be inline.  */
14599       tree fn = origin;
14600
14601       if (TYPE_P (fn))
14602         fn = TYPE_STUB_DECL (fn);
14603
14604       fn = decl_function_context (fn);
14605       if (fn)
14606         dwarf2out_abstract_function (fn);
14607     }
14608
14609   if (DECL_P (origin))
14610     origin_die = lookup_decl_die (origin);
14611   else if (TYPE_P (origin))
14612     origin_die = lookup_type_die (origin);
14613
14614   /* XXX: Functions that are never lowered don't always have correct block
14615      trees (in the case of java, they simply have no block tree, in some other
14616      languages).  For these functions, there is nothing we can really do to
14617      output correct debug info for inlined functions in all cases.  Rather
14618      than die, we'll just produce deficient debug info now, in that we will
14619      have variables without a proper abstract origin.  In the future, when all
14620      functions are lowered, we should re-add a gcc_assert (origin_die)
14621      here.  */
14622
14623   if (origin_die)
14624     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
14625   return origin_die;
14626 }
14627
14628 /* We do not currently support the pure_virtual attribute.  */
14629
14630 static inline void
14631 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
14632 {
14633   if (DECL_VINDEX (func_decl))
14634     {
14635       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14636
14637       if (host_integerp (DECL_VINDEX (func_decl), 0))
14638         add_AT_loc (die, DW_AT_vtable_elem_location,
14639                     new_loc_descr (DW_OP_constu,
14640                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
14641                                    0));
14642
14643       /* GNU extension: Record what type this method came from originally.  */
14644       if (debug_info_level > DINFO_LEVEL_TERSE)
14645         add_AT_die_ref (die, DW_AT_containing_type,
14646                         lookup_type_die (DECL_CONTEXT (func_decl)));
14647     }
14648 }
14649 \f
14650 /* Add source coordinate attributes for the given decl.  */
14651
14652 static void
14653 add_src_coords_attributes (dw_die_ref die, tree decl)
14654 {
14655   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14656
14657   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
14658   add_AT_unsigned (die, DW_AT_decl_line, s.line);
14659 }
14660
14661 /* Add a DW_AT_name attribute and source coordinate attribute for the
14662    given decl, but only if it actually has a name.  */
14663
14664 static void
14665 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
14666 {
14667   tree decl_name;
14668
14669   decl_name = DECL_NAME (decl);
14670   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
14671     {
14672       add_name_attribute (die, dwarf2_name (decl, 0));
14673       if (! DECL_ARTIFICIAL (decl))
14674         add_src_coords_attributes (die, decl);
14675
14676       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
14677           && TREE_PUBLIC (decl)
14678           && !DECL_ABSTRACT (decl)
14679           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
14680           && !is_fortran ())
14681         {
14682           /* Defer until we have an assembler name set.  */
14683           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
14684             {
14685               limbo_die_node *asm_name;
14686
14687               asm_name = GGC_CNEW (limbo_die_node);
14688               asm_name->die = die;
14689               asm_name->created_for = decl;
14690               asm_name->next = deferred_asm_name;
14691               deferred_asm_name = asm_name;
14692             }
14693           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
14694             add_AT_string (die, DW_AT_MIPS_linkage_name,
14695                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
14696         }
14697     }
14698
14699 #ifdef VMS_DEBUGGING_INFO
14700   /* Get the function's name, as described by its RTL.  This may be different
14701      from the DECL_NAME name used in the source file.  */
14702   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
14703     {
14704       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
14705                    XEXP (DECL_RTL (decl), 0));
14706       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
14707     }
14708 #endif
14709 }
14710
14711 /* Push a new declaration scope.  */
14712
14713 static void
14714 push_decl_scope (tree scope)
14715 {
14716   VEC_safe_push (tree, gc, decl_scope_table, scope);
14717 }
14718
14719 /* Pop a declaration scope.  */
14720
14721 static inline void
14722 pop_decl_scope (void)
14723 {
14724   VEC_pop (tree, decl_scope_table);
14725 }
14726
14727 /* Return the DIE for the scope that immediately contains this type.
14728    Non-named types get global scope.  Named types nested in other
14729    types get their containing scope if it's open, or global scope
14730    otherwise.  All other types (i.e. function-local named types) get
14731    the current active scope.  */
14732
14733 static dw_die_ref
14734 scope_die_for (tree t, dw_die_ref context_die)
14735 {
14736   dw_die_ref scope_die = NULL;
14737   tree containing_scope;
14738   int i;
14739
14740   /* Non-types always go in the current scope.  */
14741   gcc_assert (TYPE_P (t));
14742
14743   containing_scope = TYPE_CONTEXT (t);
14744
14745   /* Use the containing namespace if it was passed in (for a declaration).  */
14746   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
14747     {
14748       if (context_die == lookup_decl_die (containing_scope))
14749         /* OK */;
14750       else
14751         containing_scope = NULL_TREE;
14752     }
14753
14754   /* Ignore function type "scopes" from the C frontend.  They mean that
14755      a tagged type is local to a parmlist of a function declarator, but
14756      that isn't useful to DWARF.  */
14757   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
14758     containing_scope = NULL_TREE;
14759
14760   if (containing_scope == NULL_TREE)
14761     scope_die = comp_unit_die;
14762   else if (TYPE_P (containing_scope))
14763     {
14764       /* For types, we can just look up the appropriate DIE.  But
14765          first we check to see if we're in the middle of emitting it
14766          so we know where the new DIE should go.  */
14767       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
14768         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
14769           break;
14770
14771       if (i < 0)
14772         {
14773           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
14774                       || TREE_ASM_WRITTEN (containing_scope));
14775
14776           /* If none of the current dies are suitable, we get file scope.  */
14777           scope_die = comp_unit_die;
14778         }
14779       else
14780         scope_die = lookup_type_die (containing_scope);
14781     }
14782   else
14783     scope_die = context_die;
14784
14785   return scope_die;
14786 }
14787
14788 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
14789
14790 static inline int
14791 local_scope_p (dw_die_ref context_die)
14792 {
14793   for (; context_die; context_die = context_die->die_parent)
14794     if (context_die->die_tag == DW_TAG_inlined_subroutine
14795         || context_die->die_tag == DW_TAG_subprogram)
14796       return 1;
14797
14798   return 0;
14799 }
14800
14801 /* Returns nonzero if CONTEXT_DIE is a class.  */
14802
14803 static inline int
14804 class_scope_p (dw_die_ref context_die)
14805 {
14806   return (context_die
14807           && (context_die->die_tag == DW_TAG_structure_type
14808               || context_die->die_tag == DW_TAG_class_type
14809               || context_die->die_tag == DW_TAG_interface_type
14810               || context_die->die_tag == DW_TAG_union_type));
14811 }
14812
14813 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
14814    whether or not to treat a DIE in this context as a declaration.  */
14815
14816 static inline int
14817 class_or_namespace_scope_p (dw_die_ref context_die)
14818 {
14819   return (class_scope_p (context_die)
14820           || (context_die && context_die->die_tag == DW_TAG_namespace));
14821 }
14822
14823 /* Many forms of DIEs require a "type description" attribute.  This
14824    routine locates the proper "type descriptor" die for the type given
14825    by 'type', and adds a DW_AT_type attribute below the given die.  */
14826
14827 static void
14828 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
14829                     int decl_volatile, dw_die_ref context_die)
14830 {
14831   enum tree_code code  = TREE_CODE (type);
14832   dw_die_ref type_die  = NULL;
14833
14834   /* ??? If this type is an unnamed subrange type of an integral, floating-point
14835      or fixed-point type, use the inner type.  This is because we have no
14836      support for unnamed types in base_type_die.  This can happen if this is
14837      an Ada subrange type.  Correct solution is emit a subrange type die.  */
14838   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
14839       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
14840     type = TREE_TYPE (type), code = TREE_CODE (type);
14841
14842   if (code == ERROR_MARK
14843       /* Handle a special case.  For functions whose return type is void, we
14844          generate *no* type attribute.  (Note that no object may have type
14845          `void', so this only applies to function return types).  */
14846       || code == VOID_TYPE)
14847     return;
14848
14849   type_die = modified_type_die (type,
14850                                 decl_const || TYPE_READONLY (type),
14851                                 decl_volatile || TYPE_VOLATILE (type),
14852                                 context_die);
14853
14854   if (type_die != NULL)
14855     add_AT_die_ref (object_die, DW_AT_type, type_die);
14856 }
14857
14858 /* Given an object die, add the calling convention attribute for the
14859    function call type.  */
14860 static void
14861 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
14862 {
14863   enum dwarf_calling_convention value = DW_CC_normal;
14864
14865   value = ((enum dwarf_calling_convention)
14866            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
14867
14868   /* DWARF doesn't provide a way to identify a program's source-level
14869      entry point.  DW_AT_calling_convention attributes are only meant
14870      to describe functions' calling conventions.  However, lacking a
14871      better way to signal the Fortran main program, we use this for the
14872      time being, following existing custom.  */
14873   if (is_fortran ()
14874       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
14875     value = DW_CC_program;
14876
14877   /* Only add the attribute if the backend requests it, and
14878      is not DW_CC_normal.  */
14879   if (value && (value != DW_CC_normal))
14880     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
14881 }
14882
14883 /* Given a tree pointer to a struct, class, union, or enum type node, return
14884    a pointer to the (string) tag name for the given type, or zero if the type
14885    was declared without a tag.  */
14886
14887 static const char *
14888 type_tag (const_tree type)
14889 {
14890   const char *name = 0;
14891
14892   if (TYPE_NAME (type) != 0)
14893     {
14894       tree t = 0;
14895
14896       /* Find the IDENTIFIER_NODE for the type name.  */
14897       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
14898         t = TYPE_NAME (type);
14899
14900       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
14901          a TYPE_DECL node, regardless of whether or not a `typedef' was
14902          involved.  */
14903       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14904                && ! DECL_IGNORED_P (TYPE_NAME (type)))
14905         {
14906           /* We want to be extra verbose.  Don't call dwarf_name if
14907              DECL_NAME isn't set.  The default hook for decl_printable_name
14908              doesn't like that, and in this context it's correct to return
14909              0, instead of "<anonymous>" or the like.  */
14910           if (DECL_NAME (TYPE_NAME (type)))
14911             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
14912         }
14913
14914       /* Now get the name as a string, or invent one.  */
14915       if (!name && t != 0)
14916         name = IDENTIFIER_POINTER (t);
14917     }
14918
14919   return (name == 0 || *name == '\0') ? 0 : name;
14920 }
14921
14922 /* Return the type associated with a data member, make a special check
14923    for bit field types.  */
14924
14925 static inline tree
14926 member_declared_type (const_tree member)
14927 {
14928   return (DECL_BIT_FIELD_TYPE (member)
14929           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
14930 }
14931
14932 /* Get the decl's label, as described by its RTL. This may be different
14933    from the DECL_NAME name used in the source file.  */
14934
14935 #if 0
14936 static const char *
14937 decl_start_label (tree decl)
14938 {
14939   rtx x;
14940   const char *fnname;
14941
14942   x = DECL_RTL (decl);
14943   gcc_assert (MEM_P (x));
14944
14945   x = XEXP (x, 0);
14946   gcc_assert (GET_CODE (x) == SYMBOL_REF);
14947
14948   fnname = XSTR (x, 0);
14949   return fnname;
14950 }
14951 #endif
14952 \f
14953 /* These routines generate the internal representation of the DIE's for
14954    the compilation unit.  Debugging information is collected by walking
14955    the declaration trees passed in from dwarf2out_decl().  */
14956
14957 static void
14958 gen_array_type_die (tree type, dw_die_ref context_die)
14959 {
14960   dw_die_ref scope_die = scope_die_for (type, context_die);
14961   dw_die_ref array_die;
14962
14963   /* GNU compilers represent multidimensional array types as sequences of one
14964      dimensional array types whose element types are themselves array types.
14965      We sometimes squish that down to a single array_type DIE with multiple
14966      subscripts in the Dwarf debugging info.  The draft Dwarf specification
14967      say that we are allowed to do this kind of compression in C, because
14968      there is no difference between an array of arrays and a multidimensional
14969      array.  We don't do this for Ada to remain as close as possible to the
14970      actual representation, which is especially important against the language
14971      flexibilty wrt arrays of variable size.  */
14972
14973   bool collapse_nested_arrays = !is_ada ();
14974   tree element_type;
14975
14976   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
14977      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
14978   if (TYPE_STRING_FLAG (type)
14979       && TREE_CODE (type) == ARRAY_TYPE
14980       && is_fortran ()
14981       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
14982     {
14983       HOST_WIDE_INT size;
14984
14985       array_die = new_die (DW_TAG_string_type, scope_die, type);
14986       add_name_attribute (array_die, type_tag (type));
14987       equate_type_number_to_die (type, array_die);
14988       size = int_size_in_bytes (type);
14989       if (size >= 0)
14990         add_AT_unsigned (array_die, DW_AT_byte_size, size);
14991       else if (TYPE_DOMAIN (type) != NULL_TREE
14992                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
14993                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
14994         {
14995           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
14996           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
14997
14998           size = int_size_in_bytes (TREE_TYPE (szdecl));
14999           if (loc && size > 0)
15000             {
15001               add_AT_location_description (array_die, DW_AT_string_length, loc);
15002               if (size != DWARF2_ADDR_SIZE)
15003                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15004             }
15005         }
15006       return;
15007     }
15008
15009   /* ??? The SGI dwarf reader fails for array of array of enum types
15010      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
15011      array type comes before the outer array type.  We thus call gen_type_die
15012      before we new_die and must prevent nested array types collapsing for this
15013      target.  */
15014
15015 #ifdef MIPS_DEBUGGING_INFO
15016   gen_type_die (TREE_TYPE (type), context_die);
15017   collapse_nested_arrays = false;
15018 #endif
15019
15020   array_die = new_die (DW_TAG_array_type, scope_die, type);
15021   add_name_attribute (array_die, type_tag (type));
15022   equate_type_number_to_die (type, array_die);
15023
15024   if (TREE_CODE (type) == VECTOR_TYPE)
15025     {
15026       /* The frontend feeds us a representation for the vector as a struct
15027          containing an array.  Pull out the array type.  */
15028       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
15029       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
15030     }
15031
15032   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
15033   if (is_fortran ()
15034       && TREE_CODE (type) == ARRAY_TYPE
15035       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
15036       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
15037     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
15038
15039 #if 0
15040   /* We default the array ordering.  SDB will probably do
15041      the right things even if DW_AT_ordering is not present.  It's not even
15042      an issue until we start to get into multidimensional arrays anyway.  If
15043      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
15044      then we'll have to put the DW_AT_ordering attribute back in.  (But if
15045      and when we find out that we need to put these in, we will only do so
15046      for multidimensional arrays.  */
15047   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
15048 #endif
15049
15050 #ifdef MIPS_DEBUGGING_INFO
15051   /* The SGI compilers handle arrays of unknown bound by setting
15052      AT_declaration and not emitting any subrange DIEs.  */
15053   if (! TYPE_DOMAIN (type))
15054     add_AT_flag (array_die, DW_AT_declaration, 1);
15055   else
15056 #endif
15057     add_subscript_info (array_die, type, collapse_nested_arrays);
15058
15059   /* Add representation of the type of the elements of this array type and
15060      emit the corresponding DIE if we haven't done it already.  */  
15061   element_type = TREE_TYPE (type);
15062   if (collapse_nested_arrays)
15063     while (TREE_CODE (element_type) == ARRAY_TYPE)
15064       {
15065         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
15066           break;
15067         element_type = TREE_TYPE (element_type);
15068       }
15069
15070 #ifndef MIPS_DEBUGGING_INFO
15071   gen_type_die (element_type, context_die);
15072 #endif
15073
15074   add_type_attribute (array_die, element_type, 0, 0, context_die);
15075
15076   if (get_AT (array_die, DW_AT_name))
15077     add_pubtype (type, array_die);
15078 }
15079
15080 static dw_loc_descr_ref
15081 descr_info_loc (tree val, tree base_decl)
15082 {
15083   HOST_WIDE_INT size;
15084   dw_loc_descr_ref loc, loc2;
15085   enum dwarf_location_atom op;
15086
15087   if (val == base_decl)
15088     return new_loc_descr (DW_OP_push_object_address, 0, 0);
15089
15090   switch (TREE_CODE (val))
15091     {
15092     CASE_CONVERT:
15093       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15094     case VAR_DECL:
15095       return loc_descriptor_from_tree (val, 0);
15096     case INTEGER_CST:
15097       if (host_integerp (val, 0))
15098         return int_loc_descriptor (tree_low_cst (val, 0));
15099       break;
15100     case INDIRECT_REF:
15101       size = int_size_in_bytes (TREE_TYPE (val));
15102       if (size < 0)
15103         break;
15104       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15105       if (!loc)
15106         break;
15107       if (size == DWARF2_ADDR_SIZE)
15108         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
15109       else
15110         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
15111       return loc;
15112     case POINTER_PLUS_EXPR:
15113     case PLUS_EXPR:
15114       if (host_integerp (TREE_OPERAND (val, 1), 1)
15115           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
15116              < 16384)
15117         {
15118           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15119           if (!loc)
15120             break;
15121           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
15122         }
15123       else
15124         {
15125           op = DW_OP_plus;
15126         do_binop:
15127           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15128           if (!loc)
15129             break;
15130           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
15131           if (!loc2)
15132             break;
15133           add_loc_descr (&loc, loc2);
15134           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
15135         }
15136       return loc;
15137     case MINUS_EXPR:
15138       op = DW_OP_minus;
15139       goto do_binop;
15140     case MULT_EXPR:
15141       op = DW_OP_mul;
15142       goto do_binop;
15143     case EQ_EXPR:
15144       op = DW_OP_eq;
15145       goto do_binop;
15146     case NE_EXPR:
15147       op = DW_OP_ne;
15148       goto do_binop;
15149     default:
15150       break;
15151     }
15152   return NULL;
15153 }
15154
15155 static void
15156 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
15157                       tree val, tree base_decl)
15158 {
15159   dw_loc_descr_ref loc;
15160
15161   if (host_integerp (val, 0))
15162     {
15163       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
15164       return;
15165     }
15166
15167   loc = descr_info_loc (val, base_decl);
15168   if (!loc)
15169     return;
15170
15171   add_AT_loc (die, attr, loc);
15172 }
15173
15174 /* This routine generates DIE for array with hidden descriptor, details
15175    are filled into *info by a langhook.  */
15176
15177 static void
15178 gen_descr_array_type_die (tree type, struct array_descr_info *info,
15179                           dw_die_ref context_die)
15180 {
15181   dw_die_ref scope_die = scope_die_for (type, context_die);
15182   dw_die_ref array_die;
15183   int dim;
15184
15185   array_die = new_die (DW_TAG_array_type, scope_die, type);
15186   add_name_attribute (array_die, type_tag (type));
15187   equate_type_number_to_die (type, array_die);
15188
15189   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
15190   if (is_fortran ()
15191       && info->ndimensions >= 2)
15192     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
15193
15194   if (info->data_location)
15195     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
15196                           info->base_decl);
15197   if (info->associated)
15198     add_descr_info_field (array_die, DW_AT_associated, info->associated,
15199                           info->base_decl);
15200   if (info->allocated)
15201     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
15202                           info->base_decl);
15203
15204   for (dim = 0; dim < info->ndimensions; dim++)
15205     {
15206       dw_die_ref subrange_die
15207         = new_die (DW_TAG_subrange_type, array_die, NULL);
15208
15209       if (info->dimen[dim].lower_bound)
15210         {
15211           /* If it is the default value, omit it.  */
15212           if ((is_c_family () || is_java ())
15213               && integer_zerop (info->dimen[dim].lower_bound))
15214             ;
15215           else if (is_fortran ()
15216                    && integer_onep (info->dimen[dim].lower_bound))
15217             ;
15218           else
15219             add_descr_info_field (subrange_die, DW_AT_lower_bound,
15220                                   info->dimen[dim].lower_bound,
15221                                   info->base_decl);
15222         }
15223       if (info->dimen[dim].upper_bound)
15224         add_descr_info_field (subrange_die, DW_AT_upper_bound,
15225                               info->dimen[dim].upper_bound,
15226                               info->base_decl);
15227       if (info->dimen[dim].stride)
15228         add_descr_info_field (subrange_die, DW_AT_byte_stride,
15229                               info->dimen[dim].stride,
15230                               info->base_decl);
15231     }
15232
15233   gen_type_die (info->element_type, context_die);
15234   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
15235
15236   if (get_AT (array_die, DW_AT_name))
15237     add_pubtype (type, array_die);
15238 }
15239
15240 #if 0
15241 static void
15242 gen_entry_point_die (tree decl, dw_die_ref context_die)
15243 {
15244   tree origin = decl_ultimate_origin (decl);
15245   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
15246
15247   if (origin != NULL)
15248     add_abstract_origin_attribute (decl_die, origin);
15249   else
15250     {
15251       add_name_and_src_coords_attributes (decl_die, decl);
15252       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
15253                           0, 0, context_die);
15254     }
15255
15256   if (DECL_ABSTRACT (decl))
15257     equate_decl_number_to_die (decl, decl_die);
15258   else
15259     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
15260 }
15261 #endif
15262
15263 /* Walk through the list of incomplete types again, trying once more to
15264    emit full debugging info for them.  */
15265
15266 static void
15267 retry_incomplete_types (void)
15268 {
15269   int i;
15270
15271   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
15272     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
15273 }
15274
15275 /* Determine what tag to use for a record type.  */
15276
15277 static enum dwarf_tag
15278 record_type_tag (tree type)
15279 {
15280   if (! lang_hooks.types.classify_record)
15281     return DW_TAG_structure_type;
15282
15283   switch (lang_hooks.types.classify_record (type))
15284     {
15285     case RECORD_IS_STRUCT:
15286       return DW_TAG_structure_type;
15287
15288     case RECORD_IS_CLASS:
15289       return DW_TAG_class_type;
15290
15291     case RECORD_IS_INTERFACE:
15292       if (dwarf_version >= 3 || !dwarf_strict)
15293         return DW_TAG_interface_type;
15294       return DW_TAG_structure_type;
15295
15296     default:
15297       gcc_unreachable ();
15298     }
15299 }
15300
15301 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
15302    include all of the information about the enumeration values also. Each
15303    enumerated type name/value is listed as a child of the enumerated type
15304    DIE.  */
15305
15306 static dw_die_ref
15307 gen_enumeration_type_die (tree type, dw_die_ref context_die)
15308 {
15309   dw_die_ref type_die = lookup_type_die (type);
15310
15311   if (type_die == NULL)
15312     {
15313       type_die = new_die (DW_TAG_enumeration_type,
15314                           scope_die_for (type, context_die), type);
15315       equate_type_number_to_die (type, type_die);
15316       add_name_attribute (type_die, type_tag (type));
15317     }
15318   else if (! TYPE_SIZE (type))
15319     return type_die;
15320   else
15321     remove_AT (type_die, DW_AT_declaration);
15322
15323   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
15324      given enum type is incomplete, do not generate the DW_AT_byte_size
15325      attribute or the DW_AT_element_list attribute.  */
15326   if (TYPE_SIZE (type))
15327     {
15328       tree link;
15329
15330       TREE_ASM_WRITTEN (type) = 1;
15331       add_byte_size_attribute (type_die, type);
15332       if (TYPE_STUB_DECL (type) != NULL_TREE)
15333         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
15334
15335       /* If the first reference to this type was as the return type of an
15336          inline function, then it may not have a parent.  Fix this now.  */
15337       if (type_die->die_parent == NULL)
15338         add_child_die (scope_die_for (type, context_die), type_die);
15339
15340       for (link = TYPE_VALUES (type);
15341            link != NULL; link = TREE_CHAIN (link))
15342         {
15343           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
15344           tree value = TREE_VALUE (link);
15345
15346           add_name_attribute (enum_die,
15347                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
15348
15349           if (TREE_CODE (value) == CONST_DECL)
15350             value = DECL_INITIAL (value);
15351
15352           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
15353             /* DWARF2 does not provide a way of indicating whether or
15354                not enumeration constants are signed or unsigned.  GDB
15355                always assumes the values are signed, so we output all
15356                values as if they were signed.  That means that
15357                enumeration constants with very large unsigned values
15358                will appear to have negative values in the debugger.  */
15359             add_AT_int (enum_die, DW_AT_const_value,
15360                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
15361         }
15362     }
15363   else
15364     add_AT_flag (type_die, DW_AT_declaration, 1);
15365
15366   if (get_AT (type_die, DW_AT_name))
15367     add_pubtype (type, type_die);
15368
15369   return type_die;
15370 }
15371
15372 /* Generate a DIE to represent either a real live formal parameter decl or to
15373    represent just the type of some formal parameter position in some function
15374    type.
15375
15376    Note that this routine is a bit unusual because its argument may be a
15377    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
15378    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
15379    node.  If it's the former then this function is being called to output a
15380    DIE to represent a formal parameter object (or some inlining thereof).  If
15381    it's the latter, then this function is only being called to output a
15382    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
15383    argument type of some subprogram type.  */
15384
15385 static dw_die_ref
15386 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
15387 {
15388   tree node_or_origin = node ? node : origin;
15389   dw_die_ref parm_die
15390     = new_die (DW_TAG_formal_parameter, context_die, node);
15391
15392   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
15393     {
15394     case tcc_declaration:
15395       if (!origin)
15396         origin = decl_ultimate_origin (node);
15397       if (origin != NULL)
15398         add_abstract_origin_attribute (parm_die, origin);
15399       else
15400         {
15401           tree type = TREE_TYPE (node);
15402           add_name_and_src_coords_attributes (parm_die, node);
15403           if (decl_by_reference_p (node))
15404             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
15405                                 context_die);
15406           else
15407             add_type_attribute (parm_die, type,
15408                                 TREE_READONLY (node),
15409                                 TREE_THIS_VOLATILE (node),
15410                                 context_die);
15411           if (DECL_ARTIFICIAL (node))
15412             add_AT_flag (parm_die, DW_AT_artificial, 1);
15413         }
15414
15415       if (node && node != origin)
15416         equate_decl_number_to_die (node, parm_die);
15417       if (! DECL_ABSTRACT (node_or_origin))
15418         add_location_or_const_value_attribute (parm_die, node_or_origin,
15419                                                DW_AT_location);
15420
15421       break;
15422
15423     case tcc_type:
15424       /* We were called with some kind of a ..._TYPE node.  */
15425       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
15426       break;
15427
15428     default:
15429       gcc_unreachable ();
15430     }
15431
15432   return parm_die;
15433 }
15434
15435 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
15436    at the end of an (ANSI prototyped) formal parameters list.  */
15437
15438 static void
15439 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
15440 {
15441   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
15442 }
15443
15444 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
15445    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
15446    parameters as specified in some function type specification (except for
15447    those which appear as part of a function *definition*).  */
15448
15449 static void
15450 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
15451 {
15452   tree link;
15453   tree formal_type = NULL;
15454   tree first_parm_type;
15455   tree arg;
15456
15457   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
15458     {
15459       arg = DECL_ARGUMENTS (function_or_method_type);
15460       function_or_method_type = TREE_TYPE (function_or_method_type);
15461     }
15462   else
15463     arg = NULL_TREE;
15464
15465   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
15466
15467   /* Make our first pass over the list of formal parameter types and output a
15468      DW_TAG_formal_parameter DIE for each one.  */
15469   for (link = first_parm_type; link; )
15470     {
15471       dw_die_ref parm_die;
15472
15473       formal_type = TREE_VALUE (link);
15474       if (formal_type == void_type_node)
15475         break;
15476
15477       /* Output a (nameless) DIE to represent the formal parameter itself.  */
15478       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
15479       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
15480            && link == first_parm_type)
15481           || (arg && DECL_ARTIFICIAL (arg)))
15482         add_AT_flag (parm_die, DW_AT_artificial, 1);
15483
15484       link = TREE_CHAIN (link);
15485       if (arg)
15486         arg = TREE_CHAIN (arg);
15487     }
15488
15489   /* If this function type has an ellipsis, add a
15490      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
15491   if (formal_type != void_type_node)
15492     gen_unspecified_parameters_die (function_or_method_type, context_die);
15493
15494   /* Make our second (and final) pass over the list of formal parameter types
15495      and output DIEs to represent those types (as necessary).  */
15496   for (link = TYPE_ARG_TYPES (function_or_method_type);
15497        link && TREE_VALUE (link);
15498        link = TREE_CHAIN (link))
15499     gen_type_die (TREE_VALUE (link), context_die);
15500 }
15501
15502 /* We want to generate the DIE for TYPE so that we can generate the
15503    die for MEMBER, which has been defined; we will need to refer back
15504    to the member declaration nested within TYPE.  If we're trying to
15505    generate minimal debug info for TYPE, processing TYPE won't do the
15506    trick; we need to attach the member declaration by hand.  */
15507
15508 static void
15509 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
15510 {
15511   gen_type_die (type, context_die);
15512
15513   /* If we're trying to avoid duplicate debug info, we may not have
15514      emitted the member decl for this function.  Emit it now.  */
15515   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
15516       && ! lookup_decl_die (member))
15517     {
15518       dw_die_ref type_die;
15519       gcc_assert (!decl_ultimate_origin (member));
15520
15521       push_decl_scope (type);
15522       type_die = lookup_type_die (type);
15523       if (TREE_CODE (member) == FUNCTION_DECL)
15524         gen_subprogram_die (member, type_die);
15525       else if (TREE_CODE (member) == FIELD_DECL)
15526         {
15527           /* Ignore the nameless fields that are used to skip bits but handle
15528              C++ anonymous unions and structs.  */
15529           if (DECL_NAME (member) != NULL_TREE
15530               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
15531               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
15532             {
15533               gen_type_die (member_declared_type (member), type_die);
15534               gen_field_die (member, type_die);
15535             }
15536         }
15537       else
15538         gen_variable_die (member, NULL_TREE, type_die);
15539
15540       pop_decl_scope ();
15541     }
15542 }
15543
15544 /* Generate the DWARF2 info for the "abstract" instance of a function which we
15545    may later generate inlined and/or out-of-line instances of.  */
15546
15547 static void
15548 dwarf2out_abstract_function (tree decl)
15549 {
15550   dw_die_ref old_die;
15551   tree save_fn;
15552   tree context;
15553   int was_abstract = DECL_ABSTRACT (decl);
15554   htab_t old_decl_loc_table;
15555
15556   /* Make sure we have the actual abstract inline, not a clone.  */
15557   decl = DECL_ORIGIN (decl);
15558
15559   old_die = lookup_decl_die (decl);
15560   if (old_die && get_AT (old_die, DW_AT_inline))
15561     /* We've already generated the abstract instance.  */
15562     return;
15563
15564   /* We can be called while recursively when seeing block defining inlined subroutine
15565      DIE.  Be sure to not clobber the outer location table nor use it or we would
15566      get locations in abstract instantces.  */
15567   old_decl_loc_table = decl_loc_table;
15568   decl_loc_table = NULL;
15569
15570   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
15571      we don't get confused by DECL_ABSTRACT.  */
15572   if (debug_info_level > DINFO_LEVEL_TERSE)
15573     {
15574       context = decl_class_context (decl);
15575       if (context)
15576         gen_type_die_for_member
15577           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
15578     }
15579
15580   /* Pretend we've just finished compiling this function.  */
15581   save_fn = current_function_decl;
15582   current_function_decl = decl;
15583   push_cfun (DECL_STRUCT_FUNCTION (decl));
15584
15585   set_decl_abstract_flags (decl, 1);
15586   dwarf2out_decl (decl);
15587   if (! was_abstract)
15588     set_decl_abstract_flags (decl, 0);
15589
15590   current_function_decl = save_fn;
15591   decl_loc_table = old_decl_loc_table;
15592   pop_cfun ();
15593 }
15594
15595 /* Helper function of premark_used_types() which gets called through
15596    htab_traverse_resize().
15597
15598    Marks the DIE of a given type in *SLOT as perennial, so it never gets
15599    marked as unused by prune_unused_types.  */
15600 static int
15601 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
15602 {
15603   tree type;
15604   dw_die_ref die;
15605
15606   type = (tree) *slot;
15607   die = lookup_type_die (type);
15608   if (die != NULL)
15609     die->die_perennial_p = 1;
15610   return 1;
15611 }
15612
15613 /* Mark all members of used_types_hash as perennial.  */
15614 static void
15615 premark_used_types (void)
15616 {
15617   if (cfun && cfun->used_types_hash)
15618     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
15619 }
15620
15621 /* Generate a DIE to represent a declared function (either file-scope or
15622    block-local).  */
15623
15624 static void
15625 gen_subprogram_die (tree decl, dw_die_ref context_die)
15626 {
15627   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15628   tree origin = decl_ultimate_origin (decl);
15629   dw_die_ref subr_die;
15630   tree fn_arg_types;
15631   tree outer_scope;
15632   dw_die_ref old_die = lookup_decl_die (decl);
15633   int declaration = (current_function_decl != decl
15634                      || class_or_namespace_scope_p (context_die));
15635
15636   premark_used_types ();
15637
15638   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
15639      started to generate the abstract instance of an inline, decided to output
15640      its containing class, and proceeded to emit the declaration of the inline
15641      from the member list for the class.  If so, DECLARATION takes priority;
15642      we'll get back to the abstract instance when done with the class.  */
15643
15644   /* The class-scope declaration DIE must be the primary DIE.  */
15645   if (origin && declaration && class_or_namespace_scope_p (context_die))
15646     {
15647       origin = NULL;
15648       gcc_assert (!old_die);
15649     }
15650
15651   /* Now that the C++ front end lazily declares artificial member fns, we
15652      might need to retrofit the declaration into its class.  */
15653   if (!declaration && !origin && !old_die
15654       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
15655       && !class_or_namespace_scope_p (context_die)
15656       && debug_info_level > DINFO_LEVEL_TERSE)
15657     old_die = force_decl_die (decl);
15658
15659   if (origin != NULL)
15660     {
15661       gcc_assert (!declaration || local_scope_p (context_die));
15662
15663       /* Fixup die_parent for the abstract instance of a nested
15664          inline function.  */
15665       if (old_die && old_die->die_parent == NULL)
15666         add_child_die (context_die, old_die);
15667
15668       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15669       add_abstract_origin_attribute (subr_die, origin);
15670     }
15671   else if (old_die)
15672     {
15673       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
15674       struct dwarf_file_data * file_index = lookup_filename (s.file);
15675
15676       if (!get_AT_flag (old_die, DW_AT_declaration)
15677           /* We can have a normal definition following an inline one in the
15678              case of redefinition of GNU C extern inlines.
15679              It seems reasonable to use AT_specification in this case.  */
15680           && !get_AT (old_die, DW_AT_inline))
15681         {
15682           /* Detect and ignore this case, where we are trying to output
15683              something we have already output.  */
15684           return;
15685         }
15686
15687       /* If the definition comes from the same place as the declaration,
15688          maybe use the old DIE.  We always want the DIE for this function
15689          that has the *_pc attributes to be under comp_unit_die so the
15690          debugger can find it.  We also need to do this for abstract
15691          instances of inlines, since the spec requires the out-of-line copy
15692          to have the same parent.  For local class methods, this doesn't
15693          apply; we just use the old DIE.  */
15694       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
15695           && (DECL_ARTIFICIAL (decl)
15696               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
15697                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
15698                       == (unsigned) s.line))))
15699         {
15700           subr_die = old_die;
15701
15702           /* Clear out the declaration attribute and the formal parameters.
15703              Do not remove all children, because it is possible that this
15704              declaration die was forced using force_decl_die(). In such
15705              cases die that forced declaration die (e.g. TAG_imported_module)
15706              is one of the children that we do not want to remove.  */
15707           remove_AT (subr_die, DW_AT_declaration);
15708           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
15709         }
15710       else
15711         {
15712           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15713           add_AT_specification (subr_die, old_die);
15714           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
15715             add_AT_file (subr_die, DW_AT_decl_file, file_index);
15716           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
15717             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
15718         }
15719     }
15720   else
15721     {
15722       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15723
15724       if (TREE_PUBLIC (decl))
15725         add_AT_flag (subr_die, DW_AT_external, 1);
15726
15727       add_name_and_src_coords_attributes (subr_die, decl);
15728       if (debug_info_level > DINFO_LEVEL_TERSE)
15729         {
15730           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
15731           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
15732                               0, 0, context_die);
15733         }
15734
15735       add_pure_or_virtual_attribute (subr_die, decl);
15736       if (DECL_ARTIFICIAL (decl))
15737         add_AT_flag (subr_die, DW_AT_artificial, 1);
15738
15739       if (TREE_PROTECTED (decl))
15740         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
15741       else if (TREE_PRIVATE (decl))
15742         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
15743     }
15744
15745   if (declaration)
15746     {
15747       if (!old_die || !get_AT (old_die, DW_AT_inline))
15748         {
15749           add_AT_flag (subr_die, DW_AT_declaration, 1);
15750
15751           /* If this is an explicit function declaration then generate
15752              a DW_AT_explicit attribute.  */
15753           if (lang_hooks.decls.function_decl_explicit_p (decl)
15754               && (dwarf_version >= 3 || !dwarf_strict))
15755             add_AT_flag (subr_die, DW_AT_explicit, 1);
15756
15757           /* The first time we see a member function, it is in the context of
15758              the class to which it belongs.  We make sure of this by emitting
15759              the class first.  The next time is the definition, which is
15760              handled above.  The two may come from the same source text.
15761
15762              Note that force_decl_die() forces function declaration die. It is
15763              later reused to represent definition.  */
15764           equate_decl_number_to_die (decl, subr_die);
15765         }
15766     }
15767   else if (DECL_ABSTRACT (decl))
15768     {
15769       if (DECL_DECLARED_INLINE_P (decl))
15770         {
15771           if (cgraph_function_possibly_inlined_p (decl))
15772             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
15773           else
15774             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
15775         }
15776       else
15777         {
15778           if (cgraph_function_possibly_inlined_p (decl))
15779             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
15780           else
15781             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
15782         }
15783
15784       if (DECL_DECLARED_INLINE_P (decl)
15785           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
15786         add_AT_flag (subr_die, DW_AT_artificial, 1);
15787
15788       equate_decl_number_to_die (decl, subr_die);
15789     }
15790   else if (!DECL_EXTERNAL (decl))
15791     {
15792       HOST_WIDE_INT cfa_fb_offset;
15793
15794       if (!old_die || !get_AT (old_die, DW_AT_inline))
15795         equate_decl_number_to_die (decl, subr_die);
15796
15797       if (!flag_reorder_blocks_and_partition)
15798         {
15799           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
15800                                        current_function_funcdef_no);
15801           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
15802           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15803                                        current_function_funcdef_no);
15804           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
15805
15806           add_pubname (decl, subr_die);
15807           add_arange (decl, subr_die);
15808         }
15809       else
15810         {  /* Do nothing for now; maybe need to duplicate die, one for
15811               hot section and one for cold section, then use the hot/cold
15812               section begin/end labels to generate the aranges...  */
15813           /*
15814             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
15815             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
15816             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
15817             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
15818
15819             add_pubname (decl, subr_die);
15820             add_arange (decl, subr_die);
15821             add_arange (decl, subr_die);
15822            */
15823         }
15824
15825 #ifdef MIPS_DEBUGGING_INFO
15826       /* Add a reference to the FDE for this routine.  */
15827       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
15828 #endif
15829
15830       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
15831
15832       /* We define the "frame base" as the function's CFA.  This is more
15833          convenient for several reasons: (1) It's stable across the prologue
15834          and epilogue, which makes it better than just a frame pointer,
15835          (2) With dwarf3, there exists a one-byte encoding that allows us
15836          to reference the .debug_frame data by proxy, but failing that,
15837          (3) We can at least reuse the code inspection and interpretation
15838          code that determines the CFA position at various points in the
15839          function.  */
15840       if (dwarf_version >= 3)
15841         {
15842           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
15843           add_AT_loc (subr_die, DW_AT_frame_base, op);
15844         }
15845       else
15846         {
15847           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
15848           if (list->dw_loc_next)
15849             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
15850           else
15851             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
15852         }
15853
15854       /* Compute a displacement from the "steady-state frame pointer" to
15855          the CFA.  The former is what all stack slots and argument slots
15856          will reference in the rtl; the later is what we've told the
15857          debugger about.  We'll need to adjust all frame_base references
15858          by this displacement.  */
15859       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
15860
15861       if (cfun->static_chain_decl)
15862         add_AT_location_description (subr_die, DW_AT_static_link,
15863                  loc_list_from_tree (cfun->static_chain_decl, 2));
15864     }
15865
15866   /* Generate child dies for template paramaters.  */
15867   if (debug_info_level > DINFO_LEVEL_TERSE)
15868     gen_generic_params_dies (decl);
15869
15870   /* Now output descriptions of the arguments for this function. This gets
15871      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
15872      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
15873      `...' at the end of the formal parameter list.  In order to find out if
15874      there was a trailing ellipsis or not, we must instead look at the type
15875      associated with the FUNCTION_DECL.  This will be a node of type
15876      FUNCTION_TYPE. If the chain of type nodes hanging off of this
15877      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
15878      an ellipsis at the end.  */
15879
15880   /* In the case where we are describing a mere function declaration, all we
15881      need to do here (and all we *can* do here) is to describe the *types* of
15882      its formal parameters.  */
15883   if (debug_info_level <= DINFO_LEVEL_TERSE)
15884     ;
15885   else if (declaration)
15886     gen_formal_types_die (decl, subr_die);
15887   else
15888     {
15889       /* Generate DIEs to represent all known formal parameters.  */
15890       tree arg_decls = DECL_ARGUMENTS (decl);
15891       tree parm;
15892
15893       /* When generating DIEs, generate the unspecified_parameters DIE
15894          instead if we come across the arg "__builtin_va_alist" */
15895       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
15896         if (TREE_CODE (parm) == PARM_DECL)
15897           {
15898             if (DECL_NAME (parm)
15899                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
15900                             "__builtin_va_alist"))
15901               gen_unspecified_parameters_die (parm, subr_die);
15902             else
15903               gen_decl_die (parm, NULL, subr_die);
15904           }
15905
15906       /* Decide whether we need an unspecified_parameters DIE at the end.
15907          There are 2 more cases to do this for: 1) the ansi ... declaration -
15908          this is detectable when the end of the arg list is not a
15909          void_type_node 2) an unprototyped function declaration (not a
15910          definition).  This just means that we have no info about the
15911          parameters at all.  */
15912       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
15913       if (fn_arg_types != NULL)
15914         {
15915           /* This is the prototyped case, check for....  */
15916           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
15917             gen_unspecified_parameters_die (decl, subr_die);
15918         }
15919       else if (DECL_INITIAL (decl) == NULL_TREE)
15920         gen_unspecified_parameters_die (decl, subr_die);
15921     }
15922
15923   /* Output Dwarf info for all of the stuff within the body of the function
15924      (if it has one - it may be just a declaration).  */
15925   outer_scope = DECL_INITIAL (decl);
15926
15927   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
15928      a function.  This BLOCK actually represents the outermost binding contour
15929      for the function, i.e. the contour in which the function's formal
15930      parameters and labels get declared. Curiously, it appears that the front
15931      end doesn't actually put the PARM_DECL nodes for the current function onto
15932      the BLOCK_VARS list for this outer scope, but are strung off of the
15933      DECL_ARGUMENTS list for the function instead.
15934
15935      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
15936      the LABEL_DECL nodes for the function however, and we output DWARF info
15937      for those in decls_for_scope.  Just within the `outer_scope' there will be
15938      a BLOCK node representing the function's outermost pair of curly braces,
15939      and any blocks used for the base and member initializers of a C++
15940      constructor function.  */
15941   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
15942     {
15943       /* Emit a DW_TAG_variable DIE for a named return value.  */
15944       if (DECL_NAME (DECL_RESULT (decl)))
15945         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
15946
15947       current_function_has_inlines = 0;
15948       decls_for_scope (outer_scope, subr_die, 0);
15949
15950 #if 0 && defined (MIPS_DEBUGGING_INFO)
15951       if (current_function_has_inlines)
15952         {
15953           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
15954           if (! comp_unit_has_inlines)
15955             {
15956               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
15957               comp_unit_has_inlines = 1;
15958             }
15959         }
15960 #endif
15961     }
15962   /* Add the calling convention attribute if requested.  */
15963   add_calling_convention_attribute (subr_die, decl);
15964
15965 }
15966
15967 /* Returns a hash value for X (which really is a die_struct).  */
15968
15969 static hashval_t
15970 common_block_die_table_hash (const void *x)
15971 {
15972   const_dw_die_ref d = (const_dw_die_ref) x;
15973   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
15974 }
15975
15976 /* Return nonzero if decl_id and die_parent of die_struct X is the same
15977    as decl_id and die_parent of die_struct Y.  */
15978
15979 static int
15980 common_block_die_table_eq (const void *x, const void *y)
15981 {
15982   const_dw_die_ref d = (const_dw_die_ref) x;
15983   const_dw_die_ref e = (const_dw_die_ref) y;
15984   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
15985 }
15986
15987 /* Generate a DIE to represent a declared data object.
15988    Either DECL or ORIGIN must be non-null.  */
15989
15990 static void
15991 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
15992 {
15993   HOST_WIDE_INT off;
15994   tree com_decl;
15995   tree decl_or_origin = decl ? decl : origin;
15996   dw_die_ref var_die;
15997   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
15998   dw_die_ref origin_die;
15999   int declaration = (DECL_EXTERNAL (decl_or_origin)
16000                      /* If DECL is COMDAT and has not actually been
16001                         emitted, we cannot take its address; there
16002                         might end up being no definition anywhere in
16003                         the program.  For example, consider the C++
16004                         test case:
16005
16006                           template <class T>
16007                           struct S { static const int i = 7; };
16008
16009                           template <class T>
16010                           const int S<T>::i;
16011
16012                           int f() { return S<int>::i; }
16013
16014                         Here, S<int>::i is not DECL_EXTERNAL, but no
16015                         definition is required, so the compiler will
16016                         not emit a definition.  */
16017                      || (TREE_CODE (decl_or_origin) == VAR_DECL
16018                          && DECL_COMDAT (decl_or_origin)
16019                          && !TREE_ASM_WRITTEN (decl_or_origin))
16020                      || class_or_namespace_scope_p (context_die));
16021
16022   if (!origin)
16023     origin = decl_ultimate_origin (decl);
16024
16025   com_decl = fortran_common (decl_or_origin, &off);
16026
16027   /* Symbol in common gets emitted as a child of the common block, in the form
16028      of a data member.  */
16029   if (com_decl)
16030     {
16031       tree field;
16032       dw_die_ref com_die;
16033       dw_loc_list_ref loc;
16034       die_node com_die_arg;
16035
16036       var_die = lookup_decl_die (decl_or_origin);
16037       if (var_die)
16038         {
16039           if (get_AT (var_die, DW_AT_location) == NULL)
16040             {
16041               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
16042               if (loc)
16043                 {
16044                   if (off)
16045                     {
16046                       /* Optimize the common case.  */
16047                       if (single_element_loc_list_p (loc)
16048                           && loc->expr->dw_loc_opc == DW_OP_addr
16049                           && loc->expr->dw_loc_next == NULL
16050                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
16051                              == SYMBOL_REF)
16052                         loc->expr->dw_loc_oprnd1.v.val_addr
16053                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
16054                         else
16055                           loc_list_plus_const (loc, off);
16056                     }
16057                   add_AT_location_description (var_die, DW_AT_location, loc);
16058                   remove_AT (var_die, DW_AT_declaration);
16059                 }
16060             }
16061           return;
16062         }
16063
16064       if (common_block_die_table == NULL)
16065         common_block_die_table
16066           = htab_create_ggc (10, common_block_die_table_hash,
16067                              common_block_die_table_eq, NULL);
16068
16069       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
16070       com_die_arg.decl_id = DECL_UID (com_decl);
16071       com_die_arg.die_parent = context_die;
16072       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
16073       loc = loc_list_from_tree (com_decl, 2);
16074       if (com_die == NULL)
16075         {
16076           const char *cnam
16077             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
16078           void **slot;
16079
16080           com_die = new_die (DW_TAG_common_block, context_die, decl);
16081           add_name_and_src_coords_attributes (com_die, com_decl);
16082           if (loc)
16083             {
16084               add_AT_location_description (com_die, DW_AT_location, loc);
16085               /* Avoid sharing the same loc descriptor between
16086                  DW_TAG_common_block and DW_TAG_variable.  */
16087               loc = loc_list_from_tree (com_decl, 2);
16088             }
16089           else if (DECL_EXTERNAL (decl))
16090             add_AT_flag (com_die, DW_AT_declaration, 1);
16091           add_pubname_string (cnam, com_die); /* ??? needed? */
16092           com_die->decl_id = DECL_UID (com_decl);
16093           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
16094           *slot = (void *) com_die;
16095         }
16096       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
16097         {
16098           add_AT_location_description (com_die, DW_AT_location, loc);
16099           loc = loc_list_from_tree (com_decl, 2);
16100           remove_AT (com_die, DW_AT_declaration);
16101         }
16102       var_die = new_die (DW_TAG_variable, com_die, decl);
16103       add_name_and_src_coords_attributes (var_die, decl);
16104       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
16105                           TREE_THIS_VOLATILE (decl), context_die);
16106       add_AT_flag (var_die, DW_AT_external, 1);
16107       if (loc)
16108         {
16109           if (off)
16110             {
16111               /* Optimize the common case.  */
16112               if (single_element_loc_list_p (loc)
16113                   && loc->expr->dw_loc_opc == DW_OP_addr
16114                   && loc->expr->dw_loc_next == NULL
16115                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
16116                 loc->expr->dw_loc_oprnd1.v.val_addr
16117                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
16118               else
16119                 loc_list_plus_const (loc, off);
16120             }
16121           add_AT_location_description (var_die, DW_AT_location, loc);
16122         }
16123       else if (DECL_EXTERNAL (decl))
16124         add_AT_flag (var_die, DW_AT_declaration, 1);
16125       equate_decl_number_to_die (decl, var_die);
16126       return;
16127     }
16128
16129   /* If the compiler emitted a definition for the DECL declaration
16130      and if we already emitted a DIE for it, don't emit a second
16131      DIE for it again.  */
16132   if (old_die
16133       && declaration
16134       && old_die->die_parent == context_die)
16135     return;
16136
16137   /* For static data members, the declaration in the class is supposed
16138      to have DW_TAG_member tag; the specification should still be
16139      DW_TAG_variable referencing the DW_TAG_member DIE.  */
16140   if (declaration && class_scope_p (context_die))
16141     var_die = new_die (DW_TAG_member, context_die, decl);
16142   else
16143     var_die = new_die (DW_TAG_variable, context_die, decl);
16144
16145   origin_die = NULL;
16146   if (origin != NULL)
16147     origin_die = add_abstract_origin_attribute (var_die, origin);
16148
16149   /* Loop unrolling can create multiple blocks that refer to the same
16150      static variable, so we must test for the DW_AT_declaration flag.
16151
16152      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
16153      copy decls and set the DECL_ABSTRACT flag on them instead of
16154      sharing them.
16155
16156      ??? Duplicated blocks have been rewritten to use .debug_ranges.
16157
16158      ??? The declare_in_namespace support causes us to get two DIEs for one
16159      variable, both of which are declarations.  We want to avoid considering
16160      one to be a specification, so we must test that this DIE is not a
16161      declaration.  */
16162   else if (old_die && TREE_STATIC (decl) && ! declaration
16163            && get_AT_flag (old_die, DW_AT_declaration) == 1)
16164     {
16165       /* This is a definition of a C++ class level static.  */
16166       add_AT_specification (var_die, old_die);
16167       if (DECL_NAME (decl))
16168         {
16169           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16170           struct dwarf_file_data * file_index = lookup_filename (s.file);
16171
16172           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
16173             add_AT_file (var_die, DW_AT_decl_file, file_index);
16174
16175           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
16176             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
16177         }
16178     }
16179   else
16180     {
16181       tree type = TREE_TYPE (decl);
16182
16183       add_name_and_src_coords_attributes (var_die, decl);
16184       if (decl_by_reference_p (decl))
16185         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
16186       else
16187         add_type_attribute (var_die, type, TREE_READONLY (decl),
16188                             TREE_THIS_VOLATILE (decl), context_die);
16189
16190       if (TREE_PUBLIC (decl))
16191         add_AT_flag (var_die, DW_AT_external, 1);
16192
16193       if (DECL_ARTIFICIAL (decl))
16194         add_AT_flag (var_die, DW_AT_artificial, 1);
16195
16196       if (TREE_PROTECTED (decl))
16197         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
16198       else if (TREE_PRIVATE (decl))
16199         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
16200     }
16201
16202   if (declaration)
16203     add_AT_flag (var_die, DW_AT_declaration, 1);
16204
16205   if (decl && (DECL_ABSTRACT (decl) || declaration))
16206     equate_decl_number_to_die (decl, var_die);
16207
16208   if (! declaration
16209       && (! DECL_ABSTRACT (decl_or_origin)
16210           /* Local static vars are shared between all clones/inlines,
16211              so emit DW_AT_location on the abstract DIE if DECL_RTL is
16212              already set.  */
16213           || (TREE_CODE (decl_or_origin) == VAR_DECL
16214               && TREE_STATIC (decl_or_origin)
16215               && DECL_RTL_SET_P (decl_or_origin)))
16216       /* When abstract origin already has DW_AT_location attribute, no need
16217          to add it again.  */
16218       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
16219     {
16220       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
16221           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
16222         defer_location (decl_or_origin, var_die);
16223       else
16224         add_location_or_const_value_attribute (var_die,
16225                                                decl_or_origin,
16226                                                DW_AT_location);
16227       add_pubname (decl_or_origin, var_die);
16228     }
16229   else
16230     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
16231 }
16232
16233 /* Generate a DIE to represent a named constant.  */
16234
16235 static void
16236 gen_const_die (tree decl, dw_die_ref context_die)
16237 {
16238   dw_die_ref const_die;
16239   tree type = TREE_TYPE (decl);
16240
16241   const_die = new_die (DW_TAG_constant, context_die, decl);
16242   add_name_and_src_coords_attributes (const_die, decl);
16243   add_type_attribute (const_die, type, 1, 0, context_die);
16244   if (TREE_PUBLIC (decl))
16245     add_AT_flag (const_die, DW_AT_external, 1);
16246   if (DECL_ARTIFICIAL (decl))
16247     add_AT_flag (const_die, DW_AT_artificial, 1);
16248   tree_add_const_value_attribute_for_decl (const_die, decl);
16249 }
16250
16251 /* Generate a DIE to represent a label identifier.  */
16252
16253 static void
16254 gen_label_die (tree decl, dw_die_ref context_die)
16255 {
16256   tree origin = decl_ultimate_origin (decl);
16257   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
16258   rtx insn;
16259   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16260
16261   if (origin != NULL)
16262     add_abstract_origin_attribute (lbl_die, origin);
16263   else
16264     add_name_and_src_coords_attributes (lbl_die, decl);
16265
16266   if (DECL_ABSTRACT (decl))
16267     equate_decl_number_to_die (decl, lbl_die);
16268   else
16269     {
16270       insn = DECL_RTL_IF_SET (decl);
16271
16272       /* Deleted labels are programmer specified labels which have been
16273          eliminated because of various optimizations.  We still emit them
16274          here so that it is possible to put breakpoints on them.  */
16275       if (insn
16276           && (LABEL_P (insn)
16277               || ((NOTE_P (insn)
16278                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
16279         {
16280           /* When optimization is enabled (via -O) some parts of the compiler
16281              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
16282              represent source-level labels which were explicitly declared by
16283              the user.  This really shouldn't be happening though, so catch
16284              it if it ever does happen.  */
16285           gcc_assert (!INSN_DELETED_P (insn));
16286
16287           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
16288           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
16289         }
16290     }
16291 }
16292
16293 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
16294    attributes to the DIE for a block STMT, to describe where the inlined
16295    function was called from.  This is similar to add_src_coords_attributes.  */
16296
16297 static inline void
16298 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
16299 {
16300   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
16301
16302   if (dwarf_version >= 3 || !dwarf_strict)
16303     {
16304       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
16305       add_AT_unsigned (die, DW_AT_call_line, s.line);
16306     }
16307 }
16308
16309
16310 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
16311    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
16312
16313 static inline void
16314 add_high_low_attributes (tree stmt, dw_die_ref die)
16315 {
16316   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16317
16318   if (BLOCK_FRAGMENT_CHAIN (stmt)
16319       && (dwarf_version >= 3 || !dwarf_strict))
16320     {
16321       tree chain;
16322
16323       if (inlined_function_outer_scope_p (stmt))
16324         {
16325           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
16326                                        BLOCK_NUMBER (stmt));
16327           add_AT_lbl_id (die, DW_AT_entry_pc, label);
16328         }
16329
16330       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
16331
16332       chain = BLOCK_FRAGMENT_CHAIN (stmt);
16333       do
16334         {
16335           add_ranges (chain);
16336           chain = BLOCK_FRAGMENT_CHAIN (chain);
16337         }
16338       while (chain);
16339       add_ranges (NULL);
16340     }
16341   else
16342     {
16343       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
16344                                    BLOCK_NUMBER (stmt));
16345       add_AT_lbl_id (die, DW_AT_low_pc, label);
16346       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
16347                                    BLOCK_NUMBER (stmt));
16348       add_AT_lbl_id (die, DW_AT_high_pc, label);
16349     }
16350 }
16351
16352 /* Generate a DIE for a lexical block.  */
16353
16354 static void
16355 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
16356 {
16357   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
16358
16359   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
16360     add_high_low_attributes (stmt, stmt_die);
16361
16362   decls_for_scope (stmt, stmt_die, depth);
16363 }
16364
16365 /* Generate a DIE for an inlined subprogram.  */
16366
16367 static void
16368 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
16369 {
16370   tree decl;
16371
16372   /* The instance of function that is effectively being inlined shall not
16373      be abstract.  */
16374   gcc_assert (! BLOCK_ABSTRACT (stmt));
16375
16376   decl = block_ultimate_origin (stmt);
16377
16378   /* Emit info for the abstract instance first, if we haven't yet.  We
16379      must emit this even if the block is abstract, otherwise when we
16380      emit the block below (or elsewhere), we may end up trying to emit
16381      a die whose origin die hasn't been emitted, and crashing.  */
16382   dwarf2out_abstract_function (decl);
16383
16384   if (! BLOCK_ABSTRACT (stmt))
16385     {
16386       dw_die_ref subr_die
16387         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
16388
16389       add_abstract_origin_attribute (subr_die, decl);
16390       if (TREE_ASM_WRITTEN (stmt))
16391         add_high_low_attributes (stmt, subr_die);
16392       add_call_src_coords_attributes (stmt, subr_die);
16393
16394       decls_for_scope (stmt, subr_die, depth);
16395       current_function_has_inlines = 1;
16396     }
16397 }
16398
16399 /* Generate a DIE for a field in a record, or structure.  */
16400
16401 static void
16402 gen_field_die (tree decl, dw_die_ref context_die)
16403 {
16404   dw_die_ref decl_die;
16405
16406   if (TREE_TYPE (decl) == error_mark_node)
16407     return;
16408
16409   decl_die = new_die (DW_TAG_member, context_die, decl);
16410   add_name_and_src_coords_attributes (decl_die, decl);
16411   add_type_attribute (decl_die, member_declared_type (decl),
16412                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
16413                       context_die);
16414
16415   if (DECL_BIT_FIELD_TYPE (decl))
16416     {
16417       add_byte_size_attribute (decl_die, decl);
16418       add_bit_size_attribute (decl_die, decl);
16419       add_bit_offset_attribute (decl_die, decl);
16420     }
16421
16422   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
16423     add_data_member_location_attribute (decl_die, decl);
16424
16425   if (DECL_ARTIFICIAL (decl))
16426     add_AT_flag (decl_die, DW_AT_artificial, 1);
16427
16428   if (TREE_PROTECTED (decl))
16429     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
16430   else if (TREE_PRIVATE (decl))
16431     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
16432
16433   /* Equate decl number to die, so that we can look up this decl later on.  */
16434   equate_decl_number_to_die (decl, decl_die);
16435 }
16436
16437 #if 0
16438 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16439    Use modified_type_die instead.
16440    We keep this code here just in case these types of DIEs may be needed to
16441    represent certain things in other languages (e.g. Pascal) someday.  */
16442
16443 static void
16444 gen_pointer_type_die (tree type, dw_die_ref context_die)
16445 {
16446   dw_die_ref ptr_die
16447     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
16448
16449   equate_type_number_to_die (type, ptr_die);
16450   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16451   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16452 }
16453
16454 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16455    Use modified_type_die instead.
16456    We keep this code here just in case these types of DIEs may be needed to
16457    represent certain things in other languages (e.g. Pascal) someday.  */
16458
16459 static void
16460 gen_reference_type_die (tree type, dw_die_ref context_die)
16461 {
16462   dw_die_ref ref_die
16463     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
16464
16465   equate_type_number_to_die (type, ref_die);
16466   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
16467   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16468 }
16469 #endif
16470
16471 /* Generate a DIE for a pointer to a member type.  */
16472
16473 static void
16474 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
16475 {
16476   dw_die_ref ptr_die
16477     = new_die (DW_TAG_ptr_to_member_type,
16478                scope_die_for (type, context_die), type);
16479
16480   equate_type_number_to_die (type, ptr_die);
16481   add_AT_die_ref (ptr_die, DW_AT_containing_type,
16482                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
16483   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16484 }
16485
16486 /* Generate the DIE for the compilation unit.  */
16487
16488 static dw_die_ref
16489 gen_compile_unit_die (const char *filename)
16490 {
16491   dw_die_ref die;
16492   char producer[250];
16493   const char *language_string = lang_hooks.name;
16494   int language;
16495
16496   die = new_die (DW_TAG_compile_unit, NULL, NULL);
16497
16498   if (filename)
16499     {
16500       add_name_attribute (die, filename);
16501       /* Don't add cwd for <built-in>.  */
16502       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
16503         add_comp_dir_attribute (die);
16504     }
16505
16506   sprintf (producer, "%s %s", language_string, version_string);
16507
16508 #ifdef MIPS_DEBUGGING_INFO
16509   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
16510      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
16511      not appear in the producer string, the debugger reaches the conclusion
16512      that the object file is stripped and has no debugging information.
16513      To get the MIPS/SGI debugger to believe that there is debugging
16514      information in the object file, we add a -g to the producer string.  */
16515   if (debug_info_level > DINFO_LEVEL_TERSE)
16516     strcat (producer, " -g");
16517 #endif
16518
16519   add_AT_string (die, DW_AT_producer, producer);
16520
16521   language = DW_LANG_C89;
16522   if (strcmp (language_string, "GNU C++") == 0)
16523     language = DW_LANG_C_plus_plus;
16524   else if (strcmp (language_string, "GNU F77") == 0)
16525     language = DW_LANG_Fortran77;
16526   else if (strcmp (language_string, "GNU Pascal") == 0)
16527     language = DW_LANG_Pascal83;
16528   else if (dwarf_version >= 3 || !dwarf_strict)
16529     {
16530       if (strcmp (language_string, "GNU Ada") == 0)
16531         language = DW_LANG_Ada95;
16532       else if (strcmp (language_string, "GNU Fortran") == 0)
16533         language = DW_LANG_Fortran95;
16534       else if (strcmp (language_string, "GNU Java") == 0)
16535         language = DW_LANG_Java;
16536       else if (strcmp (language_string, "GNU Objective-C") == 0)
16537         language = DW_LANG_ObjC;
16538       else if (strcmp (language_string, "GNU Objective-C++") == 0)
16539         language = DW_LANG_ObjC_plus_plus;
16540     }
16541
16542   add_AT_unsigned (die, DW_AT_language, language);
16543   return die;
16544 }
16545
16546 /* Generate the DIE for a base class.  */
16547
16548 static void
16549 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
16550 {
16551   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
16552
16553   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
16554   add_data_member_location_attribute (die, binfo);
16555
16556   if (BINFO_VIRTUAL_P (binfo))
16557     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16558
16559   if (access == access_public_node)
16560     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16561   else if (access == access_protected_node)
16562     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16563 }
16564
16565 /* Generate a DIE for a class member.  */
16566
16567 static void
16568 gen_member_die (tree type, dw_die_ref context_die)
16569 {
16570   tree member;
16571   tree binfo = TYPE_BINFO (type);
16572   dw_die_ref child;
16573
16574   /* If this is not an incomplete type, output descriptions of each of its
16575      members. Note that as we output the DIEs necessary to represent the
16576      members of this record or union type, we will also be trying to output
16577      DIEs to represent the *types* of those members. However the `type'
16578      function (above) will specifically avoid generating type DIEs for member
16579      types *within* the list of member DIEs for this (containing) type except
16580      for those types (of members) which are explicitly marked as also being
16581      members of this (containing) type themselves.  The g++ front- end can
16582      force any given type to be treated as a member of some other (containing)
16583      type by setting the TYPE_CONTEXT of the given (member) type to point to
16584      the TREE node representing the appropriate (containing) type.  */
16585
16586   /* First output info about the base classes.  */
16587   if (binfo)
16588     {
16589       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
16590       int i;
16591       tree base;
16592
16593       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
16594         gen_inheritance_die (base,
16595                              (accesses ? VEC_index (tree, accesses, i)
16596                               : access_public_node), context_die);
16597     }
16598
16599   /* Now output info about the data members and type members.  */
16600   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
16601     {
16602       /* If we thought we were generating minimal debug info for TYPE
16603          and then changed our minds, some of the member declarations
16604          may have already been defined.  Don't define them again, but
16605          do put them in the right order.  */
16606
16607       child = lookup_decl_die (member);
16608       if (child)
16609         splice_child_die (context_die, child);
16610       else
16611         gen_decl_die (member, NULL, context_die);
16612     }
16613
16614   /* Now output info about the function members (if any).  */
16615   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
16616     {
16617       /* Don't include clones in the member list.  */
16618       if (DECL_ABSTRACT_ORIGIN (member))
16619         continue;
16620
16621       child = lookup_decl_die (member);
16622       if (child)
16623         splice_child_die (context_die, child);
16624       else
16625         gen_decl_die (member, NULL, context_die);
16626     }
16627 }
16628
16629 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
16630    is set, we pretend that the type was never defined, so we only get the
16631    member DIEs needed by later specification DIEs.  */
16632
16633 static void
16634 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
16635                                 enum debug_info_usage usage)
16636 {
16637   dw_die_ref type_die = lookup_type_die (type);
16638   dw_die_ref scope_die = 0;
16639   int nested = 0;
16640   int complete = (TYPE_SIZE (type)
16641                   && (! TYPE_STUB_DECL (type)
16642                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
16643   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
16644   complete = complete && should_emit_struct_debug (type, usage);
16645
16646   if (type_die && ! complete)
16647     return;
16648
16649   if (TYPE_CONTEXT (type) != NULL_TREE
16650       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
16651           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
16652     nested = 1;
16653
16654   scope_die = scope_die_for (type, context_die);
16655
16656   if (! type_die || (nested && scope_die == comp_unit_die))
16657     /* First occurrence of type or toplevel definition of nested class.  */
16658     {
16659       dw_die_ref old_die = type_die;
16660
16661       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
16662                           ? record_type_tag (type) : DW_TAG_union_type,
16663                           scope_die, type);
16664       equate_type_number_to_die (type, type_die);
16665       if (old_die)
16666         add_AT_specification (type_die, old_die);
16667       else
16668         add_name_attribute (type_die, type_tag (type));
16669     }
16670   else
16671     remove_AT (type_die, DW_AT_declaration);
16672
16673   /* Generate child dies for template paramaters.  */
16674   if (debug_info_level > DINFO_LEVEL_TERSE
16675       && COMPLETE_TYPE_P (type))
16676     gen_generic_params_dies (type);
16677
16678   /* If this type has been completed, then give it a byte_size attribute and
16679      then give a list of members.  */
16680   if (complete && !ns_decl)
16681     {
16682       /* Prevent infinite recursion in cases where the type of some member of
16683          this type is expressed in terms of this type itself.  */
16684       TREE_ASM_WRITTEN (type) = 1;
16685       add_byte_size_attribute (type_die, type);
16686       if (TYPE_STUB_DECL (type) != NULL_TREE)
16687         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16688
16689       /* If the first reference to this type was as the return type of an
16690          inline function, then it may not have a parent.  Fix this now.  */
16691       if (type_die->die_parent == NULL)
16692         add_child_die (scope_die, type_die);
16693
16694       push_decl_scope (type);
16695       gen_member_die (type, type_die);
16696       pop_decl_scope ();
16697
16698       /* GNU extension: Record what type our vtable lives in.  */
16699       if (TYPE_VFIELD (type))
16700         {
16701           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
16702
16703           gen_type_die (vtype, context_die);
16704           add_AT_die_ref (type_die, DW_AT_containing_type,
16705                           lookup_type_die (vtype));
16706         }
16707     }
16708   else
16709     {
16710       add_AT_flag (type_die, DW_AT_declaration, 1);
16711
16712       /* We don't need to do this for function-local types.  */
16713       if (TYPE_STUB_DECL (type)
16714           && ! decl_function_context (TYPE_STUB_DECL (type)))
16715         VEC_safe_push (tree, gc, incomplete_types, type);
16716     }
16717
16718   if (get_AT (type_die, DW_AT_name))
16719     add_pubtype (type, type_die);
16720 }
16721
16722 /* Generate a DIE for a subroutine _type_.  */
16723
16724 static void
16725 gen_subroutine_type_die (tree type, dw_die_ref context_die)
16726 {
16727   tree return_type = TREE_TYPE (type);
16728   dw_die_ref subr_die
16729     = new_die (DW_TAG_subroutine_type,
16730                scope_die_for (type, context_die), type);
16731
16732   equate_type_number_to_die (type, subr_die);
16733   add_prototyped_attribute (subr_die, type);
16734   add_type_attribute (subr_die, return_type, 0, 0, context_die);
16735   gen_formal_types_die (type, subr_die);
16736
16737   if (get_AT (subr_die, DW_AT_name))
16738     add_pubtype (type, subr_die);
16739 }
16740
16741 /* Generate a DIE for a type definition.  */
16742
16743 static void
16744 gen_typedef_die (tree decl, dw_die_ref context_die)
16745 {
16746   dw_die_ref type_die;
16747   tree origin;
16748
16749   if (TREE_ASM_WRITTEN (decl))
16750     return;
16751
16752   TREE_ASM_WRITTEN (decl) = 1;
16753   type_die = new_die (DW_TAG_typedef, context_die, decl);
16754   origin = decl_ultimate_origin (decl);
16755   if (origin != NULL)
16756     add_abstract_origin_attribute (type_die, origin);
16757   else
16758     {
16759       tree type;
16760
16761       add_name_and_src_coords_attributes (type_die, decl);
16762       if (DECL_ORIGINAL_TYPE (decl))
16763         {
16764           type = DECL_ORIGINAL_TYPE (decl);
16765
16766           gcc_assert (type != TREE_TYPE (decl));
16767           equate_type_number_to_die (TREE_TYPE (decl), type_die);
16768         }
16769       else
16770         type = TREE_TYPE (decl);
16771
16772       add_type_attribute (type_die, type, TREE_READONLY (decl),
16773                           TREE_THIS_VOLATILE (decl), context_die);
16774     }
16775
16776   if (DECL_ABSTRACT (decl))
16777     equate_decl_number_to_die (decl, type_die);
16778
16779   if (get_AT (type_die, DW_AT_name))
16780     add_pubtype (decl, type_die);
16781 }
16782
16783 /* Generate a type description DIE.  */
16784
16785 static void
16786 gen_type_die_with_usage (tree type, dw_die_ref context_die,
16787                                 enum debug_info_usage usage)
16788 {
16789   int need_pop;
16790   struct array_descr_info info;
16791
16792   if (type == NULL_TREE || type == error_mark_node)
16793     return;
16794
16795   /* If TYPE is a typedef type variant, let's generate debug info
16796      for the parent typedef which TYPE is a type of.  */
16797   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16798       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
16799     {
16800       if (TREE_ASM_WRITTEN (type))
16801         return;
16802
16803       /* Prevent broken recursion; we can't hand off to the same type.  */
16804       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
16805
16806       /* Use the DIE of the containing namespace as the parent DIE of
16807          the type description DIE we want to generate.  */
16808       if (DECL_CONTEXT (TYPE_NAME (type))
16809           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
16810         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
16811
16812       TREE_ASM_WRITTEN (type) = 1;
16813       gen_decl_die (TYPE_NAME (type), NULL, context_die);
16814       return;
16815     }
16816
16817   /* If this is an array type with hidden descriptor, handle it first.  */
16818   if (!TREE_ASM_WRITTEN (type)
16819       && lang_hooks.types.get_array_descr_info
16820       && lang_hooks.types.get_array_descr_info (type, &info)
16821       && (dwarf_version >= 3 || !dwarf_strict))
16822     {
16823       gen_descr_array_type_die (type, &info, context_die);
16824       TREE_ASM_WRITTEN (type) = 1;
16825       return;
16826     }
16827
16828   /* We are going to output a DIE to represent the unqualified version
16829      of this type (i.e. without any const or volatile qualifiers) so
16830      get the main variant (i.e. the unqualified version) of this type
16831      now.  (Vectors are special because the debugging info is in the
16832      cloned type itself).  */
16833   if (TREE_CODE (type) != VECTOR_TYPE)
16834     type = type_main_variant (type);
16835
16836   if (TREE_ASM_WRITTEN (type))
16837     return;
16838
16839   switch (TREE_CODE (type))
16840     {
16841     case ERROR_MARK:
16842       break;
16843
16844     case POINTER_TYPE:
16845     case REFERENCE_TYPE:
16846       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
16847          ensures that the gen_type_die recursion will terminate even if the
16848          type is recursive.  Recursive types are possible in Ada.  */
16849       /* ??? We could perhaps do this for all types before the switch
16850          statement.  */
16851       TREE_ASM_WRITTEN (type) = 1;
16852
16853       /* For these types, all that is required is that we output a DIE (or a
16854          set of DIEs) to represent the "basis" type.  */
16855       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16856                                 DINFO_USAGE_IND_USE);
16857       break;
16858
16859     case OFFSET_TYPE:
16860       /* This code is used for C++ pointer-to-data-member types.
16861          Output a description of the relevant class type.  */
16862       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
16863                                         DINFO_USAGE_IND_USE);
16864
16865       /* Output a description of the type of the object pointed to.  */
16866       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16867                                         DINFO_USAGE_IND_USE);
16868
16869       /* Now output a DIE to represent this pointer-to-data-member type
16870          itself.  */
16871       gen_ptr_to_mbr_type_die (type, context_die);
16872       break;
16873
16874     case FUNCTION_TYPE:
16875       /* Force out return type (in case it wasn't forced out already).  */
16876       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16877                                         DINFO_USAGE_DIR_USE);
16878       gen_subroutine_type_die (type, context_die);
16879       break;
16880
16881     case METHOD_TYPE:
16882       /* Force out return type (in case it wasn't forced out already).  */
16883       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16884                                         DINFO_USAGE_DIR_USE);
16885       gen_subroutine_type_die (type, context_die);
16886       break;
16887
16888     case ARRAY_TYPE:
16889       gen_array_type_die (type, context_die);
16890       break;
16891
16892     case VECTOR_TYPE:
16893       gen_array_type_die (type, context_die);
16894       break;
16895
16896     case ENUMERAL_TYPE:
16897     case RECORD_TYPE:
16898     case UNION_TYPE:
16899     case QUAL_UNION_TYPE:
16900       /* If this is a nested type whose containing class hasn't been written
16901          out yet, writing it out will cover this one, too.  This does not apply
16902          to instantiations of member class templates; they need to be added to
16903          the containing class as they are generated.  FIXME: This hurts the
16904          idea of combining type decls from multiple TUs, since we can't predict
16905          what set of template instantiations we'll get.  */
16906       if (TYPE_CONTEXT (type)
16907           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
16908           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
16909         {
16910           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
16911
16912           if (TREE_ASM_WRITTEN (type))
16913             return;
16914
16915           /* If that failed, attach ourselves to the stub.  */
16916           push_decl_scope (TYPE_CONTEXT (type));
16917           context_die = lookup_type_die (TYPE_CONTEXT (type));
16918           need_pop = 1;
16919         }
16920       else if (TYPE_CONTEXT (type) != NULL_TREE
16921                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
16922         {
16923           /* If this type is local to a function that hasn't been written
16924              out yet, use a NULL context for now; it will be fixed up in
16925              decls_for_scope.  */
16926           context_die = lookup_decl_die (TYPE_CONTEXT (type));
16927           need_pop = 0;
16928         }
16929       else
16930         {
16931           context_die = declare_in_namespace (type, context_die);
16932           need_pop = 0;
16933         }
16934
16935       if (TREE_CODE (type) == ENUMERAL_TYPE)
16936         {
16937           /* This might have been written out by the call to
16938              declare_in_namespace.  */
16939           if (!TREE_ASM_WRITTEN (type))
16940             gen_enumeration_type_die (type, context_die);
16941         }
16942       else
16943         gen_struct_or_union_type_die (type, context_die, usage);
16944
16945       if (need_pop)
16946         pop_decl_scope ();
16947
16948       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
16949          it up if it is ever completed.  gen_*_type_die will set it for us
16950          when appropriate.  */
16951       return;
16952
16953     case VOID_TYPE:
16954     case INTEGER_TYPE:
16955     case REAL_TYPE:
16956     case FIXED_POINT_TYPE:
16957     case COMPLEX_TYPE:
16958     case BOOLEAN_TYPE:
16959       /* No DIEs needed for fundamental types.  */
16960       break;
16961
16962     case LANG_TYPE:
16963       /* No Dwarf representation currently defined.  */
16964       break;
16965
16966     default:
16967       gcc_unreachable ();
16968     }
16969
16970   TREE_ASM_WRITTEN (type) = 1;
16971 }
16972
16973 static void
16974 gen_type_die (tree type, dw_die_ref context_die)
16975 {
16976   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
16977 }
16978
16979 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
16980    things which are local to the given block.  */
16981
16982 static void
16983 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
16984 {
16985   int must_output_die = 0;
16986   bool inlined_func;
16987
16988   /* Ignore blocks that are NULL.  */
16989   if (stmt == NULL_TREE)
16990     return;
16991
16992   inlined_func = inlined_function_outer_scope_p (stmt);
16993
16994   /* If the block is one fragment of a non-contiguous block, do not
16995      process the variables, since they will have been done by the
16996      origin block.  Do process subblocks.  */
16997   if (BLOCK_FRAGMENT_ORIGIN (stmt))
16998     {
16999       tree sub;
17000
17001       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
17002         gen_block_die (sub, context_die, depth + 1);
17003
17004       return;
17005     }
17006
17007   /* Determine if we need to output any Dwarf DIEs at all to represent this
17008      block.  */
17009   if (inlined_func)
17010     /* The outer scopes for inlinings *must* always be represented.  We
17011        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
17012     must_output_die = 1;
17013   else
17014     {
17015       /* Determine if this block directly contains any "significant"
17016          local declarations which we will need to output DIEs for.  */
17017       if (debug_info_level > DINFO_LEVEL_TERSE)
17018         /* We are not in terse mode so *any* local declaration counts
17019            as being a "significant" one.  */
17020         must_output_die = ((BLOCK_VARS (stmt) != NULL
17021                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
17022                            && (TREE_USED (stmt)
17023                                || TREE_ASM_WRITTEN (stmt)
17024                                || BLOCK_ABSTRACT (stmt)));
17025       else if ((TREE_USED (stmt)
17026                 || TREE_ASM_WRITTEN (stmt)
17027                 || BLOCK_ABSTRACT (stmt))
17028                && !dwarf2out_ignore_block (stmt))
17029         must_output_die = 1;
17030     }
17031
17032   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
17033      DIE for any block which contains no significant local declarations at
17034      all.  Rather, in such cases we just call `decls_for_scope' so that any
17035      needed Dwarf info for any sub-blocks will get properly generated. Note
17036      that in terse mode, our definition of what constitutes a "significant"
17037      local declaration gets restricted to include only inlined function
17038      instances and local (nested) function definitions.  */
17039   if (must_output_die)
17040     {
17041       if (inlined_func)
17042         {
17043           /* If STMT block is abstract, that means we have been called
17044              indirectly from dwarf2out_abstract_function.
17045              That function rightfully marks the descendent blocks (of
17046              the abstract function it is dealing with) as being abstract,
17047              precisely to prevent us from emitting any
17048              DW_TAG_inlined_subroutine DIE as a descendent
17049              of an abstract function instance. So in that case, we should
17050              not call gen_inlined_subroutine_die.
17051
17052              Later though, when cgraph asks dwarf2out to emit info
17053              for the concrete instance of the function decl into which
17054              the concrete instance of STMT got inlined, the later will lead
17055              to the generation of a DW_TAG_inlined_subroutine DIE.  */
17056           if (! BLOCK_ABSTRACT (stmt))
17057             gen_inlined_subroutine_die (stmt, context_die, depth);
17058         }
17059       else
17060         gen_lexical_block_die (stmt, context_die, depth);
17061     }
17062   else
17063     decls_for_scope (stmt, context_die, depth);
17064 }
17065
17066 /* Process variable DECL (or variable with origin ORIGIN) within
17067    block STMT and add it to CONTEXT_DIE.  */
17068 static void
17069 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
17070 {
17071   dw_die_ref die;
17072   tree decl_or_origin = decl ? decl : origin;
17073   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
17074
17075   if (ultimate_origin)
17076     origin = ultimate_origin;
17077
17078   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
17079     die = lookup_decl_die (decl_or_origin);
17080   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
17081            && TYPE_DECL_IS_STUB (decl_or_origin))
17082     die = lookup_type_die (TREE_TYPE (decl_or_origin));
17083   else
17084     die = NULL;
17085
17086   if (die != NULL && die->die_parent == NULL)
17087     add_child_die (context_die, die);
17088   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
17089     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
17090                                          stmt, context_die);
17091   else
17092     gen_decl_die (decl, origin, context_die);
17093 }
17094
17095 /* Generate all of the decls declared within a given scope and (recursively)
17096    all of its sub-blocks.  */
17097
17098 static void
17099 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
17100 {
17101   tree decl;
17102   unsigned int i;
17103   tree subblocks;
17104
17105   /* Ignore NULL blocks.  */
17106   if (stmt == NULL_TREE)
17107     return;
17108
17109   /* Output the DIEs to represent all of the data objects and typedefs
17110      declared directly within this block but not within any nested
17111      sub-blocks.  Also, nested function and tag DIEs have been
17112      generated with a parent of NULL; fix that up now.  */
17113   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
17114     process_scope_var (stmt, decl, NULL_TREE, context_die);
17115   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17116     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
17117                        context_die);
17118
17119   /* If we're at -g1, we're not interested in subblocks.  */
17120   if (debug_info_level <= DINFO_LEVEL_TERSE)
17121     return;
17122
17123   /* Output the DIEs to represent all sub-blocks (and the items declared
17124      therein) of this block.  */
17125   for (subblocks = BLOCK_SUBBLOCKS (stmt);
17126        subblocks != NULL;
17127        subblocks = BLOCK_CHAIN (subblocks))
17128     gen_block_die (subblocks, context_die, depth + 1);
17129 }
17130
17131 /* Is this a typedef we can avoid emitting?  */
17132
17133 static inline int
17134 is_redundant_typedef (const_tree decl)
17135 {
17136   if (TYPE_DECL_IS_STUB (decl))
17137     return 1;
17138
17139   if (DECL_ARTIFICIAL (decl)
17140       && DECL_CONTEXT (decl)
17141       && is_tagged_type (DECL_CONTEXT (decl))
17142       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
17143       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
17144     /* Also ignore the artificial member typedef for the class name.  */
17145     return 1;
17146
17147   return 0;
17148 }
17149
17150 /* Returns the DIE for a context.  */
17151
17152 static inline dw_die_ref
17153 get_context_die (tree context)
17154 {
17155   if (context)
17156     {
17157       /* Find die that represents this context.  */
17158       if (TYPE_P (context))
17159         return force_type_die (context);
17160       else
17161         return force_decl_die (context);
17162     }
17163   return comp_unit_die;
17164 }
17165
17166 /* Returns the DIE for decl.  A DIE will always be returned.  */
17167
17168 static dw_die_ref
17169 force_decl_die (tree decl)
17170 {
17171   dw_die_ref decl_die;
17172   unsigned saved_external_flag;
17173   tree save_fn = NULL_TREE;
17174   decl_die = lookup_decl_die (decl);
17175   if (!decl_die)
17176     {
17177       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
17178
17179       decl_die = lookup_decl_die (decl);
17180       if (decl_die)
17181         return decl_die;
17182
17183       switch (TREE_CODE (decl))
17184         {
17185         case FUNCTION_DECL:
17186           /* Clear current_function_decl, so that gen_subprogram_die thinks
17187              that this is a declaration. At this point, we just want to force
17188              declaration die.  */
17189           save_fn = current_function_decl;
17190           current_function_decl = NULL_TREE;
17191           gen_subprogram_die (decl, context_die);
17192           current_function_decl = save_fn;
17193           break;
17194
17195         case VAR_DECL:
17196           /* Set external flag to force declaration die. Restore it after
17197            gen_decl_die() call.  */
17198           saved_external_flag = DECL_EXTERNAL (decl);
17199           DECL_EXTERNAL (decl) = 1;
17200           gen_decl_die (decl, NULL, context_die);
17201           DECL_EXTERNAL (decl) = saved_external_flag;
17202           break;
17203
17204         case NAMESPACE_DECL:
17205           if (dwarf_version >= 3 || !dwarf_strict)
17206             dwarf2out_decl (decl);
17207           else
17208             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
17209             decl_die = comp_unit_die;
17210           break;
17211
17212         default:
17213           gcc_unreachable ();
17214         }
17215
17216       /* We should be able to find the DIE now.  */
17217       if (!decl_die)
17218         decl_die = lookup_decl_die (decl);
17219       gcc_assert (decl_die);
17220     }
17221
17222   return decl_die;
17223 }
17224
17225 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
17226    always returned.  */
17227
17228 static dw_die_ref
17229 force_type_die (tree type)
17230 {
17231   dw_die_ref type_die;
17232
17233   type_die = lookup_type_die (type);
17234   if (!type_die)
17235     {
17236       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
17237
17238       type_die = modified_type_die (type, TYPE_READONLY (type),
17239                                     TYPE_VOLATILE (type), context_die);
17240       gcc_assert (type_die);
17241     }
17242   return type_die;
17243 }
17244
17245 /* Force out any required namespaces to be able to output DECL,
17246    and return the new context_die for it, if it's changed.  */
17247
17248 static dw_die_ref
17249 setup_namespace_context (tree thing, dw_die_ref context_die)
17250 {
17251   tree context = (DECL_P (thing)
17252                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
17253   if (context && TREE_CODE (context) == NAMESPACE_DECL)
17254     /* Force out the namespace.  */
17255     context_die = force_decl_die (context);
17256
17257   return context_die;
17258 }
17259
17260 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
17261    type) within its namespace, if appropriate.
17262
17263    For compatibility with older debuggers, namespace DIEs only contain
17264    declarations; all definitions are emitted at CU scope.  */
17265
17266 static dw_die_ref
17267 declare_in_namespace (tree thing, dw_die_ref context_die)
17268 {
17269   dw_die_ref ns_context;
17270
17271   if (debug_info_level <= DINFO_LEVEL_TERSE)
17272     return context_die;
17273
17274   /* If this decl is from an inlined function, then don't try to emit it in its
17275      namespace, as we will get confused.  It would have already been emitted
17276      when the abstract instance of the inline function was emitted anyways.  */
17277   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
17278     return context_die;
17279
17280   ns_context = setup_namespace_context (thing, context_die);
17281
17282   if (ns_context != context_die)
17283     {
17284       if (is_fortran ())
17285         return ns_context;
17286       if (DECL_P (thing))
17287         gen_decl_die (thing, NULL, ns_context);
17288       else
17289         gen_type_die (thing, ns_context);
17290     }
17291   return context_die;
17292 }
17293
17294 /* Generate a DIE for a namespace or namespace alias.  */
17295
17296 static void
17297 gen_namespace_die (tree decl, dw_die_ref context_die)
17298 {
17299   dw_die_ref namespace_die;
17300
17301   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
17302      they are an alias of.  */
17303   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
17304     {
17305       /* Output a real namespace or module.  */
17306       context_die = setup_namespace_context (decl, comp_unit_die);
17307       namespace_die = new_die (is_fortran ()
17308                                ? DW_TAG_module : DW_TAG_namespace,
17309                                context_die, decl);
17310       /* For Fortran modules defined in different CU don't add src coords.  */
17311       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
17312         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
17313       else
17314         add_name_and_src_coords_attributes (namespace_die, decl);
17315       if (DECL_EXTERNAL (decl))
17316         add_AT_flag (namespace_die, DW_AT_declaration, 1);
17317       equate_decl_number_to_die (decl, namespace_die);
17318     }
17319   else
17320     {
17321       /* Output a namespace alias.  */
17322
17323       /* Force out the namespace we are an alias of, if necessary.  */
17324       dw_die_ref origin_die
17325         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
17326
17327       if (DECL_CONTEXT (decl) == NULL_TREE
17328           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
17329         context_die = setup_namespace_context (decl, comp_unit_die);
17330       /* Now create the namespace alias DIE.  */
17331       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
17332       add_name_and_src_coords_attributes (namespace_die, decl);
17333       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
17334       equate_decl_number_to_die (decl, namespace_die);
17335     }
17336 }
17337
17338 /* Generate Dwarf debug information for a decl described by DECL.  */
17339
17340 static void
17341 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
17342 {
17343   tree decl_or_origin = decl ? decl : origin;
17344   tree class_origin = NULL;
17345
17346   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
17347     return;
17348
17349   switch (TREE_CODE (decl_or_origin))
17350     {
17351     case ERROR_MARK:
17352       break;
17353
17354     case CONST_DECL:
17355       if (!is_fortran ())
17356         {
17357           /* The individual enumerators of an enum type get output when we output
17358              the Dwarf representation of the relevant enum type itself.  */
17359           break;
17360         }
17361
17362       /* Emit its type.  */
17363       gen_type_die (TREE_TYPE (decl), context_die);
17364
17365       /* And its containing namespace.  */
17366       context_die = declare_in_namespace (decl, context_die);
17367
17368       gen_const_die (decl, context_die);
17369       break;
17370
17371     case FUNCTION_DECL:
17372       /* Don't output any DIEs to represent mere function declarations,
17373          unless they are class members or explicit block externs.  */
17374       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
17375           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
17376           && (current_function_decl == NULL_TREE
17377               || DECL_ARTIFICIAL (decl_or_origin)))
17378         break;
17379
17380 #if 0
17381       /* FIXME */
17382       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
17383          on local redeclarations of global functions.  That seems broken.  */
17384       if (current_function_decl != decl)
17385         /* This is only a declaration.  */;
17386 #endif
17387
17388       /* If we're emitting a clone, emit info for the abstract instance.  */
17389       if (origin || DECL_ORIGIN (decl) != decl)
17390         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
17391
17392       /* If we're emitting an out-of-line copy of an inline function,
17393          emit info for the abstract instance and set up to refer to it.  */
17394       else if (cgraph_function_possibly_inlined_p (decl)
17395                && ! DECL_ABSTRACT (decl)
17396                && ! class_or_namespace_scope_p (context_die)
17397                /* dwarf2out_abstract_function won't emit a die if this is just
17398                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
17399                   that case, because that works only if we have a die.  */
17400                && DECL_INITIAL (decl) != NULL_TREE)
17401         {
17402           dwarf2out_abstract_function (decl);
17403           set_decl_origin_self (decl);
17404         }
17405
17406       /* Otherwise we're emitting the primary DIE for this decl.  */
17407       else if (debug_info_level > DINFO_LEVEL_TERSE)
17408         {
17409           /* Before we describe the FUNCTION_DECL itself, make sure that we
17410              have described its return type.  */
17411           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
17412
17413           /* And its virtual context.  */
17414           if (DECL_VINDEX (decl) != NULL_TREE)
17415             gen_type_die (DECL_CONTEXT (decl), context_die);
17416
17417           /* And its containing type.  */
17418           if (!origin)
17419             origin = decl_class_context (decl);
17420           if (origin != NULL_TREE)
17421             gen_type_die_for_member (origin, decl, context_die);
17422
17423           /* And its containing namespace.  */
17424           context_die = declare_in_namespace (decl, context_die);
17425         }
17426
17427       /* Now output a DIE to represent the function itself.  */
17428       if (decl)
17429         gen_subprogram_die (decl, context_die);
17430       break;
17431
17432     case TYPE_DECL:
17433       /* If we are in terse mode, don't generate any DIEs to represent any
17434          actual typedefs.  */
17435       if (debug_info_level <= DINFO_LEVEL_TERSE)
17436         break;
17437
17438       /* In the special case of a TYPE_DECL node representing the declaration
17439          of some type tag, if the given TYPE_DECL is marked as having been
17440          instantiated from some other (original) TYPE_DECL node (e.g. one which
17441          was generated within the original definition of an inline function) we
17442          used to generate a special (abbreviated) DW_TAG_structure_type,
17443          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
17444          should be actually referencing those DIEs, as variable DIEs with that
17445          type would be emitted already in the abstract origin, so it was always
17446          removed during unused type prunning.  Don't add anything in this
17447          case.  */
17448       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
17449         break;
17450
17451       if (is_redundant_typedef (decl))
17452         gen_type_die (TREE_TYPE (decl), context_die);
17453       else
17454         /* Output a DIE to represent the typedef itself.  */
17455         gen_typedef_die (decl, context_die);
17456       break;
17457
17458     case LABEL_DECL:
17459       if (debug_info_level >= DINFO_LEVEL_NORMAL)
17460         gen_label_die (decl, context_die);
17461       break;
17462
17463     case VAR_DECL:
17464     case RESULT_DECL:
17465       /* If we are in terse mode, don't generate any DIEs to represent any
17466          variable declarations or definitions.  */
17467       if (debug_info_level <= DINFO_LEVEL_TERSE)
17468         break;
17469
17470       /* Output any DIEs that are needed to specify the type of this data
17471          object.  */
17472       if (decl_by_reference_p (decl_or_origin))
17473         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17474       else
17475         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17476
17477       /* And its containing type.  */
17478       class_origin = decl_class_context (decl_or_origin);
17479       if (class_origin != NULL_TREE)
17480         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
17481
17482       /* And its containing namespace.  */
17483       context_die = declare_in_namespace (decl_or_origin, context_die);
17484
17485       /* Now output the DIE to represent the data object itself.  This gets
17486          complicated because of the possibility that the VAR_DECL really
17487          represents an inlined instance of a formal parameter for an inline
17488          function.  */
17489       if (!origin)
17490         origin = decl_ultimate_origin (decl);
17491       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
17492         gen_formal_parameter_die (decl, origin, context_die);
17493       else
17494         gen_variable_die (decl, origin, context_die);
17495       break;
17496
17497     case FIELD_DECL:
17498       /* Ignore the nameless fields that are used to skip bits but handle C++
17499          anonymous unions and structs.  */
17500       if (DECL_NAME (decl) != NULL_TREE
17501           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
17502           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
17503         {
17504           gen_type_die (member_declared_type (decl), context_die);
17505           gen_field_die (decl, context_die);
17506         }
17507       break;
17508
17509     case PARM_DECL:
17510       if (DECL_BY_REFERENCE (decl_or_origin))
17511         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17512       else
17513         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17514       gen_formal_parameter_die (decl, origin, context_die);
17515       break;
17516
17517     case NAMESPACE_DECL:
17518     case IMPORTED_DECL:
17519       if (dwarf_version >= 3 || !dwarf_strict)
17520         gen_namespace_die (decl, context_die);
17521       break;
17522
17523     default:
17524       /* Probably some frontend-internal decl.  Assume we don't care.  */
17525       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
17526       break;
17527     }
17528 }
17529 \f
17530 /* Output debug information for global decl DECL.  Called from toplev.c after
17531    compilation proper has finished.  */
17532
17533 static void
17534 dwarf2out_global_decl (tree decl)
17535 {
17536   /* Output DWARF2 information for file-scope tentative data object
17537      declarations, file-scope (extern) function declarations (which
17538      had no corresponding body) and file-scope tagged type declarations
17539      and definitions which have not yet been forced out.  */
17540   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
17541     dwarf2out_decl (decl);
17542 }
17543
17544 /* Output debug information for type decl DECL.  Called from toplev.c
17545    and from language front ends (to record built-in types).  */
17546 static void
17547 dwarf2out_type_decl (tree decl, int local)
17548 {
17549   if (!local)
17550     dwarf2out_decl (decl);
17551 }
17552
17553 /* Output debug information for imported module or decl DECL.
17554    NAME is non-NULL name in the lexical block if the decl has been renamed.
17555    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
17556    that DECL belongs to.
17557    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
17558 static void
17559 dwarf2out_imported_module_or_decl_1 (tree decl,
17560                                      tree name,
17561                                      tree lexical_block,
17562                                      dw_die_ref lexical_block_die)
17563 {
17564   expanded_location xloc;
17565   dw_die_ref imported_die = NULL;
17566   dw_die_ref at_import_die;
17567
17568   if (TREE_CODE (decl) == IMPORTED_DECL)
17569     {
17570       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
17571       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
17572       gcc_assert (decl);
17573     }
17574   else
17575     xloc = expand_location (input_location);
17576
17577   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
17578     {
17579       if (is_base_type (TREE_TYPE (decl)))
17580         at_import_die = base_type_die (TREE_TYPE (decl));
17581       else
17582         at_import_die = force_type_die (TREE_TYPE (decl));
17583       /* For namespace N { typedef void T; } using N::T; base_type_die
17584          returns NULL, but DW_TAG_imported_declaration requires
17585          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
17586       if (!at_import_die)
17587         {
17588           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
17589           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
17590           at_import_die = lookup_type_die (TREE_TYPE (decl));
17591           gcc_assert (at_import_die);
17592         }
17593     }
17594   else
17595     {
17596       at_import_die = lookup_decl_die (decl);
17597       if (!at_import_die)
17598         {
17599           /* If we're trying to avoid duplicate debug info, we may not have
17600              emitted the member decl for this field.  Emit it now.  */
17601           if (TREE_CODE (decl) == FIELD_DECL)
17602             {
17603               tree type = DECL_CONTEXT (decl);
17604
17605               if (TYPE_CONTEXT (type)
17606                   && TYPE_P (TYPE_CONTEXT (type))
17607                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
17608                                                 DINFO_USAGE_DIR_USE))
17609                 return;
17610               gen_type_die_for_member (type, decl,
17611                                        get_context_die (TYPE_CONTEXT (type)));
17612             }
17613           at_import_die = force_decl_die (decl);
17614         }
17615     }
17616
17617   if (TREE_CODE (decl) == NAMESPACE_DECL)
17618     {
17619       if (dwarf_version >= 3 || !dwarf_strict)
17620         imported_die = new_die (DW_TAG_imported_module,
17621                                 lexical_block_die,
17622                                 lexical_block);
17623       else
17624         return;
17625     }
17626   else
17627     imported_die = new_die (DW_TAG_imported_declaration,
17628                             lexical_block_die,
17629                             lexical_block);
17630
17631   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
17632   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
17633   if (name)
17634     add_AT_string (imported_die, DW_AT_name,
17635                    IDENTIFIER_POINTER (name));
17636   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
17637 }
17638
17639 /* Output debug information for imported module or decl DECL.
17640    NAME is non-NULL name in context if the decl has been renamed.
17641    CHILD is true if decl is one of the renamed decls as part of
17642    importing whole module.  */
17643
17644 static void
17645 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
17646                                    bool child)
17647 {
17648   /* dw_die_ref at_import_die;  */
17649   dw_die_ref scope_die;
17650
17651   if (debug_info_level <= DINFO_LEVEL_TERSE)
17652     return;
17653
17654   gcc_assert (decl);
17655
17656   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
17657      We need decl DIE for reference and scope die. First, get DIE for the decl
17658      itself.  */
17659
17660   /* Get the scope die for decl context. Use comp_unit_die for global module
17661      or decl. If die is not found for non globals, force new die.  */
17662   if (context
17663       && TYPE_P (context)
17664       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
17665     return;
17666
17667   if (!(dwarf_version >= 3 || !dwarf_strict))
17668     return;
17669
17670   scope_die = get_context_die (context);
17671
17672   if (child)
17673     {
17674       gcc_assert (scope_die->die_child);
17675       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
17676       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
17677       scope_die = scope_die->die_child;
17678     }
17679
17680   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
17681   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
17682
17683 }
17684
17685 /* Write the debugging output for DECL.  */
17686
17687 void
17688 dwarf2out_decl (tree decl)
17689 {
17690   dw_die_ref context_die = comp_unit_die;
17691
17692   switch (TREE_CODE (decl))
17693     {
17694     case ERROR_MARK:
17695       return;
17696
17697     case FUNCTION_DECL:
17698       /* What we would really like to do here is to filter out all mere
17699          file-scope declarations of file-scope functions which are never
17700          referenced later within this translation unit (and keep all of ones
17701          that *are* referenced later on) but we aren't clairvoyant, so we have
17702          no idea which functions will be referenced in the future (i.e. later
17703          on within the current translation unit). So here we just ignore all
17704          file-scope function declarations which are not also definitions.  If
17705          and when the debugger needs to know something about these functions,
17706          it will have to hunt around and find the DWARF information associated
17707          with the definition of the function.
17708
17709          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
17710          nodes represent definitions and which ones represent mere
17711          declarations.  We have to check DECL_INITIAL instead. That's because
17712          the C front-end supports some weird semantics for "extern inline"
17713          function definitions.  These can get inlined within the current
17714          translation unit (and thus, we need to generate Dwarf info for their
17715          abstract instances so that the Dwarf info for the concrete inlined
17716          instances can have something to refer to) but the compiler never
17717          generates any out-of-lines instances of such things (despite the fact
17718          that they *are* definitions).
17719
17720          The important point is that the C front-end marks these "extern
17721          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
17722          them anyway. Note that the C++ front-end also plays some similar games
17723          for inline function definitions appearing within include files which
17724          also contain `#pragma interface' pragmas.  */
17725       if (DECL_INITIAL (decl) == NULL_TREE)
17726         return;
17727
17728       /* If we're a nested function, initially use a parent of NULL; if we're
17729          a plain function, this will be fixed up in decls_for_scope.  If
17730          we're a method, it will be ignored, since we already have a DIE.  */
17731       if (decl_function_context (decl)
17732           /* But if we're in terse mode, we don't care about scope.  */
17733           && debug_info_level > DINFO_LEVEL_TERSE)
17734         context_die = NULL;
17735       break;
17736
17737     case VAR_DECL:
17738       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
17739          declaration and if the declaration was never even referenced from
17740          within this entire compilation unit.  We suppress these DIEs in
17741          order to save space in the .debug section (by eliminating entries
17742          which are probably useless).  Note that we must not suppress
17743          block-local extern declarations (whether used or not) because that
17744          would screw-up the debugger's name lookup mechanism and cause it to
17745          miss things which really ought to be in scope at a given point.  */
17746       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
17747         return;
17748
17749       /* For local statics lookup proper context die.  */
17750       if (TREE_STATIC (decl) && decl_function_context (decl))
17751         context_die = lookup_decl_die (DECL_CONTEXT (decl));
17752
17753       /* If we are in terse mode, don't generate any DIEs to represent any
17754          variable declarations or definitions.  */
17755       if (debug_info_level <= DINFO_LEVEL_TERSE)
17756         return;
17757       break;
17758
17759     case CONST_DECL:
17760       if (debug_info_level <= DINFO_LEVEL_TERSE)
17761         return;
17762       if (!is_fortran ())
17763         return;
17764       if (TREE_STATIC (decl) && decl_function_context (decl))
17765         context_die = lookup_decl_die (DECL_CONTEXT (decl));
17766       break;
17767
17768     case NAMESPACE_DECL:
17769     case IMPORTED_DECL:
17770       if (debug_info_level <= DINFO_LEVEL_TERSE)
17771         return;
17772       if (lookup_decl_die (decl) != NULL)
17773         return;
17774       break;
17775
17776     case TYPE_DECL:
17777       /* Don't emit stubs for types unless they are needed by other DIEs.  */
17778       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
17779         return;
17780
17781       /* Don't bother trying to generate any DIEs to represent any of the
17782          normal built-in types for the language we are compiling.  */
17783       if (DECL_IS_BUILTIN (decl))
17784         {
17785           /* OK, we need to generate one for `bool' so GDB knows what type
17786              comparisons have.  */
17787           if (is_cxx ()
17788               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
17789               && ! DECL_IGNORED_P (decl))
17790             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
17791
17792           return;
17793         }
17794
17795       /* If we are in terse mode, don't generate any DIEs for types.  */
17796       if (debug_info_level <= DINFO_LEVEL_TERSE)
17797         return;
17798
17799       /* If we're a function-scope tag, initially use a parent of NULL;
17800          this will be fixed up in decls_for_scope.  */
17801       if (decl_function_context (decl))
17802         context_die = NULL;
17803
17804       break;
17805
17806     default:
17807       return;
17808     }
17809
17810   gen_decl_die (decl, NULL, context_die);
17811 }
17812
17813 /* Output a marker (i.e. a label) for the beginning of the generated code for
17814    a lexical block.  */
17815
17816 static void
17817 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
17818                        unsigned int blocknum)
17819 {
17820   switch_to_section (current_function_section ());
17821   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
17822 }
17823
17824 /* Output a marker (i.e. a label) for the end of the generated code for a
17825    lexical block.  */
17826
17827 static void
17828 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
17829 {
17830   switch_to_section (current_function_section ());
17831   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
17832 }
17833
17834 /* Returns nonzero if it is appropriate not to emit any debugging
17835    information for BLOCK, because it doesn't contain any instructions.
17836
17837    Don't allow this for blocks with nested functions or local classes
17838    as we would end up with orphans, and in the presence of scheduling
17839    we may end up calling them anyway.  */
17840
17841 static bool
17842 dwarf2out_ignore_block (const_tree block)
17843 {
17844   tree decl;
17845   unsigned int i;
17846
17847   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
17848     if (TREE_CODE (decl) == FUNCTION_DECL
17849         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
17850       return 0;
17851   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
17852     {
17853       decl = BLOCK_NONLOCALIZED_VAR (block, i);
17854       if (TREE_CODE (decl) == FUNCTION_DECL
17855           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
17856       return 0;
17857     }
17858
17859   return 1;
17860 }
17861
17862 /* Hash table routines for file_hash.  */
17863
17864 static int
17865 file_table_eq (const void *p1_p, const void *p2_p)
17866 {
17867   const struct dwarf_file_data *const p1 =
17868     (const struct dwarf_file_data *) p1_p;
17869   const char *const p2 = (const char *) p2_p;
17870   return strcmp (p1->filename, p2) == 0;
17871 }
17872
17873 static hashval_t
17874 file_table_hash (const void *p_p)
17875 {
17876   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
17877   return htab_hash_string (p->filename);
17878 }
17879
17880 /* Lookup FILE_NAME (in the list of filenames that we know about here in
17881    dwarf2out.c) and return its "index".  The index of each (known) filename is
17882    just a unique number which is associated with only that one filename.  We
17883    need such numbers for the sake of generating labels (in the .debug_sfnames
17884    section) and references to those files numbers (in the .debug_srcinfo
17885    and.debug_macinfo sections).  If the filename given as an argument is not
17886    found in our current list, add it to the list and assign it the next
17887    available unique index number.  In order to speed up searches, we remember
17888    the index of the filename was looked up last.  This handles the majority of
17889    all searches.  */
17890
17891 static struct dwarf_file_data *
17892 lookup_filename (const char *file_name)
17893 {
17894   void ** slot;
17895   struct dwarf_file_data * created;
17896
17897   /* Check to see if the file name that was searched on the previous
17898      call matches this file name.  If so, return the index.  */
17899   if (file_table_last_lookup
17900       && (file_name == file_table_last_lookup->filename
17901           || strcmp (file_table_last_lookup->filename, file_name) == 0))
17902     return file_table_last_lookup;
17903
17904   /* Didn't match the previous lookup, search the table.  */
17905   slot = htab_find_slot_with_hash (file_table, file_name,
17906                                    htab_hash_string (file_name), INSERT);
17907   if (*slot)
17908     return (struct dwarf_file_data *) *slot;
17909
17910   created = GGC_NEW (struct dwarf_file_data);
17911   created->filename = file_name;
17912   created->emitted_number = 0;
17913   *slot = created;
17914   return created;
17915 }
17916
17917 /* If the assembler will construct the file table, then translate the compiler
17918    internal file table number into the assembler file table number, and emit
17919    a .file directive if we haven't already emitted one yet.  The file table
17920    numbers are different because we prune debug info for unused variables and
17921    types, which may include filenames.  */
17922
17923 static int
17924 maybe_emit_file (struct dwarf_file_data * fd)
17925 {
17926   if (! fd->emitted_number)
17927     {
17928       if (last_emitted_file)
17929         fd->emitted_number = last_emitted_file->emitted_number + 1;
17930       else
17931         fd->emitted_number = 1;
17932       last_emitted_file = fd;
17933
17934       if (DWARF2_ASM_LINE_DEBUG_INFO)
17935         {
17936           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
17937           output_quoted_string (asm_out_file,
17938                                 remap_debug_filename (fd->filename));
17939           fputc ('\n', asm_out_file);
17940         }
17941     }
17942
17943   return fd->emitted_number;
17944 }
17945
17946 /* Schedule generation of a DW_AT_const_value attribute to DIE.
17947    That generation should happen after function debug info has been
17948    generated. The value of the attribute is the constant value of ARG.  */
17949
17950 static void
17951 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
17952 {
17953   die_arg_entry entry;
17954
17955   if (!die || !arg)
17956     return;
17957
17958   if (!tmpl_value_parm_die_table)
17959     tmpl_value_parm_die_table
17960       = VEC_alloc (die_arg_entry, gc, 32);
17961
17962   entry.die = die;
17963   entry.arg = arg;
17964   VEC_safe_push (die_arg_entry, gc,
17965                  tmpl_value_parm_die_table,
17966                  &entry);
17967 }
17968
17969 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
17970    by append_entry_to_tmpl_value_parm_die_table. This function must
17971    be called after function DIEs have been generated.  */
17972
17973 static void
17974 gen_remaining_tmpl_value_param_die_attribute (void)
17975 {
17976   if (tmpl_value_parm_die_table)
17977     {
17978       unsigned i;
17979       die_arg_entry *e;
17980
17981       for (i = 0;
17982            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
17983            i++)
17984         tree_add_const_value_attribute (e->die, e->arg);
17985     }
17986 }
17987
17988
17989 /* Replace DW_AT_name for the decl with name.  */
17990  
17991 static void
17992 dwarf2out_set_name (tree decl, tree name)
17993 {
17994   dw_die_ref die;
17995   dw_attr_ref attr;
17996
17997   die = TYPE_SYMTAB_DIE (decl);
17998   if (!die)
17999     return;
18000
18001   attr = get_AT (die, DW_AT_name);
18002   if (attr)
18003     {
18004       struct indirect_string_node *node;
18005
18006       node = find_AT_string (dwarf2_name (name, 0));
18007       /* replace the string.  */
18008       attr->dw_attr_val.v.val_str = node;
18009     }
18010
18011   else
18012     add_name_attribute (die, dwarf2_name (name, 0));
18013 }
18014
18015 /* Called by the final INSN scan whenever we see a var location.  We
18016    use it to drop labels in the right places, and throw the location in
18017    our lookup table.  */
18018
18019 static void
18020 dwarf2out_var_location (rtx loc_note)
18021 {
18022   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
18023   struct var_loc_node *newloc;
18024   rtx next_real;
18025   static const char *last_label;
18026   static const char *last_postcall_label;
18027   static bool last_in_cold_section_p;
18028   tree decl;
18029
18030   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
18031     return;
18032
18033   next_real = next_real_insn (loc_note);
18034   /* If there are no instructions which would be affected by this note,
18035      don't do anything.  */
18036   if (next_real == NULL_RTX)
18037     return;
18038
18039   newloc = GGC_CNEW (struct var_loc_node);
18040   /* If there were no real insns between note we processed last time
18041      and this note, use the label we emitted last time.  */
18042   if (last_var_location_insn == NULL_RTX
18043       || last_var_location_insn != next_real
18044       || last_in_cold_section_p != in_cold_section_p)
18045     {
18046       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
18047       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
18048       loclabel_num++;
18049       last_label = ggc_strdup (loclabel);
18050       if (!NOTE_DURING_CALL_P (loc_note))
18051         last_postcall_label = NULL;
18052     }
18053   newloc->var_loc_note = loc_note;
18054   newloc->next = NULL;
18055
18056   if (!NOTE_DURING_CALL_P (loc_note))
18057     newloc->label = last_label;
18058   else
18059     {
18060       if (!last_postcall_label)
18061         {
18062           sprintf (loclabel, "%s-1", last_label);
18063           last_postcall_label = ggc_strdup (loclabel);
18064         }
18065       newloc->label = last_postcall_label;
18066     }
18067
18068   if (cfun && in_cold_section_p)
18069     newloc->section_label = crtl->subsections.cold_section_label;
18070   else
18071     newloc->section_label = text_section_label;
18072
18073   last_var_location_insn = next_real;
18074   last_in_cold_section_p = in_cold_section_p;
18075   decl = NOTE_VAR_LOCATION_DECL (loc_note);
18076   add_var_loc_to_decl (decl, newloc);
18077 }
18078
18079 /* We need to reset the locations at the beginning of each
18080    function. We can't do this in the end_function hook, because the
18081    declarations that use the locations won't have been output when
18082    that hook is called.  Also compute have_multiple_function_sections here.  */
18083
18084 static void
18085 dwarf2out_begin_function (tree fun)
18086 {
18087   htab_empty (decl_loc_table);
18088
18089   if (function_section (fun) != text_section)
18090     have_multiple_function_sections = true;
18091
18092   dwarf2out_note_section_used ();
18093 }
18094
18095 /* Output a label to mark the beginning of a source code line entry
18096    and record information relating to this source line, in
18097    'line_info_table' for later output of the .debug_line section.  */
18098
18099 static void
18100 dwarf2out_source_line (unsigned int line, const char *filename,
18101                        int discriminator, bool is_stmt)
18102 {
18103   static bool last_is_stmt = true;
18104
18105   if (debug_info_level >= DINFO_LEVEL_NORMAL
18106       && line != 0)
18107     {
18108       int file_num = maybe_emit_file (lookup_filename (filename));
18109
18110       switch_to_section (current_function_section ());
18111
18112       /* If requested, emit something human-readable.  */
18113       if (flag_debug_asm)
18114         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
18115                  filename, line);
18116
18117       if (DWARF2_ASM_LINE_DEBUG_INFO)
18118         {
18119           /* Emit the .loc directive understood by GNU as.  */
18120           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
18121           if (is_stmt != last_is_stmt)
18122             {
18123               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
18124               last_is_stmt = is_stmt;
18125             }
18126           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
18127             fprintf (asm_out_file, " discriminator %d", discriminator);
18128           fputc ('\n', asm_out_file);
18129
18130           /* Indicate that line number info exists.  */
18131           line_info_table_in_use++;
18132         }
18133       else if (function_section (current_function_decl) != text_section)
18134         {
18135           dw_separate_line_info_ref line_info;
18136           targetm.asm_out.internal_label (asm_out_file,
18137                                           SEPARATE_LINE_CODE_LABEL,
18138                                           separate_line_info_table_in_use);
18139
18140           /* Expand the line info table if necessary.  */
18141           if (separate_line_info_table_in_use
18142               == separate_line_info_table_allocated)
18143             {
18144               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
18145               separate_line_info_table
18146                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
18147                                  separate_line_info_table,
18148                                  separate_line_info_table_allocated);
18149               memset (separate_line_info_table
18150                        + separate_line_info_table_in_use,
18151                       0,
18152                       (LINE_INFO_TABLE_INCREMENT
18153                        * sizeof (dw_separate_line_info_entry)));
18154             }
18155
18156           /* Add the new entry at the end of the line_info_table.  */
18157           line_info
18158             = &separate_line_info_table[separate_line_info_table_in_use++];
18159           line_info->dw_file_num = file_num;
18160           line_info->dw_line_num = line;
18161           line_info->function = current_function_funcdef_no;
18162         }
18163       else
18164         {
18165           dw_line_info_ref line_info;
18166
18167           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
18168                                      line_info_table_in_use);
18169
18170           /* Expand the line info table if necessary.  */
18171           if (line_info_table_in_use == line_info_table_allocated)
18172             {
18173               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
18174               line_info_table
18175                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
18176                                  line_info_table_allocated);
18177               memset (line_info_table + line_info_table_in_use, 0,
18178                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
18179             }
18180
18181           /* Add the new entry at the end of the line_info_table.  */
18182           line_info = &line_info_table[line_info_table_in_use++];
18183           line_info->dw_file_num = file_num;
18184           line_info->dw_line_num = line;
18185         }
18186     }
18187 }
18188
18189 /* Record the beginning of a new source file.  */
18190
18191 static void
18192 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
18193 {
18194   if (flag_eliminate_dwarf2_dups)
18195     {
18196       /* Record the beginning of the file for break_out_includes.  */
18197       dw_die_ref bincl_die;
18198
18199       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
18200       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
18201     }
18202
18203   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18204     {
18205       int file_num = maybe_emit_file (lookup_filename (filename));
18206
18207       switch_to_section (debug_macinfo_section);
18208       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
18209       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
18210                                    lineno);
18211
18212       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
18213     }
18214 }
18215
18216 /* Record the end of a source file.  */
18217
18218 static void
18219 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
18220 {
18221   if (flag_eliminate_dwarf2_dups)
18222     /* Record the end of the file for break_out_includes.  */
18223     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
18224
18225   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18226     {
18227       switch_to_section (debug_macinfo_section);
18228       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
18229     }
18230 }
18231
18232 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
18233    the tail part of the directive line, i.e. the part which is past the
18234    initial whitespace, #, whitespace, directive-name, whitespace part.  */
18235
18236 static void
18237 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
18238                   const char *buffer ATTRIBUTE_UNUSED)
18239 {
18240   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18241     {
18242       switch_to_section (debug_macinfo_section);
18243       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
18244       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
18245       dw2_asm_output_nstring (buffer, -1, "The macro");
18246     }
18247 }
18248
18249 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
18250    the tail part of the directive line, i.e. the part which is past the
18251    initial whitespace, #, whitespace, directive-name, whitespace part.  */
18252
18253 static void
18254 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
18255                  const char *buffer ATTRIBUTE_UNUSED)
18256 {
18257   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18258     {
18259       switch_to_section (debug_macinfo_section);
18260       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
18261       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
18262       dw2_asm_output_nstring (buffer, -1, "The macro");
18263     }
18264 }
18265
18266 /* Set up for Dwarf output at the start of compilation.  */
18267
18268 static void
18269 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
18270 {
18271   /* Allocate the file_table.  */
18272   file_table = htab_create_ggc (50, file_table_hash,
18273                                 file_table_eq, NULL);
18274
18275   /* Allocate the decl_die_table.  */
18276   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
18277                                     decl_die_table_eq, NULL);
18278
18279   /* Allocate the decl_loc_table.  */
18280   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
18281                                     decl_loc_table_eq, NULL);
18282
18283   /* Allocate the initial hunk of the decl_scope_table.  */
18284   decl_scope_table = VEC_alloc (tree, gc, 256);
18285
18286   /* Allocate the initial hunk of the abbrev_die_table.  */
18287   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
18288   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
18289   /* Zero-th entry is allocated, but unused.  */
18290   abbrev_die_table_in_use = 1;
18291
18292   /* Allocate the initial hunk of the line_info_table.  */
18293   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
18294   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
18295
18296   /* Zero-th entry is allocated, but unused.  */
18297   line_info_table_in_use = 1;
18298
18299   /* Allocate the pubtypes and pubnames vectors.  */
18300   pubname_table = VEC_alloc (pubname_entry, gc, 32);
18301   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
18302
18303   /* Generate the initial DIE for the .debug section.  Note that the (string)
18304      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
18305      will (typically) be a relative pathname and that this pathname should be
18306      taken as being relative to the directory from which the compiler was
18307      invoked when the given (base) source file was compiled.  We will fill
18308      in this value in dwarf2out_finish.  */
18309   comp_unit_die = gen_compile_unit_die (NULL);
18310
18311   incomplete_types = VEC_alloc (tree, gc, 64);
18312
18313   used_rtx_array = VEC_alloc (rtx, gc, 32);
18314
18315   debug_info_section = get_section (DEBUG_INFO_SECTION,
18316                                     SECTION_DEBUG, NULL);
18317   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
18318                                       SECTION_DEBUG, NULL);
18319   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
18320                                        SECTION_DEBUG, NULL);
18321   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
18322                                        SECTION_DEBUG, NULL);
18323   debug_line_section = get_section (DEBUG_LINE_SECTION,
18324                                     SECTION_DEBUG, NULL);
18325   debug_loc_section = get_section (DEBUG_LOC_SECTION,
18326                                    SECTION_DEBUG, NULL);
18327   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
18328                                         SECTION_DEBUG, NULL);
18329   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
18330                                         SECTION_DEBUG, NULL);
18331   debug_str_section = get_section (DEBUG_STR_SECTION,
18332                                    DEBUG_STR_SECTION_FLAGS, NULL);
18333   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
18334                                       SECTION_DEBUG, NULL);
18335   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
18336                                      SECTION_DEBUG, NULL);
18337
18338   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
18339   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
18340                                DEBUG_ABBREV_SECTION_LABEL, 0);
18341   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
18342   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
18343                                COLD_TEXT_SECTION_LABEL, 0);
18344   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
18345
18346   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
18347                                DEBUG_INFO_SECTION_LABEL, 0);
18348   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
18349                                DEBUG_LINE_SECTION_LABEL, 0);
18350   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
18351                                DEBUG_RANGES_SECTION_LABEL, 0);
18352   switch_to_section (debug_abbrev_section);
18353   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
18354   switch_to_section (debug_info_section);
18355   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
18356   switch_to_section (debug_line_section);
18357   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
18358
18359   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18360     {
18361       switch_to_section (debug_macinfo_section);
18362       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
18363                                    DEBUG_MACINFO_SECTION_LABEL, 0);
18364       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
18365     }
18366
18367   switch_to_section (text_section);
18368   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
18369   if (flag_reorder_blocks_and_partition)
18370     {
18371       cold_text_section = unlikely_text_section ();
18372       switch_to_section (cold_text_section);
18373       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
18374     }
18375 }
18376
18377 /* A helper function for dwarf2out_finish called through
18378    htab_traverse.  Emit one queued .debug_str string.  */
18379
18380 static int
18381 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
18382 {
18383   struct indirect_string_node *node = (struct indirect_string_node *) *h;
18384
18385   if (node->label && node->refcount)
18386     {
18387       switch_to_section (debug_str_section);
18388       ASM_OUTPUT_LABEL (asm_out_file, node->label);
18389       assemble_string (node->str, strlen (node->str) + 1);
18390     }
18391
18392   return 1;
18393 }
18394
18395 #if ENABLE_ASSERT_CHECKING
18396 /* Verify that all marks are clear.  */
18397
18398 static void
18399 verify_marks_clear (dw_die_ref die)
18400 {
18401   dw_die_ref c;
18402
18403   gcc_assert (! die->die_mark);
18404   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
18405 }
18406 #endif /* ENABLE_ASSERT_CHECKING */
18407
18408 /* Clear the marks for a die and its children.
18409    Be cool if the mark isn't set.  */
18410
18411 static void
18412 prune_unmark_dies (dw_die_ref die)
18413 {
18414   dw_die_ref c;
18415
18416   if (die->die_mark)
18417     die->die_mark = 0;
18418   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
18419 }
18420
18421 /* Given DIE that we're marking as used, find any other dies
18422    it references as attributes and mark them as used.  */
18423
18424 static void
18425 prune_unused_types_walk_attribs (dw_die_ref die)
18426 {
18427   dw_attr_ref a;
18428   unsigned ix;
18429
18430   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18431     {
18432       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
18433         {
18434           /* A reference to another DIE.
18435              Make sure that it will get emitted.  */
18436           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
18437         }
18438       /* Set the string's refcount to 0 so that prune_unused_types_mark
18439          accounts properly for it.  */
18440       if (AT_class (a) == dw_val_class_str)
18441         a->dw_attr_val.v.val_str->refcount = 0;
18442     }
18443 }
18444
18445
18446 /* Mark DIE as being used.  If DOKIDS is true, then walk down
18447    to DIE's children.  */
18448
18449 static void
18450 prune_unused_types_mark (dw_die_ref die, int dokids)
18451 {
18452   dw_die_ref c;
18453
18454   if (die->die_mark == 0)
18455     {
18456       /* We haven't done this node yet.  Mark it as used.  */
18457       die->die_mark = 1;
18458
18459       /* We also have to mark its parents as used.
18460          (But we don't want to mark our parents' kids due to this.)  */
18461       if (die->die_parent)
18462         prune_unused_types_mark (die->die_parent, 0);
18463
18464       /* Mark any referenced nodes.  */
18465       prune_unused_types_walk_attribs (die);
18466
18467       /* If this node is a specification,
18468          also mark the definition, if it exists.  */
18469       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
18470         prune_unused_types_mark (die->die_definition, 1);
18471     }
18472
18473   if (dokids && die->die_mark != 2)
18474     {
18475       /* We need to walk the children, but haven't done so yet.
18476          Remember that we've walked the kids.  */
18477       die->die_mark = 2;
18478
18479       /* If this is an array type, we need to make sure our
18480          kids get marked, even if they're types.  */
18481       if (die->die_tag == DW_TAG_array_type)
18482         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
18483       else
18484         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18485     }
18486 }
18487
18488 /* For local classes, look if any static member functions were emitted
18489    and if so, mark them.  */
18490
18491 static void
18492 prune_unused_types_walk_local_classes (dw_die_ref die)
18493 {
18494   dw_die_ref c;
18495
18496   if (die->die_mark == 2)
18497     return;
18498
18499   switch (die->die_tag)
18500     {
18501     case DW_TAG_structure_type:
18502     case DW_TAG_union_type:
18503     case DW_TAG_class_type:
18504       break;
18505
18506     case DW_TAG_subprogram:
18507       if (!get_AT_flag (die, DW_AT_declaration)
18508           || die->die_definition != NULL)
18509         prune_unused_types_mark (die, 1);
18510       return;
18511
18512     default:
18513       return;
18514     }
18515
18516   /* Mark children.  */
18517   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
18518 }
18519
18520 /* Walk the tree DIE and mark types that we actually use.  */
18521
18522 static void
18523 prune_unused_types_walk (dw_die_ref die)
18524 {
18525   dw_die_ref c;
18526
18527   /* Don't do anything if this node is already marked and
18528      children have been marked as well.  */
18529   if (die->die_mark == 2)
18530     return;
18531
18532   switch (die->die_tag)
18533     {
18534     case DW_TAG_structure_type:
18535     case DW_TAG_union_type:
18536     case DW_TAG_class_type:
18537       if (die->die_perennial_p)
18538         break;
18539
18540       for (c = die->die_parent; c; c = c->die_parent)
18541         if (c->die_tag == DW_TAG_subprogram)
18542           break;
18543
18544       /* Finding used static member functions inside of classes
18545          is needed just for local classes, because for other classes
18546          static member function DIEs with DW_AT_specification
18547          are emitted outside of the DW_TAG_*_type.  If we ever change
18548          it, we'd need to call this even for non-local classes.  */
18549       if (c)
18550         prune_unused_types_walk_local_classes (die);
18551
18552       /* It's a type node --- don't mark it.  */
18553       return;
18554
18555     case DW_TAG_const_type:
18556     case DW_TAG_packed_type:
18557     case DW_TAG_pointer_type:
18558     case DW_TAG_reference_type:
18559     case DW_TAG_volatile_type:
18560     case DW_TAG_typedef:
18561     case DW_TAG_array_type:
18562     case DW_TAG_interface_type:
18563     case DW_TAG_friend:
18564     case DW_TAG_variant_part:
18565     case DW_TAG_enumeration_type:
18566     case DW_TAG_subroutine_type:
18567     case DW_TAG_string_type:
18568     case DW_TAG_set_type:
18569     case DW_TAG_subrange_type:
18570     case DW_TAG_ptr_to_member_type:
18571     case DW_TAG_file_type:
18572       if (die->die_perennial_p)
18573         break;
18574
18575       /* It's a type node --- don't mark it.  */
18576       return;
18577
18578     default:
18579       /* Mark everything else.  */
18580       break;
18581   }
18582
18583   if (die->die_mark == 0)
18584     {
18585       die->die_mark = 1;
18586
18587       /* Now, mark any dies referenced from here.  */
18588       prune_unused_types_walk_attribs (die);
18589     }
18590
18591   die->die_mark = 2;
18592
18593   /* Mark children.  */
18594   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18595 }
18596
18597 /* Increment the string counts on strings referred to from DIE's
18598    attributes.  */
18599
18600 static void
18601 prune_unused_types_update_strings (dw_die_ref die)
18602 {
18603   dw_attr_ref a;
18604   unsigned ix;
18605
18606   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18607     if (AT_class (a) == dw_val_class_str)
18608       {
18609         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
18610         s->refcount++;
18611         /* Avoid unnecessarily putting strings that are used less than
18612            twice in the hash table.  */
18613         if (s->refcount
18614             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
18615           {
18616             void ** slot;
18617             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
18618                                              htab_hash_string (s->str),
18619                                              INSERT);
18620             gcc_assert (*slot == NULL);
18621             *slot = s;
18622           }
18623       }
18624 }
18625
18626 /* Remove from the tree DIE any dies that aren't marked.  */
18627
18628 static void
18629 prune_unused_types_prune (dw_die_ref die)
18630 {
18631   dw_die_ref c;
18632
18633   gcc_assert (die->die_mark);
18634   prune_unused_types_update_strings (die);
18635
18636   if (! die->die_child)
18637     return;
18638
18639   c = die->die_child;
18640   do {
18641     dw_die_ref prev = c;
18642     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
18643       if (c == die->die_child)
18644         {
18645           /* No marked children between 'prev' and the end of the list.  */
18646           if (prev == c)
18647             /* No marked children at all.  */
18648             die->die_child = NULL;
18649           else
18650             {
18651               prev->die_sib = c->die_sib;
18652               die->die_child = prev;
18653             }
18654           return;
18655         }
18656
18657     if (c != prev->die_sib)
18658       prev->die_sib = c;
18659     prune_unused_types_prune (c);
18660   } while (c != die->die_child);
18661 }
18662
18663 /* A helper function for dwarf2out_finish called through
18664    htab_traverse.  Clear .debug_str strings that we haven't already
18665    decided to emit.  */
18666
18667 static int
18668 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
18669 {
18670   struct indirect_string_node *node = (struct indirect_string_node *) *h;
18671
18672   if (!node->label || !node->refcount)
18673     htab_clear_slot (debug_str_hash, h);
18674
18675   return 1;
18676 }
18677
18678 /* Remove dies representing declarations that we never use.  */
18679
18680 static void
18681 prune_unused_types (void)
18682 {
18683   unsigned int i;
18684   limbo_die_node *node;
18685   pubname_ref pub;
18686
18687 #if ENABLE_ASSERT_CHECKING
18688   /* All the marks should already be clear.  */
18689   verify_marks_clear (comp_unit_die);
18690   for (node = limbo_die_list; node; node = node->next)
18691     verify_marks_clear (node->die);
18692 #endif /* ENABLE_ASSERT_CHECKING */
18693
18694   /* Set the mark on nodes that are actually used.  */
18695   prune_unused_types_walk (comp_unit_die);
18696   for (node = limbo_die_list; node; node = node->next)
18697     prune_unused_types_walk (node->die);
18698
18699   /* Also set the mark on nodes referenced from the
18700      pubname_table or arange_table.  */
18701   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
18702     prune_unused_types_mark (pub->die, 1);
18703   for (i = 0; i < arange_table_in_use; i++)
18704     prune_unused_types_mark (arange_table[i], 1);
18705
18706   /* Get rid of nodes that aren't marked; and update the string counts.  */
18707   if (debug_str_hash && debug_str_hash_forced)
18708     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
18709   else if (debug_str_hash)
18710     htab_empty (debug_str_hash);
18711   prune_unused_types_prune (comp_unit_die);
18712   for (node = limbo_die_list; node; node = node->next)
18713     prune_unused_types_prune (node->die);
18714
18715   /* Leave the marks clear.  */
18716   prune_unmark_dies (comp_unit_die);
18717   for (node = limbo_die_list; node; node = node->next)
18718     prune_unmark_dies (node->die);
18719 }
18720
18721 /* Set the parameter to true if there are any relative pathnames in
18722    the file table.  */
18723 static int
18724 file_table_relative_p (void ** slot, void *param)
18725 {
18726   bool *p = (bool *) param;
18727   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
18728   if (!IS_ABSOLUTE_PATH (d->filename))
18729     {
18730       *p = true;
18731       return 0;
18732     }
18733   return 1;
18734 }
18735
18736 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
18737    to the location it would have been added, should we know its
18738    DECL_ASSEMBLER_NAME when we added other attributes.  This will
18739    probably improve compactness of debug info, removing equivalent
18740    abbrevs, and hide any differences caused by deferring the
18741    computation of the assembler name, triggered by e.g. PCH.  */
18742
18743 static inline void
18744 move_linkage_attr (dw_die_ref die)
18745 {
18746   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
18747   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
18748
18749   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
18750
18751   while (--ix > 0)
18752     {
18753       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
18754
18755       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
18756         break;
18757     }
18758
18759   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
18760     {
18761       VEC_pop (dw_attr_node, die->die_attr);
18762       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
18763     }
18764 }
18765
18766 /* Output stuff that dwarf requires at the end of every file,
18767    and generate the DWARF-2 debugging info.  */
18768
18769 static void
18770 dwarf2out_finish (const char *filename)
18771 {
18772   limbo_die_node *node, *next_node;
18773   dw_die_ref die = 0;
18774   unsigned int i;
18775
18776   gen_remaining_tmpl_value_param_die_attribute ();
18777
18778   /* Add the name for the main input file now.  We delayed this from
18779      dwarf2out_init to avoid complications with PCH.  */
18780   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
18781   if (!IS_ABSOLUTE_PATH (filename))
18782     add_comp_dir_attribute (comp_unit_die);
18783   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
18784     {
18785       bool p = false;
18786       htab_traverse (file_table, file_table_relative_p, &p);
18787       if (p)
18788         add_comp_dir_attribute (comp_unit_die);
18789     }
18790
18791   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
18792     {
18793       add_location_or_const_value_attribute (
18794         VEC_index (deferred_locations, deferred_locations_list, i)->die,
18795         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
18796         DW_AT_location);
18797     }
18798
18799   /* Traverse the limbo die list, and add parent/child links.  The only
18800      dies without parents that should be here are concrete instances of
18801      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
18802      For concrete instances, we can get the parent die from the abstract
18803      instance.  */
18804   for (node = limbo_die_list; node; node = next_node)
18805     {
18806       next_node = node->next;
18807       die = node->die;
18808
18809       if (die->die_parent == NULL)
18810         {
18811           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
18812
18813           if (origin)
18814             add_child_die (origin->die_parent, die);
18815           else if (die == comp_unit_die)
18816             ;
18817           else if (errorcount > 0 || sorrycount > 0)
18818             /* It's OK to be confused by errors in the input.  */
18819             add_child_die (comp_unit_die, die);
18820           else
18821             {
18822               /* In certain situations, the lexical block containing a
18823                  nested function can be optimized away, which results
18824                  in the nested function die being orphaned.  Likewise
18825                  with the return type of that nested function.  Force
18826                  this to be a child of the containing function.
18827
18828                  It may happen that even the containing function got fully
18829                  inlined and optimized out.  In that case we are lost and
18830                  assign the empty child.  This should not be big issue as
18831                  the function is likely unreachable too.  */
18832               tree context = NULL_TREE;
18833
18834               gcc_assert (node->created_for);
18835
18836               if (DECL_P (node->created_for))
18837                 context = DECL_CONTEXT (node->created_for);
18838               else if (TYPE_P (node->created_for))
18839                 context = TYPE_CONTEXT (node->created_for);
18840
18841               gcc_assert (context
18842                           && (TREE_CODE (context) == FUNCTION_DECL
18843                               || TREE_CODE (context) == NAMESPACE_DECL));
18844
18845               origin = lookup_decl_die (context);
18846               if (origin)
18847                 add_child_die (origin, die);
18848               else
18849                 add_child_die (comp_unit_die, die);
18850             }
18851         }
18852     }
18853
18854   limbo_die_list = NULL;
18855
18856   for (node = deferred_asm_name; node; node = node->next)
18857     {
18858       tree decl = node->created_for;
18859       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18860         {
18861           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
18862                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
18863           move_linkage_attr (node->die);
18864         }
18865     }
18866
18867   deferred_asm_name = NULL;
18868
18869   /* Walk through the list of incomplete types again, trying once more to
18870      emit full debugging info for them.  */
18871   retry_incomplete_types ();
18872
18873   if (flag_eliminate_unused_debug_types)
18874     prune_unused_types ();
18875
18876   /* Generate separate CUs for each of the include files we've seen.
18877      They will go into limbo_die_list.  */
18878   if (flag_eliminate_dwarf2_dups)
18879     break_out_includes (comp_unit_die);
18880
18881   /* Traverse the DIE's and add add sibling attributes to those DIE's
18882      that have children.  */
18883   add_sibling_attributes (comp_unit_die);
18884   for (node = limbo_die_list; node; node = node->next)
18885     add_sibling_attributes (node->die);
18886
18887   /* Output a terminator label for the .text section.  */
18888   switch_to_section (text_section);
18889   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
18890   if (flag_reorder_blocks_and_partition)
18891     {
18892       switch_to_section (unlikely_text_section ());
18893       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
18894     }
18895
18896   /* We can only use the low/high_pc attributes if all of the code was
18897      in .text.  */
18898   if (!have_multiple_function_sections
18899       || !(dwarf_version >= 3 || !dwarf_strict))
18900     {
18901       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
18902       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
18903     }
18904
18905   else
18906     {
18907       unsigned fde_idx = 0;
18908
18909       /* We need to give .debug_loc and .debug_ranges an appropriate
18910          "base address".  Use zero so that these addresses become
18911          absolute.  Historically, we've emitted the unexpected
18912          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
18913          Emit both to give time for other tools to adapt.  */
18914       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
18915       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
18916
18917       add_AT_range_list (comp_unit_die, DW_AT_ranges,
18918                          add_ranges_by_labels (text_section_label,
18919                                                text_end_label));
18920       if (flag_reorder_blocks_and_partition)
18921         add_ranges_by_labels (cold_text_section_label,
18922                               cold_end_label);
18923
18924       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
18925         {
18926           dw_fde_ref fde = &fde_table[fde_idx];
18927
18928           if (fde->dw_fde_switched_sections)
18929             {
18930               if (!fde->in_std_section)
18931                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
18932                                       fde->dw_fde_hot_section_end_label);
18933               if (!fde->cold_in_std_section)
18934                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
18935                                       fde->dw_fde_unlikely_section_end_label);
18936             }
18937           else if (!fde->in_std_section)
18938             add_ranges_by_labels (fde->dw_fde_begin,
18939                                   fde->dw_fde_end);
18940         }
18941
18942       add_ranges (NULL);
18943     }
18944
18945   /* Output location list section if necessary.  */
18946   if (have_location_lists)
18947     {
18948       /* Output the location lists info.  */
18949       switch_to_section (debug_loc_section);
18950       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
18951                                    DEBUG_LOC_SECTION_LABEL, 0);
18952       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
18953       output_location_lists (die);
18954     }
18955
18956   if (debug_info_level >= DINFO_LEVEL_NORMAL)
18957     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
18958                     debug_line_section_label);
18959
18960   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18961     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
18962
18963   /* Output all of the compilation units.  We put the main one last so that
18964      the offsets are available to output_pubnames.  */
18965   for (node = limbo_die_list; node; node = node->next)
18966     output_comp_unit (node->die, 0);
18967
18968   /* Output the main compilation unit if non-empty or if .debug_macinfo
18969      has been emitted.  */
18970   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
18971
18972   /* Output the abbreviation table.  */
18973   switch_to_section (debug_abbrev_section);
18974   output_abbrev_section ();
18975
18976   /* Output public names table if necessary.  */
18977   if (!VEC_empty (pubname_entry, pubname_table))
18978     {
18979       switch_to_section (debug_pubnames_section);
18980       output_pubnames (pubname_table);
18981     }
18982
18983   /* Output public types table if necessary.  */
18984   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
18985      It shouldn't hurt to emit it always, since pure DWARF2 consumers
18986      simply won't look for the section.  */
18987   if (!VEC_empty (pubname_entry, pubtype_table))
18988     {
18989       switch_to_section (debug_pubtypes_section);
18990       output_pubnames (pubtype_table);
18991     }
18992
18993   /* Output the address range information.  We only put functions in the arange
18994      table, so don't write it out if we don't have any.  */
18995   if (fde_table_in_use)
18996     {
18997       switch_to_section (debug_aranges_section);
18998       output_aranges ();
18999     }
19000
19001   /* Output ranges section if necessary.  */
19002   if (ranges_table_in_use)
19003     {
19004       switch_to_section (debug_ranges_section);
19005       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
19006       output_ranges ();
19007     }
19008
19009   /* Output the source line correspondence table.  We must do this
19010      even if there is no line information.  Otherwise, on an empty
19011      translation unit, we will generate a present, but empty,
19012      .debug_info section.  IRIX 6.5 `nm' will then complain when
19013      examining the file.  This is done late so that any filenames
19014      used by the debug_info section are marked as 'used'.  */
19015   if (! DWARF2_ASM_LINE_DEBUG_INFO)
19016     {
19017       switch_to_section (debug_line_section);
19018       output_line_info ();
19019     }
19020
19021   /* Have to end the macro section.  */
19022   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
19023     {
19024       switch_to_section (debug_macinfo_section);
19025       dw2_asm_output_data (1, 0, "End compilation unit");
19026     }
19027
19028   /* If we emitted any DW_FORM_strp form attribute, output the string
19029      table too.  */
19030   if (debug_str_hash)
19031     htab_traverse (debug_str_hash, output_indirect_string, NULL);
19032 }
19033 #else
19034
19035 /* This should never be used, but its address is needed for comparisons.  */
19036 const struct gcc_debug_hooks dwarf2_debug_hooks =
19037 {
19038   0,            /* init */
19039   0,            /* finish */
19040   0,            /* define */
19041   0,            /* undef */
19042   0,            /* start_source_file */
19043   0,            /* end_source_file */
19044   0,            /* begin_block */
19045   0,            /* end_block */
19046   0,            /* ignore_block */
19047   0,            /* source_line */
19048   0,            /* begin_prologue */
19049   0,            /* end_prologue */
19050   0,            /* end_epilogue */
19051   0,            /* begin_function */
19052   0,            /* end_function */
19053   0,            /* function_decl */
19054   0,            /* global_decl */
19055   0,            /* type_decl */
19056   0,            /* imported_module_or_decl */
19057   0,            /* deferred_inline_function */
19058   0,            /* outlining_inline_function */
19059   0,            /* label */
19060   0,            /* handle_pch */
19061   0,            /* var_location */
19062   0,            /* switch_text_section */
19063   0,            /* set_name */
19064   0             /* start_end_main_source_file */
19065 };
19066
19067 #endif /* DWARF2_DEBUGGING_INFO */
19068
19069 #include "gt-dwarf2out.h"