OSDN Git Service

PR debug/40521
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94
95 #ifdef DWARF2_DEBUGGING_INFO
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97
98 static rtx last_var_location_insn;
99 #endif
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 #ifndef DWARF2_FRAME_INFO
118 # ifdef DWARF2_DEBUGGING_INFO
119 #  define DWARF2_FRAME_INFO \
120   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
121 # else
122 #  define DWARF2_FRAME_INFO 0
123 # endif
124 #endif
125
126 /* Map register numbers held in the call frame info that gcc has
127    collected using DWARF_FRAME_REGNUM to those that should be output in
128    .debug_frame and .eh_frame.  */
129 #ifndef DWARF2_FRAME_REG_OUT
130 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
131 #endif
132
133 /* Save the result of dwarf2out_do_frame across PCH.  */
134 static GTY(()) bool saved_do_cfi_asm = 0;
135
136 /* Decide whether we want to emit frame unwind information for the current
137    translation unit.  */
138
139 int
140 dwarf2out_do_frame (void)
141 {
142   /* We want to emit correct CFA location expressions or lists, so we
143      have to return true if we're going to output debug info, even if
144      we're not going to output frame or unwind info.  */
145   return (write_symbols == DWARF2_DEBUG
146           || write_symbols == VMS_AND_DWARF2_DEBUG
147           || DWARF2_FRAME_INFO || saved_do_cfi_asm
148 #ifdef DWARF2_UNWIND_INFO
149           || (DWARF2_UNWIND_INFO
150               && (flag_unwind_tables
151                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
152 #endif
153           );
154 }
155
156 /* Decide whether to emit frame unwind via assembler directives.  */
157
158 int
159 dwarf2out_do_cfi_asm (void)
160 {
161   int enc;
162
163 #ifdef MIPS_DEBUGGING_INFO
164   return false;
165 #endif
166   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
167     return false;
168   if (saved_do_cfi_asm)
169     return true;
170   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
171     return false;
172
173   /* Make sure the personality encoding is one the assembler can support.
174      In particular, aligned addresses can't be handled.  */
175   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
176   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
177     return false;
178   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
179   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
180     return false;
181
182   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE)
183     {
184 #ifdef TARGET_UNWIND_INFO
185       return false;
186 #else
187       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
188         return false;
189 #endif
190     }
191
192   saved_do_cfi_asm = true;
193   return true;
194 }
195
196 /* The size of the target's pointer type.  */
197 #ifndef PTR_SIZE
198 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
199 #endif
200
201 /* Array of RTXes referenced by the debugging information, which therefore
202    must be kept around forever.  */
203 static GTY(()) VEC(rtx,gc) *used_rtx_array;
204
205 /* A pointer to the base of a list of incomplete types which might be
206    completed at some later time.  incomplete_types_list needs to be a
207    VEC(tree,gc) because we want to tell the garbage collector about
208    it.  */
209 static GTY(()) VEC(tree,gc) *incomplete_types;
210
211 /* A pointer to the base of a table of references to declaration
212    scopes.  This table is a display which tracks the nesting
213    of declaration scopes at the current scope and containing
214    scopes.  This table is used to find the proper place to
215    define type declaration DIE's.  */
216 static GTY(()) VEC(tree,gc) *decl_scope_table;
217
218 /* Pointers to various DWARF2 sections.  */
219 static GTY(()) section *debug_info_section;
220 static GTY(()) section *debug_abbrev_section;
221 static GTY(()) section *debug_aranges_section;
222 static GTY(()) section *debug_macinfo_section;
223 static GTY(()) section *debug_line_section;
224 static GTY(()) section *debug_loc_section;
225 static GTY(()) section *debug_pubnames_section;
226 static GTY(()) section *debug_pubtypes_section;
227 static GTY(()) section *debug_str_section;
228 static GTY(()) section *debug_ranges_section;
229 static GTY(()) section *debug_frame_section;
230
231 /* Personality decl of current unit.  Used only when assembler does not support
232    personality CFI.  */
233 static GTY(()) rtx current_unit_personality;
234
235 /* How to start an assembler comment.  */
236 #ifndef ASM_COMMENT_START
237 #define ASM_COMMENT_START ";#"
238 #endif
239
240 typedef struct dw_cfi_struct *dw_cfi_ref;
241 typedef struct dw_fde_struct *dw_fde_ref;
242 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
243
244 /* Call frames are described using a sequence of Call Frame
245    Information instructions.  The register number, offset
246    and address fields are provided as possible operands;
247    their use is selected by the opcode field.  */
248
249 enum dw_cfi_oprnd_type {
250   dw_cfi_oprnd_unused,
251   dw_cfi_oprnd_reg_num,
252   dw_cfi_oprnd_offset,
253   dw_cfi_oprnd_addr,
254   dw_cfi_oprnd_loc
255 };
256
257 typedef union GTY(()) dw_cfi_oprnd_struct {
258   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
259   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
260   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
261   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
262 }
263 dw_cfi_oprnd;
264
265 typedef struct GTY(()) dw_cfi_struct {
266   dw_cfi_ref dw_cfi_next;
267   enum dwarf_call_frame_info dw_cfi_opc;
268   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
269     dw_cfi_oprnd1;
270   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
271     dw_cfi_oprnd2;
272 }
273 dw_cfi_node;
274
275 /* This is how we define the location of the CFA. We use to handle it
276    as REG + OFFSET all the time,  but now it can be more complex.
277    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
278    Instead of passing around REG and OFFSET, we pass a copy
279    of this structure.  */
280 typedef struct GTY(()) cfa_loc {
281   HOST_WIDE_INT offset;
282   HOST_WIDE_INT base_offset;
283   unsigned int reg;
284   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
285   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
286 } dw_cfa_location;
287
288 /* All call frame descriptions (FDE's) in the GCC generated DWARF
289    refer to a single Common Information Entry (CIE), defined at
290    the beginning of the .debug_frame section.  This use of a single
291    CIE obviates the need to keep track of multiple CIE's
292    in the DWARF generation routines below.  */
293
294 typedef struct GTY(()) dw_fde_struct {
295   tree decl;
296   const char *dw_fde_begin;
297   const char *dw_fde_current_label;
298   const char *dw_fde_end;
299   const char *dw_fde_hot_section_label;
300   const char *dw_fde_hot_section_end_label;
301   const char *dw_fde_unlikely_section_label;
302   const char *dw_fde_unlikely_section_end_label;
303   dw_cfi_ref dw_fde_cfi;
304   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
305   unsigned funcdef_number;
306   HOST_WIDE_INT stack_realignment;
307   /* Dynamic realign argument pointer register.  */
308   unsigned int drap_reg;
309   /* Virtual dynamic realign argument pointer register.  */
310   unsigned int vdrap_reg;
311   unsigned all_throwers_are_sibcalls : 1;
312   unsigned nothrow : 1;
313   unsigned uses_eh_lsda : 1;
314   /* Whether we did stack realign in this call frame.  */
315   unsigned stack_realign : 1;
316   /* Whether dynamic realign argument pointer register has been saved.  */
317   unsigned drap_reg_saved: 1;
318   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
319   unsigned in_std_section : 1;
320   /* True iff dw_fde_unlikely_section_label is in text_section or
321      cold_text_section.  */
322   unsigned cold_in_std_section : 1;
323   /* True iff switched sections.  */
324   unsigned dw_fde_switched_sections : 1;
325   /* True iff switching from cold to hot section.  */
326   unsigned dw_fde_switched_cold_to_hot : 1;
327 }
328 dw_fde_node;
329
330 /* Maximum size (in bytes) of an artificially generated label.  */
331 #define MAX_ARTIFICIAL_LABEL_BYTES      30
332
333 /* The size of addresses as they appear in the Dwarf 2 data.
334    Some architectures use word addresses to refer to code locations,
335    but Dwarf 2 info always uses byte addresses.  On such machines,
336    Dwarf 2 addresses need to be larger than the architecture's
337    pointers.  */
338 #ifndef DWARF2_ADDR_SIZE
339 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
340 #endif
341
342 /* The size in bytes of a DWARF field indicating an offset or length
343    relative to a debug info section, specified to be 4 bytes in the
344    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
345    as PTR_SIZE.  */
346
347 #ifndef DWARF_OFFSET_SIZE
348 #define DWARF_OFFSET_SIZE 4
349 #endif
350
351 /* According to the (draft) DWARF 3 specification, the initial length
352    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
353    bytes are 0xffffffff, followed by the length stored in the next 8
354    bytes.
355
356    However, the SGI/MIPS ABI uses an initial length which is equal to
357    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
358
359 #ifndef DWARF_INITIAL_LENGTH_SIZE
360 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
361 #endif
362
363 /* Round SIZE up to the nearest BOUNDARY.  */
364 #define DWARF_ROUND(SIZE,BOUNDARY) \
365   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
366
367 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
368 #ifndef DWARF_CIE_DATA_ALIGNMENT
369 #ifdef STACK_GROWS_DOWNWARD
370 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
371 #else
372 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
373 #endif
374 #endif
375
376 /* CIE identifier.  */
377 #if HOST_BITS_PER_WIDE_INT >= 64
378 #define DWARF_CIE_ID \
379   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
380 #else
381 #define DWARF_CIE_ID DW_CIE_ID
382 #endif
383
384 /* A pointer to the base of a table that contains frame description
385    information for each routine.  */
386 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
387
388 /* Number of elements currently allocated for fde_table.  */
389 static GTY(()) unsigned fde_table_allocated;
390
391 /* Number of elements in fde_table currently in use.  */
392 static GTY(()) unsigned fde_table_in_use;
393
394 /* Size (in elements) of increments by which we may expand the
395    fde_table.  */
396 #define FDE_TABLE_INCREMENT 256
397
398 /* Get the current fde_table entry we should use.  */
399
400 static inline dw_fde_ref
401 current_fde (void)
402 {
403   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
404 }
405
406 /* A list of call frame insns for the CIE.  */
407 static GTY(()) dw_cfi_ref cie_cfi_head;
408
409 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
410 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
411    attribute that accelerates the lookup of the FDE associated
412    with the subprogram.  This variable holds the table index of the FDE
413    associated with the current function (body) definition.  */
414 static unsigned current_funcdef_fde;
415 #endif
416
417 struct GTY(()) indirect_string_node {
418   const char *str;
419   unsigned int refcount;
420   enum dwarf_form form;
421   char *label;
422 };
423
424 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
425
426 /* True if the compilation unit has location entries that reference
427    debug strings.  */
428 static GTY(()) bool debug_str_hash_forced = false;
429
430 static GTY(()) int dw2_string_counter;
431 static GTY(()) unsigned long dwarf2out_cfi_label_num;
432
433 /* True if the compilation unit places functions in more than one section.  */
434 static GTY(()) bool have_multiple_function_sections = false;
435
436 /* Whether the default text and cold text sections have been used at all.  */
437
438 static GTY(()) bool text_section_used = false;
439 static GTY(()) bool cold_text_section_used = false;
440
441 /* The default cold text section.  */
442 static GTY(()) section *cold_text_section;
443
444 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
445
446 /* Forward declarations for functions defined in this file.  */
447
448 static char *stripattributes (const char *);
449 static const char *dwarf_cfi_name (unsigned);
450 static dw_cfi_ref new_cfi (void);
451 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
452 static void add_fde_cfi (const char *, dw_cfi_ref);
453 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
454 static void lookup_cfa (dw_cfa_location *);
455 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
456 #ifdef DWARF2_UNWIND_INFO
457 static void initial_return_save (rtx);
458 #endif
459 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
460                                           HOST_WIDE_INT);
461 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
462 static void output_cfi_directive (dw_cfi_ref);
463 static void output_call_frame_info (int);
464 static void dwarf2out_note_section_used (void);
465 static void dwarf2out_stack_adjust (rtx, bool);
466 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
467 static void flush_queued_reg_saves (void);
468 static bool clobbers_queued_reg_save (const_rtx);
469 static void dwarf2out_frame_debug_expr (rtx, const char *);
470
471 /* Support for complex CFA locations.  */
472 static void output_cfa_loc (dw_cfi_ref);
473 static void output_cfa_loc_raw (dw_cfi_ref);
474 static void get_cfa_from_loc_descr (dw_cfa_location *,
475                                     struct dw_loc_descr_struct *);
476 static struct dw_loc_descr_struct *build_cfa_loc
477   (dw_cfa_location *, HOST_WIDE_INT);
478 static struct dw_loc_descr_struct *build_cfa_aligned_loc
479   (HOST_WIDE_INT, HOST_WIDE_INT);
480 static void def_cfa_1 (const char *, dw_cfa_location *);
481
482 /* How to start an assembler comment.  */
483 #ifndef ASM_COMMENT_START
484 #define ASM_COMMENT_START ";#"
485 #endif
486
487 /* Data and reference forms for relocatable data.  */
488 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
489 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
490
491 #ifndef DEBUG_FRAME_SECTION
492 #define DEBUG_FRAME_SECTION     ".debug_frame"
493 #endif
494
495 #ifndef FUNC_BEGIN_LABEL
496 #define FUNC_BEGIN_LABEL        "LFB"
497 #endif
498
499 #ifndef FUNC_END_LABEL
500 #define FUNC_END_LABEL          "LFE"
501 #endif
502
503 #ifndef FRAME_BEGIN_LABEL
504 #define FRAME_BEGIN_LABEL       "Lframe"
505 #endif
506 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
507 #define CIE_END_LABEL           "LECIE"
508 #define FDE_LABEL               "LSFDE"
509 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
510 #define FDE_END_LABEL           "LEFDE"
511 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
512 #define LINE_NUMBER_END_LABEL   "LELT"
513 #define LN_PROLOG_AS_LABEL      "LASLTP"
514 #define LN_PROLOG_END_LABEL     "LELTP"
515 #define DIE_LABEL_PREFIX        "DW"
516
517 /* The DWARF 2 CFA column which tracks the return address.  Normally this
518    is the column for PC, or the first column after all of the hard
519    registers.  */
520 #ifndef DWARF_FRAME_RETURN_COLUMN
521 #ifdef PC_REGNUM
522 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
523 #else
524 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
525 #endif
526 #endif
527
528 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
529    default, we just provide columns for all registers.  */
530 #ifndef DWARF_FRAME_REGNUM
531 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
532 #endif
533 \f
534 /* Hook used by __throw.  */
535
536 rtx
537 expand_builtin_dwarf_sp_column (void)
538 {
539   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
540   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
541 }
542
543 /* Return a pointer to a copy of the section string name S with all
544    attributes stripped off, and an asterisk prepended (for assemble_name).  */
545
546 static inline char *
547 stripattributes (const char *s)
548 {
549   char *stripped = XNEWVEC (char, strlen (s) + 2);
550   char *p = stripped;
551
552   *p++ = '*';
553
554   while (*s && *s != ',')
555     *p++ = *s++;
556
557   *p = '\0';
558   return stripped;
559 }
560
561 /* MEM is a memory reference for the register size table, each element of
562    which has mode MODE.  Initialize column C as a return address column.  */
563
564 static void
565 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
566 {
567   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
568   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
569   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
570 }
571
572 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
573
574 static inline HOST_WIDE_INT
575 div_data_align (HOST_WIDE_INT off)
576 {
577   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
578   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
579   return r;
580 }
581
582 /* Return true if we need a signed version of a given opcode
583    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
584
585 static inline bool
586 need_data_align_sf_opcode (HOST_WIDE_INT off)
587 {
588   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
589 }
590
591 /* Generate code to initialize the register size table.  */
592
593 void
594 expand_builtin_init_dwarf_reg_sizes (tree address)
595 {
596   unsigned int i;
597   enum machine_mode mode = TYPE_MODE (char_type_node);
598   rtx addr = expand_normal (address);
599   rtx mem = gen_rtx_MEM (BLKmode, addr);
600   bool wrote_return_column = false;
601
602   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
603     {
604       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
605
606       if (rnum < DWARF_FRAME_REGISTERS)
607         {
608           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
609           enum machine_mode save_mode = reg_raw_mode[i];
610           HOST_WIDE_INT size;
611
612           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
613             save_mode = choose_hard_reg_mode (i, 1, true);
614           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
615             {
616               if (save_mode == VOIDmode)
617                 continue;
618               wrote_return_column = true;
619             }
620           size = GET_MODE_SIZE (save_mode);
621           if (offset < 0)
622             continue;
623
624           emit_move_insn (adjust_address (mem, mode, offset),
625                           gen_int_mode (size, mode));
626         }
627     }
628
629   if (!wrote_return_column)
630     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
631
632 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
633   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
634 #endif
635
636   targetm.init_dwarf_reg_sizes_extra (address);
637 }
638
639 /* Convert a DWARF call frame info. operation to its string name */
640
641 static const char *
642 dwarf_cfi_name (unsigned int cfi_opc)
643 {
644   switch (cfi_opc)
645     {
646     case DW_CFA_advance_loc:
647       return "DW_CFA_advance_loc";
648     case DW_CFA_offset:
649       return "DW_CFA_offset";
650     case DW_CFA_restore:
651       return "DW_CFA_restore";
652     case DW_CFA_nop:
653       return "DW_CFA_nop";
654     case DW_CFA_set_loc:
655       return "DW_CFA_set_loc";
656     case DW_CFA_advance_loc1:
657       return "DW_CFA_advance_loc1";
658     case DW_CFA_advance_loc2:
659       return "DW_CFA_advance_loc2";
660     case DW_CFA_advance_loc4:
661       return "DW_CFA_advance_loc4";
662     case DW_CFA_offset_extended:
663       return "DW_CFA_offset_extended";
664     case DW_CFA_restore_extended:
665       return "DW_CFA_restore_extended";
666     case DW_CFA_undefined:
667       return "DW_CFA_undefined";
668     case DW_CFA_same_value:
669       return "DW_CFA_same_value";
670     case DW_CFA_register:
671       return "DW_CFA_register";
672     case DW_CFA_remember_state:
673       return "DW_CFA_remember_state";
674     case DW_CFA_restore_state:
675       return "DW_CFA_restore_state";
676     case DW_CFA_def_cfa:
677       return "DW_CFA_def_cfa";
678     case DW_CFA_def_cfa_register:
679       return "DW_CFA_def_cfa_register";
680     case DW_CFA_def_cfa_offset:
681       return "DW_CFA_def_cfa_offset";
682
683     /* DWARF 3 */
684     case DW_CFA_def_cfa_expression:
685       return "DW_CFA_def_cfa_expression";
686     case DW_CFA_expression:
687       return "DW_CFA_expression";
688     case DW_CFA_offset_extended_sf:
689       return "DW_CFA_offset_extended_sf";
690     case DW_CFA_def_cfa_sf:
691       return "DW_CFA_def_cfa_sf";
692     case DW_CFA_def_cfa_offset_sf:
693       return "DW_CFA_def_cfa_offset_sf";
694
695     /* SGI/MIPS specific */
696     case DW_CFA_MIPS_advance_loc8:
697       return "DW_CFA_MIPS_advance_loc8";
698
699     /* GNU extensions */
700     case DW_CFA_GNU_window_save:
701       return "DW_CFA_GNU_window_save";
702     case DW_CFA_GNU_args_size:
703       return "DW_CFA_GNU_args_size";
704     case DW_CFA_GNU_negative_offset_extended:
705       return "DW_CFA_GNU_negative_offset_extended";
706
707     default:
708       return "DW_CFA_<unknown>";
709     }
710 }
711
712 /* Return a pointer to a newly allocated Call Frame Instruction.  */
713
714 static inline dw_cfi_ref
715 new_cfi (void)
716 {
717   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
718
719   cfi->dw_cfi_next = NULL;
720   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
721   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
722
723   return cfi;
724 }
725
726 /* Add a Call Frame Instruction to list of instructions.  */
727
728 static inline void
729 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
730 {
731   dw_cfi_ref *p;
732   dw_fde_ref fde = current_fde ();
733
734   /* When DRAP is used, CFA is defined with an expression.  Redefine
735      CFA may lead to a different CFA value.   */
736   /* ??? Of course, this heuristic fails when we're annotating epilogues,
737      because of course we'll always want to redefine the CFA back to the
738      stack pointer on the way out.  Where should we move this check?  */
739   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
740     switch (cfi->dw_cfi_opc)
741       {
742         case DW_CFA_def_cfa_register:
743         case DW_CFA_def_cfa_offset:
744         case DW_CFA_def_cfa_offset_sf:
745         case DW_CFA_def_cfa:
746         case DW_CFA_def_cfa_sf:
747           gcc_unreachable ();
748
749         default:
750           break;
751       }
752
753   /* Find the end of the chain.  */
754   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
755     ;
756
757   *p = cfi;
758 }
759
760 /* Generate a new label for the CFI info to refer to.  FORCE is true
761    if a label needs to be output even when using .cfi_* directives.  */
762
763 char *
764 dwarf2out_cfi_label (bool force)
765 {
766   static char label[20];
767
768   if (!force && dwarf2out_do_cfi_asm ())
769     {
770       /* In this case, we will be emitting the asm directive instead of
771          the label, so just return a placeholder to keep the rest of the
772          interfaces happy.  */
773       strcpy (label, "<do not output>");
774     }
775   else
776     {
777       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
778       ASM_OUTPUT_LABEL (asm_out_file, label);
779     }
780
781   return label;
782 }
783
784 /* True if remember_state should be emitted before following CFI directive.  */
785 static bool emit_cfa_remember;
786
787 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
788    or to the CIE if LABEL is NULL.  */
789
790 static void
791 add_fde_cfi (const char *label, dw_cfi_ref cfi)
792 {
793   dw_cfi_ref *list_head;
794
795   if (emit_cfa_remember)
796     {
797       dw_cfi_ref cfi_remember;
798
799       /* Emit the state save.  */
800       emit_cfa_remember = false;
801       cfi_remember = new_cfi (); 
802       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
803       add_fde_cfi (label, cfi_remember);
804     }
805
806   list_head = &cie_cfi_head;
807
808   if (dwarf2out_do_cfi_asm ())
809     {
810       if (label)
811         {
812           dw_fde_ref fde = current_fde ();
813
814           gcc_assert (fde != NULL);
815
816           /* We still have to add the cfi to the list so that lookup_cfa
817              works later on.  When -g2 and above we even need to force
818              emitting of CFI labels and add to list a DW_CFA_set_loc for
819              convert_cfa_to_fb_loc_list purposes.  If we're generating
820              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
821              convert_cfa_to_fb_loc_list.  */
822           if (dwarf_version == 2
823               && debug_info_level > DINFO_LEVEL_TERSE
824               && (write_symbols == DWARF2_DEBUG
825                   || write_symbols == VMS_AND_DWARF2_DEBUG))
826             {
827               switch (cfi->dw_cfi_opc)
828                 {
829                 case DW_CFA_def_cfa_offset:
830                 case DW_CFA_def_cfa_offset_sf:
831                 case DW_CFA_def_cfa_register:
832                 case DW_CFA_def_cfa:
833                 case DW_CFA_def_cfa_sf:
834                 case DW_CFA_def_cfa_expression:
835                 case DW_CFA_restore_state:
836                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
837                     label = dwarf2out_cfi_label (true);
838
839                   if (fde->dw_fde_current_label == NULL
840                       || strcmp (label, fde->dw_fde_current_label) != 0)
841                     {
842                       dw_cfi_ref xcfi;
843
844                       label = xstrdup (label);
845
846                       /* Set the location counter to the new label.  */
847                       xcfi = new_cfi ();
848                       /* It doesn't metter whether DW_CFA_set_loc
849                          or DW_CFA_advance_loc4 is added here, those aren't
850                          emitted into assembly, only looked up by
851                          convert_cfa_to_fb_loc_list.  */
852                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
853                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
854                       add_cfi (&fde->dw_fde_cfi, xcfi);
855                       fde->dw_fde_current_label = label;
856                     }
857                   break;
858                 default:
859                   break;
860                 }
861             }
862
863           output_cfi_directive (cfi);
864
865           list_head = &fde->dw_fde_cfi;
866         }
867       /* ??? If this is a CFI for the CIE, we don't emit.  This
868          assumes that the standard CIE contents that the assembler
869          uses matches the standard CIE contents that the compiler
870          uses.  This is probably a bad assumption.  I'm not quite
871          sure how to address this for now.  */
872     }
873   else if (label)
874     {
875       dw_fde_ref fde = current_fde ();
876
877       gcc_assert (fde != NULL);
878
879       if (*label == 0)
880         label = dwarf2out_cfi_label (false);
881
882       if (fde->dw_fde_current_label == NULL
883           || strcmp (label, fde->dw_fde_current_label) != 0)
884         {
885           dw_cfi_ref xcfi;
886
887           label = xstrdup (label);
888
889           /* Set the location counter to the new label.  */
890           xcfi = new_cfi ();
891           /* If we have a current label, advance from there, otherwise
892              set the location directly using set_loc.  */
893           xcfi->dw_cfi_opc = fde->dw_fde_current_label
894                              ? DW_CFA_advance_loc4
895                              : DW_CFA_set_loc;
896           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
897           add_cfi (&fde->dw_fde_cfi, xcfi);
898
899           fde->dw_fde_current_label = label;
900         }
901
902       list_head = &fde->dw_fde_cfi;
903     }
904
905   add_cfi (list_head, cfi);
906 }
907
908 /* Subroutine of lookup_cfa.  */
909
910 static void
911 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
912 {
913   switch (cfi->dw_cfi_opc)
914     {
915     case DW_CFA_def_cfa_offset:
916     case DW_CFA_def_cfa_offset_sf:
917       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
918       break;
919     case DW_CFA_def_cfa_register:
920       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
921       break;
922     case DW_CFA_def_cfa:
923     case DW_CFA_def_cfa_sf:
924       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
925       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
926       break;
927     case DW_CFA_def_cfa_expression:
928       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
929       break;
930
931     case DW_CFA_remember_state:
932       gcc_assert (!remember->in_use);
933       *remember = *loc;
934       remember->in_use = 1;
935       break;
936     case DW_CFA_restore_state:
937       gcc_assert (remember->in_use);
938       *loc = *remember;
939       remember->in_use = 0;
940       break;
941
942     default:
943       break;
944     }
945 }
946
947 /* Find the previous value for the CFA.  */
948
949 static void
950 lookup_cfa (dw_cfa_location *loc)
951 {
952   dw_cfi_ref cfi;
953   dw_fde_ref fde;
954   dw_cfa_location remember;
955
956   memset (loc, 0, sizeof (*loc));
957   loc->reg = INVALID_REGNUM;
958   remember = *loc;
959
960   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
961     lookup_cfa_1 (cfi, loc, &remember);
962
963   fde = current_fde ();
964   if (fde)
965     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
966       lookup_cfa_1 (cfi, loc, &remember);
967 }
968
969 /* The current rule for calculating the DWARF2 canonical frame address.  */
970 static dw_cfa_location cfa;
971
972 /* The register used for saving registers to the stack, and its offset
973    from the CFA.  */
974 static dw_cfa_location cfa_store;
975
976 /* The current save location around an epilogue.  */
977 static dw_cfa_location cfa_remember;
978
979 /* The running total of the size of arguments pushed onto the stack.  */
980 static HOST_WIDE_INT args_size;
981
982 /* The last args_size we actually output.  */
983 static HOST_WIDE_INT old_args_size;
984
985 /* Entry point to update the canonical frame address (CFA).
986    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
987    calculated from REG+OFFSET.  */
988
989 void
990 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
991 {
992   dw_cfa_location loc;
993   loc.indirect = 0;
994   loc.base_offset = 0;
995   loc.reg = reg;
996   loc.offset = offset;
997   def_cfa_1 (label, &loc);
998 }
999
1000 /* Determine if two dw_cfa_location structures define the same data.  */
1001
1002 static bool
1003 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1004 {
1005   return (loc1->reg == loc2->reg
1006           && loc1->offset == loc2->offset
1007           && loc1->indirect == loc2->indirect
1008           && (loc1->indirect == 0
1009               || loc1->base_offset == loc2->base_offset));
1010 }
1011
1012 /* This routine does the actual work.  The CFA is now calculated from
1013    the dw_cfa_location structure.  */
1014
1015 static void
1016 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1017 {
1018   dw_cfi_ref cfi;
1019   dw_cfa_location old_cfa, loc;
1020
1021   cfa = *loc_p;
1022   loc = *loc_p;
1023
1024   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1025     cfa_store.offset = loc.offset;
1026
1027   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1028   lookup_cfa (&old_cfa);
1029
1030   /* If nothing changed, no need to issue any call frame instructions.  */
1031   if (cfa_equal_p (&loc, &old_cfa))
1032     return;
1033
1034   cfi = new_cfi ();
1035
1036   if (loc.reg == old_cfa.reg && !loc.indirect)
1037     {
1038       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1039          the CFA register did not change but the offset did.  The data 
1040          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1041          in the assembler via the .cfi_def_cfa_offset directive.  */
1042       if (loc.offset < 0)
1043         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1044       else
1045         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1046       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1047     }
1048
1049 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1050   else if (loc.offset == old_cfa.offset
1051            && old_cfa.reg != INVALID_REGNUM
1052            && !loc.indirect)
1053     {
1054       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1055          indicating the CFA register has changed to <register> but the
1056          offset has not changed.  */
1057       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1058       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1059     }
1060 #endif
1061
1062   else if (loc.indirect == 0)
1063     {
1064       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1065          indicating the CFA register has changed to <register> with
1066          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1067          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1068          directive.  */
1069       if (loc.offset < 0)
1070         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1071       else
1072         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1073       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1074       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1075     }
1076   else
1077     {
1078       /* Construct a DW_CFA_def_cfa_expression instruction to
1079          calculate the CFA using a full location expression since no
1080          register-offset pair is available.  */
1081       struct dw_loc_descr_struct *loc_list;
1082
1083       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1084       loc_list = build_cfa_loc (&loc, 0);
1085       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1086     }
1087
1088   add_fde_cfi (label, cfi);
1089 }
1090
1091 /* Add the CFI for saving a register.  REG is the CFA column number.
1092    LABEL is passed to add_fde_cfi.
1093    If SREG is -1, the register is saved at OFFSET from the CFA;
1094    otherwise it is saved in SREG.  */
1095
1096 static void
1097 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1098 {
1099   dw_cfi_ref cfi = new_cfi ();
1100   dw_fde_ref fde = current_fde ();
1101
1102   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1103
1104   /* When stack is aligned, store REG using DW_CFA_expression with
1105      FP.  */
1106   if (fde
1107       && fde->stack_realign
1108       && sreg == INVALID_REGNUM)
1109     {
1110       cfi->dw_cfi_opc = DW_CFA_expression;
1111       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1112       cfi->dw_cfi_oprnd1.dw_cfi_loc
1113         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1114     }
1115   else if (sreg == INVALID_REGNUM)
1116     {
1117       if (need_data_align_sf_opcode (offset))
1118         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1119       else if (reg & ~0x3f)
1120         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1121       else
1122         cfi->dw_cfi_opc = DW_CFA_offset;
1123       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1124     }
1125   else if (sreg == reg)
1126     cfi->dw_cfi_opc = DW_CFA_same_value;
1127   else
1128     {
1129       cfi->dw_cfi_opc = DW_CFA_register;
1130       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1131     }
1132
1133   add_fde_cfi (label, cfi);
1134 }
1135
1136 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1137    This CFI tells the unwinder that it needs to restore the window registers
1138    from the previous frame's window save area.
1139
1140    ??? Perhaps we should note in the CIE where windows are saved (instead of
1141    assuming 0(cfa)) and what registers are in the window.  */
1142
1143 void
1144 dwarf2out_window_save (const char *label)
1145 {
1146   dw_cfi_ref cfi = new_cfi ();
1147
1148   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1149   add_fde_cfi (label, cfi);
1150 }
1151
1152 /* Add a CFI to update the running total of the size of arguments
1153    pushed onto the stack.  */
1154
1155 void
1156 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1157 {
1158   dw_cfi_ref cfi;
1159
1160   if (size == old_args_size)
1161     return;
1162
1163   old_args_size = size;
1164
1165   cfi = new_cfi ();
1166   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1167   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1168   add_fde_cfi (label, cfi);
1169 }
1170
1171 /* Entry point for saving a register to the stack.  REG is the GCC register
1172    number.  LABEL and OFFSET are passed to reg_save.  */
1173
1174 void
1175 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1176 {
1177   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1178 }
1179
1180 /* Entry point for saving the return address in the stack.
1181    LABEL and OFFSET are passed to reg_save.  */
1182
1183 void
1184 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1185 {
1186   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1187 }
1188
1189 /* Entry point for saving the return address in a register.
1190    LABEL and SREG are passed to reg_save.  */
1191
1192 void
1193 dwarf2out_return_reg (const char *label, unsigned int sreg)
1194 {
1195   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1196 }
1197
1198 #ifdef DWARF2_UNWIND_INFO
1199 /* Record the initial position of the return address.  RTL is
1200    INCOMING_RETURN_ADDR_RTX.  */
1201
1202 static void
1203 initial_return_save (rtx rtl)
1204 {
1205   unsigned int reg = INVALID_REGNUM;
1206   HOST_WIDE_INT offset = 0;
1207
1208   switch (GET_CODE (rtl))
1209     {
1210     case REG:
1211       /* RA is in a register.  */
1212       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1213       break;
1214
1215     case MEM:
1216       /* RA is on the stack.  */
1217       rtl = XEXP (rtl, 0);
1218       switch (GET_CODE (rtl))
1219         {
1220         case REG:
1221           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1222           offset = 0;
1223           break;
1224
1225         case PLUS:
1226           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1227           offset = INTVAL (XEXP (rtl, 1));
1228           break;
1229
1230         case MINUS:
1231           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1232           offset = -INTVAL (XEXP (rtl, 1));
1233           break;
1234
1235         default:
1236           gcc_unreachable ();
1237         }
1238
1239       break;
1240
1241     case PLUS:
1242       /* The return address is at some offset from any value we can
1243          actually load.  For instance, on the SPARC it is in %i7+8. Just
1244          ignore the offset for now; it doesn't matter for unwinding frames.  */
1245       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1246       initial_return_save (XEXP (rtl, 0));
1247       return;
1248
1249     default:
1250       gcc_unreachable ();
1251     }
1252
1253   if (reg != DWARF_FRAME_RETURN_COLUMN)
1254     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1255 }
1256 #endif
1257
1258 /* Given a SET, calculate the amount of stack adjustment it
1259    contains.  */
1260
1261 static HOST_WIDE_INT
1262 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1263                      HOST_WIDE_INT cur_offset)
1264 {
1265   const_rtx src = SET_SRC (pattern);
1266   const_rtx dest = SET_DEST (pattern);
1267   HOST_WIDE_INT offset = 0;
1268   enum rtx_code code;
1269
1270   if (dest == stack_pointer_rtx)
1271     {
1272       code = GET_CODE (src);
1273
1274       /* Assume (set (reg sp) (reg whatever)) sets args_size
1275          level to 0.  */
1276       if (code == REG && src != stack_pointer_rtx)
1277         {
1278           offset = -cur_args_size;
1279 #ifndef STACK_GROWS_DOWNWARD
1280           offset = -offset;
1281 #endif
1282           return offset - cur_offset;
1283         }
1284
1285       if (! (code == PLUS || code == MINUS)
1286           || XEXP (src, 0) != stack_pointer_rtx
1287           || !CONST_INT_P (XEXP (src, 1)))
1288         return 0;
1289
1290       /* (set (reg sp) (plus (reg sp) (const_int))) */
1291       offset = INTVAL (XEXP (src, 1));
1292       if (code == PLUS)
1293         offset = -offset;
1294       return offset;
1295     }
1296
1297   if (MEM_P (src) && !MEM_P (dest))
1298     dest = src;
1299   if (MEM_P (dest))
1300     {
1301       /* (set (mem (pre_dec (reg sp))) (foo)) */
1302       src = XEXP (dest, 0);
1303       code = GET_CODE (src);
1304
1305       switch (code)
1306         {
1307         case PRE_MODIFY:
1308         case POST_MODIFY:
1309           if (XEXP (src, 0) == stack_pointer_rtx)
1310             {
1311               rtx val = XEXP (XEXP (src, 1), 1);
1312               /* We handle only adjustments by constant amount.  */
1313               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1314                           && CONST_INT_P (val));
1315               offset = -INTVAL (val);
1316               break;
1317             }
1318           return 0;
1319
1320         case PRE_DEC:
1321         case POST_DEC:
1322           if (XEXP (src, 0) == stack_pointer_rtx)
1323             {
1324               offset = GET_MODE_SIZE (GET_MODE (dest));
1325               break;
1326             }
1327           return 0;
1328
1329         case PRE_INC:
1330         case POST_INC:
1331           if (XEXP (src, 0) == stack_pointer_rtx)
1332             {
1333               offset = -GET_MODE_SIZE (GET_MODE (dest));
1334               break;
1335             }
1336           return 0;
1337
1338         default:
1339           return 0;
1340         }
1341     }
1342   else
1343     return 0;
1344
1345   return offset;
1346 }
1347
1348 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1349    indexed by INSN_UID.  */
1350
1351 static HOST_WIDE_INT *barrier_args_size;
1352
1353 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1354
1355 static HOST_WIDE_INT
1356 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1357                              VEC (rtx, heap) **next)
1358 {
1359   HOST_WIDE_INT offset = 0;
1360   int i;
1361
1362   if (! RTX_FRAME_RELATED_P (insn))
1363     {
1364       if (prologue_epilogue_contains (insn))
1365         /* Nothing */;
1366       else if (GET_CODE (PATTERN (insn)) == SET)
1367         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1368       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1369                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1370         {
1371           /* There may be stack adjustments inside compound insns.  Search
1372              for them.  */
1373           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1374             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1375               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1376                                              cur_args_size, offset);
1377         }
1378     }
1379   else
1380     {
1381       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1382
1383       if (expr)
1384         {
1385           expr = XEXP (expr, 0);
1386           if (GET_CODE (expr) == PARALLEL
1387               || GET_CODE (expr) == SEQUENCE)
1388             for (i = 1; i < XVECLEN (expr, 0); i++)
1389               {
1390                 rtx elem = XVECEXP (expr, 0, i);
1391
1392                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1393                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1394               }
1395         }
1396     }
1397
1398 #ifndef STACK_GROWS_DOWNWARD
1399   offset = -offset;
1400 #endif
1401
1402   cur_args_size += offset;
1403   if (cur_args_size < 0)
1404     cur_args_size = 0;
1405
1406   if (JUMP_P (insn))
1407     {
1408       rtx dest = JUMP_LABEL (insn);
1409
1410       if (dest)
1411         {
1412           if (barrier_args_size [INSN_UID (dest)] < 0)
1413             {
1414               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1415               VEC_safe_push (rtx, heap, *next, dest);
1416             }
1417         }
1418     }
1419
1420   return cur_args_size;
1421 }
1422
1423 /* Walk the whole function and compute args_size on BARRIERs.  */
1424
1425 static void
1426 compute_barrier_args_size (void)
1427 {
1428   int max_uid = get_max_uid (), i;
1429   rtx insn;
1430   VEC (rtx, heap) *worklist, *next, *tmp;
1431
1432   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1433   for (i = 0; i < max_uid; i++)
1434     barrier_args_size[i] = -1;
1435
1436   worklist = VEC_alloc (rtx, heap, 20);
1437   next = VEC_alloc (rtx, heap, 20);
1438   insn = get_insns ();
1439   barrier_args_size[INSN_UID (insn)] = 0;
1440   VEC_quick_push (rtx, worklist, insn);
1441   for (;;)
1442     {
1443       while (!VEC_empty (rtx, worklist))
1444         {
1445           rtx prev, body, first_insn;
1446           HOST_WIDE_INT cur_args_size;
1447
1448           first_insn = insn = VEC_pop (rtx, worklist);
1449           cur_args_size = barrier_args_size[INSN_UID (insn)];
1450           prev = prev_nonnote_insn (insn);
1451           if (prev && BARRIER_P (prev))
1452             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1453
1454           for (; insn; insn = NEXT_INSN (insn))
1455             {
1456               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1457                 continue;
1458               if (BARRIER_P (insn))
1459                 break;
1460
1461               if (LABEL_P (insn))
1462                 {
1463                   if (insn == first_insn)
1464                     continue;
1465                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1466                     {
1467                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1468                       continue;
1469                     }
1470                   else
1471                     {
1472                       /* The insns starting with this label have been
1473                          already scanned or are in the worklist.  */
1474                       break;
1475                     }
1476                 }
1477
1478               body = PATTERN (insn);
1479               if (GET_CODE (body) == SEQUENCE)
1480                 {
1481                   HOST_WIDE_INT dest_args_size = cur_args_size;
1482                   for (i = 1; i < XVECLEN (body, 0); i++)
1483                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1484                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1485                       dest_args_size
1486                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1487                                                        dest_args_size, &next);
1488                     else
1489                       cur_args_size
1490                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1491                                                        cur_args_size, &next);
1492
1493                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1494                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1495                                                  dest_args_size, &next);
1496                   else
1497                     cur_args_size
1498                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1499                                                      cur_args_size, &next);
1500                 }
1501               else
1502                 cur_args_size
1503                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1504             }
1505         }
1506
1507       if (VEC_empty (rtx, next))
1508         break;
1509
1510       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1511       tmp = next;
1512       next = worklist;
1513       worklist = tmp;
1514       VEC_truncate (rtx, next, 0);
1515     }
1516
1517   VEC_free (rtx, heap, worklist);
1518   VEC_free (rtx, heap, next);
1519 }
1520
1521
1522 /* Check INSN to see if it looks like a push or a stack adjustment, and
1523    make a note of it if it does.  EH uses this information to find out how
1524    much extra space it needs to pop off the stack.  */
1525
1526 static void
1527 dwarf2out_stack_adjust (rtx insn, bool after_p)
1528 {
1529   HOST_WIDE_INT offset;
1530   const char *label;
1531   int i;
1532
1533   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1534      with this function.  Proper support would require all frame-related
1535      insns to be marked, and to be able to handle saving state around
1536      epilogues textually in the middle of the function.  */
1537   if (prologue_epilogue_contains (insn))
1538     return;
1539
1540   /* If INSN is an instruction from target of an annulled branch, the
1541      effects are for the target only and so current argument size
1542      shouldn't change at all.  */
1543   if (final_sequence
1544       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1545       && INSN_FROM_TARGET_P (insn))
1546     return;
1547
1548   /* If only calls can throw, and we have a frame pointer,
1549      save up adjustments until we see the CALL_INSN.  */
1550   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1551     {
1552       if (CALL_P (insn) && !after_p)
1553         {
1554           /* Extract the size of the args from the CALL rtx itself.  */
1555           insn = PATTERN (insn);
1556           if (GET_CODE (insn) == PARALLEL)
1557             insn = XVECEXP (insn, 0, 0);
1558           if (GET_CODE (insn) == SET)
1559             insn = SET_SRC (insn);
1560           gcc_assert (GET_CODE (insn) == CALL);
1561           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1562         }
1563       return;
1564     }
1565
1566   if (CALL_P (insn) && !after_p)
1567     {
1568       if (!flag_asynchronous_unwind_tables)
1569         dwarf2out_args_size ("", args_size);
1570       return;
1571     }
1572   else if (BARRIER_P (insn))
1573     {
1574       /* Don't call compute_barrier_args_size () if the only
1575          BARRIER is at the end of function.  */
1576       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1577         compute_barrier_args_size ();
1578       if (barrier_args_size == NULL)
1579         offset = 0;
1580       else
1581         {
1582           offset = barrier_args_size[INSN_UID (insn)];
1583           if (offset < 0)
1584             offset = 0;
1585         }
1586
1587       offset -= args_size;
1588 #ifndef STACK_GROWS_DOWNWARD
1589       offset = -offset;
1590 #endif
1591     }
1592   else if (GET_CODE (PATTERN (insn)) == SET)
1593     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1594   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1595            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1596     {
1597       /* There may be stack adjustments inside compound insns.  Search
1598          for them.  */
1599       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1600         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1601           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1602                                          args_size, offset);
1603     }
1604   else
1605     return;
1606
1607   if (offset == 0)
1608     return;
1609
1610   label = dwarf2out_cfi_label (false);
1611   dwarf2out_args_size_adjust (offset, label);
1612 }
1613
1614 /* Adjust args_size based on stack adjustment OFFSET.  */
1615
1616 static void
1617 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1618 {
1619   if (cfa.reg == STACK_POINTER_REGNUM)
1620     cfa.offset += offset;
1621
1622   if (cfa_store.reg == STACK_POINTER_REGNUM)
1623     cfa_store.offset += offset;
1624
1625 #ifndef STACK_GROWS_DOWNWARD
1626   offset = -offset;
1627 #endif
1628
1629   args_size += offset;
1630   if (args_size < 0)
1631     args_size = 0;
1632
1633   def_cfa_1 (label, &cfa);
1634   if (flag_asynchronous_unwind_tables)
1635     dwarf2out_args_size (label, args_size);
1636 }
1637
1638 #endif
1639
1640 /* We delay emitting a register save until either (a) we reach the end
1641    of the prologue or (b) the register is clobbered.  This clusters
1642    register saves so that there are fewer pc advances.  */
1643
1644 struct GTY(()) queued_reg_save {
1645   struct queued_reg_save *next;
1646   rtx reg;
1647   HOST_WIDE_INT cfa_offset;
1648   rtx saved_reg;
1649 };
1650
1651 static GTY(()) struct queued_reg_save *queued_reg_saves;
1652
1653 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1654 struct GTY(()) reg_saved_in_data {
1655   rtx orig_reg;
1656   rtx saved_in_reg;
1657 };
1658
1659 /* A list of registers saved in other registers.
1660    The list intentionally has a small maximum capacity of 4; if your
1661    port needs more than that, you might consider implementing a
1662    more efficient data structure.  */
1663 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1664 static GTY(()) size_t num_regs_saved_in_regs;
1665
1666 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1667 static const char *last_reg_save_label;
1668
1669 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1670    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1671
1672 static void
1673 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1674 {
1675   struct queued_reg_save *q;
1676
1677   /* Duplicates waste space, but it's also necessary to remove them
1678      for correctness, since the queue gets output in reverse
1679      order.  */
1680   for (q = queued_reg_saves; q != NULL; q = q->next)
1681     if (REGNO (q->reg) == REGNO (reg))
1682       break;
1683
1684   if (q == NULL)
1685     {
1686       q = GGC_NEW (struct queued_reg_save);
1687       q->next = queued_reg_saves;
1688       queued_reg_saves = q;
1689     }
1690
1691   q->reg = reg;
1692   q->cfa_offset = offset;
1693   q->saved_reg = sreg;
1694
1695   last_reg_save_label = label;
1696 }
1697
1698 /* Output all the entries in QUEUED_REG_SAVES.  */
1699
1700 static void
1701 flush_queued_reg_saves (void)
1702 {
1703   struct queued_reg_save *q;
1704
1705   for (q = queued_reg_saves; q; q = q->next)
1706     {
1707       size_t i;
1708       unsigned int reg, sreg;
1709
1710       for (i = 0; i < num_regs_saved_in_regs; i++)
1711         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1712           break;
1713       if (q->saved_reg && i == num_regs_saved_in_regs)
1714         {
1715           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1716           num_regs_saved_in_regs++;
1717         }
1718       if (i != num_regs_saved_in_regs)
1719         {
1720           regs_saved_in_regs[i].orig_reg = q->reg;
1721           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1722         }
1723
1724       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1725       if (q->saved_reg)
1726         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1727       else
1728         sreg = INVALID_REGNUM;
1729       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1730     }
1731
1732   queued_reg_saves = NULL;
1733   last_reg_save_label = NULL;
1734 }
1735
1736 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1737    location for?  Or, does it clobber a register which we've previously
1738    said that some other register is saved in, and for which we now
1739    have a new location for?  */
1740
1741 static bool
1742 clobbers_queued_reg_save (const_rtx insn)
1743 {
1744   struct queued_reg_save *q;
1745
1746   for (q = queued_reg_saves; q; q = q->next)
1747     {
1748       size_t i;
1749       if (modified_in_p (q->reg, insn))
1750         return true;
1751       for (i = 0; i < num_regs_saved_in_regs; i++)
1752         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1753             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1754           return true;
1755     }
1756
1757   return false;
1758 }
1759
1760 /* Entry point for saving the first register into the second.  */
1761
1762 void
1763 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1764 {
1765   size_t i;
1766   unsigned int regno, sregno;
1767
1768   for (i = 0; i < num_regs_saved_in_regs; i++)
1769     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1770       break;
1771   if (i == num_regs_saved_in_regs)
1772     {
1773       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1774       num_regs_saved_in_regs++;
1775     }
1776   regs_saved_in_regs[i].orig_reg = reg;
1777   regs_saved_in_regs[i].saved_in_reg = sreg;
1778
1779   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1780   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1781   reg_save (label, regno, sregno, 0);
1782 }
1783
1784 /* What register, if any, is currently saved in REG?  */
1785
1786 static rtx
1787 reg_saved_in (rtx reg)
1788 {
1789   unsigned int regn = REGNO (reg);
1790   size_t i;
1791   struct queued_reg_save *q;
1792
1793   for (q = queued_reg_saves; q; q = q->next)
1794     if (q->saved_reg && regn == REGNO (q->saved_reg))
1795       return q->reg;
1796
1797   for (i = 0; i < num_regs_saved_in_regs; i++)
1798     if (regs_saved_in_regs[i].saved_in_reg
1799         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1800       return regs_saved_in_regs[i].orig_reg;
1801
1802   return NULL_RTX;
1803 }
1804
1805
1806 /* A temporary register holding an integral value used in adjusting SP
1807    or setting up the store_reg.  The "offset" field holds the integer
1808    value, not an offset.  */
1809 static dw_cfa_location cfa_temp;
1810
1811 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1812
1813 static void
1814 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1815 {
1816   memset (&cfa, 0, sizeof (cfa));
1817
1818   switch (GET_CODE (pat))
1819     {
1820     case PLUS:
1821       cfa.reg = REGNO (XEXP (pat, 0));
1822       cfa.offset = INTVAL (XEXP (pat, 1));
1823       break;
1824
1825     case REG:
1826       cfa.reg = REGNO (pat);
1827       break;
1828
1829     default:
1830       /* Recurse and define an expression.  */
1831       gcc_unreachable ();
1832     }
1833
1834   def_cfa_1 (label, &cfa);
1835 }
1836
1837 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1838
1839 static void
1840 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1841 {
1842   rtx src, dest;
1843
1844   gcc_assert (GET_CODE (pat) == SET);
1845   dest = XEXP (pat, 0);
1846   src = XEXP (pat, 1);
1847
1848   switch (GET_CODE (src))
1849     {
1850     case PLUS:
1851       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1852       cfa.offset -= INTVAL (XEXP (src, 1));
1853       break;
1854
1855     case REG:
1856         break;
1857
1858     default:
1859         gcc_unreachable ();
1860     }
1861
1862   cfa.reg = REGNO (dest);
1863   gcc_assert (cfa.indirect == 0);
1864
1865   def_cfa_1 (label, &cfa);
1866 }
1867
1868 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1869
1870 static void
1871 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1872 {
1873   HOST_WIDE_INT offset;
1874   rtx src, addr, span;
1875
1876   src = XEXP (set, 1);
1877   addr = XEXP (set, 0);
1878   gcc_assert (MEM_P (addr));
1879   addr = XEXP (addr, 0);
1880   
1881   /* As documented, only consider extremely simple addresses.  */
1882   switch (GET_CODE (addr))
1883     {
1884     case REG:
1885       gcc_assert (REGNO (addr) == cfa.reg);
1886       offset = -cfa.offset;
1887       break;
1888     case PLUS:
1889       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1890       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1891       break;
1892     default:
1893       gcc_unreachable ();
1894     }
1895
1896   span = targetm.dwarf_register_span (src);
1897
1898   /* ??? We'd like to use queue_reg_save, but we need to come up with
1899      a different flushing heuristic for epilogues.  */
1900   if (!span)
1901     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1902   else
1903     {
1904       /* We have a PARALLEL describing where the contents of SRC live.
1905          Queue register saves for each piece of the PARALLEL.  */
1906       int par_index;
1907       int limit;
1908       HOST_WIDE_INT span_offset = offset;
1909
1910       gcc_assert (GET_CODE (span) == PARALLEL);
1911
1912       limit = XVECLEN (span, 0);
1913       for (par_index = 0; par_index < limit; par_index++)
1914         {
1915           rtx elem = XVECEXP (span, 0, par_index);
1916
1917           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1918                     INVALID_REGNUM, span_offset);
1919           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1920         }
1921     }
1922 }
1923
1924 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1925
1926 static void
1927 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1928 {
1929   rtx src, dest;
1930   unsigned sregno, dregno;
1931
1932   src = XEXP (set, 1);
1933   dest = XEXP (set, 0);
1934
1935   if (src == pc_rtx)
1936     sregno = DWARF_FRAME_RETURN_COLUMN;
1937   else
1938     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1939
1940   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1941
1942   /* ??? We'd like to use queue_reg_save, but we need to come up with
1943      a different flushing heuristic for epilogues.  */
1944   reg_save (label, sregno, dregno, 0);
1945 }
1946
1947 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1948
1949 static void
1950 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1951 {
1952   dw_cfi_ref cfi = new_cfi ();
1953   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1954
1955   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1956   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1957
1958   add_fde_cfi (label, cfi);
1959 }
1960
1961 /* Record call frame debugging information for an expression EXPR,
1962    which either sets SP or FP (adjusting how we calculate the frame
1963    address) or saves a register to the stack or another register.
1964    LABEL indicates the address of EXPR.
1965
1966    This function encodes a state machine mapping rtxes to actions on
1967    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1968    users need not read the source code.
1969
1970   The High-Level Picture
1971
1972   Changes in the register we use to calculate the CFA: Currently we
1973   assume that if you copy the CFA register into another register, we
1974   should take the other one as the new CFA register; this seems to
1975   work pretty well.  If it's wrong for some target, it's simple
1976   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1977
1978   Changes in the register we use for saving registers to the stack:
1979   This is usually SP, but not always.  Again, we deduce that if you
1980   copy SP into another register (and SP is not the CFA register),
1981   then the new register is the one we will be using for register
1982   saves.  This also seems to work.
1983
1984   Register saves: There's not much guesswork about this one; if
1985   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1986   register save, and the register used to calculate the destination
1987   had better be the one we think we're using for this purpose.
1988   It's also assumed that a copy from a call-saved register to another
1989   register is saving that register if RTX_FRAME_RELATED_P is set on
1990   that instruction.  If the copy is from a call-saved register to
1991   the *same* register, that means that the register is now the same
1992   value as in the caller.
1993
1994   Except: If the register being saved is the CFA register, and the
1995   offset is nonzero, we are saving the CFA, so we assume we have to
1996   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1997   the intent is to save the value of SP from the previous frame.
1998
1999   In addition, if a register has previously been saved to a different
2000   register,
2001
2002   Invariants / Summaries of Rules
2003
2004   cfa          current rule for calculating the CFA.  It usually
2005                consists of a register and an offset.
2006   cfa_store    register used by prologue code to save things to the stack
2007                cfa_store.offset is the offset from the value of
2008                cfa_store.reg to the actual CFA
2009   cfa_temp     register holding an integral value.  cfa_temp.offset
2010                stores the value, which will be used to adjust the
2011                stack pointer.  cfa_temp is also used like cfa_store,
2012                to track stores to the stack via fp or a temp reg.
2013
2014   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2015                with cfa.reg as the first operand changes the cfa.reg and its
2016                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2017                cfa_temp.offset.
2018
2019   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2020                expression yielding a constant.  This sets cfa_temp.reg
2021                and cfa_temp.offset.
2022
2023   Rule 5:      Create a new register cfa_store used to save items to the
2024                stack.
2025
2026   Rules 10-14: Save a register to the stack.  Define offset as the
2027                difference of the original location and cfa_store's
2028                location (or cfa_temp's location if cfa_temp is used).
2029
2030   Rules 16-20: If AND operation happens on sp in prologue, we assume
2031                stack is realigned.  We will use a group of DW_OP_XXX
2032                expressions to represent the location of the stored
2033                register instead of CFA+offset.
2034
2035   The Rules
2036
2037   "{a,b}" indicates a choice of a xor b.
2038   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2039
2040   Rule 1:
2041   (set <reg1> <reg2>:cfa.reg)
2042   effects: cfa.reg = <reg1>
2043            cfa.offset unchanged
2044            cfa_temp.reg = <reg1>
2045            cfa_temp.offset = cfa.offset
2046
2047   Rule 2:
2048   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2049                               {<const_int>,<reg>:cfa_temp.reg}))
2050   effects: cfa.reg = sp if fp used
2051            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2052            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2053              if cfa_store.reg==sp
2054
2055   Rule 3:
2056   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2057   effects: cfa.reg = fp
2058            cfa_offset += +/- <const_int>
2059
2060   Rule 4:
2061   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2062   constraints: <reg1> != fp
2063                <reg1> != sp
2064   effects: cfa.reg = <reg1>
2065            cfa_temp.reg = <reg1>
2066            cfa_temp.offset = cfa.offset
2067
2068   Rule 5:
2069   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2070   constraints: <reg1> != fp
2071                <reg1> != sp
2072   effects: cfa_store.reg = <reg1>
2073            cfa_store.offset = cfa.offset - cfa_temp.offset
2074
2075   Rule 6:
2076   (set <reg> <const_int>)
2077   effects: cfa_temp.reg = <reg>
2078            cfa_temp.offset = <const_int>
2079
2080   Rule 7:
2081   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2082   effects: cfa_temp.reg = <reg1>
2083            cfa_temp.offset |= <const_int>
2084
2085   Rule 8:
2086   (set <reg> (high <exp>))
2087   effects: none
2088
2089   Rule 9:
2090   (set <reg> (lo_sum <exp> <const_int>))
2091   effects: cfa_temp.reg = <reg>
2092            cfa_temp.offset = <const_int>
2093
2094   Rule 10:
2095   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2096   effects: cfa_store.offset -= <const_int>
2097            cfa.offset = cfa_store.offset if cfa.reg == sp
2098            cfa.reg = sp
2099            cfa.base_offset = -cfa_store.offset
2100
2101   Rule 11:
2102   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2103   effects: cfa_store.offset += -/+ mode_size(mem)
2104            cfa.offset = cfa_store.offset if cfa.reg == sp
2105            cfa.reg = sp
2106            cfa.base_offset = -cfa_store.offset
2107
2108   Rule 12:
2109   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2110
2111        <reg2>)
2112   effects: cfa.reg = <reg1>
2113            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2114
2115   Rule 13:
2116   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2117   effects: cfa.reg = <reg1>
2118            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2119
2120   Rule 14:
2121   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2122   effects: cfa.reg = <reg1>
2123            cfa.base_offset = -cfa_temp.offset
2124            cfa_temp.offset -= mode_size(mem)
2125
2126   Rule 15:
2127   (set <reg> {unspec, unspec_volatile})
2128   effects: target-dependent
2129
2130   Rule 16:
2131   (set sp (and: sp <const_int>))
2132   constraints: cfa_store.reg == sp
2133   effects: current_fde.stack_realign = 1
2134            cfa_store.offset = 0
2135            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2136
2137   Rule 17:
2138   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2139   effects: cfa_store.offset += -/+ mode_size(mem)
2140
2141   Rule 18:
2142   (set (mem ({pre_inc, pre_dec} sp)) fp)
2143   constraints: fde->stack_realign == 1
2144   effects: cfa_store.offset = 0
2145            cfa.reg != HARD_FRAME_POINTER_REGNUM
2146
2147   Rule 19:
2148   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2149   constraints: fde->stack_realign == 1
2150                && cfa.offset == 0
2151                && cfa.indirect == 0
2152                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2153   effects: Use DW_CFA_def_cfa_expression to define cfa
2154            cfa.reg == fde->drap_reg
2155
2156   Rule 20:
2157   (set reg fde->drap_reg)
2158   constraints: fde->vdrap_reg == INVALID_REGNUM
2159   effects: fde->vdrap_reg = reg.
2160   (set mem fde->drap_reg)
2161   constraints: fde->drap_reg_saved == 1
2162   effects: none.  */
2163
2164 static void
2165 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2166 {
2167   rtx src, dest, span;
2168   HOST_WIDE_INT offset;
2169   dw_fde_ref fde;
2170
2171   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2172      the PARALLEL independently. The first element is always processed if
2173      it is a SET. This is for backward compatibility.   Other elements
2174      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2175      flag is set in them.  */
2176   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2177     {
2178       int par_index;
2179       int limit = XVECLEN (expr, 0);
2180       rtx elem;
2181
2182       /* PARALLELs have strict read-modify-write semantics, so we
2183          ought to evaluate every rvalue before changing any lvalue.
2184          It's cumbersome to do that in general, but there's an
2185          easy approximation that is enough for all current users:
2186          handle register saves before register assignments.  */
2187       if (GET_CODE (expr) == PARALLEL)
2188         for (par_index = 0; par_index < limit; par_index++)
2189           {
2190             elem = XVECEXP (expr, 0, par_index);
2191             if (GET_CODE (elem) == SET
2192                 && MEM_P (SET_DEST (elem))
2193                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2194               dwarf2out_frame_debug_expr (elem, label);
2195           }
2196
2197       for (par_index = 0; par_index < limit; par_index++)
2198         {
2199           elem = XVECEXP (expr, 0, par_index);
2200           if (GET_CODE (elem) == SET
2201               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2202               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2203             dwarf2out_frame_debug_expr (elem, label);
2204           else if (GET_CODE (elem) == SET
2205                    && par_index != 0
2206                    && !RTX_FRAME_RELATED_P (elem))
2207             {
2208               /* Stack adjustment combining might combine some post-prologue
2209                  stack adjustment into a prologue stack adjustment.  */
2210               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2211
2212               if (offset != 0)
2213                 dwarf2out_args_size_adjust (offset, label);
2214             }
2215         }
2216       return;
2217     }
2218
2219   gcc_assert (GET_CODE (expr) == SET);
2220
2221   src = SET_SRC (expr);
2222   dest = SET_DEST (expr);
2223
2224   if (REG_P (src))
2225     {
2226       rtx rsi = reg_saved_in (src);
2227       if (rsi)
2228         src = rsi;
2229     }
2230
2231   fde = current_fde ();
2232
2233   if (REG_P (src)
2234       && fde
2235       && fde->drap_reg == REGNO (src)
2236       && (fde->drap_reg_saved
2237           || REG_P (dest)))
2238     {
2239       /* Rule 20 */
2240       /* If we are saving dynamic realign argument pointer to a
2241          register, the destination is virtual dynamic realign
2242          argument pointer.  It may be used to access argument.  */
2243       if (REG_P (dest))
2244         {
2245           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2246           fde->vdrap_reg = REGNO (dest);
2247         }
2248       return;
2249     }
2250
2251   switch (GET_CODE (dest))
2252     {
2253     case REG:
2254       switch (GET_CODE (src))
2255         {
2256           /* Setting FP from SP.  */
2257         case REG:
2258           if (cfa.reg == (unsigned) REGNO (src))
2259             {
2260               /* Rule 1 */
2261               /* Update the CFA rule wrt SP or FP.  Make sure src is
2262                  relative to the current CFA register.
2263
2264                  We used to require that dest be either SP or FP, but the
2265                  ARM copies SP to a temporary register, and from there to
2266                  FP.  So we just rely on the backends to only set
2267                  RTX_FRAME_RELATED_P on appropriate insns.  */
2268               cfa.reg = REGNO (dest);
2269               cfa_temp.reg = cfa.reg;
2270               cfa_temp.offset = cfa.offset;
2271             }
2272           else
2273             {
2274               /* Saving a register in a register.  */
2275               gcc_assert (!fixed_regs [REGNO (dest)]
2276                           /* For the SPARC and its register window.  */
2277                           || (DWARF_FRAME_REGNUM (REGNO (src))
2278                               == DWARF_FRAME_RETURN_COLUMN));
2279
2280               /* After stack is aligned, we can only save SP in FP
2281                  if drap register is used.  In this case, we have
2282                  to restore stack pointer with the CFA value and we
2283                  don't generate this DWARF information.  */
2284               if (fde
2285                   && fde->stack_realign
2286                   && REGNO (src) == STACK_POINTER_REGNUM)
2287                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2288                             && fde->drap_reg != INVALID_REGNUM
2289                             && cfa.reg != REGNO (src));
2290               else
2291                 queue_reg_save (label, src, dest, 0);
2292             }
2293           break;
2294
2295         case PLUS:
2296         case MINUS:
2297         case LO_SUM:
2298           if (dest == stack_pointer_rtx)
2299             {
2300               /* Rule 2 */
2301               /* Adjusting SP.  */
2302               switch (GET_CODE (XEXP (src, 1)))
2303                 {
2304                 case CONST_INT:
2305                   offset = INTVAL (XEXP (src, 1));
2306                   break;
2307                 case REG:
2308                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2309                               == cfa_temp.reg);
2310                   offset = cfa_temp.offset;
2311                   break;
2312                 default:
2313                   gcc_unreachable ();
2314                 }
2315
2316               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2317                 {
2318                   /* Restoring SP from FP in the epilogue.  */
2319                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2320                   cfa.reg = STACK_POINTER_REGNUM;
2321                 }
2322               else if (GET_CODE (src) == LO_SUM)
2323                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2324                 ;
2325               else
2326                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2327
2328               if (GET_CODE (src) != MINUS)
2329                 offset = -offset;
2330               if (cfa.reg == STACK_POINTER_REGNUM)
2331                 cfa.offset += offset;
2332               if (cfa_store.reg == STACK_POINTER_REGNUM)
2333                 cfa_store.offset += offset;
2334             }
2335           else if (dest == hard_frame_pointer_rtx)
2336             {
2337               /* Rule 3 */
2338               /* Either setting the FP from an offset of the SP,
2339                  or adjusting the FP */
2340               gcc_assert (frame_pointer_needed);
2341
2342               gcc_assert (REG_P (XEXP (src, 0))
2343                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2344                           && CONST_INT_P (XEXP (src, 1)));
2345               offset = INTVAL (XEXP (src, 1));
2346               if (GET_CODE (src) != MINUS)
2347                 offset = -offset;
2348               cfa.offset += offset;
2349               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2350             }
2351           else
2352             {
2353               gcc_assert (GET_CODE (src) != MINUS);
2354
2355               /* Rule 4 */
2356               if (REG_P (XEXP (src, 0))
2357                   && REGNO (XEXP (src, 0)) == cfa.reg
2358                   && CONST_INT_P (XEXP (src, 1)))
2359                 {
2360                   /* Setting a temporary CFA register that will be copied
2361                      into the FP later on.  */
2362                   offset = - INTVAL (XEXP (src, 1));
2363                   cfa.offset += offset;
2364                   cfa.reg = REGNO (dest);
2365                   /* Or used to save regs to the stack.  */
2366                   cfa_temp.reg = cfa.reg;
2367                   cfa_temp.offset = cfa.offset;
2368                 }
2369
2370               /* Rule 5 */
2371               else if (REG_P (XEXP (src, 0))
2372                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2373                        && XEXP (src, 1) == stack_pointer_rtx)
2374                 {
2375                   /* Setting a scratch register that we will use instead
2376                      of SP for saving registers to the stack.  */
2377                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2378                   cfa_store.reg = REGNO (dest);
2379                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2380                 }
2381
2382               /* Rule 9 */
2383               else if (GET_CODE (src) == LO_SUM
2384                        && CONST_INT_P (XEXP (src, 1)))
2385                 {
2386                   cfa_temp.reg = REGNO (dest);
2387                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2388                 }
2389               else
2390                 gcc_unreachable ();
2391             }
2392           break;
2393
2394           /* Rule 6 */
2395         case CONST_INT:
2396           cfa_temp.reg = REGNO (dest);
2397           cfa_temp.offset = INTVAL (src);
2398           break;
2399
2400           /* Rule 7 */
2401         case IOR:
2402           gcc_assert (REG_P (XEXP (src, 0))
2403                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2404                       && CONST_INT_P (XEXP (src, 1)));
2405
2406           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2407             cfa_temp.reg = REGNO (dest);
2408           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2409           break;
2410
2411           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2412              which will fill in all of the bits.  */
2413           /* Rule 8 */
2414         case HIGH:
2415           break;
2416
2417           /* Rule 15 */
2418         case UNSPEC:
2419         case UNSPEC_VOLATILE:
2420           gcc_assert (targetm.dwarf_handle_frame_unspec);
2421           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2422           return;
2423
2424           /* Rule 16 */
2425         case AND:
2426           /* If this AND operation happens on stack pointer in prologue,
2427              we assume the stack is realigned and we extract the
2428              alignment.  */
2429           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2430             {
2431               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2432               fde->stack_realign = 1;
2433               fde->stack_realignment = INTVAL (XEXP (src, 1));
2434               cfa_store.offset = 0;
2435
2436               if (cfa.reg != STACK_POINTER_REGNUM
2437                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2438                 fde->drap_reg = cfa.reg;
2439             }
2440           return;
2441
2442         default:
2443           gcc_unreachable ();
2444         }
2445
2446       def_cfa_1 (label, &cfa);
2447       break;
2448
2449     case MEM:
2450
2451       /* Saving a register to the stack.  Make sure dest is relative to the
2452          CFA register.  */
2453       switch (GET_CODE (XEXP (dest, 0)))
2454         {
2455           /* Rule 10 */
2456           /* With a push.  */
2457         case PRE_MODIFY:
2458           /* We can't handle variable size modifications.  */
2459           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2460                       == CONST_INT);
2461           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2462
2463           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2464                       && cfa_store.reg == STACK_POINTER_REGNUM);
2465
2466           cfa_store.offset += offset;
2467           if (cfa.reg == STACK_POINTER_REGNUM)
2468             cfa.offset = cfa_store.offset;
2469
2470           offset = -cfa_store.offset;
2471           break;
2472
2473           /* Rule 11 */
2474         case PRE_INC:
2475         case PRE_DEC:
2476           offset = GET_MODE_SIZE (GET_MODE (dest));
2477           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2478             offset = -offset;
2479
2480           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2481                        == STACK_POINTER_REGNUM)
2482                       && cfa_store.reg == STACK_POINTER_REGNUM);
2483
2484           cfa_store.offset += offset;
2485
2486           /* Rule 18: If stack is aligned, we will use FP as a
2487              reference to represent the address of the stored
2488              regiser.  */
2489           if (fde
2490               && fde->stack_realign
2491               && src == hard_frame_pointer_rtx)
2492             {
2493               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2494               cfa_store.offset = 0;
2495             }
2496
2497           if (cfa.reg == STACK_POINTER_REGNUM)
2498             cfa.offset = cfa_store.offset;
2499
2500           offset = -cfa_store.offset;
2501           break;
2502
2503           /* Rule 12 */
2504           /* With an offset.  */
2505         case PLUS:
2506         case MINUS:
2507         case LO_SUM:
2508           {
2509             int regno;
2510
2511             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2512                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2513             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2514             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2515               offset = -offset;
2516
2517             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2518
2519             if (cfa_store.reg == (unsigned) regno)
2520               offset -= cfa_store.offset;
2521             else
2522               {
2523                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2524                 offset -= cfa_temp.offset;
2525               }
2526           }
2527           break;
2528
2529           /* Rule 13 */
2530           /* Without an offset.  */
2531         case REG:
2532           {
2533             int regno = REGNO (XEXP (dest, 0));
2534
2535             if (cfa_store.reg == (unsigned) regno)
2536               offset = -cfa_store.offset;
2537             else
2538               {
2539                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2540                 offset = -cfa_temp.offset;
2541               }
2542           }
2543           break;
2544
2545           /* Rule 14 */
2546         case POST_INC:
2547           gcc_assert (cfa_temp.reg
2548                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2549           offset = -cfa_temp.offset;
2550           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2551           break;
2552
2553         default:
2554           gcc_unreachable ();
2555         }
2556
2557         /* Rule 17 */
2558         /* If the source operand of this MEM operation is not a
2559            register, basically the source is return address.  Here
2560            we only care how much stack grew and we don't save it.  */
2561       if (!REG_P (src))
2562         break;
2563
2564       if (REGNO (src) != STACK_POINTER_REGNUM
2565           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2566           && (unsigned) REGNO (src) == cfa.reg)
2567         {
2568           /* We're storing the current CFA reg into the stack.  */
2569
2570           if (cfa.offset == 0)
2571             {
2572               /* Rule 19 */
2573               /* If stack is aligned, putting CFA reg into stack means
2574                  we can no longer use reg + offset to represent CFA.
2575                  Here we use DW_CFA_def_cfa_expression instead.  The
2576                  result of this expression equals to the original CFA
2577                  value.  */
2578               if (fde
2579                   && fde->stack_realign
2580                   && cfa.indirect == 0
2581                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2582                 {
2583                   dw_cfa_location cfa_exp;
2584
2585                   gcc_assert (fde->drap_reg == cfa.reg);
2586
2587                   cfa_exp.indirect = 1;
2588                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2589                   cfa_exp.base_offset = offset;
2590                   cfa_exp.offset = 0;
2591
2592                   fde->drap_reg_saved = 1;
2593
2594                   def_cfa_1 (label, &cfa_exp);
2595                   break;
2596                 }
2597
2598               /* If the source register is exactly the CFA, assume
2599                  we're saving SP like any other register; this happens
2600                  on the ARM.  */
2601               def_cfa_1 (label, &cfa);
2602               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2603               break;
2604             }
2605           else
2606             {
2607               /* Otherwise, we'll need to look in the stack to
2608                  calculate the CFA.  */
2609               rtx x = XEXP (dest, 0);
2610
2611               if (!REG_P (x))
2612                 x = XEXP (x, 0);
2613               gcc_assert (REG_P (x));
2614
2615               cfa.reg = REGNO (x);
2616               cfa.base_offset = offset;
2617               cfa.indirect = 1;
2618               def_cfa_1 (label, &cfa);
2619               break;
2620             }
2621         }
2622
2623       def_cfa_1 (label, &cfa);
2624       {
2625         span = targetm.dwarf_register_span (src);
2626
2627         if (!span)
2628           queue_reg_save (label, src, NULL_RTX, offset);
2629         else
2630           {
2631             /* We have a PARALLEL describing where the contents of SRC
2632                live.  Queue register saves for each piece of the
2633                PARALLEL.  */
2634             int par_index;
2635             int limit;
2636             HOST_WIDE_INT span_offset = offset;
2637
2638             gcc_assert (GET_CODE (span) == PARALLEL);
2639
2640             limit = XVECLEN (span, 0);
2641             for (par_index = 0; par_index < limit; par_index++)
2642               {
2643                 rtx elem = XVECEXP (span, 0, par_index);
2644
2645                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2646                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2647               }
2648           }
2649       }
2650       break;
2651
2652     default:
2653       gcc_unreachable ();
2654     }
2655 }
2656
2657 /* Record call frame debugging information for INSN, which either
2658    sets SP or FP (adjusting how we calculate the frame address) or saves a
2659    register to the stack.  If INSN is NULL_RTX, initialize our state.
2660
2661    If AFTER_P is false, we're being called before the insn is emitted,
2662    otherwise after.  Call instructions get invoked twice.  */
2663
2664 void
2665 dwarf2out_frame_debug (rtx insn, bool after_p)
2666 {
2667   const char *label;
2668   rtx note, n;
2669   bool handled_one = false;
2670
2671   if (insn == NULL_RTX)
2672     {
2673       size_t i;
2674
2675       /* Flush any queued register saves.  */
2676       flush_queued_reg_saves ();
2677
2678       /* Set up state for generating call frame debug info.  */
2679       lookup_cfa (&cfa);
2680       gcc_assert (cfa.reg
2681                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2682
2683       cfa.reg = STACK_POINTER_REGNUM;
2684       cfa_store = cfa;
2685       cfa_temp.reg = -1;
2686       cfa_temp.offset = 0;
2687
2688       for (i = 0; i < num_regs_saved_in_regs; i++)
2689         {
2690           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2691           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2692         }
2693       num_regs_saved_in_regs = 0;
2694
2695       if (barrier_args_size)
2696         {
2697           XDELETEVEC (barrier_args_size);
2698           barrier_args_size = NULL;
2699         }
2700       return;
2701     }
2702
2703   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2704     flush_queued_reg_saves ();
2705
2706   if (! RTX_FRAME_RELATED_P (insn))
2707     {
2708       if (!ACCUMULATE_OUTGOING_ARGS)
2709         dwarf2out_stack_adjust (insn, after_p);
2710       return;
2711     }
2712
2713   label = dwarf2out_cfi_label (false);
2714
2715   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2716     switch (REG_NOTE_KIND (note))
2717       {
2718       case REG_FRAME_RELATED_EXPR:
2719         insn = XEXP (note, 0);
2720         goto found;
2721
2722       case REG_CFA_DEF_CFA:
2723         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2724         handled_one = true;
2725         break;
2726
2727       case REG_CFA_ADJUST_CFA:
2728         n = XEXP (note, 0);
2729         if (n == NULL)
2730           {
2731             n = PATTERN (insn);
2732             if (GET_CODE (n) == PARALLEL)
2733               n = XVECEXP (n, 0, 0);
2734           }
2735         dwarf2out_frame_debug_adjust_cfa (n, label);
2736         handled_one = true;
2737         break;
2738
2739       case REG_CFA_OFFSET:
2740         n = XEXP (note, 0);
2741         if (n == NULL)
2742           n = single_set (insn);
2743         dwarf2out_frame_debug_cfa_offset (n, label);
2744         handled_one = true;
2745         break;
2746
2747       case REG_CFA_REGISTER:
2748         n = XEXP (note, 0);
2749         if (n == NULL)
2750           {
2751             n = PATTERN (insn);
2752             if (GET_CODE (n) == PARALLEL)
2753               n = XVECEXP (n, 0, 0);
2754           }
2755         dwarf2out_frame_debug_cfa_register (n, label);
2756         handled_one = true;
2757         break;
2758
2759       case REG_CFA_RESTORE:
2760         n = XEXP (note, 0);
2761         if (n == NULL)
2762           {
2763             n = PATTERN (insn);
2764             if (GET_CODE (n) == PARALLEL)
2765               n = XVECEXP (n, 0, 0);
2766             n = XEXP (n, 0);
2767           }
2768         dwarf2out_frame_debug_cfa_restore (n, label);
2769         handled_one = true;
2770         break;
2771
2772       default:
2773         break;
2774       }
2775   if (handled_one)
2776     return;
2777
2778   insn = PATTERN (insn);
2779  found:
2780   dwarf2out_frame_debug_expr (insn, label);
2781 }
2782
2783 /* Determine if we need to save and restore CFI information around this
2784    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2785    we do need to save/restore, then emit the save now, and insert a
2786    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2787
2788 void
2789 dwarf2out_begin_epilogue (rtx insn)
2790 {
2791   bool saw_frp = false;
2792   rtx i;
2793
2794   /* Scan forward to the return insn, noticing if there are possible
2795      frame related insns.  */
2796   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2797     {
2798       if (!INSN_P (i))
2799         continue;
2800
2801       /* Look for both regular and sibcalls to end the block.  */
2802       if (returnjump_p (i))
2803         break;
2804       if (CALL_P (i) && SIBLING_CALL_P (i))
2805         break;
2806
2807       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2808         {
2809           int idx;
2810           rtx seq = PATTERN (i);
2811
2812           if (returnjump_p (XVECEXP (seq, 0, 0)))
2813             break;
2814           if (CALL_P (XVECEXP (seq, 0, 0))
2815               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2816             break;
2817
2818           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2819             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2820               saw_frp = true;
2821         }
2822
2823       if (RTX_FRAME_RELATED_P (i))
2824         saw_frp = true;
2825     }
2826
2827   /* If the port doesn't emit epilogue unwind info, we don't need a
2828      save/restore pair.  */
2829   if (!saw_frp)
2830     return;
2831
2832   /* Otherwise, search forward to see if the return insn was the last
2833      basic block of the function.  If so, we don't need save/restore.  */
2834   gcc_assert (i != NULL);
2835   i = next_real_insn (i);
2836   if (i == NULL)
2837     return;
2838
2839   /* Insert the restore before that next real insn in the stream, and before
2840      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2841      properly nested.  This should be after any label or alignment.  This
2842      will be pushed into the CFI stream by the function below.  */
2843   while (1)
2844     {
2845       rtx p = PREV_INSN (i);
2846       if (!NOTE_P (p))
2847         break;
2848       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2849         break;
2850       i = p;
2851     }
2852   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2853
2854   emit_cfa_remember = true;
2855
2856   /* And emulate the state save.  */
2857   gcc_assert (!cfa_remember.in_use);
2858   cfa_remember = cfa;
2859   cfa_remember.in_use = 1;
2860 }
2861
2862 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2863
2864 void
2865 dwarf2out_frame_debug_restore_state (void)
2866 {
2867   dw_cfi_ref cfi = new_cfi (); 
2868   const char *label = dwarf2out_cfi_label (false);
2869
2870   cfi->dw_cfi_opc = DW_CFA_restore_state;
2871   add_fde_cfi (label, cfi);
2872
2873   gcc_assert (cfa_remember.in_use);
2874   cfa = cfa_remember;
2875   cfa_remember.in_use = 0;
2876 }
2877
2878 #endif
2879
2880 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2881 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2882  (enum dwarf_call_frame_info cfi);
2883
2884 static enum dw_cfi_oprnd_type
2885 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2886 {
2887   switch (cfi)
2888     {
2889     case DW_CFA_nop:
2890     case DW_CFA_GNU_window_save:
2891     case DW_CFA_remember_state:
2892     case DW_CFA_restore_state:
2893       return dw_cfi_oprnd_unused;
2894
2895     case DW_CFA_set_loc:
2896     case DW_CFA_advance_loc1:
2897     case DW_CFA_advance_loc2:
2898     case DW_CFA_advance_loc4:
2899     case DW_CFA_MIPS_advance_loc8:
2900       return dw_cfi_oprnd_addr;
2901
2902     case DW_CFA_offset:
2903     case DW_CFA_offset_extended:
2904     case DW_CFA_def_cfa:
2905     case DW_CFA_offset_extended_sf:
2906     case DW_CFA_def_cfa_sf:
2907     case DW_CFA_restore:
2908     case DW_CFA_restore_extended:
2909     case DW_CFA_undefined:
2910     case DW_CFA_same_value:
2911     case DW_CFA_def_cfa_register:
2912     case DW_CFA_register:
2913       return dw_cfi_oprnd_reg_num;
2914
2915     case DW_CFA_def_cfa_offset:
2916     case DW_CFA_GNU_args_size:
2917     case DW_CFA_def_cfa_offset_sf:
2918       return dw_cfi_oprnd_offset;
2919
2920     case DW_CFA_def_cfa_expression:
2921     case DW_CFA_expression:
2922       return dw_cfi_oprnd_loc;
2923
2924     default:
2925       gcc_unreachable ();
2926     }
2927 }
2928
2929 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2930 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2931  (enum dwarf_call_frame_info cfi);
2932
2933 static enum dw_cfi_oprnd_type
2934 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2935 {
2936   switch (cfi)
2937     {
2938     case DW_CFA_def_cfa:
2939     case DW_CFA_def_cfa_sf:
2940     case DW_CFA_offset:
2941     case DW_CFA_offset_extended_sf:
2942     case DW_CFA_offset_extended:
2943       return dw_cfi_oprnd_offset;
2944
2945     case DW_CFA_register:
2946       return dw_cfi_oprnd_reg_num;
2947
2948     default:
2949       return dw_cfi_oprnd_unused;
2950     }
2951 }
2952
2953 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2954
2955 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2956    switch to the data section instead, and write out a synthetic start label
2957    for collect2 the first time around.  */
2958
2959 static void
2960 switch_to_eh_frame_section (bool back)
2961 {
2962   tree label;
2963
2964 #ifdef EH_FRAME_SECTION_NAME
2965   if (eh_frame_section == 0)
2966     {
2967       int flags;
2968
2969       if (EH_TABLES_CAN_BE_READ_ONLY)
2970         {
2971           int fde_encoding;
2972           int per_encoding;
2973           int lsda_encoding;
2974
2975           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2976                                                        /*global=*/0);
2977           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2978                                                        /*global=*/1);
2979           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2980                                                         /*global=*/0);
2981           flags = ((! flag_pic
2982                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2983                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2984                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2985                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2986                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2987                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2988                    ? 0 : SECTION_WRITE);
2989         }
2990       else
2991         flags = SECTION_WRITE;
2992       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2993     }
2994 #endif
2995
2996   if (eh_frame_section)
2997     switch_to_section (eh_frame_section);
2998   else
2999     {
3000       /* We have no special eh_frame section.  Put the information in
3001          the data section and emit special labels to guide collect2.  */
3002       switch_to_section (data_section);
3003
3004       if (!back)
3005         {
3006           label = get_file_function_name ("F");
3007           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3008           targetm.asm_out.globalize_label (asm_out_file,
3009                                            IDENTIFIER_POINTER (label));
3010           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3011         }
3012     }
3013 }
3014
3015 /* Switch [BACK] to the eh or debug frame table section, depending on
3016    FOR_EH.  */
3017
3018 static void
3019 switch_to_frame_table_section (int for_eh, bool back)
3020 {
3021   if (for_eh)
3022     switch_to_eh_frame_section (back);
3023   else
3024     {
3025       if (!debug_frame_section)
3026         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3027                                            SECTION_DEBUG, NULL);
3028       switch_to_section (debug_frame_section);
3029     }
3030 }
3031
3032 /* Output a Call Frame Information opcode and its operand(s).  */
3033
3034 static void
3035 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3036 {
3037   unsigned long r;
3038   HOST_WIDE_INT off;
3039
3040   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3041     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3042                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3043                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3044                          ((unsigned HOST_WIDE_INT)
3045                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3046   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3047     {
3048       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3049       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3050                            "DW_CFA_offset, column 0x%lx", r);
3051       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3052       dw2_asm_output_data_uleb128 (off, NULL);
3053     }
3054   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3055     {
3056       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3057       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3058                            "DW_CFA_restore, column 0x%lx", r);
3059     }
3060   else
3061     {
3062       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3063                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3064
3065       switch (cfi->dw_cfi_opc)
3066         {
3067         case DW_CFA_set_loc:
3068           if (for_eh)
3069             dw2_asm_output_encoded_addr_rtx (
3070                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3071                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3072                 false, NULL);
3073           else
3074             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3075                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3076           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3077           break;
3078
3079         case DW_CFA_advance_loc1:
3080           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3081                                 fde->dw_fde_current_label, NULL);
3082           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3083           break;
3084
3085         case DW_CFA_advance_loc2:
3086           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3087                                 fde->dw_fde_current_label, NULL);
3088           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3089           break;
3090
3091         case DW_CFA_advance_loc4:
3092           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3093                                 fde->dw_fde_current_label, NULL);
3094           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3095           break;
3096
3097         case DW_CFA_MIPS_advance_loc8:
3098           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3099                                 fde->dw_fde_current_label, NULL);
3100           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3101           break;
3102
3103         case DW_CFA_offset_extended:
3104           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3105           dw2_asm_output_data_uleb128 (r, NULL);
3106           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3107           dw2_asm_output_data_uleb128 (off, NULL);
3108           break;
3109
3110         case DW_CFA_def_cfa:
3111           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3112           dw2_asm_output_data_uleb128 (r, NULL);
3113           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3114           break;
3115
3116         case DW_CFA_offset_extended_sf:
3117           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3118           dw2_asm_output_data_uleb128 (r, NULL);
3119           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3120           dw2_asm_output_data_sleb128 (off, NULL);
3121           break;
3122
3123         case DW_CFA_def_cfa_sf:
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           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3127           dw2_asm_output_data_sleb128 (off, NULL);
3128           break;
3129
3130         case DW_CFA_restore_extended:
3131         case DW_CFA_undefined:
3132         case DW_CFA_same_value:
3133         case DW_CFA_def_cfa_register:
3134           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3135           dw2_asm_output_data_uleb128 (r, NULL);
3136           break;
3137
3138         case DW_CFA_register:
3139           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3140           dw2_asm_output_data_uleb128 (r, NULL);
3141           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3142           dw2_asm_output_data_uleb128 (r, NULL);
3143           break;
3144
3145         case DW_CFA_def_cfa_offset:
3146         case DW_CFA_GNU_args_size:
3147           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3148           break;
3149
3150         case DW_CFA_def_cfa_offset_sf:
3151           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3152           dw2_asm_output_data_sleb128 (off, NULL);
3153           break;
3154
3155         case DW_CFA_GNU_window_save:
3156           break;
3157
3158         case DW_CFA_def_cfa_expression:
3159         case DW_CFA_expression:
3160           output_cfa_loc (cfi);
3161           break;
3162
3163         case DW_CFA_GNU_negative_offset_extended:
3164           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3165           gcc_unreachable ();
3166
3167         default:
3168           break;
3169         }
3170     }
3171 }
3172
3173 /* Similar, but do it via assembler directives instead.  */
3174
3175 static void
3176 output_cfi_directive (dw_cfi_ref cfi)
3177 {
3178   unsigned long r, r2;
3179
3180   switch (cfi->dw_cfi_opc)
3181     {
3182     case DW_CFA_advance_loc:
3183     case DW_CFA_advance_loc1:
3184     case DW_CFA_advance_loc2:
3185     case DW_CFA_advance_loc4:
3186     case DW_CFA_MIPS_advance_loc8:
3187     case DW_CFA_set_loc:
3188       /* Should only be created by add_fde_cfi in a code path not
3189          followed when emitting via directives.  The assembler is
3190          going to take care of this for us.  */
3191       gcc_unreachable ();
3192
3193     case DW_CFA_offset:
3194     case DW_CFA_offset_extended:
3195     case DW_CFA_offset_extended_sf:
3196       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3197       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3198                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3199       break;
3200
3201     case DW_CFA_restore:
3202     case DW_CFA_restore_extended:
3203       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3204       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3205       break;
3206
3207     case DW_CFA_undefined:
3208       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3209       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3210       break;
3211
3212     case DW_CFA_same_value:
3213       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3214       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3215       break;
3216
3217     case DW_CFA_def_cfa:
3218     case DW_CFA_def_cfa_sf:
3219       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3220       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3221                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3222       break;
3223
3224     case DW_CFA_def_cfa_register:
3225       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3226       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3227       break;
3228
3229     case DW_CFA_register:
3230       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3231       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3232       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3233       break;
3234
3235     case DW_CFA_def_cfa_offset:
3236     case DW_CFA_def_cfa_offset_sf:
3237       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3238                HOST_WIDE_INT_PRINT_DEC"\n",
3239                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3240       break;
3241
3242     case DW_CFA_remember_state:
3243       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3244       break;
3245     case DW_CFA_restore_state:
3246       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3247       break;
3248
3249     case DW_CFA_GNU_args_size:
3250       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3251       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3252       if (flag_debug_asm)
3253         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3254                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3255       fputc ('\n', asm_out_file);
3256       break;
3257
3258     case DW_CFA_GNU_window_save:
3259       fprintf (asm_out_file, "\t.cfi_window_save\n");
3260       break;
3261
3262     case DW_CFA_def_cfa_expression:
3263     case DW_CFA_expression:
3264       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3265       output_cfa_loc_raw (cfi);
3266       fputc ('\n', asm_out_file);
3267       break;
3268
3269     default:
3270       gcc_unreachable ();
3271     }
3272 }
3273
3274 DEF_VEC_P (dw_cfi_ref);
3275 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3276
3277 /* Output CFIs to bring current FDE to the same state as after executing
3278    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3279    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3280    other arguments to pass to output_cfi.  */
3281
3282 static void
3283 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3284 {
3285   struct dw_cfi_struct cfi_buf;
3286   dw_cfi_ref cfi2;
3287   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3288   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3289   unsigned int len, idx;
3290
3291   for (;; cfi = cfi->dw_cfi_next)
3292     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3293       {
3294       case DW_CFA_advance_loc:
3295       case DW_CFA_advance_loc1:
3296       case DW_CFA_advance_loc2:
3297       case DW_CFA_advance_loc4:
3298       case DW_CFA_MIPS_advance_loc8:
3299       case DW_CFA_set_loc:
3300         /* All advances should be ignored.  */
3301         break;
3302       case DW_CFA_remember_state:
3303         {
3304           dw_cfi_ref args_size = cfi_args_size;
3305
3306           /* Skip everything between .cfi_remember_state and
3307              .cfi_restore_state.  */
3308           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3309             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3310               break;
3311             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3312               args_size = cfi2;
3313             else
3314               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3315
3316           if (cfi2 == NULL)
3317             goto flush_all;
3318           else
3319             {
3320               cfi = cfi2;
3321               cfi_args_size = args_size;
3322             }
3323           break;
3324         }
3325       case DW_CFA_GNU_args_size:
3326         cfi_args_size = cfi;
3327         break;
3328       case DW_CFA_GNU_window_save:
3329         goto flush_all;
3330       case DW_CFA_offset:
3331       case DW_CFA_offset_extended:
3332       case DW_CFA_offset_extended_sf:
3333       case DW_CFA_restore:
3334       case DW_CFA_restore_extended:
3335       case DW_CFA_undefined:
3336       case DW_CFA_same_value:
3337       case DW_CFA_register:
3338       case DW_CFA_val_offset:
3339       case DW_CFA_val_offset_sf:
3340       case DW_CFA_expression:
3341       case DW_CFA_val_expression:
3342       case DW_CFA_GNU_negative_offset_extended:
3343         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3344           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3345                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3346         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3347         break;
3348       case DW_CFA_def_cfa:
3349       case DW_CFA_def_cfa_sf:
3350       case DW_CFA_def_cfa_expression:
3351         cfi_cfa = cfi;
3352         cfi_cfa_offset = cfi;
3353         break;
3354       case DW_CFA_def_cfa_register:
3355         cfi_cfa = cfi;
3356         break;
3357       case DW_CFA_def_cfa_offset:
3358       case DW_CFA_def_cfa_offset_sf:
3359         cfi_cfa_offset = cfi;
3360         break;
3361       case DW_CFA_nop:
3362         gcc_assert (cfi == NULL);
3363       flush_all:
3364         len = VEC_length (dw_cfi_ref, regs);
3365         for (idx = 0; idx < len; idx++)
3366           {
3367             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3368             if (cfi2 != NULL
3369                 && cfi2->dw_cfi_opc != DW_CFA_restore
3370                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3371               {
3372                 if (do_cfi_asm)
3373                   output_cfi_directive (cfi2);
3374                 else
3375                   output_cfi (cfi2, fde, for_eh);
3376               }
3377           }
3378         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3379           {
3380             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3381             cfi_buf = *cfi_cfa;
3382             switch (cfi_cfa_offset->dw_cfi_opc)
3383               {
3384               case DW_CFA_def_cfa_offset:
3385                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3386                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3387                 break;
3388               case DW_CFA_def_cfa_offset_sf:
3389                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3390                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3391                 break;
3392               case DW_CFA_def_cfa:
3393               case DW_CFA_def_cfa_sf:
3394                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3395                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3396                 break;
3397               default:
3398                 gcc_unreachable ();
3399               }
3400             cfi_cfa = &cfi_buf;
3401           }
3402         else if (cfi_cfa_offset)
3403           cfi_cfa = cfi_cfa_offset;
3404         if (cfi_cfa)
3405           {
3406             if (do_cfi_asm)
3407               output_cfi_directive (cfi_cfa);
3408             else
3409               output_cfi (cfi_cfa, fde, for_eh);
3410           }
3411         cfi_cfa = NULL;
3412         cfi_cfa_offset = NULL;
3413         if (cfi_args_size
3414             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3415           {
3416             if (do_cfi_asm)
3417               output_cfi_directive (cfi_args_size);
3418             else
3419               output_cfi (cfi_args_size, fde, for_eh);
3420           }
3421         cfi_args_size = NULL;
3422         if (cfi == NULL)
3423           {
3424             VEC_free (dw_cfi_ref, heap, regs);
3425             return;
3426           }
3427         else if (do_cfi_asm)
3428           output_cfi_directive (cfi);
3429         else
3430           output_cfi (cfi, fde, for_eh);
3431         break;
3432       default:
3433         gcc_unreachable ();
3434     }
3435 }
3436
3437 /* Output one FDE.  */
3438
3439 static void
3440 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3441             char *section_start_label, int fde_encoding, char *augmentation,
3442             bool any_lsda_needed, int lsda_encoding)
3443 {
3444   const char *begin, *end;
3445   static unsigned int j;
3446   char l1[20], l2[20];
3447   dw_cfi_ref cfi;
3448
3449   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3450                                 /* empty */ 0);
3451   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3452                                   for_eh + j);
3453   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3454   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3455   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3456     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3457                          " indicating 64-bit DWARF extension");
3458   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3459                         "FDE Length");
3460   ASM_OUTPUT_LABEL (asm_out_file, l1);
3461
3462   if (for_eh)
3463     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3464   else
3465     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3466                            debug_frame_section, "FDE CIE offset");
3467
3468   if (!fde->dw_fde_switched_sections)
3469     {
3470       begin = fde->dw_fde_begin;
3471       end = fde->dw_fde_end;
3472     }
3473   else
3474     {
3475       /* For the first section, prefer dw_fde_begin over
3476          dw_fde_{hot,cold}_section_label, as the latter
3477          might be separated from the real start of the
3478          function by alignment padding.  */
3479       if (!second)
3480         begin = fde->dw_fde_begin;
3481       else if (fde->dw_fde_switched_cold_to_hot)
3482         begin = fde->dw_fde_hot_section_label;
3483       else
3484         begin = fde->dw_fde_unlikely_section_label;
3485       if (second ^ fde->dw_fde_switched_cold_to_hot)
3486         end = fde->dw_fde_unlikely_section_end_label;
3487       else
3488         end = fde->dw_fde_hot_section_end_label;
3489     }
3490
3491   if (for_eh)
3492     {
3493       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3494       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3495       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3496                                        "FDE initial location");
3497       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3498                             end, begin, "FDE address range");
3499     }
3500   else
3501     {
3502       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3503       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3504     }
3505
3506   if (augmentation[0])
3507     {
3508       if (any_lsda_needed)
3509         {
3510           int size = size_of_encoded_value (lsda_encoding);
3511
3512           if (lsda_encoding == DW_EH_PE_aligned)
3513             {
3514               int offset = (  4         /* Length */
3515                             + 4         /* CIE offset */
3516                             + 2 * size_of_encoded_value (fde_encoding)
3517                             + 1         /* Augmentation size */ );
3518               int pad = -offset & (PTR_SIZE - 1);
3519
3520               size += pad;
3521               gcc_assert (size_of_uleb128 (size) == 1);
3522             }
3523
3524           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3525
3526           if (fde->uses_eh_lsda)
3527             {
3528               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3529                                            fde->funcdef_number);
3530               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3531                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3532                                                false,
3533                                                "Language Specific Data Area");
3534             }
3535           else
3536             {
3537               if (lsda_encoding == DW_EH_PE_aligned)
3538                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3539               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3540                                    "Language Specific Data Area (none)");
3541             }
3542         }
3543       else
3544         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3545     }
3546
3547   /* Loop through the Call Frame Instructions associated with
3548      this FDE.  */
3549   fde->dw_fde_current_label = begin;
3550   if (!fde->dw_fde_switched_sections)
3551     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3552       output_cfi (cfi, fde, for_eh);
3553   else if (!second)
3554     {
3555       if (fde->dw_fde_switch_cfi)
3556         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3557           {
3558             output_cfi (cfi, fde, for_eh);
3559             if (cfi == fde->dw_fde_switch_cfi)
3560               break;
3561           }
3562     }
3563   else
3564     {
3565       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3566
3567       if (fde->dw_fde_switch_cfi)
3568         {
3569           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3570           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3571           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3572           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3573         }
3574       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3575         output_cfi (cfi, fde, for_eh);
3576     }
3577
3578   /* If we are to emit a ref/link from function bodies to their frame tables,
3579      do it now.  This is typically performed to make sure that tables
3580      associated with functions are dragged with them and not discarded in
3581      garbage collecting links. We need to do this on a per function basis to
3582      cope with -ffunction-sections.  */
3583
3584 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3585   /* Switch to the function section, emit the ref to the tables, and
3586      switch *back* into the table section.  */
3587   switch_to_section (function_section (fde->decl));
3588   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3589   switch_to_frame_table_section (for_eh, true);
3590 #endif
3591
3592   /* Pad the FDE out to an address sized boundary.  */
3593   ASM_OUTPUT_ALIGN (asm_out_file,
3594                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3595   ASM_OUTPUT_LABEL (asm_out_file, l2);
3596
3597   j += 2;
3598 }
3599
3600 /* Output the call frame information used to record information
3601    that relates to calculating the frame pointer, and records the
3602    location of saved registers.  */
3603
3604 static void
3605 output_call_frame_info (int for_eh)
3606 {
3607   unsigned int i;
3608   dw_fde_ref fde;
3609   dw_cfi_ref cfi;
3610   char l1[20], l2[20], section_start_label[20];
3611   bool any_lsda_needed = false;
3612   char augmentation[6];
3613   int augmentation_size;
3614   int fde_encoding = DW_EH_PE_absptr;
3615   int per_encoding = DW_EH_PE_absptr;
3616   int lsda_encoding = DW_EH_PE_absptr;
3617   int return_reg;
3618   rtx personality = NULL;
3619   int dw_cie_version;
3620
3621   /* Don't emit a CIE if there won't be any FDEs.  */
3622   if (fde_table_in_use == 0)
3623     return;
3624
3625   /* Nothing to do if the assembler's doing it all.  */
3626   if (dwarf2out_do_cfi_asm ())
3627     return;
3628
3629   /* If we make FDEs linkonce, we may have to emit an empty label for
3630      an FDE that wouldn't otherwise be emitted.  We want to avoid
3631      having an FDE kept around when the function it refers to is
3632      discarded.  Example where this matters: a primary function
3633      template in C++ requires EH information, but an explicit
3634      specialization doesn't.  */
3635   if (TARGET_USES_WEAK_UNWIND_INFO
3636       && ! flag_asynchronous_unwind_tables
3637       && flag_exceptions
3638       && for_eh)
3639     for (i = 0; i < fde_table_in_use; i++)
3640       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3641           && !fde_table[i].uses_eh_lsda
3642           && ! DECL_WEAK (fde_table[i].decl))
3643         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3644                                       for_eh, /* empty */ 1);
3645
3646   /* If we don't have any functions we'll want to unwind out of, don't
3647      emit any EH unwind information.  Note that if exceptions aren't
3648      enabled, we won't have collected nothrow information, and if we
3649      asked for asynchronous tables, we always want this info.  */
3650   if (for_eh)
3651     {
3652       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3653
3654       for (i = 0; i < fde_table_in_use; i++)
3655         if (fde_table[i].uses_eh_lsda)
3656           any_eh_needed = any_lsda_needed = true;
3657         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3658           any_eh_needed = true;
3659         else if (! fde_table[i].nothrow
3660                  && ! fde_table[i].all_throwers_are_sibcalls)
3661           any_eh_needed = true;
3662
3663       if (! any_eh_needed)
3664         return;
3665     }
3666
3667   /* We're going to be generating comments, so turn on app.  */
3668   if (flag_debug_asm)
3669     app_enable ();
3670
3671   /* Switch to the proper frame section, first time.  */
3672   switch_to_frame_table_section (for_eh, false);
3673
3674   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3675   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3676
3677   /* Output the CIE.  */
3678   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3679   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3680   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3681     dw2_asm_output_data (4, 0xffffffff,
3682       "Initial length escape value indicating 64-bit DWARF extension");
3683   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3684                         "Length of Common Information Entry");
3685   ASM_OUTPUT_LABEL (asm_out_file, l1);
3686
3687   /* Now that the CIE pointer is PC-relative for EH,
3688      use 0 to identify the CIE.  */
3689   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3690                        (for_eh ? 0 : DWARF_CIE_ID),
3691                        "CIE Identifier Tag");
3692
3693   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3694      use CIE version 1, unless that would produce incorrect results
3695      due to overflowing the return register column.  */
3696   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3697   dw_cie_version = 1;
3698   if (return_reg >= 256 || dwarf_version > 2)
3699     dw_cie_version = 3;
3700   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3701
3702   augmentation[0] = 0;
3703   augmentation_size = 0;
3704
3705   personality = current_unit_personality;
3706   if (for_eh)
3707     {
3708       char *p;
3709
3710       /* Augmentation:
3711          z      Indicates that a uleb128 is present to size the
3712                 augmentation section.
3713          L      Indicates the encoding (and thus presence) of
3714                 an LSDA pointer in the FDE augmentation.
3715          R      Indicates a non-default pointer encoding for
3716                 FDE code pointers.
3717          P      Indicates the presence of an encoding + language
3718                 personality routine in the CIE augmentation.  */
3719
3720       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3721       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3722       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3723
3724       p = augmentation + 1;
3725       if (personality)
3726         {
3727           *p++ = 'P';
3728           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3729           assemble_external_libcall (personality);
3730         }
3731       if (any_lsda_needed)
3732         {
3733           *p++ = 'L';
3734           augmentation_size += 1;
3735         }
3736       if (fde_encoding != DW_EH_PE_absptr)
3737         {
3738           *p++ = 'R';
3739           augmentation_size += 1;
3740         }
3741       if (p > augmentation + 1)
3742         {
3743           augmentation[0] = 'z';
3744           *p = '\0';
3745         }
3746
3747       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3748       if (personality && per_encoding == DW_EH_PE_aligned)
3749         {
3750           int offset = (  4             /* Length */
3751                         + 4             /* CIE Id */
3752                         + 1             /* CIE version */
3753                         + strlen (augmentation) + 1     /* Augmentation */
3754                         + size_of_uleb128 (1)           /* Code alignment */
3755                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3756                         + 1             /* RA column */
3757                         + 1             /* Augmentation size */
3758                         + 1             /* Personality encoding */ );
3759           int pad = -offset & (PTR_SIZE - 1);
3760
3761           augmentation_size += pad;
3762
3763           /* Augmentations should be small, so there's scarce need to
3764              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3765           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3766         }
3767     }
3768
3769   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3770   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3771   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3772                                "CIE Data Alignment Factor");
3773
3774   if (dw_cie_version == 1)
3775     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3776   else
3777     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3778
3779   if (augmentation[0])
3780     {
3781       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3782       if (personality)
3783         {
3784           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3785                                eh_data_format_name (per_encoding));
3786           dw2_asm_output_encoded_addr_rtx (per_encoding,
3787                                            personality,
3788                                            true, NULL);
3789         }
3790
3791       if (any_lsda_needed)
3792         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3793                              eh_data_format_name (lsda_encoding));
3794
3795       if (fde_encoding != DW_EH_PE_absptr)
3796         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3797                              eh_data_format_name (fde_encoding));
3798     }
3799
3800   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3801     output_cfi (cfi, NULL, for_eh);
3802
3803   /* Pad the CIE out to an address sized boundary.  */
3804   ASM_OUTPUT_ALIGN (asm_out_file,
3805                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3806   ASM_OUTPUT_LABEL (asm_out_file, l2);
3807
3808   /* Loop through all of the FDE's.  */
3809   for (i = 0; i < fde_table_in_use; i++)
3810     {
3811       unsigned int k;
3812       fde = &fde_table[i];
3813
3814       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3815       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3816           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3817           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3818           && !fde->uses_eh_lsda)
3819         continue;
3820
3821       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3822         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3823                     augmentation, any_lsda_needed, lsda_encoding);
3824     }
3825
3826   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3827     dw2_asm_output_data (4, 0, "End of Table");
3828 #ifdef MIPS_DEBUGGING_INFO
3829   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3830      get a value of 0.  Putting .align 0 after the label fixes it.  */
3831   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3832 #endif
3833
3834   /* Turn off app to make assembly quicker.  */
3835   if (flag_debug_asm)
3836     app_disable ();
3837 }
3838
3839 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3840
3841 static void
3842 dwarf2out_do_cfi_startproc (bool second)
3843 {
3844   int enc;
3845   rtx ref;
3846   rtx personality = get_personality_function (current_function_decl);
3847
3848   fprintf (asm_out_file, "\t.cfi_startproc\n");
3849
3850   if (personality)
3851     {
3852       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3853       ref = personality;
3854
3855       /* ??? The GAS support isn't entirely consistent.  We have to
3856          handle indirect support ourselves, but PC-relative is done
3857          in the assembler.  Further, the assembler can't handle any
3858          of the weirder relocation types.  */
3859       if (enc & DW_EH_PE_indirect)
3860         ref = dw2_force_const_mem (ref, true);
3861
3862       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3863       output_addr_const (asm_out_file, ref);
3864       fputc ('\n', asm_out_file);
3865     }
3866
3867   if (crtl->uses_eh_lsda)
3868     {
3869       char lab[20];
3870
3871       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3872       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3873                                    current_function_funcdef_no);
3874       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3875       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3876
3877       if (enc & DW_EH_PE_indirect)
3878         ref = dw2_force_const_mem (ref, true);
3879
3880       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3881       output_addr_const (asm_out_file, ref);
3882       fputc ('\n', asm_out_file);
3883     }
3884 }
3885
3886 /* Output a marker (i.e. a label) for the beginning of a function, before
3887    the prologue.  */
3888
3889 void
3890 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3891                           const char *file ATTRIBUTE_UNUSED)
3892 {
3893   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3894   char * dup_label;
3895   dw_fde_ref fde;
3896   section *fnsec;
3897
3898   current_function_func_begin_label = NULL;
3899
3900 #ifdef TARGET_UNWIND_INFO
3901   /* ??? current_function_func_begin_label is also used by except.c
3902      for call-site information.  We must emit this label if it might
3903      be used.  */
3904   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3905       && ! dwarf2out_do_frame ())
3906     return;
3907 #else
3908   if (! dwarf2out_do_frame ())
3909     return;
3910 #endif
3911
3912   fnsec = function_section (current_function_decl);
3913   switch_to_section (fnsec);
3914   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3915                                current_function_funcdef_no);
3916   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3917                           current_function_funcdef_no);
3918   dup_label = xstrdup (label);
3919   current_function_func_begin_label = dup_label;
3920
3921 #ifdef TARGET_UNWIND_INFO
3922   /* We can elide the fde allocation if we're not emitting debug info.  */
3923   if (! dwarf2out_do_frame ())
3924     return;
3925 #endif
3926
3927   /* Expand the fde table if necessary.  */
3928   if (fde_table_in_use == fde_table_allocated)
3929     {
3930       fde_table_allocated += FDE_TABLE_INCREMENT;
3931       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3932       memset (fde_table + fde_table_in_use, 0,
3933               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3934     }
3935
3936   /* Record the FDE associated with this function.  */
3937   current_funcdef_fde = fde_table_in_use;
3938
3939   /* Add the new FDE at the end of the fde_table.  */
3940   fde = &fde_table[fde_table_in_use++];
3941   fde->decl = current_function_decl;
3942   fde->dw_fde_begin = dup_label;
3943   fde->dw_fde_current_label = dup_label;
3944   fde->dw_fde_hot_section_label = NULL;
3945   fde->dw_fde_hot_section_end_label = NULL;
3946   fde->dw_fde_unlikely_section_label = NULL;
3947   fde->dw_fde_unlikely_section_end_label = NULL;
3948   fde->dw_fde_switched_sections = 0;
3949   fde->dw_fde_switched_cold_to_hot = 0;
3950   fde->dw_fde_end = NULL;
3951   fde->dw_fde_cfi = NULL;
3952   fde->dw_fde_switch_cfi = NULL;
3953   fde->funcdef_number = current_function_funcdef_no;
3954   fde->nothrow = crtl->nothrow;
3955   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3956   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3957   fde->drap_reg = INVALID_REGNUM;
3958   fde->vdrap_reg = INVALID_REGNUM;
3959   if (flag_reorder_blocks_and_partition)
3960     {
3961       section *unlikelysec;
3962       if (first_function_block_is_cold)
3963         fde->in_std_section = 1;
3964       else
3965         fde->in_std_section
3966           = (fnsec == text_section
3967              || (cold_text_section && fnsec == cold_text_section));
3968       unlikelysec = unlikely_text_section ();
3969       fde->cold_in_std_section
3970         = (unlikelysec == text_section
3971            || (cold_text_section && unlikelysec == cold_text_section));
3972     }
3973   else
3974     {
3975       fde->in_std_section
3976         = (fnsec == text_section
3977            || (cold_text_section && fnsec == cold_text_section));
3978       fde->cold_in_std_section = 0;
3979     }
3980
3981   args_size = old_args_size = 0;
3982
3983   /* We only want to output line number information for the genuine dwarf2
3984      prologue case, not the eh frame case.  */
3985 #ifdef DWARF2_DEBUGGING_INFO
3986   if (file)
3987     dwarf2out_source_line (line, file, 0, true);
3988 #endif
3989
3990   if (dwarf2out_do_cfi_asm ())
3991     dwarf2out_do_cfi_startproc (false);
3992   else
3993     {
3994       rtx personality = get_personality_function (current_function_decl);
3995       if (!current_unit_personality)
3996         current_unit_personality = personality;
3997
3998       /* We cannot keep a current personality per function as without CFI
3999          asm at the point where we emit the CFI data there is no current
4000          function anymore.  */
4001       if (personality
4002           && current_unit_personality != personality)
4003         sorry ("Multiple EH personalities are supported only with assemblers "
4004                "supporting .cfi.personality directive.");
4005     }
4006 }
4007
4008 /* Output a marker (i.e. a label) for the absolute end of the generated code
4009    for a function definition.  This gets called *after* the epilogue code has
4010    been generated.  */
4011
4012 void
4013 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4014                         const char *file ATTRIBUTE_UNUSED)
4015 {
4016   dw_fde_ref fde;
4017   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4018
4019 #ifdef DWARF2_DEBUGGING_INFO
4020   last_var_location_insn = NULL_RTX;
4021 #endif
4022
4023   if (dwarf2out_do_cfi_asm ())
4024     fprintf (asm_out_file, "\t.cfi_endproc\n");
4025
4026   /* Output a label to mark the endpoint of the code generated for this
4027      function.  */
4028   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4029                                current_function_funcdef_no);
4030   ASM_OUTPUT_LABEL (asm_out_file, label);
4031   fde = current_fde ();
4032   gcc_assert (fde != NULL);
4033   fde->dw_fde_end = xstrdup (label);
4034 }
4035
4036 void
4037 dwarf2out_frame_init (void)
4038 {
4039   /* Allocate the initial hunk of the fde_table.  */
4040   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4041   fde_table_allocated = FDE_TABLE_INCREMENT;
4042   fde_table_in_use = 0;
4043
4044   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4045      sake of lookup_cfa.  */
4046
4047   /* On entry, the Canonical Frame Address is at SP.  */
4048   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4049
4050 #ifdef DWARF2_UNWIND_INFO
4051   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4052     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4053 #endif
4054 }
4055
4056 void
4057 dwarf2out_frame_finish (void)
4058 {
4059   /* Output call frame information.  */
4060   if (DWARF2_FRAME_INFO)
4061     output_call_frame_info (0);
4062
4063 #ifndef TARGET_UNWIND_INFO
4064   /* Output another copy for the unwinder.  */
4065   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4066     output_call_frame_info (1);
4067 #endif
4068 }
4069
4070 /* Note that the current function section is being used for code.  */
4071
4072 static void
4073 dwarf2out_note_section_used (void)
4074 {
4075   section *sec = current_function_section ();
4076   if (sec == text_section)
4077     text_section_used = true;
4078   else if (sec == cold_text_section)
4079     cold_text_section_used = true;
4080 }
4081
4082 void
4083 dwarf2out_switch_text_section (void)
4084 {
4085   dw_fde_ref fde = current_fde ();
4086
4087   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4088
4089   fde->dw_fde_switched_sections = 1;
4090   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4091
4092   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4093   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4094   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4095   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4096   have_multiple_function_sections = true;
4097
4098   /* Reset the current label on switching text sections, so that we
4099      don't attempt to advance_loc4 between labels in different sections.  */
4100   fde->dw_fde_current_label = NULL;
4101
4102   /* There is no need to mark used sections when not debugging.  */
4103   if (cold_text_section != NULL)
4104     dwarf2out_note_section_used ();
4105
4106   if (dwarf2out_do_cfi_asm ())
4107     fprintf (asm_out_file, "\t.cfi_endproc\n");
4108
4109   /* Now do the real section switch.  */
4110   switch_to_section (current_function_section ());
4111
4112   if (dwarf2out_do_cfi_asm ())
4113     {
4114       dwarf2out_do_cfi_startproc (true);
4115       /* As this is a different FDE, insert all current CFI instructions
4116          again.  */
4117       output_cfis (fde->dw_fde_cfi, true, fde, true);
4118     }
4119   else
4120     {
4121       dw_cfi_ref cfi = fde->dw_fde_cfi;
4122
4123       cfi = fde->dw_fde_cfi;
4124       if (cfi)
4125         while (cfi->dw_cfi_next != NULL)
4126           cfi = cfi->dw_cfi_next;
4127       fde->dw_fde_switch_cfi = cfi;
4128     }
4129 }
4130 #endif
4131 \f
4132 /* And now, the subset of the debugging information support code necessary
4133    for emitting location expressions.  */
4134
4135 /* Data about a single source file.  */
4136 struct GTY(()) dwarf_file_data {
4137   const char * filename;
4138   int emitted_number;
4139 };
4140
4141 typedef struct dw_val_struct *dw_val_ref;
4142 typedef struct die_struct *dw_die_ref;
4143 typedef const struct die_struct *const_dw_die_ref;
4144 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4145 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4146
4147 typedef struct GTY(()) deferred_locations_struct
4148 {
4149   tree variable;
4150   dw_die_ref die;
4151 } deferred_locations;
4152
4153 DEF_VEC_O(deferred_locations);
4154 DEF_VEC_ALLOC_O(deferred_locations,gc);
4155
4156 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4157
4158 /* Each DIE may have a series of attribute/value pairs.  Values
4159    can take on several forms.  The forms that are used in this
4160    implementation are listed below.  */
4161
4162 enum dw_val_class
4163 {
4164   dw_val_class_addr,
4165   dw_val_class_offset,
4166   dw_val_class_loc,
4167   dw_val_class_loc_list,
4168   dw_val_class_range_list,
4169   dw_val_class_const,
4170   dw_val_class_unsigned_const,
4171   dw_val_class_const_double,
4172   dw_val_class_vec,
4173   dw_val_class_flag,
4174   dw_val_class_die_ref,
4175   dw_val_class_fde_ref,
4176   dw_val_class_lbl_id,
4177   dw_val_class_lineptr,
4178   dw_val_class_str,
4179   dw_val_class_macptr,
4180   dw_val_class_file
4181 };
4182
4183 /* Describe a floating point constant value, or a vector constant value.  */
4184
4185 typedef struct GTY(()) dw_vec_struct {
4186   unsigned char * GTY((length ("%h.length"))) array;
4187   unsigned length;
4188   unsigned elt_size;
4189 }
4190 dw_vec_const;
4191
4192 /* The dw_val_node describes an attribute's value, as it is
4193    represented internally.  */
4194
4195 typedef struct GTY(()) dw_val_struct {
4196   enum dw_val_class val_class;
4197   union dw_val_struct_union
4198     {
4199       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4200       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4201       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4202       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4203       HOST_WIDE_INT GTY ((default)) val_int;
4204       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4205       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4206       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4207       struct dw_val_die_union
4208         {
4209           dw_die_ref die;
4210           int external;
4211         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4212       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4213       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4214       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4215       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4216       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4217     }
4218   GTY ((desc ("%1.val_class"))) v;
4219 }
4220 dw_val_node;
4221
4222 /* Locations in memory are described using a sequence of stack machine
4223    operations.  */
4224
4225 typedef struct GTY(()) dw_loc_descr_struct {
4226   dw_loc_descr_ref dw_loc_next;
4227   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4228   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4229      from DW_OP_addr with a dtp-relative symbol relocation.  */
4230   unsigned int dtprel : 1;
4231   int dw_loc_addr;
4232   dw_val_node dw_loc_oprnd1;
4233   dw_val_node dw_loc_oprnd2;
4234 }
4235 dw_loc_descr_node;
4236
4237 /* Location lists are ranges + location descriptions for that range,
4238    so you can track variables that are in different places over
4239    their entire life.  */
4240 typedef struct GTY(()) dw_loc_list_struct {
4241   dw_loc_list_ref dw_loc_next;
4242   const char *begin; /* Label for begin address of range */
4243   const char *end;  /* Label for end address of range */
4244   char *ll_symbol; /* Label for beginning of location list.
4245                       Only on head of list */
4246   const char *section; /* Section this loclist is relative to */
4247   dw_loc_descr_ref expr;
4248 } dw_loc_list_node;
4249
4250 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4251
4252 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4253
4254 /* Convert a DWARF stack opcode into its string name.  */
4255
4256 static const char *
4257 dwarf_stack_op_name (unsigned int op)
4258 {
4259   switch (op)
4260     {
4261     case DW_OP_addr:
4262       return "DW_OP_addr";
4263     case DW_OP_deref:
4264       return "DW_OP_deref";
4265     case DW_OP_const1u:
4266       return "DW_OP_const1u";
4267     case DW_OP_const1s:
4268       return "DW_OP_const1s";
4269     case DW_OP_const2u:
4270       return "DW_OP_const2u";
4271     case DW_OP_const2s:
4272       return "DW_OP_const2s";
4273     case DW_OP_const4u:
4274       return "DW_OP_const4u";
4275     case DW_OP_const4s:
4276       return "DW_OP_const4s";
4277     case DW_OP_const8u:
4278       return "DW_OP_const8u";
4279     case DW_OP_const8s:
4280       return "DW_OP_const8s";
4281     case DW_OP_constu:
4282       return "DW_OP_constu";
4283     case DW_OP_consts:
4284       return "DW_OP_consts";
4285     case DW_OP_dup:
4286       return "DW_OP_dup";
4287     case DW_OP_drop:
4288       return "DW_OP_drop";
4289     case DW_OP_over:
4290       return "DW_OP_over";
4291     case DW_OP_pick:
4292       return "DW_OP_pick";
4293     case DW_OP_swap:
4294       return "DW_OP_swap";
4295     case DW_OP_rot:
4296       return "DW_OP_rot";
4297     case DW_OP_xderef:
4298       return "DW_OP_xderef";
4299     case DW_OP_abs:
4300       return "DW_OP_abs";
4301     case DW_OP_and:
4302       return "DW_OP_and";
4303     case DW_OP_div:
4304       return "DW_OP_div";
4305     case DW_OP_minus:
4306       return "DW_OP_minus";
4307     case DW_OP_mod:
4308       return "DW_OP_mod";
4309     case DW_OP_mul:
4310       return "DW_OP_mul";
4311     case DW_OP_neg:
4312       return "DW_OP_neg";
4313     case DW_OP_not:
4314       return "DW_OP_not";
4315     case DW_OP_or:
4316       return "DW_OP_or";
4317     case DW_OP_plus:
4318       return "DW_OP_plus";
4319     case DW_OP_plus_uconst:
4320       return "DW_OP_plus_uconst";
4321     case DW_OP_shl:
4322       return "DW_OP_shl";
4323     case DW_OP_shr:
4324       return "DW_OP_shr";
4325     case DW_OP_shra:
4326       return "DW_OP_shra";
4327     case DW_OP_xor:
4328       return "DW_OP_xor";
4329     case DW_OP_bra:
4330       return "DW_OP_bra";
4331     case DW_OP_eq:
4332       return "DW_OP_eq";
4333     case DW_OP_ge:
4334       return "DW_OP_ge";
4335     case DW_OP_gt:
4336       return "DW_OP_gt";
4337     case DW_OP_le:
4338       return "DW_OP_le";
4339     case DW_OP_lt:
4340       return "DW_OP_lt";
4341     case DW_OP_ne:
4342       return "DW_OP_ne";
4343     case DW_OP_skip:
4344       return "DW_OP_skip";
4345     case DW_OP_lit0:
4346       return "DW_OP_lit0";
4347     case DW_OP_lit1:
4348       return "DW_OP_lit1";
4349     case DW_OP_lit2:
4350       return "DW_OP_lit2";
4351     case DW_OP_lit3:
4352       return "DW_OP_lit3";
4353     case DW_OP_lit4:
4354       return "DW_OP_lit4";
4355     case DW_OP_lit5:
4356       return "DW_OP_lit5";
4357     case DW_OP_lit6:
4358       return "DW_OP_lit6";
4359     case DW_OP_lit7:
4360       return "DW_OP_lit7";
4361     case DW_OP_lit8:
4362       return "DW_OP_lit8";
4363     case DW_OP_lit9:
4364       return "DW_OP_lit9";
4365     case DW_OP_lit10:
4366       return "DW_OP_lit10";
4367     case DW_OP_lit11:
4368       return "DW_OP_lit11";
4369     case DW_OP_lit12:
4370       return "DW_OP_lit12";
4371     case DW_OP_lit13:
4372       return "DW_OP_lit13";
4373     case DW_OP_lit14:
4374       return "DW_OP_lit14";
4375     case DW_OP_lit15:
4376       return "DW_OP_lit15";
4377     case DW_OP_lit16:
4378       return "DW_OP_lit16";
4379     case DW_OP_lit17:
4380       return "DW_OP_lit17";
4381     case DW_OP_lit18:
4382       return "DW_OP_lit18";
4383     case DW_OP_lit19:
4384       return "DW_OP_lit19";
4385     case DW_OP_lit20:
4386       return "DW_OP_lit20";
4387     case DW_OP_lit21:
4388       return "DW_OP_lit21";
4389     case DW_OP_lit22:
4390       return "DW_OP_lit22";
4391     case DW_OP_lit23:
4392       return "DW_OP_lit23";
4393     case DW_OP_lit24:
4394       return "DW_OP_lit24";
4395     case DW_OP_lit25:
4396       return "DW_OP_lit25";
4397     case DW_OP_lit26:
4398       return "DW_OP_lit26";
4399     case DW_OP_lit27:
4400       return "DW_OP_lit27";
4401     case DW_OP_lit28:
4402       return "DW_OP_lit28";
4403     case DW_OP_lit29:
4404       return "DW_OP_lit29";
4405     case DW_OP_lit30:
4406       return "DW_OP_lit30";
4407     case DW_OP_lit31:
4408       return "DW_OP_lit31";
4409     case DW_OP_reg0:
4410       return "DW_OP_reg0";
4411     case DW_OP_reg1:
4412       return "DW_OP_reg1";
4413     case DW_OP_reg2:
4414       return "DW_OP_reg2";
4415     case DW_OP_reg3:
4416       return "DW_OP_reg3";
4417     case DW_OP_reg4:
4418       return "DW_OP_reg4";
4419     case DW_OP_reg5:
4420       return "DW_OP_reg5";
4421     case DW_OP_reg6:
4422       return "DW_OP_reg6";
4423     case DW_OP_reg7:
4424       return "DW_OP_reg7";
4425     case DW_OP_reg8:
4426       return "DW_OP_reg8";
4427     case DW_OP_reg9:
4428       return "DW_OP_reg9";
4429     case DW_OP_reg10:
4430       return "DW_OP_reg10";
4431     case DW_OP_reg11:
4432       return "DW_OP_reg11";
4433     case DW_OP_reg12:
4434       return "DW_OP_reg12";
4435     case DW_OP_reg13:
4436       return "DW_OP_reg13";
4437     case DW_OP_reg14:
4438       return "DW_OP_reg14";
4439     case DW_OP_reg15:
4440       return "DW_OP_reg15";
4441     case DW_OP_reg16:
4442       return "DW_OP_reg16";
4443     case DW_OP_reg17:
4444       return "DW_OP_reg17";
4445     case DW_OP_reg18:
4446       return "DW_OP_reg18";
4447     case DW_OP_reg19:
4448       return "DW_OP_reg19";
4449     case DW_OP_reg20:
4450       return "DW_OP_reg20";
4451     case DW_OP_reg21:
4452       return "DW_OP_reg21";
4453     case DW_OP_reg22:
4454       return "DW_OP_reg22";
4455     case DW_OP_reg23:
4456       return "DW_OP_reg23";
4457     case DW_OP_reg24:
4458       return "DW_OP_reg24";
4459     case DW_OP_reg25:
4460       return "DW_OP_reg25";
4461     case DW_OP_reg26:
4462       return "DW_OP_reg26";
4463     case DW_OP_reg27:
4464       return "DW_OP_reg27";
4465     case DW_OP_reg28:
4466       return "DW_OP_reg28";
4467     case DW_OP_reg29:
4468       return "DW_OP_reg29";
4469     case DW_OP_reg30:
4470       return "DW_OP_reg30";
4471     case DW_OP_reg31:
4472       return "DW_OP_reg31";
4473     case DW_OP_breg0:
4474       return "DW_OP_breg0";
4475     case DW_OP_breg1:
4476       return "DW_OP_breg1";
4477     case DW_OP_breg2:
4478       return "DW_OP_breg2";
4479     case DW_OP_breg3:
4480       return "DW_OP_breg3";
4481     case DW_OP_breg4:
4482       return "DW_OP_breg4";
4483     case DW_OP_breg5:
4484       return "DW_OP_breg5";
4485     case DW_OP_breg6:
4486       return "DW_OP_breg6";
4487     case DW_OP_breg7:
4488       return "DW_OP_breg7";
4489     case DW_OP_breg8:
4490       return "DW_OP_breg8";
4491     case DW_OP_breg9:
4492       return "DW_OP_breg9";
4493     case DW_OP_breg10:
4494       return "DW_OP_breg10";
4495     case DW_OP_breg11:
4496       return "DW_OP_breg11";
4497     case DW_OP_breg12:
4498       return "DW_OP_breg12";
4499     case DW_OP_breg13:
4500       return "DW_OP_breg13";
4501     case DW_OP_breg14:
4502       return "DW_OP_breg14";
4503     case DW_OP_breg15:
4504       return "DW_OP_breg15";
4505     case DW_OP_breg16:
4506       return "DW_OP_breg16";
4507     case DW_OP_breg17:
4508       return "DW_OP_breg17";
4509     case DW_OP_breg18:
4510       return "DW_OP_breg18";
4511     case DW_OP_breg19:
4512       return "DW_OP_breg19";
4513     case DW_OP_breg20:
4514       return "DW_OP_breg20";
4515     case DW_OP_breg21:
4516       return "DW_OP_breg21";
4517     case DW_OP_breg22:
4518       return "DW_OP_breg22";
4519     case DW_OP_breg23:
4520       return "DW_OP_breg23";
4521     case DW_OP_breg24:
4522       return "DW_OP_breg24";
4523     case DW_OP_breg25:
4524       return "DW_OP_breg25";
4525     case DW_OP_breg26:
4526       return "DW_OP_breg26";
4527     case DW_OP_breg27:
4528       return "DW_OP_breg27";
4529     case DW_OP_breg28:
4530       return "DW_OP_breg28";
4531     case DW_OP_breg29:
4532       return "DW_OP_breg29";
4533     case DW_OP_breg30:
4534       return "DW_OP_breg30";
4535     case DW_OP_breg31:
4536       return "DW_OP_breg31";
4537     case DW_OP_regx:
4538       return "DW_OP_regx";
4539     case DW_OP_fbreg:
4540       return "DW_OP_fbreg";
4541     case DW_OP_bregx:
4542       return "DW_OP_bregx";
4543     case DW_OP_piece:
4544       return "DW_OP_piece";
4545     case DW_OP_deref_size:
4546       return "DW_OP_deref_size";
4547     case DW_OP_xderef_size:
4548       return "DW_OP_xderef_size";
4549     case DW_OP_nop:
4550       return "DW_OP_nop";
4551
4552     case DW_OP_push_object_address:
4553       return "DW_OP_push_object_address";
4554     case DW_OP_call2:
4555       return "DW_OP_call2";
4556     case DW_OP_call4:
4557       return "DW_OP_call4";
4558     case DW_OP_call_ref:
4559       return "DW_OP_call_ref";
4560     case DW_OP_implicit_value:
4561       return "DW_OP_implicit_value";
4562     case DW_OP_stack_value:
4563       return "DW_OP_stack_value";
4564     case DW_OP_form_tls_address:
4565       return "DW_OP_form_tls_address";
4566     case DW_OP_call_frame_cfa:
4567       return "DW_OP_call_frame_cfa";
4568     case DW_OP_bit_piece:
4569       return "DW_OP_bit_piece";
4570
4571     case DW_OP_GNU_push_tls_address:
4572       return "DW_OP_GNU_push_tls_address";
4573     case DW_OP_GNU_uninit:
4574       return "DW_OP_GNU_uninit";
4575     case DW_OP_GNU_encoded_addr:
4576       return "DW_OP_GNU_encoded_addr";
4577
4578     default:
4579       return "OP_<unknown>";
4580     }
4581 }
4582
4583 /* Return a pointer to a newly allocated location description.  Location
4584    descriptions are simple expression terms that can be strung
4585    together to form more complicated location (address) descriptions.  */
4586
4587 static inline dw_loc_descr_ref
4588 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4589                unsigned HOST_WIDE_INT oprnd2)
4590 {
4591   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4592
4593   descr->dw_loc_opc = op;
4594   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4595   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4596   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4597   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4598
4599   return descr;
4600 }
4601
4602 /* Return a pointer to a newly allocated location description for
4603    REG and OFFSET.  */
4604
4605 static inline dw_loc_descr_ref
4606 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4607 {
4608   if (reg <= 31)
4609     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4610                           offset, 0);
4611   else
4612     return new_loc_descr (DW_OP_bregx, reg, offset);
4613 }
4614
4615 /* Add a location description term to a location description expression.  */
4616
4617 static inline void
4618 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4619 {
4620   dw_loc_descr_ref *d;
4621
4622   /* Find the end of the chain.  */
4623   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4624     ;
4625
4626   *d = descr;
4627 }
4628
4629 /* Add a constant OFFSET to a location expression.  */
4630
4631 static void
4632 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4633 {
4634   dw_loc_descr_ref loc;
4635   HOST_WIDE_INT *p;
4636
4637   gcc_assert (*list_head != NULL);
4638
4639   if (!offset)
4640     return;
4641
4642   /* Find the end of the chain.  */
4643   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4644     ;
4645
4646   p = NULL;
4647   if (loc->dw_loc_opc == DW_OP_fbreg
4648       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4649     p = &loc->dw_loc_oprnd1.v.val_int;
4650   else if (loc->dw_loc_opc == DW_OP_bregx)
4651     p = &loc->dw_loc_oprnd2.v.val_int;
4652
4653   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4654      offset.  Don't optimize if an signed integer overflow would happen.  */
4655   if (p != NULL
4656       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4657           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4658     *p += offset;
4659
4660   else if (offset > 0)
4661     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4662
4663   else
4664     {
4665       loc->dw_loc_next = int_loc_descriptor (offset);
4666       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4667     }
4668 }
4669
4670 #ifdef DWARF2_DEBUGGING_INFO
4671 /* Add a constant OFFSET to a location list.  */
4672
4673 static void
4674 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4675 {
4676   dw_loc_list_ref d;
4677   for (d = list_head; d != NULL; d = d->dw_loc_next)
4678     loc_descr_plus_const (&d->expr, offset);
4679 }
4680 #endif
4681
4682 /* Return the size of a location descriptor.  */
4683
4684 static unsigned long
4685 size_of_loc_descr (dw_loc_descr_ref loc)
4686 {
4687   unsigned long size = 1;
4688
4689   switch (loc->dw_loc_opc)
4690     {
4691     case DW_OP_addr:
4692       size += DWARF2_ADDR_SIZE;
4693       break;
4694     case DW_OP_const1u:
4695     case DW_OP_const1s:
4696       size += 1;
4697       break;
4698     case DW_OP_const2u:
4699     case DW_OP_const2s:
4700       size += 2;
4701       break;
4702     case DW_OP_const4u:
4703     case DW_OP_const4s:
4704       size += 4;
4705       break;
4706     case DW_OP_const8u:
4707     case DW_OP_const8s:
4708       size += 8;
4709       break;
4710     case DW_OP_constu:
4711       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4712       break;
4713     case DW_OP_consts:
4714       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4715       break;
4716     case DW_OP_pick:
4717       size += 1;
4718       break;
4719     case DW_OP_plus_uconst:
4720       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4721       break;
4722     case DW_OP_skip:
4723     case DW_OP_bra:
4724       size += 2;
4725       break;
4726     case DW_OP_breg0:
4727     case DW_OP_breg1:
4728     case DW_OP_breg2:
4729     case DW_OP_breg3:
4730     case DW_OP_breg4:
4731     case DW_OP_breg5:
4732     case DW_OP_breg6:
4733     case DW_OP_breg7:
4734     case DW_OP_breg8:
4735     case DW_OP_breg9:
4736     case DW_OP_breg10:
4737     case DW_OP_breg11:
4738     case DW_OP_breg12:
4739     case DW_OP_breg13:
4740     case DW_OP_breg14:
4741     case DW_OP_breg15:
4742     case DW_OP_breg16:
4743     case DW_OP_breg17:
4744     case DW_OP_breg18:
4745     case DW_OP_breg19:
4746     case DW_OP_breg20:
4747     case DW_OP_breg21:
4748     case DW_OP_breg22:
4749     case DW_OP_breg23:
4750     case DW_OP_breg24:
4751     case DW_OP_breg25:
4752     case DW_OP_breg26:
4753     case DW_OP_breg27:
4754     case DW_OP_breg28:
4755     case DW_OP_breg29:
4756     case DW_OP_breg30:
4757     case DW_OP_breg31:
4758       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4759       break;
4760     case DW_OP_regx:
4761       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4762       break;
4763     case DW_OP_fbreg:
4764       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4765       break;
4766     case DW_OP_bregx:
4767       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4768       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4769       break;
4770     case DW_OP_piece:
4771       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4772       break;
4773     case DW_OP_deref_size:
4774     case DW_OP_xderef_size:
4775       size += 1;
4776       break;
4777     case DW_OP_call2:
4778       size += 2;
4779       break;
4780     case DW_OP_call4:
4781       size += 4;
4782       break;
4783     case DW_OP_call_ref:
4784       size += DWARF2_ADDR_SIZE;
4785       break;
4786     case DW_OP_implicit_value:
4787       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4788               + loc->dw_loc_oprnd1.v.val_unsigned;
4789       break;
4790     default:
4791       break;
4792     }
4793
4794   return size;
4795 }
4796
4797 /* Return the size of a series of location descriptors.  */
4798
4799 static unsigned long
4800 size_of_locs (dw_loc_descr_ref loc)
4801 {
4802   dw_loc_descr_ref l;
4803   unsigned long size;
4804
4805   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4806      field, to avoid writing to a PCH file.  */
4807   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4808     {
4809       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4810         break;
4811       size += size_of_loc_descr (l);
4812     }
4813   if (! l)
4814     return size;
4815
4816   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4817     {
4818       l->dw_loc_addr = size;
4819       size += size_of_loc_descr (l);
4820     }
4821
4822   return size;
4823 }
4824
4825 #ifdef DWARF2_DEBUGGING_INFO
4826 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4827 #endif
4828
4829 /* Output location description stack opcode's operands (if any).  */
4830
4831 static void
4832 output_loc_operands (dw_loc_descr_ref loc)
4833 {
4834   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4835   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4836
4837   switch (loc->dw_loc_opc)
4838     {
4839 #ifdef DWARF2_DEBUGGING_INFO
4840     case DW_OP_const2u:
4841     case DW_OP_const2s:
4842       dw2_asm_output_data (2, val1->v.val_int, NULL);
4843       break;
4844     case DW_OP_const4u:
4845     case DW_OP_const4s:
4846       dw2_asm_output_data (4, val1->v.val_int, NULL);
4847       break;
4848     case DW_OP_const8u:
4849     case DW_OP_const8s:
4850       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4851       dw2_asm_output_data (8, val1->v.val_int, NULL);
4852       break;
4853     case DW_OP_skip:
4854     case DW_OP_bra:
4855       {
4856         int offset;
4857
4858         gcc_assert (val1->val_class == dw_val_class_loc);
4859         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4860
4861         dw2_asm_output_data (2, offset, NULL);
4862       }
4863       break;
4864     case DW_OP_implicit_value:
4865       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4866       switch (val2->val_class)
4867         {
4868         case dw_val_class_const:
4869           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4870           break;
4871         case dw_val_class_vec:
4872           {
4873             unsigned int elt_size = val2->v.val_vec.elt_size;
4874             unsigned int len = val2->v.val_vec.length;
4875             unsigned int i;
4876             unsigned char *p;
4877
4878             if (elt_size > sizeof (HOST_WIDE_INT))
4879               {
4880                 elt_size /= 2;
4881                 len *= 2;
4882               }
4883             for (i = 0, p = val2->v.val_vec.array;
4884                  i < len;
4885                  i++, p += elt_size)
4886               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4887                                    "fp or vector constant word %u", i);
4888           }
4889           break;
4890         case dw_val_class_const_double:
4891           {
4892             unsigned HOST_WIDE_INT first, second;
4893
4894             if (WORDS_BIG_ENDIAN)
4895               {
4896                 first = val2->v.val_double.high;
4897                 second = val2->v.val_double.low;
4898               }
4899             else
4900               {
4901                 first = val2->v.val_double.low;
4902                 second = val2->v.val_double.high;
4903               }
4904             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4905                                  first, NULL);
4906             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4907                                  second, NULL);
4908           }
4909           break;
4910         case dw_val_class_addr:
4911           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4912           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4913           break;
4914         default:
4915           gcc_unreachable ();
4916         }
4917       break;
4918 #else
4919     case DW_OP_const2u:
4920     case DW_OP_const2s:
4921     case DW_OP_const4u:
4922     case DW_OP_const4s:
4923     case DW_OP_const8u:
4924     case DW_OP_const8s:
4925     case DW_OP_skip:
4926     case DW_OP_bra:
4927     case DW_OP_implicit_value:
4928       /* We currently don't make any attempt to make sure these are
4929          aligned properly like we do for the main unwind info, so
4930          don't support emitting things larger than a byte if we're
4931          only doing unwinding.  */
4932       gcc_unreachable ();
4933 #endif
4934     case DW_OP_const1u:
4935     case DW_OP_const1s:
4936       dw2_asm_output_data (1, val1->v.val_int, NULL);
4937       break;
4938     case DW_OP_constu:
4939       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4940       break;
4941     case DW_OP_consts:
4942       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4943       break;
4944     case DW_OP_pick:
4945       dw2_asm_output_data (1, val1->v.val_int, NULL);
4946       break;
4947     case DW_OP_plus_uconst:
4948       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4949       break;
4950     case DW_OP_breg0:
4951     case DW_OP_breg1:
4952     case DW_OP_breg2:
4953     case DW_OP_breg3:
4954     case DW_OP_breg4:
4955     case DW_OP_breg5:
4956     case DW_OP_breg6:
4957     case DW_OP_breg7:
4958     case DW_OP_breg8:
4959     case DW_OP_breg9:
4960     case DW_OP_breg10:
4961     case DW_OP_breg11:
4962     case DW_OP_breg12:
4963     case DW_OP_breg13:
4964     case DW_OP_breg14:
4965     case DW_OP_breg15:
4966     case DW_OP_breg16:
4967     case DW_OP_breg17:
4968     case DW_OP_breg18:
4969     case DW_OP_breg19:
4970     case DW_OP_breg20:
4971     case DW_OP_breg21:
4972     case DW_OP_breg22:
4973     case DW_OP_breg23:
4974     case DW_OP_breg24:
4975     case DW_OP_breg25:
4976     case DW_OP_breg26:
4977     case DW_OP_breg27:
4978     case DW_OP_breg28:
4979     case DW_OP_breg29:
4980     case DW_OP_breg30:
4981     case DW_OP_breg31:
4982       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4983       break;
4984     case DW_OP_regx:
4985       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4986       break;
4987     case DW_OP_fbreg:
4988       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4989       break;
4990     case DW_OP_bregx:
4991       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4992       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4993       break;
4994     case DW_OP_piece:
4995       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4996       break;
4997     case DW_OP_deref_size:
4998     case DW_OP_xderef_size:
4999       dw2_asm_output_data (1, val1->v.val_int, NULL);
5000       break;
5001
5002     case DW_OP_addr:
5003       if (loc->dtprel)
5004         {
5005           if (targetm.asm_out.output_dwarf_dtprel)
5006             {
5007               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5008                                                    DWARF2_ADDR_SIZE,
5009                                                    val1->v.val_addr);
5010               fputc ('\n', asm_out_file);
5011             }
5012           else
5013             gcc_unreachable ();
5014         }
5015       else
5016         {
5017 #ifdef DWARF2_DEBUGGING_INFO
5018           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5019 #else
5020           gcc_unreachable ();
5021 #endif
5022         }
5023       break;
5024
5025     default:
5026       /* Other codes have no operands.  */
5027       break;
5028     }
5029 }
5030
5031 /* Output a sequence of location operations.  */
5032
5033 static void
5034 output_loc_sequence (dw_loc_descr_ref loc)
5035 {
5036   for (; loc != NULL; loc = loc->dw_loc_next)
5037     {
5038       /* Output the opcode.  */
5039       dw2_asm_output_data (1, loc->dw_loc_opc,
5040                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5041
5042       /* Output the operand(s) (if any).  */
5043       output_loc_operands (loc);
5044     }
5045 }
5046
5047 /* Output location description stack opcode's operands (if any).
5048    The output is single bytes on a line, suitable for .cfi_escape.  */
5049
5050 static void
5051 output_loc_operands_raw (dw_loc_descr_ref loc)
5052 {
5053   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5054   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5055
5056   switch (loc->dw_loc_opc)
5057     {
5058     case DW_OP_addr:
5059     case DW_OP_implicit_value:
5060       /* We cannot output addresses in .cfi_escape, only bytes.  */
5061       gcc_unreachable ();
5062
5063     case DW_OP_const1u:
5064     case DW_OP_const1s:
5065     case DW_OP_pick:
5066     case DW_OP_deref_size:
5067     case DW_OP_xderef_size:
5068       fputc (',', asm_out_file);
5069       dw2_asm_output_data_raw (1, val1->v.val_int);
5070       break;
5071
5072     case DW_OP_const2u:
5073     case DW_OP_const2s:
5074       fputc (',', asm_out_file);
5075       dw2_asm_output_data_raw (2, val1->v.val_int);
5076       break;
5077
5078     case DW_OP_const4u:
5079     case DW_OP_const4s:
5080       fputc (',', asm_out_file);
5081       dw2_asm_output_data_raw (4, val1->v.val_int);
5082       break;
5083
5084     case DW_OP_const8u:
5085     case DW_OP_const8s:
5086       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5087       fputc (',', asm_out_file);
5088       dw2_asm_output_data_raw (8, val1->v.val_int);
5089       break;
5090
5091     case DW_OP_skip:
5092     case DW_OP_bra:
5093       {
5094         int offset;
5095
5096         gcc_assert (val1->val_class == dw_val_class_loc);
5097         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5098
5099         fputc (',', asm_out_file);
5100         dw2_asm_output_data_raw (2, offset);
5101       }
5102       break;
5103
5104     case DW_OP_constu:
5105     case DW_OP_plus_uconst:
5106     case DW_OP_regx:
5107     case DW_OP_piece:
5108       fputc (',', asm_out_file);
5109       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5110       break;
5111
5112     case DW_OP_consts:
5113     case DW_OP_breg0:
5114     case DW_OP_breg1:
5115     case DW_OP_breg2:
5116     case DW_OP_breg3:
5117     case DW_OP_breg4:
5118     case DW_OP_breg5:
5119     case DW_OP_breg6:
5120     case DW_OP_breg7:
5121     case DW_OP_breg8:
5122     case DW_OP_breg9:
5123     case DW_OP_breg10:
5124     case DW_OP_breg11:
5125     case DW_OP_breg12:
5126     case DW_OP_breg13:
5127     case DW_OP_breg14:
5128     case DW_OP_breg15:
5129     case DW_OP_breg16:
5130     case DW_OP_breg17:
5131     case DW_OP_breg18:
5132     case DW_OP_breg19:
5133     case DW_OP_breg20:
5134     case DW_OP_breg21:
5135     case DW_OP_breg22:
5136     case DW_OP_breg23:
5137     case DW_OP_breg24:
5138     case DW_OP_breg25:
5139     case DW_OP_breg26:
5140     case DW_OP_breg27:
5141     case DW_OP_breg28:
5142     case DW_OP_breg29:
5143     case DW_OP_breg30:
5144     case DW_OP_breg31:
5145     case DW_OP_fbreg:
5146       fputc (',', asm_out_file);
5147       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5148       break;
5149
5150     case DW_OP_bregx:
5151       fputc (',', asm_out_file);
5152       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5153       fputc (',', asm_out_file);
5154       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5155       break;
5156
5157     default:
5158       /* Other codes have no operands.  */
5159       break;
5160     }
5161 }
5162
5163 static void
5164 output_loc_sequence_raw (dw_loc_descr_ref loc)
5165 {
5166   while (1)
5167     {
5168       /* Output the opcode.  */
5169       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5170       output_loc_operands_raw (loc);
5171
5172       if (!loc->dw_loc_next)
5173         break;
5174       loc = loc->dw_loc_next;
5175
5176       fputc (',', asm_out_file);
5177     }
5178 }
5179
5180 /* This routine will generate the correct assembly data for a location
5181    description based on a cfi entry with a complex address.  */
5182
5183 static void
5184 output_cfa_loc (dw_cfi_ref cfi)
5185 {
5186   dw_loc_descr_ref loc;
5187   unsigned long size;
5188
5189   if (cfi->dw_cfi_opc == DW_CFA_expression)
5190     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5191
5192   /* Output the size of the block.  */
5193   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5194   size = size_of_locs (loc);
5195   dw2_asm_output_data_uleb128 (size, NULL);
5196
5197   /* Now output the operations themselves.  */
5198   output_loc_sequence (loc);
5199 }
5200
5201 /* Similar, but used for .cfi_escape.  */
5202
5203 static void
5204 output_cfa_loc_raw (dw_cfi_ref cfi)
5205 {
5206   dw_loc_descr_ref loc;
5207   unsigned long size;
5208
5209   if (cfi->dw_cfi_opc == DW_CFA_expression)
5210     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5211
5212   /* Output the size of the block.  */
5213   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5214   size = size_of_locs (loc);
5215   dw2_asm_output_data_uleb128_raw (size);
5216   fputc (',', asm_out_file);
5217
5218   /* Now output the operations themselves.  */
5219   output_loc_sequence_raw (loc);
5220 }
5221
5222 /* This function builds a dwarf location descriptor sequence from a
5223    dw_cfa_location, adding the given OFFSET to the result of the
5224    expression.  */
5225
5226 static struct dw_loc_descr_struct *
5227 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5228 {
5229   struct dw_loc_descr_struct *head, *tmp;
5230
5231   offset += cfa->offset;
5232
5233   if (cfa->indirect)
5234     {
5235       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5236       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5237       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5238       add_loc_descr (&head, tmp);
5239       if (offset != 0)
5240         {
5241           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5242           add_loc_descr (&head, tmp);
5243         }
5244     }
5245   else
5246     head = new_reg_loc_descr (cfa->reg, offset);
5247
5248   return head;
5249 }
5250
5251 /* This function builds a dwarf location descriptor sequence for
5252    the address at OFFSET from the CFA when stack is aligned to
5253    ALIGNMENT byte.  */
5254
5255 static struct dw_loc_descr_struct *
5256 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5257 {
5258   struct dw_loc_descr_struct *head;
5259   unsigned int dwarf_fp
5260     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5261
5262  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5263   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5264     {
5265       head = new_reg_loc_descr (dwarf_fp, 0);
5266       add_loc_descr (&head, int_loc_descriptor (alignment));
5267       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5268       loc_descr_plus_const (&head, offset);
5269     }
5270   else
5271     head = new_reg_loc_descr (dwarf_fp, offset);
5272   return head;
5273 }
5274
5275 /* This function fills in aa dw_cfa_location structure from a dwarf location
5276    descriptor sequence.  */
5277
5278 static void
5279 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5280 {
5281   struct dw_loc_descr_struct *ptr;
5282   cfa->offset = 0;
5283   cfa->base_offset = 0;
5284   cfa->indirect = 0;
5285   cfa->reg = -1;
5286
5287   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5288     {
5289       enum dwarf_location_atom op = ptr->dw_loc_opc;
5290
5291       switch (op)
5292         {
5293         case DW_OP_reg0:
5294         case DW_OP_reg1:
5295         case DW_OP_reg2:
5296         case DW_OP_reg3:
5297         case DW_OP_reg4:
5298         case DW_OP_reg5:
5299         case DW_OP_reg6:
5300         case DW_OP_reg7:
5301         case DW_OP_reg8:
5302         case DW_OP_reg9:
5303         case DW_OP_reg10:
5304         case DW_OP_reg11:
5305         case DW_OP_reg12:
5306         case DW_OP_reg13:
5307         case DW_OP_reg14:
5308         case DW_OP_reg15:
5309         case DW_OP_reg16:
5310         case DW_OP_reg17:
5311         case DW_OP_reg18:
5312         case DW_OP_reg19:
5313         case DW_OP_reg20:
5314         case DW_OP_reg21:
5315         case DW_OP_reg22:
5316         case DW_OP_reg23:
5317         case DW_OP_reg24:
5318         case DW_OP_reg25:
5319         case DW_OP_reg26:
5320         case DW_OP_reg27:
5321         case DW_OP_reg28:
5322         case DW_OP_reg29:
5323         case DW_OP_reg30:
5324         case DW_OP_reg31:
5325           cfa->reg = op - DW_OP_reg0;
5326           break;
5327         case DW_OP_regx:
5328           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5329           break;
5330         case DW_OP_breg0:
5331         case DW_OP_breg1:
5332         case DW_OP_breg2:
5333         case DW_OP_breg3:
5334         case DW_OP_breg4:
5335         case DW_OP_breg5:
5336         case DW_OP_breg6:
5337         case DW_OP_breg7:
5338         case DW_OP_breg8:
5339         case DW_OP_breg9:
5340         case DW_OP_breg10:
5341         case DW_OP_breg11:
5342         case DW_OP_breg12:
5343         case DW_OP_breg13:
5344         case DW_OP_breg14:
5345         case DW_OP_breg15:
5346         case DW_OP_breg16:
5347         case DW_OP_breg17:
5348         case DW_OP_breg18:
5349         case DW_OP_breg19:
5350         case DW_OP_breg20:
5351         case DW_OP_breg21:
5352         case DW_OP_breg22:
5353         case DW_OP_breg23:
5354         case DW_OP_breg24:
5355         case DW_OP_breg25:
5356         case DW_OP_breg26:
5357         case DW_OP_breg27:
5358         case DW_OP_breg28:
5359         case DW_OP_breg29:
5360         case DW_OP_breg30:
5361         case DW_OP_breg31:
5362           cfa->reg = op - DW_OP_breg0;
5363           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5364           break;
5365         case DW_OP_bregx:
5366           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5367           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5368           break;
5369         case DW_OP_deref:
5370           cfa->indirect = 1;
5371           break;
5372         case DW_OP_plus_uconst:
5373           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5374           break;
5375         default:
5376           internal_error ("DW_LOC_OP %s not implemented",
5377                           dwarf_stack_op_name (ptr->dw_loc_opc));
5378         }
5379     }
5380 }
5381 #endif /* .debug_frame support */
5382 \f
5383 /* And now, the support for symbolic debugging information.  */
5384 #ifdef DWARF2_DEBUGGING_INFO
5385
5386 /* .debug_str support.  */
5387 static int output_indirect_string (void **, void *);
5388
5389 static void dwarf2out_init (const char *);
5390 static void dwarf2out_finish (const char *);
5391 static void dwarf2out_define (unsigned int, const char *);
5392 static void dwarf2out_undef (unsigned int, const char *);
5393 static void dwarf2out_start_source_file (unsigned, const char *);
5394 static void dwarf2out_end_source_file (unsigned);
5395 static void dwarf2out_begin_block (unsigned, unsigned);
5396 static void dwarf2out_end_block (unsigned, unsigned);
5397 static bool dwarf2out_ignore_block (const_tree);
5398 static void dwarf2out_global_decl (tree);
5399 static void dwarf2out_type_decl (tree, int);
5400 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5401 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5402                                                  dw_die_ref);
5403 static void dwarf2out_abstract_function (tree);
5404 static void dwarf2out_var_location (rtx);
5405 static void dwarf2out_begin_function (tree);
5406 static void dwarf2out_set_name (tree, tree);
5407
5408 /* The debug hooks structure.  */
5409
5410 const struct gcc_debug_hooks dwarf2_debug_hooks =
5411 {
5412   dwarf2out_init,
5413   dwarf2out_finish,
5414   dwarf2out_define,
5415   dwarf2out_undef,
5416   dwarf2out_start_source_file,
5417   dwarf2out_end_source_file,
5418   dwarf2out_begin_block,
5419   dwarf2out_end_block,
5420   dwarf2out_ignore_block,
5421   dwarf2out_source_line,
5422   dwarf2out_begin_prologue,
5423   debug_nothing_int_charstar,   /* end_prologue */
5424   dwarf2out_end_epilogue,
5425   dwarf2out_begin_function,
5426   debug_nothing_int,            /* end_function */
5427   dwarf2out_decl,               /* function_decl */
5428   dwarf2out_global_decl,
5429   dwarf2out_type_decl,          /* type_decl */
5430   dwarf2out_imported_module_or_decl,
5431   debug_nothing_tree,           /* deferred_inline_function */
5432   /* The DWARF 2 backend tries to reduce debugging bloat by not
5433      emitting the abstract description of inline functions until
5434      something tries to reference them.  */
5435   dwarf2out_abstract_function,  /* outlining_inline_function */
5436   debug_nothing_rtx,            /* label */
5437   debug_nothing_int,            /* handle_pch */
5438   dwarf2out_var_location,
5439   dwarf2out_switch_text_section,
5440   dwarf2out_set_name,
5441   1                             /* start_end_main_source_file */
5442 };
5443 #endif
5444 \f
5445 /* NOTE: In the comments in this file, many references are made to
5446    "Debugging Information Entries".  This term is abbreviated as `DIE'
5447    throughout the remainder of this file.  */
5448
5449 /* An internal representation of the DWARF output is built, and then
5450    walked to generate the DWARF debugging info.  The walk of the internal
5451    representation is done after the entire program has been compiled.
5452    The types below are used to describe the internal representation.  */
5453
5454 /* Various DIE's use offsets relative to the beginning of the
5455    .debug_info section to refer to each other.  */
5456
5457 typedef long int dw_offset;
5458
5459 /* Define typedefs here to avoid circular dependencies.  */
5460
5461 typedef struct dw_attr_struct *dw_attr_ref;
5462 typedef struct dw_line_info_struct *dw_line_info_ref;
5463 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5464 typedef struct pubname_struct *pubname_ref;
5465 typedef struct dw_ranges_struct *dw_ranges_ref;
5466 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5467
5468 /* Each entry in the line_info_table maintains the file and
5469    line number associated with the label generated for that
5470    entry.  The label gives the PC value associated with
5471    the line number entry.  */
5472
5473 typedef struct GTY(()) dw_line_info_struct {
5474   unsigned long dw_file_num;
5475   unsigned long dw_line_num;
5476 }
5477 dw_line_info_entry;
5478
5479 /* Line information for functions in separate sections; each one gets its
5480    own sequence.  */
5481 typedef struct GTY(()) dw_separate_line_info_struct {
5482   unsigned long dw_file_num;
5483   unsigned long dw_line_num;
5484   unsigned long function;
5485 }
5486 dw_separate_line_info_entry;
5487
5488 /* Each DIE attribute has a field specifying the attribute kind,
5489    a link to the next attribute in the chain, and an attribute value.
5490    Attributes are typically linked below the DIE they modify.  */
5491
5492 typedef struct GTY(()) dw_attr_struct {
5493   enum dwarf_attribute dw_attr;
5494   dw_val_node dw_attr_val;
5495 }
5496 dw_attr_node;
5497
5498 DEF_VEC_O(dw_attr_node);
5499 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5500
5501 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5502    The children of each node form a circular list linked by
5503    die_sib.  die_child points to the node *before* the "first" child node.  */
5504
5505 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5506   enum dwarf_tag die_tag;
5507   char *die_symbol;
5508   VEC(dw_attr_node,gc) * die_attr;
5509   dw_die_ref die_parent;
5510   dw_die_ref die_child;
5511   dw_die_ref die_sib;
5512   dw_die_ref die_definition; /* ref from a specification to its definition */
5513   dw_offset die_offset;
5514   unsigned long die_abbrev;
5515   int die_mark;
5516   /* Die is used and must not be pruned as unused.  */
5517   int die_perennial_p;
5518   unsigned int decl_id;
5519 }
5520 die_node;
5521
5522 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5523 #define FOR_EACH_CHILD(die, c, expr) do {       \
5524   c = die->die_child;                           \
5525   if (c) do {                                   \
5526     c = c->die_sib;                             \
5527     expr;                                       \
5528   } while (c != die->die_child);                \
5529 } while (0)
5530
5531 /* The pubname structure */
5532
5533 typedef struct GTY(()) pubname_struct {
5534   dw_die_ref die;
5535   const char *name;
5536 }
5537 pubname_entry;
5538
5539 DEF_VEC_O(pubname_entry);
5540 DEF_VEC_ALLOC_O(pubname_entry, gc);
5541
5542 struct GTY(()) dw_ranges_struct {
5543   /* If this is positive, it's a block number, otherwise it's a
5544      bitwise-negated index into dw_ranges_by_label.  */
5545   int num;
5546 };
5547
5548 struct GTY(()) dw_ranges_by_label_struct {
5549   const char *begin;
5550   const char *end;
5551 };
5552
5553 /* The limbo die list structure.  */
5554 typedef struct GTY(()) limbo_die_struct {
5555   dw_die_ref die;
5556   tree created_for;
5557   struct limbo_die_struct *next;
5558 }
5559 limbo_die_node;
5560
5561 /* How to start an assembler comment.  */
5562 #ifndef ASM_COMMENT_START
5563 #define ASM_COMMENT_START ";#"
5564 #endif
5565
5566 /* Define a macro which returns nonzero for a TYPE_DECL which was
5567    implicitly generated for a tagged type.
5568
5569    Note that unlike the gcc front end (which generates a NULL named
5570    TYPE_DECL node for each complete tagged type, each array type, and
5571    each function type node created) the g++ front end generates a
5572    _named_ TYPE_DECL node for each tagged type node created.
5573    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5574    generate a DW_TAG_typedef DIE for them.  */
5575
5576 #define TYPE_DECL_IS_STUB(decl)                         \
5577   (DECL_NAME (decl) == NULL_TREE                        \
5578    || (DECL_ARTIFICIAL (decl)                           \
5579        && is_tagged_type (TREE_TYPE (decl))             \
5580        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5581            /* This is necessary for stub decls that     \
5582               appear in nested inline functions.  */    \
5583            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5584                && (decl_ultimate_origin (decl)          \
5585                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5586
5587 /* Information concerning the compilation unit's programming
5588    language, and compiler version.  */
5589
5590 /* Fixed size portion of the DWARF compilation unit header.  */
5591 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5592   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5593
5594 /* Fixed size portion of public names info.  */
5595 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5596
5597 /* Fixed size portion of the address range info.  */
5598 #define DWARF_ARANGES_HEADER_SIZE                                       \
5599   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5600                 DWARF2_ADDR_SIZE * 2)                                   \
5601    - DWARF_INITIAL_LENGTH_SIZE)
5602
5603 /* Size of padding portion in the address range info.  It must be
5604    aligned to twice the pointer size.  */
5605 #define DWARF_ARANGES_PAD_SIZE \
5606   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5607                 DWARF2_ADDR_SIZE * 2)                              \
5608    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5609
5610 /* Use assembler line directives if available.  */
5611 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5612 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5613 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5614 #else
5615 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5616 #endif
5617 #endif
5618
5619 /* Minimum line offset in a special line info. opcode.
5620    This value was chosen to give a reasonable range of values.  */
5621 #define DWARF_LINE_BASE  -10
5622
5623 /* First special line opcode - leave room for the standard opcodes.  */
5624 #define DWARF_LINE_OPCODE_BASE  10
5625
5626 /* Range of line offsets in a special line info. opcode.  */
5627 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5628
5629 /* Flag that indicates the initial value of the is_stmt_start flag.
5630    In the present implementation, we do not mark any lines as
5631    the beginning of a source statement, because that information
5632    is not made available by the GCC front-end.  */
5633 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5634
5635 #ifdef DWARF2_DEBUGGING_INFO
5636 /* This location is used by calc_die_sizes() to keep track
5637    the offset of each DIE within the .debug_info section.  */
5638 static unsigned long next_die_offset;
5639 #endif
5640
5641 /* Record the root of the DIE's built for the current compilation unit.  */
5642 static GTY(()) dw_die_ref comp_unit_die;
5643
5644 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5645 static GTY(()) limbo_die_node *limbo_die_list;
5646
5647 /* A list of DIEs for which we may have to generate
5648    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5649    set.  */
5650 static GTY(()) limbo_die_node *deferred_asm_name;
5651
5652 /* Filenames referenced by this compilation unit.  */
5653 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5654
5655 /* A hash table of references to DIE's that describe declarations.
5656    The key is a DECL_UID() which is a unique number identifying each decl.  */
5657 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5658
5659 /* A hash table of references to DIE's that describe COMMON blocks.
5660    The key is DECL_UID() ^ die_parent.  */
5661 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5662
5663 typedef struct GTY(()) die_arg_entry_struct {
5664     dw_die_ref die;
5665     tree arg;
5666 } die_arg_entry;
5667
5668 DEF_VEC_O(die_arg_entry);
5669 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5670
5671 /* Node of the variable location list.  */
5672 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5673   rtx GTY (()) var_loc_note;
5674   const char * GTY (()) label;
5675   const char * GTY (()) section_label;
5676   struct var_loc_node * GTY (()) next;
5677 };
5678
5679 /* Variable location list.  */
5680 struct GTY (()) var_loc_list_def {
5681   struct var_loc_node * GTY (()) first;
5682
5683   /* Do not mark the last element of the chained list because
5684      it is marked through the chain.  */
5685   struct var_loc_node * GTY ((skip ("%h"))) last;
5686
5687   /* DECL_UID of the variable decl.  */
5688   unsigned int decl_id;
5689 };
5690 typedef struct var_loc_list_def var_loc_list;
5691
5692
5693 /* Table of decl location linked lists.  */
5694 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5695
5696 /* A pointer to the base of a list of references to DIE's that
5697    are uniquely identified by their tag, presence/absence of
5698    children DIE's, and list of attribute/value pairs.  */
5699 static GTY((length ("abbrev_die_table_allocated")))
5700   dw_die_ref *abbrev_die_table;
5701
5702 /* Number of elements currently allocated for abbrev_die_table.  */
5703 static GTY(()) unsigned abbrev_die_table_allocated;
5704
5705 /* Number of elements in type_die_table currently in use.  */
5706 static GTY(()) unsigned abbrev_die_table_in_use;
5707
5708 /* Size (in elements) of increments by which we may expand the
5709    abbrev_die_table.  */
5710 #define ABBREV_DIE_TABLE_INCREMENT 256
5711
5712 /* A pointer to the base of a table that contains line information
5713    for each source code line in .text in the compilation unit.  */
5714 static GTY((length ("line_info_table_allocated")))
5715      dw_line_info_ref line_info_table;
5716
5717 /* Number of elements currently allocated for line_info_table.  */
5718 static GTY(()) unsigned line_info_table_allocated;
5719
5720 /* Number of elements in line_info_table currently in use.  */
5721 static GTY(()) unsigned line_info_table_in_use;
5722
5723 /* A pointer to the base of a table that contains line information
5724    for each source code line outside of .text in the compilation unit.  */
5725 static GTY ((length ("separate_line_info_table_allocated")))
5726      dw_separate_line_info_ref separate_line_info_table;
5727
5728 /* Number of elements currently allocated for separate_line_info_table.  */
5729 static GTY(()) unsigned separate_line_info_table_allocated;
5730
5731 /* Number of elements in separate_line_info_table currently in use.  */
5732 static GTY(()) unsigned separate_line_info_table_in_use;
5733
5734 /* Size (in elements) of increments by which we may expand the
5735    line_info_table.  */
5736 #define LINE_INFO_TABLE_INCREMENT 1024
5737
5738 /* A pointer to the base of a table that contains a list of publicly
5739    accessible names.  */
5740 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5741
5742 /* A pointer to the base of a table that contains a list of publicly
5743    accessible types.  */
5744 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5745
5746 /* Array of dies for which we should generate .debug_arange info.  */
5747 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5748
5749 /* Number of elements currently allocated for arange_table.  */
5750 static GTY(()) unsigned arange_table_allocated;
5751
5752 /* Number of elements in arange_table currently in use.  */
5753 static GTY(()) unsigned arange_table_in_use;
5754
5755 /* Size (in elements) of increments by which we may expand the
5756    arange_table.  */
5757 #define ARANGE_TABLE_INCREMENT 64
5758
5759 /* Array of dies for which we should generate .debug_ranges info.  */
5760 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5761
5762 /* Number of elements currently allocated for ranges_table.  */
5763 static GTY(()) unsigned ranges_table_allocated;
5764
5765 /* Number of elements in ranges_table currently in use.  */
5766 static GTY(()) unsigned ranges_table_in_use;
5767
5768 /* Array of pairs of labels referenced in ranges_table.  */
5769 static GTY ((length ("ranges_by_label_allocated")))
5770      dw_ranges_by_label_ref ranges_by_label;
5771
5772 /* Number of elements currently allocated for ranges_by_label.  */
5773 static GTY(()) unsigned ranges_by_label_allocated;
5774
5775 /* Number of elements in ranges_by_label currently in use.  */
5776 static GTY(()) unsigned ranges_by_label_in_use;
5777
5778 /* Size (in elements) of increments by which we may expand the
5779    ranges_table.  */
5780 #define RANGES_TABLE_INCREMENT 64
5781
5782 /* Whether we have location lists that need outputting */
5783 static GTY(()) bool have_location_lists;
5784
5785 /* Unique label counter.  */
5786 static GTY(()) unsigned int loclabel_num;
5787
5788 #ifdef DWARF2_DEBUGGING_INFO
5789 /* Record whether the function being analyzed contains inlined functions.  */
5790 static int current_function_has_inlines;
5791 #endif
5792 #if 0 && defined (MIPS_DEBUGGING_INFO)
5793 static int comp_unit_has_inlines;
5794 #endif
5795
5796 /* The last file entry emitted by maybe_emit_file().  */
5797 static GTY(()) struct dwarf_file_data * last_emitted_file;
5798
5799 /* Number of internal labels generated by gen_internal_sym().  */
5800 static GTY(()) int label_num;
5801
5802 /* Cached result of previous call to lookup_filename.  */
5803 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5804
5805 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5806
5807 #ifdef DWARF2_DEBUGGING_INFO
5808
5809 /* Offset from the "steady-state frame pointer" to the frame base,
5810    within the current function.  */
5811 static HOST_WIDE_INT frame_pointer_fb_offset;
5812
5813 /* Forward declarations for functions defined in this file.  */
5814
5815 static int is_pseudo_reg (const_rtx);
5816 static tree type_main_variant (tree);
5817 static int is_tagged_type (const_tree);
5818 static const char *dwarf_tag_name (unsigned);
5819 static const char *dwarf_attr_name (unsigned);
5820 static const char *dwarf_form_name (unsigned);
5821 static tree decl_ultimate_origin (const_tree);
5822 static tree decl_class_context (tree);
5823 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5824 static inline enum dw_val_class AT_class (dw_attr_ref);
5825 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5826 static inline unsigned AT_flag (dw_attr_ref);
5827 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5828 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5829 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5830 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5831 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
5832                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
5833 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5834                                unsigned int, unsigned char *);
5835 static hashval_t debug_str_do_hash (const void *);
5836 static int debug_str_eq (const void *, const void *);
5837 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5838 static inline const char *AT_string (dw_attr_ref);
5839 static enum dwarf_form AT_string_form (dw_attr_ref);
5840 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5841 static void add_AT_specification (dw_die_ref, dw_die_ref);
5842 static inline dw_die_ref AT_ref (dw_attr_ref);
5843 static inline int AT_ref_external (dw_attr_ref);
5844 static inline void set_AT_ref_external (dw_attr_ref, int);
5845 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5846 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5847 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5848 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5849                              dw_loc_list_ref);
5850 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5851 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5852 static inline rtx AT_addr (dw_attr_ref);
5853 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5854 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5855 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5856 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5857                            unsigned HOST_WIDE_INT);
5858 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5859                                unsigned long);
5860 static inline const char *AT_lbl (dw_attr_ref);
5861 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5862 static const char *get_AT_low_pc (dw_die_ref);
5863 static const char *get_AT_hi_pc (dw_die_ref);
5864 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5865 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5866 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5867 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5868 static bool is_c_family (void);
5869 static bool is_cxx (void);
5870 static bool is_java (void);
5871 static bool is_fortran (void);
5872 static bool is_ada (void);
5873 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5874 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5875 static void add_child_die (dw_die_ref, dw_die_ref);
5876 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5877 static dw_die_ref lookup_type_die (tree);
5878 static void equate_type_number_to_die (tree, dw_die_ref);
5879 static hashval_t decl_die_table_hash (const void *);
5880 static int decl_die_table_eq (const void *, const void *);
5881 static dw_die_ref lookup_decl_die (tree);
5882 static hashval_t common_block_die_table_hash (const void *);
5883 static int common_block_die_table_eq (const void *, const void *);
5884 static hashval_t decl_loc_table_hash (const void *);
5885 static int decl_loc_table_eq (const void *, const void *);
5886 static var_loc_list *lookup_decl_loc (const_tree);
5887 static void equate_decl_number_to_die (tree, dw_die_ref);
5888 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5889 static void print_spaces (FILE *);
5890 static void print_die (dw_die_ref, FILE *);
5891 static void print_dwarf_line_table (FILE *);
5892 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5893 static dw_die_ref pop_compile_unit (dw_die_ref);
5894 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5895 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5896 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5897 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5898 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5899 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5900 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5901 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5902 static void compute_section_prefix (dw_die_ref);
5903 static int is_type_die (dw_die_ref);
5904 static int is_comdat_die (dw_die_ref);
5905 static int is_symbol_die (dw_die_ref);
5906 static void assign_symbol_names (dw_die_ref);
5907 static void break_out_includes (dw_die_ref);
5908 static hashval_t htab_cu_hash (const void *);
5909 static int htab_cu_eq (const void *, const void *);
5910 static void htab_cu_del (void *);
5911 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5912 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5913 static void add_sibling_attributes (dw_die_ref);
5914 static void build_abbrev_table (dw_die_ref);
5915 static void output_location_lists (dw_die_ref);
5916 static int constant_size (unsigned HOST_WIDE_INT);
5917 static unsigned long size_of_die (dw_die_ref);
5918 static void calc_die_sizes (dw_die_ref);
5919 static void mark_dies (dw_die_ref);
5920 static void unmark_dies (dw_die_ref);
5921 static void unmark_all_dies (dw_die_ref);
5922 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5923 static unsigned long size_of_aranges (void);
5924 static enum dwarf_form value_format (dw_attr_ref);
5925 static void output_value_format (dw_attr_ref);
5926 static void output_abbrev_section (void);
5927 static void output_die_symbol (dw_die_ref);
5928 static void output_die (dw_die_ref);
5929 static void output_compilation_unit_header (void);
5930 static void output_comp_unit (dw_die_ref, int);
5931 static const char *dwarf2_name (tree, int);
5932 static void add_pubname (tree, dw_die_ref);
5933 static void add_pubname_string (const char *, dw_die_ref);
5934 static void add_pubtype (tree, dw_die_ref);
5935 static void output_pubnames (VEC (pubname_entry,gc) *);
5936 static void add_arange (tree, dw_die_ref);
5937 static void output_aranges (void);
5938 static unsigned int add_ranges_num (int);
5939 static unsigned int add_ranges (const_tree);
5940 static unsigned int add_ranges_by_labels (const char *, const char *);
5941 static void output_ranges (void);
5942 static void output_line_info (void);
5943 static void output_file_names (void);
5944 static dw_die_ref base_type_die (tree);
5945 static int is_base_type (tree);
5946 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5947 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5948 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
5949 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
5950 static int type_is_enum (const_tree);
5951 static unsigned int dbx_reg_number (const_rtx);
5952 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5953 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5954 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5955                                                 enum var_init_status);
5956 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5957                                                      enum var_init_status);
5958 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5959                                          enum var_init_status);
5960 static int is_based_loc (const_rtx);
5961 static int resolve_one_addr (rtx *, void *);
5962 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5963                                             enum var_init_status);
5964 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5965                                                enum var_init_status);
5966 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
5967                                         enum var_init_status);
5968 static dw_loc_list_ref loc_list_from_tree (tree, int);
5969 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
5970 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5971 static tree field_type (const_tree);
5972 static unsigned int simple_type_align_in_bits (const_tree);
5973 static unsigned int simple_decl_align_in_bits (const_tree);
5974 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5975 static HOST_WIDE_INT field_byte_offset (const_tree);
5976 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5977                                          dw_loc_list_ref);
5978 static void add_data_member_location_attribute (dw_die_ref, tree);
5979 static bool add_const_value_attribute (dw_die_ref, rtx);
5980 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5981 static void insert_float (const_rtx, unsigned char *);
5982 static rtx rtl_for_decl_location (tree);
5983 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
5984                                                    enum dwarf_attribute);
5985 static bool tree_add_const_value_attribute (dw_die_ref, tree);
5986 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
5987 static void add_name_attribute (dw_die_ref, const char *);
5988 static void add_comp_dir_attribute (dw_die_ref);
5989 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5990 static void add_subscript_info (dw_die_ref, tree, bool);
5991 static void add_byte_size_attribute (dw_die_ref, tree);
5992 static void add_bit_offset_attribute (dw_die_ref, tree);
5993 static void add_bit_size_attribute (dw_die_ref, tree);
5994 static void add_prototyped_attribute (dw_die_ref, tree);
5995 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5996 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5997 static void add_src_coords_attributes (dw_die_ref, tree);
5998 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5999 static void push_decl_scope (tree);
6000 static void pop_decl_scope (void);
6001 static dw_die_ref scope_die_for (tree, dw_die_ref);
6002 static inline int local_scope_p (dw_die_ref);
6003 static inline int class_scope_p (dw_die_ref);
6004 static inline int class_or_namespace_scope_p (dw_die_ref);
6005 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6006 static void add_calling_convention_attribute (dw_die_ref, tree);
6007 static const char *type_tag (const_tree);
6008 static tree member_declared_type (const_tree);
6009 #if 0
6010 static const char *decl_start_label (tree);
6011 #endif
6012 static void gen_array_type_die (tree, dw_die_ref);
6013 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6014 #if 0
6015 static void gen_entry_point_die (tree, dw_die_ref);
6016 #endif
6017 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6018 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6019 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6020 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6021 static void gen_formal_types_die (tree, dw_die_ref);
6022 static void gen_subprogram_die (tree, dw_die_ref);
6023 static void gen_variable_die (tree, tree, dw_die_ref);
6024 static void gen_const_die (tree, dw_die_ref);
6025 static void gen_label_die (tree, dw_die_ref);
6026 static void gen_lexical_block_die (tree, dw_die_ref, int);
6027 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6028 static void gen_field_die (tree, dw_die_ref);
6029 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6030 static dw_die_ref gen_compile_unit_die (const char *);
6031 static void gen_inheritance_die (tree, tree, dw_die_ref);
6032 static void gen_member_die (tree, dw_die_ref);
6033 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6034                                                 enum debug_info_usage);
6035 static void gen_subroutine_type_die (tree, dw_die_ref);
6036 static void gen_typedef_die (tree, dw_die_ref);
6037 static void gen_type_die (tree, dw_die_ref);
6038 static void gen_block_die (tree, dw_die_ref, int);
6039 static void decls_for_scope (tree, dw_die_ref, int);
6040 static int is_redundant_typedef (const_tree);
6041 static inline dw_die_ref get_context_die (tree);
6042 static void gen_namespace_die (tree, dw_die_ref);
6043 static void gen_decl_die (tree, tree, dw_die_ref);
6044 static dw_die_ref force_decl_die (tree);
6045 static dw_die_ref force_type_die (tree);
6046 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6047 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6048 static struct dwarf_file_data * lookup_filename (const char *);
6049 static void retry_incomplete_types (void);
6050 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6051 static void gen_generic_params_dies (tree);
6052 static void splice_child_die (dw_die_ref, dw_die_ref);
6053 static int file_info_cmp (const void *, const void *);
6054 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6055                                      const char *, const char *, unsigned);
6056 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
6057                                        const char *, const char *,
6058                                        const char *);
6059 static void output_loc_list (dw_loc_list_ref);
6060 static char *gen_internal_sym (const char *);
6061
6062 static void prune_unmark_dies (dw_die_ref);
6063 static void prune_unused_types_mark (dw_die_ref, int);
6064 static void prune_unused_types_walk (dw_die_ref);
6065 static void prune_unused_types_walk_attribs (dw_die_ref);
6066 static void prune_unused_types_prune (dw_die_ref);
6067 static void prune_unused_types (void);
6068 static int maybe_emit_file (struct dwarf_file_data *fd);
6069 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6070 static void gen_remaining_tmpl_value_param_die_attribute (void);
6071
6072 /* Section names used to hold DWARF debugging information.  */
6073 #ifndef DEBUG_INFO_SECTION
6074 #define DEBUG_INFO_SECTION      ".debug_info"
6075 #endif
6076 #ifndef DEBUG_ABBREV_SECTION
6077 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6078 #endif
6079 #ifndef DEBUG_ARANGES_SECTION
6080 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6081 #endif
6082 #ifndef DEBUG_MACINFO_SECTION
6083 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6084 #endif
6085 #ifndef DEBUG_LINE_SECTION
6086 #define DEBUG_LINE_SECTION      ".debug_line"
6087 #endif
6088 #ifndef DEBUG_LOC_SECTION
6089 #define DEBUG_LOC_SECTION       ".debug_loc"
6090 #endif
6091 #ifndef DEBUG_PUBNAMES_SECTION
6092 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6093 #endif
6094 #ifndef DEBUG_PUBTYPES_SECTION
6095 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6096 #endif
6097 #ifndef DEBUG_STR_SECTION
6098 #define DEBUG_STR_SECTION       ".debug_str"
6099 #endif
6100 #ifndef DEBUG_RANGES_SECTION
6101 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6102 #endif
6103
6104 /* Standard ELF section names for compiled code and data.  */
6105 #ifndef TEXT_SECTION_NAME
6106 #define TEXT_SECTION_NAME       ".text"
6107 #endif
6108
6109 /* Section flags for .debug_str section.  */
6110 #define DEBUG_STR_SECTION_FLAGS \
6111   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6112    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6113    : SECTION_DEBUG)
6114
6115 /* Labels we insert at beginning sections we can reference instead of
6116    the section names themselves.  */
6117
6118 #ifndef TEXT_SECTION_LABEL
6119 #define TEXT_SECTION_LABEL              "Ltext"
6120 #endif
6121 #ifndef COLD_TEXT_SECTION_LABEL
6122 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6123 #endif
6124 #ifndef DEBUG_LINE_SECTION_LABEL
6125 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6126 #endif
6127 #ifndef DEBUG_INFO_SECTION_LABEL
6128 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6129 #endif
6130 #ifndef DEBUG_ABBREV_SECTION_LABEL
6131 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6132 #endif
6133 #ifndef DEBUG_LOC_SECTION_LABEL
6134 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6135 #endif
6136 #ifndef DEBUG_RANGES_SECTION_LABEL
6137 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6138 #endif
6139 #ifndef DEBUG_MACINFO_SECTION_LABEL
6140 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6141 #endif
6142
6143 /* Definitions of defaults for formats and names of various special
6144    (artificial) labels which may be generated within this file (when the -g
6145    options is used and DWARF2_DEBUGGING_INFO is in effect.
6146    If necessary, these may be overridden from within the tm.h file, but
6147    typically, overriding these defaults is unnecessary.  */
6148
6149 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6150 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6151 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6152 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6153 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6154 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6155 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6156 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6157 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6158 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6159
6160 #ifndef TEXT_END_LABEL
6161 #define TEXT_END_LABEL          "Letext"
6162 #endif
6163 #ifndef COLD_END_LABEL
6164 #define COLD_END_LABEL          "Letext_cold"
6165 #endif
6166 #ifndef BLOCK_BEGIN_LABEL
6167 #define BLOCK_BEGIN_LABEL       "LBB"
6168 #endif
6169 #ifndef BLOCK_END_LABEL
6170 #define BLOCK_END_LABEL         "LBE"
6171 #endif
6172 #ifndef LINE_CODE_LABEL
6173 #define LINE_CODE_LABEL         "LM"
6174 #endif
6175 #ifndef SEPARATE_LINE_CODE_LABEL
6176 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6177 #endif
6178
6179 \f
6180 /* We allow a language front-end to designate a function that is to be
6181    called to "demangle" any name before it is put into a DIE.  */
6182
6183 static const char *(*demangle_name_func) (const char *);
6184
6185 void
6186 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6187 {
6188   demangle_name_func = func;
6189 }
6190
6191 /* Test if rtl node points to a pseudo register.  */
6192
6193 static inline int
6194 is_pseudo_reg (const_rtx rtl)
6195 {
6196   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6197           || (GET_CODE (rtl) == SUBREG
6198               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6199 }
6200
6201 /* Return a reference to a type, with its const and volatile qualifiers
6202    removed.  */
6203
6204 static inline tree
6205 type_main_variant (tree type)
6206 {
6207   type = TYPE_MAIN_VARIANT (type);
6208
6209   /* ??? There really should be only one main variant among any group of
6210      variants of a given type (and all of the MAIN_VARIANT values for all
6211      members of the group should point to that one type) but sometimes the C
6212      front-end messes this up for array types, so we work around that bug
6213      here.  */
6214   if (TREE_CODE (type) == ARRAY_TYPE)
6215     while (type != TYPE_MAIN_VARIANT (type))
6216       type = TYPE_MAIN_VARIANT (type);
6217
6218   return type;
6219 }
6220
6221 /* Return nonzero if the given type node represents a tagged type.  */
6222
6223 static inline int
6224 is_tagged_type (const_tree type)
6225 {
6226   enum tree_code code = TREE_CODE (type);
6227
6228   return (code == RECORD_TYPE || code == UNION_TYPE
6229           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6230 }
6231
6232 /* Convert a DIE tag into its string name.  */
6233
6234 static const char *
6235 dwarf_tag_name (unsigned int tag)
6236 {
6237   switch (tag)
6238     {
6239     case DW_TAG_padding:
6240       return "DW_TAG_padding";
6241     case DW_TAG_array_type:
6242       return "DW_TAG_array_type";
6243     case DW_TAG_class_type:
6244       return "DW_TAG_class_type";
6245     case DW_TAG_entry_point:
6246       return "DW_TAG_entry_point";
6247     case DW_TAG_enumeration_type:
6248       return "DW_TAG_enumeration_type";
6249     case DW_TAG_formal_parameter:
6250       return "DW_TAG_formal_parameter";
6251     case DW_TAG_imported_declaration:
6252       return "DW_TAG_imported_declaration";
6253     case DW_TAG_label:
6254       return "DW_TAG_label";
6255     case DW_TAG_lexical_block:
6256       return "DW_TAG_lexical_block";
6257     case DW_TAG_member:
6258       return "DW_TAG_member";
6259     case DW_TAG_pointer_type:
6260       return "DW_TAG_pointer_type";
6261     case DW_TAG_reference_type:
6262       return "DW_TAG_reference_type";
6263     case DW_TAG_compile_unit:
6264       return "DW_TAG_compile_unit";
6265     case DW_TAG_string_type:
6266       return "DW_TAG_string_type";
6267     case DW_TAG_structure_type:
6268       return "DW_TAG_structure_type";
6269     case DW_TAG_subroutine_type:
6270       return "DW_TAG_subroutine_type";
6271     case DW_TAG_typedef:
6272       return "DW_TAG_typedef";
6273     case DW_TAG_union_type:
6274       return "DW_TAG_union_type";
6275     case DW_TAG_unspecified_parameters:
6276       return "DW_TAG_unspecified_parameters";
6277     case DW_TAG_variant:
6278       return "DW_TAG_variant";
6279     case DW_TAG_common_block:
6280       return "DW_TAG_common_block";
6281     case DW_TAG_common_inclusion:
6282       return "DW_TAG_common_inclusion";
6283     case DW_TAG_inheritance:
6284       return "DW_TAG_inheritance";
6285     case DW_TAG_inlined_subroutine:
6286       return "DW_TAG_inlined_subroutine";
6287     case DW_TAG_module:
6288       return "DW_TAG_module";
6289     case DW_TAG_ptr_to_member_type:
6290       return "DW_TAG_ptr_to_member_type";
6291     case DW_TAG_set_type:
6292       return "DW_TAG_set_type";
6293     case DW_TAG_subrange_type:
6294       return "DW_TAG_subrange_type";
6295     case DW_TAG_with_stmt:
6296       return "DW_TAG_with_stmt";
6297     case DW_TAG_access_declaration:
6298       return "DW_TAG_access_declaration";
6299     case DW_TAG_base_type:
6300       return "DW_TAG_base_type";
6301     case DW_TAG_catch_block:
6302       return "DW_TAG_catch_block";
6303     case DW_TAG_const_type:
6304       return "DW_TAG_const_type";
6305     case DW_TAG_constant:
6306       return "DW_TAG_constant";
6307     case DW_TAG_enumerator:
6308       return "DW_TAG_enumerator";
6309     case DW_TAG_file_type:
6310       return "DW_TAG_file_type";
6311     case DW_TAG_friend:
6312       return "DW_TAG_friend";
6313     case DW_TAG_namelist:
6314       return "DW_TAG_namelist";
6315     case DW_TAG_namelist_item:
6316       return "DW_TAG_namelist_item";
6317     case DW_TAG_packed_type:
6318       return "DW_TAG_packed_type";
6319     case DW_TAG_subprogram:
6320       return "DW_TAG_subprogram";
6321     case DW_TAG_template_type_param:
6322       return "DW_TAG_template_type_param";
6323     case DW_TAG_template_value_param:
6324       return "DW_TAG_template_value_param";
6325     case DW_TAG_thrown_type:
6326       return "DW_TAG_thrown_type";
6327     case DW_TAG_try_block:
6328       return "DW_TAG_try_block";
6329     case DW_TAG_variant_part:
6330       return "DW_TAG_variant_part";
6331     case DW_TAG_variable:
6332       return "DW_TAG_variable";
6333     case DW_TAG_volatile_type:
6334       return "DW_TAG_volatile_type";
6335     case DW_TAG_dwarf_procedure:
6336       return "DW_TAG_dwarf_procedure";
6337     case DW_TAG_restrict_type:
6338       return "DW_TAG_restrict_type";
6339     case DW_TAG_interface_type:
6340       return "DW_TAG_interface_type";
6341     case DW_TAG_namespace:
6342       return "DW_TAG_namespace";
6343     case DW_TAG_imported_module:
6344       return "DW_TAG_imported_module";
6345     case DW_TAG_unspecified_type:
6346       return "DW_TAG_unspecified_type";
6347     case DW_TAG_partial_unit:
6348       return "DW_TAG_partial_unit";
6349     case DW_TAG_imported_unit:
6350       return "DW_TAG_imported_unit";
6351     case DW_TAG_condition:
6352       return "DW_TAG_condition";
6353     case DW_TAG_shared_type:
6354       return "DW_TAG_shared_type";
6355     case DW_TAG_GNU_template_parameter_pack:
6356       return "DW_TAG_GNU_template_parameter_pack";
6357     case DW_TAG_GNU_formal_parameter_pack:
6358       return "DW_TAG_GNU_formal_parameter_pack";
6359     case DW_TAG_MIPS_loop:
6360       return "DW_TAG_MIPS_loop";
6361     case DW_TAG_format_label:
6362       return "DW_TAG_format_label";
6363     case DW_TAG_function_template:
6364       return "DW_TAG_function_template";
6365     case DW_TAG_class_template:
6366       return "DW_TAG_class_template";
6367     case DW_TAG_GNU_BINCL:
6368       return "DW_TAG_GNU_BINCL";
6369     case DW_TAG_GNU_EINCL:
6370       return "DW_TAG_GNU_EINCL";
6371     case DW_TAG_GNU_template_template_param:
6372       return "DW_TAG_GNU_template_template_param";
6373     default:
6374       return "DW_TAG_<unknown>";
6375     }
6376 }
6377
6378 /* Convert a DWARF attribute code into its string name.  */
6379
6380 static const char *
6381 dwarf_attr_name (unsigned int attr)
6382 {
6383   switch (attr)
6384     {
6385     case DW_AT_sibling:
6386       return "DW_AT_sibling";
6387     case DW_AT_location:
6388       return "DW_AT_location";
6389     case DW_AT_name:
6390       return "DW_AT_name";
6391     case DW_AT_ordering:
6392       return "DW_AT_ordering";
6393     case DW_AT_subscr_data:
6394       return "DW_AT_subscr_data";
6395     case DW_AT_byte_size:
6396       return "DW_AT_byte_size";
6397     case DW_AT_bit_offset:
6398       return "DW_AT_bit_offset";
6399     case DW_AT_bit_size:
6400       return "DW_AT_bit_size";
6401     case DW_AT_element_list:
6402       return "DW_AT_element_list";
6403     case DW_AT_stmt_list:
6404       return "DW_AT_stmt_list";
6405     case DW_AT_low_pc:
6406       return "DW_AT_low_pc";
6407     case DW_AT_high_pc:
6408       return "DW_AT_high_pc";
6409     case DW_AT_language:
6410       return "DW_AT_language";
6411     case DW_AT_member:
6412       return "DW_AT_member";
6413     case DW_AT_discr:
6414       return "DW_AT_discr";
6415     case DW_AT_discr_value:
6416       return "DW_AT_discr_value";
6417     case DW_AT_visibility:
6418       return "DW_AT_visibility";
6419     case DW_AT_import:
6420       return "DW_AT_import";
6421     case DW_AT_string_length:
6422       return "DW_AT_string_length";
6423     case DW_AT_common_reference:
6424       return "DW_AT_common_reference";
6425     case DW_AT_comp_dir:
6426       return "DW_AT_comp_dir";
6427     case DW_AT_const_value:
6428       return "DW_AT_const_value";
6429     case DW_AT_containing_type:
6430       return "DW_AT_containing_type";
6431     case DW_AT_default_value:
6432       return "DW_AT_default_value";
6433     case DW_AT_inline:
6434       return "DW_AT_inline";
6435     case DW_AT_is_optional:
6436       return "DW_AT_is_optional";
6437     case DW_AT_lower_bound:
6438       return "DW_AT_lower_bound";
6439     case DW_AT_producer:
6440       return "DW_AT_producer";
6441     case DW_AT_prototyped:
6442       return "DW_AT_prototyped";
6443     case DW_AT_return_addr:
6444       return "DW_AT_return_addr";
6445     case DW_AT_start_scope:
6446       return "DW_AT_start_scope";
6447     case DW_AT_bit_stride:
6448       return "DW_AT_bit_stride";
6449     case DW_AT_upper_bound:
6450       return "DW_AT_upper_bound";
6451     case DW_AT_abstract_origin:
6452       return "DW_AT_abstract_origin";
6453     case DW_AT_accessibility:
6454       return "DW_AT_accessibility";
6455     case DW_AT_address_class:
6456       return "DW_AT_address_class";
6457     case DW_AT_artificial:
6458       return "DW_AT_artificial";
6459     case DW_AT_base_types:
6460       return "DW_AT_base_types";
6461     case DW_AT_calling_convention:
6462       return "DW_AT_calling_convention";
6463     case DW_AT_count:
6464       return "DW_AT_count";
6465     case DW_AT_data_member_location:
6466       return "DW_AT_data_member_location";
6467     case DW_AT_decl_column:
6468       return "DW_AT_decl_column";
6469     case DW_AT_decl_file:
6470       return "DW_AT_decl_file";
6471     case DW_AT_decl_line:
6472       return "DW_AT_decl_line";
6473     case DW_AT_declaration:
6474       return "DW_AT_declaration";
6475     case DW_AT_discr_list:
6476       return "DW_AT_discr_list";
6477     case DW_AT_encoding:
6478       return "DW_AT_encoding";
6479     case DW_AT_external:
6480       return "DW_AT_external";
6481     case DW_AT_explicit:
6482       return "DW_AT_explicit";
6483     case DW_AT_frame_base:
6484       return "DW_AT_frame_base";
6485     case DW_AT_friend:
6486       return "DW_AT_friend";
6487     case DW_AT_identifier_case:
6488       return "DW_AT_identifier_case";
6489     case DW_AT_macro_info:
6490       return "DW_AT_macro_info";
6491     case DW_AT_namelist_items:
6492       return "DW_AT_namelist_items";
6493     case DW_AT_priority:
6494       return "DW_AT_priority";
6495     case DW_AT_segment:
6496       return "DW_AT_segment";
6497     case DW_AT_specification:
6498       return "DW_AT_specification";
6499     case DW_AT_static_link:
6500       return "DW_AT_static_link";
6501     case DW_AT_type:
6502       return "DW_AT_type";
6503     case DW_AT_use_location:
6504       return "DW_AT_use_location";
6505     case DW_AT_variable_parameter:
6506       return "DW_AT_variable_parameter";
6507     case DW_AT_virtuality:
6508       return "DW_AT_virtuality";
6509     case DW_AT_vtable_elem_location:
6510       return "DW_AT_vtable_elem_location";
6511
6512     case DW_AT_allocated:
6513       return "DW_AT_allocated";
6514     case DW_AT_associated:
6515       return "DW_AT_associated";
6516     case DW_AT_data_location:
6517       return "DW_AT_data_location";
6518     case DW_AT_byte_stride:
6519       return "DW_AT_byte_stride";
6520     case DW_AT_entry_pc:
6521       return "DW_AT_entry_pc";
6522     case DW_AT_use_UTF8:
6523       return "DW_AT_use_UTF8";
6524     case DW_AT_extension:
6525       return "DW_AT_extension";
6526     case DW_AT_ranges:
6527       return "DW_AT_ranges";
6528     case DW_AT_trampoline:
6529       return "DW_AT_trampoline";
6530     case DW_AT_call_column:
6531       return "DW_AT_call_column";
6532     case DW_AT_call_file:
6533       return "DW_AT_call_file";
6534     case DW_AT_call_line:
6535       return "DW_AT_call_line";
6536
6537     case DW_AT_MIPS_fde:
6538       return "DW_AT_MIPS_fde";
6539     case DW_AT_MIPS_loop_begin:
6540       return "DW_AT_MIPS_loop_begin";
6541     case DW_AT_MIPS_tail_loop_begin:
6542       return "DW_AT_MIPS_tail_loop_begin";
6543     case DW_AT_MIPS_epilog_begin:
6544       return "DW_AT_MIPS_epilog_begin";
6545     case DW_AT_MIPS_loop_unroll_factor:
6546       return "DW_AT_MIPS_loop_unroll_factor";
6547     case DW_AT_MIPS_software_pipeline_depth:
6548       return "DW_AT_MIPS_software_pipeline_depth";
6549     case DW_AT_MIPS_linkage_name:
6550       return "DW_AT_MIPS_linkage_name";
6551     case DW_AT_MIPS_stride:
6552       return "DW_AT_MIPS_stride";
6553     case DW_AT_MIPS_abstract_name:
6554       return "DW_AT_MIPS_abstract_name";
6555     case DW_AT_MIPS_clone_origin:
6556       return "DW_AT_MIPS_clone_origin";
6557     case DW_AT_MIPS_has_inlines:
6558       return "DW_AT_MIPS_has_inlines";
6559
6560     case DW_AT_sf_names:
6561       return "DW_AT_sf_names";
6562     case DW_AT_src_info:
6563       return "DW_AT_src_info";
6564     case DW_AT_mac_info:
6565       return "DW_AT_mac_info";
6566     case DW_AT_src_coords:
6567       return "DW_AT_src_coords";
6568     case DW_AT_body_begin:
6569       return "DW_AT_body_begin";
6570     case DW_AT_body_end:
6571       return "DW_AT_body_end";
6572     case DW_AT_GNU_vector:
6573       return "DW_AT_GNU_vector";
6574     case DW_AT_GNU_template_name:
6575       return "DW_AT_GNU_template_name";
6576
6577     case DW_AT_VMS_rtnbeg_pd_address:
6578       return "DW_AT_VMS_rtnbeg_pd_address";
6579
6580     default:
6581       return "DW_AT_<unknown>";
6582     }
6583 }
6584
6585 /* Convert a DWARF value form code into its string name.  */
6586
6587 static const char *
6588 dwarf_form_name (unsigned int form)
6589 {
6590   switch (form)
6591     {
6592     case DW_FORM_addr:
6593       return "DW_FORM_addr";
6594     case DW_FORM_block2:
6595       return "DW_FORM_block2";
6596     case DW_FORM_block4:
6597       return "DW_FORM_block4";
6598     case DW_FORM_data2:
6599       return "DW_FORM_data2";
6600     case DW_FORM_data4:
6601       return "DW_FORM_data4";
6602     case DW_FORM_data8:
6603       return "DW_FORM_data8";
6604     case DW_FORM_string:
6605       return "DW_FORM_string";
6606     case DW_FORM_block:
6607       return "DW_FORM_block";
6608     case DW_FORM_block1:
6609       return "DW_FORM_block1";
6610     case DW_FORM_data1:
6611       return "DW_FORM_data1";
6612     case DW_FORM_flag:
6613       return "DW_FORM_flag";
6614     case DW_FORM_sdata:
6615       return "DW_FORM_sdata";
6616     case DW_FORM_strp:
6617       return "DW_FORM_strp";
6618     case DW_FORM_udata:
6619       return "DW_FORM_udata";
6620     case DW_FORM_ref_addr:
6621       return "DW_FORM_ref_addr";
6622     case DW_FORM_ref1:
6623       return "DW_FORM_ref1";
6624     case DW_FORM_ref2:
6625       return "DW_FORM_ref2";
6626     case DW_FORM_ref4:
6627       return "DW_FORM_ref4";
6628     case DW_FORM_ref8:
6629       return "DW_FORM_ref8";
6630     case DW_FORM_ref_udata:
6631       return "DW_FORM_ref_udata";
6632     case DW_FORM_indirect:
6633       return "DW_FORM_indirect";
6634     default:
6635       return "DW_FORM_<unknown>";
6636     }
6637 }
6638 \f
6639 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6640    instance of an inlined instance of a decl which is local to an inline
6641    function, so we have to trace all of the way back through the origin chain
6642    to find out what sort of node actually served as the original seed for the
6643    given block.  */
6644
6645 static tree
6646 decl_ultimate_origin (const_tree decl)
6647 {
6648   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6649     return NULL_TREE;
6650
6651   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6652      nodes in the function to point to themselves; ignore that if
6653      we're trying to output the abstract instance of this function.  */
6654   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6655     return NULL_TREE;
6656
6657   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6658      most distant ancestor, this should never happen.  */
6659   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6660
6661   return DECL_ABSTRACT_ORIGIN (decl);
6662 }
6663
6664 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6665    of a virtual function may refer to a base class, so we check the 'this'
6666    parameter.  */
6667
6668 static tree
6669 decl_class_context (tree decl)
6670 {
6671   tree context = NULL_TREE;
6672
6673   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6674     context = DECL_CONTEXT (decl);
6675   else
6676     context = TYPE_MAIN_VARIANT
6677       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6678
6679   if (context && !TYPE_P (context))
6680     context = NULL_TREE;
6681
6682   return context;
6683 }
6684 \f
6685 /* Add an attribute/value pair to a DIE.  */
6686
6687 static inline void
6688 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6689 {
6690   /* Maybe this should be an assert?  */
6691   if (die == NULL)
6692     return;
6693
6694   if (die->die_attr == NULL)
6695     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6696   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6697 }
6698
6699 static inline enum dw_val_class
6700 AT_class (dw_attr_ref a)
6701 {
6702   return a->dw_attr_val.val_class;
6703 }
6704
6705 /* Add a flag value attribute to a DIE.  */
6706
6707 static inline void
6708 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6709 {
6710   dw_attr_node attr;
6711
6712   attr.dw_attr = attr_kind;
6713   attr.dw_attr_val.val_class = dw_val_class_flag;
6714   attr.dw_attr_val.v.val_flag = flag;
6715   add_dwarf_attr (die, &attr);
6716 }
6717
6718 static inline unsigned
6719 AT_flag (dw_attr_ref a)
6720 {
6721   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6722   return a->dw_attr_val.v.val_flag;
6723 }
6724
6725 /* Add a signed integer attribute value to a DIE.  */
6726
6727 static inline void
6728 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6729 {
6730   dw_attr_node attr;
6731
6732   attr.dw_attr = attr_kind;
6733   attr.dw_attr_val.val_class = dw_val_class_const;
6734   attr.dw_attr_val.v.val_int = int_val;
6735   add_dwarf_attr (die, &attr);
6736 }
6737
6738 static inline HOST_WIDE_INT
6739 AT_int (dw_attr_ref a)
6740 {
6741   gcc_assert (a && AT_class (a) == dw_val_class_const);
6742   return a->dw_attr_val.v.val_int;
6743 }
6744
6745 /* Add an unsigned integer attribute value to a DIE.  */
6746
6747 static inline void
6748 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6749                  unsigned HOST_WIDE_INT unsigned_val)
6750 {
6751   dw_attr_node attr;
6752
6753   attr.dw_attr = attr_kind;
6754   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6755   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6756   add_dwarf_attr (die, &attr);
6757 }
6758
6759 static inline unsigned HOST_WIDE_INT
6760 AT_unsigned (dw_attr_ref a)
6761 {
6762   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6763   return a->dw_attr_val.v.val_unsigned;
6764 }
6765
6766 /* Add an unsigned double integer attribute value to a DIE.  */
6767
6768 static inline void
6769 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
6770                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
6771 {
6772   dw_attr_node attr;
6773
6774   attr.dw_attr = attr_kind;
6775   attr.dw_attr_val.val_class = dw_val_class_const_double;
6776   attr.dw_attr_val.v.val_double.high = high;
6777   attr.dw_attr_val.v.val_double.low = low;
6778   add_dwarf_attr (die, &attr);
6779 }
6780
6781 /* Add a floating point attribute value to a DIE and return it.  */
6782
6783 static inline void
6784 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6785             unsigned int length, unsigned int elt_size, unsigned char *array)
6786 {
6787   dw_attr_node attr;
6788
6789   attr.dw_attr = attr_kind;
6790   attr.dw_attr_val.val_class = dw_val_class_vec;
6791   attr.dw_attr_val.v.val_vec.length = length;
6792   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6793   attr.dw_attr_val.v.val_vec.array = array;
6794   add_dwarf_attr (die, &attr);
6795 }
6796
6797 /* Hash and equality functions for debug_str_hash.  */
6798
6799 static hashval_t
6800 debug_str_do_hash (const void *x)
6801 {
6802   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6803 }
6804
6805 static int
6806 debug_str_eq (const void *x1, const void *x2)
6807 {
6808   return strcmp ((((const struct indirect_string_node *)x1)->str),
6809                  (const char *)x2) == 0;
6810 }
6811
6812 /* Add STR to the indirect string hash table.  */
6813
6814 static struct indirect_string_node *
6815 find_AT_string (const char *str)
6816 {
6817   struct indirect_string_node *node;
6818   void **slot;
6819
6820   if (! debug_str_hash)
6821     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6822                                       debug_str_eq, NULL);
6823
6824   slot = htab_find_slot_with_hash (debug_str_hash, str,
6825                                    htab_hash_string (str), INSERT);
6826   if (*slot == NULL)
6827     {
6828       node = (struct indirect_string_node *)
6829                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6830       node->str = ggc_strdup (str);
6831       *slot = node;
6832     }
6833   else
6834     node = (struct indirect_string_node *) *slot;
6835
6836   node->refcount++;
6837   return node;
6838 }
6839
6840 /* Add a string attribute value to a DIE.  */
6841
6842 static inline void
6843 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6844 {
6845   dw_attr_node attr;
6846   struct indirect_string_node *node;
6847
6848   node = find_AT_string (str);
6849
6850   attr.dw_attr = attr_kind;
6851   attr.dw_attr_val.val_class = dw_val_class_str;
6852   attr.dw_attr_val.v.val_str = node;
6853   add_dwarf_attr (die, &attr);
6854 }
6855
6856 /* Create a label for an indirect string node, ensuring it is going to
6857    be output, unless its reference count goes down to zero.  */
6858
6859 static inline void
6860 gen_label_for_indirect_string (struct indirect_string_node *node)
6861 {
6862   char label[32];
6863
6864   if (node->label)
6865     return;
6866
6867   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6868   ++dw2_string_counter;
6869   node->label = xstrdup (label);
6870 }
6871
6872 /* Create a SYMBOL_REF rtx whose value is the initial address of a
6873    debug string STR.  */
6874
6875 static inline rtx
6876 get_debug_string_label (const char *str)
6877 {
6878   struct indirect_string_node *node = find_AT_string (str);
6879
6880   debug_str_hash_forced = true;
6881
6882   gen_label_for_indirect_string (node);
6883
6884   return gen_rtx_SYMBOL_REF (Pmode, node->label);
6885 }
6886
6887 static inline const char *
6888 AT_string (dw_attr_ref a)
6889 {
6890   gcc_assert (a && AT_class (a) == dw_val_class_str);
6891   return a->dw_attr_val.v.val_str->str;
6892 }
6893
6894 /* Find out whether a string should be output inline in DIE
6895    or out-of-line in .debug_str section.  */
6896
6897 static enum dwarf_form
6898 AT_string_form (dw_attr_ref a)
6899 {
6900   struct indirect_string_node *node;
6901   unsigned int len;
6902
6903   gcc_assert (a && AT_class (a) == dw_val_class_str);
6904
6905   node = a->dw_attr_val.v.val_str;
6906   if (node->form)
6907     return node->form;
6908
6909   len = strlen (node->str) + 1;
6910
6911   /* If the string is shorter or equal to the size of the reference, it is
6912      always better to put it inline.  */
6913   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6914     return node->form = DW_FORM_string;
6915
6916   /* If we cannot expect the linker to merge strings in .debug_str
6917      section, only put it into .debug_str if it is worth even in this
6918      single module.  */
6919   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
6920       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
6921       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
6922     return node->form = DW_FORM_string;
6923
6924   gen_label_for_indirect_string (node);
6925
6926   return node->form = DW_FORM_strp;
6927 }
6928
6929 /* Add a DIE reference attribute value to a DIE.  */
6930
6931 static inline void
6932 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6933 {
6934   dw_attr_node attr;
6935
6936   attr.dw_attr = attr_kind;
6937   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6938   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6939   attr.dw_attr_val.v.val_die_ref.external = 0;
6940   add_dwarf_attr (die, &attr);
6941 }
6942
6943 /* Add an AT_specification attribute to a DIE, and also make the back
6944    pointer from the specification to the definition.  */
6945
6946 static inline void
6947 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6948 {
6949   add_AT_die_ref (die, DW_AT_specification, targ_die);
6950   gcc_assert (!targ_die->die_definition);
6951   targ_die->die_definition = die;
6952 }
6953
6954 static inline dw_die_ref
6955 AT_ref (dw_attr_ref a)
6956 {
6957   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6958   return a->dw_attr_val.v.val_die_ref.die;
6959 }
6960
6961 static inline int
6962 AT_ref_external (dw_attr_ref a)
6963 {
6964   if (a && AT_class (a) == dw_val_class_die_ref)
6965     return a->dw_attr_val.v.val_die_ref.external;
6966
6967   return 0;
6968 }
6969
6970 static inline void
6971 set_AT_ref_external (dw_attr_ref a, int i)
6972 {
6973   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6974   a->dw_attr_val.v.val_die_ref.external = i;
6975 }
6976
6977 /* Add an FDE reference attribute value to a DIE.  */
6978
6979 static inline void
6980 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6981 {
6982   dw_attr_node attr;
6983
6984   attr.dw_attr = attr_kind;
6985   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6986   attr.dw_attr_val.v.val_fde_index = targ_fde;
6987   add_dwarf_attr (die, &attr);
6988 }
6989
6990 /* Add a location description attribute value to a DIE.  */
6991
6992 static inline void
6993 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6994 {
6995   dw_attr_node attr;
6996
6997   attr.dw_attr = attr_kind;
6998   attr.dw_attr_val.val_class = dw_val_class_loc;
6999   attr.dw_attr_val.v.val_loc = loc;
7000   add_dwarf_attr (die, &attr);
7001 }
7002
7003 static inline dw_loc_descr_ref
7004 AT_loc (dw_attr_ref a)
7005 {
7006   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7007   return a->dw_attr_val.v.val_loc;
7008 }
7009
7010 static inline void
7011 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7012 {
7013   dw_attr_node attr;
7014
7015   attr.dw_attr = attr_kind;
7016   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7017   attr.dw_attr_val.v.val_loc_list = loc_list;
7018   add_dwarf_attr (die, &attr);
7019   have_location_lists = true;
7020 }
7021
7022 static inline dw_loc_list_ref
7023 AT_loc_list (dw_attr_ref a)
7024 {
7025   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7026   return a->dw_attr_val.v.val_loc_list;
7027 }
7028
7029 /* Add an address constant attribute value to a DIE.  */
7030
7031 static inline void
7032 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7033 {
7034   dw_attr_node attr;
7035
7036   attr.dw_attr = attr_kind;
7037   attr.dw_attr_val.val_class = dw_val_class_addr;
7038   attr.dw_attr_val.v.val_addr = addr;
7039   add_dwarf_attr (die, &attr);
7040 }
7041
7042 /* Get the RTX from to an address DIE attribute.  */
7043
7044 static inline rtx
7045 AT_addr (dw_attr_ref a)
7046 {
7047   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7048   return a->dw_attr_val.v.val_addr;
7049 }
7050
7051 /* Add a file attribute value to a DIE.  */
7052
7053 static inline void
7054 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7055              struct dwarf_file_data *fd)
7056 {
7057   dw_attr_node attr;
7058
7059   attr.dw_attr = attr_kind;
7060   attr.dw_attr_val.val_class = dw_val_class_file;
7061   attr.dw_attr_val.v.val_file = fd;
7062   add_dwarf_attr (die, &attr);
7063 }
7064
7065 /* Get the dwarf_file_data from a file DIE attribute.  */
7066
7067 static inline struct dwarf_file_data *
7068 AT_file (dw_attr_ref a)
7069 {
7070   gcc_assert (a && AT_class (a) == dw_val_class_file);
7071   return a->dw_attr_val.v.val_file;
7072 }
7073
7074 /* Add a label identifier attribute value to a DIE.  */
7075
7076 static inline void
7077 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7078 {
7079   dw_attr_node attr;
7080
7081   attr.dw_attr = attr_kind;
7082   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7083   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7084   add_dwarf_attr (die, &attr);
7085 }
7086
7087 /* Add a section offset attribute value to a DIE, an offset into the
7088    debug_line section.  */
7089
7090 static inline void
7091 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7092                 const char *label)
7093 {
7094   dw_attr_node attr;
7095
7096   attr.dw_attr = attr_kind;
7097   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7098   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7099   add_dwarf_attr (die, &attr);
7100 }
7101
7102 /* Add a section offset attribute value to a DIE, an offset into the
7103    debug_macinfo section.  */
7104
7105 static inline void
7106 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7107                const char *label)
7108 {
7109   dw_attr_node attr;
7110
7111   attr.dw_attr = attr_kind;
7112   attr.dw_attr_val.val_class = dw_val_class_macptr;
7113   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7114   add_dwarf_attr (die, &attr);
7115 }
7116
7117 /* Add an offset attribute value to a DIE.  */
7118
7119 static inline void
7120 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7121                unsigned HOST_WIDE_INT offset)
7122 {
7123   dw_attr_node attr;
7124
7125   attr.dw_attr = attr_kind;
7126   attr.dw_attr_val.val_class = dw_val_class_offset;
7127   attr.dw_attr_val.v.val_offset = offset;
7128   add_dwarf_attr (die, &attr);
7129 }
7130
7131 /* Add an range_list attribute value to a DIE.  */
7132
7133 static void
7134 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7135                    long unsigned int offset)
7136 {
7137   dw_attr_node attr;
7138
7139   attr.dw_attr = attr_kind;
7140   attr.dw_attr_val.val_class = dw_val_class_range_list;
7141   attr.dw_attr_val.v.val_offset = offset;
7142   add_dwarf_attr (die, &attr);
7143 }
7144
7145 static inline const char *
7146 AT_lbl (dw_attr_ref a)
7147 {
7148   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7149                     || AT_class (a) == dw_val_class_lineptr
7150                     || AT_class (a) == dw_val_class_macptr));
7151   return a->dw_attr_val.v.val_lbl_id;
7152 }
7153
7154 /* Get the attribute of type attr_kind.  */
7155
7156 static dw_attr_ref
7157 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7158 {
7159   dw_attr_ref a;
7160   unsigned ix;
7161   dw_die_ref spec = NULL;
7162
7163   if (! die)
7164     return NULL;
7165
7166   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7167     if (a->dw_attr == attr_kind)
7168       return a;
7169     else if (a->dw_attr == DW_AT_specification
7170              || a->dw_attr == DW_AT_abstract_origin)
7171       spec = AT_ref (a);
7172
7173   if (spec)
7174     return get_AT (spec, attr_kind);
7175
7176   return NULL;
7177 }
7178
7179 /* Return the "low pc" attribute value, typically associated with a subprogram
7180    DIE.  Return null if the "low pc" attribute is either not present, or if it
7181    cannot be represented as an assembler label identifier.  */
7182
7183 static inline const char *
7184 get_AT_low_pc (dw_die_ref die)
7185 {
7186   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7187
7188   return a ? AT_lbl (a) : NULL;
7189 }
7190
7191 /* Return the "high pc" attribute value, typically associated with a subprogram
7192    DIE.  Return null if the "high pc" attribute is either not present, or if it
7193    cannot be represented as an assembler label identifier.  */
7194
7195 static inline const char *
7196 get_AT_hi_pc (dw_die_ref die)
7197 {
7198   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7199
7200   return a ? AT_lbl (a) : NULL;
7201 }
7202
7203 /* Return the value of the string attribute designated by ATTR_KIND, or
7204    NULL if it is not present.  */
7205
7206 static inline const char *
7207 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7208 {
7209   dw_attr_ref a = get_AT (die, attr_kind);
7210
7211   return a ? AT_string (a) : NULL;
7212 }
7213
7214 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7215    if it is not present.  */
7216
7217 static inline int
7218 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7219 {
7220   dw_attr_ref a = get_AT (die, attr_kind);
7221
7222   return a ? AT_flag (a) : 0;
7223 }
7224
7225 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7226    if it is not present.  */
7227
7228 static inline unsigned
7229 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7230 {
7231   dw_attr_ref a = get_AT (die, attr_kind);
7232
7233   return a ? AT_unsigned (a) : 0;
7234 }
7235
7236 static inline dw_die_ref
7237 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7238 {
7239   dw_attr_ref a = get_AT (die, attr_kind);
7240
7241   return a ? AT_ref (a) : NULL;
7242 }
7243
7244 static inline struct dwarf_file_data *
7245 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7246 {
7247   dw_attr_ref a = get_AT (die, attr_kind);
7248
7249   return a ? AT_file (a) : NULL;
7250 }
7251
7252 /* Return TRUE if the language is C or C++.  */
7253
7254 static inline bool
7255 is_c_family (void)
7256 {
7257   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7258
7259   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7260           || lang == DW_LANG_C99
7261           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7262 }
7263
7264 /* Return TRUE if the language is C++.  */
7265
7266 static inline bool
7267 is_cxx (void)
7268 {
7269   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7270
7271   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7272 }
7273
7274 /* Return TRUE if the language is Fortran.  */
7275
7276 static inline bool
7277 is_fortran (void)
7278 {
7279   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7280
7281   return (lang == DW_LANG_Fortran77
7282           || lang == DW_LANG_Fortran90
7283           || lang == DW_LANG_Fortran95);
7284 }
7285
7286 /* Return TRUE if the language is Java.  */
7287
7288 static inline bool
7289 is_java (void)
7290 {
7291   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7292
7293   return lang == DW_LANG_Java;
7294 }
7295
7296 /* Return TRUE if the language is Ada.  */
7297
7298 static inline bool
7299 is_ada (void)
7300 {
7301   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7302
7303   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7304 }
7305
7306 /* Remove the specified attribute if present.  */
7307
7308 static void
7309 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7310 {
7311   dw_attr_ref a;
7312   unsigned ix;
7313
7314   if (! die)
7315     return;
7316
7317   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7318     if (a->dw_attr == attr_kind)
7319       {
7320         if (AT_class (a) == dw_val_class_str)
7321           if (a->dw_attr_val.v.val_str->refcount)
7322             a->dw_attr_val.v.val_str->refcount--;
7323
7324         /* VEC_ordered_remove should help reduce the number of abbrevs
7325            that are needed.  */
7326         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7327         return;
7328       }
7329 }
7330
7331 /* Remove CHILD from its parent.  PREV must have the property that
7332    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7333
7334 static void
7335 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7336 {
7337   gcc_assert (child->die_parent == prev->die_parent);
7338   gcc_assert (prev->die_sib == child);
7339   if (prev == child)
7340     {
7341       gcc_assert (child->die_parent->die_child == child);
7342       prev = NULL;
7343     }
7344   else
7345     prev->die_sib = child->die_sib;
7346   if (child->die_parent->die_child == child)
7347     child->die_parent->die_child = prev;
7348 }
7349
7350 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7351    matches TAG.  */
7352
7353 static void
7354 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7355 {
7356   dw_die_ref c;
7357
7358   c = die->die_child;
7359   if (c) do {
7360     dw_die_ref prev = c;
7361     c = c->die_sib;
7362     while (c->die_tag == tag)
7363       {
7364         remove_child_with_prev (c, prev);
7365         /* Might have removed every child.  */
7366         if (c == c->die_sib)
7367           return;
7368         c = c->die_sib;
7369       }
7370   } while (c != die->die_child);
7371 }
7372
7373 /* Add a CHILD_DIE as the last child of DIE.  */
7374
7375 static void
7376 add_child_die (dw_die_ref die, dw_die_ref child_die)
7377 {
7378   /* FIXME this should probably be an assert.  */
7379   if (! die || ! child_die)
7380     return;
7381   gcc_assert (die != child_die);
7382
7383   child_die->die_parent = die;
7384   if (die->die_child)
7385     {
7386       child_die->die_sib = die->die_child->die_sib;
7387       die->die_child->die_sib = child_die;
7388     }
7389   else
7390     child_die->die_sib = child_die;
7391   die->die_child = child_die;
7392 }
7393
7394 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7395    is the specification, to the end of PARENT's list of children.
7396    This is done by removing and re-adding it.  */
7397
7398 static void
7399 splice_child_die (dw_die_ref parent, dw_die_ref child)
7400 {
7401   dw_die_ref p;
7402
7403   /* We want the declaration DIE from inside the class, not the
7404      specification DIE at toplevel.  */
7405   if (child->die_parent != parent)
7406     {
7407       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7408
7409       if (tmp)
7410         child = tmp;
7411     }
7412
7413   gcc_assert (child->die_parent == parent
7414               || (child->die_parent
7415                   == get_AT_ref (parent, DW_AT_specification)));
7416
7417   for (p = child->die_parent->die_child; ; p = p->die_sib)
7418     if (p->die_sib == child)
7419       {
7420         remove_child_with_prev (child, p);
7421         break;
7422       }
7423
7424   add_child_die (parent, child);
7425 }
7426
7427 /* Return a pointer to a newly created DIE node.  */
7428
7429 static inline dw_die_ref
7430 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7431 {
7432   dw_die_ref die = GGC_CNEW (die_node);
7433
7434   die->die_tag = tag_value;
7435
7436   if (parent_die != NULL)
7437     add_child_die (parent_die, die);
7438   else
7439     {
7440       limbo_die_node *limbo_node;
7441
7442       limbo_node = GGC_CNEW (limbo_die_node);
7443       limbo_node->die = die;
7444       limbo_node->created_for = t;
7445       limbo_node->next = limbo_die_list;
7446       limbo_die_list = limbo_node;
7447     }
7448
7449   return die;
7450 }
7451
7452 /* Return the DIE associated with the given type specifier.  */
7453
7454 static inline dw_die_ref
7455 lookup_type_die (tree type)
7456 {
7457   return TYPE_SYMTAB_DIE (type);
7458 }
7459
7460 /* Equate a DIE to a given type specifier.  */
7461
7462 static inline void
7463 equate_type_number_to_die (tree type, dw_die_ref type_die)
7464 {
7465   TYPE_SYMTAB_DIE (type) = type_die;
7466 }
7467
7468 /* Returns a hash value for X (which really is a die_struct).  */
7469
7470 static hashval_t
7471 decl_die_table_hash (const void *x)
7472 {
7473   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7474 }
7475
7476 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7477
7478 static int
7479 decl_die_table_eq (const void *x, const void *y)
7480 {
7481   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7482 }
7483
7484 /* Return the DIE associated with a given declaration.  */
7485
7486 static inline dw_die_ref
7487 lookup_decl_die (tree decl)
7488 {
7489   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7490 }
7491
7492 /* Returns a hash value for X (which really is a var_loc_list).  */
7493
7494 static hashval_t
7495 decl_loc_table_hash (const void *x)
7496 {
7497   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7498 }
7499
7500 /* Return nonzero if decl_id of var_loc_list X is the same as
7501    UID of decl *Y.  */
7502
7503 static int
7504 decl_loc_table_eq (const void *x, const void *y)
7505 {
7506   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7507 }
7508
7509 /* Return the var_loc list associated with a given declaration.  */
7510
7511 static inline var_loc_list *
7512 lookup_decl_loc (const_tree decl)
7513 {
7514   if (!decl_loc_table)
7515     return NULL;
7516   return (var_loc_list *)
7517     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7518 }
7519
7520 /* Equate a DIE to a particular declaration.  */
7521
7522 static void
7523 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7524 {
7525   unsigned int decl_id = DECL_UID (decl);
7526   void **slot;
7527
7528   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7529   *slot = decl_die;
7530   decl_die->decl_id = decl_id;
7531 }
7532
7533 /* Add a variable location node to the linked list for DECL.  */
7534
7535 static void
7536 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7537 {
7538   unsigned int decl_id = DECL_UID (decl);
7539   var_loc_list *temp;
7540   void **slot;
7541
7542   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7543   if (*slot == NULL)
7544     {
7545       temp = GGC_CNEW (var_loc_list);
7546       temp->decl_id = decl_id;
7547       *slot = temp;
7548     }
7549   else
7550     temp = (var_loc_list *) *slot;
7551
7552   if (temp->last)
7553     {
7554       /* If the current location is the same as the end of the list,
7555          and either both or neither of the locations is uninitialized,
7556          we have nothing to do.  */
7557       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7558                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7559           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7560                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7561               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7562                    == VAR_INIT_STATUS_UNINITIALIZED)
7563                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7564                       == VAR_INIT_STATUS_UNINITIALIZED))))
7565         {
7566           /* Add LOC to the end of list and update LAST.  */
7567           temp->last->next = loc;
7568           temp->last = loc;
7569         }
7570     }
7571   /* Do not add empty location to the beginning of the list.  */
7572   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7573     {
7574       temp->first = loc;
7575       temp->last = loc;
7576     }
7577 }
7578 \f
7579 /* Keep track of the number of spaces used to indent the
7580    output of the debugging routines that print the structure of
7581    the DIE internal representation.  */
7582 static int print_indent;
7583
7584 /* Indent the line the number of spaces given by print_indent.  */
7585
7586 static inline void
7587 print_spaces (FILE *outfile)
7588 {
7589   fprintf (outfile, "%*s", print_indent, "");
7590 }
7591
7592 /* Print the information associated with a given DIE, and its children.
7593    This routine is a debugging aid only.  */
7594
7595 static void
7596 print_die (dw_die_ref die, FILE *outfile)
7597 {
7598   dw_attr_ref a;
7599   dw_die_ref c;
7600   unsigned ix;
7601
7602   print_spaces (outfile);
7603   fprintf (outfile, "DIE %4ld: %s\n",
7604            die->die_offset, dwarf_tag_name (die->die_tag));
7605   print_spaces (outfile);
7606   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7607   fprintf (outfile, " offset: %ld\n", die->die_offset);
7608
7609   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7610     {
7611       print_spaces (outfile);
7612       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7613
7614       switch (AT_class (a))
7615         {
7616         case dw_val_class_addr:
7617           fprintf (outfile, "address");
7618           break;
7619         case dw_val_class_offset:
7620           fprintf (outfile, "offset");
7621           break;
7622         case dw_val_class_loc:
7623           fprintf (outfile, "location descriptor");
7624           break;
7625         case dw_val_class_loc_list:
7626           fprintf (outfile, "location list -> label:%s",
7627                    AT_loc_list (a)->ll_symbol);
7628           break;
7629         case dw_val_class_range_list:
7630           fprintf (outfile, "range list");
7631           break;
7632         case dw_val_class_const:
7633           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7634           break;
7635         case dw_val_class_unsigned_const:
7636           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7637           break;
7638         case dw_val_class_const_double:
7639           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
7640                             HOST_WIDE_INT_PRINT_UNSIGNED")",
7641                    a->dw_attr_val.v.val_double.high,
7642                    a->dw_attr_val.v.val_double.low);
7643           break;
7644         case dw_val_class_vec:
7645           fprintf (outfile, "floating-point or vector constant");
7646           break;
7647         case dw_val_class_flag:
7648           fprintf (outfile, "%u", AT_flag (a));
7649           break;
7650         case dw_val_class_die_ref:
7651           if (AT_ref (a) != NULL)
7652             {
7653               if (AT_ref (a)->die_symbol)
7654                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7655               else
7656                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7657             }
7658           else
7659             fprintf (outfile, "die -> <null>");
7660           break;
7661         case dw_val_class_lbl_id:
7662         case dw_val_class_lineptr:
7663         case dw_val_class_macptr:
7664           fprintf (outfile, "label: %s", AT_lbl (a));
7665           break;
7666         case dw_val_class_str:
7667           if (AT_string (a) != NULL)
7668             fprintf (outfile, "\"%s\"", AT_string (a));
7669           else
7670             fprintf (outfile, "<null>");
7671           break;
7672         case dw_val_class_file:
7673           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7674                    AT_file (a)->emitted_number);
7675           break;
7676         default:
7677           break;
7678         }
7679
7680       fprintf (outfile, "\n");
7681     }
7682
7683   if (die->die_child != NULL)
7684     {
7685       print_indent += 4;
7686       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7687       print_indent -= 4;
7688     }
7689   if (print_indent == 0)
7690     fprintf (outfile, "\n");
7691 }
7692
7693 /* Print the contents of the source code line number correspondence table.
7694    This routine is a debugging aid only.  */
7695
7696 static void
7697 print_dwarf_line_table (FILE *outfile)
7698 {
7699   unsigned i;
7700   dw_line_info_ref line_info;
7701
7702   fprintf (outfile, "\n\nDWARF source line information\n");
7703   for (i = 1; i < line_info_table_in_use; i++)
7704     {
7705       line_info = &line_info_table[i];
7706       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7707                line_info->dw_file_num,
7708                line_info->dw_line_num);
7709     }
7710
7711   fprintf (outfile, "\n\n");
7712 }
7713
7714 /* Print the information collected for a given DIE.  */
7715
7716 void
7717 debug_dwarf_die (dw_die_ref die)
7718 {
7719   print_die (die, stderr);
7720 }
7721
7722 /* Print all DWARF information collected for the compilation unit.
7723    This routine is a debugging aid only.  */
7724
7725 void
7726 debug_dwarf (void)
7727 {
7728   print_indent = 0;
7729   print_die (comp_unit_die, stderr);
7730   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7731     print_dwarf_line_table (stderr);
7732 }
7733 \f
7734 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7735    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7736    DIE that marks the start of the DIEs for this include file.  */
7737
7738 static dw_die_ref
7739 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7740 {
7741   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7742   dw_die_ref new_unit = gen_compile_unit_die (filename);
7743
7744   new_unit->die_sib = old_unit;
7745   return new_unit;
7746 }
7747
7748 /* Close an include-file CU and reopen the enclosing one.  */
7749
7750 static dw_die_ref
7751 pop_compile_unit (dw_die_ref old_unit)
7752 {
7753   dw_die_ref new_unit = old_unit->die_sib;
7754
7755   old_unit->die_sib = NULL;
7756   return new_unit;
7757 }
7758
7759 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7760 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7761
7762 /* Calculate the checksum of a location expression.  */
7763
7764 static inline void
7765 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7766 {
7767   int tem;
7768
7769   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7770   CHECKSUM (tem);
7771   CHECKSUM (loc->dw_loc_oprnd1);
7772   CHECKSUM (loc->dw_loc_oprnd2);
7773 }
7774
7775 /* Calculate the checksum of an attribute.  */
7776
7777 static void
7778 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7779 {
7780   dw_loc_descr_ref loc;
7781   rtx r;
7782
7783   CHECKSUM (at->dw_attr);
7784
7785   /* We don't care that this was compiled with a different compiler
7786      snapshot; if the output is the same, that's what matters.  */
7787   if (at->dw_attr == DW_AT_producer)
7788     return;
7789
7790   switch (AT_class (at))
7791     {
7792     case dw_val_class_const:
7793       CHECKSUM (at->dw_attr_val.v.val_int);
7794       break;
7795     case dw_val_class_unsigned_const:
7796       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7797       break;
7798     case dw_val_class_const_double:
7799       CHECKSUM (at->dw_attr_val.v.val_double);
7800       break;
7801     case dw_val_class_vec:
7802       CHECKSUM (at->dw_attr_val.v.val_vec);
7803       break;
7804     case dw_val_class_flag:
7805       CHECKSUM (at->dw_attr_val.v.val_flag);
7806       break;
7807     case dw_val_class_str:
7808       CHECKSUM_STRING (AT_string (at));
7809       break;
7810
7811     case dw_val_class_addr:
7812       r = AT_addr (at);
7813       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7814       CHECKSUM_STRING (XSTR (r, 0));
7815       break;
7816
7817     case dw_val_class_offset:
7818       CHECKSUM (at->dw_attr_val.v.val_offset);
7819       break;
7820
7821     case dw_val_class_loc:
7822       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7823         loc_checksum (loc, ctx);
7824       break;
7825
7826     case dw_val_class_die_ref:
7827       die_checksum (AT_ref (at), ctx, mark);
7828       break;
7829
7830     case dw_val_class_fde_ref:
7831     case dw_val_class_lbl_id:
7832     case dw_val_class_lineptr:
7833     case dw_val_class_macptr:
7834       break;
7835
7836     case dw_val_class_file:
7837       CHECKSUM_STRING (AT_file (at)->filename);
7838       break;
7839
7840     default:
7841       break;
7842     }
7843 }
7844
7845 /* Calculate the checksum of a DIE.  */
7846
7847 static void
7848 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7849 {
7850   dw_die_ref c;
7851   dw_attr_ref a;
7852   unsigned ix;
7853
7854   /* To avoid infinite recursion.  */
7855   if (die->die_mark)
7856     {
7857       CHECKSUM (die->die_mark);
7858       return;
7859     }
7860   die->die_mark = ++(*mark);
7861
7862   CHECKSUM (die->die_tag);
7863
7864   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7865     attr_checksum (a, ctx, mark);
7866
7867   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7868 }
7869
7870 #undef CHECKSUM
7871 #undef CHECKSUM_STRING
7872
7873 /* Do the location expressions look same?  */
7874 static inline int
7875 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7876 {
7877   return loc1->dw_loc_opc == loc2->dw_loc_opc
7878          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7879          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7880 }
7881
7882 /* Do the values look the same?  */
7883 static int
7884 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7885 {
7886   dw_loc_descr_ref loc1, loc2;
7887   rtx r1, r2;
7888
7889   if (v1->val_class != v2->val_class)
7890     return 0;
7891
7892   switch (v1->val_class)
7893     {
7894     case dw_val_class_const:
7895       return v1->v.val_int == v2->v.val_int;
7896     case dw_val_class_unsigned_const:
7897       return v1->v.val_unsigned == v2->v.val_unsigned;
7898     case dw_val_class_const_double:
7899       return v1->v.val_double.high == v2->v.val_double.high
7900              && v1->v.val_double.low == v2->v.val_double.low;
7901     case dw_val_class_vec:
7902       if (v1->v.val_vec.length != v2->v.val_vec.length
7903           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7904         return 0;
7905       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7906                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7907         return 0;
7908       return 1;
7909     case dw_val_class_flag:
7910       return v1->v.val_flag == v2->v.val_flag;
7911     case dw_val_class_str:
7912       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7913
7914     case dw_val_class_addr:
7915       r1 = v1->v.val_addr;
7916       r2 = v2->v.val_addr;
7917       if (GET_CODE (r1) != GET_CODE (r2))
7918         return 0;
7919       return !rtx_equal_p (r1, r2);
7920
7921     case dw_val_class_offset:
7922       return v1->v.val_offset == v2->v.val_offset;
7923
7924     case dw_val_class_loc:
7925       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7926            loc1 && loc2;
7927            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7928         if (!same_loc_p (loc1, loc2, mark))
7929           return 0;
7930       return !loc1 && !loc2;
7931
7932     case dw_val_class_die_ref:
7933       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7934
7935     case dw_val_class_fde_ref:
7936     case dw_val_class_lbl_id:
7937     case dw_val_class_lineptr:
7938     case dw_val_class_macptr:
7939       return 1;
7940
7941     case dw_val_class_file:
7942       return v1->v.val_file == v2->v.val_file;
7943
7944     default:
7945       return 1;
7946     }
7947 }
7948
7949 /* Do the attributes look the same?  */
7950
7951 static int
7952 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7953 {
7954   if (at1->dw_attr != at2->dw_attr)
7955     return 0;
7956
7957   /* We don't care that this was compiled with a different compiler
7958      snapshot; if the output is the same, that's what matters. */
7959   if (at1->dw_attr == DW_AT_producer)
7960     return 1;
7961
7962   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7963 }
7964
7965 /* Do the dies look the same?  */
7966
7967 static int
7968 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7969 {
7970   dw_die_ref c1, c2;
7971   dw_attr_ref a1;
7972   unsigned ix;
7973
7974   /* To avoid infinite recursion.  */
7975   if (die1->die_mark)
7976     return die1->die_mark == die2->die_mark;
7977   die1->die_mark = die2->die_mark = ++(*mark);
7978
7979   if (die1->die_tag != die2->die_tag)
7980     return 0;
7981
7982   if (VEC_length (dw_attr_node, die1->die_attr)
7983       != VEC_length (dw_attr_node, die2->die_attr))
7984     return 0;
7985
7986   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7987     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7988       return 0;
7989
7990   c1 = die1->die_child;
7991   c2 = die2->die_child;
7992   if (! c1)
7993     {
7994       if (c2)
7995         return 0;
7996     }
7997   else
7998     for (;;)
7999       {
8000         if (!same_die_p (c1, c2, mark))
8001           return 0;
8002         c1 = c1->die_sib;
8003         c2 = c2->die_sib;
8004         if (c1 == die1->die_child)
8005           {
8006             if (c2 == die2->die_child)
8007               break;
8008             else
8009               return 0;
8010           }
8011     }
8012
8013   return 1;
8014 }
8015
8016 /* Do the dies look the same?  Wrapper around same_die_p.  */
8017
8018 static int
8019 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
8020 {
8021   int mark = 0;
8022   int ret = same_die_p (die1, die2, &mark);
8023
8024   unmark_all_dies (die1);
8025   unmark_all_dies (die2);
8026
8027   return ret;
8028 }
8029
8030 /* The prefix to attach to symbols on DIEs in the current comdat debug
8031    info section.  */
8032 static char *comdat_symbol_id;
8033
8034 /* The index of the current symbol within the current comdat CU.  */
8035 static unsigned int comdat_symbol_number;
8036
8037 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8038    children, and set comdat_symbol_id accordingly.  */
8039
8040 static void
8041 compute_section_prefix (dw_die_ref unit_die)
8042 {
8043   const char *die_name = get_AT_string (unit_die, DW_AT_name);
8044   const char *base = die_name ? lbasename (die_name) : "anonymous";
8045   char *name = XALLOCAVEC (char, strlen (base) + 64);
8046   char *p;
8047   int i, mark;
8048   unsigned char checksum[16];
8049   struct md5_ctx ctx;
8050
8051   /* Compute the checksum of the DIE, then append part of it as hex digits to
8052      the name filename of the unit.  */
8053
8054   md5_init_ctx (&ctx);
8055   mark = 0;
8056   die_checksum (unit_die, &ctx, &mark);
8057   unmark_all_dies (unit_die);
8058   md5_finish_ctx (&ctx, checksum);
8059
8060   sprintf (name, "%s.", base);
8061   clean_symbol_name (name);
8062
8063   p = name + strlen (name);
8064   for (i = 0; i < 4; i++)
8065     {
8066       sprintf (p, "%.2x", checksum[i]);
8067       p += 2;
8068     }
8069
8070   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
8071   comdat_symbol_number = 0;
8072 }
8073
8074 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
8075
8076 static int
8077 is_type_die (dw_die_ref die)
8078 {
8079   switch (die->die_tag)
8080     {
8081     case DW_TAG_array_type:
8082     case DW_TAG_class_type:
8083     case DW_TAG_interface_type:
8084     case DW_TAG_enumeration_type:
8085     case DW_TAG_pointer_type:
8086     case DW_TAG_reference_type:
8087     case DW_TAG_string_type:
8088     case DW_TAG_structure_type:
8089     case DW_TAG_subroutine_type:
8090     case DW_TAG_union_type:
8091     case DW_TAG_ptr_to_member_type:
8092     case DW_TAG_set_type:
8093     case DW_TAG_subrange_type:
8094     case DW_TAG_base_type:
8095     case DW_TAG_const_type:
8096     case DW_TAG_file_type:
8097     case DW_TAG_packed_type:
8098     case DW_TAG_volatile_type:
8099     case DW_TAG_typedef:
8100       return 1;
8101     default:
8102       return 0;
8103     }
8104 }
8105
8106 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
8107    Basically, we want to choose the bits that are likely to be shared between
8108    compilations (types) and leave out the bits that are specific to individual
8109    compilations (functions).  */
8110
8111 static int
8112 is_comdat_die (dw_die_ref c)
8113 {
8114   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
8115      we do for stabs.  The advantage is a greater likelihood of sharing between
8116      objects that don't include headers in the same order (and therefore would
8117      put the base types in a different comdat).  jason 8/28/00 */
8118
8119   if (c->die_tag == DW_TAG_base_type)
8120     return 0;
8121
8122   if (c->die_tag == DW_TAG_pointer_type
8123       || c->die_tag == DW_TAG_reference_type
8124       || c->die_tag == DW_TAG_const_type
8125       || c->die_tag == DW_TAG_volatile_type)
8126     {
8127       dw_die_ref t = get_AT_ref (c, DW_AT_type);
8128
8129       return t ? is_comdat_die (t) : 0;
8130     }
8131
8132   return is_type_die (c);
8133 }
8134
8135 /* Returns 1 iff C is the sort of DIE that might be referred to from another
8136    compilation unit.  */
8137
8138 static int
8139 is_symbol_die (dw_die_ref c)
8140 {
8141   return (is_type_die (c)
8142           || (get_AT (c, DW_AT_declaration)
8143               && !get_AT (c, DW_AT_specification))
8144           || c->die_tag == DW_TAG_namespace
8145           || c->die_tag == DW_TAG_module);
8146 }
8147
8148 static char *
8149 gen_internal_sym (const char *prefix)
8150 {
8151   char buf[256];
8152
8153   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8154   return xstrdup (buf);
8155 }
8156
8157 /* Assign symbols to all worthy DIEs under DIE.  */
8158
8159 static void
8160 assign_symbol_names (dw_die_ref die)
8161 {
8162   dw_die_ref c;
8163
8164   if (is_symbol_die (die))
8165     {
8166       if (comdat_symbol_id)
8167         {
8168           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
8169
8170           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
8171                    comdat_symbol_id, comdat_symbol_number++);
8172           die->die_symbol = xstrdup (p);
8173         }
8174       else
8175         die->die_symbol = gen_internal_sym ("LDIE");
8176     }
8177
8178   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
8179 }
8180
8181 struct cu_hash_table_entry
8182 {
8183   dw_die_ref cu;
8184   unsigned min_comdat_num, max_comdat_num;
8185   struct cu_hash_table_entry *next;
8186 };
8187
8188 /* Routines to manipulate hash table of CUs.  */
8189 static hashval_t
8190 htab_cu_hash (const void *of)
8191 {
8192   const struct cu_hash_table_entry *const entry =
8193     (const struct cu_hash_table_entry *) of;
8194
8195   return htab_hash_string (entry->cu->die_symbol);
8196 }
8197
8198 static int
8199 htab_cu_eq (const void *of1, const void *of2)
8200 {
8201   const struct cu_hash_table_entry *const entry1 =
8202     (const struct cu_hash_table_entry *) of1;
8203   const struct die_struct *const entry2 = (const struct die_struct *) of2;
8204
8205   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
8206 }
8207
8208 static void
8209 htab_cu_del (void *what)
8210 {
8211   struct cu_hash_table_entry *next,
8212     *entry = (struct cu_hash_table_entry *) what;
8213
8214   while (entry)
8215     {
8216       next = entry->next;
8217       free (entry);
8218       entry = next;
8219     }
8220 }
8221
8222 /* Check whether we have already seen this CU and set up SYM_NUM
8223    accordingly.  */
8224 static int
8225 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
8226 {
8227   struct cu_hash_table_entry dummy;
8228   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
8229
8230   dummy.max_comdat_num = 0;
8231
8232   slot = (struct cu_hash_table_entry **)
8233     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8234         INSERT);
8235   entry = *slot;
8236
8237   for (; entry; last = entry, entry = entry->next)
8238     {
8239       if (same_die_p_wrap (cu, entry->cu))
8240         break;
8241     }
8242
8243   if (entry)
8244     {
8245       *sym_num = entry->min_comdat_num;
8246       return 1;
8247     }
8248
8249   entry = XCNEW (struct cu_hash_table_entry);
8250   entry->cu = cu;
8251   entry->min_comdat_num = *sym_num = last->max_comdat_num;
8252   entry->next = *slot;
8253   *slot = entry;
8254
8255   return 0;
8256 }
8257
8258 /* Record SYM_NUM to record of CU in HTABLE.  */
8259 static void
8260 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
8261 {
8262   struct cu_hash_table_entry **slot, *entry;
8263
8264   slot = (struct cu_hash_table_entry **)
8265     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8266         NO_INSERT);
8267   entry = *slot;
8268
8269   entry->max_comdat_num = sym_num;
8270 }
8271
8272 /* Traverse the DIE (which is always comp_unit_die), and set up
8273    additional compilation units for each of the include files we see
8274    bracketed by BINCL/EINCL.  */
8275
8276 static void
8277 break_out_includes (dw_die_ref die)
8278 {
8279   dw_die_ref c;
8280   dw_die_ref unit = NULL;
8281   limbo_die_node *node, **pnode;
8282   htab_t cu_hash_table;
8283
8284   c = die->die_child;
8285   if (c) do {
8286     dw_die_ref prev = c;
8287     c = c->die_sib;
8288     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
8289            || (unit && is_comdat_die (c)))
8290       {
8291         dw_die_ref next = c->die_sib;
8292
8293         /* This DIE is for a secondary CU; remove it from the main one.  */
8294         remove_child_with_prev (c, prev);
8295
8296         if (c->die_tag == DW_TAG_GNU_BINCL)
8297           unit = push_new_compile_unit (unit, c);
8298         else if (c->die_tag == DW_TAG_GNU_EINCL)
8299           unit = pop_compile_unit (unit);
8300         else
8301           add_child_die (unit, c);
8302         c = next;
8303         if (c == die->die_child)
8304           break;
8305       }
8306   } while (c != die->die_child);
8307
8308 #if 0
8309   /* We can only use this in debugging, since the frontend doesn't check
8310      to make sure that we leave every include file we enter.  */
8311   gcc_assert (!unit);
8312 #endif
8313
8314   assign_symbol_names (die);
8315   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
8316   for (node = limbo_die_list, pnode = &limbo_die_list;
8317        node;
8318        node = node->next)
8319     {
8320       int is_dupl;
8321
8322       compute_section_prefix (node->die);
8323       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
8324                         &comdat_symbol_number);
8325       assign_symbol_names (node->die);
8326       if (is_dupl)
8327         *pnode = node->next;
8328       else
8329         {
8330           pnode = &node->next;
8331           record_comdat_symbol_number (node->die, cu_hash_table,
8332                 comdat_symbol_number);
8333         }
8334     }
8335   htab_delete (cu_hash_table);
8336 }
8337
8338 /* Traverse the DIE and add a sibling attribute if it may have the
8339    effect of speeding up access to siblings.  To save some space,
8340    avoid generating sibling attributes for DIE's without children.  */
8341
8342 static void
8343 add_sibling_attributes (dw_die_ref die)
8344 {
8345   dw_die_ref c;
8346
8347   if (! die->die_child)
8348     return;
8349
8350   if (die->die_parent && die != die->die_parent->die_child)
8351     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8352
8353   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8354 }
8355
8356 /* Output all location lists for the DIE and its children.  */
8357
8358 static void
8359 output_location_lists (dw_die_ref die)
8360 {
8361   dw_die_ref c;
8362   dw_attr_ref a;
8363   unsigned ix;
8364
8365   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8366     if (AT_class (a) == dw_val_class_loc_list)
8367       output_loc_list (AT_loc_list (a));
8368
8369   FOR_EACH_CHILD (die, c, output_location_lists (c));
8370 }
8371
8372 /* The format of each DIE (and its attribute value pairs) is encoded in an
8373    abbreviation table.  This routine builds the abbreviation table and assigns
8374    a unique abbreviation id for each abbreviation entry.  The children of each
8375    die are visited recursively.  */
8376
8377 static void
8378 build_abbrev_table (dw_die_ref die)
8379 {
8380   unsigned long abbrev_id;
8381   unsigned int n_alloc;
8382   dw_die_ref c;
8383   dw_attr_ref a;
8384   unsigned ix;
8385
8386   /* Scan the DIE references, and mark as external any that refer to
8387      DIEs from other CUs (i.e. those which are not marked).  */
8388   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8389     if (AT_class (a) == dw_val_class_die_ref
8390         && AT_ref (a)->die_mark == 0)
8391       {
8392         gcc_assert (AT_ref (a)->die_symbol);
8393         set_AT_ref_external (a, 1);
8394       }
8395
8396   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8397     {
8398       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8399       dw_attr_ref die_a, abbrev_a;
8400       unsigned ix;
8401       bool ok = true;
8402
8403       if (abbrev->die_tag != die->die_tag)
8404         continue;
8405       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8406         continue;
8407
8408       if (VEC_length (dw_attr_node, abbrev->die_attr)
8409           != VEC_length (dw_attr_node, die->die_attr))
8410         continue;
8411
8412       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
8413         {
8414           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8415           if ((abbrev_a->dw_attr != die_a->dw_attr)
8416               || (value_format (abbrev_a) != value_format (die_a)))
8417             {
8418               ok = false;
8419               break;
8420             }
8421         }
8422       if (ok)
8423         break;
8424     }
8425
8426   if (abbrev_id >= abbrev_die_table_in_use)
8427     {
8428       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8429         {
8430           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8431           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8432                                             n_alloc);
8433
8434           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8435                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8436           abbrev_die_table_allocated = n_alloc;
8437         }
8438
8439       ++abbrev_die_table_in_use;
8440       abbrev_die_table[abbrev_id] = die;
8441     }
8442
8443   die->die_abbrev = abbrev_id;
8444   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8445 }
8446 \f
8447 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8448
8449 static int
8450 constant_size (unsigned HOST_WIDE_INT value)
8451 {
8452   int log;
8453
8454   if (value == 0)
8455     log = 0;
8456   else
8457     log = floor_log2 (value);
8458
8459   log = log / 8;
8460   log = 1 << (floor_log2 (log) + 1);
8461
8462   return log;
8463 }
8464
8465 /* Return the size of a DIE as it is represented in the
8466    .debug_info section.  */
8467
8468 static unsigned long
8469 size_of_die (dw_die_ref die)
8470 {
8471   unsigned long size = 0;
8472   dw_attr_ref a;
8473   unsigned ix;
8474
8475   size += size_of_uleb128 (die->die_abbrev);
8476   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8477     {
8478       switch (AT_class (a))
8479         {
8480         case dw_val_class_addr:
8481           size += DWARF2_ADDR_SIZE;
8482           break;
8483         case dw_val_class_offset:
8484           size += DWARF_OFFSET_SIZE;
8485           break;
8486         case dw_val_class_loc:
8487           {
8488             unsigned long lsize = size_of_locs (AT_loc (a));
8489
8490             /* Block length.  */
8491             size += constant_size (lsize);
8492             size += lsize;
8493           }
8494           break;
8495         case dw_val_class_loc_list:
8496           size += DWARF_OFFSET_SIZE;
8497           break;
8498         case dw_val_class_range_list:
8499           size += DWARF_OFFSET_SIZE;
8500           break;
8501         case dw_val_class_const:
8502           size += size_of_sleb128 (AT_int (a));
8503           break;
8504         case dw_val_class_unsigned_const:
8505           size += constant_size (AT_unsigned (a));
8506           break;
8507         case dw_val_class_const_double:
8508           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8509           if (HOST_BITS_PER_WIDE_INT >= 64)
8510             size++; /* block */
8511           break;
8512         case dw_val_class_vec:
8513           size += constant_size (a->dw_attr_val.v.val_vec.length
8514                                  * a->dw_attr_val.v.val_vec.elt_size)
8515                   + a->dw_attr_val.v.val_vec.length
8516                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8517           break;
8518         case dw_val_class_flag:
8519           size += 1;
8520           break;
8521         case dw_val_class_die_ref:
8522           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8523              whereas in DWARF3 it's always sized as an offset.  */
8524           if (AT_ref_external (a) && dwarf_version == 2)
8525             size += DWARF2_ADDR_SIZE;
8526           else
8527             size += DWARF_OFFSET_SIZE;
8528           break;
8529         case dw_val_class_fde_ref:
8530           size += DWARF_OFFSET_SIZE;
8531           break;
8532         case dw_val_class_lbl_id:
8533           size += DWARF2_ADDR_SIZE;
8534           break;
8535         case dw_val_class_lineptr:
8536         case dw_val_class_macptr:
8537           size += DWARF_OFFSET_SIZE;
8538           break;
8539         case dw_val_class_str:
8540           if (AT_string_form (a) == DW_FORM_strp)
8541             size += DWARF_OFFSET_SIZE;
8542           else
8543             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8544           break;
8545         case dw_val_class_file:
8546           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8547           break;
8548         default:
8549           gcc_unreachable ();
8550         }
8551     }
8552
8553   return size;
8554 }
8555
8556 /* Size the debugging information associated with a given DIE.  Visits the
8557    DIE's children recursively.  Updates the global variable next_die_offset, on
8558    each time through.  Uses the current value of next_die_offset to update the
8559    die_offset field in each DIE.  */
8560
8561 static void
8562 calc_die_sizes (dw_die_ref die)
8563 {
8564   dw_die_ref c;
8565
8566   die->die_offset = next_die_offset;
8567   next_die_offset += size_of_die (die);
8568
8569   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8570
8571   if (die->die_child != NULL)
8572     /* Count the null byte used to terminate sibling lists.  */
8573     next_die_offset += 1;
8574 }
8575
8576 /* Set the marks for a die and its children.  We do this so
8577    that we know whether or not a reference needs to use FORM_ref_addr; only
8578    DIEs in the same CU will be marked.  We used to clear out the offset
8579    and use that as the flag, but ran into ordering problems.  */
8580
8581 static void
8582 mark_dies (dw_die_ref die)
8583 {
8584   dw_die_ref c;
8585
8586   gcc_assert (!die->die_mark);
8587
8588   die->die_mark = 1;
8589   FOR_EACH_CHILD (die, c, mark_dies (c));
8590 }
8591
8592 /* Clear the marks for a die and its children.  */
8593
8594 static void
8595 unmark_dies (dw_die_ref die)
8596 {
8597   dw_die_ref c;
8598
8599   gcc_assert (die->die_mark);
8600
8601   die->die_mark = 0;
8602   FOR_EACH_CHILD (die, c, unmark_dies (c));
8603 }
8604
8605 /* Clear the marks for a die, its children and referred dies.  */
8606
8607 static void
8608 unmark_all_dies (dw_die_ref die)
8609 {
8610   dw_die_ref c;
8611   dw_attr_ref a;
8612   unsigned ix;
8613
8614   if (!die->die_mark)
8615     return;
8616   die->die_mark = 0;
8617
8618   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8619
8620   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8621     if (AT_class (a) == dw_val_class_die_ref)
8622       unmark_all_dies (AT_ref (a));
8623 }
8624
8625 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8626    generated for the compilation unit.  */
8627
8628 static unsigned long
8629 size_of_pubnames (VEC (pubname_entry, gc) * names)
8630 {
8631   unsigned long size;
8632   unsigned i;
8633   pubname_ref p;
8634
8635   size = DWARF_PUBNAMES_HEADER_SIZE;
8636   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8637     if (names != pubtype_table
8638         || p->die->die_offset != 0
8639         || !flag_eliminate_unused_debug_types)
8640       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8641
8642   size += DWARF_OFFSET_SIZE;
8643   return size;
8644 }
8645
8646 /* Return the size of the information in the .debug_aranges section.  */
8647
8648 static unsigned long
8649 size_of_aranges (void)
8650 {
8651   unsigned long size;
8652
8653   size = DWARF_ARANGES_HEADER_SIZE;
8654
8655   /* Count the address/length pair for this compilation unit.  */
8656   if (text_section_used)
8657     size += 2 * DWARF2_ADDR_SIZE;
8658   if (cold_text_section_used)
8659     size += 2 * DWARF2_ADDR_SIZE;
8660   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8661
8662   /* Count the two zero words used to terminated the address range table.  */
8663   size += 2 * DWARF2_ADDR_SIZE;
8664   return size;
8665 }
8666 \f
8667 /* Select the encoding of an attribute value.  */
8668
8669 static enum dwarf_form
8670 value_format (dw_attr_ref a)
8671 {
8672   switch (a->dw_attr_val.val_class)
8673     {
8674     case dw_val_class_addr:
8675       /* Only very few attributes allow DW_FORM_addr.  */
8676       switch (a->dw_attr)
8677         {
8678         case DW_AT_low_pc:
8679         case DW_AT_high_pc:
8680         case DW_AT_entry_pc:
8681         case DW_AT_trampoline:
8682           return DW_FORM_addr;
8683         default:
8684           break;
8685         }
8686       switch (DWARF2_ADDR_SIZE)
8687         {
8688         case 1:
8689           return DW_FORM_data1;
8690         case 2:
8691           return DW_FORM_data2;
8692         case 4:
8693           return DW_FORM_data4;
8694         case 8:
8695           return DW_FORM_data8;
8696         default:
8697           gcc_unreachable ();
8698         }
8699     case dw_val_class_range_list:
8700     case dw_val_class_offset:
8701     case dw_val_class_loc_list:
8702       switch (DWARF_OFFSET_SIZE)
8703         {
8704         case 4:
8705           return DW_FORM_data4;
8706         case 8:
8707           return DW_FORM_data8;
8708         default:
8709           gcc_unreachable ();
8710         }
8711     case dw_val_class_loc:
8712       switch (constant_size (size_of_locs (AT_loc (a))))
8713         {
8714         case 1:
8715           return DW_FORM_block1;
8716         case 2:
8717           return DW_FORM_block2;
8718         default:
8719           gcc_unreachable ();
8720         }
8721     case dw_val_class_const:
8722       return DW_FORM_sdata;
8723     case dw_val_class_unsigned_const:
8724       switch (constant_size (AT_unsigned (a)))
8725         {
8726         case 1:
8727           return DW_FORM_data1;
8728         case 2:
8729           return DW_FORM_data2;
8730         case 4:
8731           return DW_FORM_data4;
8732         case 8:
8733           return DW_FORM_data8;
8734         default:
8735           gcc_unreachable ();
8736         }
8737     case dw_val_class_const_double:
8738       switch (HOST_BITS_PER_WIDE_INT)
8739         {
8740         case 8:
8741           return DW_FORM_data2;
8742         case 16:
8743           return DW_FORM_data4;
8744         case 32:
8745           return DW_FORM_data8;
8746         case 64:
8747         default:
8748           return DW_FORM_block1;
8749         }
8750     case dw_val_class_vec:
8751       switch (constant_size (a->dw_attr_val.v.val_vec.length
8752                              * a->dw_attr_val.v.val_vec.elt_size))
8753         {
8754         case 1:
8755           return DW_FORM_block1;
8756         case 2:
8757           return DW_FORM_block2;
8758         case 4:
8759           return DW_FORM_block4;
8760         default:
8761           gcc_unreachable ();
8762         }
8763     case dw_val_class_flag:
8764       return DW_FORM_flag;
8765     case dw_val_class_die_ref:
8766       if (AT_ref_external (a))
8767         return DW_FORM_ref_addr;
8768       else
8769         return DW_FORM_ref;
8770     case dw_val_class_fde_ref:
8771       return DW_FORM_data;
8772     case dw_val_class_lbl_id:
8773       return DW_FORM_addr;
8774     case dw_val_class_lineptr:
8775     case dw_val_class_macptr:
8776       return DW_FORM_data;
8777     case dw_val_class_str:
8778       return AT_string_form (a);
8779     case dw_val_class_file:
8780       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8781         {
8782         case 1:
8783           return DW_FORM_data1;
8784         case 2:
8785           return DW_FORM_data2;
8786         case 4:
8787           return DW_FORM_data4;
8788         default:
8789           gcc_unreachable ();
8790         }
8791
8792     default:
8793       gcc_unreachable ();
8794     }
8795 }
8796
8797 /* Output the encoding of an attribute value.  */
8798
8799 static void
8800 output_value_format (dw_attr_ref a)
8801 {
8802   enum dwarf_form form = value_format (a);
8803
8804   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8805 }
8806
8807 /* Output the .debug_abbrev section which defines the DIE abbreviation
8808    table.  */
8809
8810 static void
8811 output_abbrev_section (void)
8812 {
8813   unsigned long abbrev_id;
8814
8815   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8816     {
8817       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8818       unsigned ix;
8819       dw_attr_ref a_attr;
8820
8821       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8822       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8823                                    dwarf_tag_name (abbrev->die_tag));
8824
8825       if (abbrev->die_child != NULL)
8826         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8827       else
8828         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8829
8830       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8831            ix++)
8832         {
8833           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8834                                        dwarf_attr_name (a_attr->dw_attr));
8835           output_value_format (a_attr);
8836         }
8837
8838       dw2_asm_output_data (1, 0, NULL);
8839       dw2_asm_output_data (1, 0, NULL);
8840     }
8841
8842   /* Terminate the table.  */
8843   dw2_asm_output_data (1, 0, NULL);
8844 }
8845
8846 /* Output a symbol we can use to refer to this DIE from another CU.  */
8847
8848 static inline void
8849 output_die_symbol (dw_die_ref die)
8850 {
8851   char *sym = die->die_symbol;
8852
8853   if (sym == 0)
8854     return;
8855
8856   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8857     /* We make these global, not weak; if the target doesn't support
8858        .linkonce, it doesn't support combining the sections, so debugging
8859        will break.  */
8860     targetm.asm_out.globalize_label (asm_out_file, sym);
8861
8862   ASM_OUTPUT_LABEL (asm_out_file, sym);
8863 }
8864
8865 /* Return a new location list, given the begin and end range, and the
8866    expression. gensym tells us whether to generate a new internal symbol for
8867    this location list node, which is done for the head of the list only.  */
8868
8869 static inline dw_loc_list_ref
8870 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8871               const char *section, unsigned int gensym)
8872 {
8873   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8874
8875   retlist->begin = begin;
8876   retlist->end = end;
8877   retlist->expr = expr;
8878   retlist->section = section;
8879   if (gensym)
8880     retlist->ll_symbol = gen_internal_sym ("LLST");
8881
8882   return retlist;
8883 }
8884
8885 /* Add a location description expression to a location list.  */
8886
8887 static inline void
8888 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8889                            const char *begin, const char *end,
8890                            const char *section)
8891 {
8892   dw_loc_list_ref *d;
8893
8894   /* Find the end of the chain.  */
8895   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8896     ;
8897
8898   /* Add a new location list node to the list.  */
8899   *d = new_loc_list (descr, begin, end, section, 0);
8900 }
8901
8902 /* Output the location list given to us.  */
8903
8904 static void
8905 output_loc_list (dw_loc_list_ref list_head)
8906 {
8907   dw_loc_list_ref curr = list_head;
8908
8909   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8910
8911   /* Walk the location list, and output each range + expression.  */
8912   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8913     {
8914       unsigned long size;
8915       /* Don't output an entry that starts and ends at the same address.  */
8916       if (strcmp (curr->begin, curr->end) == 0)
8917         continue;
8918       if (!have_multiple_function_sections)
8919         {
8920           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8921                                 "Location list begin address (%s)",
8922                                 list_head->ll_symbol);
8923           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8924                                 "Location list end address (%s)",
8925                                 list_head->ll_symbol);
8926         }
8927       else
8928         {
8929           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8930                                "Location list begin address (%s)",
8931                                list_head->ll_symbol);
8932           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8933                                "Location list end address (%s)",
8934                                list_head->ll_symbol);
8935         }
8936       size = size_of_locs (curr->expr);
8937
8938       /* Output the block length for this list of location operations.  */
8939       gcc_assert (size <= 0xffff);
8940       dw2_asm_output_data (2, size, "%s", "Location expression size");
8941
8942       output_loc_sequence (curr->expr);
8943     }
8944
8945   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8946                        "Location list terminator begin (%s)",
8947                        list_head->ll_symbol);
8948   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8949                        "Location list terminator end (%s)",
8950                        list_head->ll_symbol);
8951 }
8952
8953 /* Output the DIE and its attributes.  Called recursively to generate
8954    the definitions of each child DIE.  */
8955
8956 static void
8957 output_die (dw_die_ref die)
8958 {
8959   dw_attr_ref a;
8960   dw_die_ref c;
8961   unsigned long size;
8962   unsigned ix;
8963
8964   /* If someone in another CU might refer to us, set up a symbol for
8965      them to point to.  */
8966   if (die->die_symbol)
8967     output_die_symbol (die);
8968
8969   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8970                                (unsigned long)die->die_offset,
8971                                dwarf_tag_name (die->die_tag));
8972
8973   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8974     {
8975       const char *name = dwarf_attr_name (a->dw_attr);
8976
8977       switch (AT_class (a))
8978         {
8979         case dw_val_class_addr:
8980           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8981           break;
8982
8983         case dw_val_class_offset:
8984           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8985                                "%s", name);
8986           break;
8987
8988         case dw_val_class_range_list:
8989           {
8990             char *p = strchr (ranges_section_label, '\0');
8991
8992             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8993                      a->dw_attr_val.v.val_offset);
8994             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8995                                    debug_ranges_section, "%s", name);
8996             *p = '\0';
8997           }
8998           break;
8999
9000         case dw_val_class_loc:
9001           size = size_of_locs (AT_loc (a));
9002
9003           /* Output the block length for this list of location operations.  */
9004           dw2_asm_output_data (constant_size (size), size, "%s", name);
9005
9006           output_loc_sequence (AT_loc (a));
9007           break;
9008
9009         case dw_val_class_const:
9010           /* ??? It would be slightly more efficient to use a scheme like is
9011              used for unsigned constants below, but gdb 4.x does not sign
9012              extend.  Gdb 5.x does sign extend.  */
9013           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9014           break;
9015
9016         case dw_val_class_unsigned_const:
9017           dw2_asm_output_data (constant_size (AT_unsigned (a)),
9018                                AT_unsigned (a), "%s", name);
9019           break;
9020
9021         case dw_val_class_const_double:
9022           {
9023             unsigned HOST_WIDE_INT first, second;
9024
9025             if (HOST_BITS_PER_WIDE_INT >= 64)
9026               dw2_asm_output_data (1,
9027                                    2 * HOST_BITS_PER_WIDE_INT
9028                                    / HOST_BITS_PER_CHAR,
9029                                    NULL);
9030
9031             if (WORDS_BIG_ENDIAN)
9032               {
9033                 first = a->dw_attr_val.v.val_double.high;
9034                 second = a->dw_attr_val.v.val_double.low;
9035               }
9036             else
9037               {
9038                 first = a->dw_attr_val.v.val_double.low;
9039                 second = a->dw_attr_val.v.val_double.high;
9040               }
9041
9042             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9043                                  first, name);
9044             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9045                                  second, NULL);
9046           }
9047           break;
9048
9049         case dw_val_class_vec:
9050           {
9051             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9052             unsigned int len = a->dw_attr_val.v.val_vec.length;
9053             unsigned int i;
9054             unsigned char *p;
9055
9056             dw2_asm_output_data (constant_size (len * elt_size),
9057                                  len * elt_size, "%s", name);
9058             if (elt_size > sizeof (HOST_WIDE_INT))
9059               {
9060                 elt_size /= 2;
9061                 len *= 2;
9062               }
9063             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9064                  i < len;
9065                  i++, p += elt_size)
9066               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9067                                    "fp or vector constant word %u", i);
9068             break;
9069           }
9070
9071         case dw_val_class_flag:
9072           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9073           break;
9074
9075         case dw_val_class_loc_list:
9076           {
9077             char *sym = AT_loc_list (a)->ll_symbol;
9078
9079             gcc_assert (sym);
9080             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9081                                    "%s", name);
9082           }
9083           break;
9084
9085         case dw_val_class_die_ref:
9086           if (AT_ref_external (a))
9087             {
9088               char *sym = AT_ref (a)->die_symbol;
9089               int size;
9090
9091               gcc_assert (sym);
9092
9093               /* In DWARF2, DW_FORM_ref_addr is sized by target address
9094                  length, whereas in DWARF3 it's always sized as an offset.  */
9095               if (dwarf_version == 2)
9096                 size = DWARF2_ADDR_SIZE;
9097               else
9098                 size = DWARF_OFFSET_SIZE;
9099               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
9100             }
9101           else
9102             {
9103               gcc_assert (AT_ref (a)->die_offset);
9104               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9105                                    "%s", name);
9106             }
9107           break;
9108
9109         case dw_val_class_fde_ref:
9110           {
9111             char l1[20];
9112
9113             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9114                                          a->dw_attr_val.v.val_fde_index * 2);
9115             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9116                                    "%s", name);
9117           }
9118           break;
9119
9120         case dw_val_class_lbl_id:
9121           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9122           break;
9123
9124         case dw_val_class_lineptr:
9125           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9126                                  debug_line_section, "%s", name);
9127           break;
9128
9129         case dw_val_class_macptr:
9130           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9131                                  debug_macinfo_section, "%s", name);
9132           break;
9133
9134         case dw_val_class_str:
9135           if (AT_string_form (a) == DW_FORM_strp)
9136             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9137                                    a->dw_attr_val.v.val_str->label,
9138                                    debug_str_section,
9139                                    "%s: \"%s\"", name, AT_string (a));
9140           else
9141             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9142           break;
9143
9144         case dw_val_class_file:
9145           {
9146             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9147
9148             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9149                                  a->dw_attr_val.v.val_file->filename);
9150             break;
9151           }
9152
9153         default:
9154           gcc_unreachable ();
9155         }
9156     }
9157
9158   FOR_EACH_CHILD (die, c, output_die (c));
9159
9160   /* Add null byte to terminate sibling list.  */
9161   if (die->die_child != NULL)
9162     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
9163                          (unsigned long) die->die_offset);
9164 }
9165
9166 /* Output the compilation unit that appears at the beginning of the
9167    .debug_info section, and precedes the DIE descriptions.  */
9168
9169 static void
9170 output_compilation_unit_header (void)
9171 {
9172   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9173     dw2_asm_output_data (4, 0xffffffff,
9174       "Initial length escape value indicating 64-bit DWARF extension");
9175   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9176                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9177                        "Length of Compilation Unit Info");
9178   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9179   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9180                          debug_abbrev_section,
9181                          "Offset Into Abbrev. Section");
9182   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9183 }
9184
9185 /* Output the compilation unit DIE and its children.  */
9186
9187 static void
9188 output_comp_unit (dw_die_ref die, int output_if_empty)
9189 {
9190   const char *secname;
9191   char *oldsym, *tmp;
9192
9193   /* Unless we are outputting main CU, we may throw away empty ones.  */
9194   if (!output_if_empty && die->die_child == NULL)
9195     return;
9196
9197   /* Even if there are no children of this DIE, we must output the information
9198      about the compilation unit.  Otherwise, on an empty translation unit, we
9199      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9200      will then complain when examining the file.  First mark all the DIEs in
9201      this CU so we know which get local refs.  */
9202   mark_dies (die);
9203
9204   build_abbrev_table (die);
9205
9206   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9207   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9208   calc_die_sizes (die);
9209
9210   oldsym = die->die_symbol;
9211   if (oldsym)
9212     {
9213       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9214
9215       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9216       secname = tmp;
9217       die->die_symbol = NULL;
9218       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9219     }
9220   else
9221     switch_to_section (debug_info_section);
9222
9223   /* Output debugging information.  */
9224   output_compilation_unit_header ();
9225   output_die (die);
9226
9227   /* Leave the marks on the main CU, so we can check them in
9228      output_pubnames.  */
9229   if (oldsym)
9230     {
9231       unmark_dies (die);
9232       die->die_symbol = oldsym;
9233     }
9234 }
9235
9236 /* Return the DWARF2/3 pubname associated with a decl.  */
9237
9238 static const char *
9239 dwarf2_name (tree decl, int scope)
9240 {
9241   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9242 }
9243
9244 /* Add a new entry to .debug_pubnames if appropriate.  */
9245
9246 static void
9247 add_pubname_string (const char *str, dw_die_ref die)
9248 {
9249   pubname_entry e;
9250
9251   e.die = die;
9252   e.name = xstrdup (str);
9253   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9254 }
9255
9256 static void
9257 add_pubname (tree decl, dw_die_ref die)
9258 {
9259   if (TREE_PUBLIC (decl))
9260     add_pubname_string (dwarf2_name (decl, 1), die);
9261 }
9262
9263 /* Add a new entry to .debug_pubtypes if appropriate.  */
9264
9265 static void
9266 add_pubtype (tree decl, dw_die_ref die)
9267 {
9268   pubname_entry e;
9269
9270   e.name = NULL;
9271   if ((TREE_PUBLIC (decl)
9272        || die->die_parent == comp_unit_die)
9273       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9274     {
9275       e.die = die;
9276       if (TYPE_P (decl))
9277         {
9278           if (TYPE_NAME (decl))
9279             {
9280               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9281                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9282               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9283                        && DECL_NAME (TYPE_NAME (decl)))
9284                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9285               else
9286                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9287             }
9288         }
9289       else
9290         e.name = xstrdup (dwarf2_name (decl, 1));
9291
9292       /* If we don't have a name for the type, there's no point in adding
9293          it to the table.  */
9294       if (e.name && e.name[0] != '\0')
9295         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9296     }
9297 }
9298
9299 /* Output the public names table used to speed up access to externally
9300    visible names; or the public types table used to find type definitions.  */
9301
9302 static void
9303 output_pubnames (VEC (pubname_entry, gc) * names)
9304 {
9305   unsigned i;
9306   unsigned long pubnames_length = size_of_pubnames (names);
9307   pubname_ref pub;
9308
9309   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9310     dw2_asm_output_data (4, 0xffffffff,
9311       "Initial length escape value indicating 64-bit DWARF extension");
9312   if (names == pubname_table)
9313     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9314                          "Length of Public Names Info");
9315   else
9316     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9317                          "Length of Public Type Names Info");
9318   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
9319   dw2_asm_output_data (2, 2, "DWARF Version");
9320   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9321                          debug_info_section,
9322                          "Offset of Compilation Unit Info");
9323   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9324                        "Compilation Unit Length");
9325
9326   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
9327     {
9328       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9329       if (names == pubname_table)
9330         gcc_assert (pub->die->die_mark);
9331
9332       if (names != pubtype_table
9333           || pub->die->die_offset != 0
9334           || !flag_eliminate_unused_debug_types)
9335         {
9336           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9337                                "DIE offset");
9338
9339           dw2_asm_output_nstring (pub->name, -1, "external name");
9340         }
9341     }
9342
9343   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9344 }
9345
9346 /* Add a new entry to .debug_aranges if appropriate.  */
9347
9348 static void
9349 add_arange (tree decl, dw_die_ref die)
9350 {
9351   if (! DECL_SECTION_NAME (decl))
9352     return;
9353
9354   if (arange_table_in_use == arange_table_allocated)
9355     {
9356       arange_table_allocated += ARANGE_TABLE_INCREMENT;
9357       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
9358                                     arange_table_allocated);
9359       memset (arange_table + arange_table_in_use, 0,
9360               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
9361     }
9362
9363   arange_table[arange_table_in_use++] = die;
9364 }
9365
9366 /* Output the information that goes into the .debug_aranges table.
9367    Namely, define the beginning and ending address range of the
9368    text section generated for this compilation unit.  */
9369
9370 static void
9371 output_aranges (void)
9372 {
9373   unsigned i;
9374   unsigned long aranges_length = size_of_aranges ();
9375
9376   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9377     dw2_asm_output_data (4, 0xffffffff,
9378       "Initial length escape value indicating 64-bit DWARF extension");
9379   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9380                        "Length of Address Ranges Info");
9381   /* Version number for aranges is still 2, even in DWARF3.  */
9382   dw2_asm_output_data (2, 2, "DWARF Version");
9383   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9384                          debug_info_section,
9385                          "Offset of Compilation Unit Info");
9386   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9387   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9388
9389   /* We need to align to twice the pointer size here.  */
9390   if (DWARF_ARANGES_PAD_SIZE)
9391     {
9392       /* Pad using a 2 byte words so that padding is correct for any
9393          pointer size.  */
9394       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9395                            2 * DWARF2_ADDR_SIZE);
9396       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9397         dw2_asm_output_data (2, 0, NULL);
9398     }
9399
9400   /* It is necessary not to output these entries if the sections were
9401      not used; if the sections were not used, the length will be 0 and
9402      the address may end up as 0 if the section is discarded by ld
9403      --gc-sections, leaving an invalid (0, 0) entry that can be
9404      confused with the terminator.  */
9405   if (text_section_used)
9406     {
9407       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9408       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9409                             text_section_label, "Length");
9410     }
9411   if (cold_text_section_used)
9412     {
9413       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9414                            "Address");
9415       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9416                             cold_text_section_label, "Length");
9417     }
9418
9419   for (i = 0; i < arange_table_in_use; i++)
9420     {
9421       dw_die_ref die = arange_table[i];
9422
9423       /* We shouldn't see aranges for DIEs outside of the main CU.  */
9424       gcc_assert (die->die_mark);
9425
9426       if (die->die_tag == DW_TAG_subprogram)
9427         {
9428           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
9429                                "Address");
9430           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
9431                                 get_AT_low_pc (die), "Length");
9432         }
9433       else
9434         {
9435           /* A static variable; extract the symbol from DW_AT_location.
9436              Note that this code isn't currently hit, as we only emit
9437              aranges for functions (jason 9/23/99).  */
9438           dw_attr_ref a = get_AT (die, DW_AT_location);
9439           dw_loc_descr_ref loc;
9440
9441           gcc_assert (a && AT_class (a) == dw_val_class_loc);
9442
9443           loc = AT_loc (a);
9444           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
9445
9446           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
9447                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
9448           dw2_asm_output_data (DWARF2_ADDR_SIZE,
9449                                get_AT_unsigned (die, DW_AT_byte_size),
9450                                "Length");
9451         }
9452     }
9453
9454   /* Output the terminator words.  */
9455   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9456   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9457 }
9458
9459 /* Add a new entry to .debug_ranges.  Return the offset at which it
9460    was placed.  */
9461
9462 static unsigned int
9463 add_ranges_num (int num)
9464 {
9465   unsigned int in_use = ranges_table_in_use;
9466
9467   if (in_use == ranges_table_allocated)
9468     {
9469       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9470       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9471                                     ranges_table_allocated);
9472       memset (ranges_table + ranges_table_in_use, 0,
9473               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9474     }
9475
9476   ranges_table[in_use].num = num;
9477   ranges_table_in_use = in_use + 1;
9478
9479   return in_use * 2 * DWARF2_ADDR_SIZE;
9480 }
9481
9482 /* Add a new entry to .debug_ranges corresponding to a block, or a
9483    range terminator if BLOCK is NULL.  */
9484
9485 static unsigned int
9486 add_ranges (const_tree block)
9487 {
9488   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9489 }
9490
9491 /* Add a new entry to .debug_ranges corresponding to a pair of
9492    labels.  */
9493
9494 static unsigned int
9495 add_ranges_by_labels (const char *begin, const char *end)
9496 {
9497   unsigned int in_use = ranges_by_label_in_use;
9498
9499   if (in_use == ranges_by_label_allocated)
9500     {
9501       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9502       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9503                                        ranges_by_label,
9504                                        ranges_by_label_allocated);
9505       memset (ranges_by_label + ranges_by_label_in_use, 0,
9506               RANGES_TABLE_INCREMENT
9507               * sizeof (struct dw_ranges_by_label_struct));
9508     }
9509
9510   ranges_by_label[in_use].begin = begin;
9511   ranges_by_label[in_use].end = end;
9512   ranges_by_label_in_use = in_use + 1;
9513
9514   return add_ranges_num (-(int)in_use - 1);
9515 }
9516
9517 static void
9518 output_ranges (void)
9519 {
9520   unsigned i;
9521   static const char *const start_fmt = "Offset 0x%x";
9522   const char *fmt = start_fmt;
9523
9524   for (i = 0; i < ranges_table_in_use; i++)
9525     {
9526       int block_num = ranges_table[i].num;
9527
9528       if (block_num > 0)
9529         {
9530           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9531           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9532
9533           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9534           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9535
9536           /* If all code is in the text section, then the compilation
9537              unit base address defaults to DW_AT_low_pc, which is the
9538              base of the text section.  */
9539           if (!have_multiple_function_sections)
9540             {
9541               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9542                                     text_section_label,
9543                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9544               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9545                                     text_section_label, NULL);
9546             }
9547
9548           /* Otherwise, the compilation unit base address is zero,
9549              which allows us to use absolute addresses, and not worry
9550              about whether the target supports cross-section
9551              arithmetic.  */
9552           else
9553             {
9554               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9555                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9556               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9557             }
9558
9559           fmt = NULL;
9560         }
9561
9562       /* Negative block_num stands for an index into ranges_by_label.  */
9563       else if (block_num < 0)
9564         {
9565           int lab_idx = - block_num - 1;
9566
9567           if (!have_multiple_function_sections)
9568             {
9569               gcc_unreachable ();
9570 #if 0
9571               /* If we ever use add_ranges_by_labels () for a single
9572                  function section, all we have to do is to take out
9573                  the #if 0 above.  */
9574               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9575                                     ranges_by_label[lab_idx].begin,
9576                                     text_section_label,
9577                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9578               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9579                                     ranges_by_label[lab_idx].end,
9580                                     text_section_label, NULL);
9581 #endif
9582             }
9583           else
9584             {
9585               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9586                                    ranges_by_label[lab_idx].begin,
9587                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9588               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9589                                    ranges_by_label[lab_idx].end,
9590                                    NULL);
9591             }
9592         }
9593       else
9594         {
9595           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9596           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9597           fmt = start_fmt;
9598         }
9599     }
9600 }
9601
9602 /* Data structure containing information about input files.  */
9603 struct file_info
9604 {
9605   const char *path;     /* Complete file name.  */
9606   const char *fname;    /* File name part.  */
9607   int length;           /* Length of entire string.  */
9608   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9609   int dir_idx;          /* Index in directory table.  */
9610 };
9611
9612 /* Data structure containing information about directories with source
9613    files.  */
9614 struct dir_info
9615 {
9616   const char *path;     /* Path including directory name.  */
9617   int length;           /* Path length.  */
9618   int prefix;           /* Index of directory entry which is a prefix.  */
9619   int count;            /* Number of files in this directory.  */
9620   int dir_idx;          /* Index of directory used as base.  */
9621 };
9622
9623 /* Callback function for file_info comparison.  We sort by looking at
9624    the directories in the path.  */
9625
9626 static int
9627 file_info_cmp (const void *p1, const void *p2)
9628 {
9629   const struct file_info *const s1 = (const struct file_info *) p1;
9630   const struct file_info *const s2 = (const struct file_info *) p2;
9631   const unsigned char *cp1;
9632   const unsigned char *cp2;
9633
9634   /* Take care of file names without directories.  We need to make sure that
9635      we return consistent values to qsort since some will get confused if
9636      we return the same value when identical operands are passed in opposite
9637      orders.  So if neither has a directory, return 0 and otherwise return
9638      1 or -1 depending on which one has the directory.  */
9639   if ((s1->path == s1->fname || s2->path == s2->fname))
9640     return (s2->path == s2->fname) - (s1->path == s1->fname);
9641
9642   cp1 = (const unsigned char *) s1->path;
9643   cp2 = (const unsigned char *) s2->path;
9644
9645   while (1)
9646     {
9647       ++cp1;
9648       ++cp2;
9649       /* Reached the end of the first path?  If so, handle like above.  */
9650       if ((cp1 == (const unsigned char *) s1->fname)
9651           || (cp2 == (const unsigned char *) s2->fname))
9652         return ((cp2 == (const unsigned char *) s2->fname)
9653                 - (cp1 == (const unsigned char *) s1->fname));
9654
9655       /* Character of current path component the same?  */
9656       else if (*cp1 != *cp2)
9657         return *cp1 - *cp2;
9658     }
9659 }
9660
9661 struct file_name_acquire_data
9662 {
9663   struct file_info *files;
9664   int used_files;
9665   int max_files;
9666 };
9667
9668 /* Traversal function for the hash table.  */
9669
9670 static int
9671 file_name_acquire (void ** slot, void *data)
9672 {
9673   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9674   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9675   struct file_info *fi;
9676   const char *f;
9677
9678   gcc_assert (fnad->max_files >= d->emitted_number);
9679
9680   if (! d->emitted_number)
9681     return 1;
9682
9683   gcc_assert (fnad->max_files != fnad->used_files);
9684
9685   fi = fnad->files + fnad->used_files++;
9686
9687   /* Skip all leading "./".  */
9688   f = d->filename;
9689   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9690     f += 2;
9691
9692   /* Create a new array entry.  */
9693   fi->path = f;
9694   fi->length = strlen (f);
9695   fi->file_idx = d;
9696
9697   /* Search for the file name part.  */
9698   f = strrchr (f, DIR_SEPARATOR);
9699 #if defined (DIR_SEPARATOR_2)
9700   {
9701     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9702
9703     if (g != NULL)
9704       {
9705         if (f == NULL || f < g)
9706           f = g;
9707       }
9708   }
9709 #endif
9710
9711   fi->fname = f == NULL ? fi->path : f + 1;
9712   return 1;
9713 }
9714
9715 /* Output the directory table and the file name table.  We try to minimize
9716    the total amount of memory needed.  A heuristic is used to avoid large
9717    slowdowns with many input files.  */
9718
9719 static void
9720 output_file_names (void)
9721 {
9722   struct file_name_acquire_data fnad;
9723   int numfiles;
9724   struct file_info *files;
9725   struct dir_info *dirs;
9726   int *saved;
9727   int *savehere;
9728   int *backmap;
9729   int ndirs;
9730   int idx_offset;
9731   int i;
9732   int idx;
9733
9734   if (!last_emitted_file)
9735     {
9736       dw2_asm_output_data (1, 0, "End directory table");
9737       dw2_asm_output_data (1, 0, "End file name table");
9738       return;
9739     }
9740
9741   numfiles = last_emitted_file->emitted_number;
9742
9743   /* Allocate the various arrays we need.  */
9744   files = XALLOCAVEC (struct file_info, numfiles);
9745   dirs = XALLOCAVEC (struct dir_info, numfiles);
9746
9747   fnad.files = files;
9748   fnad.used_files = 0;
9749   fnad.max_files = numfiles;
9750   htab_traverse (file_table, file_name_acquire, &fnad);
9751   gcc_assert (fnad.used_files == fnad.max_files);
9752
9753   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9754
9755   /* Find all the different directories used.  */
9756   dirs[0].path = files[0].path;
9757   dirs[0].length = files[0].fname - files[0].path;
9758   dirs[0].prefix = -1;
9759   dirs[0].count = 1;
9760   dirs[0].dir_idx = 0;
9761   files[0].dir_idx = 0;
9762   ndirs = 1;
9763
9764   for (i = 1; i < numfiles; i++)
9765     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9766         && memcmp (dirs[ndirs - 1].path, files[i].path,
9767                    dirs[ndirs - 1].length) == 0)
9768       {
9769         /* Same directory as last entry.  */
9770         files[i].dir_idx = ndirs - 1;
9771         ++dirs[ndirs - 1].count;
9772       }
9773     else
9774       {
9775         int j;
9776
9777         /* This is a new directory.  */
9778         dirs[ndirs].path = files[i].path;
9779         dirs[ndirs].length = files[i].fname - files[i].path;
9780         dirs[ndirs].count = 1;
9781         dirs[ndirs].dir_idx = ndirs;
9782         files[i].dir_idx = ndirs;
9783
9784         /* Search for a prefix.  */
9785         dirs[ndirs].prefix = -1;
9786         for (j = 0; j < ndirs; j++)
9787           if (dirs[j].length < dirs[ndirs].length
9788               && dirs[j].length > 1
9789               && (dirs[ndirs].prefix == -1
9790                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9791               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9792             dirs[ndirs].prefix = j;
9793
9794         ++ndirs;
9795       }
9796
9797   /* Now to the actual work.  We have to find a subset of the directories which
9798      allow expressing the file name using references to the directory table
9799      with the least amount of characters.  We do not do an exhaustive search
9800      where we would have to check out every combination of every single
9801      possible prefix.  Instead we use a heuristic which provides nearly optimal
9802      results in most cases and never is much off.  */
9803   saved = XALLOCAVEC (int, ndirs);
9804   savehere = XALLOCAVEC (int, ndirs);
9805
9806   memset (saved, '\0', ndirs * sizeof (saved[0]));
9807   for (i = 0; i < ndirs; i++)
9808     {
9809       int j;
9810       int total;
9811
9812       /* We can always save some space for the current directory.  But this
9813          does not mean it will be enough to justify adding the directory.  */
9814       savehere[i] = dirs[i].length;
9815       total = (savehere[i] - saved[i]) * dirs[i].count;
9816
9817       for (j = i + 1; j < ndirs; j++)
9818         {
9819           savehere[j] = 0;
9820           if (saved[j] < dirs[i].length)
9821             {
9822               /* Determine whether the dirs[i] path is a prefix of the
9823                  dirs[j] path.  */
9824               int k;
9825
9826               k = dirs[j].prefix;
9827               while (k != -1 && k != (int) i)
9828                 k = dirs[k].prefix;
9829
9830               if (k == (int) i)
9831                 {
9832                   /* Yes it is.  We can possibly save some memory by
9833                      writing the filenames in dirs[j] relative to
9834                      dirs[i].  */
9835                   savehere[j] = dirs[i].length;
9836                   total += (savehere[j] - saved[j]) * dirs[j].count;
9837                 }
9838             }
9839         }
9840
9841       /* Check whether we can save enough to justify adding the dirs[i]
9842          directory.  */
9843       if (total > dirs[i].length + 1)
9844         {
9845           /* It's worthwhile adding.  */
9846           for (j = i; j < ndirs; j++)
9847             if (savehere[j] > 0)
9848               {
9849                 /* Remember how much we saved for this directory so far.  */
9850                 saved[j] = savehere[j];
9851
9852                 /* Remember the prefix directory.  */
9853                 dirs[j].dir_idx = i;
9854               }
9855         }
9856     }
9857
9858   /* Emit the directory name table.  */
9859   idx = 1;
9860   idx_offset = dirs[0].length > 0 ? 1 : 0;
9861   for (i = 1 - idx_offset; i < ndirs; i++)
9862     dw2_asm_output_nstring (dirs[i].path,
9863                             dirs[i].length
9864                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9865                             "Directory Entry: 0x%x", i + idx_offset);
9866
9867   dw2_asm_output_data (1, 0, "End directory table");
9868
9869   /* We have to emit them in the order of emitted_number since that's
9870      used in the debug info generation.  To do this efficiently we
9871      generate a back-mapping of the indices first.  */
9872   backmap = XALLOCAVEC (int, numfiles);
9873   for (i = 0; i < numfiles; i++)
9874     backmap[files[i].file_idx->emitted_number - 1] = i;
9875
9876   /* Now write all the file names.  */
9877   for (i = 0; i < numfiles; i++)
9878     {
9879       int file_idx = backmap[i];
9880       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9881
9882 #ifdef VMS_DEBUGGING_INFO
9883 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9884
9885       /* Setting these fields can lead to debugger miscomparisons,
9886          but VMS Debug requires them to be set correctly.  */
9887
9888       int ver;
9889       long long cdt;
9890       long siz;
9891       int maxfilelen = strlen (files[file_idx].path)
9892                                + dirs[dir_idx].length
9893                                + MAX_VMS_VERSION_LEN + 1;
9894       char *filebuf = XALLOCAVEC (char, maxfilelen);
9895
9896       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9897       snprintf (filebuf, maxfilelen, "%s;%d",
9898                 files[file_idx].path + dirs[dir_idx].length, ver);
9899
9900       dw2_asm_output_nstring
9901         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
9902
9903       /* Include directory index.  */
9904       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9905
9906       /* Modification time.  */
9907       dw2_asm_output_data_uleb128
9908         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9909           ? cdt : 0,
9910          NULL);
9911
9912       /* File length in bytes.  */
9913       dw2_asm_output_data_uleb128
9914         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9915           ? siz : 0,
9916          NULL);
9917 #else
9918       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9919                               "File Entry: 0x%x", (unsigned) i + 1);
9920
9921       /* Include directory index.  */
9922       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9923
9924       /* Modification time.  */
9925       dw2_asm_output_data_uleb128 (0, NULL);
9926
9927       /* File length in bytes.  */
9928       dw2_asm_output_data_uleb128 (0, NULL);
9929 #endif
9930     }
9931
9932   dw2_asm_output_data (1, 0, "End file name table");
9933 }
9934
9935
9936 /* Output the source line number correspondence information.  This
9937    information goes into the .debug_line section.  */
9938
9939 static void
9940 output_line_info (void)
9941 {
9942   char l1[20], l2[20], p1[20], p2[20];
9943   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9944   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9945   unsigned opc;
9946   unsigned n_op_args;
9947   unsigned long lt_index;
9948   unsigned long current_line;
9949   long line_offset;
9950   long line_delta;
9951   unsigned long current_file;
9952   unsigned long function;
9953
9954   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9955   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9956   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9957   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9958
9959   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9960     dw2_asm_output_data (4, 0xffffffff,
9961       "Initial length escape value indicating 64-bit DWARF extension");
9962   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9963                         "Length of Source Line Info");
9964   ASM_OUTPUT_LABEL (asm_out_file, l1);
9965
9966   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9967   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9968   ASM_OUTPUT_LABEL (asm_out_file, p1);
9969
9970   /* Define the architecture-dependent minimum instruction length (in
9971    bytes).  In this implementation of DWARF, this field is used for
9972    information purposes only.  Since GCC generates assembly language,
9973    we have no a priori knowledge of how many instruction bytes are
9974    generated for each source line, and therefore can use only the
9975    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9976    commands.  Accordingly, we fix this as `1', which is "correct
9977    enough" for all architectures, and don't let the target override.  */
9978   dw2_asm_output_data (1, 1,
9979                        "Minimum Instruction Length");
9980
9981   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9982                        "Default is_stmt_start flag");
9983   dw2_asm_output_data (1, DWARF_LINE_BASE,
9984                        "Line Base Value (Special Opcodes)");
9985   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9986                        "Line Range Value (Special Opcodes)");
9987   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9988                        "Special Opcode Base");
9989
9990   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9991     {
9992       switch (opc)
9993         {
9994         case DW_LNS_advance_pc:
9995         case DW_LNS_advance_line:
9996         case DW_LNS_set_file:
9997         case DW_LNS_set_column:
9998         case DW_LNS_fixed_advance_pc:
9999           n_op_args = 1;
10000           break;
10001         default:
10002           n_op_args = 0;
10003           break;
10004         }
10005
10006       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
10007                            opc, n_op_args);
10008     }
10009
10010   /* Write out the information about the files we use.  */
10011   output_file_names ();
10012   ASM_OUTPUT_LABEL (asm_out_file, p2);
10013
10014   /* We used to set the address register to the first location in the text
10015      section here, but that didn't accomplish anything since we already
10016      have a line note for the opening brace of the first function.  */
10017
10018   /* Generate the line number to PC correspondence table, encoded as
10019      a series of state machine operations.  */
10020   current_file = 1;
10021   current_line = 1;
10022
10023   if (cfun && in_cold_section_p)
10024     strcpy (prev_line_label, crtl->subsections.cold_section_label);
10025   else
10026     strcpy (prev_line_label, text_section_label);
10027   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
10028     {
10029       dw_line_info_ref line_info = &line_info_table[lt_index];
10030
10031 #if 0
10032       /* Disable this optimization for now; GDB wants to see two line notes
10033          at the beginning of a function so it can find the end of the
10034          prologue.  */
10035
10036       /* Don't emit anything for redundant notes.  Just updating the
10037          address doesn't accomplish anything, because we already assume
10038          that anything after the last address is this line.  */
10039       if (line_info->dw_line_num == current_line
10040           && line_info->dw_file_num == current_file)
10041         continue;
10042 #endif
10043
10044       /* Emit debug info for the address of the current line.
10045
10046          Unfortunately, we have little choice here currently, and must always
10047          use the most general form.  GCC does not know the address delta
10048          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
10049          attributes which will give an upper bound on the address range.  We
10050          could perhaps use length attributes to determine when it is safe to
10051          use DW_LNS_fixed_advance_pc.  */
10052
10053       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
10054       if (0)
10055         {
10056           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
10057           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10058                                "DW_LNS_fixed_advance_pc");
10059           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10060         }
10061       else
10062         {
10063           /* This can handle any delta.  This takes
10064              4+DWARF2_ADDR_SIZE bytes.  */
10065           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10066           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10067           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10068           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10069         }
10070
10071       strcpy (prev_line_label, line_label);
10072
10073       /* Emit debug info for the source file of the current line, if
10074          different from the previous line.  */
10075       if (line_info->dw_file_num != current_file)
10076         {
10077           current_file = line_info->dw_file_num;
10078           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10079           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10080         }
10081
10082       /* Emit debug info for the current line number, choosing the encoding
10083          that uses the least amount of space.  */
10084       if (line_info->dw_line_num != current_line)
10085         {
10086           line_offset = line_info->dw_line_num - current_line;
10087           line_delta = line_offset - DWARF_LINE_BASE;
10088           current_line = line_info->dw_line_num;
10089           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10090             /* This can handle deltas from -10 to 234, using the current
10091                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
10092                takes 1 byte.  */
10093             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10094                                  "line %lu", current_line);
10095           else
10096             {
10097               /* This can handle any delta.  This takes at least 4 bytes,
10098                  depending on the value being encoded.  */
10099               dw2_asm_output_data (1, DW_LNS_advance_line,
10100                                    "advance to line %lu", current_line);
10101               dw2_asm_output_data_sleb128 (line_offset, NULL);
10102               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10103             }
10104         }
10105       else
10106         /* We still need to start a new row, so output a copy insn.  */
10107         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10108     }
10109
10110   /* Emit debug info for the address of the end of the function.  */
10111   if (0)
10112     {
10113       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10114                            "DW_LNS_fixed_advance_pc");
10115       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
10116     }
10117   else
10118     {
10119       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10120       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10121       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10122       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
10123     }
10124
10125   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10126   dw2_asm_output_data_uleb128 (1, NULL);
10127   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10128
10129   function = 0;
10130   current_file = 1;
10131   current_line = 1;
10132   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
10133     {
10134       dw_separate_line_info_ref line_info
10135         = &separate_line_info_table[lt_index];
10136
10137 #if 0
10138       /* Don't emit anything for redundant notes.  */
10139       if (line_info->dw_line_num == current_line
10140           && line_info->dw_file_num == current_file
10141           && line_info->function == function)
10142         goto cont;
10143 #endif
10144
10145       /* Emit debug info for the address of the current line.  If this is
10146          a new function, or the first line of a function, then we need
10147          to handle it differently.  */
10148       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
10149                                    lt_index);
10150       if (function != line_info->function)
10151         {
10152           function = line_info->function;
10153
10154           /* Set the address register to the first line in the function.  */
10155           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10156           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10157           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10158           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10159         }
10160       else
10161         {
10162           /* ??? See the DW_LNS_advance_pc comment above.  */
10163           if (0)
10164             {
10165               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10166                                    "DW_LNS_fixed_advance_pc");
10167               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10168             }
10169           else
10170             {
10171               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10172               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10173               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10174               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10175             }
10176         }
10177
10178       strcpy (prev_line_label, line_label);
10179
10180       /* Emit debug info for the source file of the current line, if
10181          different from the previous line.  */
10182       if (line_info->dw_file_num != current_file)
10183         {
10184           current_file = line_info->dw_file_num;
10185           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10186           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10187         }
10188
10189       /* Emit debug info for the current line number, choosing the encoding
10190          that uses the least amount of space.  */
10191       if (line_info->dw_line_num != current_line)
10192         {
10193           line_offset = line_info->dw_line_num - current_line;
10194           line_delta = line_offset - DWARF_LINE_BASE;
10195           current_line = line_info->dw_line_num;
10196           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10197             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10198                                  "line %lu", current_line);
10199           else
10200             {
10201               dw2_asm_output_data (1, DW_LNS_advance_line,
10202                                    "advance to line %lu", current_line);
10203               dw2_asm_output_data_sleb128 (line_offset, NULL);
10204               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10205             }
10206         }
10207       else
10208         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10209
10210 #if 0
10211     cont:
10212 #endif
10213
10214       lt_index++;
10215
10216       /* If we're done with a function, end its sequence.  */
10217       if (lt_index == separate_line_info_table_in_use
10218           || separate_line_info_table[lt_index].function != function)
10219         {
10220           current_file = 1;
10221           current_line = 1;
10222
10223           /* Emit debug info for the address of the end of the function.  */
10224           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
10225           if (0)
10226             {
10227               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10228                                    "DW_LNS_fixed_advance_pc");
10229               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10230             }
10231           else
10232             {
10233               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10234               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10235               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10236               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10237             }
10238
10239           /* Output the marker for the end of this sequence.  */
10240           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10241           dw2_asm_output_data_uleb128 (1, NULL);
10242           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10243         }
10244     }
10245
10246   /* Output the marker for the end of the line number info.  */
10247   ASM_OUTPUT_LABEL (asm_out_file, l2);
10248 }
10249 \f
10250 /* Given a pointer to a tree node for some base type, return a pointer to
10251    a DIE that describes the given type.
10252
10253    This routine must only be called for GCC type nodes that correspond to
10254    Dwarf base (fundamental) types.  */
10255
10256 static dw_die_ref
10257 base_type_die (tree type)
10258 {
10259   dw_die_ref base_type_result;
10260   enum dwarf_type encoding;
10261
10262   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10263     return 0;
10264
10265   /* If this is a subtype that should not be emitted as a subrange type,
10266      use the base type.  See subrange_type_for_debug_p.  */
10267   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10268     type = TREE_TYPE (type);
10269
10270   switch (TREE_CODE (type))
10271     {
10272     case INTEGER_TYPE:
10273       if (TYPE_STRING_FLAG (type))
10274         {
10275           if (TYPE_UNSIGNED (type))
10276             encoding = DW_ATE_unsigned_char;
10277           else
10278             encoding = DW_ATE_signed_char;
10279         }
10280       else if (TYPE_UNSIGNED (type))
10281         encoding = DW_ATE_unsigned;
10282       else
10283         encoding = DW_ATE_signed;
10284       break;
10285
10286     case REAL_TYPE:
10287       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10288         {
10289           if (dwarf_version >= 3 || !dwarf_strict)
10290             encoding = DW_ATE_decimal_float;
10291           else
10292             encoding = DW_ATE_lo_user;
10293         }
10294       else
10295         encoding = DW_ATE_float;
10296       break;
10297
10298     case FIXED_POINT_TYPE:
10299       if (!(dwarf_version >= 3 || !dwarf_strict))
10300         encoding = DW_ATE_lo_user;
10301       else if (TYPE_UNSIGNED (type))
10302         encoding = DW_ATE_unsigned_fixed;
10303       else
10304         encoding = DW_ATE_signed_fixed;
10305       break;
10306
10307       /* Dwarf2 doesn't know anything about complex ints, so use
10308          a user defined type for it.  */
10309     case COMPLEX_TYPE:
10310       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10311         encoding = DW_ATE_complex_float;
10312       else
10313         encoding = DW_ATE_lo_user;
10314       break;
10315
10316     case BOOLEAN_TYPE:
10317       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10318       encoding = DW_ATE_boolean;
10319       break;
10320
10321     default:
10322       /* No other TREE_CODEs are Dwarf fundamental types.  */
10323       gcc_unreachable ();
10324     }
10325
10326   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
10327
10328   /* This probably indicates a bug.  */
10329   if (! TYPE_NAME (type))
10330     add_name_attribute (base_type_result, "__unknown__");
10331
10332   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10333                    int_size_in_bytes (type));
10334   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10335
10336   return base_type_result;
10337 }
10338
10339 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10340    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10341
10342 static inline int
10343 is_base_type (tree type)
10344 {
10345   switch (TREE_CODE (type))
10346     {
10347     case ERROR_MARK:
10348     case VOID_TYPE:
10349     case INTEGER_TYPE:
10350     case REAL_TYPE:
10351     case FIXED_POINT_TYPE:
10352     case COMPLEX_TYPE:
10353     case BOOLEAN_TYPE:
10354       return 1;
10355
10356     case ARRAY_TYPE:
10357     case RECORD_TYPE:
10358     case UNION_TYPE:
10359     case QUAL_UNION_TYPE:
10360     case ENUMERAL_TYPE:
10361     case FUNCTION_TYPE:
10362     case METHOD_TYPE:
10363     case POINTER_TYPE:
10364     case REFERENCE_TYPE:
10365     case OFFSET_TYPE:
10366     case LANG_TYPE:
10367     case VECTOR_TYPE:
10368       return 0;
10369
10370     default:
10371       gcc_unreachable ();
10372     }
10373
10374   return 0;
10375 }
10376
10377 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10378    node, return the size in bits for the type if it is a constant, or else
10379    return the alignment for the type if the type's size is not constant, or
10380    else return BITS_PER_WORD if the type actually turns out to be an
10381    ERROR_MARK node.  */
10382
10383 static inline unsigned HOST_WIDE_INT
10384 simple_type_size_in_bits (const_tree type)
10385 {
10386   if (TREE_CODE (type) == ERROR_MARK)
10387     return BITS_PER_WORD;
10388   else if (TYPE_SIZE (type) == NULL_TREE)
10389     return 0;
10390   else if (host_integerp (TYPE_SIZE (type), 1))
10391     return tree_low_cst (TYPE_SIZE (type), 1);
10392   else
10393     return TYPE_ALIGN (type);
10394 }
10395
10396 /*  Given a pointer to a tree node for a subrange type, return a pointer
10397     to a DIE that describes the given type.  */
10398
10399 static dw_die_ref
10400 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10401 {
10402   dw_die_ref subrange_die;
10403   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10404
10405   if (context_die == NULL)
10406     context_die = comp_unit_die;
10407
10408   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10409
10410   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10411     {
10412       /* The size of the subrange type and its base type do not match,
10413          so we need to generate a size attribute for the subrange type.  */
10414       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10415     }
10416
10417   if (low)
10418     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10419   if (high)
10420     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10421
10422   return subrange_die;
10423 }
10424
10425 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10426    entry that chains various modifiers in front of the given type.  */
10427
10428 static dw_die_ref
10429 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10430                    dw_die_ref context_die)
10431 {
10432   enum tree_code code = TREE_CODE (type);
10433   dw_die_ref mod_type_die;
10434   dw_die_ref sub_die = NULL;
10435   tree item_type = NULL;
10436   tree qualified_type;
10437   tree name, low, high;
10438
10439   if (code == ERROR_MARK)
10440     return NULL;
10441
10442   /* See if we already have the appropriately qualified variant of
10443      this type.  */
10444   qualified_type
10445     = get_qualified_type (type,
10446                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10447                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10448
10449   /* If we do, then we can just use its DIE, if it exists.  */
10450   if (qualified_type)
10451     {
10452       mod_type_die = lookup_type_die (qualified_type);
10453       if (mod_type_die)
10454         return mod_type_die;
10455     }
10456
10457   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10458
10459   /* Handle C typedef types.  */
10460   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
10461     {
10462       tree dtype = TREE_TYPE (name);
10463
10464       if (qualified_type == dtype)
10465         {
10466           /* For a named type, use the typedef.  */
10467           gen_type_die (qualified_type, context_die);
10468           return lookup_type_die (qualified_type);
10469         }
10470       else if (is_const_type < TYPE_READONLY (dtype)
10471                || is_volatile_type < TYPE_VOLATILE (dtype)
10472                || (is_const_type <= TYPE_READONLY (dtype)
10473                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10474                    && DECL_ORIGINAL_TYPE (name) != type))
10475         /* cv-unqualified version of named type.  Just use the unnamed
10476            type to which it refers.  */
10477         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10478                                   is_const_type, is_volatile_type,
10479                                   context_die);
10480       /* Else cv-qualified version of named type; fall through.  */
10481     }
10482
10483   if (is_const_type)
10484     {
10485       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
10486       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10487     }
10488   else if (is_volatile_type)
10489     {
10490       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
10491       sub_die = modified_type_die (type, 0, 0, context_die);
10492     }
10493   else if (code == POINTER_TYPE)
10494     {
10495       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
10496       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10497                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10498       item_type = TREE_TYPE (type);
10499     }
10500   else if (code == REFERENCE_TYPE)
10501     {
10502       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
10503       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10504                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10505       item_type = TREE_TYPE (type);
10506     }
10507   else if (code == INTEGER_TYPE
10508            && TREE_TYPE (type) != NULL_TREE
10509            && subrange_type_for_debug_p (type, &low, &high))
10510     {
10511       mod_type_die = subrange_type_die (type, low, high, context_die);
10512       item_type = TREE_TYPE (type);
10513     }
10514   else if (is_base_type (type))
10515     mod_type_die = base_type_die (type);
10516   else
10517     {
10518       gen_type_die (type, context_die);
10519
10520       /* We have to get the type_main_variant here (and pass that to the
10521          `lookup_type_die' routine) because the ..._TYPE node we have
10522          might simply be a *copy* of some original type node (where the
10523          copy was created to help us keep track of typedef names) and
10524          that copy might have a different TYPE_UID from the original
10525          ..._TYPE node.  */
10526       if (TREE_CODE (type) != VECTOR_TYPE)
10527         return lookup_type_die (type_main_variant (type));
10528       else
10529         /* Vectors have the debugging information in the type,
10530            not the main variant.  */
10531         return lookup_type_die (type);
10532     }
10533
10534   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10535      don't output a DW_TAG_typedef, since there isn't one in the
10536      user's program; just attach a DW_AT_name to the type.  */
10537   if (name
10538       && (TREE_CODE (name) != TYPE_DECL
10539           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
10540     {
10541       if (TREE_CODE (name) == TYPE_DECL)
10542         /* Could just call add_name_and_src_coords_attributes here,
10543            but since this is a builtin type it doesn't have any
10544            useful source coordinates anyway.  */
10545         name = DECL_NAME (name);
10546       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10547     }
10548
10549   if (qualified_type)
10550     equate_type_number_to_die (qualified_type, mod_type_die);
10551
10552   if (item_type)
10553     /* We must do this after the equate_type_number_to_die call, in case
10554        this is a recursive type.  This ensures that the modified_type_die
10555        recursion will terminate even if the type is recursive.  Recursive
10556        types are possible in Ada.  */
10557     sub_die = modified_type_die (item_type,
10558                                  TYPE_READONLY (item_type),
10559                                  TYPE_VOLATILE (item_type),
10560                                  context_die);
10561
10562   if (sub_die != NULL)
10563     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10564
10565   return mod_type_die;
10566 }
10567
10568 /* Generate DIEs for the generic parameters of T.
10569    T must be either a generic type or a generic function.
10570    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10571
10572 static void
10573 gen_generic_params_dies (tree t)
10574 {
10575   tree parms, args;
10576   int parms_num, i;
10577   dw_die_ref die = NULL;
10578
10579   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10580     return;
10581
10582   if (TYPE_P (t))
10583     die = lookup_type_die (t);
10584   else if (DECL_P (t))
10585     die = lookup_decl_die (t);
10586
10587   gcc_assert (die);
10588
10589   parms = lang_hooks.get_innermost_generic_parms (t);
10590   if (!parms)
10591     /* T has no generic parameter. It means T is neither a generic type
10592        or function. End of story.  */
10593     return;
10594
10595   parms_num = TREE_VEC_LENGTH (parms);
10596   args = lang_hooks.get_innermost_generic_args (t);
10597   for (i = 0; i < parms_num; i++)
10598     {
10599       tree parm, arg, arg_pack_elems;
10600
10601       parm = TREE_VEC_ELT (parms, i);
10602       arg = TREE_VEC_ELT (args, i);
10603       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10604       gcc_assert (parm && TREE_VALUE (parm) && arg);
10605
10606       if (parm && TREE_VALUE (parm) && arg)
10607         {
10608           /* If PARM represents a template parameter pack,
10609              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10610              by DW_TAG_template_*_parameter DIEs for the argument
10611              pack elements of ARG. Note that ARG would then be
10612              an argument pack.  */
10613           if (arg_pack_elems)
10614             template_parameter_pack_die (TREE_VALUE (parm),
10615                                          arg_pack_elems,
10616                                          die);
10617           else
10618             generic_parameter_die (TREE_VALUE (parm), arg,
10619                                    true /* Emit DW_AT_name */, die);
10620         }
10621     }
10622 }
10623
10624 /* Create and return a DIE for PARM which should be
10625    the representation of a generic type parameter.
10626    For instance, in the C++ front end, PARM would be a template parameter.
10627    ARG is the argument to PARM.
10628    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10629    name of the PARM.
10630    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10631    as a child node.  */
10632
10633 static dw_die_ref
10634 generic_parameter_die (tree parm, tree arg,
10635                        bool emit_name_p,
10636                        dw_die_ref parent_die)
10637 {
10638   dw_die_ref tmpl_die = NULL;
10639   const char *name = NULL;
10640
10641   if (!parm || !DECL_NAME (parm) || !arg)
10642     return NULL;
10643
10644   /* We support non-type generic parameters and arguments,
10645      type generic parameters and arguments, as well as
10646      generic generic parameters (a.k.a. template template parameters in C++)
10647      and arguments.  */
10648   if (TREE_CODE (parm) == PARM_DECL)
10649     /* PARM is a nontype generic parameter  */
10650     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10651   else if (TREE_CODE (parm) == TYPE_DECL)
10652     /* PARM is a type generic parameter.  */
10653     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10654   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10655     /* PARM is a generic generic parameter.
10656        Its DIE is a GNU extension. It shall have a
10657        DW_AT_name attribute to represent the name of the template template
10658        parameter, and a DW_AT_GNU_template_name attribute to represent the
10659        name of the template template argument.  */
10660     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10661                         parent_die, parm);
10662   else
10663     gcc_unreachable ();
10664
10665   if (tmpl_die)
10666     {
10667       tree tmpl_type;
10668
10669       /* If PARM is a generic parameter pack, it means we are
10670          emitting debug info for a template argument pack element.
10671          In other terms, ARG is a template argument pack element.
10672          In that case, we don't emit any DW_AT_name attribute for
10673          the die.  */
10674       if (emit_name_p)
10675         {
10676           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10677           gcc_assert (name);
10678           add_AT_string (tmpl_die, DW_AT_name, name);
10679         }
10680
10681       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10682         {
10683           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10684              TMPL_DIE should have a child DW_AT_type attribute that is set
10685              to the type of the argument to PARM, which is ARG.
10686              If PARM is a type generic parameter, TMPL_DIE should have a
10687              child DW_AT_type that is set to ARG.  */
10688           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10689           add_type_attribute (tmpl_die, tmpl_type, 0,
10690                               TREE_THIS_VOLATILE (tmpl_type),
10691                               parent_die);
10692         }
10693       else
10694         {
10695           /* So TMPL_DIE is a DIE representing a
10696              a generic generic template parameter, a.k.a template template
10697              parameter in C++ and arg is a template.  */
10698
10699           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10700              to the name of the argument.  */
10701           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10702           add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10703         }
10704
10705       if (TREE_CODE (parm) == PARM_DECL)
10706         /* So PARM is a non-type generic parameter.
10707            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10708            attribute of TMPL_DIE which value represents the value
10709            of ARG.
10710            We must be careful here:
10711            The value of ARG might reference some function decls.
10712            We might currently be emitting debug info for a generic
10713            type and types are emitted before function decls, we don't
10714            know if the function decls referenced by ARG will actually be
10715            emitted after cgraph computations.
10716            So must defer the generation of the DW_AT_const_value to
10717            after cgraph is ready.  */
10718         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10719     }
10720
10721   return tmpl_die;
10722 }
10723
10724 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10725    PARM_PACK must be a template parameter pack. The returned DIE
10726    will be child DIE of PARENT_DIE.  */
10727
10728 static dw_die_ref
10729 template_parameter_pack_die (tree parm_pack,
10730                              tree parm_pack_args,
10731                              dw_die_ref parent_die)
10732 {
10733   dw_die_ref die;
10734   int j;
10735
10736   gcc_assert (parent_die
10737               && parm_pack
10738               && DECL_NAME (parm_pack));
10739
10740   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10741   add_AT_string (die, DW_AT_name, IDENTIFIER_POINTER (DECL_NAME (parm_pack)));
10742
10743   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10744     generic_parameter_die (parm_pack,
10745                            TREE_VEC_ELT (parm_pack_args, j),
10746                            false /* Don't emit DW_AT_name */,
10747                            die);
10748   return die;
10749 }
10750
10751 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10752    an enumerated type.  */
10753
10754 static inline int
10755 type_is_enum (const_tree type)
10756 {
10757   return TREE_CODE (type) == ENUMERAL_TYPE;
10758 }
10759
10760 /* Return the DBX register number described by a given RTL node.  */
10761
10762 static unsigned int
10763 dbx_reg_number (const_rtx rtl)
10764 {
10765   unsigned regno = REGNO (rtl);
10766
10767   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10768
10769 #ifdef LEAF_REG_REMAP
10770   if (current_function_uses_only_leaf_regs)
10771     {
10772       int leaf_reg = LEAF_REG_REMAP (regno);
10773       if (leaf_reg != -1)
10774         regno = (unsigned) leaf_reg;
10775     }
10776 #endif
10777
10778   return DBX_REGISTER_NUMBER (regno);
10779 }
10780
10781 /* Optionally add a DW_OP_piece term to a location description expression.
10782    DW_OP_piece is only added if the location description expression already
10783    doesn't end with DW_OP_piece.  */
10784
10785 static void
10786 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10787 {
10788   dw_loc_descr_ref loc;
10789
10790   if (*list_head != NULL)
10791     {
10792       /* Find the end of the chain.  */
10793       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10794         ;
10795
10796       if (loc->dw_loc_opc != DW_OP_piece)
10797         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10798     }
10799 }
10800
10801 /* Return a location descriptor that designates a machine register or
10802    zero if there is none.  */
10803
10804 static dw_loc_descr_ref
10805 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10806 {
10807   rtx regs;
10808
10809   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10810     return 0;
10811
10812   regs = targetm.dwarf_register_span (rtl);
10813
10814   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10815     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10816   else
10817     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10818 }
10819
10820 /* Return a location descriptor that designates a machine register for
10821    a given hard register number.  */
10822
10823 static dw_loc_descr_ref
10824 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10825 {
10826   dw_loc_descr_ref reg_loc_descr;
10827
10828   if (regno <= 31)
10829     reg_loc_descr
10830       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10831   else
10832     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10833
10834   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10835     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10836
10837   return reg_loc_descr;
10838 }
10839
10840 /* Given an RTL of a register, return a location descriptor that
10841    designates a value that spans more than one register.  */
10842
10843 static dw_loc_descr_ref
10844 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10845                              enum var_init_status initialized)
10846 {
10847   int nregs, size, i;
10848   unsigned reg;
10849   dw_loc_descr_ref loc_result = NULL;
10850
10851   reg = REGNO (rtl);
10852 #ifdef LEAF_REG_REMAP
10853   if (current_function_uses_only_leaf_regs)
10854     {
10855       int leaf_reg = LEAF_REG_REMAP (reg);
10856       if (leaf_reg != -1)
10857         reg = (unsigned) leaf_reg;
10858     }
10859 #endif
10860   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10861   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10862
10863   /* Simple, contiguous registers.  */
10864   if (regs == NULL_RTX)
10865     {
10866       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10867
10868       loc_result = NULL;
10869       while (nregs--)
10870         {
10871           dw_loc_descr_ref t;
10872
10873           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10874                                       VAR_INIT_STATUS_INITIALIZED);
10875           add_loc_descr (&loc_result, t);
10876           add_loc_descr_op_piece (&loc_result, size);
10877           ++reg;
10878         }
10879       return loc_result;
10880     }
10881
10882   /* Now onto stupid register sets in non contiguous locations.  */
10883
10884   gcc_assert (GET_CODE (regs) == PARALLEL);
10885
10886   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10887   loc_result = NULL;
10888
10889   for (i = 0; i < XVECLEN (regs, 0); ++i)
10890     {
10891       dw_loc_descr_ref t;
10892
10893       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10894                                   VAR_INIT_STATUS_INITIALIZED);
10895       add_loc_descr (&loc_result, t);
10896       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10897       add_loc_descr_op_piece (&loc_result, size);
10898     }
10899
10900   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10901     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10902   return loc_result;
10903 }
10904
10905 #endif /* DWARF2_DEBUGGING_INFO */
10906
10907 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10908
10909 /* Return a location descriptor that designates a constant.  */
10910
10911 static dw_loc_descr_ref
10912 int_loc_descriptor (HOST_WIDE_INT i)
10913 {
10914   enum dwarf_location_atom op;
10915
10916   /* Pick the smallest representation of a constant, rather than just
10917      defaulting to the LEB encoding.  */
10918   if (i >= 0)
10919     {
10920       if (i <= 31)
10921         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10922       else if (i <= 0xff)
10923         op = DW_OP_const1u;
10924       else if (i <= 0xffff)
10925         op = DW_OP_const2u;
10926       else if (HOST_BITS_PER_WIDE_INT == 32
10927                || i <= 0xffffffff)
10928         op = DW_OP_const4u;
10929       else
10930         op = DW_OP_constu;
10931     }
10932   else
10933     {
10934       if (i >= -0x80)
10935         op = DW_OP_const1s;
10936       else if (i >= -0x8000)
10937         op = DW_OP_const2s;
10938       else if (HOST_BITS_PER_WIDE_INT == 32
10939                || i >= -0x80000000)
10940         op = DW_OP_const4s;
10941       else
10942         op = DW_OP_consts;
10943     }
10944
10945   return new_loc_descr (op, i, 0);
10946 }
10947 #endif
10948
10949 #ifdef DWARF2_DEBUGGING_INFO
10950 /* Return loc description representing "address" of integer value.
10951    This can appear only as toplevel expression.  */
10952
10953 static dw_loc_descr_ref
10954 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10955 {
10956   int litsize;
10957   dw_loc_descr_ref loc_result = NULL;
10958
10959   if (!(dwarf_version >= 4 || !dwarf_strict))
10960     return NULL;
10961
10962   if (i >= 0)
10963     {
10964       if (i <= 31)
10965         litsize = 1;
10966       else if (i <= 0xff)
10967         litsize = 2;
10968       else if (i <= 0xffff)
10969         litsize = 3;
10970       else if (HOST_BITS_PER_WIDE_INT == 32
10971                || i <= 0xffffffff)
10972         litsize = 5;
10973       else
10974         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10975     }
10976   else
10977     {
10978       if (i >= -0x80)
10979         litsize = 2;
10980       else if (i >= -0x8000)
10981         litsize = 3;
10982       else if (HOST_BITS_PER_WIDE_INT == 32
10983                || i >= -0x80000000)
10984         litsize = 5;
10985       else
10986         litsize = 1 + size_of_sleb128 (i);
10987     }
10988   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10989      is more compact.  For DW_OP_stack_value we need:
10990      litsize + 1 (DW_OP_stack_value)
10991      and for DW_OP_implicit_value:
10992      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10993   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10994     {
10995       loc_result = int_loc_descriptor (i);
10996       add_loc_descr (&loc_result,
10997                      new_loc_descr (DW_OP_stack_value, 0, 0));
10998       return loc_result;
10999     }
11000
11001   loc_result = new_loc_descr (DW_OP_implicit_value,
11002                               size, 0);
11003   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11004   loc_result->dw_loc_oprnd2.v.val_int = i;
11005   return loc_result;
11006 }
11007
11008 /* Return a location descriptor that designates a base+offset location.  */
11009
11010 static dw_loc_descr_ref
11011 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11012                  enum var_init_status initialized)
11013 {
11014   unsigned int regno;
11015   dw_loc_descr_ref result;
11016   dw_fde_ref fde = current_fde ();
11017
11018   /* We only use "frame base" when we're sure we're talking about the
11019      post-prologue local stack frame.  We do this by *not* running
11020      register elimination until this point, and recognizing the special
11021      argument pointer and soft frame pointer rtx's.  */
11022   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11023     {
11024       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11025
11026       if (elim != reg)
11027         {
11028           if (GET_CODE (elim) == PLUS)
11029             {
11030               offset += INTVAL (XEXP (elim, 1));
11031               elim = XEXP (elim, 0);
11032             }
11033           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11034                        && (elim == hard_frame_pointer_rtx
11035                            || elim == stack_pointer_rtx))
11036                       || elim == (frame_pointer_needed
11037                                   ? hard_frame_pointer_rtx
11038                                   : stack_pointer_rtx));
11039
11040           /* If drap register is used to align stack, use frame
11041              pointer + offset to access stack variables.  If stack
11042              is aligned without drap, use stack pointer + offset to
11043              access stack variables.  */
11044           if (crtl->stack_realign_tried
11045               && reg == frame_pointer_rtx)
11046             {
11047               int base_reg
11048                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11049                                       ? HARD_FRAME_POINTER_REGNUM
11050                                       : STACK_POINTER_REGNUM);
11051               return new_reg_loc_descr (base_reg, offset);
11052             }
11053
11054           offset += frame_pointer_fb_offset;
11055           return new_loc_descr (DW_OP_fbreg, offset, 0);
11056         }
11057     }
11058   else if (fde
11059            && fde->drap_reg != INVALID_REGNUM
11060            && (fde->drap_reg == REGNO (reg)
11061                || fde->vdrap_reg == REGNO (reg)))
11062     {
11063       /* Use cfa+offset to represent the location of arguments passed
11064          on stack when drap is used to align stack.  */
11065       return new_loc_descr (DW_OP_fbreg, offset, 0);
11066     }
11067
11068   regno = dbx_reg_number (reg);
11069   if (regno <= 31)
11070     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11071                             offset, 0);
11072   else
11073     result = new_loc_descr (DW_OP_bregx, regno, offset);
11074
11075   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11076     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11077
11078   return result;
11079 }
11080
11081 /* Return true if this RTL expression describes a base+offset calculation.  */
11082
11083 static inline int
11084 is_based_loc (const_rtx rtl)
11085 {
11086   return (GET_CODE (rtl) == PLUS
11087           && ((REG_P (XEXP (rtl, 0))
11088                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11089                && CONST_INT_P (XEXP (rtl, 1)))));
11090 }
11091
11092 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11093    failed.  */
11094
11095 static dw_loc_descr_ref
11096 tls_mem_loc_descriptor (rtx mem)
11097 {
11098   tree base;
11099   dw_loc_descr_ref loc_result;
11100
11101   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
11102     return NULL;
11103
11104   base = get_base_address (MEM_EXPR (mem));
11105   if (base == NULL
11106       || TREE_CODE (base) != VAR_DECL
11107       || !DECL_THREAD_LOCAL_P (base))
11108     return NULL;
11109
11110   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11111   if (loc_result == NULL)
11112     return NULL;
11113
11114   if (INTVAL (MEM_OFFSET (mem)))
11115     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
11116
11117   return loc_result;
11118 }
11119
11120 /* Output debug info about reason why we failed to expand expression as dwarf
11121    expression.  */
11122
11123 static void
11124 expansion_failed (tree expr, rtx rtl, char const *reason)
11125 {
11126   if (dump_file && (dump_flags & TDF_DETAILS))
11127     {
11128       fprintf (dump_file, "Failed to expand as dwarf: ");
11129       if (expr)
11130         print_generic_expr (dump_file, expr, dump_flags);
11131       if (rtl)
11132         {
11133           fprintf (dump_file, "\n");
11134           print_rtl (dump_file, rtl);
11135         }
11136       fprintf (dump_file, "\nReason: %s\n", reason);
11137     }
11138 }
11139
11140 /* Helper function for const_ok_for_output, called either directly
11141    or via for_each_rtx.  */
11142
11143 static int
11144 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11145 {
11146   rtx rtl = *rtlp;
11147
11148   if (GET_CODE (rtl) != SYMBOL_REF)
11149     return 0;
11150
11151   if (CONSTANT_POOL_ADDRESS_P (rtl))
11152     {
11153       bool marked;
11154       get_pool_constant_mark (rtl, &marked);
11155       /* If all references to this pool constant were optimized away,
11156          it was not output and thus we can't represent it.  */
11157       if (!marked)
11158         {
11159           expansion_failed (NULL_TREE, rtl,
11160                             "Constant was removed from constant pool.\n");
11161           return 1;
11162         }
11163     }
11164
11165   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11166     return 1;
11167
11168   /* Avoid references to external symbols in debug info, on several targets
11169      the linker might even refuse to link when linking a shared library,
11170      and in many other cases the relocations for .debug_info/.debug_loc are
11171      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11172      to be defined within the same shared library or executable are fine.  */
11173   if (SYMBOL_REF_EXTERNAL_P (rtl))
11174     {
11175       tree decl = SYMBOL_REF_DECL (rtl);
11176
11177       if (decl == NULL || !targetm.binds_local_p (decl))
11178         {
11179           expansion_failed (NULL_TREE, rtl,
11180                             "Symbol not defined in current TU.\n");
11181           return 1;
11182         }
11183     }
11184
11185   return 0;
11186 }
11187
11188 /* Return true if constant RTL can be emitted in DW_OP_addr or
11189    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11190    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11191
11192 static bool
11193 const_ok_for_output (rtx rtl)
11194 {
11195   if (GET_CODE (rtl) == SYMBOL_REF)
11196     return const_ok_for_output_1 (&rtl, NULL) == 0;
11197
11198   if (GET_CODE (rtl) == CONST)
11199     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11200
11201   return true;
11202 }
11203
11204 /* The following routine converts the RTL for a variable or parameter
11205    (resident in memory) into an equivalent Dwarf representation of a
11206    mechanism for getting the address of that same variable onto the top of a
11207    hypothetical "address evaluation" stack.
11208
11209    When creating memory location descriptors, we are effectively transforming
11210    the RTL for a memory-resident object into its Dwarf postfix expression
11211    equivalent.  This routine recursively descends an RTL tree, turning
11212    it into Dwarf postfix code as it goes.
11213
11214    MODE is the mode of the memory reference, needed to handle some
11215    autoincrement addressing modes.
11216
11217    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
11218    location list for RTL.
11219
11220    Return 0 if we can't represent the location.  */
11221
11222 static dw_loc_descr_ref
11223 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11224                     enum var_init_status initialized)
11225 {
11226   dw_loc_descr_ref mem_loc_result = NULL;
11227   enum dwarf_location_atom op;
11228   dw_loc_descr_ref op0, op1;
11229
11230   /* Note that for a dynamically sized array, the location we will generate a
11231      description of here will be the lowest numbered location which is
11232      actually within the array.  That's *not* necessarily the same as the
11233      zeroth element of the array.  */
11234
11235   rtl = targetm.delegitimize_address (rtl);
11236
11237   switch (GET_CODE (rtl))
11238     {
11239     case POST_INC:
11240     case POST_DEC:
11241     case POST_MODIFY:
11242       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
11243          just fall into the SUBREG code.  */
11244
11245       /* ... fall through ...  */
11246
11247     case SUBREG:
11248       /* The case of a subreg may arise when we have a local (register)
11249          variable or a formal (register) parameter which doesn't quite fill
11250          up an entire register.  For now, just assume that it is
11251          legitimate to make the Dwarf info refer to the whole register which
11252          contains the given subreg.  */
11253       rtl = XEXP (rtl, 0);
11254       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
11255         break;
11256       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
11257       break;
11258
11259     case REG:
11260       /* Whenever a register number forms a part of the description of the
11261          method for calculating the (dynamic) address of a memory resident
11262          object, DWARF rules require the register number be referred to as
11263          a "base register".  This distinction is not based in any way upon
11264          what category of register the hardware believes the given register
11265          belongs to.  This is strictly DWARF terminology we're dealing with
11266          here. Note that in cases where the location of a memory-resident
11267          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11268          OP_CONST (0)) the actual DWARF location descriptor that we generate
11269          may just be OP_BASEREG (basereg).  This may look deceptively like
11270          the object in question was allocated to a register (rather than in
11271          memory) so DWARF consumers need to be aware of the subtle
11272          distinction between OP_REG and OP_BASEREG.  */
11273       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11274         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11275       else if (stack_realign_drap
11276                && crtl->drap_reg
11277                && crtl->args.internal_arg_pointer == rtl
11278                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11279         {
11280           /* If RTL is internal_arg_pointer, which has been optimized
11281              out, use DRAP instead.  */
11282           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11283                                             VAR_INIT_STATUS_INITIALIZED);
11284         }
11285       break;
11286
11287     case SIGN_EXTEND:
11288     case ZERO_EXTEND:
11289       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11290                                 VAR_INIT_STATUS_INITIALIZED);
11291       if (op0 == 0)
11292         break;
11293       else
11294         {
11295           int shift = DWARF2_ADDR_SIZE
11296                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11297           shift *= BITS_PER_UNIT;
11298           if (GET_CODE (rtl) == SIGN_EXTEND)
11299             op = DW_OP_shra;
11300           else
11301             op = DW_OP_shr;
11302           mem_loc_result = op0;
11303           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11304           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11305           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11306           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11307         }
11308       break;
11309
11310     case MEM:
11311       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11312                                            VAR_INIT_STATUS_INITIALIZED);
11313       if (mem_loc_result == NULL)
11314         mem_loc_result = tls_mem_loc_descriptor (rtl);
11315       if (mem_loc_result != 0)
11316         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11317       break;
11318
11319     case LO_SUM:
11320          rtl = XEXP (rtl, 1);
11321
11322       /* ... fall through ...  */
11323
11324     case LABEL_REF:
11325       /* Some ports can transform a symbol ref into a label ref, because
11326          the symbol ref is too far away and has to be dumped into a constant
11327          pool.  */
11328     case CONST:
11329     case SYMBOL_REF:
11330       /* Alternatively, the symbol in the constant pool might be referenced
11331          by a different symbol.  */
11332       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
11333         {
11334           bool marked;
11335           rtx tmp = get_pool_constant_mark (rtl, &marked);
11336
11337           if (GET_CODE (tmp) == SYMBOL_REF)
11338             {
11339               rtl = tmp;
11340               if (CONSTANT_POOL_ADDRESS_P (tmp))
11341                 get_pool_constant_mark (tmp, &marked);
11342               else
11343                 marked = true;
11344             }
11345
11346           /* If all references to this pool constant were optimized away,
11347              it was not output and thus we can't represent it.
11348              FIXME: might try to use DW_OP_const_value here, though
11349              DW_OP_piece complicates it.  */
11350           if (!marked)
11351             {
11352               expansion_failed (NULL_TREE, rtl,
11353                                 "Constant was removed from constant pool.\n");
11354               return 0;
11355             }
11356         }
11357
11358       if (GET_CODE (rtl) == SYMBOL_REF
11359           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11360         {
11361           dw_loc_descr_ref temp;
11362
11363           /* If this is not defined, we have no way to emit the data.  */
11364           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11365             break;
11366
11367           temp = new_loc_descr (DW_OP_addr, 0, 0);
11368           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11369           temp->dw_loc_oprnd1.v.val_addr = rtl;
11370           temp->dtprel = true;
11371
11372           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11373           add_loc_descr (&mem_loc_result, temp);
11374
11375           break;
11376         }
11377
11378       if (!const_ok_for_output (rtl))
11379         break;
11380
11381     symref:
11382       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11383       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11384       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11385       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11386       break;
11387
11388     case CONCAT:
11389     case CONCATN:
11390     case VAR_LOCATION:
11391       expansion_failed (NULL_TREE, rtl,
11392                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11393       return 0;
11394
11395     case PRE_MODIFY:
11396       /* Extract the PLUS expression nested inside and fall into
11397          PLUS code below.  */
11398       rtl = XEXP (rtl, 1);
11399       goto plus;
11400
11401     case PRE_INC:
11402     case PRE_DEC:
11403       /* Turn these into a PLUS expression and fall into the PLUS code
11404          below.  */
11405       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
11406                           GEN_INT (GET_CODE (rtl) == PRE_INC
11407                                    ? GET_MODE_UNIT_SIZE (mode)
11408                                    : -GET_MODE_UNIT_SIZE (mode)));
11409
11410       /* ... fall through ...  */
11411
11412     case PLUS:
11413     plus:
11414       if (is_based_loc (rtl))
11415         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11416                                           INTVAL (XEXP (rtl, 1)),
11417                                           VAR_INIT_STATUS_INITIALIZED);
11418       else
11419         {
11420           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
11421                                                VAR_INIT_STATUS_INITIALIZED);
11422           if (mem_loc_result == 0)
11423             break;
11424
11425           if (CONST_INT_P (XEXP (rtl, 1)))
11426             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11427           else
11428             {
11429               dw_loc_descr_ref mem_loc_result2
11430                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11431                                       VAR_INIT_STATUS_INITIALIZED);
11432               if (mem_loc_result2 == 0)
11433                 break;
11434               add_loc_descr (&mem_loc_result, mem_loc_result2);
11435               add_loc_descr (&mem_loc_result,
11436                              new_loc_descr (DW_OP_plus, 0, 0));
11437             }
11438         }
11439       break;
11440
11441     /* If a pseudo-reg is optimized away, it is possible for it to
11442        be replaced with a MEM containing a multiply or shift.  */
11443     case MINUS:
11444       op = DW_OP_minus;
11445       goto do_binop;
11446
11447     case MULT:
11448       op = DW_OP_mul;
11449       goto do_binop;
11450
11451     case DIV:
11452       op = DW_OP_div;
11453       goto do_binop;
11454
11455     case MOD:
11456       op = DW_OP_mod;
11457       goto do_binop;
11458
11459     case ASHIFT:
11460       op = DW_OP_shl;
11461       goto do_binop;
11462
11463     case ASHIFTRT:
11464       op = DW_OP_shra;
11465       goto do_binop;
11466
11467     case LSHIFTRT:
11468       op = DW_OP_shr;
11469       goto do_binop;
11470
11471     case AND:
11472       op = DW_OP_and;
11473       goto do_binop;
11474
11475     case IOR:
11476       op = DW_OP_or;
11477       goto do_binop;
11478
11479     case XOR:
11480       op = DW_OP_xor;
11481       goto do_binop;
11482
11483     do_binop:
11484       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11485                                 VAR_INIT_STATUS_INITIALIZED);
11486       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11487                                 VAR_INIT_STATUS_INITIALIZED);
11488
11489       if (op0 == 0 || op1 == 0)
11490         break;
11491
11492       mem_loc_result = op0;
11493       add_loc_descr (&mem_loc_result, op1);
11494       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11495       break;
11496
11497     case NOT:
11498       op = DW_OP_not;
11499       goto do_unop;
11500
11501     case ABS:
11502       op = DW_OP_abs;
11503       goto do_unop;
11504
11505     case NEG:
11506       op = DW_OP_neg;
11507       goto do_unop;
11508
11509     do_unop:
11510       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11511                                 VAR_INIT_STATUS_INITIALIZED);
11512
11513       if (op0 == 0)
11514         break;
11515
11516       mem_loc_result = op0;
11517       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11518       break;
11519
11520     case CONST_INT:
11521       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11522       break;
11523
11524     case EQ:
11525       op = DW_OP_eq;
11526       goto do_scompare;
11527
11528     case GE:
11529       op = DW_OP_ge;
11530       goto do_scompare;
11531
11532     case GT:
11533       op = DW_OP_gt;
11534       goto do_scompare;
11535
11536     case LE:
11537       op = DW_OP_le;
11538       goto do_scompare;
11539
11540     case LT:
11541       op = DW_OP_lt;
11542       goto do_scompare;
11543
11544     case NE:
11545       op = DW_OP_ne;
11546       goto do_scompare;
11547
11548     do_scompare:
11549       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11550           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11551           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11552         break;
11553
11554       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11555                                 VAR_INIT_STATUS_INITIALIZED);
11556       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11557                                 VAR_INIT_STATUS_INITIALIZED);
11558
11559       if (op0 == 0 || op1 == 0)
11560         break;
11561
11562       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11563         {
11564           int shift = DWARF2_ADDR_SIZE
11565                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11566           shift *= BITS_PER_UNIT;
11567           add_loc_descr (&op0, int_loc_descriptor (shift));
11568           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11569           if (CONST_INT_P (XEXP (rtl, 1)))
11570             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11571           else
11572             {
11573               add_loc_descr (&op1, int_loc_descriptor (shift));
11574               add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11575             }
11576         }
11577
11578     do_compare:
11579       mem_loc_result = op0;
11580       add_loc_descr (&mem_loc_result, op1);
11581       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11582       if (STORE_FLAG_VALUE != 1)
11583         {
11584           add_loc_descr (&mem_loc_result,
11585                          int_loc_descriptor (STORE_FLAG_VALUE));
11586           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11587         }
11588       break;
11589
11590     case GEU:
11591       op = DW_OP_ge;
11592       goto do_ucompare;
11593
11594     case GTU:
11595       op = DW_OP_gt;
11596       goto do_ucompare;
11597
11598     case LEU:
11599       op = DW_OP_le;
11600       goto do_ucompare;
11601
11602     case LTU:
11603       op = DW_OP_lt;
11604       goto do_ucompare;
11605
11606     do_ucompare:
11607       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11608           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11609           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11610         break;
11611
11612       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11613                                 VAR_INIT_STATUS_INITIALIZED);
11614       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11615                                 VAR_INIT_STATUS_INITIALIZED);
11616
11617       if (op0 == 0 || op1 == 0)
11618         break;
11619
11620       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11621         {
11622           HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11623           add_loc_descr (&op0, int_loc_descriptor (mask));
11624           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11625           if (CONST_INT_P (XEXP (rtl, 1)))
11626             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11627           else
11628             {
11629               add_loc_descr (&op1, int_loc_descriptor (mask));
11630               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11631             }
11632         }
11633       else
11634         {
11635           HOST_WIDE_INT bias = 1;
11636           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11637           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11638           if (CONST_INT_P (XEXP (rtl, 1)))
11639             op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11640                                       + INTVAL (XEXP (rtl, 1)));
11641           else
11642             add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11643         }
11644       goto do_compare;
11645
11646     case SMIN:
11647     case SMAX:
11648     case UMIN:
11649     case UMAX:
11650       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11651           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11652           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11653         break;
11654
11655       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11656                                 VAR_INIT_STATUS_INITIALIZED);
11657       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11658                                 VAR_INIT_STATUS_INITIALIZED);
11659
11660       if (op0 == 0 || op1 == 0)
11661         break;
11662
11663       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11664       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11665       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11666       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11667         {
11668           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11669             {
11670               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11671               add_loc_descr (&op0, int_loc_descriptor (mask));
11672               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11673               add_loc_descr (&op1, int_loc_descriptor (mask));
11674               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11675             }
11676           else
11677             {
11678               HOST_WIDE_INT bias = 1;
11679               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11680               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11681               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11682             }
11683         }
11684       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11685         {
11686           int shift = DWARF2_ADDR_SIZE
11687                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11688           shift *= BITS_PER_UNIT;
11689           add_loc_descr (&op0, int_loc_descriptor (shift));
11690           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11691           add_loc_descr (&op1, int_loc_descriptor (shift));
11692           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11693         }
11694
11695       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11696         op = DW_OP_lt;
11697       else
11698         op = DW_OP_gt;
11699       mem_loc_result = op0;
11700       add_loc_descr (&mem_loc_result, op1);
11701       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11702       {
11703         dw_loc_descr_ref bra_node, drop_node;
11704
11705         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11706         add_loc_descr (&mem_loc_result, bra_node);
11707         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11708         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11709         add_loc_descr (&mem_loc_result, drop_node);
11710         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11711         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11712       }
11713       break;
11714
11715     case ZERO_EXTRACT:
11716     case SIGN_EXTRACT:
11717       if (CONST_INT_P (XEXP (rtl, 1))
11718           && CONST_INT_P (XEXP (rtl, 2))
11719           && ((unsigned) INTVAL (XEXP (rtl, 1))
11720               + (unsigned) INTVAL (XEXP (rtl, 2))
11721               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
11722           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
11723           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11724         {
11725           int shift, size;
11726           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11727                                     VAR_INIT_STATUS_INITIALIZED);
11728           if (op0 == 0)
11729             break;
11730           if (GET_CODE (rtl) == SIGN_EXTRACT)
11731             op = DW_OP_shra;
11732           else
11733             op = DW_OP_shr;
11734           mem_loc_result = op0;
11735           size = INTVAL (XEXP (rtl, 1));
11736           shift = INTVAL (XEXP (rtl, 2));
11737           if (BITS_BIG_ENDIAN)
11738             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11739                     - shift - size;
11740           add_loc_descr (&mem_loc_result,
11741                          int_loc_descriptor (DWARF2_ADDR_SIZE - shift - size));
11742           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11743           add_loc_descr (&mem_loc_result,
11744                          int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11745           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11746         }
11747       break;
11748
11749     case COMPARE:
11750     case IF_THEN_ELSE:
11751     case ROTATE:
11752     case ROTATERT:
11753     case TRUNCATE:
11754       /* In theory, we could implement the above.  */
11755       /* DWARF cannot represent the unsigned compare operations
11756          natively.  */
11757     case SS_MULT:
11758     case US_MULT:
11759     case SS_DIV:
11760     case US_DIV:
11761     case UDIV:
11762     case UMOD:
11763     case UNORDERED:
11764     case ORDERED:
11765     case UNEQ:
11766     case UNGE:
11767     case UNLE:
11768     case UNLT:
11769     case LTGT:
11770     case FLOAT_EXTEND:
11771     case FLOAT_TRUNCATE:
11772     case FLOAT:
11773     case UNSIGNED_FLOAT:
11774     case FIX:
11775     case UNSIGNED_FIX:
11776     case FRACT_CONVERT:
11777     case UNSIGNED_FRACT_CONVERT:
11778     case SAT_FRACT:
11779     case UNSIGNED_SAT_FRACT:
11780     case SQRT:
11781     case BSWAP:
11782     case FFS:
11783     case CLZ:
11784     case CTZ:
11785     case POPCOUNT:
11786     case PARITY:
11787     case ASM_OPERANDS:
11788     case UNSPEC:
11789     case HIGH:
11790       /* If delegitimize_address couldn't do anything with the UNSPEC, we
11791          can't express it in the debug info.  This can happen e.g. with some
11792          TLS UNSPECs.  */
11793       break;
11794
11795     case CONST_STRING:
11796       resolve_one_addr (&rtl, NULL);
11797       goto symref;
11798
11799     default:
11800 #ifdef ENABLE_CHECKING
11801       print_rtl (stderr, rtl);
11802       gcc_unreachable ();
11803 #else
11804       break;
11805 #endif
11806     }
11807
11808   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11809     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11810
11811   return mem_loc_result;
11812 }
11813
11814 /* Return a descriptor that describes the concatenation of two locations.
11815    This is typically a complex variable.  */
11816
11817 static dw_loc_descr_ref
11818 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
11819 {
11820   dw_loc_descr_ref cc_loc_result = NULL;
11821   dw_loc_descr_ref x0_ref
11822     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11823   dw_loc_descr_ref x1_ref
11824     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11825
11826   if (x0_ref == 0 || x1_ref == 0)
11827     return 0;
11828
11829   cc_loc_result = x0_ref;
11830   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
11831
11832   add_loc_descr (&cc_loc_result, x1_ref);
11833   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
11834
11835   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11836     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11837
11838   return cc_loc_result;
11839 }
11840
11841 /* Return a descriptor that describes the concatenation of N
11842    locations.  */
11843
11844 static dw_loc_descr_ref
11845 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
11846 {
11847   unsigned int i;
11848   dw_loc_descr_ref cc_loc_result = NULL;
11849   unsigned int n = XVECLEN (concatn, 0);
11850
11851   for (i = 0; i < n; ++i)
11852     {
11853       dw_loc_descr_ref ref;
11854       rtx x = XVECEXP (concatn, 0, i);
11855
11856       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11857       if (ref == NULL)
11858         return NULL;
11859
11860       add_loc_descr (&cc_loc_result, ref);
11861       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
11862     }
11863
11864   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11865     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11866
11867   return cc_loc_result;
11868 }
11869
11870 /* Output a proper Dwarf location descriptor for a variable or parameter
11871    which is either allocated in a register or in a memory location.  For a
11872    register, we just generate an OP_REG and the register number.  For a
11873    memory location we provide a Dwarf postfix expression describing how to
11874    generate the (dynamic) address of the object onto the address stack.
11875
11876    MODE is mode of the decl if this loc_descriptor is going to be used in
11877    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
11878    allowed, VOIDmode otherwise.
11879
11880    If we don't know how to describe it, return 0.  */
11881
11882 static dw_loc_descr_ref
11883 loc_descriptor (rtx rtl, enum machine_mode mode,
11884                 enum var_init_status initialized)
11885 {
11886   dw_loc_descr_ref loc_result = NULL;
11887
11888   switch (GET_CODE (rtl))
11889     {
11890     case SUBREG:
11891       /* The case of a subreg may arise when we have a local (register)
11892          variable or a formal (register) parameter which doesn't quite fill
11893          up an entire register.  For now, just assume that it is
11894          legitimate to make the Dwarf info refer to the whole register which
11895          contains the given subreg.  */
11896       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
11897       break;
11898
11899     case REG:
11900       loc_result = reg_loc_descriptor (rtl, initialized);
11901       break;
11902
11903     case SIGN_EXTEND:
11904     case ZERO_EXTEND:
11905       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
11906       break;
11907
11908     case MEM:
11909       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11910                                        initialized);
11911       if (loc_result == NULL)
11912         loc_result = tls_mem_loc_descriptor (rtl);
11913       break;
11914
11915     case CONCAT:
11916       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
11917                                           initialized);
11918       break;
11919
11920     case CONCATN:
11921       loc_result = concatn_loc_descriptor (rtl, initialized);
11922       break;
11923
11924     case VAR_LOCATION:
11925       /* Single part.  */
11926       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
11927         {
11928           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), mode,
11929                                        initialized);
11930           break;
11931         }
11932
11933       rtl = XEXP (rtl, 1);
11934       /* FALLTHRU */
11935
11936     case PARALLEL:
11937       {
11938         rtvec par_elems = XVEC (rtl, 0);
11939         int num_elem = GET_NUM_ELEM (par_elems);
11940         enum machine_mode mode;
11941         int i;
11942
11943         /* Create the first one, so we have something to add to.  */
11944         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
11945                                      VOIDmode, initialized);
11946         if (loc_result == NULL)
11947           return NULL;
11948         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
11949         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11950         for (i = 1; i < num_elem; i++)
11951           {
11952             dw_loc_descr_ref temp;
11953
11954             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
11955                                    VOIDmode, initialized);
11956             if (temp == NULL)
11957               return NULL;
11958             add_loc_descr (&loc_result, temp);
11959             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
11960             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11961           }
11962       }
11963       break;
11964
11965     case CONST_INT:
11966       if (mode != VOIDmode && mode != BLKmode)
11967         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
11968                                                     INTVAL (rtl));
11969       break;
11970
11971     case CONST_DOUBLE:
11972       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
11973         {
11974           /* Note that a CONST_DOUBLE rtx could represent either an integer
11975              or a floating-point constant.  A CONST_DOUBLE is used whenever
11976              the constant requires more than one word in order to be
11977              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11978           if (GET_MODE (rtl) != VOIDmode)
11979             mode = GET_MODE (rtl);
11980
11981           loc_result = new_loc_descr (DW_OP_implicit_value,
11982                                       GET_MODE_SIZE (mode), 0);
11983           if (SCALAR_FLOAT_MODE_P (mode))
11984             {
11985               unsigned int length = GET_MODE_SIZE (mode);
11986               unsigned char *array = GGC_NEWVEC (unsigned char, length);
11987
11988               insert_float (rtl, array);
11989               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11990               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11991               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11992               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11993             }
11994           else
11995             {
11996               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
11997               loc_result->dw_loc_oprnd2.v.val_double.high
11998                 = CONST_DOUBLE_HIGH (rtl);
11999               loc_result->dw_loc_oprnd2.v.val_double.low
12000                 = CONST_DOUBLE_LOW (rtl);
12001             }
12002         }
12003       break;
12004
12005     case CONST_VECTOR:
12006       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12007         {
12008           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12009           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12010           unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
12011           unsigned int i;
12012           unsigned char *p;
12013
12014           mode = GET_MODE (rtl);
12015           switch (GET_MODE_CLASS (mode))
12016             {
12017             case MODE_VECTOR_INT:
12018               for (i = 0, p = array; i < length; i++, p += elt_size)
12019                 {
12020                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12021                   HOST_WIDE_INT lo, hi;
12022
12023                   switch (GET_CODE (elt))
12024                     {
12025                     case CONST_INT:
12026                       lo = INTVAL (elt);
12027                       hi = -(lo < 0);
12028                       break;
12029
12030                     case CONST_DOUBLE:
12031                       lo = CONST_DOUBLE_LOW (elt);
12032                       hi = CONST_DOUBLE_HIGH (elt);
12033                       break;
12034
12035                     default:
12036                       gcc_unreachable ();
12037                     }
12038
12039                   if (elt_size <= sizeof (HOST_WIDE_INT))
12040                     insert_int (lo, elt_size, p);
12041                   else
12042                     {
12043                       unsigned char *p0 = p;
12044                       unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
12045
12046                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12047                       if (WORDS_BIG_ENDIAN)
12048                         {
12049                           p0 = p1;
12050                           p1 = p;
12051                         }
12052                       insert_int (lo, sizeof (HOST_WIDE_INT), p0);
12053                       insert_int (hi, sizeof (HOST_WIDE_INT), p1);
12054                     }
12055                 }
12056               break;
12057
12058             case MODE_VECTOR_FLOAT:
12059               for (i = 0, p = array; i < length; i++, p += elt_size)
12060                 {
12061                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12062                   insert_float (elt, p);
12063                 }
12064               break;
12065
12066             default:
12067               gcc_unreachable ();
12068             }
12069
12070           loc_result = new_loc_descr (DW_OP_implicit_value,
12071                                       length * elt_size, 0);
12072           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12073           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12074           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12075           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12076         }
12077       break;
12078
12079     case CONST:
12080       if (mode == VOIDmode
12081           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12082           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12083           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12084         {
12085           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12086           break;
12087         }
12088       /* FALLTHROUGH */
12089     case SYMBOL_REF:
12090       if (!const_ok_for_output (rtl))
12091         break;
12092     case LABEL_REF:
12093       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12094           && (dwarf_version >= 4 || !dwarf_strict))
12095         {
12096           loc_result = new_loc_descr (DW_OP_implicit_value,
12097                                       DWARF2_ADDR_SIZE, 0);
12098           loc_result->dw_loc_oprnd2.val_class = dw_val_class_addr;
12099           loc_result->dw_loc_oprnd2.v.val_addr = rtl;
12100           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12101         }
12102       break;
12103
12104     default:
12105       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12106           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12107           && (dwarf_version >= 4 || !dwarf_strict))
12108         {
12109           /* Value expression.  */
12110           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
12111           if (loc_result)
12112             add_loc_descr (&loc_result,
12113                            new_loc_descr (DW_OP_stack_value, 0, 0));
12114         }
12115       break;
12116     }
12117
12118   return loc_result;
12119 }
12120
12121 /* We need to figure out what section we should use as the base for the
12122    address ranges where a given location is valid.
12123    1. If this particular DECL has a section associated with it, use that.
12124    2. If this function has a section associated with it, use that.
12125    3. Otherwise, use the text section.
12126    XXX: If you split a variable across multiple sections, we won't notice.  */
12127
12128 static const char *
12129 secname_for_decl (const_tree decl)
12130 {
12131   const char *secname;
12132
12133   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12134     {
12135       tree sectree = DECL_SECTION_NAME (decl);
12136       secname = TREE_STRING_POINTER (sectree);
12137     }
12138   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12139     {
12140       tree sectree = DECL_SECTION_NAME (current_function_decl);
12141       secname = TREE_STRING_POINTER (sectree);
12142     }
12143   else if (cfun && in_cold_section_p)
12144     secname = crtl->subsections.cold_section_label;
12145   else
12146     secname = text_section_label;
12147
12148   return secname;
12149 }
12150
12151 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12152
12153 static bool
12154 decl_by_reference_p (tree decl)
12155 {
12156   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12157            || TREE_CODE (decl) == VAR_DECL)
12158           && DECL_BY_REFERENCE (decl));
12159 }
12160
12161
12162 /* Dereference a location expression LOC if DECL is passed by invisible
12163    reference.  */
12164
12165 static dw_loc_descr_ref
12166 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12167 {
12168   HOST_WIDE_INT size;
12169   enum dwarf_location_atom op;
12170
12171   if (loc == NULL)
12172     return NULL;
12173
12174   if (!decl_by_reference_p (decl))
12175     return loc;
12176
12177   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12178      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
12179      location expression is considered to be address of a memory location,
12180      rather than the register itself.  */
12181   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12182        || loc->dw_loc_opc == DW_OP_regx)
12183       && (loc->dw_loc_next == NULL
12184           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12185               && loc->dw_loc_next->dw_loc_next == NULL)))
12186     {
12187       if (loc->dw_loc_opc == DW_OP_regx)
12188         {
12189           loc->dw_loc_opc = DW_OP_bregx;
12190           loc->dw_loc_oprnd2.v.val_int = 0;
12191         }
12192       else
12193         {
12194           loc->dw_loc_opc
12195             = (enum dwarf_location_atom)
12196               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12197           loc->dw_loc_oprnd1.v.val_int = 0;
12198         }
12199       return loc;
12200     }
12201
12202   size = int_size_in_bytes (TREE_TYPE (decl));
12203   if (size > DWARF2_ADDR_SIZE || size == -1)
12204     return 0;
12205   else if (size == DWARF2_ADDR_SIZE)
12206     op = DW_OP_deref;
12207   else
12208     op = DW_OP_deref_size;
12209   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12210   return loc;
12211 }
12212
12213 /* Return single element location list containing loc descr REF.  */
12214
12215 static dw_loc_list_ref
12216 single_element_loc_list (dw_loc_descr_ref ref)
12217 {
12218   return new_loc_list (ref, NULL, NULL, NULL, 0);
12219 }
12220
12221 /* Return dwarf representation of location list representing for
12222    LOC_LIST of DECL.  */
12223
12224 static dw_loc_list_ref
12225 dw_loc_list (var_loc_list * loc_list, tree decl, bool toplevel)
12226 {
12227   const char *endname, *secname;
12228   dw_loc_list_ref list;
12229   rtx varloc;
12230   enum var_init_status initialized;
12231   struct var_loc_node *node;
12232   dw_loc_descr_ref descr;
12233   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12234
12235   bool by_reference = decl_by_reference_p (decl);
12236
12237   /* Now that we know what section we are using for a base,
12238      actually construct the list of locations.
12239      The first location information is what is passed to the
12240      function that creates the location list, and the remaining
12241      locations just get added on to that list.
12242      Note that we only know the start address for a location
12243      (IE location changes), so to build the range, we use
12244      the range [current location start, next location start].
12245      This means we have to special case the last node, and generate
12246      a range of [last location start, end of function label].  */
12247
12248   node = loc_list->first;
12249   varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12250   secname = secname_for_decl (decl);
12251
12252   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12253     initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12254   else
12255     initialized = VAR_INIT_STATUS_INITIALIZED;
12256
12257   if (!toplevel || by_reference)
12258     {
12259       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12260       /* Single part.  */
12261       if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
12262         descr = loc_by_reference (mem_loc_descriptor (XEXP (XEXP (varloc, 1), 0),
12263                                                       TYPE_MODE (TREE_TYPE (decl)),
12264                                                       initialized),
12265                                   decl);
12266       else
12267         descr = NULL;
12268     }
12269   else
12270     descr = loc_descriptor (varloc, DECL_MODE (decl), initialized);
12271
12272   if (loc_list && loc_list->first != loc_list->last)
12273     list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12274   else
12275     return single_element_loc_list (descr);
12276   node = node->next;
12277
12278   if (!node)
12279     return NULL;
12280
12281   for (; node->next; node = node->next)
12282     if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12283       {
12284         /* The variable has a location between NODE->LABEL and
12285            NODE->NEXT->LABEL.  */
12286         enum var_init_status initialized =
12287           NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12288         varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12289         if (!toplevel || by_reference)
12290           {
12291             gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12292             /* Single part.  */
12293             if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
12294               descr = mem_loc_descriptor (XEXP (XEXP (varloc, 1), 0),
12295                                           TYPE_MODE (TREE_TYPE (decl)), initialized);
12296             else
12297               descr = NULL;
12298             descr = loc_by_reference (descr, decl);
12299           }
12300         else
12301           descr = loc_descriptor (varloc, DECL_MODE (decl), initialized);
12302         add_loc_descr_to_loc_list (&list, descr,
12303                                    node->label, node->next->label, secname);
12304       }
12305
12306   /* If the variable has a location at the last label
12307      it keeps its location until the end of function.  */
12308   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12309     {
12310       enum var_init_status initialized =
12311         NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12312
12313       if (!current_function_decl)
12314         endname = text_end_label;
12315       else
12316         {
12317           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12318                                        current_function_funcdef_no);
12319           endname = ggc_strdup (label_id);
12320         }
12321
12322       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12323       if (!toplevel || by_reference)
12324         {
12325           gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12326           /* Single part.  */
12327           if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
12328             descr = mem_loc_descriptor (XEXP (XEXP (varloc, 1), 0),
12329                                         TYPE_MODE (TREE_TYPE (decl)), initialized);
12330           else
12331             descr = NULL;
12332           descr = loc_by_reference (descr, decl);
12333         }
12334       else
12335         descr = loc_descriptor (varloc, DECL_MODE (decl), initialized);
12336       add_loc_descr_to_loc_list (&list, descr, node->label, endname, secname);
12337     }
12338   return list;
12339 }
12340
12341 /* Return if the loc_list has only single element and thus can be represented
12342    as location description.   */
12343
12344 static bool
12345 single_element_loc_list_p (dw_loc_list_ref list)
12346 {
12347   return (!list->dw_loc_next && !list->begin && !list->end);
12348 }
12349
12350 /* To each location in list LIST add loc descr REF.  */
12351
12352 static void
12353 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
12354 {
12355   dw_loc_descr_ref copy;
12356   add_loc_descr (&list->expr, ref);
12357   list = list->dw_loc_next;
12358   while (list)
12359     {
12360       copy = GGC_CNEW (dw_loc_descr_node);
12361       memcpy (copy, ref, sizeof (dw_loc_descr_node));
12362       add_loc_descr (&list->expr, copy);
12363       while (copy->dw_loc_next)
12364         {
12365           dw_loc_descr_ref new_copy = GGC_CNEW (dw_loc_descr_node);
12366           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
12367           copy->dw_loc_next = new_copy;
12368           copy = new_copy;
12369         }
12370       list = list->dw_loc_next;
12371     }
12372 }
12373
12374 /* Given two lists RET and LIST
12375    produce location list that is result of adding expression in LIST
12376    to expression in RET on each possition in program.
12377    Might be destructive on both RET and LIST.
12378
12379    TODO: We handle only simple cases of RET or LIST having at most one
12380    element. General case would inolve sorting the lists in program order
12381    and merging them that will need some additional work.  
12382    Adding that will improve quality of debug info especially for SRA-ed
12383    structures.  */
12384
12385 static void
12386 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
12387 {
12388   if (!list)
12389     return;
12390   if (!*ret)
12391     {
12392       *ret = list;
12393       return;
12394     }
12395   if (!list->dw_loc_next)
12396     {
12397       add_loc_descr_to_each (*ret, list->expr);
12398       return;
12399     }
12400   if (!(*ret)->dw_loc_next)
12401     {
12402       add_loc_descr_to_each (list, (*ret)->expr);
12403       *ret = list;
12404       return;
12405     }
12406   expansion_failed (NULL_TREE, NULL_RTX,
12407                     "Don't know how to merge two non-trivial"
12408                     " location lists.\n");
12409   *ret = NULL;
12410   return;
12411 }
12412
12413 /* LOC is constant expression.  Try a luck, look it up in constant
12414    pool and return its loc_descr of its address.  */
12415
12416 static dw_loc_descr_ref
12417 cst_pool_loc_descr (tree loc)
12418 {
12419   /* Get an RTL for this, if something has been emitted.  */
12420   rtx rtl = lookup_constant_def (loc);
12421   enum machine_mode mode;
12422
12423   if (!rtl || !MEM_P (rtl))
12424     {
12425       gcc_assert (!rtl);
12426       return 0;
12427     }
12428   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
12429
12430   /* TODO: We might get more coverage if we was actually delaying expansion
12431      of all expressions till end of compilation when constant pools are fully
12432      populated.  */
12433   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
12434     {
12435       expansion_failed (loc, NULL_RTX,
12436                         "CST value in contant pool but not marked.");
12437       return 0;
12438     }
12439   mode = GET_MODE (rtl);
12440   rtl = XEXP (rtl, 0);
12441   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12442 }
12443
12444 /* Return dw_loc_list representing address of addr_expr LOC
12445    by looking for innder INDIRECT_REF expression and turing it
12446    into simple arithmetics.  */
12447
12448 static dw_loc_list_ref
12449 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
12450 {
12451   tree obj, offset;
12452   HOST_WIDE_INT bitsize, bitpos, bytepos;
12453   enum machine_mode mode;
12454   int volatilep;
12455   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
12456   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12457
12458   obj = get_inner_reference (TREE_OPERAND (loc, 0),
12459                              &bitsize, &bitpos, &offset, &mode,
12460                              &unsignedp, &volatilep, false);
12461   STRIP_NOPS (obj);
12462   if (bitpos % BITS_PER_UNIT)
12463     {
12464       expansion_failed (loc, NULL_RTX, "bitfield access");
12465       return 0;
12466     }
12467   if (!INDIRECT_REF_P (obj))
12468     {
12469       expansion_failed (obj,
12470                         NULL_RTX, "no indirect ref in inner refrence");
12471       return 0;
12472     }
12473   if (!offset && !bitpos)
12474     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
12475   else if (toplev
12476            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
12477            && (dwarf_version >= 4 || !dwarf_strict))
12478     {
12479       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
12480       if (!list_ret)
12481         return 0;
12482       if (offset)
12483         {
12484           /* Variable offset.  */
12485           list_ret1 = loc_list_from_tree (offset, 0);
12486           if (list_ret1 == 0)
12487             return 0;
12488           add_loc_list (&list_ret, list_ret1);
12489           if (!list_ret)
12490             return 0;
12491           add_loc_descr_to_each (list_ret,
12492                                  new_loc_descr (DW_OP_plus, 0, 0));
12493         }
12494       bytepos = bitpos / BITS_PER_UNIT;
12495       if (bytepos > 0)
12496         add_loc_descr_to_each (list_ret,
12497                                new_loc_descr (DW_OP_plus_uconst,
12498                                               bytepos, 0));
12499       else if (bytepos < 0)
12500         loc_list_plus_const (list_ret, bytepos);
12501       add_loc_descr_to_each (list_ret,
12502                              new_loc_descr (DW_OP_stack_value, 0, 0));
12503     }
12504   return list_ret;
12505 }
12506
12507
12508 /* Generate Dwarf location list representing LOC.
12509    If WANT_ADDRESS is false, expression computing LOC will be computed
12510    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
12511    if WANT_ADDRESS is 2, expression computing address useable in location
12512      will be returned (i.e. DW_OP_reg can be used
12513      to refer to register values) 
12514    TODO: Dwarf4 adds types to the stack machine that ought to be used here
12515    DW_OP_stack_value will help in cases where we fail to find address of the
12516    expression.
12517  */
12518
12519 static dw_loc_list_ref
12520 loc_list_from_tree (tree loc, int want_address)
12521 {
12522   dw_loc_descr_ref ret = NULL, ret1 = NULL;
12523   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12524   int have_address = 0;
12525   enum dwarf_location_atom op;
12526
12527   /* ??? Most of the time we do not take proper care for sign/zero
12528      extending the values properly.  Hopefully this won't be a real
12529      problem...  */
12530
12531   switch (TREE_CODE (loc))
12532     {
12533     case ERROR_MARK:
12534       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
12535       return 0;
12536
12537     case PLACEHOLDER_EXPR:
12538       /* This case involves extracting fields from an object to determine the
12539          position of other fields.  We don't try to encode this here.  The
12540          only user of this is Ada, which encodes the needed information using
12541          the names of types.  */
12542       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
12543       return 0;
12544
12545     case CALL_EXPR:
12546       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
12547       /* There are no opcodes for these operations.  */
12548       return 0;
12549
12550     case PREINCREMENT_EXPR:
12551     case PREDECREMENT_EXPR:
12552     case POSTINCREMENT_EXPR:
12553     case POSTDECREMENT_EXPR:
12554       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
12555       /* There are no opcodes for these operations.  */
12556       return 0;
12557
12558     case ADDR_EXPR:
12559       /* If we already want an address, see if there is INDIRECT_REF inside
12560          e.g. for &this->field.  */
12561       if (want_address)
12562         {
12563           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
12564                        (loc, want_address == 2);
12565           if (list_ret)
12566             have_address = 1;
12567           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
12568                    && (ret = cst_pool_loc_descr (loc)))
12569             have_address = 1;
12570         }
12571         /* Otherwise, process the argument and look for the address.  */
12572       if (!list_ret && !ret)
12573         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
12574       else
12575         {
12576           if (want_address)
12577             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
12578           return NULL;
12579         }
12580       break;
12581
12582     case VAR_DECL:
12583       if (DECL_THREAD_LOCAL_P (loc))
12584         {
12585           rtx rtl;
12586           enum dwarf_location_atom first_op;
12587           enum dwarf_location_atom second_op;
12588           bool dtprel = false;
12589
12590           if (targetm.have_tls)
12591             {
12592               /* If this is not defined, we have no way to emit the
12593                  data.  */
12594               if (!targetm.asm_out.output_dwarf_dtprel)
12595                 return 0;
12596
12597                /* The way DW_OP_GNU_push_tls_address is specified, we
12598                   can only look up addresses of objects in the current
12599                   module.  */
12600               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
12601                 return 0;
12602               first_op = DW_OP_addr;
12603               dtprel = true;
12604               second_op = DW_OP_GNU_push_tls_address;
12605             }
12606           else
12607             {
12608               if (!targetm.emutls.debug_form_tls_address
12609                   || !(dwarf_version >= 3 || !dwarf_strict))
12610                 return 0;
12611               loc = emutls_decl (loc);
12612               first_op = DW_OP_addr;
12613               second_op = DW_OP_form_tls_address;
12614             }
12615
12616           rtl = rtl_for_decl_location (loc);
12617           if (rtl == NULL_RTX)
12618             return 0;
12619
12620           if (!MEM_P (rtl))
12621             return 0;
12622           rtl = XEXP (rtl, 0);
12623           if (! CONSTANT_P (rtl))
12624             return 0;
12625
12626           ret = new_loc_descr (first_op, 0, 0);
12627           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12628           ret->dw_loc_oprnd1.v.val_addr = rtl;
12629           ret->dtprel = dtprel;
12630
12631           ret1 = new_loc_descr (second_op, 0, 0);
12632           add_loc_descr (&ret, ret1);
12633
12634           have_address = 1;
12635           break;
12636         }
12637       /* FALLTHRU */
12638
12639     case PARM_DECL:
12640       if (DECL_HAS_VALUE_EXPR_P (loc))
12641         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
12642                                    want_address);
12643       /* FALLTHRU */
12644
12645     case RESULT_DECL:
12646     case FUNCTION_DECL:
12647       {
12648         rtx rtl = rtl_for_decl_location (loc);
12649         var_loc_list *loc_list = lookup_decl_loc (loc);
12650
12651         if (loc_list && loc_list->first
12652             && (list_ret = dw_loc_list (loc_list, loc, want_address == 2)))
12653           have_address = 1;
12654         else if (rtl == NULL_RTX)
12655           {
12656             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
12657             return 0;
12658           }
12659         else if (CONST_INT_P (rtl))
12660           {
12661             HOST_WIDE_INT val = INTVAL (rtl);
12662             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
12663               val &= GET_MODE_MASK (DECL_MODE (loc));
12664             ret = int_loc_descriptor (val);
12665           }
12666         else if (GET_CODE (rtl) == CONST_STRING)
12667           {
12668             expansion_failed (loc, NULL_RTX, "CONST_STRING");
12669             return 0;
12670           }
12671         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
12672           {
12673             ret = new_loc_descr (DW_OP_addr, 0, 0);
12674             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12675             ret->dw_loc_oprnd1.v.val_addr = rtl;
12676           }
12677         else
12678           {
12679             enum machine_mode mode;
12680
12681             /* Certain constructs can only be represented at top-level.  */
12682             if (want_address == 2)
12683               {
12684                 ret = loc_descriptor (rtl, VOIDmode,
12685                                       VAR_INIT_STATUS_INITIALIZED);
12686                 have_address = 1;
12687               }
12688             else
12689               {
12690                 mode = GET_MODE (rtl);
12691                 if (MEM_P (rtl))
12692                   {
12693                     rtl = XEXP (rtl, 0);
12694                     have_address = 1;
12695                   }
12696                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12697               }
12698             if (!ret)
12699               expansion_failed (loc, rtl,
12700                                 "failed to produce loc descriptor for rtl");
12701           }
12702       }
12703       break;
12704
12705     case INDIRECT_REF:
12706     case ALIGN_INDIRECT_REF:
12707     case MISALIGNED_INDIRECT_REF:
12708       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12709       have_address = 1;
12710       break;
12711
12712     case COMPOUND_EXPR:
12713       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
12714
12715     CASE_CONVERT:
12716     case VIEW_CONVERT_EXPR:
12717     case SAVE_EXPR:
12718     case MODIFY_EXPR:
12719       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
12720
12721     case COMPONENT_REF:
12722     case BIT_FIELD_REF:
12723     case ARRAY_REF:
12724     case ARRAY_RANGE_REF:
12725     case REALPART_EXPR:
12726     case IMAGPART_EXPR:
12727       {
12728         tree obj, offset;
12729         HOST_WIDE_INT bitsize, bitpos, bytepos;
12730         enum machine_mode mode;
12731         int volatilep;
12732         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
12733
12734         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
12735                                    &unsignedp, &volatilep, false);
12736
12737         gcc_assert (obj != loc);
12738
12739         list_ret = loc_list_from_tree (obj,
12740                                        want_address == 2
12741                                        && !bitpos && !offset ? 2 : 1);
12742         /* TODO: We can extract value of the small expression via shifting even
12743            for nonzero bitpos.  */
12744         if (list_ret == 0)
12745           return 0;
12746         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
12747           {
12748             expansion_failed (loc, NULL_RTX,
12749                               "bitfield access");
12750             return 0;
12751           }
12752
12753         if (offset != NULL_TREE)
12754           {
12755             /* Variable offset.  */
12756             list_ret1 = loc_list_from_tree (offset, 0);
12757             if (list_ret1 == 0)
12758               return 0;
12759             add_loc_list (&list_ret, list_ret1);
12760             if (!list_ret)
12761               return 0;
12762             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
12763           }
12764
12765         bytepos = bitpos / BITS_PER_UNIT;
12766         if (bytepos > 0)
12767           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
12768         else if (bytepos < 0)
12769           loc_list_plus_const (list_ret, bytepos); 
12770
12771         have_address = 1;
12772         break;
12773       }
12774
12775     case INTEGER_CST:
12776       if ((want_address || !host_integerp (loc, 0))
12777           && (ret = cst_pool_loc_descr (loc)))
12778         have_address = 1;
12779       else if (want_address == 2
12780                && host_integerp (loc, 0)
12781                && (ret = address_of_int_loc_descriptor
12782                            (int_size_in_bytes (TREE_TYPE (loc)),
12783                             tree_low_cst (loc, 0))))
12784         have_address = 1;
12785       else if (host_integerp (loc, 0))
12786         ret = int_loc_descriptor (tree_low_cst (loc, 0));
12787       else
12788         {
12789           expansion_failed (loc, NULL_RTX,
12790                             "Integer operand is not host integer");
12791           return 0;
12792         }
12793       break;
12794
12795     case CONSTRUCTOR:
12796     case REAL_CST:
12797     case STRING_CST:
12798     case COMPLEX_CST:
12799       if ((ret = cst_pool_loc_descr (loc)))
12800         have_address = 1;
12801       else
12802       /* We can construct small constants here using int_loc_descriptor.  */
12803         expansion_failed (loc, NULL_RTX,
12804                           "constructor or constant not in constant pool");
12805       break;
12806
12807     case TRUTH_AND_EXPR:
12808     case TRUTH_ANDIF_EXPR:
12809     case BIT_AND_EXPR:
12810       op = DW_OP_and;
12811       goto do_binop;
12812
12813     case TRUTH_XOR_EXPR:
12814     case BIT_XOR_EXPR:
12815       op = DW_OP_xor;
12816       goto do_binop;
12817
12818     case TRUTH_OR_EXPR:
12819     case TRUTH_ORIF_EXPR:
12820     case BIT_IOR_EXPR:
12821       op = DW_OP_or;
12822       goto do_binop;
12823
12824     case FLOOR_DIV_EXPR:
12825     case CEIL_DIV_EXPR:
12826     case ROUND_DIV_EXPR:
12827     case TRUNC_DIV_EXPR:
12828       op = DW_OP_div;
12829       goto do_binop;
12830
12831     case MINUS_EXPR:
12832       op = DW_OP_minus;
12833       goto do_binop;
12834
12835     case FLOOR_MOD_EXPR:
12836     case CEIL_MOD_EXPR:
12837     case ROUND_MOD_EXPR:
12838     case TRUNC_MOD_EXPR:
12839       op = DW_OP_mod;
12840       goto do_binop;
12841
12842     case MULT_EXPR:
12843       op = DW_OP_mul;
12844       goto do_binop;
12845
12846     case LSHIFT_EXPR:
12847       op = DW_OP_shl;
12848       goto do_binop;
12849
12850     case RSHIFT_EXPR:
12851       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
12852       goto do_binop;
12853
12854     case POINTER_PLUS_EXPR:
12855     case PLUS_EXPR:
12856       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
12857           && host_integerp (TREE_OPERAND (loc, 1), 0))
12858         {
12859           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12860           if (list_ret == 0)
12861             return 0;
12862
12863           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
12864           break;
12865         }
12866
12867       op = DW_OP_plus;
12868       goto do_binop;
12869
12870     case LE_EXPR:
12871       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12872         return 0;
12873
12874       op = DW_OP_le;
12875       goto do_binop;
12876
12877     case GE_EXPR:
12878       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12879         return 0;
12880
12881       op = DW_OP_ge;
12882       goto do_binop;
12883
12884     case LT_EXPR:
12885       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12886         return 0;
12887
12888       op = DW_OP_lt;
12889       goto do_binop;
12890
12891     case GT_EXPR:
12892       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12893         return 0;
12894
12895       op = DW_OP_gt;
12896       goto do_binop;
12897
12898     case EQ_EXPR:
12899       op = DW_OP_eq;
12900       goto do_binop;
12901
12902     case NE_EXPR:
12903       op = DW_OP_ne;
12904       goto do_binop;
12905
12906     do_binop:
12907       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12908       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
12909       if (list_ret == 0 || list_ret1 == 0)
12910         return 0;
12911
12912       add_loc_list (&list_ret, list_ret1);
12913       if (list_ret == 0)
12914         return 0;
12915       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
12916       break;
12917
12918     case TRUTH_NOT_EXPR:
12919     case BIT_NOT_EXPR:
12920       op = DW_OP_not;
12921       goto do_unop;
12922
12923     case ABS_EXPR:
12924       op = DW_OP_abs;
12925       goto do_unop;
12926
12927     case NEGATE_EXPR:
12928       op = DW_OP_neg;
12929       goto do_unop;
12930
12931     do_unop:
12932       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12933       if (list_ret == 0)
12934         return 0;
12935
12936       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
12937       break;
12938
12939     case MIN_EXPR:
12940     case MAX_EXPR:
12941       {
12942         const enum tree_code code =
12943           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
12944
12945         loc = build3 (COND_EXPR, TREE_TYPE (loc),
12946                       build2 (code, integer_type_node,
12947                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
12948                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
12949       }
12950
12951       /* ... fall through ...  */
12952
12953     case COND_EXPR:
12954       {
12955         dw_loc_descr_ref lhs
12956           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
12957         dw_loc_list_ref rhs
12958           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
12959         dw_loc_descr_ref bra_node, jump_node, tmp;
12960
12961         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12962         if (list_ret == 0 || lhs == 0 || rhs == 0)
12963           return 0;
12964
12965         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12966         add_loc_descr_to_each (list_ret, bra_node);
12967
12968         add_loc_list (&list_ret, rhs);
12969         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
12970         add_loc_descr_to_each (list_ret, jump_node);
12971
12972         add_loc_descr_to_each (list_ret, lhs);
12973         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12974         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
12975
12976         /* ??? Need a node to point the skip at.  Use a nop.  */
12977         tmp = new_loc_descr (DW_OP_nop, 0, 0);
12978         add_loc_descr_to_each (list_ret, tmp);
12979         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12980         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
12981       }
12982       break;
12983
12984     case FIX_TRUNC_EXPR:
12985       return 0;
12986
12987     default:
12988       /* Leave front-end specific codes as simply unknown.  This comes
12989          up, for instance, with the C STMT_EXPR.  */
12990       if ((unsigned int) TREE_CODE (loc)
12991           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
12992         {
12993           expansion_failed (loc, NULL_RTX,
12994                             "language specific tree node");
12995           return 0;
12996         }
12997
12998 #ifdef ENABLE_CHECKING
12999       /* Otherwise this is a generic code; we should just lists all of
13000          these explicitly.  We forgot one.  */
13001       gcc_unreachable ();
13002 #else
13003       /* In a release build, we want to degrade gracefully: better to
13004          generate incomplete debugging information than to crash.  */
13005       return NULL;
13006 #endif
13007     }
13008
13009   if (!ret && !list_ret)
13010     return 0;
13011
13012   if (want_address == 2 && !have_address
13013       && (dwarf_version >= 4 || !dwarf_strict))
13014     {
13015       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13016         {
13017           expansion_failed (loc, NULL_RTX,
13018                             "DWARF address size mismatch");
13019           return 0;
13020         }
13021       if (ret)
13022         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13023       else
13024         add_loc_descr_to_each (list_ret,
13025                                new_loc_descr (DW_OP_stack_value, 0, 0));
13026       have_address = 1;
13027     }
13028   /* Show if we can't fill the request for an address.  */
13029   if (want_address && !have_address)
13030     {
13031       expansion_failed (loc, NULL_RTX,
13032                         "Want address and only have value");
13033       return 0;
13034     }
13035
13036   gcc_assert (!ret || !list_ret);
13037
13038   /* If we've got an address and don't want one, dereference.  */
13039   if (!want_address && have_address)
13040     {
13041       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13042
13043       if (size > DWARF2_ADDR_SIZE || size == -1)
13044         {
13045           expansion_failed (loc, NULL_RTX,
13046                             "DWARF address size mismatch");
13047           return 0;
13048         }
13049       else if (size == DWARF2_ADDR_SIZE)
13050         op = DW_OP_deref;
13051       else
13052         op = DW_OP_deref_size;
13053
13054       if (ret)
13055         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13056       else
13057         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13058     }
13059   if (ret)
13060     list_ret = single_element_loc_list (ret);
13061
13062   return list_ret;
13063 }
13064
13065 /* Same as above but return only single location expression.  */
13066 static dw_loc_descr_ref
13067 loc_descriptor_from_tree (tree loc, int want_address)
13068 {
13069   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13070   if (!ret)
13071     return NULL;
13072   if (ret->dw_loc_next)
13073     {
13074       expansion_failed (loc, NULL_RTX,
13075                         "Location list where only loc descriptor needed");
13076       return NULL;
13077     }
13078   return ret->expr;
13079 }
13080
13081 /* Given a value, round it up to the lowest multiple of `boundary'
13082    which is not less than the value itself.  */
13083
13084 static inline HOST_WIDE_INT
13085 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13086 {
13087   return (((value + boundary - 1) / boundary) * boundary);
13088 }
13089
13090 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13091    pointer to the declared type for the relevant field variable, or return
13092    `integer_type_node' if the given node turns out to be an
13093    ERROR_MARK node.  */
13094
13095 static inline tree
13096 field_type (const_tree decl)
13097 {
13098   tree type;
13099
13100   if (TREE_CODE (decl) == ERROR_MARK)
13101     return integer_type_node;
13102
13103   type = DECL_BIT_FIELD_TYPE (decl);
13104   if (type == NULL_TREE)
13105     type = TREE_TYPE (decl);
13106
13107   return type;
13108 }
13109
13110 /* Given a pointer to a tree node, return the alignment in bits for
13111    it, or else return BITS_PER_WORD if the node actually turns out to
13112    be an ERROR_MARK node.  */
13113
13114 static inline unsigned
13115 simple_type_align_in_bits (const_tree type)
13116 {
13117   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13118 }
13119
13120 static inline unsigned
13121 simple_decl_align_in_bits (const_tree decl)
13122 {
13123   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13124 }
13125
13126 /* Return the result of rounding T up to ALIGN.  */
13127
13128 static inline HOST_WIDE_INT
13129 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
13130 {
13131   /* We must be careful if T is negative because HOST_WIDE_INT can be
13132      either "above" or "below" unsigned int as per the C promotion
13133      rules, depending on the host, thus making the signedness of the
13134      direct multiplication and division unpredictable.  */
13135   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
13136
13137   u += align - 1;
13138   u /= align;
13139   u *= align;
13140
13141   return (HOST_WIDE_INT) u;
13142 }
13143
13144 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13145    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13146    or return 0 if we are unable to determine what that offset is, either
13147    because the argument turns out to be a pointer to an ERROR_MARK node, or
13148    because the offset is actually variable.  (We can't handle the latter case
13149    just yet).  */
13150
13151 static HOST_WIDE_INT
13152 field_byte_offset (const_tree decl)
13153 {
13154   HOST_WIDE_INT object_offset_in_bits;
13155   HOST_WIDE_INT bitpos_int;
13156
13157   if (TREE_CODE (decl) == ERROR_MARK)
13158     return 0;
13159
13160   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13161
13162   /* We cannot yet cope with fields whose positions are variable, so
13163      for now, when we see such things, we simply return 0.  Someday, we may
13164      be able to handle such cases, but it will be damn difficult.  */
13165   if (! host_integerp (bit_position (decl), 0))
13166     return 0;
13167
13168   bitpos_int = int_bit_position (decl);
13169
13170 #ifdef PCC_BITFIELD_TYPE_MATTERS
13171   if (PCC_BITFIELD_TYPE_MATTERS)
13172     {
13173       tree type;
13174       tree field_size_tree;
13175       HOST_WIDE_INT deepest_bitpos;
13176       unsigned HOST_WIDE_INT field_size_in_bits;
13177       unsigned int type_align_in_bits;
13178       unsigned int decl_align_in_bits;
13179       unsigned HOST_WIDE_INT type_size_in_bits;
13180
13181       type = field_type (decl);
13182       type_size_in_bits = simple_type_size_in_bits (type);
13183       type_align_in_bits = simple_type_align_in_bits (type);
13184
13185       field_size_tree = DECL_SIZE (decl);
13186
13187       /* The size could be unspecified if there was an error, or for
13188          a flexible array member.  */
13189       if (!field_size_tree)
13190         field_size_tree = bitsize_zero_node;
13191
13192       /* If the size of the field is not constant, use the type size.  */
13193       if (host_integerp (field_size_tree, 1))
13194         field_size_in_bits = tree_low_cst (field_size_tree, 1);
13195       else
13196         field_size_in_bits = type_size_in_bits;
13197
13198       decl_align_in_bits = simple_decl_align_in_bits (decl);
13199
13200       /* The GCC front-end doesn't make any attempt to keep track of the
13201          starting bit offset (relative to the start of the containing
13202          structure type) of the hypothetical "containing object" for a
13203          bit-field.  Thus, when computing the byte offset value for the
13204          start of the "containing object" of a bit-field, we must deduce
13205          this information on our own. This can be rather tricky to do in
13206          some cases.  For example, handling the following structure type
13207          definition when compiling for an i386/i486 target (which only
13208          aligns long long's to 32-bit boundaries) can be very tricky:
13209
13210          struct S { int field1; long long field2:31; };
13211
13212          Fortunately, there is a simple rule-of-thumb which can be used
13213          in such cases.  When compiling for an i386/i486, GCC will
13214          allocate 8 bytes for the structure shown above.  It decides to
13215          do this based upon one simple rule for bit-field allocation.
13216          GCC allocates each "containing object" for each bit-field at
13217          the first (i.e. lowest addressed) legitimate alignment boundary
13218          (based upon the required minimum alignment for the declared
13219          type of the field) which it can possibly use, subject to the
13220          condition that there is still enough available space remaining
13221          in the containing object (when allocated at the selected point)
13222          to fully accommodate all of the bits of the bit-field itself.
13223
13224          This simple rule makes it obvious why GCC allocates 8 bytes for
13225          each object of the structure type shown above.  When looking
13226          for a place to allocate the "containing object" for `field2',
13227          the compiler simply tries to allocate a 64-bit "containing
13228          object" at each successive 32-bit boundary (starting at zero)
13229          until it finds a place to allocate that 64- bit field such that
13230          at least 31 contiguous (and previously unallocated) bits remain
13231          within that selected 64 bit field.  (As it turns out, for the
13232          example above, the compiler finds it is OK to allocate the
13233          "containing object" 64-bit field at bit-offset zero within the
13234          structure type.)
13235
13236          Here we attempt to work backwards from the limited set of facts
13237          we're given, and we try to deduce from those facts, where GCC
13238          must have believed that the containing object started (within
13239          the structure type). The value we deduce is then used (by the
13240          callers of this routine) to generate DW_AT_location and
13241          DW_AT_bit_offset attributes for fields (both bit-fields and, in
13242          the case of DW_AT_location, regular fields as well).  */
13243
13244       /* Figure out the bit-distance from the start of the structure to
13245          the "deepest" bit of the bit-field.  */
13246       deepest_bitpos = bitpos_int + field_size_in_bits;
13247
13248       /* This is the tricky part.  Use some fancy footwork to deduce
13249          where the lowest addressed bit of the containing object must
13250          be.  */
13251       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13252
13253       /* Round up to type_align by default.  This works best for
13254          bitfields.  */
13255       object_offset_in_bits
13256         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13257
13258       if (object_offset_in_bits > bitpos_int)
13259         {
13260           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13261
13262           /* Round up to decl_align instead.  */
13263           object_offset_in_bits
13264             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13265         }
13266     }
13267   else
13268 #endif
13269     object_offset_in_bits = bitpos_int;
13270
13271   return object_offset_in_bits / BITS_PER_UNIT;
13272 }
13273 \f
13274 /* The following routines define various Dwarf attributes and any data
13275    associated with them.  */
13276
13277 /* Add a location description attribute value to a DIE.
13278
13279    This emits location attributes suitable for whole variables and
13280    whole parameters.  Note that the location attributes for struct fields are
13281    generated by the routine `data_member_location_attribute' below.  */
13282
13283 static inline void
13284 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
13285                              dw_loc_list_ref descr)
13286 {
13287   if (descr == 0)
13288     return;
13289   if (single_element_loc_list_p (descr))
13290     add_AT_loc (die, attr_kind, descr->expr);
13291   else
13292     add_AT_loc_list (die, attr_kind, descr);
13293 }
13294
13295 /* Attach the specialized form of location attribute used for data members of
13296    struct and union types.  In the special case of a FIELD_DECL node which
13297    represents a bit-field, the "offset" part of this special location
13298    descriptor must indicate the distance in bytes from the lowest-addressed
13299    byte of the containing struct or union type to the lowest-addressed byte of
13300    the "containing object" for the bit-field.  (See the `field_byte_offset'
13301    function above).
13302
13303    For any given bit-field, the "containing object" is a hypothetical object
13304    (of some integral or enum type) within which the given bit-field lives.  The
13305    type of this hypothetical "containing object" is always the same as the
13306    declared type of the individual bit-field itself (for GCC anyway... the
13307    DWARF spec doesn't actually mandate this).  Note that it is the size (in
13308    bytes) of the hypothetical "containing object" which will be given in the
13309    DW_AT_byte_size attribute for this bit-field.  (See the
13310    `byte_size_attribute' function below.)  It is also used when calculating the
13311    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
13312    function below.)  */
13313
13314 static void
13315 add_data_member_location_attribute (dw_die_ref die, tree decl)
13316 {
13317   HOST_WIDE_INT offset;
13318   dw_loc_descr_ref loc_descr = 0;
13319
13320   if (TREE_CODE (decl) == TREE_BINFO)
13321     {
13322       /* We're working on the TAG_inheritance for a base class.  */
13323       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
13324         {
13325           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
13326              aren't at a fixed offset from all (sub)objects of the same
13327              type.  We need to extract the appropriate offset from our
13328              vtable.  The following dwarf expression means
13329
13330                BaseAddr = ObAddr + *((*ObAddr) - Offset)
13331
13332              This is specific to the V3 ABI, of course.  */
13333
13334           dw_loc_descr_ref tmp;
13335
13336           /* Make a copy of the object address.  */
13337           tmp = new_loc_descr (DW_OP_dup, 0, 0);
13338           add_loc_descr (&loc_descr, tmp);
13339
13340           /* Extract the vtable address.  */
13341           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13342           add_loc_descr (&loc_descr, tmp);
13343
13344           /* Calculate the address of the offset.  */
13345           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
13346           gcc_assert (offset < 0);
13347
13348           tmp = int_loc_descriptor (-offset);
13349           add_loc_descr (&loc_descr, tmp);
13350           tmp = new_loc_descr (DW_OP_minus, 0, 0);
13351           add_loc_descr (&loc_descr, tmp);
13352
13353           /* Extract the offset.  */
13354           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13355           add_loc_descr (&loc_descr, tmp);
13356
13357           /* Add it to the object address.  */
13358           tmp = new_loc_descr (DW_OP_plus, 0, 0);
13359           add_loc_descr (&loc_descr, tmp);
13360         }
13361       else
13362         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
13363     }
13364   else
13365     offset = field_byte_offset (decl);
13366
13367   if (! loc_descr)
13368     {
13369       if (dwarf_version > 2)
13370         {
13371           /* Don't need to output a location expression, just the constant. */
13372           add_AT_int (die, DW_AT_data_member_location, offset);
13373           return;
13374         }
13375       else
13376         {
13377           enum dwarf_location_atom op;
13378           
13379           /* The DWARF2 standard says that we should assume that the structure
13380              address is already on the stack, so we can specify a structure
13381              field address by using DW_OP_plus_uconst.  */
13382           
13383 #ifdef MIPS_DEBUGGING_INFO
13384           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
13385              operator correctly.  It works only if we leave the offset on the
13386              stack.  */
13387           op = DW_OP_constu;
13388 #else
13389           op = DW_OP_plus_uconst;
13390 #endif
13391           
13392           loc_descr = new_loc_descr (op, offset, 0);
13393         }
13394     }
13395
13396   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
13397 }
13398
13399 /* Writes integer values to dw_vec_const array.  */
13400
13401 static void
13402 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
13403 {
13404   while (size != 0)
13405     {
13406       *dest++ = val & 0xff;
13407       val >>= 8;
13408       --size;
13409     }
13410 }
13411
13412 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
13413
13414 static HOST_WIDE_INT
13415 extract_int (const unsigned char *src, unsigned int size)
13416 {
13417   HOST_WIDE_INT val = 0;
13418
13419   src += size;
13420   while (size != 0)
13421     {
13422       val <<= 8;
13423       val |= *--src & 0xff;
13424       --size;
13425     }
13426   return val;
13427 }
13428
13429 /* Writes floating point values to dw_vec_const array.  */
13430
13431 static void
13432 insert_float (const_rtx rtl, unsigned char *array)
13433 {
13434   REAL_VALUE_TYPE rv;
13435   long val[4];
13436   int i;
13437
13438   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
13439   real_to_target (val, &rv, GET_MODE (rtl));
13440
13441   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
13442   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
13443     {
13444       insert_int (val[i], 4, array);
13445       array += 4;
13446     }
13447 }
13448
13449 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
13450    does not have a "location" either in memory or in a register.  These
13451    things can arise in GNU C when a constant is passed as an actual parameter
13452    to an inlined function.  They can also arise in C++ where declared
13453    constants do not necessarily get memory "homes".  */
13454
13455 static bool
13456 add_const_value_attribute (dw_die_ref die, rtx rtl)
13457 {
13458   switch (GET_CODE (rtl))
13459     {
13460     case CONST_INT:
13461       {
13462         HOST_WIDE_INT val = INTVAL (rtl);
13463
13464         if (val < 0)
13465           add_AT_int (die, DW_AT_const_value, val);
13466         else
13467           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
13468       }
13469       return true;
13470
13471     case CONST_DOUBLE:
13472       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
13473          floating-point constant.  A CONST_DOUBLE is used whenever the
13474          constant requires more than one word in order to be adequately
13475          represented.  */
13476       {
13477         enum machine_mode mode = GET_MODE (rtl);
13478
13479         if (SCALAR_FLOAT_MODE_P (mode))
13480           {
13481             unsigned int length = GET_MODE_SIZE (mode);
13482             unsigned char *array = GGC_NEWVEC (unsigned char, length);
13483
13484             insert_float (rtl, array);
13485             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
13486           }
13487         else
13488           add_AT_double (die, DW_AT_const_value,
13489                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
13490       }
13491       return true;
13492
13493     case CONST_VECTOR:
13494       {
13495         enum machine_mode mode = GET_MODE (rtl);
13496         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
13497         unsigned int length = CONST_VECTOR_NUNITS (rtl);
13498         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
13499         unsigned int i;
13500         unsigned char *p;
13501
13502         switch (GET_MODE_CLASS (mode))
13503           {
13504           case MODE_VECTOR_INT:
13505             for (i = 0, p = array; i < length; i++, p += elt_size)
13506               {
13507                 rtx elt = CONST_VECTOR_ELT (rtl, i);
13508                 HOST_WIDE_INT lo, hi;
13509
13510                 switch (GET_CODE (elt))
13511                   {
13512                   case CONST_INT:
13513                     lo = INTVAL (elt);
13514                     hi = -(lo < 0);
13515                     break;
13516
13517                   case CONST_DOUBLE:
13518                     lo = CONST_DOUBLE_LOW (elt);
13519                     hi = CONST_DOUBLE_HIGH (elt);
13520                     break;
13521
13522                   default:
13523                     gcc_unreachable ();
13524                   }
13525
13526                 if (elt_size <= sizeof (HOST_WIDE_INT))
13527                   insert_int (lo, elt_size, p);
13528                 else
13529                   {
13530                     unsigned char *p0 = p;
13531                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
13532
13533                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13534                     if (WORDS_BIG_ENDIAN)
13535                       {
13536                         p0 = p1;
13537                         p1 = p;
13538                       }
13539                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
13540                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
13541                   }
13542               }
13543             break;
13544
13545           case MODE_VECTOR_FLOAT:
13546             for (i = 0, p = array; i < length; i++, p += elt_size)
13547               {
13548                 rtx elt = CONST_VECTOR_ELT (rtl, i);
13549                 insert_float (elt, p);
13550               }
13551             break;
13552
13553           default:
13554             gcc_unreachable ();
13555           }
13556
13557         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
13558       }
13559       return true;
13560
13561     case CONST_STRING:
13562       resolve_one_addr (&rtl, NULL);
13563       add_AT_addr (die, DW_AT_const_value, rtl);
13564       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13565       return true;
13566
13567     case CONST:
13568       if (CONSTANT_P (XEXP (rtl, 0)))
13569         return add_const_value_attribute (die, XEXP (rtl, 0));
13570       /* FALLTHROUGH */
13571     case SYMBOL_REF:
13572       if (!const_ok_for_output (rtl))
13573         return false;
13574     case LABEL_REF:
13575       add_AT_addr (die, DW_AT_const_value, rtl);
13576       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13577       return true;
13578
13579     case PLUS:
13580       /* In cases where an inlined instance of an inline function is passed
13581          the address of an `auto' variable (which is local to the caller) we
13582          can get a situation where the DECL_RTL of the artificial local
13583          variable (for the inlining) which acts as a stand-in for the
13584          corresponding formal parameter (of the inline function) will look
13585          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
13586          exactly a compile-time constant expression, but it isn't the address
13587          of the (artificial) local variable either.  Rather, it represents the
13588          *value* which the artificial local variable always has during its
13589          lifetime.  We currently have no way to represent such quasi-constant
13590          values in Dwarf, so for now we just punt and generate nothing.  */
13591       return false;
13592
13593     case HIGH:
13594     case CONST_FIXED:
13595       return false;
13596
13597     case MEM:
13598       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
13599           && MEM_READONLY_P (rtl)
13600           && GET_MODE (rtl) == BLKmode)
13601         {
13602           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
13603           return true;
13604         }
13605       return false;
13606
13607     default:
13608       /* No other kinds of rtx should be possible here.  */
13609       gcc_unreachable ();
13610     }
13611   return false;
13612 }
13613
13614 /* Determine whether the evaluation of EXPR references any variables
13615    or functions which aren't otherwise used (and therefore may not be
13616    output).  */
13617 static tree
13618 reference_to_unused (tree * tp, int * walk_subtrees,
13619                      void * data ATTRIBUTE_UNUSED)
13620 {
13621   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
13622     *walk_subtrees = 0;
13623
13624   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
13625       && ! TREE_ASM_WRITTEN (*tp))
13626     return *tp;
13627   /* ???  The C++ FE emits debug information for using decls, so
13628      putting gcc_unreachable here falls over.  See PR31899.  For now
13629      be conservative.  */
13630   else if (!cgraph_global_info_ready
13631            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
13632     return *tp;
13633   else if (TREE_CODE (*tp) == VAR_DECL)
13634     {
13635       struct varpool_node *node = varpool_node (*tp);
13636       if (!node->needed)
13637         return *tp;
13638     }
13639   else if (TREE_CODE (*tp) == FUNCTION_DECL
13640            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
13641     {
13642       /* The call graph machinery must have finished analyzing,
13643          optimizing and gimplifying the CU by now.
13644          So if *TP has no call graph node associated
13645          to it, it means *TP will not be emitted.  */
13646       if (!cgraph_get_node (*tp))
13647         return *tp;
13648     }
13649   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
13650     return *tp;
13651
13652   return NULL_TREE;
13653 }
13654
13655 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
13656    for use in a later add_const_value_attribute call.  */
13657
13658 static rtx
13659 rtl_for_decl_init (tree init, tree type)
13660 {
13661   rtx rtl = NULL_RTX;
13662
13663   /* If a variable is initialized with a string constant without embedded
13664      zeros, build CONST_STRING.  */
13665   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
13666     {
13667       tree enttype = TREE_TYPE (type);
13668       tree domain = TYPE_DOMAIN (type);
13669       enum machine_mode mode = TYPE_MODE (enttype);
13670
13671       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
13672           && domain
13673           && integer_zerop (TYPE_MIN_VALUE (domain))
13674           && compare_tree_int (TYPE_MAX_VALUE (domain),
13675                                TREE_STRING_LENGTH (init) - 1) == 0
13676           && ((size_t) TREE_STRING_LENGTH (init)
13677               == strlen (TREE_STRING_POINTER (init)) + 1))
13678         {
13679           rtl = gen_rtx_CONST_STRING (VOIDmode,
13680                                       ggc_strdup (TREE_STRING_POINTER (init)));
13681           rtl = gen_rtx_MEM (BLKmode, rtl);
13682           MEM_READONLY_P (rtl) = 1;
13683         }
13684     }
13685   /* Other aggregates, and complex values, could be represented using
13686      CONCAT: FIXME!  */
13687   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
13688     ;
13689   /* Vectors only work if their mode is supported by the target.
13690      FIXME: generic vectors ought to work too.  */
13691   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
13692     ;
13693   /* If the initializer is something that we know will expand into an
13694      immediate RTL constant, expand it now.  We must be careful not to
13695      reference variables which won't be output.  */
13696   else if (initializer_constant_valid_p (init, type)
13697            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
13698     {
13699       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
13700          possible.  */
13701       if (TREE_CODE (type) == VECTOR_TYPE)
13702         switch (TREE_CODE (init))
13703           {
13704           case VECTOR_CST:
13705             break;
13706           case CONSTRUCTOR:
13707             if (TREE_CONSTANT (init))
13708               {
13709                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
13710                 bool constant_p = true;
13711                 tree value;
13712                 unsigned HOST_WIDE_INT ix;
13713
13714                 /* Even when ctor is constant, it might contain non-*_CST
13715                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
13716                    belong into VECTOR_CST nodes.  */
13717                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
13718                   if (!CONSTANT_CLASS_P (value))
13719                     {
13720                       constant_p = false;
13721                       break;
13722                     }
13723
13724                 if (constant_p)
13725                   {
13726                     init = build_vector_from_ctor (type, elts);
13727                     break;
13728                   }
13729               }
13730             /* FALLTHRU */
13731
13732           default:
13733             return NULL;
13734           }
13735
13736       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
13737
13738       /* If expand_expr returns a MEM, it wasn't immediate.  */
13739       gcc_assert (!rtl || !MEM_P (rtl));
13740     }
13741
13742   return rtl;
13743 }
13744
13745 /* Generate RTL for the variable DECL to represent its location.  */
13746
13747 static rtx
13748 rtl_for_decl_location (tree decl)
13749 {
13750   rtx rtl;
13751
13752   /* Here we have to decide where we are going to say the parameter "lives"
13753      (as far as the debugger is concerned).  We only have a couple of
13754      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
13755
13756      DECL_RTL normally indicates where the parameter lives during most of the
13757      activation of the function.  If optimization is enabled however, this
13758      could be either NULL or else a pseudo-reg.  Both of those cases indicate
13759      that the parameter doesn't really live anywhere (as far as the code
13760      generation parts of GCC are concerned) during most of the function's
13761      activation.  That will happen (for example) if the parameter is never
13762      referenced within the function.
13763
13764      We could just generate a location descriptor here for all non-NULL
13765      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
13766      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
13767      where DECL_RTL is NULL or is a pseudo-reg.
13768
13769      Note however that we can only get away with using DECL_INCOMING_RTL as
13770      a backup substitute for DECL_RTL in certain limited cases.  In cases
13771      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
13772      we can be sure that the parameter was passed using the same type as it is
13773      declared to have within the function, and that its DECL_INCOMING_RTL
13774      points us to a place where a value of that type is passed.
13775
13776      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
13777      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
13778      because in these cases DECL_INCOMING_RTL points us to a value of some
13779      type which is *different* from the type of the parameter itself.  Thus,
13780      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
13781      such cases, the debugger would end up (for example) trying to fetch a
13782      `float' from a place which actually contains the first part of a
13783      `double'.  That would lead to really incorrect and confusing
13784      output at debug-time.
13785
13786      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
13787      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
13788      are a couple of exceptions however.  On little-endian machines we can
13789      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
13790      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
13791      an integral type that is smaller than TREE_TYPE (decl). These cases arise
13792      when (on a little-endian machine) a non-prototyped function has a
13793      parameter declared to be of type `short' or `char'.  In such cases,
13794      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
13795      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
13796      passed `int' value.  If the debugger then uses that address to fetch
13797      a `short' or a `char' (on a little-endian machine) the result will be
13798      the correct data, so we allow for such exceptional cases below.
13799
13800      Note that our goal here is to describe the place where the given formal
13801      parameter lives during most of the function's activation (i.e. between the
13802      end of the prologue and the start of the epilogue).  We'll do that as best
13803      as we can. Note however that if the given formal parameter is modified
13804      sometime during the execution of the function, then a stack backtrace (at
13805      debug-time) will show the function as having been called with the *new*
13806      value rather than the value which was originally passed in.  This happens
13807      rarely enough that it is not a major problem, but it *is* a problem, and
13808      I'd like to fix it.
13809
13810      A future version of dwarf2out.c may generate two additional attributes for
13811      any given DW_TAG_formal_parameter DIE which will describe the "passed
13812      type" and the "passed location" for the given formal parameter in addition
13813      to the attributes we now generate to indicate the "declared type" and the
13814      "active location" for each parameter.  This additional set of attributes
13815      could be used by debuggers for stack backtraces. Separately, note that
13816      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
13817      This happens (for example) for inlined-instances of inline function formal
13818      parameters which are never referenced.  This really shouldn't be
13819      happening.  All PARM_DECL nodes should get valid non-NULL
13820      DECL_INCOMING_RTL values.  FIXME.  */
13821
13822   /* Use DECL_RTL as the "location" unless we find something better.  */
13823   rtl = DECL_RTL_IF_SET (decl);
13824
13825   /* When generating abstract instances, ignore everything except
13826      constants, symbols living in memory, and symbols living in
13827      fixed registers.  */
13828   if (! reload_completed)
13829     {
13830       if (rtl
13831           && (CONSTANT_P (rtl)
13832               || (MEM_P (rtl)
13833                   && CONSTANT_P (XEXP (rtl, 0)))
13834               || (REG_P (rtl)
13835                   && TREE_CODE (decl) == VAR_DECL
13836                   && TREE_STATIC (decl))))
13837         {
13838           rtl = targetm.delegitimize_address (rtl);
13839           return rtl;
13840         }
13841       rtl = NULL_RTX;
13842     }
13843   else if (TREE_CODE (decl) == PARM_DECL)
13844     {
13845       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
13846         {
13847           tree declared_type = TREE_TYPE (decl);
13848           tree passed_type = DECL_ARG_TYPE (decl);
13849           enum machine_mode dmode = TYPE_MODE (declared_type);
13850           enum machine_mode pmode = TYPE_MODE (passed_type);
13851
13852           /* This decl represents a formal parameter which was optimized out.
13853              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
13854              all cases where (rtl == NULL_RTX) just below.  */
13855           if (dmode == pmode)
13856             rtl = DECL_INCOMING_RTL (decl);
13857           else if (SCALAR_INT_MODE_P (dmode)
13858                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
13859                    && DECL_INCOMING_RTL (decl))
13860             {
13861               rtx inc = DECL_INCOMING_RTL (decl);
13862               if (REG_P (inc))
13863                 rtl = inc;
13864               else if (MEM_P (inc))
13865                 {
13866                   if (BYTES_BIG_ENDIAN)
13867                     rtl = adjust_address_nv (inc, dmode,
13868                                              GET_MODE_SIZE (pmode)
13869                                              - GET_MODE_SIZE (dmode));
13870                   else
13871                     rtl = inc;
13872                 }
13873             }
13874         }
13875
13876       /* If the parm was passed in registers, but lives on the stack, then
13877          make a big endian correction if the mode of the type of the
13878          parameter is not the same as the mode of the rtl.  */
13879       /* ??? This is the same series of checks that are made in dbxout.c before
13880          we reach the big endian correction code there.  It isn't clear if all
13881          of these checks are necessary here, but keeping them all is the safe
13882          thing to do.  */
13883       else if (MEM_P (rtl)
13884                && XEXP (rtl, 0) != const0_rtx
13885                && ! CONSTANT_P (XEXP (rtl, 0))
13886                /* Not passed in memory.  */
13887                && !MEM_P (DECL_INCOMING_RTL (decl))
13888                /* Not passed by invisible reference.  */
13889                && (!REG_P (XEXP (rtl, 0))
13890                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
13891                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
13892 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
13893                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
13894 #endif
13895                      )
13896                /* Big endian correction check.  */
13897                && BYTES_BIG_ENDIAN
13898                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
13899                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
13900                    < UNITS_PER_WORD))
13901         {
13902           int offset = (UNITS_PER_WORD
13903                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
13904
13905           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13906                              plus_constant (XEXP (rtl, 0), offset));
13907         }
13908     }
13909   else if (TREE_CODE (decl) == VAR_DECL
13910            && rtl
13911            && MEM_P (rtl)
13912            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
13913            && BYTES_BIG_ENDIAN)
13914     {
13915       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
13916       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
13917
13918       /* If a variable is declared "register" yet is smaller than
13919          a register, then if we store the variable to memory, it
13920          looks like we're storing a register-sized value, when in
13921          fact we are not.  We need to adjust the offset of the
13922          storage location to reflect the actual value's bytes,
13923          else gdb will not be able to display it.  */
13924       if (rsize > dsize)
13925         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13926                            plus_constant (XEXP (rtl, 0), rsize-dsize));
13927     }
13928
13929   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
13930      and will have been substituted directly into all expressions that use it.
13931      C does not have such a concept, but C++ and other languages do.  */
13932   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
13933     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
13934
13935   if (rtl)
13936     rtl = targetm.delegitimize_address (rtl);
13937
13938   /* If we don't look past the constant pool, we risk emitting a
13939      reference to a constant pool entry that isn't referenced from
13940      code, and thus is not emitted.  */
13941   if (rtl)
13942     rtl = avoid_constant_pool_reference (rtl);
13943
13944   return rtl;
13945 }
13946
13947 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
13948    returned.  If so, the decl for the COMMON block is returned, and the
13949    value is the offset into the common block for the symbol.  */
13950
13951 static tree
13952 fortran_common (tree decl, HOST_WIDE_INT *value)
13953 {
13954   tree val_expr, cvar;
13955   enum machine_mode mode;
13956   HOST_WIDE_INT bitsize, bitpos;
13957   tree offset;
13958   int volatilep = 0, unsignedp = 0;
13959
13960   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
13961      it does not have a value (the offset into the common area), or if it
13962      is thread local (as opposed to global) then it isn't common, and shouldn't
13963      be handled as such.  */
13964   if (TREE_CODE (decl) != VAR_DECL
13965       || !TREE_PUBLIC (decl)
13966       || !TREE_STATIC (decl)
13967       || !DECL_HAS_VALUE_EXPR_P (decl)
13968       || !is_fortran ())
13969     return NULL_TREE;
13970
13971   val_expr = DECL_VALUE_EXPR (decl);
13972   if (TREE_CODE (val_expr) != COMPONENT_REF)
13973     return NULL_TREE;
13974
13975   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
13976                               &mode, &unsignedp, &volatilep, true);
13977
13978   if (cvar == NULL_TREE
13979       || TREE_CODE (cvar) != VAR_DECL
13980       || DECL_ARTIFICIAL (cvar)
13981       || !TREE_PUBLIC (cvar))
13982     return NULL_TREE;
13983
13984   *value = 0;
13985   if (offset != NULL)
13986     {
13987       if (!host_integerp (offset, 0))
13988         return NULL_TREE;
13989       *value = tree_low_cst (offset, 0);
13990     }
13991   if (bitpos != 0)
13992     *value += bitpos / BITS_PER_UNIT;
13993
13994   return cvar;
13995 }
13996
13997 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
13998    data attribute for a variable or a parameter.  We generate the
13999    DW_AT_const_value attribute only in those cases where the given variable
14000    or parameter does not have a true "location" either in memory or in a
14001    register.  This can happen (for example) when a constant is passed as an
14002    actual argument in a call to an inline function.  (It's possible that
14003    these things can crop up in other ways also.)  Note that one type of
14004    constant value which can be passed into an inlined function is a constant
14005    pointer.  This can happen for example if an actual argument in an inlined
14006    function call evaluates to a compile-time constant address.  */
14007
14008 static bool
14009 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
14010                                        enum dwarf_attribute attr)
14011 {
14012   rtx rtl;
14013   dw_loc_list_ref list;
14014   var_loc_list *loc_list;
14015
14016   if (TREE_CODE (decl) == ERROR_MARK)
14017     return false;
14018
14019   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14020               || TREE_CODE (decl) == RESULT_DECL);
14021
14022   /* Try to get some constant RTL for this decl, and use that as the value of
14023      the location.  */
14024
14025   rtl = rtl_for_decl_location (decl);
14026   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14027       && add_const_value_attribute (die, rtl))
14028     return true;
14029
14030   /* See if we have single element location list that is equivalent to
14031      a constant value.  That way we are better to use add_const_value_attribute
14032      rather than expanding constant value equivalent.  */
14033   loc_list = lookup_decl_loc (decl);
14034   if (loc_list && loc_list->first && loc_list->first == loc_list->last)
14035     {
14036       enum var_init_status status;
14037       struct var_loc_node *node;
14038
14039       node = loc_list->first;
14040       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
14041       rtl = NOTE_VAR_LOCATION (node->var_loc_note);
14042       if (GET_CODE (rtl) == VAR_LOCATION
14043           && GET_CODE (XEXP (rtl, 1)) != PARALLEL)
14044         rtl = XEXP (XEXP (rtl, 1), 0);
14045       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14046           && add_const_value_attribute (die, rtl))
14047          return true;
14048     }
14049   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14050   if (list)
14051     {
14052       add_AT_location_description (die, attr, list);
14053       return true;
14054     }
14055   /* None of that worked, so it must not really have a location;
14056      try adding a constant value attribute from the DECL_INITIAL.  */
14057   return tree_add_const_value_attribute_for_decl (die, decl);
14058 }
14059
14060 /* Add VARIABLE and DIE into deferred locations list.  */
14061
14062 static void
14063 defer_location (tree variable, dw_die_ref die)
14064 {
14065   deferred_locations entry;
14066   entry.variable = variable;
14067   entry.die = die;
14068   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
14069 }
14070
14071 /* Helper function for tree_add_const_value_attribute.  Natively encode
14072    initializer INIT into an array.  Return true if successful.  */
14073
14074 static bool
14075 native_encode_initializer (tree init, unsigned char *array, int size)
14076 {
14077   tree type;
14078
14079   if (init == NULL_TREE)
14080     return false;
14081
14082   STRIP_NOPS (init);
14083   switch (TREE_CODE (init))
14084     {
14085     case STRING_CST:
14086       type = TREE_TYPE (init);
14087       if (TREE_CODE (type) == ARRAY_TYPE)
14088         {
14089           tree enttype = TREE_TYPE (type);
14090           enum machine_mode mode = TYPE_MODE (enttype);
14091
14092           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14093             return false;
14094           if (int_size_in_bytes (type) != size)
14095             return false;
14096           if (size > TREE_STRING_LENGTH (init))
14097             {
14098               memcpy (array, TREE_STRING_POINTER (init),
14099                       TREE_STRING_LENGTH (init));
14100               memset (array + TREE_STRING_LENGTH (init),
14101                       '\0', size - TREE_STRING_LENGTH (init));
14102             }
14103           else
14104             memcpy (array, TREE_STRING_POINTER (init), size);
14105           return true;
14106         }
14107       return false;
14108     case CONSTRUCTOR:
14109       type = TREE_TYPE (init);
14110       if (int_size_in_bytes (type) != size)
14111         return false;
14112       if (TREE_CODE (type) == ARRAY_TYPE)
14113         {
14114           HOST_WIDE_INT min_index;
14115           unsigned HOST_WIDE_INT cnt;
14116           int curpos = 0, fieldsize;
14117           constructor_elt *ce;
14118
14119           if (TYPE_DOMAIN (type) == NULL_TREE
14120               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14121             return false;
14122
14123           fieldsize = int_size_in_bytes (TREE_TYPE (type));
14124           if (fieldsize <= 0)
14125             return false;
14126
14127           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14128           memset (array, '\0', size);
14129           for (cnt = 0;
14130                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
14131                cnt++)
14132             {
14133               tree val = ce->value;
14134               tree index = ce->index;
14135               int pos = curpos;
14136               if (index && TREE_CODE (index) == RANGE_EXPR)
14137                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14138                       * fieldsize;
14139               else if (index)
14140                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14141
14142               if (val)
14143                 {
14144                   STRIP_NOPS (val);
14145                   if (!native_encode_initializer (val, array + pos, fieldsize))
14146                     return false;
14147                 }
14148               curpos = pos + fieldsize;
14149               if (index && TREE_CODE (index) == RANGE_EXPR)
14150                 {
14151                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14152                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
14153                   while (count > 0)
14154                     {
14155                       if (val)
14156                         memcpy (array + curpos, array + pos, fieldsize);
14157                       curpos += fieldsize;
14158                     }
14159                 }
14160               gcc_assert (curpos <= size);
14161             }
14162           return true;
14163         }
14164       else if (TREE_CODE (type) == RECORD_TYPE
14165                || TREE_CODE (type) == UNION_TYPE)
14166         {
14167           tree field = NULL_TREE;
14168           unsigned HOST_WIDE_INT cnt;
14169           constructor_elt *ce;
14170
14171           if (int_size_in_bytes (type) != size)
14172             return false;
14173
14174           if (TREE_CODE (type) == RECORD_TYPE)
14175             field = TYPE_FIELDS (type);
14176
14177           for (cnt = 0;
14178                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
14179                cnt++, field = field ? TREE_CHAIN (field) : 0)
14180             {
14181               tree val = ce->value;
14182               int pos, fieldsize;
14183
14184               if (ce->index != 0)
14185                 field = ce->index;
14186
14187               if (val)
14188                 STRIP_NOPS (val);
14189
14190               if (field == NULL_TREE || DECL_BIT_FIELD (field))
14191                 return false;
14192
14193               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
14194                   && TYPE_DOMAIN (TREE_TYPE (field))
14195                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
14196                 return false;
14197               else if (DECL_SIZE_UNIT (field) == NULL_TREE
14198                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
14199                 return false;
14200               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
14201               pos = int_byte_position (field);
14202               gcc_assert (pos + fieldsize <= size);
14203               if (val
14204                   && !native_encode_initializer (val, array + pos, fieldsize))
14205                 return false;
14206             }
14207           return true;
14208         }
14209       return false;
14210     case VIEW_CONVERT_EXPR:
14211     case NON_LVALUE_EXPR:
14212       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
14213     default:
14214       return native_encode_expr (init, array, size) == size;
14215     }
14216 }
14217
14218 /* Attach a DW_AT_const_value attribute to DIE. The value of the
14219    attribute is the const value T.  */
14220
14221 static bool
14222 tree_add_const_value_attribute (dw_die_ref die, tree t)
14223 {
14224   tree init;
14225   tree type = TREE_TYPE (t);
14226   rtx rtl;
14227
14228   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
14229     return false;
14230
14231   init = t;
14232   gcc_assert (!DECL_P (init));
14233
14234   rtl = rtl_for_decl_init (init, type);
14235   if (rtl)
14236     return add_const_value_attribute (die, rtl);
14237   /* If the host and target are sane, try harder.  */
14238   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
14239            && initializer_constant_valid_p (init, type))
14240     {
14241       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
14242       if (size > 0 && (int) size == size)
14243         {
14244           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
14245
14246           if (native_encode_initializer (init, array, size))
14247             {
14248               add_AT_vec (die, DW_AT_const_value, size, 1, array);
14249               return true;
14250             }
14251         }
14252     }
14253   return false;
14254 }
14255
14256 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
14257    attribute is the const value of T, where T is an integral constant
14258    variable with static storage duration
14259    (so it can't be a PARM_DECL or a RESULT_DECL).  */
14260
14261 static bool
14262 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
14263 {
14264
14265   if (!decl
14266       || (TREE_CODE (decl) != VAR_DECL
14267           && TREE_CODE (decl) != CONST_DECL))
14268     return false;
14269
14270     if (TREE_READONLY (decl)
14271         && ! TREE_THIS_VOLATILE (decl)
14272         && DECL_INITIAL (decl))
14273       /* OK */;
14274     else
14275       return false;
14276
14277   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
14278 }
14279
14280 /* Convert the CFI instructions for the current function into a
14281    location list.  This is used for DW_AT_frame_base when we targeting
14282    a dwarf2 consumer that does not support the dwarf3
14283    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
14284    expressions.  */
14285
14286 static dw_loc_list_ref
14287 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
14288 {
14289   dw_fde_ref fde;
14290   dw_loc_list_ref list, *list_tail;
14291   dw_cfi_ref cfi;
14292   dw_cfa_location last_cfa, next_cfa;
14293   const char *start_label, *last_label, *section;
14294   dw_cfa_location remember;
14295
14296   fde = current_fde ();
14297   gcc_assert (fde != NULL);
14298
14299   section = secname_for_decl (current_function_decl);
14300   list_tail = &list;
14301   list = NULL;
14302
14303   memset (&next_cfa, 0, sizeof (next_cfa));
14304   next_cfa.reg = INVALID_REGNUM;
14305   remember = next_cfa;
14306
14307   start_label = fde->dw_fde_begin;
14308
14309   /* ??? Bald assumption that the CIE opcode list does not contain
14310      advance opcodes.  */
14311   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
14312     lookup_cfa_1 (cfi, &next_cfa, &remember);
14313
14314   last_cfa = next_cfa;
14315   last_label = start_label;
14316
14317   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
14318     switch (cfi->dw_cfi_opc)
14319       {
14320       case DW_CFA_set_loc:
14321       case DW_CFA_advance_loc1:
14322       case DW_CFA_advance_loc2:
14323       case DW_CFA_advance_loc4:
14324         if (!cfa_equal_p (&last_cfa, &next_cfa))
14325           {
14326             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14327                                        start_label, last_label, section,
14328                                        list == NULL);
14329
14330             list_tail = &(*list_tail)->dw_loc_next;
14331             last_cfa = next_cfa;
14332             start_label = last_label;
14333           }
14334         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
14335         break;
14336
14337       case DW_CFA_advance_loc:
14338         /* The encoding is complex enough that we should never emit this.  */
14339         gcc_unreachable ();
14340
14341       default:
14342         lookup_cfa_1 (cfi, &next_cfa, &remember);
14343         break;
14344       }
14345
14346   if (!cfa_equal_p (&last_cfa, &next_cfa))
14347     {
14348       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14349                                  start_label, last_label, section,
14350                                  list == NULL);
14351       list_tail = &(*list_tail)->dw_loc_next;
14352       start_label = last_label;
14353     }
14354   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
14355                              start_label, fde->dw_fde_end, section,
14356                              list == NULL);
14357
14358   return list;
14359 }
14360
14361 /* Compute a displacement from the "steady-state frame pointer" to the
14362    frame base (often the same as the CFA), and store it in
14363    frame_pointer_fb_offset.  OFFSET is added to the displacement
14364    before the latter is negated.  */
14365
14366 static void
14367 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
14368 {
14369   rtx reg, elim;
14370
14371 #ifdef FRAME_POINTER_CFA_OFFSET
14372   reg = frame_pointer_rtx;
14373   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
14374 #else
14375   reg = arg_pointer_rtx;
14376   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
14377 #endif
14378
14379   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
14380   if (GET_CODE (elim) == PLUS)
14381     {
14382       offset += INTVAL (XEXP (elim, 1));
14383       elim = XEXP (elim, 0);
14384     }
14385
14386   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
14387                && (elim == hard_frame_pointer_rtx
14388                    || elim == stack_pointer_rtx))
14389               || elim == (frame_pointer_needed
14390                           ? hard_frame_pointer_rtx
14391                           : stack_pointer_rtx));
14392
14393   frame_pointer_fb_offset = -offset;
14394 }
14395
14396 /* Generate a DW_AT_name attribute given some string value to be included as
14397    the value of the attribute.  */
14398
14399 static void
14400 add_name_attribute (dw_die_ref die, const char *name_string)
14401 {
14402   if (name_string != NULL && *name_string != 0)
14403     {
14404       if (demangle_name_func)
14405         name_string = (*demangle_name_func) (name_string);
14406
14407       add_AT_string (die, DW_AT_name, name_string);
14408     }
14409 }
14410
14411 /* Generate a DW_AT_comp_dir attribute for DIE.  */
14412
14413 static void
14414 add_comp_dir_attribute (dw_die_ref die)
14415 {
14416   const char *wd = get_src_pwd ();
14417   char *wd1;
14418
14419   if (wd == NULL)
14420     return;
14421
14422   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
14423     {
14424       int wdlen;
14425
14426       wdlen = strlen (wd);
14427       wd1 = GGC_NEWVEC (char, wdlen + 2);
14428       strcpy (wd1, wd);
14429       wd1 [wdlen] = DIR_SEPARATOR;
14430       wd1 [wdlen + 1] = 0;
14431       wd = wd1;
14432     }
14433
14434     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
14435 }
14436
14437 /* Given a tree node describing an array bound (either lower or upper) output
14438    a representation for that bound.  */
14439
14440 static void
14441 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
14442 {
14443   switch (TREE_CODE (bound))
14444     {
14445     case ERROR_MARK:
14446       return;
14447
14448     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
14449     case INTEGER_CST:
14450       {
14451         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
14452
14453         /* Use the default if possible.  */
14454         if (bound_attr == DW_AT_lower_bound
14455             && (((is_c_family () || is_java ()) && integer_zerop (bound))
14456                 || (is_fortran () && integer_onep (bound))))
14457           ;
14458
14459         /* Otherwise represent the bound as an unsigned value with the
14460            precision of its type.  The precision and signedness of the
14461            type will be necessary to re-interpret it unambiguously.  */
14462         else if (prec < HOST_BITS_PER_WIDE_INT)
14463           {
14464             unsigned HOST_WIDE_INT mask
14465               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
14466             add_AT_unsigned (subrange_die, bound_attr,
14467                              TREE_INT_CST_LOW (bound) & mask);
14468           }
14469         else if (prec == HOST_BITS_PER_WIDE_INT
14470                  || TREE_INT_CST_HIGH (bound) == 0)
14471           add_AT_unsigned (subrange_die, bound_attr,
14472                            TREE_INT_CST_LOW (bound));
14473         else
14474           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
14475                          TREE_INT_CST_LOW (bound));
14476       }
14477       break;
14478
14479     CASE_CONVERT:
14480     case VIEW_CONVERT_EXPR:
14481       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
14482       break;
14483
14484     case SAVE_EXPR:
14485       break;
14486
14487     case VAR_DECL:
14488     case PARM_DECL:
14489     case RESULT_DECL:
14490       {
14491         dw_die_ref decl_die = lookup_decl_die (bound);
14492         dw_loc_list_ref loc;
14493
14494         /* ??? Can this happen, or should the variable have been bound
14495            first?  Probably it can, since I imagine that we try to create
14496            the types of parameters in the order in which they exist in
14497            the list, and won't have created a forward reference to a
14498            later parameter.  */
14499         if (decl_die != NULL)
14500           add_AT_die_ref (subrange_die, bound_attr, decl_die);
14501         else
14502           {
14503             loc = loc_list_from_tree (bound, 0);
14504             add_AT_location_description (subrange_die, bound_attr, loc);
14505           }
14506         break;
14507       }
14508
14509     default:
14510       {
14511         /* Otherwise try to create a stack operation procedure to
14512            evaluate the value of the array bound.  */
14513
14514         dw_die_ref ctx, decl_die;
14515         dw_loc_list_ref list;
14516
14517         list = loc_list_from_tree (bound, 2);
14518         if (list == NULL)
14519           break;
14520
14521         if (current_function_decl == 0)
14522           ctx = comp_unit_die;
14523         else
14524           ctx = lookup_decl_die (current_function_decl);
14525
14526         decl_die = new_die (DW_TAG_variable, ctx, bound);
14527         add_AT_flag (decl_die, DW_AT_artificial, 1);
14528         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
14529         if (list->dw_loc_next)
14530           add_AT_loc_list (decl_die, DW_AT_location, list);
14531         else
14532           add_AT_loc (decl_die, DW_AT_location, list->expr);
14533
14534         add_AT_die_ref (subrange_die, bound_attr, decl_die);
14535         break;
14536       }
14537     }
14538 }
14539
14540 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
14541    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
14542    Note that the block of subscript information for an array type also
14543    includes information about the element type of the given array type.  */
14544
14545 static void
14546 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
14547 {
14548   unsigned dimension_number;
14549   tree lower, upper;
14550   dw_die_ref subrange_die;
14551
14552   for (dimension_number = 0;
14553        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
14554        type = TREE_TYPE (type), dimension_number++)
14555     {
14556       tree domain = TYPE_DOMAIN (type);
14557
14558       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
14559         break;
14560
14561       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
14562          and (in GNU C only) variable bounds.  Handle all three forms
14563          here.  */
14564       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
14565       if (domain)
14566         {
14567           /* We have an array type with specified bounds.  */
14568           lower = TYPE_MIN_VALUE (domain);
14569           upper = TYPE_MAX_VALUE (domain);
14570
14571           /* Define the index type.  */
14572           if (TREE_TYPE (domain))
14573             {
14574               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
14575                  TREE_TYPE field.  We can't emit debug info for this
14576                  because it is an unnamed integral type.  */
14577               if (TREE_CODE (domain) == INTEGER_TYPE
14578                   && TYPE_NAME (domain) == NULL_TREE
14579                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
14580                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
14581                 ;
14582               else
14583                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
14584                                     type_die);
14585             }
14586
14587           /* ??? If upper is NULL, the array has unspecified length,
14588              but it does have a lower bound.  This happens with Fortran
14589                dimension arr(N:*)
14590              Since the debugger is definitely going to need to know N
14591              to produce useful results, go ahead and output the lower
14592              bound solo, and hope the debugger can cope.  */
14593
14594           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
14595           if (upper)
14596             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
14597         }
14598
14599       /* Otherwise we have an array type with an unspecified length.  The
14600          DWARF-2 spec does not say how to handle this; let's just leave out the
14601          bounds.  */
14602     }
14603 }
14604
14605 static void
14606 add_byte_size_attribute (dw_die_ref die, tree tree_node)
14607 {
14608   unsigned size;
14609
14610   switch (TREE_CODE (tree_node))
14611     {
14612     case ERROR_MARK:
14613       size = 0;
14614       break;
14615     case ENUMERAL_TYPE:
14616     case RECORD_TYPE:
14617     case UNION_TYPE:
14618     case QUAL_UNION_TYPE:
14619       size = int_size_in_bytes (tree_node);
14620       break;
14621     case FIELD_DECL:
14622       /* For a data member of a struct or union, the DW_AT_byte_size is
14623          generally given as the number of bytes normally allocated for an
14624          object of the *declared* type of the member itself.  This is true
14625          even for bit-fields.  */
14626       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
14627       break;
14628     default:
14629       gcc_unreachable ();
14630     }
14631
14632   /* Note that `size' might be -1 when we get to this point.  If it is, that
14633      indicates that the byte size of the entity in question is variable.  We
14634      have no good way of expressing this fact in Dwarf at the present time,
14635      so just let the -1 pass on through.  */
14636   add_AT_unsigned (die, DW_AT_byte_size, size);
14637 }
14638
14639 /* For a FIELD_DECL node which represents a bit-field, output an attribute
14640    which specifies the distance in bits from the highest order bit of the
14641    "containing object" for the bit-field to the highest order bit of the
14642    bit-field itself.
14643
14644    For any given bit-field, the "containing object" is a hypothetical object
14645    (of some integral or enum type) within which the given bit-field lives.  The
14646    type of this hypothetical "containing object" is always the same as the
14647    declared type of the individual bit-field itself.  The determination of the
14648    exact location of the "containing object" for a bit-field is rather
14649    complicated.  It's handled by the `field_byte_offset' function (above).
14650
14651    Note that it is the size (in bytes) of the hypothetical "containing object"
14652    which will be given in the DW_AT_byte_size attribute for this bit-field.
14653    (See `byte_size_attribute' above).  */
14654
14655 static inline void
14656 add_bit_offset_attribute (dw_die_ref die, tree decl)
14657 {
14658   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
14659   tree type = DECL_BIT_FIELD_TYPE (decl);
14660   HOST_WIDE_INT bitpos_int;
14661   HOST_WIDE_INT highest_order_object_bit_offset;
14662   HOST_WIDE_INT highest_order_field_bit_offset;
14663   HOST_WIDE_INT unsigned bit_offset;
14664
14665   /* Must be a field and a bit field.  */
14666   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
14667
14668   /* We can't yet handle bit-fields whose offsets are variable, so if we
14669      encounter such things, just return without generating any attribute
14670      whatsoever.  Likewise for variable or too large size.  */
14671   if (! host_integerp (bit_position (decl), 0)
14672       || ! host_integerp (DECL_SIZE (decl), 1))
14673     return;
14674
14675   bitpos_int = int_bit_position (decl);
14676
14677   /* Note that the bit offset is always the distance (in bits) from the
14678      highest-order bit of the "containing object" to the highest-order bit of
14679      the bit-field itself.  Since the "high-order end" of any object or field
14680      is different on big-endian and little-endian machines, the computation
14681      below must take account of these differences.  */
14682   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
14683   highest_order_field_bit_offset = bitpos_int;
14684
14685   if (! BYTES_BIG_ENDIAN)
14686     {
14687       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
14688       highest_order_object_bit_offset += simple_type_size_in_bits (type);
14689     }
14690
14691   bit_offset
14692     = (! BYTES_BIG_ENDIAN
14693        ? highest_order_object_bit_offset - highest_order_field_bit_offset
14694        : highest_order_field_bit_offset - highest_order_object_bit_offset);
14695
14696   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
14697 }
14698
14699 /* For a FIELD_DECL node which represents a bit field, output an attribute
14700    which specifies the length in bits of the given field.  */
14701
14702 static inline void
14703 add_bit_size_attribute (dw_die_ref die, tree decl)
14704 {
14705   /* Must be a field and a bit field.  */
14706   gcc_assert (TREE_CODE (decl) == FIELD_DECL
14707               && DECL_BIT_FIELD_TYPE (decl));
14708
14709   if (host_integerp (DECL_SIZE (decl), 1))
14710     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
14711 }
14712
14713 /* If the compiled language is ANSI C, then add a 'prototyped'
14714    attribute, if arg types are given for the parameters of a function.  */
14715
14716 static inline void
14717 add_prototyped_attribute (dw_die_ref die, tree func_type)
14718 {
14719   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
14720       && TYPE_ARG_TYPES (func_type) != NULL)
14721     add_AT_flag (die, DW_AT_prototyped, 1);
14722 }
14723
14724 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
14725    by looking in either the type declaration or object declaration
14726    equate table.  */
14727
14728 static inline dw_die_ref
14729 add_abstract_origin_attribute (dw_die_ref die, tree origin)
14730 {
14731   dw_die_ref origin_die = NULL;
14732
14733   if (TREE_CODE (origin) != FUNCTION_DECL)
14734     {
14735       /* We may have gotten separated from the block for the inlined
14736          function, if we're in an exception handler or some such; make
14737          sure that the abstract function has been written out.
14738
14739          Doing this for nested functions is wrong, however; functions are
14740          distinct units, and our context might not even be inline.  */
14741       tree fn = origin;
14742
14743       if (TYPE_P (fn))
14744         fn = TYPE_STUB_DECL (fn);
14745
14746       fn = decl_function_context (fn);
14747       if (fn)
14748         dwarf2out_abstract_function (fn);
14749     }
14750
14751   if (DECL_P (origin))
14752     origin_die = lookup_decl_die (origin);
14753   else if (TYPE_P (origin))
14754     origin_die = lookup_type_die (origin);
14755
14756   /* XXX: Functions that are never lowered don't always have correct block
14757      trees (in the case of java, they simply have no block tree, in some other
14758      languages).  For these functions, there is nothing we can really do to
14759      output correct debug info for inlined functions in all cases.  Rather
14760      than die, we'll just produce deficient debug info now, in that we will
14761      have variables without a proper abstract origin.  In the future, when all
14762      functions are lowered, we should re-add a gcc_assert (origin_die)
14763      here.  */
14764
14765   if (origin_die)
14766     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
14767   return origin_die;
14768 }
14769
14770 /* We do not currently support the pure_virtual attribute.  */
14771
14772 static inline void
14773 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
14774 {
14775   if (DECL_VINDEX (func_decl))
14776     {
14777       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14778
14779       if (host_integerp (DECL_VINDEX (func_decl), 0))
14780         add_AT_loc (die, DW_AT_vtable_elem_location,
14781                     new_loc_descr (DW_OP_constu,
14782                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
14783                                    0));
14784
14785       /* GNU extension: Record what type this method came from originally.  */
14786       if (debug_info_level > DINFO_LEVEL_TERSE)
14787         add_AT_die_ref (die, DW_AT_containing_type,
14788                         lookup_type_die (DECL_CONTEXT (func_decl)));
14789     }
14790 }
14791 \f
14792 /* Add source coordinate attributes for the given decl.  */
14793
14794 static void
14795 add_src_coords_attributes (dw_die_ref die, tree decl)
14796 {
14797   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14798
14799   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
14800   add_AT_unsigned (die, DW_AT_decl_line, s.line);
14801 }
14802
14803 /* Add a DW_AT_name attribute and source coordinate attribute for the
14804    given decl, but only if it actually has a name.  */
14805
14806 static void
14807 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
14808 {
14809   tree decl_name;
14810
14811   decl_name = DECL_NAME (decl);
14812   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
14813     {
14814       add_name_attribute (die, dwarf2_name (decl, 0));
14815       if (! DECL_ARTIFICIAL (decl))
14816         add_src_coords_attributes (die, decl);
14817
14818       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
14819           && TREE_PUBLIC (decl)
14820           && !DECL_ABSTRACT (decl)
14821           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
14822           && !is_fortran ())
14823         {
14824           /* Defer until we have an assembler name set.  */
14825           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
14826             {
14827               limbo_die_node *asm_name;
14828
14829               asm_name = GGC_CNEW (limbo_die_node);
14830               asm_name->die = die;
14831               asm_name->created_for = decl;
14832               asm_name->next = deferred_asm_name;
14833               deferred_asm_name = asm_name;
14834             }
14835           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
14836             add_AT_string (die, DW_AT_MIPS_linkage_name,
14837                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
14838         }
14839     }
14840
14841 #ifdef VMS_DEBUGGING_INFO
14842   /* Get the function's name, as described by its RTL.  This may be different
14843      from the DECL_NAME name used in the source file.  */
14844   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
14845     {
14846       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
14847                    XEXP (DECL_RTL (decl), 0));
14848       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
14849     }
14850 #endif
14851 }
14852
14853 /* Push a new declaration scope.  */
14854
14855 static void
14856 push_decl_scope (tree scope)
14857 {
14858   VEC_safe_push (tree, gc, decl_scope_table, scope);
14859 }
14860
14861 /* Pop a declaration scope.  */
14862
14863 static inline void
14864 pop_decl_scope (void)
14865 {
14866   VEC_pop (tree, decl_scope_table);
14867 }
14868
14869 /* Return the DIE for the scope that immediately contains this type.
14870    Non-named types get global scope.  Named types nested in other
14871    types get their containing scope if it's open, or global scope
14872    otherwise.  All other types (i.e. function-local named types) get
14873    the current active scope.  */
14874
14875 static dw_die_ref
14876 scope_die_for (tree t, dw_die_ref context_die)
14877 {
14878   dw_die_ref scope_die = NULL;
14879   tree containing_scope;
14880   int i;
14881
14882   /* Non-types always go in the current scope.  */
14883   gcc_assert (TYPE_P (t));
14884
14885   containing_scope = TYPE_CONTEXT (t);
14886
14887   /* Use the containing namespace if it was passed in (for a declaration).  */
14888   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
14889     {
14890       if (context_die == lookup_decl_die (containing_scope))
14891         /* OK */;
14892       else
14893         containing_scope = NULL_TREE;
14894     }
14895
14896   /* Ignore function type "scopes" from the C frontend.  They mean that
14897      a tagged type is local to a parmlist of a function declarator, but
14898      that isn't useful to DWARF.  */
14899   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
14900     containing_scope = NULL_TREE;
14901
14902   if (containing_scope == NULL_TREE)
14903     scope_die = comp_unit_die;
14904   else if (TYPE_P (containing_scope))
14905     {
14906       /* For types, we can just look up the appropriate DIE.  But
14907          first we check to see if we're in the middle of emitting it
14908          so we know where the new DIE should go.  */
14909       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
14910         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
14911           break;
14912
14913       if (i < 0)
14914         {
14915           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
14916                       || TREE_ASM_WRITTEN (containing_scope));
14917
14918           /* If none of the current dies are suitable, we get file scope.  */
14919           scope_die = comp_unit_die;
14920         }
14921       else
14922         scope_die = lookup_type_die (containing_scope);
14923     }
14924   else
14925     scope_die = context_die;
14926
14927   return scope_die;
14928 }
14929
14930 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
14931
14932 static inline int
14933 local_scope_p (dw_die_ref context_die)
14934 {
14935   for (; context_die; context_die = context_die->die_parent)
14936     if (context_die->die_tag == DW_TAG_inlined_subroutine
14937         || context_die->die_tag == DW_TAG_subprogram)
14938       return 1;
14939
14940   return 0;
14941 }
14942
14943 /* Returns nonzero if CONTEXT_DIE is a class.  */
14944
14945 static inline int
14946 class_scope_p (dw_die_ref context_die)
14947 {
14948   return (context_die
14949           && (context_die->die_tag == DW_TAG_structure_type
14950               || context_die->die_tag == DW_TAG_class_type
14951               || context_die->die_tag == DW_TAG_interface_type
14952               || context_die->die_tag == DW_TAG_union_type));
14953 }
14954
14955 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
14956    whether or not to treat a DIE in this context as a declaration.  */
14957
14958 static inline int
14959 class_or_namespace_scope_p (dw_die_ref context_die)
14960 {
14961   return (class_scope_p (context_die)
14962           || (context_die && context_die->die_tag == DW_TAG_namespace));
14963 }
14964
14965 /* Many forms of DIEs require a "type description" attribute.  This
14966    routine locates the proper "type descriptor" die for the type given
14967    by 'type', and adds a DW_AT_type attribute below the given die.  */
14968
14969 static void
14970 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
14971                     int decl_volatile, dw_die_ref context_die)
14972 {
14973   enum tree_code code  = TREE_CODE (type);
14974   dw_die_ref type_die  = NULL;
14975
14976   /* ??? If this type is an unnamed subrange type of an integral, floating-point
14977      or fixed-point type, use the inner type.  This is because we have no
14978      support for unnamed types in base_type_die.  This can happen if this is
14979      an Ada subrange type.  Correct solution is emit a subrange type die.  */
14980   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
14981       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
14982     type = TREE_TYPE (type), code = TREE_CODE (type);
14983
14984   if (code == ERROR_MARK
14985       /* Handle a special case.  For functions whose return type is void, we
14986          generate *no* type attribute.  (Note that no object may have type
14987          `void', so this only applies to function return types).  */
14988       || code == VOID_TYPE)
14989     return;
14990
14991   type_die = modified_type_die (type,
14992                                 decl_const || TYPE_READONLY (type),
14993                                 decl_volatile || TYPE_VOLATILE (type),
14994                                 context_die);
14995
14996   if (type_die != NULL)
14997     add_AT_die_ref (object_die, DW_AT_type, type_die);
14998 }
14999
15000 /* Given an object die, add the calling convention attribute for the
15001    function call type.  */
15002 static void
15003 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
15004 {
15005   enum dwarf_calling_convention value = DW_CC_normal;
15006
15007   value = ((enum dwarf_calling_convention)
15008            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
15009
15010   /* DWARF doesn't provide a way to identify a program's source-level
15011      entry point.  DW_AT_calling_convention attributes are only meant
15012      to describe functions' calling conventions.  However, lacking a
15013      better way to signal the Fortran main program, we use this for the
15014      time being, following existing custom.  */
15015   if (is_fortran ()
15016       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
15017     value = DW_CC_program;
15018
15019   /* Only add the attribute if the backend requests it, and
15020      is not DW_CC_normal.  */
15021   if (value && (value != DW_CC_normal))
15022     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
15023 }
15024
15025 /* Given a tree pointer to a struct, class, union, or enum type node, return
15026    a pointer to the (string) tag name for the given type, or zero if the type
15027    was declared without a tag.  */
15028
15029 static const char *
15030 type_tag (const_tree type)
15031 {
15032   const char *name = 0;
15033
15034   if (TYPE_NAME (type) != 0)
15035     {
15036       tree t = 0;
15037
15038       /* Find the IDENTIFIER_NODE for the type name.  */
15039       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
15040         t = TYPE_NAME (type);
15041
15042       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
15043          a TYPE_DECL node, regardless of whether or not a `typedef' was
15044          involved.  */
15045       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15046                && ! DECL_IGNORED_P (TYPE_NAME (type)))
15047         {
15048           /* We want to be extra verbose.  Don't call dwarf_name if
15049              DECL_NAME isn't set.  The default hook for decl_printable_name
15050              doesn't like that, and in this context it's correct to return
15051              0, instead of "<anonymous>" or the like.  */
15052           if (DECL_NAME (TYPE_NAME (type)))
15053             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
15054         }
15055
15056       /* Now get the name as a string, or invent one.  */
15057       if (!name && t != 0)
15058         name = IDENTIFIER_POINTER (t);
15059     }
15060
15061   return (name == 0 || *name == '\0') ? 0 : name;
15062 }
15063
15064 /* Return the type associated with a data member, make a special check
15065    for bit field types.  */
15066
15067 static inline tree
15068 member_declared_type (const_tree member)
15069 {
15070   return (DECL_BIT_FIELD_TYPE (member)
15071           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
15072 }
15073
15074 /* Get the decl's label, as described by its RTL. This may be different
15075    from the DECL_NAME name used in the source file.  */
15076
15077 #if 0
15078 static const char *
15079 decl_start_label (tree decl)
15080 {
15081   rtx x;
15082   const char *fnname;
15083
15084   x = DECL_RTL (decl);
15085   gcc_assert (MEM_P (x));
15086
15087   x = XEXP (x, 0);
15088   gcc_assert (GET_CODE (x) == SYMBOL_REF);
15089
15090   fnname = XSTR (x, 0);
15091   return fnname;
15092 }
15093 #endif
15094 \f
15095 /* These routines generate the internal representation of the DIE's for
15096    the compilation unit.  Debugging information is collected by walking
15097    the declaration trees passed in from dwarf2out_decl().  */
15098
15099 static void
15100 gen_array_type_die (tree type, dw_die_ref context_die)
15101 {
15102   dw_die_ref scope_die = scope_die_for (type, context_die);
15103   dw_die_ref array_die;
15104
15105   /* GNU compilers represent multidimensional array types as sequences of one
15106      dimensional array types whose element types are themselves array types.
15107      We sometimes squish that down to a single array_type DIE with multiple
15108      subscripts in the Dwarf debugging info.  The draft Dwarf specification
15109      say that we are allowed to do this kind of compression in C, because
15110      there is no difference between an array of arrays and a multidimensional
15111      array.  We don't do this for Ada to remain as close as possible to the
15112      actual representation, which is especially important against the language
15113      flexibilty wrt arrays of variable size.  */
15114
15115   bool collapse_nested_arrays = !is_ada ();
15116   tree element_type;
15117
15118   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
15119      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
15120   if (TYPE_STRING_FLAG (type)
15121       && TREE_CODE (type) == ARRAY_TYPE
15122       && is_fortran ()
15123       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
15124     {
15125       HOST_WIDE_INT size;
15126
15127       array_die = new_die (DW_TAG_string_type, scope_die, type);
15128       add_name_attribute (array_die, type_tag (type));
15129       equate_type_number_to_die (type, array_die);
15130       size = int_size_in_bytes (type);
15131       if (size >= 0)
15132         add_AT_unsigned (array_die, DW_AT_byte_size, size);
15133       else if (TYPE_DOMAIN (type) != NULL_TREE
15134                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
15135                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
15136         {
15137           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
15138           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
15139
15140           size = int_size_in_bytes (TREE_TYPE (szdecl));
15141           if (loc && size > 0)
15142             {
15143               add_AT_location_description (array_die, DW_AT_string_length, loc);
15144               if (size != DWARF2_ADDR_SIZE)
15145                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15146             }
15147         }
15148       return;
15149     }
15150
15151   /* ??? The SGI dwarf reader fails for array of array of enum types
15152      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
15153      array type comes before the outer array type.  We thus call gen_type_die
15154      before we new_die and must prevent nested array types collapsing for this
15155      target.  */
15156
15157 #ifdef MIPS_DEBUGGING_INFO
15158   gen_type_die (TREE_TYPE (type), context_die);
15159   collapse_nested_arrays = false;
15160 #endif
15161
15162   array_die = new_die (DW_TAG_array_type, scope_die, type);
15163   add_name_attribute (array_die, type_tag (type));
15164   equate_type_number_to_die (type, array_die);
15165
15166   if (TREE_CODE (type) == VECTOR_TYPE)
15167     {
15168       /* The frontend feeds us a representation for the vector as a struct
15169          containing an array.  Pull out the array type.  */
15170       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
15171       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
15172     }
15173
15174   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
15175   if (is_fortran ()
15176       && TREE_CODE (type) == ARRAY_TYPE
15177       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
15178       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
15179     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
15180
15181 #if 0
15182   /* We default the array ordering.  SDB will probably do
15183      the right things even if DW_AT_ordering is not present.  It's not even
15184      an issue until we start to get into multidimensional arrays anyway.  If
15185      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
15186      then we'll have to put the DW_AT_ordering attribute back in.  (But if
15187      and when we find out that we need to put these in, we will only do so
15188      for multidimensional arrays.  */
15189   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
15190 #endif
15191
15192 #ifdef MIPS_DEBUGGING_INFO
15193   /* The SGI compilers handle arrays of unknown bound by setting
15194      AT_declaration and not emitting any subrange DIEs.  */
15195   if (! TYPE_DOMAIN (type))
15196     add_AT_flag (array_die, DW_AT_declaration, 1);
15197   else
15198 #endif
15199     add_subscript_info (array_die, type, collapse_nested_arrays);
15200
15201   /* Add representation of the type of the elements of this array type and
15202      emit the corresponding DIE if we haven't done it already.  */  
15203   element_type = TREE_TYPE (type);
15204   if (collapse_nested_arrays)
15205     while (TREE_CODE (element_type) == ARRAY_TYPE)
15206       {
15207         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
15208           break;
15209         element_type = TREE_TYPE (element_type);
15210       }
15211
15212 #ifndef MIPS_DEBUGGING_INFO
15213   gen_type_die (element_type, context_die);
15214 #endif
15215
15216   add_type_attribute (array_die, element_type, 0, 0, context_die);
15217
15218   if (get_AT (array_die, DW_AT_name))
15219     add_pubtype (type, array_die);
15220 }
15221
15222 static dw_loc_descr_ref
15223 descr_info_loc (tree val, tree base_decl)
15224 {
15225   HOST_WIDE_INT size;
15226   dw_loc_descr_ref loc, loc2;
15227   enum dwarf_location_atom op;
15228
15229   if (val == base_decl)
15230     return new_loc_descr (DW_OP_push_object_address, 0, 0);
15231
15232   switch (TREE_CODE (val))
15233     {
15234     CASE_CONVERT:
15235       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15236     case VAR_DECL:
15237       return loc_descriptor_from_tree (val, 0);
15238     case INTEGER_CST:
15239       if (host_integerp (val, 0))
15240         return int_loc_descriptor (tree_low_cst (val, 0));
15241       break;
15242     case INDIRECT_REF:
15243       size = int_size_in_bytes (TREE_TYPE (val));
15244       if (size < 0)
15245         break;
15246       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15247       if (!loc)
15248         break;
15249       if (size == DWARF2_ADDR_SIZE)
15250         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
15251       else
15252         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
15253       return loc;
15254     case POINTER_PLUS_EXPR:
15255     case PLUS_EXPR:
15256       if (host_integerp (TREE_OPERAND (val, 1), 1)
15257           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
15258              < 16384)
15259         {
15260           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15261           if (!loc)
15262             break;
15263           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
15264         }
15265       else
15266         {
15267           op = DW_OP_plus;
15268         do_binop:
15269           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15270           if (!loc)
15271             break;
15272           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
15273           if (!loc2)
15274             break;
15275           add_loc_descr (&loc, loc2);
15276           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
15277         }
15278       return loc;
15279     case MINUS_EXPR:
15280       op = DW_OP_minus;
15281       goto do_binop;
15282     case MULT_EXPR:
15283       op = DW_OP_mul;
15284       goto do_binop;
15285     case EQ_EXPR:
15286       op = DW_OP_eq;
15287       goto do_binop;
15288     case NE_EXPR:
15289       op = DW_OP_ne;
15290       goto do_binop;
15291     default:
15292       break;
15293     }
15294   return NULL;
15295 }
15296
15297 static void
15298 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
15299                       tree val, tree base_decl)
15300 {
15301   dw_loc_descr_ref loc;
15302
15303   if (host_integerp (val, 0))
15304     {
15305       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
15306       return;
15307     }
15308
15309   loc = descr_info_loc (val, base_decl);
15310   if (!loc)
15311     return;
15312
15313   add_AT_loc (die, attr, loc);
15314 }
15315
15316 /* This routine generates DIE for array with hidden descriptor, details
15317    are filled into *info by a langhook.  */
15318
15319 static void
15320 gen_descr_array_type_die (tree type, struct array_descr_info *info,
15321                           dw_die_ref context_die)
15322 {
15323   dw_die_ref scope_die = scope_die_for (type, context_die);
15324   dw_die_ref array_die;
15325   int dim;
15326
15327   array_die = new_die (DW_TAG_array_type, scope_die, type);
15328   add_name_attribute (array_die, type_tag (type));
15329   equate_type_number_to_die (type, array_die);
15330
15331   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
15332   if (is_fortran ()
15333       && info->ndimensions >= 2)
15334     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
15335
15336   if (info->data_location)
15337     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
15338                           info->base_decl);
15339   if (info->associated)
15340     add_descr_info_field (array_die, DW_AT_associated, info->associated,
15341                           info->base_decl);
15342   if (info->allocated)
15343     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
15344                           info->base_decl);
15345
15346   for (dim = 0; dim < info->ndimensions; dim++)
15347     {
15348       dw_die_ref subrange_die
15349         = new_die (DW_TAG_subrange_type, array_die, NULL);
15350
15351       if (info->dimen[dim].lower_bound)
15352         {
15353           /* If it is the default value, omit it.  */
15354           if ((is_c_family () || is_java ())
15355               && integer_zerop (info->dimen[dim].lower_bound))
15356             ;
15357           else if (is_fortran ()
15358                    && integer_onep (info->dimen[dim].lower_bound))
15359             ;
15360           else
15361             add_descr_info_field (subrange_die, DW_AT_lower_bound,
15362                                   info->dimen[dim].lower_bound,
15363                                   info->base_decl);
15364         }
15365       if (info->dimen[dim].upper_bound)
15366         add_descr_info_field (subrange_die, DW_AT_upper_bound,
15367                               info->dimen[dim].upper_bound,
15368                               info->base_decl);
15369       if (info->dimen[dim].stride)
15370         add_descr_info_field (subrange_die, DW_AT_byte_stride,
15371                               info->dimen[dim].stride,
15372                               info->base_decl);
15373     }
15374
15375   gen_type_die (info->element_type, context_die);
15376   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
15377
15378   if (get_AT (array_die, DW_AT_name))
15379     add_pubtype (type, array_die);
15380 }
15381
15382 #if 0
15383 static void
15384 gen_entry_point_die (tree decl, dw_die_ref context_die)
15385 {
15386   tree origin = decl_ultimate_origin (decl);
15387   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
15388
15389   if (origin != NULL)
15390     add_abstract_origin_attribute (decl_die, origin);
15391   else
15392     {
15393       add_name_and_src_coords_attributes (decl_die, decl);
15394       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
15395                           0, 0, context_die);
15396     }
15397
15398   if (DECL_ABSTRACT (decl))
15399     equate_decl_number_to_die (decl, decl_die);
15400   else
15401     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
15402 }
15403 #endif
15404
15405 /* Walk through the list of incomplete types again, trying once more to
15406    emit full debugging info for them.  */
15407
15408 static void
15409 retry_incomplete_types (void)
15410 {
15411   int i;
15412
15413   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
15414     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
15415 }
15416
15417 /* Determine what tag to use for a record type.  */
15418
15419 static enum dwarf_tag
15420 record_type_tag (tree type)
15421 {
15422   if (! lang_hooks.types.classify_record)
15423     return DW_TAG_structure_type;
15424
15425   switch (lang_hooks.types.classify_record (type))
15426     {
15427     case RECORD_IS_STRUCT:
15428       return DW_TAG_structure_type;
15429
15430     case RECORD_IS_CLASS:
15431       return DW_TAG_class_type;
15432
15433     case RECORD_IS_INTERFACE:
15434       if (dwarf_version >= 3 || !dwarf_strict)
15435         return DW_TAG_interface_type;
15436       return DW_TAG_structure_type;
15437
15438     default:
15439       gcc_unreachable ();
15440     }
15441 }
15442
15443 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
15444    include all of the information about the enumeration values also. Each
15445    enumerated type name/value is listed as a child of the enumerated type
15446    DIE.  */
15447
15448 static dw_die_ref
15449 gen_enumeration_type_die (tree type, dw_die_ref context_die)
15450 {
15451   dw_die_ref type_die = lookup_type_die (type);
15452
15453   if (type_die == NULL)
15454     {
15455       type_die = new_die (DW_TAG_enumeration_type,
15456                           scope_die_for (type, context_die), type);
15457       equate_type_number_to_die (type, type_die);
15458       add_name_attribute (type_die, type_tag (type));
15459     }
15460   else if (! TYPE_SIZE (type))
15461     return type_die;
15462   else
15463     remove_AT (type_die, DW_AT_declaration);
15464
15465   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
15466      given enum type is incomplete, do not generate the DW_AT_byte_size
15467      attribute or the DW_AT_element_list attribute.  */
15468   if (TYPE_SIZE (type))
15469     {
15470       tree link;
15471
15472       TREE_ASM_WRITTEN (type) = 1;
15473       add_byte_size_attribute (type_die, type);
15474       if (TYPE_STUB_DECL (type) != NULL_TREE)
15475         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
15476
15477       /* If the first reference to this type was as the return type of an
15478          inline function, then it may not have a parent.  Fix this now.  */
15479       if (type_die->die_parent == NULL)
15480         add_child_die (scope_die_for (type, context_die), type_die);
15481
15482       for (link = TYPE_VALUES (type);
15483            link != NULL; link = TREE_CHAIN (link))
15484         {
15485           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
15486           tree value = TREE_VALUE (link);
15487
15488           add_name_attribute (enum_die,
15489                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
15490
15491           if (TREE_CODE (value) == CONST_DECL)
15492             value = DECL_INITIAL (value);
15493
15494           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
15495             /* DWARF2 does not provide a way of indicating whether or
15496                not enumeration constants are signed or unsigned.  GDB
15497                always assumes the values are signed, so we output all
15498                values as if they were signed.  That means that
15499                enumeration constants with very large unsigned values
15500                will appear to have negative values in the debugger.  */
15501             add_AT_int (enum_die, DW_AT_const_value,
15502                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
15503         }
15504     }
15505   else
15506     add_AT_flag (type_die, DW_AT_declaration, 1);
15507
15508   if (get_AT (type_die, DW_AT_name))
15509     add_pubtype (type, type_die);
15510
15511   return type_die;
15512 }
15513
15514 /* Generate a DIE to represent either a real live formal parameter decl or to
15515    represent just the type of some formal parameter position in some function
15516    type.
15517
15518    Note that this routine is a bit unusual because its argument may be a
15519    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
15520    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
15521    node.  If it's the former then this function is being called to output a
15522    DIE to represent a formal parameter object (or some inlining thereof).  If
15523    it's the latter, then this function is only being called to output a
15524    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
15525    argument type of some subprogram type.
15526    If EMIT_NAME_P is true, name and source coordinate attributes
15527    are emitted.  */
15528
15529 static dw_die_ref
15530 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
15531                           dw_die_ref context_die)
15532 {
15533   tree node_or_origin = node ? node : origin;
15534   dw_die_ref parm_die
15535     = new_die (DW_TAG_formal_parameter, context_die, node);
15536
15537   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
15538     {
15539     case tcc_declaration:
15540       if (!origin)
15541         origin = decl_ultimate_origin (node);
15542       if (origin != NULL)
15543         add_abstract_origin_attribute (parm_die, origin);
15544       else
15545         {
15546           tree type = TREE_TYPE (node);
15547           if (emit_name_p)
15548             add_name_and_src_coords_attributes (parm_die, node);
15549           if (decl_by_reference_p (node))
15550             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
15551                                 context_die);
15552           else
15553             add_type_attribute (parm_die, type,
15554                                 TREE_READONLY (node),
15555                                 TREE_THIS_VOLATILE (node),
15556                                 context_die);
15557           if (DECL_ARTIFICIAL (node))
15558             add_AT_flag (parm_die, DW_AT_artificial, 1);
15559         }
15560
15561       if (node && node != origin)
15562         equate_decl_number_to_die (node, parm_die);
15563       if (! DECL_ABSTRACT (node_or_origin))
15564         add_location_or_const_value_attribute (parm_die, node_or_origin,
15565                                                DW_AT_location);
15566
15567       break;
15568
15569     case tcc_type:
15570       /* We were called with some kind of a ..._TYPE node.  */
15571       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
15572       break;
15573
15574     default:
15575       gcc_unreachable ();
15576     }
15577
15578   return parm_die;
15579 }
15580
15581 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
15582    children DW_TAG_formal_parameter DIEs representing the arguments of the
15583    parameter pack.
15584
15585    PARM_PACK must be a function parameter pack.
15586    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
15587    must point to the subsequent arguments of the function PACK_ARG belongs to.
15588    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
15589    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
15590    following the last one for which a DIE was generated.  */
15591
15592 static dw_die_ref
15593 gen_formal_parameter_pack_die  (tree parm_pack,
15594                                 tree pack_arg,
15595                                 dw_die_ref subr_die,
15596                                 tree *next_arg)
15597 {
15598   tree arg;
15599   dw_die_ref parm_pack_die;
15600
15601   gcc_assert (parm_pack
15602               && lang_hooks.function_parameter_pack_p (parm_pack)
15603               && DECL_NAME (parm_pack)
15604               && subr_die);
15605
15606   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
15607   add_AT_string (parm_pack_die, DW_AT_name,
15608                  IDENTIFIER_POINTER (DECL_NAME (parm_pack)));
15609
15610   for (arg = pack_arg; arg; arg = TREE_CHAIN (arg))
15611     {
15612       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
15613                                                                  parm_pack))
15614         break;
15615       gen_formal_parameter_die (arg, NULL,
15616                                 false /* Don't emit name attribute.  */,
15617                                 parm_pack_die);
15618     }
15619   if (next_arg)
15620     *next_arg = arg;
15621   return parm_pack_die;
15622 }
15623
15624 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
15625    at the end of an (ANSI prototyped) formal parameters list.  */
15626
15627 static void
15628 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
15629 {
15630   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
15631 }
15632
15633 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
15634    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
15635    parameters as specified in some function type specification (except for
15636    those which appear as part of a function *definition*).  */
15637
15638 static void
15639 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
15640 {
15641   tree link;
15642   tree formal_type = NULL;
15643   tree first_parm_type;
15644   tree arg;
15645
15646   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
15647     {
15648       arg = DECL_ARGUMENTS (function_or_method_type);
15649       function_or_method_type = TREE_TYPE (function_or_method_type);
15650     }
15651   else
15652     arg = NULL_TREE;
15653
15654   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
15655
15656   /* Make our first pass over the list of formal parameter types and output a
15657      DW_TAG_formal_parameter DIE for each one.  */
15658   for (link = first_parm_type; link; )
15659     {
15660       dw_die_ref parm_die;
15661
15662       formal_type = TREE_VALUE (link);
15663       if (formal_type == void_type_node)
15664         break;
15665
15666       /* Output a (nameless) DIE to represent the formal parameter itself.  */
15667       parm_die = gen_formal_parameter_die (formal_type, NULL,
15668                                            true /* Emit name attribute.  */,
15669                                            context_die);
15670       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
15671            && link == first_parm_type)
15672           || (arg && DECL_ARTIFICIAL (arg)))
15673         add_AT_flag (parm_die, DW_AT_artificial, 1);
15674
15675       link = TREE_CHAIN (link);
15676       if (arg)
15677         arg = TREE_CHAIN (arg);
15678     }
15679
15680   /* If this function type has an ellipsis, add a
15681      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
15682   if (formal_type != void_type_node)
15683     gen_unspecified_parameters_die (function_or_method_type, context_die);
15684
15685   /* Make our second (and final) pass over the list of formal parameter types
15686      and output DIEs to represent those types (as necessary).  */
15687   for (link = TYPE_ARG_TYPES (function_or_method_type);
15688        link && TREE_VALUE (link);
15689        link = TREE_CHAIN (link))
15690     gen_type_die (TREE_VALUE (link), context_die);
15691 }
15692
15693 /* We want to generate the DIE for TYPE so that we can generate the
15694    die for MEMBER, which has been defined; we will need to refer back
15695    to the member declaration nested within TYPE.  If we're trying to
15696    generate minimal debug info for TYPE, processing TYPE won't do the
15697    trick; we need to attach the member declaration by hand.  */
15698
15699 static void
15700 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
15701 {
15702   gen_type_die (type, context_die);
15703
15704   /* If we're trying to avoid duplicate debug info, we may not have
15705      emitted the member decl for this function.  Emit it now.  */
15706   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
15707       && ! lookup_decl_die (member))
15708     {
15709       dw_die_ref type_die;
15710       gcc_assert (!decl_ultimate_origin (member));
15711
15712       push_decl_scope (type);
15713       type_die = lookup_type_die (type);
15714       if (TREE_CODE (member) == FUNCTION_DECL)
15715         gen_subprogram_die (member, type_die);
15716       else if (TREE_CODE (member) == FIELD_DECL)
15717         {
15718           /* Ignore the nameless fields that are used to skip bits but handle
15719              C++ anonymous unions and structs.  */
15720           if (DECL_NAME (member) != NULL_TREE
15721               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
15722               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
15723             {
15724               gen_type_die (member_declared_type (member), type_die);
15725               gen_field_die (member, type_die);
15726             }
15727         }
15728       else
15729         gen_variable_die (member, NULL_TREE, type_die);
15730
15731       pop_decl_scope ();
15732     }
15733 }
15734
15735 /* Generate the DWARF2 info for the "abstract" instance of a function which we
15736    may later generate inlined and/or out-of-line instances of.  */
15737
15738 static void
15739 dwarf2out_abstract_function (tree decl)
15740 {
15741   dw_die_ref old_die;
15742   tree save_fn;
15743   tree context;
15744   int was_abstract = DECL_ABSTRACT (decl);
15745   htab_t old_decl_loc_table;
15746
15747   /* Make sure we have the actual abstract inline, not a clone.  */
15748   decl = DECL_ORIGIN (decl);
15749
15750   old_die = lookup_decl_die (decl);
15751   if (old_die && get_AT (old_die, DW_AT_inline))
15752     /* We've already generated the abstract instance.  */
15753     return;
15754
15755   /* We can be called while recursively when seeing block defining inlined subroutine
15756      DIE.  Be sure to not clobber the outer location table nor use it or we would
15757      get locations in abstract instantces.  */
15758   old_decl_loc_table = decl_loc_table;
15759   decl_loc_table = NULL;
15760
15761   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
15762      we don't get confused by DECL_ABSTRACT.  */
15763   if (debug_info_level > DINFO_LEVEL_TERSE)
15764     {
15765       context = decl_class_context (decl);
15766       if (context)
15767         gen_type_die_for_member
15768           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
15769     }
15770
15771   /* Pretend we've just finished compiling this function.  */
15772   save_fn = current_function_decl;
15773   current_function_decl = decl;
15774   push_cfun (DECL_STRUCT_FUNCTION (decl));
15775
15776   set_decl_abstract_flags (decl, 1);
15777   dwarf2out_decl (decl);
15778   if (! was_abstract)
15779     set_decl_abstract_flags (decl, 0);
15780
15781   current_function_decl = save_fn;
15782   decl_loc_table = old_decl_loc_table;
15783   pop_cfun ();
15784 }
15785
15786 /* Helper function of premark_used_types() which gets called through
15787    htab_traverse.
15788
15789    Marks the DIE of a given type in *SLOT as perennial, so it never gets
15790    marked as unused by prune_unused_types.  */
15791
15792 static int
15793 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
15794 {
15795   tree type;
15796   dw_die_ref die;
15797
15798   type = (tree) *slot;
15799   die = lookup_type_die (type);
15800   if (die != NULL)
15801     die->die_perennial_p = 1;
15802   return 1;
15803 }
15804
15805 /* Helper function of premark_types_used_by_global_vars which gets called
15806    through htab_traverse.
15807
15808    Marks the DIE of a given type in *SLOT as perennial, so it never gets
15809    marked as unused by prune_unused_types. The DIE of the type is marked
15810    only if the global variable using the type will actually be emitted.  */
15811
15812 static int
15813 premark_types_used_by_global_vars_helper (void **slot,
15814                                           void *data ATTRIBUTE_UNUSED)
15815 {
15816   struct types_used_by_vars_entry *entry;
15817   dw_die_ref die;
15818
15819   entry = (struct types_used_by_vars_entry *) *slot;
15820   gcc_assert (entry->type != NULL
15821               && entry->var_decl != NULL);
15822   die = lookup_type_die (entry->type);
15823   if (die)
15824     {
15825       /* Ask cgraph if the global variable really is to be emitted.
15826          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
15827       struct varpool_node *node = varpool_node (entry->var_decl);
15828       if (node->needed)
15829         {
15830           die->die_perennial_p = 1;
15831           /* Keep the parent DIEs as well.  */
15832           while ((die = die->die_parent) && die->die_perennial_p == 0)
15833             die->die_perennial_p = 1;
15834         }
15835     }
15836   return 1;
15837 }
15838
15839 /* Mark all members of used_types_hash as perennial.  */
15840
15841 static void
15842 premark_used_types (void)
15843 {
15844   if (cfun && cfun->used_types_hash)
15845     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
15846 }
15847
15848 /* Mark all members of types_used_by_vars_entry as perennial.  */
15849
15850 static void
15851 premark_types_used_by_global_vars (void)
15852 {
15853   if (types_used_by_vars_hash)
15854     htab_traverse (types_used_by_vars_hash,
15855                    premark_types_used_by_global_vars_helper, NULL);
15856 }
15857
15858 /* Generate a DIE to represent a declared function (either file-scope or
15859    block-local).  */
15860
15861 static void
15862 gen_subprogram_die (tree decl, dw_die_ref context_die)
15863 {
15864   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15865   tree origin = decl_ultimate_origin (decl);
15866   dw_die_ref subr_die;
15867   tree fn_arg_types;
15868   tree outer_scope;
15869   dw_die_ref old_die = lookup_decl_die (decl);
15870   int declaration = (current_function_decl != decl
15871                      || class_or_namespace_scope_p (context_die));
15872
15873   premark_used_types ();
15874
15875   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
15876      started to generate the abstract instance of an inline, decided to output
15877      its containing class, and proceeded to emit the declaration of the inline
15878      from the member list for the class.  If so, DECLARATION takes priority;
15879      we'll get back to the abstract instance when done with the class.  */
15880
15881   /* The class-scope declaration DIE must be the primary DIE.  */
15882   if (origin && declaration && class_or_namespace_scope_p (context_die))
15883     {
15884       origin = NULL;
15885       gcc_assert (!old_die);
15886     }
15887
15888   /* Now that the C++ front end lazily declares artificial member fns, we
15889      might need to retrofit the declaration into its class.  */
15890   if (!declaration && !origin && !old_die
15891       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
15892       && !class_or_namespace_scope_p (context_die)
15893       && debug_info_level > DINFO_LEVEL_TERSE)
15894     old_die = force_decl_die (decl);
15895
15896   if (origin != NULL)
15897     {
15898       gcc_assert (!declaration || local_scope_p (context_die));
15899
15900       /* Fixup die_parent for the abstract instance of a nested
15901          inline function.  */
15902       if (old_die && old_die->die_parent == NULL)
15903         add_child_die (context_die, old_die);
15904
15905       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15906       add_abstract_origin_attribute (subr_die, origin);
15907     }
15908   else if (old_die)
15909     {
15910       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
15911       struct dwarf_file_data * file_index = lookup_filename (s.file);
15912
15913       if (!get_AT_flag (old_die, DW_AT_declaration)
15914           /* We can have a normal definition following an inline one in the
15915              case of redefinition of GNU C extern inlines.
15916              It seems reasonable to use AT_specification in this case.  */
15917           && !get_AT (old_die, DW_AT_inline))
15918         {
15919           /* Detect and ignore this case, where we are trying to output
15920              something we have already output.  */
15921           return;
15922         }
15923
15924       /* If the definition comes from the same place as the declaration,
15925          maybe use the old DIE.  We always want the DIE for this function
15926          that has the *_pc attributes to be under comp_unit_die so the
15927          debugger can find it.  We also need to do this for abstract
15928          instances of inlines, since the spec requires the out-of-line copy
15929          to have the same parent.  For local class methods, this doesn't
15930          apply; we just use the old DIE.  */
15931       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
15932           && (DECL_ARTIFICIAL (decl)
15933               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
15934                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
15935                       == (unsigned) s.line))))
15936         {
15937           subr_die = old_die;
15938
15939           /* Clear out the declaration attribute and the formal parameters.
15940              Do not remove all children, because it is possible that this
15941              declaration die was forced using force_decl_die(). In such
15942              cases die that forced declaration die (e.g. TAG_imported_module)
15943              is one of the children that we do not want to remove.  */
15944           remove_AT (subr_die, DW_AT_declaration);
15945           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
15946         }
15947       else
15948         {
15949           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15950           add_AT_specification (subr_die, old_die);
15951           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
15952             add_AT_file (subr_die, DW_AT_decl_file, file_index);
15953           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
15954             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
15955         }
15956     }
15957   else
15958     {
15959       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15960
15961       if (TREE_PUBLIC (decl))
15962         add_AT_flag (subr_die, DW_AT_external, 1);
15963
15964       add_name_and_src_coords_attributes (subr_die, decl);
15965       if (debug_info_level > DINFO_LEVEL_TERSE)
15966         {
15967           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
15968           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
15969                               0, 0, context_die);
15970         }
15971
15972       add_pure_or_virtual_attribute (subr_die, decl);
15973       if (DECL_ARTIFICIAL (decl))
15974         add_AT_flag (subr_die, DW_AT_artificial, 1);
15975
15976       if (TREE_PROTECTED (decl))
15977         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
15978       else if (TREE_PRIVATE (decl))
15979         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
15980     }
15981
15982   if (declaration)
15983     {
15984       if (!old_die || !get_AT (old_die, DW_AT_inline))
15985         {
15986           add_AT_flag (subr_die, DW_AT_declaration, 1);
15987
15988           /* If this is an explicit function declaration then generate
15989              a DW_AT_explicit attribute.  */
15990           if (lang_hooks.decls.function_decl_explicit_p (decl)
15991               && (dwarf_version >= 3 || !dwarf_strict))
15992             add_AT_flag (subr_die, DW_AT_explicit, 1);
15993
15994           /* The first time we see a member function, it is in the context of
15995              the class to which it belongs.  We make sure of this by emitting
15996              the class first.  The next time is the definition, which is
15997              handled above.  The two may come from the same source text.
15998
15999              Note that force_decl_die() forces function declaration die. It is
16000              later reused to represent definition.  */
16001           equate_decl_number_to_die (decl, subr_die);
16002         }
16003     }
16004   else if (DECL_ABSTRACT (decl))
16005     {
16006       if (DECL_DECLARED_INLINE_P (decl))
16007         {
16008           if (cgraph_function_possibly_inlined_p (decl))
16009             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
16010           else
16011             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
16012         }
16013       else
16014         {
16015           if (cgraph_function_possibly_inlined_p (decl))
16016             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
16017           else
16018             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
16019         }
16020
16021       if (DECL_DECLARED_INLINE_P (decl)
16022           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
16023         add_AT_flag (subr_die, DW_AT_artificial, 1);
16024
16025       equate_decl_number_to_die (decl, subr_die);
16026     }
16027   else if (!DECL_EXTERNAL (decl))
16028     {
16029       HOST_WIDE_INT cfa_fb_offset;
16030
16031       if (!old_die || !get_AT (old_die, DW_AT_inline))
16032         equate_decl_number_to_die (decl, subr_die);
16033
16034       if (!flag_reorder_blocks_and_partition)
16035         {
16036           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
16037                                        current_function_funcdef_no);
16038           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
16039           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16040                                        current_function_funcdef_no);
16041           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
16042
16043           add_pubname (decl, subr_die);
16044           add_arange (decl, subr_die);
16045         }
16046       else
16047         {  /* Do nothing for now; maybe need to duplicate die, one for
16048               hot section and one for cold section, then use the hot/cold
16049               section begin/end labels to generate the aranges...  */
16050           /*
16051             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
16052             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
16053             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
16054             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
16055
16056             add_pubname (decl, subr_die);
16057             add_arange (decl, subr_die);
16058             add_arange (decl, subr_die);
16059            */
16060         }
16061
16062 #ifdef MIPS_DEBUGGING_INFO
16063       /* Add a reference to the FDE for this routine.  */
16064       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
16065 #endif
16066
16067       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
16068
16069       /* We define the "frame base" as the function's CFA.  This is more
16070          convenient for several reasons: (1) It's stable across the prologue
16071          and epilogue, which makes it better than just a frame pointer,
16072          (2) With dwarf3, there exists a one-byte encoding that allows us
16073          to reference the .debug_frame data by proxy, but failing that,
16074          (3) We can at least reuse the code inspection and interpretation
16075          code that determines the CFA position at various points in the
16076          function.  */
16077       if (dwarf_version >= 3)
16078         {
16079           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
16080           add_AT_loc (subr_die, DW_AT_frame_base, op);
16081         }
16082       else
16083         {
16084           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
16085           if (list->dw_loc_next)
16086             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
16087           else
16088             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
16089         }
16090
16091       /* Compute a displacement from the "steady-state frame pointer" to
16092          the CFA.  The former is what all stack slots and argument slots
16093          will reference in the rtl; the later is what we've told the
16094          debugger about.  We'll need to adjust all frame_base references
16095          by this displacement.  */
16096       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
16097
16098       if (cfun->static_chain_decl)
16099         add_AT_location_description (subr_die, DW_AT_static_link,
16100                  loc_list_from_tree (cfun->static_chain_decl, 2));
16101     }
16102
16103   /* Generate child dies for template paramaters.  */
16104   if (debug_info_level > DINFO_LEVEL_TERSE)
16105     gen_generic_params_dies (decl);
16106
16107   /* Now output descriptions of the arguments for this function. This gets
16108      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
16109      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
16110      `...' at the end of the formal parameter list.  In order to find out if
16111      there was a trailing ellipsis or not, we must instead look at the type
16112      associated with the FUNCTION_DECL.  This will be a node of type
16113      FUNCTION_TYPE. If the chain of type nodes hanging off of this
16114      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
16115      an ellipsis at the end.  */
16116
16117   /* In the case where we are describing a mere function declaration, all we
16118      need to do here (and all we *can* do here) is to describe the *types* of
16119      its formal parameters.  */
16120   if (debug_info_level <= DINFO_LEVEL_TERSE)
16121     ;
16122   else if (declaration)
16123     gen_formal_types_die (decl, subr_die);
16124   else
16125     {
16126       /* Generate DIEs to represent all known formal parameters.  */
16127       tree parm = DECL_ARGUMENTS (decl);
16128       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
16129       tree generic_decl_parm = generic_decl
16130                                 ? DECL_ARGUMENTS (generic_decl)
16131                                 : NULL;
16132
16133       /* Now we want to walk the list of parameters of the function and
16134          emit their relevant DIEs.
16135
16136          We consider the case of DECL being an instance of a generic function
16137          as well as it being a normal function.
16138
16139          If DECL is an instance of a generic function we walk the
16140          parameters of the generic function declaration _and_ the parameters of
16141          DECL itself. This is useful because we want to emit specific DIEs for
16142          function parameter packs and those are declared as part of the
16143          generic function declaration. In that particular case,
16144          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
16145          That DIE has children DIEs representing the set of arguments
16146          of the pack. Note that the set of pack arguments can be empty.
16147          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
16148          children DIE.
16149         
16150          Otherwise, we just consider the parameters of DECL.  */
16151       while (generic_decl_parm || parm)
16152         {
16153           if (generic_decl_parm
16154               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
16155             gen_formal_parameter_pack_die (generic_decl_parm,
16156                                            parm, subr_die,
16157                                            &parm);
16158           else if (parm)
16159             {
16160               gen_decl_die (parm, NULL, subr_die);
16161               parm = TREE_CHAIN (parm);
16162             }
16163
16164           if (generic_decl_parm)
16165             generic_decl_parm = TREE_CHAIN (generic_decl_parm);
16166         }
16167
16168       /* Decide whether we need an unspecified_parameters DIE at the end.
16169          There are 2 more cases to do this for: 1) the ansi ... declaration -
16170          this is detectable when the end of the arg list is not a
16171          void_type_node 2) an unprototyped function declaration (not a
16172          definition).  This just means that we have no info about the
16173          parameters at all.  */
16174       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
16175       if (fn_arg_types != NULL)
16176         {
16177           /* This is the prototyped case, check for....  */
16178           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
16179             gen_unspecified_parameters_die (decl, subr_die);
16180         }
16181       else if (DECL_INITIAL (decl) == NULL_TREE)
16182         gen_unspecified_parameters_die (decl, subr_die);
16183     }
16184
16185   /* Output Dwarf info for all of the stuff within the body of the function
16186      (if it has one - it may be just a declaration).  */
16187   outer_scope = DECL_INITIAL (decl);
16188
16189   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
16190      a function.  This BLOCK actually represents the outermost binding contour
16191      for the function, i.e. the contour in which the function's formal
16192      parameters and labels get declared. Curiously, it appears that the front
16193      end doesn't actually put the PARM_DECL nodes for the current function onto
16194      the BLOCK_VARS list for this outer scope, but are strung off of the
16195      DECL_ARGUMENTS list for the function instead.
16196
16197      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
16198      the LABEL_DECL nodes for the function however, and we output DWARF info
16199      for those in decls_for_scope.  Just within the `outer_scope' there will be
16200      a BLOCK node representing the function's outermost pair of curly braces,
16201      and any blocks used for the base and member initializers of a C++
16202      constructor function.  */
16203   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
16204     {
16205       /* Emit a DW_TAG_variable DIE for a named return value.  */
16206       if (DECL_NAME (DECL_RESULT (decl)))
16207         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
16208
16209       current_function_has_inlines = 0;
16210       decls_for_scope (outer_scope, subr_die, 0);
16211
16212 #if 0 && defined (MIPS_DEBUGGING_INFO)
16213       if (current_function_has_inlines)
16214         {
16215           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
16216           if (! comp_unit_has_inlines)
16217             {
16218               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
16219               comp_unit_has_inlines = 1;
16220             }
16221         }
16222 #endif
16223     }
16224   /* Add the calling convention attribute if requested.  */
16225   add_calling_convention_attribute (subr_die, decl);
16226
16227 }
16228
16229 /* Returns a hash value for X (which really is a die_struct).  */
16230
16231 static hashval_t
16232 common_block_die_table_hash (const void *x)
16233 {
16234   const_dw_die_ref d = (const_dw_die_ref) x;
16235   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
16236 }
16237
16238 /* Return nonzero if decl_id and die_parent of die_struct X is the same
16239    as decl_id and die_parent of die_struct Y.  */
16240
16241 static int
16242 common_block_die_table_eq (const void *x, const void *y)
16243 {
16244   const_dw_die_ref d = (const_dw_die_ref) x;
16245   const_dw_die_ref e = (const_dw_die_ref) y;
16246   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
16247 }
16248
16249 /* Generate a DIE to represent a declared data object.
16250    Either DECL or ORIGIN must be non-null.  */
16251
16252 static void
16253 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
16254 {
16255   HOST_WIDE_INT off;
16256   tree com_decl;
16257   tree decl_or_origin = decl ? decl : origin;
16258   dw_die_ref var_die;
16259   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
16260   dw_die_ref origin_die;
16261   int declaration = (DECL_EXTERNAL (decl_or_origin)
16262                      /* If DECL is COMDAT and has not actually been
16263                         emitted, we cannot take its address; there
16264                         might end up being no definition anywhere in
16265                         the program.  For example, consider the C++
16266                         test case:
16267
16268                           template <class T>
16269                           struct S { static const int i = 7; };
16270
16271                           template <class T>
16272                           const int S<T>::i;
16273
16274                           int f() { return S<int>::i; }
16275
16276                         Here, S<int>::i is not DECL_EXTERNAL, but no
16277                         definition is required, so the compiler will
16278                         not emit a definition.  */
16279                      || (TREE_CODE (decl_or_origin) == VAR_DECL
16280                          && DECL_COMDAT (decl_or_origin)
16281                          && !TREE_ASM_WRITTEN (decl_or_origin))
16282                      || class_or_namespace_scope_p (context_die));
16283
16284   if (!origin)
16285     origin = decl_ultimate_origin (decl);
16286
16287   com_decl = fortran_common (decl_or_origin, &off);
16288
16289   /* Symbol in common gets emitted as a child of the common block, in the form
16290      of a data member.  */
16291   if (com_decl)
16292     {
16293       tree field;
16294       dw_die_ref com_die;
16295       dw_loc_list_ref loc;
16296       die_node com_die_arg;
16297
16298       var_die = lookup_decl_die (decl_or_origin);
16299       if (var_die)
16300         {
16301           if (get_AT (var_die, DW_AT_location) == NULL)
16302             {
16303               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
16304               if (loc)
16305                 {
16306                   if (off)
16307                     {
16308                       /* Optimize the common case.  */
16309                       if (single_element_loc_list_p (loc)
16310                           && loc->expr->dw_loc_opc == DW_OP_addr
16311                           && loc->expr->dw_loc_next == NULL
16312                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
16313                              == SYMBOL_REF)
16314                         loc->expr->dw_loc_oprnd1.v.val_addr
16315                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
16316                         else
16317                           loc_list_plus_const (loc, off);
16318                     }
16319                   add_AT_location_description (var_die, DW_AT_location, loc);
16320                   remove_AT (var_die, DW_AT_declaration);
16321                 }
16322             }
16323           return;
16324         }
16325
16326       if (common_block_die_table == NULL)
16327         common_block_die_table
16328           = htab_create_ggc (10, common_block_die_table_hash,
16329                              common_block_die_table_eq, NULL);
16330
16331       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
16332       com_die_arg.decl_id = DECL_UID (com_decl);
16333       com_die_arg.die_parent = context_die;
16334       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
16335       loc = loc_list_from_tree (com_decl, 2);
16336       if (com_die == NULL)
16337         {
16338           const char *cnam
16339             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
16340           void **slot;
16341
16342           com_die = new_die (DW_TAG_common_block, context_die, decl);
16343           add_name_and_src_coords_attributes (com_die, com_decl);
16344           if (loc)
16345             {
16346               add_AT_location_description (com_die, DW_AT_location, loc);
16347               /* Avoid sharing the same loc descriptor between
16348                  DW_TAG_common_block and DW_TAG_variable.  */
16349               loc = loc_list_from_tree (com_decl, 2);
16350             }
16351           else if (DECL_EXTERNAL (decl))
16352             add_AT_flag (com_die, DW_AT_declaration, 1);
16353           add_pubname_string (cnam, com_die); /* ??? needed? */
16354           com_die->decl_id = DECL_UID (com_decl);
16355           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
16356           *slot = (void *) com_die;
16357         }
16358       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
16359         {
16360           add_AT_location_description (com_die, DW_AT_location, loc);
16361           loc = loc_list_from_tree (com_decl, 2);
16362           remove_AT (com_die, DW_AT_declaration);
16363         }
16364       var_die = new_die (DW_TAG_variable, com_die, decl);
16365       add_name_and_src_coords_attributes (var_die, decl);
16366       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
16367                           TREE_THIS_VOLATILE (decl), context_die);
16368       add_AT_flag (var_die, DW_AT_external, 1);
16369       if (loc)
16370         {
16371           if (off)
16372             {
16373               /* Optimize the common case.  */
16374               if (single_element_loc_list_p (loc)
16375                   && loc->expr->dw_loc_opc == DW_OP_addr
16376                   && loc->expr->dw_loc_next == NULL
16377                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
16378                 loc->expr->dw_loc_oprnd1.v.val_addr
16379                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
16380               else
16381                 loc_list_plus_const (loc, off);
16382             }
16383           add_AT_location_description (var_die, DW_AT_location, loc);
16384         }
16385       else if (DECL_EXTERNAL (decl))
16386         add_AT_flag (var_die, DW_AT_declaration, 1);
16387       equate_decl_number_to_die (decl, var_die);
16388       return;
16389     }
16390
16391   /* If the compiler emitted a definition for the DECL declaration
16392      and if we already emitted a DIE for it, don't emit a second
16393      DIE for it again.  */
16394   if (old_die
16395       && declaration
16396       && old_die->die_parent == context_die)
16397     return;
16398
16399   /* For static data members, the declaration in the class is supposed
16400      to have DW_TAG_member tag; the specification should still be
16401      DW_TAG_variable referencing the DW_TAG_member DIE.  */
16402   if (declaration && class_scope_p (context_die))
16403     var_die = new_die (DW_TAG_member, context_die, decl);
16404   else
16405     var_die = new_die (DW_TAG_variable, context_die, decl);
16406
16407   origin_die = NULL;
16408   if (origin != NULL)
16409     origin_die = add_abstract_origin_attribute (var_die, origin);
16410
16411   /* Loop unrolling can create multiple blocks that refer to the same
16412      static variable, so we must test for the DW_AT_declaration flag.
16413
16414      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
16415      copy decls and set the DECL_ABSTRACT flag on them instead of
16416      sharing them.
16417
16418      ??? Duplicated blocks have been rewritten to use .debug_ranges.
16419
16420      ??? The declare_in_namespace support causes us to get two DIEs for one
16421      variable, both of which are declarations.  We want to avoid considering
16422      one to be a specification, so we must test that this DIE is not a
16423      declaration.  */
16424   else if (old_die && TREE_STATIC (decl) && ! declaration
16425            && get_AT_flag (old_die, DW_AT_declaration) == 1)
16426     {
16427       /* This is a definition of a C++ class level static.  */
16428       add_AT_specification (var_die, old_die);
16429       if (DECL_NAME (decl))
16430         {
16431           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16432           struct dwarf_file_data * file_index = lookup_filename (s.file);
16433
16434           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
16435             add_AT_file (var_die, DW_AT_decl_file, file_index);
16436
16437           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
16438             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
16439         }
16440     }
16441   else
16442     {
16443       tree type = TREE_TYPE (decl);
16444
16445       add_name_and_src_coords_attributes (var_die, decl);
16446       if (decl_by_reference_p (decl))
16447         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
16448       else
16449         add_type_attribute (var_die, type, TREE_READONLY (decl),
16450                             TREE_THIS_VOLATILE (decl), context_die);
16451
16452       if (TREE_PUBLIC (decl))
16453         add_AT_flag (var_die, DW_AT_external, 1);
16454
16455       if (DECL_ARTIFICIAL (decl))
16456         add_AT_flag (var_die, DW_AT_artificial, 1);
16457
16458       if (TREE_PROTECTED (decl))
16459         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
16460       else if (TREE_PRIVATE (decl))
16461         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
16462     }
16463
16464   if (declaration)
16465     add_AT_flag (var_die, DW_AT_declaration, 1);
16466
16467   if (decl && (DECL_ABSTRACT (decl) || declaration))
16468     equate_decl_number_to_die (decl, var_die);
16469
16470   if (! declaration
16471       && (! DECL_ABSTRACT (decl_or_origin)
16472           /* Local static vars are shared between all clones/inlines,
16473              so emit DW_AT_location on the abstract DIE if DECL_RTL is
16474              already set.  */
16475           || (TREE_CODE (decl_or_origin) == VAR_DECL
16476               && TREE_STATIC (decl_or_origin)
16477               && DECL_RTL_SET_P (decl_or_origin)))
16478       /* When abstract origin already has DW_AT_location attribute, no need
16479          to add it again.  */
16480       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
16481     {
16482       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
16483           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
16484         defer_location (decl_or_origin, var_die);
16485       else
16486         add_location_or_const_value_attribute (var_die,
16487                                                decl_or_origin,
16488                                                DW_AT_location);
16489       add_pubname (decl_or_origin, var_die);
16490     }
16491   else
16492     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
16493 }
16494
16495 /* Generate a DIE to represent a named constant.  */
16496
16497 static void
16498 gen_const_die (tree decl, dw_die_ref context_die)
16499 {
16500   dw_die_ref const_die;
16501   tree type = TREE_TYPE (decl);
16502
16503   const_die = new_die (DW_TAG_constant, context_die, decl);
16504   add_name_and_src_coords_attributes (const_die, decl);
16505   add_type_attribute (const_die, type, 1, 0, context_die);
16506   if (TREE_PUBLIC (decl))
16507     add_AT_flag (const_die, DW_AT_external, 1);
16508   if (DECL_ARTIFICIAL (decl))
16509     add_AT_flag (const_die, DW_AT_artificial, 1);
16510   tree_add_const_value_attribute_for_decl (const_die, decl);
16511 }
16512
16513 /* Generate a DIE to represent a label identifier.  */
16514
16515 static void
16516 gen_label_die (tree decl, dw_die_ref context_die)
16517 {
16518   tree origin = decl_ultimate_origin (decl);
16519   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
16520   rtx insn;
16521   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16522
16523   if (origin != NULL)
16524     add_abstract_origin_attribute (lbl_die, origin);
16525   else
16526     add_name_and_src_coords_attributes (lbl_die, decl);
16527
16528   if (DECL_ABSTRACT (decl))
16529     equate_decl_number_to_die (decl, lbl_die);
16530   else
16531     {
16532       insn = DECL_RTL_IF_SET (decl);
16533
16534       /* Deleted labels are programmer specified labels which have been
16535          eliminated because of various optimizations.  We still emit them
16536          here so that it is possible to put breakpoints on them.  */
16537       if (insn
16538           && (LABEL_P (insn)
16539               || ((NOTE_P (insn)
16540                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
16541         {
16542           /* When optimization is enabled (via -O) some parts of the compiler
16543              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
16544              represent source-level labels which were explicitly declared by
16545              the user.  This really shouldn't be happening though, so catch
16546              it if it ever does happen.  */
16547           gcc_assert (!INSN_DELETED_P (insn));
16548
16549           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
16550           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
16551         }
16552     }
16553 }
16554
16555 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
16556    attributes to the DIE for a block STMT, to describe where the inlined
16557    function was called from.  This is similar to add_src_coords_attributes.  */
16558
16559 static inline void
16560 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
16561 {
16562   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
16563
16564   if (dwarf_version >= 3 || !dwarf_strict)
16565     {
16566       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
16567       add_AT_unsigned (die, DW_AT_call_line, s.line);
16568     }
16569 }
16570
16571
16572 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
16573    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
16574
16575 static inline void
16576 add_high_low_attributes (tree stmt, dw_die_ref die)
16577 {
16578   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16579
16580   if (BLOCK_FRAGMENT_CHAIN (stmt)
16581       && (dwarf_version >= 3 || !dwarf_strict))
16582     {
16583       tree chain;
16584
16585       if (inlined_function_outer_scope_p (stmt))
16586         {
16587           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
16588                                        BLOCK_NUMBER (stmt));
16589           add_AT_lbl_id (die, DW_AT_entry_pc, label);
16590         }
16591
16592       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
16593
16594       chain = BLOCK_FRAGMENT_CHAIN (stmt);
16595       do
16596         {
16597           add_ranges (chain);
16598           chain = BLOCK_FRAGMENT_CHAIN (chain);
16599         }
16600       while (chain);
16601       add_ranges (NULL);
16602     }
16603   else
16604     {
16605       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
16606                                    BLOCK_NUMBER (stmt));
16607       add_AT_lbl_id (die, DW_AT_low_pc, label);
16608       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
16609                                    BLOCK_NUMBER (stmt));
16610       add_AT_lbl_id (die, DW_AT_high_pc, label);
16611     }
16612 }
16613
16614 /* Generate a DIE for a lexical block.  */
16615
16616 static void
16617 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
16618 {
16619   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
16620
16621   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
16622     add_high_low_attributes (stmt, stmt_die);
16623
16624   decls_for_scope (stmt, stmt_die, depth);
16625 }
16626
16627 /* Generate a DIE for an inlined subprogram.  */
16628
16629 static void
16630 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
16631 {
16632   tree decl;
16633
16634   /* The instance of function that is effectively being inlined shall not
16635      be abstract.  */
16636   gcc_assert (! BLOCK_ABSTRACT (stmt));
16637
16638   decl = block_ultimate_origin (stmt);
16639
16640   /* Emit info for the abstract instance first, if we haven't yet.  We
16641      must emit this even if the block is abstract, otherwise when we
16642      emit the block below (or elsewhere), we may end up trying to emit
16643      a die whose origin die hasn't been emitted, and crashing.  */
16644   dwarf2out_abstract_function (decl);
16645
16646   if (! BLOCK_ABSTRACT (stmt))
16647     {
16648       dw_die_ref subr_die
16649         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
16650
16651       add_abstract_origin_attribute (subr_die, decl);
16652       if (TREE_ASM_WRITTEN (stmt))
16653         add_high_low_attributes (stmt, subr_die);
16654       add_call_src_coords_attributes (stmt, subr_die);
16655
16656       decls_for_scope (stmt, subr_die, depth);
16657       current_function_has_inlines = 1;
16658     }
16659 }
16660
16661 /* Generate a DIE for a field in a record, or structure.  */
16662
16663 static void
16664 gen_field_die (tree decl, dw_die_ref context_die)
16665 {
16666   dw_die_ref decl_die;
16667
16668   if (TREE_TYPE (decl) == error_mark_node)
16669     return;
16670
16671   decl_die = new_die (DW_TAG_member, context_die, decl);
16672   add_name_and_src_coords_attributes (decl_die, decl);
16673   add_type_attribute (decl_die, member_declared_type (decl),
16674                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
16675                       context_die);
16676
16677   if (DECL_BIT_FIELD_TYPE (decl))
16678     {
16679       add_byte_size_attribute (decl_die, decl);
16680       add_bit_size_attribute (decl_die, decl);
16681       add_bit_offset_attribute (decl_die, decl);
16682     }
16683
16684   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
16685     add_data_member_location_attribute (decl_die, decl);
16686
16687   if (DECL_ARTIFICIAL (decl))
16688     add_AT_flag (decl_die, DW_AT_artificial, 1);
16689
16690   if (TREE_PROTECTED (decl))
16691     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
16692   else if (TREE_PRIVATE (decl))
16693     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
16694
16695   /* Equate decl number to die, so that we can look up this decl later on.  */
16696   equate_decl_number_to_die (decl, decl_die);
16697 }
16698
16699 #if 0
16700 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16701    Use modified_type_die instead.
16702    We keep this code here just in case these types of DIEs may be needed to
16703    represent certain things in other languages (e.g. Pascal) someday.  */
16704
16705 static void
16706 gen_pointer_type_die (tree type, dw_die_ref context_die)
16707 {
16708   dw_die_ref ptr_die
16709     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
16710
16711   equate_type_number_to_die (type, ptr_die);
16712   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16713   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16714 }
16715
16716 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16717    Use modified_type_die instead.
16718    We keep this code here just in case these types of DIEs may be needed to
16719    represent certain things in other languages (e.g. Pascal) someday.  */
16720
16721 static void
16722 gen_reference_type_die (tree type, dw_die_ref context_die)
16723 {
16724   dw_die_ref ref_die
16725     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
16726
16727   equate_type_number_to_die (type, ref_die);
16728   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
16729   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16730 }
16731 #endif
16732
16733 /* Generate a DIE for a pointer to a member type.  */
16734
16735 static void
16736 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
16737 {
16738   dw_die_ref ptr_die
16739     = new_die (DW_TAG_ptr_to_member_type,
16740                scope_die_for (type, context_die), type);
16741
16742   equate_type_number_to_die (type, ptr_die);
16743   add_AT_die_ref (ptr_die, DW_AT_containing_type,
16744                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
16745   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16746 }
16747
16748 /* Generate the DIE for the compilation unit.  */
16749
16750 static dw_die_ref
16751 gen_compile_unit_die (const char *filename)
16752 {
16753   dw_die_ref die;
16754   char producer[250];
16755   const char *language_string = lang_hooks.name;
16756   int language;
16757
16758   die = new_die (DW_TAG_compile_unit, NULL, NULL);
16759
16760   if (filename)
16761     {
16762       add_name_attribute (die, filename);
16763       /* Don't add cwd for <built-in>.  */
16764       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
16765         add_comp_dir_attribute (die);
16766     }
16767
16768   sprintf (producer, "%s %s", language_string, version_string);
16769
16770 #ifdef MIPS_DEBUGGING_INFO
16771   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
16772      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
16773      not appear in the producer string, the debugger reaches the conclusion
16774      that the object file is stripped and has no debugging information.
16775      To get the MIPS/SGI debugger to believe that there is debugging
16776      information in the object file, we add a -g to the producer string.  */
16777   if (debug_info_level > DINFO_LEVEL_TERSE)
16778     strcat (producer, " -g");
16779 #endif
16780
16781   add_AT_string (die, DW_AT_producer, producer);
16782
16783   language = DW_LANG_C89;
16784   if (strcmp (language_string, "GNU C++") == 0)
16785     language = DW_LANG_C_plus_plus;
16786   else if (strcmp (language_string, "GNU F77") == 0)
16787     language = DW_LANG_Fortran77;
16788   else if (strcmp (language_string, "GNU Pascal") == 0)
16789     language = DW_LANG_Pascal83;
16790   else if (dwarf_version >= 3 || !dwarf_strict)
16791     {
16792       if (strcmp (language_string, "GNU Ada") == 0)
16793         language = DW_LANG_Ada95;
16794       else if (strcmp (language_string, "GNU Fortran") == 0)
16795         language = DW_LANG_Fortran95;
16796       else if (strcmp (language_string, "GNU Java") == 0)
16797         language = DW_LANG_Java;
16798       else if (strcmp (language_string, "GNU Objective-C") == 0)
16799         language = DW_LANG_ObjC;
16800       else if (strcmp (language_string, "GNU Objective-C++") == 0)
16801         language = DW_LANG_ObjC_plus_plus;
16802     }
16803
16804   add_AT_unsigned (die, DW_AT_language, language);
16805   return die;
16806 }
16807
16808 /* Generate the DIE for a base class.  */
16809
16810 static void
16811 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
16812 {
16813   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
16814
16815   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
16816   add_data_member_location_attribute (die, binfo);
16817
16818   if (BINFO_VIRTUAL_P (binfo))
16819     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16820
16821   if (access == access_public_node)
16822     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16823   else if (access == access_protected_node)
16824     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16825 }
16826
16827 /* Generate a DIE for a class member.  */
16828
16829 static void
16830 gen_member_die (tree type, dw_die_ref context_die)
16831 {
16832   tree member;
16833   tree binfo = TYPE_BINFO (type);
16834   dw_die_ref child;
16835
16836   /* If this is not an incomplete type, output descriptions of each of its
16837      members. Note that as we output the DIEs necessary to represent the
16838      members of this record or union type, we will also be trying to output
16839      DIEs to represent the *types* of those members. However the `type'
16840      function (above) will specifically avoid generating type DIEs for member
16841      types *within* the list of member DIEs for this (containing) type except
16842      for those types (of members) which are explicitly marked as also being
16843      members of this (containing) type themselves.  The g++ front- end can
16844      force any given type to be treated as a member of some other (containing)
16845      type by setting the TYPE_CONTEXT of the given (member) type to point to
16846      the TREE node representing the appropriate (containing) type.  */
16847
16848   /* First output info about the base classes.  */
16849   if (binfo)
16850     {
16851       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
16852       int i;
16853       tree base;
16854
16855       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
16856         gen_inheritance_die (base,
16857                              (accesses ? VEC_index (tree, accesses, i)
16858                               : access_public_node), context_die);
16859     }
16860
16861   /* Now output info about the data members and type members.  */
16862   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
16863     {
16864       /* If we thought we were generating minimal debug info for TYPE
16865          and then changed our minds, some of the member declarations
16866          may have already been defined.  Don't define them again, but
16867          do put them in the right order.  */
16868
16869       child = lookup_decl_die (member);
16870       if (child)
16871         splice_child_die (context_die, child);
16872       else
16873         gen_decl_die (member, NULL, context_die);
16874     }
16875
16876   /* Now output info about the function members (if any).  */
16877   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
16878     {
16879       /* Don't include clones in the member list.  */
16880       if (DECL_ABSTRACT_ORIGIN (member))
16881         continue;
16882
16883       child = lookup_decl_die (member);
16884       if (child)
16885         splice_child_die (context_die, child);
16886       else
16887         gen_decl_die (member, NULL, context_die);
16888     }
16889 }
16890
16891 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
16892    is set, we pretend that the type was never defined, so we only get the
16893    member DIEs needed by later specification DIEs.  */
16894
16895 static void
16896 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
16897                                 enum debug_info_usage usage)
16898 {
16899   dw_die_ref type_die = lookup_type_die (type);
16900   dw_die_ref scope_die = 0;
16901   int nested = 0;
16902   int complete = (TYPE_SIZE (type)
16903                   && (! TYPE_STUB_DECL (type)
16904                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
16905   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
16906   complete = complete && should_emit_struct_debug (type, usage);
16907
16908   if (type_die && ! complete)
16909     return;
16910
16911   if (TYPE_CONTEXT (type) != NULL_TREE
16912       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
16913           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
16914     nested = 1;
16915
16916   scope_die = scope_die_for (type, context_die);
16917
16918   if (! type_die || (nested && scope_die == comp_unit_die))
16919     /* First occurrence of type or toplevel definition of nested class.  */
16920     {
16921       dw_die_ref old_die = type_die;
16922
16923       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
16924                           ? record_type_tag (type) : DW_TAG_union_type,
16925                           scope_die, type);
16926       equate_type_number_to_die (type, type_die);
16927       if (old_die)
16928         add_AT_specification (type_die, old_die);
16929       else
16930         add_name_attribute (type_die, type_tag (type));
16931     }
16932   else
16933     remove_AT (type_die, DW_AT_declaration);
16934
16935   /* Generate child dies for template paramaters.  */
16936   if (debug_info_level > DINFO_LEVEL_TERSE
16937       && COMPLETE_TYPE_P (type))
16938     gen_generic_params_dies (type);
16939
16940   /* If this type has been completed, then give it a byte_size attribute and
16941      then give a list of members.  */
16942   if (complete && !ns_decl)
16943     {
16944       /* Prevent infinite recursion in cases where the type of some member of
16945          this type is expressed in terms of this type itself.  */
16946       TREE_ASM_WRITTEN (type) = 1;
16947       add_byte_size_attribute (type_die, type);
16948       if (TYPE_STUB_DECL (type) != NULL_TREE)
16949         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16950
16951       /* If the first reference to this type was as the return type of an
16952          inline function, then it may not have a parent.  Fix this now.  */
16953       if (type_die->die_parent == NULL)
16954         add_child_die (scope_die, type_die);
16955
16956       push_decl_scope (type);
16957       gen_member_die (type, type_die);
16958       pop_decl_scope ();
16959
16960       /* GNU extension: Record what type our vtable lives in.  */
16961       if (TYPE_VFIELD (type))
16962         {
16963           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
16964
16965           gen_type_die (vtype, context_die);
16966           add_AT_die_ref (type_die, DW_AT_containing_type,
16967                           lookup_type_die (vtype));
16968         }
16969     }
16970   else
16971     {
16972       add_AT_flag (type_die, DW_AT_declaration, 1);
16973
16974       /* We don't need to do this for function-local types.  */
16975       if (TYPE_STUB_DECL (type)
16976           && ! decl_function_context (TYPE_STUB_DECL (type)))
16977         VEC_safe_push (tree, gc, incomplete_types, type);
16978     }
16979
16980   if (get_AT (type_die, DW_AT_name))
16981     add_pubtype (type, type_die);
16982 }
16983
16984 /* Generate a DIE for a subroutine _type_.  */
16985
16986 static void
16987 gen_subroutine_type_die (tree type, dw_die_ref context_die)
16988 {
16989   tree return_type = TREE_TYPE (type);
16990   dw_die_ref subr_die
16991     = new_die (DW_TAG_subroutine_type,
16992                scope_die_for (type, context_die), type);
16993
16994   equate_type_number_to_die (type, subr_die);
16995   add_prototyped_attribute (subr_die, type);
16996   add_type_attribute (subr_die, return_type, 0, 0, context_die);
16997   gen_formal_types_die (type, subr_die);
16998
16999   if (get_AT (subr_die, DW_AT_name))
17000     add_pubtype (type, subr_die);
17001 }
17002
17003 /* Generate a DIE for a type definition.  */
17004
17005 static void
17006 gen_typedef_die (tree decl, dw_die_ref context_die)
17007 {
17008   dw_die_ref type_die;
17009   tree origin;
17010
17011   if (TREE_ASM_WRITTEN (decl))
17012     return;
17013
17014   TREE_ASM_WRITTEN (decl) = 1;
17015   type_die = new_die (DW_TAG_typedef, context_die, decl);
17016   origin = decl_ultimate_origin (decl);
17017   if (origin != NULL)
17018     add_abstract_origin_attribute (type_die, origin);
17019   else
17020     {
17021       tree type;
17022
17023       add_name_and_src_coords_attributes (type_die, decl);
17024       if (DECL_ORIGINAL_TYPE (decl))
17025         {
17026           type = DECL_ORIGINAL_TYPE (decl);
17027
17028           gcc_assert (type != TREE_TYPE (decl));
17029           equate_type_number_to_die (TREE_TYPE (decl), type_die);
17030         }
17031       else
17032         type = TREE_TYPE (decl);
17033
17034       add_type_attribute (type_die, type, TREE_READONLY (decl),
17035                           TREE_THIS_VOLATILE (decl), context_die);
17036     }
17037
17038   if (DECL_ABSTRACT (decl))
17039     equate_decl_number_to_die (decl, type_die);
17040
17041   if (get_AT (type_die, DW_AT_name))
17042     add_pubtype (decl, type_die);
17043 }
17044
17045 /* Generate a type description DIE.  */
17046
17047 static void
17048 gen_type_die_with_usage (tree type, dw_die_ref context_die,
17049                                 enum debug_info_usage usage)
17050 {
17051   int need_pop;
17052   struct array_descr_info info;
17053
17054   if (type == NULL_TREE || type == error_mark_node)
17055     return;
17056
17057   /* If TYPE is a typedef type variant, let's generate debug info
17058      for the parent typedef which TYPE is a type of.  */
17059   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17060       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
17061     {
17062       if (TREE_ASM_WRITTEN (type))
17063         return;
17064
17065       /* Prevent broken recursion; we can't hand off to the same type.  */
17066       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
17067
17068       /* Use the DIE of the containing namespace as the parent DIE of
17069          the type description DIE we want to generate.  */
17070       if (DECL_CONTEXT (TYPE_NAME (type))
17071           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
17072         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
17073
17074       TREE_ASM_WRITTEN (type) = 1;
17075       gen_decl_die (TYPE_NAME (type), NULL, context_die);
17076       return;
17077     }
17078
17079   /* If this is an array type with hidden descriptor, handle it first.  */
17080   if (!TREE_ASM_WRITTEN (type)
17081       && lang_hooks.types.get_array_descr_info
17082       && lang_hooks.types.get_array_descr_info (type, &info)
17083       && (dwarf_version >= 3 || !dwarf_strict))
17084     {
17085       gen_descr_array_type_die (type, &info, context_die);
17086       TREE_ASM_WRITTEN (type) = 1;
17087       return;
17088     }
17089
17090   /* We are going to output a DIE to represent the unqualified version
17091      of this type (i.e. without any const or volatile qualifiers) so
17092      get the main variant (i.e. the unqualified version) of this type
17093      now.  (Vectors are special because the debugging info is in the
17094      cloned type itself).  */
17095   if (TREE_CODE (type) != VECTOR_TYPE)
17096     type = type_main_variant (type);
17097
17098   if (TREE_ASM_WRITTEN (type))
17099     return;
17100
17101   switch (TREE_CODE (type))
17102     {
17103     case ERROR_MARK:
17104       break;
17105
17106     case POINTER_TYPE:
17107     case REFERENCE_TYPE:
17108       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
17109          ensures that the gen_type_die recursion will terminate even if the
17110          type is recursive.  Recursive types are possible in Ada.  */
17111       /* ??? We could perhaps do this for all types before the switch
17112          statement.  */
17113       TREE_ASM_WRITTEN (type) = 1;
17114
17115       /* For these types, all that is required is that we output a DIE (or a
17116          set of DIEs) to represent the "basis" type.  */
17117       gen_type_die_with_usage (TREE_TYPE (type), context_die,
17118                                 DINFO_USAGE_IND_USE);
17119       break;
17120
17121     case OFFSET_TYPE:
17122       /* This code is used for C++ pointer-to-data-member types.
17123          Output a description of the relevant class type.  */
17124       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
17125                                         DINFO_USAGE_IND_USE);
17126
17127       /* Output a description of the type of the object pointed to.  */
17128       gen_type_die_with_usage (TREE_TYPE (type), context_die,
17129                                         DINFO_USAGE_IND_USE);
17130
17131       /* Now output a DIE to represent this pointer-to-data-member type
17132          itself.  */
17133       gen_ptr_to_mbr_type_die (type, context_die);
17134       break;
17135
17136     case FUNCTION_TYPE:
17137       /* Force out return type (in case it wasn't forced out already).  */
17138       gen_type_die_with_usage (TREE_TYPE (type), context_die,
17139                                         DINFO_USAGE_DIR_USE);
17140       gen_subroutine_type_die (type, context_die);
17141       break;
17142
17143     case METHOD_TYPE:
17144       /* Force out return type (in case it wasn't forced out already).  */
17145       gen_type_die_with_usage (TREE_TYPE (type), context_die,
17146                                         DINFO_USAGE_DIR_USE);
17147       gen_subroutine_type_die (type, context_die);
17148       break;
17149
17150     case ARRAY_TYPE:
17151       gen_array_type_die (type, context_die);
17152       break;
17153
17154     case VECTOR_TYPE:
17155       gen_array_type_die (type, context_die);
17156       break;
17157
17158     case ENUMERAL_TYPE:
17159     case RECORD_TYPE:
17160     case UNION_TYPE:
17161     case QUAL_UNION_TYPE:
17162       /* If this is a nested type whose containing class hasn't been written
17163          out yet, writing it out will cover this one, too.  This does not apply
17164          to instantiations of member class templates; they need to be added to
17165          the containing class as they are generated.  FIXME: This hurts the
17166          idea of combining type decls from multiple TUs, since we can't predict
17167          what set of template instantiations we'll get.  */
17168       if (TYPE_CONTEXT (type)
17169           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
17170           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
17171         {
17172           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
17173
17174           if (TREE_ASM_WRITTEN (type))
17175             return;
17176
17177           /* If that failed, attach ourselves to the stub.  */
17178           push_decl_scope (TYPE_CONTEXT (type));
17179           context_die = lookup_type_die (TYPE_CONTEXT (type));
17180           need_pop = 1;
17181         }
17182       else if (TYPE_CONTEXT (type) != NULL_TREE
17183                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
17184         {
17185           /* If this type is local to a function that hasn't been written
17186              out yet, use a NULL context for now; it will be fixed up in
17187              decls_for_scope.  */
17188           context_die = lookup_decl_die (TYPE_CONTEXT (type));
17189           need_pop = 0;
17190         }
17191       else
17192         {
17193           context_die = declare_in_namespace (type, context_die);
17194           need_pop = 0;
17195         }
17196
17197       if (TREE_CODE (type) == ENUMERAL_TYPE)
17198         {
17199           /* This might have been written out by the call to
17200              declare_in_namespace.  */
17201           if (!TREE_ASM_WRITTEN (type))
17202             gen_enumeration_type_die (type, context_die);
17203         }
17204       else
17205         gen_struct_or_union_type_die (type, context_die, usage);
17206
17207       if (need_pop)
17208         pop_decl_scope ();
17209
17210       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
17211          it up if it is ever completed.  gen_*_type_die will set it for us
17212          when appropriate.  */
17213       return;
17214
17215     case VOID_TYPE:
17216     case INTEGER_TYPE:
17217     case REAL_TYPE:
17218     case FIXED_POINT_TYPE:
17219     case COMPLEX_TYPE:
17220     case BOOLEAN_TYPE:
17221       /* No DIEs needed for fundamental types.  */
17222       break;
17223
17224     case LANG_TYPE:
17225       /* No Dwarf representation currently defined.  */
17226       break;
17227
17228     default:
17229       gcc_unreachable ();
17230     }
17231
17232   TREE_ASM_WRITTEN (type) = 1;
17233 }
17234
17235 static void
17236 gen_type_die (tree type, dw_die_ref context_die)
17237 {
17238   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
17239 }
17240
17241 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
17242    things which are local to the given block.  */
17243
17244 static void
17245 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
17246 {
17247   int must_output_die = 0;
17248   bool inlined_func;
17249
17250   /* Ignore blocks that are NULL.  */
17251   if (stmt == NULL_TREE)
17252     return;
17253
17254   inlined_func = inlined_function_outer_scope_p (stmt);
17255
17256   /* If the block is one fragment of a non-contiguous block, do not
17257      process the variables, since they will have been done by the
17258      origin block.  Do process subblocks.  */
17259   if (BLOCK_FRAGMENT_ORIGIN (stmt))
17260     {
17261       tree sub;
17262
17263       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
17264         gen_block_die (sub, context_die, depth + 1);
17265
17266       return;
17267     }
17268
17269   /* Determine if we need to output any Dwarf DIEs at all to represent this
17270      block.  */
17271   if (inlined_func)
17272     /* The outer scopes for inlinings *must* always be represented.  We
17273        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
17274     must_output_die = 1;
17275   else
17276     {
17277       /* Determine if this block directly contains any "significant"
17278          local declarations which we will need to output DIEs for.  */
17279       if (debug_info_level > DINFO_LEVEL_TERSE)
17280         /* We are not in terse mode so *any* local declaration counts
17281            as being a "significant" one.  */
17282         must_output_die = ((BLOCK_VARS (stmt) != NULL
17283                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
17284                            && (TREE_USED (stmt)
17285                                || TREE_ASM_WRITTEN (stmt)
17286                                || BLOCK_ABSTRACT (stmt)));
17287       else if ((TREE_USED (stmt)
17288                 || TREE_ASM_WRITTEN (stmt)
17289                 || BLOCK_ABSTRACT (stmt))
17290                && !dwarf2out_ignore_block (stmt))
17291         must_output_die = 1;
17292     }
17293
17294   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
17295      DIE for any block which contains no significant local declarations at
17296      all.  Rather, in such cases we just call `decls_for_scope' so that any
17297      needed Dwarf info for any sub-blocks will get properly generated. Note
17298      that in terse mode, our definition of what constitutes a "significant"
17299      local declaration gets restricted to include only inlined function
17300      instances and local (nested) function definitions.  */
17301   if (must_output_die)
17302     {
17303       if (inlined_func)
17304         {
17305           /* If STMT block is abstract, that means we have been called
17306              indirectly from dwarf2out_abstract_function.
17307              That function rightfully marks the descendent blocks (of
17308              the abstract function it is dealing with) as being abstract,
17309              precisely to prevent us from emitting any
17310              DW_TAG_inlined_subroutine DIE as a descendent
17311              of an abstract function instance. So in that case, we should
17312              not call gen_inlined_subroutine_die.
17313
17314              Later though, when cgraph asks dwarf2out to emit info
17315              for the concrete instance of the function decl into which
17316              the concrete instance of STMT got inlined, the later will lead
17317              to the generation of a DW_TAG_inlined_subroutine DIE.  */
17318           if (! BLOCK_ABSTRACT (stmt))
17319             gen_inlined_subroutine_die (stmt, context_die, depth);
17320         }
17321       else
17322         gen_lexical_block_die (stmt, context_die, depth);
17323     }
17324   else
17325     decls_for_scope (stmt, context_die, depth);
17326 }
17327
17328 /* Process variable DECL (or variable with origin ORIGIN) within
17329    block STMT and add it to CONTEXT_DIE.  */
17330 static void
17331 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
17332 {
17333   dw_die_ref die;
17334   tree decl_or_origin = decl ? decl : origin;
17335   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
17336
17337   if (ultimate_origin)
17338     origin = ultimate_origin;
17339
17340   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
17341     die = lookup_decl_die (decl_or_origin);
17342   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
17343            && TYPE_DECL_IS_STUB (decl_or_origin))
17344     die = lookup_type_die (TREE_TYPE (decl_or_origin));
17345   else
17346     die = NULL;
17347
17348   if (die != NULL && die->die_parent == NULL)
17349     add_child_die (context_die, die);
17350   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
17351     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
17352                                          stmt, context_die);
17353   else
17354     gen_decl_die (decl, origin, context_die);
17355 }
17356
17357 /* Generate all of the decls declared within a given scope and (recursively)
17358    all of its sub-blocks.  */
17359
17360 static void
17361 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
17362 {
17363   tree decl;
17364   unsigned int i;
17365   tree subblocks;
17366
17367   /* Ignore NULL blocks.  */
17368   if (stmt == NULL_TREE)
17369     return;
17370
17371   /* Output the DIEs to represent all of the data objects and typedefs
17372      declared directly within this block but not within any nested
17373      sub-blocks.  Also, nested function and tag DIEs have been
17374      generated with a parent of NULL; fix that up now.  */
17375   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
17376     process_scope_var (stmt, decl, NULL_TREE, context_die);
17377   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17378     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
17379                        context_die);
17380
17381   /* If we're at -g1, we're not interested in subblocks.  */
17382   if (debug_info_level <= DINFO_LEVEL_TERSE)
17383     return;
17384
17385   /* Output the DIEs to represent all sub-blocks (and the items declared
17386      therein) of this block.  */
17387   for (subblocks = BLOCK_SUBBLOCKS (stmt);
17388        subblocks != NULL;
17389        subblocks = BLOCK_CHAIN (subblocks))
17390     gen_block_die (subblocks, context_die, depth + 1);
17391 }
17392
17393 /* Is this a typedef we can avoid emitting?  */
17394
17395 static inline int
17396 is_redundant_typedef (const_tree decl)
17397 {
17398   if (TYPE_DECL_IS_STUB (decl))
17399     return 1;
17400
17401   if (DECL_ARTIFICIAL (decl)
17402       && DECL_CONTEXT (decl)
17403       && is_tagged_type (DECL_CONTEXT (decl))
17404       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
17405       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
17406     /* Also ignore the artificial member typedef for the class name.  */
17407     return 1;
17408
17409   return 0;
17410 }
17411
17412 /* Returns the DIE for a context.  */
17413
17414 static inline dw_die_ref
17415 get_context_die (tree context)
17416 {
17417   if (context)
17418     {
17419       /* Find die that represents this context.  */
17420       if (TYPE_P (context))
17421         return force_type_die (context);
17422       else
17423         return force_decl_die (context);
17424     }
17425   return comp_unit_die;
17426 }
17427
17428 /* Returns the DIE for decl.  A DIE will always be returned.  */
17429
17430 static dw_die_ref
17431 force_decl_die (tree decl)
17432 {
17433   dw_die_ref decl_die;
17434   unsigned saved_external_flag;
17435   tree save_fn = NULL_TREE;
17436   decl_die = lookup_decl_die (decl);
17437   if (!decl_die)
17438     {
17439       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
17440
17441       decl_die = lookup_decl_die (decl);
17442       if (decl_die)
17443         return decl_die;
17444
17445       switch (TREE_CODE (decl))
17446         {
17447         case FUNCTION_DECL:
17448           /* Clear current_function_decl, so that gen_subprogram_die thinks
17449              that this is a declaration. At this point, we just want to force
17450              declaration die.  */
17451           save_fn = current_function_decl;
17452           current_function_decl = NULL_TREE;
17453           gen_subprogram_die (decl, context_die);
17454           current_function_decl = save_fn;
17455           break;
17456
17457         case VAR_DECL:
17458           /* Set external flag to force declaration die. Restore it after
17459            gen_decl_die() call.  */
17460           saved_external_flag = DECL_EXTERNAL (decl);
17461           DECL_EXTERNAL (decl) = 1;
17462           gen_decl_die (decl, NULL, context_die);
17463           DECL_EXTERNAL (decl) = saved_external_flag;
17464           break;
17465
17466         case NAMESPACE_DECL:
17467           if (dwarf_version >= 3 || !dwarf_strict)
17468             dwarf2out_decl (decl);
17469           else
17470             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
17471             decl_die = comp_unit_die;
17472           break;
17473
17474         default:
17475           gcc_unreachable ();
17476         }
17477
17478       /* We should be able to find the DIE now.  */
17479       if (!decl_die)
17480         decl_die = lookup_decl_die (decl);
17481       gcc_assert (decl_die);
17482     }
17483
17484   return decl_die;
17485 }
17486
17487 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
17488    always returned.  */
17489
17490 static dw_die_ref
17491 force_type_die (tree type)
17492 {
17493   dw_die_ref type_die;
17494
17495   type_die = lookup_type_die (type);
17496   if (!type_die)
17497     {
17498       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
17499
17500       type_die = modified_type_die (type, TYPE_READONLY (type),
17501                                     TYPE_VOLATILE (type), context_die);
17502       gcc_assert (type_die);
17503     }
17504   return type_die;
17505 }
17506
17507 /* Force out any required namespaces to be able to output DECL,
17508    and return the new context_die for it, if it's changed.  */
17509
17510 static dw_die_ref
17511 setup_namespace_context (tree thing, dw_die_ref context_die)
17512 {
17513   tree context = (DECL_P (thing)
17514                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
17515   if (context && TREE_CODE (context) == NAMESPACE_DECL)
17516     /* Force out the namespace.  */
17517     context_die = force_decl_die (context);
17518
17519   return context_die;
17520 }
17521
17522 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
17523    type) within its namespace, if appropriate.
17524
17525    For compatibility with older debuggers, namespace DIEs only contain
17526    declarations; all definitions are emitted at CU scope.  */
17527
17528 static dw_die_ref
17529 declare_in_namespace (tree thing, dw_die_ref context_die)
17530 {
17531   dw_die_ref ns_context;
17532
17533   if (debug_info_level <= DINFO_LEVEL_TERSE)
17534     return context_die;
17535
17536   /* If this decl is from an inlined function, then don't try to emit it in its
17537      namespace, as we will get confused.  It would have already been emitted
17538      when the abstract instance of the inline function was emitted anyways.  */
17539   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
17540     return context_die;
17541
17542   ns_context = setup_namespace_context (thing, context_die);
17543
17544   if (ns_context != context_die)
17545     {
17546       if (is_fortran ())
17547         return ns_context;
17548       if (DECL_P (thing))
17549         gen_decl_die (thing, NULL, ns_context);
17550       else
17551         gen_type_die (thing, ns_context);
17552     }
17553   return context_die;
17554 }
17555
17556 /* Generate a DIE for a namespace or namespace alias.  */
17557
17558 static void
17559 gen_namespace_die (tree decl, dw_die_ref context_die)
17560 {
17561   dw_die_ref namespace_die;
17562
17563   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
17564      they are an alias of.  */
17565   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
17566     {
17567       /* Output a real namespace or module.  */
17568       context_die = setup_namespace_context (decl, comp_unit_die);
17569       namespace_die = new_die (is_fortran ()
17570                                ? DW_TAG_module : DW_TAG_namespace,
17571                                context_die, decl);
17572       /* For Fortran modules defined in different CU don't add src coords.  */
17573       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
17574         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
17575       else
17576         add_name_and_src_coords_attributes (namespace_die, decl);
17577       if (DECL_EXTERNAL (decl))
17578         add_AT_flag (namespace_die, DW_AT_declaration, 1);
17579       equate_decl_number_to_die (decl, namespace_die);
17580     }
17581   else
17582     {
17583       /* Output a namespace alias.  */
17584
17585       /* Force out the namespace we are an alias of, if necessary.  */
17586       dw_die_ref origin_die
17587         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
17588
17589       if (DECL_CONTEXT (decl) == NULL_TREE
17590           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
17591         context_die = setup_namespace_context (decl, comp_unit_die);
17592       /* Now create the namespace alias DIE.  */
17593       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
17594       add_name_and_src_coords_attributes (namespace_die, decl);
17595       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
17596       equate_decl_number_to_die (decl, namespace_die);
17597     }
17598 }
17599
17600 /* Generate Dwarf debug information for a decl described by DECL.  */
17601
17602 static void
17603 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
17604 {
17605   tree decl_or_origin = decl ? decl : origin;
17606   tree class_origin = NULL;
17607
17608   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
17609     return;
17610
17611   switch (TREE_CODE (decl_or_origin))
17612     {
17613     case ERROR_MARK:
17614       break;
17615
17616     case CONST_DECL:
17617       if (!is_fortran ())
17618         {
17619           /* The individual enumerators of an enum type get output when we output
17620              the Dwarf representation of the relevant enum type itself.  */
17621           break;
17622         }
17623
17624       /* Emit its type.  */
17625       gen_type_die (TREE_TYPE (decl), context_die);
17626
17627       /* And its containing namespace.  */
17628       context_die = declare_in_namespace (decl, context_die);
17629
17630       gen_const_die (decl, context_die);
17631       break;
17632
17633     case FUNCTION_DECL:
17634       /* Don't output any DIEs to represent mere function declarations,
17635          unless they are class members or explicit block externs.  */
17636       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
17637           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
17638           && (current_function_decl == NULL_TREE
17639               || DECL_ARTIFICIAL (decl_or_origin)))
17640         break;
17641
17642 #if 0
17643       /* FIXME */
17644       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
17645          on local redeclarations of global functions.  That seems broken.  */
17646       if (current_function_decl != decl)
17647         /* This is only a declaration.  */;
17648 #endif
17649
17650       /* If we're emitting a clone, emit info for the abstract instance.  */
17651       if (origin || DECL_ORIGIN (decl) != decl)
17652         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
17653
17654       /* If we're emitting an out-of-line copy of an inline function,
17655          emit info for the abstract instance and set up to refer to it.  */
17656       else if (cgraph_function_possibly_inlined_p (decl)
17657                && ! DECL_ABSTRACT (decl)
17658                && ! class_or_namespace_scope_p (context_die)
17659                /* dwarf2out_abstract_function won't emit a die if this is just
17660                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
17661                   that case, because that works only if we have a die.  */
17662                && DECL_INITIAL (decl) != NULL_TREE)
17663         {
17664           dwarf2out_abstract_function (decl);
17665           set_decl_origin_self (decl);
17666         }
17667
17668       /* Otherwise we're emitting the primary DIE for this decl.  */
17669       else if (debug_info_level > DINFO_LEVEL_TERSE)
17670         {
17671           /* Before we describe the FUNCTION_DECL itself, make sure that we
17672              have described its return type.  */
17673           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
17674
17675           /* And its virtual context.  */
17676           if (DECL_VINDEX (decl) != NULL_TREE)
17677             gen_type_die (DECL_CONTEXT (decl), context_die);
17678
17679           /* And its containing type.  */
17680           if (!origin)
17681             origin = decl_class_context (decl);
17682           if (origin != NULL_TREE)
17683             gen_type_die_for_member (origin, decl, context_die);
17684
17685           /* And its containing namespace.  */
17686           context_die = declare_in_namespace (decl, context_die);
17687         }
17688
17689       /* Now output a DIE to represent the function itself.  */
17690       if (decl)
17691         gen_subprogram_die (decl, context_die);
17692       break;
17693
17694     case TYPE_DECL:
17695       /* If we are in terse mode, don't generate any DIEs to represent any
17696          actual typedefs.  */
17697       if (debug_info_level <= DINFO_LEVEL_TERSE)
17698         break;
17699
17700       /* In the special case of a TYPE_DECL node representing the declaration
17701          of some type tag, if the given TYPE_DECL is marked as having been
17702          instantiated from some other (original) TYPE_DECL node (e.g. one which
17703          was generated within the original definition of an inline function) we
17704          used to generate a special (abbreviated) DW_TAG_structure_type,
17705          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
17706          should be actually referencing those DIEs, as variable DIEs with that
17707          type would be emitted already in the abstract origin, so it was always
17708          removed during unused type prunning.  Don't add anything in this
17709          case.  */
17710       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
17711         break;
17712
17713       if (is_redundant_typedef (decl))
17714         gen_type_die (TREE_TYPE (decl), context_die);
17715       else
17716         /* Output a DIE to represent the typedef itself.  */
17717         gen_typedef_die (decl, context_die);
17718       break;
17719
17720     case LABEL_DECL:
17721       if (debug_info_level >= DINFO_LEVEL_NORMAL)
17722         gen_label_die (decl, context_die);
17723       break;
17724
17725     case VAR_DECL:
17726     case RESULT_DECL:
17727       /* If we are in terse mode, don't generate any DIEs to represent any
17728          variable declarations or definitions.  */
17729       if (debug_info_level <= DINFO_LEVEL_TERSE)
17730         break;
17731
17732       /* Output any DIEs that are needed to specify the type of this data
17733          object.  */
17734       if (decl_by_reference_p (decl_or_origin))
17735         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17736       else
17737         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17738
17739       /* And its containing type.  */
17740       class_origin = decl_class_context (decl_or_origin);
17741       if (class_origin != NULL_TREE)
17742         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
17743
17744       /* And its containing namespace.  */
17745       context_die = declare_in_namespace (decl_or_origin, context_die);
17746
17747       /* Now output the DIE to represent the data object itself.  This gets
17748          complicated because of the possibility that the VAR_DECL really
17749          represents an inlined instance of a formal parameter for an inline
17750          function.  */
17751       if (!origin)
17752         origin = decl_ultimate_origin (decl);
17753       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
17754         gen_formal_parameter_die (decl, origin,
17755                                   true /* Emit name attribute.  */,
17756                                   context_die);
17757       else
17758         gen_variable_die (decl, origin, context_die);
17759       break;
17760
17761     case FIELD_DECL:
17762       /* Ignore the nameless fields that are used to skip bits but handle C++
17763          anonymous unions and structs.  */
17764       if (DECL_NAME (decl) != NULL_TREE
17765           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
17766           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
17767         {
17768           gen_type_die (member_declared_type (decl), context_die);
17769           gen_field_die (decl, context_die);
17770         }
17771       break;
17772
17773     case PARM_DECL:
17774       if (DECL_BY_REFERENCE (decl_or_origin))
17775         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17776       else
17777         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17778       gen_formal_parameter_die (decl, origin,
17779                                 true /* Emit name attribute.  */,
17780                                 context_die);
17781       break;
17782
17783     case NAMESPACE_DECL:
17784     case IMPORTED_DECL:
17785       if (dwarf_version >= 3 || !dwarf_strict)
17786         gen_namespace_die (decl, context_die);
17787       break;
17788
17789     default:
17790       /* Probably some frontend-internal decl.  Assume we don't care.  */
17791       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
17792       break;
17793     }
17794 }
17795 \f
17796 /* Output debug information for global decl DECL.  Called from toplev.c after
17797    compilation proper has finished.  */
17798
17799 static void
17800 dwarf2out_global_decl (tree decl)
17801 {
17802   /* Output DWARF2 information for file-scope tentative data object
17803      declarations, file-scope (extern) function declarations (which
17804      had no corresponding body) and file-scope tagged type declarations
17805      and definitions which have not yet been forced out.  */
17806   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
17807     dwarf2out_decl (decl);
17808 }
17809
17810 /* Output debug information for type decl DECL.  Called from toplev.c
17811    and from language front ends (to record built-in types).  */
17812 static void
17813 dwarf2out_type_decl (tree decl, int local)
17814 {
17815   if (!local)
17816     dwarf2out_decl (decl);
17817 }
17818
17819 /* Output debug information for imported module or decl DECL.
17820    NAME is non-NULL name in the lexical block if the decl has been renamed.
17821    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
17822    that DECL belongs to.
17823    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
17824 static void
17825 dwarf2out_imported_module_or_decl_1 (tree decl,
17826                                      tree name,
17827                                      tree lexical_block,
17828                                      dw_die_ref lexical_block_die)
17829 {
17830   expanded_location xloc;
17831   dw_die_ref imported_die = NULL;
17832   dw_die_ref at_import_die;
17833
17834   if (TREE_CODE (decl) == IMPORTED_DECL)
17835     {
17836       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
17837       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
17838       gcc_assert (decl);
17839     }
17840   else
17841     xloc = expand_location (input_location);
17842
17843   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
17844     {
17845       if (is_base_type (TREE_TYPE (decl)))
17846         at_import_die = base_type_die (TREE_TYPE (decl));
17847       else
17848         at_import_die = force_type_die (TREE_TYPE (decl));
17849       /* For namespace N { typedef void T; } using N::T; base_type_die
17850          returns NULL, but DW_TAG_imported_declaration requires
17851          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
17852       if (!at_import_die)
17853         {
17854           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
17855           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
17856           at_import_die = lookup_type_die (TREE_TYPE (decl));
17857           gcc_assert (at_import_die);
17858         }
17859     }
17860   else
17861     {
17862       at_import_die = lookup_decl_die (decl);
17863       if (!at_import_die)
17864         {
17865           /* If we're trying to avoid duplicate debug info, we may not have
17866              emitted the member decl for this field.  Emit it now.  */
17867           if (TREE_CODE (decl) == FIELD_DECL)
17868             {
17869               tree type = DECL_CONTEXT (decl);
17870
17871               if (TYPE_CONTEXT (type)
17872                   && TYPE_P (TYPE_CONTEXT (type))
17873                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
17874                                                 DINFO_USAGE_DIR_USE))
17875                 return;
17876               gen_type_die_for_member (type, decl,
17877                                        get_context_die (TYPE_CONTEXT (type)));
17878             }
17879           at_import_die = force_decl_die (decl);
17880         }
17881     }
17882
17883   if (TREE_CODE (decl) == NAMESPACE_DECL)
17884     {
17885       if (dwarf_version >= 3 || !dwarf_strict)
17886         imported_die = new_die (DW_TAG_imported_module,
17887                                 lexical_block_die,
17888                                 lexical_block);
17889       else
17890         return;
17891     }
17892   else
17893     imported_die = new_die (DW_TAG_imported_declaration,
17894                             lexical_block_die,
17895                             lexical_block);
17896
17897   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
17898   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
17899   if (name)
17900     add_AT_string (imported_die, DW_AT_name,
17901                    IDENTIFIER_POINTER (name));
17902   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
17903 }
17904
17905 /* Output debug information for imported module or decl DECL.
17906    NAME is non-NULL name in context if the decl has been renamed.
17907    CHILD is true if decl is one of the renamed decls as part of
17908    importing whole module.  */
17909
17910 static void
17911 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
17912                                    bool child)
17913 {
17914   /* dw_die_ref at_import_die;  */
17915   dw_die_ref scope_die;
17916
17917   if (debug_info_level <= DINFO_LEVEL_TERSE)
17918     return;
17919
17920   gcc_assert (decl);
17921
17922   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
17923      We need decl DIE for reference and scope die. First, get DIE for the decl
17924      itself.  */
17925
17926   /* Get the scope die for decl context. Use comp_unit_die for global module
17927      or decl. If die is not found for non globals, force new die.  */
17928   if (context
17929       && TYPE_P (context)
17930       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
17931     return;
17932
17933   if (!(dwarf_version >= 3 || !dwarf_strict))
17934     return;
17935
17936   scope_die = get_context_die (context);
17937
17938   if (child)
17939     {
17940       gcc_assert (scope_die->die_child);
17941       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
17942       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
17943       scope_die = scope_die->die_child;
17944     }
17945
17946   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
17947   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
17948
17949 }
17950
17951 /* Write the debugging output for DECL.  */
17952
17953 void
17954 dwarf2out_decl (tree decl)
17955 {
17956   dw_die_ref context_die = comp_unit_die;
17957
17958   switch (TREE_CODE (decl))
17959     {
17960     case ERROR_MARK:
17961       return;
17962
17963     case FUNCTION_DECL:
17964       /* What we would really like to do here is to filter out all mere
17965          file-scope declarations of file-scope functions which are never
17966          referenced later within this translation unit (and keep all of ones
17967          that *are* referenced later on) but we aren't clairvoyant, so we have
17968          no idea which functions will be referenced in the future (i.e. later
17969          on within the current translation unit). So here we just ignore all
17970          file-scope function declarations which are not also definitions.  If
17971          and when the debugger needs to know something about these functions,
17972          it will have to hunt around and find the DWARF information associated
17973          with the definition of the function.
17974
17975          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
17976          nodes represent definitions and which ones represent mere
17977          declarations.  We have to check DECL_INITIAL instead. That's because
17978          the C front-end supports some weird semantics for "extern inline"
17979          function definitions.  These can get inlined within the current
17980          translation unit (and thus, we need to generate Dwarf info for their
17981          abstract instances so that the Dwarf info for the concrete inlined
17982          instances can have something to refer to) but the compiler never
17983          generates any out-of-lines instances of such things (despite the fact
17984          that they *are* definitions).
17985
17986          The important point is that the C front-end marks these "extern
17987          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
17988          them anyway. Note that the C++ front-end also plays some similar games
17989          for inline function definitions appearing within include files which
17990          also contain `#pragma interface' pragmas.  */
17991       if (DECL_INITIAL (decl) == NULL_TREE)
17992         return;
17993
17994       /* If we're a nested function, initially use a parent of NULL; if we're
17995          a plain function, this will be fixed up in decls_for_scope.  If
17996          we're a method, it will be ignored, since we already have a DIE.  */
17997       if (decl_function_context (decl)
17998           /* But if we're in terse mode, we don't care about scope.  */
17999           && debug_info_level > DINFO_LEVEL_TERSE)
18000         context_die = NULL;
18001       break;
18002
18003     case VAR_DECL:
18004       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
18005          declaration and if the declaration was never even referenced from
18006          within this entire compilation unit.  We suppress these DIEs in
18007          order to save space in the .debug section (by eliminating entries
18008          which are probably useless).  Note that we must not suppress
18009          block-local extern declarations (whether used or not) because that
18010          would screw-up the debugger's name lookup mechanism and cause it to
18011          miss things which really ought to be in scope at a given point.  */
18012       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
18013         return;
18014
18015       /* For local statics lookup proper context die.  */
18016       if (TREE_STATIC (decl) && decl_function_context (decl))
18017         context_die = lookup_decl_die (DECL_CONTEXT (decl));
18018
18019       /* If we are in terse mode, don't generate any DIEs to represent any
18020          variable declarations or definitions.  */
18021       if (debug_info_level <= DINFO_LEVEL_TERSE)
18022         return;
18023       break;
18024
18025     case CONST_DECL:
18026       if (debug_info_level <= DINFO_LEVEL_TERSE)
18027         return;
18028       if (!is_fortran ())
18029         return;
18030       if (TREE_STATIC (decl) && decl_function_context (decl))
18031         context_die = lookup_decl_die (DECL_CONTEXT (decl));
18032       break;
18033
18034     case NAMESPACE_DECL:
18035     case IMPORTED_DECL:
18036       if (debug_info_level <= DINFO_LEVEL_TERSE)
18037         return;
18038       if (lookup_decl_die (decl) != NULL)
18039         return;
18040       break;
18041
18042     case TYPE_DECL:
18043       /* Don't emit stubs for types unless they are needed by other DIEs.  */
18044       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
18045         return;
18046
18047       /* Don't bother trying to generate any DIEs to represent any of the
18048          normal built-in types for the language we are compiling.  */
18049       if (DECL_IS_BUILTIN (decl))
18050         {
18051           /* OK, we need to generate one for `bool' so GDB knows what type
18052              comparisons have.  */
18053           if (is_cxx ()
18054               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
18055               && ! DECL_IGNORED_P (decl))
18056             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
18057
18058           return;
18059         }
18060
18061       /* If we are in terse mode, don't generate any DIEs for types.  */
18062       if (debug_info_level <= DINFO_LEVEL_TERSE)
18063         return;
18064
18065       /* If we're a function-scope tag, initially use a parent of NULL;
18066          this will be fixed up in decls_for_scope.  */
18067       if (decl_function_context (decl))
18068         context_die = NULL;
18069
18070       break;
18071
18072     default:
18073       return;
18074     }
18075
18076   gen_decl_die (decl, NULL, context_die);
18077 }
18078
18079 /* Output a marker (i.e. a label) for the beginning of the generated code for
18080    a lexical block.  */
18081
18082 static void
18083 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
18084                        unsigned int blocknum)
18085 {
18086   switch_to_section (current_function_section ());
18087   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
18088 }
18089
18090 /* Output a marker (i.e. a label) for the end of the generated code for a
18091    lexical block.  */
18092
18093 static void
18094 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
18095 {
18096   switch_to_section (current_function_section ());
18097   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
18098 }
18099
18100 /* Returns nonzero if it is appropriate not to emit any debugging
18101    information for BLOCK, because it doesn't contain any instructions.
18102
18103    Don't allow this for blocks with nested functions or local classes
18104    as we would end up with orphans, and in the presence of scheduling
18105    we may end up calling them anyway.  */
18106
18107 static bool
18108 dwarf2out_ignore_block (const_tree block)
18109 {
18110   tree decl;
18111   unsigned int i;
18112
18113   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
18114     if (TREE_CODE (decl) == FUNCTION_DECL
18115         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
18116       return 0;
18117   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
18118     {
18119       decl = BLOCK_NONLOCALIZED_VAR (block, i);
18120       if (TREE_CODE (decl) == FUNCTION_DECL
18121           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
18122       return 0;
18123     }
18124
18125   return 1;
18126 }
18127
18128 /* Hash table routines for file_hash.  */
18129
18130 static int
18131 file_table_eq (const void *p1_p, const void *p2_p)
18132 {
18133   const struct dwarf_file_data *const p1 =
18134     (const struct dwarf_file_data *) p1_p;
18135   const char *const p2 = (const char *) p2_p;
18136   return strcmp (p1->filename, p2) == 0;
18137 }
18138
18139 static hashval_t
18140 file_table_hash (const void *p_p)
18141 {
18142   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
18143   return htab_hash_string (p->filename);
18144 }
18145
18146 /* Lookup FILE_NAME (in the list of filenames that we know about here in
18147    dwarf2out.c) and return its "index".  The index of each (known) filename is
18148    just a unique number which is associated with only that one filename.  We
18149    need such numbers for the sake of generating labels (in the .debug_sfnames
18150    section) and references to those files numbers (in the .debug_srcinfo
18151    and.debug_macinfo sections).  If the filename given as an argument is not
18152    found in our current list, add it to the list and assign it the next
18153    available unique index number.  In order to speed up searches, we remember
18154    the index of the filename was looked up last.  This handles the majority of
18155    all searches.  */
18156
18157 static struct dwarf_file_data *
18158 lookup_filename (const char *file_name)
18159 {
18160   void ** slot;
18161   struct dwarf_file_data * created;
18162
18163   /* Check to see if the file name that was searched on the previous
18164      call matches this file name.  If so, return the index.  */
18165   if (file_table_last_lookup
18166       && (file_name == file_table_last_lookup->filename
18167           || strcmp (file_table_last_lookup->filename, file_name) == 0))
18168     return file_table_last_lookup;
18169
18170   /* Didn't match the previous lookup, search the table.  */
18171   slot = htab_find_slot_with_hash (file_table, file_name,
18172                                    htab_hash_string (file_name), INSERT);
18173   if (*slot)
18174     return (struct dwarf_file_data *) *slot;
18175
18176   created = GGC_NEW (struct dwarf_file_data);
18177   created->filename = file_name;
18178   created->emitted_number = 0;
18179   *slot = created;
18180   return created;
18181 }
18182
18183 /* If the assembler will construct the file table, then translate the compiler
18184    internal file table number into the assembler file table number, and emit
18185    a .file directive if we haven't already emitted one yet.  The file table
18186    numbers are different because we prune debug info for unused variables and
18187    types, which may include filenames.  */
18188
18189 static int
18190 maybe_emit_file (struct dwarf_file_data * fd)
18191 {
18192   if (! fd->emitted_number)
18193     {
18194       if (last_emitted_file)
18195         fd->emitted_number = last_emitted_file->emitted_number + 1;
18196       else
18197         fd->emitted_number = 1;
18198       last_emitted_file = fd;
18199
18200       if (DWARF2_ASM_LINE_DEBUG_INFO)
18201         {
18202           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
18203           output_quoted_string (asm_out_file,
18204                                 remap_debug_filename (fd->filename));
18205           fputc ('\n', asm_out_file);
18206         }
18207     }
18208
18209   return fd->emitted_number;
18210 }
18211
18212 /* Schedule generation of a DW_AT_const_value attribute to DIE.
18213    That generation should happen after function debug info has been
18214    generated. The value of the attribute is the constant value of ARG.  */
18215
18216 static void
18217 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
18218 {
18219   die_arg_entry entry;
18220
18221   if (!die || !arg)
18222     return;
18223
18224   if (!tmpl_value_parm_die_table)
18225     tmpl_value_parm_die_table
18226       = VEC_alloc (die_arg_entry, gc, 32);
18227
18228   entry.die = die;
18229   entry.arg = arg;
18230   VEC_safe_push (die_arg_entry, gc,
18231                  tmpl_value_parm_die_table,
18232                  &entry);
18233 }
18234
18235 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
18236    by append_entry_to_tmpl_value_parm_die_table. This function must
18237    be called after function DIEs have been generated.  */
18238
18239 static void
18240 gen_remaining_tmpl_value_param_die_attribute (void)
18241 {
18242   if (tmpl_value_parm_die_table)
18243     {
18244       unsigned i;
18245       die_arg_entry *e;
18246
18247       for (i = 0;
18248            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
18249            i++)
18250         tree_add_const_value_attribute (e->die, e->arg);
18251     }
18252 }
18253
18254
18255 /* Replace DW_AT_name for the decl with name.  */
18256  
18257 static void
18258 dwarf2out_set_name (tree decl, tree name)
18259 {
18260   dw_die_ref die;
18261   dw_attr_ref attr;
18262
18263   die = TYPE_SYMTAB_DIE (decl);
18264   if (!die)
18265     return;
18266
18267   attr = get_AT (die, DW_AT_name);
18268   if (attr)
18269     {
18270       struct indirect_string_node *node;
18271
18272       node = find_AT_string (dwarf2_name (name, 0));
18273       /* replace the string.  */
18274       attr->dw_attr_val.v.val_str = node;
18275     }
18276
18277   else
18278     add_name_attribute (die, dwarf2_name (name, 0));
18279 }
18280
18281 /* Called by the final INSN scan whenever we see a var location.  We
18282    use it to drop labels in the right places, and throw the location in
18283    our lookup table.  */
18284
18285 static void
18286 dwarf2out_var_location (rtx loc_note)
18287 {
18288   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
18289   struct var_loc_node *newloc;
18290   rtx next_real;
18291   static const char *last_label;
18292   static const char *last_postcall_label;
18293   static bool last_in_cold_section_p;
18294   tree decl;
18295
18296   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
18297     return;
18298
18299   next_real = next_real_insn (loc_note);
18300   /* If there are no instructions which would be affected by this note,
18301      don't do anything.  */
18302   if (next_real == NULL_RTX)
18303     return;
18304
18305   newloc = GGC_CNEW (struct var_loc_node);
18306   /* If there were no real insns between note we processed last time
18307      and this note, use the label we emitted last time.  */
18308   if (last_var_location_insn == NULL_RTX
18309       || last_var_location_insn != next_real
18310       || last_in_cold_section_p != in_cold_section_p)
18311     {
18312       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
18313       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
18314       loclabel_num++;
18315       last_label = ggc_strdup (loclabel);
18316       if (!NOTE_DURING_CALL_P (loc_note))
18317         last_postcall_label = NULL;
18318     }
18319   newloc->var_loc_note = loc_note;
18320   newloc->next = NULL;
18321
18322   if (!NOTE_DURING_CALL_P (loc_note))
18323     newloc->label = last_label;
18324   else
18325     {
18326       if (!last_postcall_label)
18327         {
18328           sprintf (loclabel, "%s-1", last_label);
18329           last_postcall_label = ggc_strdup (loclabel);
18330         }
18331       newloc->label = last_postcall_label;
18332     }
18333
18334   if (cfun && in_cold_section_p)
18335     newloc->section_label = crtl->subsections.cold_section_label;
18336   else
18337     newloc->section_label = text_section_label;
18338
18339   last_var_location_insn = next_real;
18340   last_in_cold_section_p = in_cold_section_p;
18341   decl = NOTE_VAR_LOCATION_DECL (loc_note);
18342   add_var_loc_to_decl (decl, newloc);
18343 }
18344
18345 /* We need to reset the locations at the beginning of each
18346    function. We can't do this in the end_function hook, because the
18347    declarations that use the locations won't have been output when
18348    that hook is called.  Also compute have_multiple_function_sections here.  */
18349
18350 static void
18351 dwarf2out_begin_function (tree fun)
18352 {
18353   htab_empty (decl_loc_table);
18354
18355   if (function_section (fun) != text_section)
18356     have_multiple_function_sections = true;
18357
18358   dwarf2out_note_section_used ();
18359 }
18360
18361 /* Output a label to mark the beginning of a source code line entry
18362    and record information relating to this source line, in
18363    'line_info_table' for later output of the .debug_line section.  */
18364
18365 static void
18366 dwarf2out_source_line (unsigned int line, const char *filename,
18367                        int discriminator, bool is_stmt)
18368 {
18369   static bool last_is_stmt = true;
18370
18371   if (debug_info_level >= DINFO_LEVEL_NORMAL
18372       && line != 0)
18373     {
18374       int file_num = maybe_emit_file (lookup_filename (filename));
18375
18376       switch_to_section (current_function_section ());
18377
18378       /* If requested, emit something human-readable.  */
18379       if (flag_debug_asm)
18380         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
18381                  filename, line);
18382
18383       if (DWARF2_ASM_LINE_DEBUG_INFO)
18384         {
18385           /* Emit the .loc directive understood by GNU as.  */
18386           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
18387           if (is_stmt != last_is_stmt)
18388             {
18389               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
18390               last_is_stmt = is_stmt;
18391             }
18392           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
18393             fprintf (asm_out_file, " discriminator %d", discriminator);
18394           fputc ('\n', asm_out_file);
18395
18396           /* Indicate that line number info exists.  */
18397           line_info_table_in_use++;
18398         }
18399       else if (function_section (current_function_decl) != text_section)
18400         {
18401           dw_separate_line_info_ref line_info;
18402           targetm.asm_out.internal_label (asm_out_file,
18403                                           SEPARATE_LINE_CODE_LABEL,
18404                                           separate_line_info_table_in_use);
18405
18406           /* Expand the line info table if necessary.  */
18407           if (separate_line_info_table_in_use
18408               == separate_line_info_table_allocated)
18409             {
18410               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
18411               separate_line_info_table
18412                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
18413                                  separate_line_info_table,
18414                                  separate_line_info_table_allocated);
18415               memset (separate_line_info_table
18416                        + separate_line_info_table_in_use,
18417                       0,
18418                       (LINE_INFO_TABLE_INCREMENT
18419                        * sizeof (dw_separate_line_info_entry)));
18420             }
18421
18422           /* Add the new entry at the end of the line_info_table.  */
18423           line_info
18424             = &separate_line_info_table[separate_line_info_table_in_use++];
18425           line_info->dw_file_num = file_num;
18426           line_info->dw_line_num = line;
18427           line_info->function = current_function_funcdef_no;
18428         }
18429       else
18430         {
18431           dw_line_info_ref line_info;
18432
18433           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
18434                                      line_info_table_in_use);
18435
18436           /* Expand the line info table if necessary.  */
18437           if (line_info_table_in_use == line_info_table_allocated)
18438             {
18439               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
18440               line_info_table
18441                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
18442                                  line_info_table_allocated);
18443               memset (line_info_table + line_info_table_in_use, 0,
18444                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
18445             }
18446
18447           /* Add the new entry at the end of the line_info_table.  */
18448           line_info = &line_info_table[line_info_table_in_use++];
18449           line_info->dw_file_num = file_num;
18450           line_info->dw_line_num = line;
18451         }
18452     }
18453 }
18454
18455 /* Record the beginning of a new source file.  */
18456
18457 static void
18458 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
18459 {
18460   if (flag_eliminate_dwarf2_dups)
18461     {
18462       /* Record the beginning of the file for break_out_includes.  */
18463       dw_die_ref bincl_die;
18464
18465       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
18466       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
18467     }
18468
18469   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18470     {
18471       int file_num = maybe_emit_file (lookup_filename (filename));
18472
18473       switch_to_section (debug_macinfo_section);
18474       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
18475       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
18476                                    lineno);
18477
18478       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
18479     }
18480 }
18481
18482 /* Record the end of a source file.  */
18483
18484 static void
18485 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
18486 {
18487   if (flag_eliminate_dwarf2_dups)
18488     /* Record the end of the file for break_out_includes.  */
18489     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
18490
18491   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18492     {
18493       switch_to_section (debug_macinfo_section);
18494       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
18495     }
18496 }
18497
18498 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
18499    the tail part of the directive line, i.e. the part which is past the
18500    initial whitespace, #, whitespace, directive-name, whitespace part.  */
18501
18502 static void
18503 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
18504                   const char *buffer ATTRIBUTE_UNUSED)
18505 {
18506   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18507     {
18508       switch_to_section (debug_macinfo_section);
18509       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
18510       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
18511       dw2_asm_output_nstring (buffer, -1, "The macro");
18512     }
18513 }
18514
18515 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
18516    the tail part of the directive line, i.e. the part which is past the
18517    initial whitespace, #, whitespace, directive-name, whitespace part.  */
18518
18519 static void
18520 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
18521                  const char *buffer ATTRIBUTE_UNUSED)
18522 {
18523   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18524     {
18525       switch_to_section (debug_macinfo_section);
18526       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
18527       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
18528       dw2_asm_output_nstring (buffer, -1, "The macro");
18529     }
18530 }
18531
18532 /* Set up for Dwarf output at the start of compilation.  */
18533
18534 static void
18535 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
18536 {
18537   /* Allocate the file_table.  */
18538   file_table = htab_create_ggc (50, file_table_hash,
18539                                 file_table_eq, NULL);
18540
18541   /* Allocate the decl_die_table.  */
18542   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
18543                                     decl_die_table_eq, NULL);
18544
18545   /* Allocate the decl_loc_table.  */
18546   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
18547                                     decl_loc_table_eq, NULL);
18548
18549   /* Allocate the initial hunk of the decl_scope_table.  */
18550   decl_scope_table = VEC_alloc (tree, gc, 256);
18551
18552   /* Allocate the initial hunk of the abbrev_die_table.  */
18553   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
18554   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
18555   /* Zero-th entry is allocated, but unused.  */
18556   abbrev_die_table_in_use = 1;
18557
18558   /* Allocate the initial hunk of the line_info_table.  */
18559   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
18560   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
18561
18562   /* Zero-th entry is allocated, but unused.  */
18563   line_info_table_in_use = 1;
18564
18565   /* Allocate the pubtypes and pubnames vectors.  */
18566   pubname_table = VEC_alloc (pubname_entry, gc, 32);
18567   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
18568
18569   /* Generate the initial DIE for the .debug section.  Note that the (string)
18570      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
18571      will (typically) be a relative pathname and that this pathname should be
18572      taken as being relative to the directory from which the compiler was
18573      invoked when the given (base) source file was compiled.  We will fill
18574      in this value in dwarf2out_finish.  */
18575   comp_unit_die = gen_compile_unit_die (NULL);
18576
18577   incomplete_types = VEC_alloc (tree, gc, 64);
18578
18579   used_rtx_array = VEC_alloc (rtx, gc, 32);
18580
18581   debug_info_section = get_section (DEBUG_INFO_SECTION,
18582                                     SECTION_DEBUG, NULL);
18583   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
18584                                       SECTION_DEBUG, NULL);
18585   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
18586                                        SECTION_DEBUG, NULL);
18587   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
18588                                        SECTION_DEBUG, NULL);
18589   debug_line_section = get_section (DEBUG_LINE_SECTION,
18590                                     SECTION_DEBUG, NULL);
18591   debug_loc_section = get_section (DEBUG_LOC_SECTION,
18592                                    SECTION_DEBUG, NULL);
18593   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
18594                                         SECTION_DEBUG, NULL);
18595   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
18596                                         SECTION_DEBUG, NULL);
18597   debug_str_section = get_section (DEBUG_STR_SECTION,
18598                                    DEBUG_STR_SECTION_FLAGS, NULL);
18599   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
18600                                       SECTION_DEBUG, NULL);
18601   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
18602                                      SECTION_DEBUG, NULL);
18603
18604   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
18605   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
18606                                DEBUG_ABBREV_SECTION_LABEL, 0);
18607   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
18608   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
18609                                COLD_TEXT_SECTION_LABEL, 0);
18610   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
18611
18612   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
18613                                DEBUG_INFO_SECTION_LABEL, 0);
18614   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
18615                                DEBUG_LINE_SECTION_LABEL, 0);
18616   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
18617                                DEBUG_RANGES_SECTION_LABEL, 0);
18618   switch_to_section (debug_abbrev_section);
18619   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
18620   switch_to_section (debug_info_section);
18621   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
18622   switch_to_section (debug_line_section);
18623   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
18624
18625   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18626     {
18627       switch_to_section (debug_macinfo_section);
18628       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
18629                                    DEBUG_MACINFO_SECTION_LABEL, 0);
18630       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
18631     }
18632
18633   switch_to_section (text_section);
18634   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
18635   if (flag_reorder_blocks_and_partition)
18636     {
18637       cold_text_section = unlikely_text_section ();
18638       switch_to_section (cold_text_section);
18639       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
18640     }
18641
18642 #ifdef HAVE_GAS_CFI_SECTIONS_DIRECTIVE
18643   if (dwarf2out_do_cfi_asm ())
18644     {
18645 #ifndef TARGET_UNWIND_INFO
18646       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
18647 #endif
18648         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
18649     }
18650 #endif
18651 }
18652
18653 /* A helper function for dwarf2out_finish called through
18654    htab_traverse.  Emit one queued .debug_str string.  */
18655
18656 static int
18657 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
18658 {
18659   struct indirect_string_node *node = (struct indirect_string_node *) *h;
18660
18661   if (node->label && node->refcount)
18662     {
18663       switch_to_section (debug_str_section);
18664       ASM_OUTPUT_LABEL (asm_out_file, node->label);
18665       assemble_string (node->str, strlen (node->str) + 1);
18666     }
18667
18668   return 1;
18669 }
18670
18671 #if ENABLE_ASSERT_CHECKING
18672 /* Verify that all marks are clear.  */
18673
18674 static void
18675 verify_marks_clear (dw_die_ref die)
18676 {
18677   dw_die_ref c;
18678
18679   gcc_assert (! die->die_mark);
18680   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
18681 }
18682 #endif /* ENABLE_ASSERT_CHECKING */
18683
18684 /* Clear the marks for a die and its children.
18685    Be cool if the mark isn't set.  */
18686
18687 static void
18688 prune_unmark_dies (dw_die_ref die)
18689 {
18690   dw_die_ref c;
18691
18692   if (die->die_mark)
18693     die->die_mark = 0;
18694   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
18695 }
18696
18697 /* Given DIE that we're marking as used, find any other dies
18698    it references as attributes and mark them as used.  */
18699
18700 static void
18701 prune_unused_types_walk_attribs (dw_die_ref die)
18702 {
18703   dw_attr_ref a;
18704   unsigned ix;
18705
18706   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18707     {
18708       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
18709         {
18710           /* A reference to another DIE.
18711              Make sure that it will get emitted.  */
18712           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
18713         }
18714       /* Set the string's refcount to 0 so that prune_unused_types_mark
18715          accounts properly for it.  */
18716       if (AT_class (a) == dw_val_class_str)
18717         a->dw_attr_val.v.val_str->refcount = 0;
18718     }
18719 }
18720
18721
18722 /* Mark DIE as being used.  If DOKIDS is true, then walk down
18723    to DIE's children.  */
18724
18725 static void
18726 prune_unused_types_mark (dw_die_ref die, int dokids)
18727 {
18728   dw_die_ref c;
18729
18730   if (die->die_mark == 0)
18731     {
18732       /* We haven't done this node yet.  Mark it as used.  */
18733       die->die_mark = 1;
18734
18735       /* We also have to mark its parents as used.
18736          (But we don't want to mark our parents' kids due to this.)  */
18737       if (die->die_parent)
18738         prune_unused_types_mark (die->die_parent, 0);
18739
18740       /* Mark any referenced nodes.  */
18741       prune_unused_types_walk_attribs (die);
18742
18743       /* If this node is a specification,
18744          also mark the definition, if it exists.  */
18745       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
18746         prune_unused_types_mark (die->die_definition, 1);
18747     }
18748
18749   if (dokids && die->die_mark != 2)
18750     {
18751       /* We need to walk the children, but haven't done so yet.
18752          Remember that we've walked the kids.  */
18753       die->die_mark = 2;
18754
18755       /* If this is an array type, we need to make sure our
18756          kids get marked, even if they're types.  */
18757       if (die->die_tag == DW_TAG_array_type)
18758         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
18759       else
18760         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18761     }
18762 }
18763
18764 /* For local classes, look if any static member functions were emitted
18765    and if so, mark them.  */
18766
18767 static void
18768 prune_unused_types_walk_local_classes (dw_die_ref die)
18769 {
18770   dw_die_ref c;
18771
18772   if (die->die_mark == 2)
18773     return;
18774
18775   switch (die->die_tag)
18776     {
18777     case DW_TAG_structure_type:
18778     case DW_TAG_union_type:
18779     case DW_TAG_class_type:
18780       break;
18781
18782     case DW_TAG_subprogram:
18783       if (!get_AT_flag (die, DW_AT_declaration)
18784           || die->die_definition != NULL)
18785         prune_unused_types_mark (die, 1);
18786       return;
18787
18788     default:
18789       return;
18790     }
18791
18792   /* Mark children.  */
18793   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
18794 }
18795
18796 /* Walk the tree DIE and mark types that we actually use.  */
18797
18798 static void
18799 prune_unused_types_walk (dw_die_ref die)
18800 {
18801   dw_die_ref c;
18802
18803   /* Don't do anything if this node is already marked and
18804      children have been marked as well.  */
18805   if (die->die_mark == 2)
18806     return;
18807
18808   switch (die->die_tag)
18809     {
18810     case DW_TAG_structure_type:
18811     case DW_TAG_union_type:
18812     case DW_TAG_class_type:
18813       if (die->die_perennial_p)
18814         break;
18815
18816       for (c = die->die_parent; c; c = c->die_parent)
18817         if (c->die_tag == DW_TAG_subprogram)
18818           break;
18819
18820       /* Finding used static member functions inside of classes
18821          is needed just for local classes, because for other classes
18822          static member function DIEs with DW_AT_specification
18823          are emitted outside of the DW_TAG_*_type.  If we ever change
18824          it, we'd need to call this even for non-local classes.  */
18825       if (c)
18826         prune_unused_types_walk_local_classes (die);
18827
18828       /* It's a type node --- don't mark it.  */
18829       return;
18830
18831     case DW_TAG_const_type:
18832     case DW_TAG_packed_type:
18833     case DW_TAG_pointer_type:
18834     case DW_TAG_reference_type:
18835     case DW_TAG_volatile_type:
18836     case DW_TAG_typedef:
18837     case DW_TAG_array_type:
18838     case DW_TAG_interface_type:
18839     case DW_TAG_friend:
18840     case DW_TAG_variant_part:
18841     case DW_TAG_enumeration_type:
18842     case DW_TAG_subroutine_type:
18843     case DW_TAG_string_type:
18844     case DW_TAG_set_type:
18845     case DW_TAG_subrange_type:
18846     case DW_TAG_ptr_to_member_type:
18847     case DW_TAG_file_type:
18848       if (die->die_perennial_p)
18849         break;
18850
18851       /* It's a type node --- don't mark it.  */
18852       return;
18853
18854     default:
18855       /* Mark everything else.  */
18856       break;
18857   }
18858
18859   if (die->die_mark == 0)
18860     {
18861       die->die_mark = 1;
18862
18863       /* Now, mark any dies referenced from here.  */
18864       prune_unused_types_walk_attribs (die);
18865     }
18866
18867   die->die_mark = 2;
18868
18869   /* Mark children.  */
18870   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18871 }
18872
18873 /* Increment the string counts on strings referred to from DIE's
18874    attributes.  */
18875
18876 static void
18877 prune_unused_types_update_strings (dw_die_ref die)
18878 {
18879   dw_attr_ref a;
18880   unsigned ix;
18881
18882   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18883     if (AT_class (a) == dw_val_class_str)
18884       {
18885         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
18886         s->refcount++;
18887         /* Avoid unnecessarily putting strings that are used less than
18888            twice in the hash table.  */
18889         if (s->refcount
18890             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
18891           {
18892             void ** slot;
18893             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
18894                                              htab_hash_string (s->str),
18895                                              INSERT);
18896             gcc_assert (*slot == NULL);
18897             *slot = s;
18898           }
18899       }
18900 }
18901
18902 /* Remove from the tree DIE any dies that aren't marked.  */
18903
18904 static void
18905 prune_unused_types_prune (dw_die_ref die)
18906 {
18907   dw_die_ref c;
18908
18909   gcc_assert (die->die_mark);
18910   prune_unused_types_update_strings (die);
18911
18912   if (! die->die_child)
18913     return;
18914
18915   c = die->die_child;
18916   do {
18917     dw_die_ref prev = c;
18918     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
18919       if (c == die->die_child)
18920         {
18921           /* No marked children between 'prev' and the end of the list.  */
18922           if (prev == c)
18923             /* No marked children at all.  */
18924             die->die_child = NULL;
18925           else
18926             {
18927               prev->die_sib = c->die_sib;
18928               die->die_child = prev;
18929             }
18930           return;
18931         }
18932
18933     if (c != prev->die_sib)
18934       prev->die_sib = c;
18935     prune_unused_types_prune (c);
18936   } while (c != die->die_child);
18937 }
18938
18939 /* A helper function for dwarf2out_finish called through
18940    htab_traverse.  Clear .debug_str strings that we haven't already
18941    decided to emit.  */
18942
18943 static int
18944 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
18945 {
18946   struct indirect_string_node *node = (struct indirect_string_node *) *h;
18947
18948   if (!node->label || !node->refcount)
18949     htab_clear_slot (debug_str_hash, h);
18950
18951   return 1;
18952 }
18953
18954 /* Remove dies representing declarations that we never use.  */
18955
18956 static void
18957 prune_unused_types (void)
18958 {
18959   unsigned int i;
18960   limbo_die_node *node;
18961   pubname_ref pub;
18962
18963 #if ENABLE_ASSERT_CHECKING
18964   /* All the marks should already be clear.  */
18965   verify_marks_clear (comp_unit_die);
18966   for (node = limbo_die_list; node; node = node->next)
18967     verify_marks_clear (node->die);
18968 #endif /* ENABLE_ASSERT_CHECKING */
18969
18970   /* Mark types that are used in global variables.  */
18971   premark_types_used_by_global_vars ();
18972
18973   /* Set the mark on nodes that are actually used.  */
18974   prune_unused_types_walk (comp_unit_die);
18975   for (node = limbo_die_list; node; node = node->next)
18976     prune_unused_types_walk (node->die);
18977
18978   /* Also set the mark on nodes referenced from the
18979      pubname_table or arange_table.  */
18980   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
18981     prune_unused_types_mark (pub->die, 1);
18982   for (i = 0; i < arange_table_in_use; i++)
18983     prune_unused_types_mark (arange_table[i], 1);
18984
18985   /* Get rid of nodes that aren't marked; and update the string counts.  */
18986   if (debug_str_hash && debug_str_hash_forced)
18987     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
18988   else if (debug_str_hash)
18989     htab_empty (debug_str_hash);
18990   prune_unused_types_prune (comp_unit_die);
18991   for (node = limbo_die_list; node; node = node->next)
18992     prune_unused_types_prune (node->die);
18993
18994   /* Leave the marks clear.  */
18995   prune_unmark_dies (comp_unit_die);
18996   for (node = limbo_die_list; node; node = node->next)
18997     prune_unmark_dies (node->die);
18998 }
18999
19000 /* Set the parameter to true if there are any relative pathnames in
19001    the file table.  */
19002 static int
19003 file_table_relative_p (void ** slot, void *param)
19004 {
19005   bool *p = (bool *) param;
19006   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
19007   if (!IS_ABSOLUTE_PATH (d->filename))
19008     {
19009       *p = true;
19010       return 0;
19011     }
19012   return 1;
19013 }
19014
19015 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
19016    to the location it would have been added, should we know its
19017    DECL_ASSEMBLER_NAME when we added other attributes.  This will
19018    probably improve compactness of debug info, removing equivalent
19019    abbrevs, and hide any differences caused by deferring the
19020    computation of the assembler name, triggered by e.g. PCH.  */
19021
19022 static inline void
19023 move_linkage_attr (dw_die_ref die)
19024 {
19025   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
19026   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
19027
19028   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
19029
19030   while (--ix > 0)
19031     {
19032       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
19033
19034       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
19035         break;
19036     }
19037
19038   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
19039     {
19040       VEC_pop (dw_attr_node, die->die_attr);
19041       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
19042     }
19043 }
19044
19045 /* Helper function for resolve_addr, attempt to resolve
19046    one CONST_STRING, return non-zero if not successful.  Similarly verify that
19047    SYMBOL_REFs refer to variables emitted in the current CU.  */
19048
19049 static int
19050 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
19051 {
19052   rtx rtl = *addr;
19053
19054   if (GET_CODE (rtl) == CONST_STRING)
19055     {
19056       size_t len = strlen (XSTR (rtl, 0)) + 1;
19057       tree t = build_string (len, XSTR (rtl, 0));
19058       tree tlen = build_int_cst (NULL_TREE, len - 1);
19059       TREE_TYPE (t)
19060         = build_array_type (char_type_node, build_index_type (tlen));
19061       rtl = lookup_constant_def (t);
19062       if (!rtl || !MEM_P (rtl))
19063         return 1;
19064       rtl = XEXP (rtl, 0);
19065       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
19066       *addr = rtl;
19067       return 0;
19068     }
19069
19070   if (GET_CODE (rtl) == SYMBOL_REF
19071       && SYMBOL_REF_DECL (rtl)
19072       && TREE_CODE (SYMBOL_REF_DECL (rtl)) == VAR_DECL
19073       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
19074     return 1;
19075
19076   if (GET_CODE (rtl) == CONST
19077       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
19078     return 1;
19079
19080   return 0;
19081 }
19082
19083 /* Helper function for resolve_addr, handle one location
19084    expression, return false if at least one CONST_STRING or SYMBOL_REF in
19085    the location list couldn't be resolved.  */
19086
19087 static bool
19088 resolve_addr_in_expr (dw_loc_descr_ref loc)
19089 {
19090   for (; loc; loc = loc->dw_loc_next)
19091     if ((loc->dw_loc_opc == DW_OP_addr
19092          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
19093         || (loc->dw_loc_opc == DW_OP_implicit_value
19094             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
19095             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
19096       return false;
19097   return true;
19098 }
19099
19100 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
19101    an address in .rodata section if the string literal is emitted there,
19102    or remove the containing location list or replace DW_AT_const_value
19103    with DW_AT_location and empty location expression, if it isn't found
19104    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
19105    to something that has been emitted in the current CU.  */
19106
19107 static void
19108 resolve_addr (dw_die_ref die)
19109 {
19110   dw_die_ref c;
19111   dw_attr_ref a;
19112   dw_loc_list_ref curr;
19113   unsigned ix;
19114
19115   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
19116     switch (AT_class (a))
19117       {
19118       case dw_val_class_loc_list:
19119         for (curr = AT_loc_list (a); curr != NULL; curr = curr->dw_loc_next)
19120           if (!resolve_addr_in_expr (curr->expr))
19121             curr->expr = NULL;
19122         break;
19123       case dw_val_class_loc:
19124         if (!resolve_addr_in_expr (AT_loc (a)))
19125           a->dw_attr_val.v.val_loc = NULL;
19126         break;
19127       case dw_val_class_addr:
19128         if (a->dw_attr == DW_AT_const_value
19129             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
19130           {
19131             a->dw_attr = DW_AT_location;
19132             a->dw_attr_val.val_class = dw_val_class_loc;
19133             a->dw_attr_val.v.val_loc = NULL;
19134           }
19135         break;
19136       default:
19137         break;
19138       }
19139
19140   FOR_EACH_CHILD (die, c, resolve_addr (c));
19141 }
19142
19143 /* Output stuff that dwarf requires at the end of every file,
19144    and generate the DWARF-2 debugging info.  */
19145
19146 static void
19147 dwarf2out_finish (const char *filename)
19148 {
19149   limbo_die_node *node, *next_node;
19150   dw_die_ref die = 0;
19151   unsigned int i;
19152
19153   gen_remaining_tmpl_value_param_die_attribute ();
19154
19155   /* Add the name for the main input file now.  We delayed this from
19156      dwarf2out_init to avoid complications with PCH.  */
19157   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
19158   if (!IS_ABSOLUTE_PATH (filename))
19159     add_comp_dir_attribute (comp_unit_die);
19160   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
19161     {
19162       bool p = false;
19163       htab_traverse (file_table, file_table_relative_p, &p);
19164       if (p)
19165         add_comp_dir_attribute (comp_unit_die);
19166     }
19167
19168   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
19169     {
19170       add_location_or_const_value_attribute (
19171         VEC_index (deferred_locations, deferred_locations_list, i)->die,
19172         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
19173         DW_AT_location);
19174     }
19175
19176   /* Traverse the limbo die list, and add parent/child links.  The only
19177      dies without parents that should be here are concrete instances of
19178      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
19179      For concrete instances, we can get the parent die from the abstract
19180      instance.  */
19181   for (node = limbo_die_list; node; node = next_node)
19182     {
19183       next_node = node->next;
19184       die = node->die;
19185
19186       if (die->die_parent == NULL)
19187         {
19188           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
19189
19190           if (origin)
19191             add_child_die (origin->die_parent, die);
19192           else if (die == comp_unit_die)
19193             ;
19194           else if (errorcount > 0 || sorrycount > 0)
19195             /* It's OK to be confused by errors in the input.  */
19196             add_child_die (comp_unit_die, die);
19197           else
19198             {
19199               /* In certain situations, the lexical block containing a
19200                  nested function can be optimized away, which results
19201                  in the nested function die being orphaned.  Likewise
19202                  with the return type of that nested function.  Force
19203                  this to be a child of the containing function.
19204
19205                  It may happen that even the containing function got fully
19206                  inlined and optimized out.  In that case we are lost and
19207                  assign the empty child.  This should not be big issue as
19208                  the function is likely unreachable too.  */
19209               tree context = NULL_TREE;
19210
19211               gcc_assert (node->created_for);
19212
19213               if (DECL_P (node->created_for))
19214                 context = DECL_CONTEXT (node->created_for);
19215               else if (TYPE_P (node->created_for))
19216                 context = TYPE_CONTEXT (node->created_for);
19217
19218               gcc_assert (context
19219                           && (TREE_CODE (context) == FUNCTION_DECL
19220                               || TREE_CODE (context) == NAMESPACE_DECL));
19221
19222               origin = lookup_decl_die (context);
19223               if (origin)
19224                 add_child_die (origin, die);
19225               else
19226                 add_child_die (comp_unit_die, die);
19227             }
19228         }
19229     }
19230
19231   limbo_die_list = NULL;
19232
19233   resolve_addr (comp_unit_die);
19234
19235   for (node = deferred_asm_name; node; node = node->next)
19236     {
19237       tree decl = node->created_for;
19238       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
19239         {
19240           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
19241                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
19242           move_linkage_attr (node->die);
19243         }
19244     }
19245
19246   deferred_asm_name = NULL;
19247
19248   /* Walk through the list of incomplete types again, trying once more to
19249      emit full debugging info for them.  */
19250   retry_incomplete_types ();
19251
19252   if (flag_eliminate_unused_debug_types)
19253     prune_unused_types ();
19254
19255   /* Generate separate CUs for each of the include files we've seen.
19256      They will go into limbo_die_list.  */
19257   if (flag_eliminate_dwarf2_dups)
19258     break_out_includes (comp_unit_die);
19259
19260   /* Traverse the DIE's and add add sibling attributes to those DIE's
19261      that have children.  */
19262   add_sibling_attributes (comp_unit_die);
19263   for (node = limbo_die_list; node; node = node->next)
19264     add_sibling_attributes (node->die);
19265
19266   /* Output a terminator label for the .text section.  */
19267   switch_to_section (text_section);
19268   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
19269   if (flag_reorder_blocks_and_partition)
19270     {
19271       switch_to_section (unlikely_text_section ());
19272       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
19273     }
19274
19275   /* We can only use the low/high_pc attributes if all of the code was
19276      in .text.  */
19277   if (!have_multiple_function_sections
19278       || !(dwarf_version >= 3 || !dwarf_strict))
19279     {
19280       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
19281       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
19282     }
19283
19284   else
19285     {
19286       unsigned fde_idx = 0;
19287
19288       /* We need to give .debug_loc and .debug_ranges an appropriate
19289          "base address".  Use zero so that these addresses become
19290          absolute.  Historically, we've emitted the unexpected
19291          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
19292          Emit both to give time for other tools to adapt.  */
19293       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
19294       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
19295
19296       add_AT_range_list (comp_unit_die, DW_AT_ranges,
19297                          add_ranges_by_labels (text_section_label,
19298                                                text_end_label));
19299       if (flag_reorder_blocks_and_partition)
19300         add_ranges_by_labels (cold_text_section_label,
19301                               cold_end_label);
19302
19303       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
19304         {
19305           dw_fde_ref fde = &fde_table[fde_idx];
19306
19307           if (fde->dw_fde_switched_sections)
19308             {
19309               if (!fde->in_std_section)
19310                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
19311                                       fde->dw_fde_hot_section_end_label);
19312               if (!fde->cold_in_std_section)
19313                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
19314                                       fde->dw_fde_unlikely_section_end_label);
19315             }
19316           else if (!fde->in_std_section)
19317             add_ranges_by_labels (fde->dw_fde_begin,
19318                                   fde->dw_fde_end);
19319         }
19320
19321       add_ranges (NULL);
19322     }
19323
19324   /* Output location list section if necessary.  */
19325   if (have_location_lists)
19326     {
19327       /* Output the location lists info.  */
19328       switch_to_section (debug_loc_section);
19329       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
19330                                    DEBUG_LOC_SECTION_LABEL, 0);
19331       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
19332       output_location_lists (die);
19333     }
19334
19335   if (debug_info_level >= DINFO_LEVEL_NORMAL)
19336     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
19337                     debug_line_section_label);
19338
19339   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
19340     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
19341
19342   /* Output all of the compilation units.  We put the main one last so that
19343      the offsets are available to output_pubnames.  */
19344   for (node = limbo_die_list; node; node = node->next)
19345     output_comp_unit (node->die, 0);
19346
19347   /* Output the main compilation unit if non-empty or if .debug_macinfo
19348      has been emitted.  */
19349   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
19350
19351   /* Output the abbreviation table.  */
19352   switch_to_section (debug_abbrev_section);
19353   output_abbrev_section ();
19354
19355   /* Output public names table if necessary.  */
19356   if (!VEC_empty (pubname_entry, pubname_table))
19357     {
19358       switch_to_section (debug_pubnames_section);
19359       output_pubnames (pubname_table);
19360     }
19361
19362   /* Output public types table if necessary.  */
19363   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
19364      It shouldn't hurt to emit it always, since pure DWARF2 consumers
19365      simply won't look for the section.  */
19366   if (!VEC_empty (pubname_entry, pubtype_table))
19367     {
19368       switch_to_section (debug_pubtypes_section);
19369       output_pubnames (pubtype_table);
19370     }
19371
19372   /* Output the address range information.  We only put functions in the arange
19373      table, so don't write it out if we don't have any.  */
19374   if (fde_table_in_use)
19375     {
19376       switch_to_section (debug_aranges_section);
19377       output_aranges ();
19378     }
19379
19380   /* Output ranges section if necessary.  */
19381   if (ranges_table_in_use)
19382     {
19383       switch_to_section (debug_ranges_section);
19384       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
19385       output_ranges ();
19386     }
19387
19388   /* Output the source line correspondence table.  We must do this
19389      even if there is no line information.  Otherwise, on an empty
19390      translation unit, we will generate a present, but empty,
19391      .debug_info section.  IRIX 6.5 `nm' will then complain when
19392      examining the file.  This is done late so that any filenames
19393      used by the debug_info section are marked as 'used'.  */
19394   if (! DWARF2_ASM_LINE_DEBUG_INFO)
19395     {
19396       switch_to_section (debug_line_section);
19397       output_line_info ();
19398     }
19399
19400   /* Have to end the macro section.  */
19401   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
19402     {
19403       switch_to_section (debug_macinfo_section);
19404       dw2_asm_output_data (1, 0, "End compilation unit");
19405     }
19406
19407   /* If we emitted any DW_FORM_strp form attribute, output the string
19408      table too.  */
19409   if (debug_str_hash)
19410     htab_traverse (debug_str_hash, output_indirect_string, NULL);
19411 }
19412 #else
19413
19414 /* This should never be used, but its address is needed for comparisons.  */
19415 const struct gcc_debug_hooks dwarf2_debug_hooks =
19416 {
19417   0,            /* init */
19418   0,            /* finish */
19419   0,            /* define */
19420   0,            /* undef */
19421   0,            /* start_source_file */
19422   0,            /* end_source_file */
19423   0,            /* begin_block */
19424   0,            /* end_block */
19425   0,            /* ignore_block */
19426   0,            /* source_line */
19427   0,            /* begin_prologue */
19428   0,            /* end_prologue */
19429   0,            /* end_epilogue */
19430   0,            /* begin_function */
19431   0,            /* end_function */
19432   0,            /* function_decl */
19433   0,            /* global_decl */
19434   0,            /* type_decl */
19435   0,            /* imported_module_or_decl */
19436   0,            /* deferred_inline_function */
19437   0,            /* outlining_inline_function */
19438   0,            /* label */
19439   0,            /* handle_pch */
19440   0,            /* var_location */
19441   0,            /* switch_text_section */
19442   0,            /* set_name */
19443   0             /* start_end_main_source_file */
19444 };
19445
19446 #endif /* DWARF2_DEBUGGING_INFO */
19447
19448 #include "gt-dwarf2out.h"