OSDN Git Service

* dwarf2out.c (modified_type_die): Don't add DW_AT_name to
[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 /* The size in bytes of a DWARF 4 type signature.  */
352
353 #ifndef DWARF_TYPE_SIGNATURE_SIZE
354 #define DWARF_TYPE_SIGNATURE_SIZE 8
355 #endif
356
357 /* According to the (draft) DWARF 3 specification, the initial length
358    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
359    bytes are 0xffffffff, followed by the length stored in the next 8
360    bytes.
361
362    However, the SGI/MIPS ABI uses an initial length which is equal to
363    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
364
365 #ifndef DWARF_INITIAL_LENGTH_SIZE
366 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
367 #endif
368
369 /* Round SIZE up to the nearest BOUNDARY.  */
370 #define DWARF_ROUND(SIZE,BOUNDARY) \
371   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
372
373 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
374 #ifndef DWARF_CIE_DATA_ALIGNMENT
375 #ifdef STACK_GROWS_DOWNWARD
376 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
377 #else
378 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
379 #endif
380 #endif
381
382 /* CIE identifier.  */
383 #if HOST_BITS_PER_WIDE_INT >= 64
384 #define DWARF_CIE_ID \
385   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
386 #else
387 #define DWARF_CIE_ID DW_CIE_ID
388 #endif
389
390 /* A pointer to the base of a table that contains frame description
391    information for each routine.  */
392 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
393
394 /* Number of elements currently allocated for fde_table.  */
395 static GTY(()) unsigned fde_table_allocated;
396
397 /* Number of elements in fde_table currently in use.  */
398 static GTY(()) unsigned fde_table_in_use;
399
400 /* Size (in elements) of increments by which we may expand the
401    fde_table.  */
402 #define FDE_TABLE_INCREMENT 256
403
404 /* Get the current fde_table entry we should use.  */
405
406 static inline dw_fde_ref
407 current_fde (void)
408 {
409   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
410 }
411
412 /* A list of call frame insns for the CIE.  */
413 static GTY(()) dw_cfi_ref cie_cfi_head;
414
415 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
416 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
417    attribute that accelerates the lookup of the FDE associated
418    with the subprogram.  This variable holds the table index of the FDE
419    associated with the current function (body) definition.  */
420 static unsigned current_funcdef_fde;
421 #endif
422
423 struct GTY(()) indirect_string_node {
424   const char *str;
425   unsigned int refcount;
426   enum dwarf_form form;
427   char *label;
428 };
429
430 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
431
432 /* True if the compilation unit has location entries that reference
433    debug strings.  */
434 static GTY(()) bool debug_str_hash_forced = false;
435
436 static GTY(()) int dw2_string_counter;
437 static GTY(()) unsigned long dwarf2out_cfi_label_num;
438
439 /* True if the compilation unit places functions in more than one section.  */
440 static GTY(()) bool have_multiple_function_sections = false;
441
442 /* Whether the default text and cold text sections have been used at all.  */
443
444 static GTY(()) bool text_section_used = false;
445 static GTY(()) bool cold_text_section_used = false;
446
447 /* The default cold text section.  */
448 static GTY(()) section *cold_text_section;
449
450 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
451
452 /* Forward declarations for functions defined in this file.  */
453
454 static char *stripattributes (const char *);
455 static const char *dwarf_cfi_name (unsigned);
456 static dw_cfi_ref new_cfi (void);
457 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
458 static void add_fde_cfi (const char *, dw_cfi_ref);
459 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
460 static void lookup_cfa (dw_cfa_location *);
461 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
462 #ifdef DWARF2_UNWIND_INFO
463 static void initial_return_save (rtx);
464 #endif
465 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
466                                           HOST_WIDE_INT);
467 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
468 static void output_cfi_directive (dw_cfi_ref);
469 static void output_call_frame_info (int);
470 static void dwarf2out_note_section_used (void);
471 static void dwarf2out_stack_adjust (rtx, bool);
472 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
473 static void flush_queued_reg_saves (void);
474 static bool clobbers_queued_reg_save (const_rtx);
475 static void dwarf2out_frame_debug_expr (rtx, const char *);
476
477 /* Support for complex CFA locations.  */
478 static void output_cfa_loc (dw_cfi_ref);
479 static void output_cfa_loc_raw (dw_cfi_ref);
480 static void get_cfa_from_loc_descr (dw_cfa_location *,
481                                     struct dw_loc_descr_struct *);
482 static struct dw_loc_descr_struct *build_cfa_loc
483   (dw_cfa_location *, HOST_WIDE_INT);
484 static struct dw_loc_descr_struct *build_cfa_aligned_loc
485   (HOST_WIDE_INT, HOST_WIDE_INT);
486 static void def_cfa_1 (const char *, dw_cfa_location *);
487
488 /* How to start an assembler comment.  */
489 #ifndef ASM_COMMENT_START
490 #define ASM_COMMENT_START ";#"
491 #endif
492
493 /* Data and reference forms for relocatable data.  */
494 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
495 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
496
497 #ifndef DEBUG_FRAME_SECTION
498 #define DEBUG_FRAME_SECTION     ".debug_frame"
499 #endif
500
501 #ifndef FUNC_BEGIN_LABEL
502 #define FUNC_BEGIN_LABEL        "LFB"
503 #endif
504
505 #ifndef FUNC_END_LABEL
506 #define FUNC_END_LABEL          "LFE"
507 #endif
508
509 #ifndef FRAME_BEGIN_LABEL
510 #define FRAME_BEGIN_LABEL       "Lframe"
511 #endif
512 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
513 #define CIE_END_LABEL           "LECIE"
514 #define FDE_LABEL               "LSFDE"
515 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
516 #define FDE_END_LABEL           "LEFDE"
517 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
518 #define LINE_NUMBER_END_LABEL   "LELT"
519 #define LN_PROLOG_AS_LABEL      "LASLTP"
520 #define LN_PROLOG_END_LABEL     "LELTP"
521 #define DIE_LABEL_PREFIX        "DW"
522
523 /* The DWARF 2 CFA column which tracks the return address.  Normally this
524    is the column for PC, or the first column after all of the hard
525    registers.  */
526 #ifndef DWARF_FRAME_RETURN_COLUMN
527 #ifdef PC_REGNUM
528 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
529 #else
530 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
531 #endif
532 #endif
533
534 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
535    default, we just provide columns for all registers.  */
536 #ifndef DWARF_FRAME_REGNUM
537 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
538 #endif
539 \f
540 /* Hook used by __throw.  */
541
542 rtx
543 expand_builtin_dwarf_sp_column (void)
544 {
545   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
546   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
547 }
548
549 /* Return a pointer to a copy of the section string name S with all
550    attributes stripped off, and an asterisk prepended (for assemble_name).  */
551
552 static inline char *
553 stripattributes (const char *s)
554 {
555   char *stripped = XNEWVEC (char, strlen (s) + 2);
556   char *p = stripped;
557
558   *p++ = '*';
559
560   while (*s && *s != ',')
561     *p++ = *s++;
562
563   *p = '\0';
564   return stripped;
565 }
566
567 /* MEM is a memory reference for the register size table, each element of
568    which has mode MODE.  Initialize column C as a return address column.  */
569
570 static void
571 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
572 {
573   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
574   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
575   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
576 }
577
578 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
579
580 static inline HOST_WIDE_INT
581 div_data_align (HOST_WIDE_INT off)
582 {
583   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
584   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
585   return r;
586 }
587
588 /* Return true if we need a signed version of a given opcode
589    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
590
591 static inline bool
592 need_data_align_sf_opcode (HOST_WIDE_INT off)
593 {
594   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
595 }
596
597 /* Generate code to initialize the register size table.  */
598
599 void
600 expand_builtin_init_dwarf_reg_sizes (tree address)
601 {
602   unsigned int i;
603   enum machine_mode mode = TYPE_MODE (char_type_node);
604   rtx addr = expand_normal (address);
605   rtx mem = gen_rtx_MEM (BLKmode, addr);
606   bool wrote_return_column = false;
607
608   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
609     {
610       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
611
612       if (rnum < DWARF_FRAME_REGISTERS)
613         {
614           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
615           enum machine_mode save_mode = reg_raw_mode[i];
616           HOST_WIDE_INT size;
617
618           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
619             save_mode = choose_hard_reg_mode (i, 1, true);
620           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
621             {
622               if (save_mode == VOIDmode)
623                 continue;
624               wrote_return_column = true;
625             }
626           size = GET_MODE_SIZE (save_mode);
627           if (offset < 0)
628             continue;
629
630           emit_move_insn (adjust_address (mem, mode, offset),
631                           gen_int_mode (size, mode));
632         }
633     }
634
635   if (!wrote_return_column)
636     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
637
638 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
639   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
640 #endif
641
642   targetm.init_dwarf_reg_sizes_extra (address);
643 }
644
645 /* Convert a DWARF call frame info. operation to its string name */
646
647 static const char *
648 dwarf_cfi_name (unsigned int cfi_opc)
649 {
650   switch (cfi_opc)
651     {
652     case DW_CFA_advance_loc:
653       return "DW_CFA_advance_loc";
654     case DW_CFA_offset:
655       return "DW_CFA_offset";
656     case DW_CFA_restore:
657       return "DW_CFA_restore";
658     case DW_CFA_nop:
659       return "DW_CFA_nop";
660     case DW_CFA_set_loc:
661       return "DW_CFA_set_loc";
662     case DW_CFA_advance_loc1:
663       return "DW_CFA_advance_loc1";
664     case DW_CFA_advance_loc2:
665       return "DW_CFA_advance_loc2";
666     case DW_CFA_advance_loc4:
667       return "DW_CFA_advance_loc4";
668     case DW_CFA_offset_extended:
669       return "DW_CFA_offset_extended";
670     case DW_CFA_restore_extended:
671       return "DW_CFA_restore_extended";
672     case DW_CFA_undefined:
673       return "DW_CFA_undefined";
674     case DW_CFA_same_value:
675       return "DW_CFA_same_value";
676     case DW_CFA_register:
677       return "DW_CFA_register";
678     case DW_CFA_remember_state:
679       return "DW_CFA_remember_state";
680     case DW_CFA_restore_state:
681       return "DW_CFA_restore_state";
682     case DW_CFA_def_cfa:
683       return "DW_CFA_def_cfa";
684     case DW_CFA_def_cfa_register:
685       return "DW_CFA_def_cfa_register";
686     case DW_CFA_def_cfa_offset:
687       return "DW_CFA_def_cfa_offset";
688
689     /* DWARF 3 */
690     case DW_CFA_def_cfa_expression:
691       return "DW_CFA_def_cfa_expression";
692     case DW_CFA_expression:
693       return "DW_CFA_expression";
694     case DW_CFA_offset_extended_sf:
695       return "DW_CFA_offset_extended_sf";
696     case DW_CFA_def_cfa_sf:
697       return "DW_CFA_def_cfa_sf";
698     case DW_CFA_def_cfa_offset_sf:
699       return "DW_CFA_def_cfa_offset_sf";
700
701     /* SGI/MIPS specific */
702     case DW_CFA_MIPS_advance_loc8:
703       return "DW_CFA_MIPS_advance_loc8";
704
705     /* GNU extensions */
706     case DW_CFA_GNU_window_save:
707       return "DW_CFA_GNU_window_save";
708     case DW_CFA_GNU_args_size:
709       return "DW_CFA_GNU_args_size";
710     case DW_CFA_GNU_negative_offset_extended:
711       return "DW_CFA_GNU_negative_offset_extended";
712
713     default:
714       return "DW_CFA_<unknown>";
715     }
716 }
717
718 /* Return a pointer to a newly allocated Call Frame Instruction.  */
719
720 static inline dw_cfi_ref
721 new_cfi (void)
722 {
723   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
724
725   cfi->dw_cfi_next = NULL;
726   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
727   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
728
729   return cfi;
730 }
731
732 /* Add a Call Frame Instruction to list of instructions.  */
733
734 static inline void
735 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
736 {
737   dw_cfi_ref *p;
738   dw_fde_ref fde = current_fde ();
739
740   /* When DRAP is used, CFA is defined with an expression.  Redefine
741      CFA may lead to a different CFA value.   */
742   /* ??? Of course, this heuristic fails when we're annotating epilogues,
743      because of course we'll always want to redefine the CFA back to the
744      stack pointer on the way out.  Where should we move this check?  */
745   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
746     switch (cfi->dw_cfi_opc)
747       {
748         case DW_CFA_def_cfa_register:
749         case DW_CFA_def_cfa_offset:
750         case DW_CFA_def_cfa_offset_sf:
751         case DW_CFA_def_cfa:
752         case DW_CFA_def_cfa_sf:
753           gcc_unreachable ();
754
755         default:
756           break;
757       }
758
759   /* Find the end of the chain.  */
760   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
761     ;
762
763   *p = cfi;
764 }
765
766 /* Generate a new label for the CFI info to refer to.  FORCE is true
767    if a label needs to be output even when using .cfi_* directives.  */
768
769 char *
770 dwarf2out_cfi_label (bool force)
771 {
772   static char label[20];
773
774   if (!force && dwarf2out_do_cfi_asm ())
775     {
776       /* In this case, we will be emitting the asm directive instead of
777          the label, so just return a placeholder to keep the rest of the
778          interfaces happy.  */
779       strcpy (label, "<do not output>");
780     }
781   else
782     {
783       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
784       ASM_OUTPUT_LABEL (asm_out_file, label);
785     }
786
787   return label;
788 }
789
790 /* True if remember_state should be emitted before following CFI directive.  */
791 static bool emit_cfa_remember;
792
793 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
794    or to the CIE if LABEL is NULL.  */
795
796 static void
797 add_fde_cfi (const char *label, dw_cfi_ref cfi)
798 {
799   dw_cfi_ref *list_head;
800
801   if (emit_cfa_remember)
802     {
803       dw_cfi_ref cfi_remember;
804
805       /* Emit the state save.  */
806       emit_cfa_remember = false;
807       cfi_remember = new_cfi (); 
808       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
809       add_fde_cfi (label, cfi_remember);
810     }
811
812   list_head = &cie_cfi_head;
813
814   if (dwarf2out_do_cfi_asm ())
815     {
816       if (label)
817         {
818           dw_fde_ref fde = current_fde ();
819
820           gcc_assert (fde != NULL);
821
822           /* We still have to add the cfi to the list so that lookup_cfa
823              works later on.  When -g2 and above we even need to force
824              emitting of CFI labels and add to list a DW_CFA_set_loc for
825              convert_cfa_to_fb_loc_list purposes.  If we're generating
826              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
827              convert_cfa_to_fb_loc_list.  */
828           if (dwarf_version == 2
829               && debug_info_level > DINFO_LEVEL_TERSE
830               && (write_symbols == DWARF2_DEBUG
831                   || write_symbols == VMS_AND_DWARF2_DEBUG))
832             {
833               switch (cfi->dw_cfi_opc)
834                 {
835                 case DW_CFA_def_cfa_offset:
836                 case DW_CFA_def_cfa_offset_sf:
837                 case DW_CFA_def_cfa_register:
838                 case DW_CFA_def_cfa:
839                 case DW_CFA_def_cfa_sf:
840                 case DW_CFA_def_cfa_expression:
841                 case DW_CFA_restore_state:
842                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
843                     label = dwarf2out_cfi_label (true);
844
845                   if (fde->dw_fde_current_label == NULL
846                       || strcmp (label, fde->dw_fde_current_label) != 0)
847                     {
848                       dw_cfi_ref xcfi;
849
850                       label = xstrdup (label);
851
852                       /* Set the location counter to the new label.  */
853                       xcfi = new_cfi ();
854                       /* It doesn't metter whether DW_CFA_set_loc
855                          or DW_CFA_advance_loc4 is added here, those aren't
856                          emitted into assembly, only looked up by
857                          convert_cfa_to_fb_loc_list.  */
858                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
859                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
860                       add_cfi (&fde->dw_fde_cfi, xcfi);
861                       fde->dw_fde_current_label = label;
862                     }
863                   break;
864                 default:
865                   break;
866                 }
867             }
868
869           output_cfi_directive (cfi);
870
871           list_head = &fde->dw_fde_cfi;
872         }
873       /* ??? If this is a CFI for the CIE, we don't emit.  This
874          assumes that the standard CIE contents that the assembler
875          uses matches the standard CIE contents that the compiler
876          uses.  This is probably a bad assumption.  I'm not quite
877          sure how to address this for now.  */
878     }
879   else if (label)
880     {
881       dw_fde_ref fde = current_fde ();
882
883       gcc_assert (fde != NULL);
884
885       if (*label == 0)
886         label = dwarf2out_cfi_label (false);
887
888       if (fde->dw_fde_current_label == NULL
889           || strcmp (label, fde->dw_fde_current_label) != 0)
890         {
891           dw_cfi_ref xcfi;
892
893           label = xstrdup (label);
894
895           /* Set the location counter to the new label.  */
896           xcfi = new_cfi ();
897           /* If we have a current label, advance from there, otherwise
898              set the location directly using set_loc.  */
899           xcfi->dw_cfi_opc = fde->dw_fde_current_label
900                              ? DW_CFA_advance_loc4
901                              : DW_CFA_set_loc;
902           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
903           add_cfi (&fde->dw_fde_cfi, xcfi);
904
905           fde->dw_fde_current_label = label;
906         }
907
908       list_head = &fde->dw_fde_cfi;
909     }
910
911   add_cfi (list_head, cfi);
912 }
913
914 /* Subroutine of lookup_cfa.  */
915
916 static void
917 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
918 {
919   switch (cfi->dw_cfi_opc)
920     {
921     case DW_CFA_def_cfa_offset:
922     case DW_CFA_def_cfa_offset_sf:
923       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
924       break;
925     case DW_CFA_def_cfa_register:
926       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
927       break;
928     case DW_CFA_def_cfa:
929     case DW_CFA_def_cfa_sf:
930       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
931       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
932       break;
933     case DW_CFA_def_cfa_expression:
934       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
935       break;
936
937     case DW_CFA_remember_state:
938       gcc_assert (!remember->in_use);
939       *remember = *loc;
940       remember->in_use = 1;
941       break;
942     case DW_CFA_restore_state:
943       gcc_assert (remember->in_use);
944       *loc = *remember;
945       remember->in_use = 0;
946       break;
947
948     default:
949       break;
950     }
951 }
952
953 /* Find the previous value for the CFA.  */
954
955 static void
956 lookup_cfa (dw_cfa_location *loc)
957 {
958   dw_cfi_ref cfi;
959   dw_fde_ref fde;
960   dw_cfa_location remember;
961
962   memset (loc, 0, sizeof (*loc));
963   loc->reg = INVALID_REGNUM;
964   remember = *loc;
965
966   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
967     lookup_cfa_1 (cfi, loc, &remember);
968
969   fde = current_fde ();
970   if (fde)
971     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
972       lookup_cfa_1 (cfi, loc, &remember);
973 }
974
975 /* The current rule for calculating the DWARF2 canonical frame address.  */
976 static dw_cfa_location cfa;
977
978 /* The register used for saving registers to the stack, and its offset
979    from the CFA.  */
980 static dw_cfa_location cfa_store;
981
982 /* The current save location around an epilogue.  */
983 static dw_cfa_location cfa_remember;
984
985 /* The running total of the size of arguments pushed onto the stack.  */
986 static HOST_WIDE_INT args_size;
987
988 /* The last args_size we actually output.  */
989 static HOST_WIDE_INT old_args_size;
990
991 /* Entry point to update the canonical frame address (CFA).
992    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
993    calculated from REG+OFFSET.  */
994
995 void
996 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
997 {
998   dw_cfa_location loc;
999   loc.indirect = 0;
1000   loc.base_offset = 0;
1001   loc.reg = reg;
1002   loc.offset = offset;
1003   def_cfa_1 (label, &loc);
1004 }
1005
1006 /* Determine if two dw_cfa_location structures define the same data.  */
1007
1008 static bool
1009 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1010 {
1011   return (loc1->reg == loc2->reg
1012           && loc1->offset == loc2->offset
1013           && loc1->indirect == loc2->indirect
1014           && (loc1->indirect == 0
1015               || loc1->base_offset == loc2->base_offset));
1016 }
1017
1018 /* This routine does the actual work.  The CFA is now calculated from
1019    the dw_cfa_location structure.  */
1020
1021 static void
1022 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1023 {
1024   dw_cfi_ref cfi;
1025   dw_cfa_location old_cfa, loc;
1026
1027   cfa = *loc_p;
1028   loc = *loc_p;
1029
1030   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1031     cfa_store.offset = loc.offset;
1032
1033   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1034   lookup_cfa (&old_cfa);
1035
1036   /* If nothing changed, no need to issue any call frame instructions.  */
1037   if (cfa_equal_p (&loc, &old_cfa))
1038     return;
1039
1040   cfi = new_cfi ();
1041
1042   if (loc.reg == old_cfa.reg && !loc.indirect)
1043     {
1044       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1045          the CFA register did not change but the offset did.  The data 
1046          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1047          in the assembler via the .cfi_def_cfa_offset directive.  */
1048       if (loc.offset < 0)
1049         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1050       else
1051         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1052       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1053     }
1054
1055 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1056   else if (loc.offset == old_cfa.offset
1057            && old_cfa.reg != INVALID_REGNUM
1058            && !loc.indirect)
1059     {
1060       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1061          indicating the CFA register has changed to <register> but the
1062          offset has not changed.  */
1063       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1064       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1065     }
1066 #endif
1067
1068   else if (loc.indirect == 0)
1069     {
1070       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1071          indicating the CFA register has changed to <register> with
1072          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1073          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1074          directive.  */
1075       if (loc.offset < 0)
1076         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1077       else
1078         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1079       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1080       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1081     }
1082   else
1083     {
1084       /* Construct a DW_CFA_def_cfa_expression instruction to
1085          calculate the CFA using a full location expression since no
1086          register-offset pair is available.  */
1087       struct dw_loc_descr_struct *loc_list;
1088
1089       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1090       loc_list = build_cfa_loc (&loc, 0);
1091       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1092     }
1093
1094   add_fde_cfi (label, cfi);
1095 }
1096
1097 /* Add the CFI for saving a register.  REG is the CFA column number.
1098    LABEL is passed to add_fde_cfi.
1099    If SREG is -1, the register is saved at OFFSET from the CFA;
1100    otherwise it is saved in SREG.  */
1101
1102 static void
1103 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1104 {
1105   dw_cfi_ref cfi = new_cfi ();
1106   dw_fde_ref fde = current_fde ();
1107
1108   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1109
1110   /* When stack is aligned, store REG using DW_CFA_expression with
1111      FP.  */
1112   if (fde
1113       && fde->stack_realign
1114       && sreg == INVALID_REGNUM)
1115     {
1116       cfi->dw_cfi_opc = DW_CFA_expression;
1117       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1118       cfi->dw_cfi_oprnd1.dw_cfi_loc
1119         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1120     }
1121   else if (sreg == INVALID_REGNUM)
1122     {
1123       if (need_data_align_sf_opcode (offset))
1124         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1125       else if (reg & ~0x3f)
1126         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1127       else
1128         cfi->dw_cfi_opc = DW_CFA_offset;
1129       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1130     }
1131   else if (sreg == reg)
1132     cfi->dw_cfi_opc = DW_CFA_same_value;
1133   else
1134     {
1135       cfi->dw_cfi_opc = DW_CFA_register;
1136       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1137     }
1138
1139   add_fde_cfi (label, cfi);
1140 }
1141
1142 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1143    This CFI tells the unwinder that it needs to restore the window registers
1144    from the previous frame's window save area.
1145
1146    ??? Perhaps we should note in the CIE where windows are saved (instead of
1147    assuming 0(cfa)) and what registers are in the window.  */
1148
1149 void
1150 dwarf2out_window_save (const char *label)
1151 {
1152   dw_cfi_ref cfi = new_cfi ();
1153
1154   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1155   add_fde_cfi (label, cfi);
1156 }
1157
1158 /* Add a CFI to update the running total of the size of arguments
1159    pushed onto the stack.  */
1160
1161 void
1162 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1163 {
1164   dw_cfi_ref cfi;
1165
1166   if (size == old_args_size)
1167     return;
1168
1169   old_args_size = size;
1170
1171   cfi = new_cfi ();
1172   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1173   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1174   add_fde_cfi (label, cfi);
1175 }
1176
1177 /* Entry point for saving a register to the stack.  REG is the GCC register
1178    number.  LABEL and OFFSET are passed to reg_save.  */
1179
1180 void
1181 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1182 {
1183   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1184 }
1185
1186 /* Entry point for saving the return address in the stack.
1187    LABEL and OFFSET are passed to reg_save.  */
1188
1189 void
1190 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1191 {
1192   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1193 }
1194
1195 /* Entry point for saving the return address in a register.
1196    LABEL and SREG are passed to reg_save.  */
1197
1198 void
1199 dwarf2out_return_reg (const char *label, unsigned int sreg)
1200 {
1201   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1202 }
1203
1204 #ifdef DWARF2_UNWIND_INFO
1205 /* Record the initial position of the return address.  RTL is
1206    INCOMING_RETURN_ADDR_RTX.  */
1207
1208 static void
1209 initial_return_save (rtx rtl)
1210 {
1211   unsigned int reg = INVALID_REGNUM;
1212   HOST_WIDE_INT offset = 0;
1213
1214   switch (GET_CODE (rtl))
1215     {
1216     case REG:
1217       /* RA is in a register.  */
1218       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1219       break;
1220
1221     case MEM:
1222       /* RA is on the stack.  */
1223       rtl = XEXP (rtl, 0);
1224       switch (GET_CODE (rtl))
1225         {
1226         case REG:
1227           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1228           offset = 0;
1229           break;
1230
1231         case PLUS:
1232           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1233           offset = INTVAL (XEXP (rtl, 1));
1234           break;
1235
1236         case MINUS:
1237           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1238           offset = -INTVAL (XEXP (rtl, 1));
1239           break;
1240
1241         default:
1242           gcc_unreachable ();
1243         }
1244
1245       break;
1246
1247     case PLUS:
1248       /* The return address is at some offset from any value we can
1249          actually load.  For instance, on the SPARC it is in %i7+8. Just
1250          ignore the offset for now; it doesn't matter for unwinding frames.  */
1251       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1252       initial_return_save (XEXP (rtl, 0));
1253       return;
1254
1255     default:
1256       gcc_unreachable ();
1257     }
1258
1259   if (reg != DWARF_FRAME_RETURN_COLUMN)
1260     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1261 }
1262 #endif
1263
1264 /* Given a SET, calculate the amount of stack adjustment it
1265    contains.  */
1266
1267 static HOST_WIDE_INT
1268 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1269                      HOST_WIDE_INT cur_offset)
1270 {
1271   const_rtx src = SET_SRC (pattern);
1272   const_rtx dest = SET_DEST (pattern);
1273   HOST_WIDE_INT offset = 0;
1274   enum rtx_code code;
1275
1276   if (dest == stack_pointer_rtx)
1277     {
1278       code = GET_CODE (src);
1279
1280       /* Assume (set (reg sp) (reg whatever)) sets args_size
1281          level to 0.  */
1282       if (code == REG && src != stack_pointer_rtx)
1283         {
1284           offset = -cur_args_size;
1285 #ifndef STACK_GROWS_DOWNWARD
1286           offset = -offset;
1287 #endif
1288           return offset - cur_offset;
1289         }
1290
1291       if (! (code == PLUS || code == MINUS)
1292           || XEXP (src, 0) != stack_pointer_rtx
1293           || !CONST_INT_P (XEXP (src, 1)))
1294         return 0;
1295
1296       /* (set (reg sp) (plus (reg sp) (const_int))) */
1297       offset = INTVAL (XEXP (src, 1));
1298       if (code == PLUS)
1299         offset = -offset;
1300       return offset;
1301     }
1302
1303   if (MEM_P (src) && !MEM_P (dest))
1304     dest = src;
1305   if (MEM_P (dest))
1306     {
1307       /* (set (mem (pre_dec (reg sp))) (foo)) */
1308       src = XEXP (dest, 0);
1309       code = GET_CODE (src);
1310
1311       switch (code)
1312         {
1313         case PRE_MODIFY:
1314         case POST_MODIFY:
1315           if (XEXP (src, 0) == stack_pointer_rtx)
1316             {
1317               rtx val = XEXP (XEXP (src, 1), 1);
1318               /* We handle only adjustments by constant amount.  */
1319               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1320                           && CONST_INT_P (val));
1321               offset = -INTVAL (val);
1322               break;
1323             }
1324           return 0;
1325
1326         case PRE_DEC:
1327         case POST_DEC:
1328           if (XEXP (src, 0) == stack_pointer_rtx)
1329             {
1330               offset = GET_MODE_SIZE (GET_MODE (dest));
1331               break;
1332             }
1333           return 0;
1334
1335         case PRE_INC:
1336         case POST_INC:
1337           if (XEXP (src, 0) == stack_pointer_rtx)
1338             {
1339               offset = -GET_MODE_SIZE (GET_MODE (dest));
1340               break;
1341             }
1342           return 0;
1343
1344         default:
1345           return 0;
1346         }
1347     }
1348   else
1349     return 0;
1350
1351   return offset;
1352 }
1353
1354 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1355    indexed by INSN_UID.  */
1356
1357 static HOST_WIDE_INT *barrier_args_size;
1358
1359 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1360
1361 static HOST_WIDE_INT
1362 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1363                              VEC (rtx, heap) **next)
1364 {
1365   HOST_WIDE_INT offset = 0;
1366   int i;
1367
1368   if (! RTX_FRAME_RELATED_P (insn))
1369     {
1370       if (prologue_epilogue_contains (insn))
1371         /* Nothing */;
1372       else if (GET_CODE (PATTERN (insn)) == SET)
1373         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1374       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1375                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1376         {
1377           /* There may be stack adjustments inside compound insns.  Search
1378              for them.  */
1379           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1380             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1381               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1382                                              cur_args_size, offset);
1383         }
1384     }
1385   else
1386     {
1387       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1388
1389       if (expr)
1390         {
1391           expr = XEXP (expr, 0);
1392           if (GET_CODE (expr) == PARALLEL
1393               || GET_CODE (expr) == SEQUENCE)
1394             for (i = 1; i < XVECLEN (expr, 0); i++)
1395               {
1396                 rtx elem = XVECEXP (expr, 0, i);
1397
1398                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1399                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1400               }
1401         }
1402     }
1403
1404 #ifndef STACK_GROWS_DOWNWARD
1405   offset = -offset;
1406 #endif
1407
1408   cur_args_size += offset;
1409   if (cur_args_size < 0)
1410     cur_args_size = 0;
1411
1412   if (JUMP_P (insn))
1413     {
1414       rtx dest = JUMP_LABEL (insn);
1415
1416       if (dest)
1417         {
1418           if (barrier_args_size [INSN_UID (dest)] < 0)
1419             {
1420               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1421               VEC_safe_push (rtx, heap, *next, dest);
1422             }
1423         }
1424     }
1425
1426   return cur_args_size;
1427 }
1428
1429 /* Walk the whole function and compute args_size on BARRIERs.  */
1430
1431 static void
1432 compute_barrier_args_size (void)
1433 {
1434   int max_uid = get_max_uid (), i;
1435   rtx insn;
1436   VEC (rtx, heap) *worklist, *next, *tmp;
1437
1438   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1439   for (i = 0; i < max_uid; i++)
1440     barrier_args_size[i] = -1;
1441
1442   worklist = VEC_alloc (rtx, heap, 20);
1443   next = VEC_alloc (rtx, heap, 20);
1444   insn = get_insns ();
1445   barrier_args_size[INSN_UID (insn)] = 0;
1446   VEC_quick_push (rtx, worklist, insn);
1447   for (;;)
1448     {
1449       while (!VEC_empty (rtx, worklist))
1450         {
1451           rtx prev, body, first_insn;
1452           HOST_WIDE_INT cur_args_size;
1453
1454           first_insn = insn = VEC_pop (rtx, worklist);
1455           cur_args_size = barrier_args_size[INSN_UID (insn)];
1456           prev = prev_nonnote_insn (insn);
1457           if (prev && BARRIER_P (prev))
1458             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1459
1460           for (; insn; insn = NEXT_INSN (insn))
1461             {
1462               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1463                 continue;
1464               if (BARRIER_P (insn))
1465                 break;
1466
1467               if (LABEL_P (insn))
1468                 {
1469                   if (insn == first_insn)
1470                     continue;
1471                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1472                     {
1473                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1474                       continue;
1475                     }
1476                   else
1477                     {
1478                       /* The insns starting with this label have been
1479                          already scanned or are in the worklist.  */
1480                       break;
1481                     }
1482                 }
1483
1484               body = PATTERN (insn);
1485               if (GET_CODE (body) == SEQUENCE)
1486                 {
1487                   HOST_WIDE_INT dest_args_size = cur_args_size;
1488                   for (i = 1; i < XVECLEN (body, 0); i++)
1489                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1490                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1491                       dest_args_size
1492                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1493                                                        dest_args_size, &next);
1494                     else
1495                       cur_args_size
1496                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1497                                                        cur_args_size, &next);
1498
1499                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1500                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1501                                                  dest_args_size, &next);
1502                   else
1503                     cur_args_size
1504                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1505                                                      cur_args_size, &next);
1506                 }
1507               else
1508                 cur_args_size
1509                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1510             }
1511         }
1512
1513       if (VEC_empty (rtx, next))
1514         break;
1515
1516       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1517       tmp = next;
1518       next = worklist;
1519       worklist = tmp;
1520       VEC_truncate (rtx, next, 0);
1521     }
1522
1523   VEC_free (rtx, heap, worklist);
1524   VEC_free (rtx, heap, next);
1525 }
1526
1527
1528 /* Check INSN to see if it looks like a push or a stack adjustment, and
1529    make a note of it if it does.  EH uses this information to find out how
1530    much extra space it needs to pop off the stack.  */
1531
1532 static void
1533 dwarf2out_stack_adjust (rtx insn, bool after_p)
1534 {
1535   HOST_WIDE_INT offset;
1536   const char *label;
1537   int i;
1538
1539   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1540      with this function.  Proper support would require all frame-related
1541      insns to be marked, and to be able to handle saving state around
1542      epilogues textually in the middle of the function.  */
1543   if (prologue_epilogue_contains (insn))
1544     return;
1545
1546   /* If INSN is an instruction from target of an annulled branch, the
1547      effects are for the target only and so current argument size
1548      shouldn't change at all.  */
1549   if (final_sequence
1550       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1551       && INSN_FROM_TARGET_P (insn))
1552     return;
1553
1554   /* If only calls can throw, and we have a frame pointer,
1555      save up adjustments until we see the CALL_INSN.  */
1556   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1557     {
1558       if (CALL_P (insn) && !after_p)
1559         {
1560           /* Extract the size of the args from the CALL rtx itself.  */
1561           insn = PATTERN (insn);
1562           if (GET_CODE (insn) == PARALLEL)
1563             insn = XVECEXP (insn, 0, 0);
1564           if (GET_CODE (insn) == SET)
1565             insn = SET_SRC (insn);
1566           gcc_assert (GET_CODE (insn) == CALL);
1567           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1568         }
1569       return;
1570     }
1571
1572   if (CALL_P (insn) && !after_p)
1573     {
1574       if (!flag_asynchronous_unwind_tables)
1575         dwarf2out_args_size ("", args_size);
1576       return;
1577     }
1578   else if (BARRIER_P (insn))
1579     {
1580       /* Don't call compute_barrier_args_size () if the only
1581          BARRIER is at the end of function.  */
1582       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1583         compute_barrier_args_size ();
1584       if (barrier_args_size == NULL)
1585         offset = 0;
1586       else
1587         {
1588           offset = barrier_args_size[INSN_UID (insn)];
1589           if (offset < 0)
1590             offset = 0;
1591         }
1592
1593       offset -= args_size;
1594 #ifndef STACK_GROWS_DOWNWARD
1595       offset = -offset;
1596 #endif
1597     }
1598   else if (GET_CODE (PATTERN (insn)) == SET)
1599     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1600   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1601            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1602     {
1603       /* There may be stack adjustments inside compound insns.  Search
1604          for them.  */
1605       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1606         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1607           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1608                                          args_size, offset);
1609     }
1610   else
1611     return;
1612
1613   if (offset == 0)
1614     return;
1615
1616   label = dwarf2out_cfi_label (false);
1617   dwarf2out_args_size_adjust (offset, label);
1618 }
1619
1620 /* Adjust args_size based on stack adjustment OFFSET.  */
1621
1622 static void
1623 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1624 {
1625   if (cfa.reg == STACK_POINTER_REGNUM)
1626     cfa.offset += offset;
1627
1628   if (cfa_store.reg == STACK_POINTER_REGNUM)
1629     cfa_store.offset += offset;
1630
1631 #ifndef STACK_GROWS_DOWNWARD
1632   offset = -offset;
1633 #endif
1634
1635   args_size += offset;
1636   if (args_size < 0)
1637     args_size = 0;
1638
1639   def_cfa_1 (label, &cfa);
1640   if (flag_asynchronous_unwind_tables)
1641     dwarf2out_args_size (label, args_size);
1642 }
1643
1644 #endif
1645
1646 /* We delay emitting a register save until either (a) we reach the end
1647    of the prologue or (b) the register is clobbered.  This clusters
1648    register saves so that there are fewer pc advances.  */
1649
1650 struct GTY(()) queued_reg_save {
1651   struct queued_reg_save *next;
1652   rtx reg;
1653   HOST_WIDE_INT cfa_offset;
1654   rtx saved_reg;
1655 };
1656
1657 static GTY(()) struct queued_reg_save *queued_reg_saves;
1658
1659 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1660 struct GTY(()) reg_saved_in_data {
1661   rtx orig_reg;
1662   rtx saved_in_reg;
1663 };
1664
1665 /* A list of registers saved in other registers.
1666    The list intentionally has a small maximum capacity of 4; if your
1667    port needs more than that, you might consider implementing a
1668    more efficient data structure.  */
1669 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1670 static GTY(()) size_t num_regs_saved_in_regs;
1671
1672 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1673 static const char *last_reg_save_label;
1674
1675 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1676    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1677
1678 static void
1679 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1680 {
1681   struct queued_reg_save *q;
1682
1683   /* Duplicates waste space, but it's also necessary to remove them
1684      for correctness, since the queue gets output in reverse
1685      order.  */
1686   for (q = queued_reg_saves; q != NULL; q = q->next)
1687     if (REGNO (q->reg) == REGNO (reg))
1688       break;
1689
1690   if (q == NULL)
1691     {
1692       q = GGC_NEW (struct queued_reg_save);
1693       q->next = queued_reg_saves;
1694       queued_reg_saves = q;
1695     }
1696
1697   q->reg = reg;
1698   q->cfa_offset = offset;
1699   q->saved_reg = sreg;
1700
1701   last_reg_save_label = label;
1702 }
1703
1704 /* Output all the entries in QUEUED_REG_SAVES.  */
1705
1706 static void
1707 flush_queued_reg_saves (void)
1708 {
1709   struct queued_reg_save *q;
1710
1711   for (q = queued_reg_saves; q; q = q->next)
1712     {
1713       size_t i;
1714       unsigned int reg, sreg;
1715
1716       for (i = 0; i < num_regs_saved_in_regs; i++)
1717         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1718           break;
1719       if (q->saved_reg && i == num_regs_saved_in_regs)
1720         {
1721           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1722           num_regs_saved_in_regs++;
1723         }
1724       if (i != num_regs_saved_in_regs)
1725         {
1726           regs_saved_in_regs[i].orig_reg = q->reg;
1727           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1728         }
1729
1730       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1731       if (q->saved_reg)
1732         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1733       else
1734         sreg = INVALID_REGNUM;
1735       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1736     }
1737
1738   queued_reg_saves = NULL;
1739   last_reg_save_label = NULL;
1740 }
1741
1742 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1743    location for?  Or, does it clobber a register which we've previously
1744    said that some other register is saved in, and for which we now
1745    have a new location for?  */
1746
1747 static bool
1748 clobbers_queued_reg_save (const_rtx insn)
1749 {
1750   struct queued_reg_save *q;
1751
1752   for (q = queued_reg_saves; q; q = q->next)
1753     {
1754       size_t i;
1755       if (modified_in_p (q->reg, insn))
1756         return true;
1757       for (i = 0; i < num_regs_saved_in_regs; i++)
1758         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1759             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1760           return true;
1761     }
1762
1763   return false;
1764 }
1765
1766 /* Entry point for saving the first register into the second.  */
1767
1768 void
1769 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1770 {
1771   size_t i;
1772   unsigned int regno, sregno;
1773
1774   for (i = 0; i < num_regs_saved_in_regs; i++)
1775     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1776       break;
1777   if (i == num_regs_saved_in_regs)
1778     {
1779       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1780       num_regs_saved_in_regs++;
1781     }
1782   regs_saved_in_regs[i].orig_reg = reg;
1783   regs_saved_in_regs[i].saved_in_reg = sreg;
1784
1785   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1786   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1787   reg_save (label, regno, sregno, 0);
1788 }
1789
1790 /* What register, if any, is currently saved in REG?  */
1791
1792 static rtx
1793 reg_saved_in (rtx reg)
1794 {
1795   unsigned int regn = REGNO (reg);
1796   size_t i;
1797   struct queued_reg_save *q;
1798
1799   for (q = queued_reg_saves; q; q = q->next)
1800     if (q->saved_reg && regn == REGNO (q->saved_reg))
1801       return q->reg;
1802
1803   for (i = 0; i < num_regs_saved_in_regs; i++)
1804     if (regs_saved_in_regs[i].saved_in_reg
1805         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1806       return regs_saved_in_regs[i].orig_reg;
1807
1808   return NULL_RTX;
1809 }
1810
1811
1812 /* A temporary register holding an integral value used in adjusting SP
1813    or setting up the store_reg.  The "offset" field holds the integer
1814    value, not an offset.  */
1815 static dw_cfa_location cfa_temp;
1816
1817 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1818
1819 static void
1820 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1821 {
1822   memset (&cfa, 0, sizeof (cfa));
1823
1824   switch (GET_CODE (pat))
1825     {
1826     case PLUS:
1827       cfa.reg = REGNO (XEXP (pat, 0));
1828       cfa.offset = INTVAL (XEXP (pat, 1));
1829       break;
1830
1831     case REG:
1832       cfa.reg = REGNO (pat);
1833       break;
1834
1835     default:
1836       /* Recurse and define an expression.  */
1837       gcc_unreachable ();
1838     }
1839
1840   def_cfa_1 (label, &cfa);
1841 }
1842
1843 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1844
1845 static void
1846 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1847 {
1848   rtx src, dest;
1849
1850   gcc_assert (GET_CODE (pat) == SET);
1851   dest = XEXP (pat, 0);
1852   src = XEXP (pat, 1);
1853
1854   switch (GET_CODE (src))
1855     {
1856     case PLUS:
1857       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1858       cfa.offset -= INTVAL (XEXP (src, 1));
1859       break;
1860
1861     case REG:
1862         break;
1863
1864     default:
1865         gcc_unreachable ();
1866     }
1867
1868   cfa.reg = REGNO (dest);
1869   gcc_assert (cfa.indirect == 0);
1870
1871   def_cfa_1 (label, &cfa);
1872 }
1873
1874 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1875
1876 static void
1877 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1878 {
1879   HOST_WIDE_INT offset;
1880   rtx src, addr, span;
1881
1882   src = XEXP (set, 1);
1883   addr = XEXP (set, 0);
1884   gcc_assert (MEM_P (addr));
1885   addr = XEXP (addr, 0);
1886   
1887   /* As documented, only consider extremely simple addresses.  */
1888   switch (GET_CODE (addr))
1889     {
1890     case REG:
1891       gcc_assert (REGNO (addr) == cfa.reg);
1892       offset = -cfa.offset;
1893       break;
1894     case PLUS:
1895       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1896       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1897       break;
1898     default:
1899       gcc_unreachable ();
1900     }
1901
1902   span = targetm.dwarf_register_span (src);
1903
1904   /* ??? We'd like to use queue_reg_save, but we need to come up with
1905      a different flushing heuristic for epilogues.  */
1906   if (!span)
1907     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1908   else
1909     {
1910       /* We have a PARALLEL describing where the contents of SRC live.
1911          Queue register saves for each piece of the PARALLEL.  */
1912       int par_index;
1913       int limit;
1914       HOST_WIDE_INT span_offset = offset;
1915
1916       gcc_assert (GET_CODE (span) == PARALLEL);
1917
1918       limit = XVECLEN (span, 0);
1919       for (par_index = 0; par_index < limit; par_index++)
1920         {
1921           rtx elem = XVECEXP (span, 0, par_index);
1922
1923           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1924                     INVALID_REGNUM, span_offset);
1925           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1926         }
1927     }
1928 }
1929
1930 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1931
1932 static void
1933 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1934 {
1935   rtx src, dest;
1936   unsigned sregno, dregno;
1937
1938   src = XEXP (set, 1);
1939   dest = XEXP (set, 0);
1940
1941   if (src == pc_rtx)
1942     sregno = DWARF_FRAME_RETURN_COLUMN;
1943   else
1944     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1945
1946   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1947
1948   /* ??? We'd like to use queue_reg_save, but we need to come up with
1949      a different flushing heuristic for epilogues.  */
1950   reg_save (label, sregno, dregno, 0);
1951 }
1952
1953 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1954
1955 static void
1956 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1957 {
1958   dw_cfi_ref cfi = new_cfi ();
1959   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1960
1961   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1962   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1963
1964   add_fde_cfi (label, cfi);
1965 }
1966
1967 /* Record call frame debugging information for an expression EXPR,
1968    which either sets SP or FP (adjusting how we calculate the frame
1969    address) or saves a register to the stack or another register.
1970    LABEL indicates the address of EXPR.
1971
1972    This function encodes a state machine mapping rtxes to actions on
1973    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1974    users need not read the source code.
1975
1976   The High-Level Picture
1977
1978   Changes in the register we use to calculate the CFA: Currently we
1979   assume that if you copy the CFA register into another register, we
1980   should take the other one as the new CFA register; this seems to
1981   work pretty well.  If it's wrong for some target, it's simple
1982   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1983
1984   Changes in the register we use for saving registers to the stack:
1985   This is usually SP, but not always.  Again, we deduce that if you
1986   copy SP into another register (and SP is not the CFA register),
1987   then the new register is the one we will be using for register
1988   saves.  This also seems to work.
1989
1990   Register saves: There's not much guesswork about this one; if
1991   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1992   register save, and the register used to calculate the destination
1993   had better be the one we think we're using for this purpose.
1994   It's also assumed that a copy from a call-saved register to another
1995   register is saving that register if RTX_FRAME_RELATED_P is set on
1996   that instruction.  If the copy is from a call-saved register to
1997   the *same* register, that means that the register is now the same
1998   value as in the caller.
1999
2000   Except: If the register being saved is the CFA register, and the
2001   offset is nonzero, we are saving the CFA, so we assume we have to
2002   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2003   the intent is to save the value of SP from the previous frame.
2004
2005   In addition, if a register has previously been saved to a different
2006   register,
2007
2008   Invariants / Summaries of Rules
2009
2010   cfa          current rule for calculating the CFA.  It usually
2011                consists of a register and an offset.
2012   cfa_store    register used by prologue code to save things to the stack
2013                cfa_store.offset is the offset from the value of
2014                cfa_store.reg to the actual CFA
2015   cfa_temp     register holding an integral value.  cfa_temp.offset
2016                stores the value, which will be used to adjust the
2017                stack pointer.  cfa_temp is also used like cfa_store,
2018                to track stores to the stack via fp or a temp reg.
2019
2020   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2021                with cfa.reg as the first operand changes the cfa.reg and its
2022                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2023                cfa_temp.offset.
2024
2025   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2026                expression yielding a constant.  This sets cfa_temp.reg
2027                and cfa_temp.offset.
2028
2029   Rule 5:      Create a new register cfa_store used to save items to the
2030                stack.
2031
2032   Rules 10-14: Save a register to the stack.  Define offset as the
2033                difference of the original location and cfa_store's
2034                location (or cfa_temp's location if cfa_temp is used).
2035
2036   Rules 16-20: If AND operation happens on sp in prologue, we assume
2037                stack is realigned.  We will use a group of DW_OP_XXX
2038                expressions to represent the location of the stored
2039                register instead of CFA+offset.
2040
2041   The Rules
2042
2043   "{a,b}" indicates a choice of a xor b.
2044   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2045
2046   Rule 1:
2047   (set <reg1> <reg2>:cfa.reg)
2048   effects: cfa.reg = <reg1>
2049            cfa.offset unchanged
2050            cfa_temp.reg = <reg1>
2051            cfa_temp.offset = cfa.offset
2052
2053   Rule 2:
2054   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2055                               {<const_int>,<reg>:cfa_temp.reg}))
2056   effects: cfa.reg = sp if fp used
2057            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2058            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2059              if cfa_store.reg==sp
2060
2061   Rule 3:
2062   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2063   effects: cfa.reg = fp
2064            cfa_offset += +/- <const_int>
2065
2066   Rule 4:
2067   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2068   constraints: <reg1> != fp
2069                <reg1> != sp
2070   effects: cfa.reg = <reg1>
2071            cfa_temp.reg = <reg1>
2072            cfa_temp.offset = cfa.offset
2073
2074   Rule 5:
2075   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2076   constraints: <reg1> != fp
2077                <reg1> != sp
2078   effects: cfa_store.reg = <reg1>
2079            cfa_store.offset = cfa.offset - cfa_temp.offset
2080
2081   Rule 6:
2082   (set <reg> <const_int>)
2083   effects: cfa_temp.reg = <reg>
2084            cfa_temp.offset = <const_int>
2085
2086   Rule 7:
2087   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2088   effects: cfa_temp.reg = <reg1>
2089            cfa_temp.offset |= <const_int>
2090
2091   Rule 8:
2092   (set <reg> (high <exp>))
2093   effects: none
2094
2095   Rule 9:
2096   (set <reg> (lo_sum <exp> <const_int>))
2097   effects: cfa_temp.reg = <reg>
2098            cfa_temp.offset = <const_int>
2099
2100   Rule 10:
2101   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2102   effects: cfa_store.offset -= <const_int>
2103            cfa.offset = cfa_store.offset if cfa.reg == sp
2104            cfa.reg = sp
2105            cfa.base_offset = -cfa_store.offset
2106
2107   Rule 11:
2108   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2109   effects: cfa_store.offset += -/+ mode_size(mem)
2110            cfa.offset = cfa_store.offset if cfa.reg == sp
2111            cfa.reg = sp
2112            cfa.base_offset = -cfa_store.offset
2113
2114   Rule 12:
2115   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2116
2117        <reg2>)
2118   effects: cfa.reg = <reg1>
2119            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2120
2121   Rule 13:
2122   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2123   effects: cfa.reg = <reg1>
2124            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2125
2126   Rule 14:
2127   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2128   effects: cfa.reg = <reg1>
2129            cfa.base_offset = -cfa_temp.offset
2130            cfa_temp.offset -= mode_size(mem)
2131
2132   Rule 15:
2133   (set <reg> {unspec, unspec_volatile})
2134   effects: target-dependent
2135
2136   Rule 16:
2137   (set sp (and: sp <const_int>))
2138   constraints: cfa_store.reg == sp
2139   effects: current_fde.stack_realign = 1
2140            cfa_store.offset = 0
2141            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2142
2143   Rule 17:
2144   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2145   effects: cfa_store.offset += -/+ mode_size(mem)
2146
2147   Rule 18:
2148   (set (mem ({pre_inc, pre_dec} sp)) fp)
2149   constraints: fde->stack_realign == 1
2150   effects: cfa_store.offset = 0
2151            cfa.reg != HARD_FRAME_POINTER_REGNUM
2152
2153   Rule 19:
2154   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2155   constraints: fde->stack_realign == 1
2156                && cfa.offset == 0
2157                && cfa.indirect == 0
2158                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2159   effects: Use DW_CFA_def_cfa_expression to define cfa
2160            cfa.reg == fde->drap_reg
2161
2162   Rule 20:
2163   (set reg fde->drap_reg)
2164   constraints: fde->vdrap_reg == INVALID_REGNUM
2165   effects: fde->vdrap_reg = reg.
2166   (set mem fde->drap_reg)
2167   constraints: fde->drap_reg_saved == 1
2168   effects: none.  */
2169
2170 static void
2171 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2172 {
2173   rtx src, dest, span;
2174   HOST_WIDE_INT offset;
2175   dw_fde_ref fde;
2176
2177   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2178      the PARALLEL independently. The first element is always processed if
2179      it is a SET. This is for backward compatibility.   Other elements
2180      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2181      flag is set in them.  */
2182   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2183     {
2184       int par_index;
2185       int limit = XVECLEN (expr, 0);
2186       rtx elem;
2187
2188       /* PARALLELs have strict read-modify-write semantics, so we
2189          ought to evaluate every rvalue before changing any lvalue.
2190          It's cumbersome to do that in general, but there's an
2191          easy approximation that is enough for all current users:
2192          handle register saves before register assignments.  */
2193       if (GET_CODE (expr) == PARALLEL)
2194         for (par_index = 0; par_index < limit; par_index++)
2195           {
2196             elem = XVECEXP (expr, 0, par_index);
2197             if (GET_CODE (elem) == SET
2198                 && MEM_P (SET_DEST (elem))
2199                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2200               dwarf2out_frame_debug_expr (elem, label);
2201           }
2202
2203       for (par_index = 0; par_index < limit; par_index++)
2204         {
2205           elem = XVECEXP (expr, 0, par_index);
2206           if (GET_CODE (elem) == SET
2207               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2208               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2209             dwarf2out_frame_debug_expr (elem, label);
2210           else if (GET_CODE (elem) == SET
2211                    && par_index != 0
2212                    && !RTX_FRAME_RELATED_P (elem))
2213             {
2214               /* Stack adjustment combining might combine some post-prologue
2215                  stack adjustment into a prologue stack adjustment.  */
2216               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2217
2218               if (offset != 0)
2219                 dwarf2out_args_size_adjust (offset, label);
2220             }
2221         }
2222       return;
2223     }
2224
2225   gcc_assert (GET_CODE (expr) == SET);
2226
2227   src = SET_SRC (expr);
2228   dest = SET_DEST (expr);
2229
2230   if (REG_P (src))
2231     {
2232       rtx rsi = reg_saved_in (src);
2233       if (rsi)
2234         src = rsi;
2235     }
2236
2237   fde = current_fde ();
2238
2239   if (REG_P (src)
2240       && fde
2241       && fde->drap_reg == REGNO (src)
2242       && (fde->drap_reg_saved
2243           || REG_P (dest)))
2244     {
2245       /* Rule 20 */
2246       /* If we are saving dynamic realign argument pointer to a
2247          register, the destination is virtual dynamic realign
2248          argument pointer.  It may be used to access argument.  */
2249       if (REG_P (dest))
2250         {
2251           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2252           fde->vdrap_reg = REGNO (dest);
2253         }
2254       return;
2255     }
2256
2257   switch (GET_CODE (dest))
2258     {
2259     case REG:
2260       switch (GET_CODE (src))
2261         {
2262           /* Setting FP from SP.  */
2263         case REG:
2264           if (cfa.reg == (unsigned) REGNO (src))
2265             {
2266               /* Rule 1 */
2267               /* Update the CFA rule wrt SP or FP.  Make sure src is
2268                  relative to the current CFA register.
2269
2270                  We used to require that dest be either SP or FP, but the
2271                  ARM copies SP to a temporary register, and from there to
2272                  FP.  So we just rely on the backends to only set
2273                  RTX_FRAME_RELATED_P on appropriate insns.  */
2274               cfa.reg = REGNO (dest);
2275               cfa_temp.reg = cfa.reg;
2276               cfa_temp.offset = cfa.offset;
2277             }
2278           else
2279             {
2280               /* Saving a register in a register.  */
2281               gcc_assert (!fixed_regs [REGNO (dest)]
2282                           /* For the SPARC and its register window.  */
2283                           || (DWARF_FRAME_REGNUM (REGNO (src))
2284                               == DWARF_FRAME_RETURN_COLUMN));
2285
2286               /* After stack is aligned, we can only save SP in FP
2287                  if drap register is used.  In this case, we have
2288                  to restore stack pointer with the CFA value and we
2289                  don't generate this DWARF information.  */
2290               if (fde
2291                   && fde->stack_realign
2292                   && REGNO (src) == STACK_POINTER_REGNUM)
2293                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2294                             && fde->drap_reg != INVALID_REGNUM
2295                             && cfa.reg != REGNO (src));
2296               else
2297                 queue_reg_save (label, src, dest, 0);
2298             }
2299           break;
2300
2301         case PLUS:
2302         case MINUS:
2303         case LO_SUM:
2304           if (dest == stack_pointer_rtx)
2305             {
2306               /* Rule 2 */
2307               /* Adjusting SP.  */
2308               switch (GET_CODE (XEXP (src, 1)))
2309                 {
2310                 case CONST_INT:
2311                   offset = INTVAL (XEXP (src, 1));
2312                   break;
2313                 case REG:
2314                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2315                               == cfa_temp.reg);
2316                   offset = cfa_temp.offset;
2317                   break;
2318                 default:
2319                   gcc_unreachable ();
2320                 }
2321
2322               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2323                 {
2324                   /* Restoring SP from FP in the epilogue.  */
2325                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2326                   cfa.reg = STACK_POINTER_REGNUM;
2327                 }
2328               else if (GET_CODE (src) == LO_SUM)
2329                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2330                 ;
2331               else
2332                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2333
2334               if (GET_CODE (src) != MINUS)
2335                 offset = -offset;
2336               if (cfa.reg == STACK_POINTER_REGNUM)
2337                 cfa.offset += offset;
2338               if (cfa_store.reg == STACK_POINTER_REGNUM)
2339                 cfa_store.offset += offset;
2340             }
2341           else if (dest == hard_frame_pointer_rtx)
2342             {
2343               /* Rule 3 */
2344               /* Either setting the FP from an offset of the SP,
2345                  or adjusting the FP */
2346               gcc_assert (frame_pointer_needed);
2347
2348               gcc_assert (REG_P (XEXP (src, 0))
2349                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2350                           && CONST_INT_P (XEXP (src, 1)));
2351               offset = INTVAL (XEXP (src, 1));
2352               if (GET_CODE (src) != MINUS)
2353                 offset = -offset;
2354               cfa.offset += offset;
2355               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2356             }
2357           else
2358             {
2359               gcc_assert (GET_CODE (src) != MINUS);
2360
2361               /* Rule 4 */
2362               if (REG_P (XEXP (src, 0))
2363                   && REGNO (XEXP (src, 0)) == cfa.reg
2364                   && CONST_INT_P (XEXP (src, 1)))
2365                 {
2366                   /* Setting a temporary CFA register that will be copied
2367                      into the FP later on.  */
2368                   offset = - INTVAL (XEXP (src, 1));
2369                   cfa.offset += offset;
2370                   cfa.reg = REGNO (dest);
2371                   /* Or used to save regs to the stack.  */
2372                   cfa_temp.reg = cfa.reg;
2373                   cfa_temp.offset = cfa.offset;
2374                 }
2375
2376               /* Rule 5 */
2377               else if (REG_P (XEXP (src, 0))
2378                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2379                        && XEXP (src, 1) == stack_pointer_rtx)
2380                 {
2381                   /* Setting a scratch register that we will use instead
2382                      of SP for saving registers to the stack.  */
2383                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2384                   cfa_store.reg = REGNO (dest);
2385                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2386                 }
2387
2388               /* Rule 9 */
2389               else if (GET_CODE (src) == LO_SUM
2390                        && CONST_INT_P (XEXP (src, 1)))
2391                 {
2392                   cfa_temp.reg = REGNO (dest);
2393                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2394                 }
2395               else
2396                 gcc_unreachable ();
2397             }
2398           break;
2399
2400           /* Rule 6 */
2401         case CONST_INT:
2402           cfa_temp.reg = REGNO (dest);
2403           cfa_temp.offset = INTVAL (src);
2404           break;
2405
2406           /* Rule 7 */
2407         case IOR:
2408           gcc_assert (REG_P (XEXP (src, 0))
2409                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2410                       && CONST_INT_P (XEXP (src, 1)));
2411
2412           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2413             cfa_temp.reg = REGNO (dest);
2414           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2415           break;
2416
2417           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2418              which will fill in all of the bits.  */
2419           /* Rule 8 */
2420         case HIGH:
2421           break;
2422
2423           /* Rule 15 */
2424         case UNSPEC:
2425         case UNSPEC_VOLATILE:
2426           gcc_assert (targetm.dwarf_handle_frame_unspec);
2427           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2428           return;
2429
2430           /* Rule 16 */
2431         case AND:
2432           /* If this AND operation happens on stack pointer in prologue,
2433              we assume the stack is realigned and we extract the
2434              alignment.  */
2435           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2436             {
2437               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2438               fde->stack_realign = 1;
2439               fde->stack_realignment = INTVAL (XEXP (src, 1));
2440               cfa_store.offset = 0;
2441
2442               if (cfa.reg != STACK_POINTER_REGNUM
2443                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2444                 fde->drap_reg = cfa.reg;
2445             }
2446           return;
2447
2448         default:
2449           gcc_unreachable ();
2450         }
2451
2452       def_cfa_1 (label, &cfa);
2453       break;
2454
2455     case MEM:
2456
2457       /* Saving a register to the stack.  Make sure dest is relative to the
2458          CFA register.  */
2459       switch (GET_CODE (XEXP (dest, 0)))
2460         {
2461           /* Rule 10 */
2462           /* With a push.  */
2463         case PRE_MODIFY:
2464           /* We can't handle variable size modifications.  */
2465           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2466                       == CONST_INT);
2467           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2468
2469           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2470                       && cfa_store.reg == STACK_POINTER_REGNUM);
2471
2472           cfa_store.offset += offset;
2473           if (cfa.reg == STACK_POINTER_REGNUM)
2474             cfa.offset = cfa_store.offset;
2475
2476           offset = -cfa_store.offset;
2477           break;
2478
2479           /* Rule 11 */
2480         case PRE_INC:
2481         case PRE_DEC:
2482           offset = GET_MODE_SIZE (GET_MODE (dest));
2483           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2484             offset = -offset;
2485
2486           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2487                        == STACK_POINTER_REGNUM)
2488                       && cfa_store.reg == STACK_POINTER_REGNUM);
2489
2490           cfa_store.offset += offset;
2491
2492           /* Rule 18: If stack is aligned, we will use FP as a
2493              reference to represent the address of the stored
2494              regiser.  */
2495           if (fde
2496               && fde->stack_realign
2497               && src == hard_frame_pointer_rtx)
2498             {
2499               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2500               cfa_store.offset = 0;
2501             }
2502
2503           if (cfa.reg == STACK_POINTER_REGNUM)
2504             cfa.offset = cfa_store.offset;
2505
2506           offset = -cfa_store.offset;
2507           break;
2508
2509           /* Rule 12 */
2510           /* With an offset.  */
2511         case PLUS:
2512         case MINUS:
2513         case LO_SUM:
2514           {
2515             int regno;
2516
2517             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2518                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2519             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2520             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2521               offset = -offset;
2522
2523             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2524
2525             if (cfa_store.reg == (unsigned) regno)
2526               offset -= cfa_store.offset;
2527             else
2528               {
2529                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2530                 offset -= cfa_temp.offset;
2531               }
2532           }
2533           break;
2534
2535           /* Rule 13 */
2536           /* Without an offset.  */
2537         case REG:
2538           {
2539             int regno = REGNO (XEXP (dest, 0));
2540
2541             if (cfa_store.reg == (unsigned) regno)
2542               offset = -cfa_store.offset;
2543             else
2544               {
2545                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2546                 offset = -cfa_temp.offset;
2547               }
2548           }
2549           break;
2550
2551           /* Rule 14 */
2552         case POST_INC:
2553           gcc_assert (cfa_temp.reg
2554                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2555           offset = -cfa_temp.offset;
2556           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2557           break;
2558
2559         default:
2560           gcc_unreachable ();
2561         }
2562
2563         /* Rule 17 */
2564         /* If the source operand of this MEM operation is not a
2565            register, basically the source is return address.  Here
2566            we only care how much stack grew and we don't save it.  */
2567       if (!REG_P (src))
2568         break;
2569
2570       if (REGNO (src) != STACK_POINTER_REGNUM
2571           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2572           && (unsigned) REGNO (src) == cfa.reg)
2573         {
2574           /* We're storing the current CFA reg into the stack.  */
2575
2576           if (cfa.offset == 0)
2577             {
2578               /* Rule 19 */
2579               /* If stack is aligned, putting CFA reg into stack means
2580                  we can no longer use reg + offset to represent CFA.
2581                  Here we use DW_CFA_def_cfa_expression instead.  The
2582                  result of this expression equals to the original CFA
2583                  value.  */
2584               if (fde
2585                   && fde->stack_realign
2586                   && cfa.indirect == 0
2587                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2588                 {
2589                   dw_cfa_location cfa_exp;
2590
2591                   gcc_assert (fde->drap_reg == cfa.reg);
2592
2593                   cfa_exp.indirect = 1;
2594                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2595                   cfa_exp.base_offset = offset;
2596                   cfa_exp.offset = 0;
2597
2598                   fde->drap_reg_saved = 1;
2599
2600                   def_cfa_1 (label, &cfa_exp);
2601                   break;
2602                 }
2603
2604               /* If the source register is exactly the CFA, assume
2605                  we're saving SP like any other register; this happens
2606                  on the ARM.  */
2607               def_cfa_1 (label, &cfa);
2608               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2609               break;
2610             }
2611           else
2612             {
2613               /* Otherwise, we'll need to look in the stack to
2614                  calculate the CFA.  */
2615               rtx x = XEXP (dest, 0);
2616
2617               if (!REG_P (x))
2618                 x = XEXP (x, 0);
2619               gcc_assert (REG_P (x));
2620
2621               cfa.reg = REGNO (x);
2622               cfa.base_offset = offset;
2623               cfa.indirect = 1;
2624               def_cfa_1 (label, &cfa);
2625               break;
2626             }
2627         }
2628
2629       def_cfa_1 (label, &cfa);
2630       {
2631         span = targetm.dwarf_register_span (src);
2632
2633         if (!span)
2634           queue_reg_save (label, src, NULL_RTX, offset);
2635         else
2636           {
2637             /* We have a PARALLEL describing where the contents of SRC
2638                live.  Queue register saves for each piece of the
2639                PARALLEL.  */
2640             int par_index;
2641             int limit;
2642             HOST_WIDE_INT span_offset = offset;
2643
2644             gcc_assert (GET_CODE (span) == PARALLEL);
2645
2646             limit = XVECLEN (span, 0);
2647             for (par_index = 0; par_index < limit; par_index++)
2648               {
2649                 rtx elem = XVECEXP (span, 0, par_index);
2650
2651                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2652                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2653               }
2654           }
2655       }
2656       break;
2657
2658     default:
2659       gcc_unreachable ();
2660     }
2661 }
2662
2663 /* Record call frame debugging information for INSN, which either
2664    sets SP or FP (adjusting how we calculate the frame address) or saves a
2665    register to the stack.  If INSN is NULL_RTX, initialize our state.
2666
2667    If AFTER_P is false, we're being called before the insn is emitted,
2668    otherwise after.  Call instructions get invoked twice.  */
2669
2670 void
2671 dwarf2out_frame_debug (rtx insn, bool after_p)
2672 {
2673   const char *label;
2674   rtx note, n;
2675   bool handled_one = false;
2676
2677   if (insn == NULL_RTX)
2678     {
2679       size_t i;
2680
2681       /* Flush any queued register saves.  */
2682       flush_queued_reg_saves ();
2683
2684       /* Set up state for generating call frame debug info.  */
2685       lookup_cfa (&cfa);
2686       gcc_assert (cfa.reg
2687                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2688
2689       cfa.reg = STACK_POINTER_REGNUM;
2690       cfa_store = cfa;
2691       cfa_temp.reg = -1;
2692       cfa_temp.offset = 0;
2693
2694       for (i = 0; i < num_regs_saved_in_regs; i++)
2695         {
2696           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2697           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2698         }
2699       num_regs_saved_in_regs = 0;
2700
2701       if (barrier_args_size)
2702         {
2703           XDELETEVEC (barrier_args_size);
2704           barrier_args_size = NULL;
2705         }
2706       return;
2707     }
2708
2709   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2710     flush_queued_reg_saves ();
2711
2712   if (! RTX_FRAME_RELATED_P (insn))
2713     {
2714       if (!ACCUMULATE_OUTGOING_ARGS)
2715         dwarf2out_stack_adjust (insn, after_p);
2716       return;
2717     }
2718
2719   label = dwarf2out_cfi_label (false);
2720
2721   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2722     switch (REG_NOTE_KIND (note))
2723       {
2724       case REG_FRAME_RELATED_EXPR:
2725         insn = XEXP (note, 0);
2726         goto found;
2727
2728       case REG_CFA_DEF_CFA:
2729         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2730         handled_one = true;
2731         break;
2732
2733       case REG_CFA_ADJUST_CFA:
2734         n = XEXP (note, 0);
2735         if (n == NULL)
2736           {
2737             n = PATTERN (insn);
2738             if (GET_CODE (n) == PARALLEL)
2739               n = XVECEXP (n, 0, 0);
2740           }
2741         dwarf2out_frame_debug_adjust_cfa (n, label);
2742         handled_one = true;
2743         break;
2744
2745       case REG_CFA_OFFSET:
2746         n = XEXP (note, 0);
2747         if (n == NULL)
2748           n = single_set (insn);
2749         dwarf2out_frame_debug_cfa_offset (n, label);
2750         handled_one = true;
2751         break;
2752
2753       case REG_CFA_REGISTER:
2754         n = XEXP (note, 0);
2755         if (n == NULL)
2756           {
2757             n = PATTERN (insn);
2758             if (GET_CODE (n) == PARALLEL)
2759               n = XVECEXP (n, 0, 0);
2760           }
2761         dwarf2out_frame_debug_cfa_register (n, label);
2762         handled_one = true;
2763         break;
2764
2765       case REG_CFA_RESTORE:
2766         n = XEXP (note, 0);
2767         if (n == NULL)
2768           {
2769             n = PATTERN (insn);
2770             if (GET_CODE (n) == PARALLEL)
2771               n = XVECEXP (n, 0, 0);
2772             n = XEXP (n, 0);
2773           }
2774         dwarf2out_frame_debug_cfa_restore (n, label);
2775         handled_one = true;
2776         break;
2777
2778       default:
2779         break;
2780       }
2781   if (handled_one)
2782     return;
2783
2784   insn = PATTERN (insn);
2785  found:
2786   dwarf2out_frame_debug_expr (insn, label);
2787 }
2788
2789 /* Determine if we need to save and restore CFI information around this
2790    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2791    we do need to save/restore, then emit the save now, and insert a
2792    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2793
2794 void
2795 dwarf2out_begin_epilogue (rtx insn)
2796 {
2797   bool saw_frp = false;
2798   rtx i;
2799
2800   /* Scan forward to the return insn, noticing if there are possible
2801      frame related insns.  */
2802   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2803     {
2804       if (!INSN_P (i))
2805         continue;
2806
2807       /* Look for both regular and sibcalls to end the block.  */
2808       if (returnjump_p (i))
2809         break;
2810       if (CALL_P (i) && SIBLING_CALL_P (i))
2811         break;
2812
2813       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2814         {
2815           int idx;
2816           rtx seq = PATTERN (i);
2817
2818           if (returnjump_p (XVECEXP (seq, 0, 0)))
2819             break;
2820           if (CALL_P (XVECEXP (seq, 0, 0))
2821               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2822             break;
2823
2824           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2825             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2826               saw_frp = true;
2827         }
2828
2829       if (RTX_FRAME_RELATED_P (i))
2830         saw_frp = true;
2831     }
2832
2833   /* If the port doesn't emit epilogue unwind info, we don't need a
2834      save/restore pair.  */
2835   if (!saw_frp)
2836     return;
2837
2838   /* Otherwise, search forward to see if the return insn was the last
2839      basic block of the function.  If so, we don't need save/restore.  */
2840   gcc_assert (i != NULL);
2841   i = next_real_insn (i);
2842   if (i == NULL)
2843     return;
2844
2845   /* Insert the restore before that next real insn in the stream, and before
2846      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2847      properly nested.  This should be after any label or alignment.  This
2848      will be pushed into the CFI stream by the function below.  */
2849   while (1)
2850     {
2851       rtx p = PREV_INSN (i);
2852       if (!NOTE_P (p))
2853         break;
2854       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2855         break;
2856       i = p;
2857     }
2858   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2859
2860   emit_cfa_remember = true;
2861
2862   /* And emulate the state save.  */
2863   gcc_assert (!cfa_remember.in_use);
2864   cfa_remember = cfa;
2865   cfa_remember.in_use = 1;
2866 }
2867
2868 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2869
2870 void
2871 dwarf2out_frame_debug_restore_state (void)
2872 {
2873   dw_cfi_ref cfi = new_cfi (); 
2874   const char *label = dwarf2out_cfi_label (false);
2875
2876   cfi->dw_cfi_opc = DW_CFA_restore_state;
2877   add_fde_cfi (label, cfi);
2878
2879   gcc_assert (cfa_remember.in_use);
2880   cfa = cfa_remember;
2881   cfa_remember.in_use = 0;
2882 }
2883
2884 #endif
2885
2886 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2887 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2888  (enum dwarf_call_frame_info cfi);
2889
2890 static enum dw_cfi_oprnd_type
2891 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2892 {
2893   switch (cfi)
2894     {
2895     case DW_CFA_nop:
2896     case DW_CFA_GNU_window_save:
2897     case DW_CFA_remember_state:
2898     case DW_CFA_restore_state:
2899       return dw_cfi_oprnd_unused;
2900
2901     case DW_CFA_set_loc:
2902     case DW_CFA_advance_loc1:
2903     case DW_CFA_advance_loc2:
2904     case DW_CFA_advance_loc4:
2905     case DW_CFA_MIPS_advance_loc8:
2906       return dw_cfi_oprnd_addr;
2907
2908     case DW_CFA_offset:
2909     case DW_CFA_offset_extended:
2910     case DW_CFA_def_cfa:
2911     case DW_CFA_offset_extended_sf:
2912     case DW_CFA_def_cfa_sf:
2913     case DW_CFA_restore:
2914     case DW_CFA_restore_extended:
2915     case DW_CFA_undefined:
2916     case DW_CFA_same_value:
2917     case DW_CFA_def_cfa_register:
2918     case DW_CFA_register:
2919       return dw_cfi_oprnd_reg_num;
2920
2921     case DW_CFA_def_cfa_offset:
2922     case DW_CFA_GNU_args_size:
2923     case DW_CFA_def_cfa_offset_sf:
2924       return dw_cfi_oprnd_offset;
2925
2926     case DW_CFA_def_cfa_expression:
2927     case DW_CFA_expression:
2928       return dw_cfi_oprnd_loc;
2929
2930     default:
2931       gcc_unreachable ();
2932     }
2933 }
2934
2935 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2936 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2937  (enum dwarf_call_frame_info cfi);
2938
2939 static enum dw_cfi_oprnd_type
2940 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2941 {
2942   switch (cfi)
2943     {
2944     case DW_CFA_def_cfa:
2945     case DW_CFA_def_cfa_sf:
2946     case DW_CFA_offset:
2947     case DW_CFA_offset_extended_sf:
2948     case DW_CFA_offset_extended:
2949       return dw_cfi_oprnd_offset;
2950
2951     case DW_CFA_register:
2952       return dw_cfi_oprnd_reg_num;
2953
2954     default:
2955       return dw_cfi_oprnd_unused;
2956     }
2957 }
2958
2959 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2960
2961 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2962    switch to the data section instead, and write out a synthetic start label
2963    for collect2 the first time around.  */
2964
2965 static void
2966 switch_to_eh_frame_section (bool back)
2967 {
2968   tree label;
2969
2970 #ifdef EH_FRAME_SECTION_NAME
2971   if (eh_frame_section == 0)
2972     {
2973       int flags;
2974
2975       if (EH_TABLES_CAN_BE_READ_ONLY)
2976         {
2977           int fde_encoding;
2978           int per_encoding;
2979           int lsda_encoding;
2980
2981           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2982                                                        /*global=*/0);
2983           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2984                                                        /*global=*/1);
2985           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2986                                                         /*global=*/0);
2987           flags = ((! flag_pic
2988                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2989                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2990                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2991                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2992                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2993                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2994                    ? 0 : SECTION_WRITE);
2995         }
2996       else
2997         flags = SECTION_WRITE;
2998       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2999     }
3000 #endif
3001
3002   if (eh_frame_section)
3003     switch_to_section (eh_frame_section);
3004   else
3005     {
3006       /* We have no special eh_frame section.  Put the information in
3007          the data section and emit special labels to guide collect2.  */
3008       switch_to_section (data_section);
3009
3010       if (!back)
3011         {
3012           label = get_file_function_name ("F");
3013           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3014           targetm.asm_out.globalize_label (asm_out_file,
3015                                            IDENTIFIER_POINTER (label));
3016           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3017         }
3018     }
3019 }
3020
3021 /* Switch [BACK] to the eh or debug frame table section, depending on
3022    FOR_EH.  */
3023
3024 static void
3025 switch_to_frame_table_section (int for_eh, bool back)
3026 {
3027   if (for_eh)
3028     switch_to_eh_frame_section (back);
3029   else
3030     {
3031       if (!debug_frame_section)
3032         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3033                                            SECTION_DEBUG, NULL);
3034       switch_to_section (debug_frame_section);
3035     }
3036 }
3037
3038 /* Output a Call Frame Information opcode and its operand(s).  */
3039
3040 static void
3041 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3042 {
3043   unsigned long r;
3044   HOST_WIDE_INT off;
3045
3046   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3047     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3048                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3049                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3050                          ((unsigned HOST_WIDE_INT)
3051                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3052   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3053     {
3054       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3055       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3056                            "DW_CFA_offset, column 0x%lx", r);
3057       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3058       dw2_asm_output_data_uleb128 (off, NULL);
3059     }
3060   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3061     {
3062       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3063       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3064                            "DW_CFA_restore, column 0x%lx", r);
3065     }
3066   else
3067     {
3068       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3069                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3070
3071       switch (cfi->dw_cfi_opc)
3072         {
3073         case DW_CFA_set_loc:
3074           if (for_eh)
3075             dw2_asm_output_encoded_addr_rtx (
3076                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3077                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3078                 false, NULL);
3079           else
3080             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3081                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3082           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3083           break;
3084
3085         case DW_CFA_advance_loc1:
3086           dw2_asm_output_delta (1, 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_loc2:
3092           dw2_asm_output_delta (2, 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_advance_loc4:
3098           dw2_asm_output_delta (4, 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_MIPS_advance_loc8:
3104           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3105                                 fde->dw_fde_current_label, NULL);
3106           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3107           break;
3108
3109         case DW_CFA_offset_extended:
3110           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3111           dw2_asm_output_data_uleb128 (r, NULL);
3112           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3113           dw2_asm_output_data_uleb128 (off, NULL);
3114           break;
3115
3116         case DW_CFA_def_cfa:
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           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3120           break;
3121
3122         case DW_CFA_offset_extended_sf:
3123           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3124           dw2_asm_output_data_uleb128 (r, NULL);
3125           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3126           dw2_asm_output_data_sleb128 (off, NULL);
3127           break;
3128
3129         case DW_CFA_def_cfa_sf:
3130           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3131           dw2_asm_output_data_uleb128 (r, NULL);
3132           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3133           dw2_asm_output_data_sleb128 (off, NULL);
3134           break;
3135
3136         case DW_CFA_restore_extended:
3137         case DW_CFA_undefined:
3138         case DW_CFA_same_value:
3139         case DW_CFA_def_cfa_register:
3140           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3141           dw2_asm_output_data_uleb128 (r, NULL);
3142           break;
3143
3144         case DW_CFA_register:
3145           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3146           dw2_asm_output_data_uleb128 (r, NULL);
3147           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3148           dw2_asm_output_data_uleb128 (r, NULL);
3149           break;
3150
3151         case DW_CFA_def_cfa_offset:
3152         case DW_CFA_GNU_args_size:
3153           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3154           break;
3155
3156         case DW_CFA_def_cfa_offset_sf:
3157           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3158           dw2_asm_output_data_sleb128 (off, NULL);
3159           break;
3160
3161         case DW_CFA_GNU_window_save:
3162           break;
3163
3164         case DW_CFA_def_cfa_expression:
3165         case DW_CFA_expression:
3166           output_cfa_loc (cfi);
3167           break;
3168
3169         case DW_CFA_GNU_negative_offset_extended:
3170           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3171           gcc_unreachable ();
3172
3173         default:
3174           break;
3175         }
3176     }
3177 }
3178
3179 /* Similar, but do it via assembler directives instead.  */
3180
3181 static void
3182 output_cfi_directive (dw_cfi_ref cfi)
3183 {
3184   unsigned long r, r2;
3185
3186   switch (cfi->dw_cfi_opc)
3187     {
3188     case DW_CFA_advance_loc:
3189     case DW_CFA_advance_loc1:
3190     case DW_CFA_advance_loc2:
3191     case DW_CFA_advance_loc4:
3192     case DW_CFA_MIPS_advance_loc8:
3193     case DW_CFA_set_loc:
3194       /* Should only be created by add_fde_cfi in a code path not
3195          followed when emitting via directives.  The assembler is
3196          going to take care of this for us.  */
3197       gcc_unreachable ();
3198
3199     case DW_CFA_offset:
3200     case DW_CFA_offset_extended:
3201     case DW_CFA_offset_extended_sf:
3202       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3203       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3204                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3205       break;
3206
3207     case DW_CFA_restore:
3208     case DW_CFA_restore_extended:
3209       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3210       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3211       break;
3212
3213     case DW_CFA_undefined:
3214       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3215       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3216       break;
3217
3218     case DW_CFA_same_value:
3219       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3220       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3221       break;
3222
3223     case DW_CFA_def_cfa:
3224     case DW_CFA_def_cfa_sf:
3225       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3226       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3227                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3228       break;
3229
3230     case DW_CFA_def_cfa_register:
3231       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3232       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3233       break;
3234
3235     case DW_CFA_register:
3236       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3237       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3238       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3239       break;
3240
3241     case DW_CFA_def_cfa_offset:
3242     case DW_CFA_def_cfa_offset_sf:
3243       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3244                HOST_WIDE_INT_PRINT_DEC"\n",
3245                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3246       break;
3247
3248     case DW_CFA_remember_state:
3249       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3250       break;
3251     case DW_CFA_restore_state:
3252       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3253       break;
3254
3255     case DW_CFA_GNU_args_size:
3256       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3257       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3258       if (flag_debug_asm)
3259         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3260                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3261       fputc ('\n', asm_out_file);
3262       break;
3263
3264     case DW_CFA_GNU_window_save:
3265       fprintf (asm_out_file, "\t.cfi_window_save\n");
3266       break;
3267
3268     case DW_CFA_def_cfa_expression:
3269     case DW_CFA_expression:
3270       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3271       output_cfa_loc_raw (cfi);
3272       fputc ('\n', asm_out_file);
3273       break;
3274
3275     default:
3276       gcc_unreachable ();
3277     }
3278 }
3279
3280 DEF_VEC_P (dw_cfi_ref);
3281 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3282
3283 /* Output CFIs to bring current FDE to the same state as after executing
3284    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3285    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3286    other arguments to pass to output_cfi.  */
3287
3288 static void
3289 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3290 {
3291   struct dw_cfi_struct cfi_buf;
3292   dw_cfi_ref cfi2;
3293   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3294   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3295   unsigned int len, idx;
3296
3297   for (;; cfi = cfi->dw_cfi_next)
3298     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3299       {
3300       case DW_CFA_advance_loc:
3301       case DW_CFA_advance_loc1:
3302       case DW_CFA_advance_loc2:
3303       case DW_CFA_advance_loc4:
3304       case DW_CFA_MIPS_advance_loc8:
3305       case DW_CFA_set_loc:
3306         /* All advances should be ignored.  */
3307         break;
3308       case DW_CFA_remember_state:
3309         {
3310           dw_cfi_ref args_size = cfi_args_size;
3311
3312           /* Skip everything between .cfi_remember_state and
3313              .cfi_restore_state.  */
3314           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3315             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3316               break;
3317             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3318               args_size = cfi2;
3319             else
3320               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3321
3322           if (cfi2 == NULL)
3323             goto flush_all;
3324           else
3325             {
3326               cfi = cfi2;
3327               cfi_args_size = args_size;
3328             }
3329           break;
3330         }
3331       case DW_CFA_GNU_args_size:
3332         cfi_args_size = cfi;
3333         break;
3334       case DW_CFA_GNU_window_save:
3335         goto flush_all;
3336       case DW_CFA_offset:
3337       case DW_CFA_offset_extended:
3338       case DW_CFA_offset_extended_sf:
3339       case DW_CFA_restore:
3340       case DW_CFA_restore_extended:
3341       case DW_CFA_undefined:
3342       case DW_CFA_same_value:
3343       case DW_CFA_register:
3344       case DW_CFA_val_offset:
3345       case DW_CFA_val_offset_sf:
3346       case DW_CFA_expression:
3347       case DW_CFA_val_expression:
3348       case DW_CFA_GNU_negative_offset_extended:
3349         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3350           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3351                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3352         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3353         break;
3354       case DW_CFA_def_cfa:
3355       case DW_CFA_def_cfa_sf:
3356       case DW_CFA_def_cfa_expression:
3357         cfi_cfa = cfi;
3358         cfi_cfa_offset = cfi;
3359         break;
3360       case DW_CFA_def_cfa_register:
3361         cfi_cfa = cfi;
3362         break;
3363       case DW_CFA_def_cfa_offset:
3364       case DW_CFA_def_cfa_offset_sf:
3365         cfi_cfa_offset = cfi;
3366         break;
3367       case DW_CFA_nop:
3368         gcc_assert (cfi == NULL);
3369       flush_all:
3370         len = VEC_length (dw_cfi_ref, regs);
3371         for (idx = 0; idx < len; idx++)
3372           {
3373             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3374             if (cfi2 != NULL
3375                 && cfi2->dw_cfi_opc != DW_CFA_restore
3376                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3377               {
3378                 if (do_cfi_asm)
3379                   output_cfi_directive (cfi2);
3380                 else
3381                   output_cfi (cfi2, fde, for_eh);
3382               }
3383           }
3384         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3385           {
3386             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3387             cfi_buf = *cfi_cfa;
3388             switch (cfi_cfa_offset->dw_cfi_opc)
3389               {
3390               case DW_CFA_def_cfa_offset:
3391                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3392                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3393                 break;
3394               case DW_CFA_def_cfa_offset_sf:
3395                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3396                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3397                 break;
3398               case DW_CFA_def_cfa:
3399               case DW_CFA_def_cfa_sf:
3400                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3401                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3402                 break;
3403               default:
3404                 gcc_unreachable ();
3405               }
3406             cfi_cfa = &cfi_buf;
3407           }
3408         else if (cfi_cfa_offset)
3409           cfi_cfa = cfi_cfa_offset;
3410         if (cfi_cfa)
3411           {
3412             if (do_cfi_asm)
3413               output_cfi_directive (cfi_cfa);
3414             else
3415               output_cfi (cfi_cfa, fde, for_eh);
3416           }
3417         cfi_cfa = NULL;
3418         cfi_cfa_offset = NULL;
3419         if (cfi_args_size
3420             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3421           {
3422             if (do_cfi_asm)
3423               output_cfi_directive (cfi_args_size);
3424             else
3425               output_cfi (cfi_args_size, fde, for_eh);
3426           }
3427         cfi_args_size = NULL;
3428         if (cfi == NULL)
3429           {
3430             VEC_free (dw_cfi_ref, heap, regs);
3431             return;
3432           }
3433         else if (do_cfi_asm)
3434           output_cfi_directive (cfi);
3435         else
3436           output_cfi (cfi, fde, for_eh);
3437         break;
3438       default:
3439         gcc_unreachable ();
3440     }
3441 }
3442
3443 /* Output one FDE.  */
3444
3445 static void
3446 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3447             char *section_start_label, int fde_encoding, char *augmentation,
3448             bool any_lsda_needed, int lsda_encoding)
3449 {
3450   const char *begin, *end;
3451   static unsigned int j;
3452   char l1[20], l2[20];
3453   dw_cfi_ref cfi;
3454
3455   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3456                                 /* empty */ 0);
3457   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3458                                   for_eh + j);
3459   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3460   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3461   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3462     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3463                          " indicating 64-bit DWARF extension");
3464   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3465                         "FDE Length");
3466   ASM_OUTPUT_LABEL (asm_out_file, l1);
3467
3468   if (for_eh)
3469     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3470   else
3471     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3472                            debug_frame_section, "FDE CIE offset");
3473
3474   if (!fde->dw_fde_switched_sections)
3475     {
3476       begin = fde->dw_fde_begin;
3477       end = fde->dw_fde_end;
3478     }
3479   else
3480     {
3481       /* For the first section, prefer dw_fde_begin over
3482          dw_fde_{hot,cold}_section_label, as the latter
3483          might be separated from the real start of the
3484          function by alignment padding.  */
3485       if (!second)
3486         begin = fde->dw_fde_begin;
3487       else if (fde->dw_fde_switched_cold_to_hot)
3488         begin = fde->dw_fde_hot_section_label;
3489       else
3490         begin = fde->dw_fde_unlikely_section_label;
3491       if (second ^ fde->dw_fde_switched_cold_to_hot)
3492         end = fde->dw_fde_unlikely_section_end_label;
3493       else
3494         end = fde->dw_fde_hot_section_end_label;
3495     }
3496
3497   if (for_eh)
3498     {
3499       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3500       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3501       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3502                                        "FDE initial location");
3503       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3504                             end, begin, "FDE address range");
3505     }
3506   else
3507     {
3508       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3509       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3510     }
3511
3512   if (augmentation[0])
3513     {
3514       if (any_lsda_needed)
3515         {
3516           int size = size_of_encoded_value (lsda_encoding);
3517
3518           if (lsda_encoding == DW_EH_PE_aligned)
3519             {
3520               int offset = (  4         /* Length */
3521                             + 4         /* CIE offset */
3522                             + 2 * size_of_encoded_value (fde_encoding)
3523                             + 1         /* Augmentation size */ );
3524               int pad = -offset & (PTR_SIZE - 1);
3525
3526               size += pad;
3527               gcc_assert (size_of_uleb128 (size) == 1);
3528             }
3529
3530           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3531
3532           if (fde->uses_eh_lsda)
3533             {
3534               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3535                                            fde->funcdef_number);
3536               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3537                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3538                                                false,
3539                                                "Language Specific Data Area");
3540             }
3541           else
3542             {
3543               if (lsda_encoding == DW_EH_PE_aligned)
3544                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3545               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3546                                    "Language Specific Data Area (none)");
3547             }
3548         }
3549       else
3550         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3551     }
3552
3553   /* Loop through the Call Frame Instructions associated with
3554      this FDE.  */
3555   fde->dw_fde_current_label = begin;
3556   if (!fde->dw_fde_switched_sections)
3557     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3558       output_cfi (cfi, fde, for_eh);
3559   else if (!second)
3560     {
3561       if (fde->dw_fde_switch_cfi)
3562         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3563           {
3564             output_cfi (cfi, fde, for_eh);
3565             if (cfi == fde->dw_fde_switch_cfi)
3566               break;
3567           }
3568     }
3569   else
3570     {
3571       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3572
3573       if (fde->dw_fde_switch_cfi)
3574         {
3575           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3576           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3577           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3578           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3579         }
3580       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3581         output_cfi (cfi, fde, for_eh);
3582     }
3583
3584   /* If we are to emit a ref/link from function bodies to their frame tables,
3585      do it now.  This is typically performed to make sure that tables
3586      associated with functions are dragged with them and not discarded in
3587      garbage collecting links. We need to do this on a per function basis to
3588      cope with -ffunction-sections.  */
3589
3590 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3591   /* Switch to the function section, emit the ref to the tables, and
3592      switch *back* into the table section.  */
3593   switch_to_section (function_section (fde->decl));
3594   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3595   switch_to_frame_table_section (for_eh, true);
3596 #endif
3597
3598   /* Pad the FDE out to an address sized boundary.  */
3599   ASM_OUTPUT_ALIGN (asm_out_file,
3600                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3601   ASM_OUTPUT_LABEL (asm_out_file, l2);
3602
3603   j += 2;
3604 }
3605
3606 /* Output the call frame information used to record information
3607    that relates to calculating the frame pointer, and records the
3608    location of saved registers.  */
3609
3610 static void
3611 output_call_frame_info (int for_eh)
3612 {
3613   unsigned int i;
3614   dw_fde_ref fde;
3615   dw_cfi_ref cfi;
3616   char l1[20], l2[20], section_start_label[20];
3617   bool any_lsda_needed = false;
3618   char augmentation[6];
3619   int augmentation_size;
3620   int fde_encoding = DW_EH_PE_absptr;
3621   int per_encoding = DW_EH_PE_absptr;
3622   int lsda_encoding = DW_EH_PE_absptr;
3623   int return_reg;
3624   rtx personality = NULL;
3625   int dw_cie_version;
3626
3627   /* Don't emit a CIE if there won't be any FDEs.  */
3628   if (fde_table_in_use == 0)
3629     return;
3630
3631   /* Nothing to do if the assembler's doing it all.  */
3632   if (dwarf2out_do_cfi_asm ())
3633     return;
3634
3635   /* If we make FDEs linkonce, we may have to emit an empty label for
3636      an FDE that wouldn't otherwise be emitted.  We want to avoid
3637      having an FDE kept around when the function it refers to is
3638      discarded.  Example where this matters: a primary function
3639      template in C++ requires EH information, but an explicit
3640      specialization doesn't.  */
3641   if (TARGET_USES_WEAK_UNWIND_INFO
3642       && ! flag_asynchronous_unwind_tables
3643       && flag_exceptions
3644       && for_eh)
3645     for (i = 0; i < fde_table_in_use; i++)
3646       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3647           && !fde_table[i].uses_eh_lsda
3648           && ! DECL_WEAK (fde_table[i].decl))
3649         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3650                                       for_eh, /* empty */ 1);
3651
3652   /* If we don't have any functions we'll want to unwind out of, don't
3653      emit any EH unwind information.  Note that if exceptions aren't
3654      enabled, we won't have collected nothrow information, and if we
3655      asked for asynchronous tables, we always want this info.  */
3656   if (for_eh)
3657     {
3658       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3659
3660       for (i = 0; i < fde_table_in_use; i++)
3661         if (fde_table[i].uses_eh_lsda)
3662           any_eh_needed = any_lsda_needed = true;
3663         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3664           any_eh_needed = true;
3665         else if (! fde_table[i].nothrow
3666                  && ! fde_table[i].all_throwers_are_sibcalls)
3667           any_eh_needed = true;
3668
3669       if (! any_eh_needed)
3670         return;
3671     }
3672
3673   /* We're going to be generating comments, so turn on app.  */
3674   if (flag_debug_asm)
3675     app_enable ();
3676
3677   /* Switch to the proper frame section, first time.  */
3678   switch_to_frame_table_section (for_eh, false);
3679
3680   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3681   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3682
3683   /* Output the CIE.  */
3684   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3685   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3686   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3687     dw2_asm_output_data (4, 0xffffffff,
3688       "Initial length escape value indicating 64-bit DWARF extension");
3689   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3690                         "Length of Common Information Entry");
3691   ASM_OUTPUT_LABEL (asm_out_file, l1);
3692
3693   /* Now that the CIE pointer is PC-relative for EH,
3694      use 0 to identify the CIE.  */
3695   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3696                        (for_eh ? 0 : DWARF_CIE_ID),
3697                        "CIE Identifier Tag");
3698
3699   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3700      use CIE version 1, unless that would produce incorrect results
3701      due to overflowing the return register column.  */
3702   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3703   dw_cie_version = 1;
3704   if (return_reg >= 256 || dwarf_version > 2)
3705     dw_cie_version = 3;
3706   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3707
3708   augmentation[0] = 0;
3709   augmentation_size = 0;
3710
3711   personality = current_unit_personality;
3712   if (for_eh)
3713     {
3714       char *p;
3715
3716       /* Augmentation:
3717          z      Indicates that a uleb128 is present to size the
3718                 augmentation section.
3719          L      Indicates the encoding (and thus presence) of
3720                 an LSDA pointer in the FDE augmentation.
3721          R      Indicates a non-default pointer encoding for
3722                 FDE code pointers.
3723          P      Indicates the presence of an encoding + language
3724                 personality routine in the CIE augmentation.  */
3725
3726       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3727       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3728       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3729
3730       p = augmentation + 1;
3731       if (personality)
3732         {
3733           *p++ = 'P';
3734           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3735           assemble_external_libcall (personality);
3736         }
3737       if (any_lsda_needed)
3738         {
3739           *p++ = 'L';
3740           augmentation_size += 1;
3741         }
3742       if (fde_encoding != DW_EH_PE_absptr)
3743         {
3744           *p++ = 'R';
3745           augmentation_size += 1;
3746         }
3747       if (p > augmentation + 1)
3748         {
3749           augmentation[0] = 'z';
3750           *p = '\0';
3751         }
3752
3753       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3754       if (personality && per_encoding == DW_EH_PE_aligned)
3755         {
3756           int offset = (  4             /* Length */
3757                         + 4             /* CIE Id */
3758                         + 1             /* CIE version */
3759                         + strlen (augmentation) + 1     /* Augmentation */
3760                         + size_of_uleb128 (1)           /* Code alignment */
3761                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3762                         + 1             /* RA column */
3763                         + 1             /* Augmentation size */
3764                         + 1             /* Personality encoding */ );
3765           int pad = -offset & (PTR_SIZE - 1);
3766
3767           augmentation_size += pad;
3768
3769           /* Augmentations should be small, so there's scarce need to
3770              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3771           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3772         }
3773     }
3774
3775   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3776   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3777   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3778                                "CIE Data Alignment Factor");
3779
3780   if (dw_cie_version == 1)
3781     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3782   else
3783     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3784
3785   if (augmentation[0])
3786     {
3787       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3788       if (personality)
3789         {
3790           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3791                                eh_data_format_name (per_encoding));
3792           dw2_asm_output_encoded_addr_rtx (per_encoding,
3793                                            personality,
3794                                            true, NULL);
3795         }
3796
3797       if (any_lsda_needed)
3798         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3799                              eh_data_format_name (lsda_encoding));
3800
3801       if (fde_encoding != DW_EH_PE_absptr)
3802         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3803                              eh_data_format_name (fde_encoding));
3804     }
3805
3806   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3807     output_cfi (cfi, NULL, for_eh);
3808
3809   /* Pad the CIE out to an address sized boundary.  */
3810   ASM_OUTPUT_ALIGN (asm_out_file,
3811                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3812   ASM_OUTPUT_LABEL (asm_out_file, l2);
3813
3814   /* Loop through all of the FDE's.  */
3815   for (i = 0; i < fde_table_in_use; i++)
3816     {
3817       unsigned int k;
3818       fde = &fde_table[i];
3819
3820       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3821       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3822           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3823           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3824           && !fde->uses_eh_lsda)
3825         continue;
3826
3827       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3828         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3829                     augmentation, any_lsda_needed, lsda_encoding);
3830     }
3831
3832   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3833     dw2_asm_output_data (4, 0, "End of Table");
3834 #ifdef MIPS_DEBUGGING_INFO
3835   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3836      get a value of 0.  Putting .align 0 after the label fixes it.  */
3837   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3838 #endif
3839
3840   /* Turn off app to make assembly quicker.  */
3841   if (flag_debug_asm)
3842     app_disable ();
3843 }
3844
3845 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3846
3847 static void
3848 dwarf2out_do_cfi_startproc (bool second)
3849 {
3850   int enc;
3851   rtx ref;
3852   rtx personality = get_personality_function (current_function_decl);
3853
3854   fprintf (asm_out_file, "\t.cfi_startproc\n");
3855
3856   if (personality)
3857     {
3858       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3859       ref = personality;
3860
3861       /* ??? The GAS support isn't entirely consistent.  We have to
3862          handle indirect support ourselves, but PC-relative is done
3863          in the assembler.  Further, the assembler can't handle any
3864          of the weirder relocation types.  */
3865       if (enc & DW_EH_PE_indirect)
3866         ref = dw2_force_const_mem (ref, true);
3867
3868       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3869       output_addr_const (asm_out_file, ref);
3870       fputc ('\n', asm_out_file);
3871     }
3872
3873   if (crtl->uses_eh_lsda)
3874     {
3875       char lab[20];
3876
3877       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3878       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3879                                    current_function_funcdef_no);
3880       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3881       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3882
3883       if (enc & DW_EH_PE_indirect)
3884         ref = dw2_force_const_mem (ref, true);
3885
3886       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3887       output_addr_const (asm_out_file, ref);
3888       fputc ('\n', asm_out_file);
3889     }
3890 }
3891
3892 /* Output a marker (i.e. a label) for the beginning of a function, before
3893    the prologue.  */
3894
3895 void
3896 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3897                           const char *file ATTRIBUTE_UNUSED)
3898 {
3899   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3900   char * dup_label;
3901   dw_fde_ref fde;
3902   section *fnsec;
3903
3904   current_function_func_begin_label = NULL;
3905
3906 #ifdef TARGET_UNWIND_INFO
3907   /* ??? current_function_func_begin_label is also used by except.c
3908      for call-site information.  We must emit this label if it might
3909      be used.  */
3910   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3911       && ! dwarf2out_do_frame ())
3912     return;
3913 #else
3914   if (! dwarf2out_do_frame ())
3915     return;
3916 #endif
3917
3918   fnsec = function_section (current_function_decl);
3919   switch_to_section (fnsec);
3920   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3921                                current_function_funcdef_no);
3922   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3923                           current_function_funcdef_no);
3924   dup_label = xstrdup (label);
3925   current_function_func_begin_label = dup_label;
3926
3927 #ifdef TARGET_UNWIND_INFO
3928   /* We can elide the fde allocation if we're not emitting debug info.  */
3929   if (! dwarf2out_do_frame ())
3930     return;
3931 #endif
3932
3933   /* Expand the fde table if necessary.  */
3934   if (fde_table_in_use == fde_table_allocated)
3935     {
3936       fde_table_allocated += FDE_TABLE_INCREMENT;
3937       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3938       memset (fde_table + fde_table_in_use, 0,
3939               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3940     }
3941
3942   /* Record the FDE associated with this function.  */
3943   current_funcdef_fde = fde_table_in_use;
3944
3945   /* Add the new FDE at the end of the fde_table.  */
3946   fde = &fde_table[fde_table_in_use++];
3947   fde->decl = current_function_decl;
3948   fde->dw_fde_begin = dup_label;
3949   fde->dw_fde_current_label = dup_label;
3950   fde->dw_fde_hot_section_label = NULL;
3951   fde->dw_fde_hot_section_end_label = NULL;
3952   fde->dw_fde_unlikely_section_label = NULL;
3953   fde->dw_fde_unlikely_section_end_label = NULL;
3954   fde->dw_fde_switched_sections = 0;
3955   fde->dw_fde_switched_cold_to_hot = 0;
3956   fde->dw_fde_end = NULL;
3957   fde->dw_fde_cfi = NULL;
3958   fde->dw_fde_switch_cfi = NULL;
3959   fde->funcdef_number = current_function_funcdef_no;
3960   fde->nothrow = crtl->nothrow;
3961   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3962   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3963   fde->drap_reg = INVALID_REGNUM;
3964   fde->vdrap_reg = INVALID_REGNUM;
3965   if (flag_reorder_blocks_and_partition)
3966     {
3967       section *unlikelysec;
3968       if (first_function_block_is_cold)
3969         fde->in_std_section = 1;
3970       else
3971         fde->in_std_section
3972           = (fnsec == text_section
3973              || (cold_text_section && fnsec == cold_text_section));
3974       unlikelysec = unlikely_text_section ();
3975       fde->cold_in_std_section
3976         = (unlikelysec == text_section
3977            || (cold_text_section && unlikelysec == cold_text_section));
3978     }
3979   else
3980     {
3981       fde->in_std_section
3982         = (fnsec == text_section
3983            || (cold_text_section && fnsec == cold_text_section));
3984       fde->cold_in_std_section = 0;
3985     }
3986
3987   args_size = old_args_size = 0;
3988
3989   /* We only want to output line number information for the genuine dwarf2
3990      prologue case, not the eh frame case.  */
3991 #ifdef DWARF2_DEBUGGING_INFO
3992   if (file)
3993     dwarf2out_source_line (line, file, 0, true);
3994 #endif
3995
3996   if (dwarf2out_do_cfi_asm ())
3997     dwarf2out_do_cfi_startproc (false);
3998   else
3999     {
4000       rtx personality = get_personality_function (current_function_decl);
4001       if (!current_unit_personality)
4002         current_unit_personality = personality;
4003
4004       /* We cannot keep a current personality per function as without CFI
4005          asm at the point where we emit the CFI data there is no current
4006          function anymore.  */
4007       if (personality
4008           && current_unit_personality != personality)
4009         sorry ("Multiple EH personalities are supported only with assemblers "
4010                "supporting .cfi.personality directive.");
4011     }
4012 }
4013
4014 /* Output a marker (i.e. a label) for the absolute end of the generated code
4015    for a function definition.  This gets called *after* the epilogue code has
4016    been generated.  */
4017
4018 void
4019 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4020                         const char *file ATTRIBUTE_UNUSED)
4021 {
4022   dw_fde_ref fde;
4023   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4024
4025 #ifdef DWARF2_DEBUGGING_INFO
4026   last_var_location_insn = NULL_RTX;
4027 #endif
4028
4029   if (dwarf2out_do_cfi_asm ())
4030     fprintf (asm_out_file, "\t.cfi_endproc\n");
4031
4032   /* Output a label to mark the endpoint of the code generated for this
4033      function.  */
4034   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4035                                current_function_funcdef_no);
4036   ASM_OUTPUT_LABEL (asm_out_file, label);
4037   fde = current_fde ();
4038   gcc_assert (fde != NULL);
4039   fde->dw_fde_end = xstrdup (label);
4040 }
4041
4042 void
4043 dwarf2out_frame_init (void)
4044 {
4045   /* Allocate the initial hunk of the fde_table.  */
4046   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4047   fde_table_allocated = FDE_TABLE_INCREMENT;
4048   fde_table_in_use = 0;
4049
4050   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4051      sake of lookup_cfa.  */
4052
4053   /* On entry, the Canonical Frame Address is at SP.  */
4054   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4055
4056 #ifdef DWARF2_UNWIND_INFO
4057   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4058     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4059 #endif
4060 }
4061
4062 void
4063 dwarf2out_frame_finish (void)
4064 {
4065   /* Output call frame information.  */
4066   if (DWARF2_FRAME_INFO)
4067     output_call_frame_info (0);
4068
4069 #ifndef TARGET_UNWIND_INFO
4070   /* Output another copy for the unwinder.  */
4071   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4072     output_call_frame_info (1);
4073 #endif
4074 }
4075
4076 /* Note that the current function section is being used for code.  */
4077
4078 static void
4079 dwarf2out_note_section_used (void)
4080 {
4081   section *sec = current_function_section ();
4082   if (sec == text_section)
4083     text_section_used = true;
4084   else if (sec == cold_text_section)
4085     cold_text_section_used = true;
4086 }
4087
4088 void
4089 dwarf2out_switch_text_section (void)
4090 {
4091   dw_fde_ref fde = current_fde ();
4092
4093   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4094
4095   fde->dw_fde_switched_sections = 1;
4096   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4097
4098   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4099   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4100   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4101   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4102   have_multiple_function_sections = true;
4103
4104   /* Reset the current label on switching text sections, so that we
4105      don't attempt to advance_loc4 between labels in different sections.  */
4106   fde->dw_fde_current_label = NULL;
4107
4108   /* There is no need to mark used sections when not debugging.  */
4109   if (cold_text_section != NULL)
4110     dwarf2out_note_section_used ();
4111
4112   if (dwarf2out_do_cfi_asm ())
4113     fprintf (asm_out_file, "\t.cfi_endproc\n");
4114
4115   /* Now do the real section switch.  */
4116   switch_to_section (current_function_section ());
4117
4118   if (dwarf2out_do_cfi_asm ())
4119     {
4120       dwarf2out_do_cfi_startproc (true);
4121       /* As this is a different FDE, insert all current CFI instructions
4122          again.  */
4123       output_cfis (fde->dw_fde_cfi, true, fde, true);
4124     }
4125   else
4126     {
4127       dw_cfi_ref cfi = fde->dw_fde_cfi;
4128
4129       cfi = fde->dw_fde_cfi;
4130       if (cfi)
4131         while (cfi->dw_cfi_next != NULL)
4132           cfi = cfi->dw_cfi_next;
4133       fde->dw_fde_switch_cfi = cfi;
4134     }
4135 }
4136 #endif
4137 \f
4138 /* And now, the subset of the debugging information support code necessary
4139    for emitting location expressions.  */
4140
4141 /* Data about a single source file.  */
4142 struct GTY(()) dwarf_file_data {
4143   const char * filename;
4144   int emitted_number;
4145 };
4146
4147 typedef struct dw_val_struct *dw_val_ref;
4148 typedef struct die_struct *dw_die_ref;
4149 typedef const struct die_struct *const_dw_die_ref;
4150 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4151 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4152
4153 typedef struct GTY(()) deferred_locations_struct
4154 {
4155   tree variable;
4156   dw_die_ref die;
4157 } deferred_locations;
4158
4159 DEF_VEC_O(deferred_locations);
4160 DEF_VEC_ALLOC_O(deferred_locations,gc);
4161
4162 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4163
4164 DEF_VEC_P(dw_die_ref);
4165 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4166
4167 /* Each DIE may have a series of attribute/value pairs.  Values
4168    can take on several forms.  The forms that are used in this
4169    implementation are listed below.  */
4170
4171 enum dw_val_class
4172 {
4173   dw_val_class_addr,
4174   dw_val_class_offset,
4175   dw_val_class_loc,
4176   dw_val_class_loc_list,
4177   dw_val_class_range_list,
4178   dw_val_class_const,
4179   dw_val_class_unsigned_const,
4180   dw_val_class_const_double,
4181   dw_val_class_vec,
4182   dw_val_class_flag,
4183   dw_val_class_die_ref,
4184   dw_val_class_fde_ref,
4185   dw_val_class_lbl_id,
4186   dw_val_class_lineptr,
4187   dw_val_class_str,
4188   dw_val_class_macptr,
4189   dw_val_class_file,
4190   dw_val_class_data8
4191 };
4192
4193 /* Describe a floating point constant value, or a vector constant value.  */
4194
4195 typedef struct GTY(()) dw_vec_struct {
4196   unsigned char * GTY((length ("%h.length"))) array;
4197   unsigned length;
4198   unsigned elt_size;
4199 }
4200 dw_vec_const;
4201
4202 /* The dw_val_node describes an attribute's value, as it is
4203    represented internally.  */
4204
4205 typedef struct GTY(()) dw_val_struct {
4206   enum dw_val_class val_class;
4207   union dw_val_struct_union
4208     {
4209       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4210       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4211       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4212       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4213       HOST_WIDE_INT GTY ((default)) val_int;
4214       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4215       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4216       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4217       struct dw_val_die_union
4218         {
4219           dw_die_ref die;
4220           int external;
4221         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4222       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4223       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4224       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4225       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4226       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4227       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4228     }
4229   GTY ((desc ("%1.val_class"))) v;
4230 }
4231 dw_val_node;
4232
4233 /* Locations in memory are described using a sequence of stack machine
4234    operations.  */
4235
4236 typedef struct GTY(()) dw_loc_descr_struct {
4237   dw_loc_descr_ref dw_loc_next;
4238   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4239   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4240      from DW_OP_addr with a dtp-relative symbol relocation.  */
4241   unsigned int dtprel : 1;
4242   int dw_loc_addr;
4243   dw_val_node dw_loc_oprnd1;
4244   dw_val_node dw_loc_oprnd2;
4245 }
4246 dw_loc_descr_node;
4247
4248 /* Location lists are ranges + location descriptions for that range,
4249    so you can track variables that are in different places over
4250    their entire life.  */
4251 typedef struct GTY(()) dw_loc_list_struct {
4252   dw_loc_list_ref dw_loc_next;
4253   const char *begin; /* Label for begin address of range */
4254   const char *end;  /* Label for end address of range */
4255   char *ll_symbol; /* Label for beginning of location list.
4256                       Only on head of list */
4257   const char *section; /* Section this loclist is relative to */
4258   dw_loc_descr_ref expr;
4259 } dw_loc_list_node;
4260
4261 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4262
4263 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4264
4265 /* Convert a DWARF stack opcode into its string name.  */
4266
4267 static const char *
4268 dwarf_stack_op_name (unsigned int op)
4269 {
4270   switch (op)
4271     {
4272     case DW_OP_addr:
4273       return "DW_OP_addr";
4274     case DW_OP_deref:
4275       return "DW_OP_deref";
4276     case DW_OP_const1u:
4277       return "DW_OP_const1u";
4278     case DW_OP_const1s:
4279       return "DW_OP_const1s";
4280     case DW_OP_const2u:
4281       return "DW_OP_const2u";
4282     case DW_OP_const2s:
4283       return "DW_OP_const2s";
4284     case DW_OP_const4u:
4285       return "DW_OP_const4u";
4286     case DW_OP_const4s:
4287       return "DW_OP_const4s";
4288     case DW_OP_const8u:
4289       return "DW_OP_const8u";
4290     case DW_OP_const8s:
4291       return "DW_OP_const8s";
4292     case DW_OP_constu:
4293       return "DW_OP_constu";
4294     case DW_OP_consts:
4295       return "DW_OP_consts";
4296     case DW_OP_dup:
4297       return "DW_OP_dup";
4298     case DW_OP_drop:
4299       return "DW_OP_drop";
4300     case DW_OP_over:
4301       return "DW_OP_over";
4302     case DW_OP_pick:
4303       return "DW_OP_pick";
4304     case DW_OP_swap:
4305       return "DW_OP_swap";
4306     case DW_OP_rot:
4307       return "DW_OP_rot";
4308     case DW_OP_xderef:
4309       return "DW_OP_xderef";
4310     case DW_OP_abs:
4311       return "DW_OP_abs";
4312     case DW_OP_and:
4313       return "DW_OP_and";
4314     case DW_OP_div:
4315       return "DW_OP_div";
4316     case DW_OP_minus:
4317       return "DW_OP_minus";
4318     case DW_OP_mod:
4319       return "DW_OP_mod";
4320     case DW_OP_mul:
4321       return "DW_OP_mul";
4322     case DW_OP_neg:
4323       return "DW_OP_neg";
4324     case DW_OP_not:
4325       return "DW_OP_not";
4326     case DW_OP_or:
4327       return "DW_OP_or";
4328     case DW_OP_plus:
4329       return "DW_OP_plus";
4330     case DW_OP_plus_uconst:
4331       return "DW_OP_plus_uconst";
4332     case DW_OP_shl:
4333       return "DW_OP_shl";
4334     case DW_OP_shr:
4335       return "DW_OP_shr";
4336     case DW_OP_shra:
4337       return "DW_OP_shra";
4338     case DW_OP_xor:
4339       return "DW_OP_xor";
4340     case DW_OP_bra:
4341       return "DW_OP_bra";
4342     case DW_OP_eq:
4343       return "DW_OP_eq";
4344     case DW_OP_ge:
4345       return "DW_OP_ge";
4346     case DW_OP_gt:
4347       return "DW_OP_gt";
4348     case DW_OP_le:
4349       return "DW_OP_le";
4350     case DW_OP_lt:
4351       return "DW_OP_lt";
4352     case DW_OP_ne:
4353       return "DW_OP_ne";
4354     case DW_OP_skip:
4355       return "DW_OP_skip";
4356     case DW_OP_lit0:
4357       return "DW_OP_lit0";
4358     case DW_OP_lit1:
4359       return "DW_OP_lit1";
4360     case DW_OP_lit2:
4361       return "DW_OP_lit2";
4362     case DW_OP_lit3:
4363       return "DW_OP_lit3";
4364     case DW_OP_lit4:
4365       return "DW_OP_lit4";
4366     case DW_OP_lit5:
4367       return "DW_OP_lit5";
4368     case DW_OP_lit6:
4369       return "DW_OP_lit6";
4370     case DW_OP_lit7:
4371       return "DW_OP_lit7";
4372     case DW_OP_lit8:
4373       return "DW_OP_lit8";
4374     case DW_OP_lit9:
4375       return "DW_OP_lit9";
4376     case DW_OP_lit10:
4377       return "DW_OP_lit10";
4378     case DW_OP_lit11:
4379       return "DW_OP_lit11";
4380     case DW_OP_lit12:
4381       return "DW_OP_lit12";
4382     case DW_OP_lit13:
4383       return "DW_OP_lit13";
4384     case DW_OP_lit14:
4385       return "DW_OP_lit14";
4386     case DW_OP_lit15:
4387       return "DW_OP_lit15";
4388     case DW_OP_lit16:
4389       return "DW_OP_lit16";
4390     case DW_OP_lit17:
4391       return "DW_OP_lit17";
4392     case DW_OP_lit18:
4393       return "DW_OP_lit18";
4394     case DW_OP_lit19:
4395       return "DW_OP_lit19";
4396     case DW_OP_lit20:
4397       return "DW_OP_lit20";
4398     case DW_OP_lit21:
4399       return "DW_OP_lit21";
4400     case DW_OP_lit22:
4401       return "DW_OP_lit22";
4402     case DW_OP_lit23:
4403       return "DW_OP_lit23";
4404     case DW_OP_lit24:
4405       return "DW_OP_lit24";
4406     case DW_OP_lit25:
4407       return "DW_OP_lit25";
4408     case DW_OP_lit26:
4409       return "DW_OP_lit26";
4410     case DW_OP_lit27:
4411       return "DW_OP_lit27";
4412     case DW_OP_lit28:
4413       return "DW_OP_lit28";
4414     case DW_OP_lit29:
4415       return "DW_OP_lit29";
4416     case DW_OP_lit30:
4417       return "DW_OP_lit30";
4418     case DW_OP_lit31:
4419       return "DW_OP_lit31";
4420     case DW_OP_reg0:
4421       return "DW_OP_reg0";
4422     case DW_OP_reg1:
4423       return "DW_OP_reg1";
4424     case DW_OP_reg2:
4425       return "DW_OP_reg2";
4426     case DW_OP_reg3:
4427       return "DW_OP_reg3";
4428     case DW_OP_reg4:
4429       return "DW_OP_reg4";
4430     case DW_OP_reg5:
4431       return "DW_OP_reg5";
4432     case DW_OP_reg6:
4433       return "DW_OP_reg6";
4434     case DW_OP_reg7:
4435       return "DW_OP_reg7";
4436     case DW_OP_reg8:
4437       return "DW_OP_reg8";
4438     case DW_OP_reg9:
4439       return "DW_OP_reg9";
4440     case DW_OP_reg10:
4441       return "DW_OP_reg10";
4442     case DW_OP_reg11:
4443       return "DW_OP_reg11";
4444     case DW_OP_reg12:
4445       return "DW_OP_reg12";
4446     case DW_OP_reg13:
4447       return "DW_OP_reg13";
4448     case DW_OP_reg14:
4449       return "DW_OP_reg14";
4450     case DW_OP_reg15:
4451       return "DW_OP_reg15";
4452     case DW_OP_reg16:
4453       return "DW_OP_reg16";
4454     case DW_OP_reg17:
4455       return "DW_OP_reg17";
4456     case DW_OP_reg18:
4457       return "DW_OP_reg18";
4458     case DW_OP_reg19:
4459       return "DW_OP_reg19";
4460     case DW_OP_reg20:
4461       return "DW_OP_reg20";
4462     case DW_OP_reg21:
4463       return "DW_OP_reg21";
4464     case DW_OP_reg22:
4465       return "DW_OP_reg22";
4466     case DW_OP_reg23:
4467       return "DW_OP_reg23";
4468     case DW_OP_reg24:
4469       return "DW_OP_reg24";
4470     case DW_OP_reg25:
4471       return "DW_OP_reg25";
4472     case DW_OP_reg26:
4473       return "DW_OP_reg26";
4474     case DW_OP_reg27:
4475       return "DW_OP_reg27";
4476     case DW_OP_reg28:
4477       return "DW_OP_reg28";
4478     case DW_OP_reg29:
4479       return "DW_OP_reg29";
4480     case DW_OP_reg30:
4481       return "DW_OP_reg30";
4482     case DW_OP_reg31:
4483       return "DW_OP_reg31";
4484     case DW_OP_breg0:
4485       return "DW_OP_breg0";
4486     case DW_OP_breg1:
4487       return "DW_OP_breg1";
4488     case DW_OP_breg2:
4489       return "DW_OP_breg2";
4490     case DW_OP_breg3:
4491       return "DW_OP_breg3";
4492     case DW_OP_breg4:
4493       return "DW_OP_breg4";
4494     case DW_OP_breg5:
4495       return "DW_OP_breg5";
4496     case DW_OP_breg6:
4497       return "DW_OP_breg6";
4498     case DW_OP_breg7:
4499       return "DW_OP_breg7";
4500     case DW_OP_breg8:
4501       return "DW_OP_breg8";
4502     case DW_OP_breg9:
4503       return "DW_OP_breg9";
4504     case DW_OP_breg10:
4505       return "DW_OP_breg10";
4506     case DW_OP_breg11:
4507       return "DW_OP_breg11";
4508     case DW_OP_breg12:
4509       return "DW_OP_breg12";
4510     case DW_OP_breg13:
4511       return "DW_OP_breg13";
4512     case DW_OP_breg14:
4513       return "DW_OP_breg14";
4514     case DW_OP_breg15:
4515       return "DW_OP_breg15";
4516     case DW_OP_breg16:
4517       return "DW_OP_breg16";
4518     case DW_OP_breg17:
4519       return "DW_OP_breg17";
4520     case DW_OP_breg18:
4521       return "DW_OP_breg18";
4522     case DW_OP_breg19:
4523       return "DW_OP_breg19";
4524     case DW_OP_breg20:
4525       return "DW_OP_breg20";
4526     case DW_OP_breg21:
4527       return "DW_OP_breg21";
4528     case DW_OP_breg22:
4529       return "DW_OP_breg22";
4530     case DW_OP_breg23:
4531       return "DW_OP_breg23";
4532     case DW_OP_breg24:
4533       return "DW_OP_breg24";
4534     case DW_OP_breg25:
4535       return "DW_OP_breg25";
4536     case DW_OP_breg26:
4537       return "DW_OP_breg26";
4538     case DW_OP_breg27:
4539       return "DW_OP_breg27";
4540     case DW_OP_breg28:
4541       return "DW_OP_breg28";
4542     case DW_OP_breg29:
4543       return "DW_OP_breg29";
4544     case DW_OP_breg30:
4545       return "DW_OP_breg30";
4546     case DW_OP_breg31:
4547       return "DW_OP_breg31";
4548     case DW_OP_regx:
4549       return "DW_OP_regx";
4550     case DW_OP_fbreg:
4551       return "DW_OP_fbreg";
4552     case DW_OP_bregx:
4553       return "DW_OP_bregx";
4554     case DW_OP_piece:
4555       return "DW_OP_piece";
4556     case DW_OP_deref_size:
4557       return "DW_OP_deref_size";
4558     case DW_OP_xderef_size:
4559       return "DW_OP_xderef_size";
4560     case DW_OP_nop:
4561       return "DW_OP_nop";
4562
4563     case DW_OP_push_object_address:
4564       return "DW_OP_push_object_address";
4565     case DW_OP_call2:
4566       return "DW_OP_call2";
4567     case DW_OP_call4:
4568       return "DW_OP_call4";
4569     case DW_OP_call_ref:
4570       return "DW_OP_call_ref";
4571     case DW_OP_implicit_value:
4572       return "DW_OP_implicit_value";
4573     case DW_OP_stack_value:
4574       return "DW_OP_stack_value";
4575     case DW_OP_form_tls_address:
4576       return "DW_OP_form_tls_address";
4577     case DW_OP_call_frame_cfa:
4578       return "DW_OP_call_frame_cfa";
4579     case DW_OP_bit_piece:
4580       return "DW_OP_bit_piece";
4581
4582     case DW_OP_GNU_push_tls_address:
4583       return "DW_OP_GNU_push_tls_address";
4584     case DW_OP_GNU_uninit:
4585       return "DW_OP_GNU_uninit";
4586     case DW_OP_GNU_encoded_addr:
4587       return "DW_OP_GNU_encoded_addr";
4588
4589     default:
4590       return "OP_<unknown>";
4591     }
4592 }
4593
4594 /* Return a pointer to a newly allocated location description.  Location
4595    descriptions are simple expression terms that can be strung
4596    together to form more complicated location (address) descriptions.  */
4597
4598 static inline dw_loc_descr_ref
4599 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4600                unsigned HOST_WIDE_INT oprnd2)
4601 {
4602   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4603
4604   descr->dw_loc_opc = op;
4605   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4606   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4607   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4608   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4609
4610   return descr;
4611 }
4612
4613 /* Return a pointer to a newly allocated location description for
4614    REG and OFFSET.  */
4615
4616 static inline dw_loc_descr_ref
4617 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4618 {
4619   if (reg <= 31)
4620     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4621                           offset, 0);
4622   else
4623     return new_loc_descr (DW_OP_bregx, reg, offset);
4624 }
4625
4626 /* Add a location description term to a location description expression.  */
4627
4628 static inline void
4629 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4630 {
4631   dw_loc_descr_ref *d;
4632
4633   /* Find the end of the chain.  */
4634   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4635     ;
4636
4637   *d = descr;
4638 }
4639
4640 /* Add a constant OFFSET to a location expression.  */
4641
4642 static void
4643 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4644 {
4645   dw_loc_descr_ref loc;
4646   HOST_WIDE_INT *p;
4647
4648   gcc_assert (*list_head != NULL);
4649
4650   if (!offset)
4651     return;
4652
4653   /* Find the end of the chain.  */
4654   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4655     ;
4656
4657   p = NULL;
4658   if (loc->dw_loc_opc == DW_OP_fbreg
4659       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4660     p = &loc->dw_loc_oprnd1.v.val_int;
4661   else if (loc->dw_loc_opc == DW_OP_bregx)
4662     p = &loc->dw_loc_oprnd2.v.val_int;
4663
4664   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4665      offset.  Don't optimize if an signed integer overflow would happen.  */
4666   if (p != NULL
4667       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4668           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4669     *p += offset;
4670
4671   else if (offset > 0)
4672     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4673
4674   else
4675     {
4676       loc->dw_loc_next = int_loc_descriptor (offset);
4677       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4678     }
4679 }
4680
4681 #ifdef DWARF2_DEBUGGING_INFO
4682 /* Add a constant OFFSET to a location list.  */
4683
4684 static void
4685 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4686 {
4687   dw_loc_list_ref d;
4688   for (d = list_head; d != NULL; d = d->dw_loc_next)
4689     loc_descr_plus_const (&d->expr, offset);
4690 }
4691 #endif
4692
4693 /* Return the size of a location descriptor.  */
4694
4695 static unsigned long
4696 size_of_loc_descr (dw_loc_descr_ref loc)
4697 {
4698   unsigned long size = 1;
4699
4700   switch (loc->dw_loc_opc)
4701     {
4702     case DW_OP_addr:
4703       size += DWARF2_ADDR_SIZE;
4704       break;
4705     case DW_OP_const1u:
4706     case DW_OP_const1s:
4707       size += 1;
4708       break;
4709     case DW_OP_const2u:
4710     case DW_OP_const2s:
4711       size += 2;
4712       break;
4713     case DW_OP_const4u:
4714     case DW_OP_const4s:
4715       size += 4;
4716       break;
4717     case DW_OP_const8u:
4718     case DW_OP_const8s:
4719       size += 8;
4720       break;
4721     case DW_OP_constu:
4722       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4723       break;
4724     case DW_OP_consts:
4725       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4726       break;
4727     case DW_OP_pick:
4728       size += 1;
4729       break;
4730     case DW_OP_plus_uconst:
4731       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4732       break;
4733     case DW_OP_skip:
4734     case DW_OP_bra:
4735       size += 2;
4736       break;
4737     case DW_OP_breg0:
4738     case DW_OP_breg1:
4739     case DW_OP_breg2:
4740     case DW_OP_breg3:
4741     case DW_OP_breg4:
4742     case DW_OP_breg5:
4743     case DW_OP_breg6:
4744     case DW_OP_breg7:
4745     case DW_OP_breg8:
4746     case DW_OP_breg9:
4747     case DW_OP_breg10:
4748     case DW_OP_breg11:
4749     case DW_OP_breg12:
4750     case DW_OP_breg13:
4751     case DW_OP_breg14:
4752     case DW_OP_breg15:
4753     case DW_OP_breg16:
4754     case DW_OP_breg17:
4755     case DW_OP_breg18:
4756     case DW_OP_breg19:
4757     case DW_OP_breg20:
4758     case DW_OP_breg21:
4759     case DW_OP_breg22:
4760     case DW_OP_breg23:
4761     case DW_OP_breg24:
4762     case DW_OP_breg25:
4763     case DW_OP_breg26:
4764     case DW_OP_breg27:
4765     case DW_OP_breg28:
4766     case DW_OP_breg29:
4767     case DW_OP_breg30:
4768     case DW_OP_breg31:
4769       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4770       break;
4771     case DW_OP_regx:
4772       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4773       break;
4774     case DW_OP_fbreg:
4775       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4776       break;
4777     case DW_OP_bregx:
4778       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4779       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4780       break;
4781     case DW_OP_piece:
4782       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4783       break;
4784     case DW_OP_deref_size:
4785     case DW_OP_xderef_size:
4786       size += 1;
4787       break;
4788     case DW_OP_call2:
4789       size += 2;
4790       break;
4791     case DW_OP_call4:
4792       size += 4;
4793       break;
4794     case DW_OP_call_ref:
4795       size += DWARF2_ADDR_SIZE;
4796       break;
4797     case DW_OP_implicit_value:
4798       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4799               + loc->dw_loc_oprnd1.v.val_unsigned;
4800       break;
4801     default:
4802       break;
4803     }
4804
4805   return size;
4806 }
4807
4808 /* Return the size of a series of location descriptors.  */
4809
4810 static unsigned long
4811 size_of_locs (dw_loc_descr_ref loc)
4812 {
4813   dw_loc_descr_ref l;
4814   unsigned long size;
4815
4816   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4817      field, to avoid writing to a PCH file.  */
4818   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4819     {
4820       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4821         break;
4822       size += size_of_loc_descr (l);
4823     }
4824   if (! l)
4825     return size;
4826
4827   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4828     {
4829       l->dw_loc_addr = size;
4830       size += size_of_loc_descr (l);
4831     }
4832
4833   return size;
4834 }
4835
4836 #ifdef DWARF2_DEBUGGING_INFO
4837 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4838 #endif
4839
4840 /* Output location description stack opcode's operands (if any).  */
4841
4842 static void
4843 output_loc_operands (dw_loc_descr_ref loc)
4844 {
4845   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4846   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4847
4848   switch (loc->dw_loc_opc)
4849     {
4850 #ifdef DWARF2_DEBUGGING_INFO
4851     case DW_OP_const2u:
4852     case DW_OP_const2s:
4853       dw2_asm_output_data (2, val1->v.val_int, NULL);
4854       break;
4855     case DW_OP_const4u:
4856     case DW_OP_const4s:
4857       dw2_asm_output_data (4, val1->v.val_int, NULL);
4858       break;
4859     case DW_OP_const8u:
4860     case DW_OP_const8s:
4861       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4862       dw2_asm_output_data (8, val1->v.val_int, NULL);
4863       break;
4864     case DW_OP_skip:
4865     case DW_OP_bra:
4866       {
4867         int offset;
4868
4869         gcc_assert (val1->val_class == dw_val_class_loc);
4870         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4871
4872         dw2_asm_output_data (2, offset, NULL);
4873       }
4874       break;
4875     case DW_OP_implicit_value:
4876       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4877       switch (val2->val_class)
4878         {
4879         case dw_val_class_const:
4880           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4881           break;
4882         case dw_val_class_vec:
4883           {
4884             unsigned int elt_size = val2->v.val_vec.elt_size;
4885             unsigned int len = val2->v.val_vec.length;
4886             unsigned int i;
4887             unsigned char *p;
4888
4889             if (elt_size > sizeof (HOST_WIDE_INT))
4890               {
4891                 elt_size /= 2;
4892                 len *= 2;
4893               }
4894             for (i = 0, p = val2->v.val_vec.array;
4895                  i < len;
4896                  i++, p += elt_size)
4897               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4898                                    "fp or vector constant word %u", i);
4899           }
4900           break;
4901         case dw_val_class_const_double:
4902           {
4903             unsigned HOST_WIDE_INT first, second;
4904
4905             if (WORDS_BIG_ENDIAN)
4906               {
4907                 first = val2->v.val_double.high;
4908                 second = val2->v.val_double.low;
4909               }
4910             else
4911               {
4912                 first = val2->v.val_double.low;
4913                 second = val2->v.val_double.high;
4914               }
4915             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4916                                  first, NULL);
4917             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4918                                  second, NULL);
4919           }
4920           break;
4921         case dw_val_class_addr:
4922           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4923           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4924           break;
4925         default:
4926           gcc_unreachable ();
4927         }
4928       break;
4929 #else
4930     case DW_OP_const2u:
4931     case DW_OP_const2s:
4932     case DW_OP_const4u:
4933     case DW_OP_const4s:
4934     case DW_OP_const8u:
4935     case DW_OP_const8s:
4936     case DW_OP_skip:
4937     case DW_OP_bra:
4938     case DW_OP_implicit_value:
4939       /* We currently don't make any attempt to make sure these are
4940          aligned properly like we do for the main unwind info, so
4941          don't support emitting things larger than a byte if we're
4942          only doing unwinding.  */
4943       gcc_unreachable ();
4944 #endif
4945     case DW_OP_const1u:
4946     case DW_OP_const1s:
4947       dw2_asm_output_data (1, val1->v.val_int, NULL);
4948       break;
4949     case DW_OP_constu:
4950       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4951       break;
4952     case DW_OP_consts:
4953       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4954       break;
4955     case DW_OP_pick:
4956       dw2_asm_output_data (1, val1->v.val_int, NULL);
4957       break;
4958     case DW_OP_plus_uconst:
4959       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4960       break;
4961     case DW_OP_breg0:
4962     case DW_OP_breg1:
4963     case DW_OP_breg2:
4964     case DW_OP_breg3:
4965     case DW_OP_breg4:
4966     case DW_OP_breg5:
4967     case DW_OP_breg6:
4968     case DW_OP_breg7:
4969     case DW_OP_breg8:
4970     case DW_OP_breg9:
4971     case DW_OP_breg10:
4972     case DW_OP_breg11:
4973     case DW_OP_breg12:
4974     case DW_OP_breg13:
4975     case DW_OP_breg14:
4976     case DW_OP_breg15:
4977     case DW_OP_breg16:
4978     case DW_OP_breg17:
4979     case DW_OP_breg18:
4980     case DW_OP_breg19:
4981     case DW_OP_breg20:
4982     case DW_OP_breg21:
4983     case DW_OP_breg22:
4984     case DW_OP_breg23:
4985     case DW_OP_breg24:
4986     case DW_OP_breg25:
4987     case DW_OP_breg26:
4988     case DW_OP_breg27:
4989     case DW_OP_breg28:
4990     case DW_OP_breg29:
4991     case DW_OP_breg30:
4992     case DW_OP_breg31:
4993       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4994       break;
4995     case DW_OP_regx:
4996       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4997       break;
4998     case DW_OP_fbreg:
4999       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5000       break;
5001     case DW_OP_bregx:
5002       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5003       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5004       break;
5005     case DW_OP_piece:
5006       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5007       break;
5008     case DW_OP_deref_size:
5009     case DW_OP_xderef_size:
5010       dw2_asm_output_data (1, val1->v.val_int, NULL);
5011       break;
5012
5013     case DW_OP_addr:
5014       if (loc->dtprel)
5015         {
5016           if (targetm.asm_out.output_dwarf_dtprel)
5017             {
5018               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5019                                                    DWARF2_ADDR_SIZE,
5020                                                    val1->v.val_addr);
5021               fputc ('\n', asm_out_file);
5022             }
5023           else
5024             gcc_unreachable ();
5025         }
5026       else
5027         {
5028 #ifdef DWARF2_DEBUGGING_INFO
5029           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5030 #else
5031           gcc_unreachable ();
5032 #endif
5033         }
5034       break;
5035
5036     default:
5037       /* Other codes have no operands.  */
5038       break;
5039     }
5040 }
5041
5042 /* Output a sequence of location operations.  */
5043
5044 static void
5045 output_loc_sequence (dw_loc_descr_ref loc)
5046 {
5047   for (; loc != NULL; loc = loc->dw_loc_next)
5048     {
5049       /* Output the opcode.  */
5050       dw2_asm_output_data (1, loc->dw_loc_opc,
5051                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5052
5053       /* Output the operand(s) (if any).  */
5054       output_loc_operands (loc);
5055     }
5056 }
5057
5058 /* Output location description stack opcode's operands (if any).
5059    The output is single bytes on a line, suitable for .cfi_escape.  */
5060
5061 static void
5062 output_loc_operands_raw (dw_loc_descr_ref loc)
5063 {
5064   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5065   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5066
5067   switch (loc->dw_loc_opc)
5068     {
5069     case DW_OP_addr:
5070     case DW_OP_implicit_value:
5071       /* We cannot output addresses in .cfi_escape, only bytes.  */
5072       gcc_unreachable ();
5073
5074     case DW_OP_const1u:
5075     case DW_OP_const1s:
5076     case DW_OP_pick:
5077     case DW_OP_deref_size:
5078     case DW_OP_xderef_size:
5079       fputc (',', asm_out_file);
5080       dw2_asm_output_data_raw (1, val1->v.val_int);
5081       break;
5082
5083     case DW_OP_const2u:
5084     case DW_OP_const2s:
5085       fputc (',', asm_out_file);
5086       dw2_asm_output_data_raw (2, val1->v.val_int);
5087       break;
5088
5089     case DW_OP_const4u:
5090     case DW_OP_const4s:
5091       fputc (',', asm_out_file);
5092       dw2_asm_output_data_raw (4, val1->v.val_int);
5093       break;
5094
5095     case DW_OP_const8u:
5096     case DW_OP_const8s:
5097       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5098       fputc (',', asm_out_file);
5099       dw2_asm_output_data_raw (8, val1->v.val_int);
5100       break;
5101
5102     case DW_OP_skip:
5103     case DW_OP_bra:
5104       {
5105         int offset;
5106
5107         gcc_assert (val1->val_class == dw_val_class_loc);
5108         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5109
5110         fputc (',', asm_out_file);
5111         dw2_asm_output_data_raw (2, offset);
5112       }
5113       break;
5114
5115     case DW_OP_constu:
5116     case DW_OP_plus_uconst:
5117     case DW_OP_regx:
5118     case DW_OP_piece:
5119       fputc (',', asm_out_file);
5120       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5121       break;
5122
5123     case DW_OP_consts:
5124     case DW_OP_breg0:
5125     case DW_OP_breg1:
5126     case DW_OP_breg2:
5127     case DW_OP_breg3:
5128     case DW_OP_breg4:
5129     case DW_OP_breg5:
5130     case DW_OP_breg6:
5131     case DW_OP_breg7:
5132     case DW_OP_breg8:
5133     case DW_OP_breg9:
5134     case DW_OP_breg10:
5135     case DW_OP_breg11:
5136     case DW_OP_breg12:
5137     case DW_OP_breg13:
5138     case DW_OP_breg14:
5139     case DW_OP_breg15:
5140     case DW_OP_breg16:
5141     case DW_OP_breg17:
5142     case DW_OP_breg18:
5143     case DW_OP_breg19:
5144     case DW_OP_breg20:
5145     case DW_OP_breg21:
5146     case DW_OP_breg22:
5147     case DW_OP_breg23:
5148     case DW_OP_breg24:
5149     case DW_OP_breg25:
5150     case DW_OP_breg26:
5151     case DW_OP_breg27:
5152     case DW_OP_breg28:
5153     case DW_OP_breg29:
5154     case DW_OP_breg30:
5155     case DW_OP_breg31:
5156     case DW_OP_fbreg:
5157       fputc (',', asm_out_file);
5158       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5159       break;
5160
5161     case DW_OP_bregx:
5162       fputc (',', asm_out_file);
5163       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5164       fputc (',', asm_out_file);
5165       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5166       break;
5167
5168     default:
5169       /* Other codes have no operands.  */
5170       break;
5171     }
5172 }
5173
5174 static void
5175 output_loc_sequence_raw (dw_loc_descr_ref loc)
5176 {
5177   while (1)
5178     {
5179       /* Output the opcode.  */
5180       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5181       output_loc_operands_raw (loc);
5182
5183       if (!loc->dw_loc_next)
5184         break;
5185       loc = loc->dw_loc_next;
5186
5187       fputc (',', asm_out_file);
5188     }
5189 }
5190
5191 /* This routine will generate the correct assembly data for a location
5192    description based on a cfi entry with a complex address.  */
5193
5194 static void
5195 output_cfa_loc (dw_cfi_ref cfi)
5196 {
5197   dw_loc_descr_ref loc;
5198   unsigned long size;
5199
5200   if (cfi->dw_cfi_opc == DW_CFA_expression)
5201     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5202
5203   /* Output the size of the block.  */
5204   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5205   size = size_of_locs (loc);
5206   dw2_asm_output_data_uleb128 (size, NULL);
5207
5208   /* Now output the operations themselves.  */
5209   output_loc_sequence (loc);
5210 }
5211
5212 /* Similar, but used for .cfi_escape.  */
5213
5214 static void
5215 output_cfa_loc_raw (dw_cfi_ref cfi)
5216 {
5217   dw_loc_descr_ref loc;
5218   unsigned long size;
5219
5220   if (cfi->dw_cfi_opc == DW_CFA_expression)
5221     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5222
5223   /* Output the size of the block.  */
5224   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5225   size = size_of_locs (loc);
5226   dw2_asm_output_data_uleb128_raw (size);
5227   fputc (',', asm_out_file);
5228
5229   /* Now output the operations themselves.  */
5230   output_loc_sequence_raw (loc);
5231 }
5232
5233 /* This function builds a dwarf location descriptor sequence from a
5234    dw_cfa_location, adding the given OFFSET to the result of the
5235    expression.  */
5236
5237 static struct dw_loc_descr_struct *
5238 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5239 {
5240   struct dw_loc_descr_struct *head, *tmp;
5241
5242   offset += cfa->offset;
5243
5244   if (cfa->indirect)
5245     {
5246       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5247       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5248       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5249       add_loc_descr (&head, tmp);
5250       if (offset != 0)
5251         {
5252           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5253           add_loc_descr (&head, tmp);
5254         }
5255     }
5256   else
5257     head = new_reg_loc_descr (cfa->reg, offset);
5258
5259   return head;
5260 }
5261
5262 /* This function builds a dwarf location descriptor sequence for
5263    the address at OFFSET from the CFA when stack is aligned to
5264    ALIGNMENT byte.  */
5265
5266 static struct dw_loc_descr_struct *
5267 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5268 {
5269   struct dw_loc_descr_struct *head;
5270   unsigned int dwarf_fp
5271     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5272
5273  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5274   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5275     {
5276       head = new_reg_loc_descr (dwarf_fp, 0);
5277       add_loc_descr (&head, int_loc_descriptor (alignment));
5278       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5279       loc_descr_plus_const (&head, offset);
5280     }
5281   else
5282     head = new_reg_loc_descr (dwarf_fp, offset);
5283   return head;
5284 }
5285
5286 /* This function fills in aa dw_cfa_location structure from a dwarf location
5287    descriptor sequence.  */
5288
5289 static void
5290 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5291 {
5292   struct dw_loc_descr_struct *ptr;
5293   cfa->offset = 0;
5294   cfa->base_offset = 0;
5295   cfa->indirect = 0;
5296   cfa->reg = -1;
5297
5298   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5299     {
5300       enum dwarf_location_atom op = ptr->dw_loc_opc;
5301
5302       switch (op)
5303         {
5304         case DW_OP_reg0:
5305         case DW_OP_reg1:
5306         case DW_OP_reg2:
5307         case DW_OP_reg3:
5308         case DW_OP_reg4:
5309         case DW_OP_reg5:
5310         case DW_OP_reg6:
5311         case DW_OP_reg7:
5312         case DW_OP_reg8:
5313         case DW_OP_reg9:
5314         case DW_OP_reg10:
5315         case DW_OP_reg11:
5316         case DW_OP_reg12:
5317         case DW_OP_reg13:
5318         case DW_OP_reg14:
5319         case DW_OP_reg15:
5320         case DW_OP_reg16:
5321         case DW_OP_reg17:
5322         case DW_OP_reg18:
5323         case DW_OP_reg19:
5324         case DW_OP_reg20:
5325         case DW_OP_reg21:
5326         case DW_OP_reg22:
5327         case DW_OP_reg23:
5328         case DW_OP_reg24:
5329         case DW_OP_reg25:
5330         case DW_OP_reg26:
5331         case DW_OP_reg27:
5332         case DW_OP_reg28:
5333         case DW_OP_reg29:
5334         case DW_OP_reg30:
5335         case DW_OP_reg31:
5336           cfa->reg = op - DW_OP_reg0;
5337           break;
5338         case DW_OP_regx:
5339           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5340           break;
5341         case DW_OP_breg0:
5342         case DW_OP_breg1:
5343         case DW_OP_breg2:
5344         case DW_OP_breg3:
5345         case DW_OP_breg4:
5346         case DW_OP_breg5:
5347         case DW_OP_breg6:
5348         case DW_OP_breg7:
5349         case DW_OP_breg8:
5350         case DW_OP_breg9:
5351         case DW_OP_breg10:
5352         case DW_OP_breg11:
5353         case DW_OP_breg12:
5354         case DW_OP_breg13:
5355         case DW_OP_breg14:
5356         case DW_OP_breg15:
5357         case DW_OP_breg16:
5358         case DW_OP_breg17:
5359         case DW_OP_breg18:
5360         case DW_OP_breg19:
5361         case DW_OP_breg20:
5362         case DW_OP_breg21:
5363         case DW_OP_breg22:
5364         case DW_OP_breg23:
5365         case DW_OP_breg24:
5366         case DW_OP_breg25:
5367         case DW_OP_breg26:
5368         case DW_OP_breg27:
5369         case DW_OP_breg28:
5370         case DW_OP_breg29:
5371         case DW_OP_breg30:
5372         case DW_OP_breg31:
5373           cfa->reg = op - DW_OP_breg0;
5374           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5375           break;
5376         case DW_OP_bregx:
5377           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5378           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5379           break;
5380         case DW_OP_deref:
5381           cfa->indirect = 1;
5382           break;
5383         case DW_OP_plus_uconst:
5384           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5385           break;
5386         default:
5387           internal_error ("DW_LOC_OP %s not implemented",
5388                           dwarf_stack_op_name (ptr->dw_loc_opc));
5389         }
5390     }
5391 }
5392 #endif /* .debug_frame support */
5393 \f
5394 /* And now, the support for symbolic debugging information.  */
5395 #ifdef DWARF2_DEBUGGING_INFO
5396
5397 /* .debug_str support.  */
5398 static int output_indirect_string (void **, void *);
5399
5400 static void dwarf2out_init (const char *);
5401 static void dwarf2out_finish (const char *);
5402 static void dwarf2out_define (unsigned int, const char *);
5403 static void dwarf2out_undef (unsigned int, const char *);
5404 static void dwarf2out_start_source_file (unsigned, const char *);
5405 static void dwarf2out_end_source_file (unsigned);
5406 static void dwarf2out_begin_block (unsigned, unsigned);
5407 static void dwarf2out_end_block (unsigned, unsigned);
5408 static bool dwarf2out_ignore_block (const_tree);
5409 static void dwarf2out_global_decl (tree);
5410 static void dwarf2out_type_decl (tree, int);
5411 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5412 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5413                                                  dw_die_ref);
5414 static void dwarf2out_abstract_function (tree);
5415 static void dwarf2out_var_location (rtx);
5416 static void dwarf2out_begin_function (tree);
5417 static void dwarf2out_set_name (tree, tree);
5418
5419 /* The debug hooks structure.  */
5420
5421 const struct gcc_debug_hooks dwarf2_debug_hooks =
5422 {
5423   dwarf2out_init,
5424   dwarf2out_finish,
5425   dwarf2out_define,
5426   dwarf2out_undef,
5427   dwarf2out_start_source_file,
5428   dwarf2out_end_source_file,
5429   dwarf2out_begin_block,
5430   dwarf2out_end_block,
5431   dwarf2out_ignore_block,
5432   dwarf2out_source_line,
5433   dwarf2out_begin_prologue,
5434   debug_nothing_int_charstar,   /* end_prologue */
5435   dwarf2out_end_epilogue,
5436   dwarf2out_begin_function,
5437   debug_nothing_int,            /* end_function */
5438   dwarf2out_decl,               /* function_decl */
5439   dwarf2out_global_decl,
5440   dwarf2out_type_decl,          /* type_decl */
5441   dwarf2out_imported_module_or_decl,
5442   debug_nothing_tree,           /* deferred_inline_function */
5443   /* The DWARF 2 backend tries to reduce debugging bloat by not
5444      emitting the abstract description of inline functions until
5445      something tries to reference them.  */
5446   dwarf2out_abstract_function,  /* outlining_inline_function */
5447   debug_nothing_rtx,            /* label */
5448   debug_nothing_int,            /* handle_pch */
5449   dwarf2out_var_location,
5450   dwarf2out_switch_text_section,
5451   dwarf2out_set_name,
5452   1                             /* start_end_main_source_file */
5453 };
5454 #endif
5455 \f
5456 /* NOTE: In the comments in this file, many references are made to
5457    "Debugging Information Entries".  This term is abbreviated as `DIE'
5458    throughout the remainder of this file.  */
5459
5460 /* An internal representation of the DWARF output is built, and then
5461    walked to generate the DWARF debugging info.  The walk of the internal
5462    representation is done after the entire program has been compiled.
5463    The types below are used to describe the internal representation.  */
5464
5465 /* Various DIE's use offsets relative to the beginning of the
5466    .debug_info section to refer to each other.  */
5467
5468 typedef long int dw_offset;
5469
5470 /* Define typedefs here to avoid circular dependencies.  */
5471
5472 typedef struct dw_attr_struct *dw_attr_ref;
5473 typedef struct dw_line_info_struct *dw_line_info_ref;
5474 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5475 typedef struct pubname_struct *pubname_ref;
5476 typedef struct dw_ranges_struct *dw_ranges_ref;
5477 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5478 typedef struct comdat_type_struct *comdat_type_node_ref;
5479
5480 /* Each entry in the line_info_table maintains the file and
5481    line number associated with the label generated for that
5482    entry.  The label gives the PC value associated with
5483    the line number entry.  */
5484
5485 typedef struct GTY(()) dw_line_info_struct {
5486   unsigned long dw_file_num;
5487   unsigned long dw_line_num;
5488 }
5489 dw_line_info_entry;
5490
5491 /* Line information for functions in separate sections; each one gets its
5492    own sequence.  */
5493 typedef struct GTY(()) dw_separate_line_info_struct {
5494   unsigned long dw_file_num;
5495   unsigned long dw_line_num;
5496   unsigned long function;
5497 }
5498 dw_separate_line_info_entry;
5499
5500 /* Each DIE attribute has a field specifying the attribute kind,
5501    a link to the next attribute in the chain, and an attribute value.
5502    Attributes are typically linked below the DIE they modify.  */
5503
5504 typedef struct GTY(()) dw_attr_struct {
5505   enum dwarf_attribute dw_attr;
5506   dw_val_node dw_attr_val;
5507 }
5508 dw_attr_node;
5509
5510 DEF_VEC_O(dw_attr_node);
5511 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5512
5513 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5514    The children of each node form a circular list linked by
5515    die_sib.  die_child points to the node *before* the "first" child node.  */
5516
5517 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5518   enum dwarf_tag die_tag;
5519   union die_symbol_or_type_node
5520     {
5521       char * GTY ((tag ("0"))) die_symbol;
5522       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5523     }
5524   GTY ((desc ("dwarf_version >= 4"))) die_id;
5525   VEC(dw_attr_node,gc) * die_attr;
5526   dw_die_ref die_parent;
5527   dw_die_ref die_child;
5528   dw_die_ref die_sib;
5529   dw_die_ref die_definition; /* ref from a specification to its definition */
5530   dw_offset die_offset;
5531   unsigned long die_abbrev;
5532   int die_mark;
5533   /* Die is used and must not be pruned as unused.  */
5534   int die_perennial_p;
5535   unsigned int decl_id;
5536 }
5537 die_node;
5538
5539 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5540 #define FOR_EACH_CHILD(die, c, expr) do {       \
5541   c = die->die_child;                           \
5542   if (c) do {                                   \
5543     c = c->die_sib;                             \
5544     expr;                                       \
5545   } while (c != die->die_child);                \
5546 } while (0)
5547
5548 /* The pubname structure */
5549
5550 typedef struct GTY(()) pubname_struct {
5551   dw_die_ref die;
5552   const char *name;
5553 }
5554 pubname_entry;
5555
5556 DEF_VEC_O(pubname_entry);
5557 DEF_VEC_ALLOC_O(pubname_entry, gc);
5558
5559 struct GTY(()) dw_ranges_struct {
5560   /* If this is positive, it's a block number, otherwise it's a
5561      bitwise-negated index into dw_ranges_by_label.  */
5562   int num;
5563 };
5564
5565 struct GTY(()) dw_ranges_by_label_struct {
5566   const char *begin;
5567   const char *end;
5568 };
5569
5570 /* The comdat type node structure.  */
5571 typedef struct GTY(()) comdat_type_struct
5572 {
5573   dw_die_ref root_die;
5574   dw_die_ref type_die;
5575   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5576   struct comdat_type_struct *next;
5577 }
5578 comdat_type_node;
5579
5580 /* The limbo die list structure.  */
5581 typedef struct GTY(()) limbo_die_struct {
5582   dw_die_ref die;
5583   tree created_for;
5584   struct limbo_die_struct *next;
5585 }
5586 limbo_die_node;
5587
5588 typedef struct GTY(()) skeleton_chain_struct
5589 {
5590   dw_die_ref old_die;
5591   dw_die_ref new_die;
5592   struct skeleton_chain_struct *parent;
5593 }
5594 skeleton_chain_node;
5595
5596 /* How to start an assembler comment.  */
5597 #ifndef ASM_COMMENT_START
5598 #define ASM_COMMENT_START ";#"
5599 #endif
5600
5601 /* Define a macro which returns nonzero for a TYPE_DECL which was
5602    implicitly generated for a tagged type.
5603
5604    Note that unlike the gcc front end (which generates a NULL named
5605    TYPE_DECL node for each complete tagged type, each array type, and
5606    each function type node created) the g++ front end generates a
5607    _named_ TYPE_DECL node for each tagged type node created.
5608    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5609    generate a DW_TAG_typedef DIE for them.  */
5610
5611 #define TYPE_DECL_IS_STUB(decl)                         \
5612   (DECL_NAME (decl) == NULL_TREE                        \
5613    || (DECL_ARTIFICIAL (decl)                           \
5614        && is_tagged_type (TREE_TYPE (decl))             \
5615        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5616            /* This is necessary for stub decls that     \
5617               appear in nested inline functions.  */    \
5618            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5619                && (decl_ultimate_origin (decl)          \
5620                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5621
5622 /* Information concerning the compilation unit's programming
5623    language, and compiler version.  */
5624
5625 /* Fixed size portion of the DWARF compilation unit header.  */
5626 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5627   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5628
5629 /* Fixed size portion of the DWARF comdat type unit header.  */
5630 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5631   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5632    + DWARF_OFFSET_SIZE)
5633
5634 /* Fixed size portion of public names info.  */
5635 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5636
5637 /* Fixed size portion of the address range info.  */
5638 #define DWARF_ARANGES_HEADER_SIZE                                       \
5639   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5640                 DWARF2_ADDR_SIZE * 2)                                   \
5641    - DWARF_INITIAL_LENGTH_SIZE)
5642
5643 /* Size of padding portion in the address range info.  It must be
5644    aligned to twice the pointer size.  */
5645 #define DWARF_ARANGES_PAD_SIZE \
5646   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5647                 DWARF2_ADDR_SIZE * 2)                              \
5648    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5649
5650 /* Use assembler line directives if available.  */
5651 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5652 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5653 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5654 #else
5655 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5656 #endif
5657 #endif
5658
5659 /* Minimum line offset in a special line info. opcode.
5660    This value was chosen to give a reasonable range of values.  */
5661 #define DWARF_LINE_BASE  -10
5662
5663 /* First special line opcode - leave room for the standard opcodes.  */
5664 #define DWARF_LINE_OPCODE_BASE  10
5665
5666 /* Range of line offsets in a special line info. opcode.  */
5667 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5668
5669 /* Flag that indicates the initial value of the is_stmt_start flag.
5670    In the present implementation, we do not mark any lines as
5671    the beginning of a source statement, because that information
5672    is not made available by the GCC front-end.  */
5673 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5674
5675 #ifdef DWARF2_DEBUGGING_INFO
5676 /* This location is used by calc_die_sizes() to keep track
5677    the offset of each DIE within the .debug_info section.  */
5678 static unsigned long next_die_offset;
5679 #endif
5680
5681 /* Record the root of the DIE's built for the current compilation unit.  */
5682 static GTY(()) dw_die_ref comp_unit_die;
5683
5684 /* A list of type DIEs that have been separated into comdat sections.  */
5685 static GTY(()) comdat_type_node *comdat_type_list;
5686
5687 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5688 static GTY(()) limbo_die_node *limbo_die_list;
5689
5690 /* A list of DIEs for which we may have to generate
5691    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5692    set.  */
5693 static GTY(()) limbo_die_node *deferred_asm_name;
5694
5695 /* Filenames referenced by this compilation unit.  */
5696 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5697
5698 /* A hash table of references to DIE's that describe declarations.
5699    The key is a DECL_UID() which is a unique number identifying each decl.  */
5700 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5701
5702 /* A hash table of references to DIE's that describe COMMON blocks.
5703    The key is DECL_UID() ^ die_parent.  */
5704 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5705
5706 typedef struct GTY(()) die_arg_entry_struct {
5707     dw_die_ref die;
5708     tree arg;
5709 } die_arg_entry;
5710
5711 DEF_VEC_O(die_arg_entry);
5712 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5713
5714 /* Node of the variable location list.  */
5715 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5716   rtx GTY (()) var_loc_note;
5717   const char * GTY (()) label;
5718   const char * GTY (()) section_label;
5719   struct var_loc_node * GTY (()) next;
5720 };
5721
5722 /* Variable location list.  */
5723 struct GTY (()) var_loc_list_def {
5724   struct var_loc_node * GTY (()) first;
5725
5726   /* Do not mark the last element of the chained list because
5727      it is marked through the chain.  */
5728   struct var_loc_node * GTY ((skip ("%h"))) last;
5729
5730   /* DECL_UID of the variable decl.  */
5731   unsigned int decl_id;
5732 };
5733 typedef struct var_loc_list_def var_loc_list;
5734
5735
5736 /* Table of decl location linked lists.  */
5737 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5738
5739 /* A pointer to the base of a list of references to DIE's that
5740    are uniquely identified by their tag, presence/absence of
5741    children DIE's, and list of attribute/value pairs.  */
5742 static GTY((length ("abbrev_die_table_allocated")))
5743   dw_die_ref *abbrev_die_table;
5744
5745 /* Number of elements currently allocated for abbrev_die_table.  */
5746 static GTY(()) unsigned abbrev_die_table_allocated;
5747
5748 /* Number of elements in type_die_table currently in use.  */
5749 static GTY(()) unsigned abbrev_die_table_in_use;
5750
5751 /* Size (in elements) of increments by which we may expand the
5752    abbrev_die_table.  */
5753 #define ABBREV_DIE_TABLE_INCREMENT 256
5754
5755 /* A pointer to the base of a table that contains line information
5756    for each source code line in .text in the compilation unit.  */
5757 static GTY((length ("line_info_table_allocated")))
5758      dw_line_info_ref line_info_table;
5759
5760 /* Number of elements currently allocated for line_info_table.  */
5761 static GTY(()) unsigned line_info_table_allocated;
5762
5763 /* Number of elements in line_info_table currently in use.  */
5764 static GTY(()) unsigned line_info_table_in_use;
5765
5766 /* A pointer to the base of a table that contains line information
5767    for each source code line outside of .text in the compilation unit.  */
5768 static GTY ((length ("separate_line_info_table_allocated")))
5769      dw_separate_line_info_ref separate_line_info_table;
5770
5771 /* Number of elements currently allocated for separate_line_info_table.  */
5772 static GTY(()) unsigned separate_line_info_table_allocated;
5773
5774 /* Number of elements in separate_line_info_table currently in use.  */
5775 static GTY(()) unsigned separate_line_info_table_in_use;
5776
5777 /* Size (in elements) of increments by which we may expand the
5778    line_info_table.  */
5779 #define LINE_INFO_TABLE_INCREMENT 1024
5780
5781 /* A pointer to the base of a table that contains a list of publicly
5782    accessible names.  */
5783 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5784
5785 /* A pointer to the base of a table that contains a list of publicly
5786    accessible types.  */
5787 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5788
5789 /* Array of dies for which we should generate .debug_arange info.  */
5790 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5791
5792 /* Number of elements currently allocated for arange_table.  */
5793 static GTY(()) unsigned arange_table_allocated;
5794
5795 /* Number of elements in arange_table currently in use.  */
5796 static GTY(()) unsigned arange_table_in_use;
5797
5798 /* Size (in elements) of increments by which we may expand the
5799    arange_table.  */
5800 #define ARANGE_TABLE_INCREMENT 64
5801
5802 /* Array of dies for which we should generate .debug_ranges info.  */
5803 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5804
5805 /* Number of elements currently allocated for ranges_table.  */
5806 static GTY(()) unsigned ranges_table_allocated;
5807
5808 /* Number of elements in ranges_table currently in use.  */
5809 static GTY(()) unsigned ranges_table_in_use;
5810
5811 /* Array of pairs of labels referenced in ranges_table.  */
5812 static GTY ((length ("ranges_by_label_allocated")))
5813      dw_ranges_by_label_ref ranges_by_label;
5814
5815 /* Number of elements currently allocated for ranges_by_label.  */
5816 static GTY(()) unsigned ranges_by_label_allocated;
5817
5818 /* Number of elements in ranges_by_label currently in use.  */
5819 static GTY(()) unsigned ranges_by_label_in_use;
5820
5821 /* Size (in elements) of increments by which we may expand the
5822    ranges_table.  */
5823 #define RANGES_TABLE_INCREMENT 64
5824
5825 /* Whether we have location lists that need outputting */
5826 static GTY(()) bool have_location_lists;
5827
5828 /* Unique label counter.  */
5829 static GTY(()) unsigned int loclabel_num;
5830
5831 #ifdef DWARF2_DEBUGGING_INFO
5832 /* Record whether the function being analyzed contains inlined functions.  */
5833 static int current_function_has_inlines;
5834 #endif
5835 #if 0 && defined (MIPS_DEBUGGING_INFO)
5836 static int comp_unit_has_inlines;
5837 #endif
5838
5839 /* The last file entry emitted by maybe_emit_file().  */
5840 static GTY(()) struct dwarf_file_data * last_emitted_file;
5841
5842 /* Number of internal labels generated by gen_internal_sym().  */
5843 static GTY(()) int label_num;
5844
5845 /* Cached result of previous call to lookup_filename.  */
5846 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5847
5848 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5849
5850 #ifdef DWARF2_DEBUGGING_INFO
5851
5852 /* Offset from the "steady-state frame pointer" to the frame base,
5853    within the current function.  */
5854 static HOST_WIDE_INT frame_pointer_fb_offset;
5855
5856 /* Forward declarations for functions defined in this file.  */
5857
5858 static int is_pseudo_reg (const_rtx);
5859 static tree type_main_variant (tree);
5860 static int is_tagged_type (const_tree);
5861 static const char *dwarf_tag_name (unsigned);
5862 static const char *dwarf_attr_name (unsigned);
5863 static const char *dwarf_form_name (unsigned);
5864 static tree decl_ultimate_origin (const_tree);
5865 static tree decl_class_context (tree);
5866 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5867 static inline enum dw_val_class AT_class (dw_attr_ref);
5868 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5869 static inline unsigned AT_flag (dw_attr_ref);
5870 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5871 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5872 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5873 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5874 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
5875                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
5876 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5877                                unsigned int, unsigned char *);
5878 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
5879 static hashval_t debug_str_do_hash (const void *);
5880 static int debug_str_eq (const void *, const void *);
5881 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5882 static inline const char *AT_string (dw_attr_ref);
5883 static enum dwarf_form AT_string_form (dw_attr_ref);
5884 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5885 static void add_AT_specification (dw_die_ref, dw_die_ref);
5886 static inline dw_die_ref AT_ref (dw_attr_ref);
5887 static inline int AT_ref_external (dw_attr_ref);
5888 static inline void set_AT_ref_external (dw_attr_ref, int);
5889 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5890 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5891 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5892 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5893                              dw_loc_list_ref);
5894 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5895 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5896 static inline rtx AT_addr (dw_attr_ref);
5897 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5898 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5899 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5900 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5901                            unsigned HOST_WIDE_INT);
5902 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5903                                unsigned long);
5904 static inline const char *AT_lbl (dw_attr_ref);
5905 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5906 static const char *get_AT_low_pc (dw_die_ref);
5907 static const char *get_AT_hi_pc (dw_die_ref);
5908 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5909 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5910 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5911 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5912 static bool is_c_family (void);
5913 static bool is_cxx (void);
5914 static bool is_java (void);
5915 static bool is_fortran (void);
5916 static bool is_ada (void);
5917 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5918 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5919 static void add_child_die (dw_die_ref, dw_die_ref);
5920 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5921 static dw_die_ref lookup_type_die (tree);
5922 static void equate_type_number_to_die (tree, dw_die_ref);
5923 static hashval_t decl_die_table_hash (const void *);
5924 static int decl_die_table_eq (const void *, const void *);
5925 static dw_die_ref lookup_decl_die (tree);
5926 static hashval_t common_block_die_table_hash (const void *);
5927 static int common_block_die_table_eq (const void *, const void *);
5928 static hashval_t decl_loc_table_hash (const void *);
5929 static int decl_loc_table_eq (const void *, const void *);
5930 static var_loc_list *lookup_decl_loc (const_tree);
5931 static void equate_decl_number_to_die (tree, dw_die_ref);
5932 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5933 static void print_spaces (FILE *);
5934 static void print_die (dw_die_ref, FILE *);
5935 static void print_dwarf_line_table (FILE *);
5936 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5937 static dw_die_ref pop_compile_unit (dw_die_ref);
5938 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5939 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5940 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5941 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
5942 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
5943 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
5944 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
5945                                    struct md5_ctx *, int *);
5946 struct checksum_attributes;
5947 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
5948 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
5949 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
5950 static void generate_type_signature (dw_die_ref, comdat_type_node *);
5951 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5952 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5953 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5954 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5955 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5956 static void compute_section_prefix (dw_die_ref);
5957 static int is_type_die (dw_die_ref);
5958 static int is_comdat_die (dw_die_ref);
5959 static int is_symbol_die (dw_die_ref);
5960 static void assign_symbol_names (dw_die_ref);
5961 static void break_out_includes (dw_die_ref);
5962 static int is_declaration_die (dw_die_ref);
5963 static int should_move_die_to_comdat (dw_die_ref);
5964 static dw_die_ref clone_as_declaration (dw_die_ref);
5965 static dw_die_ref clone_die (dw_die_ref);
5966 static dw_die_ref clone_tree (dw_die_ref);
5967 static void copy_declaration_context (dw_die_ref, dw_die_ref);
5968 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
5969 static void generate_skeleton_bottom_up (skeleton_chain_node *);
5970 static dw_die_ref generate_skeleton (dw_die_ref);
5971 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
5972                                                          dw_die_ref);
5973 static void break_out_comdat_types (dw_die_ref);
5974 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
5975 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
5976 static void copy_decls_for_unworthy_types (dw_die_ref);
5977
5978 static hashval_t htab_cu_hash (const void *);
5979 static int htab_cu_eq (const void *, const void *);
5980 static void htab_cu_del (void *);
5981 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5982 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5983 static void add_sibling_attributes (dw_die_ref);
5984 static void build_abbrev_table (dw_die_ref);
5985 static void output_location_lists (dw_die_ref);
5986 static int constant_size (unsigned HOST_WIDE_INT);
5987 static unsigned long size_of_die (dw_die_ref);
5988 static void calc_die_sizes (dw_die_ref);
5989 static void mark_dies (dw_die_ref);
5990 static void unmark_dies (dw_die_ref);
5991 static void unmark_all_dies (dw_die_ref);
5992 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5993 static unsigned long size_of_aranges (void);
5994 static enum dwarf_form value_format (dw_attr_ref);
5995 static void output_value_format (dw_attr_ref);
5996 static void output_abbrev_section (void);
5997 static void output_die_symbol (dw_die_ref);
5998 static void output_die (dw_die_ref);
5999 static void output_compilation_unit_header (void);
6000 static void output_comp_unit (dw_die_ref, int);
6001 static void output_comdat_type_unit (comdat_type_node *);
6002 static const char *dwarf2_name (tree, int);
6003 static void add_pubname (tree, dw_die_ref);
6004 static void add_pubname_string (const char *, dw_die_ref);
6005 static void add_pubtype (tree, dw_die_ref);
6006 static void output_pubnames (VEC (pubname_entry,gc) *);
6007 static void add_arange (tree, dw_die_ref);
6008 static void output_aranges (void);
6009 static unsigned int add_ranges_num (int);
6010 static unsigned int add_ranges (const_tree);
6011 static unsigned int add_ranges_by_labels (const char *, const char *);
6012 static void output_ranges (void);
6013 static void output_line_info (void);
6014 static void output_file_names (void);
6015 static dw_die_ref base_type_die (tree);
6016 static int is_base_type (tree);
6017 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6018 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6019 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6020 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6021 static int type_is_enum (const_tree);
6022 static unsigned int dbx_reg_number (const_rtx);
6023 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6024 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6025 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6026                                                 enum var_init_status);
6027 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6028                                                      enum var_init_status);
6029 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6030                                          enum var_init_status);
6031 static int is_based_loc (const_rtx);
6032 static int resolve_one_addr (rtx *, void *);
6033 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
6034                                             enum var_init_status);
6035 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6036                                                enum var_init_status);
6037 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6038                                         enum var_init_status);
6039 static dw_loc_list_ref loc_list_from_tree (tree, int);
6040 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6041 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6042 static tree field_type (const_tree);
6043 static unsigned int simple_type_align_in_bits (const_tree);
6044 static unsigned int simple_decl_align_in_bits (const_tree);
6045 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6046 static HOST_WIDE_INT field_byte_offset (const_tree);
6047 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6048                                          dw_loc_list_ref);
6049 static void add_data_member_location_attribute (dw_die_ref, tree);
6050 static bool add_const_value_attribute (dw_die_ref, rtx);
6051 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6052 static void insert_float (const_rtx, unsigned char *);
6053 static rtx rtl_for_decl_location (tree);
6054 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6055                                                    enum dwarf_attribute);
6056 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6057 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6058 static void add_name_attribute (dw_die_ref, const char *);
6059 static void add_comp_dir_attribute (dw_die_ref);
6060 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6061 static void add_subscript_info (dw_die_ref, tree, bool);
6062 static void add_byte_size_attribute (dw_die_ref, tree);
6063 static void add_bit_offset_attribute (dw_die_ref, tree);
6064 static void add_bit_size_attribute (dw_die_ref, tree);
6065 static void add_prototyped_attribute (dw_die_ref, tree);
6066 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6067 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6068 static void add_src_coords_attributes (dw_die_ref, tree);
6069 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6070 static void push_decl_scope (tree);
6071 static void pop_decl_scope (void);
6072 static dw_die_ref scope_die_for (tree, dw_die_ref);
6073 static inline int local_scope_p (dw_die_ref);
6074 static inline int class_scope_p (dw_die_ref);
6075 static inline int class_or_namespace_scope_p (dw_die_ref);
6076 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6077 static void add_calling_convention_attribute (dw_die_ref, tree);
6078 static const char *type_tag (const_tree);
6079 static tree member_declared_type (const_tree);
6080 #if 0
6081 static const char *decl_start_label (tree);
6082 #endif
6083 static void gen_array_type_die (tree, dw_die_ref);
6084 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6085 #if 0
6086 static void gen_entry_point_die (tree, dw_die_ref);
6087 #endif
6088 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6089 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6090 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6091 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6092 static void gen_formal_types_die (tree, dw_die_ref);
6093 static void gen_subprogram_die (tree, dw_die_ref);
6094 static void gen_variable_die (tree, tree, dw_die_ref);
6095 static void gen_const_die (tree, dw_die_ref);
6096 static void gen_label_die (tree, dw_die_ref);
6097 static void gen_lexical_block_die (tree, dw_die_ref, int);
6098 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6099 static void gen_field_die (tree, dw_die_ref);
6100 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6101 static dw_die_ref gen_compile_unit_die (const char *);
6102 static void gen_inheritance_die (tree, tree, dw_die_ref);
6103 static void gen_member_die (tree, dw_die_ref);
6104 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6105                                                 enum debug_info_usage);
6106 static void gen_subroutine_type_die (tree, dw_die_ref);
6107 static void gen_typedef_die (tree, dw_die_ref);
6108 static void gen_type_die (tree, dw_die_ref);
6109 static void gen_block_die (tree, dw_die_ref, int);
6110 static void decls_for_scope (tree, dw_die_ref, int);
6111 static int is_redundant_typedef (const_tree);
6112 static inline dw_die_ref get_context_die (tree);
6113 static void gen_namespace_die (tree, dw_die_ref);
6114 static void gen_decl_die (tree, tree, dw_die_ref);
6115 static dw_die_ref force_decl_die (tree);
6116 static dw_die_ref force_type_die (tree);
6117 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6118 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6119 static struct dwarf_file_data * lookup_filename (const char *);
6120 static void retry_incomplete_types (void);
6121 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6122 static void gen_generic_params_dies (tree);
6123 static void splice_child_die (dw_die_ref, dw_die_ref);
6124 static int file_info_cmp (const void *, const void *);
6125 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6126                                      const char *, const char *, unsigned);
6127 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
6128                                        const char *, const char *,
6129                                        const char *);
6130 static void output_loc_list (dw_loc_list_ref);
6131 static char *gen_internal_sym (const char *);
6132
6133 static void prune_unmark_dies (dw_die_ref);
6134 static void prune_unused_types_mark (dw_die_ref, int);
6135 static void prune_unused_types_walk (dw_die_ref);
6136 static void prune_unused_types_walk_attribs (dw_die_ref);
6137 static void prune_unused_types_prune (dw_die_ref);
6138 static void prune_unused_types (void);
6139 static int maybe_emit_file (struct dwarf_file_data *fd);
6140 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6141 static void gen_remaining_tmpl_value_param_die_attribute (void);
6142
6143 /* Section names used to hold DWARF debugging information.  */
6144 #ifndef DEBUG_INFO_SECTION
6145 #define DEBUG_INFO_SECTION      ".debug_info"
6146 #endif
6147 #ifndef DEBUG_ABBREV_SECTION
6148 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6149 #endif
6150 #ifndef DEBUG_ARANGES_SECTION
6151 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6152 #endif
6153 #ifndef DEBUG_MACINFO_SECTION
6154 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6155 #endif
6156 #ifndef DEBUG_LINE_SECTION
6157 #define DEBUG_LINE_SECTION      ".debug_line"
6158 #endif
6159 #ifndef DEBUG_LOC_SECTION
6160 #define DEBUG_LOC_SECTION       ".debug_loc"
6161 #endif
6162 #ifndef DEBUG_PUBNAMES_SECTION
6163 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6164 #endif
6165 #ifndef DEBUG_PUBTYPES_SECTION
6166 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6167 #endif
6168 #ifndef DEBUG_STR_SECTION
6169 #define DEBUG_STR_SECTION       ".debug_str"
6170 #endif
6171 #ifndef DEBUG_RANGES_SECTION
6172 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6173 #endif
6174
6175 /* Standard ELF section names for compiled code and data.  */
6176 #ifndef TEXT_SECTION_NAME
6177 #define TEXT_SECTION_NAME       ".text"
6178 #endif
6179
6180 /* Section flags for .debug_str section.  */
6181 #define DEBUG_STR_SECTION_FLAGS \
6182   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6183    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6184    : SECTION_DEBUG)
6185
6186 /* Labels we insert at beginning sections we can reference instead of
6187    the section names themselves.  */
6188
6189 #ifndef TEXT_SECTION_LABEL
6190 #define TEXT_SECTION_LABEL              "Ltext"
6191 #endif
6192 #ifndef COLD_TEXT_SECTION_LABEL
6193 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6194 #endif
6195 #ifndef DEBUG_LINE_SECTION_LABEL
6196 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6197 #endif
6198 #ifndef DEBUG_INFO_SECTION_LABEL
6199 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6200 #endif
6201 #ifndef DEBUG_ABBREV_SECTION_LABEL
6202 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6203 #endif
6204 #ifndef DEBUG_LOC_SECTION_LABEL
6205 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6206 #endif
6207 #ifndef DEBUG_RANGES_SECTION_LABEL
6208 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6209 #endif
6210 #ifndef DEBUG_MACINFO_SECTION_LABEL
6211 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6212 #endif
6213
6214 /* Definitions of defaults for formats and names of various special
6215    (artificial) labels which may be generated within this file (when the -g
6216    options is used and DWARF2_DEBUGGING_INFO is in effect.
6217    If necessary, these may be overridden from within the tm.h file, but
6218    typically, overriding these defaults is unnecessary.  */
6219
6220 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6221 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6222 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6223 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6224 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6225 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6226 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6227 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6228 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6229 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6230
6231 #ifndef TEXT_END_LABEL
6232 #define TEXT_END_LABEL          "Letext"
6233 #endif
6234 #ifndef COLD_END_LABEL
6235 #define COLD_END_LABEL          "Letext_cold"
6236 #endif
6237 #ifndef BLOCK_BEGIN_LABEL
6238 #define BLOCK_BEGIN_LABEL       "LBB"
6239 #endif
6240 #ifndef BLOCK_END_LABEL
6241 #define BLOCK_END_LABEL         "LBE"
6242 #endif
6243 #ifndef LINE_CODE_LABEL
6244 #define LINE_CODE_LABEL         "LM"
6245 #endif
6246 #ifndef SEPARATE_LINE_CODE_LABEL
6247 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6248 #endif
6249
6250 \f
6251 /* We allow a language front-end to designate a function that is to be
6252    called to "demangle" any name before it is put into a DIE.  */
6253
6254 static const char *(*demangle_name_func) (const char *);
6255
6256 void
6257 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6258 {
6259   demangle_name_func = func;
6260 }
6261
6262 /* Test if rtl node points to a pseudo register.  */
6263
6264 static inline int
6265 is_pseudo_reg (const_rtx rtl)
6266 {
6267   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6268           || (GET_CODE (rtl) == SUBREG
6269               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6270 }
6271
6272 /* Return a reference to a type, with its const and volatile qualifiers
6273    removed.  */
6274
6275 static inline tree
6276 type_main_variant (tree type)
6277 {
6278   type = TYPE_MAIN_VARIANT (type);
6279
6280   /* ??? There really should be only one main variant among any group of
6281      variants of a given type (and all of the MAIN_VARIANT values for all
6282      members of the group should point to that one type) but sometimes the C
6283      front-end messes this up for array types, so we work around that bug
6284      here.  */
6285   if (TREE_CODE (type) == ARRAY_TYPE)
6286     while (type != TYPE_MAIN_VARIANT (type))
6287       type = TYPE_MAIN_VARIANT (type);
6288
6289   return type;
6290 }
6291
6292 /* Return nonzero if the given type node represents a tagged type.  */
6293
6294 static inline int
6295 is_tagged_type (const_tree type)
6296 {
6297   enum tree_code code = TREE_CODE (type);
6298
6299   return (code == RECORD_TYPE || code == UNION_TYPE
6300           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6301 }
6302
6303 /* Convert a DIE tag into its string name.  */
6304
6305 static const char *
6306 dwarf_tag_name (unsigned int tag)
6307 {
6308   switch (tag)
6309     {
6310     case DW_TAG_padding:
6311       return "DW_TAG_padding";
6312     case DW_TAG_array_type:
6313       return "DW_TAG_array_type";
6314     case DW_TAG_class_type:
6315       return "DW_TAG_class_type";
6316     case DW_TAG_entry_point:
6317       return "DW_TAG_entry_point";
6318     case DW_TAG_enumeration_type:
6319       return "DW_TAG_enumeration_type";
6320     case DW_TAG_formal_parameter:
6321       return "DW_TAG_formal_parameter";
6322     case DW_TAG_imported_declaration:
6323       return "DW_TAG_imported_declaration";
6324     case DW_TAG_label:
6325       return "DW_TAG_label";
6326     case DW_TAG_lexical_block:
6327       return "DW_TAG_lexical_block";
6328     case DW_TAG_member:
6329       return "DW_TAG_member";
6330     case DW_TAG_pointer_type:
6331       return "DW_TAG_pointer_type";
6332     case DW_TAG_reference_type:
6333       return "DW_TAG_reference_type";
6334     case DW_TAG_compile_unit:
6335       return "DW_TAG_compile_unit";
6336     case DW_TAG_string_type:
6337       return "DW_TAG_string_type";
6338     case DW_TAG_structure_type:
6339       return "DW_TAG_structure_type";
6340     case DW_TAG_subroutine_type:
6341       return "DW_TAG_subroutine_type";
6342     case DW_TAG_typedef:
6343       return "DW_TAG_typedef";
6344     case DW_TAG_union_type:
6345       return "DW_TAG_union_type";
6346     case DW_TAG_unspecified_parameters:
6347       return "DW_TAG_unspecified_parameters";
6348     case DW_TAG_variant:
6349       return "DW_TAG_variant";
6350     case DW_TAG_common_block:
6351       return "DW_TAG_common_block";
6352     case DW_TAG_common_inclusion:
6353       return "DW_TAG_common_inclusion";
6354     case DW_TAG_inheritance:
6355       return "DW_TAG_inheritance";
6356     case DW_TAG_inlined_subroutine:
6357       return "DW_TAG_inlined_subroutine";
6358     case DW_TAG_module:
6359       return "DW_TAG_module";
6360     case DW_TAG_ptr_to_member_type:
6361       return "DW_TAG_ptr_to_member_type";
6362     case DW_TAG_set_type:
6363       return "DW_TAG_set_type";
6364     case DW_TAG_subrange_type:
6365       return "DW_TAG_subrange_type";
6366     case DW_TAG_with_stmt:
6367       return "DW_TAG_with_stmt";
6368     case DW_TAG_access_declaration:
6369       return "DW_TAG_access_declaration";
6370     case DW_TAG_base_type:
6371       return "DW_TAG_base_type";
6372     case DW_TAG_catch_block:
6373       return "DW_TAG_catch_block";
6374     case DW_TAG_const_type:
6375       return "DW_TAG_const_type";
6376     case DW_TAG_constant:
6377       return "DW_TAG_constant";
6378     case DW_TAG_enumerator:
6379       return "DW_TAG_enumerator";
6380     case DW_TAG_file_type:
6381       return "DW_TAG_file_type";
6382     case DW_TAG_friend:
6383       return "DW_TAG_friend";
6384     case DW_TAG_namelist:
6385       return "DW_TAG_namelist";
6386     case DW_TAG_namelist_item:
6387       return "DW_TAG_namelist_item";
6388     case DW_TAG_packed_type:
6389       return "DW_TAG_packed_type";
6390     case DW_TAG_subprogram:
6391       return "DW_TAG_subprogram";
6392     case DW_TAG_template_type_param:
6393       return "DW_TAG_template_type_param";
6394     case DW_TAG_template_value_param:
6395       return "DW_TAG_template_value_param";
6396     case DW_TAG_thrown_type:
6397       return "DW_TAG_thrown_type";
6398     case DW_TAG_try_block:
6399       return "DW_TAG_try_block";
6400     case DW_TAG_variant_part:
6401       return "DW_TAG_variant_part";
6402     case DW_TAG_variable:
6403       return "DW_TAG_variable";
6404     case DW_TAG_volatile_type:
6405       return "DW_TAG_volatile_type";
6406     case DW_TAG_dwarf_procedure:
6407       return "DW_TAG_dwarf_procedure";
6408     case DW_TAG_restrict_type:
6409       return "DW_TAG_restrict_type";
6410     case DW_TAG_interface_type:
6411       return "DW_TAG_interface_type";
6412     case DW_TAG_namespace:
6413       return "DW_TAG_namespace";
6414     case DW_TAG_imported_module:
6415       return "DW_TAG_imported_module";
6416     case DW_TAG_unspecified_type:
6417       return "DW_TAG_unspecified_type";
6418     case DW_TAG_partial_unit:
6419       return "DW_TAG_partial_unit";
6420     case DW_TAG_imported_unit:
6421       return "DW_TAG_imported_unit";
6422     case DW_TAG_condition:
6423       return "DW_TAG_condition";
6424     case DW_TAG_shared_type:
6425       return "DW_TAG_shared_type";
6426     case DW_TAG_type_unit:
6427       return "DW_TAG_type_unit";
6428     case DW_TAG_GNU_template_parameter_pack:
6429       return "DW_TAG_GNU_template_parameter_pack";
6430     case DW_TAG_GNU_formal_parameter_pack:
6431       return "DW_TAG_GNU_formal_parameter_pack";
6432     case DW_TAG_MIPS_loop:
6433       return "DW_TAG_MIPS_loop";
6434     case DW_TAG_format_label:
6435       return "DW_TAG_format_label";
6436     case DW_TAG_function_template:
6437       return "DW_TAG_function_template";
6438     case DW_TAG_class_template:
6439       return "DW_TAG_class_template";
6440     case DW_TAG_GNU_BINCL:
6441       return "DW_TAG_GNU_BINCL";
6442     case DW_TAG_GNU_EINCL:
6443       return "DW_TAG_GNU_EINCL";
6444     case DW_TAG_GNU_template_template_param:
6445       return "DW_TAG_GNU_template_template_param";
6446     default:
6447       return "DW_TAG_<unknown>";
6448     }
6449 }
6450
6451 /* Convert a DWARF attribute code into its string name.  */
6452
6453 static const char *
6454 dwarf_attr_name (unsigned int attr)
6455 {
6456   switch (attr)
6457     {
6458     case DW_AT_sibling:
6459       return "DW_AT_sibling";
6460     case DW_AT_location:
6461       return "DW_AT_location";
6462     case DW_AT_name:
6463       return "DW_AT_name";
6464     case DW_AT_ordering:
6465       return "DW_AT_ordering";
6466     case DW_AT_subscr_data:
6467       return "DW_AT_subscr_data";
6468     case DW_AT_byte_size:
6469       return "DW_AT_byte_size";
6470     case DW_AT_bit_offset:
6471       return "DW_AT_bit_offset";
6472     case DW_AT_bit_size:
6473       return "DW_AT_bit_size";
6474     case DW_AT_element_list:
6475       return "DW_AT_element_list";
6476     case DW_AT_stmt_list:
6477       return "DW_AT_stmt_list";
6478     case DW_AT_low_pc:
6479       return "DW_AT_low_pc";
6480     case DW_AT_high_pc:
6481       return "DW_AT_high_pc";
6482     case DW_AT_language:
6483       return "DW_AT_language";
6484     case DW_AT_member:
6485       return "DW_AT_member";
6486     case DW_AT_discr:
6487       return "DW_AT_discr";
6488     case DW_AT_discr_value:
6489       return "DW_AT_discr_value";
6490     case DW_AT_visibility:
6491       return "DW_AT_visibility";
6492     case DW_AT_import:
6493       return "DW_AT_import";
6494     case DW_AT_string_length:
6495       return "DW_AT_string_length";
6496     case DW_AT_common_reference:
6497       return "DW_AT_common_reference";
6498     case DW_AT_comp_dir:
6499       return "DW_AT_comp_dir";
6500     case DW_AT_const_value:
6501       return "DW_AT_const_value";
6502     case DW_AT_containing_type:
6503       return "DW_AT_containing_type";
6504     case DW_AT_default_value:
6505       return "DW_AT_default_value";
6506     case DW_AT_inline:
6507       return "DW_AT_inline";
6508     case DW_AT_is_optional:
6509       return "DW_AT_is_optional";
6510     case DW_AT_lower_bound:
6511       return "DW_AT_lower_bound";
6512     case DW_AT_producer:
6513       return "DW_AT_producer";
6514     case DW_AT_prototyped:
6515       return "DW_AT_prototyped";
6516     case DW_AT_return_addr:
6517       return "DW_AT_return_addr";
6518     case DW_AT_start_scope:
6519       return "DW_AT_start_scope";
6520     case DW_AT_bit_stride:
6521       return "DW_AT_bit_stride";
6522     case DW_AT_upper_bound:
6523       return "DW_AT_upper_bound";
6524     case DW_AT_abstract_origin:
6525       return "DW_AT_abstract_origin";
6526     case DW_AT_accessibility:
6527       return "DW_AT_accessibility";
6528     case DW_AT_address_class:
6529       return "DW_AT_address_class";
6530     case DW_AT_artificial:
6531       return "DW_AT_artificial";
6532     case DW_AT_base_types:
6533       return "DW_AT_base_types";
6534     case DW_AT_calling_convention:
6535       return "DW_AT_calling_convention";
6536     case DW_AT_count:
6537       return "DW_AT_count";
6538     case DW_AT_data_member_location:
6539       return "DW_AT_data_member_location";
6540     case DW_AT_decl_column:
6541       return "DW_AT_decl_column";
6542     case DW_AT_decl_file:
6543       return "DW_AT_decl_file";
6544     case DW_AT_decl_line:
6545       return "DW_AT_decl_line";
6546     case DW_AT_declaration:
6547       return "DW_AT_declaration";
6548     case DW_AT_discr_list:
6549       return "DW_AT_discr_list";
6550     case DW_AT_encoding:
6551       return "DW_AT_encoding";
6552     case DW_AT_external:
6553       return "DW_AT_external";
6554     case DW_AT_explicit:
6555       return "DW_AT_explicit";
6556     case DW_AT_frame_base:
6557       return "DW_AT_frame_base";
6558     case DW_AT_friend:
6559       return "DW_AT_friend";
6560     case DW_AT_identifier_case:
6561       return "DW_AT_identifier_case";
6562     case DW_AT_macro_info:
6563       return "DW_AT_macro_info";
6564     case DW_AT_namelist_items:
6565       return "DW_AT_namelist_items";
6566     case DW_AT_priority:
6567       return "DW_AT_priority";
6568     case DW_AT_segment:
6569       return "DW_AT_segment";
6570     case DW_AT_specification:
6571       return "DW_AT_specification";
6572     case DW_AT_static_link:
6573       return "DW_AT_static_link";
6574     case DW_AT_type:
6575       return "DW_AT_type";
6576     case DW_AT_use_location:
6577       return "DW_AT_use_location";
6578     case DW_AT_variable_parameter:
6579       return "DW_AT_variable_parameter";
6580     case DW_AT_virtuality:
6581       return "DW_AT_virtuality";
6582     case DW_AT_vtable_elem_location:
6583       return "DW_AT_vtable_elem_location";
6584
6585     case DW_AT_allocated:
6586       return "DW_AT_allocated";
6587     case DW_AT_associated:
6588       return "DW_AT_associated";
6589     case DW_AT_data_location:
6590       return "DW_AT_data_location";
6591     case DW_AT_byte_stride:
6592       return "DW_AT_byte_stride";
6593     case DW_AT_entry_pc:
6594       return "DW_AT_entry_pc";
6595     case DW_AT_use_UTF8:
6596       return "DW_AT_use_UTF8";
6597     case DW_AT_extension:
6598       return "DW_AT_extension";
6599     case DW_AT_ranges:
6600       return "DW_AT_ranges";
6601     case DW_AT_trampoline:
6602       return "DW_AT_trampoline";
6603     case DW_AT_call_column:
6604       return "DW_AT_call_column";
6605     case DW_AT_call_file:
6606       return "DW_AT_call_file";
6607     case DW_AT_call_line:
6608       return "DW_AT_call_line";
6609
6610     case DW_AT_signature:
6611       return "DW_AT_signature";
6612
6613     case DW_AT_MIPS_fde:
6614       return "DW_AT_MIPS_fde";
6615     case DW_AT_MIPS_loop_begin:
6616       return "DW_AT_MIPS_loop_begin";
6617     case DW_AT_MIPS_tail_loop_begin:
6618       return "DW_AT_MIPS_tail_loop_begin";
6619     case DW_AT_MIPS_epilog_begin:
6620       return "DW_AT_MIPS_epilog_begin";
6621     case DW_AT_MIPS_loop_unroll_factor:
6622       return "DW_AT_MIPS_loop_unroll_factor";
6623     case DW_AT_MIPS_software_pipeline_depth:
6624       return "DW_AT_MIPS_software_pipeline_depth";
6625     case DW_AT_MIPS_linkage_name:
6626       return "DW_AT_MIPS_linkage_name";
6627     case DW_AT_MIPS_stride:
6628       return "DW_AT_MIPS_stride";
6629     case DW_AT_MIPS_abstract_name:
6630       return "DW_AT_MIPS_abstract_name";
6631     case DW_AT_MIPS_clone_origin:
6632       return "DW_AT_MIPS_clone_origin";
6633     case DW_AT_MIPS_has_inlines:
6634       return "DW_AT_MIPS_has_inlines";
6635
6636     case DW_AT_sf_names:
6637       return "DW_AT_sf_names";
6638     case DW_AT_src_info:
6639       return "DW_AT_src_info";
6640     case DW_AT_mac_info:
6641       return "DW_AT_mac_info";
6642     case DW_AT_src_coords:
6643       return "DW_AT_src_coords";
6644     case DW_AT_body_begin:
6645       return "DW_AT_body_begin";
6646     case DW_AT_body_end:
6647       return "DW_AT_body_end";
6648     case DW_AT_GNU_vector:
6649       return "DW_AT_GNU_vector";
6650     case DW_AT_GNU_template_name:
6651       return "DW_AT_GNU_template_name";
6652
6653     case DW_AT_VMS_rtnbeg_pd_address:
6654       return "DW_AT_VMS_rtnbeg_pd_address";
6655
6656     default:
6657       return "DW_AT_<unknown>";
6658     }
6659 }
6660
6661 /* Convert a DWARF value form code into its string name.  */
6662
6663 static const char *
6664 dwarf_form_name (unsigned int form)
6665 {
6666   switch (form)
6667     {
6668     case DW_FORM_addr:
6669       return "DW_FORM_addr";
6670     case DW_FORM_block2:
6671       return "DW_FORM_block2";
6672     case DW_FORM_block4:
6673       return "DW_FORM_block4";
6674     case DW_FORM_data2:
6675       return "DW_FORM_data2";
6676     case DW_FORM_data4:
6677       return "DW_FORM_data4";
6678     case DW_FORM_data8:
6679       return "DW_FORM_data8";
6680     case DW_FORM_string:
6681       return "DW_FORM_string";
6682     case DW_FORM_block:
6683       return "DW_FORM_block";
6684     case DW_FORM_block1:
6685       return "DW_FORM_block1";
6686     case DW_FORM_data1:
6687       return "DW_FORM_data1";
6688     case DW_FORM_flag:
6689       return "DW_FORM_flag";
6690     case DW_FORM_sdata:
6691       return "DW_FORM_sdata";
6692     case DW_FORM_strp:
6693       return "DW_FORM_strp";
6694     case DW_FORM_udata:
6695       return "DW_FORM_udata";
6696     case DW_FORM_ref_addr:
6697       return "DW_FORM_ref_addr";
6698     case DW_FORM_ref1:
6699       return "DW_FORM_ref1";
6700     case DW_FORM_ref2:
6701       return "DW_FORM_ref2";
6702     case DW_FORM_ref4:
6703       return "DW_FORM_ref4";
6704     case DW_FORM_ref8:
6705       return "DW_FORM_ref8";
6706     case DW_FORM_ref_udata:
6707       return "DW_FORM_ref_udata";
6708     case DW_FORM_indirect:
6709       return "DW_FORM_indirect";
6710     default:
6711       return "DW_FORM_<unknown>";
6712     }
6713 }
6714 \f
6715 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6716    instance of an inlined instance of a decl which is local to an inline
6717    function, so we have to trace all of the way back through the origin chain
6718    to find out what sort of node actually served as the original seed for the
6719    given block.  */
6720
6721 static tree
6722 decl_ultimate_origin (const_tree decl)
6723 {
6724   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6725     return NULL_TREE;
6726
6727   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6728      nodes in the function to point to themselves; ignore that if
6729      we're trying to output the abstract instance of this function.  */
6730   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6731     return NULL_TREE;
6732
6733   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6734      most distant ancestor, this should never happen.  */
6735   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6736
6737   return DECL_ABSTRACT_ORIGIN (decl);
6738 }
6739
6740 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6741    of a virtual function may refer to a base class, so we check the 'this'
6742    parameter.  */
6743
6744 static tree
6745 decl_class_context (tree decl)
6746 {
6747   tree context = NULL_TREE;
6748
6749   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6750     context = DECL_CONTEXT (decl);
6751   else
6752     context = TYPE_MAIN_VARIANT
6753       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6754
6755   if (context && !TYPE_P (context))
6756     context = NULL_TREE;
6757
6758   return context;
6759 }
6760 \f
6761 /* Add an attribute/value pair to a DIE.  */
6762
6763 static inline void
6764 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6765 {
6766   /* Maybe this should be an assert?  */
6767   if (die == NULL)
6768     return;
6769
6770   if (die->die_attr == NULL)
6771     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6772   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6773 }
6774
6775 static inline enum dw_val_class
6776 AT_class (dw_attr_ref a)
6777 {
6778   return a->dw_attr_val.val_class;
6779 }
6780
6781 /* Add a flag value attribute to a DIE.  */
6782
6783 static inline void
6784 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6785 {
6786   dw_attr_node attr;
6787
6788   attr.dw_attr = attr_kind;
6789   attr.dw_attr_val.val_class = dw_val_class_flag;
6790   attr.dw_attr_val.v.val_flag = flag;
6791   add_dwarf_attr (die, &attr);
6792 }
6793
6794 static inline unsigned
6795 AT_flag (dw_attr_ref a)
6796 {
6797   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6798   return a->dw_attr_val.v.val_flag;
6799 }
6800
6801 /* Add a signed integer attribute value to a DIE.  */
6802
6803 static inline void
6804 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6805 {
6806   dw_attr_node attr;
6807
6808   attr.dw_attr = attr_kind;
6809   attr.dw_attr_val.val_class = dw_val_class_const;
6810   attr.dw_attr_val.v.val_int = int_val;
6811   add_dwarf_attr (die, &attr);
6812 }
6813
6814 static inline HOST_WIDE_INT
6815 AT_int (dw_attr_ref a)
6816 {
6817   gcc_assert (a && AT_class (a) == dw_val_class_const);
6818   return a->dw_attr_val.v.val_int;
6819 }
6820
6821 /* Add an unsigned integer attribute value to a DIE.  */
6822
6823 static inline void
6824 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6825                  unsigned HOST_WIDE_INT unsigned_val)
6826 {
6827   dw_attr_node attr;
6828
6829   attr.dw_attr = attr_kind;
6830   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6831   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6832   add_dwarf_attr (die, &attr);
6833 }
6834
6835 static inline unsigned HOST_WIDE_INT
6836 AT_unsigned (dw_attr_ref a)
6837 {
6838   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6839   return a->dw_attr_val.v.val_unsigned;
6840 }
6841
6842 /* Add an unsigned double integer attribute value to a DIE.  */
6843
6844 static inline void
6845 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
6846                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
6847 {
6848   dw_attr_node attr;
6849
6850   attr.dw_attr = attr_kind;
6851   attr.dw_attr_val.val_class = dw_val_class_const_double;
6852   attr.dw_attr_val.v.val_double.high = high;
6853   attr.dw_attr_val.v.val_double.low = low;
6854   add_dwarf_attr (die, &attr);
6855 }
6856
6857 /* Add a floating point attribute value to a DIE and return it.  */
6858
6859 static inline void
6860 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6861             unsigned int length, unsigned int elt_size, unsigned char *array)
6862 {
6863   dw_attr_node attr;
6864
6865   attr.dw_attr = attr_kind;
6866   attr.dw_attr_val.val_class = dw_val_class_vec;
6867   attr.dw_attr_val.v.val_vec.length = length;
6868   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6869   attr.dw_attr_val.v.val_vec.array = array;
6870   add_dwarf_attr (die, &attr);
6871 }
6872
6873 /* Add an 8-byte data attribute value to a DIE.  */
6874
6875 static inline void
6876 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
6877               unsigned char data8[8])
6878 {
6879   dw_attr_node attr;
6880
6881   attr.dw_attr = attr_kind;
6882   attr.dw_attr_val.val_class = dw_val_class_data8;
6883   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
6884   add_dwarf_attr (die, &attr);
6885 }
6886
6887 /* Hash and equality functions for debug_str_hash.  */
6888
6889 static hashval_t
6890 debug_str_do_hash (const void *x)
6891 {
6892   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6893 }
6894
6895 static int
6896 debug_str_eq (const void *x1, const void *x2)
6897 {
6898   return strcmp ((((const struct indirect_string_node *)x1)->str),
6899                  (const char *)x2) == 0;
6900 }
6901
6902 /* Add STR to the indirect string hash table.  */
6903
6904 static struct indirect_string_node *
6905 find_AT_string (const char *str)
6906 {
6907   struct indirect_string_node *node;
6908   void **slot;
6909
6910   if (! debug_str_hash)
6911     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6912                                       debug_str_eq, NULL);
6913
6914   slot = htab_find_slot_with_hash (debug_str_hash, str,
6915                                    htab_hash_string (str), INSERT);
6916   if (*slot == NULL)
6917     {
6918       node = (struct indirect_string_node *)
6919                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6920       node->str = ggc_strdup (str);
6921       *slot = node;
6922     }
6923   else
6924     node = (struct indirect_string_node *) *slot;
6925
6926   node->refcount++;
6927   return node;
6928 }
6929
6930 /* Add a string attribute value to a DIE.  */
6931
6932 static inline void
6933 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6934 {
6935   dw_attr_node attr;
6936   struct indirect_string_node *node;
6937
6938   node = find_AT_string (str);
6939
6940   attr.dw_attr = attr_kind;
6941   attr.dw_attr_val.val_class = dw_val_class_str;
6942   attr.dw_attr_val.v.val_str = node;
6943   add_dwarf_attr (die, &attr);
6944 }
6945
6946 /* Create a label for an indirect string node, ensuring it is going to
6947    be output, unless its reference count goes down to zero.  */
6948
6949 static inline void
6950 gen_label_for_indirect_string (struct indirect_string_node *node)
6951 {
6952   char label[32];
6953
6954   if (node->label)
6955     return;
6956
6957   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6958   ++dw2_string_counter;
6959   node->label = xstrdup (label);
6960 }
6961
6962 /* Create a SYMBOL_REF rtx whose value is the initial address of a
6963    debug string STR.  */
6964
6965 static inline rtx
6966 get_debug_string_label (const char *str)
6967 {
6968   struct indirect_string_node *node = find_AT_string (str);
6969
6970   debug_str_hash_forced = true;
6971
6972   gen_label_for_indirect_string (node);
6973
6974   return gen_rtx_SYMBOL_REF (Pmode, node->label);
6975 }
6976
6977 static inline const char *
6978 AT_string (dw_attr_ref a)
6979 {
6980   gcc_assert (a && AT_class (a) == dw_val_class_str);
6981   return a->dw_attr_val.v.val_str->str;
6982 }
6983
6984 /* Find out whether a string should be output inline in DIE
6985    or out-of-line in .debug_str section.  */
6986
6987 static enum dwarf_form
6988 AT_string_form (dw_attr_ref a)
6989 {
6990   struct indirect_string_node *node;
6991   unsigned int len;
6992
6993   gcc_assert (a && AT_class (a) == dw_val_class_str);
6994
6995   node = a->dw_attr_val.v.val_str;
6996   if (node->form)
6997     return node->form;
6998
6999   len = strlen (node->str) + 1;
7000
7001   /* If the string is shorter or equal to the size of the reference, it is
7002      always better to put it inline.  */
7003   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7004     return node->form = DW_FORM_string;
7005
7006   /* If we cannot expect the linker to merge strings in .debug_str
7007      section, only put it into .debug_str if it is worth even in this
7008      single module.  */
7009   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7010       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7011       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7012     return node->form = DW_FORM_string;
7013
7014   gen_label_for_indirect_string (node);
7015
7016   return node->form = DW_FORM_strp;
7017 }
7018
7019 /* Add a DIE reference attribute value to a DIE.  */
7020
7021 static inline void
7022 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7023 {
7024   dw_attr_node attr;
7025
7026   attr.dw_attr = attr_kind;
7027   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7028   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7029   attr.dw_attr_val.v.val_die_ref.external = 0;
7030   add_dwarf_attr (die, &attr);
7031 }
7032
7033 /* Add an AT_specification attribute to a DIE, and also make the back
7034    pointer from the specification to the definition.  */
7035
7036 static inline void
7037 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7038 {
7039   add_AT_die_ref (die, DW_AT_specification, targ_die);
7040   gcc_assert (!targ_die->die_definition);
7041   targ_die->die_definition = die;
7042 }
7043
7044 static inline dw_die_ref
7045 AT_ref (dw_attr_ref a)
7046 {
7047   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7048   return a->dw_attr_val.v.val_die_ref.die;
7049 }
7050
7051 static inline int
7052 AT_ref_external (dw_attr_ref a)
7053 {
7054   if (a && AT_class (a) == dw_val_class_die_ref)
7055     return a->dw_attr_val.v.val_die_ref.external;
7056
7057   return 0;
7058 }
7059
7060 static inline void
7061 set_AT_ref_external (dw_attr_ref a, int i)
7062 {
7063   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7064   a->dw_attr_val.v.val_die_ref.external = i;
7065 }
7066
7067 /* Add an FDE reference attribute value to a DIE.  */
7068
7069 static inline void
7070 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7071 {
7072   dw_attr_node attr;
7073
7074   attr.dw_attr = attr_kind;
7075   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7076   attr.dw_attr_val.v.val_fde_index = targ_fde;
7077   add_dwarf_attr (die, &attr);
7078 }
7079
7080 /* Add a location description attribute value to a DIE.  */
7081
7082 static inline void
7083 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7084 {
7085   dw_attr_node attr;
7086
7087   attr.dw_attr = attr_kind;
7088   attr.dw_attr_val.val_class = dw_val_class_loc;
7089   attr.dw_attr_val.v.val_loc = loc;
7090   add_dwarf_attr (die, &attr);
7091 }
7092
7093 static inline dw_loc_descr_ref
7094 AT_loc (dw_attr_ref a)
7095 {
7096   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7097   return a->dw_attr_val.v.val_loc;
7098 }
7099
7100 static inline void
7101 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7102 {
7103   dw_attr_node attr;
7104
7105   attr.dw_attr = attr_kind;
7106   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7107   attr.dw_attr_val.v.val_loc_list = loc_list;
7108   add_dwarf_attr (die, &attr);
7109   have_location_lists = true;
7110 }
7111
7112 static inline dw_loc_list_ref
7113 AT_loc_list (dw_attr_ref a)
7114 {
7115   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7116   return a->dw_attr_val.v.val_loc_list;
7117 }
7118
7119 /* Add an address constant attribute value to a DIE.  */
7120
7121 static inline void
7122 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7123 {
7124   dw_attr_node attr;
7125
7126   attr.dw_attr = attr_kind;
7127   attr.dw_attr_val.val_class = dw_val_class_addr;
7128   attr.dw_attr_val.v.val_addr = addr;
7129   add_dwarf_attr (die, &attr);
7130 }
7131
7132 /* Get the RTX from to an address DIE attribute.  */
7133
7134 static inline rtx
7135 AT_addr (dw_attr_ref a)
7136 {
7137   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7138   return a->dw_attr_val.v.val_addr;
7139 }
7140
7141 /* Add a file attribute value to a DIE.  */
7142
7143 static inline void
7144 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7145              struct dwarf_file_data *fd)
7146 {
7147   dw_attr_node attr;
7148
7149   attr.dw_attr = attr_kind;
7150   attr.dw_attr_val.val_class = dw_val_class_file;
7151   attr.dw_attr_val.v.val_file = fd;
7152   add_dwarf_attr (die, &attr);
7153 }
7154
7155 /* Get the dwarf_file_data from a file DIE attribute.  */
7156
7157 static inline struct dwarf_file_data *
7158 AT_file (dw_attr_ref a)
7159 {
7160   gcc_assert (a && AT_class (a) == dw_val_class_file);
7161   return a->dw_attr_val.v.val_file;
7162 }
7163
7164 /* Add a label identifier attribute value to a DIE.  */
7165
7166 static inline void
7167 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7168 {
7169   dw_attr_node attr;
7170
7171   attr.dw_attr = attr_kind;
7172   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7173   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7174   add_dwarf_attr (die, &attr);
7175 }
7176
7177 /* Add a section offset attribute value to a DIE, an offset into the
7178    debug_line section.  */
7179
7180 static inline void
7181 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7182                 const char *label)
7183 {
7184   dw_attr_node attr;
7185
7186   attr.dw_attr = attr_kind;
7187   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7188   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7189   add_dwarf_attr (die, &attr);
7190 }
7191
7192 /* Add a section offset attribute value to a DIE, an offset into the
7193    debug_macinfo section.  */
7194
7195 static inline void
7196 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7197                const char *label)
7198 {
7199   dw_attr_node attr;
7200
7201   attr.dw_attr = attr_kind;
7202   attr.dw_attr_val.val_class = dw_val_class_macptr;
7203   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7204   add_dwarf_attr (die, &attr);
7205 }
7206
7207 /* Add an offset attribute value to a DIE.  */
7208
7209 static inline void
7210 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7211                unsigned HOST_WIDE_INT offset)
7212 {
7213   dw_attr_node attr;
7214
7215   attr.dw_attr = attr_kind;
7216   attr.dw_attr_val.val_class = dw_val_class_offset;
7217   attr.dw_attr_val.v.val_offset = offset;
7218   add_dwarf_attr (die, &attr);
7219 }
7220
7221 /* Add an range_list attribute value to a DIE.  */
7222
7223 static void
7224 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7225                    long unsigned int offset)
7226 {
7227   dw_attr_node attr;
7228
7229   attr.dw_attr = attr_kind;
7230   attr.dw_attr_val.val_class = dw_val_class_range_list;
7231   attr.dw_attr_val.v.val_offset = offset;
7232   add_dwarf_attr (die, &attr);
7233 }
7234
7235 static inline const char *
7236 AT_lbl (dw_attr_ref a)
7237 {
7238   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7239                     || AT_class (a) == dw_val_class_lineptr
7240                     || AT_class (a) == dw_val_class_macptr));
7241   return a->dw_attr_val.v.val_lbl_id;
7242 }
7243
7244 /* Get the attribute of type attr_kind.  */
7245
7246 static dw_attr_ref
7247 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7248 {
7249   dw_attr_ref a;
7250   unsigned ix;
7251   dw_die_ref spec = NULL;
7252
7253   if (! die)
7254     return NULL;
7255
7256   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7257     if (a->dw_attr == attr_kind)
7258       return a;
7259     else if (a->dw_attr == DW_AT_specification
7260              || a->dw_attr == DW_AT_abstract_origin)
7261       spec = AT_ref (a);
7262
7263   if (spec)
7264     return get_AT (spec, attr_kind);
7265
7266   return NULL;
7267 }
7268
7269 /* Return the "low pc" attribute value, typically associated with a subprogram
7270    DIE.  Return null if the "low pc" attribute is either not present, or if it
7271    cannot be represented as an assembler label identifier.  */
7272
7273 static inline const char *
7274 get_AT_low_pc (dw_die_ref die)
7275 {
7276   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7277
7278   return a ? AT_lbl (a) : NULL;
7279 }
7280
7281 /* Return the "high pc" attribute value, typically associated with a subprogram
7282    DIE.  Return null if the "high pc" attribute is either not present, or if it
7283    cannot be represented as an assembler label identifier.  */
7284
7285 static inline const char *
7286 get_AT_hi_pc (dw_die_ref die)
7287 {
7288   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7289
7290   return a ? AT_lbl (a) : NULL;
7291 }
7292
7293 /* Return the value of the string attribute designated by ATTR_KIND, or
7294    NULL if it is not present.  */
7295
7296 static inline const char *
7297 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7298 {
7299   dw_attr_ref a = get_AT (die, attr_kind);
7300
7301   return a ? AT_string (a) : NULL;
7302 }
7303
7304 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7305    if it is not present.  */
7306
7307 static inline int
7308 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7309 {
7310   dw_attr_ref a = get_AT (die, attr_kind);
7311
7312   return a ? AT_flag (a) : 0;
7313 }
7314
7315 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7316    if it is not present.  */
7317
7318 static inline unsigned
7319 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7320 {
7321   dw_attr_ref a = get_AT (die, attr_kind);
7322
7323   return a ? AT_unsigned (a) : 0;
7324 }
7325
7326 static inline dw_die_ref
7327 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7328 {
7329   dw_attr_ref a = get_AT (die, attr_kind);
7330
7331   return a ? AT_ref (a) : NULL;
7332 }
7333
7334 static inline struct dwarf_file_data *
7335 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7336 {
7337   dw_attr_ref a = get_AT (die, attr_kind);
7338
7339   return a ? AT_file (a) : NULL;
7340 }
7341
7342 /* Return TRUE if the language is C or C++.  */
7343
7344 static inline bool
7345 is_c_family (void)
7346 {
7347   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7348
7349   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7350           || lang == DW_LANG_C99
7351           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7352 }
7353
7354 /* Return TRUE if the language is C++.  */
7355
7356 static inline bool
7357 is_cxx (void)
7358 {
7359   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7360
7361   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7362 }
7363
7364 /* Return TRUE if the language is Fortran.  */
7365
7366 static inline bool
7367 is_fortran (void)
7368 {
7369   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7370
7371   return (lang == DW_LANG_Fortran77
7372           || lang == DW_LANG_Fortran90
7373           || lang == DW_LANG_Fortran95);
7374 }
7375
7376 /* Return TRUE if the language is Java.  */
7377
7378 static inline bool
7379 is_java (void)
7380 {
7381   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7382
7383   return lang == DW_LANG_Java;
7384 }
7385
7386 /* Return TRUE if the language is Ada.  */
7387
7388 static inline bool
7389 is_ada (void)
7390 {
7391   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7392
7393   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7394 }
7395
7396 /* Remove the specified attribute if present.  */
7397
7398 static void
7399 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7400 {
7401   dw_attr_ref a;
7402   unsigned ix;
7403
7404   if (! die)
7405     return;
7406
7407   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7408     if (a->dw_attr == attr_kind)
7409       {
7410         if (AT_class (a) == dw_val_class_str)
7411           if (a->dw_attr_val.v.val_str->refcount)
7412             a->dw_attr_val.v.val_str->refcount--;
7413
7414         /* VEC_ordered_remove should help reduce the number of abbrevs
7415            that are needed.  */
7416         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7417         return;
7418       }
7419 }
7420
7421 /* Remove CHILD from its parent.  PREV must have the property that
7422    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7423
7424 static void
7425 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7426 {
7427   gcc_assert (child->die_parent == prev->die_parent);
7428   gcc_assert (prev->die_sib == child);
7429   if (prev == child)
7430     {
7431       gcc_assert (child->die_parent->die_child == child);
7432       prev = NULL;
7433     }
7434   else
7435     prev->die_sib = child->die_sib;
7436   if (child->die_parent->die_child == child)
7437     child->die_parent->die_child = prev;
7438 }
7439
7440 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7441    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7442
7443 static void
7444 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7445 {
7446   dw_die_ref parent = old_child->die_parent;
7447
7448   gcc_assert (parent == prev->die_parent);
7449   gcc_assert (prev->die_sib == old_child);
7450
7451   new_child->die_parent = parent;
7452   if (prev == old_child)
7453     {
7454       gcc_assert (parent->die_child == old_child);
7455       new_child->die_sib = new_child;
7456     }
7457   else
7458     {
7459       prev->die_sib = new_child;
7460       new_child->die_sib = old_child->die_sib;
7461     }
7462   if (old_child->die_parent->die_child == old_child)
7463     old_child->die_parent->die_child = new_child;
7464 }
7465
7466 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7467
7468 static void
7469 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7470 {
7471   dw_die_ref c;
7472   new_parent->die_child = old_parent->die_child;
7473   old_parent->die_child = NULL;
7474   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7475 }
7476
7477 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7478    matches TAG.  */
7479
7480 static void
7481 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7482 {
7483   dw_die_ref c;
7484
7485   c = die->die_child;
7486   if (c) do {
7487     dw_die_ref prev = c;
7488     c = c->die_sib;
7489     while (c->die_tag == tag)
7490       {
7491         remove_child_with_prev (c, prev);
7492         /* Might have removed every child.  */
7493         if (c == c->die_sib)
7494           return;
7495         c = c->die_sib;
7496       }
7497   } while (c != die->die_child);
7498 }
7499
7500 /* Add a CHILD_DIE as the last child of DIE.  */
7501
7502 static void
7503 add_child_die (dw_die_ref die, dw_die_ref child_die)
7504 {
7505   /* FIXME this should probably be an assert.  */
7506   if (! die || ! child_die)
7507     return;
7508   gcc_assert (die != child_die);
7509
7510   child_die->die_parent = die;
7511   if (die->die_child)
7512     {
7513       child_die->die_sib = die->die_child->die_sib;
7514       die->die_child->die_sib = child_die;
7515     }
7516   else
7517     child_die->die_sib = child_die;
7518   die->die_child = child_die;
7519 }
7520
7521 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7522    is the specification, to the end of PARENT's list of children.
7523    This is done by removing and re-adding it.  */
7524
7525 static void
7526 splice_child_die (dw_die_ref parent, dw_die_ref child)
7527 {
7528   dw_die_ref p;
7529
7530   /* We want the declaration DIE from inside the class, not the
7531      specification DIE at toplevel.  */
7532   if (child->die_parent != parent)
7533     {
7534       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7535
7536       if (tmp)
7537         child = tmp;
7538     }
7539
7540   gcc_assert (child->die_parent == parent
7541               || (child->die_parent
7542                   == get_AT_ref (parent, DW_AT_specification)));
7543
7544   for (p = child->die_parent->die_child; ; p = p->die_sib)
7545     if (p->die_sib == child)
7546       {
7547         remove_child_with_prev (child, p);
7548         break;
7549       }
7550
7551   add_child_die (parent, child);
7552 }
7553
7554 /* Return a pointer to a newly created DIE node.  */
7555
7556 static inline dw_die_ref
7557 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7558 {
7559   dw_die_ref die = GGC_CNEW (die_node);
7560
7561   die->die_tag = tag_value;
7562
7563   if (parent_die != NULL)
7564     add_child_die (parent_die, die);
7565   else
7566     {
7567       limbo_die_node *limbo_node;
7568
7569       limbo_node = GGC_CNEW (limbo_die_node);
7570       limbo_node->die = die;
7571       limbo_node->created_for = t;
7572       limbo_node->next = limbo_die_list;
7573       limbo_die_list = limbo_node;
7574     }
7575
7576   return die;
7577 }
7578
7579 /* Return the DIE associated with the given type specifier.  */
7580
7581 static inline dw_die_ref
7582 lookup_type_die (tree type)
7583 {
7584   return TYPE_SYMTAB_DIE (type);
7585 }
7586
7587 /* Equate a DIE to a given type specifier.  */
7588
7589 static inline void
7590 equate_type_number_to_die (tree type, dw_die_ref type_die)
7591 {
7592   TYPE_SYMTAB_DIE (type) = type_die;
7593 }
7594
7595 /* Returns a hash value for X (which really is a die_struct).  */
7596
7597 static hashval_t
7598 decl_die_table_hash (const void *x)
7599 {
7600   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7601 }
7602
7603 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7604
7605 static int
7606 decl_die_table_eq (const void *x, const void *y)
7607 {
7608   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7609 }
7610
7611 /* Return the DIE associated with a given declaration.  */
7612
7613 static inline dw_die_ref
7614 lookup_decl_die (tree decl)
7615 {
7616   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7617 }
7618
7619 /* Returns a hash value for X (which really is a var_loc_list).  */
7620
7621 static hashval_t
7622 decl_loc_table_hash (const void *x)
7623 {
7624   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7625 }
7626
7627 /* Return nonzero if decl_id of var_loc_list X is the same as
7628    UID of decl *Y.  */
7629
7630 static int
7631 decl_loc_table_eq (const void *x, const void *y)
7632 {
7633   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7634 }
7635
7636 /* Return the var_loc list associated with a given declaration.  */
7637
7638 static inline var_loc_list *
7639 lookup_decl_loc (const_tree decl)
7640 {
7641   if (!decl_loc_table)
7642     return NULL;
7643   return (var_loc_list *)
7644     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7645 }
7646
7647 /* Equate a DIE to a particular declaration.  */
7648
7649 static void
7650 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7651 {
7652   unsigned int decl_id = DECL_UID (decl);
7653   void **slot;
7654
7655   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7656   *slot = decl_die;
7657   decl_die->decl_id = decl_id;
7658 }
7659
7660 /* Add a variable location node to the linked list for DECL.  */
7661
7662 static void
7663 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7664 {
7665   unsigned int decl_id = DECL_UID (decl);
7666   var_loc_list *temp;
7667   void **slot;
7668
7669   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7670   if (*slot == NULL)
7671     {
7672       temp = GGC_CNEW (var_loc_list);
7673       temp->decl_id = decl_id;
7674       *slot = temp;
7675     }
7676   else
7677     temp = (var_loc_list *) *slot;
7678
7679   if (temp->last)
7680     {
7681       /* If the current location is the same as the end of the list,
7682          and either both or neither of the locations is uninitialized,
7683          we have nothing to do.  */
7684       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7685                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7686           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7687                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7688               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7689                    == VAR_INIT_STATUS_UNINITIALIZED)
7690                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7691                       == VAR_INIT_STATUS_UNINITIALIZED))))
7692         {
7693           /* Add LOC to the end of list and update LAST.  */
7694           temp->last->next = loc;
7695           temp->last = loc;
7696         }
7697     }
7698   /* Do not add empty location to the beginning of the list.  */
7699   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7700     {
7701       temp->first = loc;
7702       temp->last = loc;
7703     }
7704 }
7705 \f
7706 /* Keep track of the number of spaces used to indent the
7707    output of the debugging routines that print the structure of
7708    the DIE internal representation.  */
7709 static int print_indent;
7710
7711 /* Indent the line the number of spaces given by print_indent.  */
7712
7713 static inline void
7714 print_spaces (FILE *outfile)
7715 {
7716   fprintf (outfile, "%*s", print_indent, "");
7717 }
7718
7719 /* Print a type signature in hex.  */
7720
7721 static inline void
7722 print_signature (FILE *outfile, char *sig)
7723 {
7724   int i;
7725
7726   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
7727     fprintf (outfile, "%02x", sig[i] & 0xff);
7728 }
7729
7730 /* Print the information associated with a given DIE, and its children.
7731    This routine is a debugging aid only.  */
7732
7733 static void
7734 print_die (dw_die_ref die, FILE *outfile)
7735 {
7736   dw_attr_ref a;
7737   dw_die_ref c;
7738   unsigned ix;
7739
7740   print_spaces (outfile);
7741   fprintf (outfile, "DIE %4ld: %s\n",
7742            die->die_offset, dwarf_tag_name (die->die_tag));
7743   print_spaces (outfile);
7744   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7745   fprintf (outfile, " offset: %ld\n", die->die_offset);
7746   if (dwarf_version >= 4 && die->die_id.die_type_node)
7747     {
7748       print_spaces (outfile);
7749       fprintf (outfile, "  signature: ");
7750       print_signature (outfile, die->die_id.die_type_node->signature);
7751       fprintf (outfile, "\n");
7752     }
7753
7754   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7755     {
7756       print_spaces (outfile);
7757       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7758
7759       switch (AT_class (a))
7760         {
7761         case dw_val_class_addr:
7762           fprintf (outfile, "address");
7763           break;
7764         case dw_val_class_offset:
7765           fprintf (outfile, "offset");
7766           break;
7767         case dw_val_class_loc:
7768           fprintf (outfile, "location descriptor");
7769           break;
7770         case dw_val_class_loc_list:
7771           fprintf (outfile, "location list -> label:%s",
7772                    AT_loc_list (a)->ll_symbol);
7773           break;
7774         case dw_val_class_range_list:
7775           fprintf (outfile, "range list");
7776           break;
7777         case dw_val_class_const:
7778           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7779           break;
7780         case dw_val_class_unsigned_const:
7781           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7782           break;
7783         case dw_val_class_const_double:
7784           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
7785                             HOST_WIDE_INT_PRINT_UNSIGNED")",
7786                    a->dw_attr_val.v.val_double.high,
7787                    a->dw_attr_val.v.val_double.low);
7788           break;
7789         case dw_val_class_vec:
7790           fprintf (outfile, "floating-point or vector constant");
7791           break;
7792         case dw_val_class_flag:
7793           fprintf (outfile, "%u", AT_flag (a));
7794           break;
7795         case dw_val_class_die_ref:
7796           if (AT_ref (a) != NULL)
7797             {
7798               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
7799                 {
7800                   fprintf (outfile, "die -> signature: ");
7801                   print_signature (outfile,
7802                                    AT_ref (a)->die_id.die_type_node->signature);
7803                 }
7804               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
7805                 fprintf (outfile, "die -> label: %s",
7806                          AT_ref (a)->die_id.die_symbol);
7807               else
7808                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7809             }
7810           else
7811             fprintf (outfile, "die -> <null>");
7812           break;
7813         case dw_val_class_lbl_id:
7814         case dw_val_class_lineptr:
7815         case dw_val_class_macptr:
7816           fprintf (outfile, "label: %s", AT_lbl (a));
7817           break;
7818         case dw_val_class_str:
7819           if (AT_string (a) != NULL)
7820             fprintf (outfile, "\"%s\"", AT_string (a));
7821           else
7822             fprintf (outfile, "<null>");
7823           break;
7824         case dw_val_class_file:
7825           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7826                    AT_file (a)->emitted_number);
7827           break;
7828         case dw_val_class_data8:
7829           {
7830             int i;
7831
7832             for (i = 0; i < 8; i++)
7833               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
7834             break;
7835           }
7836         default:
7837           break;
7838         }
7839
7840       fprintf (outfile, "\n");
7841     }
7842
7843   if (die->die_child != NULL)
7844     {
7845       print_indent += 4;
7846       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7847       print_indent -= 4;
7848     }
7849   if (print_indent == 0)
7850     fprintf (outfile, "\n");
7851 }
7852
7853 /* Print the contents of the source code line number correspondence table.
7854    This routine is a debugging aid only.  */
7855
7856 static void
7857 print_dwarf_line_table (FILE *outfile)
7858 {
7859   unsigned i;
7860   dw_line_info_ref line_info;
7861
7862   fprintf (outfile, "\n\nDWARF source line information\n");
7863   for (i = 1; i < line_info_table_in_use; i++)
7864     {
7865       line_info = &line_info_table[i];
7866       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7867                line_info->dw_file_num,
7868                line_info->dw_line_num);
7869     }
7870
7871   fprintf (outfile, "\n\n");
7872 }
7873
7874 /* Print the information collected for a given DIE.  */
7875
7876 void
7877 debug_dwarf_die (dw_die_ref die)
7878 {
7879   print_die (die, stderr);
7880 }
7881
7882 /* Print all DWARF information collected for the compilation unit.
7883    This routine is a debugging aid only.  */
7884
7885 void
7886 debug_dwarf (void)
7887 {
7888   print_indent = 0;
7889   print_die (comp_unit_die, stderr);
7890   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7891     print_dwarf_line_table (stderr);
7892 }
7893 \f
7894 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7895    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7896    DIE that marks the start of the DIEs for this include file.  */
7897
7898 static dw_die_ref
7899 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7900 {
7901   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7902   dw_die_ref new_unit = gen_compile_unit_die (filename);
7903
7904   new_unit->die_sib = old_unit;
7905   return new_unit;
7906 }
7907
7908 /* Close an include-file CU and reopen the enclosing one.  */
7909
7910 static dw_die_ref
7911 pop_compile_unit (dw_die_ref old_unit)
7912 {
7913   dw_die_ref new_unit = old_unit->die_sib;
7914
7915   old_unit->die_sib = NULL;
7916   return new_unit;
7917 }
7918
7919 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7920 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7921
7922 /* Calculate the checksum of a location expression.  */
7923
7924 static inline void
7925 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7926 {
7927   int tem;
7928
7929   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7930   CHECKSUM (tem);
7931   CHECKSUM (loc->dw_loc_oprnd1);
7932   CHECKSUM (loc->dw_loc_oprnd2);
7933 }
7934
7935 /* Calculate the checksum of an attribute.  */
7936
7937 static void
7938 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7939 {
7940   dw_loc_descr_ref loc;
7941   rtx r;
7942
7943   CHECKSUM (at->dw_attr);
7944
7945   /* We don't care that this was compiled with a different compiler
7946      snapshot; if the output is the same, that's what matters.  */
7947   if (at->dw_attr == DW_AT_producer)
7948     return;
7949
7950   switch (AT_class (at))
7951     {
7952     case dw_val_class_const:
7953       CHECKSUM (at->dw_attr_val.v.val_int);
7954       break;
7955     case dw_val_class_unsigned_const:
7956       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7957       break;
7958     case dw_val_class_const_double:
7959       CHECKSUM (at->dw_attr_val.v.val_double);
7960       break;
7961     case dw_val_class_vec:
7962       CHECKSUM (at->dw_attr_val.v.val_vec);
7963       break;
7964     case dw_val_class_flag:
7965       CHECKSUM (at->dw_attr_val.v.val_flag);
7966       break;
7967     case dw_val_class_str:
7968       CHECKSUM_STRING (AT_string (at));
7969       break;
7970
7971     case dw_val_class_addr:
7972       r = AT_addr (at);
7973       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7974       CHECKSUM_STRING (XSTR (r, 0));
7975       break;
7976
7977     case dw_val_class_offset:
7978       CHECKSUM (at->dw_attr_val.v.val_offset);
7979       break;
7980
7981     case dw_val_class_loc:
7982       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7983         loc_checksum (loc, ctx);
7984       break;
7985
7986     case dw_val_class_die_ref:
7987       die_checksum (AT_ref (at), ctx, mark);
7988       break;
7989
7990     case dw_val_class_fde_ref:
7991     case dw_val_class_lbl_id:
7992     case dw_val_class_lineptr:
7993     case dw_val_class_macptr:
7994       break;
7995
7996     case dw_val_class_file:
7997       CHECKSUM_STRING (AT_file (at)->filename);
7998       break;
7999
8000     case dw_val_class_data8:
8001       CHECKSUM (at->dw_attr_val.v.val_data8);
8002       break;
8003
8004     default:
8005       break;
8006     }
8007 }
8008
8009 /* Calculate the checksum of a DIE.  */
8010
8011 static void
8012 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8013 {
8014   dw_die_ref c;
8015   dw_attr_ref a;
8016   unsigned ix;
8017
8018   /* To avoid infinite recursion.  */
8019   if (die->die_mark)
8020     {
8021       CHECKSUM (die->die_mark);
8022       return;
8023     }
8024   die->die_mark = ++(*mark);
8025
8026   CHECKSUM (die->die_tag);
8027
8028   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8029     attr_checksum (a, ctx, mark);
8030
8031   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8032 }
8033
8034 #undef CHECKSUM
8035 #undef CHECKSUM_STRING
8036
8037 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8038 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8039 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8040 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8041 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8042 #define CHECKSUM_ATTR(FOO) \
8043   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8044
8045 /* Calculate the checksum of a number in signed LEB128 format.  */
8046
8047 static void
8048 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8049 {
8050   unsigned char byte;
8051   bool more;
8052
8053   while (1)
8054     {
8055       byte = (value & 0x7f);
8056       value >>= 7;
8057       more = !((value == 0 && (byte & 0x40) == 0)
8058                 || (value == -1 && (byte & 0x40) != 0));
8059       if (more)
8060         byte |= 0x80;
8061       CHECKSUM (byte);
8062       if (!more)
8063         break;
8064     }
8065 }
8066
8067 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8068
8069 static void
8070 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8071 {
8072   while (1)
8073     {
8074       unsigned char byte = (value & 0x7f);
8075       value >>= 7;
8076       if (value != 0)
8077         /* More bytes to follow.  */
8078         byte |= 0x80;
8079       CHECKSUM (byte);
8080       if (value == 0)
8081         break;
8082     }
8083 }
8084
8085 /* Checksum the context of the DIE.  This adds the names of any
8086    surrounding namespaces or structures to the checksum.  */
8087
8088 static void
8089 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8090 {
8091   const char *name;
8092   dw_die_ref spec;
8093   int tag = die->die_tag;
8094
8095   if (tag != DW_TAG_namespace
8096       && tag != DW_TAG_structure_type
8097       && tag != DW_TAG_class_type)
8098     return;
8099
8100   name = get_AT_string (die, DW_AT_name);
8101
8102   spec = get_AT_ref (die, DW_AT_specification);
8103   if (spec != NULL)
8104     die = spec;
8105
8106   if (die->die_parent != NULL)
8107     checksum_die_context (die->die_parent, ctx);
8108
8109   CHECKSUM_ULEB128 ('C');
8110   CHECKSUM_ULEB128 (tag);
8111   if (name != NULL)
8112     CHECKSUM_STRING (name);
8113 }
8114
8115 /* Calculate the checksum of a location expression.  */
8116
8117 static inline void
8118 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8119 {
8120   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8121      were emitted as a DW_FORM_sdata instead of a location expression.  */
8122   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8123     {
8124       CHECKSUM_ULEB128 (DW_FORM_sdata);
8125       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8126       return;
8127     }
8128
8129   /* Otherwise, just checksum the raw location expression.  */
8130   while (loc != NULL)
8131     {
8132       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8133       CHECKSUM (loc->dw_loc_oprnd1);
8134       CHECKSUM (loc->dw_loc_oprnd2);
8135       loc = loc->dw_loc_next;
8136     }
8137 }
8138
8139 /* Calculate the checksum of an attribute.  */
8140
8141 static void
8142 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8143                        struct md5_ctx *ctx, int *mark)
8144 {
8145   dw_loc_descr_ref loc;
8146   rtx r;
8147
8148   if (AT_class (at) == dw_val_class_die_ref)
8149     {
8150       dw_die_ref target_die = AT_ref (at);
8151
8152       /* For pointer and reference types, we checksum only the (qualified)
8153          name of the target type (if there is a name).  For friend entries,
8154          we checksum only the (qualified) name of the target type or function.
8155          This allows the checksum to remain the same whether the target type
8156          is complete or not.  */
8157       if ((at->dw_attr == DW_AT_type
8158            && (tag == DW_TAG_pointer_type
8159                || tag == DW_TAG_reference_type
8160                || tag == DW_TAG_ptr_to_member_type))
8161           || (at->dw_attr == DW_AT_friend
8162               && tag == DW_TAG_friend))
8163         {
8164           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8165
8166           if (name_attr != NULL)
8167             {
8168               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8169
8170               if (decl == NULL)
8171                 decl = target_die;
8172               CHECKSUM_ULEB128 ('N');
8173               CHECKSUM_ULEB128 (at->dw_attr);
8174               if (decl->die_parent != NULL)
8175                 checksum_die_context (decl->die_parent, ctx);
8176               CHECKSUM_ULEB128 ('E');
8177               CHECKSUM_STRING (AT_string (name_attr));
8178               return;
8179             }
8180         }
8181
8182       /* For all other references to another DIE, we check to see if the
8183          target DIE has already been visited.  If it has, we emit a
8184          backward reference; if not, we descend recursively.  */
8185       if (target_die->die_mark > 0)
8186         {
8187           CHECKSUM_ULEB128 ('R');
8188           CHECKSUM_ULEB128 (at->dw_attr);
8189           CHECKSUM_ULEB128 (target_die->die_mark);
8190         }
8191       else
8192         {
8193           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8194
8195           if (decl == NULL)
8196             decl = target_die;
8197           target_die->die_mark = ++(*mark);
8198           CHECKSUM_ULEB128 ('T');
8199           CHECKSUM_ULEB128 (at->dw_attr);
8200           if (decl->die_parent != NULL)
8201             checksum_die_context (decl->die_parent, ctx);
8202           die_checksum_ordered (target_die, ctx, mark);
8203         }
8204       return;
8205     }
8206
8207   CHECKSUM_ULEB128 ('A');
8208   CHECKSUM_ULEB128 (at->dw_attr);
8209
8210   switch (AT_class (at))
8211     {
8212     case dw_val_class_const:
8213       CHECKSUM_ULEB128 (DW_FORM_sdata);
8214       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8215       break;
8216
8217     case dw_val_class_unsigned_const:
8218       CHECKSUM_ULEB128 (DW_FORM_sdata);
8219       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8220       break;
8221
8222     case dw_val_class_const_double:
8223       CHECKSUM_ULEB128 (DW_FORM_block);
8224       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8225       CHECKSUM (at->dw_attr_val.v.val_double);
8226       break;
8227
8228     case dw_val_class_vec:
8229       CHECKSUM_ULEB128 (DW_FORM_block);
8230       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8231       CHECKSUM (at->dw_attr_val.v.val_vec);
8232       break;
8233
8234     case dw_val_class_flag:
8235       CHECKSUM_ULEB128 (DW_FORM_flag);
8236       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8237       break;
8238
8239     case dw_val_class_str:
8240       CHECKSUM_ULEB128 (DW_FORM_string);
8241       CHECKSUM_STRING (AT_string (at));
8242       break;
8243
8244     case dw_val_class_addr:
8245       r = AT_addr (at);
8246       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8247       CHECKSUM_ULEB128 (DW_FORM_string);
8248       CHECKSUM_STRING (XSTR (r, 0));
8249       break;
8250
8251     case dw_val_class_offset:
8252       CHECKSUM_ULEB128 (DW_FORM_sdata);
8253       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8254       break;
8255
8256     case dw_val_class_loc:
8257       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8258         loc_checksum_ordered (loc, ctx);
8259       break;
8260
8261     case dw_val_class_fde_ref:
8262     case dw_val_class_lbl_id:
8263     case dw_val_class_lineptr:
8264     case dw_val_class_macptr:
8265       break;
8266
8267     case dw_val_class_file:
8268       CHECKSUM_ULEB128 (DW_FORM_string);
8269       CHECKSUM_STRING (AT_file (at)->filename);
8270       break;
8271
8272     case dw_val_class_data8:
8273       CHECKSUM (at->dw_attr_val.v.val_data8);
8274       break;
8275
8276     default:
8277       break;
8278     }
8279 }
8280
8281 struct checksum_attributes
8282 {
8283   dw_attr_ref at_name;
8284   dw_attr_ref at_type;
8285   dw_attr_ref at_friend;
8286   dw_attr_ref at_accessibility;
8287   dw_attr_ref at_address_class;
8288   dw_attr_ref at_allocated;
8289   dw_attr_ref at_artificial;
8290   dw_attr_ref at_associated;
8291   dw_attr_ref at_binary_scale;
8292   dw_attr_ref at_bit_offset;
8293   dw_attr_ref at_bit_size;
8294   dw_attr_ref at_bit_stride;
8295   dw_attr_ref at_byte_size;
8296   dw_attr_ref at_byte_stride;
8297   dw_attr_ref at_const_value;
8298   dw_attr_ref at_containing_type;
8299   dw_attr_ref at_count;
8300   dw_attr_ref at_data_location;
8301   dw_attr_ref at_data_member_location;
8302   dw_attr_ref at_decimal_scale;
8303   dw_attr_ref at_decimal_sign;
8304   dw_attr_ref at_default_value;
8305   dw_attr_ref at_digit_count;
8306   dw_attr_ref at_discr;
8307   dw_attr_ref at_discr_list;
8308   dw_attr_ref at_discr_value;
8309   dw_attr_ref at_encoding;
8310   dw_attr_ref at_endianity;
8311   dw_attr_ref at_explicit;
8312   dw_attr_ref at_is_optional;
8313   dw_attr_ref at_location;
8314   dw_attr_ref at_lower_bound;
8315   dw_attr_ref at_mutable;
8316   dw_attr_ref at_ordering;
8317   dw_attr_ref at_picture_string;
8318   dw_attr_ref at_prototyped;
8319   dw_attr_ref at_small;
8320   dw_attr_ref at_segment;
8321   dw_attr_ref at_string_length;
8322   dw_attr_ref at_threads_scaled;
8323   dw_attr_ref at_upper_bound;
8324   dw_attr_ref at_use_location;
8325   dw_attr_ref at_use_UTF8;
8326   dw_attr_ref at_variable_parameter;
8327   dw_attr_ref at_virtuality;
8328   dw_attr_ref at_visibility;
8329   dw_attr_ref at_vtable_elem_location;
8330 };
8331
8332 /* Collect the attributes that we will want to use for the checksum.  */
8333
8334 static void
8335 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8336 {
8337   dw_attr_ref a;
8338   unsigned ix;
8339
8340   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8341     {
8342       switch (a->dw_attr)
8343         {
8344         case DW_AT_name:
8345           attrs->at_name = a;
8346           break;
8347         case DW_AT_type:
8348           attrs->at_type = a;
8349           break;
8350         case DW_AT_friend:
8351           attrs->at_friend = a;
8352           break;
8353         case DW_AT_accessibility:
8354           attrs->at_accessibility = a;
8355           break;
8356         case DW_AT_address_class:
8357           attrs->at_address_class = a;
8358           break;
8359         case DW_AT_allocated:
8360           attrs->at_allocated = a;
8361           break;
8362         case DW_AT_artificial:
8363           attrs->at_artificial = a;
8364           break;
8365         case DW_AT_associated:
8366           attrs->at_associated = a;
8367           break;
8368         case DW_AT_binary_scale:
8369           attrs->at_binary_scale = a;
8370           break;
8371         case DW_AT_bit_offset:
8372           attrs->at_bit_offset = a;
8373           break;
8374         case DW_AT_bit_size:
8375           attrs->at_bit_size = a;
8376           break;
8377         case DW_AT_bit_stride:
8378           attrs->at_bit_stride = a;
8379           break;
8380         case DW_AT_byte_size:
8381           attrs->at_byte_size = a;
8382           break;
8383         case DW_AT_byte_stride:
8384           attrs->at_byte_stride = a;
8385           break;
8386         case DW_AT_const_value:
8387           attrs->at_const_value = a;
8388           break;
8389         case DW_AT_containing_type:
8390           attrs->at_containing_type = a;
8391           break;
8392         case DW_AT_count:
8393           attrs->at_count = a;
8394           break;
8395         case DW_AT_data_location:
8396           attrs->at_data_location = a;
8397           break;
8398         case DW_AT_data_member_location:
8399           attrs->at_data_member_location = a;
8400           break;
8401         case DW_AT_decimal_scale:
8402           attrs->at_decimal_scale = a;
8403           break;
8404         case DW_AT_decimal_sign:
8405           attrs->at_decimal_sign = a;
8406           break;
8407         case DW_AT_default_value:
8408           attrs->at_default_value = a;
8409           break;
8410         case DW_AT_digit_count:
8411           attrs->at_digit_count = a;
8412           break;
8413         case DW_AT_discr:
8414           attrs->at_discr = a;
8415           break;
8416         case DW_AT_discr_list:
8417           attrs->at_discr_list = a;
8418           break;
8419         case DW_AT_discr_value:
8420           attrs->at_discr_value = a;
8421           break;
8422         case DW_AT_encoding:
8423           attrs->at_encoding = a;
8424           break;
8425         case DW_AT_endianity:
8426           attrs->at_endianity = a;
8427           break;
8428         case DW_AT_explicit:
8429           attrs->at_explicit = a;
8430           break;
8431         case DW_AT_is_optional:
8432           attrs->at_is_optional = a;
8433           break;
8434         case DW_AT_location:
8435           attrs->at_location = a;
8436           break;
8437         case DW_AT_lower_bound:
8438           attrs->at_lower_bound = a;
8439           break;
8440         case DW_AT_mutable:
8441           attrs->at_mutable = a;
8442           break;
8443         case DW_AT_ordering:
8444           attrs->at_ordering = a;
8445           break;
8446         case DW_AT_picture_string:
8447           attrs->at_picture_string = a;
8448           break;
8449         case DW_AT_prototyped:
8450           attrs->at_prototyped = a;
8451           break;
8452         case DW_AT_small:
8453           attrs->at_small = a;
8454           break;
8455         case DW_AT_segment:
8456           attrs->at_segment = a;
8457           break;
8458         case DW_AT_string_length:
8459           attrs->at_string_length = a;
8460           break;
8461         case DW_AT_threads_scaled:
8462           attrs->at_threads_scaled = a;
8463           break;
8464         case DW_AT_upper_bound:
8465           attrs->at_upper_bound = a;
8466           break;
8467         case DW_AT_use_location:
8468           attrs->at_use_location = a;
8469           break;
8470         case DW_AT_use_UTF8:
8471           attrs->at_use_UTF8 = a;
8472           break;
8473         case DW_AT_variable_parameter:
8474           attrs->at_variable_parameter = a;
8475           break;
8476         case DW_AT_virtuality:
8477           attrs->at_virtuality = a;
8478           break;
8479         case DW_AT_visibility:
8480           attrs->at_visibility = a;
8481           break;
8482         case DW_AT_vtable_elem_location:
8483           attrs->at_vtable_elem_location = a;
8484           break;
8485         default:
8486           break;
8487         }
8488     }
8489 }
8490
8491 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
8492
8493 static void
8494 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8495 {
8496   dw_die_ref c;
8497   dw_die_ref decl;
8498   struct checksum_attributes attrs;
8499
8500   CHECKSUM_ULEB128 ('D');
8501   CHECKSUM_ULEB128 (die->die_tag);
8502
8503   memset (&attrs, 0, sizeof (attrs));
8504
8505   decl = get_AT_ref (die, DW_AT_specification);
8506   if (decl != NULL)
8507     collect_checksum_attributes (&attrs, decl);
8508   collect_checksum_attributes (&attrs, die);
8509
8510   CHECKSUM_ATTR (attrs.at_name);
8511   CHECKSUM_ATTR (attrs.at_accessibility);
8512   CHECKSUM_ATTR (attrs.at_address_class);
8513   CHECKSUM_ATTR (attrs.at_allocated);
8514   CHECKSUM_ATTR (attrs.at_artificial);
8515   CHECKSUM_ATTR (attrs.at_associated);
8516   CHECKSUM_ATTR (attrs.at_binary_scale);
8517   CHECKSUM_ATTR (attrs.at_bit_offset);
8518   CHECKSUM_ATTR (attrs.at_bit_size);
8519   CHECKSUM_ATTR (attrs.at_bit_stride);
8520   CHECKSUM_ATTR (attrs.at_byte_size);
8521   CHECKSUM_ATTR (attrs.at_byte_stride);
8522   CHECKSUM_ATTR (attrs.at_const_value);
8523   CHECKSUM_ATTR (attrs.at_containing_type);
8524   CHECKSUM_ATTR (attrs.at_count);
8525   CHECKSUM_ATTR (attrs.at_data_location);
8526   CHECKSUM_ATTR (attrs.at_data_member_location);
8527   CHECKSUM_ATTR (attrs.at_decimal_scale);
8528   CHECKSUM_ATTR (attrs.at_decimal_sign);
8529   CHECKSUM_ATTR (attrs.at_default_value);
8530   CHECKSUM_ATTR (attrs.at_digit_count);
8531   CHECKSUM_ATTR (attrs.at_discr);
8532   CHECKSUM_ATTR (attrs.at_discr_list);
8533   CHECKSUM_ATTR (attrs.at_discr_value);
8534   CHECKSUM_ATTR (attrs.at_encoding);
8535   CHECKSUM_ATTR (attrs.at_endianity);
8536   CHECKSUM_ATTR (attrs.at_explicit);
8537   CHECKSUM_ATTR (attrs.at_is_optional);
8538   CHECKSUM_ATTR (attrs.at_location);
8539   CHECKSUM_ATTR (attrs.at_lower_bound);
8540   CHECKSUM_ATTR (attrs.at_mutable);
8541   CHECKSUM_ATTR (attrs.at_ordering);
8542   CHECKSUM_ATTR (attrs.at_picture_string);
8543   CHECKSUM_ATTR (attrs.at_prototyped);
8544   CHECKSUM_ATTR (attrs.at_small);
8545   CHECKSUM_ATTR (attrs.at_segment);
8546   CHECKSUM_ATTR (attrs.at_string_length);
8547   CHECKSUM_ATTR (attrs.at_threads_scaled);
8548   CHECKSUM_ATTR (attrs.at_upper_bound);
8549   CHECKSUM_ATTR (attrs.at_use_location);
8550   CHECKSUM_ATTR (attrs.at_use_UTF8);
8551   CHECKSUM_ATTR (attrs.at_variable_parameter);
8552   CHECKSUM_ATTR (attrs.at_virtuality);
8553   CHECKSUM_ATTR (attrs.at_visibility);
8554   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
8555   CHECKSUM_ATTR (attrs.at_type);
8556   CHECKSUM_ATTR (attrs.at_friend);
8557
8558   /* Checksum the child DIEs, except for nested types and member functions.  */
8559   c = die->die_child;
8560   if (c) do {
8561     dw_attr_ref name_attr;
8562
8563     c = c->die_sib;
8564     name_attr = get_AT (c, DW_AT_name);
8565     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
8566         && name_attr != NULL)
8567       {
8568         CHECKSUM_ULEB128 ('S');
8569         CHECKSUM_ULEB128 (c->die_tag);
8570         CHECKSUM_STRING (AT_string (name_attr));
8571       }
8572     else
8573       {
8574         /* Mark this DIE so it gets processed when unmarking.  */
8575         if (c->die_mark == 0)
8576           c->die_mark = -1;
8577         die_checksum_ordered (c, ctx, mark);
8578       }
8579   } while (c != die->die_child);
8580
8581   CHECKSUM_ULEB128 (0);
8582 }
8583
8584 #undef CHECKSUM
8585 #undef CHECKSUM_STRING
8586 #undef CHECKSUM_ATTR
8587 #undef CHECKSUM_LEB128
8588 #undef CHECKSUM_ULEB128
8589
8590 /* Generate the type signature for DIE.  This is computed by generating an
8591    MD5 checksum over the DIE's tag, its relevant attributes, and its
8592    children.  Attributes that are references to other DIEs are processed
8593    by recursion, using the MARK field to prevent infinite recursion.
8594    If the DIE is nested inside a namespace or another type, we also
8595    need to include that context in the signature.  The lower 64 bits
8596    of the resulting MD5 checksum comprise the signature.  */
8597
8598 static void
8599 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
8600 {
8601   int mark;
8602   const char *name;
8603   unsigned char checksum[16];
8604   struct md5_ctx ctx;
8605   dw_die_ref decl;
8606
8607   name = get_AT_string (die, DW_AT_name);
8608   decl = get_AT_ref (die, DW_AT_specification);
8609
8610   /* First, compute a signature for just the type name (and its surrounding
8611      context, if any.  This is stored in the type unit DIE for link-time
8612      ODR (one-definition rule) checking.  */
8613
8614   if (is_cxx() && name != NULL)
8615     {
8616       md5_init_ctx (&ctx);
8617
8618       /* Checksum the names of surrounding namespaces and structures.  */
8619       if (decl != NULL && decl->die_parent != NULL)
8620         checksum_die_context (decl->die_parent, &ctx);
8621
8622       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
8623       md5_process_bytes (name, strlen (name) + 1, &ctx);
8624       md5_finish_ctx (&ctx, checksum);
8625
8626       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
8627     }
8628
8629   /* Next, compute the complete type signature.  */
8630
8631   md5_init_ctx (&ctx);
8632   mark = 1;
8633   die->die_mark = mark;
8634
8635   /* Checksum the names of surrounding namespaces and structures.  */
8636   if (decl != NULL && decl->die_parent != NULL)
8637     checksum_die_context (decl->die_parent, &ctx);
8638
8639   /* Checksum the DIE and its children.  */
8640   die_checksum_ordered (die, &ctx, &mark);
8641   unmark_all_dies (die);
8642   md5_finish_ctx (&ctx, checksum);
8643
8644   /* Store the signature in the type node and link the type DIE and the
8645      type node together.  */
8646   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
8647           DWARF_TYPE_SIGNATURE_SIZE);
8648   die->die_id.die_type_node = type_node;
8649   type_node->type_die = die;
8650
8651   /* If the DIE is a specification, link its declaration to the type node
8652      as well.  */
8653   if (decl != NULL)
8654     decl->die_id.die_type_node = type_node;
8655 }
8656
8657 /* Do the location expressions look same?  */
8658 static inline int
8659 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
8660 {
8661   return loc1->dw_loc_opc == loc2->dw_loc_opc
8662          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
8663          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
8664 }
8665
8666 /* Do the values look the same?  */
8667 static int
8668 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
8669 {
8670   dw_loc_descr_ref loc1, loc2;
8671   rtx r1, r2;
8672
8673   if (v1->val_class != v2->val_class)
8674     return 0;
8675
8676   switch (v1->val_class)
8677     {
8678     case dw_val_class_const:
8679       return v1->v.val_int == v2->v.val_int;
8680     case dw_val_class_unsigned_const:
8681       return v1->v.val_unsigned == v2->v.val_unsigned;
8682     case dw_val_class_const_double:
8683       return v1->v.val_double.high == v2->v.val_double.high
8684              && v1->v.val_double.low == v2->v.val_double.low;
8685     case dw_val_class_vec:
8686       if (v1->v.val_vec.length != v2->v.val_vec.length
8687           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
8688         return 0;
8689       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
8690                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
8691         return 0;
8692       return 1;
8693     case dw_val_class_flag:
8694       return v1->v.val_flag == v2->v.val_flag;
8695     case dw_val_class_str:
8696       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
8697
8698     case dw_val_class_addr:
8699       r1 = v1->v.val_addr;
8700       r2 = v2->v.val_addr;
8701       if (GET_CODE (r1) != GET_CODE (r2))
8702         return 0;
8703       return !rtx_equal_p (r1, r2);
8704
8705     case dw_val_class_offset:
8706       return v1->v.val_offset == v2->v.val_offset;
8707
8708     case dw_val_class_loc:
8709       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
8710            loc1 && loc2;
8711            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
8712         if (!same_loc_p (loc1, loc2, mark))
8713           return 0;
8714       return !loc1 && !loc2;
8715
8716     case dw_val_class_die_ref:
8717       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
8718
8719     case dw_val_class_fde_ref:
8720     case dw_val_class_lbl_id:
8721     case dw_val_class_lineptr:
8722     case dw_val_class_macptr:
8723       return 1;
8724
8725     case dw_val_class_file:
8726       return v1->v.val_file == v2->v.val_file;
8727
8728     case dw_val_class_data8:
8729       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
8730
8731     default:
8732       return 1;
8733     }
8734 }
8735
8736 /* Do the attributes look the same?  */
8737
8738 static int
8739 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
8740 {
8741   if (at1->dw_attr != at2->dw_attr)
8742     return 0;
8743
8744   /* We don't care that this was compiled with a different compiler
8745      snapshot; if the output is the same, that's what matters. */
8746   if (at1->dw_attr == DW_AT_producer)
8747     return 1;
8748
8749   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
8750 }
8751
8752 /* Do the dies look the same?  */
8753
8754 static int
8755 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
8756 {
8757   dw_die_ref c1, c2;
8758   dw_attr_ref a1;
8759   unsigned ix;
8760
8761   /* To avoid infinite recursion.  */
8762   if (die1->die_mark)
8763     return die1->die_mark == die2->die_mark;
8764   die1->die_mark = die2->die_mark = ++(*mark);
8765
8766   if (die1->die_tag != die2->die_tag)
8767     return 0;
8768
8769   if (VEC_length (dw_attr_node, die1->die_attr)
8770       != VEC_length (dw_attr_node, die2->die_attr))
8771     return 0;
8772
8773   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
8774     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
8775       return 0;
8776
8777   c1 = die1->die_child;
8778   c2 = die2->die_child;
8779   if (! c1)
8780     {
8781       if (c2)
8782         return 0;
8783     }
8784   else
8785     for (;;)
8786       {
8787         if (!same_die_p (c1, c2, mark))
8788           return 0;
8789         c1 = c1->die_sib;
8790         c2 = c2->die_sib;
8791         if (c1 == die1->die_child)
8792           {
8793             if (c2 == die2->die_child)
8794               break;
8795             else
8796               return 0;
8797           }
8798     }
8799
8800   return 1;
8801 }
8802
8803 /* Do the dies look the same?  Wrapper around same_die_p.  */
8804
8805 static int
8806 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
8807 {
8808   int mark = 0;
8809   int ret = same_die_p (die1, die2, &mark);
8810
8811   unmark_all_dies (die1);
8812   unmark_all_dies (die2);
8813
8814   return ret;
8815 }
8816
8817 /* The prefix to attach to symbols on DIEs in the current comdat debug
8818    info section.  */
8819 static char *comdat_symbol_id;
8820
8821 /* The index of the current symbol within the current comdat CU.  */
8822 static unsigned int comdat_symbol_number;
8823
8824 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8825    children, and set comdat_symbol_id accordingly.  */
8826
8827 static void
8828 compute_section_prefix (dw_die_ref unit_die)
8829 {
8830   const char *die_name = get_AT_string (unit_die, DW_AT_name);
8831   const char *base = die_name ? lbasename (die_name) : "anonymous";
8832   char *name = XALLOCAVEC (char, strlen (base) + 64);
8833   char *p;
8834   int i, mark;
8835   unsigned char checksum[16];
8836   struct md5_ctx ctx;
8837
8838   /* Compute the checksum of the DIE, then append part of it as hex digits to
8839      the name filename of the unit.  */
8840
8841   md5_init_ctx (&ctx);
8842   mark = 0;
8843   die_checksum (unit_die, &ctx, &mark);
8844   unmark_all_dies (unit_die);
8845   md5_finish_ctx (&ctx, checksum);
8846
8847   sprintf (name, "%s.", base);
8848   clean_symbol_name (name);
8849
8850   p = name + strlen (name);
8851   for (i = 0; i < 4; i++)
8852     {
8853       sprintf (p, "%.2x", checksum[i]);
8854       p += 2;
8855     }
8856
8857   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
8858   comdat_symbol_number = 0;
8859 }
8860
8861 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
8862
8863 static int
8864 is_type_die (dw_die_ref die)
8865 {
8866   switch (die->die_tag)
8867     {
8868     case DW_TAG_array_type:
8869     case DW_TAG_class_type:
8870     case DW_TAG_interface_type:
8871     case DW_TAG_enumeration_type:
8872     case DW_TAG_pointer_type:
8873     case DW_TAG_reference_type:
8874     case DW_TAG_string_type:
8875     case DW_TAG_structure_type:
8876     case DW_TAG_subroutine_type:
8877     case DW_TAG_union_type:
8878     case DW_TAG_ptr_to_member_type:
8879     case DW_TAG_set_type:
8880     case DW_TAG_subrange_type:
8881     case DW_TAG_base_type:
8882     case DW_TAG_const_type:
8883     case DW_TAG_file_type:
8884     case DW_TAG_packed_type:
8885     case DW_TAG_volatile_type:
8886     case DW_TAG_typedef:
8887       return 1;
8888     default:
8889       return 0;
8890     }
8891 }
8892
8893 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
8894    Basically, we want to choose the bits that are likely to be shared between
8895    compilations (types) and leave out the bits that are specific to individual
8896    compilations (functions).  */
8897
8898 static int
8899 is_comdat_die (dw_die_ref c)
8900 {
8901   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
8902      we do for stabs.  The advantage is a greater likelihood of sharing between
8903      objects that don't include headers in the same order (and therefore would
8904      put the base types in a different comdat).  jason 8/28/00 */
8905
8906   if (c->die_tag == DW_TAG_base_type)
8907     return 0;
8908
8909   if (c->die_tag == DW_TAG_pointer_type
8910       || c->die_tag == DW_TAG_reference_type
8911       || c->die_tag == DW_TAG_const_type
8912       || c->die_tag == DW_TAG_volatile_type)
8913     {
8914       dw_die_ref t = get_AT_ref (c, DW_AT_type);
8915
8916       return t ? is_comdat_die (t) : 0;
8917     }
8918
8919   return is_type_die (c);
8920 }
8921
8922 /* Returns 1 iff C is the sort of DIE that might be referred to from another
8923    compilation unit.  */
8924
8925 static int
8926 is_symbol_die (dw_die_ref c)
8927 {
8928   return (is_type_die (c)
8929           || is_declaration_die (c)
8930           || c->die_tag == DW_TAG_namespace
8931           || c->die_tag == DW_TAG_module);
8932 }
8933
8934 static char *
8935 gen_internal_sym (const char *prefix)
8936 {
8937   char buf[256];
8938
8939   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8940   return xstrdup (buf);
8941 }
8942
8943 /* Assign symbols to all worthy DIEs under DIE.  */
8944
8945 static void
8946 assign_symbol_names (dw_die_ref die)
8947 {
8948   dw_die_ref c;
8949
8950   if (is_symbol_die (die))
8951     {
8952       if (comdat_symbol_id)
8953         {
8954           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
8955
8956           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
8957                    comdat_symbol_id, comdat_symbol_number++);
8958           die->die_id.die_symbol = xstrdup (p);
8959         }
8960       else
8961         die->die_id.die_symbol = gen_internal_sym ("LDIE");
8962     }
8963
8964   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
8965 }
8966
8967 struct cu_hash_table_entry
8968 {
8969   dw_die_ref cu;
8970   unsigned min_comdat_num, max_comdat_num;
8971   struct cu_hash_table_entry *next;
8972 };
8973
8974 /* Routines to manipulate hash table of CUs.  */
8975 static hashval_t
8976 htab_cu_hash (const void *of)
8977 {
8978   const struct cu_hash_table_entry *const entry =
8979     (const struct cu_hash_table_entry *) of;
8980
8981   return htab_hash_string (entry->cu->die_id.die_symbol);
8982 }
8983
8984 static int
8985 htab_cu_eq (const void *of1, const void *of2)
8986 {
8987   const struct cu_hash_table_entry *const entry1 =
8988     (const struct cu_hash_table_entry *) of1;
8989   const struct die_struct *const entry2 = (const struct die_struct *) of2;
8990
8991   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
8992 }
8993
8994 static void
8995 htab_cu_del (void *what)
8996 {
8997   struct cu_hash_table_entry *next,
8998     *entry = (struct cu_hash_table_entry *) what;
8999
9000   while (entry)
9001     {
9002       next = entry->next;
9003       free (entry);
9004       entry = next;
9005     }
9006 }
9007
9008 /* Check whether we have already seen this CU and set up SYM_NUM
9009    accordingly.  */
9010 static int
9011 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9012 {
9013   struct cu_hash_table_entry dummy;
9014   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9015
9016   dummy.max_comdat_num = 0;
9017
9018   slot = (struct cu_hash_table_entry **)
9019     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9020         INSERT);
9021   entry = *slot;
9022
9023   for (; entry; last = entry, entry = entry->next)
9024     {
9025       if (same_die_p_wrap (cu, entry->cu))
9026         break;
9027     }
9028
9029   if (entry)
9030     {
9031       *sym_num = entry->min_comdat_num;
9032       return 1;
9033     }
9034
9035   entry = XCNEW (struct cu_hash_table_entry);
9036   entry->cu = cu;
9037   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9038   entry->next = *slot;
9039   *slot = entry;
9040
9041   return 0;
9042 }
9043
9044 /* Record SYM_NUM to record of CU in HTABLE.  */
9045 static void
9046 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9047 {
9048   struct cu_hash_table_entry **slot, *entry;
9049
9050   slot = (struct cu_hash_table_entry **)
9051     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9052         NO_INSERT);
9053   entry = *slot;
9054
9055   entry->max_comdat_num = sym_num;
9056 }
9057
9058 /* Traverse the DIE (which is always comp_unit_die), and set up
9059    additional compilation units for each of the include files we see
9060    bracketed by BINCL/EINCL.  */
9061
9062 static void
9063 break_out_includes (dw_die_ref die)
9064 {
9065   dw_die_ref c;
9066   dw_die_ref unit = NULL;
9067   limbo_die_node *node, **pnode;
9068   htab_t cu_hash_table;
9069
9070   c = die->die_child;
9071   if (c) do {
9072     dw_die_ref prev = c;
9073     c = c->die_sib;
9074     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9075            || (unit && is_comdat_die (c)))
9076       {
9077         dw_die_ref next = c->die_sib;
9078
9079         /* This DIE is for a secondary CU; remove it from the main one.  */
9080         remove_child_with_prev (c, prev);
9081
9082         if (c->die_tag == DW_TAG_GNU_BINCL)
9083           unit = push_new_compile_unit (unit, c);
9084         else if (c->die_tag == DW_TAG_GNU_EINCL)
9085           unit = pop_compile_unit (unit);
9086         else
9087           add_child_die (unit, c);
9088         c = next;
9089         if (c == die->die_child)
9090           break;
9091       }
9092   } while (c != die->die_child);
9093
9094 #if 0
9095   /* We can only use this in debugging, since the frontend doesn't check
9096      to make sure that we leave every include file we enter.  */
9097   gcc_assert (!unit);
9098 #endif
9099
9100   assign_symbol_names (die);
9101   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9102   for (node = limbo_die_list, pnode = &limbo_die_list;
9103        node;
9104        node = node->next)
9105     {
9106       int is_dupl;
9107
9108       compute_section_prefix (node->die);
9109       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9110                         &comdat_symbol_number);
9111       assign_symbol_names (node->die);
9112       if (is_dupl)
9113         *pnode = node->next;
9114       else
9115         {
9116           pnode = &node->next;
9117           record_comdat_symbol_number (node->die, cu_hash_table,
9118                 comdat_symbol_number);
9119         }
9120     }
9121   htab_delete (cu_hash_table);
9122 }
9123
9124 /* Return non-zero if this DIE is a declaration.  */
9125
9126 static int
9127 is_declaration_die (dw_die_ref die)
9128 {
9129   dw_attr_ref a;
9130   unsigned ix;
9131
9132   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9133     if (a->dw_attr == DW_AT_declaration)
9134       return 1;
9135
9136   return 0;
9137 }
9138
9139 /* Return non-zero if this is a type DIE that should be moved to a
9140    COMDAT .debug_types section.  */
9141
9142 static int
9143 should_move_die_to_comdat (dw_die_ref die)
9144 {
9145   switch (die->die_tag)
9146     {
9147     case DW_TAG_class_type:
9148     case DW_TAG_structure_type:
9149     case DW_TAG_enumeration_type:
9150     case DW_TAG_union_type:
9151       /* Don't move declarations or inlined instances.  */
9152       if (is_declaration_die (die) || get_AT (die, DW_AT_abstract_origin))
9153         return 0;
9154       return 1;
9155     case DW_TAG_array_type:
9156     case DW_TAG_interface_type:
9157     case DW_TAG_pointer_type:
9158     case DW_TAG_reference_type:
9159     case DW_TAG_string_type:
9160     case DW_TAG_subroutine_type:
9161     case DW_TAG_ptr_to_member_type:
9162     case DW_TAG_set_type:
9163     case DW_TAG_subrange_type:
9164     case DW_TAG_base_type:
9165     case DW_TAG_const_type:
9166     case DW_TAG_file_type:
9167     case DW_TAG_packed_type:
9168     case DW_TAG_volatile_type:
9169     case DW_TAG_typedef:
9170     default:
9171       return 0;
9172     }
9173 }
9174
9175 /* Make a clone of DIE.  */
9176
9177 static dw_die_ref
9178 clone_die (dw_die_ref die)
9179 {
9180   dw_die_ref clone;
9181   dw_attr_ref a;
9182   unsigned ix;
9183
9184   clone = GGC_CNEW (die_node);
9185   clone->die_tag = die->die_tag;
9186
9187   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9188     add_dwarf_attr (clone, a);
9189
9190   return clone;
9191 }
9192
9193 /* Make a clone of the tree rooted at DIE.  */
9194
9195 static dw_die_ref
9196 clone_tree (dw_die_ref die)
9197 {
9198   dw_die_ref c;
9199   dw_die_ref clone = clone_die (die);
9200
9201   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9202
9203   return clone;
9204 }
9205
9206 /* Make a clone of DIE as a declaration.  */
9207
9208 static dw_die_ref
9209 clone_as_declaration (dw_die_ref die)
9210 {
9211   dw_die_ref clone;
9212   dw_die_ref decl;
9213   dw_attr_ref a;
9214   unsigned ix;
9215
9216   /* If the DIE is already a declaration, just clone it.  */
9217   if (is_declaration_die (die))
9218     return clone_die (die);
9219
9220   /* If the DIE is a specification, just clone its declaration DIE.  */
9221   decl = get_AT_ref (die, DW_AT_specification);
9222   if (decl != NULL)
9223     return clone_die (decl);
9224
9225   clone = GGC_CNEW (die_node);
9226   clone->die_tag = die->die_tag;
9227
9228   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9229     {
9230       /* We don't want to copy over all attributes.
9231          For example we don't want DW_AT_byte_size because otherwise we will no
9232          longer have a declaration and GDB will treat it as a definition.  */
9233
9234       switch (a->dw_attr)
9235         {
9236         case DW_AT_artificial:
9237         case DW_AT_containing_type:
9238         case DW_AT_external:
9239         case DW_AT_name:
9240         case DW_AT_type:
9241         case DW_AT_virtuality:
9242         case DW_AT_MIPS_linkage_name:
9243           add_dwarf_attr (clone, a);
9244           break;
9245         case DW_AT_byte_size:
9246         default:
9247           break;
9248         }
9249     }
9250
9251   if (die->die_id.die_type_node)
9252     add_AT_die_ref (clone, DW_AT_signature, die);
9253
9254   add_AT_flag (clone, DW_AT_declaration, 1);
9255   return clone;
9256 }
9257
9258 /* Copy the declaration context to the new compile unit DIE.  This includes
9259    any surrounding namespace or type declarations.  If the DIE has an
9260    AT_specification attribute, it also includes attributes and children
9261    attached to the specification.  */
9262
9263 static void
9264 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9265 {
9266   dw_die_ref decl;
9267   dw_die_ref new_decl;
9268
9269   decl = get_AT_ref (die, DW_AT_specification);
9270   if (decl == NULL)
9271     decl = die;
9272   else
9273     {
9274       unsigned ix;
9275       dw_die_ref c;
9276       dw_attr_ref a;
9277
9278       /* Copy the type node pointer from the new DIE to the original
9279          declaration DIE so we can forward references later.  */
9280       decl->die_id.die_type_node = die->die_id.die_type_node;
9281
9282       remove_AT (die, DW_AT_specification);
9283
9284       for (ix = 0; VEC_iterate (dw_attr_node, decl->die_attr, ix, a); ix++)
9285         {
9286           if (a->dw_attr != DW_AT_name
9287               && a->dw_attr != DW_AT_declaration
9288               && a->dw_attr != DW_AT_external)
9289             add_dwarf_attr (die, a);
9290         }
9291
9292       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9293     }
9294
9295   if (decl->die_parent != NULL
9296       && decl->die_parent->die_tag != DW_TAG_compile_unit
9297       && decl->die_parent->die_tag != DW_TAG_type_unit)
9298     {
9299       new_decl = copy_ancestor_tree (unit, decl, NULL);
9300       if (new_decl != NULL)
9301         {
9302           remove_AT (new_decl, DW_AT_signature);
9303           add_AT_specification (die, new_decl);
9304         }
9305     }
9306 }
9307
9308 /* Generate the skeleton ancestor tree for the given NODE, then clone
9309    the DIE and add the clone into the tree.  */
9310
9311 static void
9312 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9313 {
9314   if (node->new_die != NULL)
9315     return;
9316
9317   node->new_die = clone_as_declaration (node->old_die);
9318
9319   if (node->parent != NULL)
9320     {
9321       generate_skeleton_ancestor_tree (node->parent);
9322       add_child_die (node->parent->new_die, node->new_die);
9323     }
9324 }
9325
9326 /* Generate a skeleton tree of DIEs containing any declarations that are
9327    found in the original tree.  We traverse the tree looking for declaration
9328    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9329
9330 static void
9331 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9332 {
9333   skeleton_chain_node node;
9334   dw_die_ref c;
9335   dw_die_ref first;
9336   dw_die_ref prev = NULL;
9337   dw_die_ref next = NULL;
9338
9339   node.parent = parent;
9340
9341   first = c = parent->old_die->die_child;
9342   if (c)
9343     next = c->die_sib;
9344   if (c) do {
9345     if (prev == NULL || prev->die_sib == c)
9346       prev = c;
9347     c = next;
9348     next = (c == first ? NULL : c->die_sib);
9349     node.old_die = c;
9350     node.new_die = NULL;
9351     if (is_declaration_die (c))
9352       {
9353         /* Clone the existing DIE, move the original to the skeleton
9354            tree (which is in the main CU), and put the clone, with
9355            all the original's children, where the original came from.  */
9356         dw_die_ref clone = clone_die (c);
9357         move_all_children (c, clone);
9358
9359         replace_child (c, clone, prev);
9360         generate_skeleton_ancestor_tree (parent);
9361         add_child_die (parent->new_die, c);
9362         node.new_die = c;
9363         c = clone;
9364       }
9365     generate_skeleton_bottom_up (&node);
9366   } while (next != NULL);
9367 }
9368
9369 /* Wrapper function for generate_skeleton_bottom_up.  */
9370
9371 static dw_die_ref
9372 generate_skeleton (dw_die_ref die)
9373 {
9374   skeleton_chain_node node;
9375
9376   node.old_die = die;
9377   node.new_die = NULL;
9378   node.parent = NULL;
9379
9380   /* If this type definition is nested inside another type,
9381      always leave at least a declaration in its place.  */
9382   if (die->die_parent != NULL && is_type_die (die->die_parent))
9383     node.new_die = clone_as_declaration (die);
9384
9385   generate_skeleton_bottom_up (&node);
9386   return node.new_die;
9387 }
9388
9389 /* Remove the DIE from its parent, possibly replacing it with a cloned
9390    declaration.  The original DIE will be moved to a new compile unit
9391    so that existing references to it follow it to the new location.  If
9392    any of the original DIE's descendants is a declaration, we need to
9393    replace the original DIE with a skeleton tree and move the
9394    declarations back into the skeleton tree.  */
9395
9396 static dw_die_ref
9397 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9398 {
9399   dw_die_ref skeleton;
9400
9401   skeleton = generate_skeleton (child);
9402   if (skeleton == NULL)
9403     remove_child_with_prev (child, prev);
9404   else
9405     {
9406       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9407       replace_child (child, skeleton, prev);
9408     }
9409
9410   return skeleton;
9411 }
9412
9413 /* Traverse the DIE and set up additional .debug_types sections for each
9414    type worthy of being placed in a COMDAT section.  */
9415
9416 static void
9417 break_out_comdat_types (dw_die_ref die)
9418 {
9419   dw_die_ref c;
9420   dw_die_ref first;
9421   dw_die_ref prev = NULL;
9422   dw_die_ref next = NULL;
9423   dw_die_ref unit = NULL;
9424
9425   first = c = die->die_child;
9426   if (c)
9427     next = c->die_sib;
9428   if (c) do {
9429     if (prev == NULL || prev->die_sib == c)
9430       prev = c;
9431     c = next;
9432     next = (c == first ? NULL : c->die_sib);
9433     if (should_move_die_to_comdat (c))
9434       {
9435         dw_die_ref replacement;
9436         comdat_type_node_ref type_node;
9437
9438         /* Create a new type unit DIE as the root for the new tree, and
9439            add it to the list of comdat types.  */
9440         unit = new_die (DW_TAG_type_unit, NULL, NULL);
9441         add_AT_unsigned (unit, DW_AT_language,
9442                          get_AT_unsigned (comp_unit_die, DW_AT_language));
9443         type_node = GGC_CNEW (comdat_type_node);
9444         type_node->root_die = unit;
9445         type_node->next = comdat_type_list;
9446         comdat_type_list = type_node;
9447
9448         /* Generate the type signature.  */
9449         generate_type_signature (c, type_node);
9450
9451         /* Copy the declaration context, attributes, and children of the
9452            declaration into the new compile unit DIE.  */
9453         copy_declaration_context (unit, c);
9454
9455         /* Remove this DIE from the main CU.  */
9456         replacement = remove_child_or_replace_with_skeleton (c, prev);
9457
9458         /* Break out nested types into their own type units.  */
9459         break_out_comdat_types (c);
9460
9461         /* Add the DIE to the new compunit.  */
9462         add_child_die (unit, c);
9463
9464         if (replacement != NULL)
9465           c = replacement;
9466       }
9467     else if (c->die_tag == DW_TAG_namespace
9468              || c->die_tag == DW_TAG_class_type
9469              || c->die_tag == DW_TAG_structure_type
9470              || c->die_tag == DW_TAG_union_type)
9471       {
9472         /* Look for nested types that can be broken out.  */
9473         break_out_comdat_types (c);
9474       }
9475   } while (next != NULL);
9476 }
9477
9478 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
9479
9480 struct decl_table_entry
9481 {
9482   dw_die_ref orig;
9483   dw_die_ref copy;
9484 };
9485
9486 /* Routines to manipulate hash table of copied declarations.  */
9487
9488 static hashval_t
9489 htab_decl_hash (const void *of)
9490 {
9491   const struct decl_table_entry *const entry =
9492     (const struct decl_table_entry *) of;
9493
9494   return htab_hash_pointer (entry->orig);
9495 }
9496
9497 static int
9498 htab_decl_eq (const void *of1, const void *of2)
9499 {
9500   const struct decl_table_entry *const entry1 =
9501     (const struct decl_table_entry *) of1;
9502   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9503
9504   return entry1->orig == entry2;
9505 }
9506
9507 static void
9508 htab_decl_del (void *what)
9509 {
9510   struct decl_table_entry *entry = (struct decl_table_entry *) what;
9511
9512   free (entry);
9513 }
9514
9515 /* Copy DIE and its ancestors, up to, but not including, the compile unit 
9516    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
9517    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
9518    to check if the ancestor has already been copied into UNIT.  */
9519
9520 static dw_die_ref
9521 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
9522 {
9523   dw_die_ref parent = die->die_parent;
9524   dw_die_ref new_parent = unit;
9525   dw_die_ref copy;
9526   void **slot = NULL;
9527   struct decl_table_entry *entry = NULL;
9528
9529   if (decl_table)
9530     {
9531       /* Check if the entry has already been copied to UNIT.  */
9532       slot = htab_find_slot_with_hash (decl_table, die,
9533                                        htab_hash_pointer (die), INSERT);
9534       if (*slot != HTAB_EMPTY_ENTRY)
9535         {
9536           entry = (struct decl_table_entry *) *slot;
9537           return entry->copy;
9538         }
9539
9540       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
9541       entry = XCNEW (struct decl_table_entry);
9542       entry->orig = die;
9543       entry->copy = NULL;
9544       *slot = entry;
9545     }
9546
9547   if (parent != NULL)
9548     {
9549       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
9550       if (spec != NULL)
9551         parent = spec;
9552       if (parent->die_tag != DW_TAG_compile_unit
9553           && parent->die_tag != DW_TAG_type_unit)
9554         new_parent = copy_ancestor_tree (unit, parent, decl_table);
9555     }
9556
9557   copy = clone_as_declaration (die);
9558   add_child_die (new_parent, copy);
9559
9560   if (decl_table != NULL)
9561     {
9562       /* Make sure the copy is marked as part of the type unit.  */
9563       copy->die_mark = 1;
9564       /* Record the pointer to the copy.  */
9565       entry->copy = copy;
9566     }
9567
9568   return copy;
9569 }
9570
9571 /* Walk the DIE and its children, looking for references to incomplete
9572    or trivial types that are unmarked (i.e., that are not in the current
9573    type_unit).  */
9574
9575 static void
9576 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
9577 {
9578   dw_die_ref c;
9579   dw_attr_ref a;
9580   unsigned ix;
9581
9582   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9583     {
9584       if (AT_class (a) == dw_val_class_die_ref)
9585         {
9586           dw_die_ref targ = AT_ref (a);
9587           comdat_type_node_ref type_node = targ->die_id.die_type_node;
9588           void **slot;
9589           struct decl_table_entry *entry;
9590
9591           if (targ->die_mark != 0 || type_node != NULL)
9592             continue;
9593
9594           slot = htab_find_slot_with_hash (decl_table, targ,
9595                                            htab_hash_pointer (targ), INSERT);
9596
9597           if (*slot != HTAB_EMPTY_ENTRY)
9598             {
9599               /* TARG has already been copied, so we just need to
9600                  modify the reference to point to the copy.  */
9601               entry = (struct decl_table_entry *) *slot;
9602               a->dw_attr_val.v.val_die_ref.die = entry->copy;
9603             }
9604           else
9605             {
9606               dw_die_ref parent = unit;
9607               dw_die_ref copy = clone_tree (targ);
9608
9609               /* Make sure the cloned tree is marked as part of the
9610                  type unit.  */
9611               mark_dies (copy);
9612
9613               /* Record in DECL_TABLE that TARG has been copied.
9614                  Need to do this now, before the recursive call,
9615                  because DECL_TABLE may be expanded and SLOT
9616                  would no longer be a valid pointer.  */
9617               entry = XCNEW (struct decl_table_entry);
9618               entry->orig = targ;
9619               entry->copy = copy;
9620               *slot = entry;
9621
9622               /* If TARG has surrounding context, copy its ancestor tree
9623                  into the new type unit.  */
9624               if (targ->die_parent != NULL
9625                   && targ->die_parent->die_tag != DW_TAG_compile_unit
9626                   && targ->die_parent->die_tag != DW_TAG_type_unit)
9627                 parent = copy_ancestor_tree (unit, targ->die_parent,
9628                                              decl_table);
9629
9630               add_child_die (parent, copy);
9631               a->dw_attr_val.v.val_die_ref.die = copy;
9632
9633               /* Make sure the newly-copied DIE is walked.  If it was
9634                  installed in a previously-added context, it won't
9635                  get visited otherwise.  */
9636               if (parent != unit)
9637                 copy_decls_walk (unit, parent, decl_table);
9638             }
9639         }
9640     }
9641
9642   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
9643 }
9644
9645 /* Copy declarations for "unworthy" types into the new comdat section.
9646    Incomplete types, modified types, and certain other types aren't broken
9647    out into comdat sections of their own, so they don't have a signature,
9648    and we need to copy the declaration into the same section so that we
9649    don't have an external reference.  */
9650
9651 static void
9652 copy_decls_for_unworthy_types (dw_die_ref unit)
9653 {
9654   htab_t decl_table;
9655
9656   mark_dies (unit);
9657   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
9658   copy_decls_walk (unit, unit, decl_table);
9659   htab_delete (decl_table);
9660   unmark_dies (unit);
9661 }
9662
9663 /* Traverse the DIE and add a sibling attribute if it may have the
9664    effect of speeding up access to siblings.  To save some space,
9665    avoid generating sibling attributes for DIE's without children.  */
9666
9667 static void
9668 add_sibling_attributes (dw_die_ref die)
9669 {
9670   dw_die_ref c;
9671
9672   if (! die->die_child)
9673     return;
9674
9675   if (die->die_parent && die != die->die_parent->die_child)
9676     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
9677
9678   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
9679 }
9680
9681 /* Output all location lists for the DIE and its children.  */
9682
9683 static void
9684 output_location_lists (dw_die_ref die)
9685 {
9686   dw_die_ref c;
9687   dw_attr_ref a;
9688   unsigned ix;
9689
9690   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9691     if (AT_class (a) == dw_val_class_loc_list)
9692       output_loc_list (AT_loc_list (a));
9693
9694   FOR_EACH_CHILD (die, c, output_location_lists (c));
9695 }
9696
9697 /* The format of each DIE (and its attribute value pairs) is encoded in an
9698    abbreviation table.  This routine builds the abbreviation table and assigns
9699    a unique abbreviation id for each abbreviation entry.  The children of each
9700    die are visited recursively.  */
9701
9702 static void
9703 build_abbrev_table (dw_die_ref die)
9704 {
9705   unsigned long abbrev_id;
9706   unsigned int n_alloc;
9707   dw_die_ref c;
9708   dw_attr_ref a;
9709   unsigned ix;
9710
9711   /* Scan the DIE references, and mark as external any that refer to
9712      DIEs from other CUs (i.e. those which are not marked).  */
9713   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9714     if (AT_class (a) == dw_val_class_die_ref
9715         && AT_ref (a)->die_mark == 0)
9716       {
9717         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
9718         set_AT_ref_external (a, 1);
9719       }
9720
9721   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
9722     {
9723       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
9724       dw_attr_ref die_a, abbrev_a;
9725       unsigned ix;
9726       bool ok = true;
9727
9728       if (abbrev->die_tag != die->die_tag)
9729         continue;
9730       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
9731         continue;
9732
9733       if (VEC_length (dw_attr_node, abbrev->die_attr)
9734           != VEC_length (dw_attr_node, die->die_attr))
9735         continue;
9736
9737       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
9738         {
9739           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
9740           if ((abbrev_a->dw_attr != die_a->dw_attr)
9741               || (value_format (abbrev_a) != value_format (die_a)))
9742             {
9743               ok = false;
9744               break;
9745             }
9746         }
9747       if (ok)
9748         break;
9749     }
9750
9751   if (abbrev_id >= abbrev_die_table_in_use)
9752     {
9753       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
9754         {
9755           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
9756           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
9757                                             n_alloc);
9758
9759           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
9760                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
9761           abbrev_die_table_allocated = n_alloc;
9762         }
9763
9764       ++abbrev_die_table_in_use;
9765       abbrev_die_table[abbrev_id] = die;
9766     }
9767
9768   die->die_abbrev = abbrev_id;
9769   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
9770 }
9771 \f
9772 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
9773
9774 static int
9775 constant_size (unsigned HOST_WIDE_INT value)
9776 {
9777   int log;
9778
9779   if (value == 0)
9780     log = 0;
9781   else
9782     log = floor_log2 (value);
9783
9784   log = log / 8;
9785   log = 1 << (floor_log2 (log) + 1);
9786
9787   return log;
9788 }
9789
9790 /* Return the size of a DIE as it is represented in the
9791    .debug_info section.  */
9792
9793 static unsigned long
9794 size_of_die (dw_die_ref die)
9795 {
9796   unsigned long size = 0;
9797   dw_attr_ref a;
9798   unsigned ix;
9799
9800   size += size_of_uleb128 (die->die_abbrev);
9801   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9802     {
9803       switch (AT_class (a))
9804         {
9805         case dw_val_class_addr:
9806           size += DWARF2_ADDR_SIZE;
9807           break;
9808         case dw_val_class_offset:
9809           size += DWARF_OFFSET_SIZE;
9810           break;
9811         case dw_val_class_loc:
9812           {
9813             unsigned long lsize = size_of_locs (AT_loc (a));
9814
9815             /* Block length.  */
9816             size += constant_size (lsize);
9817             size += lsize;
9818           }
9819           break;
9820         case dw_val_class_loc_list:
9821           size += DWARF_OFFSET_SIZE;
9822           break;
9823         case dw_val_class_range_list:
9824           size += DWARF_OFFSET_SIZE;
9825           break;
9826         case dw_val_class_const:
9827           size += size_of_sleb128 (AT_int (a));
9828           break;
9829         case dw_val_class_unsigned_const:
9830           size += constant_size (AT_unsigned (a));
9831           break;
9832         case dw_val_class_const_double:
9833           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
9834           if (HOST_BITS_PER_WIDE_INT >= 64)
9835             size++; /* block */
9836           break;
9837         case dw_val_class_vec:
9838           size += constant_size (a->dw_attr_val.v.val_vec.length
9839                                  * a->dw_attr_val.v.val_vec.elt_size)
9840                   + a->dw_attr_val.v.val_vec.length
9841                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
9842           break;
9843         case dw_val_class_flag:
9844           size += 1;
9845           break;
9846         case dw_val_class_die_ref:
9847           if (AT_ref_external (a))
9848             {
9849               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
9850                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
9851                  is sized by target address length, whereas in DWARF3
9852                  it's always sized as an offset.  */
9853               if (dwarf_version >= 4)
9854                 size += DWARF_TYPE_SIGNATURE_SIZE;
9855               else if (dwarf_version == 2)
9856                 size += DWARF2_ADDR_SIZE;
9857               else
9858                 size += DWARF_OFFSET_SIZE;
9859             }
9860           else
9861             size += DWARF_OFFSET_SIZE;
9862           break;
9863         case dw_val_class_fde_ref:
9864           size += DWARF_OFFSET_SIZE;
9865           break;
9866         case dw_val_class_lbl_id:
9867           size += DWARF2_ADDR_SIZE;
9868           break;
9869         case dw_val_class_lineptr:
9870         case dw_val_class_macptr:
9871           size += DWARF_OFFSET_SIZE;
9872           break;
9873         case dw_val_class_str:
9874           if (AT_string_form (a) == DW_FORM_strp)
9875             size += DWARF_OFFSET_SIZE;
9876           else
9877             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
9878           break;
9879         case dw_val_class_file:
9880           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
9881           break;
9882         case dw_val_class_data8:
9883           size += 8;
9884           break;
9885         default:
9886           gcc_unreachable ();
9887         }
9888     }
9889
9890   return size;
9891 }
9892
9893 /* Size the debugging information associated with a given DIE.  Visits the
9894    DIE's children recursively.  Updates the global variable next_die_offset, on
9895    each time through.  Uses the current value of next_die_offset to update the
9896    die_offset field in each DIE.  */
9897
9898 static void
9899 calc_die_sizes (dw_die_ref die)
9900 {
9901   dw_die_ref c;
9902
9903   die->die_offset = next_die_offset;
9904   next_die_offset += size_of_die (die);
9905
9906   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
9907
9908   if (die->die_child != NULL)
9909     /* Count the null byte used to terminate sibling lists.  */
9910     next_die_offset += 1;
9911 }
9912
9913 /* Set the marks for a die and its children.  We do this so
9914    that we know whether or not a reference needs to use FORM_ref_addr; only
9915    DIEs in the same CU will be marked.  We used to clear out the offset
9916    and use that as the flag, but ran into ordering problems.  */
9917
9918 static void
9919 mark_dies (dw_die_ref die)
9920 {
9921   dw_die_ref c;
9922
9923   gcc_assert (!die->die_mark);
9924
9925   die->die_mark = 1;
9926   FOR_EACH_CHILD (die, c, mark_dies (c));
9927 }
9928
9929 /* Clear the marks for a die and its children.  */
9930
9931 static void
9932 unmark_dies (dw_die_ref die)
9933 {
9934   dw_die_ref c;
9935
9936   if (dwarf_version < 4)
9937     gcc_assert (die->die_mark);
9938
9939   die->die_mark = 0;
9940   FOR_EACH_CHILD (die, c, unmark_dies (c));
9941 }
9942
9943 /* Clear the marks for a die, its children and referred dies.  */
9944
9945 static void
9946 unmark_all_dies (dw_die_ref die)
9947 {
9948   dw_die_ref c;
9949   dw_attr_ref a;
9950   unsigned ix;
9951
9952   if (!die->die_mark)
9953     return;
9954   die->die_mark = 0;
9955
9956   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
9957
9958   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9959     if (AT_class (a) == dw_val_class_die_ref)
9960       unmark_all_dies (AT_ref (a));
9961 }
9962
9963 /* Return the size of the .debug_pubnames or .debug_pubtypes table
9964    generated for the compilation unit.  */
9965
9966 static unsigned long
9967 size_of_pubnames (VEC (pubname_entry, gc) * names)
9968 {
9969   unsigned long size;
9970   unsigned i;
9971   pubname_ref p;
9972
9973   size = DWARF_PUBNAMES_HEADER_SIZE;
9974   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
9975     if (names != pubtype_table
9976         || p->die->die_offset != 0
9977         || !flag_eliminate_unused_debug_types)
9978       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
9979
9980   size += DWARF_OFFSET_SIZE;
9981   return size;
9982 }
9983
9984 /* Return the size of the information in the .debug_aranges section.  */
9985
9986 static unsigned long
9987 size_of_aranges (void)
9988 {
9989   unsigned long size;
9990
9991   size = DWARF_ARANGES_HEADER_SIZE;
9992
9993   /* Count the address/length pair for this compilation unit.  */
9994   if (text_section_used)
9995     size += 2 * DWARF2_ADDR_SIZE;
9996   if (cold_text_section_used)
9997     size += 2 * DWARF2_ADDR_SIZE;
9998   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
9999
10000   /* Count the two zero words used to terminated the address range table.  */
10001   size += 2 * DWARF2_ADDR_SIZE;
10002   return size;
10003 }
10004 \f
10005 /* Select the encoding of an attribute value.  */
10006
10007 static enum dwarf_form
10008 value_format (dw_attr_ref a)
10009 {
10010   switch (a->dw_attr_val.val_class)
10011     {
10012     case dw_val_class_addr:
10013       /* Only very few attributes allow DW_FORM_addr.  */
10014       switch (a->dw_attr)
10015         {
10016         case DW_AT_low_pc:
10017         case DW_AT_high_pc:
10018         case DW_AT_entry_pc:
10019         case DW_AT_trampoline:
10020           return DW_FORM_addr;
10021         default:
10022           break;
10023         }
10024       switch (DWARF2_ADDR_SIZE)
10025         {
10026         case 1:
10027           return DW_FORM_data1;
10028         case 2:
10029           return DW_FORM_data2;
10030         case 4:
10031           return DW_FORM_data4;
10032         case 8:
10033           return DW_FORM_data8;
10034         default:
10035           gcc_unreachable ();
10036         }
10037     case dw_val_class_range_list:
10038     case dw_val_class_offset:
10039     case dw_val_class_loc_list:
10040       switch (DWARF_OFFSET_SIZE)
10041         {
10042         case 4:
10043           return DW_FORM_data4;
10044         case 8:
10045           return DW_FORM_data8;
10046         default:
10047           gcc_unreachable ();
10048         }
10049     case dw_val_class_loc:
10050       switch (constant_size (size_of_locs (AT_loc (a))))
10051         {
10052         case 1:
10053           return DW_FORM_block1;
10054         case 2:
10055           return DW_FORM_block2;
10056         default:
10057           gcc_unreachable ();
10058         }
10059     case dw_val_class_const:
10060       return DW_FORM_sdata;
10061     case dw_val_class_unsigned_const:
10062       switch (constant_size (AT_unsigned (a)))
10063         {
10064         case 1:
10065           return DW_FORM_data1;
10066         case 2:
10067           return DW_FORM_data2;
10068         case 4:
10069           return DW_FORM_data4;
10070         case 8:
10071           return DW_FORM_data8;
10072         default:
10073           gcc_unreachable ();
10074         }
10075     case dw_val_class_const_double:
10076       switch (HOST_BITS_PER_WIDE_INT)
10077         {
10078         case 8:
10079           return DW_FORM_data2;
10080         case 16:
10081           return DW_FORM_data4;
10082         case 32:
10083           return DW_FORM_data8;
10084         case 64:
10085         default:
10086           return DW_FORM_block1;
10087         }
10088     case dw_val_class_vec:
10089       switch (constant_size (a->dw_attr_val.v.val_vec.length
10090                              * a->dw_attr_val.v.val_vec.elt_size))
10091         {
10092         case 1:
10093           return DW_FORM_block1;
10094         case 2:
10095           return DW_FORM_block2;
10096         case 4:
10097           return DW_FORM_block4;
10098         default:
10099           gcc_unreachable ();
10100         }
10101     case dw_val_class_flag:
10102       return DW_FORM_flag;
10103     case dw_val_class_die_ref:
10104       if (AT_ref_external (a))
10105         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10106       else
10107         return DW_FORM_ref;
10108     case dw_val_class_fde_ref:
10109       return DW_FORM_data;
10110     case dw_val_class_lbl_id:
10111       return DW_FORM_addr;
10112     case dw_val_class_lineptr:
10113     case dw_val_class_macptr:
10114       return DW_FORM_data;
10115     case dw_val_class_str:
10116       return AT_string_form (a);
10117     case dw_val_class_file:
10118       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10119         {
10120         case 1:
10121           return DW_FORM_data1;
10122         case 2:
10123           return DW_FORM_data2;
10124         case 4:
10125           return DW_FORM_data4;
10126         default:
10127           gcc_unreachable ();
10128         }
10129
10130     case dw_val_class_data8:
10131       return DW_FORM_data8;
10132
10133     default:
10134       gcc_unreachable ();
10135     }
10136 }
10137
10138 /* Output the encoding of an attribute value.  */
10139
10140 static void
10141 output_value_format (dw_attr_ref a)
10142 {
10143   enum dwarf_form form = value_format (a);
10144
10145   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10146 }
10147
10148 /* Output the .debug_abbrev section which defines the DIE abbreviation
10149    table.  */
10150
10151 static void
10152 output_abbrev_section (void)
10153 {
10154   unsigned long abbrev_id;
10155
10156   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10157     {
10158       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10159       unsigned ix;
10160       dw_attr_ref a_attr;
10161
10162       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10163       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10164                                    dwarf_tag_name (abbrev->die_tag));
10165
10166       if (abbrev->die_child != NULL)
10167         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10168       else
10169         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10170
10171       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10172            ix++)
10173         {
10174           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10175                                        dwarf_attr_name (a_attr->dw_attr));
10176           output_value_format (a_attr);
10177         }
10178
10179       dw2_asm_output_data (1, 0, NULL);
10180       dw2_asm_output_data (1, 0, NULL);
10181     }
10182
10183   /* Terminate the table.  */
10184   dw2_asm_output_data (1, 0, NULL);
10185 }
10186
10187 /* Output a symbol we can use to refer to this DIE from another CU.  */
10188
10189 static inline void
10190 output_die_symbol (dw_die_ref die)
10191 {
10192   char *sym = die->die_id.die_symbol;
10193
10194   if (sym == 0)
10195     return;
10196
10197   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10198     /* We make these global, not weak; if the target doesn't support
10199        .linkonce, it doesn't support combining the sections, so debugging
10200        will break.  */
10201     targetm.asm_out.globalize_label (asm_out_file, sym);
10202
10203   ASM_OUTPUT_LABEL (asm_out_file, sym);
10204 }
10205
10206 /* Return a new location list, given the begin and end range, and the
10207    expression. gensym tells us whether to generate a new internal symbol for
10208    this location list node, which is done for the head of the list only.  */
10209
10210 static inline dw_loc_list_ref
10211 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10212               const char *section, unsigned int gensym)
10213 {
10214   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
10215
10216   retlist->begin = begin;
10217   retlist->end = end;
10218   retlist->expr = expr;
10219   retlist->section = section;
10220   if (gensym)
10221     retlist->ll_symbol = gen_internal_sym ("LLST");
10222
10223   return retlist;
10224 }
10225
10226 /* Add a location description expression to a location list.  */
10227
10228 static inline void
10229 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
10230                            const char *begin, const char *end,
10231                            const char *section)
10232 {
10233   dw_loc_list_ref *d;
10234
10235   /* Find the end of the chain.  */
10236   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
10237     ;
10238
10239   /* Add a new location list node to the list.  */
10240   *d = new_loc_list (descr, begin, end, section, 0);
10241 }
10242
10243 /* Output the location list given to us.  */
10244
10245 static void
10246 output_loc_list (dw_loc_list_ref list_head)
10247 {
10248   dw_loc_list_ref curr = list_head;
10249
10250   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10251
10252   /* Walk the location list, and output each range + expression.  */
10253   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10254     {
10255       unsigned long size;
10256       /* Don't output an entry that starts and ends at the same address.  */
10257       if (strcmp (curr->begin, curr->end) == 0)
10258         continue;
10259       if (!have_multiple_function_sections)
10260         {
10261           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10262                                 "Location list begin address (%s)",
10263                                 list_head->ll_symbol);
10264           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10265                                 "Location list end address (%s)",
10266                                 list_head->ll_symbol);
10267         }
10268       else
10269         {
10270           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10271                                "Location list begin address (%s)",
10272                                list_head->ll_symbol);
10273           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10274                                "Location list end address (%s)",
10275                                list_head->ll_symbol);
10276         }
10277       size = size_of_locs (curr->expr);
10278
10279       /* Output the block length for this list of location operations.  */
10280       gcc_assert (size <= 0xffff);
10281       dw2_asm_output_data (2, size, "%s", "Location expression size");
10282
10283       output_loc_sequence (curr->expr);
10284     }
10285
10286   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10287                        "Location list terminator begin (%s)",
10288                        list_head->ll_symbol);
10289   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10290                        "Location list terminator end (%s)",
10291                        list_head->ll_symbol);
10292 }
10293
10294 /* Output a type signature.  */
10295
10296 static inline void
10297 output_signature (const char *sig, const char *name)
10298 {
10299   int i;
10300
10301   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10302     dw2_asm_output_data (1, sig[i], "%s", name);
10303 }
10304
10305 /* Output the DIE and its attributes.  Called recursively to generate
10306    the definitions of each child DIE.  */
10307
10308 static void
10309 output_die (dw_die_ref die)
10310 {
10311   dw_attr_ref a;
10312   dw_die_ref c;
10313   unsigned long size;
10314   unsigned ix;
10315
10316   /* If someone in another CU might refer to us, set up a symbol for
10317      them to point to.  */
10318   if (dwarf_version < 4 && die->die_id.die_symbol)
10319     output_die_symbol (die);
10320
10321   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
10322                                (unsigned long)die->die_offset,
10323                                dwarf_tag_name (die->die_tag));
10324
10325   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10326     {
10327       const char *name = dwarf_attr_name (a->dw_attr);
10328
10329       switch (AT_class (a))
10330         {
10331         case dw_val_class_addr:
10332           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10333           break;
10334
10335         case dw_val_class_offset:
10336           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10337                                "%s", name);
10338           break;
10339
10340         case dw_val_class_range_list:
10341           {
10342             char *p = strchr (ranges_section_label, '\0');
10343
10344             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10345                      a->dw_attr_val.v.val_offset);
10346             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10347                                    debug_ranges_section, "%s", name);
10348             *p = '\0';
10349           }
10350           break;
10351
10352         case dw_val_class_loc:
10353           size = size_of_locs (AT_loc (a));
10354
10355           /* Output the block length for this list of location operations.  */
10356           dw2_asm_output_data (constant_size (size), size, "%s", name);
10357
10358           output_loc_sequence (AT_loc (a));
10359           break;
10360
10361         case dw_val_class_const:
10362           /* ??? It would be slightly more efficient to use a scheme like is
10363              used for unsigned constants below, but gdb 4.x does not sign
10364              extend.  Gdb 5.x does sign extend.  */
10365           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10366           break;
10367
10368         case dw_val_class_unsigned_const:
10369           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10370                                AT_unsigned (a), "%s", name);
10371           break;
10372
10373         case dw_val_class_const_double:
10374           {
10375             unsigned HOST_WIDE_INT first, second;
10376
10377             if (HOST_BITS_PER_WIDE_INT >= 64)
10378               dw2_asm_output_data (1,
10379                                    2 * HOST_BITS_PER_WIDE_INT
10380                                    / HOST_BITS_PER_CHAR,
10381                                    NULL);
10382
10383             if (WORDS_BIG_ENDIAN)
10384               {
10385                 first = a->dw_attr_val.v.val_double.high;
10386                 second = a->dw_attr_val.v.val_double.low;
10387               }
10388             else
10389               {
10390                 first = a->dw_attr_val.v.val_double.low;
10391                 second = a->dw_attr_val.v.val_double.high;
10392               }
10393
10394             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10395                                  first, name);
10396             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10397                                  second, NULL);
10398           }
10399           break;
10400
10401         case dw_val_class_vec:
10402           {
10403             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10404             unsigned int len = a->dw_attr_val.v.val_vec.length;
10405             unsigned int i;
10406             unsigned char *p;
10407
10408             dw2_asm_output_data (constant_size (len * elt_size),
10409                                  len * elt_size, "%s", name);
10410             if (elt_size > sizeof (HOST_WIDE_INT))
10411               {
10412                 elt_size /= 2;
10413                 len *= 2;
10414               }
10415             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10416                  i < len;
10417                  i++, p += elt_size)
10418               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10419                                    "fp or vector constant word %u", i);
10420             break;
10421           }
10422
10423         case dw_val_class_flag:
10424           dw2_asm_output_data (1, AT_flag (a), "%s", name);
10425           break;
10426
10427         case dw_val_class_loc_list:
10428           {
10429             char *sym = AT_loc_list (a)->ll_symbol;
10430
10431             gcc_assert (sym);
10432             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
10433                                    "%s", name);
10434           }
10435           break;
10436
10437         case dw_val_class_die_ref:
10438           if (AT_ref_external (a))
10439             {
10440               if (dwarf_version >= 4)
10441                 {
10442                   comdat_type_node_ref type_node =
10443                     AT_ref (a)->die_id.die_type_node;
10444
10445                   gcc_assert (type_node);
10446                   output_signature (type_node->signature, name);
10447                 }
10448               else
10449                 {
10450                   char *sym = AT_ref (a)->die_id.die_symbol;
10451                   int size;
10452
10453                   gcc_assert (sym);
10454                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
10455                      length, whereas in DWARF3 it's always sized as an
10456                      offset.  */
10457                   if (dwarf_version == 2)
10458                     size = DWARF2_ADDR_SIZE;
10459                   else
10460                     size = DWARF_OFFSET_SIZE;
10461                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
10462                                          name);
10463                 }
10464             }
10465           else
10466             {
10467               gcc_assert (AT_ref (a)->die_offset);
10468               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
10469                                    "%s", name);
10470             }
10471           break;
10472
10473         case dw_val_class_fde_ref:
10474           {
10475             char l1[20];
10476
10477             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
10478                                          a->dw_attr_val.v.val_fde_index * 2);
10479             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
10480                                    "%s", name);
10481           }
10482           break;
10483
10484         case dw_val_class_lbl_id:
10485           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
10486           break;
10487
10488         case dw_val_class_lineptr:
10489           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10490                                  debug_line_section, "%s", name);
10491           break;
10492
10493         case dw_val_class_macptr:
10494           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
10495                                  debug_macinfo_section, "%s", name);
10496           break;
10497
10498         case dw_val_class_str:
10499           if (AT_string_form (a) == DW_FORM_strp)
10500             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
10501                                    a->dw_attr_val.v.val_str->label,
10502                                    debug_str_section,
10503                                    "%s: \"%s\"", name, AT_string (a));
10504           else
10505             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
10506           break;
10507
10508         case dw_val_class_file:
10509           {
10510             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
10511
10512             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
10513                                  a->dw_attr_val.v.val_file->filename);
10514             break;
10515           }
10516
10517         case dw_val_class_data8:
10518           {
10519             int i;
10520
10521             for (i = 0; i < 8; i++)
10522               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
10523                                    "%s", name);
10524             break;
10525           }
10526
10527         default:
10528           gcc_unreachable ();
10529         }
10530     }
10531
10532   FOR_EACH_CHILD (die, c, output_die (c));
10533
10534   /* Add null byte to terminate sibling list.  */
10535   if (die->die_child != NULL)
10536     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
10537                          (unsigned long) die->die_offset);
10538 }
10539
10540 /* Output the compilation unit that appears at the beginning of the
10541    .debug_info section, and precedes the DIE descriptions.  */
10542
10543 static void
10544 output_compilation_unit_header (void)
10545 {
10546   int ver = dwarf_version;
10547
10548   /* Don't mark the output as DWARF-4 until we make full use of the
10549      version 4 extensions, and gdb supports them.  For now, -gdwarf-4
10550      selects only a few extensions from the DWARF-4 spec.  */
10551   if (ver > 3)
10552     ver = 3;
10553   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10554     dw2_asm_output_data (4, 0xffffffff,
10555       "Initial length escape value indicating 64-bit DWARF extension");
10556   dw2_asm_output_data (DWARF_OFFSET_SIZE,
10557                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
10558                        "Length of Compilation Unit Info");
10559   dw2_asm_output_data (2, ver, "DWARF version number");
10560   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
10561                          debug_abbrev_section,
10562                          "Offset Into Abbrev. Section");
10563   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
10564 }
10565
10566 /* Output the compilation unit DIE and its children.  */
10567
10568 static void
10569 output_comp_unit (dw_die_ref die, int output_if_empty)
10570 {
10571   const char *secname;
10572   char *oldsym, *tmp;
10573
10574   /* Unless we are outputting main CU, we may throw away empty ones.  */
10575   if (!output_if_empty && die->die_child == NULL)
10576     return;
10577
10578   /* Even if there are no children of this DIE, we must output the information
10579      about the compilation unit.  Otherwise, on an empty translation unit, we
10580      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
10581      will then complain when examining the file.  First mark all the DIEs in
10582      this CU so we know which get local refs.  */
10583   mark_dies (die);
10584
10585   build_abbrev_table (die);
10586
10587   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
10588   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
10589   calc_die_sizes (die);
10590
10591   oldsym = die->die_id.die_symbol;
10592   if (oldsym)
10593     {
10594       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
10595
10596       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
10597       secname = tmp;
10598       die->die_id.die_symbol = NULL;
10599       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10600     }
10601   else
10602     switch_to_section (debug_info_section);
10603
10604   /* Output debugging information.  */
10605   output_compilation_unit_header ();
10606   output_die (die);
10607
10608   /* Leave the marks on the main CU, so we can check them in
10609      output_pubnames.  */
10610   if (oldsym)
10611     {
10612       unmark_dies (die);
10613       die->die_id.die_symbol = oldsym;
10614     }
10615 }
10616
10617 /* Output a comdat type unit DIE and its children.  */
10618
10619 static void
10620 output_comdat_type_unit (comdat_type_node *node)
10621 {
10622   const char *secname;
10623   char *tmp;
10624   int i;
10625 #if defined (OBJECT_FORMAT_ELF)
10626   tree comdat_key;
10627 #endif
10628
10629   /* First mark all the DIEs in this CU so we know which get local refs.  */
10630   mark_dies (node->root_die);
10631
10632   build_abbrev_table (node->root_die);
10633
10634   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
10635   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
10636   calc_die_sizes (node->root_die);
10637
10638 #if defined (OBJECT_FORMAT_ELF)
10639   secname = ".debug_types";
10640   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10641   sprintf (tmp, "wt.");
10642   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10643     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
10644   comdat_key = get_identifier (tmp);
10645   targetm.asm_out.named_section (secname,
10646                                  SECTION_DEBUG | SECTION_LINKONCE,
10647                                  comdat_key);
10648 #else
10649   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
10650   sprintf (tmp, ".gnu.linkonce.wt.");
10651   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10652     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
10653   secname = tmp;
10654   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
10655 #endif
10656
10657   /* Output debugging information.  */
10658   output_compilation_unit_header ();
10659   output_signature (node->signature, "Type Signature");
10660   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
10661                        "Offset to Type DIE");
10662   output_die (node->root_die);
10663
10664   unmark_dies (node->root_die);
10665 }
10666
10667 /* Return the DWARF2/3 pubname associated with a decl.  */
10668
10669 static const char *
10670 dwarf2_name (tree decl, int scope)
10671 {
10672   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
10673 }
10674
10675 /* Add a new entry to .debug_pubnames if appropriate.  */
10676
10677 static void
10678 add_pubname_string (const char *str, dw_die_ref die)
10679 {
10680   pubname_entry e;
10681
10682   e.die = die;
10683   e.name = xstrdup (str);
10684   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
10685 }
10686
10687 static void
10688 add_pubname (tree decl, dw_die_ref die)
10689 {
10690   if (TREE_PUBLIC (decl))
10691     add_pubname_string (dwarf2_name (decl, 1), die);
10692 }
10693
10694 /* Add a new entry to .debug_pubtypes if appropriate.  */
10695
10696 static void
10697 add_pubtype (tree decl, dw_die_ref die)
10698 {
10699   pubname_entry e;
10700
10701   e.name = NULL;
10702   if ((TREE_PUBLIC (decl)
10703        || die->die_parent == comp_unit_die)
10704       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
10705     {
10706       e.die = die;
10707       if (TYPE_P (decl))
10708         {
10709           if (TYPE_NAME (decl))
10710             {
10711               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
10712                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
10713               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
10714                        && DECL_NAME (TYPE_NAME (decl)))
10715                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
10716               else
10717                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
10718             }
10719         }
10720       else
10721         e.name = xstrdup (dwarf2_name (decl, 1));
10722
10723       /* If we don't have a name for the type, there's no point in adding
10724          it to the table.  */
10725       if (e.name && e.name[0] != '\0')
10726         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
10727     }
10728 }
10729
10730 /* Output the public names table used to speed up access to externally
10731    visible names; or the public types table used to find type definitions.  */
10732
10733 static void
10734 output_pubnames (VEC (pubname_entry, gc) * names)
10735 {
10736   unsigned i;
10737   unsigned long pubnames_length = size_of_pubnames (names);
10738   pubname_ref pub;
10739
10740   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10741     dw2_asm_output_data (4, 0xffffffff,
10742       "Initial length escape value indicating 64-bit DWARF extension");
10743   if (names == pubname_table)
10744     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10745                          "Length of Public Names Info");
10746   else
10747     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
10748                          "Length of Public Type Names Info");
10749   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
10750   dw2_asm_output_data (2, 2, "DWARF Version");
10751   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10752                          debug_info_section,
10753                          "Offset of Compilation Unit Info");
10754   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
10755                        "Compilation Unit Length");
10756
10757   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
10758     {
10759       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
10760       if (names == pubname_table)
10761         gcc_assert (pub->die->die_mark);
10762
10763       if (names != pubtype_table
10764           || pub->die->die_offset != 0
10765           || !flag_eliminate_unused_debug_types)
10766         {
10767           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
10768                                "DIE offset");
10769
10770           dw2_asm_output_nstring (pub->name, -1, "external name");
10771         }
10772     }
10773
10774   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
10775 }
10776
10777 /* Add a new entry to .debug_aranges if appropriate.  */
10778
10779 static void
10780 add_arange (tree decl, dw_die_ref die)
10781 {
10782   if (! DECL_SECTION_NAME (decl))
10783     return;
10784
10785   if (arange_table_in_use == arange_table_allocated)
10786     {
10787       arange_table_allocated += ARANGE_TABLE_INCREMENT;
10788       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
10789                                     arange_table_allocated);
10790       memset (arange_table + arange_table_in_use, 0,
10791               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
10792     }
10793
10794   arange_table[arange_table_in_use++] = die;
10795 }
10796
10797 /* Output the information that goes into the .debug_aranges table.
10798    Namely, define the beginning and ending address range of the
10799    text section generated for this compilation unit.  */
10800
10801 static void
10802 output_aranges (void)
10803 {
10804   unsigned i;
10805   unsigned long aranges_length = size_of_aranges ();
10806
10807   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
10808     dw2_asm_output_data (4, 0xffffffff,
10809       "Initial length escape value indicating 64-bit DWARF extension");
10810   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
10811                        "Length of Address Ranges Info");
10812   /* Version number for aranges is still 2, even in DWARF3.  */
10813   dw2_asm_output_data (2, 2, "DWARF Version");
10814   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
10815                          debug_info_section,
10816                          "Offset of Compilation Unit Info");
10817   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
10818   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
10819
10820   /* We need to align to twice the pointer size here.  */
10821   if (DWARF_ARANGES_PAD_SIZE)
10822     {
10823       /* Pad using a 2 byte words so that padding is correct for any
10824          pointer size.  */
10825       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
10826                            2 * DWARF2_ADDR_SIZE);
10827       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
10828         dw2_asm_output_data (2, 0, NULL);
10829     }
10830
10831   /* It is necessary not to output these entries if the sections were
10832      not used; if the sections were not used, the length will be 0 and
10833      the address may end up as 0 if the section is discarded by ld
10834      --gc-sections, leaving an invalid (0, 0) entry that can be
10835      confused with the terminator.  */
10836   if (text_section_used)
10837     {
10838       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
10839       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
10840                             text_section_label, "Length");
10841     }
10842   if (cold_text_section_used)
10843     {
10844       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
10845                            "Address");
10846       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
10847                             cold_text_section_label, "Length");
10848     }
10849
10850   for (i = 0; i < arange_table_in_use; i++)
10851     {
10852       dw_die_ref die = arange_table[i];
10853
10854       /* We shouldn't see aranges for DIEs outside of the main CU.  */
10855       gcc_assert (die->die_mark);
10856
10857       if (die->die_tag == DW_TAG_subprogram)
10858         {
10859           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
10860                                "Address");
10861           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
10862                                 get_AT_low_pc (die), "Length");
10863         }
10864       else
10865         {
10866           /* A static variable; extract the symbol from DW_AT_location.
10867              Note that this code isn't currently hit, as we only emit
10868              aranges for functions (jason 9/23/99).  */
10869           dw_attr_ref a = get_AT (die, DW_AT_location);
10870           dw_loc_descr_ref loc;
10871
10872           gcc_assert (a && AT_class (a) == dw_val_class_loc);
10873
10874           loc = AT_loc (a);
10875           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
10876
10877           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
10878                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
10879           dw2_asm_output_data (DWARF2_ADDR_SIZE,
10880                                get_AT_unsigned (die, DW_AT_byte_size),
10881                                "Length");
10882         }
10883     }
10884
10885   /* Output the terminator words.  */
10886   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10887   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
10888 }
10889
10890 /* Add a new entry to .debug_ranges.  Return the offset at which it
10891    was placed.  */
10892
10893 static unsigned int
10894 add_ranges_num (int num)
10895 {
10896   unsigned int in_use = ranges_table_in_use;
10897
10898   if (in_use == ranges_table_allocated)
10899     {
10900       ranges_table_allocated += RANGES_TABLE_INCREMENT;
10901       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
10902                                     ranges_table_allocated);
10903       memset (ranges_table + ranges_table_in_use, 0,
10904               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
10905     }
10906
10907   ranges_table[in_use].num = num;
10908   ranges_table_in_use = in_use + 1;
10909
10910   return in_use * 2 * DWARF2_ADDR_SIZE;
10911 }
10912
10913 /* Add a new entry to .debug_ranges corresponding to a block, or a
10914    range terminator if BLOCK is NULL.  */
10915
10916 static unsigned int
10917 add_ranges (const_tree block)
10918 {
10919   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
10920 }
10921
10922 /* Add a new entry to .debug_ranges corresponding to a pair of
10923    labels.  */
10924
10925 static unsigned int
10926 add_ranges_by_labels (const char *begin, const char *end)
10927 {
10928   unsigned int in_use = ranges_by_label_in_use;
10929
10930   if (in_use == ranges_by_label_allocated)
10931     {
10932       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
10933       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
10934                                        ranges_by_label,
10935                                        ranges_by_label_allocated);
10936       memset (ranges_by_label + ranges_by_label_in_use, 0,
10937               RANGES_TABLE_INCREMENT
10938               * sizeof (struct dw_ranges_by_label_struct));
10939     }
10940
10941   ranges_by_label[in_use].begin = begin;
10942   ranges_by_label[in_use].end = end;
10943   ranges_by_label_in_use = in_use + 1;
10944
10945   return add_ranges_num (-(int)in_use - 1);
10946 }
10947
10948 static void
10949 output_ranges (void)
10950 {
10951   unsigned i;
10952   static const char *const start_fmt = "Offset 0x%x";
10953   const char *fmt = start_fmt;
10954
10955   for (i = 0; i < ranges_table_in_use; i++)
10956     {
10957       int block_num = ranges_table[i].num;
10958
10959       if (block_num > 0)
10960         {
10961           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
10962           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
10963
10964           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
10965           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
10966
10967           /* If all code is in the text section, then the compilation
10968              unit base address defaults to DW_AT_low_pc, which is the
10969              base of the text section.  */
10970           if (!have_multiple_function_sections)
10971             {
10972               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
10973                                     text_section_label,
10974                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
10975               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
10976                                     text_section_label, NULL);
10977             }
10978
10979           /* Otherwise, the compilation unit base address is zero,
10980              which allows us to use absolute addresses, and not worry
10981              about whether the target supports cross-section
10982              arithmetic.  */
10983           else
10984             {
10985               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
10986                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
10987               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
10988             }
10989
10990           fmt = NULL;
10991         }
10992
10993       /* Negative block_num stands for an index into ranges_by_label.  */
10994       else if (block_num < 0)
10995         {
10996           int lab_idx = - block_num - 1;
10997
10998           if (!have_multiple_function_sections)
10999             {
11000               gcc_unreachable ();
11001 #if 0
11002               /* If we ever use add_ranges_by_labels () for a single
11003                  function section, all we have to do is to take out
11004                  the #if 0 above.  */
11005               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11006                                     ranges_by_label[lab_idx].begin,
11007                                     text_section_label,
11008                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11009               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11010                                     ranges_by_label[lab_idx].end,
11011                                     text_section_label, NULL);
11012 #endif
11013             }
11014           else
11015             {
11016               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11017                                    ranges_by_label[lab_idx].begin,
11018                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11019               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11020                                    ranges_by_label[lab_idx].end,
11021                                    NULL);
11022             }
11023         }
11024       else
11025         {
11026           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11027           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11028           fmt = start_fmt;
11029         }
11030     }
11031 }
11032
11033 /* Data structure containing information about input files.  */
11034 struct file_info
11035 {
11036   const char *path;     /* Complete file name.  */
11037   const char *fname;    /* File name part.  */
11038   int length;           /* Length of entire string.  */
11039   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11040   int dir_idx;          /* Index in directory table.  */
11041 };
11042
11043 /* Data structure containing information about directories with source
11044    files.  */
11045 struct dir_info
11046 {
11047   const char *path;     /* Path including directory name.  */
11048   int length;           /* Path length.  */
11049   int prefix;           /* Index of directory entry which is a prefix.  */
11050   int count;            /* Number of files in this directory.  */
11051   int dir_idx;          /* Index of directory used as base.  */
11052 };
11053
11054 /* Callback function for file_info comparison.  We sort by looking at
11055    the directories in the path.  */
11056
11057 static int
11058 file_info_cmp (const void *p1, const void *p2)
11059 {
11060   const struct file_info *const s1 = (const struct file_info *) p1;
11061   const struct file_info *const s2 = (const struct file_info *) p2;
11062   const unsigned char *cp1;
11063   const unsigned char *cp2;
11064
11065   /* Take care of file names without directories.  We need to make sure that
11066      we return consistent values to qsort since some will get confused if
11067      we return the same value when identical operands are passed in opposite
11068      orders.  So if neither has a directory, return 0 and otherwise return
11069      1 or -1 depending on which one has the directory.  */
11070   if ((s1->path == s1->fname || s2->path == s2->fname))
11071     return (s2->path == s2->fname) - (s1->path == s1->fname);
11072
11073   cp1 = (const unsigned char *) s1->path;
11074   cp2 = (const unsigned char *) s2->path;
11075
11076   while (1)
11077     {
11078       ++cp1;
11079       ++cp2;
11080       /* Reached the end of the first path?  If so, handle like above.  */
11081       if ((cp1 == (const unsigned char *) s1->fname)
11082           || (cp2 == (const unsigned char *) s2->fname))
11083         return ((cp2 == (const unsigned char *) s2->fname)
11084                 - (cp1 == (const unsigned char *) s1->fname));
11085
11086       /* Character of current path component the same?  */
11087       else if (*cp1 != *cp2)
11088         return *cp1 - *cp2;
11089     }
11090 }
11091
11092 struct file_name_acquire_data
11093 {
11094   struct file_info *files;
11095   int used_files;
11096   int max_files;
11097 };
11098
11099 /* Traversal function for the hash table.  */
11100
11101 static int
11102 file_name_acquire (void ** slot, void *data)
11103 {
11104   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11105   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11106   struct file_info *fi;
11107   const char *f;
11108
11109   gcc_assert (fnad->max_files >= d->emitted_number);
11110
11111   if (! d->emitted_number)
11112     return 1;
11113
11114   gcc_assert (fnad->max_files != fnad->used_files);
11115
11116   fi = fnad->files + fnad->used_files++;
11117
11118   /* Skip all leading "./".  */
11119   f = d->filename;
11120   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11121     f += 2;
11122
11123   /* Create a new array entry.  */
11124   fi->path = f;
11125   fi->length = strlen (f);
11126   fi->file_idx = d;
11127
11128   /* Search for the file name part.  */
11129   f = strrchr (f, DIR_SEPARATOR);
11130 #if defined (DIR_SEPARATOR_2)
11131   {
11132     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11133
11134     if (g != NULL)
11135       {
11136         if (f == NULL || f < g)
11137           f = g;
11138       }
11139   }
11140 #endif
11141
11142   fi->fname = f == NULL ? fi->path : f + 1;
11143   return 1;
11144 }
11145
11146 /* Output the directory table and the file name table.  We try to minimize
11147    the total amount of memory needed.  A heuristic is used to avoid large
11148    slowdowns with many input files.  */
11149
11150 static void
11151 output_file_names (void)
11152 {
11153   struct file_name_acquire_data fnad;
11154   int numfiles;
11155   struct file_info *files;
11156   struct dir_info *dirs;
11157   int *saved;
11158   int *savehere;
11159   int *backmap;
11160   int ndirs;
11161   int idx_offset;
11162   int i;
11163   int idx;
11164
11165   if (!last_emitted_file)
11166     {
11167       dw2_asm_output_data (1, 0, "End directory table");
11168       dw2_asm_output_data (1, 0, "End file name table");
11169       return;
11170     }
11171
11172   numfiles = last_emitted_file->emitted_number;
11173
11174   /* Allocate the various arrays we need.  */
11175   files = XALLOCAVEC (struct file_info, numfiles);
11176   dirs = XALLOCAVEC (struct dir_info, numfiles);
11177
11178   fnad.files = files;
11179   fnad.used_files = 0;
11180   fnad.max_files = numfiles;
11181   htab_traverse (file_table, file_name_acquire, &fnad);
11182   gcc_assert (fnad.used_files == fnad.max_files);
11183
11184   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11185
11186   /* Find all the different directories used.  */
11187   dirs[0].path = files[0].path;
11188   dirs[0].length = files[0].fname - files[0].path;
11189   dirs[0].prefix = -1;
11190   dirs[0].count = 1;
11191   dirs[0].dir_idx = 0;
11192   files[0].dir_idx = 0;
11193   ndirs = 1;
11194
11195   for (i = 1; i < numfiles; i++)
11196     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11197         && memcmp (dirs[ndirs - 1].path, files[i].path,
11198                    dirs[ndirs - 1].length) == 0)
11199       {
11200         /* Same directory as last entry.  */
11201         files[i].dir_idx = ndirs - 1;
11202         ++dirs[ndirs - 1].count;
11203       }
11204     else
11205       {
11206         int j;
11207
11208         /* This is a new directory.  */
11209         dirs[ndirs].path = files[i].path;
11210         dirs[ndirs].length = files[i].fname - files[i].path;
11211         dirs[ndirs].count = 1;
11212         dirs[ndirs].dir_idx = ndirs;
11213         files[i].dir_idx = ndirs;
11214
11215         /* Search for a prefix.  */
11216         dirs[ndirs].prefix = -1;
11217         for (j = 0; j < ndirs; j++)
11218           if (dirs[j].length < dirs[ndirs].length
11219               && dirs[j].length > 1
11220               && (dirs[ndirs].prefix == -1
11221                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11222               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11223             dirs[ndirs].prefix = j;
11224
11225         ++ndirs;
11226       }
11227
11228   /* Now to the actual work.  We have to find a subset of the directories which
11229      allow expressing the file name using references to the directory table
11230      with the least amount of characters.  We do not do an exhaustive search
11231      where we would have to check out every combination of every single
11232      possible prefix.  Instead we use a heuristic which provides nearly optimal
11233      results in most cases and never is much off.  */
11234   saved = XALLOCAVEC (int, ndirs);
11235   savehere = XALLOCAVEC (int, ndirs);
11236
11237   memset (saved, '\0', ndirs * sizeof (saved[0]));
11238   for (i = 0; i < ndirs; i++)
11239     {
11240       int j;
11241       int total;
11242
11243       /* We can always save some space for the current directory.  But this
11244          does not mean it will be enough to justify adding the directory.  */
11245       savehere[i] = dirs[i].length;
11246       total = (savehere[i] - saved[i]) * dirs[i].count;
11247
11248       for (j = i + 1; j < ndirs; j++)
11249         {
11250           savehere[j] = 0;
11251           if (saved[j] < dirs[i].length)
11252             {
11253               /* Determine whether the dirs[i] path is a prefix of the
11254                  dirs[j] path.  */
11255               int k;
11256
11257               k = dirs[j].prefix;
11258               while (k != -1 && k != (int) i)
11259                 k = dirs[k].prefix;
11260
11261               if (k == (int) i)
11262                 {
11263                   /* Yes it is.  We can possibly save some memory by
11264                      writing the filenames in dirs[j] relative to
11265                      dirs[i].  */
11266                   savehere[j] = dirs[i].length;
11267                   total += (savehere[j] - saved[j]) * dirs[j].count;
11268                 }
11269             }
11270         }
11271
11272       /* Check whether we can save enough to justify adding the dirs[i]
11273          directory.  */
11274       if (total > dirs[i].length + 1)
11275         {
11276           /* It's worthwhile adding.  */
11277           for (j = i; j < ndirs; j++)
11278             if (savehere[j] > 0)
11279               {
11280                 /* Remember how much we saved for this directory so far.  */
11281                 saved[j] = savehere[j];
11282
11283                 /* Remember the prefix directory.  */
11284                 dirs[j].dir_idx = i;
11285               }
11286         }
11287     }
11288
11289   /* Emit the directory name table.  */
11290   idx = 1;
11291   idx_offset = dirs[0].length > 0 ? 1 : 0;
11292   for (i = 1 - idx_offset; i < ndirs; i++)
11293     dw2_asm_output_nstring (dirs[i].path,
11294                             dirs[i].length
11295                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11296                             "Directory Entry: 0x%x", i + idx_offset);
11297
11298   dw2_asm_output_data (1, 0, "End directory table");
11299
11300   /* We have to emit them in the order of emitted_number since that's
11301      used in the debug info generation.  To do this efficiently we
11302      generate a back-mapping of the indices first.  */
11303   backmap = XALLOCAVEC (int, numfiles);
11304   for (i = 0; i < numfiles; i++)
11305     backmap[files[i].file_idx->emitted_number - 1] = i;
11306
11307   /* Now write all the file names.  */
11308   for (i = 0; i < numfiles; i++)
11309     {
11310       int file_idx = backmap[i];
11311       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11312
11313 #ifdef VMS_DEBUGGING_INFO
11314 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11315
11316       /* Setting these fields can lead to debugger miscomparisons,
11317          but VMS Debug requires them to be set correctly.  */
11318
11319       int ver;
11320       long long cdt;
11321       long siz;
11322       int maxfilelen = strlen (files[file_idx].path)
11323                                + dirs[dir_idx].length
11324                                + MAX_VMS_VERSION_LEN + 1;
11325       char *filebuf = XALLOCAVEC (char, maxfilelen);
11326
11327       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11328       snprintf (filebuf, maxfilelen, "%s;%d",
11329                 files[file_idx].path + dirs[dir_idx].length, ver);
11330
11331       dw2_asm_output_nstring
11332         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
11333
11334       /* Include directory index.  */
11335       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11336
11337       /* Modification time.  */
11338       dw2_asm_output_data_uleb128
11339         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11340           ? cdt : 0,
11341          NULL);
11342
11343       /* File length in bytes.  */
11344       dw2_asm_output_data_uleb128
11345         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11346           ? siz : 0,
11347          NULL);
11348 #else
11349       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11350                               "File Entry: 0x%x", (unsigned) i + 1);
11351
11352       /* Include directory index.  */
11353       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11354
11355       /* Modification time.  */
11356       dw2_asm_output_data_uleb128 (0, NULL);
11357
11358       /* File length in bytes.  */
11359       dw2_asm_output_data_uleb128 (0, NULL);
11360 #endif
11361     }
11362
11363   dw2_asm_output_data (1, 0, "End file name table");
11364 }
11365
11366
11367 /* Output the source line number correspondence information.  This
11368    information goes into the .debug_line section.  */
11369
11370 static void
11371 output_line_info (void)
11372 {
11373   char l1[20], l2[20], p1[20], p2[20];
11374   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11375   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11376   unsigned opc;
11377   unsigned n_op_args;
11378   unsigned long lt_index;
11379   unsigned long current_line;
11380   long line_offset;
11381   long line_delta;
11382   unsigned long current_file;
11383   unsigned long function;
11384   int ver = dwarf_version;
11385
11386   /* Don't mark the output as DWARF-4 until we make full use of the
11387      version 4 extensions, and gdb supports them.  For now, -gdwarf-4
11388      selects only a few extensions from the DWARF-4 spec.  */
11389   if (ver > 3)
11390     ver = 3;
11391
11392   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
11393   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
11394   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
11395   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
11396
11397   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11398     dw2_asm_output_data (4, 0xffffffff,
11399       "Initial length escape value indicating 64-bit DWARF extension");
11400   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11401                         "Length of Source Line Info");
11402   ASM_OUTPUT_LABEL (asm_out_file, l1);
11403
11404   dw2_asm_output_data (2, ver, "DWARF Version");
11405   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
11406   ASM_OUTPUT_LABEL (asm_out_file, p1);
11407
11408   /* Define the architecture-dependent minimum instruction length (in
11409    bytes).  In this implementation of DWARF, this field is used for
11410    information purposes only.  Since GCC generates assembly language,
11411    we have no a priori knowledge of how many instruction bytes are
11412    generated for each source line, and therefore can use only the
11413    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
11414    commands.  Accordingly, we fix this as `1', which is "correct
11415    enough" for all architectures, and don't let the target override.  */
11416   dw2_asm_output_data (1, 1,
11417                        "Minimum Instruction Length");
11418
11419   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
11420                        "Default is_stmt_start flag");
11421   dw2_asm_output_data (1, DWARF_LINE_BASE,
11422                        "Line Base Value (Special Opcodes)");
11423   dw2_asm_output_data (1, DWARF_LINE_RANGE,
11424                        "Line Range Value (Special Opcodes)");
11425   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
11426                        "Special Opcode Base");
11427
11428   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
11429     {
11430       switch (opc)
11431         {
11432         case DW_LNS_advance_pc:
11433         case DW_LNS_advance_line:
11434         case DW_LNS_set_file:
11435         case DW_LNS_set_column:
11436         case DW_LNS_fixed_advance_pc:
11437           n_op_args = 1;
11438           break;
11439         default:
11440           n_op_args = 0;
11441           break;
11442         }
11443
11444       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
11445                            opc, n_op_args);
11446     }
11447
11448   /* Write out the information about the files we use.  */
11449   output_file_names ();
11450   ASM_OUTPUT_LABEL (asm_out_file, p2);
11451
11452   /* We used to set the address register to the first location in the text
11453      section here, but that didn't accomplish anything since we already
11454      have a line note for the opening brace of the first function.  */
11455
11456   /* Generate the line number to PC correspondence table, encoded as
11457      a series of state machine operations.  */
11458   current_file = 1;
11459   current_line = 1;
11460
11461   if (cfun && in_cold_section_p)
11462     strcpy (prev_line_label, crtl->subsections.cold_section_label);
11463   else
11464     strcpy (prev_line_label, text_section_label);
11465   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
11466     {
11467       dw_line_info_ref line_info = &line_info_table[lt_index];
11468
11469 #if 0
11470       /* Disable this optimization for now; GDB wants to see two line notes
11471          at the beginning of a function so it can find the end of the
11472          prologue.  */
11473
11474       /* Don't emit anything for redundant notes.  Just updating the
11475          address doesn't accomplish anything, because we already assume
11476          that anything after the last address is this line.  */
11477       if (line_info->dw_line_num == current_line
11478           && line_info->dw_file_num == current_file)
11479         continue;
11480 #endif
11481
11482       /* Emit debug info for the address of the current line.
11483
11484          Unfortunately, we have little choice here currently, and must always
11485          use the most general form.  GCC does not know the address delta
11486          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
11487          attributes which will give an upper bound on the address range.  We
11488          could perhaps use length attributes to determine when it is safe to
11489          use DW_LNS_fixed_advance_pc.  */
11490
11491       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
11492       if (0)
11493         {
11494           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
11495           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11496                                "DW_LNS_fixed_advance_pc");
11497           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11498         }
11499       else
11500         {
11501           /* This can handle any delta.  This takes
11502              4+DWARF2_ADDR_SIZE bytes.  */
11503           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11504           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11505           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11506           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11507         }
11508
11509       strcpy (prev_line_label, line_label);
11510
11511       /* Emit debug info for the source file of the current line, if
11512          different from the previous line.  */
11513       if (line_info->dw_file_num != current_file)
11514         {
11515           current_file = line_info->dw_file_num;
11516           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
11517           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
11518         }
11519
11520       /* Emit debug info for the current line number, choosing the encoding
11521          that uses the least amount of space.  */
11522       if (line_info->dw_line_num != current_line)
11523         {
11524           line_offset = line_info->dw_line_num - current_line;
11525           line_delta = line_offset - DWARF_LINE_BASE;
11526           current_line = line_info->dw_line_num;
11527           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11528             /* This can handle deltas from -10 to 234, using the current
11529                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
11530                takes 1 byte.  */
11531             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11532                                  "line %lu", current_line);
11533           else
11534             {
11535               /* This can handle any delta.  This takes at least 4 bytes,
11536                  depending on the value being encoded.  */
11537               dw2_asm_output_data (1, DW_LNS_advance_line,
11538                                    "advance to line %lu", current_line);
11539               dw2_asm_output_data_sleb128 (line_offset, NULL);
11540               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11541             }
11542         }
11543       else
11544         /* We still need to start a new row, so output a copy insn.  */
11545         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11546     }
11547
11548   /* Emit debug info for the address of the end of the function.  */
11549   if (0)
11550     {
11551       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11552                            "DW_LNS_fixed_advance_pc");
11553       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
11554     }
11555   else
11556     {
11557       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11558       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11559       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11560       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
11561     }
11562
11563   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
11564   dw2_asm_output_data_uleb128 (1, NULL);
11565   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11566
11567   function = 0;
11568   current_file = 1;
11569   current_line = 1;
11570   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
11571     {
11572       dw_separate_line_info_ref line_info
11573         = &separate_line_info_table[lt_index];
11574
11575 #if 0
11576       /* Don't emit anything for redundant notes.  */
11577       if (line_info->dw_line_num == current_line
11578           && line_info->dw_file_num == current_file
11579           && line_info->function == function)
11580         goto cont;
11581 #endif
11582
11583       /* Emit debug info for the address of the current line.  If this is
11584          a new function, or the first line of a function, then we need
11585          to handle it differently.  */
11586       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
11587                                    lt_index);
11588       if (function != line_info->function)
11589         {
11590           function = line_info->function;
11591
11592           /* Set the address register to the first line in the function.  */
11593           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11594           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11595           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11596           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11597         }
11598       else
11599         {
11600           /* ??? See the DW_LNS_advance_pc comment above.  */
11601           if (0)
11602             {
11603               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11604                                    "DW_LNS_fixed_advance_pc");
11605               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11606             }
11607           else
11608             {
11609               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11610               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11611               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11612               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11613             }
11614         }
11615
11616       strcpy (prev_line_label, line_label);
11617
11618       /* Emit debug info for the source file of the current line, if
11619          different from the previous line.  */
11620       if (line_info->dw_file_num != current_file)
11621         {
11622           current_file = line_info->dw_file_num;
11623           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
11624           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
11625         }
11626
11627       /* Emit debug info for the current line number, choosing the encoding
11628          that uses the least amount of space.  */
11629       if (line_info->dw_line_num != current_line)
11630         {
11631           line_offset = line_info->dw_line_num - current_line;
11632           line_delta = line_offset - DWARF_LINE_BASE;
11633           current_line = line_info->dw_line_num;
11634           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
11635             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
11636                                  "line %lu", current_line);
11637           else
11638             {
11639               dw2_asm_output_data (1, DW_LNS_advance_line,
11640                                    "advance to line %lu", current_line);
11641               dw2_asm_output_data_sleb128 (line_offset, NULL);
11642               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11643             }
11644         }
11645       else
11646         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
11647
11648 #if 0
11649     cont:
11650 #endif
11651
11652       lt_index++;
11653
11654       /* If we're done with a function, end its sequence.  */
11655       if (lt_index == separate_line_info_table_in_use
11656           || separate_line_info_table[lt_index].function != function)
11657         {
11658           current_file = 1;
11659           current_line = 1;
11660
11661           /* Emit debug info for the address of the end of the function.  */
11662           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
11663           if (0)
11664             {
11665               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
11666                                    "DW_LNS_fixed_advance_pc");
11667               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
11668             }
11669           else
11670             {
11671               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
11672               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
11673               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
11674               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
11675             }
11676
11677           /* Output the marker for the end of this sequence.  */
11678           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
11679           dw2_asm_output_data_uleb128 (1, NULL);
11680           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
11681         }
11682     }
11683
11684   /* Output the marker for the end of the line number info.  */
11685   ASM_OUTPUT_LABEL (asm_out_file, l2);
11686 }
11687 \f
11688 /* Given a pointer to a tree node for some base type, return a pointer to
11689    a DIE that describes the given type.
11690
11691    This routine must only be called for GCC type nodes that correspond to
11692    Dwarf base (fundamental) types.  */
11693
11694 static dw_die_ref
11695 base_type_die (tree type)
11696 {
11697   dw_die_ref base_type_result;
11698   enum dwarf_type encoding;
11699
11700   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
11701     return 0;
11702
11703   /* If this is a subtype that should not be emitted as a subrange type,
11704      use the base type.  See subrange_type_for_debug_p.  */
11705   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
11706     type = TREE_TYPE (type);
11707
11708   switch (TREE_CODE (type))
11709     {
11710     case INTEGER_TYPE:
11711       if (TYPE_STRING_FLAG (type))
11712         {
11713           if (TYPE_UNSIGNED (type))
11714             encoding = DW_ATE_unsigned_char;
11715           else
11716             encoding = DW_ATE_signed_char;
11717         }
11718       else if (TYPE_UNSIGNED (type))
11719         encoding = DW_ATE_unsigned;
11720       else
11721         encoding = DW_ATE_signed;
11722       break;
11723
11724     case REAL_TYPE:
11725       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
11726         {
11727           if (dwarf_version >= 3 || !dwarf_strict)
11728             encoding = DW_ATE_decimal_float;
11729           else
11730             encoding = DW_ATE_lo_user;
11731         }
11732       else
11733         encoding = DW_ATE_float;
11734       break;
11735
11736     case FIXED_POINT_TYPE:
11737       if (!(dwarf_version >= 3 || !dwarf_strict))
11738         encoding = DW_ATE_lo_user;
11739       else if (TYPE_UNSIGNED (type))
11740         encoding = DW_ATE_unsigned_fixed;
11741       else
11742         encoding = DW_ATE_signed_fixed;
11743       break;
11744
11745       /* Dwarf2 doesn't know anything about complex ints, so use
11746          a user defined type for it.  */
11747     case COMPLEX_TYPE:
11748       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
11749         encoding = DW_ATE_complex_float;
11750       else
11751         encoding = DW_ATE_lo_user;
11752       break;
11753
11754     case BOOLEAN_TYPE:
11755       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
11756       encoding = DW_ATE_boolean;
11757       break;
11758
11759     default:
11760       /* No other TREE_CODEs are Dwarf fundamental types.  */
11761       gcc_unreachable ();
11762     }
11763
11764   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
11765
11766   /* This probably indicates a bug.  */
11767   if (! TYPE_NAME (type))
11768     add_name_attribute (base_type_result, "__unknown__");
11769
11770   add_AT_unsigned (base_type_result, DW_AT_byte_size,
11771                    int_size_in_bytes (type));
11772   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
11773
11774   return base_type_result;
11775 }
11776
11777 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
11778    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
11779
11780 static inline int
11781 is_base_type (tree type)
11782 {
11783   switch (TREE_CODE (type))
11784     {
11785     case ERROR_MARK:
11786     case VOID_TYPE:
11787     case INTEGER_TYPE:
11788     case REAL_TYPE:
11789     case FIXED_POINT_TYPE:
11790     case COMPLEX_TYPE:
11791     case BOOLEAN_TYPE:
11792       return 1;
11793
11794     case ARRAY_TYPE:
11795     case RECORD_TYPE:
11796     case UNION_TYPE:
11797     case QUAL_UNION_TYPE:
11798     case ENUMERAL_TYPE:
11799     case FUNCTION_TYPE:
11800     case METHOD_TYPE:
11801     case POINTER_TYPE:
11802     case REFERENCE_TYPE:
11803     case OFFSET_TYPE:
11804     case LANG_TYPE:
11805     case VECTOR_TYPE:
11806       return 0;
11807
11808     default:
11809       gcc_unreachable ();
11810     }
11811
11812   return 0;
11813 }
11814
11815 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
11816    node, return the size in bits for the type if it is a constant, or else
11817    return the alignment for the type if the type's size is not constant, or
11818    else return BITS_PER_WORD if the type actually turns out to be an
11819    ERROR_MARK node.  */
11820
11821 static inline unsigned HOST_WIDE_INT
11822 simple_type_size_in_bits (const_tree type)
11823 {
11824   if (TREE_CODE (type) == ERROR_MARK)
11825     return BITS_PER_WORD;
11826   else if (TYPE_SIZE (type) == NULL_TREE)
11827     return 0;
11828   else if (host_integerp (TYPE_SIZE (type), 1))
11829     return tree_low_cst (TYPE_SIZE (type), 1);
11830   else
11831     return TYPE_ALIGN (type);
11832 }
11833
11834 /*  Given a pointer to a tree node for a subrange type, return a pointer
11835     to a DIE that describes the given type.  */
11836
11837 static dw_die_ref
11838 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
11839 {
11840   dw_die_ref subrange_die;
11841   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
11842
11843   if (context_die == NULL)
11844     context_die = comp_unit_die;
11845
11846   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
11847
11848   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
11849     {
11850       /* The size of the subrange type and its base type do not match,
11851          so we need to generate a size attribute for the subrange type.  */
11852       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
11853     }
11854
11855   if (low)
11856     add_bound_info (subrange_die, DW_AT_lower_bound, low);
11857   if (high)
11858     add_bound_info (subrange_die, DW_AT_upper_bound, high);
11859
11860   return subrange_die;
11861 }
11862
11863 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
11864    entry that chains various modifiers in front of the given type.  */
11865
11866 static dw_die_ref
11867 modified_type_die (tree type, int is_const_type, int is_volatile_type,
11868                    dw_die_ref context_die)
11869 {
11870   enum tree_code code = TREE_CODE (type);
11871   dw_die_ref mod_type_die;
11872   dw_die_ref sub_die = NULL;
11873   tree item_type = NULL;
11874   tree qualified_type;
11875   tree name, low, high;
11876
11877   if (code == ERROR_MARK)
11878     return NULL;
11879
11880   /* See if we already have the appropriately qualified variant of
11881      this type.  */
11882   qualified_type
11883     = get_qualified_type (type,
11884                           ((is_const_type ? TYPE_QUAL_CONST : 0)
11885                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
11886
11887   /* If we do, then we can just use its DIE, if it exists.  */
11888   if (qualified_type)
11889     {
11890       mod_type_die = lookup_type_die (qualified_type);
11891       if (mod_type_die)
11892         return mod_type_die;
11893     }
11894
11895   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
11896
11897   /* Handle C typedef types.  */
11898   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
11899     {
11900       tree dtype = TREE_TYPE (name);
11901
11902       if (qualified_type == dtype)
11903         {
11904           /* For a named type, use the typedef.  */
11905           gen_type_die (qualified_type, context_die);
11906           return lookup_type_die (qualified_type);
11907         }
11908       else if (is_const_type < TYPE_READONLY (dtype)
11909                || is_volatile_type < TYPE_VOLATILE (dtype)
11910                || (is_const_type <= TYPE_READONLY (dtype)
11911                    && is_volatile_type <= TYPE_VOLATILE (dtype)
11912                    && DECL_ORIGINAL_TYPE (name) != type))
11913         /* cv-unqualified version of named type.  Just use the unnamed
11914            type to which it refers.  */
11915         return modified_type_die (DECL_ORIGINAL_TYPE (name),
11916                                   is_const_type, is_volatile_type,
11917                                   context_die);
11918       /* Else cv-qualified version of named type; fall through.  */
11919     }
11920
11921   if (is_const_type)
11922     {
11923       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
11924       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
11925     }
11926   else if (is_volatile_type)
11927     {
11928       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
11929       sub_die = modified_type_die (type, 0, 0, context_die);
11930     }
11931   else if (code == POINTER_TYPE)
11932     {
11933       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
11934       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11935                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
11936       item_type = TREE_TYPE (type);
11937     }
11938   else if (code == REFERENCE_TYPE)
11939     {
11940       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
11941       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
11942                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
11943       item_type = TREE_TYPE (type);
11944     }
11945   else if (code == INTEGER_TYPE
11946            && TREE_TYPE (type) != NULL_TREE
11947            && subrange_type_for_debug_p (type, &low, &high))
11948     {
11949       mod_type_die = subrange_type_die (type, low, high, context_die);
11950       item_type = TREE_TYPE (type);
11951     }
11952   else if (is_base_type (type))
11953     mod_type_die = base_type_die (type);
11954   else
11955     {
11956       gen_type_die (type, context_die);
11957
11958       /* We have to get the type_main_variant here (and pass that to the
11959          `lookup_type_die' routine) because the ..._TYPE node we have
11960          might simply be a *copy* of some original type node (where the
11961          copy was created to help us keep track of typedef names) and
11962          that copy might have a different TYPE_UID from the original
11963          ..._TYPE node.  */
11964       if (TREE_CODE (type) != VECTOR_TYPE)
11965         return lookup_type_die (type_main_variant (type));
11966       else
11967         /* Vectors have the debugging information in the type,
11968            not the main variant.  */
11969         return lookup_type_die (type);
11970     }
11971
11972   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
11973      don't output a DW_TAG_typedef, since there isn't one in the
11974      user's program; just attach a DW_AT_name to the type.
11975      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
11976      if the base type already has the same name.  */
11977   if (name
11978       && ((TREE_CODE (name) != TYPE_DECL
11979            && (qualified_type == TYPE_MAIN_VARIANT (type)
11980                || (!is_const_type && !is_volatile_type)))
11981           || (TREE_CODE (name) == TYPE_DECL
11982               && TREE_TYPE (name) == qualified_type
11983               && DECL_NAME (name))))
11984     {
11985       if (TREE_CODE (name) == TYPE_DECL)
11986         /* Could just call add_name_and_src_coords_attributes here,
11987            but since this is a builtin type it doesn't have any
11988            useful source coordinates anyway.  */
11989         name = DECL_NAME (name);
11990       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
11991     }
11992
11993   if (qualified_type)
11994     equate_type_number_to_die (qualified_type, mod_type_die);
11995
11996   if (item_type)
11997     /* We must do this after the equate_type_number_to_die call, in case
11998        this is a recursive type.  This ensures that the modified_type_die
11999        recursion will terminate even if the type is recursive.  Recursive
12000        types are possible in Ada.  */
12001     sub_die = modified_type_die (item_type,
12002                                  TYPE_READONLY (item_type),
12003                                  TYPE_VOLATILE (item_type),
12004                                  context_die);
12005
12006   if (sub_die != NULL)
12007     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12008
12009   return mod_type_die;
12010 }
12011
12012 /* Generate DIEs for the generic parameters of T.
12013    T must be either a generic type or a generic function.
12014    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12015
12016 static void
12017 gen_generic_params_dies (tree t)
12018 {
12019   tree parms, args;
12020   int parms_num, i;
12021   dw_die_ref die = NULL;
12022
12023   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12024     return;
12025
12026   if (TYPE_P (t))
12027     die = lookup_type_die (t);
12028   else if (DECL_P (t))
12029     die = lookup_decl_die (t);
12030
12031   gcc_assert (die);
12032
12033   parms = lang_hooks.get_innermost_generic_parms (t);
12034   if (!parms)
12035     /* T has no generic parameter. It means T is neither a generic type
12036        or function. End of story.  */
12037     return;
12038
12039   parms_num = TREE_VEC_LENGTH (parms);
12040   args = lang_hooks.get_innermost_generic_args (t);
12041   for (i = 0; i < parms_num; i++)
12042     {
12043       tree parm, arg, arg_pack_elems;
12044
12045       parm = TREE_VEC_ELT (parms, i);
12046       arg = TREE_VEC_ELT (args, i);
12047       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12048       gcc_assert (parm && TREE_VALUE (parm) && arg);
12049
12050       if (parm && TREE_VALUE (parm) && arg)
12051         {
12052           /* If PARM represents a template parameter pack,
12053              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12054              by DW_TAG_template_*_parameter DIEs for the argument
12055              pack elements of ARG. Note that ARG would then be
12056              an argument pack.  */
12057           if (arg_pack_elems)
12058             template_parameter_pack_die (TREE_VALUE (parm),
12059                                          arg_pack_elems,
12060                                          die);
12061           else
12062             generic_parameter_die (TREE_VALUE (parm), arg,
12063                                    true /* Emit DW_AT_name */, die);
12064         }
12065     }
12066 }
12067
12068 /* Create and return a DIE for PARM which should be
12069    the representation of a generic type parameter.
12070    For instance, in the C++ front end, PARM would be a template parameter.
12071    ARG is the argument to PARM.
12072    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12073    name of the PARM.
12074    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12075    as a child node.  */
12076
12077 static dw_die_ref
12078 generic_parameter_die (tree parm, tree arg,
12079                        bool emit_name_p,
12080                        dw_die_ref parent_die)
12081 {
12082   dw_die_ref tmpl_die = NULL;
12083   const char *name = NULL;
12084
12085   if (!parm || !DECL_NAME (parm) || !arg)
12086     return NULL;
12087
12088   /* We support non-type generic parameters and arguments,
12089      type generic parameters and arguments, as well as
12090      generic generic parameters (a.k.a. template template parameters in C++)
12091      and arguments.  */
12092   if (TREE_CODE (parm) == PARM_DECL)
12093     /* PARM is a nontype generic parameter  */
12094     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12095   else if (TREE_CODE (parm) == TYPE_DECL)
12096     /* PARM is a type generic parameter.  */
12097     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12098   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12099     /* PARM is a generic generic parameter.
12100        Its DIE is a GNU extension. It shall have a
12101        DW_AT_name attribute to represent the name of the template template
12102        parameter, and a DW_AT_GNU_template_name attribute to represent the
12103        name of the template template argument.  */
12104     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12105                         parent_die, parm);
12106   else
12107     gcc_unreachable ();
12108
12109   if (tmpl_die)
12110     {
12111       tree tmpl_type;
12112
12113       /* If PARM is a generic parameter pack, it means we are
12114          emitting debug info for a template argument pack element.
12115          In other terms, ARG is a template argument pack element.
12116          In that case, we don't emit any DW_AT_name attribute for
12117          the die.  */
12118       if (emit_name_p)
12119         {
12120           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12121           gcc_assert (name);
12122           add_AT_string (tmpl_die, DW_AT_name, name);
12123         }
12124
12125       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12126         {
12127           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12128              TMPL_DIE should have a child DW_AT_type attribute that is set
12129              to the type of the argument to PARM, which is ARG.
12130              If PARM is a type generic parameter, TMPL_DIE should have a
12131              child DW_AT_type that is set to ARG.  */
12132           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12133           add_type_attribute (tmpl_die, tmpl_type, 0,
12134                               TREE_THIS_VOLATILE (tmpl_type),
12135                               parent_die);
12136         }
12137       else
12138         {
12139           /* So TMPL_DIE is a DIE representing a
12140              a generic generic template parameter, a.k.a template template
12141              parameter in C++ and arg is a template.  */
12142
12143           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12144              to the name of the argument.  */
12145           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12146           add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12147         }
12148
12149       if (TREE_CODE (parm) == PARM_DECL)
12150         /* So PARM is a non-type generic parameter.
12151            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12152            attribute of TMPL_DIE which value represents the value
12153            of ARG.
12154            We must be careful here:
12155            The value of ARG might reference some function decls.
12156            We might currently be emitting debug info for a generic
12157            type and types are emitted before function decls, we don't
12158            know if the function decls referenced by ARG will actually be
12159            emitted after cgraph computations.
12160            So must defer the generation of the DW_AT_const_value to
12161            after cgraph is ready.  */
12162         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12163     }
12164
12165   return tmpl_die;
12166 }
12167
12168 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12169    PARM_PACK must be a template parameter pack. The returned DIE
12170    will be child DIE of PARENT_DIE.  */
12171
12172 static dw_die_ref
12173 template_parameter_pack_die (tree parm_pack,
12174                              tree parm_pack_args,
12175                              dw_die_ref parent_die)
12176 {
12177   dw_die_ref die;
12178   int j;
12179
12180   gcc_assert (parent_die
12181               && parm_pack
12182               && DECL_NAME (parm_pack));
12183
12184   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12185   add_AT_string (die, DW_AT_name, IDENTIFIER_POINTER (DECL_NAME (parm_pack)));
12186
12187   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12188     generic_parameter_die (parm_pack,
12189                            TREE_VEC_ELT (parm_pack_args, j),
12190                            false /* Don't emit DW_AT_name */,
12191                            die);
12192   return die;
12193 }
12194
12195 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12196    an enumerated type.  */
12197
12198 static inline int
12199 type_is_enum (const_tree type)
12200 {
12201   return TREE_CODE (type) == ENUMERAL_TYPE;
12202 }
12203
12204 /* Return the DBX register number described by a given RTL node.  */
12205
12206 static unsigned int
12207 dbx_reg_number (const_rtx rtl)
12208 {
12209   unsigned regno = REGNO (rtl);
12210
12211   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12212
12213 #ifdef LEAF_REG_REMAP
12214   if (current_function_uses_only_leaf_regs)
12215     {
12216       int leaf_reg = LEAF_REG_REMAP (regno);
12217       if (leaf_reg != -1)
12218         regno = (unsigned) leaf_reg;
12219     }
12220 #endif
12221
12222   return DBX_REGISTER_NUMBER (regno);
12223 }
12224
12225 /* Optionally add a DW_OP_piece term to a location description expression.
12226    DW_OP_piece is only added if the location description expression already
12227    doesn't end with DW_OP_piece.  */
12228
12229 static void
12230 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
12231 {
12232   dw_loc_descr_ref loc;
12233
12234   if (*list_head != NULL)
12235     {
12236       /* Find the end of the chain.  */
12237       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
12238         ;
12239
12240       if (loc->dw_loc_opc != DW_OP_piece)
12241         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
12242     }
12243 }
12244
12245 /* Return a location descriptor that designates a machine register or
12246    zero if there is none.  */
12247
12248 static dw_loc_descr_ref
12249 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
12250 {
12251   rtx regs;
12252
12253   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
12254     return 0;
12255
12256   regs = targetm.dwarf_register_span (rtl);
12257
12258   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
12259     return multiple_reg_loc_descriptor (rtl, regs, initialized);
12260   else
12261     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
12262 }
12263
12264 /* Return a location descriptor that designates a machine register for
12265    a given hard register number.  */
12266
12267 static dw_loc_descr_ref
12268 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
12269 {
12270   dw_loc_descr_ref reg_loc_descr;
12271
12272   if (regno <= 31)
12273     reg_loc_descr
12274       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
12275   else
12276     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
12277
12278   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12279     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12280
12281   return reg_loc_descr;
12282 }
12283
12284 /* Given an RTL of a register, return a location descriptor that
12285    designates a value that spans more than one register.  */
12286
12287 static dw_loc_descr_ref
12288 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
12289                              enum var_init_status initialized)
12290 {
12291   int nregs, size, i;
12292   unsigned reg;
12293   dw_loc_descr_ref loc_result = NULL;
12294
12295   reg = REGNO (rtl);
12296 #ifdef LEAF_REG_REMAP
12297   if (current_function_uses_only_leaf_regs)
12298     {
12299       int leaf_reg = LEAF_REG_REMAP (reg);
12300       if (leaf_reg != -1)
12301         reg = (unsigned) leaf_reg;
12302     }
12303 #endif
12304   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
12305   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
12306
12307   /* Simple, contiguous registers.  */
12308   if (regs == NULL_RTX)
12309     {
12310       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
12311
12312       loc_result = NULL;
12313       while (nregs--)
12314         {
12315           dw_loc_descr_ref t;
12316
12317           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
12318                                       VAR_INIT_STATUS_INITIALIZED);
12319           add_loc_descr (&loc_result, t);
12320           add_loc_descr_op_piece (&loc_result, size);
12321           ++reg;
12322         }
12323       return loc_result;
12324     }
12325
12326   /* Now onto stupid register sets in non contiguous locations.  */
12327
12328   gcc_assert (GET_CODE (regs) == PARALLEL);
12329
12330   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
12331   loc_result = NULL;
12332
12333   for (i = 0; i < XVECLEN (regs, 0); ++i)
12334     {
12335       dw_loc_descr_ref t;
12336
12337       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
12338                                   VAR_INIT_STATUS_INITIALIZED);
12339       add_loc_descr (&loc_result, t);
12340       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
12341       add_loc_descr_op_piece (&loc_result, size);
12342     }
12343
12344   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12345     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12346   return loc_result;
12347 }
12348
12349 #endif /* DWARF2_DEBUGGING_INFO */
12350
12351 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
12352
12353 /* Return a location descriptor that designates a constant.  */
12354
12355 static dw_loc_descr_ref
12356 int_loc_descriptor (HOST_WIDE_INT i)
12357 {
12358   enum dwarf_location_atom op;
12359
12360   /* Pick the smallest representation of a constant, rather than just
12361      defaulting to the LEB encoding.  */
12362   if (i >= 0)
12363     {
12364       if (i <= 31)
12365         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
12366       else if (i <= 0xff)
12367         op = DW_OP_const1u;
12368       else if (i <= 0xffff)
12369         op = DW_OP_const2u;
12370       else if (HOST_BITS_PER_WIDE_INT == 32
12371                || i <= 0xffffffff)
12372         op = DW_OP_const4u;
12373       else
12374         op = DW_OP_constu;
12375     }
12376   else
12377     {
12378       if (i >= -0x80)
12379         op = DW_OP_const1s;
12380       else if (i >= -0x8000)
12381         op = DW_OP_const2s;
12382       else if (HOST_BITS_PER_WIDE_INT == 32
12383                || i >= -0x80000000)
12384         op = DW_OP_const4s;
12385       else
12386         op = DW_OP_consts;
12387     }
12388
12389   return new_loc_descr (op, i, 0);
12390 }
12391 #endif
12392
12393 #ifdef DWARF2_DEBUGGING_INFO
12394 /* Return loc description representing "address" of integer value.
12395    This can appear only as toplevel expression.  */
12396
12397 static dw_loc_descr_ref
12398 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
12399 {
12400   int litsize;
12401   dw_loc_descr_ref loc_result = NULL;
12402
12403   if (!(dwarf_version >= 4 || !dwarf_strict))
12404     return NULL;
12405
12406   if (i >= 0)
12407     {
12408       if (i <= 31)
12409         litsize = 1;
12410       else if (i <= 0xff)
12411         litsize = 2;
12412       else if (i <= 0xffff)
12413         litsize = 3;
12414       else if (HOST_BITS_PER_WIDE_INT == 32
12415                || i <= 0xffffffff)
12416         litsize = 5;
12417       else
12418         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
12419     }
12420   else
12421     {
12422       if (i >= -0x80)
12423         litsize = 2;
12424       else if (i >= -0x8000)
12425         litsize = 3;
12426       else if (HOST_BITS_PER_WIDE_INT == 32
12427                || i >= -0x80000000)
12428         litsize = 5;
12429       else
12430         litsize = 1 + size_of_sleb128 (i);
12431     }
12432   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
12433      is more compact.  For DW_OP_stack_value we need:
12434      litsize + 1 (DW_OP_stack_value)
12435      and for DW_OP_implicit_value:
12436      1 (DW_OP_implicit_value) + 1 (length) + size.  */
12437   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
12438     {
12439       loc_result = int_loc_descriptor (i);
12440       add_loc_descr (&loc_result,
12441                      new_loc_descr (DW_OP_stack_value, 0, 0));
12442       return loc_result;
12443     }
12444
12445   loc_result = new_loc_descr (DW_OP_implicit_value,
12446                               size, 0);
12447   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12448   loc_result->dw_loc_oprnd2.v.val_int = i;
12449   return loc_result;
12450 }
12451
12452 /* Return a location descriptor that designates a base+offset location.  */
12453
12454 static dw_loc_descr_ref
12455 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
12456                  enum var_init_status initialized)
12457 {
12458   unsigned int regno;
12459   dw_loc_descr_ref result;
12460   dw_fde_ref fde = current_fde ();
12461
12462   /* We only use "frame base" when we're sure we're talking about the
12463      post-prologue local stack frame.  We do this by *not* running
12464      register elimination until this point, and recognizing the special
12465      argument pointer and soft frame pointer rtx's.  */
12466   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
12467     {
12468       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
12469
12470       if (elim != reg)
12471         {
12472           if (GET_CODE (elim) == PLUS)
12473             {
12474               offset += INTVAL (XEXP (elim, 1));
12475               elim = XEXP (elim, 0);
12476             }
12477           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
12478                        && (elim == hard_frame_pointer_rtx
12479                            || elim == stack_pointer_rtx))
12480                       || elim == (frame_pointer_needed
12481                                   ? hard_frame_pointer_rtx
12482                                   : stack_pointer_rtx));
12483
12484           /* If drap register is used to align stack, use frame
12485              pointer + offset to access stack variables.  If stack
12486              is aligned without drap, use stack pointer + offset to
12487              access stack variables.  */
12488           if (crtl->stack_realign_tried
12489               && reg == frame_pointer_rtx)
12490             {
12491               int base_reg
12492                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
12493                                       ? HARD_FRAME_POINTER_REGNUM
12494                                       : STACK_POINTER_REGNUM);
12495               return new_reg_loc_descr (base_reg, offset);
12496             }
12497
12498           offset += frame_pointer_fb_offset;
12499           return new_loc_descr (DW_OP_fbreg, offset, 0);
12500         }
12501     }
12502   else if (fde
12503            && fde->drap_reg != INVALID_REGNUM
12504            && (fde->drap_reg == REGNO (reg)
12505                || fde->vdrap_reg == REGNO (reg)))
12506     {
12507       /* Use cfa+offset to represent the location of arguments passed
12508          on stack when drap is used to align stack.  */
12509       return new_loc_descr (DW_OP_fbreg, offset, 0);
12510     }
12511
12512   regno = dbx_reg_number (reg);
12513   if (regno <= 31)
12514     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
12515                             offset, 0);
12516   else
12517     result = new_loc_descr (DW_OP_bregx, regno, offset);
12518
12519   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12520     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12521
12522   return result;
12523 }
12524
12525 /* Return true if this RTL expression describes a base+offset calculation.  */
12526
12527 static inline int
12528 is_based_loc (const_rtx rtl)
12529 {
12530   return (GET_CODE (rtl) == PLUS
12531           && ((REG_P (XEXP (rtl, 0))
12532                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
12533                && CONST_INT_P (XEXP (rtl, 1)))));
12534 }
12535
12536 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
12537    failed.  */
12538
12539 static dw_loc_descr_ref
12540 tls_mem_loc_descriptor (rtx mem)
12541 {
12542   tree base;
12543   dw_loc_descr_ref loc_result;
12544
12545   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
12546     return NULL;
12547
12548   base = get_base_address (MEM_EXPR (mem));
12549   if (base == NULL
12550       || TREE_CODE (base) != VAR_DECL
12551       || !DECL_THREAD_LOCAL_P (base))
12552     return NULL;
12553
12554   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
12555   if (loc_result == NULL)
12556     return NULL;
12557
12558   if (INTVAL (MEM_OFFSET (mem)))
12559     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
12560
12561   return loc_result;
12562 }
12563
12564 /* Output debug info about reason why we failed to expand expression as dwarf
12565    expression.  */
12566
12567 static void
12568 expansion_failed (tree expr, rtx rtl, char const *reason)
12569 {
12570   if (dump_file && (dump_flags & TDF_DETAILS))
12571     {
12572       fprintf (dump_file, "Failed to expand as dwarf: ");
12573       if (expr)
12574         print_generic_expr (dump_file, expr, dump_flags);
12575       if (rtl)
12576         {
12577           fprintf (dump_file, "\n");
12578           print_rtl (dump_file, rtl);
12579         }
12580       fprintf (dump_file, "\nReason: %s\n", reason);
12581     }
12582 }
12583
12584 /* Helper function for const_ok_for_output, called either directly
12585    or via for_each_rtx.  */
12586
12587 static int
12588 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
12589 {
12590   rtx rtl = *rtlp;
12591
12592   if (GET_CODE (rtl) != SYMBOL_REF)
12593     return 0;
12594
12595   if (CONSTANT_POOL_ADDRESS_P (rtl))
12596     {
12597       bool marked;
12598       get_pool_constant_mark (rtl, &marked);
12599       /* If all references to this pool constant were optimized away,
12600          it was not output and thus we can't represent it.  */
12601       if (!marked)
12602         {
12603           expansion_failed (NULL_TREE, rtl,
12604                             "Constant was removed from constant pool.\n");
12605           return 1;
12606         }
12607     }
12608
12609   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12610     return 1;
12611
12612   /* Avoid references to external symbols in debug info, on several targets
12613      the linker might even refuse to link when linking a shared library,
12614      and in many other cases the relocations for .debug_info/.debug_loc are
12615      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
12616      to be defined within the same shared library or executable are fine.  */
12617   if (SYMBOL_REF_EXTERNAL_P (rtl))
12618     {
12619       tree decl = SYMBOL_REF_DECL (rtl);
12620
12621       if (decl == NULL || !targetm.binds_local_p (decl))
12622         {
12623           expansion_failed (NULL_TREE, rtl,
12624                             "Symbol not defined in current TU.\n");
12625           return 1;
12626         }
12627     }
12628
12629   return 0;
12630 }
12631
12632 /* Return true if constant RTL can be emitted in DW_OP_addr or
12633    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
12634    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
12635
12636 static bool
12637 const_ok_for_output (rtx rtl)
12638 {
12639   if (GET_CODE (rtl) == SYMBOL_REF)
12640     return const_ok_for_output_1 (&rtl, NULL) == 0;
12641
12642   if (GET_CODE (rtl) == CONST)
12643     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
12644
12645   return true;
12646 }
12647
12648 /* The following routine converts the RTL for a variable or parameter
12649    (resident in memory) into an equivalent Dwarf representation of a
12650    mechanism for getting the address of that same variable onto the top of a
12651    hypothetical "address evaluation" stack.
12652
12653    When creating memory location descriptors, we are effectively transforming
12654    the RTL for a memory-resident object into its Dwarf postfix expression
12655    equivalent.  This routine recursively descends an RTL tree, turning
12656    it into Dwarf postfix code as it goes.
12657
12658    MODE is the mode of the memory reference, needed to handle some
12659    autoincrement addressing modes.
12660
12661    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
12662    location list for RTL.
12663
12664    Return 0 if we can't represent the location.  */
12665
12666 static dw_loc_descr_ref
12667 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
12668                     enum var_init_status initialized)
12669 {
12670   dw_loc_descr_ref mem_loc_result = NULL;
12671   enum dwarf_location_atom op;
12672   dw_loc_descr_ref op0, op1;
12673
12674   /* Note that for a dynamically sized array, the location we will generate a
12675      description of here will be the lowest numbered location which is
12676      actually within the array.  That's *not* necessarily the same as the
12677      zeroth element of the array.  */
12678
12679   rtl = targetm.delegitimize_address (rtl);
12680
12681   switch (GET_CODE (rtl))
12682     {
12683     case POST_INC:
12684     case POST_DEC:
12685     case POST_MODIFY:
12686       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
12687          just fall into the SUBREG code.  */
12688
12689       /* ... fall through ...  */
12690
12691     case SUBREG:
12692       /* The case of a subreg may arise when we have a local (register)
12693          variable or a formal (register) parameter which doesn't quite fill
12694          up an entire register.  For now, just assume that it is
12695          legitimate to make the Dwarf info refer to the whole register which
12696          contains the given subreg.  */
12697       rtl = XEXP (rtl, 0);
12698       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
12699         break;
12700       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
12701       break;
12702
12703     case REG:
12704       /* Whenever a register number forms a part of the description of the
12705          method for calculating the (dynamic) address of a memory resident
12706          object, DWARF rules require the register number be referred to as
12707          a "base register".  This distinction is not based in any way upon
12708          what category of register the hardware believes the given register
12709          belongs to.  This is strictly DWARF terminology we're dealing with
12710          here. Note that in cases where the location of a memory-resident
12711          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12712          OP_CONST (0)) the actual DWARF location descriptor that we generate
12713          may just be OP_BASEREG (basereg).  This may look deceptively like
12714          the object in question was allocated to a register (rather than in
12715          memory) so DWARF consumers need to be aware of the subtle
12716          distinction between OP_REG and OP_BASEREG.  */
12717       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12718         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12719       else if (stack_realign_drap
12720                && crtl->drap_reg
12721                && crtl->args.internal_arg_pointer == rtl
12722                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12723         {
12724           /* If RTL is internal_arg_pointer, which has been optimized
12725              out, use DRAP instead.  */
12726           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12727                                             VAR_INIT_STATUS_INITIALIZED);
12728         }
12729       break;
12730
12731     case SIGN_EXTEND:
12732     case ZERO_EXTEND:
12733       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
12734                                 VAR_INIT_STATUS_INITIALIZED);
12735       if (op0 == 0)
12736         break;
12737       else
12738         {
12739           int shift = DWARF2_ADDR_SIZE
12740                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12741           shift *= BITS_PER_UNIT;
12742           if (GET_CODE (rtl) == SIGN_EXTEND)
12743             op = DW_OP_shra;
12744           else
12745             op = DW_OP_shr;
12746           mem_loc_result = op0;
12747           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12748           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12749           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12750           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12751         }
12752       break;
12753
12754     case MEM:
12755       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
12756                                            VAR_INIT_STATUS_INITIALIZED);
12757       if (mem_loc_result == NULL)
12758         mem_loc_result = tls_mem_loc_descriptor (rtl);
12759       if (mem_loc_result != 0)
12760         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12761       break;
12762
12763     case LO_SUM:
12764          rtl = XEXP (rtl, 1);
12765
12766       /* ... fall through ...  */
12767
12768     case LABEL_REF:
12769       /* Some ports can transform a symbol ref into a label ref, because
12770          the symbol ref is too far away and has to be dumped into a constant
12771          pool.  */
12772     case CONST:
12773     case SYMBOL_REF:
12774       /* Alternatively, the symbol in the constant pool might be referenced
12775          by a different symbol.  */
12776       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
12777         {
12778           bool marked;
12779           rtx tmp = get_pool_constant_mark (rtl, &marked);
12780
12781           if (GET_CODE (tmp) == SYMBOL_REF)
12782             {
12783               rtl = tmp;
12784               if (CONSTANT_POOL_ADDRESS_P (tmp))
12785                 get_pool_constant_mark (tmp, &marked);
12786               else
12787                 marked = true;
12788             }
12789
12790           /* If all references to this pool constant were optimized away,
12791              it was not output and thus we can't represent it.
12792              FIXME: might try to use DW_OP_const_value here, though
12793              DW_OP_piece complicates it.  */
12794           if (!marked)
12795             {
12796               expansion_failed (NULL_TREE, rtl,
12797                                 "Constant was removed from constant pool.\n");
12798               return 0;
12799             }
12800         }
12801
12802       if (GET_CODE (rtl) == SYMBOL_REF
12803           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12804         {
12805           dw_loc_descr_ref temp;
12806
12807           /* If this is not defined, we have no way to emit the data.  */
12808           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12809             break;
12810
12811           temp = new_loc_descr (DW_OP_addr, 0, 0);
12812           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
12813           temp->dw_loc_oprnd1.v.val_addr = rtl;
12814           temp->dtprel = true;
12815
12816           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12817           add_loc_descr (&mem_loc_result, temp);
12818
12819           break;
12820         }
12821
12822       if (!const_ok_for_output (rtl))
12823         break;
12824
12825     symref:
12826       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12827       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12828       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12829       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12830       break;
12831
12832     case CONCAT:
12833     case CONCATN:
12834     case VAR_LOCATION:
12835       expansion_failed (NULL_TREE, rtl,
12836                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12837       return 0;
12838
12839     case PRE_MODIFY:
12840       /* Extract the PLUS expression nested inside and fall into
12841          PLUS code below.  */
12842       rtl = XEXP (rtl, 1);
12843       goto plus;
12844
12845     case PRE_INC:
12846     case PRE_DEC:
12847       /* Turn these into a PLUS expression and fall into the PLUS code
12848          below.  */
12849       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
12850                           GEN_INT (GET_CODE (rtl) == PRE_INC
12851                                    ? GET_MODE_UNIT_SIZE (mode)
12852                                    : -GET_MODE_UNIT_SIZE (mode)));
12853
12854       /* ... fall through ...  */
12855
12856     case PLUS:
12857     plus:
12858       if (is_based_loc (rtl))
12859         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12860                                           INTVAL (XEXP (rtl, 1)),
12861                                           VAR_INIT_STATUS_INITIALIZED);
12862       else
12863         {
12864           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
12865                                                VAR_INIT_STATUS_INITIALIZED);
12866           if (mem_loc_result == 0)
12867             break;
12868
12869           if (CONST_INT_P (XEXP (rtl, 1)))
12870             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12871           else
12872             {
12873               dw_loc_descr_ref mem_loc_result2
12874                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
12875                                       VAR_INIT_STATUS_INITIALIZED);
12876               if (mem_loc_result2 == 0)
12877                 break;
12878               add_loc_descr (&mem_loc_result, mem_loc_result2);
12879               add_loc_descr (&mem_loc_result,
12880                              new_loc_descr (DW_OP_plus, 0, 0));
12881             }
12882         }
12883       break;
12884
12885     /* If a pseudo-reg is optimized away, it is possible for it to
12886        be replaced with a MEM containing a multiply or shift.  */
12887     case MINUS:
12888       op = DW_OP_minus;
12889       goto do_binop;
12890
12891     case MULT:
12892       op = DW_OP_mul;
12893       goto do_binop;
12894
12895     case DIV:
12896       op = DW_OP_div;
12897       goto do_binop;
12898
12899     case MOD:
12900       op = DW_OP_mod;
12901       goto do_binop;
12902
12903     case ASHIFT:
12904       op = DW_OP_shl;
12905       goto do_binop;
12906
12907     case ASHIFTRT:
12908       op = DW_OP_shra;
12909       goto do_binop;
12910
12911     case LSHIFTRT:
12912       op = DW_OP_shr;
12913       goto do_binop;
12914
12915     case AND:
12916       op = DW_OP_and;
12917       goto do_binop;
12918
12919     case IOR:
12920       op = DW_OP_or;
12921       goto do_binop;
12922
12923     case XOR:
12924       op = DW_OP_xor;
12925       goto do_binop;
12926
12927     do_binop:
12928       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
12929                                 VAR_INIT_STATUS_INITIALIZED);
12930       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
12931                                 VAR_INIT_STATUS_INITIALIZED);
12932
12933       if (op0 == 0 || op1 == 0)
12934         break;
12935
12936       mem_loc_result = op0;
12937       add_loc_descr (&mem_loc_result, op1);
12938       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12939       break;
12940
12941     case NOT:
12942       op = DW_OP_not;
12943       goto do_unop;
12944
12945     case ABS:
12946       op = DW_OP_abs;
12947       goto do_unop;
12948
12949     case NEG:
12950       op = DW_OP_neg;
12951       goto do_unop;
12952
12953     do_unop:
12954       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
12955                                 VAR_INIT_STATUS_INITIALIZED);
12956
12957       if (op0 == 0)
12958         break;
12959
12960       mem_loc_result = op0;
12961       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12962       break;
12963
12964     case CONST_INT:
12965       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12966       break;
12967
12968     case EQ:
12969       op = DW_OP_eq;
12970       goto do_scompare;
12971
12972     case GE:
12973       op = DW_OP_ge;
12974       goto do_scompare;
12975
12976     case GT:
12977       op = DW_OP_gt;
12978       goto do_scompare;
12979
12980     case LE:
12981       op = DW_OP_le;
12982       goto do_scompare;
12983
12984     case LT:
12985       op = DW_OP_lt;
12986       goto do_scompare;
12987
12988     case NE:
12989       op = DW_OP_ne;
12990       goto do_scompare;
12991
12992     do_scompare:
12993       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
12994           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
12995           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
12996         break;
12997
12998       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
12999                                 VAR_INIT_STATUS_INITIALIZED);
13000       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13001                                 VAR_INIT_STATUS_INITIALIZED);
13002
13003       if (op0 == 0 || op1 == 0)
13004         break;
13005
13006       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13007         {
13008           int shift = DWARF2_ADDR_SIZE
13009                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13010           shift *= BITS_PER_UNIT;
13011           add_loc_descr (&op0, int_loc_descriptor (shift));
13012           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13013           if (CONST_INT_P (XEXP (rtl, 1)))
13014             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13015           else
13016             {
13017               add_loc_descr (&op1, int_loc_descriptor (shift));
13018               add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13019             }
13020         }
13021
13022     do_compare:
13023       mem_loc_result = op0;
13024       add_loc_descr (&mem_loc_result, op1);
13025       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13026       if (STORE_FLAG_VALUE != 1)
13027         {
13028           add_loc_descr (&mem_loc_result,
13029                          int_loc_descriptor (STORE_FLAG_VALUE));
13030           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13031         }
13032       break;
13033
13034     case GEU:
13035       op = DW_OP_ge;
13036       goto do_ucompare;
13037
13038     case GTU:
13039       op = DW_OP_gt;
13040       goto do_ucompare;
13041
13042     case LEU:
13043       op = DW_OP_le;
13044       goto do_ucompare;
13045
13046     case LTU:
13047       op = DW_OP_lt;
13048       goto do_ucompare;
13049
13050     do_ucompare:
13051       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13052           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13053           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13054         break;
13055
13056       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13057                                 VAR_INIT_STATUS_INITIALIZED);
13058       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13059                                 VAR_INIT_STATUS_INITIALIZED);
13060
13061       if (op0 == 0 || op1 == 0)
13062         break;
13063
13064       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13065         {
13066           HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
13067           add_loc_descr (&op0, int_loc_descriptor (mask));
13068           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13069           if (CONST_INT_P (XEXP (rtl, 1)))
13070             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13071           else
13072             {
13073               add_loc_descr (&op1, int_loc_descriptor (mask));
13074               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13075             }
13076         }
13077       else
13078         {
13079           HOST_WIDE_INT bias = 1;
13080           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13081           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13082           if (CONST_INT_P (XEXP (rtl, 1)))
13083             op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13084                                       + INTVAL (XEXP (rtl, 1)));
13085           else
13086             add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13087         }
13088       goto do_compare;
13089
13090     case SMIN:
13091     case SMAX:
13092     case UMIN:
13093     case UMAX:
13094       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13095           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13096           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13097         break;
13098
13099       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13100                                 VAR_INIT_STATUS_INITIALIZED);
13101       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13102                                 VAR_INIT_STATUS_INITIALIZED);
13103
13104       if (op0 == 0 || op1 == 0)
13105         break;
13106
13107       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
13108       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
13109       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
13110       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
13111         {
13112           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13113             {
13114               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
13115               add_loc_descr (&op0, int_loc_descriptor (mask));
13116               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13117               add_loc_descr (&op1, int_loc_descriptor (mask));
13118               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13119             }
13120           else
13121             {
13122               HOST_WIDE_INT bias = 1;
13123               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13124               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13125               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13126             }
13127         }
13128       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
13129         {
13130           int shift = DWARF2_ADDR_SIZE
13131                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13132           shift *= BITS_PER_UNIT;
13133           add_loc_descr (&op0, int_loc_descriptor (shift));
13134           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13135           add_loc_descr (&op1, int_loc_descriptor (shift));
13136           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13137         }
13138
13139       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
13140         op = DW_OP_lt;
13141       else
13142         op = DW_OP_gt;
13143       mem_loc_result = op0;
13144       add_loc_descr (&mem_loc_result, op1);
13145       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13146       {
13147         dw_loc_descr_ref bra_node, drop_node;
13148
13149         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13150         add_loc_descr (&mem_loc_result, bra_node);
13151         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
13152         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
13153         add_loc_descr (&mem_loc_result, drop_node);
13154         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13155         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
13156       }
13157       break;
13158
13159     case ZERO_EXTRACT:
13160     case SIGN_EXTRACT:
13161       if (CONST_INT_P (XEXP (rtl, 1))
13162           && CONST_INT_P (XEXP (rtl, 2))
13163           && ((unsigned) INTVAL (XEXP (rtl, 1))
13164               + (unsigned) INTVAL (XEXP (rtl, 2))
13165               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
13166           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13167           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
13168         {
13169           int shift, size;
13170           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13171                                     VAR_INIT_STATUS_INITIALIZED);
13172           if (op0 == 0)
13173             break;
13174           if (GET_CODE (rtl) == SIGN_EXTRACT)
13175             op = DW_OP_shra;
13176           else
13177             op = DW_OP_shr;
13178           mem_loc_result = op0;
13179           size = INTVAL (XEXP (rtl, 1));
13180           shift = INTVAL (XEXP (rtl, 2));
13181           if (BITS_BIG_ENDIAN)
13182             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
13183                     - shift - size;
13184           add_loc_descr (&mem_loc_result,
13185                          int_loc_descriptor (DWARF2_ADDR_SIZE - shift - size));
13186           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13187           add_loc_descr (&mem_loc_result,
13188                          int_loc_descriptor (DWARF2_ADDR_SIZE - size));
13189           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13190         }
13191       break;
13192
13193     case COMPARE:
13194     case IF_THEN_ELSE:
13195     case ROTATE:
13196     case ROTATERT:
13197     case TRUNCATE:
13198       /* In theory, we could implement the above.  */
13199       /* DWARF cannot represent the unsigned compare operations
13200          natively.  */
13201     case SS_MULT:
13202     case US_MULT:
13203     case SS_DIV:
13204     case US_DIV:
13205     case UDIV:
13206     case UMOD:
13207     case UNORDERED:
13208     case ORDERED:
13209     case UNEQ:
13210     case UNGE:
13211     case UNLE:
13212     case UNLT:
13213     case LTGT:
13214     case FLOAT_EXTEND:
13215     case FLOAT_TRUNCATE:
13216     case FLOAT:
13217     case UNSIGNED_FLOAT:
13218     case FIX:
13219     case UNSIGNED_FIX:
13220     case FRACT_CONVERT:
13221     case UNSIGNED_FRACT_CONVERT:
13222     case SAT_FRACT:
13223     case UNSIGNED_SAT_FRACT:
13224     case SQRT:
13225     case BSWAP:
13226     case FFS:
13227     case CLZ:
13228     case CTZ:
13229     case POPCOUNT:
13230     case PARITY:
13231     case ASM_OPERANDS:
13232     case UNSPEC:
13233     case HIGH:
13234       /* If delegitimize_address couldn't do anything with the UNSPEC, we
13235          can't express it in the debug info.  This can happen e.g. with some
13236          TLS UNSPECs.  */
13237       break;
13238
13239     case CONST_STRING:
13240       resolve_one_addr (&rtl, NULL);
13241       goto symref;
13242
13243     default:
13244 #ifdef ENABLE_CHECKING
13245       print_rtl (stderr, rtl);
13246       gcc_unreachable ();
13247 #else
13248       break;
13249 #endif
13250     }
13251
13252   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13253     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13254
13255   return mem_loc_result;
13256 }
13257
13258 /* Return a descriptor that describes the concatenation of two locations.
13259    This is typically a complex variable.  */
13260
13261 static dw_loc_descr_ref
13262 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
13263 {
13264   dw_loc_descr_ref cc_loc_result = NULL;
13265   dw_loc_descr_ref x0_ref
13266     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13267   dw_loc_descr_ref x1_ref
13268     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13269
13270   if (x0_ref == 0 || x1_ref == 0)
13271     return 0;
13272
13273   cc_loc_result = x0_ref;
13274   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
13275
13276   add_loc_descr (&cc_loc_result, x1_ref);
13277   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
13278
13279   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13280     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13281
13282   return cc_loc_result;
13283 }
13284
13285 /* Return a descriptor that describes the concatenation of N
13286    locations.  */
13287
13288 static dw_loc_descr_ref
13289 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
13290 {
13291   unsigned int i;
13292   dw_loc_descr_ref cc_loc_result = NULL;
13293   unsigned int n = XVECLEN (concatn, 0);
13294
13295   for (i = 0; i < n; ++i)
13296     {
13297       dw_loc_descr_ref ref;
13298       rtx x = XVECEXP (concatn, 0, i);
13299
13300       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
13301       if (ref == NULL)
13302         return NULL;
13303
13304       add_loc_descr (&cc_loc_result, ref);
13305       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
13306     }
13307
13308   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13309     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13310
13311   return cc_loc_result;
13312 }
13313
13314 /* Output a proper Dwarf location descriptor for a variable or parameter
13315    which is either allocated in a register or in a memory location.  For a
13316    register, we just generate an OP_REG and the register number.  For a
13317    memory location we provide a Dwarf postfix expression describing how to
13318    generate the (dynamic) address of the object onto the address stack.
13319
13320    MODE is mode of the decl if this loc_descriptor is going to be used in
13321    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
13322    allowed, VOIDmode otherwise.
13323
13324    If we don't know how to describe it, return 0.  */
13325
13326 static dw_loc_descr_ref
13327 loc_descriptor (rtx rtl, enum machine_mode mode,
13328                 enum var_init_status initialized)
13329 {
13330   dw_loc_descr_ref loc_result = NULL;
13331
13332   switch (GET_CODE (rtl))
13333     {
13334     case SUBREG:
13335       /* The case of a subreg may arise when we have a local (register)
13336          variable or a formal (register) parameter which doesn't quite fill
13337          up an entire register.  For now, just assume that it is
13338          legitimate to make the Dwarf info refer to the whole register which
13339          contains the given subreg.  */
13340       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
13341       break;
13342
13343     case REG:
13344       loc_result = reg_loc_descriptor (rtl, initialized);
13345       break;
13346
13347     case SIGN_EXTEND:
13348     case ZERO_EXTEND:
13349       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13350       break;
13351
13352     case MEM:
13353       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13354                                        initialized);
13355       if (loc_result == NULL)
13356         loc_result = tls_mem_loc_descriptor (rtl);
13357       break;
13358
13359     case CONCAT:
13360       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
13361                                           initialized);
13362       break;
13363
13364     case CONCATN:
13365       loc_result = concatn_loc_descriptor (rtl, initialized);
13366       break;
13367
13368     case VAR_LOCATION:
13369       /* Single part.  */
13370       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
13371         {
13372           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), mode,
13373                                        initialized);
13374           break;
13375         }
13376
13377       rtl = XEXP (rtl, 1);
13378       /* FALLTHRU */
13379
13380     case PARALLEL:
13381       {
13382         rtvec par_elems = XVEC (rtl, 0);
13383         int num_elem = GET_NUM_ELEM (par_elems);
13384         enum machine_mode mode;
13385         int i;
13386
13387         /* Create the first one, so we have something to add to.  */
13388         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
13389                                      VOIDmode, initialized);
13390         if (loc_result == NULL)
13391           return NULL;
13392         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
13393         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13394         for (i = 1; i < num_elem; i++)
13395           {
13396             dw_loc_descr_ref temp;
13397
13398             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
13399                                    VOIDmode, initialized);
13400             if (temp == NULL)
13401               return NULL;
13402             add_loc_descr (&loc_result, temp);
13403             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
13404             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
13405           }
13406       }
13407       break;
13408
13409     case CONST_INT:
13410       if (mode != VOIDmode && mode != BLKmode)
13411         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
13412                                                     INTVAL (rtl));
13413       break;
13414
13415     case CONST_DOUBLE:
13416       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13417         {
13418           /* Note that a CONST_DOUBLE rtx could represent either an integer
13419              or a floating-point constant.  A CONST_DOUBLE is used whenever
13420              the constant requires more than one word in order to be
13421              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13422           if (GET_MODE (rtl) != VOIDmode)
13423             mode = GET_MODE (rtl);
13424
13425           loc_result = new_loc_descr (DW_OP_implicit_value,
13426                                       GET_MODE_SIZE (mode), 0);
13427           if (SCALAR_FLOAT_MODE_P (mode))
13428             {
13429               unsigned int length = GET_MODE_SIZE (mode);
13430               unsigned char *array = GGC_NEWVEC (unsigned char, length);
13431
13432               insert_float (rtl, array);
13433               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13434               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13435               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13436               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13437             }
13438           else
13439             {
13440               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13441               loc_result->dw_loc_oprnd2.v.val_double.high
13442                 = CONST_DOUBLE_HIGH (rtl);
13443               loc_result->dw_loc_oprnd2.v.val_double.low
13444                 = CONST_DOUBLE_LOW (rtl);
13445             }
13446         }
13447       break;
13448
13449     case CONST_VECTOR:
13450       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13451         {
13452           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13453           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13454           unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
13455           unsigned int i;
13456           unsigned char *p;
13457
13458           mode = GET_MODE (rtl);
13459           switch (GET_MODE_CLASS (mode))
13460             {
13461             case MODE_VECTOR_INT:
13462               for (i = 0, p = array; i < length; i++, p += elt_size)
13463                 {
13464                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13465                   HOST_WIDE_INT lo, hi;
13466
13467                   switch (GET_CODE (elt))
13468                     {
13469                     case CONST_INT:
13470                       lo = INTVAL (elt);
13471                       hi = -(lo < 0);
13472                       break;
13473
13474                     case CONST_DOUBLE:
13475                       lo = CONST_DOUBLE_LOW (elt);
13476                       hi = CONST_DOUBLE_HIGH (elt);
13477                       break;
13478
13479                     default:
13480                       gcc_unreachable ();
13481                     }
13482
13483                   if (elt_size <= sizeof (HOST_WIDE_INT))
13484                     insert_int (lo, elt_size, p);
13485                   else
13486                     {
13487                       unsigned char *p0 = p;
13488                       unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
13489
13490                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13491                       if (WORDS_BIG_ENDIAN)
13492                         {
13493                           p0 = p1;
13494                           p1 = p;
13495                         }
13496                       insert_int (lo, sizeof (HOST_WIDE_INT), p0);
13497                       insert_int (hi, sizeof (HOST_WIDE_INT), p1);
13498                     }
13499                 }
13500               break;
13501
13502             case MODE_VECTOR_FLOAT:
13503               for (i = 0, p = array; i < length; i++, p += elt_size)
13504                 {
13505                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13506                   insert_float (elt, p);
13507                 }
13508               break;
13509
13510             default:
13511               gcc_unreachable ();
13512             }
13513
13514           loc_result = new_loc_descr (DW_OP_implicit_value,
13515                                       length * elt_size, 0);
13516           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13517           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13518           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13519           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13520         }
13521       break;
13522
13523     case CONST:
13524       if (mode == VOIDmode
13525           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
13526           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
13527           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13528         {
13529           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13530           break;
13531         }
13532       /* FALLTHROUGH */
13533     case SYMBOL_REF:
13534       if (!const_ok_for_output (rtl))
13535         break;
13536     case LABEL_REF:
13537       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13538           && (dwarf_version >= 4 || !dwarf_strict))
13539         {
13540           loc_result = new_loc_descr (DW_OP_implicit_value,
13541                                       DWARF2_ADDR_SIZE, 0);
13542           loc_result->dw_loc_oprnd2.val_class = dw_val_class_addr;
13543           loc_result->dw_loc_oprnd2.v.val_addr = rtl;
13544           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13545         }
13546       break;
13547
13548     default:
13549       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13550           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13551           && (dwarf_version >= 4 || !dwarf_strict))
13552         {
13553           /* Value expression.  */
13554           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13555           if (loc_result)
13556             add_loc_descr (&loc_result,
13557                            new_loc_descr (DW_OP_stack_value, 0, 0));
13558         }
13559       break;
13560     }
13561
13562   return loc_result;
13563 }
13564
13565 /* We need to figure out what section we should use as the base for the
13566    address ranges where a given location is valid.
13567    1. If this particular DECL has a section associated with it, use that.
13568    2. If this function has a section associated with it, use that.
13569    3. Otherwise, use the text section.
13570    XXX: If you split a variable across multiple sections, we won't notice.  */
13571
13572 static const char *
13573 secname_for_decl (const_tree decl)
13574 {
13575   const char *secname;
13576
13577   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13578     {
13579       tree sectree = DECL_SECTION_NAME (decl);
13580       secname = TREE_STRING_POINTER (sectree);
13581     }
13582   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13583     {
13584       tree sectree = DECL_SECTION_NAME (current_function_decl);
13585       secname = TREE_STRING_POINTER (sectree);
13586     }
13587   else if (cfun && in_cold_section_p)
13588     secname = crtl->subsections.cold_section_label;
13589   else
13590     secname = text_section_label;
13591
13592   return secname;
13593 }
13594
13595 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13596
13597 static bool
13598 decl_by_reference_p (tree decl)
13599 {
13600   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13601            || TREE_CODE (decl) == VAR_DECL)
13602           && DECL_BY_REFERENCE (decl));
13603 }
13604
13605 /* Return single element location list containing loc descr REF.  */
13606
13607 static dw_loc_list_ref
13608 single_element_loc_list (dw_loc_descr_ref ref)
13609 {
13610   return new_loc_list (ref, NULL, NULL, NULL, 0);
13611 }
13612
13613 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13614    for VARLOC.  */
13615
13616 static dw_loc_descr_ref
13617 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13618                enum var_init_status initialized)
13619 {
13620   int have_address = 0;
13621   dw_loc_descr_ref descr;
13622   enum machine_mode mode;
13623
13624   if (want_address != 2)
13625     {
13626       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13627       /* Single part.  */
13628       if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
13629         {
13630           varloc = XEXP (XEXP (varloc, 1), 0);
13631           mode = GET_MODE (varloc);
13632           if (MEM_P (varloc))
13633             {
13634               varloc = XEXP (varloc, 0);
13635               have_address = 1;
13636             }
13637           descr = mem_loc_descriptor (varloc, mode, initialized);
13638         }
13639       else
13640         return 0;
13641     }
13642   else
13643     {
13644       descr = loc_descriptor (varloc, DECL_MODE (loc), initialized);
13645       have_address = 1;
13646     }
13647
13648   if (!descr)
13649     return 0;
13650
13651   if (want_address == 2 && !have_address
13652       && (dwarf_version >= 4 || !dwarf_strict))
13653     {
13654       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13655         {
13656           expansion_failed (loc, NULL_RTX,
13657                             "DWARF address size mismatch");
13658           return 0;
13659         }
13660       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13661       have_address = 1;
13662     }
13663   /* Show if we can't fill the request for an address.  */
13664   if (want_address && !have_address)
13665     {
13666       expansion_failed (loc, NULL_RTX,
13667                         "Want address and only have value");
13668       return 0;
13669     }
13670
13671   /* If we've got an address and don't want one, dereference.  */
13672   if (!want_address && have_address)
13673     {
13674       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13675       enum dwarf_location_atom op;
13676
13677       if (size > DWARF2_ADDR_SIZE || size == -1)
13678         {
13679           expansion_failed (loc, NULL_RTX,
13680                             "DWARF address size mismatch");
13681           return 0;
13682         }
13683       else if (size == DWARF2_ADDR_SIZE)
13684         op = DW_OP_deref;
13685       else
13686         op = DW_OP_deref_size;
13687
13688       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13689     }
13690
13691   return descr;
13692 }
13693
13694 /* Return dwarf representation of location list representing for
13695    LOC_LIST of DECL.  WANT_ADDRESS has the same meaning as in
13696    loc_list_from_tree function.  */
13697
13698 static dw_loc_list_ref
13699 dw_loc_list (var_loc_list * loc_list, tree decl, int want_address)
13700 {
13701   const char *endname, *secname;
13702   dw_loc_list_ref list;
13703   rtx varloc;
13704   enum var_init_status initialized;
13705   struct var_loc_node *node;
13706   dw_loc_descr_ref descr;
13707   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13708
13709   /* Now that we know what section we are using for a base,
13710      actually construct the list of locations.
13711      The first location information is what is passed to the
13712      function that creates the location list, and the remaining
13713      locations just get added on to that list.
13714      Note that we only know the start address for a location
13715      (IE location changes), so to build the range, we use
13716      the range [current location start, next location start].
13717      This means we have to special case the last node, and generate
13718      a range of [last location start, end of function label].  */
13719
13720   node = loc_list->first;
13721   secname = secname_for_decl (decl);
13722
13723   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
13724     initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13725   else
13726     initialized = VAR_INIT_STATUS_INITIALIZED;
13727   varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13728   descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13729
13730   if (loc_list && loc_list->first != loc_list->last)
13731     list = new_loc_list (descr, node->label, node->next->label, secname, 1);
13732   else
13733     return single_element_loc_list (descr);
13734   node = node->next;
13735
13736   if (!node)
13737     return NULL;
13738
13739   for (; node->next; node = node->next)
13740     if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13741       {
13742         /* The variable has a location between NODE->LABEL and
13743            NODE->NEXT->LABEL.  */
13744         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13745         varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13746         descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13747         add_loc_descr_to_loc_list (&list, descr,
13748                                    node->label, node->next->label, secname);
13749       }
13750
13751   /* If the variable has a location at the last label
13752      it keeps its location until the end of function.  */
13753   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13754     {
13755       if (!current_function_decl)
13756         endname = text_end_label;
13757       else
13758         {
13759           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13760                                        current_function_funcdef_no);
13761           endname = ggc_strdup (label_id);
13762         }
13763
13764       initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13765       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13766       descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13767       add_loc_descr_to_loc_list (&list, descr, node->label, endname, secname);
13768     }
13769   return list;
13770 }
13771
13772 /* Return if the loc_list has only single element and thus can be represented
13773    as location description.   */
13774
13775 static bool
13776 single_element_loc_list_p (dw_loc_list_ref list)
13777 {
13778   return (!list->dw_loc_next && !list->begin && !list->end);
13779 }
13780
13781 /* To each location in list LIST add loc descr REF.  */
13782
13783 static void
13784 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13785 {
13786   dw_loc_descr_ref copy;
13787   add_loc_descr (&list->expr, ref);
13788   list = list->dw_loc_next;
13789   while (list)
13790     {
13791       copy = GGC_CNEW (dw_loc_descr_node);
13792       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13793       add_loc_descr (&list->expr, copy);
13794       while (copy->dw_loc_next)
13795         {
13796           dw_loc_descr_ref new_copy = GGC_CNEW (dw_loc_descr_node);
13797           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13798           copy->dw_loc_next = new_copy;
13799           copy = new_copy;
13800         }
13801       list = list->dw_loc_next;
13802     }
13803 }
13804
13805 /* Given two lists RET and LIST
13806    produce location list that is result of adding expression in LIST
13807    to expression in RET on each possition in program.
13808    Might be destructive on both RET and LIST.
13809
13810    TODO: We handle only simple cases of RET or LIST having at most one
13811    element. General case would inolve sorting the lists in program order
13812    and merging them that will need some additional work.  
13813    Adding that will improve quality of debug info especially for SRA-ed
13814    structures.  */
13815
13816 static void
13817 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13818 {
13819   if (!list)
13820     return;
13821   if (!*ret)
13822     {
13823       *ret = list;
13824       return;
13825     }
13826   if (!list->dw_loc_next)
13827     {
13828       add_loc_descr_to_each (*ret, list->expr);
13829       return;
13830     }
13831   if (!(*ret)->dw_loc_next)
13832     {
13833       add_loc_descr_to_each (list, (*ret)->expr);
13834       *ret = list;
13835       return;
13836     }
13837   expansion_failed (NULL_TREE, NULL_RTX,
13838                     "Don't know how to merge two non-trivial"
13839                     " location lists.\n");
13840   *ret = NULL;
13841   return;
13842 }
13843
13844 /* LOC is constant expression.  Try a luck, look it up in constant
13845    pool and return its loc_descr of its address.  */
13846
13847 static dw_loc_descr_ref
13848 cst_pool_loc_descr (tree loc)
13849 {
13850   /* Get an RTL for this, if something has been emitted.  */
13851   rtx rtl = lookup_constant_def (loc);
13852   enum machine_mode mode;
13853
13854   if (!rtl || !MEM_P (rtl))
13855     {
13856       gcc_assert (!rtl);
13857       return 0;
13858     }
13859   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13860
13861   /* TODO: We might get more coverage if we was actually delaying expansion
13862      of all expressions till end of compilation when constant pools are fully
13863      populated.  */
13864   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13865     {
13866       expansion_failed (loc, NULL_RTX,
13867                         "CST value in contant pool but not marked.");
13868       return 0;
13869     }
13870   mode = GET_MODE (rtl);
13871   rtl = XEXP (rtl, 0);
13872   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
13873 }
13874
13875 /* Return dw_loc_list representing address of addr_expr LOC
13876    by looking for innder INDIRECT_REF expression and turing it
13877    into simple arithmetics.  */
13878
13879 static dw_loc_list_ref
13880 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13881 {
13882   tree obj, offset;
13883   HOST_WIDE_INT bitsize, bitpos, bytepos;
13884   enum machine_mode mode;
13885   int volatilep;
13886   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13887   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13888
13889   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13890                              &bitsize, &bitpos, &offset, &mode,
13891                              &unsignedp, &volatilep, false);
13892   STRIP_NOPS (obj);
13893   if (bitpos % BITS_PER_UNIT)
13894     {
13895       expansion_failed (loc, NULL_RTX, "bitfield access");
13896       return 0;
13897     }
13898   if (!INDIRECT_REF_P (obj))
13899     {
13900       expansion_failed (obj,
13901                         NULL_RTX, "no indirect ref in inner refrence");
13902       return 0;
13903     }
13904   if (!offset && !bitpos)
13905     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13906   else if (toplev
13907            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13908            && (dwarf_version >= 4 || !dwarf_strict))
13909     {
13910       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13911       if (!list_ret)
13912         return 0;
13913       if (offset)
13914         {
13915           /* Variable offset.  */
13916           list_ret1 = loc_list_from_tree (offset, 0);
13917           if (list_ret1 == 0)
13918             return 0;
13919           add_loc_list (&list_ret, list_ret1);
13920           if (!list_ret)
13921             return 0;
13922           add_loc_descr_to_each (list_ret,
13923                                  new_loc_descr (DW_OP_plus, 0, 0));
13924         }
13925       bytepos = bitpos / BITS_PER_UNIT;
13926       if (bytepos > 0)
13927         add_loc_descr_to_each (list_ret,
13928                                new_loc_descr (DW_OP_plus_uconst,
13929                                               bytepos, 0));
13930       else if (bytepos < 0)
13931         loc_list_plus_const (list_ret, bytepos);
13932       add_loc_descr_to_each (list_ret,
13933                              new_loc_descr (DW_OP_stack_value, 0, 0));
13934     }
13935   return list_ret;
13936 }
13937
13938
13939 /* Generate Dwarf location list representing LOC.
13940    If WANT_ADDRESS is false, expression computing LOC will be computed
13941    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13942    if WANT_ADDRESS is 2, expression computing address useable in location
13943      will be returned (i.e. DW_OP_reg can be used
13944      to refer to register values).  */
13945
13946 static dw_loc_list_ref
13947 loc_list_from_tree (tree loc, int want_address)
13948 {
13949   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13950   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13951   int have_address = 0;
13952   enum dwarf_location_atom op;
13953
13954   /* ??? Most of the time we do not take proper care for sign/zero
13955      extending the values properly.  Hopefully this won't be a real
13956      problem...  */
13957
13958   switch (TREE_CODE (loc))
13959     {
13960     case ERROR_MARK:
13961       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13962       return 0;
13963
13964     case PLACEHOLDER_EXPR:
13965       /* This case involves extracting fields from an object to determine the
13966          position of other fields.  We don't try to encode this here.  The
13967          only user of this is Ada, which encodes the needed information using
13968          the names of types.  */
13969       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13970       return 0;
13971
13972     case CALL_EXPR:
13973       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13974       /* There are no opcodes for these operations.  */
13975       return 0;
13976
13977     case PREINCREMENT_EXPR:
13978     case PREDECREMENT_EXPR:
13979     case POSTINCREMENT_EXPR:
13980     case POSTDECREMENT_EXPR:
13981       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13982       /* There are no opcodes for these operations.  */
13983       return 0;
13984
13985     case ADDR_EXPR:
13986       /* If we already want an address, see if there is INDIRECT_REF inside
13987          e.g. for &this->field.  */
13988       if (want_address)
13989         {
13990           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13991                        (loc, want_address == 2);
13992           if (list_ret)
13993             have_address = 1;
13994           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13995                    && (ret = cst_pool_loc_descr (loc)))
13996             have_address = 1;
13997         }
13998         /* Otherwise, process the argument and look for the address.  */
13999       if (!list_ret && !ret)
14000         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
14001       else
14002         {
14003           if (want_address)
14004             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
14005           return NULL;
14006         }
14007       break;
14008
14009     case VAR_DECL:
14010       if (DECL_THREAD_LOCAL_P (loc))
14011         {
14012           rtx rtl;
14013           enum dwarf_location_atom first_op;
14014           enum dwarf_location_atom second_op;
14015           bool dtprel = false;
14016
14017           if (targetm.have_tls)
14018             {
14019               /* If this is not defined, we have no way to emit the
14020                  data.  */
14021               if (!targetm.asm_out.output_dwarf_dtprel)
14022                 return 0;
14023
14024                /* The way DW_OP_GNU_push_tls_address is specified, we
14025                   can only look up addresses of objects in the current
14026                   module.  */
14027               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
14028                 return 0;
14029               first_op = DW_OP_addr;
14030               dtprel = true;
14031               second_op = DW_OP_GNU_push_tls_address;
14032             }
14033           else
14034             {
14035               if (!targetm.emutls.debug_form_tls_address
14036                   || !(dwarf_version >= 3 || !dwarf_strict))
14037                 return 0;
14038               loc = emutls_decl (loc);
14039               first_op = DW_OP_addr;
14040               second_op = DW_OP_form_tls_address;
14041             }
14042
14043           rtl = rtl_for_decl_location (loc);
14044           if (rtl == NULL_RTX)
14045             return 0;
14046
14047           if (!MEM_P (rtl))
14048             return 0;
14049           rtl = XEXP (rtl, 0);
14050           if (! CONSTANT_P (rtl))
14051             return 0;
14052
14053           ret = new_loc_descr (first_op, 0, 0);
14054           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
14055           ret->dw_loc_oprnd1.v.val_addr = rtl;
14056           ret->dtprel = dtprel;
14057
14058           ret1 = new_loc_descr (second_op, 0, 0);
14059           add_loc_descr (&ret, ret1);
14060
14061           have_address = 1;
14062           break;
14063         }
14064       /* FALLTHRU */
14065
14066     case PARM_DECL:
14067       if (DECL_HAS_VALUE_EXPR_P (loc))
14068         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
14069                                    want_address);
14070       /* FALLTHRU */
14071
14072     case RESULT_DECL:
14073     case FUNCTION_DECL:
14074       {
14075         rtx rtl = rtl_for_decl_location (loc);
14076         var_loc_list *loc_list = lookup_decl_loc (loc);
14077
14078         if (loc_list && loc_list->first
14079             && (list_ret = dw_loc_list (loc_list, loc, want_address)))
14080           have_address = want_address != 0;
14081         else if (rtl == NULL_RTX)
14082           {
14083             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
14084             return 0;
14085           }
14086         else if (CONST_INT_P (rtl))
14087           {
14088             HOST_WIDE_INT val = INTVAL (rtl);
14089             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14090               val &= GET_MODE_MASK (DECL_MODE (loc));
14091             ret = int_loc_descriptor (val);
14092           }
14093         else if (GET_CODE (rtl) == CONST_STRING)
14094           {
14095             expansion_failed (loc, NULL_RTX, "CONST_STRING");
14096             return 0;
14097           }
14098         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
14099           {
14100             ret = new_loc_descr (DW_OP_addr, 0, 0);
14101             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
14102             ret->dw_loc_oprnd1.v.val_addr = rtl;
14103           }
14104         else
14105           {
14106             enum machine_mode mode;
14107
14108             /* Certain constructs can only be represented at top-level.  */
14109             if (want_address == 2)
14110               {
14111                 ret = loc_descriptor (rtl, VOIDmode,
14112                                       VAR_INIT_STATUS_INITIALIZED);
14113                 have_address = 1;
14114               }
14115             else
14116               {
14117                 mode = GET_MODE (rtl);
14118                 if (MEM_P (rtl))
14119                   {
14120                     rtl = XEXP (rtl, 0);
14121                     have_address = 1;
14122                   }
14123                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14124               }
14125             if (!ret)
14126               expansion_failed (loc, rtl,
14127                                 "failed to produce loc descriptor for rtl");
14128           }
14129       }
14130       break;
14131
14132     case INDIRECT_REF:
14133     case ALIGN_INDIRECT_REF:
14134     case MISALIGNED_INDIRECT_REF:
14135       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14136       have_address = 1;
14137       break;
14138
14139     case COMPOUND_EXPR:
14140       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
14141
14142     CASE_CONVERT:
14143     case VIEW_CONVERT_EXPR:
14144     case SAVE_EXPR:
14145     case MODIFY_EXPR:
14146       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
14147
14148     case COMPONENT_REF:
14149     case BIT_FIELD_REF:
14150     case ARRAY_REF:
14151     case ARRAY_RANGE_REF:
14152     case REALPART_EXPR:
14153     case IMAGPART_EXPR:
14154       {
14155         tree obj, offset;
14156         HOST_WIDE_INT bitsize, bitpos, bytepos;
14157         enum machine_mode mode;
14158         int volatilep;
14159         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14160
14161         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
14162                                    &unsignedp, &volatilep, false);
14163
14164         gcc_assert (obj != loc);
14165
14166         list_ret = loc_list_from_tree (obj,
14167                                        want_address == 2
14168                                        && !bitpos && !offset ? 2 : 1);
14169         /* TODO: We can extract value of the small expression via shifting even
14170            for nonzero bitpos.  */
14171         if (list_ret == 0)
14172           return 0;
14173         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
14174           {
14175             expansion_failed (loc, NULL_RTX,
14176                               "bitfield access");
14177             return 0;
14178           }
14179
14180         if (offset != NULL_TREE)
14181           {
14182             /* Variable offset.  */
14183             list_ret1 = loc_list_from_tree (offset, 0);
14184             if (list_ret1 == 0)
14185               return 0;
14186             add_loc_list (&list_ret, list_ret1);
14187             if (!list_ret)
14188               return 0;
14189             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
14190           }
14191
14192         bytepos = bitpos / BITS_PER_UNIT;
14193         if (bytepos > 0)
14194           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
14195         else if (bytepos < 0)
14196           loc_list_plus_const (list_ret, bytepos); 
14197
14198         have_address = 1;
14199         break;
14200       }
14201
14202     case INTEGER_CST:
14203       if ((want_address || !host_integerp (loc, 0))
14204           && (ret = cst_pool_loc_descr (loc)))
14205         have_address = 1;
14206       else if (want_address == 2
14207                && host_integerp (loc, 0)
14208                && (ret = address_of_int_loc_descriptor
14209                            (int_size_in_bytes (TREE_TYPE (loc)),
14210                             tree_low_cst (loc, 0))))
14211         have_address = 1;
14212       else if (host_integerp (loc, 0))
14213         ret = int_loc_descriptor (tree_low_cst (loc, 0));
14214       else
14215         {
14216           expansion_failed (loc, NULL_RTX,
14217                             "Integer operand is not host integer");
14218           return 0;
14219         }
14220       break;
14221
14222     case CONSTRUCTOR:
14223     case REAL_CST:
14224     case STRING_CST:
14225     case COMPLEX_CST:
14226       if ((ret = cst_pool_loc_descr (loc)))
14227         have_address = 1;
14228       else
14229       /* We can construct small constants here using int_loc_descriptor.  */
14230         expansion_failed (loc, NULL_RTX,
14231                           "constructor or constant not in constant pool");
14232       break;
14233
14234     case TRUTH_AND_EXPR:
14235     case TRUTH_ANDIF_EXPR:
14236     case BIT_AND_EXPR:
14237       op = DW_OP_and;
14238       goto do_binop;
14239
14240     case TRUTH_XOR_EXPR:
14241     case BIT_XOR_EXPR:
14242       op = DW_OP_xor;
14243       goto do_binop;
14244
14245     case TRUTH_OR_EXPR:
14246     case TRUTH_ORIF_EXPR:
14247     case BIT_IOR_EXPR:
14248       op = DW_OP_or;
14249       goto do_binop;
14250
14251     case FLOOR_DIV_EXPR:
14252     case CEIL_DIV_EXPR:
14253     case ROUND_DIV_EXPR:
14254     case TRUNC_DIV_EXPR:
14255       op = DW_OP_div;
14256       goto do_binop;
14257
14258     case MINUS_EXPR:
14259       op = DW_OP_minus;
14260       goto do_binop;
14261
14262     case FLOOR_MOD_EXPR:
14263     case CEIL_MOD_EXPR:
14264     case ROUND_MOD_EXPR:
14265     case TRUNC_MOD_EXPR:
14266       op = DW_OP_mod;
14267       goto do_binop;
14268
14269     case MULT_EXPR:
14270       op = DW_OP_mul;
14271       goto do_binop;
14272
14273     case LSHIFT_EXPR:
14274       op = DW_OP_shl;
14275       goto do_binop;
14276
14277     case RSHIFT_EXPR:
14278       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14279       goto do_binop;
14280
14281     case POINTER_PLUS_EXPR:
14282     case PLUS_EXPR:
14283       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
14284           && host_integerp (TREE_OPERAND (loc, 1), 0))
14285         {
14286           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14287           if (list_ret == 0)
14288             return 0;
14289
14290           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14291           break;
14292         }
14293
14294       op = DW_OP_plus;
14295       goto do_binop;
14296
14297     case LE_EXPR:
14298       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14299         return 0;
14300
14301       op = DW_OP_le;
14302       goto do_binop;
14303
14304     case GE_EXPR:
14305       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14306         return 0;
14307
14308       op = DW_OP_ge;
14309       goto do_binop;
14310
14311     case LT_EXPR:
14312       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14313         return 0;
14314
14315       op = DW_OP_lt;
14316       goto do_binop;
14317
14318     case GT_EXPR:
14319       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14320         return 0;
14321
14322       op = DW_OP_gt;
14323       goto do_binop;
14324
14325     case EQ_EXPR:
14326       op = DW_OP_eq;
14327       goto do_binop;
14328
14329     case NE_EXPR:
14330       op = DW_OP_ne;
14331       goto do_binop;
14332
14333     do_binop:
14334       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14335       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14336       if (list_ret == 0 || list_ret1 == 0)
14337         return 0;
14338
14339       add_loc_list (&list_ret, list_ret1);
14340       if (list_ret == 0)
14341         return 0;
14342       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14343       break;
14344
14345     case TRUTH_NOT_EXPR:
14346     case BIT_NOT_EXPR:
14347       op = DW_OP_not;
14348       goto do_unop;
14349
14350     case ABS_EXPR:
14351       op = DW_OP_abs;
14352       goto do_unop;
14353
14354     case NEGATE_EXPR:
14355       op = DW_OP_neg;
14356       goto do_unop;
14357
14358     do_unop:
14359       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14360       if (list_ret == 0)
14361         return 0;
14362
14363       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14364       break;
14365
14366     case MIN_EXPR:
14367     case MAX_EXPR:
14368       {
14369         const enum tree_code code =
14370           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14371
14372         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14373                       build2 (code, integer_type_node,
14374                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14375                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14376       }
14377
14378       /* ... fall through ...  */
14379
14380     case COND_EXPR:
14381       {
14382         dw_loc_descr_ref lhs
14383           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14384         dw_loc_list_ref rhs
14385           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14386         dw_loc_descr_ref bra_node, jump_node, tmp;
14387
14388         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14389         if (list_ret == 0 || lhs == 0 || rhs == 0)
14390           return 0;
14391
14392         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14393         add_loc_descr_to_each (list_ret, bra_node);
14394
14395         add_loc_list (&list_ret, rhs);
14396         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14397         add_loc_descr_to_each (list_ret, jump_node);
14398
14399         add_loc_descr_to_each (list_ret, lhs);
14400         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14401         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14402
14403         /* ??? Need a node to point the skip at.  Use a nop.  */
14404         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14405         add_loc_descr_to_each (list_ret, tmp);
14406         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14407         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14408       }
14409       break;
14410
14411     case FIX_TRUNC_EXPR:
14412       return 0;
14413
14414     default:
14415       /* Leave front-end specific codes as simply unknown.  This comes
14416          up, for instance, with the C STMT_EXPR.  */
14417       if ((unsigned int) TREE_CODE (loc)
14418           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14419         {
14420           expansion_failed (loc, NULL_RTX,
14421                             "language specific tree node");
14422           return 0;
14423         }
14424
14425 #ifdef ENABLE_CHECKING
14426       /* Otherwise this is a generic code; we should just lists all of
14427          these explicitly.  We forgot one.  */
14428       gcc_unreachable ();
14429 #else
14430       /* In a release build, we want to degrade gracefully: better to
14431          generate incomplete debugging information than to crash.  */
14432       return NULL;
14433 #endif
14434     }
14435
14436   if (!ret && !list_ret)
14437     return 0;
14438
14439   if (want_address == 2 && !have_address
14440       && (dwarf_version >= 4 || !dwarf_strict))
14441     {
14442       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14443         {
14444           expansion_failed (loc, NULL_RTX,
14445                             "DWARF address size mismatch");
14446           return 0;
14447         }
14448       if (ret)
14449         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14450       else
14451         add_loc_descr_to_each (list_ret,
14452                                new_loc_descr (DW_OP_stack_value, 0, 0));
14453       have_address = 1;
14454     }
14455   /* Show if we can't fill the request for an address.  */
14456   if (want_address && !have_address)
14457     {
14458       expansion_failed (loc, NULL_RTX,
14459                         "Want address and only have value");
14460       return 0;
14461     }
14462
14463   gcc_assert (!ret || !list_ret);
14464
14465   /* If we've got an address and don't want one, dereference.  */
14466   if (!want_address && have_address)
14467     {
14468       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14469
14470       if (size > DWARF2_ADDR_SIZE || size == -1)
14471         {
14472           expansion_failed (loc, NULL_RTX,
14473                             "DWARF address size mismatch");
14474           return 0;
14475         }
14476       else if (size == DWARF2_ADDR_SIZE)
14477         op = DW_OP_deref;
14478       else
14479         op = DW_OP_deref_size;
14480
14481       if (ret)
14482         add_loc_descr (&ret, new_loc_descr (op, size, 0));
14483       else
14484         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14485     }
14486   if (ret)
14487     list_ret = single_element_loc_list (ret);
14488
14489   return list_ret;
14490 }
14491
14492 /* Same as above but return only single location expression.  */
14493 static dw_loc_descr_ref
14494 loc_descriptor_from_tree (tree loc, int want_address)
14495 {
14496   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14497   if (!ret)
14498     return NULL;
14499   if (ret->dw_loc_next)
14500     {
14501       expansion_failed (loc, NULL_RTX,
14502                         "Location list where only loc descriptor needed");
14503       return NULL;
14504     }
14505   return ret->expr;
14506 }
14507
14508 /* Given a value, round it up to the lowest multiple of `boundary'
14509    which is not less than the value itself.  */
14510
14511 static inline HOST_WIDE_INT
14512 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14513 {
14514   return (((value + boundary - 1) / boundary) * boundary);
14515 }
14516
14517 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14518    pointer to the declared type for the relevant field variable, or return
14519    `integer_type_node' if the given node turns out to be an
14520    ERROR_MARK node.  */
14521
14522 static inline tree
14523 field_type (const_tree decl)
14524 {
14525   tree type;
14526
14527   if (TREE_CODE (decl) == ERROR_MARK)
14528     return integer_type_node;
14529
14530   type = DECL_BIT_FIELD_TYPE (decl);
14531   if (type == NULL_TREE)
14532     type = TREE_TYPE (decl);
14533
14534   return type;
14535 }
14536
14537 /* Given a pointer to a tree node, return the alignment in bits for
14538    it, or else return BITS_PER_WORD if the node actually turns out to
14539    be an ERROR_MARK node.  */
14540
14541 static inline unsigned
14542 simple_type_align_in_bits (const_tree type)
14543 {
14544   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14545 }
14546
14547 static inline unsigned
14548 simple_decl_align_in_bits (const_tree decl)
14549 {
14550   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14551 }
14552
14553 /* Return the result of rounding T up to ALIGN.  */
14554
14555 static inline HOST_WIDE_INT
14556 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
14557 {
14558   /* We must be careful if T is negative because HOST_WIDE_INT can be
14559      either "above" or "below" unsigned int as per the C promotion
14560      rules, depending on the host, thus making the signedness of the
14561      direct multiplication and division unpredictable.  */
14562   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
14563
14564   u += align - 1;
14565   u /= align;
14566   u *= align;
14567
14568   return (HOST_WIDE_INT) u;
14569 }
14570
14571 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14572    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14573    or return 0 if we are unable to determine what that offset is, either
14574    because the argument turns out to be a pointer to an ERROR_MARK node, or
14575    because the offset is actually variable.  (We can't handle the latter case
14576    just yet).  */
14577
14578 static HOST_WIDE_INT
14579 field_byte_offset (const_tree decl)
14580 {
14581   HOST_WIDE_INT object_offset_in_bits;
14582   HOST_WIDE_INT bitpos_int;
14583
14584   if (TREE_CODE (decl) == ERROR_MARK)
14585     return 0;
14586
14587   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14588
14589   /* We cannot yet cope with fields whose positions are variable, so
14590      for now, when we see such things, we simply return 0.  Someday, we may
14591      be able to handle such cases, but it will be damn difficult.  */
14592   if (! host_integerp (bit_position (decl), 0))
14593     return 0;
14594
14595   bitpos_int = int_bit_position (decl);
14596
14597 #ifdef PCC_BITFIELD_TYPE_MATTERS
14598   if (PCC_BITFIELD_TYPE_MATTERS)
14599     {
14600       tree type;
14601       tree field_size_tree;
14602       HOST_WIDE_INT deepest_bitpos;
14603       unsigned HOST_WIDE_INT field_size_in_bits;
14604       unsigned int type_align_in_bits;
14605       unsigned int decl_align_in_bits;
14606       unsigned HOST_WIDE_INT type_size_in_bits;
14607
14608       type = field_type (decl);
14609       type_size_in_bits = simple_type_size_in_bits (type);
14610       type_align_in_bits = simple_type_align_in_bits (type);
14611
14612       field_size_tree = DECL_SIZE (decl);
14613
14614       /* The size could be unspecified if there was an error, or for
14615          a flexible array member.  */
14616       if (!field_size_tree)
14617         field_size_tree = bitsize_zero_node;
14618
14619       /* If the size of the field is not constant, use the type size.  */
14620       if (host_integerp (field_size_tree, 1))
14621         field_size_in_bits = tree_low_cst (field_size_tree, 1);
14622       else
14623         field_size_in_bits = type_size_in_bits;
14624
14625       decl_align_in_bits = simple_decl_align_in_bits (decl);
14626
14627       /* The GCC front-end doesn't make any attempt to keep track of the
14628          starting bit offset (relative to the start of the containing
14629          structure type) of the hypothetical "containing object" for a
14630          bit-field.  Thus, when computing the byte offset value for the
14631          start of the "containing object" of a bit-field, we must deduce
14632          this information on our own. This can be rather tricky to do in
14633          some cases.  For example, handling the following structure type
14634          definition when compiling for an i386/i486 target (which only
14635          aligns long long's to 32-bit boundaries) can be very tricky:
14636
14637          struct S { int field1; long long field2:31; };
14638
14639          Fortunately, there is a simple rule-of-thumb which can be used
14640          in such cases.  When compiling for an i386/i486, GCC will
14641          allocate 8 bytes for the structure shown above.  It decides to
14642          do this based upon one simple rule for bit-field allocation.
14643          GCC allocates each "containing object" for each bit-field at
14644          the first (i.e. lowest addressed) legitimate alignment boundary
14645          (based upon the required minimum alignment for the declared
14646          type of the field) which it can possibly use, subject to the
14647          condition that there is still enough available space remaining
14648          in the containing object (when allocated at the selected point)
14649          to fully accommodate all of the bits of the bit-field itself.
14650
14651          This simple rule makes it obvious why GCC allocates 8 bytes for
14652          each object of the structure type shown above.  When looking
14653          for a place to allocate the "containing object" for `field2',
14654          the compiler simply tries to allocate a 64-bit "containing
14655          object" at each successive 32-bit boundary (starting at zero)
14656          until it finds a place to allocate that 64- bit field such that
14657          at least 31 contiguous (and previously unallocated) bits remain
14658          within that selected 64 bit field.  (As it turns out, for the
14659          example above, the compiler finds it is OK to allocate the
14660          "containing object" 64-bit field at bit-offset zero within the
14661          structure type.)
14662
14663          Here we attempt to work backwards from the limited set of facts
14664          we're given, and we try to deduce from those facts, where GCC
14665          must have believed that the containing object started (within
14666          the structure type). The value we deduce is then used (by the
14667          callers of this routine) to generate DW_AT_location and
14668          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14669          the case of DW_AT_location, regular fields as well).  */
14670
14671       /* Figure out the bit-distance from the start of the structure to
14672          the "deepest" bit of the bit-field.  */
14673       deepest_bitpos = bitpos_int + field_size_in_bits;
14674
14675       /* This is the tricky part.  Use some fancy footwork to deduce
14676          where the lowest addressed bit of the containing object must
14677          be.  */
14678       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14679
14680       /* Round up to type_align by default.  This works best for
14681          bitfields.  */
14682       object_offset_in_bits
14683         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14684
14685       if (object_offset_in_bits > bitpos_int)
14686         {
14687           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14688
14689           /* Round up to decl_align instead.  */
14690           object_offset_in_bits
14691             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14692         }
14693     }
14694   else
14695 #endif
14696     object_offset_in_bits = bitpos_int;
14697
14698   return object_offset_in_bits / BITS_PER_UNIT;
14699 }
14700 \f
14701 /* The following routines define various Dwarf attributes and any data
14702    associated with them.  */
14703
14704 /* Add a location description attribute value to a DIE.
14705
14706    This emits location attributes suitable for whole variables and
14707    whole parameters.  Note that the location attributes for struct fields are
14708    generated by the routine `data_member_location_attribute' below.  */
14709
14710 static inline void
14711 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14712                              dw_loc_list_ref descr)
14713 {
14714   if (descr == 0)
14715     return;
14716   if (single_element_loc_list_p (descr))
14717     add_AT_loc (die, attr_kind, descr->expr);
14718   else
14719     add_AT_loc_list (die, attr_kind, descr);
14720 }
14721
14722 /* Attach the specialized form of location attribute used for data members of
14723    struct and union types.  In the special case of a FIELD_DECL node which
14724    represents a bit-field, the "offset" part of this special location
14725    descriptor must indicate the distance in bytes from the lowest-addressed
14726    byte of the containing struct or union type to the lowest-addressed byte of
14727    the "containing object" for the bit-field.  (See the `field_byte_offset'
14728    function above).
14729
14730    For any given bit-field, the "containing object" is a hypothetical object
14731    (of some integral or enum type) within which the given bit-field lives.  The
14732    type of this hypothetical "containing object" is always the same as the
14733    declared type of the individual bit-field itself (for GCC anyway... the
14734    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14735    bytes) of the hypothetical "containing object" which will be given in the
14736    DW_AT_byte_size attribute for this bit-field.  (See the
14737    `byte_size_attribute' function below.)  It is also used when calculating the
14738    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14739    function below.)  */
14740
14741 static void
14742 add_data_member_location_attribute (dw_die_ref die, tree decl)
14743 {
14744   HOST_WIDE_INT offset;
14745   dw_loc_descr_ref loc_descr = 0;
14746
14747   if (TREE_CODE (decl) == TREE_BINFO)
14748     {
14749       /* We're working on the TAG_inheritance for a base class.  */
14750       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14751         {
14752           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14753              aren't at a fixed offset from all (sub)objects of the same
14754              type.  We need to extract the appropriate offset from our
14755              vtable.  The following dwarf expression means
14756
14757                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14758
14759              This is specific to the V3 ABI, of course.  */
14760
14761           dw_loc_descr_ref tmp;
14762
14763           /* Make a copy of the object address.  */
14764           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14765           add_loc_descr (&loc_descr, tmp);
14766
14767           /* Extract the vtable address.  */
14768           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14769           add_loc_descr (&loc_descr, tmp);
14770
14771           /* Calculate the address of the offset.  */
14772           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14773           gcc_assert (offset < 0);
14774
14775           tmp = int_loc_descriptor (-offset);
14776           add_loc_descr (&loc_descr, tmp);
14777           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14778           add_loc_descr (&loc_descr, tmp);
14779
14780           /* Extract the offset.  */
14781           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14782           add_loc_descr (&loc_descr, tmp);
14783
14784           /* Add it to the object address.  */
14785           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14786           add_loc_descr (&loc_descr, tmp);
14787         }
14788       else
14789         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14790     }
14791   else
14792     offset = field_byte_offset (decl);
14793
14794   if (! loc_descr)
14795     {
14796       if (dwarf_version > 2)
14797         {
14798           /* Don't need to output a location expression, just the constant. */
14799           add_AT_int (die, DW_AT_data_member_location, offset);
14800           return;
14801         }
14802       else
14803         {
14804           enum dwarf_location_atom op;
14805           
14806           /* The DWARF2 standard says that we should assume that the structure
14807              address is already on the stack, so we can specify a structure
14808              field address by using DW_OP_plus_uconst.  */
14809           
14810 #ifdef MIPS_DEBUGGING_INFO
14811           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14812              operator correctly.  It works only if we leave the offset on the
14813              stack.  */
14814           op = DW_OP_constu;
14815 #else
14816           op = DW_OP_plus_uconst;
14817 #endif
14818           
14819           loc_descr = new_loc_descr (op, offset, 0);
14820         }
14821     }
14822
14823   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14824 }
14825
14826 /* Writes integer values to dw_vec_const array.  */
14827
14828 static void
14829 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14830 {
14831   while (size != 0)
14832     {
14833       *dest++ = val & 0xff;
14834       val >>= 8;
14835       --size;
14836     }
14837 }
14838
14839 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14840
14841 static HOST_WIDE_INT
14842 extract_int (const unsigned char *src, unsigned int size)
14843 {
14844   HOST_WIDE_INT val = 0;
14845
14846   src += size;
14847   while (size != 0)
14848     {
14849       val <<= 8;
14850       val |= *--src & 0xff;
14851       --size;
14852     }
14853   return val;
14854 }
14855
14856 /* Writes floating point values to dw_vec_const array.  */
14857
14858 static void
14859 insert_float (const_rtx rtl, unsigned char *array)
14860 {
14861   REAL_VALUE_TYPE rv;
14862   long val[4];
14863   int i;
14864
14865   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14866   real_to_target (val, &rv, GET_MODE (rtl));
14867
14868   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14869   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14870     {
14871       insert_int (val[i], 4, array);
14872       array += 4;
14873     }
14874 }
14875
14876 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14877    does not have a "location" either in memory or in a register.  These
14878    things can arise in GNU C when a constant is passed as an actual parameter
14879    to an inlined function.  They can also arise in C++ where declared
14880    constants do not necessarily get memory "homes".  */
14881
14882 static bool
14883 add_const_value_attribute (dw_die_ref die, rtx rtl)
14884 {
14885   switch (GET_CODE (rtl))
14886     {
14887     case CONST_INT:
14888       {
14889         HOST_WIDE_INT val = INTVAL (rtl);
14890
14891         if (val < 0)
14892           add_AT_int (die, DW_AT_const_value, val);
14893         else
14894           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14895       }
14896       return true;
14897
14898     case CONST_DOUBLE:
14899       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14900          floating-point constant.  A CONST_DOUBLE is used whenever the
14901          constant requires more than one word in order to be adequately
14902          represented.  */
14903       {
14904         enum machine_mode mode = GET_MODE (rtl);
14905
14906         if (SCALAR_FLOAT_MODE_P (mode))
14907           {
14908             unsigned int length = GET_MODE_SIZE (mode);
14909             unsigned char *array = GGC_NEWVEC (unsigned char, length);
14910
14911             insert_float (rtl, array);
14912             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14913           }
14914         else
14915           add_AT_double (die, DW_AT_const_value,
14916                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14917       }
14918       return true;
14919
14920     case CONST_VECTOR:
14921       {
14922         enum machine_mode mode = GET_MODE (rtl);
14923         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14924         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14925         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
14926         unsigned int i;
14927         unsigned char *p;
14928
14929         switch (GET_MODE_CLASS (mode))
14930           {
14931           case MODE_VECTOR_INT:
14932             for (i = 0, p = array; i < length; i++, p += elt_size)
14933               {
14934                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14935                 HOST_WIDE_INT lo, hi;
14936
14937                 switch (GET_CODE (elt))
14938                   {
14939                   case CONST_INT:
14940                     lo = INTVAL (elt);
14941                     hi = -(lo < 0);
14942                     break;
14943
14944                   case CONST_DOUBLE:
14945                     lo = CONST_DOUBLE_LOW (elt);
14946                     hi = CONST_DOUBLE_HIGH (elt);
14947                     break;
14948
14949                   default:
14950                     gcc_unreachable ();
14951                   }
14952
14953                 if (elt_size <= sizeof (HOST_WIDE_INT))
14954                   insert_int (lo, elt_size, p);
14955                 else
14956                   {
14957                     unsigned char *p0 = p;
14958                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
14959
14960                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14961                     if (WORDS_BIG_ENDIAN)
14962                       {
14963                         p0 = p1;
14964                         p1 = p;
14965                       }
14966                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
14967                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
14968                   }
14969               }
14970             break;
14971
14972           case MODE_VECTOR_FLOAT:
14973             for (i = 0, p = array; i < length; i++, p += elt_size)
14974               {
14975                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14976                 insert_float (elt, p);
14977               }
14978             break;
14979
14980           default:
14981             gcc_unreachable ();
14982           }
14983
14984         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14985       }
14986       return true;
14987
14988     case CONST_STRING:
14989       resolve_one_addr (&rtl, NULL);
14990       add_AT_addr (die, DW_AT_const_value, rtl);
14991       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14992       return true;
14993
14994     case CONST:
14995       if (CONSTANT_P (XEXP (rtl, 0)))
14996         return add_const_value_attribute (die, XEXP (rtl, 0));
14997       /* FALLTHROUGH */
14998     case SYMBOL_REF:
14999       if (!const_ok_for_output (rtl))
15000         return false;
15001     case LABEL_REF:
15002       add_AT_addr (die, DW_AT_const_value, rtl);
15003       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15004       return true;
15005
15006     case PLUS:
15007       /* In cases where an inlined instance of an inline function is passed
15008          the address of an `auto' variable (which is local to the caller) we
15009          can get a situation where the DECL_RTL of the artificial local
15010          variable (for the inlining) which acts as a stand-in for the
15011          corresponding formal parameter (of the inline function) will look
15012          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
15013          exactly a compile-time constant expression, but it isn't the address
15014          of the (artificial) local variable either.  Rather, it represents the
15015          *value* which the artificial local variable always has during its
15016          lifetime.  We currently have no way to represent such quasi-constant
15017          values in Dwarf, so for now we just punt and generate nothing.  */
15018       return false;
15019
15020     case HIGH:
15021     case CONST_FIXED:
15022       return false;
15023
15024     case MEM:
15025       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
15026           && MEM_READONLY_P (rtl)
15027           && GET_MODE (rtl) == BLKmode)
15028         {
15029           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
15030           return true;
15031         }
15032       return false;
15033
15034     default:
15035       /* No other kinds of rtx should be possible here.  */
15036       gcc_unreachable ();
15037     }
15038   return false;
15039 }
15040
15041 /* Determine whether the evaluation of EXPR references any variables
15042    or functions which aren't otherwise used (and therefore may not be
15043    output).  */
15044 static tree
15045 reference_to_unused (tree * tp, int * walk_subtrees,
15046                      void * data ATTRIBUTE_UNUSED)
15047 {
15048   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
15049     *walk_subtrees = 0;
15050
15051   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
15052       && ! TREE_ASM_WRITTEN (*tp))
15053     return *tp;
15054   /* ???  The C++ FE emits debug information for using decls, so
15055      putting gcc_unreachable here falls over.  See PR31899.  For now
15056      be conservative.  */
15057   else if (!cgraph_global_info_ready
15058            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
15059     return *tp;
15060   else if (TREE_CODE (*tp) == VAR_DECL)
15061     {
15062       struct varpool_node *node = varpool_node (*tp);
15063       if (!node->needed)
15064         return *tp;
15065     }
15066   else if (TREE_CODE (*tp) == FUNCTION_DECL
15067            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
15068     {
15069       /* The call graph machinery must have finished analyzing,
15070          optimizing and gimplifying the CU by now.
15071          So if *TP has no call graph node associated
15072          to it, it means *TP will not be emitted.  */
15073       if (!cgraph_get_node (*tp))
15074         return *tp;
15075     }
15076   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
15077     return *tp;
15078
15079   return NULL_TREE;
15080 }
15081
15082 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
15083    for use in a later add_const_value_attribute call.  */
15084
15085 static rtx
15086 rtl_for_decl_init (tree init, tree type)
15087 {
15088   rtx rtl = NULL_RTX;
15089
15090   /* If a variable is initialized with a string constant without embedded
15091      zeros, build CONST_STRING.  */
15092   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
15093     {
15094       tree enttype = TREE_TYPE (type);
15095       tree domain = TYPE_DOMAIN (type);
15096       enum machine_mode mode = TYPE_MODE (enttype);
15097
15098       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
15099           && domain
15100           && integer_zerop (TYPE_MIN_VALUE (domain))
15101           && compare_tree_int (TYPE_MAX_VALUE (domain),
15102                                TREE_STRING_LENGTH (init) - 1) == 0
15103           && ((size_t) TREE_STRING_LENGTH (init)
15104               == strlen (TREE_STRING_POINTER (init)) + 1))
15105         {
15106           rtl = gen_rtx_CONST_STRING (VOIDmode,
15107                                       ggc_strdup (TREE_STRING_POINTER (init)));
15108           rtl = gen_rtx_MEM (BLKmode, rtl);
15109           MEM_READONLY_P (rtl) = 1;
15110         }
15111     }
15112   /* Other aggregates, and complex values, could be represented using
15113      CONCAT: FIXME!  */
15114   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
15115     ;
15116   /* Vectors only work if their mode is supported by the target.
15117      FIXME: generic vectors ought to work too.  */
15118   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
15119     ;
15120   /* If the initializer is something that we know will expand into an
15121      immediate RTL constant, expand it now.  We must be careful not to
15122      reference variables which won't be output.  */
15123   else if (initializer_constant_valid_p (init, type)
15124            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
15125     {
15126       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
15127          possible.  */
15128       if (TREE_CODE (type) == VECTOR_TYPE)
15129         switch (TREE_CODE (init))
15130           {
15131           case VECTOR_CST:
15132             break;
15133           case CONSTRUCTOR:
15134             if (TREE_CONSTANT (init))
15135               {
15136                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
15137                 bool constant_p = true;
15138                 tree value;
15139                 unsigned HOST_WIDE_INT ix;
15140
15141                 /* Even when ctor is constant, it might contain non-*_CST
15142                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15143                    belong into VECTOR_CST nodes.  */
15144                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15145                   if (!CONSTANT_CLASS_P (value))
15146                     {
15147                       constant_p = false;
15148                       break;
15149                     }
15150
15151                 if (constant_p)
15152                   {
15153                     init = build_vector_from_ctor (type, elts);
15154                     break;
15155                   }
15156               }
15157             /* FALLTHRU */
15158
15159           default:
15160             return NULL;
15161           }
15162
15163       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15164
15165       /* If expand_expr returns a MEM, it wasn't immediate.  */
15166       gcc_assert (!rtl || !MEM_P (rtl));
15167     }
15168
15169   return rtl;
15170 }
15171
15172 /* Generate RTL for the variable DECL to represent its location.  */
15173
15174 static rtx
15175 rtl_for_decl_location (tree decl)
15176 {
15177   rtx rtl;
15178
15179   /* Here we have to decide where we are going to say the parameter "lives"
15180      (as far as the debugger is concerned).  We only have a couple of
15181      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15182
15183      DECL_RTL normally indicates where the parameter lives during most of the
15184      activation of the function.  If optimization is enabled however, this
15185      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15186      that the parameter doesn't really live anywhere (as far as the code
15187      generation parts of GCC are concerned) during most of the function's
15188      activation.  That will happen (for example) if the parameter is never
15189      referenced within the function.
15190
15191      We could just generate a location descriptor here for all non-NULL
15192      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15193      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15194      where DECL_RTL is NULL or is a pseudo-reg.
15195
15196      Note however that we can only get away with using DECL_INCOMING_RTL as
15197      a backup substitute for DECL_RTL in certain limited cases.  In cases
15198      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15199      we can be sure that the parameter was passed using the same type as it is
15200      declared to have within the function, and that its DECL_INCOMING_RTL
15201      points us to a place where a value of that type is passed.
15202
15203      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15204      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15205      because in these cases DECL_INCOMING_RTL points us to a value of some
15206      type which is *different* from the type of the parameter itself.  Thus,
15207      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15208      such cases, the debugger would end up (for example) trying to fetch a
15209      `float' from a place which actually contains the first part of a
15210      `double'.  That would lead to really incorrect and confusing
15211      output at debug-time.
15212
15213      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15214      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15215      are a couple of exceptions however.  On little-endian machines we can
15216      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15217      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15218      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15219      when (on a little-endian machine) a non-prototyped function has a
15220      parameter declared to be of type `short' or `char'.  In such cases,
15221      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15222      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15223      passed `int' value.  If the debugger then uses that address to fetch
15224      a `short' or a `char' (on a little-endian machine) the result will be
15225      the correct data, so we allow for such exceptional cases below.
15226
15227      Note that our goal here is to describe the place where the given formal
15228      parameter lives during most of the function's activation (i.e. between the
15229      end of the prologue and the start of the epilogue).  We'll do that as best
15230      as we can. Note however that if the given formal parameter is modified
15231      sometime during the execution of the function, then a stack backtrace (at
15232      debug-time) will show the function as having been called with the *new*
15233      value rather than the value which was originally passed in.  This happens
15234      rarely enough that it is not a major problem, but it *is* a problem, and
15235      I'd like to fix it.
15236
15237      A future version of dwarf2out.c may generate two additional attributes for
15238      any given DW_TAG_formal_parameter DIE which will describe the "passed
15239      type" and the "passed location" for the given formal parameter in addition
15240      to the attributes we now generate to indicate the "declared type" and the
15241      "active location" for each parameter.  This additional set of attributes
15242      could be used by debuggers for stack backtraces. Separately, note that
15243      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15244      This happens (for example) for inlined-instances of inline function formal
15245      parameters which are never referenced.  This really shouldn't be
15246      happening.  All PARM_DECL nodes should get valid non-NULL
15247      DECL_INCOMING_RTL values.  FIXME.  */
15248
15249   /* Use DECL_RTL as the "location" unless we find something better.  */
15250   rtl = DECL_RTL_IF_SET (decl);
15251
15252   /* When generating abstract instances, ignore everything except
15253      constants, symbols living in memory, and symbols living in
15254      fixed registers.  */
15255   if (! reload_completed)
15256     {
15257       if (rtl
15258           && (CONSTANT_P (rtl)
15259               || (MEM_P (rtl)
15260                   && CONSTANT_P (XEXP (rtl, 0)))
15261               || (REG_P (rtl)
15262                   && TREE_CODE (decl) == VAR_DECL
15263                   && TREE_STATIC (decl))))
15264         {
15265           rtl = targetm.delegitimize_address (rtl);
15266           return rtl;
15267         }
15268       rtl = NULL_RTX;
15269     }
15270   else if (TREE_CODE (decl) == PARM_DECL)
15271     {
15272       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
15273         {
15274           tree declared_type = TREE_TYPE (decl);
15275           tree passed_type = DECL_ARG_TYPE (decl);
15276           enum machine_mode dmode = TYPE_MODE (declared_type);
15277           enum machine_mode pmode = TYPE_MODE (passed_type);
15278
15279           /* This decl represents a formal parameter which was optimized out.
15280              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15281              all cases where (rtl == NULL_RTX) just below.  */
15282           if (dmode == pmode)
15283             rtl = DECL_INCOMING_RTL (decl);
15284           else if (SCALAR_INT_MODE_P (dmode)
15285                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15286                    && DECL_INCOMING_RTL (decl))
15287             {
15288               rtx inc = DECL_INCOMING_RTL (decl);
15289               if (REG_P (inc))
15290                 rtl = inc;
15291               else if (MEM_P (inc))
15292                 {
15293                   if (BYTES_BIG_ENDIAN)
15294                     rtl = adjust_address_nv (inc, dmode,
15295                                              GET_MODE_SIZE (pmode)
15296                                              - GET_MODE_SIZE (dmode));
15297                   else
15298                     rtl = inc;
15299                 }
15300             }
15301         }
15302
15303       /* If the parm was passed in registers, but lives on the stack, then
15304          make a big endian correction if the mode of the type of the
15305          parameter is not the same as the mode of the rtl.  */
15306       /* ??? This is the same series of checks that are made in dbxout.c before
15307          we reach the big endian correction code there.  It isn't clear if all
15308          of these checks are necessary here, but keeping them all is the safe
15309          thing to do.  */
15310       else if (MEM_P (rtl)
15311                && XEXP (rtl, 0) != const0_rtx
15312                && ! CONSTANT_P (XEXP (rtl, 0))
15313                /* Not passed in memory.  */
15314                && !MEM_P (DECL_INCOMING_RTL (decl))
15315                /* Not passed by invisible reference.  */
15316                && (!REG_P (XEXP (rtl, 0))
15317                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15318                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15319 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
15320                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15321 #endif
15322                      )
15323                /* Big endian correction check.  */
15324                && BYTES_BIG_ENDIAN
15325                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15326                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15327                    < UNITS_PER_WORD))
15328         {
15329           int offset = (UNITS_PER_WORD
15330                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15331
15332           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15333                              plus_constant (XEXP (rtl, 0), offset));
15334         }
15335     }
15336   else if (TREE_CODE (decl) == VAR_DECL
15337            && rtl
15338            && MEM_P (rtl)
15339            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15340            && BYTES_BIG_ENDIAN)
15341     {
15342       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15343       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15344
15345       /* If a variable is declared "register" yet is smaller than
15346          a register, then if we store the variable to memory, it
15347          looks like we're storing a register-sized value, when in
15348          fact we are not.  We need to adjust the offset of the
15349          storage location to reflect the actual value's bytes,
15350          else gdb will not be able to display it.  */
15351       if (rsize > dsize)
15352         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15353                            plus_constant (XEXP (rtl, 0), rsize-dsize));
15354     }
15355
15356   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15357      and will have been substituted directly into all expressions that use it.
15358      C does not have such a concept, but C++ and other languages do.  */
15359   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15360     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15361
15362   if (rtl)
15363     rtl = targetm.delegitimize_address (rtl);
15364
15365   /* If we don't look past the constant pool, we risk emitting a
15366      reference to a constant pool entry that isn't referenced from
15367      code, and thus is not emitted.  */
15368   if (rtl)
15369     rtl = avoid_constant_pool_reference (rtl);
15370
15371   return rtl;
15372 }
15373
15374 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15375    returned.  If so, the decl for the COMMON block is returned, and the
15376    value is the offset into the common block for the symbol.  */
15377
15378 static tree
15379 fortran_common (tree decl, HOST_WIDE_INT *value)
15380 {
15381   tree val_expr, cvar;
15382   enum machine_mode mode;
15383   HOST_WIDE_INT bitsize, bitpos;
15384   tree offset;
15385   int volatilep = 0, unsignedp = 0;
15386
15387   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
15388      it does not have a value (the offset into the common area), or if it
15389      is thread local (as opposed to global) then it isn't common, and shouldn't
15390      be handled as such.  */
15391   if (TREE_CODE (decl) != VAR_DECL
15392       || !TREE_PUBLIC (decl)
15393       || !TREE_STATIC (decl)
15394       || !DECL_HAS_VALUE_EXPR_P (decl)
15395       || !is_fortran ())
15396     return NULL_TREE;
15397
15398   val_expr = DECL_VALUE_EXPR (decl);
15399   if (TREE_CODE (val_expr) != COMPONENT_REF)
15400     return NULL_TREE;
15401
15402   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15403                               &mode, &unsignedp, &volatilep, true);
15404
15405   if (cvar == NULL_TREE
15406       || TREE_CODE (cvar) != VAR_DECL
15407       || DECL_ARTIFICIAL (cvar)
15408       || !TREE_PUBLIC (cvar))
15409     return NULL_TREE;
15410
15411   *value = 0;
15412   if (offset != NULL)
15413     {
15414       if (!host_integerp (offset, 0))
15415         return NULL_TREE;
15416       *value = tree_low_cst (offset, 0);
15417     }
15418   if (bitpos != 0)
15419     *value += bitpos / BITS_PER_UNIT;
15420
15421   return cvar;
15422 }
15423
15424 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15425    data attribute for a variable or a parameter.  We generate the
15426    DW_AT_const_value attribute only in those cases where the given variable
15427    or parameter does not have a true "location" either in memory or in a
15428    register.  This can happen (for example) when a constant is passed as an
15429    actual argument in a call to an inline function.  (It's possible that
15430    these things can crop up in other ways also.)  Note that one type of
15431    constant value which can be passed into an inlined function is a constant
15432    pointer.  This can happen for example if an actual argument in an inlined
15433    function call evaluates to a compile-time constant address.  */
15434
15435 static bool
15436 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
15437                                        enum dwarf_attribute attr)
15438 {
15439   rtx rtl;
15440   dw_loc_list_ref list;
15441   var_loc_list *loc_list;
15442
15443   if (TREE_CODE (decl) == ERROR_MARK)
15444     return false;
15445
15446   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15447               || TREE_CODE (decl) == RESULT_DECL);
15448
15449   /* Try to get some constant RTL for this decl, and use that as the value of
15450      the location.  */
15451
15452   rtl = rtl_for_decl_location (decl);
15453   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15454       && add_const_value_attribute (die, rtl))
15455     return true;
15456
15457   /* See if we have single element location list that is equivalent to
15458      a constant value.  That way we are better to use add_const_value_attribute
15459      rather than expanding constant value equivalent.  */
15460   loc_list = lookup_decl_loc (decl);
15461   if (loc_list && loc_list->first && loc_list->first == loc_list->last)
15462     {
15463       enum var_init_status status;
15464       struct var_loc_node *node;
15465
15466       node = loc_list->first;
15467       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
15468       rtl = NOTE_VAR_LOCATION (node->var_loc_note);
15469       if (GET_CODE (rtl) == VAR_LOCATION
15470           && GET_CODE (XEXP (rtl, 1)) != PARALLEL)
15471         rtl = XEXP (XEXP (rtl, 1), 0);
15472       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15473           && add_const_value_attribute (die, rtl))
15474          return true;
15475     }
15476   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15477   if (list)
15478     {
15479       add_AT_location_description (die, attr, list);
15480       return true;
15481     }
15482   /* None of that worked, so it must not really have a location;
15483      try adding a constant value attribute from the DECL_INITIAL.  */
15484   return tree_add_const_value_attribute_for_decl (die, decl);
15485 }
15486
15487 /* Add VARIABLE and DIE into deferred locations list.  */
15488
15489 static void
15490 defer_location (tree variable, dw_die_ref die)
15491 {
15492   deferred_locations entry;
15493   entry.variable = variable;
15494   entry.die = die;
15495   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15496 }
15497
15498 /* Helper function for tree_add_const_value_attribute.  Natively encode
15499    initializer INIT into an array.  Return true if successful.  */
15500
15501 static bool
15502 native_encode_initializer (tree init, unsigned char *array, int size)
15503 {
15504   tree type;
15505
15506   if (init == NULL_TREE)
15507     return false;
15508
15509   STRIP_NOPS (init);
15510   switch (TREE_CODE (init))
15511     {
15512     case STRING_CST:
15513       type = TREE_TYPE (init);
15514       if (TREE_CODE (type) == ARRAY_TYPE)
15515         {
15516           tree enttype = TREE_TYPE (type);
15517           enum machine_mode mode = TYPE_MODE (enttype);
15518
15519           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15520             return false;
15521           if (int_size_in_bytes (type) != size)
15522             return false;
15523           if (size > TREE_STRING_LENGTH (init))
15524             {
15525               memcpy (array, TREE_STRING_POINTER (init),
15526                       TREE_STRING_LENGTH (init));
15527               memset (array + TREE_STRING_LENGTH (init),
15528                       '\0', size - TREE_STRING_LENGTH (init));
15529             }
15530           else
15531             memcpy (array, TREE_STRING_POINTER (init), size);
15532           return true;
15533         }
15534       return false;
15535     case CONSTRUCTOR:
15536       type = TREE_TYPE (init);
15537       if (int_size_in_bytes (type) != size)
15538         return false;
15539       if (TREE_CODE (type) == ARRAY_TYPE)
15540         {
15541           HOST_WIDE_INT min_index;
15542           unsigned HOST_WIDE_INT cnt;
15543           int curpos = 0, fieldsize;
15544           constructor_elt *ce;
15545
15546           if (TYPE_DOMAIN (type) == NULL_TREE
15547               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15548             return false;
15549
15550           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15551           if (fieldsize <= 0)
15552             return false;
15553
15554           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15555           memset (array, '\0', size);
15556           for (cnt = 0;
15557                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
15558                cnt++)
15559             {
15560               tree val = ce->value;
15561               tree index = ce->index;
15562               int pos = curpos;
15563               if (index && TREE_CODE (index) == RANGE_EXPR)
15564                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15565                       * fieldsize;
15566               else if (index)
15567                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15568
15569               if (val)
15570                 {
15571                   STRIP_NOPS (val);
15572                   if (!native_encode_initializer (val, array + pos, fieldsize))
15573                     return false;
15574                 }
15575               curpos = pos + fieldsize;
15576               if (index && TREE_CODE (index) == RANGE_EXPR)
15577                 {
15578                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15579                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15580                   while (count > 0)
15581                     {
15582                       if (val)
15583                         memcpy (array + curpos, array + pos, fieldsize);
15584                       curpos += fieldsize;
15585                     }
15586                 }
15587               gcc_assert (curpos <= size);
15588             }
15589           return true;
15590         }
15591       else if (TREE_CODE (type) == RECORD_TYPE
15592                || TREE_CODE (type) == UNION_TYPE)
15593         {
15594           tree field = NULL_TREE;
15595           unsigned HOST_WIDE_INT cnt;
15596           constructor_elt *ce;
15597
15598           if (int_size_in_bytes (type) != size)
15599             return false;
15600
15601           if (TREE_CODE (type) == RECORD_TYPE)
15602             field = TYPE_FIELDS (type);
15603
15604           for (cnt = 0;
15605                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
15606                cnt++, field = field ? TREE_CHAIN (field) : 0)
15607             {
15608               tree val = ce->value;
15609               int pos, fieldsize;
15610
15611               if (ce->index != 0)
15612                 field = ce->index;
15613
15614               if (val)
15615                 STRIP_NOPS (val);
15616
15617               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15618                 return false;
15619
15620               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15621                   && TYPE_DOMAIN (TREE_TYPE (field))
15622                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15623                 return false;
15624               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15625                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15626                 return false;
15627               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15628               pos = int_byte_position (field);
15629               gcc_assert (pos + fieldsize <= size);
15630               if (val
15631                   && !native_encode_initializer (val, array + pos, fieldsize))
15632                 return false;
15633             }
15634           return true;
15635         }
15636       return false;
15637     case VIEW_CONVERT_EXPR:
15638     case NON_LVALUE_EXPR:
15639       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15640     default:
15641       return native_encode_expr (init, array, size) == size;
15642     }
15643 }
15644
15645 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15646    attribute is the const value T.  */
15647
15648 static bool
15649 tree_add_const_value_attribute (dw_die_ref die, tree t)
15650 {
15651   tree init;
15652   tree type = TREE_TYPE (t);
15653   rtx rtl;
15654
15655   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15656     return false;
15657
15658   init = t;
15659   gcc_assert (!DECL_P (init));
15660
15661   rtl = rtl_for_decl_init (init, type);
15662   if (rtl)
15663     return add_const_value_attribute (die, rtl);
15664   /* If the host and target are sane, try harder.  */
15665   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15666            && initializer_constant_valid_p (init, type))
15667     {
15668       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15669       if (size > 0 && (int) size == size)
15670         {
15671           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
15672
15673           if (native_encode_initializer (init, array, size))
15674             {
15675               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15676               return true;
15677             }
15678         }
15679     }
15680   return false;
15681 }
15682
15683 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15684    attribute is the const value of T, where T is an integral constant
15685    variable with static storage duration
15686    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15687
15688 static bool
15689 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15690 {
15691
15692   if (!decl
15693       || (TREE_CODE (decl) != VAR_DECL
15694           && TREE_CODE (decl) != CONST_DECL))
15695     return false;
15696
15697     if (TREE_READONLY (decl)
15698         && ! TREE_THIS_VOLATILE (decl)
15699         && DECL_INITIAL (decl))
15700       /* OK */;
15701     else
15702       return false;
15703
15704   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15705 }
15706
15707 /* Convert the CFI instructions for the current function into a
15708    location list.  This is used for DW_AT_frame_base when we targeting
15709    a dwarf2 consumer that does not support the dwarf3
15710    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15711    expressions.  */
15712
15713 static dw_loc_list_ref
15714 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15715 {
15716   dw_fde_ref fde;
15717   dw_loc_list_ref list, *list_tail;
15718   dw_cfi_ref cfi;
15719   dw_cfa_location last_cfa, next_cfa;
15720   const char *start_label, *last_label, *section;
15721   dw_cfa_location remember;
15722
15723   fde = current_fde ();
15724   gcc_assert (fde != NULL);
15725
15726   section = secname_for_decl (current_function_decl);
15727   list_tail = &list;
15728   list = NULL;
15729
15730   memset (&next_cfa, 0, sizeof (next_cfa));
15731   next_cfa.reg = INVALID_REGNUM;
15732   remember = next_cfa;
15733
15734   start_label = fde->dw_fde_begin;
15735
15736   /* ??? Bald assumption that the CIE opcode list does not contain
15737      advance opcodes.  */
15738   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
15739     lookup_cfa_1 (cfi, &next_cfa, &remember);
15740
15741   last_cfa = next_cfa;
15742   last_label = start_label;
15743
15744   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
15745     switch (cfi->dw_cfi_opc)
15746       {
15747       case DW_CFA_set_loc:
15748       case DW_CFA_advance_loc1:
15749       case DW_CFA_advance_loc2:
15750       case DW_CFA_advance_loc4:
15751         if (!cfa_equal_p (&last_cfa, &next_cfa))
15752           {
15753             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15754                                        start_label, last_label, section,
15755                                        list == NULL);
15756
15757             list_tail = &(*list_tail)->dw_loc_next;
15758             last_cfa = next_cfa;
15759             start_label = last_label;
15760           }
15761         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15762         break;
15763
15764       case DW_CFA_advance_loc:
15765         /* The encoding is complex enough that we should never emit this.  */
15766         gcc_unreachable ();
15767
15768       default:
15769         lookup_cfa_1 (cfi, &next_cfa, &remember);
15770         break;
15771       }
15772
15773   if (!cfa_equal_p (&last_cfa, &next_cfa))
15774     {
15775       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15776                                  start_label, last_label, section,
15777                                  list == NULL);
15778       list_tail = &(*list_tail)->dw_loc_next;
15779       start_label = last_label;
15780     }
15781   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15782                              start_label, fde->dw_fde_end, section,
15783                              list == NULL);
15784
15785   return list;
15786 }
15787
15788 /* Compute a displacement from the "steady-state frame pointer" to the
15789    frame base (often the same as the CFA), and store it in
15790    frame_pointer_fb_offset.  OFFSET is added to the displacement
15791    before the latter is negated.  */
15792
15793 static void
15794 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15795 {
15796   rtx reg, elim;
15797
15798 #ifdef FRAME_POINTER_CFA_OFFSET
15799   reg = frame_pointer_rtx;
15800   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15801 #else
15802   reg = arg_pointer_rtx;
15803   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15804 #endif
15805
15806   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15807   if (GET_CODE (elim) == PLUS)
15808     {
15809       offset += INTVAL (XEXP (elim, 1));
15810       elim = XEXP (elim, 0);
15811     }
15812
15813   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
15814                && (elim == hard_frame_pointer_rtx
15815                    || elim == stack_pointer_rtx))
15816               || elim == (frame_pointer_needed
15817                           ? hard_frame_pointer_rtx
15818                           : stack_pointer_rtx));
15819
15820   frame_pointer_fb_offset = -offset;
15821 }
15822
15823 /* Generate a DW_AT_name attribute given some string value to be included as
15824    the value of the attribute.  */
15825
15826 static void
15827 add_name_attribute (dw_die_ref die, const char *name_string)
15828 {
15829   if (name_string != NULL && *name_string != 0)
15830     {
15831       if (demangle_name_func)
15832         name_string = (*demangle_name_func) (name_string);
15833
15834       add_AT_string (die, DW_AT_name, name_string);
15835     }
15836 }
15837
15838 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15839
15840 static void
15841 add_comp_dir_attribute (dw_die_ref die)
15842 {
15843   const char *wd = get_src_pwd ();
15844   char *wd1;
15845
15846   if (wd == NULL)
15847     return;
15848
15849   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15850     {
15851       int wdlen;
15852
15853       wdlen = strlen (wd);
15854       wd1 = GGC_NEWVEC (char, wdlen + 2);
15855       strcpy (wd1, wd);
15856       wd1 [wdlen] = DIR_SEPARATOR;
15857       wd1 [wdlen + 1] = 0;
15858       wd = wd1;
15859     }
15860
15861     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15862 }
15863
15864 /* Given a tree node describing an array bound (either lower or upper) output
15865    a representation for that bound.  */
15866
15867 static void
15868 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15869 {
15870   switch (TREE_CODE (bound))
15871     {
15872     case ERROR_MARK:
15873       return;
15874
15875     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15876     case INTEGER_CST:
15877       {
15878         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15879
15880         /* Use the default if possible.  */
15881         if (bound_attr == DW_AT_lower_bound
15882             && (((is_c_family () || is_java ()) && integer_zerop (bound))
15883                 || (is_fortran () && integer_onep (bound))))
15884           ;
15885
15886         /* Otherwise represent the bound as an unsigned value with the
15887            precision of its type.  The precision and signedness of the
15888            type will be necessary to re-interpret it unambiguously.  */
15889         else if (prec < HOST_BITS_PER_WIDE_INT)
15890           {
15891             unsigned HOST_WIDE_INT mask
15892               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15893             add_AT_unsigned (subrange_die, bound_attr,
15894                              TREE_INT_CST_LOW (bound) & mask);
15895           }
15896         else if (prec == HOST_BITS_PER_WIDE_INT
15897                  || TREE_INT_CST_HIGH (bound) == 0)
15898           add_AT_unsigned (subrange_die, bound_attr,
15899                            TREE_INT_CST_LOW (bound));
15900         else
15901           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15902                          TREE_INT_CST_LOW (bound));
15903       }
15904       break;
15905
15906     CASE_CONVERT:
15907     case VIEW_CONVERT_EXPR:
15908       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15909       break;
15910
15911     case SAVE_EXPR:
15912       break;
15913
15914     case VAR_DECL:
15915     case PARM_DECL:
15916     case RESULT_DECL:
15917       {
15918         dw_die_ref decl_die = lookup_decl_die (bound);
15919         dw_loc_list_ref loc;
15920
15921         /* ??? Can this happen, or should the variable have been bound
15922            first?  Probably it can, since I imagine that we try to create
15923            the types of parameters in the order in which they exist in
15924            the list, and won't have created a forward reference to a
15925            later parameter.  */
15926         if (decl_die != NULL)
15927           add_AT_die_ref (subrange_die, bound_attr, decl_die);
15928         else
15929           {
15930             loc = loc_list_from_tree (bound, 0);
15931             add_AT_location_description (subrange_die, bound_attr, loc);
15932           }
15933         break;
15934       }
15935
15936     default:
15937       {
15938         /* Otherwise try to create a stack operation procedure to
15939            evaluate the value of the array bound.  */
15940
15941         dw_die_ref ctx, decl_die;
15942         dw_loc_list_ref list;
15943
15944         list = loc_list_from_tree (bound, 2);
15945         if (list == NULL)
15946           break;
15947
15948         if (current_function_decl == 0)
15949           ctx = comp_unit_die;
15950         else
15951           ctx = lookup_decl_die (current_function_decl);
15952
15953         decl_die = new_die (DW_TAG_variable, ctx, bound);
15954         add_AT_flag (decl_die, DW_AT_artificial, 1);
15955         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15956         if (list->dw_loc_next)
15957           add_AT_loc_list (decl_die, DW_AT_location, list);
15958         else
15959           add_AT_loc (decl_die, DW_AT_location, list->expr);
15960
15961         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15962         break;
15963       }
15964     }
15965 }
15966
15967 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15968    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15969    Note that the block of subscript information for an array type also
15970    includes information about the element type of the given array type.  */
15971
15972 static void
15973 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15974 {
15975   unsigned dimension_number;
15976   tree lower, upper;
15977   dw_die_ref subrange_die;
15978
15979   for (dimension_number = 0;
15980        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15981        type = TREE_TYPE (type), dimension_number++)
15982     {
15983       tree domain = TYPE_DOMAIN (type);
15984
15985       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15986         break;
15987
15988       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15989          and (in GNU C only) variable bounds.  Handle all three forms
15990          here.  */
15991       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15992       if (domain)
15993         {
15994           /* We have an array type with specified bounds.  */
15995           lower = TYPE_MIN_VALUE (domain);
15996           upper = TYPE_MAX_VALUE (domain);
15997
15998           /* Define the index type.  */
15999           if (TREE_TYPE (domain))
16000             {
16001               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16002                  TREE_TYPE field.  We can't emit debug info for this
16003                  because it is an unnamed integral type.  */
16004               if (TREE_CODE (domain) == INTEGER_TYPE
16005                   && TYPE_NAME (domain) == NULL_TREE
16006                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16007                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16008                 ;
16009               else
16010                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16011                                     type_die);
16012             }
16013
16014           /* ??? If upper is NULL, the array has unspecified length,
16015              but it does have a lower bound.  This happens with Fortran
16016                dimension arr(N:*)
16017              Since the debugger is definitely going to need to know N
16018              to produce useful results, go ahead and output the lower
16019              bound solo, and hope the debugger can cope.  */
16020
16021           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16022           if (upper)
16023             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16024         }
16025
16026       /* Otherwise we have an array type with an unspecified length.  The
16027          DWARF-2 spec does not say how to handle this; let's just leave out the
16028          bounds.  */
16029     }
16030 }
16031
16032 static void
16033 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16034 {
16035   unsigned size;
16036
16037   switch (TREE_CODE (tree_node))
16038     {
16039     case ERROR_MARK:
16040       size = 0;
16041       break;
16042     case ENUMERAL_TYPE:
16043     case RECORD_TYPE:
16044     case UNION_TYPE:
16045     case QUAL_UNION_TYPE:
16046       size = int_size_in_bytes (tree_node);
16047       break;
16048     case FIELD_DECL:
16049       /* For a data member of a struct or union, the DW_AT_byte_size is
16050          generally given as the number of bytes normally allocated for an
16051          object of the *declared* type of the member itself.  This is true
16052          even for bit-fields.  */
16053       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16054       break;
16055     default:
16056       gcc_unreachable ();
16057     }
16058
16059   /* Note that `size' might be -1 when we get to this point.  If it is, that
16060      indicates that the byte size of the entity in question is variable.  We
16061      have no good way of expressing this fact in Dwarf at the present time,
16062      so just let the -1 pass on through.  */
16063   add_AT_unsigned (die, DW_AT_byte_size, size);
16064 }
16065
16066 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16067    which specifies the distance in bits from the highest order bit of the
16068    "containing object" for the bit-field to the highest order bit of the
16069    bit-field itself.
16070
16071    For any given bit-field, the "containing object" is a hypothetical object
16072    (of some integral or enum type) within which the given bit-field lives.  The
16073    type of this hypothetical "containing object" is always the same as the
16074    declared type of the individual bit-field itself.  The determination of the
16075    exact location of the "containing object" for a bit-field is rather
16076    complicated.  It's handled by the `field_byte_offset' function (above).
16077
16078    Note that it is the size (in bytes) of the hypothetical "containing object"
16079    which will be given in the DW_AT_byte_size attribute for this bit-field.
16080    (See `byte_size_attribute' above).  */
16081
16082 static inline void
16083 add_bit_offset_attribute (dw_die_ref die, tree decl)
16084 {
16085   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16086   tree type = DECL_BIT_FIELD_TYPE (decl);
16087   HOST_WIDE_INT bitpos_int;
16088   HOST_WIDE_INT highest_order_object_bit_offset;
16089   HOST_WIDE_INT highest_order_field_bit_offset;
16090   HOST_WIDE_INT unsigned bit_offset;
16091
16092   /* Must be a field and a bit field.  */
16093   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16094
16095   /* We can't yet handle bit-fields whose offsets are variable, so if we
16096      encounter such things, just return without generating any attribute
16097      whatsoever.  Likewise for variable or too large size.  */
16098   if (! host_integerp (bit_position (decl), 0)
16099       || ! host_integerp (DECL_SIZE (decl), 1))
16100     return;
16101
16102   bitpos_int = int_bit_position (decl);
16103
16104   /* Note that the bit offset is always the distance (in bits) from the
16105      highest-order bit of the "containing object" to the highest-order bit of
16106      the bit-field itself.  Since the "high-order end" of any object or field
16107      is different on big-endian and little-endian machines, the computation
16108      below must take account of these differences.  */
16109   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16110   highest_order_field_bit_offset = bitpos_int;
16111
16112   if (! BYTES_BIG_ENDIAN)
16113     {
16114       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16115       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16116     }
16117
16118   bit_offset
16119     = (! BYTES_BIG_ENDIAN
16120        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16121        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16122
16123   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
16124 }
16125
16126 /* For a FIELD_DECL node which represents a bit field, output an attribute
16127    which specifies the length in bits of the given field.  */
16128
16129 static inline void
16130 add_bit_size_attribute (dw_die_ref die, tree decl)
16131 {
16132   /* Must be a field and a bit field.  */
16133   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16134               && DECL_BIT_FIELD_TYPE (decl));
16135
16136   if (host_integerp (DECL_SIZE (decl), 1))
16137     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16138 }
16139
16140 /* If the compiled language is ANSI C, then add a 'prototyped'
16141    attribute, if arg types are given for the parameters of a function.  */
16142
16143 static inline void
16144 add_prototyped_attribute (dw_die_ref die, tree func_type)
16145 {
16146   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
16147       && TYPE_ARG_TYPES (func_type) != NULL)
16148     add_AT_flag (die, DW_AT_prototyped, 1);
16149 }
16150
16151 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
16152    by looking in either the type declaration or object declaration
16153    equate table.  */
16154
16155 static inline dw_die_ref
16156 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16157 {
16158   dw_die_ref origin_die = NULL;
16159
16160   if (TREE_CODE (origin) != FUNCTION_DECL)
16161     {
16162       /* We may have gotten separated from the block for the inlined
16163          function, if we're in an exception handler or some such; make
16164          sure that the abstract function has been written out.
16165
16166          Doing this for nested functions is wrong, however; functions are
16167          distinct units, and our context might not even be inline.  */
16168       tree fn = origin;
16169
16170       if (TYPE_P (fn))
16171         fn = TYPE_STUB_DECL (fn);
16172
16173       fn = decl_function_context (fn);
16174       if (fn)
16175         dwarf2out_abstract_function (fn);
16176     }
16177
16178   if (DECL_P (origin))
16179     origin_die = lookup_decl_die (origin);
16180   else if (TYPE_P (origin))
16181     origin_die = lookup_type_die (origin);
16182
16183   /* XXX: Functions that are never lowered don't always have correct block
16184      trees (in the case of java, they simply have no block tree, in some other
16185      languages).  For these functions, there is nothing we can really do to
16186      output correct debug info for inlined functions in all cases.  Rather
16187      than die, we'll just produce deficient debug info now, in that we will
16188      have variables without a proper abstract origin.  In the future, when all
16189      functions are lowered, we should re-add a gcc_assert (origin_die)
16190      here.  */
16191
16192   if (origin_die)
16193     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16194   return origin_die;
16195 }
16196
16197 /* We do not currently support the pure_virtual attribute.  */
16198
16199 static inline void
16200 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16201 {
16202   if (DECL_VINDEX (func_decl))
16203     {
16204       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16205
16206       if (host_integerp (DECL_VINDEX (func_decl), 0))
16207         add_AT_loc (die, DW_AT_vtable_elem_location,
16208                     new_loc_descr (DW_OP_constu,
16209                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
16210                                    0));
16211
16212       /* GNU extension: Record what type this method came from originally.  */
16213       if (debug_info_level > DINFO_LEVEL_TERSE)
16214         add_AT_die_ref (die, DW_AT_containing_type,
16215                         lookup_type_die (DECL_CONTEXT (func_decl)));
16216     }
16217 }
16218 \f
16219 /* Add source coordinate attributes for the given decl.  */
16220
16221 static void
16222 add_src_coords_attributes (dw_die_ref die, tree decl)
16223 {
16224   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16225
16226   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16227   add_AT_unsigned (die, DW_AT_decl_line, s.line);
16228 }
16229
16230 /* Add a DW_AT_name attribute and source coordinate attribute for the
16231    given decl, but only if it actually has a name.  */
16232
16233 static void
16234 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16235 {
16236   tree decl_name;
16237
16238   decl_name = DECL_NAME (decl);
16239   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16240     {
16241       add_name_attribute (die, dwarf2_name (decl, 0));
16242       if (! DECL_ARTIFICIAL (decl))
16243         add_src_coords_attributes (die, decl);
16244
16245       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16246           && TREE_PUBLIC (decl)
16247           && !DECL_ABSTRACT (decl)
16248           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16249           && !is_fortran ())
16250         {
16251           /* Defer until we have an assembler name set.  */
16252           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16253             {
16254               limbo_die_node *asm_name;
16255
16256               asm_name = GGC_CNEW (limbo_die_node);
16257               asm_name->die = die;
16258               asm_name->created_for = decl;
16259               asm_name->next = deferred_asm_name;
16260               deferred_asm_name = asm_name;
16261             }
16262           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16263             add_AT_string (die, DW_AT_MIPS_linkage_name,
16264                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
16265         }
16266     }
16267
16268 #ifdef VMS_DEBUGGING_INFO
16269   /* Get the function's name, as described by its RTL.  This may be different
16270      from the DECL_NAME name used in the source file.  */
16271   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16272     {
16273       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16274                    XEXP (DECL_RTL (decl), 0));
16275       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
16276     }
16277 #endif
16278 }
16279
16280 /* Push a new declaration scope.  */
16281
16282 static void
16283 push_decl_scope (tree scope)
16284 {
16285   VEC_safe_push (tree, gc, decl_scope_table, scope);
16286 }
16287
16288 /* Pop a declaration scope.  */
16289
16290 static inline void
16291 pop_decl_scope (void)
16292 {
16293   VEC_pop (tree, decl_scope_table);
16294 }
16295
16296 /* Return the DIE for the scope that immediately contains this type.
16297    Non-named types get global scope.  Named types nested in other
16298    types get their containing scope if it's open, or global scope
16299    otherwise.  All other types (i.e. function-local named types) get
16300    the current active scope.  */
16301
16302 static dw_die_ref
16303 scope_die_for (tree t, dw_die_ref context_die)
16304 {
16305   dw_die_ref scope_die = NULL;
16306   tree containing_scope;
16307   int i;
16308
16309   /* Non-types always go in the current scope.  */
16310   gcc_assert (TYPE_P (t));
16311
16312   containing_scope = TYPE_CONTEXT (t);
16313
16314   /* Use the containing namespace if it was passed in (for a declaration).  */
16315   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16316     {
16317       if (context_die == lookup_decl_die (containing_scope))
16318         /* OK */;
16319       else
16320         containing_scope = NULL_TREE;
16321     }
16322
16323   /* Ignore function type "scopes" from the C frontend.  They mean that
16324      a tagged type is local to a parmlist of a function declarator, but
16325      that isn't useful to DWARF.  */
16326   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16327     containing_scope = NULL_TREE;
16328
16329   if (containing_scope == NULL_TREE)
16330     scope_die = comp_unit_die;
16331   else if (TYPE_P (containing_scope))
16332     {
16333       /* For types, we can just look up the appropriate DIE.  But
16334          first we check to see if we're in the middle of emitting it
16335          so we know where the new DIE should go.  */
16336       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16337         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16338           break;
16339
16340       if (i < 0)
16341         {
16342           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16343                       || TREE_ASM_WRITTEN (containing_scope));
16344
16345           /* If none of the current dies are suitable, we get file scope.  */
16346           scope_die = comp_unit_die;
16347         }
16348       else
16349         scope_die = lookup_type_die (containing_scope);
16350     }
16351   else
16352     scope_die = context_die;
16353
16354   return scope_die;
16355 }
16356
16357 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16358
16359 static inline int
16360 local_scope_p (dw_die_ref context_die)
16361 {
16362   for (; context_die; context_die = context_die->die_parent)
16363     if (context_die->die_tag == DW_TAG_inlined_subroutine
16364         || context_die->die_tag == DW_TAG_subprogram)
16365       return 1;
16366
16367   return 0;
16368 }
16369
16370 /* Returns nonzero if CONTEXT_DIE is a class.  */
16371
16372 static inline int
16373 class_scope_p (dw_die_ref context_die)
16374 {
16375   return (context_die
16376           && (context_die->die_tag == DW_TAG_structure_type
16377               || context_die->die_tag == DW_TAG_class_type
16378               || context_die->die_tag == DW_TAG_interface_type
16379               || context_die->die_tag == DW_TAG_union_type));
16380 }
16381
16382 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16383    whether or not to treat a DIE in this context as a declaration.  */
16384
16385 static inline int
16386 class_or_namespace_scope_p (dw_die_ref context_die)
16387 {
16388   return (class_scope_p (context_die)
16389           || (context_die && context_die->die_tag == DW_TAG_namespace));
16390 }
16391
16392 /* Many forms of DIEs require a "type description" attribute.  This
16393    routine locates the proper "type descriptor" die for the type given
16394    by 'type', and adds a DW_AT_type attribute below the given die.  */
16395
16396 static void
16397 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16398                     int decl_volatile, dw_die_ref context_die)
16399 {
16400   enum tree_code code  = TREE_CODE (type);
16401   dw_die_ref type_die  = NULL;
16402
16403   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16404      or fixed-point type, use the inner type.  This is because we have no
16405      support for unnamed types in base_type_die.  This can happen if this is
16406      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16407   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16408       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16409     type = TREE_TYPE (type), code = TREE_CODE (type);
16410
16411   if (code == ERROR_MARK
16412       /* Handle a special case.  For functions whose return type is void, we
16413          generate *no* type attribute.  (Note that no object may have type
16414          `void', so this only applies to function return types).  */
16415       || code == VOID_TYPE)
16416     return;
16417
16418   type_die = modified_type_die (type,
16419                                 decl_const || TYPE_READONLY (type),
16420                                 decl_volatile || TYPE_VOLATILE (type),
16421                                 context_die);
16422
16423   if (type_die != NULL)
16424     add_AT_die_ref (object_die, DW_AT_type, type_die);
16425 }
16426
16427 /* Given an object die, add the calling convention attribute for the
16428    function call type.  */
16429 static void
16430 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16431 {
16432   enum dwarf_calling_convention value = DW_CC_normal;
16433
16434   value = ((enum dwarf_calling_convention)
16435            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16436
16437   /* DWARF doesn't provide a way to identify a program's source-level
16438      entry point.  DW_AT_calling_convention attributes are only meant
16439      to describe functions' calling conventions.  However, lacking a
16440      better way to signal the Fortran main program, we use this for the
16441      time being, following existing custom.  */
16442   if (is_fortran ()
16443       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16444     value = DW_CC_program;
16445
16446   /* Only add the attribute if the backend requests it, and
16447      is not DW_CC_normal.  */
16448   if (value && (value != DW_CC_normal))
16449     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16450 }
16451
16452 /* Given a tree pointer to a struct, class, union, or enum type node, return
16453    a pointer to the (string) tag name for the given type, or zero if the type
16454    was declared without a tag.  */
16455
16456 static const char *
16457 type_tag (const_tree type)
16458 {
16459   const char *name = 0;
16460
16461   if (TYPE_NAME (type) != 0)
16462     {
16463       tree t = 0;
16464
16465       /* Find the IDENTIFIER_NODE for the type name.  */
16466       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
16467         t = TYPE_NAME (type);
16468
16469       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16470          a TYPE_DECL node, regardless of whether or not a `typedef' was
16471          involved.  */
16472       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16473                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16474         {
16475           /* We want to be extra verbose.  Don't call dwarf_name if
16476              DECL_NAME isn't set.  The default hook for decl_printable_name
16477              doesn't like that, and in this context it's correct to return
16478              0, instead of "<anonymous>" or the like.  */
16479           if (DECL_NAME (TYPE_NAME (type)))
16480             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16481         }
16482
16483       /* Now get the name as a string, or invent one.  */
16484       if (!name && t != 0)
16485         name = IDENTIFIER_POINTER (t);
16486     }
16487
16488   return (name == 0 || *name == '\0') ? 0 : name;
16489 }
16490
16491 /* Return the type associated with a data member, make a special check
16492    for bit field types.  */
16493
16494 static inline tree
16495 member_declared_type (const_tree member)
16496 {
16497   return (DECL_BIT_FIELD_TYPE (member)
16498           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16499 }
16500
16501 /* Get the decl's label, as described by its RTL. This may be different
16502    from the DECL_NAME name used in the source file.  */
16503
16504 #if 0
16505 static const char *
16506 decl_start_label (tree decl)
16507 {
16508   rtx x;
16509   const char *fnname;
16510
16511   x = DECL_RTL (decl);
16512   gcc_assert (MEM_P (x));
16513
16514   x = XEXP (x, 0);
16515   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16516
16517   fnname = XSTR (x, 0);
16518   return fnname;
16519 }
16520 #endif
16521 \f
16522 /* These routines generate the internal representation of the DIE's for
16523    the compilation unit.  Debugging information is collected by walking
16524    the declaration trees passed in from dwarf2out_decl().  */
16525
16526 static void
16527 gen_array_type_die (tree type, dw_die_ref context_die)
16528 {
16529   dw_die_ref scope_die = scope_die_for (type, context_die);
16530   dw_die_ref array_die;
16531
16532   /* GNU compilers represent multidimensional array types as sequences of one
16533      dimensional array types whose element types are themselves array types.
16534      We sometimes squish that down to a single array_type DIE with multiple
16535      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16536      say that we are allowed to do this kind of compression in C, because
16537      there is no difference between an array of arrays and a multidimensional
16538      array.  We don't do this for Ada to remain as close as possible to the
16539      actual representation, which is especially important against the language
16540      flexibilty wrt arrays of variable size.  */
16541
16542   bool collapse_nested_arrays = !is_ada ();
16543   tree element_type;
16544
16545   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16546      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16547   if (TYPE_STRING_FLAG (type)
16548       && TREE_CODE (type) == ARRAY_TYPE
16549       && is_fortran ()
16550       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16551     {
16552       HOST_WIDE_INT size;
16553
16554       array_die = new_die (DW_TAG_string_type, scope_die, type);
16555       add_name_attribute (array_die, type_tag (type));
16556       equate_type_number_to_die (type, array_die);
16557       size = int_size_in_bytes (type);
16558       if (size >= 0)
16559         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16560       else if (TYPE_DOMAIN (type) != NULL_TREE
16561                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16562                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16563         {
16564           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16565           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16566
16567           size = int_size_in_bytes (TREE_TYPE (szdecl));
16568           if (loc && size > 0)
16569             {
16570               add_AT_location_description (array_die, DW_AT_string_length, loc);
16571               if (size != DWARF2_ADDR_SIZE)
16572                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16573             }
16574         }
16575       return;
16576     }
16577
16578   /* ??? The SGI dwarf reader fails for array of array of enum types
16579      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16580      array type comes before the outer array type.  We thus call gen_type_die
16581      before we new_die and must prevent nested array types collapsing for this
16582      target.  */
16583
16584 #ifdef MIPS_DEBUGGING_INFO
16585   gen_type_die (TREE_TYPE (type), context_die);
16586   collapse_nested_arrays = false;
16587 #endif
16588
16589   array_die = new_die (DW_TAG_array_type, scope_die, type);
16590   add_name_attribute (array_die, type_tag (type));
16591   equate_type_number_to_die (type, array_die);
16592
16593   if (TREE_CODE (type) == VECTOR_TYPE)
16594     {
16595       /* The frontend feeds us a representation for the vector as a struct
16596          containing an array.  Pull out the array type.  */
16597       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
16598       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16599     }
16600
16601   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16602   if (is_fortran ()
16603       && TREE_CODE (type) == ARRAY_TYPE
16604       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16605       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16606     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16607
16608 #if 0
16609   /* We default the array ordering.  SDB will probably do
16610      the right things even if DW_AT_ordering is not present.  It's not even
16611      an issue until we start to get into multidimensional arrays anyway.  If
16612      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16613      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16614      and when we find out that we need to put these in, we will only do so
16615      for multidimensional arrays.  */
16616   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16617 #endif
16618
16619 #ifdef MIPS_DEBUGGING_INFO
16620   /* The SGI compilers handle arrays of unknown bound by setting
16621      AT_declaration and not emitting any subrange DIEs.  */
16622   if (! TYPE_DOMAIN (type))
16623     add_AT_flag (array_die, DW_AT_declaration, 1);
16624   else
16625 #endif
16626     add_subscript_info (array_die, type, collapse_nested_arrays);
16627
16628   /* Add representation of the type of the elements of this array type and
16629      emit the corresponding DIE if we haven't done it already.  */  
16630   element_type = TREE_TYPE (type);
16631   if (collapse_nested_arrays)
16632     while (TREE_CODE (element_type) == ARRAY_TYPE)
16633       {
16634         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16635           break;
16636         element_type = TREE_TYPE (element_type);
16637       }
16638
16639 #ifndef MIPS_DEBUGGING_INFO
16640   gen_type_die (element_type, context_die);
16641 #endif
16642
16643   add_type_attribute (array_die, element_type, 0, 0, context_die);
16644
16645   if (get_AT (array_die, DW_AT_name))
16646     add_pubtype (type, array_die);
16647 }
16648
16649 static dw_loc_descr_ref
16650 descr_info_loc (tree val, tree base_decl)
16651 {
16652   HOST_WIDE_INT size;
16653   dw_loc_descr_ref loc, loc2;
16654   enum dwarf_location_atom op;
16655
16656   if (val == base_decl)
16657     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16658
16659   switch (TREE_CODE (val))
16660     {
16661     CASE_CONVERT:
16662       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16663     case VAR_DECL:
16664       return loc_descriptor_from_tree (val, 0);
16665     case INTEGER_CST:
16666       if (host_integerp (val, 0))
16667         return int_loc_descriptor (tree_low_cst (val, 0));
16668       break;
16669     case INDIRECT_REF:
16670       size = int_size_in_bytes (TREE_TYPE (val));
16671       if (size < 0)
16672         break;
16673       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16674       if (!loc)
16675         break;
16676       if (size == DWARF2_ADDR_SIZE)
16677         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16678       else
16679         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16680       return loc;
16681     case POINTER_PLUS_EXPR:
16682     case PLUS_EXPR:
16683       if (host_integerp (TREE_OPERAND (val, 1), 1)
16684           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16685              < 16384)
16686         {
16687           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16688           if (!loc)
16689             break;
16690           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16691         }
16692       else
16693         {
16694           op = DW_OP_plus;
16695         do_binop:
16696           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16697           if (!loc)
16698             break;
16699           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16700           if (!loc2)
16701             break;
16702           add_loc_descr (&loc, loc2);
16703           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16704         }
16705       return loc;
16706     case MINUS_EXPR:
16707       op = DW_OP_minus;
16708       goto do_binop;
16709     case MULT_EXPR:
16710       op = DW_OP_mul;
16711       goto do_binop;
16712     case EQ_EXPR:
16713       op = DW_OP_eq;
16714       goto do_binop;
16715     case NE_EXPR:
16716       op = DW_OP_ne;
16717       goto do_binop;
16718     default:
16719       break;
16720     }
16721   return NULL;
16722 }
16723
16724 static void
16725 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16726                       tree val, tree base_decl)
16727 {
16728   dw_loc_descr_ref loc;
16729
16730   if (host_integerp (val, 0))
16731     {
16732       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16733       return;
16734     }
16735
16736   loc = descr_info_loc (val, base_decl);
16737   if (!loc)
16738     return;
16739
16740   add_AT_loc (die, attr, loc);
16741 }
16742
16743 /* This routine generates DIE for array with hidden descriptor, details
16744    are filled into *info by a langhook.  */
16745
16746 static void
16747 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16748                           dw_die_ref context_die)
16749 {
16750   dw_die_ref scope_die = scope_die_for (type, context_die);
16751   dw_die_ref array_die;
16752   int dim;
16753
16754   array_die = new_die (DW_TAG_array_type, scope_die, type);
16755   add_name_attribute (array_die, type_tag (type));
16756   equate_type_number_to_die (type, array_die);
16757
16758   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16759   if (is_fortran ()
16760       && info->ndimensions >= 2)
16761     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16762
16763   if (info->data_location)
16764     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16765                           info->base_decl);
16766   if (info->associated)
16767     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16768                           info->base_decl);
16769   if (info->allocated)
16770     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16771                           info->base_decl);
16772
16773   for (dim = 0; dim < info->ndimensions; dim++)
16774     {
16775       dw_die_ref subrange_die
16776         = new_die (DW_TAG_subrange_type, array_die, NULL);
16777
16778       if (info->dimen[dim].lower_bound)
16779         {
16780           /* If it is the default value, omit it.  */
16781           if ((is_c_family () || is_java ())
16782               && integer_zerop (info->dimen[dim].lower_bound))
16783             ;
16784           else if (is_fortran ()
16785                    && integer_onep (info->dimen[dim].lower_bound))
16786             ;
16787           else
16788             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16789                                   info->dimen[dim].lower_bound,
16790                                   info->base_decl);
16791         }
16792       if (info->dimen[dim].upper_bound)
16793         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16794                               info->dimen[dim].upper_bound,
16795                               info->base_decl);
16796       if (info->dimen[dim].stride)
16797         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16798                               info->dimen[dim].stride,
16799                               info->base_decl);
16800     }
16801
16802   gen_type_die (info->element_type, context_die);
16803   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16804
16805   if (get_AT (array_die, DW_AT_name))
16806     add_pubtype (type, array_die);
16807 }
16808
16809 #if 0
16810 static void
16811 gen_entry_point_die (tree decl, dw_die_ref context_die)
16812 {
16813   tree origin = decl_ultimate_origin (decl);
16814   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16815
16816   if (origin != NULL)
16817     add_abstract_origin_attribute (decl_die, origin);
16818   else
16819     {
16820       add_name_and_src_coords_attributes (decl_die, decl);
16821       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16822                           0, 0, context_die);
16823     }
16824
16825   if (DECL_ABSTRACT (decl))
16826     equate_decl_number_to_die (decl, decl_die);
16827   else
16828     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16829 }
16830 #endif
16831
16832 /* Walk through the list of incomplete types again, trying once more to
16833    emit full debugging info for them.  */
16834
16835 static void
16836 retry_incomplete_types (void)
16837 {
16838   int i;
16839
16840   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16841     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
16842 }
16843
16844 /* Determine what tag to use for a record type.  */
16845
16846 static enum dwarf_tag
16847 record_type_tag (tree type)
16848 {
16849   if (! lang_hooks.types.classify_record)
16850     return DW_TAG_structure_type;
16851
16852   switch (lang_hooks.types.classify_record (type))
16853     {
16854     case RECORD_IS_STRUCT:
16855       return DW_TAG_structure_type;
16856
16857     case RECORD_IS_CLASS:
16858       return DW_TAG_class_type;
16859
16860     case RECORD_IS_INTERFACE:
16861       if (dwarf_version >= 3 || !dwarf_strict)
16862         return DW_TAG_interface_type;
16863       return DW_TAG_structure_type;
16864
16865     default:
16866       gcc_unreachable ();
16867     }
16868 }
16869
16870 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16871    include all of the information about the enumeration values also. Each
16872    enumerated type name/value is listed as a child of the enumerated type
16873    DIE.  */
16874
16875 static dw_die_ref
16876 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16877 {
16878   dw_die_ref type_die = lookup_type_die (type);
16879
16880   if (type_die == NULL)
16881     {
16882       type_die = new_die (DW_TAG_enumeration_type,
16883                           scope_die_for (type, context_die), type);
16884       equate_type_number_to_die (type, type_die);
16885       add_name_attribute (type_die, type_tag (type));
16886     }
16887   else if (! TYPE_SIZE (type))
16888     return type_die;
16889   else
16890     remove_AT (type_die, DW_AT_declaration);
16891
16892   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16893      given enum type is incomplete, do not generate the DW_AT_byte_size
16894      attribute or the DW_AT_element_list attribute.  */
16895   if (TYPE_SIZE (type))
16896     {
16897       tree link;
16898
16899       TREE_ASM_WRITTEN (type) = 1;
16900       add_byte_size_attribute (type_die, type);
16901       if (TYPE_STUB_DECL (type) != NULL_TREE)
16902         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16903
16904       /* If the first reference to this type was as the return type of an
16905          inline function, then it may not have a parent.  Fix this now.  */
16906       if (type_die->die_parent == NULL)
16907         add_child_die (scope_die_for (type, context_die), type_die);
16908
16909       for (link = TYPE_VALUES (type);
16910            link != NULL; link = TREE_CHAIN (link))
16911         {
16912           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16913           tree value = TREE_VALUE (link);
16914
16915           add_name_attribute (enum_die,
16916                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16917
16918           if (TREE_CODE (value) == CONST_DECL)
16919             value = DECL_INITIAL (value);
16920
16921           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16922             /* DWARF2 does not provide a way of indicating whether or
16923                not enumeration constants are signed or unsigned.  GDB
16924                always assumes the values are signed, so we output all
16925                values as if they were signed.  That means that
16926                enumeration constants with very large unsigned values
16927                will appear to have negative values in the debugger.  */
16928             add_AT_int (enum_die, DW_AT_const_value,
16929                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16930         }
16931     }
16932   else
16933     add_AT_flag (type_die, DW_AT_declaration, 1);
16934
16935   if (get_AT (type_die, DW_AT_name))
16936     add_pubtype (type, type_die);
16937
16938   return type_die;
16939 }
16940
16941 /* Generate a DIE to represent either a real live formal parameter decl or to
16942    represent just the type of some formal parameter position in some function
16943    type.
16944
16945    Note that this routine is a bit unusual because its argument may be a
16946    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16947    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16948    node.  If it's the former then this function is being called to output a
16949    DIE to represent a formal parameter object (or some inlining thereof).  If
16950    it's the latter, then this function is only being called to output a
16951    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16952    argument type of some subprogram type.
16953    If EMIT_NAME_P is true, name and source coordinate attributes
16954    are emitted.  */
16955
16956 static dw_die_ref
16957 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16958                           dw_die_ref context_die)
16959 {
16960   tree node_or_origin = node ? node : origin;
16961   dw_die_ref parm_die
16962     = new_die (DW_TAG_formal_parameter, context_die, node);
16963
16964   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16965     {
16966     case tcc_declaration:
16967       if (!origin)
16968         origin = decl_ultimate_origin (node);
16969       if (origin != NULL)
16970         add_abstract_origin_attribute (parm_die, origin);
16971       else
16972         {
16973           tree type = TREE_TYPE (node);
16974           if (emit_name_p)
16975             add_name_and_src_coords_attributes (parm_die, node);
16976           if (decl_by_reference_p (node))
16977             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16978                                 context_die);
16979           else
16980             add_type_attribute (parm_die, type,
16981                                 TREE_READONLY (node),
16982                                 TREE_THIS_VOLATILE (node),
16983                                 context_die);
16984           if (DECL_ARTIFICIAL (node))
16985             add_AT_flag (parm_die, DW_AT_artificial, 1);
16986         }
16987
16988       if (node && node != origin)
16989         equate_decl_number_to_die (node, parm_die);
16990       if (! DECL_ABSTRACT (node_or_origin))
16991         add_location_or_const_value_attribute (parm_die, node_or_origin,
16992                                                DW_AT_location);
16993
16994       break;
16995
16996     case tcc_type:
16997       /* We were called with some kind of a ..._TYPE node.  */
16998       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16999       break;
17000
17001     default:
17002       gcc_unreachable ();
17003     }
17004
17005   return parm_die;
17006 }
17007
17008 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17009    children DW_TAG_formal_parameter DIEs representing the arguments of the
17010    parameter pack.
17011
17012    PARM_PACK must be a function parameter pack.
17013    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17014    must point to the subsequent arguments of the function PACK_ARG belongs to.
17015    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17016    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17017    following the last one for which a DIE was generated.  */
17018
17019 static dw_die_ref
17020 gen_formal_parameter_pack_die  (tree parm_pack,
17021                                 tree pack_arg,
17022                                 dw_die_ref subr_die,
17023                                 tree *next_arg)
17024 {
17025   tree arg;
17026   dw_die_ref parm_pack_die;
17027
17028   gcc_assert (parm_pack
17029               && lang_hooks.function_parameter_pack_p (parm_pack)
17030               && DECL_NAME (parm_pack)
17031               && subr_die);
17032
17033   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17034   add_AT_string (parm_pack_die, DW_AT_name,
17035                  IDENTIFIER_POINTER (DECL_NAME (parm_pack)));
17036
17037   for (arg = pack_arg; arg; arg = TREE_CHAIN (arg))
17038     {
17039       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17040                                                                  parm_pack))
17041         break;
17042       gen_formal_parameter_die (arg, NULL,
17043                                 false /* Don't emit name attribute.  */,
17044                                 parm_pack_die);
17045     }
17046   if (next_arg)
17047     *next_arg = arg;
17048   return parm_pack_die;
17049 }
17050
17051 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17052    at the end of an (ANSI prototyped) formal parameters list.  */
17053
17054 static void
17055 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17056 {
17057   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17058 }
17059
17060 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17061    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17062    parameters as specified in some function type specification (except for
17063    those which appear as part of a function *definition*).  */
17064
17065 static void
17066 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17067 {
17068   tree link;
17069   tree formal_type = NULL;
17070   tree first_parm_type;
17071   tree arg;
17072
17073   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17074     {
17075       arg = DECL_ARGUMENTS (function_or_method_type);
17076       function_or_method_type = TREE_TYPE (function_or_method_type);
17077     }
17078   else
17079     arg = NULL_TREE;
17080
17081   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17082
17083   /* Make our first pass over the list of formal parameter types and output a
17084      DW_TAG_formal_parameter DIE for each one.  */
17085   for (link = first_parm_type; link; )
17086     {
17087       dw_die_ref parm_die;
17088
17089       formal_type = TREE_VALUE (link);
17090       if (formal_type == void_type_node)
17091         break;
17092
17093       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17094       parm_die = gen_formal_parameter_die (formal_type, NULL,
17095                                            true /* Emit name attribute.  */,
17096                                            context_die);
17097       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
17098            && link == first_parm_type)
17099           || (arg && DECL_ARTIFICIAL (arg)))
17100         add_AT_flag (parm_die, DW_AT_artificial, 1);
17101
17102       link = TREE_CHAIN (link);
17103       if (arg)
17104         arg = TREE_CHAIN (arg);
17105     }
17106
17107   /* If this function type has an ellipsis, add a
17108      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
17109   if (formal_type != void_type_node)
17110     gen_unspecified_parameters_die (function_or_method_type, context_die);
17111
17112   /* Make our second (and final) pass over the list of formal parameter types
17113      and output DIEs to represent those types (as necessary).  */
17114   for (link = TYPE_ARG_TYPES (function_or_method_type);
17115        link && TREE_VALUE (link);
17116        link = TREE_CHAIN (link))
17117     gen_type_die (TREE_VALUE (link), context_die);
17118 }
17119
17120 /* We want to generate the DIE for TYPE so that we can generate the
17121    die for MEMBER, which has been defined; we will need to refer back
17122    to the member declaration nested within TYPE.  If we're trying to
17123    generate minimal debug info for TYPE, processing TYPE won't do the
17124    trick; we need to attach the member declaration by hand.  */
17125
17126 static void
17127 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17128 {
17129   gen_type_die (type, context_die);
17130
17131   /* If we're trying to avoid duplicate debug info, we may not have
17132      emitted the member decl for this function.  Emit it now.  */
17133   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17134       && ! lookup_decl_die (member))
17135     {
17136       dw_die_ref type_die;
17137       gcc_assert (!decl_ultimate_origin (member));
17138
17139       push_decl_scope (type);
17140       type_die = lookup_type_die (type);
17141       if (TREE_CODE (member) == FUNCTION_DECL)
17142         gen_subprogram_die (member, type_die);
17143       else if (TREE_CODE (member) == FIELD_DECL)
17144         {
17145           /* Ignore the nameless fields that are used to skip bits but handle
17146              C++ anonymous unions and structs.  */
17147           if (DECL_NAME (member) != NULL_TREE
17148               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17149               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17150             {
17151               gen_type_die (member_declared_type (member), type_die);
17152               gen_field_die (member, type_die);
17153             }
17154         }
17155       else
17156         gen_variable_die (member, NULL_TREE, type_die);
17157
17158       pop_decl_scope ();
17159     }
17160 }
17161
17162 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17163    may later generate inlined and/or out-of-line instances of.  */
17164
17165 static void
17166 dwarf2out_abstract_function (tree decl)
17167 {
17168   dw_die_ref old_die;
17169   tree save_fn;
17170   tree context;
17171   int was_abstract = DECL_ABSTRACT (decl);
17172   htab_t old_decl_loc_table;
17173
17174   /* Make sure we have the actual abstract inline, not a clone.  */
17175   decl = DECL_ORIGIN (decl);
17176
17177   old_die = lookup_decl_die (decl);
17178   if (old_die && get_AT (old_die, DW_AT_inline))
17179     /* We've already generated the abstract instance.  */
17180     return;
17181
17182   /* We can be called while recursively when seeing block defining inlined subroutine
17183      DIE.  Be sure to not clobber the outer location table nor use it or we would
17184      get locations in abstract instantces.  */
17185   old_decl_loc_table = decl_loc_table;
17186   decl_loc_table = NULL;
17187
17188   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17189      we don't get confused by DECL_ABSTRACT.  */
17190   if (debug_info_level > DINFO_LEVEL_TERSE)
17191     {
17192       context = decl_class_context (decl);
17193       if (context)
17194         gen_type_die_for_member
17195           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
17196     }
17197
17198   /* Pretend we've just finished compiling this function.  */
17199   save_fn = current_function_decl;
17200   current_function_decl = decl;
17201   push_cfun (DECL_STRUCT_FUNCTION (decl));
17202
17203   set_decl_abstract_flags (decl, 1);
17204   dwarf2out_decl (decl);
17205   if (! was_abstract)
17206     set_decl_abstract_flags (decl, 0);
17207
17208   current_function_decl = save_fn;
17209   decl_loc_table = old_decl_loc_table;
17210   pop_cfun ();
17211 }
17212
17213 /* Helper function of premark_used_types() which gets called through
17214    htab_traverse.
17215
17216    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17217    marked as unused by prune_unused_types.  */
17218
17219 static int
17220 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17221 {
17222   tree type;
17223   dw_die_ref die;
17224
17225   type = (tree) *slot;
17226   die = lookup_type_die (type);
17227   if (die != NULL)
17228     die->die_perennial_p = 1;
17229   return 1;
17230 }
17231
17232 /* Helper function of premark_types_used_by_global_vars which gets called
17233    through htab_traverse.
17234
17235    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17236    marked as unused by prune_unused_types. The DIE of the type is marked
17237    only if the global variable using the type will actually be emitted.  */
17238
17239 static int
17240 premark_types_used_by_global_vars_helper (void **slot,
17241                                           void *data ATTRIBUTE_UNUSED)
17242 {
17243   struct types_used_by_vars_entry *entry;
17244   dw_die_ref die;
17245
17246   entry = (struct types_used_by_vars_entry *) *slot;
17247   gcc_assert (entry->type != NULL
17248               && entry->var_decl != NULL);
17249   die = lookup_type_die (entry->type);
17250   if (die)
17251     {
17252       /* Ask cgraph if the global variable really is to be emitted.
17253          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17254       struct varpool_node *node = varpool_node (entry->var_decl);
17255       if (node->needed)
17256         {
17257           die->die_perennial_p = 1;
17258           /* Keep the parent DIEs as well.  */
17259           while ((die = die->die_parent) && die->die_perennial_p == 0)
17260             die->die_perennial_p = 1;
17261         }
17262     }
17263   return 1;
17264 }
17265
17266 /* Mark all members of used_types_hash as perennial.  */
17267
17268 static void
17269 premark_used_types (void)
17270 {
17271   if (cfun && cfun->used_types_hash)
17272     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17273 }
17274
17275 /* Mark all members of types_used_by_vars_entry as perennial.  */
17276
17277 static void
17278 premark_types_used_by_global_vars (void)
17279 {
17280   if (types_used_by_vars_hash)
17281     htab_traverse (types_used_by_vars_hash,
17282                    premark_types_used_by_global_vars_helper, NULL);
17283 }
17284
17285 /* Generate a DIE to represent a declared function (either file-scope or
17286    block-local).  */
17287
17288 static void
17289 gen_subprogram_die (tree decl, dw_die_ref context_die)
17290 {
17291   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17292   tree origin = decl_ultimate_origin (decl);
17293   dw_die_ref subr_die;
17294   tree fn_arg_types;
17295   tree outer_scope;
17296   dw_die_ref old_die = lookup_decl_die (decl);
17297   int declaration = (current_function_decl != decl
17298                      || class_or_namespace_scope_p (context_die));
17299
17300   premark_used_types ();
17301
17302   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17303      started to generate the abstract instance of an inline, decided to output
17304      its containing class, and proceeded to emit the declaration of the inline
17305      from the member list for the class.  If so, DECLARATION takes priority;
17306      we'll get back to the abstract instance when done with the class.  */
17307
17308   /* The class-scope declaration DIE must be the primary DIE.  */
17309   if (origin && declaration && class_or_namespace_scope_p (context_die))
17310     {
17311       origin = NULL;
17312       gcc_assert (!old_die);
17313     }
17314
17315   /* Now that the C++ front end lazily declares artificial member fns, we
17316      might need to retrofit the declaration into its class.  */
17317   if (!declaration && !origin && !old_die
17318       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17319       && !class_or_namespace_scope_p (context_die)
17320       && debug_info_level > DINFO_LEVEL_TERSE)
17321     old_die = force_decl_die (decl);
17322
17323   if (origin != NULL)
17324     {
17325       gcc_assert (!declaration || local_scope_p (context_die));
17326
17327       /* Fixup die_parent for the abstract instance of a nested
17328          inline function.  */
17329       if (old_die && old_die->die_parent == NULL)
17330         add_child_die (context_die, old_die);
17331
17332       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17333       add_abstract_origin_attribute (subr_die, origin);
17334     }
17335   else if (old_die)
17336     {
17337       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17338       struct dwarf_file_data * file_index = lookup_filename (s.file);
17339
17340       if (!get_AT_flag (old_die, DW_AT_declaration)
17341           /* We can have a normal definition following an inline one in the
17342              case of redefinition of GNU C extern inlines.
17343              It seems reasonable to use AT_specification in this case.  */
17344           && !get_AT (old_die, DW_AT_inline))
17345         {
17346           /* Detect and ignore this case, where we are trying to output
17347              something we have already output.  */
17348           return;
17349         }
17350
17351       /* If the definition comes from the same place as the declaration,
17352          maybe use the old DIE.  We always want the DIE for this function
17353          that has the *_pc attributes to be under comp_unit_die so the
17354          debugger can find it.  We also need to do this for abstract
17355          instances of inlines, since the spec requires the out-of-line copy
17356          to have the same parent.  For local class methods, this doesn't
17357          apply; we just use the old DIE.  */
17358       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
17359           && (DECL_ARTIFICIAL (decl)
17360               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17361                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17362                       == (unsigned) s.line))))
17363         {
17364           subr_die = old_die;
17365
17366           /* Clear out the declaration attribute and the formal parameters.
17367              Do not remove all children, because it is possible that this
17368              declaration die was forced using force_decl_die(). In such
17369              cases die that forced declaration die (e.g. TAG_imported_module)
17370              is one of the children that we do not want to remove.  */
17371           remove_AT (subr_die, DW_AT_declaration);
17372           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17373         }
17374       else
17375         {
17376           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17377           add_AT_specification (subr_die, old_die);
17378           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17379             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17380           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17381             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17382         }
17383     }
17384   else
17385     {
17386       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17387
17388       if (TREE_PUBLIC (decl))
17389         add_AT_flag (subr_die, DW_AT_external, 1);
17390
17391       add_name_and_src_coords_attributes (subr_die, decl);
17392       if (debug_info_level > DINFO_LEVEL_TERSE)
17393         {
17394           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17395           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17396                               0, 0, context_die);
17397         }
17398
17399       add_pure_or_virtual_attribute (subr_die, decl);
17400       if (DECL_ARTIFICIAL (decl))
17401         add_AT_flag (subr_die, DW_AT_artificial, 1);
17402
17403       if (TREE_PROTECTED (decl))
17404         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
17405       else if (TREE_PRIVATE (decl))
17406         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
17407     }
17408
17409   if (declaration)
17410     {
17411       if (!old_die || !get_AT (old_die, DW_AT_inline))
17412         {
17413           add_AT_flag (subr_die, DW_AT_declaration, 1);
17414
17415           /* If this is an explicit function declaration then generate
17416              a DW_AT_explicit attribute.  */
17417           if (lang_hooks.decls.function_decl_explicit_p (decl)
17418               && (dwarf_version >= 3 || !dwarf_strict))
17419             add_AT_flag (subr_die, DW_AT_explicit, 1);
17420
17421           /* The first time we see a member function, it is in the context of
17422              the class to which it belongs.  We make sure of this by emitting
17423              the class first.  The next time is the definition, which is
17424              handled above.  The two may come from the same source text.
17425
17426              Note that force_decl_die() forces function declaration die. It is
17427              later reused to represent definition.  */
17428           equate_decl_number_to_die (decl, subr_die);
17429         }
17430     }
17431   else if (DECL_ABSTRACT (decl))
17432     {
17433       if (DECL_DECLARED_INLINE_P (decl))
17434         {
17435           if (cgraph_function_possibly_inlined_p (decl))
17436             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17437           else
17438             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17439         }
17440       else
17441         {
17442           if (cgraph_function_possibly_inlined_p (decl))
17443             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17444           else
17445             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17446         }
17447
17448       if (DECL_DECLARED_INLINE_P (decl)
17449           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17450         add_AT_flag (subr_die, DW_AT_artificial, 1);
17451
17452       equate_decl_number_to_die (decl, subr_die);
17453     }
17454   else if (!DECL_EXTERNAL (decl))
17455     {
17456       HOST_WIDE_INT cfa_fb_offset;
17457
17458       if (!old_die || !get_AT (old_die, DW_AT_inline))
17459         equate_decl_number_to_die (decl, subr_die);
17460
17461       if (!flag_reorder_blocks_and_partition)
17462         {
17463           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17464                                        current_function_funcdef_no);
17465           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17466           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17467                                        current_function_funcdef_no);
17468           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17469
17470           add_pubname (decl, subr_die);
17471           add_arange (decl, subr_die);
17472         }
17473       else
17474         {  /* Do nothing for now; maybe need to duplicate die, one for
17475               hot section and one for cold section, then use the hot/cold
17476               section begin/end labels to generate the aranges...  */
17477           /*
17478             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
17479             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
17480             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
17481             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
17482
17483             add_pubname (decl, subr_die);
17484             add_arange (decl, subr_die);
17485             add_arange (decl, subr_die);
17486            */
17487         }
17488
17489 #ifdef MIPS_DEBUGGING_INFO
17490       /* Add a reference to the FDE for this routine.  */
17491       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
17492 #endif
17493
17494       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17495
17496       /* We define the "frame base" as the function's CFA.  This is more
17497          convenient for several reasons: (1) It's stable across the prologue
17498          and epilogue, which makes it better than just a frame pointer,
17499          (2) With dwarf3, there exists a one-byte encoding that allows us
17500          to reference the .debug_frame data by proxy, but failing that,
17501          (3) We can at least reuse the code inspection and interpretation
17502          code that determines the CFA position at various points in the
17503          function.  */
17504       if (dwarf_version >= 3)
17505         {
17506           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17507           add_AT_loc (subr_die, DW_AT_frame_base, op);
17508         }
17509       else
17510         {
17511           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17512           if (list->dw_loc_next)
17513             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17514           else
17515             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17516         }
17517
17518       /* Compute a displacement from the "steady-state frame pointer" to
17519          the CFA.  The former is what all stack slots and argument slots
17520          will reference in the rtl; the later is what we've told the
17521          debugger about.  We'll need to adjust all frame_base references
17522          by this displacement.  */
17523       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17524
17525       if (cfun->static_chain_decl)
17526         add_AT_location_description (subr_die, DW_AT_static_link,
17527                  loc_list_from_tree (cfun->static_chain_decl, 2));
17528     }
17529
17530   /* Generate child dies for template paramaters.  */
17531   if (debug_info_level > DINFO_LEVEL_TERSE)
17532     gen_generic_params_dies (decl);
17533
17534   /* Now output descriptions of the arguments for this function. This gets
17535      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17536      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17537      `...' at the end of the formal parameter list.  In order to find out if
17538      there was a trailing ellipsis or not, we must instead look at the type
17539      associated with the FUNCTION_DECL.  This will be a node of type
17540      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17541      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17542      an ellipsis at the end.  */
17543
17544   /* In the case where we are describing a mere function declaration, all we
17545      need to do here (and all we *can* do here) is to describe the *types* of
17546      its formal parameters.  */
17547   if (debug_info_level <= DINFO_LEVEL_TERSE)
17548     ;
17549   else if (declaration)
17550     gen_formal_types_die (decl, subr_die);
17551   else
17552     {
17553       /* Generate DIEs to represent all known formal parameters.  */
17554       tree parm = DECL_ARGUMENTS (decl);
17555       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17556       tree generic_decl_parm = generic_decl
17557                                 ? DECL_ARGUMENTS (generic_decl)
17558                                 : NULL;
17559
17560       /* Now we want to walk the list of parameters of the function and
17561          emit their relevant DIEs.
17562
17563          We consider the case of DECL being an instance of a generic function
17564          as well as it being a normal function.
17565
17566          If DECL is an instance of a generic function we walk the
17567          parameters of the generic function declaration _and_ the parameters of
17568          DECL itself. This is useful because we want to emit specific DIEs for
17569          function parameter packs and those are declared as part of the
17570          generic function declaration. In that particular case,
17571          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17572          That DIE has children DIEs representing the set of arguments
17573          of the pack. Note that the set of pack arguments can be empty.
17574          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17575          children DIE.
17576         
17577          Otherwise, we just consider the parameters of DECL.  */
17578       while (generic_decl_parm || parm)
17579         {
17580           if (generic_decl_parm
17581               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17582             gen_formal_parameter_pack_die (generic_decl_parm,
17583                                            parm, subr_die,
17584                                            &parm);
17585           else if (parm)
17586             {
17587               gen_decl_die (parm, NULL, subr_die);
17588               parm = TREE_CHAIN (parm);
17589             }
17590
17591           if (generic_decl_parm)
17592             generic_decl_parm = TREE_CHAIN (generic_decl_parm);
17593         }
17594
17595       /* Decide whether we need an unspecified_parameters DIE at the end.
17596          There are 2 more cases to do this for: 1) the ansi ... declaration -
17597          this is detectable when the end of the arg list is not a
17598          void_type_node 2) an unprototyped function declaration (not a
17599          definition).  This just means that we have no info about the
17600          parameters at all.  */
17601       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
17602       if (fn_arg_types != NULL)
17603         {
17604           /* This is the prototyped case, check for....  */
17605           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
17606             gen_unspecified_parameters_die (decl, subr_die);
17607         }
17608       else if (DECL_INITIAL (decl) == NULL_TREE)
17609         gen_unspecified_parameters_die (decl, subr_die);
17610     }
17611
17612   /* Output Dwarf info for all of the stuff within the body of the function
17613      (if it has one - it may be just a declaration).  */
17614   outer_scope = DECL_INITIAL (decl);
17615
17616   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17617      a function.  This BLOCK actually represents the outermost binding contour
17618      for the function, i.e. the contour in which the function's formal
17619      parameters and labels get declared. Curiously, it appears that the front
17620      end doesn't actually put the PARM_DECL nodes for the current function onto
17621      the BLOCK_VARS list for this outer scope, but are strung off of the
17622      DECL_ARGUMENTS list for the function instead.
17623
17624      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17625      the LABEL_DECL nodes for the function however, and we output DWARF info
17626      for those in decls_for_scope.  Just within the `outer_scope' there will be
17627      a BLOCK node representing the function's outermost pair of curly braces,
17628      and any blocks used for the base and member initializers of a C++
17629      constructor function.  */
17630   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17631     {
17632       /* Emit a DW_TAG_variable DIE for a named return value.  */
17633       if (DECL_NAME (DECL_RESULT (decl)))
17634         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17635
17636       current_function_has_inlines = 0;
17637       decls_for_scope (outer_scope, subr_die, 0);
17638
17639 #if 0 && defined (MIPS_DEBUGGING_INFO)
17640       if (current_function_has_inlines)
17641         {
17642           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
17643           if (! comp_unit_has_inlines)
17644             {
17645               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
17646               comp_unit_has_inlines = 1;
17647             }
17648         }
17649 #endif
17650     }
17651   /* Add the calling convention attribute if requested.  */
17652   add_calling_convention_attribute (subr_die, decl);
17653
17654 }
17655
17656 /* Returns a hash value for X (which really is a die_struct).  */
17657
17658 static hashval_t
17659 common_block_die_table_hash (const void *x)
17660 {
17661   const_dw_die_ref d = (const_dw_die_ref) x;
17662   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17663 }
17664
17665 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17666    as decl_id and die_parent of die_struct Y.  */
17667
17668 static int
17669 common_block_die_table_eq (const void *x, const void *y)
17670 {
17671   const_dw_die_ref d = (const_dw_die_ref) x;
17672   const_dw_die_ref e = (const_dw_die_ref) y;
17673   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17674 }
17675
17676 /* Generate a DIE to represent a declared data object.
17677    Either DECL or ORIGIN must be non-null.  */
17678
17679 static void
17680 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17681 {
17682   HOST_WIDE_INT off;
17683   tree com_decl;
17684   tree decl_or_origin = decl ? decl : origin;
17685   dw_die_ref var_die;
17686   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17687   dw_die_ref origin_die;
17688   int declaration = (DECL_EXTERNAL (decl_or_origin)
17689                      /* If DECL is COMDAT and has not actually been
17690                         emitted, we cannot take its address; there
17691                         might end up being no definition anywhere in
17692                         the program.  For example, consider the C++
17693                         test case:
17694
17695                           template <class T>
17696                           struct S { static const int i = 7; };
17697
17698                           template <class T>
17699                           const int S<T>::i;
17700
17701                           int f() { return S<int>::i; }
17702
17703                         Here, S<int>::i is not DECL_EXTERNAL, but no
17704                         definition is required, so the compiler will
17705                         not emit a definition.  */
17706                      || (TREE_CODE (decl_or_origin) == VAR_DECL
17707                          && DECL_COMDAT (decl_or_origin)
17708                          && !TREE_ASM_WRITTEN (decl_or_origin))
17709                      || class_or_namespace_scope_p (context_die));
17710
17711   if (!origin)
17712     origin = decl_ultimate_origin (decl);
17713
17714   com_decl = fortran_common (decl_or_origin, &off);
17715
17716   /* Symbol in common gets emitted as a child of the common block, in the form
17717      of a data member.  */
17718   if (com_decl)
17719     {
17720       tree field;
17721       dw_die_ref com_die;
17722       dw_loc_list_ref loc;
17723       die_node com_die_arg;
17724
17725       var_die = lookup_decl_die (decl_or_origin);
17726       if (var_die)
17727         {
17728           if (get_AT (var_die, DW_AT_location) == NULL)
17729             {
17730               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17731               if (loc)
17732                 {
17733                   if (off)
17734                     {
17735                       /* Optimize the common case.  */
17736                       if (single_element_loc_list_p (loc)
17737                           && loc->expr->dw_loc_opc == DW_OP_addr
17738                           && loc->expr->dw_loc_next == NULL
17739                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17740                              == SYMBOL_REF)
17741                         loc->expr->dw_loc_oprnd1.v.val_addr
17742                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17743                         else
17744                           loc_list_plus_const (loc, off);
17745                     }
17746                   add_AT_location_description (var_die, DW_AT_location, loc);
17747                   remove_AT (var_die, DW_AT_declaration);
17748                 }
17749             }
17750           return;
17751         }
17752
17753       if (common_block_die_table == NULL)
17754         common_block_die_table
17755           = htab_create_ggc (10, common_block_die_table_hash,
17756                              common_block_die_table_eq, NULL);
17757
17758       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
17759       com_die_arg.decl_id = DECL_UID (com_decl);
17760       com_die_arg.die_parent = context_die;
17761       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17762       loc = loc_list_from_tree (com_decl, 2);
17763       if (com_die == NULL)
17764         {
17765           const char *cnam
17766             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17767           void **slot;
17768
17769           com_die = new_die (DW_TAG_common_block, context_die, decl);
17770           add_name_and_src_coords_attributes (com_die, com_decl);
17771           if (loc)
17772             {
17773               add_AT_location_description (com_die, DW_AT_location, loc);
17774               /* Avoid sharing the same loc descriptor between
17775                  DW_TAG_common_block and DW_TAG_variable.  */
17776               loc = loc_list_from_tree (com_decl, 2);
17777             }
17778           else if (DECL_EXTERNAL (decl))
17779             add_AT_flag (com_die, DW_AT_declaration, 1);
17780           add_pubname_string (cnam, com_die); /* ??? needed? */
17781           com_die->decl_id = DECL_UID (com_decl);
17782           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17783           *slot = (void *) com_die;
17784         }
17785       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17786         {
17787           add_AT_location_description (com_die, DW_AT_location, loc);
17788           loc = loc_list_from_tree (com_decl, 2);
17789           remove_AT (com_die, DW_AT_declaration);
17790         }
17791       var_die = new_die (DW_TAG_variable, com_die, decl);
17792       add_name_and_src_coords_attributes (var_die, decl);
17793       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17794                           TREE_THIS_VOLATILE (decl), context_die);
17795       add_AT_flag (var_die, DW_AT_external, 1);
17796       if (loc)
17797         {
17798           if (off)
17799             {
17800               /* Optimize the common case.  */
17801               if (single_element_loc_list_p (loc)
17802                   && loc->expr->dw_loc_opc == DW_OP_addr
17803                   && loc->expr->dw_loc_next == NULL
17804                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17805                 loc->expr->dw_loc_oprnd1.v.val_addr
17806                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17807               else
17808                 loc_list_plus_const (loc, off);
17809             }
17810           add_AT_location_description (var_die, DW_AT_location, loc);
17811         }
17812       else if (DECL_EXTERNAL (decl))
17813         add_AT_flag (var_die, DW_AT_declaration, 1);
17814       equate_decl_number_to_die (decl, var_die);
17815       return;
17816     }
17817
17818   /* If the compiler emitted a definition for the DECL declaration
17819      and if we already emitted a DIE for it, don't emit a second
17820      DIE for it again.  */
17821   if (old_die
17822       && declaration
17823       && old_die->die_parent == context_die)
17824     return;
17825
17826   /* For static data members, the declaration in the class is supposed
17827      to have DW_TAG_member tag; the specification should still be
17828      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17829   if (declaration && class_scope_p (context_die))
17830     var_die = new_die (DW_TAG_member, context_die, decl);
17831   else
17832     var_die = new_die (DW_TAG_variable, context_die, decl);
17833
17834   origin_die = NULL;
17835   if (origin != NULL)
17836     origin_die = add_abstract_origin_attribute (var_die, origin);
17837
17838   /* Loop unrolling can create multiple blocks that refer to the same
17839      static variable, so we must test for the DW_AT_declaration flag.
17840
17841      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17842      copy decls and set the DECL_ABSTRACT flag on them instead of
17843      sharing them.
17844
17845      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17846
17847      ??? The declare_in_namespace support causes us to get two DIEs for one
17848      variable, both of which are declarations.  We want to avoid considering
17849      one to be a specification, so we must test that this DIE is not a
17850      declaration.  */
17851   else if (old_die && TREE_STATIC (decl) && ! declaration
17852            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17853     {
17854       /* This is a definition of a C++ class level static.  */
17855       add_AT_specification (var_die, old_die);
17856       if (DECL_NAME (decl))
17857         {
17858           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17859           struct dwarf_file_data * file_index = lookup_filename (s.file);
17860
17861           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17862             add_AT_file (var_die, DW_AT_decl_file, file_index);
17863
17864           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17865             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17866         }
17867     }
17868   else
17869     {
17870       tree type = TREE_TYPE (decl);
17871
17872       add_name_and_src_coords_attributes (var_die, decl);
17873       if (decl_by_reference_p (decl))
17874         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17875       else
17876         add_type_attribute (var_die, type, TREE_READONLY (decl),
17877                             TREE_THIS_VOLATILE (decl), context_die);
17878
17879       if (TREE_PUBLIC (decl))
17880         add_AT_flag (var_die, DW_AT_external, 1);
17881
17882       if (DECL_ARTIFICIAL (decl))
17883         add_AT_flag (var_die, DW_AT_artificial, 1);
17884
17885       if (TREE_PROTECTED (decl))
17886         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
17887       else if (TREE_PRIVATE (decl))
17888         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
17889     }
17890
17891   if (declaration)
17892     add_AT_flag (var_die, DW_AT_declaration, 1);
17893
17894   if (decl && (DECL_ABSTRACT (decl) || declaration))
17895     equate_decl_number_to_die (decl, var_die);
17896
17897   if (! declaration
17898       && (! DECL_ABSTRACT (decl_or_origin)
17899           /* Local static vars are shared between all clones/inlines,
17900              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17901              already set.  */
17902           || (TREE_CODE (decl_or_origin) == VAR_DECL
17903               && TREE_STATIC (decl_or_origin)
17904               && DECL_RTL_SET_P (decl_or_origin)))
17905       /* When abstract origin already has DW_AT_location attribute, no need
17906          to add it again.  */
17907       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17908     {
17909       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17910           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17911         defer_location (decl_or_origin, var_die);
17912       else
17913         add_location_or_const_value_attribute (var_die,
17914                                                decl_or_origin,
17915                                                DW_AT_location);
17916       add_pubname (decl_or_origin, var_die);
17917     }
17918   else
17919     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17920 }
17921
17922 /* Generate a DIE to represent a named constant.  */
17923
17924 static void
17925 gen_const_die (tree decl, dw_die_ref context_die)
17926 {
17927   dw_die_ref const_die;
17928   tree type = TREE_TYPE (decl);
17929
17930   const_die = new_die (DW_TAG_constant, context_die, decl);
17931   add_name_and_src_coords_attributes (const_die, decl);
17932   add_type_attribute (const_die, type, 1, 0, context_die);
17933   if (TREE_PUBLIC (decl))
17934     add_AT_flag (const_die, DW_AT_external, 1);
17935   if (DECL_ARTIFICIAL (decl))
17936     add_AT_flag (const_die, DW_AT_artificial, 1);
17937   tree_add_const_value_attribute_for_decl (const_die, decl);
17938 }
17939
17940 /* Generate a DIE to represent a label identifier.  */
17941
17942 static void
17943 gen_label_die (tree decl, dw_die_ref context_die)
17944 {
17945   tree origin = decl_ultimate_origin (decl);
17946   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17947   rtx insn;
17948   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17949
17950   if (origin != NULL)
17951     add_abstract_origin_attribute (lbl_die, origin);
17952   else
17953     add_name_and_src_coords_attributes (lbl_die, decl);
17954
17955   if (DECL_ABSTRACT (decl))
17956     equate_decl_number_to_die (decl, lbl_die);
17957   else
17958     {
17959       insn = DECL_RTL_IF_SET (decl);
17960
17961       /* Deleted labels are programmer specified labels which have been
17962          eliminated because of various optimizations.  We still emit them
17963          here so that it is possible to put breakpoints on them.  */
17964       if (insn
17965           && (LABEL_P (insn)
17966               || ((NOTE_P (insn)
17967                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17968         {
17969           /* When optimization is enabled (via -O) some parts of the compiler
17970              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17971              represent source-level labels which were explicitly declared by
17972              the user.  This really shouldn't be happening though, so catch
17973              it if it ever does happen.  */
17974           gcc_assert (!INSN_DELETED_P (insn));
17975
17976           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17977           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17978         }
17979     }
17980 }
17981
17982 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
17983    attributes to the DIE for a block STMT, to describe where the inlined
17984    function was called from.  This is similar to add_src_coords_attributes.  */
17985
17986 static inline void
17987 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17988 {
17989   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17990
17991   if (dwarf_version >= 3 || !dwarf_strict)
17992     {
17993       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17994       add_AT_unsigned (die, DW_AT_call_line, s.line);
17995     }
17996 }
17997
17998
17999 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18000    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18001
18002 static inline void
18003 add_high_low_attributes (tree stmt, dw_die_ref die)
18004 {
18005   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18006
18007   if (BLOCK_FRAGMENT_CHAIN (stmt)
18008       && (dwarf_version >= 3 || !dwarf_strict))
18009     {
18010       tree chain;
18011
18012       if (inlined_function_outer_scope_p (stmt))
18013         {
18014           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18015                                        BLOCK_NUMBER (stmt));
18016           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18017         }
18018
18019       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18020
18021       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18022       do
18023         {
18024           add_ranges (chain);
18025           chain = BLOCK_FRAGMENT_CHAIN (chain);
18026         }
18027       while (chain);
18028       add_ranges (NULL);
18029     }
18030   else
18031     {
18032       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18033                                    BLOCK_NUMBER (stmt));
18034       add_AT_lbl_id (die, DW_AT_low_pc, label);
18035       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18036                                    BLOCK_NUMBER (stmt));
18037       add_AT_lbl_id (die, DW_AT_high_pc, label);
18038     }
18039 }
18040
18041 /* Generate a DIE for a lexical block.  */
18042
18043 static void
18044 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18045 {
18046   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18047
18048   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18049     add_high_low_attributes (stmt, stmt_die);
18050
18051   decls_for_scope (stmt, stmt_die, depth);
18052 }
18053
18054 /* Generate a DIE for an inlined subprogram.  */
18055
18056 static void
18057 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18058 {
18059   tree decl;
18060
18061   /* The instance of function that is effectively being inlined shall not
18062      be abstract.  */
18063   gcc_assert (! BLOCK_ABSTRACT (stmt));
18064
18065   decl = block_ultimate_origin (stmt);
18066
18067   /* Emit info for the abstract instance first, if we haven't yet.  We
18068      must emit this even if the block is abstract, otherwise when we
18069      emit the block below (or elsewhere), we may end up trying to emit
18070      a die whose origin die hasn't been emitted, and crashing.  */
18071   dwarf2out_abstract_function (decl);
18072
18073   if (! BLOCK_ABSTRACT (stmt))
18074     {
18075       dw_die_ref subr_die
18076         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18077
18078       add_abstract_origin_attribute (subr_die, decl);
18079       if (TREE_ASM_WRITTEN (stmt))
18080         add_high_low_attributes (stmt, subr_die);
18081       add_call_src_coords_attributes (stmt, subr_die);
18082
18083       decls_for_scope (stmt, subr_die, depth);
18084       current_function_has_inlines = 1;
18085     }
18086 }
18087
18088 /* Generate a DIE for a field in a record, or structure.  */
18089
18090 static void
18091 gen_field_die (tree decl, dw_die_ref context_die)
18092 {
18093   dw_die_ref decl_die;
18094
18095   if (TREE_TYPE (decl) == error_mark_node)
18096     return;
18097
18098   decl_die = new_die (DW_TAG_member, context_die, decl);
18099   add_name_and_src_coords_attributes (decl_die, decl);
18100   add_type_attribute (decl_die, member_declared_type (decl),
18101                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18102                       context_die);
18103
18104   if (DECL_BIT_FIELD_TYPE (decl))
18105     {
18106       add_byte_size_attribute (decl_die, decl);
18107       add_bit_size_attribute (decl_die, decl);
18108       add_bit_offset_attribute (decl_die, decl);
18109     }
18110
18111   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18112     add_data_member_location_attribute (decl_die, decl);
18113
18114   if (DECL_ARTIFICIAL (decl))
18115     add_AT_flag (decl_die, DW_AT_artificial, 1);
18116
18117   if (TREE_PROTECTED (decl))
18118     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
18119   else if (TREE_PRIVATE (decl))
18120     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
18121
18122   /* Equate decl number to die, so that we can look up this decl later on.  */
18123   equate_decl_number_to_die (decl, decl_die);
18124 }
18125
18126 #if 0
18127 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18128    Use modified_type_die instead.
18129    We keep this code here just in case these types of DIEs may be needed to
18130    represent certain things in other languages (e.g. Pascal) someday.  */
18131
18132 static void
18133 gen_pointer_type_die (tree type, dw_die_ref context_die)
18134 {
18135   dw_die_ref ptr_die
18136     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18137
18138   equate_type_number_to_die (type, ptr_die);
18139   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18140   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18141 }
18142
18143 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18144    Use modified_type_die instead.
18145    We keep this code here just in case these types of DIEs may be needed to
18146    represent certain things in other languages (e.g. Pascal) someday.  */
18147
18148 static void
18149 gen_reference_type_die (tree type, dw_die_ref context_die)
18150 {
18151   dw_die_ref ref_die
18152     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
18153
18154   equate_type_number_to_die (type, ref_die);
18155   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18156   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18157 }
18158 #endif
18159
18160 /* Generate a DIE for a pointer to a member type.  */
18161
18162 static void
18163 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18164 {
18165   dw_die_ref ptr_die
18166     = new_die (DW_TAG_ptr_to_member_type,
18167                scope_die_for (type, context_die), type);
18168
18169   equate_type_number_to_die (type, ptr_die);
18170   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18171                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18172   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18173 }
18174
18175 /* Generate the DIE for the compilation unit.  */
18176
18177 static dw_die_ref
18178 gen_compile_unit_die (const char *filename)
18179 {
18180   dw_die_ref die;
18181   char producer[250];
18182   const char *language_string = lang_hooks.name;
18183   int language;
18184
18185   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18186
18187   if (filename)
18188     {
18189       add_name_attribute (die, filename);
18190       /* Don't add cwd for <built-in>.  */
18191       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18192         add_comp_dir_attribute (die);
18193     }
18194
18195   sprintf (producer, "%s %s", language_string, version_string);
18196
18197 #ifdef MIPS_DEBUGGING_INFO
18198   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
18199      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
18200      not appear in the producer string, the debugger reaches the conclusion
18201      that the object file is stripped and has no debugging information.
18202      To get the MIPS/SGI debugger to believe that there is debugging
18203      information in the object file, we add a -g to the producer string.  */
18204   if (debug_info_level > DINFO_LEVEL_TERSE)
18205     strcat (producer, " -g");
18206 #endif
18207
18208   add_AT_string (die, DW_AT_producer, producer);
18209
18210   language = DW_LANG_C89;
18211   if (strcmp (language_string, "GNU C++") == 0)
18212     language = DW_LANG_C_plus_plus;
18213   else if (strcmp (language_string, "GNU F77") == 0)
18214     language = DW_LANG_Fortran77;
18215   else if (strcmp (language_string, "GNU Pascal") == 0)
18216     language = DW_LANG_Pascal83;
18217   else if (dwarf_version >= 3 || !dwarf_strict)
18218     {
18219       if (strcmp (language_string, "GNU Ada") == 0)
18220         language = DW_LANG_Ada95;
18221       else if (strcmp (language_string, "GNU Fortran") == 0)
18222         language = DW_LANG_Fortran95;
18223       else if (strcmp (language_string, "GNU Java") == 0)
18224         language = DW_LANG_Java;
18225       else if (strcmp (language_string, "GNU Objective-C") == 0)
18226         language = DW_LANG_ObjC;
18227       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18228         language = DW_LANG_ObjC_plus_plus;
18229     }
18230
18231   add_AT_unsigned (die, DW_AT_language, language);
18232   return die;
18233 }
18234
18235 /* Generate the DIE for a base class.  */
18236
18237 static void
18238 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18239 {
18240   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18241
18242   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18243   add_data_member_location_attribute (die, binfo);
18244
18245   if (BINFO_VIRTUAL_P (binfo))
18246     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18247
18248   if (access == access_public_node)
18249     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18250   else if (access == access_protected_node)
18251     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18252 }
18253
18254 /* Generate a DIE for a class member.  */
18255
18256 static void
18257 gen_member_die (tree type, dw_die_ref context_die)
18258 {
18259   tree member;
18260   tree binfo = TYPE_BINFO (type);
18261   dw_die_ref child;
18262
18263   /* If this is not an incomplete type, output descriptions of each of its
18264      members. Note that as we output the DIEs necessary to represent the
18265      members of this record or union type, we will also be trying to output
18266      DIEs to represent the *types* of those members. However the `type'
18267      function (above) will specifically avoid generating type DIEs for member
18268      types *within* the list of member DIEs for this (containing) type except
18269      for those types (of members) which are explicitly marked as also being
18270      members of this (containing) type themselves.  The g++ front- end can
18271      force any given type to be treated as a member of some other (containing)
18272      type by setting the TYPE_CONTEXT of the given (member) type to point to
18273      the TREE node representing the appropriate (containing) type.  */
18274
18275   /* First output info about the base classes.  */
18276   if (binfo)
18277     {
18278       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18279       int i;
18280       tree base;
18281
18282       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18283         gen_inheritance_die (base,
18284                              (accesses ? VEC_index (tree, accesses, i)
18285                               : access_public_node), context_die);
18286     }
18287
18288   /* Now output info about the data members and type members.  */
18289   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
18290     {
18291       /* If we thought we were generating minimal debug info for TYPE
18292          and then changed our minds, some of the member declarations
18293          may have already been defined.  Don't define them again, but
18294          do put them in the right order.  */
18295
18296       child = lookup_decl_die (member);
18297       if (child)
18298         splice_child_die (context_die, child);
18299       else
18300         gen_decl_die (member, NULL, context_die);
18301     }
18302
18303   /* Now output info about the function members (if any).  */
18304   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
18305     {
18306       /* Don't include clones in the member list.  */
18307       if (DECL_ABSTRACT_ORIGIN (member))
18308         continue;
18309
18310       child = lookup_decl_die (member);
18311       if (child)
18312         splice_child_die (context_die, child);
18313       else
18314         gen_decl_die (member, NULL, context_die);
18315     }
18316 }
18317
18318 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18319    is set, we pretend that the type was never defined, so we only get the
18320    member DIEs needed by later specification DIEs.  */
18321
18322 static void
18323 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18324                                 enum debug_info_usage usage)
18325 {
18326   dw_die_ref type_die = lookup_type_die (type);
18327   dw_die_ref scope_die = 0;
18328   int nested = 0;
18329   int complete = (TYPE_SIZE (type)
18330                   && (! TYPE_STUB_DECL (type)
18331                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18332   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18333   complete = complete && should_emit_struct_debug (type, usage);
18334
18335   if (type_die && ! complete)
18336     return;
18337
18338   if (TYPE_CONTEXT (type) != NULL_TREE
18339       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18340           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18341     nested = 1;
18342
18343   scope_die = scope_die_for (type, context_die);
18344
18345   if (! type_die || (nested && scope_die == comp_unit_die))
18346     /* First occurrence of type or toplevel definition of nested class.  */
18347     {
18348       dw_die_ref old_die = type_die;
18349
18350       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18351                           ? record_type_tag (type) : DW_TAG_union_type,
18352                           scope_die, type);
18353       equate_type_number_to_die (type, type_die);
18354       if (old_die)
18355         add_AT_specification (type_die, old_die);
18356       else
18357         add_name_attribute (type_die, type_tag (type));
18358     }
18359   else
18360     remove_AT (type_die, DW_AT_declaration);
18361
18362   /* Generate child dies for template paramaters.  */
18363   if (debug_info_level > DINFO_LEVEL_TERSE
18364       && COMPLETE_TYPE_P (type))
18365     gen_generic_params_dies (type);
18366
18367   /* If this type has been completed, then give it a byte_size attribute and
18368      then give a list of members.  */
18369   if (complete && !ns_decl)
18370     {
18371       /* Prevent infinite recursion in cases where the type of some member of
18372          this type is expressed in terms of this type itself.  */
18373       TREE_ASM_WRITTEN (type) = 1;
18374       add_byte_size_attribute (type_die, type);
18375       if (TYPE_STUB_DECL (type) != NULL_TREE)
18376         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18377
18378       /* If the first reference to this type was as the return type of an
18379          inline function, then it may not have a parent.  Fix this now.  */
18380       if (type_die->die_parent == NULL)
18381         add_child_die (scope_die, type_die);
18382
18383       push_decl_scope (type);
18384       gen_member_die (type, type_die);
18385       pop_decl_scope ();
18386
18387       /* GNU extension: Record what type our vtable lives in.  */
18388       if (TYPE_VFIELD (type))
18389         {
18390           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18391
18392           gen_type_die (vtype, context_die);
18393           add_AT_die_ref (type_die, DW_AT_containing_type,
18394                           lookup_type_die (vtype));
18395         }
18396     }
18397   else
18398     {
18399       add_AT_flag (type_die, DW_AT_declaration, 1);
18400
18401       /* We don't need to do this for function-local types.  */
18402       if (TYPE_STUB_DECL (type)
18403           && ! decl_function_context (TYPE_STUB_DECL (type)))
18404         VEC_safe_push (tree, gc, incomplete_types, type);
18405     }
18406
18407   if (get_AT (type_die, DW_AT_name))
18408     add_pubtype (type, type_die);
18409 }
18410
18411 /* Generate a DIE for a subroutine _type_.  */
18412
18413 static void
18414 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18415 {
18416   tree return_type = TREE_TYPE (type);
18417   dw_die_ref subr_die
18418     = new_die (DW_TAG_subroutine_type,
18419                scope_die_for (type, context_die), type);
18420
18421   equate_type_number_to_die (type, subr_die);
18422   add_prototyped_attribute (subr_die, type);
18423   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18424   gen_formal_types_die (type, subr_die);
18425
18426   if (get_AT (subr_die, DW_AT_name))
18427     add_pubtype (type, subr_die);
18428 }
18429
18430 /* Generate a DIE for a type definition.  */
18431
18432 static void
18433 gen_typedef_die (tree decl, dw_die_ref context_die)
18434 {
18435   dw_die_ref type_die;
18436   tree origin;
18437
18438   if (TREE_ASM_WRITTEN (decl))
18439     return;
18440
18441   TREE_ASM_WRITTEN (decl) = 1;
18442   type_die = new_die (DW_TAG_typedef, context_die, decl);
18443   origin = decl_ultimate_origin (decl);
18444   if (origin != NULL)
18445     add_abstract_origin_attribute (type_die, origin);
18446   else
18447     {
18448       tree type;
18449
18450       add_name_and_src_coords_attributes (type_die, decl);
18451       if (DECL_ORIGINAL_TYPE (decl))
18452         {
18453           type = DECL_ORIGINAL_TYPE (decl);
18454
18455           gcc_assert (type != TREE_TYPE (decl));
18456           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18457         }
18458       else
18459         type = TREE_TYPE (decl);
18460
18461       add_type_attribute (type_die, type, TREE_READONLY (decl),
18462                           TREE_THIS_VOLATILE (decl), context_die);
18463     }
18464
18465   if (DECL_ABSTRACT (decl))
18466     equate_decl_number_to_die (decl, type_die);
18467
18468   if (get_AT (type_die, DW_AT_name))
18469     add_pubtype (decl, type_die);
18470 }
18471
18472 /* Generate a type description DIE.  */
18473
18474 static void
18475 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18476                                 enum debug_info_usage usage)
18477 {
18478   int need_pop;
18479   struct array_descr_info info;
18480
18481   if (type == NULL_TREE || type == error_mark_node)
18482     return;
18483
18484   /* If TYPE is a typedef type variant, let's generate debug info
18485      for the parent typedef which TYPE is a type of.  */
18486   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18487       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18488     {
18489       if (TREE_ASM_WRITTEN (type))
18490         return;
18491
18492       /* Prevent broken recursion; we can't hand off to the same type.  */
18493       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18494
18495       /* Use the DIE of the containing namespace as the parent DIE of
18496          the type description DIE we want to generate.  */
18497       if (DECL_CONTEXT (TYPE_NAME (type))
18498           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18499         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18500
18501       TREE_ASM_WRITTEN (type) = 1;
18502       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18503       return;
18504     }
18505
18506   /* If this is an array type with hidden descriptor, handle it first.  */
18507   if (!TREE_ASM_WRITTEN (type)
18508       && lang_hooks.types.get_array_descr_info
18509       && lang_hooks.types.get_array_descr_info (type, &info)
18510       && (dwarf_version >= 3 || !dwarf_strict))
18511     {
18512       gen_descr_array_type_die (type, &info, context_die);
18513       TREE_ASM_WRITTEN (type) = 1;
18514       return;
18515     }
18516
18517   /* We are going to output a DIE to represent the unqualified version
18518      of this type (i.e. without any const or volatile qualifiers) so
18519      get the main variant (i.e. the unqualified version) of this type
18520      now.  (Vectors are special because the debugging info is in the
18521      cloned type itself).  */
18522   if (TREE_CODE (type) != VECTOR_TYPE)
18523     type = type_main_variant (type);
18524
18525   if (TREE_ASM_WRITTEN (type))
18526     return;
18527
18528   switch (TREE_CODE (type))
18529     {
18530     case ERROR_MARK:
18531       break;
18532
18533     case POINTER_TYPE:
18534     case REFERENCE_TYPE:
18535       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18536          ensures that the gen_type_die recursion will terminate even if the
18537          type is recursive.  Recursive types are possible in Ada.  */
18538       /* ??? We could perhaps do this for all types before the switch
18539          statement.  */
18540       TREE_ASM_WRITTEN (type) = 1;
18541
18542       /* For these types, all that is required is that we output a DIE (or a
18543          set of DIEs) to represent the "basis" type.  */
18544       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18545                                 DINFO_USAGE_IND_USE);
18546       break;
18547
18548     case OFFSET_TYPE:
18549       /* This code is used for C++ pointer-to-data-member types.
18550          Output a description of the relevant class type.  */
18551       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18552                                         DINFO_USAGE_IND_USE);
18553
18554       /* Output a description of the type of the object pointed to.  */
18555       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18556                                         DINFO_USAGE_IND_USE);
18557
18558       /* Now output a DIE to represent this pointer-to-data-member type
18559          itself.  */
18560       gen_ptr_to_mbr_type_die (type, context_die);
18561       break;
18562
18563     case FUNCTION_TYPE:
18564       /* Force out return type (in case it wasn't forced out already).  */
18565       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18566                                         DINFO_USAGE_DIR_USE);
18567       gen_subroutine_type_die (type, context_die);
18568       break;
18569
18570     case METHOD_TYPE:
18571       /* Force out return type (in case it wasn't forced out already).  */
18572       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18573                                         DINFO_USAGE_DIR_USE);
18574       gen_subroutine_type_die (type, context_die);
18575       break;
18576
18577     case ARRAY_TYPE:
18578       gen_array_type_die (type, context_die);
18579       break;
18580
18581     case VECTOR_TYPE:
18582       gen_array_type_die (type, context_die);
18583       break;
18584
18585     case ENUMERAL_TYPE:
18586     case RECORD_TYPE:
18587     case UNION_TYPE:
18588     case QUAL_UNION_TYPE:
18589       /* If this is a nested type whose containing class hasn't been written
18590          out yet, writing it out will cover this one, too.  This does not apply
18591          to instantiations of member class templates; they need to be added to
18592          the containing class as they are generated.  FIXME: This hurts the
18593          idea of combining type decls from multiple TUs, since we can't predict
18594          what set of template instantiations we'll get.  */
18595       if (TYPE_CONTEXT (type)
18596           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18597           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18598         {
18599           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18600
18601           if (TREE_ASM_WRITTEN (type))
18602             return;
18603
18604           /* If that failed, attach ourselves to the stub.  */
18605           push_decl_scope (TYPE_CONTEXT (type));
18606           context_die = lookup_type_die (TYPE_CONTEXT (type));
18607           need_pop = 1;
18608         }
18609       else if (TYPE_CONTEXT (type) != NULL_TREE
18610                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18611         {
18612           /* If this type is local to a function that hasn't been written
18613              out yet, use a NULL context for now; it will be fixed up in
18614              decls_for_scope.  */
18615           context_die = lookup_decl_die (TYPE_CONTEXT (type));
18616           need_pop = 0;
18617         }
18618       else
18619         {
18620           context_die = declare_in_namespace (type, context_die);
18621           need_pop = 0;
18622         }
18623
18624       if (TREE_CODE (type) == ENUMERAL_TYPE)
18625         {
18626           /* This might have been written out by the call to
18627              declare_in_namespace.  */
18628           if (!TREE_ASM_WRITTEN (type))
18629             gen_enumeration_type_die (type, context_die);
18630         }
18631       else
18632         gen_struct_or_union_type_die (type, context_die, usage);
18633
18634       if (need_pop)
18635         pop_decl_scope ();
18636
18637       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18638          it up if it is ever completed.  gen_*_type_die will set it for us
18639          when appropriate.  */
18640       return;
18641
18642     case VOID_TYPE:
18643     case INTEGER_TYPE:
18644     case REAL_TYPE:
18645     case FIXED_POINT_TYPE:
18646     case COMPLEX_TYPE:
18647     case BOOLEAN_TYPE:
18648       /* No DIEs needed for fundamental types.  */
18649       break;
18650
18651     case LANG_TYPE:
18652       /* No Dwarf representation currently defined.  */
18653       break;
18654
18655     default:
18656       gcc_unreachable ();
18657     }
18658
18659   TREE_ASM_WRITTEN (type) = 1;
18660 }
18661
18662 static void
18663 gen_type_die (tree type, dw_die_ref context_die)
18664 {
18665   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18666 }
18667
18668 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18669    things which are local to the given block.  */
18670
18671 static void
18672 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18673 {
18674   int must_output_die = 0;
18675   bool inlined_func;
18676
18677   /* Ignore blocks that are NULL.  */
18678   if (stmt == NULL_TREE)
18679     return;
18680
18681   inlined_func = inlined_function_outer_scope_p (stmt);
18682
18683   /* If the block is one fragment of a non-contiguous block, do not
18684      process the variables, since they will have been done by the
18685      origin block.  Do process subblocks.  */
18686   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18687     {
18688       tree sub;
18689
18690       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18691         gen_block_die (sub, context_die, depth + 1);
18692
18693       return;
18694     }
18695
18696   /* Determine if we need to output any Dwarf DIEs at all to represent this
18697      block.  */
18698   if (inlined_func)
18699     /* The outer scopes for inlinings *must* always be represented.  We
18700        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
18701     must_output_die = 1;
18702   else
18703     {
18704       /* Determine if this block directly contains any "significant"
18705          local declarations which we will need to output DIEs for.  */
18706       if (debug_info_level > DINFO_LEVEL_TERSE)
18707         /* We are not in terse mode so *any* local declaration counts
18708            as being a "significant" one.  */
18709         must_output_die = ((BLOCK_VARS (stmt) != NULL
18710                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18711                            && (TREE_USED (stmt)
18712                                || TREE_ASM_WRITTEN (stmt)
18713                                || BLOCK_ABSTRACT (stmt)));
18714       else if ((TREE_USED (stmt)
18715                 || TREE_ASM_WRITTEN (stmt)
18716                 || BLOCK_ABSTRACT (stmt))
18717                && !dwarf2out_ignore_block (stmt))
18718         must_output_die = 1;
18719     }
18720
18721   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18722      DIE for any block which contains no significant local declarations at
18723      all.  Rather, in such cases we just call `decls_for_scope' so that any
18724      needed Dwarf info for any sub-blocks will get properly generated. Note
18725      that in terse mode, our definition of what constitutes a "significant"
18726      local declaration gets restricted to include only inlined function
18727      instances and local (nested) function definitions.  */
18728   if (must_output_die)
18729     {
18730       if (inlined_func)
18731         {
18732           /* If STMT block is abstract, that means we have been called
18733              indirectly from dwarf2out_abstract_function.
18734              That function rightfully marks the descendent blocks (of
18735              the abstract function it is dealing with) as being abstract,
18736              precisely to prevent us from emitting any
18737              DW_TAG_inlined_subroutine DIE as a descendent
18738              of an abstract function instance. So in that case, we should
18739              not call gen_inlined_subroutine_die.
18740
18741              Later though, when cgraph asks dwarf2out to emit info
18742              for the concrete instance of the function decl into which
18743              the concrete instance of STMT got inlined, the later will lead
18744              to the generation of a DW_TAG_inlined_subroutine DIE.  */
18745           if (! BLOCK_ABSTRACT (stmt))
18746             gen_inlined_subroutine_die (stmt, context_die, depth);
18747         }
18748       else
18749         gen_lexical_block_die (stmt, context_die, depth);
18750     }
18751   else
18752     decls_for_scope (stmt, context_die, depth);
18753 }
18754
18755 /* Process variable DECL (or variable with origin ORIGIN) within
18756    block STMT and add it to CONTEXT_DIE.  */
18757 static void
18758 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18759 {
18760   dw_die_ref die;
18761   tree decl_or_origin = decl ? decl : origin;
18762   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
18763
18764   if (ultimate_origin)
18765     origin = ultimate_origin;
18766
18767   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18768     die = lookup_decl_die (decl_or_origin);
18769   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18770            && TYPE_DECL_IS_STUB (decl_or_origin))
18771     die = lookup_type_die (TREE_TYPE (decl_or_origin));
18772   else
18773     die = NULL;
18774
18775   if (die != NULL && die->die_parent == NULL)
18776     add_child_die (context_die, die);
18777   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18778     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18779                                          stmt, context_die);
18780   else
18781     gen_decl_die (decl, origin, context_die);
18782 }
18783
18784 /* Generate all of the decls declared within a given scope and (recursively)
18785    all of its sub-blocks.  */
18786
18787 static void
18788 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18789 {
18790   tree decl;
18791   unsigned int i;
18792   tree subblocks;
18793
18794   /* Ignore NULL blocks.  */
18795   if (stmt == NULL_TREE)
18796     return;
18797
18798   /* Output the DIEs to represent all of the data objects and typedefs
18799      declared directly within this block but not within any nested
18800      sub-blocks.  Also, nested function and tag DIEs have been
18801      generated with a parent of NULL; fix that up now.  */
18802   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
18803     process_scope_var (stmt, decl, NULL_TREE, context_die);
18804   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18805     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
18806                        context_die);
18807
18808   /* If we're at -g1, we're not interested in subblocks.  */
18809   if (debug_info_level <= DINFO_LEVEL_TERSE)
18810     return;
18811
18812   /* Output the DIEs to represent all sub-blocks (and the items declared
18813      therein) of this block.  */
18814   for (subblocks = BLOCK_SUBBLOCKS (stmt);
18815        subblocks != NULL;
18816        subblocks = BLOCK_CHAIN (subblocks))
18817     gen_block_die (subblocks, context_die, depth + 1);
18818 }
18819
18820 /* Is this a typedef we can avoid emitting?  */
18821
18822 static inline int
18823 is_redundant_typedef (const_tree decl)
18824 {
18825   if (TYPE_DECL_IS_STUB (decl))
18826     return 1;
18827
18828   if (DECL_ARTIFICIAL (decl)
18829       && DECL_CONTEXT (decl)
18830       && is_tagged_type (DECL_CONTEXT (decl))
18831       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
18832       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
18833     /* Also ignore the artificial member typedef for the class name.  */
18834     return 1;
18835
18836   return 0;
18837 }
18838
18839 /* Returns the DIE for a context.  */
18840
18841 static inline dw_die_ref
18842 get_context_die (tree context)
18843 {
18844   if (context)
18845     {
18846       /* Find die that represents this context.  */
18847       if (TYPE_P (context))
18848         return force_type_die (context);
18849       else
18850         return force_decl_die (context);
18851     }
18852   return comp_unit_die;
18853 }
18854
18855 /* Returns the DIE for decl.  A DIE will always be returned.  */
18856
18857 static dw_die_ref
18858 force_decl_die (tree decl)
18859 {
18860   dw_die_ref decl_die;
18861   unsigned saved_external_flag;
18862   tree save_fn = NULL_TREE;
18863   decl_die = lookup_decl_die (decl);
18864   if (!decl_die)
18865     {
18866       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
18867
18868       decl_die = lookup_decl_die (decl);
18869       if (decl_die)
18870         return decl_die;
18871
18872       switch (TREE_CODE (decl))
18873         {
18874         case FUNCTION_DECL:
18875           /* Clear current_function_decl, so that gen_subprogram_die thinks
18876              that this is a declaration. At this point, we just want to force
18877              declaration die.  */
18878           save_fn = current_function_decl;
18879           current_function_decl = NULL_TREE;
18880           gen_subprogram_die (decl, context_die);
18881           current_function_decl = save_fn;
18882           break;
18883
18884         case VAR_DECL:
18885           /* Set external flag to force declaration die. Restore it after
18886            gen_decl_die() call.  */
18887           saved_external_flag = DECL_EXTERNAL (decl);
18888           DECL_EXTERNAL (decl) = 1;
18889           gen_decl_die (decl, NULL, context_die);
18890           DECL_EXTERNAL (decl) = saved_external_flag;
18891           break;
18892
18893         case NAMESPACE_DECL:
18894           if (dwarf_version >= 3 || !dwarf_strict)
18895             dwarf2out_decl (decl);
18896           else
18897             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
18898             decl_die = comp_unit_die;
18899           break;
18900
18901         default:
18902           gcc_unreachable ();
18903         }
18904
18905       /* We should be able to find the DIE now.  */
18906       if (!decl_die)
18907         decl_die = lookup_decl_die (decl);
18908       gcc_assert (decl_die);
18909     }
18910
18911   return decl_die;
18912 }
18913
18914 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
18915    always returned.  */
18916
18917 static dw_die_ref
18918 force_type_die (tree type)
18919 {
18920   dw_die_ref type_die;
18921
18922   type_die = lookup_type_die (type);
18923   if (!type_die)
18924     {
18925       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
18926
18927       type_die = modified_type_die (type, TYPE_READONLY (type),
18928                                     TYPE_VOLATILE (type), context_die);
18929       gcc_assert (type_die);
18930     }
18931   return type_die;
18932 }
18933
18934 /* Force out any required namespaces to be able to output DECL,
18935    and return the new context_die for it, if it's changed.  */
18936
18937 static dw_die_ref
18938 setup_namespace_context (tree thing, dw_die_ref context_die)
18939 {
18940   tree context = (DECL_P (thing)
18941                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
18942   if (context && TREE_CODE (context) == NAMESPACE_DECL)
18943     /* Force out the namespace.  */
18944     context_die = force_decl_die (context);
18945
18946   return context_die;
18947 }
18948
18949 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
18950    type) within its namespace, if appropriate.
18951
18952    For compatibility with older debuggers, namespace DIEs only contain
18953    declarations; all definitions are emitted at CU scope.  */
18954
18955 static dw_die_ref
18956 declare_in_namespace (tree thing, dw_die_ref context_die)
18957 {
18958   dw_die_ref ns_context;
18959
18960   if (debug_info_level <= DINFO_LEVEL_TERSE)
18961     return context_die;
18962
18963   /* If this decl is from an inlined function, then don't try to emit it in its
18964      namespace, as we will get confused.  It would have already been emitted
18965      when the abstract instance of the inline function was emitted anyways.  */
18966   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
18967     return context_die;
18968
18969   ns_context = setup_namespace_context (thing, context_die);
18970
18971   if (ns_context != context_die)
18972     {
18973       if (is_fortran ())
18974         return ns_context;
18975       if (DECL_P (thing))
18976         gen_decl_die (thing, NULL, ns_context);
18977       else
18978         gen_type_die (thing, ns_context);
18979     }
18980   return context_die;
18981 }
18982
18983 /* Generate a DIE for a namespace or namespace alias.  */
18984
18985 static void
18986 gen_namespace_die (tree decl, dw_die_ref context_die)
18987 {
18988   dw_die_ref namespace_die;
18989
18990   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
18991      they are an alias of.  */
18992   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
18993     {
18994       /* Output a real namespace or module.  */
18995       context_die = setup_namespace_context (decl, comp_unit_die);
18996       namespace_die = new_die (is_fortran ()
18997                                ? DW_TAG_module : DW_TAG_namespace,
18998                                context_die, decl);
18999       /* For Fortran modules defined in different CU don't add src coords.  */
19000       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19001         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
19002       else
19003         add_name_and_src_coords_attributes (namespace_die, decl);
19004       if (DECL_EXTERNAL (decl))
19005         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19006       equate_decl_number_to_die (decl, namespace_die);
19007     }
19008   else
19009     {
19010       /* Output a namespace alias.  */
19011
19012       /* Force out the namespace we are an alias of, if necessary.  */
19013       dw_die_ref origin_die
19014         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19015
19016       if (DECL_CONTEXT (decl) == NULL_TREE
19017           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19018         context_die = setup_namespace_context (decl, comp_unit_die);
19019       /* Now create the namespace alias DIE.  */
19020       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19021       add_name_and_src_coords_attributes (namespace_die, decl);
19022       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19023       equate_decl_number_to_die (decl, namespace_die);
19024     }
19025 }
19026
19027 /* Generate Dwarf debug information for a decl described by DECL.  */
19028
19029 static void
19030 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19031 {
19032   tree decl_or_origin = decl ? decl : origin;
19033   tree class_origin = NULL;
19034
19035   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19036     return;
19037
19038   switch (TREE_CODE (decl_or_origin))
19039     {
19040     case ERROR_MARK:
19041       break;
19042
19043     case CONST_DECL:
19044       if (!is_fortran ())
19045         {
19046           /* The individual enumerators of an enum type get output when we output
19047              the Dwarf representation of the relevant enum type itself.  */
19048           break;
19049         }
19050
19051       /* Emit its type.  */
19052       gen_type_die (TREE_TYPE (decl), context_die);
19053
19054       /* And its containing namespace.  */
19055       context_die = declare_in_namespace (decl, context_die);
19056
19057       gen_const_die (decl, context_die);
19058       break;
19059
19060     case FUNCTION_DECL:
19061       /* Don't output any DIEs to represent mere function declarations,
19062          unless they are class members or explicit block externs.  */
19063       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19064           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
19065           && (current_function_decl == NULL_TREE
19066               || DECL_ARTIFICIAL (decl_or_origin)))
19067         break;
19068
19069 #if 0
19070       /* FIXME */
19071       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19072          on local redeclarations of global functions.  That seems broken.  */
19073       if (current_function_decl != decl)
19074         /* This is only a declaration.  */;
19075 #endif
19076
19077       /* If we're emitting a clone, emit info for the abstract instance.  */
19078       if (origin || DECL_ORIGIN (decl) != decl)
19079         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
19080
19081       /* If we're emitting an out-of-line copy of an inline function,
19082          emit info for the abstract instance and set up to refer to it.  */
19083       else if (cgraph_function_possibly_inlined_p (decl)
19084                && ! DECL_ABSTRACT (decl)
19085                && ! class_or_namespace_scope_p (context_die)
19086                /* dwarf2out_abstract_function won't emit a die if this is just
19087                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19088                   that case, because that works only if we have a die.  */
19089                && DECL_INITIAL (decl) != NULL_TREE)
19090         {
19091           dwarf2out_abstract_function (decl);
19092           set_decl_origin_self (decl);
19093         }
19094
19095       /* Otherwise we're emitting the primary DIE for this decl.  */
19096       else if (debug_info_level > DINFO_LEVEL_TERSE)
19097         {
19098           /* Before we describe the FUNCTION_DECL itself, make sure that we
19099              have described its return type.  */
19100           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19101
19102           /* And its virtual context.  */
19103           if (DECL_VINDEX (decl) != NULL_TREE)
19104             gen_type_die (DECL_CONTEXT (decl), context_die);
19105
19106           /* And its containing type.  */
19107           if (!origin)
19108             origin = decl_class_context (decl);
19109           if (origin != NULL_TREE)
19110             gen_type_die_for_member (origin, decl, context_die);
19111
19112           /* And its containing namespace.  */
19113           context_die = declare_in_namespace (decl, context_die);
19114         }
19115
19116       /* Now output a DIE to represent the function itself.  */
19117       if (decl)
19118         gen_subprogram_die (decl, context_die);
19119       break;
19120
19121     case TYPE_DECL:
19122       /* If we are in terse mode, don't generate any DIEs to represent any
19123          actual typedefs.  */
19124       if (debug_info_level <= DINFO_LEVEL_TERSE)
19125         break;
19126
19127       /* In the special case of a TYPE_DECL node representing the declaration
19128          of some type tag, if the given TYPE_DECL is marked as having been
19129          instantiated from some other (original) TYPE_DECL node (e.g. one which
19130          was generated within the original definition of an inline function) we
19131          used to generate a special (abbreviated) DW_TAG_structure_type,
19132          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19133          should be actually referencing those DIEs, as variable DIEs with that
19134          type would be emitted already in the abstract origin, so it was always
19135          removed during unused type prunning.  Don't add anything in this
19136          case.  */
19137       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19138         break;
19139
19140       if (is_redundant_typedef (decl))
19141         gen_type_die (TREE_TYPE (decl), context_die);
19142       else
19143         /* Output a DIE to represent the typedef itself.  */
19144         gen_typedef_die (decl, context_die);
19145       break;
19146
19147     case LABEL_DECL:
19148       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19149         gen_label_die (decl, context_die);
19150       break;
19151
19152     case VAR_DECL:
19153     case RESULT_DECL:
19154       /* If we are in terse mode, don't generate any DIEs to represent any
19155          variable declarations or definitions.  */
19156       if (debug_info_level <= DINFO_LEVEL_TERSE)
19157         break;
19158
19159       /* Output any DIEs that are needed to specify the type of this data
19160          object.  */
19161       if (decl_by_reference_p (decl_or_origin))
19162         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19163       else
19164         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19165
19166       /* And its containing type.  */
19167       class_origin = decl_class_context (decl_or_origin);
19168       if (class_origin != NULL_TREE)
19169         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19170
19171       /* And its containing namespace.  */
19172       context_die = declare_in_namespace (decl_or_origin, context_die);
19173
19174       /* Now output the DIE to represent the data object itself.  This gets
19175          complicated because of the possibility that the VAR_DECL really
19176          represents an inlined instance of a formal parameter for an inline
19177          function.  */
19178       if (!origin)
19179         origin = decl_ultimate_origin (decl);
19180       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
19181         gen_formal_parameter_die (decl, origin,
19182                                   true /* Emit name attribute.  */,
19183                                   context_die);
19184       else
19185         gen_variable_die (decl, origin, context_die);
19186       break;
19187
19188     case FIELD_DECL:
19189       /* Ignore the nameless fields that are used to skip bits but handle C++
19190          anonymous unions and structs.  */
19191       if (DECL_NAME (decl) != NULL_TREE
19192           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19193           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19194         {
19195           gen_type_die (member_declared_type (decl), context_die);
19196           gen_field_die (decl, context_die);
19197         }
19198       break;
19199
19200     case PARM_DECL:
19201       if (DECL_BY_REFERENCE (decl_or_origin))
19202         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19203       else
19204         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19205       gen_formal_parameter_die (decl, origin,
19206                                 true /* Emit name attribute.  */,
19207                                 context_die);
19208       break;
19209
19210     case NAMESPACE_DECL:
19211     case IMPORTED_DECL:
19212       if (dwarf_version >= 3 || !dwarf_strict)
19213         gen_namespace_die (decl, context_die);
19214       break;
19215
19216     default:
19217       /* Probably some frontend-internal decl.  Assume we don't care.  */
19218       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19219       break;
19220     }
19221 }
19222 \f
19223 /* Output debug information for global decl DECL.  Called from toplev.c after
19224    compilation proper has finished.  */
19225
19226 static void
19227 dwarf2out_global_decl (tree decl)
19228 {
19229   /* Output DWARF2 information for file-scope tentative data object
19230      declarations, file-scope (extern) function declarations (which
19231      had no corresponding body) and file-scope tagged type declarations
19232      and definitions which have not yet been forced out.  */
19233   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19234     dwarf2out_decl (decl);
19235 }
19236
19237 /* Output debug information for type decl DECL.  Called from toplev.c
19238    and from language front ends (to record built-in types).  */
19239 static void
19240 dwarf2out_type_decl (tree decl, int local)
19241 {
19242   if (!local)
19243     dwarf2out_decl (decl);
19244 }
19245
19246 /* Output debug information for imported module or decl DECL.
19247    NAME is non-NULL name in the lexical block if the decl has been renamed.
19248    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19249    that DECL belongs to.
19250    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19251 static void
19252 dwarf2out_imported_module_or_decl_1 (tree decl,
19253                                      tree name,
19254                                      tree lexical_block,
19255                                      dw_die_ref lexical_block_die)
19256 {
19257   expanded_location xloc;
19258   dw_die_ref imported_die = NULL;
19259   dw_die_ref at_import_die;
19260
19261   if (TREE_CODE (decl) == IMPORTED_DECL)
19262     {
19263       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19264       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19265       gcc_assert (decl);
19266     }
19267   else
19268     xloc = expand_location (input_location);
19269
19270   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19271     {
19272       if (is_base_type (TREE_TYPE (decl)))
19273         at_import_die = base_type_die (TREE_TYPE (decl));
19274       else
19275         at_import_die = force_type_die (TREE_TYPE (decl));
19276       /* For namespace N { typedef void T; } using N::T; base_type_die
19277          returns NULL, but DW_TAG_imported_declaration requires
19278          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19279       if (!at_import_die)
19280         {
19281           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19282           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19283           at_import_die = lookup_type_die (TREE_TYPE (decl));
19284           gcc_assert (at_import_die);
19285         }
19286     }
19287   else
19288     {
19289       at_import_die = lookup_decl_die (decl);
19290       if (!at_import_die)
19291         {
19292           /* If we're trying to avoid duplicate debug info, we may not have
19293              emitted the member decl for this field.  Emit it now.  */
19294           if (TREE_CODE (decl) == FIELD_DECL)
19295             {
19296               tree type = DECL_CONTEXT (decl);
19297
19298               if (TYPE_CONTEXT (type)
19299                   && TYPE_P (TYPE_CONTEXT (type))
19300                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19301                                                 DINFO_USAGE_DIR_USE))
19302                 return;
19303               gen_type_die_for_member (type, decl,
19304                                        get_context_die (TYPE_CONTEXT (type)));
19305             }
19306           at_import_die = force_decl_die (decl);
19307         }
19308     }
19309
19310   if (TREE_CODE (decl) == NAMESPACE_DECL)
19311     {
19312       if (dwarf_version >= 3 || !dwarf_strict)
19313         imported_die = new_die (DW_TAG_imported_module,
19314                                 lexical_block_die,
19315                                 lexical_block);
19316       else
19317         return;
19318     }
19319   else
19320     imported_die = new_die (DW_TAG_imported_declaration,
19321                             lexical_block_die,
19322                             lexical_block);
19323
19324   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19325   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19326   if (name)
19327     add_AT_string (imported_die, DW_AT_name,
19328                    IDENTIFIER_POINTER (name));
19329   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19330 }
19331
19332 /* Output debug information for imported module or decl DECL.
19333    NAME is non-NULL name in context if the decl has been renamed.
19334    CHILD is true if decl is one of the renamed decls as part of
19335    importing whole module.  */
19336
19337 static void
19338 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19339                                    bool child)
19340 {
19341   /* dw_die_ref at_import_die;  */
19342   dw_die_ref scope_die;
19343
19344   if (debug_info_level <= DINFO_LEVEL_TERSE)
19345     return;
19346
19347   gcc_assert (decl);
19348
19349   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19350      We need decl DIE for reference and scope die. First, get DIE for the decl
19351      itself.  */
19352
19353   /* Get the scope die for decl context. Use comp_unit_die for global module
19354      or decl. If die is not found for non globals, force new die.  */
19355   if (context
19356       && TYPE_P (context)
19357       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19358     return;
19359
19360   if (!(dwarf_version >= 3 || !dwarf_strict))
19361     return;
19362
19363   scope_die = get_context_die (context);
19364
19365   if (child)
19366     {
19367       gcc_assert (scope_die->die_child);
19368       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19369       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19370       scope_die = scope_die->die_child;
19371     }
19372
19373   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19374   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19375
19376 }
19377
19378 /* Write the debugging output for DECL.  */
19379
19380 void
19381 dwarf2out_decl (tree decl)
19382 {
19383   dw_die_ref context_die = comp_unit_die;
19384
19385   switch (TREE_CODE (decl))
19386     {
19387     case ERROR_MARK:
19388       return;
19389
19390     case FUNCTION_DECL:
19391       /* What we would really like to do here is to filter out all mere
19392          file-scope declarations of file-scope functions which are never
19393          referenced later within this translation unit (and keep all of ones
19394          that *are* referenced later on) but we aren't clairvoyant, so we have
19395          no idea which functions will be referenced in the future (i.e. later
19396          on within the current translation unit). So here we just ignore all
19397          file-scope function declarations which are not also definitions.  If
19398          and when the debugger needs to know something about these functions,
19399          it will have to hunt around and find the DWARF information associated
19400          with the definition of the function.
19401
19402          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19403          nodes represent definitions and which ones represent mere
19404          declarations.  We have to check DECL_INITIAL instead. That's because
19405          the C front-end supports some weird semantics for "extern inline"
19406          function definitions.  These can get inlined within the current
19407          translation unit (and thus, we need to generate Dwarf info for their
19408          abstract instances so that the Dwarf info for the concrete inlined
19409          instances can have something to refer to) but the compiler never
19410          generates any out-of-lines instances of such things (despite the fact
19411          that they *are* definitions).
19412
19413          The important point is that the C front-end marks these "extern
19414          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19415          them anyway. Note that the C++ front-end also plays some similar games
19416          for inline function definitions appearing within include files which
19417          also contain `#pragma interface' pragmas.  */
19418       if (DECL_INITIAL (decl) == NULL_TREE)
19419         return;
19420
19421       /* If we're a nested function, initially use a parent of NULL; if we're
19422          a plain function, this will be fixed up in decls_for_scope.  If
19423          we're a method, it will be ignored, since we already have a DIE.  */
19424       if (decl_function_context (decl)
19425           /* But if we're in terse mode, we don't care about scope.  */
19426           && debug_info_level > DINFO_LEVEL_TERSE)
19427         context_die = NULL;
19428       break;
19429
19430     case VAR_DECL:
19431       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19432          declaration and if the declaration was never even referenced from
19433          within this entire compilation unit.  We suppress these DIEs in
19434          order to save space in the .debug section (by eliminating entries
19435          which are probably useless).  Note that we must not suppress
19436          block-local extern declarations (whether used or not) because that
19437          would screw-up the debugger's name lookup mechanism and cause it to
19438          miss things which really ought to be in scope at a given point.  */
19439       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19440         return;
19441
19442       /* For local statics lookup proper context die.  */
19443       if (TREE_STATIC (decl) && decl_function_context (decl))
19444         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19445
19446       /* If we are in terse mode, don't generate any DIEs to represent any
19447          variable declarations or definitions.  */
19448       if (debug_info_level <= DINFO_LEVEL_TERSE)
19449         return;
19450       break;
19451
19452     case CONST_DECL:
19453       if (debug_info_level <= DINFO_LEVEL_TERSE)
19454         return;
19455       if (!is_fortran ())
19456         return;
19457       if (TREE_STATIC (decl) && decl_function_context (decl))
19458         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19459       break;
19460
19461     case NAMESPACE_DECL:
19462     case IMPORTED_DECL:
19463       if (debug_info_level <= DINFO_LEVEL_TERSE)
19464         return;
19465       if (lookup_decl_die (decl) != NULL)
19466         return;
19467       break;
19468
19469     case TYPE_DECL:
19470       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19471       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19472         return;
19473
19474       /* Don't bother trying to generate any DIEs to represent any of the
19475          normal built-in types for the language we are compiling.  */
19476       if (DECL_IS_BUILTIN (decl))
19477         {
19478           /* OK, we need to generate one for `bool' so GDB knows what type
19479              comparisons have.  */
19480           if (is_cxx ()
19481               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
19482               && ! DECL_IGNORED_P (decl))
19483             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
19484
19485           return;
19486         }
19487
19488       /* If we are in terse mode, don't generate any DIEs for types.  */
19489       if (debug_info_level <= DINFO_LEVEL_TERSE)
19490         return;
19491
19492       /* If we're a function-scope tag, initially use a parent of NULL;
19493          this will be fixed up in decls_for_scope.  */
19494       if (decl_function_context (decl))
19495         context_die = NULL;
19496
19497       break;
19498
19499     default:
19500       return;
19501     }
19502
19503   gen_decl_die (decl, NULL, context_die);
19504 }
19505
19506 /* Output a marker (i.e. a label) for the beginning of the generated code for
19507    a lexical block.  */
19508
19509 static void
19510 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19511                        unsigned int blocknum)
19512 {
19513   switch_to_section (current_function_section ());
19514   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19515 }
19516
19517 /* Output a marker (i.e. a label) for the end of the generated code for a
19518    lexical block.  */
19519
19520 static void
19521 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19522 {
19523   switch_to_section (current_function_section ());
19524   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19525 }
19526
19527 /* Returns nonzero if it is appropriate not to emit any debugging
19528    information for BLOCK, because it doesn't contain any instructions.
19529
19530    Don't allow this for blocks with nested functions or local classes
19531    as we would end up with orphans, and in the presence of scheduling
19532    we may end up calling them anyway.  */
19533
19534 static bool
19535 dwarf2out_ignore_block (const_tree block)
19536 {
19537   tree decl;
19538   unsigned int i;
19539
19540   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
19541     if (TREE_CODE (decl) == FUNCTION_DECL
19542         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19543       return 0;
19544   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19545     {
19546       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19547       if (TREE_CODE (decl) == FUNCTION_DECL
19548           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19549       return 0;
19550     }
19551
19552   return 1;
19553 }
19554
19555 /* Hash table routines for file_hash.  */
19556
19557 static int
19558 file_table_eq (const void *p1_p, const void *p2_p)
19559 {
19560   const struct dwarf_file_data *const p1 =
19561     (const struct dwarf_file_data *) p1_p;
19562   const char *const p2 = (const char *) p2_p;
19563   return strcmp (p1->filename, p2) == 0;
19564 }
19565
19566 static hashval_t
19567 file_table_hash (const void *p_p)
19568 {
19569   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19570   return htab_hash_string (p->filename);
19571 }
19572
19573 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19574    dwarf2out.c) and return its "index".  The index of each (known) filename is
19575    just a unique number which is associated with only that one filename.  We
19576    need such numbers for the sake of generating labels (in the .debug_sfnames
19577    section) and references to those files numbers (in the .debug_srcinfo
19578    and.debug_macinfo sections).  If the filename given as an argument is not
19579    found in our current list, add it to the list and assign it the next
19580    available unique index number.  In order to speed up searches, we remember
19581    the index of the filename was looked up last.  This handles the majority of
19582    all searches.  */
19583
19584 static struct dwarf_file_data *
19585 lookup_filename (const char *file_name)
19586 {
19587   void ** slot;
19588   struct dwarf_file_data * created;
19589
19590   /* Check to see if the file name that was searched on the previous
19591      call matches this file name.  If so, return the index.  */
19592   if (file_table_last_lookup
19593       && (file_name == file_table_last_lookup->filename
19594           || strcmp (file_table_last_lookup->filename, file_name) == 0))
19595     return file_table_last_lookup;
19596
19597   /* Didn't match the previous lookup, search the table.  */
19598   slot = htab_find_slot_with_hash (file_table, file_name,
19599                                    htab_hash_string (file_name), INSERT);
19600   if (*slot)
19601     return (struct dwarf_file_data *) *slot;
19602
19603   created = GGC_NEW (struct dwarf_file_data);
19604   created->filename = file_name;
19605   created->emitted_number = 0;
19606   *slot = created;
19607   return created;
19608 }
19609
19610 /* If the assembler will construct the file table, then translate the compiler
19611    internal file table number into the assembler file table number, and emit
19612    a .file directive if we haven't already emitted one yet.  The file table
19613    numbers are different because we prune debug info for unused variables and
19614    types, which may include filenames.  */
19615
19616 static int
19617 maybe_emit_file (struct dwarf_file_data * fd)
19618 {
19619   if (! fd->emitted_number)
19620     {
19621       if (last_emitted_file)
19622         fd->emitted_number = last_emitted_file->emitted_number + 1;
19623       else
19624         fd->emitted_number = 1;
19625       last_emitted_file = fd;
19626
19627       if (DWARF2_ASM_LINE_DEBUG_INFO)
19628         {
19629           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19630           output_quoted_string (asm_out_file,
19631                                 remap_debug_filename (fd->filename));
19632           fputc ('\n', asm_out_file);
19633         }
19634     }
19635
19636   return fd->emitted_number;
19637 }
19638
19639 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19640    That generation should happen after function debug info has been
19641    generated. The value of the attribute is the constant value of ARG.  */
19642
19643 static void
19644 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19645 {
19646   die_arg_entry entry;
19647
19648   if (!die || !arg)
19649     return;
19650
19651   if (!tmpl_value_parm_die_table)
19652     tmpl_value_parm_die_table
19653       = VEC_alloc (die_arg_entry, gc, 32);
19654
19655   entry.die = die;
19656   entry.arg = arg;
19657   VEC_safe_push (die_arg_entry, gc,
19658                  tmpl_value_parm_die_table,
19659                  &entry);
19660 }
19661
19662 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19663    by append_entry_to_tmpl_value_parm_die_table. This function must
19664    be called after function DIEs have been generated.  */
19665
19666 static void
19667 gen_remaining_tmpl_value_param_die_attribute (void)
19668 {
19669   if (tmpl_value_parm_die_table)
19670     {
19671       unsigned i;
19672       die_arg_entry *e;
19673
19674       for (i = 0;
19675            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
19676            i++)
19677         tree_add_const_value_attribute (e->die, e->arg);
19678     }
19679 }
19680
19681
19682 /* Replace DW_AT_name for the decl with name.  */
19683  
19684 static void
19685 dwarf2out_set_name (tree decl, tree name)
19686 {
19687   dw_die_ref die;
19688   dw_attr_ref attr;
19689
19690   die = TYPE_SYMTAB_DIE (decl);
19691   if (!die)
19692     return;
19693
19694   attr = get_AT (die, DW_AT_name);
19695   if (attr)
19696     {
19697       struct indirect_string_node *node;
19698
19699       node = find_AT_string (dwarf2_name (name, 0));
19700       /* replace the string.  */
19701       attr->dw_attr_val.v.val_str = node;
19702     }
19703
19704   else
19705     add_name_attribute (die, dwarf2_name (name, 0));
19706 }
19707
19708 /* Called by the final INSN scan whenever we see a var location.  We
19709    use it to drop labels in the right places, and throw the location in
19710    our lookup table.  */
19711
19712 static void
19713 dwarf2out_var_location (rtx loc_note)
19714 {
19715   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
19716   struct var_loc_node *newloc;
19717   rtx next_real;
19718   static const char *last_label;
19719   static const char *last_postcall_label;
19720   static bool last_in_cold_section_p;
19721   tree decl;
19722
19723   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
19724     return;
19725
19726   next_real = next_real_insn (loc_note);
19727   /* If there are no instructions which would be affected by this note,
19728      don't do anything.  */
19729   if (next_real == NULL_RTX)
19730     return;
19731
19732   newloc = GGC_CNEW (struct var_loc_node);
19733   /* If there were no real insns between note we processed last time
19734      and this note, use the label we emitted last time.  */
19735   if (last_var_location_insn == NULL_RTX
19736       || last_var_location_insn != next_real
19737       || last_in_cold_section_p != in_cold_section_p)
19738     {
19739       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
19740       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
19741       loclabel_num++;
19742       last_label = ggc_strdup (loclabel);
19743       if (!NOTE_DURING_CALL_P (loc_note))
19744         last_postcall_label = NULL;
19745     }
19746   newloc->var_loc_note = loc_note;
19747   newloc->next = NULL;
19748
19749   if (!NOTE_DURING_CALL_P (loc_note))
19750     newloc->label = last_label;
19751   else
19752     {
19753       if (!last_postcall_label)
19754         {
19755           sprintf (loclabel, "%s-1", last_label);
19756           last_postcall_label = ggc_strdup (loclabel);
19757         }
19758       newloc->label = last_postcall_label;
19759     }
19760
19761   if (cfun && in_cold_section_p)
19762     newloc->section_label = crtl->subsections.cold_section_label;
19763   else
19764     newloc->section_label = text_section_label;
19765
19766   last_var_location_insn = next_real;
19767   last_in_cold_section_p = in_cold_section_p;
19768   decl = NOTE_VAR_LOCATION_DECL (loc_note);
19769   add_var_loc_to_decl (decl, newloc);
19770 }
19771
19772 /* We need to reset the locations at the beginning of each
19773    function. We can't do this in the end_function hook, because the
19774    declarations that use the locations won't have been output when
19775    that hook is called.  Also compute have_multiple_function_sections here.  */
19776
19777 static void
19778 dwarf2out_begin_function (tree fun)
19779 {
19780   htab_empty (decl_loc_table);
19781
19782   if (function_section (fun) != text_section)
19783     have_multiple_function_sections = true;
19784
19785   dwarf2out_note_section_used ();
19786 }
19787
19788 /* Output a label to mark the beginning of a source code line entry
19789    and record information relating to this source line, in
19790    'line_info_table' for later output of the .debug_line section.  */
19791
19792 static void
19793 dwarf2out_source_line (unsigned int line, const char *filename,
19794                        int discriminator, bool is_stmt)
19795 {
19796   static bool last_is_stmt = true;
19797
19798   if (debug_info_level >= DINFO_LEVEL_NORMAL
19799       && line != 0)
19800     {
19801       int file_num = maybe_emit_file (lookup_filename (filename));
19802
19803       switch_to_section (current_function_section ());
19804
19805       /* If requested, emit something human-readable.  */
19806       if (flag_debug_asm)
19807         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
19808                  filename, line);
19809
19810       if (DWARF2_ASM_LINE_DEBUG_INFO)
19811         {
19812           /* Emit the .loc directive understood by GNU as.  */
19813           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
19814           if (is_stmt != last_is_stmt)
19815             {
19816               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
19817               last_is_stmt = is_stmt;
19818             }
19819           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
19820             fprintf (asm_out_file, " discriminator %d", discriminator);
19821           fputc ('\n', asm_out_file);
19822
19823           /* Indicate that line number info exists.  */
19824           line_info_table_in_use++;
19825         }
19826       else if (function_section (current_function_decl) != text_section)
19827         {
19828           dw_separate_line_info_ref line_info;
19829           targetm.asm_out.internal_label (asm_out_file,
19830                                           SEPARATE_LINE_CODE_LABEL,
19831                                           separate_line_info_table_in_use);
19832
19833           /* Expand the line info table if necessary.  */
19834           if (separate_line_info_table_in_use
19835               == separate_line_info_table_allocated)
19836             {
19837               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
19838               separate_line_info_table
19839                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
19840                                  separate_line_info_table,
19841                                  separate_line_info_table_allocated);
19842               memset (separate_line_info_table
19843                        + separate_line_info_table_in_use,
19844                       0,
19845                       (LINE_INFO_TABLE_INCREMENT
19846                        * sizeof (dw_separate_line_info_entry)));
19847             }
19848
19849           /* Add the new entry at the end of the line_info_table.  */
19850           line_info
19851             = &separate_line_info_table[separate_line_info_table_in_use++];
19852           line_info->dw_file_num = file_num;
19853           line_info->dw_line_num = line;
19854           line_info->function = current_function_funcdef_no;
19855         }
19856       else
19857         {
19858           dw_line_info_ref line_info;
19859
19860           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
19861                                      line_info_table_in_use);
19862
19863           /* Expand the line info table if necessary.  */
19864           if (line_info_table_in_use == line_info_table_allocated)
19865             {
19866               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
19867               line_info_table
19868                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
19869                                  line_info_table_allocated);
19870               memset (line_info_table + line_info_table_in_use, 0,
19871                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
19872             }
19873
19874           /* Add the new entry at the end of the line_info_table.  */
19875           line_info = &line_info_table[line_info_table_in_use++];
19876           line_info->dw_file_num = file_num;
19877           line_info->dw_line_num = line;
19878         }
19879     }
19880 }
19881
19882 /* Record the beginning of a new source file.  */
19883
19884 static void
19885 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
19886 {
19887   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
19888     {
19889       /* Record the beginning of the file for break_out_includes.  */
19890       dw_die_ref bincl_die;
19891
19892       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
19893       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
19894     }
19895
19896   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
19897     {
19898       int file_num = maybe_emit_file (lookup_filename (filename));
19899
19900       switch_to_section (debug_macinfo_section);
19901       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
19902       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
19903                                    lineno);
19904
19905       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
19906     }
19907 }
19908
19909 /* Record the end of a source file.  */
19910
19911 static void
19912 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
19913 {
19914   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
19915     /* Record the end of the file for break_out_includes.  */
19916     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
19917
19918   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
19919     {
19920       switch_to_section (debug_macinfo_section);
19921       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
19922     }
19923 }
19924
19925 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
19926    the tail part of the directive line, i.e. the part which is past the
19927    initial whitespace, #, whitespace, directive-name, whitespace part.  */
19928
19929 static void
19930 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
19931                   const char *buffer ATTRIBUTE_UNUSED)
19932 {
19933   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
19934     {
19935       switch_to_section (debug_macinfo_section);
19936       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
19937       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
19938       dw2_asm_output_nstring (buffer, -1, "The macro");
19939     }
19940 }
19941
19942 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
19943    the tail part of the directive line, i.e. the part which is past the
19944    initial whitespace, #, whitespace, directive-name, whitespace part.  */
19945
19946 static void
19947 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
19948                  const char *buffer ATTRIBUTE_UNUSED)
19949 {
19950   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
19951     {
19952       switch_to_section (debug_macinfo_section);
19953       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
19954       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
19955       dw2_asm_output_nstring (buffer, -1, "The macro");
19956     }
19957 }
19958
19959 /* Set up for Dwarf output at the start of compilation.  */
19960
19961 static void
19962 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
19963 {
19964   /* Allocate the file_table.  */
19965   file_table = htab_create_ggc (50, file_table_hash,
19966                                 file_table_eq, NULL);
19967
19968   /* Allocate the decl_die_table.  */
19969   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
19970                                     decl_die_table_eq, NULL);
19971
19972   /* Allocate the decl_loc_table.  */
19973   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
19974                                     decl_loc_table_eq, NULL);
19975
19976   /* Allocate the initial hunk of the decl_scope_table.  */
19977   decl_scope_table = VEC_alloc (tree, gc, 256);
19978
19979   /* Allocate the initial hunk of the abbrev_die_table.  */
19980   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
19981   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
19982   /* Zero-th entry is allocated, but unused.  */
19983   abbrev_die_table_in_use = 1;
19984
19985   /* Allocate the initial hunk of the line_info_table.  */
19986   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
19987   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
19988
19989   /* Zero-th entry is allocated, but unused.  */
19990   line_info_table_in_use = 1;
19991
19992   /* Allocate the pubtypes and pubnames vectors.  */
19993   pubname_table = VEC_alloc (pubname_entry, gc, 32);
19994   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
19995
19996   /* Generate the initial DIE for the .debug section.  Note that the (string)
19997      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
19998      will (typically) be a relative pathname and that this pathname should be
19999      taken as being relative to the directory from which the compiler was
20000      invoked when the given (base) source file was compiled.  We will fill
20001      in this value in dwarf2out_finish.  */
20002   comp_unit_die = gen_compile_unit_die (NULL);
20003
20004   incomplete_types = VEC_alloc (tree, gc, 64);
20005
20006   used_rtx_array = VEC_alloc (rtx, gc, 32);
20007
20008   debug_info_section = get_section (DEBUG_INFO_SECTION,
20009                                     SECTION_DEBUG, NULL);
20010   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20011                                       SECTION_DEBUG, NULL);
20012   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20013                                        SECTION_DEBUG, NULL);
20014   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
20015                                        SECTION_DEBUG, NULL);
20016   debug_line_section = get_section (DEBUG_LINE_SECTION,
20017                                     SECTION_DEBUG, NULL);
20018   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20019                                    SECTION_DEBUG, NULL);
20020   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20021                                         SECTION_DEBUG, NULL);
20022   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20023                                         SECTION_DEBUG, NULL);
20024   debug_str_section = get_section (DEBUG_STR_SECTION,
20025                                    DEBUG_STR_SECTION_FLAGS, NULL);
20026   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20027                                       SECTION_DEBUG, NULL);
20028   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20029                                      SECTION_DEBUG, NULL);
20030
20031   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20032   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20033                                DEBUG_ABBREV_SECTION_LABEL, 0);
20034   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20035   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20036                                COLD_TEXT_SECTION_LABEL, 0);
20037   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20038
20039   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20040                                DEBUG_INFO_SECTION_LABEL, 0);
20041   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20042                                DEBUG_LINE_SECTION_LABEL, 0);
20043   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20044                                DEBUG_RANGES_SECTION_LABEL, 0);
20045   switch_to_section (debug_abbrev_section);
20046   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
20047   switch_to_section (debug_info_section);
20048   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
20049   switch_to_section (debug_line_section);
20050   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
20051
20052   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20053     {
20054       switch_to_section (debug_macinfo_section);
20055       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20056                                    DEBUG_MACINFO_SECTION_LABEL, 0);
20057       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
20058     }
20059
20060   switch_to_section (text_section);
20061   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20062   if (flag_reorder_blocks_and_partition)
20063     {
20064       cold_text_section = unlikely_text_section ();
20065       switch_to_section (cold_text_section);
20066       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20067     }
20068
20069 #ifdef HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20070   if (dwarf2out_do_cfi_asm ())
20071     {
20072 #ifndef TARGET_UNWIND_INFO
20073       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
20074 #endif
20075         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20076     }
20077 #endif
20078 }
20079
20080 /* A helper function for dwarf2out_finish called through
20081    htab_traverse.  Emit one queued .debug_str string.  */
20082
20083 static int
20084 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20085 {
20086   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20087
20088   if (node->label && node->refcount)
20089     {
20090       switch_to_section (debug_str_section);
20091       ASM_OUTPUT_LABEL (asm_out_file, node->label);
20092       assemble_string (node->str, strlen (node->str) + 1);
20093     }
20094
20095   return 1;
20096 }
20097
20098 #if ENABLE_ASSERT_CHECKING
20099 /* Verify that all marks are clear.  */
20100
20101 static void
20102 verify_marks_clear (dw_die_ref die)
20103 {
20104   dw_die_ref c;
20105
20106   gcc_assert (! die->die_mark);
20107   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20108 }
20109 #endif /* ENABLE_ASSERT_CHECKING */
20110
20111 /* Clear the marks for a die and its children.
20112    Be cool if the mark isn't set.  */
20113
20114 static void
20115 prune_unmark_dies (dw_die_ref die)
20116 {
20117   dw_die_ref c;
20118
20119   if (die->die_mark)
20120     die->die_mark = 0;
20121   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20122 }
20123
20124 /* Given DIE that we're marking as used, find any other dies
20125    it references as attributes and mark them as used.  */
20126
20127 static void
20128 prune_unused_types_walk_attribs (dw_die_ref die)
20129 {
20130   dw_attr_ref a;
20131   unsigned ix;
20132
20133   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20134     {
20135       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20136         {
20137           /* A reference to another DIE.
20138              Make sure that it will get emitted.
20139              If it was broken out into a comdat group, don't follow it.  */
20140           if (dwarf_version < 4
20141               || a->dw_attr == DW_AT_specification
20142               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
20143             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20144         }
20145       /* Set the string's refcount to 0 so that prune_unused_types_mark
20146          accounts properly for it.  */
20147       if (AT_class (a) == dw_val_class_str)
20148         a->dw_attr_val.v.val_str->refcount = 0;
20149     }
20150 }
20151
20152
20153 /* Mark DIE as being used.  If DOKIDS is true, then walk down
20154    to DIE's children.  */
20155
20156 static void
20157 prune_unused_types_mark (dw_die_ref die, int dokids)
20158 {
20159   dw_die_ref c;
20160
20161   if (die->die_mark == 0)
20162     {
20163       /* We haven't done this node yet.  Mark it as used.  */
20164       die->die_mark = 1;
20165
20166       /* We also have to mark its parents as used.
20167          (But we don't want to mark our parents' kids due to this.)  */
20168       if (die->die_parent)
20169         prune_unused_types_mark (die->die_parent, 0);
20170
20171       /* Mark any referenced nodes.  */
20172       prune_unused_types_walk_attribs (die);
20173
20174       /* If this node is a specification,
20175          also mark the definition, if it exists.  */
20176       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
20177         prune_unused_types_mark (die->die_definition, 1);
20178     }
20179
20180   if (dokids && die->die_mark != 2)
20181     {
20182       /* We need to walk the children, but haven't done so yet.
20183          Remember that we've walked the kids.  */
20184       die->die_mark = 2;
20185
20186       /* If this is an array type, we need to make sure our
20187          kids get marked, even if they're types.  If we're
20188          breaking out types into comdat sections, do this
20189          for all type definitions.  */
20190       if (die->die_tag == DW_TAG_array_type
20191           || (dwarf_version >= 4 
20192               && is_type_die (die) && ! is_declaration_die (die)))
20193         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
20194       else
20195         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20196     }
20197 }
20198
20199 /* For local classes, look if any static member functions were emitted
20200    and if so, mark them.  */
20201
20202 static void
20203 prune_unused_types_walk_local_classes (dw_die_ref die)
20204 {
20205   dw_die_ref c;
20206
20207   if (die->die_mark == 2)
20208     return;
20209
20210   switch (die->die_tag)
20211     {
20212     case DW_TAG_structure_type:
20213     case DW_TAG_union_type:
20214     case DW_TAG_class_type:
20215       break;
20216
20217     case DW_TAG_subprogram:
20218       if (!get_AT_flag (die, DW_AT_declaration)
20219           || die->die_definition != NULL)
20220         prune_unused_types_mark (die, 1);
20221       return;
20222
20223     default:
20224       return;
20225     }
20226
20227   /* Mark children.  */
20228   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
20229 }
20230
20231 /* Walk the tree DIE and mark types that we actually use.  */
20232
20233 static void
20234 prune_unused_types_walk (dw_die_ref die)
20235 {
20236   dw_die_ref c;
20237
20238   /* Don't do anything if this node is already marked and
20239      children have been marked as well.  */
20240   if (die->die_mark == 2)
20241     return;
20242
20243   switch (die->die_tag)
20244     {
20245     case DW_TAG_structure_type:
20246     case DW_TAG_union_type:
20247     case DW_TAG_class_type:
20248       if (die->die_perennial_p)
20249         break;
20250
20251       for (c = die->die_parent; c; c = c->die_parent)
20252         if (c->die_tag == DW_TAG_subprogram)
20253           break;
20254
20255       /* Finding used static member functions inside of classes
20256          is needed just for local classes, because for other classes
20257          static member function DIEs with DW_AT_specification
20258          are emitted outside of the DW_TAG_*_type.  If we ever change
20259          it, we'd need to call this even for non-local classes.  */
20260       if (c)
20261         prune_unused_types_walk_local_classes (die);
20262
20263       /* It's a type node --- don't mark it.  */
20264       return;
20265
20266     case DW_TAG_const_type:
20267     case DW_TAG_packed_type:
20268     case DW_TAG_pointer_type:
20269     case DW_TAG_reference_type:
20270     case DW_TAG_volatile_type:
20271     case DW_TAG_typedef:
20272     case DW_TAG_array_type:
20273     case DW_TAG_interface_type:
20274     case DW_TAG_friend:
20275     case DW_TAG_variant_part:
20276     case DW_TAG_enumeration_type:
20277     case DW_TAG_subroutine_type:
20278     case DW_TAG_string_type:
20279     case DW_TAG_set_type:
20280     case DW_TAG_subrange_type:
20281     case DW_TAG_ptr_to_member_type:
20282     case DW_TAG_file_type:
20283       if (die->die_perennial_p)
20284         break;
20285
20286       /* It's a type node --- don't mark it.  */
20287       return;
20288
20289     default:
20290       /* Mark everything else.  */
20291       break;
20292   }
20293
20294   if (die->die_mark == 0)
20295     {
20296       die->die_mark = 1;
20297
20298       /* Now, mark any dies referenced from here.  */
20299       prune_unused_types_walk_attribs (die);
20300     }
20301
20302   die->die_mark = 2;
20303
20304   /* Mark children.  */
20305   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20306 }
20307
20308 /* Increment the string counts on strings referred to from DIE's
20309    attributes.  */
20310
20311 static void
20312 prune_unused_types_update_strings (dw_die_ref die)
20313 {
20314   dw_attr_ref a;
20315   unsigned ix;
20316
20317   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20318     if (AT_class (a) == dw_val_class_str)
20319       {
20320         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
20321         s->refcount++;
20322         /* Avoid unnecessarily putting strings that are used less than
20323            twice in the hash table.  */
20324         if (s->refcount
20325             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
20326           {
20327             void ** slot;
20328             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
20329                                              htab_hash_string (s->str),
20330                                              INSERT);
20331             gcc_assert (*slot == NULL);
20332             *slot = s;
20333           }
20334       }
20335 }
20336
20337 /* Remove from the tree DIE any dies that aren't marked.  */
20338
20339 static void
20340 prune_unused_types_prune (dw_die_ref die)
20341 {
20342   dw_die_ref c;
20343
20344   gcc_assert (die->die_mark);
20345   prune_unused_types_update_strings (die);
20346
20347   if (! die->die_child)
20348     return;
20349
20350   c = die->die_child;
20351   do {
20352     dw_die_ref prev = c;
20353     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
20354       if (c == die->die_child)
20355         {
20356           /* No marked children between 'prev' and the end of the list.  */
20357           if (prev == c)
20358             /* No marked children at all.  */
20359             die->die_child = NULL;
20360           else
20361             {
20362               prev->die_sib = c->die_sib;
20363               die->die_child = prev;
20364             }
20365           return;
20366         }
20367
20368     if (c != prev->die_sib)
20369       prev->die_sib = c;
20370     prune_unused_types_prune (c);
20371   } while (c != die->die_child);
20372 }
20373
20374 /* A helper function for dwarf2out_finish called through
20375    htab_traverse.  Clear .debug_str strings that we haven't already
20376    decided to emit.  */
20377
20378 static int
20379 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20380 {
20381   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20382
20383   if (!node->label || !node->refcount)
20384     htab_clear_slot (debug_str_hash, h);
20385
20386   return 1;
20387 }
20388
20389 /* Remove dies representing declarations that we never use.  */
20390
20391 static void
20392 prune_unused_types (void)
20393 {
20394   unsigned int i;
20395   limbo_die_node *node;
20396   comdat_type_node *ctnode;
20397   pubname_ref pub;
20398
20399 #if ENABLE_ASSERT_CHECKING
20400   /* All the marks should already be clear.  */
20401   verify_marks_clear (comp_unit_die);
20402   for (node = limbo_die_list; node; node = node->next)
20403     verify_marks_clear (node->die);
20404   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20405     verify_marks_clear (ctnode->root_die);
20406 #endif /* ENABLE_ASSERT_CHECKING */
20407
20408   /* Mark types that are used in global variables.  */
20409   premark_types_used_by_global_vars ();
20410
20411   /* Set the mark on nodes that are actually used.  */
20412   prune_unused_types_walk (comp_unit_die);
20413   for (node = limbo_die_list; node; node = node->next)
20414     prune_unused_types_walk (node->die);
20415   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20416     {
20417       prune_unused_types_walk (ctnode->root_die);
20418       prune_unused_types_mark (ctnode->type_die, 1);
20419     }
20420
20421   /* Also set the mark on nodes referenced from the
20422      pubname_table or arange_table.  */
20423   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
20424     prune_unused_types_mark (pub->die, 1);
20425   for (i = 0; i < arange_table_in_use; i++)
20426     prune_unused_types_mark (arange_table[i], 1);
20427
20428   /* Get rid of nodes that aren't marked; and update the string counts.  */
20429   if (debug_str_hash && debug_str_hash_forced)
20430     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
20431   else if (debug_str_hash)
20432     htab_empty (debug_str_hash);
20433   prune_unused_types_prune (comp_unit_die);
20434   for (node = limbo_die_list; node; node = node->next)
20435     prune_unused_types_prune (node->die);
20436   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20437     prune_unused_types_prune (ctnode->root_die);
20438
20439   /* Leave the marks clear.  */
20440   prune_unmark_dies (comp_unit_die);
20441   for (node = limbo_die_list; node; node = node->next)
20442     prune_unmark_dies (node->die);
20443   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20444     prune_unmark_dies (ctnode->root_die);
20445 }
20446
20447 /* Set the parameter to true if there are any relative pathnames in
20448    the file table.  */
20449 static int
20450 file_table_relative_p (void ** slot, void *param)
20451 {
20452   bool *p = (bool *) param;
20453   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
20454   if (!IS_ABSOLUTE_PATH (d->filename))
20455     {
20456       *p = true;
20457       return 0;
20458     }
20459   return 1;
20460 }
20461
20462 /* Routines to manipulate hash table of comdat type units.  */
20463
20464 static hashval_t
20465 htab_ct_hash (const void *of)
20466 {
20467   hashval_t h;
20468   const comdat_type_node *const type_node = (const comdat_type_node *) of;
20469
20470   memcpy (&h, type_node->signature, sizeof (h));
20471   return h;
20472 }
20473
20474 static int
20475 htab_ct_eq (const void *of1, const void *of2)
20476 {
20477   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
20478   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
20479
20480   return (! memcmp (type_node_1->signature, type_node_2->signature,
20481                     DWARF_TYPE_SIGNATURE_SIZE));
20482 }
20483
20484 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
20485    to the location it would have been added, should we know its
20486    DECL_ASSEMBLER_NAME when we added other attributes.  This will
20487    probably improve compactness of debug info, removing equivalent
20488    abbrevs, and hide any differences caused by deferring the
20489    computation of the assembler name, triggered by e.g. PCH.  */
20490
20491 static inline void
20492 move_linkage_attr (dw_die_ref die)
20493 {
20494   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
20495   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
20496
20497   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
20498
20499   while (--ix > 0)
20500     {
20501       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
20502
20503       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
20504         break;
20505     }
20506
20507   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
20508     {
20509       VEC_pop (dw_attr_node, die->die_attr);
20510       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
20511     }
20512 }
20513
20514 /* Helper function for resolve_addr, attempt to resolve
20515    one CONST_STRING, return non-zero if not successful.  Similarly verify that
20516    SYMBOL_REFs refer to variables emitted in the current CU.  */
20517
20518 static int
20519 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
20520 {
20521   rtx rtl = *addr;
20522
20523   if (GET_CODE (rtl) == CONST_STRING)
20524     {
20525       size_t len = strlen (XSTR (rtl, 0)) + 1;
20526       tree t = build_string (len, XSTR (rtl, 0));
20527       tree tlen = build_int_cst (NULL_TREE, len - 1);
20528       TREE_TYPE (t)
20529         = build_array_type (char_type_node, build_index_type (tlen));
20530       rtl = lookup_constant_def (t);
20531       if (!rtl || !MEM_P (rtl))
20532         return 1;
20533       rtl = XEXP (rtl, 0);
20534       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
20535       *addr = rtl;
20536       return 0;
20537     }
20538
20539   if (GET_CODE (rtl) == SYMBOL_REF
20540       && SYMBOL_REF_DECL (rtl)
20541       && TREE_CODE (SYMBOL_REF_DECL (rtl)) == VAR_DECL
20542       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
20543     return 1;
20544
20545   if (GET_CODE (rtl) == CONST
20546       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
20547     return 1;
20548
20549   return 0;
20550 }
20551
20552 /* Helper function for resolve_addr, handle one location
20553    expression, return false if at least one CONST_STRING or SYMBOL_REF in
20554    the location list couldn't be resolved.  */
20555
20556 static bool
20557 resolve_addr_in_expr (dw_loc_descr_ref loc)
20558 {
20559   for (; loc; loc = loc->dw_loc_next)
20560     if ((loc->dw_loc_opc == DW_OP_addr
20561          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
20562         || (loc->dw_loc_opc == DW_OP_implicit_value
20563             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
20564             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
20565       return false;
20566   return true;
20567 }
20568
20569 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
20570    an address in .rodata section if the string literal is emitted there,
20571    or remove the containing location list or replace DW_AT_const_value
20572    with DW_AT_location and empty location expression, if it isn't found
20573    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
20574    to something that has been emitted in the current CU.  */
20575
20576 static void
20577 resolve_addr (dw_die_ref die)
20578 {
20579   dw_die_ref c;
20580   dw_attr_ref a;
20581   dw_loc_list_ref curr;
20582   unsigned ix;
20583
20584   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
20585     switch (AT_class (a))
20586       {
20587       case dw_val_class_loc_list:
20588         for (curr = AT_loc_list (a); curr != NULL; curr = curr->dw_loc_next)
20589           if (!resolve_addr_in_expr (curr->expr))
20590             curr->expr = NULL;
20591         break;
20592       case dw_val_class_loc:
20593         if (!resolve_addr_in_expr (AT_loc (a)))
20594           a->dw_attr_val.v.val_loc = NULL;
20595         break;
20596       case dw_val_class_addr:
20597         if (a->dw_attr == DW_AT_const_value
20598             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
20599           {
20600             a->dw_attr = DW_AT_location;
20601             a->dw_attr_val.val_class = dw_val_class_loc;
20602             a->dw_attr_val.v.val_loc = NULL;
20603           }
20604         break;
20605       default:
20606         break;
20607       }
20608
20609   FOR_EACH_CHILD (die, c, resolve_addr (c));
20610 }
20611
20612 /* Output stuff that dwarf requires at the end of every file,
20613    and generate the DWARF-2 debugging info.  */
20614
20615 static void
20616 dwarf2out_finish (const char *filename)
20617 {
20618   limbo_die_node *node, *next_node;
20619   comdat_type_node *ctnode;
20620   htab_t comdat_type_table;
20621   dw_die_ref die = 0;
20622   unsigned int i;
20623
20624   gen_remaining_tmpl_value_param_die_attribute ();
20625
20626   /* Add the name for the main input file now.  We delayed this from
20627      dwarf2out_init to avoid complications with PCH.  */
20628   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
20629   if (!IS_ABSOLUTE_PATH (filename))
20630     add_comp_dir_attribute (comp_unit_die);
20631   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
20632     {
20633       bool p = false;
20634       htab_traverse (file_table, file_table_relative_p, &p);
20635       if (p)
20636         add_comp_dir_attribute (comp_unit_die);
20637     }
20638
20639   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
20640     {
20641       add_location_or_const_value_attribute (
20642         VEC_index (deferred_locations, deferred_locations_list, i)->die,
20643         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
20644         DW_AT_location);
20645     }
20646
20647   /* Traverse the limbo die list, and add parent/child links.  The only
20648      dies without parents that should be here are concrete instances of
20649      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
20650      For concrete instances, we can get the parent die from the abstract
20651      instance.  */
20652   for (node = limbo_die_list; node; node = next_node)
20653     {
20654       next_node = node->next;
20655       die = node->die;
20656
20657       if (die->die_parent == NULL)
20658         {
20659           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
20660
20661           if (origin)
20662             add_child_die (origin->die_parent, die);
20663           else if (die == comp_unit_die)
20664             ;
20665           else if (errorcount > 0 || sorrycount > 0)
20666             /* It's OK to be confused by errors in the input.  */
20667             add_child_die (comp_unit_die, die);
20668           else
20669             {
20670               /* In certain situations, the lexical block containing a
20671                  nested function can be optimized away, which results
20672                  in the nested function die being orphaned.  Likewise
20673                  with the return type of that nested function.  Force
20674                  this to be a child of the containing function.
20675
20676                  It may happen that even the containing function got fully
20677                  inlined and optimized out.  In that case we are lost and
20678                  assign the empty child.  This should not be big issue as
20679                  the function is likely unreachable too.  */
20680               tree context = NULL_TREE;
20681
20682               gcc_assert (node->created_for);
20683
20684               if (DECL_P (node->created_for))
20685                 context = DECL_CONTEXT (node->created_for);
20686               else if (TYPE_P (node->created_for))
20687                 context = TYPE_CONTEXT (node->created_for);
20688
20689               gcc_assert (context
20690                           && (TREE_CODE (context) == FUNCTION_DECL
20691                               || TREE_CODE (context) == NAMESPACE_DECL));
20692
20693               origin = lookup_decl_die (context);
20694               if (origin)
20695                 add_child_die (origin, die);
20696               else
20697                 add_child_die (comp_unit_die, die);
20698             }
20699         }
20700     }
20701
20702   limbo_die_list = NULL;
20703
20704   resolve_addr (comp_unit_die);
20705
20706   for (node = deferred_asm_name; node; node = node->next)
20707     {
20708       tree decl = node->created_for;
20709       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
20710         {
20711           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
20712                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
20713           move_linkage_attr (node->die);
20714         }
20715     }
20716
20717   deferred_asm_name = NULL;
20718
20719   /* Walk through the list of incomplete types again, trying once more to
20720      emit full debugging info for them.  */
20721   retry_incomplete_types ();
20722
20723   if (flag_eliminate_unused_debug_types)
20724     prune_unused_types ();
20725
20726   /* Generate separate CUs for each of the include files we've seen.
20727      They will go into limbo_die_list.  */
20728   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
20729     break_out_includes (comp_unit_die);
20730
20731   /* Generate separate COMDAT sections for type DIEs. */
20732   if (dwarf_version >= 4)
20733     {
20734       break_out_comdat_types (comp_unit_die);
20735
20736       /* Each new type_unit DIE was added to the limbo die list when created.
20737          Since these have all been added to comdat_type_list, clear the
20738          limbo die list.  */
20739       limbo_die_list = NULL;
20740
20741       /* For each new comdat type unit, copy declarations for incomplete
20742          types to make the new unit self-contained (i.e., no direct
20743          references to the main compile unit).  */
20744       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
20745         copy_decls_for_unworthy_types (ctnode->root_die);
20746       copy_decls_for_unworthy_types (comp_unit_die);
20747
20748       /* In the process of copying declarations from one unit to another,
20749          we may have left some declarations behind that are no longer
20750          referenced.  Prune them.  */
20751       prune_unused_types ();
20752     }
20753
20754   /* Traverse the DIE's and add add sibling attributes to those DIE's
20755      that have children.  */
20756   add_sibling_attributes (comp_unit_die);
20757   for (node = limbo_die_list; node; node = node->next)
20758     add_sibling_attributes (node->die);
20759   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
20760     add_sibling_attributes (ctnode->root_die);
20761
20762   /* Output a terminator label for the .text section.  */
20763   switch_to_section (text_section);
20764   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
20765   if (flag_reorder_blocks_and_partition)
20766     {
20767       switch_to_section (unlikely_text_section ());
20768       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
20769     }
20770
20771   /* We can only use the low/high_pc attributes if all of the code was
20772      in .text.  */
20773   if (!have_multiple_function_sections
20774       || !(dwarf_version >= 3 || !dwarf_strict))
20775     {
20776       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
20777       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
20778     }
20779
20780   else
20781     {
20782       unsigned fde_idx = 0;
20783
20784       /* We need to give .debug_loc and .debug_ranges an appropriate
20785          "base address".  Use zero so that these addresses become
20786          absolute.  Historically, we've emitted the unexpected
20787          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
20788          Emit both to give time for other tools to adapt.  */
20789       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
20790       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
20791
20792       add_AT_range_list (comp_unit_die, DW_AT_ranges,
20793                          add_ranges_by_labels (text_section_label,
20794                                                text_end_label));
20795       if (flag_reorder_blocks_and_partition)
20796         add_ranges_by_labels (cold_text_section_label,
20797                               cold_end_label);
20798
20799       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
20800         {
20801           dw_fde_ref fde = &fde_table[fde_idx];
20802
20803           if (fde->dw_fde_switched_sections)
20804             {
20805               if (!fde->in_std_section)
20806                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
20807                                       fde->dw_fde_hot_section_end_label);
20808               if (!fde->cold_in_std_section)
20809                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
20810                                       fde->dw_fde_unlikely_section_end_label);
20811             }
20812           else if (!fde->in_std_section)
20813             add_ranges_by_labels (fde->dw_fde_begin,
20814                                   fde->dw_fde_end);
20815         }
20816
20817       add_ranges (NULL);
20818     }
20819
20820   /* Output location list section if necessary.  */
20821   if (have_location_lists)
20822     {
20823       /* Output the location lists info.  */
20824       switch_to_section (debug_loc_section);
20825       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
20826                                    DEBUG_LOC_SECTION_LABEL, 0);
20827       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
20828       output_location_lists (die);
20829     }
20830
20831   if (debug_info_level >= DINFO_LEVEL_NORMAL)
20832     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
20833                     debug_line_section_label);
20834
20835   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20836     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
20837
20838   /* Output all of the compilation units.  We put the main one last so that
20839      the offsets are available to output_pubnames.  */
20840   for (node = limbo_die_list; node; node = node->next)
20841     output_comp_unit (node->die, 0);
20842
20843   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
20844   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
20845     {
20846       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
20847
20848       /* Don't output duplicate types.  */
20849       if (*slot != HTAB_EMPTY_ENTRY)
20850         continue;
20851
20852       /* Add a pointer to the line table for the main compilation unit
20853          so that the debugger can make sense of DW_AT_decl_file
20854          attributes.  */
20855       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20856         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
20857                         debug_line_section_label);
20858
20859       output_comdat_type_unit (ctnode);
20860       *slot = ctnode;
20861     }
20862   htab_delete (comdat_type_table);
20863
20864   /* Output the main compilation unit if non-empty or if .debug_macinfo
20865      has been emitted.  */
20866   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
20867
20868   /* Output the abbreviation table.  */
20869   switch_to_section (debug_abbrev_section);
20870   output_abbrev_section ();
20871
20872   /* Output public names table if necessary.  */
20873   if (!VEC_empty (pubname_entry, pubname_table))
20874     {
20875       switch_to_section (debug_pubnames_section);
20876       output_pubnames (pubname_table);
20877     }
20878
20879   /* Output public types table if necessary.  */
20880   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
20881      It shouldn't hurt to emit it always, since pure DWARF2 consumers
20882      simply won't look for the section.  */
20883   if (!VEC_empty (pubname_entry, pubtype_table))
20884     {
20885       switch_to_section (debug_pubtypes_section);
20886       output_pubnames (pubtype_table);
20887     }
20888
20889   /* Output the address range information.  We only put functions in the arange
20890      table, so don't write it out if we don't have any.  */
20891   if (fde_table_in_use)
20892     {
20893       switch_to_section (debug_aranges_section);
20894       output_aranges ();
20895     }
20896
20897   /* Output ranges section if necessary.  */
20898   if (ranges_table_in_use)
20899     {
20900       switch_to_section (debug_ranges_section);
20901       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
20902       output_ranges ();
20903     }
20904
20905   /* Output the source line correspondence table.  We must do this
20906      even if there is no line information.  Otherwise, on an empty
20907      translation unit, we will generate a present, but empty,
20908      .debug_info section.  IRIX 6.5 `nm' will then complain when
20909      examining the file.  This is done late so that any filenames
20910      used by the debug_info section are marked as 'used'.  */
20911   if (! DWARF2_ASM_LINE_DEBUG_INFO)
20912     {
20913       switch_to_section (debug_line_section);
20914       output_line_info ();
20915     }
20916
20917   /* Have to end the macro section.  */
20918   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20919     {
20920       switch_to_section (debug_macinfo_section);
20921       dw2_asm_output_data (1, 0, "End compilation unit");
20922     }
20923
20924   /* If we emitted any DW_FORM_strp form attribute, output the string
20925      table too.  */
20926   if (debug_str_hash)
20927     htab_traverse (debug_str_hash, output_indirect_string, NULL);
20928 }
20929 #else
20930
20931 /* This should never be used, but its address is needed for comparisons.  */
20932 const struct gcc_debug_hooks dwarf2_debug_hooks =
20933 {
20934   0,            /* init */
20935   0,            /* finish */
20936   0,            /* define */
20937   0,            /* undef */
20938   0,            /* start_source_file */
20939   0,            /* end_source_file */
20940   0,            /* begin_block */
20941   0,            /* end_block */
20942   0,            /* ignore_block */
20943   0,            /* source_line */
20944   0,            /* begin_prologue */
20945   0,            /* end_prologue */
20946   0,            /* end_epilogue */
20947   0,            /* begin_function */
20948   0,            /* end_function */
20949   0,            /* function_decl */
20950   0,            /* global_decl */
20951   0,            /* type_decl */
20952   0,            /* imported_module_or_decl */
20953   0,            /* deferred_inline_function */
20954   0,            /* outlining_inline_function */
20955   0,            /* label */
20956   0,            /* handle_pch */
20957   0,            /* var_location */
20958   0,            /* switch_text_section */
20959   0,            /* set_name */
20960   0             /* start_end_main_source_file */
20961 };
20962
20963 #endif /* DWARF2_DEBUGGING_INFO */
20964
20965 #include "gt-dwarf2out.h"