OSDN Git Service

* dwarf2out.c (dwarf2out_begin_function): Set cold_text_section
[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, 2010
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.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 "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.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 #include "tree-flow.h"
95
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
101
102 /* Define this macro to be a nonzero value if the directory specifications
103     which are output in the debug info should end with a separator.  */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106    from generating indirect strings in DWARF2 debug information, for instance
107    if your target is stuck with an old version of GDB that is unable to
108    process them properly or uses VMS Debug.  */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
114
115 /* ??? Poison these here until it can be done generically.  They've been
116    totally replaced in this file; make sure it stays that way.  */
117 #undef DWARF2_UNWIND_INFO
118 #undef DWARF2_FRAME_INFO
119 #if (GCC_VERSION >= 3000)
120  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
121 #endif
122
123 #ifndef INCOMING_RETURN_ADDR_RTX
124 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
125 #endif
126
127 /* Map register numbers held in the call frame info that gcc has
128    collected using DWARF_FRAME_REGNUM to those that should be output in
129    .debug_frame and .eh_frame.  */
130 #ifndef DWARF2_FRAME_REG_OUT
131 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
132 #endif
133
134 /* Save the result of dwarf2out_do_frame across PCH.  */
135 static GTY(()) bool saved_do_cfi_asm = 0;
136
137 /* Decide whether we want to emit frame unwind information for the current
138    translation unit.  */
139
140 int
141 dwarf2out_do_frame (void)
142 {
143   /* We want to emit correct CFA location expressions or lists, so we
144      have to return true if we're going to output debug info, even if
145      we're not going to output frame or unwind info.  */
146   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
147     return true;
148
149   if (saved_do_cfi_asm)
150     return true;
151
152   if (targetm.debug_unwind_info () == UI_DWARF2)
153     return true;
154
155   if ((flag_unwind_tables || flag_exceptions)
156       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
157     return true;
158
159   return false;
160 }
161
162 /* Decide whether to emit frame unwind via assembler directives.  */
163
164 int
165 dwarf2out_do_cfi_asm (void)
166 {
167   int enc;
168
169 #ifdef MIPS_DEBUGGING_INFO
170   return false;
171 #endif
172   if (saved_do_cfi_asm)
173     return true;
174   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
175     return false;
176   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
177     return false;
178
179   /* Make sure the personality encoding is one the assembler can support.
180      In particular, aligned addresses can't be handled.  */
181   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
182   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
183     return false;
184   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
185   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
186     return false;
187
188   /* If we can't get the assembler to emit only .debug_frame, and we don't need
189      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
190   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
191       && !flag_unwind_tables && !flag_exceptions
192       && targetm.except_unwind_info (&global_options) != UI_DWARF2)
193     return false;
194
195   saved_do_cfi_asm = true;
196   return true;
197 }
198
199 /* The size of the target's pointer type.  */
200 #ifndef PTR_SIZE
201 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
202 #endif
203
204 /* Array of RTXes referenced by the debugging information, which therefore
205    must be kept around forever.  */
206 static GTY(()) VEC(rtx,gc) *used_rtx_array;
207
208 /* A pointer to the base of a list of incomplete types which might be
209    completed at some later time.  incomplete_types_list needs to be a
210    VEC(tree,gc) because we want to tell the garbage collector about
211    it.  */
212 static GTY(()) VEC(tree,gc) *incomplete_types;
213
214 /* A pointer to the base of a table of references to declaration
215    scopes.  This table is a display which tracks the nesting
216    of declaration scopes at the current scope and containing
217    scopes.  This table is used to find the proper place to
218    define type declaration DIE's.  */
219 static GTY(()) VEC(tree,gc) *decl_scope_table;
220
221 /* Pointers to various DWARF2 sections.  */
222 static GTY(()) section *debug_info_section;
223 static GTY(()) section *debug_abbrev_section;
224 static GTY(()) section *debug_aranges_section;
225 static GTY(()) section *debug_macinfo_section;
226 static GTY(()) section *debug_line_section;
227 static GTY(()) section *debug_loc_section;
228 static GTY(()) section *debug_pubnames_section;
229 static GTY(()) section *debug_pubtypes_section;
230 static GTY(()) section *debug_dcall_section;
231 static GTY(()) section *debug_vcall_section;
232 static GTY(()) section *debug_str_section;
233 static GTY(()) section *debug_ranges_section;
234 static GTY(()) section *debug_frame_section;
235
236 /* Personality decl of current unit.  Used only when assembler does not support
237    personality CFI.  */
238 static GTY(()) rtx current_unit_personality;
239
240 /* How to start an assembler comment.  */
241 #ifndef ASM_COMMENT_START
242 #define ASM_COMMENT_START ";#"
243 #endif
244
245 typedef struct dw_cfi_struct *dw_cfi_ref;
246 typedef struct dw_fde_struct *dw_fde_ref;
247 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
248
249 /* Call frames are described using a sequence of Call Frame
250    Information instructions.  The register number, offset
251    and address fields are provided as possible operands;
252    their use is selected by the opcode field.  */
253
254 enum dw_cfi_oprnd_type {
255   dw_cfi_oprnd_unused,
256   dw_cfi_oprnd_reg_num,
257   dw_cfi_oprnd_offset,
258   dw_cfi_oprnd_addr,
259   dw_cfi_oprnd_loc
260 };
261
262 typedef union GTY(()) dw_cfi_oprnd_struct {
263   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
264   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
265   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
266   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
267 }
268 dw_cfi_oprnd;
269
270 typedef struct GTY(()) dw_cfi_struct {
271   dw_cfi_ref dw_cfi_next;
272   enum dwarf_call_frame_info dw_cfi_opc;
273   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
274     dw_cfi_oprnd1;
275   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
276     dw_cfi_oprnd2;
277 }
278 dw_cfi_node;
279
280 /* This is how we define the location of the CFA. We use to handle it
281    as REG + OFFSET all the time,  but now it can be more complex.
282    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
283    Instead of passing around REG and OFFSET, we pass a copy
284    of this structure.  */
285 typedef struct GTY(()) cfa_loc {
286   HOST_WIDE_INT offset;
287   HOST_WIDE_INT base_offset;
288   unsigned int reg;
289   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
290   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
291 } dw_cfa_location;
292
293 /* All call frame descriptions (FDE's) in the GCC generated DWARF
294    refer to a single Common Information Entry (CIE), defined at
295    the beginning of the .debug_frame section.  This use of a single
296    CIE obviates the need to keep track of multiple CIE's
297    in the DWARF generation routines below.  */
298
299 typedef struct GTY(()) dw_fde_struct {
300   tree decl;
301   const char *dw_fde_begin;
302   const char *dw_fde_current_label;
303   const char *dw_fde_end;
304   const char *dw_fde_vms_end_prologue;
305   const char *dw_fde_vms_begin_epilogue;
306   const char *dw_fde_hot_section_label;
307   const char *dw_fde_hot_section_end_label;
308   const char *dw_fde_unlikely_section_label;
309   const char *dw_fde_unlikely_section_end_label;
310   dw_cfi_ref dw_fde_cfi;
311   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
312   HOST_WIDE_INT stack_realignment;
313   unsigned funcdef_number;
314   /* Dynamic realign argument pointer register.  */
315   unsigned int drap_reg;
316   /* Virtual dynamic realign argument pointer register.  */
317   unsigned int vdrap_reg;
318   /* These 3 flags are copied from rtl_data in function.h.  */
319   unsigned all_throwers_are_sibcalls : 1;
320   unsigned uses_eh_lsda : 1;
321   unsigned nothrow : 1;
322   /* Whether we did stack realign in this call frame.  */
323   unsigned stack_realign : 1;
324   /* Whether dynamic realign argument pointer register has been saved.  */
325   unsigned drap_reg_saved: 1;
326   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
327   unsigned in_std_section : 1;
328   /* True iff dw_fde_unlikely_section_label is in text_section or
329      cold_text_section.  */
330   unsigned cold_in_std_section : 1;
331   /* True iff switched sections.  */
332   unsigned dw_fde_switched_sections : 1;
333   /* True iff switching from cold to hot section.  */
334   unsigned dw_fde_switched_cold_to_hot : 1;
335 }
336 dw_fde_node;
337
338 /* Maximum size (in bytes) of an artificially generated label.  */
339 #define MAX_ARTIFICIAL_LABEL_BYTES      30
340
341 /* The size of addresses as they appear in the Dwarf 2 data.
342    Some architectures use word addresses to refer to code locations,
343    but Dwarf 2 info always uses byte addresses.  On such machines,
344    Dwarf 2 addresses need to be larger than the architecture's
345    pointers.  */
346 #ifndef DWARF2_ADDR_SIZE
347 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
348 #endif
349
350 /* The size in bytes of a DWARF field indicating an offset or length
351    relative to a debug info section, specified to be 4 bytes in the
352    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
353    as PTR_SIZE.  */
354
355 #ifndef DWARF_OFFSET_SIZE
356 #define DWARF_OFFSET_SIZE 4
357 #endif
358
359 /* The size in bytes of a DWARF 4 type signature.  */
360
361 #ifndef DWARF_TYPE_SIGNATURE_SIZE
362 #define DWARF_TYPE_SIGNATURE_SIZE 8
363 #endif
364
365 /* According to the (draft) DWARF 3 specification, the initial length
366    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
367    bytes are 0xffffffff, followed by the length stored in the next 8
368    bytes.
369
370    However, the SGI/MIPS ABI uses an initial length which is equal to
371    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
372
373 #ifndef DWARF_INITIAL_LENGTH_SIZE
374 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
375 #endif
376
377 /* Round SIZE up to the nearest BOUNDARY.  */
378 #define DWARF_ROUND(SIZE,BOUNDARY) \
379   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
380
381 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
382 #ifndef DWARF_CIE_DATA_ALIGNMENT
383 #ifdef STACK_GROWS_DOWNWARD
384 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
385 #else
386 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
387 #endif
388 #endif
389
390 /* CIE identifier.  */
391 #if HOST_BITS_PER_WIDE_INT >= 64
392 #define DWARF_CIE_ID \
393   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
394 #else
395 #define DWARF_CIE_ID DW_CIE_ID
396 #endif
397
398 /* A pointer to the base of a table that contains frame description
399    information for each routine.  */
400 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
401
402 /* Number of elements currently allocated for fde_table.  */
403 static GTY(()) unsigned fde_table_allocated;
404
405 /* Number of elements in fde_table currently in use.  */
406 static GTY(()) unsigned fde_table_in_use;
407
408 /* Size (in elements) of increments by which we may expand the
409    fde_table.  */
410 #define FDE_TABLE_INCREMENT 256
411
412 /* Get the current fde_table entry we should use.  */
413
414 static inline dw_fde_ref
415 current_fde (void)
416 {
417   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
418 }
419
420 /* A list of call frame insns for the CIE.  */
421 static GTY(()) dw_cfi_ref cie_cfi_head;
422
423 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
424    attribute that accelerates the lookup of the FDE associated
425    with the subprogram.  This variable holds the table index of the FDE
426    associated with the current function (body) definition.  */
427 static unsigned current_funcdef_fde;
428
429 struct GTY(()) indirect_string_node {
430   const char *str;
431   unsigned int refcount;
432   enum dwarf_form form;
433   char *label;
434 };
435
436 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
437
438 /* True if the compilation unit has location entries that reference
439    debug strings.  */
440 static GTY(()) bool debug_str_hash_forced = false;
441
442 static GTY(()) int dw2_string_counter;
443 static GTY(()) unsigned long dwarf2out_cfi_label_num;
444
445 /* True if the compilation unit places functions in more than one section.  */
446 static GTY(()) bool have_multiple_function_sections = false;
447
448 /* Whether the default text and cold text sections have been used at all.  */
449
450 static GTY(()) bool text_section_used = false;
451 static GTY(()) bool cold_text_section_used = false;
452
453 /* The default cold text section.  */
454 static GTY(()) section *cold_text_section;
455
456 /* Forward declarations for functions defined in this file.  */
457
458 static char *stripattributes (const char *);
459 static const char *dwarf_cfi_name (unsigned);
460 static dw_cfi_ref new_cfi (void);
461 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
462 static void add_fde_cfi (const char *, dw_cfi_ref);
463 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
464 static void lookup_cfa (dw_cfa_location *);
465 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
466 static void initial_return_save (rtx);
467 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
468                                           HOST_WIDE_INT);
469 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
470 static void output_cfi_directive (dw_cfi_ref);
471 static void output_call_frame_info (int);
472 static void dwarf2out_note_section_used (void);
473 static bool clobbers_queued_reg_save (const_rtx);
474 static void dwarf2out_frame_debug_expr (rtx, const char *);
475
476 /* Support for complex CFA locations.  */
477 static void output_cfa_loc (dw_cfi_ref);
478 static void output_cfa_loc_raw (dw_cfi_ref);
479 static void get_cfa_from_loc_descr (dw_cfa_location *,
480                                     struct dw_loc_descr_struct *);
481 static struct dw_loc_descr_struct *build_cfa_loc
482   (dw_cfa_location *, HOST_WIDE_INT);
483 static struct dw_loc_descr_struct *build_cfa_aligned_loc
484   (HOST_WIDE_INT, HOST_WIDE_INT);
485 static void def_cfa_1 (const char *, dw_cfa_location *);
486 static struct dw_loc_descr_struct *mem_loc_descriptor
487   (rtx, enum machine_mode mode, enum var_init_status);
488
489 /* How to start an assembler comment.  */
490 #ifndef ASM_COMMENT_START
491 #define ASM_COMMENT_START ";#"
492 #endif
493
494 /* Data and reference forms for relocatable data.  */
495 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
496 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
497
498 #ifndef DEBUG_FRAME_SECTION
499 #define DEBUG_FRAME_SECTION     ".debug_frame"
500 #endif
501
502 #ifndef FUNC_BEGIN_LABEL
503 #define FUNC_BEGIN_LABEL        "LFB"
504 #endif
505
506 #ifndef FUNC_END_LABEL
507 #define FUNC_END_LABEL          "LFE"
508 #endif
509
510 #ifndef PROLOGUE_END_LABEL
511 #define PROLOGUE_END_LABEL      "LPE"
512 #endif
513
514 #ifndef EPILOGUE_BEGIN_LABEL
515 #define EPILOGUE_BEGIN_LABEL    "LEB"
516 #endif
517
518 #ifndef FRAME_BEGIN_LABEL
519 #define FRAME_BEGIN_LABEL       "Lframe"
520 #endif
521 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
522 #define CIE_END_LABEL           "LECIE"
523 #define FDE_LABEL               "LSFDE"
524 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
525 #define FDE_END_LABEL           "LEFDE"
526 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
527 #define LINE_NUMBER_END_LABEL   "LELT"
528 #define LN_PROLOG_AS_LABEL      "LASLTP"
529 #define LN_PROLOG_END_LABEL     "LELTP"
530 #define DIE_LABEL_PREFIX        "DW"
531
532 /* The DWARF 2 CFA column which tracks the return address.  Normally this
533    is the column for PC, or the first column after all of the hard
534    registers.  */
535 #ifndef DWARF_FRAME_RETURN_COLUMN
536 #ifdef PC_REGNUM
537 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
538 #else
539 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
540 #endif
541 #endif
542
543 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
544    default, we just provide columns for all registers.  */
545 #ifndef DWARF_FRAME_REGNUM
546 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
547 #endif
548 \f
549 /* Match the base name of a file to the base name of a compilation unit. */
550
551 static int
552 matches_main_base (const char *path)
553 {
554   /* Cache the last query. */
555   static const char *last_path = NULL;
556   static int last_match = 0;
557   if (path != last_path)
558     {
559       const char *base;
560       int length = base_of_path (path, &base);
561       last_path = path;
562       last_match = (length == main_input_baselength
563                     && memcmp (base, main_input_basename, length) == 0);
564     }
565   return last_match;
566 }
567
568 #ifdef DEBUG_DEBUG_STRUCT
569
570 static int
571 dump_struct_debug (tree type, enum debug_info_usage usage,
572                    enum debug_struct_file criterion, int generic,
573                    int matches, int result)
574 {
575   /* Find the type name. */
576   tree type_decl = TYPE_STUB_DECL (type);
577   tree t = type_decl;
578   const char *name = 0;
579   if (TREE_CODE (t) == TYPE_DECL)
580     t = DECL_NAME (t);
581   if (t)
582     name = IDENTIFIER_POINTER (t);
583
584   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
585            criterion,
586            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
587            matches ? "bas" : "hdr",
588            generic ? "gen" : "ord",
589            usage == DINFO_USAGE_DFN ? ";" :
590              usage == DINFO_USAGE_DIR_USE ? "." : "*",
591            result,
592            (void*) type_decl, name);
593   return result;
594 }
595 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
596   dump_struct_debug (type, usage, criterion, generic, matches, result)
597
598 #else
599
600 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
601   (result)
602
603 #endif
604
605 static bool
606 should_emit_struct_debug (tree type, enum debug_info_usage usage)
607 {
608   enum debug_struct_file criterion;
609   tree type_decl;
610   bool generic = lang_hooks.types.generic_p (type);
611
612   if (generic)
613     criterion = debug_struct_generic[usage];
614   else
615     criterion = debug_struct_ordinary[usage];
616
617   if (criterion == DINFO_STRUCT_FILE_NONE)
618     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
619   if (criterion == DINFO_STRUCT_FILE_ANY)
620     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
621
622   type_decl = TYPE_STUB_DECL (type);
623
624   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
625     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
626
627   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
628     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
629   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
630 }
631 \f
632 /* Hook used by __throw.  */
633
634 rtx
635 expand_builtin_dwarf_sp_column (void)
636 {
637   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
638   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
639 }
640
641 /* Return a pointer to a copy of the section string name S with all
642    attributes stripped off, and an asterisk prepended (for assemble_name).  */
643
644 static inline char *
645 stripattributes (const char *s)
646 {
647   char *stripped = XNEWVEC (char, strlen (s) + 2);
648   char *p = stripped;
649
650   *p++ = '*';
651
652   while (*s && *s != ',')
653     *p++ = *s++;
654
655   *p = '\0';
656   return stripped;
657 }
658
659 /* MEM is a memory reference for the register size table, each element of
660    which has mode MODE.  Initialize column C as a return address column.  */
661
662 static void
663 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
664 {
665   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
666   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
667   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
668 }
669
670 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
671
672 static inline HOST_WIDE_INT
673 div_data_align (HOST_WIDE_INT off)
674 {
675   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
676   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
677   return r;
678 }
679
680 /* Return true if we need a signed version of a given opcode
681    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
682
683 static inline bool
684 need_data_align_sf_opcode (HOST_WIDE_INT off)
685 {
686   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
687 }
688
689 /* Generate code to initialize the register size table.  */
690
691 void
692 expand_builtin_init_dwarf_reg_sizes (tree address)
693 {
694   unsigned int i;
695   enum machine_mode mode = TYPE_MODE (char_type_node);
696   rtx addr = expand_normal (address);
697   rtx mem = gen_rtx_MEM (BLKmode, addr);
698   bool wrote_return_column = false;
699
700   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
701     {
702       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
703
704       if (rnum < DWARF_FRAME_REGISTERS)
705         {
706           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
707           enum machine_mode save_mode = reg_raw_mode[i];
708           HOST_WIDE_INT size;
709
710           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
711             save_mode = choose_hard_reg_mode (i, 1, true);
712           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
713             {
714               if (save_mode == VOIDmode)
715                 continue;
716               wrote_return_column = true;
717             }
718           size = GET_MODE_SIZE (save_mode);
719           if (offset < 0)
720             continue;
721
722           emit_move_insn (adjust_address (mem, mode, offset),
723                           gen_int_mode (size, mode));
724         }
725     }
726
727   if (!wrote_return_column)
728     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
729
730 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
731   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
732 #endif
733
734   targetm.init_dwarf_reg_sizes_extra (address);
735 }
736
737 /* Convert a DWARF call frame info. operation to its string name */
738
739 static const char *
740 dwarf_cfi_name (unsigned int cfi_opc)
741 {
742   switch (cfi_opc)
743     {
744     case DW_CFA_advance_loc:
745       return "DW_CFA_advance_loc";
746     case DW_CFA_offset:
747       return "DW_CFA_offset";
748     case DW_CFA_restore:
749       return "DW_CFA_restore";
750     case DW_CFA_nop:
751       return "DW_CFA_nop";
752     case DW_CFA_set_loc:
753       return "DW_CFA_set_loc";
754     case DW_CFA_advance_loc1:
755       return "DW_CFA_advance_loc1";
756     case DW_CFA_advance_loc2:
757       return "DW_CFA_advance_loc2";
758     case DW_CFA_advance_loc4:
759       return "DW_CFA_advance_loc4";
760     case DW_CFA_offset_extended:
761       return "DW_CFA_offset_extended";
762     case DW_CFA_restore_extended:
763       return "DW_CFA_restore_extended";
764     case DW_CFA_undefined:
765       return "DW_CFA_undefined";
766     case DW_CFA_same_value:
767       return "DW_CFA_same_value";
768     case DW_CFA_register:
769       return "DW_CFA_register";
770     case DW_CFA_remember_state:
771       return "DW_CFA_remember_state";
772     case DW_CFA_restore_state:
773       return "DW_CFA_restore_state";
774     case DW_CFA_def_cfa:
775       return "DW_CFA_def_cfa";
776     case DW_CFA_def_cfa_register:
777       return "DW_CFA_def_cfa_register";
778     case DW_CFA_def_cfa_offset:
779       return "DW_CFA_def_cfa_offset";
780
781     /* DWARF 3 */
782     case DW_CFA_def_cfa_expression:
783       return "DW_CFA_def_cfa_expression";
784     case DW_CFA_expression:
785       return "DW_CFA_expression";
786     case DW_CFA_offset_extended_sf:
787       return "DW_CFA_offset_extended_sf";
788     case DW_CFA_def_cfa_sf:
789       return "DW_CFA_def_cfa_sf";
790     case DW_CFA_def_cfa_offset_sf:
791       return "DW_CFA_def_cfa_offset_sf";
792
793     /* SGI/MIPS specific */
794     case DW_CFA_MIPS_advance_loc8:
795       return "DW_CFA_MIPS_advance_loc8";
796
797     /* GNU extensions */
798     case DW_CFA_GNU_window_save:
799       return "DW_CFA_GNU_window_save";
800     case DW_CFA_GNU_args_size:
801       return "DW_CFA_GNU_args_size";
802     case DW_CFA_GNU_negative_offset_extended:
803       return "DW_CFA_GNU_negative_offset_extended";
804
805     default:
806       return "DW_CFA_<unknown>";
807     }
808 }
809
810 /* Return a pointer to a newly allocated Call Frame Instruction.  */
811
812 static inline dw_cfi_ref
813 new_cfi (void)
814 {
815   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
816
817   cfi->dw_cfi_next = NULL;
818   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
819   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
820
821   return cfi;
822 }
823
824 /* Add a Call Frame Instruction to list of instructions.  */
825
826 static inline void
827 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
828 {
829   dw_cfi_ref *p;
830   dw_fde_ref fde = current_fde ();
831
832   /* When DRAP is used, CFA is defined with an expression.  Redefine
833      CFA may lead to a different CFA value.   */
834   /* ??? Of course, this heuristic fails when we're annotating epilogues,
835      because of course we'll always want to redefine the CFA back to the
836      stack pointer on the way out.  Where should we move this check?  */
837   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
838     switch (cfi->dw_cfi_opc)
839       {
840         case DW_CFA_def_cfa_register:
841         case DW_CFA_def_cfa_offset:
842         case DW_CFA_def_cfa_offset_sf:
843         case DW_CFA_def_cfa:
844         case DW_CFA_def_cfa_sf:
845           gcc_unreachable ();
846
847         default:
848           break;
849       }
850
851   /* Find the end of the chain.  */
852   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
853     ;
854
855   *p = cfi;
856 }
857
858 /* Generate a new label for the CFI info to refer to.  FORCE is true
859    if a label needs to be output even when using .cfi_* directives.  */
860
861 char *
862 dwarf2out_cfi_label (bool force)
863 {
864   static char label[20];
865
866   if (!force && dwarf2out_do_cfi_asm ())
867     {
868       /* In this case, we will be emitting the asm directive instead of
869          the label, so just return a placeholder to keep the rest of the
870          interfaces happy.  */
871       strcpy (label, "<do not output>");
872     }
873   else
874     {
875       int num = dwarf2out_cfi_label_num++;
876       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
877       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
878     }
879
880   return label;
881 }
882
883 /* True if remember_state should be emitted before following CFI directive.  */
884 static bool emit_cfa_remember;
885
886 /* True if any CFI directives were emitted at the current insn.  */
887 static bool any_cfis_emitted;
888
889 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
890    or to the CIE if LABEL is NULL.  */
891
892 static void
893 add_fde_cfi (const char *label, dw_cfi_ref cfi)
894 {
895   dw_cfi_ref *list_head;
896
897   if (emit_cfa_remember)
898     {
899       dw_cfi_ref cfi_remember;
900
901       /* Emit the state save.  */
902       emit_cfa_remember = false;
903       cfi_remember = new_cfi ();
904       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
905       add_fde_cfi (label, cfi_remember);
906     }
907
908   list_head = &cie_cfi_head;
909
910   if (dwarf2out_do_cfi_asm ())
911     {
912       if (label)
913         {
914           dw_fde_ref fde = current_fde ();
915
916           gcc_assert (fde != NULL);
917
918           /* We still have to add the cfi to the list so that lookup_cfa
919              works later on.  When -g2 and above we even need to force
920              emitting of CFI labels and add to list a DW_CFA_set_loc for
921              convert_cfa_to_fb_loc_list purposes.  If we're generating
922              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
923              convert_cfa_to_fb_loc_list.  */
924           if (dwarf_version == 2
925               && debug_info_level > DINFO_LEVEL_TERSE
926               && (write_symbols == DWARF2_DEBUG
927                   || write_symbols == VMS_AND_DWARF2_DEBUG))
928             {
929               switch (cfi->dw_cfi_opc)
930                 {
931                 case DW_CFA_def_cfa_offset:
932                 case DW_CFA_def_cfa_offset_sf:
933                 case DW_CFA_def_cfa_register:
934                 case DW_CFA_def_cfa:
935                 case DW_CFA_def_cfa_sf:
936                 case DW_CFA_def_cfa_expression:
937                 case DW_CFA_restore_state:
938                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
939                     label = dwarf2out_cfi_label (true);
940
941                   if (fde->dw_fde_current_label == NULL
942                       || strcmp (label, fde->dw_fde_current_label) != 0)
943                     {
944                       dw_cfi_ref xcfi;
945
946                       label = xstrdup (label);
947
948                       /* Set the location counter to the new label.  */
949                       xcfi = new_cfi ();
950                       /* It doesn't metter whether DW_CFA_set_loc
951                          or DW_CFA_advance_loc4 is added here, those aren't
952                          emitted into assembly, only looked up by
953                          convert_cfa_to_fb_loc_list.  */
954                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
955                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
956                       add_cfi (&fde->dw_fde_cfi, xcfi);
957                       fde->dw_fde_current_label = label;
958                     }
959                   break;
960                 default:
961                   break;
962                 }
963             }
964
965           output_cfi_directive (cfi);
966
967           list_head = &fde->dw_fde_cfi;
968           any_cfis_emitted = true;
969         }
970       /* ??? If this is a CFI for the CIE, we don't emit.  This
971          assumes that the standard CIE contents that the assembler
972          uses matches the standard CIE contents that the compiler
973          uses.  This is probably a bad assumption.  I'm not quite
974          sure how to address this for now.  */
975     }
976   else if (label)
977     {
978       dw_fde_ref fde = current_fde ();
979
980       gcc_assert (fde != NULL);
981
982       if (*label == 0)
983         label = dwarf2out_cfi_label (false);
984
985       if (fde->dw_fde_current_label == NULL
986           || strcmp (label, fde->dw_fde_current_label) != 0)
987         {
988           dw_cfi_ref xcfi;
989
990           label = xstrdup (label);
991
992           /* Set the location counter to the new label.  */
993           xcfi = new_cfi ();
994           /* If we have a current label, advance from there, otherwise
995              set the location directly using set_loc.  */
996           xcfi->dw_cfi_opc = fde->dw_fde_current_label
997                              ? DW_CFA_advance_loc4
998                              : DW_CFA_set_loc;
999           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
1000           add_cfi (&fde->dw_fde_cfi, xcfi);
1001
1002           fde->dw_fde_current_label = label;
1003         }
1004
1005       list_head = &fde->dw_fde_cfi;
1006       any_cfis_emitted = true;
1007     }
1008
1009   add_cfi (list_head, cfi);
1010 }
1011
1012 /* Subroutine of lookup_cfa.  */
1013
1014 static void
1015 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1016 {
1017   switch (cfi->dw_cfi_opc)
1018     {
1019     case DW_CFA_def_cfa_offset:
1020     case DW_CFA_def_cfa_offset_sf:
1021       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1022       break;
1023     case DW_CFA_def_cfa_register:
1024       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1025       break;
1026     case DW_CFA_def_cfa:
1027     case DW_CFA_def_cfa_sf:
1028       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1029       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1030       break;
1031     case DW_CFA_def_cfa_expression:
1032       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1033       break;
1034
1035     case DW_CFA_remember_state:
1036       gcc_assert (!remember->in_use);
1037       *remember = *loc;
1038       remember->in_use = 1;
1039       break;
1040     case DW_CFA_restore_state:
1041       gcc_assert (remember->in_use);
1042       *loc = *remember;
1043       remember->in_use = 0;
1044       break;
1045
1046     default:
1047       break;
1048     }
1049 }
1050
1051 /* Find the previous value for the CFA.  */
1052
1053 static void
1054 lookup_cfa (dw_cfa_location *loc)
1055 {
1056   dw_cfi_ref cfi;
1057   dw_fde_ref fde;
1058   dw_cfa_location remember;
1059
1060   memset (loc, 0, sizeof (*loc));
1061   loc->reg = INVALID_REGNUM;
1062   remember = *loc;
1063
1064   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1065     lookup_cfa_1 (cfi, loc, &remember);
1066
1067   fde = current_fde ();
1068   if (fde)
1069     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1070       lookup_cfa_1 (cfi, loc, &remember);
1071 }
1072
1073 /* The current rule for calculating the DWARF2 canonical frame address.  */
1074 static dw_cfa_location cfa;
1075
1076 /* The register used for saving registers to the stack, and its offset
1077    from the CFA.  */
1078 static dw_cfa_location cfa_store;
1079
1080 /* The current save location around an epilogue.  */
1081 static dw_cfa_location cfa_remember;
1082
1083 /* The running total of the size of arguments pushed onto the stack.  */
1084 static HOST_WIDE_INT args_size;
1085
1086 /* The last args_size we actually output.  */
1087 static HOST_WIDE_INT old_args_size;
1088
1089 /* Entry point to update the canonical frame address (CFA).
1090    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1091    calculated from REG+OFFSET.  */
1092
1093 void
1094 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1095 {
1096   dw_cfa_location loc;
1097   loc.indirect = 0;
1098   loc.base_offset = 0;
1099   loc.reg = reg;
1100   loc.offset = offset;
1101   def_cfa_1 (label, &loc);
1102 }
1103
1104 /* Determine if two dw_cfa_location structures define the same data.  */
1105
1106 static bool
1107 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1108 {
1109   return (loc1->reg == loc2->reg
1110           && loc1->offset == loc2->offset
1111           && loc1->indirect == loc2->indirect
1112           && (loc1->indirect == 0
1113               || loc1->base_offset == loc2->base_offset));
1114 }
1115
1116 /* This routine does the actual work.  The CFA is now calculated from
1117    the dw_cfa_location structure.  */
1118
1119 static void
1120 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1121 {
1122   dw_cfi_ref cfi;
1123   dw_cfa_location old_cfa, loc;
1124
1125   cfa = *loc_p;
1126   loc = *loc_p;
1127
1128   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1129     cfa_store.offset = loc.offset;
1130
1131   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1132   lookup_cfa (&old_cfa);
1133
1134   /* If nothing changed, no need to issue any call frame instructions.  */
1135   if (cfa_equal_p (&loc, &old_cfa))
1136     return;
1137
1138   cfi = new_cfi ();
1139
1140   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1141     {
1142       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1143          the CFA register did not change but the offset did.  The data
1144          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1145          in the assembler via the .cfi_def_cfa_offset directive.  */
1146       if (loc.offset < 0)
1147         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1148       else
1149         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1150       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1151     }
1152
1153 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1154   else if (loc.offset == old_cfa.offset
1155            && old_cfa.reg != INVALID_REGNUM
1156            && !loc.indirect
1157            && !old_cfa.indirect)
1158     {
1159       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1160          indicating the CFA register has changed to <register> but the
1161          offset has not changed.  */
1162       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1163       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1164     }
1165 #endif
1166
1167   else if (loc.indirect == 0)
1168     {
1169       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1170          indicating the CFA register has changed to <register> with
1171          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1172          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1173          directive.  */
1174       if (loc.offset < 0)
1175         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1176       else
1177         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1178       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1179       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1180     }
1181   else
1182     {
1183       /* Construct a DW_CFA_def_cfa_expression instruction to
1184          calculate the CFA using a full location expression since no
1185          register-offset pair is available.  */
1186       struct dw_loc_descr_struct *loc_list;
1187
1188       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1189       loc_list = build_cfa_loc (&loc, 0);
1190       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1191     }
1192
1193   add_fde_cfi (label, cfi);
1194 }
1195
1196 /* Add the CFI for saving a register.  REG is the CFA column number.
1197    LABEL is passed to add_fde_cfi.
1198    If SREG is -1, the register is saved at OFFSET from the CFA;
1199    otherwise it is saved in SREG.  */
1200
1201 static void
1202 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1203 {
1204   dw_cfi_ref cfi = new_cfi ();
1205   dw_fde_ref fde = current_fde ();
1206
1207   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1208
1209   /* When stack is aligned, store REG using DW_CFA_expression with
1210      FP.  */
1211   if (fde
1212       && fde->stack_realign
1213       && sreg == INVALID_REGNUM)
1214     {
1215       cfi->dw_cfi_opc = DW_CFA_expression;
1216       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1217       cfi->dw_cfi_oprnd2.dw_cfi_loc
1218         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1219     }
1220   else if (sreg == INVALID_REGNUM)
1221     {
1222       if (need_data_align_sf_opcode (offset))
1223         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1224       else if (reg & ~0x3f)
1225         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1226       else
1227         cfi->dw_cfi_opc = DW_CFA_offset;
1228       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1229     }
1230   else if (sreg == reg)
1231     cfi->dw_cfi_opc = DW_CFA_same_value;
1232   else
1233     {
1234       cfi->dw_cfi_opc = DW_CFA_register;
1235       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1236     }
1237
1238   add_fde_cfi (label, cfi);
1239 }
1240
1241 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1242    This CFI tells the unwinder that it needs to restore the window registers
1243    from the previous frame's window save area.
1244
1245    ??? Perhaps we should note in the CIE where windows are saved (instead of
1246    assuming 0(cfa)) and what registers are in the window.  */
1247
1248 void
1249 dwarf2out_window_save (const char *label)
1250 {
1251   dw_cfi_ref cfi = new_cfi ();
1252
1253   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1254   add_fde_cfi (label, cfi);
1255 }
1256
1257 /* Entry point for saving a register to the stack.  REG is the GCC register
1258    number.  LABEL and OFFSET are passed to reg_save.  */
1259
1260 void
1261 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1262 {
1263   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1264 }
1265
1266 /* Entry point for saving the return address in the stack.
1267    LABEL and OFFSET are passed to reg_save.  */
1268
1269 void
1270 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1271 {
1272   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1273 }
1274
1275 /* Entry point for saving the return address in a register.
1276    LABEL and SREG are passed to reg_save.  */
1277
1278 void
1279 dwarf2out_return_reg (const char *label, unsigned int sreg)
1280 {
1281   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1282 }
1283
1284 /* Record the initial position of the return address.  RTL is
1285    INCOMING_RETURN_ADDR_RTX.  */
1286
1287 static void
1288 initial_return_save (rtx rtl)
1289 {
1290   unsigned int reg = INVALID_REGNUM;
1291   HOST_WIDE_INT offset = 0;
1292
1293   switch (GET_CODE (rtl))
1294     {
1295     case REG:
1296       /* RA is in a register.  */
1297       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1298       break;
1299
1300     case MEM:
1301       /* RA is on the stack.  */
1302       rtl = XEXP (rtl, 0);
1303       switch (GET_CODE (rtl))
1304         {
1305         case REG:
1306           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1307           offset = 0;
1308           break;
1309
1310         case PLUS:
1311           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1312           offset = INTVAL (XEXP (rtl, 1));
1313           break;
1314
1315         case MINUS:
1316           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1317           offset = -INTVAL (XEXP (rtl, 1));
1318           break;
1319
1320         default:
1321           gcc_unreachable ();
1322         }
1323
1324       break;
1325
1326     case PLUS:
1327       /* The return address is at some offset from any value we can
1328          actually load.  For instance, on the SPARC it is in %i7+8. Just
1329          ignore the offset for now; it doesn't matter for unwinding frames.  */
1330       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1331       initial_return_save (XEXP (rtl, 0));
1332       return;
1333
1334     default:
1335       gcc_unreachable ();
1336     }
1337
1338   if (reg != DWARF_FRAME_RETURN_COLUMN)
1339     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1340 }
1341
1342 /* Given a SET, calculate the amount of stack adjustment it
1343    contains.  */
1344
1345 static HOST_WIDE_INT
1346 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1347                      HOST_WIDE_INT cur_offset)
1348 {
1349   const_rtx src = SET_SRC (pattern);
1350   const_rtx dest = SET_DEST (pattern);
1351   HOST_WIDE_INT offset = 0;
1352   enum rtx_code code;
1353
1354   if (dest == stack_pointer_rtx)
1355     {
1356       code = GET_CODE (src);
1357
1358       /* Assume (set (reg sp) (reg whatever)) sets args_size
1359          level to 0.  */
1360       if (code == REG && src != stack_pointer_rtx)
1361         {
1362           offset = -cur_args_size;
1363 #ifndef STACK_GROWS_DOWNWARD
1364           offset = -offset;
1365 #endif
1366           return offset - cur_offset;
1367         }
1368
1369       if (! (code == PLUS || code == MINUS)
1370           || XEXP (src, 0) != stack_pointer_rtx
1371           || !CONST_INT_P (XEXP (src, 1)))
1372         return 0;
1373
1374       /* (set (reg sp) (plus (reg sp) (const_int))) */
1375       offset = INTVAL (XEXP (src, 1));
1376       if (code == PLUS)
1377         offset = -offset;
1378       return offset;
1379     }
1380
1381   if (MEM_P (src) && !MEM_P (dest))
1382     dest = src;
1383   if (MEM_P (dest))
1384     {
1385       /* (set (mem (pre_dec (reg sp))) (foo)) */
1386       src = XEXP (dest, 0);
1387       code = GET_CODE (src);
1388
1389       switch (code)
1390         {
1391         case PRE_MODIFY:
1392         case POST_MODIFY:
1393           if (XEXP (src, 0) == stack_pointer_rtx)
1394             {
1395               rtx val = XEXP (XEXP (src, 1), 1);
1396               /* We handle only adjustments by constant amount.  */
1397               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1398                           && CONST_INT_P (val));
1399               offset = -INTVAL (val);
1400               break;
1401             }
1402           return 0;
1403
1404         case PRE_DEC:
1405         case POST_DEC:
1406           if (XEXP (src, 0) == stack_pointer_rtx)
1407             {
1408               offset = GET_MODE_SIZE (GET_MODE (dest));
1409               break;
1410             }
1411           return 0;
1412
1413         case PRE_INC:
1414         case POST_INC:
1415           if (XEXP (src, 0) == stack_pointer_rtx)
1416             {
1417               offset = -GET_MODE_SIZE (GET_MODE (dest));
1418               break;
1419             }
1420           return 0;
1421
1422         default:
1423           return 0;
1424         }
1425     }
1426   else
1427     return 0;
1428
1429   return offset;
1430 }
1431
1432 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1433    indexed by INSN_UID.  */
1434
1435 static HOST_WIDE_INT *barrier_args_size;
1436
1437 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1438
1439 static HOST_WIDE_INT
1440 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1441                              VEC (rtx, heap) **next)
1442 {
1443   HOST_WIDE_INT offset = 0;
1444   int i;
1445
1446   if (! RTX_FRAME_RELATED_P (insn))
1447     {
1448       if (prologue_epilogue_contains (insn))
1449         /* Nothing */;
1450       else if (GET_CODE (PATTERN (insn)) == SET)
1451         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1452       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1453                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1454         {
1455           /* There may be stack adjustments inside compound insns.  Search
1456              for them.  */
1457           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1458             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1459               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1460                                              cur_args_size, offset);
1461         }
1462     }
1463   else
1464     {
1465       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1466
1467       if (expr)
1468         {
1469           expr = XEXP (expr, 0);
1470           if (GET_CODE (expr) == PARALLEL
1471               || GET_CODE (expr) == SEQUENCE)
1472             for (i = 1; i < XVECLEN (expr, 0); i++)
1473               {
1474                 rtx elem = XVECEXP (expr, 0, i);
1475
1476                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1477                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1478               }
1479         }
1480     }
1481
1482 #ifndef STACK_GROWS_DOWNWARD
1483   offset = -offset;
1484 #endif
1485
1486   cur_args_size += offset;
1487   if (cur_args_size < 0)
1488     cur_args_size = 0;
1489
1490   if (JUMP_P (insn))
1491     {
1492       rtx dest = JUMP_LABEL (insn);
1493
1494       if (dest)
1495         {
1496           if (barrier_args_size [INSN_UID (dest)] < 0)
1497             {
1498               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1499               VEC_safe_push (rtx, heap, *next, dest);
1500             }
1501         }
1502     }
1503
1504   return cur_args_size;
1505 }
1506
1507 /* Walk the whole function and compute args_size on BARRIERs.  */
1508
1509 static void
1510 compute_barrier_args_size (void)
1511 {
1512   int max_uid = get_max_uid (), i;
1513   rtx insn;
1514   VEC (rtx, heap) *worklist, *next, *tmp;
1515
1516   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1517   for (i = 0; i < max_uid; i++)
1518     barrier_args_size[i] = -1;
1519
1520   worklist = VEC_alloc (rtx, heap, 20);
1521   next = VEC_alloc (rtx, heap, 20);
1522   insn = get_insns ();
1523   barrier_args_size[INSN_UID (insn)] = 0;
1524   VEC_quick_push (rtx, worklist, insn);
1525   for (;;)
1526     {
1527       while (!VEC_empty (rtx, worklist))
1528         {
1529           rtx prev, body, first_insn;
1530           HOST_WIDE_INT cur_args_size;
1531
1532           first_insn = insn = VEC_pop (rtx, worklist);
1533           cur_args_size = barrier_args_size[INSN_UID (insn)];
1534           prev = prev_nonnote_insn (insn);
1535           if (prev && BARRIER_P (prev))
1536             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1537
1538           for (; insn; insn = NEXT_INSN (insn))
1539             {
1540               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1541                 continue;
1542               if (BARRIER_P (insn))
1543                 break;
1544
1545               if (LABEL_P (insn))
1546                 {
1547                   if (insn == first_insn)
1548                     continue;
1549                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1550                     {
1551                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1552                       continue;
1553                     }
1554                   else
1555                     {
1556                       /* The insns starting with this label have been
1557                          already scanned or are in the worklist.  */
1558                       break;
1559                     }
1560                 }
1561
1562               body = PATTERN (insn);
1563               if (GET_CODE (body) == SEQUENCE)
1564                 {
1565                   HOST_WIDE_INT dest_args_size = cur_args_size;
1566                   for (i = 1; i < XVECLEN (body, 0); i++)
1567                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1568                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1569                       dest_args_size
1570                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1571                                                        dest_args_size, &next);
1572                     else
1573                       cur_args_size
1574                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1575                                                        cur_args_size, &next);
1576
1577                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1578                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1579                                                  dest_args_size, &next);
1580                   else
1581                     cur_args_size
1582                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1583                                                      cur_args_size, &next);
1584                 }
1585               else
1586                 cur_args_size
1587                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1588             }
1589         }
1590
1591       if (VEC_empty (rtx, next))
1592         break;
1593
1594       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1595       tmp = next;
1596       next = worklist;
1597       worklist = tmp;
1598       VEC_truncate (rtx, next, 0);
1599     }
1600
1601   VEC_free (rtx, heap, worklist);
1602   VEC_free (rtx, heap, next);
1603 }
1604
1605 /* Add a CFI to update the running total of the size of arguments
1606    pushed onto the stack.  */
1607
1608 static void
1609 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1610 {
1611   dw_cfi_ref cfi;
1612
1613   if (size == old_args_size)
1614     return;
1615
1616   old_args_size = size;
1617
1618   cfi = new_cfi ();
1619   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1620   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1621   add_fde_cfi (label, cfi);
1622 }
1623
1624 /* Record a stack adjustment of OFFSET bytes.  */
1625
1626 static void
1627 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1628 {
1629   if (cfa.reg == STACK_POINTER_REGNUM)
1630     cfa.offset += offset;
1631
1632   if (cfa_store.reg == STACK_POINTER_REGNUM)
1633     cfa_store.offset += offset;
1634
1635   if (ACCUMULATE_OUTGOING_ARGS)
1636     return;
1637
1638 #ifndef STACK_GROWS_DOWNWARD
1639   offset = -offset;
1640 #endif
1641
1642   args_size += offset;
1643   if (args_size < 0)
1644     args_size = 0;
1645
1646   def_cfa_1 (label, &cfa);
1647   if (flag_asynchronous_unwind_tables)
1648     dwarf2out_args_size (label, args_size);
1649 }
1650
1651 /* Check INSN to see if it looks like a push or a stack adjustment, and
1652    make a note of it if it does.  EH uses this information to find out
1653    how much extra space it needs to pop off the stack.  */
1654
1655 static void
1656 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1657 {
1658   HOST_WIDE_INT offset;
1659   const char *label;
1660   int i;
1661
1662   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1663      with this function.  Proper support would require all frame-related
1664      insns to be marked, and to be able to handle saving state around
1665      epilogues textually in the middle of the function.  */
1666   if (prologue_epilogue_contains (insn))
1667     return;
1668
1669   /* If INSN is an instruction from target of an annulled branch, the
1670      effects are for the target only and so current argument size
1671      shouldn't change at all.  */
1672   if (final_sequence
1673       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1674       && INSN_FROM_TARGET_P (insn))
1675     return;
1676
1677   /* If only calls can throw, and we have a frame pointer,
1678      save up adjustments until we see the CALL_INSN.  */
1679   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1680     {
1681       if (CALL_P (insn) && !after_p)
1682         {
1683           /* Extract the size of the args from the CALL rtx itself.  */
1684           insn = PATTERN (insn);
1685           if (GET_CODE (insn) == PARALLEL)
1686             insn = XVECEXP (insn, 0, 0);
1687           if (GET_CODE (insn) == SET)
1688             insn = SET_SRC (insn);
1689           gcc_assert (GET_CODE (insn) == CALL);
1690           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1691         }
1692       return;
1693     }
1694
1695   if (CALL_P (insn) && !after_p)
1696     {
1697       if (!flag_asynchronous_unwind_tables)
1698         dwarf2out_args_size ("", args_size);
1699       return;
1700     }
1701   else if (BARRIER_P (insn))
1702     {
1703       /* Don't call compute_barrier_args_size () if the only
1704          BARRIER is at the end of function.  */
1705       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1706         compute_barrier_args_size ();
1707       if (barrier_args_size == NULL)
1708         offset = 0;
1709       else
1710         {
1711           offset = barrier_args_size[INSN_UID (insn)];
1712           if (offset < 0)
1713             offset = 0;
1714         }
1715
1716       offset -= args_size;
1717 #ifndef STACK_GROWS_DOWNWARD
1718       offset = -offset;
1719 #endif
1720     }
1721   else if (GET_CODE (PATTERN (insn)) == SET)
1722     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1723   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1724            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1725     {
1726       /* There may be stack adjustments inside compound insns.  Search
1727          for them.  */
1728       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1729         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1730           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1731                                          args_size, offset);
1732     }
1733   else
1734     return;
1735
1736   if (offset == 0)
1737     return;
1738
1739   label = dwarf2out_cfi_label (false);
1740   dwarf2out_stack_adjust (offset, label);
1741 }
1742
1743 /* We delay emitting a register save until either (a) we reach the end
1744    of the prologue or (b) the register is clobbered.  This clusters
1745    register saves so that there are fewer pc advances.  */
1746
1747 struct GTY(()) queued_reg_save {
1748   struct queued_reg_save *next;
1749   rtx reg;
1750   HOST_WIDE_INT cfa_offset;
1751   rtx saved_reg;
1752 };
1753
1754 static GTY(()) struct queued_reg_save *queued_reg_saves;
1755
1756 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1757 struct GTY(()) reg_saved_in_data {
1758   rtx orig_reg;
1759   rtx saved_in_reg;
1760 };
1761
1762 /* A list of registers saved in other registers.
1763    The list intentionally has a small maximum capacity of 4; if your
1764    port needs more than that, you might consider implementing a
1765    more efficient data structure.  */
1766 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1767 static GTY(()) size_t num_regs_saved_in_regs;
1768
1769 static const char *last_reg_save_label;
1770
1771 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1772    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1773
1774 static void
1775 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1776 {
1777   struct queued_reg_save *q;
1778
1779   /* Duplicates waste space, but it's also necessary to remove them
1780      for correctness, since the queue gets output in reverse
1781      order.  */
1782   for (q = queued_reg_saves; q != NULL; q = q->next)
1783     if (REGNO (q->reg) == REGNO (reg))
1784       break;
1785
1786   if (q == NULL)
1787     {
1788       q = ggc_alloc_queued_reg_save ();
1789       q->next = queued_reg_saves;
1790       queued_reg_saves = q;
1791     }
1792
1793   q->reg = reg;
1794   q->cfa_offset = offset;
1795   q->saved_reg = sreg;
1796
1797   last_reg_save_label = label;
1798 }
1799
1800 /* Output all the entries in QUEUED_REG_SAVES.  */
1801
1802 void
1803 dwarf2out_flush_queued_reg_saves (void)
1804 {
1805   struct queued_reg_save *q;
1806
1807   for (q = queued_reg_saves; q; q = q->next)
1808     {
1809       size_t i;
1810       unsigned int reg, sreg;
1811
1812       for (i = 0; i < num_regs_saved_in_regs; i++)
1813         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1814           break;
1815       if (q->saved_reg && i == num_regs_saved_in_regs)
1816         {
1817           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1818           num_regs_saved_in_regs++;
1819         }
1820       if (i != num_regs_saved_in_regs)
1821         {
1822           regs_saved_in_regs[i].orig_reg = q->reg;
1823           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1824         }
1825
1826       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1827       if (q->saved_reg)
1828         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1829       else
1830         sreg = INVALID_REGNUM;
1831       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1832     }
1833
1834   queued_reg_saves = NULL;
1835   last_reg_save_label = NULL;
1836 }
1837
1838 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1839    location for?  Or, does it clobber a register which we've previously
1840    said that some other register is saved in, and for which we now
1841    have a new location for?  */
1842
1843 static bool
1844 clobbers_queued_reg_save (const_rtx insn)
1845 {
1846   struct queued_reg_save *q;
1847
1848   for (q = queued_reg_saves; q; q = q->next)
1849     {
1850       size_t i;
1851       if (modified_in_p (q->reg, insn))
1852         return true;
1853       for (i = 0; i < num_regs_saved_in_regs; i++)
1854         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1855             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1856           return true;
1857     }
1858
1859   return false;
1860 }
1861
1862 /* Entry point for saving the first register into the second.  */
1863
1864 void
1865 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1866 {
1867   size_t i;
1868   unsigned int regno, sregno;
1869
1870   for (i = 0; i < num_regs_saved_in_regs; i++)
1871     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1872       break;
1873   if (i == num_regs_saved_in_regs)
1874     {
1875       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1876       num_regs_saved_in_regs++;
1877     }
1878   regs_saved_in_regs[i].orig_reg = reg;
1879   regs_saved_in_regs[i].saved_in_reg = sreg;
1880
1881   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1882   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1883   reg_save (label, regno, sregno, 0);
1884 }
1885
1886 /* What register, if any, is currently saved in REG?  */
1887
1888 static rtx
1889 reg_saved_in (rtx reg)
1890 {
1891   unsigned int regn = REGNO (reg);
1892   size_t i;
1893   struct queued_reg_save *q;
1894
1895   for (q = queued_reg_saves; q; q = q->next)
1896     if (q->saved_reg && regn == REGNO (q->saved_reg))
1897       return q->reg;
1898
1899   for (i = 0; i < num_regs_saved_in_regs; i++)
1900     if (regs_saved_in_regs[i].saved_in_reg
1901         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1902       return regs_saved_in_regs[i].orig_reg;
1903
1904   return NULL_RTX;
1905 }
1906
1907
1908 /* A temporary register holding an integral value used in adjusting SP
1909    or setting up the store_reg.  The "offset" field holds the integer
1910    value, not an offset.  */
1911 static dw_cfa_location cfa_temp;
1912
1913 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1914
1915 static void
1916 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1917 {
1918   memset (&cfa, 0, sizeof (cfa));
1919
1920   switch (GET_CODE (pat))
1921     {
1922     case PLUS:
1923       cfa.reg = REGNO (XEXP (pat, 0));
1924       cfa.offset = INTVAL (XEXP (pat, 1));
1925       break;
1926
1927     case REG:
1928       cfa.reg = REGNO (pat);
1929       break;
1930
1931     case MEM:
1932       cfa.indirect = 1;
1933       pat = XEXP (pat, 0);
1934       if (GET_CODE (pat) == PLUS)
1935         {
1936           cfa.base_offset = INTVAL (XEXP (pat, 1));
1937           pat = XEXP (pat, 0);
1938         }
1939       cfa.reg = REGNO (pat);
1940       break;
1941
1942     default:
1943       /* Recurse and define an expression.  */
1944       gcc_unreachable ();
1945     }
1946
1947   def_cfa_1 (label, &cfa);
1948 }
1949
1950 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1951
1952 static void
1953 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1954 {
1955   rtx src, dest;
1956
1957   gcc_assert (GET_CODE (pat) == SET);
1958   dest = XEXP (pat, 0);
1959   src = XEXP (pat, 1);
1960
1961   switch (GET_CODE (src))
1962     {
1963     case PLUS:
1964       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1965       cfa.offset -= INTVAL (XEXP (src, 1));
1966       break;
1967
1968     case REG:
1969         break;
1970
1971     default:
1972         gcc_unreachable ();
1973     }
1974
1975   cfa.reg = REGNO (dest);
1976   gcc_assert (cfa.indirect == 0);
1977
1978   def_cfa_1 (label, &cfa);
1979 }
1980
1981 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1982
1983 static void
1984 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1985 {
1986   HOST_WIDE_INT offset;
1987   rtx src, addr, span;
1988
1989   src = XEXP (set, 1);
1990   addr = XEXP (set, 0);
1991   gcc_assert (MEM_P (addr));
1992   addr = XEXP (addr, 0);
1993
1994   /* As documented, only consider extremely simple addresses.  */
1995   switch (GET_CODE (addr))
1996     {
1997     case REG:
1998       gcc_assert (REGNO (addr) == cfa.reg);
1999       offset = -cfa.offset;
2000       break;
2001     case PLUS:
2002       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
2003       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2004       break;
2005     default:
2006       gcc_unreachable ();
2007     }
2008
2009   span = targetm.dwarf_register_span (src);
2010
2011   /* ??? We'd like to use queue_reg_save, but we need to come up with
2012      a different flushing heuristic for epilogues.  */
2013   if (!span)
2014     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2015   else
2016     {
2017       /* We have a PARALLEL describing where the contents of SRC live.
2018          Queue register saves for each piece of the PARALLEL.  */
2019       int par_index;
2020       int limit;
2021       HOST_WIDE_INT span_offset = offset;
2022
2023       gcc_assert (GET_CODE (span) == PARALLEL);
2024
2025       limit = XVECLEN (span, 0);
2026       for (par_index = 0; par_index < limit; par_index++)
2027         {
2028           rtx elem = XVECEXP (span, 0, par_index);
2029
2030           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2031                     INVALID_REGNUM, span_offset);
2032           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2033         }
2034     }
2035 }
2036
2037 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2038
2039 static void
2040 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2041 {
2042   rtx src, dest;
2043   unsigned sregno, dregno;
2044
2045   src = XEXP (set, 1);
2046   dest = XEXP (set, 0);
2047
2048   if (src == pc_rtx)
2049     sregno = DWARF_FRAME_RETURN_COLUMN;
2050   else
2051     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2052
2053   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2054
2055   /* ??? We'd like to use queue_reg_save, but we need to come up with
2056      a different flushing heuristic for epilogues.  */
2057   reg_save (label, sregno, dregno, 0);
2058 }
2059
2060 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2061
2062 static void
2063 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2064 {
2065   rtx src, dest, span;
2066   dw_cfi_ref cfi = new_cfi ();
2067
2068   dest = SET_DEST (set);
2069   src = SET_SRC (set);
2070
2071   gcc_assert (REG_P (src));
2072   gcc_assert (MEM_P (dest));
2073
2074   span = targetm.dwarf_register_span (src);
2075   gcc_assert (!span);
2076
2077   cfi->dw_cfi_opc = DW_CFA_expression;
2078   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2079   cfi->dw_cfi_oprnd2.dw_cfi_loc
2080     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2081                           VAR_INIT_STATUS_INITIALIZED);
2082
2083   /* ??? We'd like to use queue_reg_save, were the interface different,
2084      and, as above, we could manage flushing for epilogues.  */
2085   add_fde_cfi (label, cfi);
2086 }
2087
2088 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2089
2090 static void
2091 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2092 {
2093   dw_cfi_ref cfi = new_cfi ();
2094   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2095
2096   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2097   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2098
2099   add_fde_cfi (label, cfi);
2100 }
2101
2102 /* Record call frame debugging information for an expression EXPR,
2103    which either sets SP or FP (adjusting how we calculate the frame
2104    address) or saves a register to the stack or another register.
2105    LABEL indicates the address of EXPR.
2106
2107    This function encodes a state machine mapping rtxes to actions on
2108    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2109    users need not read the source code.
2110
2111   The High-Level Picture
2112
2113   Changes in the register we use to calculate the CFA: Currently we
2114   assume that if you copy the CFA register into another register, we
2115   should take the other one as the new CFA register; this seems to
2116   work pretty well.  If it's wrong for some target, it's simple
2117   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2118
2119   Changes in the register we use for saving registers to the stack:
2120   This is usually SP, but not always.  Again, we deduce that if you
2121   copy SP into another register (and SP is not the CFA register),
2122   then the new register is the one we will be using for register
2123   saves.  This also seems to work.
2124
2125   Register saves: There's not much guesswork about this one; if
2126   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2127   register save, and the register used to calculate the destination
2128   had better be the one we think we're using for this purpose.
2129   It's also assumed that a copy from a call-saved register to another
2130   register is saving that register if RTX_FRAME_RELATED_P is set on
2131   that instruction.  If the copy is from a call-saved register to
2132   the *same* register, that means that the register is now the same
2133   value as in the caller.
2134
2135   Except: If the register being saved is the CFA register, and the
2136   offset is nonzero, we are saving the CFA, so we assume we have to
2137   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2138   the intent is to save the value of SP from the previous frame.
2139
2140   In addition, if a register has previously been saved to a different
2141   register,
2142
2143   Invariants / Summaries of Rules
2144
2145   cfa          current rule for calculating the CFA.  It usually
2146                consists of a register and an offset.
2147   cfa_store    register used by prologue code to save things to the stack
2148                cfa_store.offset is the offset from the value of
2149                cfa_store.reg to the actual CFA
2150   cfa_temp     register holding an integral value.  cfa_temp.offset
2151                stores the value, which will be used to adjust the
2152                stack pointer.  cfa_temp is also used like cfa_store,
2153                to track stores to the stack via fp or a temp reg.
2154
2155   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2156                with cfa.reg as the first operand changes the cfa.reg and its
2157                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2158                cfa_temp.offset.
2159
2160   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2161                expression yielding a constant.  This sets cfa_temp.reg
2162                and cfa_temp.offset.
2163
2164   Rule 5:      Create a new register cfa_store used to save items to the
2165                stack.
2166
2167   Rules 10-14: Save a register to the stack.  Define offset as the
2168                difference of the original location and cfa_store's
2169                location (or cfa_temp's location if cfa_temp is used).
2170
2171   Rules 16-20: If AND operation happens on sp in prologue, we assume
2172                stack is realigned.  We will use a group of DW_OP_XXX
2173                expressions to represent the location of the stored
2174                register instead of CFA+offset.
2175
2176   The Rules
2177
2178   "{a,b}" indicates a choice of a xor b.
2179   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2180
2181   Rule 1:
2182   (set <reg1> <reg2>:cfa.reg)
2183   effects: cfa.reg = <reg1>
2184            cfa.offset unchanged
2185            cfa_temp.reg = <reg1>
2186            cfa_temp.offset = cfa.offset
2187
2188   Rule 2:
2189   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2190                               {<const_int>,<reg>:cfa_temp.reg}))
2191   effects: cfa.reg = sp if fp used
2192            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2193            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2194              if cfa_store.reg==sp
2195
2196   Rule 3:
2197   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2198   effects: cfa.reg = fp
2199            cfa_offset += +/- <const_int>
2200
2201   Rule 4:
2202   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2203   constraints: <reg1> != fp
2204                <reg1> != sp
2205   effects: cfa.reg = <reg1>
2206            cfa_temp.reg = <reg1>
2207            cfa_temp.offset = cfa.offset
2208
2209   Rule 5:
2210   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2211   constraints: <reg1> != fp
2212                <reg1> != sp
2213   effects: cfa_store.reg = <reg1>
2214            cfa_store.offset = cfa.offset - cfa_temp.offset
2215
2216   Rule 6:
2217   (set <reg> <const_int>)
2218   effects: cfa_temp.reg = <reg>
2219            cfa_temp.offset = <const_int>
2220
2221   Rule 7:
2222   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2223   effects: cfa_temp.reg = <reg1>
2224            cfa_temp.offset |= <const_int>
2225
2226   Rule 8:
2227   (set <reg> (high <exp>))
2228   effects: none
2229
2230   Rule 9:
2231   (set <reg> (lo_sum <exp> <const_int>))
2232   effects: cfa_temp.reg = <reg>
2233            cfa_temp.offset = <const_int>
2234
2235   Rule 10:
2236   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2237   effects: cfa_store.offset -= <const_int>
2238            cfa.offset = cfa_store.offset if cfa.reg == sp
2239            cfa.reg = sp
2240            cfa.base_offset = -cfa_store.offset
2241
2242   Rule 11:
2243   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2244   effects: cfa_store.offset += -/+ mode_size(mem)
2245            cfa.offset = cfa_store.offset if cfa.reg == sp
2246            cfa.reg = sp
2247            cfa.base_offset = -cfa_store.offset
2248
2249   Rule 12:
2250   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2251
2252        <reg2>)
2253   effects: cfa.reg = <reg1>
2254            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2255
2256   Rule 13:
2257   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2258   effects: cfa.reg = <reg1>
2259            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2260
2261   Rule 14:
2262   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2263   effects: cfa.reg = <reg1>
2264            cfa.base_offset = -cfa_temp.offset
2265            cfa_temp.offset -= mode_size(mem)
2266
2267   Rule 15:
2268   (set <reg> {unspec, unspec_volatile})
2269   effects: target-dependent
2270
2271   Rule 16:
2272   (set sp (and: sp <const_int>))
2273   constraints: cfa_store.reg == sp
2274   effects: current_fde.stack_realign = 1
2275            cfa_store.offset = 0
2276            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2277
2278   Rule 17:
2279   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2280   effects: cfa_store.offset += -/+ mode_size(mem)
2281
2282   Rule 18:
2283   (set (mem ({pre_inc, pre_dec} sp)) fp)
2284   constraints: fde->stack_realign == 1
2285   effects: cfa_store.offset = 0
2286            cfa.reg != HARD_FRAME_POINTER_REGNUM
2287
2288   Rule 19:
2289   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2290   constraints: fde->stack_realign == 1
2291                && cfa.offset == 0
2292                && cfa.indirect == 0
2293                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2294   effects: Use DW_CFA_def_cfa_expression to define cfa
2295            cfa.reg == fde->drap_reg  */
2296
2297 static void
2298 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2299 {
2300   rtx src, dest, span;
2301   HOST_WIDE_INT offset;
2302   dw_fde_ref fde;
2303
2304   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2305      the PARALLEL independently. The first element is always processed if
2306      it is a SET. This is for backward compatibility.   Other elements
2307      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2308      flag is set in them.  */
2309   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2310     {
2311       int par_index;
2312       int limit = XVECLEN (expr, 0);
2313       rtx elem;
2314
2315       /* PARALLELs have strict read-modify-write semantics, so we
2316          ought to evaluate every rvalue before changing any lvalue.
2317          It's cumbersome to do that in general, but there's an
2318          easy approximation that is enough for all current users:
2319          handle register saves before register assignments.  */
2320       if (GET_CODE (expr) == PARALLEL)
2321         for (par_index = 0; par_index < limit; par_index++)
2322           {
2323             elem = XVECEXP (expr, 0, par_index);
2324             if (GET_CODE (elem) == SET
2325                 && MEM_P (SET_DEST (elem))
2326                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2327               dwarf2out_frame_debug_expr (elem, label);
2328           }
2329
2330       for (par_index = 0; par_index < limit; par_index++)
2331         {
2332           elem = XVECEXP (expr, 0, par_index);
2333           if (GET_CODE (elem) == SET
2334               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2335               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2336             dwarf2out_frame_debug_expr (elem, label);
2337           else if (GET_CODE (elem) == SET
2338                    && par_index != 0
2339                    && !RTX_FRAME_RELATED_P (elem))
2340             {
2341               /* Stack adjustment combining might combine some post-prologue
2342                  stack adjustment into a prologue stack adjustment.  */
2343               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2344
2345               if (offset != 0)
2346                 dwarf2out_stack_adjust (offset, label);
2347             }
2348         }
2349       return;
2350     }
2351
2352   gcc_assert (GET_CODE (expr) == SET);
2353
2354   src = SET_SRC (expr);
2355   dest = SET_DEST (expr);
2356
2357   if (REG_P (src))
2358     {
2359       rtx rsi = reg_saved_in (src);
2360       if (rsi)
2361         src = rsi;
2362     }
2363
2364   fde = current_fde ();
2365
2366   switch (GET_CODE (dest))
2367     {
2368     case REG:
2369       switch (GET_CODE (src))
2370         {
2371           /* Setting FP from SP.  */
2372         case REG:
2373           if (cfa.reg == (unsigned) REGNO (src))
2374             {
2375               /* Rule 1 */
2376               /* Update the CFA rule wrt SP or FP.  Make sure src is
2377                  relative to the current CFA register.
2378
2379                  We used to require that dest be either SP or FP, but the
2380                  ARM copies SP to a temporary register, and from there to
2381                  FP.  So we just rely on the backends to only set
2382                  RTX_FRAME_RELATED_P on appropriate insns.  */
2383               cfa.reg = REGNO (dest);
2384               cfa_temp.reg = cfa.reg;
2385               cfa_temp.offset = cfa.offset;
2386             }
2387           else
2388             {
2389               /* Saving a register in a register.  */
2390               gcc_assert (!fixed_regs [REGNO (dest)]
2391                           /* For the SPARC and its register window.  */
2392                           || (DWARF_FRAME_REGNUM (REGNO (src))
2393                               == DWARF_FRAME_RETURN_COLUMN));
2394
2395               /* After stack is aligned, we can only save SP in FP
2396                  if drap register is used.  In this case, we have
2397                  to restore stack pointer with the CFA value and we
2398                  don't generate this DWARF information.  */
2399               if (fde
2400                   && fde->stack_realign
2401                   && REGNO (src) == STACK_POINTER_REGNUM)
2402                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2403                             && fde->drap_reg != INVALID_REGNUM
2404                             && cfa.reg != REGNO (src));
2405               else
2406                 queue_reg_save (label, src, dest, 0);
2407             }
2408           break;
2409
2410         case PLUS:
2411         case MINUS:
2412         case LO_SUM:
2413           if (dest == stack_pointer_rtx)
2414             {
2415               /* Rule 2 */
2416               /* Adjusting SP.  */
2417               switch (GET_CODE (XEXP (src, 1)))
2418                 {
2419                 case CONST_INT:
2420                   offset = INTVAL (XEXP (src, 1));
2421                   break;
2422                 case REG:
2423                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2424                               == cfa_temp.reg);
2425                   offset = cfa_temp.offset;
2426                   break;
2427                 default:
2428                   gcc_unreachable ();
2429                 }
2430
2431               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2432                 {
2433                   /* Restoring SP from FP in the epilogue.  */
2434                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2435                   cfa.reg = STACK_POINTER_REGNUM;
2436                 }
2437               else if (GET_CODE (src) == LO_SUM)
2438                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2439                 ;
2440               else
2441                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2442
2443               if (GET_CODE (src) != MINUS)
2444                 offset = -offset;
2445               if (cfa.reg == STACK_POINTER_REGNUM)
2446                 cfa.offset += offset;
2447               if (cfa_store.reg == STACK_POINTER_REGNUM)
2448                 cfa_store.offset += offset;
2449             }
2450           else if (dest == hard_frame_pointer_rtx)
2451             {
2452               /* Rule 3 */
2453               /* Either setting the FP from an offset of the SP,
2454                  or adjusting the FP */
2455               gcc_assert (frame_pointer_needed);
2456
2457               gcc_assert (REG_P (XEXP (src, 0))
2458                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2459                           && CONST_INT_P (XEXP (src, 1)));
2460               offset = INTVAL (XEXP (src, 1));
2461               if (GET_CODE (src) != MINUS)
2462                 offset = -offset;
2463               cfa.offset += offset;
2464               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2465             }
2466           else
2467             {
2468               gcc_assert (GET_CODE (src) != MINUS);
2469
2470               /* Rule 4 */
2471               if (REG_P (XEXP (src, 0))
2472                   && REGNO (XEXP (src, 0)) == cfa.reg
2473                   && CONST_INT_P (XEXP (src, 1)))
2474                 {
2475                   /* Setting a temporary CFA register that will be copied
2476                      into the FP later on.  */
2477                   offset = - INTVAL (XEXP (src, 1));
2478                   cfa.offset += offset;
2479                   cfa.reg = REGNO (dest);
2480                   /* Or used to save regs to the stack.  */
2481                   cfa_temp.reg = cfa.reg;
2482                   cfa_temp.offset = cfa.offset;
2483                 }
2484
2485               /* Rule 5 */
2486               else if (REG_P (XEXP (src, 0))
2487                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2488                        && XEXP (src, 1) == stack_pointer_rtx)
2489                 {
2490                   /* Setting a scratch register that we will use instead
2491                      of SP for saving registers to the stack.  */
2492                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2493                   cfa_store.reg = REGNO (dest);
2494                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2495                 }
2496
2497               /* Rule 9 */
2498               else if (GET_CODE (src) == LO_SUM
2499                        && CONST_INT_P (XEXP (src, 1)))
2500                 {
2501                   cfa_temp.reg = REGNO (dest);
2502                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2503                 }
2504               else
2505                 gcc_unreachable ();
2506             }
2507           break;
2508
2509           /* Rule 6 */
2510         case CONST_INT:
2511           cfa_temp.reg = REGNO (dest);
2512           cfa_temp.offset = INTVAL (src);
2513           break;
2514
2515           /* Rule 7 */
2516         case IOR:
2517           gcc_assert (REG_P (XEXP (src, 0))
2518                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2519                       && CONST_INT_P (XEXP (src, 1)));
2520
2521           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2522             cfa_temp.reg = REGNO (dest);
2523           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2524           break;
2525
2526           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2527              which will fill in all of the bits.  */
2528           /* Rule 8 */
2529         case HIGH:
2530           break;
2531
2532           /* Rule 15 */
2533         case UNSPEC:
2534         case UNSPEC_VOLATILE:
2535           gcc_assert (targetm.dwarf_handle_frame_unspec);
2536           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2537           return;
2538
2539           /* Rule 16 */
2540         case AND:
2541           /* If this AND operation happens on stack pointer in prologue,
2542              we assume the stack is realigned and we extract the
2543              alignment.  */
2544           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2545             {
2546               /* We interpret reg_save differently with stack_realign set.
2547                  Thus we must flush whatever we have queued first.  */
2548               dwarf2out_flush_queued_reg_saves ();
2549
2550               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2551               fde->stack_realign = 1;
2552               fde->stack_realignment = INTVAL (XEXP (src, 1));
2553               cfa_store.offset = 0;
2554
2555               if (cfa.reg != STACK_POINTER_REGNUM
2556                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2557                 fde->drap_reg = cfa.reg;
2558             }
2559           return;
2560
2561         default:
2562           gcc_unreachable ();
2563         }
2564
2565       def_cfa_1 (label, &cfa);
2566       break;
2567
2568     case MEM:
2569
2570       /* Saving a register to the stack.  Make sure dest is relative to the
2571          CFA register.  */
2572       switch (GET_CODE (XEXP (dest, 0)))
2573         {
2574           /* Rule 10 */
2575           /* With a push.  */
2576         case PRE_MODIFY:
2577           /* We can't handle variable size modifications.  */
2578           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2579                       == CONST_INT);
2580           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2581
2582           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2583                       && cfa_store.reg == STACK_POINTER_REGNUM);
2584
2585           cfa_store.offset += offset;
2586           if (cfa.reg == STACK_POINTER_REGNUM)
2587             cfa.offset = cfa_store.offset;
2588
2589           offset = -cfa_store.offset;
2590           break;
2591
2592           /* Rule 11 */
2593         case PRE_INC:
2594         case PRE_DEC:
2595           offset = GET_MODE_SIZE (GET_MODE (dest));
2596           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2597             offset = -offset;
2598
2599           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2600                        == STACK_POINTER_REGNUM)
2601                       && cfa_store.reg == STACK_POINTER_REGNUM);
2602
2603           cfa_store.offset += offset;
2604
2605           /* Rule 18: If stack is aligned, we will use FP as a
2606              reference to represent the address of the stored
2607              regiser.  */
2608           if (fde
2609               && fde->stack_realign
2610               && src == hard_frame_pointer_rtx)
2611             {
2612               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2613               cfa_store.offset = 0;
2614             }
2615
2616           if (cfa.reg == STACK_POINTER_REGNUM)
2617             cfa.offset = cfa_store.offset;
2618
2619           offset = -cfa_store.offset;
2620           break;
2621
2622           /* Rule 12 */
2623           /* With an offset.  */
2624         case PLUS:
2625         case MINUS:
2626         case LO_SUM:
2627           {
2628             int regno;
2629
2630             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2631                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2632             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2633             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2634               offset = -offset;
2635
2636             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2637
2638             if (cfa.reg == (unsigned) regno)
2639               offset -= cfa.offset;
2640             else if (cfa_store.reg == (unsigned) regno)
2641               offset -= cfa_store.offset;
2642             else
2643               {
2644                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2645                 offset -= cfa_temp.offset;
2646               }
2647           }
2648           break;
2649
2650           /* Rule 13 */
2651           /* Without an offset.  */
2652         case REG:
2653           {
2654             int regno = REGNO (XEXP (dest, 0));
2655
2656             if (cfa.reg == (unsigned) regno)
2657               offset = -cfa.offset;
2658             else if (cfa_store.reg == (unsigned) regno)
2659               offset = -cfa_store.offset;
2660             else
2661               {
2662                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2663                 offset = -cfa_temp.offset;
2664               }
2665           }
2666           break;
2667
2668           /* Rule 14 */
2669         case POST_INC:
2670           gcc_assert (cfa_temp.reg
2671                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2672           offset = -cfa_temp.offset;
2673           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2674           break;
2675
2676         default:
2677           gcc_unreachable ();
2678         }
2679
2680         /* Rule 17 */
2681         /* If the source operand of this MEM operation is not a
2682            register, basically the source is return address.  Here
2683            we only care how much stack grew and we don't save it.  */
2684       if (!REG_P (src))
2685         break;
2686
2687       if (REGNO (src) != STACK_POINTER_REGNUM
2688           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2689           && (unsigned) REGNO (src) == cfa.reg)
2690         {
2691           /* We're storing the current CFA reg into the stack.  */
2692
2693           if (cfa.offset == 0)
2694             {
2695               /* Rule 19 */
2696               /* If stack is aligned, putting CFA reg into stack means
2697                  we can no longer use reg + offset to represent CFA.
2698                  Here we use DW_CFA_def_cfa_expression instead.  The
2699                  result of this expression equals to the original CFA
2700                  value.  */
2701               if (fde
2702                   && fde->stack_realign
2703                   && cfa.indirect == 0
2704                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2705                 {
2706                   dw_cfa_location cfa_exp;
2707
2708                   gcc_assert (fde->drap_reg == cfa.reg);
2709
2710                   cfa_exp.indirect = 1;
2711                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2712                   cfa_exp.base_offset = offset;
2713                   cfa_exp.offset = 0;
2714
2715                   fde->drap_reg_saved = 1;
2716
2717                   def_cfa_1 (label, &cfa_exp);
2718                   break;
2719                 }
2720
2721               /* If the source register is exactly the CFA, assume
2722                  we're saving SP like any other register; this happens
2723                  on the ARM.  */
2724               def_cfa_1 (label, &cfa);
2725               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2726               break;
2727             }
2728           else
2729             {
2730               /* Otherwise, we'll need to look in the stack to
2731                  calculate the CFA.  */
2732               rtx x = XEXP (dest, 0);
2733
2734               if (!REG_P (x))
2735                 x = XEXP (x, 0);
2736               gcc_assert (REG_P (x));
2737
2738               cfa.reg = REGNO (x);
2739               cfa.base_offset = offset;
2740               cfa.indirect = 1;
2741               def_cfa_1 (label, &cfa);
2742               break;
2743             }
2744         }
2745
2746       def_cfa_1 (label, &cfa);
2747       {
2748         span = targetm.dwarf_register_span (src);
2749
2750         if (!span)
2751           queue_reg_save (label, src, NULL_RTX, offset);
2752         else
2753           {
2754             /* We have a PARALLEL describing where the contents of SRC
2755                live.  Queue register saves for each piece of the
2756                PARALLEL.  */
2757             int par_index;
2758             int limit;
2759             HOST_WIDE_INT span_offset = offset;
2760
2761             gcc_assert (GET_CODE (span) == PARALLEL);
2762
2763             limit = XVECLEN (span, 0);
2764             for (par_index = 0; par_index < limit; par_index++)
2765               {
2766                 rtx elem = XVECEXP (span, 0, par_index);
2767
2768                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2769                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2770               }
2771           }
2772       }
2773       break;
2774
2775     default:
2776       gcc_unreachable ();
2777     }
2778 }
2779
2780 /* Record call frame debugging information for INSN, which either
2781    sets SP or FP (adjusting how we calculate the frame address) or saves a
2782    register to the stack.  If INSN is NULL_RTX, initialize our state.
2783
2784    If AFTER_P is false, we're being called before the insn is emitted,
2785    otherwise after.  Call instructions get invoked twice.  */
2786
2787 void
2788 dwarf2out_frame_debug (rtx insn, bool after_p)
2789 {
2790   const char *label;
2791   rtx note, n;
2792   bool handled_one = false;
2793
2794   if (insn == NULL_RTX)
2795     {
2796       size_t i;
2797
2798       /* Flush any queued register saves.  */
2799       dwarf2out_flush_queued_reg_saves ();
2800
2801       /* Set up state for generating call frame debug info.  */
2802       lookup_cfa (&cfa);
2803       gcc_assert (cfa.reg
2804                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2805
2806       cfa.reg = STACK_POINTER_REGNUM;
2807       cfa_store = cfa;
2808       cfa_temp.reg = -1;
2809       cfa_temp.offset = 0;
2810
2811       for (i = 0; i < num_regs_saved_in_regs; i++)
2812         {
2813           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2814           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2815         }
2816       num_regs_saved_in_regs = 0;
2817
2818       if (barrier_args_size)
2819         {
2820           XDELETEVEC (barrier_args_size);
2821           barrier_args_size = NULL;
2822         }
2823       return;
2824     }
2825
2826   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2827     dwarf2out_flush_queued_reg_saves ();
2828
2829   if (!RTX_FRAME_RELATED_P (insn))
2830     {
2831       /* ??? This should be done unconditionally since stack adjustments
2832          matter if the stack pointer is not the CFA register anymore but
2833          is still used to save registers.  */
2834       if (!ACCUMULATE_OUTGOING_ARGS)
2835         dwarf2out_notice_stack_adjust (insn, after_p);
2836       return;
2837     }
2838
2839   label = dwarf2out_cfi_label (false);
2840   any_cfis_emitted = false;
2841
2842   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2843     switch (REG_NOTE_KIND (note))
2844       {
2845       case REG_FRAME_RELATED_EXPR:
2846         insn = XEXP (note, 0);
2847         goto found;
2848
2849       case REG_CFA_DEF_CFA:
2850         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2851         handled_one = true;
2852         break;
2853
2854       case REG_CFA_ADJUST_CFA:
2855         n = XEXP (note, 0);
2856         if (n == NULL)
2857           {
2858             n = PATTERN (insn);
2859             if (GET_CODE (n) == PARALLEL)
2860               n = XVECEXP (n, 0, 0);
2861           }
2862         dwarf2out_frame_debug_adjust_cfa (n, label);
2863         handled_one = true;
2864         break;
2865
2866       case REG_CFA_OFFSET:
2867         n = XEXP (note, 0);
2868         if (n == NULL)
2869           n = single_set (insn);
2870         dwarf2out_frame_debug_cfa_offset (n, label);
2871         handled_one = true;
2872         break;
2873
2874       case REG_CFA_REGISTER:
2875         n = XEXP (note, 0);
2876         if (n == NULL)
2877           {
2878             n = PATTERN (insn);
2879             if (GET_CODE (n) == PARALLEL)
2880               n = XVECEXP (n, 0, 0);
2881           }
2882         dwarf2out_frame_debug_cfa_register (n, label);
2883         handled_one = true;
2884         break;
2885
2886       case REG_CFA_EXPRESSION:
2887         n = XEXP (note, 0);
2888         if (n == NULL)
2889           n = single_set (insn);
2890         dwarf2out_frame_debug_cfa_expression (n, label);
2891         handled_one = true;
2892         break;
2893
2894       case REG_CFA_RESTORE:
2895         n = XEXP (note, 0);
2896         if (n == NULL)
2897           {
2898             n = PATTERN (insn);
2899             if (GET_CODE (n) == PARALLEL)
2900               n = XVECEXP (n, 0, 0);
2901             n = XEXP (n, 0);
2902           }
2903         dwarf2out_frame_debug_cfa_restore (n, label);
2904         handled_one = true;
2905         break;
2906
2907       case REG_CFA_SET_VDRAP:
2908         n = XEXP (note, 0);
2909         if (REG_P (n))
2910           {
2911             dw_fde_ref fde = current_fde ();
2912             if (fde)
2913               {
2914                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2915                 if (REG_P (n))
2916                   fde->vdrap_reg = REGNO (n);
2917               }
2918           }
2919         handled_one = true;
2920         break;
2921
2922       default:
2923         break;
2924       }
2925   if (handled_one)
2926     {
2927       if (any_cfis_emitted)
2928         dwarf2out_flush_queued_reg_saves ();
2929       return;
2930     }
2931
2932   insn = PATTERN (insn);
2933  found:
2934   dwarf2out_frame_debug_expr (insn, label);
2935
2936   /* Check again.  A parallel can save and update the same register.
2937      We could probably check just once, here, but this is safer than
2938      removing the check above.  */
2939   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2940     dwarf2out_flush_queued_reg_saves ();
2941 }
2942
2943 /* Determine if we need to save and restore CFI information around this
2944    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2945    we do need to save/restore, then emit the save now, and insert a
2946    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2947
2948 void
2949 dwarf2out_cfi_begin_epilogue (rtx insn)
2950 {
2951   bool saw_frp = false;
2952   rtx i;
2953
2954   /* Scan forward to the return insn, noticing if there are possible
2955      frame related insns.  */
2956   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2957     {
2958       if (!INSN_P (i))
2959         continue;
2960
2961       /* Look for both regular and sibcalls to end the block.  */
2962       if (returnjump_p (i))
2963         break;
2964       if (CALL_P (i) && SIBLING_CALL_P (i))
2965         break;
2966
2967       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2968         {
2969           int idx;
2970           rtx seq = PATTERN (i);
2971
2972           if (returnjump_p (XVECEXP (seq, 0, 0)))
2973             break;
2974           if (CALL_P (XVECEXP (seq, 0, 0))
2975               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2976             break;
2977
2978           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2979             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2980               saw_frp = true;
2981         }
2982
2983       if (RTX_FRAME_RELATED_P (i))
2984         saw_frp = true;
2985     }
2986
2987   /* If the port doesn't emit epilogue unwind info, we don't need a
2988      save/restore pair.  */
2989   if (!saw_frp)
2990     return;
2991
2992   /* Otherwise, search forward to see if the return insn was the last
2993      basic block of the function.  If so, we don't need save/restore.  */
2994   gcc_assert (i != NULL);
2995   i = next_real_insn (i);
2996   if (i == NULL)
2997     return;
2998
2999   /* Insert the restore before that next real insn in the stream, and before
3000      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3001      properly nested.  This should be after any label or alignment.  This
3002      will be pushed into the CFI stream by the function below.  */
3003   while (1)
3004     {
3005       rtx p = PREV_INSN (i);
3006       if (!NOTE_P (p))
3007         break;
3008       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3009         break;
3010       i = p;
3011     }
3012   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3013
3014   emit_cfa_remember = true;
3015
3016   /* And emulate the state save.  */
3017   gcc_assert (!cfa_remember.in_use);
3018   cfa_remember = cfa;
3019   cfa_remember.in_use = 1;
3020 }
3021
3022 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3023    required.  */
3024
3025 void
3026 dwarf2out_frame_debug_restore_state (void)
3027 {
3028   dw_cfi_ref cfi = new_cfi ();
3029   const char *label = dwarf2out_cfi_label (false);
3030
3031   cfi->dw_cfi_opc = DW_CFA_restore_state;
3032   add_fde_cfi (label, cfi);
3033
3034   gcc_assert (cfa_remember.in_use);
3035   cfa = cfa_remember;
3036   cfa_remember.in_use = 0;
3037 }
3038
3039 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3040 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3041  (enum dwarf_call_frame_info cfi);
3042
3043 static enum dw_cfi_oprnd_type
3044 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3045 {
3046   switch (cfi)
3047     {
3048     case DW_CFA_nop:
3049     case DW_CFA_GNU_window_save:
3050     case DW_CFA_remember_state:
3051     case DW_CFA_restore_state:
3052       return dw_cfi_oprnd_unused;
3053
3054     case DW_CFA_set_loc:
3055     case DW_CFA_advance_loc1:
3056     case DW_CFA_advance_loc2:
3057     case DW_CFA_advance_loc4:
3058     case DW_CFA_MIPS_advance_loc8:
3059       return dw_cfi_oprnd_addr;
3060
3061     case DW_CFA_offset:
3062     case DW_CFA_offset_extended:
3063     case DW_CFA_def_cfa:
3064     case DW_CFA_offset_extended_sf:
3065     case DW_CFA_def_cfa_sf:
3066     case DW_CFA_restore:
3067     case DW_CFA_restore_extended:
3068     case DW_CFA_undefined:
3069     case DW_CFA_same_value:
3070     case DW_CFA_def_cfa_register:
3071     case DW_CFA_register:
3072     case DW_CFA_expression:
3073       return dw_cfi_oprnd_reg_num;
3074
3075     case DW_CFA_def_cfa_offset:
3076     case DW_CFA_GNU_args_size:
3077     case DW_CFA_def_cfa_offset_sf:
3078       return dw_cfi_oprnd_offset;
3079
3080     case DW_CFA_def_cfa_expression:
3081       return dw_cfi_oprnd_loc;
3082
3083     default:
3084       gcc_unreachable ();
3085     }
3086 }
3087
3088 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3089 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3090  (enum dwarf_call_frame_info cfi);
3091
3092 static enum dw_cfi_oprnd_type
3093 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3094 {
3095   switch (cfi)
3096     {
3097     case DW_CFA_def_cfa:
3098     case DW_CFA_def_cfa_sf:
3099     case DW_CFA_offset:
3100     case DW_CFA_offset_extended_sf:
3101     case DW_CFA_offset_extended:
3102       return dw_cfi_oprnd_offset;
3103
3104     case DW_CFA_register:
3105       return dw_cfi_oprnd_reg_num;
3106
3107     case DW_CFA_expression:
3108       return dw_cfi_oprnd_loc;
3109
3110     default:
3111       return dw_cfi_oprnd_unused;
3112     }
3113 }
3114
3115 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3116    switch to the data section instead, and write out a synthetic start label
3117    for collect2 the first time around.  */
3118
3119 static void
3120 switch_to_eh_frame_section (bool back)
3121 {
3122   tree label;
3123
3124 #ifdef EH_FRAME_SECTION_NAME
3125   if (eh_frame_section == 0)
3126     {
3127       int flags;
3128
3129       if (EH_TABLES_CAN_BE_READ_ONLY)
3130         {
3131           int fde_encoding;
3132           int per_encoding;
3133           int lsda_encoding;
3134
3135           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3136                                                        /*global=*/0);
3137           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3138                                                        /*global=*/1);
3139           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3140                                                         /*global=*/0);
3141           flags = ((! flag_pic
3142                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3143                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3144                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3145                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3146                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3147                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3148                    ? 0 : SECTION_WRITE);
3149         }
3150       else
3151         flags = SECTION_WRITE;
3152       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3153     }
3154 #endif /* EH_FRAME_SECTION_NAME */
3155
3156   if (eh_frame_section)
3157     switch_to_section (eh_frame_section);
3158   else
3159     {
3160       /* We have no special eh_frame section.  Put the information in
3161          the data section and emit special labels to guide collect2.  */
3162       switch_to_section (data_section);
3163
3164       if (!back)
3165         {
3166           label = get_file_function_name ("F");
3167           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3168           targetm.asm_out.globalize_label (asm_out_file,
3169                                            IDENTIFIER_POINTER (label));
3170           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3171         }
3172     }
3173 }
3174
3175 /* Switch [BACK] to the eh or debug frame table section, depending on
3176    FOR_EH.  */
3177
3178 static void
3179 switch_to_frame_table_section (int for_eh, bool back)
3180 {
3181   if (for_eh)
3182     switch_to_eh_frame_section (back);
3183   else
3184     {
3185       if (!debug_frame_section)
3186         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3187                                            SECTION_DEBUG, NULL);
3188       switch_to_section (debug_frame_section);
3189     }
3190 }
3191
3192 /* Output a Call Frame Information opcode and its operand(s).  */
3193
3194 static void
3195 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3196 {
3197   unsigned long r;
3198   HOST_WIDE_INT off;
3199
3200   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3201     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3202                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3203                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3204                          ((unsigned HOST_WIDE_INT)
3205                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3206   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3207     {
3208       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3209       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3210                            "DW_CFA_offset, column %#lx", r);
3211       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3212       dw2_asm_output_data_uleb128 (off, NULL);
3213     }
3214   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3215     {
3216       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3217       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3218                            "DW_CFA_restore, column %#lx", r);
3219     }
3220   else
3221     {
3222       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3223                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3224
3225       switch (cfi->dw_cfi_opc)
3226         {
3227         case DW_CFA_set_loc:
3228           if (for_eh)
3229             dw2_asm_output_encoded_addr_rtx (
3230                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3231                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3232                 false, NULL);
3233           else
3234             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3235                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3236           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3237           break;
3238
3239         case DW_CFA_advance_loc1:
3240           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3241                                 fde->dw_fde_current_label, NULL);
3242           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3243           break;
3244
3245         case DW_CFA_advance_loc2:
3246           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3247                                 fde->dw_fde_current_label, NULL);
3248           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3249           break;
3250
3251         case DW_CFA_advance_loc4:
3252           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3253                                 fde->dw_fde_current_label, NULL);
3254           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3255           break;
3256
3257         case DW_CFA_MIPS_advance_loc8:
3258           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3259                                 fde->dw_fde_current_label, NULL);
3260           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3261           break;
3262
3263         case DW_CFA_offset_extended:
3264           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3265           dw2_asm_output_data_uleb128 (r, NULL);
3266           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3267           dw2_asm_output_data_uleb128 (off, NULL);
3268           break;
3269
3270         case DW_CFA_def_cfa:
3271           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3272           dw2_asm_output_data_uleb128 (r, NULL);
3273           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3274           break;
3275
3276         case DW_CFA_offset_extended_sf:
3277           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3278           dw2_asm_output_data_uleb128 (r, NULL);
3279           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3280           dw2_asm_output_data_sleb128 (off, NULL);
3281           break;
3282
3283         case DW_CFA_def_cfa_sf:
3284           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3285           dw2_asm_output_data_uleb128 (r, NULL);
3286           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3287           dw2_asm_output_data_sleb128 (off, NULL);
3288           break;
3289
3290         case DW_CFA_restore_extended:
3291         case DW_CFA_undefined:
3292         case DW_CFA_same_value:
3293         case DW_CFA_def_cfa_register:
3294           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3295           dw2_asm_output_data_uleb128 (r, NULL);
3296           break;
3297
3298         case DW_CFA_register:
3299           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3300           dw2_asm_output_data_uleb128 (r, NULL);
3301           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3302           dw2_asm_output_data_uleb128 (r, NULL);
3303           break;
3304
3305         case DW_CFA_def_cfa_offset:
3306         case DW_CFA_GNU_args_size:
3307           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3308           break;
3309
3310         case DW_CFA_def_cfa_offset_sf:
3311           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3312           dw2_asm_output_data_sleb128 (off, NULL);
3313           break;
3314
3315         case DW_CFA_GNU_window_save:
3316           break;
3317
3318         case DW_CFA_def_cfa_expression:
3319         case DW_CFA_expression:
3320           output_cfa_loc (cfi);
3321           break;
3322
3323         case DW_CFA_GNU_negative_offset_extended:
3324           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3325           gcc_unreachable ();
3326
3327         default:
3328           break;
3329         }
3330     }
3331 }
3332
3333 /* Similar, but do it via assembler directives instead.  */
3334
3335 static void
3336 output_cfi_directive (dw_cfi_ref cfi)
3337 {
3338   unsigned long r, r2;
3339
3340   switch (cfi->dw_cfi_opc)
3341     {
3342     case DW_CFA_advance_loc:
3343     case DW_CFA_advance_loc1:
3344     case DW_CFA_advance_loc2:
3345     case DW_CFA_advance_loc4:
3346     case DW_CFA_MIPS_advance_loc8:
3347     case DW_CFA_set_loc:
3348       /* Should only be created by add_fde_cfi in a code path not
3349          followed when emitting via directives.  The assembler is
3350          going to take care of this for us.  */
3351       gcc_unreachable ();
3352
3353     case DW_CFA_offset:
3354     case DW_CFA_offset_extended:
3355     case DW_CFA_offset_extended_sf:
3356       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3357       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3358                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3359       break;
3360
3361     case DW_CFA_restore:
3362     case DW_CFA_restore_extended:
3363       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3364       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3365       break;
3366
3367     case DW_CFA_undefined:
3368       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3369       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3370       break;
3371
3372     case DW_CFA_same_value:
3373       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3374       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3375       break;
3376
3377     case DW_CFA_def_cfa:
3378     case DW_CFA_def_cfa_sf:
3379       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3380       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3381                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3382       break;
3383
3384     case DW_CFA_def_cfa_register:
3385       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3386       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3387       break;
3388
3389     case DW_CFA_register:
3390       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3391       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3392       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3393       break;
3394
3395     case DW_CFA_def_cfa_offset:
3396     case DW_CFA_def_cfa_offset_sf:
3397       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3398                HOST_WIDE_INT_PRINT_DEC"\n",
3399                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3400       break;
3401
3402     case DW_CFA_remember_state:
3403       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3404       break;
3405     case DW_CFA_restore_state:
3406       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3407       break;
3408
3409     case DW_CFA_GNU_args_size:
3410       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3411       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3412       if (flag_debug_asm)
3413         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3414                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3415       fputc ('\n', asm_out_file);
3416       break;
3417
3418     case DW_CFA_GNU_window_save:
3419       fprintf (asm_out_file, "\t.cfi_window_save\n");
3420       break;
3421
3422     case DW_CFA_def_cfa_expression:
3423     case DW_CFA_expression:
3424       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3425       output_cfa_loc_raw (cfi);
3426       fputc ('\n', asm_out_file);
3427       break;
3428
3429     default:
3430       gcc_unreachable ();
3431     }
3432 }
3433
3434 DEF_VEC_P (dw_cfi_ref);
3435 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3436
3437 /* Output CFIs to bring current FDE to the same state as after executing
3438    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3439    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3440    other arguments to pass to output_cfi.  */
3441
3442 static void
3443 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3444 {
3445   struct dw_cfi_struct cfi_buf;
3446   dw_cfi_ref cfi2;
3447   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3448   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3449   unsigned int len, idx;
3450
3451   for (;; cfi = cfi->dw_cfi_next)
3452     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3453       {
3454       case DW_CFA_advance_loc:
3455       case DW_CFA_advance_loc1:
3456       case DW_CFA_advance_loc2:
3457       case DW_CFA_advance_loc4:
3458       case DW_CFA_MIPS_advance_loc8:
3459       case DW_CFA_set_loc:
3460         /* All advances should be ignored.  */
3461         break;
3462       case DW_CFA_remember_state:
3463         {
3464           dw_cfi_ref args_size = cfi_args_size;
3465
3466           /* Skip everything between .cfi_remember_state and
3467              .cfi_restore_state.  */
3468           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3469             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3470               break;
3471             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3472               args_size = cfi2;
3473             else
3474               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3475
3476           if (cfi2 == NULL)
3477             goto flush_all;
3478           else
3479             {
3480               cfi = cfi2;
3481               cfi_args_size = args_size;
3482             }
3483           break;
3484         }
3485       case DW_CFA_GNU_args_size:
3486         cfi_args_size = cfi;
3487         break;
3488       case DW_CFA_GNU_window_save:
3489         goto flush_all;
3490       case DW_CFA_offset:
3491       case DW_CFA_offset_extended:
3492       case DW_CFA_offset_extended_sf:
3493       case DW_CFA_restore:
3494       case DW_CFA_restore_extended:
3495       case DW_CFA_undefined:
3496       case DW_CFA_same_value:
3497       case DW_CFA_register:
3498       case DW_CFA_val_offset:
3499       case DW_CFA_val_offset_sf:
3500       case DW_CFA_expression:
3501       case DW_CFA_val_expression:
3502       case DW_CFA_GNU_negative_offset_extended:
3503         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3504           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3505                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3506         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3507         break;
3508       case DW_CFA_def_cfa:
3509       case DW_CFA_def_cfa_sf:
3510       case DW_CFA_def_cfa_expression:
3511         cfi_cfa = cfi;
3512         cfi_cfa_offset = cfi;
3513         break;
3514       case DW_CFA_def_cfa_register:
3515         cfi_cfa = cfi;
3516         break;
3517       case DW_CFA_def_cfa_offset:
3518       case DW_CFA_def_cfa_offset_sf:
3519         cfi_cfa_offset = cfi;
3520         break;
3521       case DW_CFA_nop:
3522         gcc_assert (cfi == NULL);
3523       flush_all:
3524         len = VEC_length (dw_cfi_ref, regs);
3525         for (idx = 0; idx < len; idx++)
3526           {
3527             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3528             if (cfi2 != NULL
3529                 && cfi2->dw_cfi_opc != DW_CFA_restore
3530                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3531               {
3532                 if (do_cfi_asm)
3533                   output_cfi_directive (cfi2);
3534                 else
3535                   output_cfi (cfi2, fde, for_eh);
3536               }
3537           }
3538         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3539           {
3540             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3541             cfi_buf = *cfi_cfa;
3542             switch (cfi_cfa_offset->dw_cfi_opc)
3543               {
3544               case DW_CFA_def_cfa_offset:
3545                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3546                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3547                 break;
3548               case DW_CFA_def_cfa_offset_sf:
3549                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3550                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3551                 break;
3552               case DW_CFA_def_cfa:
3553               case DW_CFA_def_cfa_sf:
3554                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3555                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3556                 break;
3557               default:
3558                 gcc_unreachable ();
3559               }
3560             cfi_cfa = &cfi_buf;
3561           }
3562         else if (cfi_cfa_offset)
3563           cfi_cfa = cfi_cfa_offset;
3564         if (cfi_cfa)
3565           {
3566             if (do_cfi_asm)
3567               output_cfi_directive (cfi_cfa);
3568             else
3569               output_cfi (cfi_cfa, fde, for_eh);
3570           }
3571         cfi_cfa = NULL;
3572         cfi_cfa_offset = NULL;
3573         if (cfi_args_size
3574             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3575           {
3576             if (do_cfi_asm)
3577               output_cfi_directive (cfi_args_size);
3578             else
3579               output_cfi (cfi_args_size, fde, for_eh);
3580           }
3581         cfi_args_size = NULL;
3582         if (cfi == NULL)
3583           {
3584             VEC_free (dw_cfi_ref, heap, regs);
3585             return;
3586           }
3587         else if (do_cfi_asm)
3588           output_cfi_directive (cfi);
3589         else
3590           output_cfi (cfi, fde, for_eh);
3591         break;
3592       default:
3593         gcc_unreachable ();
3594     }
3595 }
3596
3597 /* Output one FDE.  */
3598
3599 static void
3600 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3601             char *section_start_label, int fde_encoding, char *augmentation,
3602             bool any_lsda_needed, int lsda_encoding)
3603 {
3604   const char *begin, *end;
3605   static unsigned int j;
3606   char l1[20], l2[20];
3607   dw_cfi_ref cfi;
3608
3609   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3610                                      /* empty */ 0);
3611   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3612                                   for_eh + j);
3613   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3614   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3615   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3616     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3617                          " indicating 64-bit DWARF extension");
3618   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3619                         "FDE Length");
3620   ASM_OUTPUT_LABEL (asm_out_file, l1);
3621
3622   if (for_eh)
3623     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3624   else
3625     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3626                            debug_frame_section, "FDE CIE offset");
3627
3628   if (!fde->dw_fde_switched_sections)
3629     {
3630       begin = fde->dw_fde_begin;
3631       end = fde->dw_fde_end;
3632     }
3633   else
3634     {
3635       /* For the first section, prefer dw_fde_begin over
3636          dw_fde_{hot,cold}_section_label, as the latter
3637          might be separated from the real start of the
3638          function by alignment padding.  */
3639       if (!second)
3640         begin = fde->dw_fde_begin;
3641       else if (fde->dw_fde_switched_cold_to_hot)
3642         begin = fde->dw_fde_hot_section_label;
3643       else
3644         begin = fde->dw_fde_unlikely_section_label;
3645       if (second ^ fde->dw_fde_switched_cold_to_hot)
3646         end = fde->dw_fde_unlikely_section_end_label;
3647       else
3648         end = fde->dw_fde_hot_section_end_label;
3649     }
3650
3651   if (for_eh)
3652     {
3653       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3654       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3655       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3656                                        "FDE initial location");
3657       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3658                             end, begin, "FDE address range");
3659     }
3660   else
3661     {
3662       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3663       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3664     }
3665
3666   if (augmentation[0])
3667     {
3668       if (any_lsda_needed)
3669         {
3670           int size = size_of_encoded_value (lsda_encoding);
3671
3672           if (lsda_encoding == DW_EH_PE_aligned)
3673             {
3674               int offset = (  4         /* Length */
3675                             + 4         /* CIE offset */
3676                             + 2 * size_of_encoded_value (fde_encoding)
3677                             + 1         /* Augmentation size */ );
3678               int pad = -offset & (PTR_SIZE - 1);
3679
3680               size += pad;
3681               gcc_assert (size_of_uleb128 (size) == 1);
3682             }
3683
3684           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3685
3686           if (fde->uses_eh_lsda)
3687             {
3688               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3689                                            fde->funcdef_number);
3690               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3691                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3692                                                false,
3693                                                "Language Specific Data Area");
3694             }
3695           else
3696             {
3697               if (lsda_encoding == DW_EH_PE_aligned)
3698                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3699               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3700                                    "Language Specific Data Area (none)");
3701             }
3702         }
3703       else
3704         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3705     }
3706
3707   /* Loop through the Call Frame Instructions associated with
3708      this FDE.  */
3709   fde->dw_fde_current_label = begin;
3710   if (!fde->dw_fde_switched_sections)
3711     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3712       output_cfi (cfi, fde, for_eh);
3713   else if (!second)
3714     {
3715       if (fde->dw_fde_switch_cfi)
3716         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3717           {
3718             output_cfi (cfi, fde, for_eh);
3719             if (cfi == fde->dw_fde_switch_cfi)
3720               break;
3721           }
3722     }
3723   else
3724     {
3725       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3726
3727       if (fde->dw_fde_switch_cfi)
3728         {
3729           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3730           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3731           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3732           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3733         }
3734       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3735         output_cfi (cfi, fde, for_eh);
3736     }
3737
3738   /* If we are to emit a ref/link from function bodies to their frame tables,
3739      do it now.  This is typically performed to make sure that tables
3740      associated with functions are dragged with them and not discarded in
3741      garbage collecting links. We need to do this on a per function basis to
3742      cope with -ffunction-sections.  */
3743
3744 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3745   /* Switch to the function section, emit the ref to the tables, and
3746      switch *back* into the table section.  */
3747   switch_to_section (function_section (fde->decl));
3748   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3749   switch_to_frame_table_section (for_eh, true);
3750 #endif
3751
3752   /* Pad the FDE out to an address sized boundary.  */
3753   ASM_OUTPUT_ALIGN (asm_out_file,
3754                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3755   ASM_OUTPUT_LABEL (asm_out_file, l2);
3756
3757   j += 2;
3758 }
3759
3760 /* Return true if frame description entry FDE is needed for EH.  */
3761
3762 static bool
3763 fde_needed_for_eh_p (dw_fde_ref fde)
3764 {
3765   if (flag_asynchronous_unwind_tables)
3766     return true;
3767
3768   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3769     return true;
3770
3771   if (fde->uses_eh_lsda)
3772     return true;
3773
3774   /* If exceptions are enabled, we have collected nothrow info.  */
3775   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3776     return false;
3777
3778   return true;
3779 }
3780
3781 /* Output the call frame information used to record information
3782    that relates to calculating the frame pointer, and records the
3783    location of saved registers.  */
3784
3785 static void
3786 output_call_frame_info (int for_eh)
3787 {
3788   unsigned int i;
3789   dw_fde_ref fde;
3790   dw_cfi_ref cfi;
3791   char l1[20], l2[20], section_start_label[20];
3792   bool any_lsda_needed = false;
3793   char augmentation[6];
3794   int augmentation_size;
3795   int fde_encoding = DW_EH_PE_absptr;
3796   int per_encoding = DW_EH_PE_absptr;
3797   int lsda_encoding = DW_EH_PE_absptr;
3798   int return_reg;
3799   rtx personality = NULL;
3800   int dw_cie_version;
3801
3802   /* Don't emit a CIE if there won't be any FDEs.  */
3803   if (fde_table_in_use == 0)
3804     return;
3805
3806   /* Nothing to do if the assembler's doing it all.  */
3807   if (dwarf2out_do_cfi_asm ())
3808     return;
3809
3810   /* If we don't have any functions we'll want to unwind out of, don't emit
3811      any EH unwind information.  If we make FDEs linkonce, we may have to
3812      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3813      want to avoid having an FDE kept around when the function it refers to
3814      is discarded.  Example where this matters: a primary function template
3815      in C++ requires EH information, an explicit specialization doesn't.  */
3816   if (for_eh)
3817     {
3818       bool any_eh_needed = false;
3819
3820       for (i = 0; i < fde_table_in_use; i++)
3821         if (fde_table[i].uses_eh_lsda)
3822           any_eh_needed = any_lsda_needed = true;
3823         else if (fde_needed_for_eh_p (&fde_table[i]))
3824           any_eh_needed = true;
3825         else if (TARGET_USES_WEAK_UNWIND_INFO)
3826           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3827                                              1, 1);
3828
3829       if (!any_eh_needed)
3830         return;
3831     }
3832
3833   /* We're going to be generating comments, so turn on app.  */
3834   if (flag_debug_asm)
3835     app_enable ();
3836
3837   /* Switch to the proper frame section, first time.  */
3838   switch_to_frame_table_section (for_eh, false);
3839
3840   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3841   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3842
3843   /* Output the CIE.  */
3844   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3845   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3846   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3847     dw2_asm_output_data (4, 0xffffffff,
3848       "Initial length escape value indicating 64-bit DWARF extension");
3849   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3850                         "Length of Common Information Entry");
3851   ASM_OUTPUT_LABEL (asm_out_file, l1);
3852
3853   /* Now that the CIE pointer is PC-relative for EH,
3854      use 0 to identify the CIE.  */
3855   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3856                        (for_eh ? 0 : DWARF_CIE_ID),
3857                        "CIE Identifier Tag");
3858
3859   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3860      use CIE version 1, unless that would produce incorrect results
3861      due to overflowing the return register column.  */
3862   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3863   dw_cie_version = 1;
3864   if (return_reg >= 256 || dwarf_version > 2)
3865     dw_cie_version = 3;
3866   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3867
3868   augmentation[0] = 0;
3869   augmentation_size = 0;
3870
3871   personality = current_unit_personality;
3872   if (for_eh)
3873     {
3874       char *p;
3875
3876       /* Augmentation:
3877          z      Indicates that a uleb128 is present to size the
3878                 augmentation section.
3879          L      Indicates the encoding (and thus presence) of
3880                 an LSDA pointer in the FDE augmentation.
3881          R      Indicates a non-default pointer encoding for
3882                 FDE code pointers.
3883          P      Indicates the presence of an encoding + language
3884                 personality routine in the CIE augmentation.  */
3885
3886       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3887       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3888       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3889
3890       p = augmentation + 1;
3891       if (personality)
3892         {
3893           *p++ = 'P';
3894           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3895           assemble_external_libcall (personality);
3896         }
3897       if (any_lsda_needed)
3898         {
3899           *p++ = 'L';
3900           augmentation_size += 1;
3901         }
3902       if (fde_encoding != DW_EH_PE_absptr)
3903         {
3904           *p++ = 'R';
3905           augmentation_size += 1;
3906         }
3907       if (p > augmentation + 1)
3908         {
3909           augmentation[0] = 'z';
3910           *p = '\0';
3911         }
3912
3913       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3914       if (personality && per_encoding == DW_EH_PE_aligned)
3915         {
3916           int offset = (  4             /* Length */
3917                         + 4             /* CIE Id */
3918                         + 1             /* CIE version */
3919                         + strlen (augmentation) + 1     /* Augmentation */
3920                         + size_of_uleb128 (1)           /* Code alignment */
3921                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3922                         + 1             /* RA column */
3923                         + 1             /* Augmentation size */
3924                         + 1             /* Personality encoding */ );
3925           int pad = -offset & (PTR_SIZE - 1);
3926
3927           augmentation_size += pad;
3928
3929           /* Augmentations should be small, so there's scarce need to
3930              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3931           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3932         }
3933     }
3934
3935   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3936   if (dw_cie_version >= 4)
3937     {
3938       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3939       dw2_asm_output_data (1, 0, "CIE Segment Size");
3940     }
3941   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3942   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3943                                "CIE Data Alignment Factor");
3944
3945   if (dw_cie_version == 1)
3946     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3947   else
3948     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3949
3950   if (augmentation[0])
3951     {
3952       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3953       if (personality)
3954         {
3955           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3956                                eh_data_format_name (per_encoding));
3957           dw2_asm_output_encoded_addr_rtx (per_encoding,
3958                                            personality,
3959                                            true, NULL);
3960         }
3961
3962       if (any_lsda_needed)
3963         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3964                              eh_data_format_name (lsda_encoding));
3965
3966       if (fde_encoding != DW_EH_PE_absptr)
3967         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3968                              eh_data_format_name (fde_encoding));
3969     }
3970
3971   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3972     output_cfi (cfi, NULL, for_eh);
3973
3974   /* Pad the CIE out to an address sized boundary.  */
3975   ASM_OUTPUT_ALIGN (asm_out_file,
3976                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3977   ASM_OUTPUT_LABEL (asm_out_file, l2);
3978
3979   /* Loop through all of the FDE's.  */
3980   for (i = 0; i < fde_table_in_use; i++)
3981     {
3982       unsigned int k;
3983       fde = &fde_table[i];
3984
3985       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3986       if (for_eh && !fde_needed_for_eh_p (fde))
3987         continue;
3988
3989       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3990         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3991                     augmentation, any_lsda_needed, lsda_encoding);
3992     }
3993
3994   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3995     dw2_asm_output_data (4, 0, "End of Table");
3996 #ifdef MIPS_DEBUGGING_INFO
3997   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3998      get a value of 0.  Putting .align 0 after the label fixes it.  */
3999   ASM_OUTPUT_ALIGN (asm_out_file, 0);
4000 #endif
4001
4002   /* Turn off app to make assembly quicker.  */
4003   if (flag_debug_asm)
4004     app_disable ();
4005 }
4006
4007 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4008
4009 static void
4010 dwarf2out_do_cfi_startproc (bool second)
4011 {
4012   int enc;
4013   rtx ref;
4014   rtx personality = get_personality_function (current_function_decl);
4015
4016   fprintf (asm_out_file, "\t.cfi_startproc\n");
4017
4018   if (personality)
4019     {
4020       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4021       ref = personality;
4022
4023       /* ??? The GAS support isn't entirely consistent.  We have to
4024          handle indirect support ourselves, but PC-relative is done
4025          in the assembler.  Further, the assembler can't handle any
4026          of the weirder relocation types.  */
4027       if (enc & DW_EH_PE_indirect)
4028         ref = dw2_force_const_mem (ref, true);
4029
4030       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4031       output_addr_const (asm_out_file, ref);
4032       fputc ('\n', asm_out_file);
4033     }
4034
4035   if (crtl->uses_eh_lsda)
4036     {
4037       char lab[20];
4038
4039       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4040       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4041                                    current_function_funcdef_no);
4042       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4043       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4044
4045       if (enc & DW_EH_PE_indirect)
4046         ref = dw2_force_const_mem (ref, true);
4047
4048       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4049       output_addr_const (asm_out_file, ref);
4050       fputc ('\n', asm_out_file);
4051     }
4052 }
4053
4054 /* Output a marker (i.e. a label) for the beginning of a function, before
4055    the prologue.  */
4056
4057 void
4058 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4059                           const char *file ATTRIBUTE_UNUSED)
4060 {
4061   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4062   char * dup_label;
4063   dw_fde_ref fde;
4064   section *fnsec;
4065   bool do_frame;
4066
4067   current_function_func_begin_label = NULL;
4068
4069   do_frame = dwarf2out_do_frame ();
4070
4071   /* ??? current_function_func_begin_label is also used by except.c for
4072      call-site information.  We must emit this label if it might be used.  */
4073   if (!do_frame
4074       && (!flag_exceptions
4075           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4076     return;
4077
4078   fnsec = function_section (current_function_decl);
4079   switch_to_section (fnsec);
4080   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4081                                current_function_funcdef_no);
4082   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4083                           current_function_funcdef_no);
4084   dup_label = xstrdup (label);
4085   current_function_func_begin_label = dup_label;
4086
4087   /* We can elide the fde allocation if we're not emitting debug info.  */
4088   if (!do_frame)
4089     return;
4090
4091   /* Expand the fde table if necessary.  */
4092   if (fde_table_in_use == fde_table_allocated)
4093     {
4094       fde_table_allocated += FDE_TABLE_INCREMENT;
4095       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4096       memset (fde_table + fde_table_in_use, 0,
4097               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4098     }
4099
4100   /* Record the FDE associated with this function.  */
4101   current_funcdef_fde = fde_table_in_use;
4102
4103   /* Add the new FDE at the end of the fde_table.  */
4104   fde = &fde_table[fde_table_in_use++];
4105   fde->decl = current_function_decl;
4106   fde->dw_fde_begin = dup_label;
4107   fde->dw_fde_current_label = dup_label;
4108   fde->dw_fde_hot_section_label = NULL;
4109   fde->dw_fde_hot_section_end_label = NULL;
4110   fde->dw_fde_unlikely_section_label = NULL;
4111   fde->dw_fde_unlikely_section_end_label = NULL;
4112   fde->dw_fde_switched_sections = 0;
4113   fde->dw_fde_switched_cold_to_hot = 0;
4114   fde->dw_fde_end = NULL;
4115   fde->dw_fde_vms_end_prologue = NULL;
4116   fde->dw_fde_vms_begin_epilogue = NULL;
4117   fde->dw_fde_cfi = NULL;
4118   fde->dw_fde_switch_cfi = NULL;
4119   fde->funcdef_number = current_function_funcdef_no;
4120   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4121   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4122   fde->nothrow = crtl->nothrow;
4123   fde->drap_reg = INVALID_REGNUM;
4124   fde->vdrap_reg = INVALID_REGNUM;
4125   if (flag_reorder_blocks_and_partition)
4126     {
4127       section *unlikelysec;
4128       if (first_function_block_is_cold)
4129         fde->in_std_section = 1;
4130       else
4131         fde->in_std_section
4132           = (fnsec == text_section
4133              || (cold_text_section && fnsec == cold_text_section));
4134       unlikelysec = unlikely_text_section ();
4135       fde->cold_in_std_section
4136         = (unlikelysec == text_section
4137            || (cold_text_section && unlikelysec == cold_text_section));
4138     }
4139   else
4140     {
4141       fde->in_std_section
4142         = (fnsec == text_section
4143            || (cold_text_section && fnsec == cold_text_section));
4144       fde->cold_in_std_section = 0;
4145     }
4146
4147   args_size = old_args_size = 0;
4148
4149   /* We only want to output line number information for the genuine dwarf2
4150      prologue case, not the eh frame case.  */
4151 #ifdef DWARF2_DEBUGGING_INFO
4152   if (file)
4153     dwarf2out_source_line (line, file, 0, true);
4154 #endif
4155
4156   if (dwarf2out_do_cfi_asm ())
4157     dwarf2out_do_cfi_startproc (false);
4158   else
4159     {
4160       rtx personality = get_personality_function (current_function_decl);
4161       if (!current_unit_personality)
4162         current_unit_personality = personality;
4163
4164       /* We cannot keep a current personality per function as without CFI
4165          asm, at the point where we emit the CFI data, there is no current
4166          function anymore.  */
4167       if (personality && current_unit_personality != personality)
4168         sorry ("multiple EH personalities are supported only with assemblers "
4169                "supporting .cfi_personality directive");
4170     }
4171 }
4172
4173 /* Output a marker (i.e. a label) for the end of the generated code
4174    for a function prologue.  This gets called *after* the prologue code has
4175    been generated.  */
4176
4177 void
4178 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4179                         const char *file ATTRIBUTE_UNUSED)
4180 {
4181   dw_fde_ref fde;
4182   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4183
4184   /* Output a label to mark the endpoint of the code generated for this
4185      function.  */
4186   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4187                                current_function_funcdef_no);
4188   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4189                           current_function_funcdef_no);
4190   fde = &fde_table[fde_table_in_use - 1];
4191   fde->dw_fde_vms_end_prologue = xstrdup (label);
4192 }
4193
4194 /* Output a marker (i.e. a label) for the beginning of the generated code
4195    for a function epilogue.  This gets called *before* the prologue code has
4196    been generated.  */
4197
4198 void
4199 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4200                           const char *file ATTRIBUTE_UNUSED)
4201 {
4202   dw_fde_ref fde;
4203   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4204
4205   fde = &fde_table[fde_table_in_use - 1];
4206   if (fde->dw_fde_vms_begin_epilogue)
4207     return;
4208
4209   /* Output a label to mark the endpoint of the code generated for this
4210      function.  */
4211   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4212                                current_function_funcdef_no);
4213   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4214                           current_function_funcdef_no);
4215   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4216 }
4217
4218 /* Output a marker (i.e. a label) for the absolute end of the generated code
4219    for a function definition.  This gets called *after* the epilogue code has
4220    been generated.  */
4221
4222 void
4223 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4224                         const char *file ATTRIBUTE_UNUSED)
4225 {
4226   dw_fde_ref fde;
4227   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4228
4229   last_var_location_insn = NULL_RTX;
4230
4231   if (dwarf2out_do_cfi_asm ())
4232     fprintf (asm_out_file, "\t.cfi_endproc\n");
4233
4234   /* Output a label to mark the endpoint of the code generated for this
4235      function.  */
4236   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4237                                current_function_funcdef_no);
4238   ASM_OUTPUT_LABEL (asm_out_file, label);
4239   fde = current_fde ();
4240   gcc_assert (fde != NULL);
4241   fde->dw_fde_end = xstrdup (label);
4242 }
4243
4244 void
4245 dwarf2out_frame_init (void)
4246 {
4247   /* Allocate the initial hunk of the fde_table.  */
4248   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4249   fde_table_allocated = FDE_TABLE_INCREMENT;
4250   fde_table_in_use = 0;
4251
4252   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4253      sake of lookup_cfa.  */
4254
4255   /* On entry, the Canonical Frame Address is at SP.  */
4256   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4257
4258   if (targetm.debug_unwind_info () == UI_DWARF2
4259       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4260     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4261 }
4262
4263 void
4264 dwarf2out_frame_finish (void)
4265 {
4266   /* Output call frame information.  */
4267   if (targetm.debug_unwind_info () == UI_DWARF2)
4268     output_call_frame_info (0);
4269
4270   /* Output another copy for the unwinder.  */
4271   if ((flag_unwind_tables || flag_exceptions)
4272       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4273     output_call_frame_info (1);
4274 }
4275
4276 /* Note that the current function section is being used for code.  */
4277
4278 static void
4279 dwarf2out_note_section_used (void)
4280 {
4281   section *sec = current_function_section ();
4282   if (sec == text_section)
4283     text_section_used = true;
4284   else if (sec == cold_text_section)
4285     cold_text_section_used = true;
4286 }
4287
4288 void
4289 dwarf2out_switch_text_section (void)
4290 {
4291   dw_fde_ref fde = current_fde ();
4292
4293   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4294
4295   fde->dw_fde_switched_sections = 1;
4296   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4297
4298   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4299   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4300   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4301   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4302   have_multiple_function_sections = true;
4303
4304   /* Reset the current label on switching text sections, so that we
4305      don't attempt to advance_loc4 between labels in different sections.  */
4306   fde->dw_fde_current_label = NULL;
4307
4308   /* There is no need to mark used sections when not debugging.  */
4309   if (cold_text_section != NULL)
4310     dwarf2out_note_section_used ();
4311
4312   if (dwarf2out_do_cfi_asm ())
4313     fprintf (asm_out_file, "\t.cfi_endproc\n");
4314
4315   /* Now do the real section switch.  */
4316   switch_to_section (current_function_section ());
4317
4318   if (dwarf2out_do_cfi_asm ())
4319     {
4320       dwarf2out_do_cfi_startproc (true);
4321       /* As this is a different FDE, insert all current CFI instructions
4322          again.  */
4323       output_cfis (fde->dw_fde_cfi, true, fde, true);
4324     }
4325   else
4326     {
4327       dw_cfi_ref cfi = fde->dw_fde_cfi;
4328
4329       cfi = fde->dw_fde_cfi;
4330       if (cfi)
4331         while (cfi->dw_cfi_next != NULL)
4332           cfi = cfi->dw_cfi_next;
4333       fde->dw_fde_switch_cfi = cfi;
4334     }
4335 }
4336 \f
4337 /* And now, the subset of the debugging information support code necessary
4338    for emitting location expressions.  */
4339
4340 /* Data about a single source file.  */
4341 struct GTY(()) dwarf_file_data {
4342   const char * filename;
4343   int emitted_number;
4344 };
4345
4346 typedef struct dw_val_struct *dw_val_ref;
4347 typedef struct die_struct *dw_die_ref;
4348 typedef const struct die_struct *const_dw_die_ref;
4349 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4350 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4351
4352 typedef struct GTY(()) deferred_locations_struct
4353 {
4354   tree variable;
4355   dw_die_ref die;
4356 } deferred_locations;
4357
4358 DEF_VEC_O(deferred_locations);
4359 DEF_VEC_ALLOC_O(deferred_locations,gc);
4360
4361 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4362
4363 DEF_VEC_P(dw_die_ref);
4364 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4365
4366 /* Each DIE may have a series of attribute/value pairs.  Values
4367    can take on several forms.  The forms that are used in this
4368    implementation are listed below.  */
4369
4370 enum dw_val_class
4371 {
4372   dw_val_class_addr,
4373   dw_val_class_offset,
4374   dw_val_class_loc,
4375   dw_val_class_loc_list,
4376   dw_val_class_range_list,
4377   dw_val_class_const,
4378   dw_val_class_unsigned_const,
4379   dw_val_class_const_double,
4380   dw_val_class_vec,
4381   dw_val_class_flag,
4382   dw_val_class_die_ref,
4383   dw_val_class_fde_ref,
4384   dw_val_class_lbl_id,
4385   dw_val_class_lineptr,
4386   dw_val_class_str,
4387   dw_val_class_macptr,
4388   dw_val_class_file,
4389   dw_val_class_data8,
4390   dw_val_class_decl_ref,
4391   dw_val_class_vms_delta
4392 };
4393
4394 /* Describe a floating point constant value, or a vector constant value.  */
4395
4396 typedef struct GTY(()) dw_vec_struct {
4397   unsigned char * GTY((length ("%h.length"))) array;
4398   unsigned length;
4399   unsigned elt_size;
4400 }
4401 dw_vec_const;
4402
4403 /* The dw_val_node describes an attribute's value, as it is
4404    represented internally.  */
4405
4406 typedef struct GTY(()) dw_val_struct {
4407   enum dw_val_class val_class;
4408   union dw_val_struct_union
4409     {
4410       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4411       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4412       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4413       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4414       HOST_WIDE_INT GTY ((default)) val_int;
4415       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4416       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4417       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4418       struct dw_val_die_union
4419         {
4420           dw_die_ref die;
4421           int external;
4422         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4423       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4424       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4425       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4426       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4427       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4428       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4429       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4430       struct dw_val_vms_delta_union
4431         {
4432           char * lbl1;
4433           char * lbl2;
4434         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4435     }
4436   GTY ((desc ("%1.val_class"))) v;
4437 }
4438 dw_val_node;
4439
4440 /* Locations in memory are described using a sequence of stack machine
4441    operations.  */
4442
4443 typedef struct GTY(()) dw_loc_descr_struct {
4444   dw_loc_descr_ref dw_loc_next;
4445   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4446   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4447      from DW_OP_addr with a dtp-relative symbol relocation.  */
4448   unsigned int dtprel : 1;
4449   int dw_loc_addr;
4450   dw_val_node dw_loc_oprnd1;
4451   dw_val_node dw_loc_oprnd2;
4452 }
4453 dw_loc_descr_node;
4454
4455 /* Location lists are ranges + location descriptions for that range,
4456    so you can track variables that are in different places over
4457    their entire life.  */
4458 typedef struct GTY(()) dw_loc_list_struct {
4459   dw_loc_list_ref dw_loc_next;
4460   const char *begin; /* Label for begin address of range */
4461   const char *end;  /* Label for end address of range */
4462   char *ll_symbol; /* Label for beginning of location list.
4463                       Only on head of list */
4464   const char *section; /* Section this loclist is relative to */
4465   dw_loc_descr_ref expr;
4466   hashval_t hash;
4467   bool emitted;
4468 } dw_loc_list_node;
4469
4470 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4471
4472 /* Convert a DWARF stack opcode into its string name.  */
4473
4474 static const char *
4475 dwarf_stack_op_name (unsigned int op)
4476 {
4477   switch (op)
4478     {
4479     case DW_OP_addr:
4480       return "DW_OP_addr";
4481     case DW_OP_deref:
4482       return "DW_OP_deref";
4483     case DW_OP_const1u:
4484       return "DW_OP_const1u";
4485     case DW_OP_const1s:
4486       return "DW_OP_const1s";
4487     case DW_OP_const2u:
4488       return "DW_OP_const2u";
4489     case DW_OP_const2s:
4490       return "DW_OP_const2s";
4491     case DW_OP_const4u:
4492       return "DW_OP_const4u";
4493     case DW_OP_const4s:
4494       return "DW_OP_const4s";
4495     case DW_OP_const8u:
4496       return "DW_OP_const8u";
4497     case DW_OP_const8s:
4498       return "DW_OP_const8s";
4499     case DW_OP_constu:
4500       return "DW_OP_constu";
4501     case DW_OP_consts:
4502       return "DW_OP_consts";
4503     case DW_OP_dup:
4504       return "DW_OP_dup";
4505     case DW_OP_drop:
4506       return "DW_OP_drop";
4507     case DW_OP_over:
4508       return "DW_OP_over";
4509     case DW_OP_pick:
4510       return "DW_OP_pick";
4511     case DW_OP_swap:
4512       return "DW_OP_swap";
4513     case DW_OP_rot:
4514       return "DW_OP_rot";
4515     case DW_OP_xderef:
4516       return "DW_OP_xderef";
4517     case DW_OP_abs:
4518       return "DW_OP_abs";
4519     case DW_OP_and:
4520       return "DW_OP_and";
4521     case DW_OP_div:
4522       return "DW_OP_div";
4523     case DW_OP_minus:
4524       return "DW_OP_minus";
4525     case DW_OP_mod:
4526       return "DW_OP_mod";
4527     case DW_OP_mul:
4528       return "DW_OP_mul";
4529     case DW_OP_neg:
4530       return "DW_OP_neg";
4531     case DW_OP_not:
4532       return "DW_OP_not";
4533     case DW_OP_or:
4534       return "DW_OP_or";
4535     case DW_OP_plus:
4536       return "DW_OP_plus";
4537     case DW_OP_plus_uconst:
4538       return "DW_OP_plus_uconst";
4539     case DW_OP_shl:
4540       return "DW_OP_shl";
4541     case DW_OP_shr:
4542       return "DW_OP_shr";
4543     case DW_OP_shra:
4544       return "DW_OP_shra";
4545     case DW_OP_xor:
4546       return "DW_OP_xor";
4547     case DW_OP_bra:
4548       return "DW_OP_bra";
4549     case DW_OP_eq:
4550       return "DW_OP_eq";
4551     case DW_OP_ge:
4552       return "DW_OP_ge";
4553     case DW_OP_gt:
4554       return "DW_OP_gt";
4555     case DW_OP_le:
4556       return "DW_OP_le";
4557     case DW_OP_lt:
4558       return "DW_OP_lt";
4559     case DW_OP_ne:
4560       return "DW_OP_ne";
4561     case DW_OP_skip:
4562       return "DW_OP_skip";
4563     case DW_OP_lit0:
4564       return "DW_OP_lit0";
4565     case DW_OP_lit1:
4566       return "DW_OP_lit1";
4567     case DW_OP_lit2:
4568       return "DW_OP_lit2";
4569     case DW_OP_lit3:
4570       return "DW_OP_lit3";
4571     case DW_OP_lit4:
4572       return "DW_OP_lit4";
4573     case DW_OP_lit5:
4574       return "DW_OP_lit5";
4575     case DW_OP_lit6:
4576       return "DW_OP_lit6";
4577     case DW_OP_lit7:
4578       return "DW_OP_lit7";
4579     case DW_OP_lit8:
4580       return "DW_OP_lit8";
4581     case DW_OP_lit9:
4582       return "DW_OP_lit9";
4583     case DW_OP_lit10:
4584       return "DW_OP_lit10";
4585     case DW_OP_lit11:
4586       return "DW_OP_lit11";
4587     case DW_OP_lit12:
4588       return "DW_OP_lit12";
4589     case DW_OP_lit13:
4590       return "DW_OP_lit13";
4591     case DW_OP_lit14:
4592       return "DW_OP_lit14";
4593     case DW_OP_lit15:
4594       return "DW_OP_lit15";
4595     case DW_OP_lit16:
4596       return "DW_OP_lit16";
4597     case DW_OP_lit17:
4598       return "DW_OP_lit17";
4599     case DW_OP_lit18:
4600       return "DW_OP_lit18";
4601     case DW_OP_lit19:
4602       return "DW_OP_lit19";
4603     case DW_OP_lit20:
4604       return "DW_OP_lit20";
4605     case DW_OP_lit21:
4606       return "DW_OP_lit21";
4607     case DW_OP_lit22:
4608       return "DW_OP_lit22";
4609     case DW_OP_lit23:
4610       return "DW_OP_lit23";
4611     case DW_OP_lit24:
4612       return "DW_OP_lit24";
4613     case DW_OP_lit25:
4614       return "DW_OP_lit25";
4615     case DW_OP_lit26:
4616       return "DW_OP_lit26";
4617     case DW_OP_lit27:
4618       return "DW_OP_lit27";
4619     case DW_OP_lit28:
4620       return "DW_OP_lit28";
4621     case DW_OP_lit29:
4622       return "DW_OP_lit29";
4623     case DW_OP_lit30:
4624       return "DW_OP_lit30";
4625     case DW_OP_lit31:
4626       return "DW_OP_lit31";
4627     case DW_OP_reg0:
4628       return "DW_OP_reg0";
4629     case DW_OP_reg1:
4630       return "DW_OP_reg1";
4631     case DW_OP_reg2:
4632       return "DW_OP_reg2";
4633     case DW_OP_reg3:
4634       return "DW_OP_reg3";
4635     case DW_OP_reg4:
4636       return "DW_OP_reg4";
4637     case DW_OP_reg5:
4638       return "DW_OP_reg5";
4639     case DW_OP_reg6:
4640       return "DW_OP_reg6";
4641     case DW_OP_reg7:
4642       return "DW_OP_reg7";
4643     case DW_OP_reg8:
4644       return "DW_OP_reg8";
4645     case DW_OP_reg9:
4646       return "DW_OP_reg9";
4647     case DW_OP_reg10:
4648       return "DW_OP_reg10";
4649     case DW_OP_reg11:
4650       return "DW_OP_reg11";
4651     case DW_OP_reg12:
4652       return "DW_OP_reg12";
4653     case DW_OP_reg13:
4654       return "DW_OP_reg13";
4655     case DW_OP_reg14:
4656       return "DW_OP_reg14";
4657     case DW_OP_reg15:
4658       return "DW_OP_reg15";
4659     case DW_OP_reg16:
4660       return "DW_OP_reg16";
4661     case DW_OP_reg17:
4662       return "DW_OP_reg17";
4663     case DW_OP_reg18:
4664       return "DW_OP_reg18";
4665     case DW_OP_reg19:
4666       return "DW_OP_reg19";
4667     case DW_OP_reg20:
4668       return "DW_OP_reg20";
4669     case DW_OP_reg21:
4670       return "DW_OP_reg21";
4671     case DW_OP_reg22:
4672       return "DW_OP_reg22";
4673     case DW_OP_reg23:
4674       return "DW_OP_reg23";
4675     case DW_OP_reg24:
4676       return "DW_OP_reg24";
4677     case DW_OP_reg25:
4678       return "DW_OP_reg25";
4679     case DW_OP_reg26:
4680       return "DW_OP_reg26";
4681     case DW_OP_reg27:
4682       return "DW_OP_reg27";
4683     case DW_OP_reg28:
4684       return "DW_OP_reg28";
4685     case DW_OP_reg29:
4686       return "DW_OP_reg29";
4687     case DW_OP_reg30:
4688       return "DW_OP_reg30";
4689     case DW_OP_reg31:
4690       return "DW_OP_reg31";
4691     case DW_OP_breg0:
4692       return "DW_OP_breg0";
4693     case DW_OP_breg1:
4694       return "DW_OP_breg1";
4695     case DW_OP_breg2:
4696       return "DW_OP_breg2";
4697     case DW_OP_breg3:
4698       return "DW_OP_breg3";
4699     case DW_OP_breg4:
4700       return "DW_OP_breg4";
4701     case DW_OP_breg5:
4702       return "DW_OP_breg5";
4703     case DW_OP_breg6:
4704       return "DW_OP_breg6";
4705     case DW_OP_breg7:
4706       return "DW_OP_breg7";
4707     case DW_OP_breg8:
4708       return "DW_OP_breg8";
4709     case DW_OP_breg9:
4710       return "DW_OP_breg9";
4711     case DW_OP_breg10:
4712       return "DW_OP_breg10";
4713     case DW_OP_breg11:
4714       return "DW_OP_breg11";
4715     case DW_OP_breg12:
4716       return "DW_OP_breg12";
4717     case DW_OP_breg13:
4718       return "DW_OP_breg13";
4719     case DW_OP_breg14:
4720       return "DW_OP_breg14";
4721     case DW_OP_breg15:
4722       return "DW_OP_breg15";
4723     case DW_OP_breg16:
4724       return "DW_OP_breg16";
4725     case DW_OP_breg17:
4726       return "DW_OP_breg17";
4727     case DW_OP_breg18:
4728       return "DW_OP_breg18";
4729     case DW_OP_breg19:
4730       return "DW_OP_breg19";
4731     case DW_OP_breg20:
4732       return "DW_OP_breg20";
4733     case DW_OP_breg21:
4734       return "DW_OP_breg21";
4735     case DW_OP_breg22:
4736       return "DW_OP_breg22";
4737     case DW_OP_breg23:
4738       return "DW_OP_breg23";
4739     case DW_OP_breg24:
4740       return "DW_OP_breg24";
4741     case DW_OP_breg25:
4742       return "DW_OP_breg25";
4743     case DW_OP_breg26:
4744       return "DW_OP_breg26";
4745     case DW_OP_breg27:
4746       return "DW_OP_breg27";
4747     case DW_OP_breg28:
4748       return "DW_OP_breg28";
4749     case DW_OP_breg29:
4750       return "DW_OP_breg29";
4751     case DW_OP_breg30:
4752       return "DW_OP_breg30";
4753     case DW_OP_breg31:
4754       return "DW_OP_breg31";
4755     case DW_OP_regx:
4756       return "DW_OP_regx";
4757     case DW_OP_fbreg:
4758       return "DW_OP_fbreg";
4759     case DW_OP_bregx:
4760       return "DW_OP_bregx";
4761     case DW_OP_piece:
4762       return "DW_OP_piece";
4763     case DW_OP_deref_size:
4764       return "DW_OP_deref_size";
4765     case DW_OP_xderef_size:
4766       return "DW_OP_xderef_size";
4767     case DW_OP_nop:
4768       return "DW_OP_nop";
4769
4770     case DW_OP_push_object_address:
4771       return "DW_OP_push_object_address";
4772     case DW_OP_call2:
4773       return "DW_OP_call2";
4774     case DW_OP_call4:
4775       return "DW_OP_call4";
4776     case DW_OP_call_ref:
4777       return "DW_OP_call_ref";
4778     case DW_OP_implicit_value:
4779       return "DW_OP_implicit_value";
4780     case DW_OP_stack_value:
4781       return "DW_OP_stack_value";
4782     case DW_OP_form_tls_address:
4783       return "DW_OP_form_tls_address";
4784     case DW_OP_call_frame_cfa:
4785       return "DW_OP_call_frame_cfa";
4786     case DW_OP_bit_piece:
4787       return "DW_OP_bit_piece";
4788
4789     case DW_OP_GNU_push_tls_address:
4790       return "DW_OP_GNU_push_tls_address";
4791     case DW_OP_GNU_uninit:
4792       return "DW_OP_GNU_uninit";
4793     case DW_OP_GNU_encoded_addr:
4794       return "DW_OP_GNU_encoded_addr";
4795     case DW_OP_GNU_implicit_pointer:
4796       return "DW_OP_GNU_implicit_pointer";
4797
4798     default:
4799       return "OP_<unknown>";
4800     }
4801 }
4802
4803 /* Return a pointer to a newly allocated location description.  Location
4804    descriptions are simple expression terms that can be strung
4805    together to form more complicated location (address) descriptions.  */
4806
4807 static inline dw_loc_descr_ref
4808 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4809                unsigned HOST_WIDE_INT oprnd2)
4810 {
4811   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4812
4813   descr->dw_loc_opc = op;
4814   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4815   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4816   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4817   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4818
4819   return descr;
4820 }
4821
4822 /* Return a pointer to a newly allocated location description for
4823    REG and OFFSET.  */
4824
4825 static inline dw_loc_descr_ref
4826 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4827 {
4828   if (reg <= 31)
4829     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4830                           offset, 0);
4831   else
4832     return new_loc_descr (DW_OP_bregx, reg, offset);
4833 }
4834
4835 /* Add a location description term to a location description expression.  */
4836
4837 static inline void
4838 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4839 {
4840   dw_loc_descr_ref *d;
4841
4842   /* Find the end of the chain.  */
4843   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4844     ;
4845
4846   *d = descr;
4847 }
4848
4849 /* Add a constant OFFSET to a location expression.  */
4850
4851 static void
4852 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4853 {
4854   dw_loc_descr_ref loc;
4855   HOST_WIDE_INT *p;
4856
4857   gcc_assert (*list_head != NULL);
4858
4859   if (!offset)
4860     return;
4861
4862   /* Find the end of the chain.  */
4863   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4864     ;
4865
4866   p = NULL;
4867   if (loc->dw_loc_opc == DW_OP_fbreg
4868       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4869     p = &loc->dw_loc_oprnd1.v.val_int;
4870   else if (loc->dw_loc_opc == DW_OP_bregx)
4871     p = &loc->dw_loc_oprnd2.v.val_int;
4872
4873   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4874      offset.  Don't optimize if an signed integer overflow would happen.  */
4875   if (p != NULL
4876       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4877           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4878     *p += offset;
4879
4880   else if (offset > 0)
4881     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4882
4883   else
4884     {
4885       loc->dw_loc_next = int_loc_descriptor (-offset);
4886       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4887     }
4888 }
4889
4890 /* Add a constant OFFSET to a location list.  */
4891
4892 static void
4893 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4894 {
4895   dw_loc_list_ref d;
4896   for (d = list_head; d != NULL; d = d->dw_loc_next)
4897     loc_descr_plus_const (&d->expr, offset);
4898 }
4899
4900 #define DWARF_REF_SIZE  \
4901   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4902
4903 /* Return the size of a location descriptor.  */
4904
4905 static unsigned long
4906 size_of_loc_descr (dw_loc_descr_ref loc)
4907 {
4908   unsigned long size = 1;
4909
4910   switch (loc->dw_loc_opc)
4911     {
4912     case DW_OP_addr:
4913       size += DWARF2_ADDR_SIZE;
4914       break;
4915     case DW_OP_const1u:
4916     case DW_OP_const1s:
4917       size += 1;
4918       break;
4919     case DW_OP_const2u:
4920     case DW_OP_const2s:
4921       size += 2;
4922       break;
4923     case DW_OP_const4u:
4924     case DW_OP_const4s:
4925       size += 4;
4926       break;
4927     case DW_OP_const8u:
4928     case DW_OP_const8s:
4929       size += 8;
4930       break;
4931     case DW_OP_constu:
4932       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4933       break;
4934     case DW_OP_consts:
4935       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4936       break;
4937     case DW_OP_pick:
4938       size += 1;
4939       break;
4940     case DW_OP_plus_uconst:
4941       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4942       break;
4943     case DW_OP_skip:
4944     case DW_OP_bra:
4945       size += 2;
4946       break;
4947     case DW_OP_breg0:
4948     case DW_OP_breg1:
4949     case DW_OP_breg2:
4950     case DW_OP_breg3:
4951     case DW_OP_breg4:
4952     case DW_OP_breg5:
4953     case DW_OP_breg6:
4954     case DW_OP_breg7:
4955     case DW_OP_breg8:
4956     case DW_OP_breg9:
4957     case DW_OP_breg10:
4958     case DW_OP_breg11:
4959     case DW_OP_breg12:
4960     case DW_OP_breg13:
4961     case DW_OP_breg14:
4962     case DW_OP_breg15:
4963     case DW_OP_breg16:
4964     case DW_OP_breg17:
4965     case DW_OP_breg18:
4966     case DW_OP_breg19:
4967     case DW_OP_breg20:
4968     case DW_OP_breg21:
4969     case DW_OP_breg22:
4970     case DW_OP_breg23:
4971     case DW_OP_breg24:
4972     case DW_OP_breg25:
4973     case DW_OP_breg26:
4974     case DW_OP_breg27:
4975     case DW_OP_breg28:
4976     case DW_OP_breg29:
4977     case DW_OP_breg30:
4978     case DW_OP_breg31:
4979       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4980       break;
4981     case DW_OP_regx:
4982       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4983       break;
4984     case DW_OP_fbreg:
4985       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4986       break;
4987     case DW_OP_bregx:
4988       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4989       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4990       break;
4991     case DW_OP_piece:
4992       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4993       break;
4994     case DW_OP_bit_piece:
4995       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4996       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4997       break;
4998     case DW_OP_deref_size:
4999     case DW_OP_xderef_size:
5000       size += 1;
5001       break;
5002     case DW_OP_call2:
5003       size += 2;
5004       break;
5005     case DW_OP_call4:
5006       size += 4;
5007       break;
5008     case DW_OP_call_ref:
5009       size += DWARF_REF_SIZE;
5010       break;
5011     case DW_OP_implicit_value:
5012       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5013               + loc->dw_loc_oprnd1.v.val_unsigned;
5014       break;
5015     case DW_OP_GNU_implicit_pointer:
5016       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5017       break;
5018     default:
5019       break;
5020     }
5021
5022   return size;
5023 }
5024
5025 /* Return the size of a series of location descriptors.  */
5026
5027 static unsigned long
5028 size_of_locs (dw_loc_descr_ref loc)
5029 {
5030   dw_loc_descr_ref l;
5031   unsigned long size;
5032
5033   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5034      field, to avoid writing to a PCH file.  */
5035   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5036     {
5037       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5038         break;
5039       size += size_of_loc_descr (l);
5040     }
5041   if (! l)
5042     return size;
5043
5044   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5045     {
5046       l->dw_loc_addr = size;
5047       size += size_of_loc_descr (l);
5048     }
5049
5050   return size;
5051 }
5052
5053 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5054 static void get_ref_die_offset_label (char *, dw_die_ref);
5055
5056 /* Output location description stack opcode's operands (if any).  */
5057
5058 static void
5059 output_loc_operands (dw_loc_descr_ref loc)
5060 {
5061   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5062   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5063
5064   switch (loc->dw_loc_opc)
5065     {
5066 #ifdef DWARF2_DEBUGGING_INFO
5067     case DW_OP_const2u:
5068     case DW_OP_const2s:
5069       dw2_asm_output_data (2, val1->v.val_int, NULL);
5070       break;
5071     case DW_OP_const4u:
5072       if (loc->dtprel)
5073         {
5074           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5075           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5076                                                val1->v.val_addr);
5077           fputc ('\n', asm_out_file);
5078           break;
5079         }
5080       /* FALLTHRU */
5081     case DW_OP_const4s:
5082       dw2_asm_output_data (4, val1->v.val_int, NULL);
5083       break;
5084     case DW_OP_const8u:
5085       if (loc->dtprel)
5086         {
5087           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5088           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5089                                                val1->v.val_addr);
5090           fputc ('\n', asm_out_file);
5091           break;
5092         }
5093       /* FALLTHRU */
5094     case DW_OP_const8s:
5095       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5096       dw2_asm_output_data (8, val1->v.val_int, NULL);
5097       break;
5098     case DW_OP_skip:
5099     case DW_OP_bra:
5100       {
5101         int offset;
5102
5103         gcc_assert (val1->val_class == dw_val_class_loc);
5104         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5105
5106         dw2_asm_output_data (2, offset, NULL);
5107       }
5108       break;
5109     case DW_OP_implicit_value:
5110       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5111       switch (val2->val_class)
5112         {
5113         case dw_val_class_const:
5114           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5115           break;
5116         case dw_val_class_vec:
5117           {
5118             unsigned int elt_size = val2->v.val_vec.elt_size;
5119             unsigned int len = val2->v.val_vec.length;
5120             unsigned int i;
5121             unsigned char *p;
5122
5123             if (elt_size > sizeof (HOST_WIDE_INT))
5124               {
5125                 elt_size /= 2;
5126                 len *= 2;
5127               }
5128             for (i = 0, p = val2->v.val_vec.array;
5129                  i < len;
5130                  i++, p += elt_size)
5131               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5132                                    "fp or vector constant word %u", i);
5133           }
5134           break;
5135         case dw_val_class_const_double:
5136           {
5137             unsigned HOST_WIDE_INT first, second;
5138
5139             if (WORDS_BIG_ENDIAN)
5140               {
5141                 first = val2->v.val_double.high;
5142                 second = val2->v.val_double.low;
5143               }
5144             else
5145               {
5146                 first = val2->v.val_double.low;
5147                 second = val2->v.val_double.high;
5148               }
5149             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5150                                  first, NULL);
5151             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5152                                  second, NULL);
5153           }
5154           break;
5155         case dw_val_class_addr:
5156           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5157           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5158           break;
5159         default:
5160           gcc_unreachable ();
5161         }
5162       break;
5163 #else
5164     case DW_OP_const2u:
5165     case DW_OP_const2s:
5166     case DW_OP_const4u:
5167     case DW_OP_const4s:
5168     case DW_OP_const8u:
5169     case DW_OP_const8s:
5170     case DW_OP_skip:
5171     case DW_OP_bra:
5172     case DW_OP_implicit_value:
5173       /* We currently don't make any attempt to make sure these are
5174          aligned properly like we do for the main unwind info, so
5175          don't support emitting things larger than a byte if we're
5176          only doing unwinding.  */
5177       gcc_unreachable ();
5178 #endif
5179     case DW_OP_const1u:
5180     case DW_OP_const1s:
5181       dw2_asm_output_data (1, val1->v.val_int, NULL);
5182       break;
5183     case DW_OP_constu:
5184       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5185       break;
5186     case DW_OP_consts:
5187       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5188       break;
5189     case DW_OP_pick:
5190       dw2_asm_output_data (1, val1->v.val_int, NULL);
5191       break;
5192     case DW_OP_plus_uconst:
5193       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5194       break;
5195     case DW_OP_breg0:
5196     case DW_OP_breg1:
5197     case DW_OP_breg2:
5198     case DW_OP_breg3:
5199     case DW_OP_breg4:
5200     case DW_OP_breg5:
5201     case DW_OP_breg6:
5202     case DW_OP_breg7:
5203     case DW_OP_breg8:
5204     case DW_OP_breg9:
5205     case DW_OP_breg10:
5206     case DW_OP_breg11:
5207     case DW_OP_breg12:
5208     case DW_OP_breg13:
5209     case DW_OP_breg14:
5210     case DW_OP_breg15:
5211     case DW_OP_breg16:
5212     case DW_OP_breg17:
5213     case DW_OP_breg18:
5214     case DW_OP_breg19:
5215     case DW_OP_breg20:
5216     case DW_OP_breg21:
5217     case DW_OP_breg22:
5218     case DW_OP_breg23:
5219     case DW_OP_breg24:
5220     case DW_OP_breg25:
5221     case DW_OP_breg26:
5222     case DW_OP_breg27:
5223     case DW_OP_breg28:
5224     case DW_OP_breg29:
5225     case DW_OP_breg30:
5226     case DW_OP_breg31:
5227       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5228       break;
5229     case DW_OP_regx:
5230       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5231       break;
5232     case DW_OP_fbreg:
5233       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5234       break;
5235     case DW_OP_bregx:
5236       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5237       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5238       break;
5239     case DW_OP_piece:
5240       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5241       break;
5242     case DW_OP_bit_piece:
5243       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5244       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5245       break;
5246     case DW_OP_deref_size:
5247     case DW_OP_xderef_size:
5248       dw2_asm_output_data (1, val1->v.val_int, NULL);
5249       break;
5250
5251     case DW_OP_addr:
5252       if (loc->dtprel)
5253         {
5254           if (targetm.asm_out.output_dwarf_dtprel)
5255             {
5256               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5257                                                    DWARF2_ADDR_SIZE,
5258                                                    val1->v.val_addr);
5259               fputc ('\n', asm_out_file);
5260             }
5261           else
5262             gcc_unreachable ();
5263         }
5264       else
5265         {
5266 #ifdef DWARF2_DEBUGGING_INFO
5267           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5268 #else
5269           gcc_unreachable ();
5270 #endif
5271         }
5272       break;
5273
5274     case DW_OP_GNU_implicit_pointer:
5275       {
5276         char label[MAX_ARTIFICIAL_LABEL_BYTES
5277                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5278         gcc_assert (val1->val_class == dw_val_class_die_ref);
5279         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5280         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5281         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5282       }
5283       break;
5284
5285     default:
5286       /* Other codes have no operands.  */
5287       break;
5288     }
5289 }
5290
5291 /* Output a sequence of location operations.  */
5292
5293 static void
5294 output_loc_sequence (dw_loc_descr_ref loc)
5295 {
5296   for (; loc != NULL; loc = loc->dw_loc_next)
5297     {
5298       /* Output the opcode.  */
5299       dw2_asm_output_data (1, loc->dw_loc_opc,
5300                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5301
5302       /* Output the operand(s) (if any).  */
5303       output_loc_operands (loc);
5304     }
5305 }
5306
5307 /* Output location description stack opcode's operands (if any).
5308    The output is single bytes on a line, suitable for .cfi_escape.  */
5309
5310 static void
5311 output_loc_operands_raw (dw_loc_descr_ref loc)
5312 {
5313   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5314   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5315
5316   switch (loc->dw_loc_opc)
5317     {
5318     case DW_OP_addr:
5319     case DW_OP_implicit_value:
5320       /* We cannot output addresses in .cfi_escape, only bytes.  */
5321       gcc_unreachable ();
5322
5323     case DW_OP_const1u:
5324     case DW_OP_const1s:
5325     case DW_OP_pick:
5326     case DW_OP_deref_size:
5327     case DW_OP_xderef_size:
5328       fputc (',', asm_out_file);
5329       dw2_asm_output_data_raw (1, val1->v.val_int);
5330       break;
5331
5332     case DW_OP_const2u:
5333     case DW_OP_const2s:
5334       fputc (',', asm_out_file);
5335       dw2_asm_output_data_raw (2, val1->v.val_int);
5336       break;
5337
5338     case DW_OP_const4u:
5339     case DW_OP_const4s:
5340       fputc (',', asm_out_file);
5341       dw2_asm_output_data_raw (4, val1->v.val_int);
5342       break;
5343
5344     case DW_OP_const8u:
5345     case DW_OP_const8s:
5346       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5347       fputc (',', asm_out_file);
5348       dw2_asm_output_data_raw (8, val1->v.val_int);
5349       break;
5350
5351     case DW_OP_skip:
5352     case DW_OP_bra:
5353       {
5354         int offset;
5355
5356         gcc_assert (val1->val_class == dw_val_class_loc);
5357         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5358
5359         fputc (',', asm_out_file);
5360         dw2_asm_output_data_raw (2, offset);
5361       }
5362       break;
5363
5364     case DW_OP_constu:
5365     case DW_OP_plus_uconst:
5366     case DW_OP_regx:
5367     case DW_OP_piece:
5368       fputc (',', asm_out_file);
5369       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5370       break;
5371
5372     case DW_OP_bit_piece:
5373       fputc (',', asm_out_file);
5374       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5375       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5376       break;
5377
5378     case DW_OP_consts:
5379     case DW_OP_breg0:
5380     case DW_OP_breg1:
5381     case DW_OP_breg2:
5382     case DW_OP_breg3:
5383     case DW_OP_breg4:
5384     case DW_OP_breg5:
5385     case DW_OP_breg6:
5386     case DW_OP_breg7:
5387     case DW_OP_breg8:
5388     case DW_OP_breg9:
5389     case DW_OP_breg10:
5390     case DW_OP_breg11:
5391     case DW_OP_breg12:
5392     case DW_OP_breg13:
5393     case DW_OP_breg14:
5394     case DW_OP_breg15:
5395     case DW_OP_breg16:
5396     case DW_OP_breg17:
5397     case DW_OP_breg18:
5398     case DW_OP_breg19:
5399     case DW_OP_breg20:
5400     case DW_OP_breg21:
5401     case DW_OP_breg22:
5402     case DW_OP_breg23:
5403     case DW_OP_breg24:
5404     case DW_OP_breg25:
5405     case DW_OP_breg26:
5406     case DW_OP_breg27:
5407     case DW_OP_breg28:
5408     case DW_OP_breg29:
5409     case DW_OP_breg30:
5410     case DW_OP_breg31:
5411     case DW_OP_fbreg:
5412       fputc (',', asm_out_file);
5413       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5414       break;
5415
5416     case DW_OP_bregx:
5417       fputc (',', asm_out_file);
5418       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5419       fputc (',', asm_out_file);
5420       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5421       break;
5422
5423     case DW_OP_GNU_implicit_pointer:
5424       gcc_unreachable ();
5425       break;
5426
5427     default:
5428       /* Other codes have no operands.  */
5429       break;
5430     }
5431 }
5432
5433 static void
5434 output_loc_sequence_raw (dw_loc_descr_ref loc)
5435 {
5436   while (1)
5437     {
5438       /* Output the opcode.  */
5439       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5440       output_loc_operands_raw (loc);
5441
5442       if (!loc->dw_loc_next)
5443         break;
5444       loc = loc->dw_loc_next;
5445
5446       fputc (',', asm_out_file);
5447     }
5448 }
5449
5450 /* This routine will generate the correct assembly data for a location
5451    description based on a cfi entry with a complex address.  */
5452
5453 static void
5454 output_cfa_loc (dw_cfi_ref cfi)
5455 {
5456   dw_loc_descr_ref loc;
5457   unsigned long size;
5458
5459   if (cfi->dw_cfi_opc == DW_CFA_expression)
5460     {
5461       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5462       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5463     }
5464   else
5465     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5466
5467   /* Output the size of the block.  */
5468   size = size_of_locs (loc);
5469   dw2_asm_output_data_uleb128 (size, NULL);
5470
5471   /* Now output the operations themselves.  */
5472   output_loc_sequence (loc);
5473 }
5474
5475 /* Similar, but used for .cfi_escape.  */
5476
5477 static void
5478 output_cfa_loc_raw (dw_cfi_ref cfi)
5479 {
5480   dw_loc_descr_ref loc;
5481   unsigned long size;
5482
5483   if (cfi->dw_cfi_opc == DW_CFA_expression)
5484     {
5485       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5486       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5487     }
5488   else
5489     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5490
5491   /* Output the size of the block.  */
5492   size = size_of_locs (loc);
5493   dw2_asm_output_data_uleb128_raw (size);
5494   fputc (',', asm_out_file);
5495
5496   /* Now output the operations themselves.  */
5497   output_loc_sequence_raw (loc);
5498 }
5499
5500 /* This function builds a dwarf location descriptor sequence from a
5501    dw_cfa_location, adding the given OFFSET to the result of the
5502    expression.  */
5503
5504 static struct dw_loc_descr_struct *
5505 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5506 {
5507   struct dw_loc_descr_struct *head, *tmp;
5508
5509   offset += cfa->offset;
5510
5511   if (cfa->indirect)
5512     {
5513       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5514       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5515       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5516       add_loc_descr (&head, tmp);
5517       if (offset != 0)
5518         {
5519           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5520           add_loc_descr (&head, tmp);
5521         }
5522     }
5523   else
5524     head = new_reg_loc_descr (cfa->reg, offset);
5525
5526   return head;
5527 }
5528
5529 /* This function builds a dwarf location descriptor sequence for
5530    the address at OFFSET from the CFA when stack is aligned to
5531    ALIGNMENT byte.  */
5532
5533 static struct dw_loc_descr_struct *
5534 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5535 {
5536   struct dw_loc_descr_struct *head;
5537   unsigned int dwarf_fp
5538     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5539
5540  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5541   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5542     {
5543       head = new_reg_loc_descr (dwarf_fp, 0);
5544       add_loc_descr (&head, int_loc_descriptor (alignment));
5545       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5546       loc_descr_plus_const (&head, offset);
5547     }
5548   else
5549     head = new_reg_loc_descr (dwarf_fp, offset);
5550   return head;
5551 }
5552
5553 /* This function fills in aa dw_cfa_location structure from a dwarf location
5554    descriptor sequence.  */
5555
5556 static void
5557 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5558 {
5559   struct dw_loc_descr_struct *ptr;
5560   cfa->offset = 0;
5561   cfa->base_offset = 0;
5562   cfa->indirect = 0;
5563   cfa->reg = -1;
5564
5565   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5566     {
5567       enum dwarf_location_atom op = ptr->dw_loc_opc;
5568
5569       switch (op)
5570         {
5571         case DW_OP_reg0:
5572         case DW_OP_reg1:
5573         case DW_OP_reg2:
5574         case DW_OP_reg3:
5575         case DW_OP_reg4:
5576         case DW_OP_reg5:
5577         case DW_OP_reg6:
5578         case DW_OP_reg7:
5579         case DW_OP_reg8:
5580         case DW_OP_reg9:
5581         case DW_OP_reg10:
5582         case DW_OP_reg11:
5583         case DW_OP_reg12:
5584         case DW_OP_reg13:
5585         case DW_OP_reg14:
5586         case DW_OP_reg15:
5587         case DW_OP_reg16:
5588         case DW_OP_reg17:
5589         case DW_OP_reg18:
5590         case DW_OP_reg19:
5591         case DW_OP_reg20:
5592         case DW_OP_reg21:
5593         case DW_OP_reg22:
5594         case DW_OP_reg23:
5595         case DW_OP_reg24:
5596         case DW_OP_reg25:
5597         case DW_OP_reg26:
5598         case DW_OP_reg27:
5599         case DW_OP_reg28:
5600         case DW_OP_reg29:
5601         case DW_OP_reg30:
5602         case DW_OP_reg31:
5603           cfa->reg = op - DW_OP_reg0;
5604           break;
5605         case DW_OP_regx:
5606           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5607           break;
5608         case DW_OP_breg0:
5609         case DW_OP_breg1:
5610         case DW_OP_breg2:
5611         case DW_OP_breg3:
5612         case DW_OP_breg4:
5613         case DW_OP_breg5:
5614         case DW_OP_breg6:
5615         case DW_OP_breg7:
5616         case DW_OP_breg8:
5617         case DW_OP_breg9:
5618         case DW_OP_breg10:
5619         case DW_OP_breg11:
5620         case DW_OP_breg12:
5621         case DW_OP_breg13:
5622         case DW_OP_breg14:
5623         case DW_OP_breg15:
5624         case DW_OP_breg16:
5625         case DW_OP_breg17:
5626         case DW_OP_breg18:
5627         case DW_OP_breg19:
5628         case DW_OP_breg20:
5629         case DW_OP_breg21:
5630         case DW_OP_breg22:
5631         case DW_OP_breg23:
5632         case DW_OP_breg24:
5633         case DW_OP_breg25:
5634         case DW_OP_breg26:
5635         case DW_OP_breg27:
5636         case DW_OP_breg28:
5637         case DW_OP_breg29:
5638         case DW_OP_breg30:
5639         case DW_OP_breg31:
5640           cfa->reg = op - DW_OP_breg0;
5641           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5642           break;
5643         case DW_OP_bregx:
5644           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5645           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5646           break;
5647         case DW_OP_deref:
5648           cfa->indirect = 1;
5649           break;
5650         case DW_OP_plus_uconst:
5651           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5652           break;
5653         default:
5654           internal_error ("DW_LOC_OP %s not implemented",
5655                           dwarf_stack_op_name (ptr->dw_loc_opc));
5656         }
5657     }
5658 }
5659 \f
5660 /* And now, the support for symbolic debugging information.  */
5661
5662 /* .debug_str support.  */
5663 static int output_indirect_string (void **, void *);
5664
5665 static void dwarf2out_init (const char *);
5666 static void dwarf2out_finish (const char *);
5667 static void dwarf2out_assembly_start (void);
5668 static void dwarf2out_define (unsigned int, const char *);
5669 static void dwarf2out_undef (unsigned int, const char *);
5670 static void dwarf2out_start_source_file (unsigned, const char *);
5671 static void dwarf2out_end_source_file (unsigned);
5672 static void dwarf2out_function_decl (tree);
5673 static void dwarf2out_begin_block (unsigned, unsigned);
5674 static void dwarf2out_end_block (unsigned, unsigned);
5675 static bool dwarf2out_ignore_block (const_tree);
5676 static void dwarf2out_global_decl (tree);
5677 static void dwarf2out_type_decl (tree, int);
5678 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5679 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5680                                                  dw_die_ref);
5681 static void dwarf2out_abstract_function (tree);
5682 static void dwarf2out_var_location (rtx);
5683 static void dwarf2out_direct_call (tree);
5684 static void dwarf2out_virtual_call_token (tree, int);
5685 static void dwarf2out_copy_call_info (rtx, rtx);
5686 static void dwarf2out_virtual_call (int);
5687 static void dwarf2out_begin_function (tree);
5688 static void dwarf2out_set_name (tree, tree);
5689
5690 /* The debug hooks structure.  */
5691
5692 const struct gcc_debug_hooks dwarf2_debug_hooks =
5693 {
5694   dwarf2out_init,
5695   dwarf2out_finish,
5696   dwarf2out_assembly_start,
5697   dwarf2out_define,
5698   dwarf2out_undef,
5699   dwarf2out_start_source_file,
5700   dwarf2out_end_source_file,
5701   dwarf2out_begin_block,
5702   dwarf2out_end_block,
5703   dwarf2out_ignore_block,
5704   dwarf2out_source_line,
5705   dwarf2out_begin_prologue,
5706 #if VMS_DEBUGGING_INFO
5707   dwarf2out_vms_end_prologue,
5708   dwarf2out_vms_begin_epilogue,
5709 #else
5710   debug_nothing_int_charstar,
5711   debug_nothing_int_charstar,
5712 #endif
5713   dwarf2out_end_epilogue,
5714   dwarf2out_begin_function,
5715   debug_nothing_int,            /* end_function */
5716   dwarf2out_function_decl,      /* function_decl */
5717   dwarf2out_global_decl,
5718   dwarf2out_type_decl,          /* type_decl */
5719   dwarf2out_imported_module_or_decl,
5720   debug_nothing_tree,           /* deferred_inline_function */
5721   /* The DWARF 2 backend tries to reduce debugging bloat by not
5722      emitting the abstract description of inline functions until
5723      something tries to reference them.  */
5724   dwarf2out_abstract_function,  /* outlining_inline_function */
5725   debug_nothing_rtx,            /* label */
5726   debug_nothing_int,            /* handle_pch */
5727   dwarf2out_var_location,
5728   dwarf2out_switch_text_section,
5729   dwarf2out_direct_call,
5730   dwarf2out_virtual_call_token,
5731   dwarf2out_copy_call_info,
5732   dwarf2out_virtual_call,
5733   dwarf2out_set_name,
5734   1,                            /* start_end_main_source_file */
5735   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5736 };
5737 \f
5738 /* NOTE: In the comments in this file, many references are made to
5739    "Debugging Information Entries".  This term is abbreviated as `DIE'
5740    throughout the remainder of this file.  */
5741
5742 /* An internal representation of the DWARF output is built, and then
5743    walked to generate the DWARF debugging info.  The walk of the internal
5744    representation is done after the entire program has been compiled.
5745    The types below are used to describe the internal representation.  */
5746
5747 /* Various DIE's use offsets relative to the beginning of the
5748    .debug_info section to refer to each other.  */
5749
5750 typedef long int dw_offset;
5751
5752 /* Define typedefs here to avoid circular dependencies.  */
5753
5754 typedef struct dw_attr_struct *dw_attr_ref;
5755 typedef struct dw_line_info_struct *dw_line_info_ref;
5756 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5757 typedef struct pubname_struct *pubname_ref;
5758 typedef struct dw_ranges_struct *dw_ranges_ref;
5759 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5760 typedef struct comdat_type_struct *comdat_type_node_ref;
5761
5762 /* Each entry in the line_info_table maintains the file and
5763    line number associated with the label generated for that
5764    entry.  The label gives the PC value associated with
5765    the line number entry.  */
5766
5767 typedef struct GTY(()) dw_line_info_struct {
5768   unsigned long dw_file_num;
5769   unsigned long dw_line_num;
5770 }
5771 dw_line_info_entry;
5772
5773 /* Line information for functions in separate sections; each one gets its
5774    own sequence.  */
5775 typedef struct GTY(()) dw_separate_line_info_struct {
5776   unsigned long dw_file_num;
5777   unsigned long dw_line_num;
5778   unsigned long function;
5779 }
5780 dw_separate_line_info_entry;
5781
5782 /* Each DIE attribute has a field specifying the attribute kind,
5783    a link to the next attribute in the chain, and an attribute value.
5784    Attributes are typically linked below the DIE they modify.  */
5785
5786 typedef struct GTY(()) dw_attr_struct {
5787   enum dwarf_attribute dw_attr;
5788   dw_val_node dw_attr_val;
5789 }
5790 dw_attr_node;
5791
5792 DEF_VEC_O(dw_attr_node);
5793 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5794
5795 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5796    The children of each node form a circular list linked by
5797    die_sib.  die_child points to the node *before* the "first" child node.  */
5798
5799 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5800   union die_symbol_or_type_node
5801     {
5802       char * GTY ((tag ("0"))) die_symbol;
5803       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5804     }
5805   GTY ((desc ("dwarf_version >= 4"))) die_id;
5806   VEC(dw_attr_node,gc) * die_attr;
5807   dw_die_ref die_parent;
5808   dw_die_ref die_child;
5809   dw_die_ref die_sib;
5810   dw_die_ref die_definition; /* ref from a specification to its definition */
5811   dw_offset die_offset;
5812   unsigned long die_abbrev;
5813   int die_mark;
5814   /* Die is used and must not be pruned as unused.  */
5815   int die_perennial_p;
5816   unsigned int decl_id;
5817   enum dwarf_tag die_tag;
5818 }
5819 die_node;
5820
5821 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5822 #define FOR_EACH_CHILD(die, c, expr) do {       \
5823   c = die->die_child;                           \
5824   if (c) do {                                   \
5825     c = c->die_sib;                             \
5826     expr;                                       \
5827   } while (c != die->die_child);                \
5828 } while (0)
5829
5830 /* The pubname structure */
5831
5832 typedef struct GTY(()) pubname_struct {
5833   dw_die_ref die;
5834   const char *name;
5835 }
5836 pubname_entry;
5837
5838 DEF_VEC_O(pubname_entry);
5839 DEF_VEC_ALLOC_O(pubname_entry, gc);
5840
5841 struct GTY(()) dw_ranges_struct {
5842   /* If this is positive, it's a block number, otherwise it's a
5843      bitwise-negated index into dw_ranges_by_label.  */
5844   int num;
5845 };
5846
5847 /* A structure to hold a macinfo entry.  */
5848
5849 typedef struct GTY(()) macinfo_struct {
5850   unsigned HOST_WIDE_INT code;
5851   unsigned HOST_WIDE_INT lineno;
5852   const char *info;
5853 }
5854 macinfo_entry;
5855
5856 DEF_VEC_O(macinfo_entry);
5857 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5858
5859 struct GTY(()) dw_ranges_by_label_struct {
5860   const char *begin;
5861   const char *end;
5862 };
5863
5864 /* The comdat type node structure.  */
5865 typedef struct GTY(()) comdat_type_struct
5866 {
5867   dw_die_ref root_die;
5868   dw_die_ref type_die;
5869   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5870   struct comdat_type_struct *next;
5871 }
5872 comdat_type_node;
5873
5874 /* The limbo die list structure.  */
5875 typedef struct GTY(()) limbo_die_struct {
5876   dw_die_ref die;
5877   tree created_for;
5878   struct limbo_die_struct *next;
5879 }
5880 limbo_die_node;
5881
5882 typedef struct GTY(()) skeleton_chain_struct
5883 {
5884   dw_die_ref old_die;
5885   dw_die_ref new_die;
5886   struct skeleton_chain_struct *parent;
5887 }
5888 skeleton_chain_node;
5889
5890 /* How to start an assembler comment.  */
5891 #ifndef ASM_COMMENT_START
5892 #define ASM_COMMENT_START ";#"
5893 #endif
5894
5895 /* Define a macro which returns nonzero for a TYPE_DECL which was
5896    implicitly generated for a tagged type.
5897
5898    Note that unlike the gcc front end (which generates a NULL named
5899    TYPE_DECL node for each complete tagged type, each array type, and
5900    each function type node created) the g++ front end generates a
5901    _named_ TYPE_DECL node for each tagged type node created.
5902    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5903    generate a DW_TAG_typedef DIE for them.  */
5904
5905 #define TYPE_DECL_IS_STUB(decl)                         \
5906   (DECL_NAME (decl) == NULL_TREE                        \
5907    || (DECL_ARTIFICIAL (decl)                           \
5908        && is_tagged_type (TREE_TYPE (decl))             \
5909        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5910            /* This is necessary for stub decls that     \
5911               appear in nested inline functions.  */    \
5912            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5913                && (decl_ultimate_origin (decl)          \
5914                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5915
5916 /* Information concerning the compilation unit's programming
5917    language, and compiler version.  */
5918
5919 /* Fixed size portion of the DWARF compilation unit header.  */
5920 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5921   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5922
5923 /* Fixed size portion of the DWARF comdat type unit header.  */
5924 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5925   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5926    + DWARF_OFFSET_SIZE)
5927
5928 /* Fixed size portion of public names info.  */
5929 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5930
5931 /* Fixed size portion of the address range info.  */
5932 #define DWARF_ARANGES_HEADER_SIZE                                       \
5933   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5934                 DWARF2_ADDR_SIZE * 2)                                   \
5935    - DWARF_INITIAL_LENGTH_SIZE)
5936
5937 /* Size of padding portion in the address range info.  It must be
5938    aligned to twice the pointer size.  */
5939 #define DWARF_ARANGES_PAD_SIZE \
5940   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5941                 DWARF2_ADDR_SIZE * 2)                              \
5942    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5943
5944 /* Use assembler line directives if available.  */
5945 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5946 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5947 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5948 #else
5949 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5950 #endif
5951 #endif
5952
5953 /* Minimum line offset in a special line info. opcode.
5954    This value was chosen to give a reasonable range of values.  */
5955 #define DWARF_LINE_BASE  -10
5956
5957 /* First special line opcode - leave room for the standard opcodes.  */
5958 #define DWARF_LINE_OPCODE_BASE  10
5959
5960 /* Range of line offsets in a special line info. opcode.  */
5961 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5962
5963 /* Flag that indicates the initial value of the is_stmt_start flag.
5964    In the present implementation, we do not mark any lines as
5965    the beginning of a source statement, because that information
5966    is not made available by the GCC front-end.  */
5967 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5968
5969 /* Maximum number of operations per instruction bundle.  */
5970 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5971 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5972 #endif
5973
5974 /* This location is used by calc_die_sizes() to keep track
5975    the offset of each DIE within the .debug_info section.  */
5976 static unsigned long next_die_offset;
5977
5978 /* Record the root of the DIE's built for the current compilation unit.  */
5979 static GTY(()) dw_die_ref single_comp_unit_die;
5980
5981 /* A list of type DIEs that have been separated into comdat sections.  */
5982 static GTY(()) comdat_type_node *comdat_type_list;
5983
5984 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5985 static GTY(()) limbo_die_node *limbo_die_list;
5986
5987 /* A list of DIEs for which we may have to generate
5988    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5989 static GTY(()) limbo_die_node *deferred_asm_name;
5990
5991 /* Filenames referenced by this compilation unit.  */
5992 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5993
5994 /* A hash table of references to DIE's that describe declarations.
5995    The key is a DECL_UID() which is a unique number identifying each decl.  */
5996 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5997
5998 /* A hash table of references to DIE's that describe COMMON blocks.
5999    The key is DECL_UID() ^ die_parent.  */
6000 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6001
6002 typedef struct GTY(()) die_arg_entry_struct {
6003     dw_die_ref die;
6004     tree arg;
6005 } die_arg_entry;
6006
6007 DEF_VEC_O(die_arg_entry);
6008 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6009
6010 /* Node of the variable location list.  */
6011 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6012   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6013      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6014      in mode of the EXPR_LIST node and first EXPR_LIST operand
6015      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6016      location or NULL for padding.  For larger bitsizes,
6017      mode is 0 and first operand is a CONCAT with bitsize
6018      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6019      NULL as second operand.  */
6020   rtx GTY (()) loc;
6021   const char * GTY (()) label;
6022   struct var_loc_node * GTY (()) next;
6023 };
6024
6025 /* Variable location list.  */
6026 struct GTY (()) var_loc_list_def {
6027   struct var_loc_node * GTY (()) first;
6028
6029   /* Pointer to the last but one or last element of the
6030      chained list.  If the list is empty, both first and
6031      last are NULL, if the list contains just one node
6032      or the last node certainly is not redundant, it points
6033      to the last node, otherwise points to the last but one.
6034      Do not mark it for GC because it is marked through the chain.  */
6035   struct var_loc_node * GTY ((skip ("%h"))) last;
6036
6037   /* DECL_UID of the variable decl.  */
6038   unsigned int decl_id;
6039 };
6040 typedef struct var_loc_list_def var_loc_list;
6041
6042
6043 /* Table of decl location linked lists.  */
6044 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6045
6046 /* A pointer to the base of a list of references to DIE's that
6047    are uniquely identified by their tag, presence/absence of
6048    children DIE's, and list of attribute/value pairs.  */
6049 static GTY((length ("abbrev_die_table_allocated")))
6050   dw_die_ref *abbrev_die_table;
6051
6052 /* Number of elements currently allocated for abbrev_die_table.  */
6053 static GTY(()) unsigned abbrev_die_table_allocated;
6054
6055 /* Number of elements in type_die_table currently in use.  */
6056 static GTY(()) unsigned abbrev_die_table_in_use;
6057
6058 /* Size (in elements) of increments by which we may expand the
6059    abbrev_die_table.  */
6060 #define ABBREV_DIE_TABLE_INCREMENT 256
6061
6062 /* A pointer to the base of a table that contains line information
6063    for each source code line in .text in the compilation unit.  */
6064 static GTY((length ("line_info_table_allocated")))
6065      dw_line_info_ref line_info_table;
6066
6067 /* Number of elements currently allocated for line_info_table.  */
6068 static GTY(()) unsigned line_info_table_allocated;
6069
6070 /* Number of elements in line_info_table currently in use.  */
6071 static GTY(()) unsigned line_info_table_in_use;
6072
6073 /* A pointer to the base of a table that contains line information
6074    for each source code line outside of .text in the compilation unit.  */
6075 static GTY ((length ("separate_line_info_table_allocated")))
6076      dw_separate_line_info_ref separate_line_info_table;
6077
6078 /* Number of elements currently allocated for separate_line_info_table.  */
6079 static GTY(()) unsigned separate_line_info_table_allocated;
6080
6081 /* Number of elements in separate_line_info_table currently in use.  */
6082 static GTY(()) unsigned separate_line_info_table_in_use;
6083
6084 /* Size (in elements) of increments by which we may expand the
6085    line_info_table.  */
6086 #define LINE_INFO_TABLE_INCREMENT 1024
6087
6088 /* A flag to tell pubnames/types export if there is an info section to
6089    refer to.  */
6090 static bool info_section_emitted;
6091
6092 /* A pointer to the base of a table that contains a list of publicly
6093    accessible names.  */
6094 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6095
6096 /* A pointer to the base of a table that contains a list of publicly
6097    accessible types.  */
6098 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6099
6100 /* A pointer to the base of a table that contains a list of macro
6101    defines/undefines (and file start/end markers).  */
6102 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6103
6104 /* Array of dies for which we should generate .debug_arange info.  */
6105 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6106
6107 /* Number of elements currently allocated for arange_table.  */
6108 static GTY(()) unsigned arange_table_allocated;
6109
6110 /* Number of elements in arange_table currently in use.  */
6111 static GTY(()) unsigned arange_table_in_use;
6112
6113 /* Size (in elements) of increments by which we may expand the
6114    arange_table.  */
6115 #define ARANGE_TABLE_INCREMENT 64
6116
6117 /* Array of dies for which we should generate .debug_ranges info.  */
6118 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6119
6120 /* Number of elements currently allocated for ranges_table.  */
6121 static GTY(()) unsigned ranges_table_allocated;
6122
6123 /* Number of elements in ranges_table currently in use.  */
6124 static GTY(()) unsigned ranges_table_in_use;
6125
6126 /* Array of pairs of labels referenced in ranges_table.  */
6127 static GTY ((length ("ranges_by_label_allocated")))
6128      dw_ranges_by_label_ref ranges_by_label;
6129
6130 /* Number of elements currently allocated for ranges_by_label.  */
6131 static GTY(()) unsigned ranges_by_label_allocated;
6132
6133 /* Number of elements in ranges_by_label currently in use.  */
6134 static GTY(()) unsigned ranges_by_label_in_use;
6135
6136 /* Size (in elements) of increments by which we may expand the
6137    ranges_table.  */
6138 #define RANGES_TABLE_INCREMENT 64
6139
6140 /* Whether we have location lists that need outputting */
6141 static GTY(()) bool have_location_lists;
6142
6143 /* Unique label counter.  */
6144 static GTY(()) unsigned int loclabel_num;
6145
6146 /* Unique label counter for point-of-call tables.  */
6147 static GTY(()) unsigned int poc_label_num;
6148
6149 /* The direct call table structure.  */
6150
6151 typedef struct GTY(()) dcall_struct {
6152   unsigned int poc_label_num;
6153   tree poc_decl;
6154   dw_die_ref targ_die;
6155 }
6156 dcall_entry;
6157
6158 DEF_VEC_O(dcall_entry);
6159 DEF_VEC_ALLOC_O(dcall_entry, gc);
6160
6161 /* The virtual call table structure.  */
6162
6163 typedef struct GTY(()) vcall_struct {
6164   unsigned int poc_label_num;
6165   unsigned int vtable_slot;
6166 }
6167 vcall_entry;
6168
6169 DEF_VEC_O(vcall_entry);
6170 DEF_VEC_ALLOC_O(vcall_entry, gc);
6171
6172 /* Pointers to the direct and virtual call tables.  */
6173 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6174 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6175
6176 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6177
6178 struct GTY (()) vcall_insn {
6179   int insn_uid;
6180   unsigned int vtable_slot;
6181 };
6182
6183 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6184
6185 /* Record whether the function being analyzed contains inlined functions.  */
6186 static int current_function_has_inlines;
6187
6188 /* The last file entry emitted by maybe_emit_file().  */
6189 static GTY(()) struct dwarf_file_data * last_emitted_file;
6190
6191 /* Number of internal labels generated by gen_internal_sym().  */
6192 static GTY(()) int label_num;
6193
6194 /* Cached result of previous call to lookup_filename.  */
6195 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6196
6197 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6198
6199 /* Offset from the "steady-state frame pointer" to the frame base,
6200    within the current function.  */
6201 static HOST_WIDE_INT frame_pointer_fb_offset;
6202
6203 /* Forward declarations for functions defined in this file.  */
6204
6205 static int is_pseudo_reg (const_rtx);
6206 static tree type_main_variant (tree);
6207 static int is_tagged_type (const_tree);
6208 static const char *dwarf_tag_name (unsigned);
6209 static const char *dwarf_attr_name (unsigned);
6210 static const char *dwarf_form_name (unsigned);
6211 static tree decl_ultimate_origin (const_tree);
6212 static tree decl_class_context (tree);
6213 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6214 static inline enum dw_val_class AT_class (dw_attr_ref);
6215 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6216 static inline unsigned AT_flag (dw_attr_ref);
6217 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6218 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6219 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6220 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6221 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6222                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6223 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6224                                unsigned int, unsigned char *);
6225 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6226 static hashval_t debug_str_do_hash (const void *);
6227 static int debug_str_eq (const void *, const void *);
6228 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6229 static inline const char *AT_string (dw_attr_ref);
6230 static enum dwarf_form AT_string_form (dw_attr_ref);
6231 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6232 static void add_AT_specification (dw_die_ref, dw_die_ref);
6233 static inline dw_die_ref AT_ref (dw_attr_ref);
6234 static inline int AT_ref_external (dw_attr_ref);
6235 static inline void set_AT_ref_external (dw_attr_ref, int);
6236 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6237 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6238 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6239 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6240                              dw_loc_list_ref);
6241 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6242 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6243 static inline rtx AT_addr (dw_attr_ref);
6244 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6245 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6246 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6247 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6248                            unsigned HOST_WIDE_INT);
6249 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6250                                unsigned long);
6251 static inline const char *AT_lbl (dw_attr_ref);
6252 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6253 static const char *get_AT_low_pc (dw_die_ref);
6254 static const char *get_AT_hi_pc (dw_die_ref);
6255 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6256 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6257 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6258 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6259 static bool is_cxx (void);
6260 static bool is_fortran (void);
6261 static bool is_ada (void);
6262 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6263 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6264 static void add_child_die (dw_die_ref, dw_die_ref);
6265 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6266 static dw_die_ref lookup_type_die (tree);
6267 static void equate_type_number_to_die (tree, dw_die_ref);
6268 static hashval_t decl_die_table_hash (const void *);
6269 static int decl_die_table_eq (const void *, const void *);
6270 static dw_die_ref lookup_decl_die (tree);
6271 static hashval_t common_block_die_table_hash (const void *);
6272 static int common_block_die_table_eq (const void *, const void *);
6273 static hashval_t decl_loc_table_hash (const void *);
6274 static int decl_loc_table_eq (const void *, const void *);
6275 static var_loc_list *lookup_decl_loc (const_tree);
6276 static void equate_decl_number_to_die (tree, dw_die_ref);
6277 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6278 static void print_spaces (FILE *);
6279 static void print_die (dw_die_ref, FILE *);
6280 static void print_dwarf_line_table (FILE *);
6281 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6282 static dw_die_ref pop_compile_unit (dw_die_ref);
6283 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6284 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6285 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6286 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6287 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6288 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6289 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6290                                    struct md5_ctx *, int *);
6291 struct checksum_attributes;
6292 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6293 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6294 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6295 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6296 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6297 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6298 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6299 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6300 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6301 static void compute_section_prefix (dw_die_ref);
6302 static int is_type_die (dw_die_ref);
6303 static int is_comdat_die (dw_die_ref);
6304 static int is_symbol_die (dw_die_ref);
6305 static void assign_symbol_names (dw_die_ref);
6306 static void break_out_includes (dw_die_ref);
6307 static int is_declaration_die (dw_die_ref);
6308 static int should_move_die_to_comdat (dw_die_ref);
6309 static dw_die_ref clone_as_declaration (dw_die_ref);
6310 static dw_die_ref clone_die (dw_die_ref);
6311 static dw_die_ref clone_tree (dw_die_ref);
6312 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6313 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6314 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6315 static dw_die_ref generate_skeleton (dw_die_ref);
6316 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6317                                                          dw_die_ref);
6318 static void break_out_comdat_types (dw_die_ref);
6319 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6320 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6321 static void copy_decls_for_unworthy_types (dw_die_ref);
6322
6323 static hashval_t htab_cu_hash (const void *);
6324 static int htab_cu_eq (const void *, const void *);
6325 static void htab_cu_del (void *);
6326 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6327 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6328 static void add_sibling_attributes (dw_die_ref);
6329 static void build_abbrev_table (dw_die_ref);
6330 static void output_location_lists (dw_die_ref);
6331 static int constant_size (unsigned HOST_WIDE_INT);
6332 static unsigned long size_of_die (dw_die_ref);
6333 static void calc_die_sizes (dw_die_ref);
6334 static void mark_dies (dw_die_ref);
6335 static void unmark_dies (dw_die_ref);
6336 static void unmark_all_dies (dw_die_ref);
6337 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6338 static unsigned long size_of_aranges (void);
6339 static enum dwarf_form value_format (dw_attr_ref);
6340 static void output_value_format (dw_attr_ref);
6341 static void output_abbrev_section (void);
6342 static void output_die_symbol (dw_die_ref);
6343 static void output_die (dw_die_ref);
6344 static void output_compilation_unit_header (void);
6345 static void output_comp_unit (dw_die_ref, int);
6346 static void output_comdat_type_unit (comdat_type_node *);
6347 static const char *dwarf2_name (tree, int);
6348 static void add_pubname (tree, dw_die_ref);
6349 static void add_pubname_string (const char *, dw_die_ref);
6350 static void add_pubtype (tree, dw_die_ref);
6351 static void output_pubnames (VEC (pubname_entry,gc) *);
6352 static void add_arange (tree, dw_die_ref);
6353 static void output_aranges (void);
6354 static unsigned int add_ranges_num (int);
6355 static unsigned int add_ranges (const_tree);
6356 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6357                                   bool *);
6358 static void output_ranges (void);
6359 static void output_line_info (void);
6360 static void output_file_names (void);
6361 static dw_die_ref base_type_die (tree);
6362 static int is_base_type (tree);
6363 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6364 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6365 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6366 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6367 static int type_is_enum (const_tree);
6368 static unsigned int dbx_reg_number (const_rtx);
6369 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6370 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6371 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6372                                                 enum var_init_status);
6373 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6374                                                      enum var_init_status);
6375 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6376                                          enum var_init_status);
6377 static int is_based_loc (const_rtx);
6378 static int resolve_one_addr (rtx *, void *);
6379 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6380                                                enum var_init_status);
6381 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6382                                         enum var_init_status);
6383 static dw_loc_list_ref loc_list_from_tree (tree, int);
6384 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6385 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6386 static tree field_type (const_tree);
6387 static unsigned int simple_type_align_in_bits (const_tree);
6388 static unsigned int simple_decl_align_in_bits (const_tree);
6389 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6390 static HOST_WIDE_INT field_byte_offset (const_tree);
6391 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6392                                          dw_loc_list_ref);
6393 static void add_data_member_location_attribute (dw_die_ref, tree);
6394 static bool add_const_value_attribute (dw_die_ref, rtx);
6395 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6396 static void insert_double (double_int, unsigned char *);
6397 static void insert_float (const_rtx, unsigned char *);
6398 static rtx rtl_for_decl_location (tree);
6399 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6400                                                    enum dwarf_attribute);
6401 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6402 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6403 static void add_name_attribute (dw_die_ref, const char *);
6404 static void add_comp_dir_attribute (dw_die_ref);
6405 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6406 static void add_subscript_info (dw_die_ref, tree, bool);
6407 static void add_byte_size_attribute (dw_die_ref, tree);
6408 static void add_bit_offset_attribute (dw_die_ref, tree);
6409 static void add_bit_size_attribute (dw_die_ref, tree);
6410 static void add_prototyped_attribute (dw_die_ref, tree);
6411 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6412 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6413 static void add_src_coords_attributes (dw_die_ref, tree);
6414 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6415 static void push_decl_scope (tree);
6416 static void pop_decl_scope (void);
6417 static dw_die_ref scope_die_for (tree, dw_die_ref);
6418 static inline int local_scope_p (dw_die_ref);
6419 static inline int class_scope_p (dw_die_ref);
6420 static inline int class_or_namespace_scope_p (dw_die_ref);
6421 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6422 static void add_calling_convention_attribute (dw_die_ref, tree);
6423 static const char *type_tag (const_tree);
6424 static tree member_declared_type (const_tree);
6425 #if 0
6426 static const char *decl_start_label (tree);
6427 #endif
6428 static void gen_array_type_die (tree, dw_die_ref);
6429 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6430 #if 0
6431 static void gen_entry_point_die (tree, dw_die_ref);
6432 #endif
6433 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6434 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6435 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6436 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6437 static void gen_formal_types_die (tree, dw_die_ref);
6438 static void gen_subprogram_die (tree, dw_die_ref);
6439 static void gen_variable_die (tree, tree, dw_die_ref);
6440 static void gen_const_die (tree, dw_die_ref);
6441 static void gen_label_die (tree, dw_die_ref);
6442 static void gen_lexical_block_die (tree, dw_die_ref, int);
6443 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6444 static void gen_field_die (tree, dw_die_ref);
6445 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6446 static dw_die_ref gen_compile_unit_die (const char *);
6447 static void gen_inheritance_die (tree, tree, dw_die_ref);
6448 static void gen_member_die (tree, dw_die_ref);
6449 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6450                                                 enum debug_info_usage);
6451 static void gen_subroutine_type_die (tree, dw_die_ref);
6452 static void gen_typedef_die (tree, dw_die_ref);
6453 static void gen_type_die (tree, dw_die_ref);
6454 static void gen_block_die (tree, dw_die_ref, int);
6455 static void decls_for_scope (tree, dw_die_ref, int);
6456 static int is_redundant_typedef (const_tree);
6457 static bool is_naming_typedef_decl (const_tree);
6458 static inline dw_die_ref get_context_die (tree);
6459 static void gen_namespace_die (tree, dw_die_ref);
6460 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6461 static dw_die_ref force_decl_die (tree);
6462 static dw_die_ref force_type_die (tree);
6463 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6464 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6465 static struct dwarf_file_data * lookup_filename (const char *);
6466 static void retry_incomplete_types (void);
6467 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6468 static void gen_generic_params_dies (tree);
6469 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6470 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6471 static void splice_child_die (dw_die_ref, dw_die_ref);
6472 static int file_info_cmp (const void *, const void *);
6473 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6474                                      const char *, const char *);
6475 static void output_loc_list (dw_loc_list_ref);
6476 static char *gen_internal_sym (const char *);
6477
6478 static void prune_unmark_dies (dw_die_ref);
6479 static void prune_unused_types_mark (dw_die_ref, int);
6480 static void prune_unused_types_walk (dw_die_ref);
6481 static void prune_unused_types_walk_attribs (dw_die_ref);
6482 static void prune_unused_types_prune (dw_die_ref);
6483 static void prune_unused_types (void);
6484 static int maybe_emit_file (struct dwarf_file_data *fd);
6485 static inline const char *AT_vms_delta1 (dw_attr_ref);
6486 static inline const char *AT_vms_delta2 (dw_attr_ref);
6487 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6488                                      const char *, const char *);
6489 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6490 static void gen_remaining_tmpl_value_param_die_attribute (void);
6491
6492 /* Section names used to hold DWARF debugging information.  */
6493 #ifndef DEBUG_INFO_SECTION
6494 #define DEBUG_INFO_SECTION      ".debug_info"
6495 #endif
6496 #ifndef DEBUG_ABBREV_SECTION
6497 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6498 #endif
6499 #ifndef DEBUG_ARANGES_SECTION
6500 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6501 #endif
6502 #ifndef DEBUG_MACINFO_SECTION
6503 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6504 #endif
6505 #ifndef DEBUG_LINE_SECTION
6506 #define DEBUG_LINE_SECTION      ".debug_line"
6507 #endif
6508 #ifndef DEBUG_LOC_SECTION
6509 #define DEBUG_LOC_SECTION       ".debug_loc"
6510 #endif
6511 #ifndef DEBUG_PUBNAMES_SECTION
6512 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6513 #endif
6514 #ifndef DEBUG_PUBTYPES_SECTION
6515 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6516 #endif
6517 #ifndef DEBUG_DCALL_SECTION
6518 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6519 #endif
6520 #ifndef DEBUG_VCALL_SECTION
6521 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6522 #endif
6523 #ifndef DEBUG_STR_SECTION
6524 #define DEBUG_STR_SECTION       ".debug_str"
6525 #endif
6526 #ifndef DEBUG_RANGES_SECTION
6527 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6528 #endif
6529
6530 /* Standard ELF section names for compiled code and data.  */
6531 #ifndef TEXT_SECTION_NAME
6532 #define TEXT_SECTION_NAME       ".text"
6533 #endif
6534
6535 /* Section flags for .debug_str section.  */
6536 #define DEBUG_STR_SECTION_FLAGS \
6537   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6538    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6539    : SECTION_DEBUG)
6540
6541 /* Labels we insert at beginning sections we can reference instead of
6542    the section names themselves.  */
6543
6544 #ifndef TEXT_SECTION_LABEL
6545 #define TEXT_SECTION_LABEL              "Ltext"
6546 #endif
6547 #ifndef COLD_TEXT_SECTION_LABEL
6548 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6549 #endif
6550 #ifndef DEBUG_LINE_SECTION_LABEL
6551 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6552 #endif
6553 #ifndef DEBUG_INFO_SECTION_LABEL
6554 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6555 #endif
6556 #ifndef DEBUG_ABBREV_SECTION_LABEL
6557 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6558 #endif
6559 #ifndef DEBUG_LOC_SECTION_LABEL
6560 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6561 #endif
6562 #ifndef DEBUG_RANGES_SECTION_LABEL
6563 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6564 #endif
6565 #ifndef DEBUG_MACINFO_SECTION_LABEL
6566 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6567 #endif
6568
6569
6570 /* Definitions of defaults for formats and names of various special
6571    (artificial) labels which may be generated within this file (when the -g
6572    options is used and DWARF2_DEBUGGING_INFO is in effect.
6573    If necessary, these may be overridden from within the tm.h file, but
6574    typically, overriding these defaults is unnecessary.  */
6575
6576 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6577 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6578 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6579 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6580 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6581 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6582 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6583 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6584 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6585 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6586
6587 #ifndef TEXT_END_LABEL
6588 #define TEXT_END_LABEL          "Letext"
6589 #endif
6590 #ifndef COLD_END_LABEL
6591 #define COLD_END_LABEL          "Letext_cold"
6592 #endif
6593 #ifndef BLOCK_BEGIN_LABEL
6594 #define BLOCK_BEGIN_LABEL       "LBB"
6595 #endif
6596 #ifndef BLOCK_END_LABEL
6597 #define BLOCK_END_LABEL         "LBE"
6598 #endif
6599 #ifndef LINE_CODE_LABEL
6600 #define LINE_CODE_LABEL         "LM"
6601 #endif
6602 #ifndef SEPARATE_LINE_CODE_LABEL
6603 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6604 #endif
6605
6606 \f
6607 /* Return the root of the DIE's built for the current compilation unit.  */
6608 static dw_die_ref
6609 comp_unit_die (void)
6610 {
6611   if (!single_comp_unit_die)
6612     single_comp_unit_die = gen_compile_unit_die (NULL);
6613   return single_comp_unit_die;
6614 }
6615
6616 /* We allow a language front-end to designate a function that is to be
6617    called to "demangle" any name before it is put into a DIE.  */
6618
6619 static const char *(*demangle_name_func) (const char *);
6620
6621 void
6622 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6623 {
6624   demangle_name_func = func;
6625 }
6626
6627 /* Test if rtl node points to a pseudo register.  */
6628
6629 static inline int
6630 is_pseudo_reg (const_rtx rtl)
6631 {
6632   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6633           || (GET_CODE (rtl) == SUBREG
6634               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6635 }
6636
6637 /* Return a reference to a type, with its const and volatile qualifiers
6638    removed.  */
6639
6640 static inline tree
6641 type_main_variant (tree type)
6642 {
6643   type = TYPE_MAIN_VARIANT (type);
6644
6645   /* ??? There really should be only one main variant among any group of
6646      variants of a given type (and all of the MAIN_VARIANT values for all
6647      members of the group should point to that one type) but sometimes the C
6648      front-end messes this up for array types, so we work around that bug
6649      here.  */
6650   if (TREE_CODE (type) == ARRAY_TYPE)
6651     while (type != TYPE_MAIN_VARIANT (type))
6652       type = TYPE_MAIN_VARIANT (type);
6653
6654   return type;
6655 }
6656
6657 /* Return nonzero if the given type node represents a tagged type.  */
6658
6659 static inline int
6660 is_tagged_type (const_tree type)
6661 {
6662   enum tree_code code = TREE_CODE (type);
6663
6664   return (code == RECORD_TYPE || code == UNION_TYPE
6665           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6666 }
6667
6668 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6669
6670 static void
6671 get_ref_die_offset_label (char *label, dw_die_ref ref)
6672 {
6673   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6674 }
6675
6676 /* Convert a DIE tag into its string name.  */
6677
6678 static const char *
6679 dwarf_tag_name (unsigned int tag)
6680 {
6681   switch (tag)
6682     {
6683     case DW_TAG_padding:
6684       return "DW_TAG_padding";
6685     case DW_TAG_array_type:
6686       return "DW_TAG_array_type";
6687     case DW_TAG_class_type:
6688       return "DW_TAG_class_type";
6689     case DW_TAG_entry_point:
6690       return "DW_TAG_entry_point";
6691     case DW_TAG_enumeration_type:
6692       return "DW_TAG_enumeration_type";
6693     case DW_TAG_formal_parameter:
6694       return "DW_TAG_formal_parameter";
6695     case DW_TAG_imported_declaration:
6696       return "DW_TAG_imported_declaration";
6697     case DW_TAG_label:
6698       return "DW_TAG_label";
6699     case DW_TAG_lexical_block:
6700       return "DW_TAG_lexical_block";
6701     case DW_TAG_member:
6702       return "DW_TAG_member";
6703     case DW_TAG_pointer_type:
6704       return "DW_TAG_pointer_type";
6705     case DW_TAG_reference_type:
6706       return "DW_TAG_reference_type";
6707     case DW_TAG_compile_unit:
6708       return "DW_TAG_compile_unit";
6709     case DW_TAG_string_type:
6710       return "DW_TAG_string_type";
6711     case DW_TAG_structure_type:
6712       return "DW_TAG_structure_type";
6713     case DW_TAG_subroutine_type:
6714       return "DW_TAG_subroutine_type";
6715     case DW_TAG_typedef:
6716       return "DW_TAG_typedef";
6717     case DW_TAG_union_type:
6718       return "DW_TAG_union_type";
6719     case DW_TAG_unspecified_parameters:
6720       return "DW_TAG_unspecified_parameters";
6721     case DW_TAG_variant:
6722       return "DW_TAG_variant";
6723     case DW_TAG_common_block:
6724       return "DW_TAG_common_block";
6725     case DW_TAG_common_inclusion:
6726       return "DW_TAG_common_inclusion";
6727     case DW_TAG_inheritance:
6728       return "DW_TAG_inheritance";
6729     case DW_TAG_inlined_subroutine:
6730       return "DW_TAG_inlined_subroutine";
6731     case DW_TAG_module:
6732       return "DW_TAG_module";
6733     case DW_TAG_ptr_to_member_type:
6734       return "DW_TAG_ptr_to_member_type";
6735     case DW_TAG_set_type:
6736       return "DW_TAG_set_type";
6737     case DW_TAG_subrange_type:
6738       return "DW_TAG_subrange_type";
6739     case DW_TAG_with_stmt:
6740       return "DW_TAG_with_stmt";
6741     case DW_TAG_access_declaration:
6742       return "DW_TAG_access_declaration";
6743     case DW_TAG_base_type:
6744       return "DW_TAG_base_type";
6745     case DW_TAG_catch_block:
6746       return "DW_TAG_catch_block";
6747     case DW_TAG_const_type:
6748       return "DW_TAG_const_type";
6749     case DW_TAG_constant:
6750       return "DW_TAG_constant";
6751     case DW_TAG_enumerator:
6752       return "DW_TAG_enumerator";
6753     case DW_TAG_file_type:
6754       return "DW_TAG_file_type";
6755     case DW_TAG_friend:
6756       return "DW_TAG_friend";
6757     case DW_TAG_namelist:
6758       return "DW_TAG_namelist";
6759     case DW_TAG_namelist_item:
6760       return "DW_TAG_namelist_item";
6761     case DW_TAG_packed_type:
6762       return "DW_TAG_packed_type";
6763     case DW_TAG_subprogram:
6764       return "DW_TAG_subprogram";
6765     case DW_TAG_template_type_param:
6766       return "DW_TAG_template_type_param";
6767     case DW_TAG_template_value_param:
6768       return "DW_TAG_template_value_param";
6769     case DW_TAG_thrown_type:
6770       return "DW_TAG_thrown_type";
6771     case DW_TAG_try_block:
6772       return "DW_TAG_try_block";
6773     case DW_TAG_variant_part:
6774       return "DW_TAG_variant_part";
6775     case DW_TAG_variable:
6776       return "DW_TAG_variable";
6777     case DW_TAG_volatile_type:
6778       return "DW_TAG_volatile_type";
6779     case DW_TAG_dwarf_procedure:
6780       return "DW_TAG_dwarf_procedure";
6781     case DW_TAG_restrict_type:
6782       return "DW_TAG_restrict_type";
6783     case DW_TAG_interface_type:
6784       return "DW_TAG_interface_type";
6785     case DW_TAG_namespace:
6786       return "DW_TAG_namespace";
6787     case DW_TAG_imported_module:
6788       return "DW_TAG_imported_module";
6789     case DW_TAG_unspecified_type:
6790       return "DW_TAG_unspecified_type";
6791     case DW_TAG_partial_unit:
6792       return "DW_TAG_partial_unit";
6793     case DW_TAG_imported_unit:
6794       return "DW_TAG_imported_unit";
6795     case DW_TAG_condition:
6796       return "DW_TAG_condition";
6797     case DW_TAG_shared_type:
6798       return "DW_TAG_shared_type";
6799     case DW_TAG_type_unit:
6800       return "DW_TAG_type_unit";
6801     case DW_TAG_rvalue_reference_type:
6802       return "DW_TAG_rvalue_reference_type";
6803     case DW_TAG_template_alias:
6804       return "DW_TAG_template_alias";
6805     case DW_TAG_GNU_template_parameter_pack:
6806       return "DW_TAG_GNU_template_parameter_pack";
6807     case DW_TAG_GNU_formal_parameter_pack:
6808       return "DW_TAG_GNU_formal_parameter_pack";
6809     case DW_TAG_MIPS_loop:
6810       return "DW_TAG_MIPS_loop";
6811     case DW_TAG_format_label:
6812       return "DW_TAG_format_label";
6813     case DW_TAG_function_template:
6814       return "DW_TAG_function_template";
6815     case DW_TAG_class_template:
6816       return "DW_TAG_class_template";
6817     case DW_TAG_GNU_BINCL:
6818       return "DW_TAG_GNU_BINCL";
6819     case DW_TAG_GNU_EINCL:
6820       return "DW_TAG_GNU_EINCL";
6821     case DW_TAG_GNU_template_template_param:
6822       return "DW_TAG_GNU_template_template_param";
6823     default:
6824       return "DW_TAG_<unknown>";
6825     }
6826 }
6827
6828 /* Convert a DWARF attribute code into its string name.  */
6829
6830 static const char *
6831 dwarf_attr_name (unsigned int attr)
6832 {
6833   switch (attr)
6834     {
6835     case DW_AT_sibling:
6836       return "DW_AT_sibling";
6837     case DW_AT_location:
6838       return "DW_AT_location";
6839     case DW_AT_name:
6840       return "DW_AT_name";
6841     case DW_AT_ordering:
6842       return "DW_AT_ordering";
6843     case DW_AT_subscr_data:
6844       return "DW_AT_subscr_data";
6845     case DW_AT_byte_size:
6846       return "DW_AT_byte_size";
6847     case DW_AT_bit_offset:
6848       return "DW_AT_bit_offset";
6849     case DW_AT_bit_size:
6850       return "DW_AT_bit_size";
6851     case DW_AT_element_list:
6852       return "DW_AT_element_list";
6853     case DW_AT_stmt_list:
6854       return "DW_AT_stmt_list";
6855     case DW_AT_low_pc:
6856       return "DW_AT_low_pc";
6857     case DW_AT_high_pc:
6858       return "DW_AT_high_pc";
6859     case DW_AT_language:
6860       return "DW_AT_language";
6861     case DW_AT_member:
6862       return "DW_AT_member";
6863     case DW_AT_discr:
6864       return "DW_AT_discr";
6865     case DW_AT_discr_value:
6866       return "DW_AT_discr_value";
6867     case DW_AT_visibility:
6868       return "DW_AT_visibility";
6869     case DW_AT_import:
6870       return "DW_AT_import";
6871     case DW_AT_string_length:
6872       return "DW_AT_string_length";
6873     case DW_AT_common_reference:
6874       return "DW_AT_common_reference";
6875     case DW_AT_comp_dir:
6876       return "DW_AT_comp_dir";
6877     case DW_AT_const_value:
6878       return "DW_AT_const_value";
6879     case DW_AT_containing_type:
6880       return "DW_AT_containing_type";
6881     case DW_AT_default_value:
6882       return "DW_AT_default_value";
6883     case DW_AT_inline:
6884       return "DW_AT_inline";
6885     case DW_AT_is_optional:
6886       return "DW_AT_is_optional";
6887     case DW_AT_lower_bound:
6888       return "DW_AT_lower_bound";
6889     case DW_AT_producer:
6890       return "DW_AT_producer";
6891     case DW_AT_prototyped:
6892       return "DW_AT_prototyped";
6893     case DW_AT_return_addr:
6894       return "DW_AT_return_addr";
6895     case DW_AT_start_scope:
6896       return "DW_AT_start_scope";
6897     case DW_AT_bit_stride:
6898       return "DW_AT_bit_stride";
6899     case DW_AT_upper_bound:
6900       return "DW_AT_upper_bound";
6901     case DW_AT_abstract_origin:
6902       return "DW_AT_abstract_origin";
6903     case DW_AT_accessibility:
6904       return "DW_AT_accessibility";
6905     case DW_AT_address_class:
6906       return "DW_AT_address_class";
6907     case DW_AT_artificial:
6908       return "DW_AT_artificial";
6909     case DW_AT_base_types:
6910       return "DW_AT_base_types";
6911     case DW_AT_calling_convention:
6912       return "DW_AT_calling_convention";
6913     case DW_AT_count:
6914       return "DW_AT_count";
6915     case DW_AT_data_member_location:
6916       return "DW_AT_data_member_location";
6917     case DW_AT_decl_column:
6918       return "DW_AT_decl_column";
6919     case DW_AT_decl_file:
6920       return "DW_AT_decl_file";
6921     case DW_AT_decl_line:
6922       return "DW_AT_decl_line";
6923     case DW_AT_declaration:
6924       return "DW_AT_declaration";
6925     case DW_AT_discr_list:
6926       return "DW_AT_discr_list";
6927     case DW_AT_encoding:
6928       return "DW_AT_encoding";
6929     case DW_AT_external:
6930       return "DW_AT_external";
6931     case DW_AT_explicit:
6932       return "DW_AT_explicit";
6933     case DW_AT_frame_base:
6934       return "DW_AT_frame_base";
6935     case DW_AT_friend:
6936       return "DW_AT_friend";
6937     case DW_AT_identifier_case:
6938       return "DW_AT_identifier_case";
6939     case DW_AT_macro_info:
6940       return "DW_AT_macro_info";
6941     case DW_AT_namelist_items:
6942       return "DW_AT_namelist_items";
6943     case DW_AT_priority:
6944       return "DW_AT_priority";
6945     case DW_AT_segment:
6946       return "DW_AT_segment";
6947     case DW_AT_specification:
6948       return "DW_AT_specification";
6949     case DW_AT_static_link:
6950       return "DW_AT_static_link";
6951     case DW_AT_type:
6952       return "DW_AT_type";
6953     case DW_AT_use_location:
6954       return "DW_AT_use_location";
6955     case DW_AT_variable_parameter:
6956       return "DW_AT_variable_parameter";
6957     case DW_AT_virtuality:
6958       return "DW_AT_virtuality";
6959     case DW_AT_vtable_elem_location:
6960       return "DW_AT_vtable_elem_location";
6961
6962     case DW_AT_allocated:
6963       return "DW_AT_allocated";
6964     case DW_AT_associated:
6965       return "DW_AT_associated";
6966     case DW_AT_data_location:
6967       return "DW_AT_data_location";
6968     case DW_AT_byte_stride:
6969       return "DW_AT_byte_stride";
6970     case DW_AT_entry_pc:
6971       return "DW_AT_entry_pc";
6972     case DW_AT_use_UTF8:
6973       return "DW_AT_use_UTF8";
6974     case DW_AT_extension:
6975       return "DW_AT_extension";
6976     case DW_AT_ranges:
6977       return "DW_AT_ranges";
6978     case DW_AT_trampoline:
6979       return "DW_AT_trampoline";
6980     case DW_AT_call_column:
6981       return "DW_AT_call_column";
6982     case DW_AT_call_file:
6983       return "DW_AT_call_file";
6984     case DW_AT_call_line:
6985       return "DW_AT_call_line";
6986     case DW_AT_object_pointer:
6987       return "DW_AT_object_pointer";
6988
6989     case DW_AT_signature:
6990       return "DW_AT_signature";
6991     case DW_AT_main_subprogram:
6992       return "DW_AT_main_subprogram";
6993     case DW_AT_data_bit_offset:
6994       return "DW_AT_data_bit_offset";
6995     case DW_AT_const_expr:
6996       return "DW_AT_const_expr";
6997     case DW_AT_enum_class:
6998       return "DW_AT_enum_class";
6999     case DW_AT_linkage_name:
7000       return "DW_AT_linkage_name";
7001
7002     case DW_AT_MIPS_fde:
7003       return "DW_AT_MIPS_fde";
7004     case DW_AT_MIPS_loop_begin:
7005       return "DW_AT_MIPS_loop_begin";
7006     case DW_AT_MIPS_tail_loop_begin:
7007       return "DW_AT_MIPS_tail_loop_begin";
7008     case DW_AT_MIPS_epilog_begin:
7009       return "DW_AT_MIPS_epilog_begin";
7010 #if VMS_DEBUGGING_INFO
7011     case DW_AT_HP_prologue:
7012       return "DW_AT_HP_prologue";
7013 #else
7014     case DW_AT_MIPS_loop_unroll_factor:
7015       return "DW_AT_MIPS_loop_unroll_factor";
7016 #endif
7017     case DW_AT_MIPS_software_pipeline_depth:
7018       return "DW_AT_MIPS_software_pipeline_depth";
7019     case DW_AT_MIPS_linkage_name:
7020       return "DW_AT_MIPS_linkage_name";
7021 #if VMS_DEBUGGING_INFO
7022     case DW_AT_HP_epilogue:
7023       return "DW_AT_HP_epilogue";
7024 #else
7025     case DW_AT_MIPS_stride:
7026       return "DW_AT_MIPS_stride";
7027 #endif
7028     case DW_AT_MIPS_abstract_name:
7029       return "DW_AT_MIPS_abstract_name";
7030     case DW_AT_MIPS_clone_origin:
7031       return "DW_AT_MIPS_clone_origin";
7032     case DW_AT_MIPS_has_inlines:
7033       return "DW_AT_MIPS_has_inlines";
7034
7035     case DW_AT_sf_names:
7036       return "DW_AT_sf_names";
7037     case DW_AT_src_info:
7038       return "DW_AT_src_info";
7039     case DW_AT_mac_info:
7040       return "DW_AT_mac_info";
7041     case DW_AT_src_coords:
7042       return "DW_AT_src_coords";
7043     case DW_AT_body_begin:
7044       return "DW_AT_body_begin";
7045     case DW_AT_body_end:
7046       return "DW_AT_body_end";
7047     case DW_AT_GNU_vector:
7048       return "DW_AT_GNU_vector";
7049     case DW_AT_GNU_guarded_by:
7050       return "DW_AT_GNU_guarded_by";
7051     case DW_AT_GNU_pt_guarded_by:
7052       return "DW_AT_GNU_pt_guarded_by";
7053     case DW_AT_GNU_guarded:
7054       return "DW_AT_GNU_guarded";
7055     case DW_AT_GNU_pt_guarded:
7056       return "DW_AT_GNU_pt_guarded";
7057     case DW_AT_GNU_locks_excluded:
7058       return "DW_AT_GNU_locks_excluded";
7059     case DW_AT_GNU_exclusive_locks_required:
7060       return "DW_AT_GNU_exclusive_locks_required";
7061     case DW_AT_GNU_shared_locks_required:
7062       return "DW_AT_GNU_shared_locks_required";
7063     case DW_AT_GNU_odr_signature:
7064       return "DW_AT_GNU_odr_signature";
7065     case DW_AT_GNU_template_name:
7066       return "DW_AT_GNU_template_name";
7067
7068     case DW_AT_VMS_rtnbeg_pd_address:
7069       return "DW_AT_VMS_rtnbeg_pd_address";
7070
7071     default:
7072       return "DW_AT_<unknown>";
7073     }
7074 }
7075
7076 /* Convert a DWARF value form code into its string name.  */
7077
7078 static const char *
7079 dwarf_form_name (unsigned int form)
7080 {
7081   switch (form)
7082     {
7083     case DW_FORM_addr:
7084       return "DW_FORM_addr";
7085     case DW_FORM_block2:
7086       return "DW_FORM_block2";
7087     case DW_FORM_block4:
7088       return "DW_FORM_block4";
7089     case DW_FORM_data2:
7090       return "DW_FORM_data2";
7091     case DW_FORM_data4:
7092       return "DW_FORM_data4";
7093     case DW_FORM_data8:
7094       return "DW_FORM_data8";
7095     case DW_FORM_string:
7096       return "DW_FORM_string";
7097     case DW_FORM_block:
7098       return "DW_FORM_block";
7099     case DW_FORM_block1:
7100       return "DW_FORM_block1";
7101     case DW_FORM_data1:
7102       return "DW_FORM_data1";
7103     case DW_FORM_flag:
7104       return "DW_FORM_flag";
7105     case DW_FORM_sdata:
7106       return "DW_FORM_sdata";
7107     case DW_FORM_strp:
7108       return "DW_FORM_strp";
7109     case DW_FORM_udata:
7110       return "DW_FORM_udata";
7111     case DW_FORM_ref_addr:
7112       return "DW_FORM_ref_addr";
7113     case DW_FORM_ref1:
7114       return "DW_FORM_ref1";
7115     case DW_FORM_ref2:
7116       return "DW_FORM_ref2";
7117     case DW_FORM_ref4:
7118       return "DW_FORM_ref4";
7119     case DW_FORM_ref8:
7120       return "DW_FORM_ref8";
7121     case DW_FORM_ref_udata:
7122       return "DW_FORM_ref_udata";
7123     case DW_FORM_indirect:
7124       return "DW_FORM_indirect";
7125     case DW_FORM_sec_offset:
7126       return "DW_FORM_sec_offset";
7127     case DW_FORM_exprloc:
7128       return "DW_FORM_exprloc";
7129     case DW_FORM_flag_present:
7130       return "DW_FORM_flag_present";
7131     case DW_FORM_ref_sig8:
7132       return "DW_FORM_ref_sig8";
7133     default:
7134       return "DW_FORM_<unknown>";
7135     }
7136 }
7137 \f
7138 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7139    instance of an inlined instance of a decl which is local to an inline
7140    function, so we have to trace all of the way back through the origin chain
7141    to find out what sort of node actually served as the original seed for the
7142    given block.  */
7143
7144 static tree
7145 decl_ultimate_origin (const_tree decl)
7146 {
7147   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7148     return NULL_TREE;
7149
7150   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7151      nodes in the function to point to themselves; ignore that if
7152      we're trying to output the abstract instance of this function.  */
7153   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7154     return NULL_TREE;
7155
7156   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7157      most distant ancestor, this should never happen.  */
7158   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7159
7160   return DECL_ABSTRACT_ORIGIN (decl);
7161 }
7162
7163 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7164    of a virtual function may refer to a base class, so we check the 'this'
7165    parameter.  */
7166
7167 static tree
7168 decl_class_context (tree decl)
7169 {
7170   tree context = NULL_TREE;
7171
7172   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7173     context = DECL_CONTEXT (decl);
7174   else
7175     context = TYPE_MAIN_VARIANT
7176       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7177
7178   if (context && !TYPE_P (context))
7179     context = NULL_TREE;
7180
7181   return context;
7182 }
7183 \f
7184 /* Add an attribute/value pair to a DIE.  */
7185
7186 static inline void
7187 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7188 {
7189   /* Maybe this should be an assert?  */
7190   if (die == NULL)
7191     return;
7192
7193   if (die->die_attr == NULL)
7194     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7195   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7196 }
7197
7198 static inline enum dw_val_class
7199 AT_class (dw_attr_ref a)
7200 {
7201   return a->dw_attr_val.val_class;
7202 }
7203
7204 /* Add a flag value attribute to a DIE.  */
7205
7206 static inline void
7207 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7208 {
7209   dw_attr_node attr;
7210
7211   attr.dw_attr = attr_kind;
7212   attr.dw_attr_val.val_class = dw_val_class_flag;
7213   attr.dw_attr_val.v.val_flag = flag;
7214   add_dwarf_attr (die, &attr);
7215 }
7216
7217 static inline unsigned
7218 AT_flag (dw_attr_ref a)
7219 {
7220   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7221   return a->dw_attr_val.v.val_flag;
7222 }
7223
7224 /* Add a signed integer attribute value to a DIE.  */
7225
7226 static inline void
7227 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7228 {
7229   dw_attr_node attr;
7230
7231   attr.dw_attr = attr_kind;
7232   attr.dw_attr_val.val_class = dw_val_class_const;
7233   attr.dw_attr_val.v.val_int = int_val;
7234   add_dwarf_attr (die, &attr);
7235 }
7236
7237 static inline HOST_WIDE_INT
7238 AT_int (dw_attr_ref a)
7239 {
7240   gcc_assert (a && AT_class (a) == dw_val_class_const);
7241   return a->dw_attr_val.v.val_int;
7242 }
7243
7244 /* Add an unsigned integer attribute value to a DIE.  */
7245
7246 static inline void
7247 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7248                  unsigned HOST_WIDE_INT unsigned_val)
7249 {
7250   dw_attr_node attr;
7251
7252   attr.dw_attr = attr_kind;
7253   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7254   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7255   add_dwarf_attr (die, &attr);
7256 }
7257
7258 static inline unsigned HOST_WIDE_INT
7259 AT_unsigned (dw_attr_ref a)
7260 {
7261   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7262   return a->dw_attr_val.v.val_unsigned;
7263 }
7264
7265 /* Add an unsigned double integer attribute value to a DIE.  */
7266
7267 static inline void
7268 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7269                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7270 {
7271   dw_attr_node attr;
7272
7273   attr.dw_attr = attr_kind;
7274   attr.dw_attr_val.val_class = dw_val_class_const_double;
7275   attr.dw_attr_val.v.val_double.high = high;
7276   attr.dw_attr_val.v.val_double.low = low;
7277   add_dwarf_attr (die, &attr);
7278 }
7279
7280 /* Add a floating point attribute value to a DIE and return it.  */
7281
7282 static inline void
7283 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7284             unsigned int length, unsigned int elt_size, unsigned char *array)
7285 {
7286   dw_attr_node attr;
7287
7288   attr.dw_attr = attr_kind;
7289   attr.dw_attr_val.val_class = dw_val_class_vec;
7290   attr.dw_attr_val.v.val_vec.length = length;
7291   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7292   attr.dw_attr_val.v.val_vec.array = array;
7293   add_dwarf_attr (die, &attr);
7294 }
7295
7296 /* Add an 8-byte data attribute value to a DIE.  */
7297
7298 static inline void
7299 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7300               unsigned char data8[8])
7301 {
7302   dw_attr_node attr;
7303
7304   attr.dw_attr = attr_kind;
7305   attr.dw_attr_val.val_class = dw_val_class_data8;
7306   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7307   add_dwarf_attr (die, &attr);
7308 }
7309
7310 /* Hash and equality functions for debug_str_hash.  */
7311
7312 static hashval_t
7313 debug_str_do_hash (const void *x)
7314 {
7315   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7316 }
7317
7318 static int
7319 debug_str_eq (const void *x1, const void *x2)
7320 {
7321   return strcmp ((((const struct indirect_string_node *)x1)->str),
7322                  (const char *)x2) == 0;
7323 }
7324
7325 /* Add STR to the indirect string hash table.  */
7326
7327 static struct indirect_string_node *
7328 find_AT_string (const char *str)
7329 {
7330   struct indirect_string_node *node;
7331   void **slot;
7332
7333   if (! debug_str_hash)
7334     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7335                                       debug_str_eq, NULL);
7336
7337   slot = htab_find_slot_with_hash (debug_str_hash, str,
7338                                    htab_hash_string (str), INSERT);
7339   if (*slot == NULL)
7340     {
7341       node = ggc_alloc_cleared_indirect_string_node ();
7342       node->str = ggc_strdup (str);
7343       *slot = node;
7344     }
7345   else
7346     node = (struct indirect_string_node *) *slot;
7347
7348   node->refcount++;
7349   return node;
7350 }
7351
7352 /* Add a string attribute value to a DIE.  */
7353
7354 static inline void
7355 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7356 {
7357   dw_attr_node attr;
7358   struct indirect_string_node *node;
7359
7360   node = find_AT_string (str);
7361
7362   attr.dw_attr = attr_kind;
7363   attr.dw_attr_val.val_class = dw_val_class_str;
7364   attr.dw_attr_val.v.val_str = node;
7365   add_dwarf_attr (die, &attr);
7366 }
7367
7368 /* Create a label for an indirect string node, ensuring it is going to
7369    be output, unless its reference count goes down to zero.  */
7370
7371 static inline void
7372 gen_label_for_indirect_string (struct indirect_string_node *node)
7373 {
7374   char label[32];
7375
7376   if (node->label)
7377     return;
7378
7379   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7380   ++dw2_string_counter;
7381   node->label = xstrdup (label);
7382 }
7383
7384 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7385    debug string STR.  */
7386
7387 static inline rtx
7388 get_debug_string_label (const char *str)
7389 {
7390   struct indirect_string_node *node = find_AT_string (str);
7391
7392   debug_str_hash_forced = true;
7393
7394   gen_label_for_indirect_string (node);
7395
7396   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7397 }
7398
7399 static inline const char *
7400 AT_string (dw_attr_ref a)
7401 {
7402   gcc_assert (a && AT_class (a) == dw_val_class_str);
7403   return a->dw_attr_val.v.val_str->str;
7404 }
7405
7406 /* Find out whether a string should be output inline in DIE
7407    or out-of-line in .debug_str section.  */
7408
7409 static enum dwarf_form
7410 AT_string_form (dw_attr_ref a)
7411 {
7412   struct indirect_string_node *node;
7413   unsigned int len;
7414
7415   gcc_assert (a && AT_class (a) == dw_val_class_str);
7416
7417   node = a->dw_attr_val.v.val_str;
7418   if (node->form)
7419     return node->form;
7420
7421   len = strlen (node->str) + 1;
7422
7423   /* If the string is shorter or equal to the size of the reference, it is
7424      always better to put it inline.  */
7425   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7426     return node->form = DW_FORM_string;
7427
7428   /* If we cannot expect the linker to merge strings in .debug_str
7429      section, only put it into .debug_str if it is worth even in this
7430      single module.  */
7431   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7432       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7433       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7434     return node->form = DW_FORM_string;
7435
7436   gen_label_for_indirect_string (node);
7437
7438   return node->form = DW_FORM_strp;
7439 }
7440
7441 /* Add a DIE reference attribute value to a DIE.  */
7442
7443 static inline void
7444 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7445 {
7446   dw_attr_node attr;
7447
7448 #ifdef ENABLE_CHECKING
7449   gcc_assert (targ_die != NULL);
7450 #else
7451   /* With LTO we can end up trying to reference something we didn't create
7452      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7453   if (targ_die == NULL)
7454     return;
7455 #endif
7456
7457   attr.dw_attr = attr_kind;
7458   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7459   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7460   attr.dw_attr_val.v.val_die_ref.external = 0;
7461   add_dwarf_attr (die, &attr);
7462 }
7463
7464 /* Add an AT_specification attribute to a DIE, and also make the back
7465    pointer from the specification to the definition.  */
7466
7467 static inline void
7468 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7469 {
7470   add_AT_die_ref (die, DW_AT_specification, targ_die);
7471   gcc_assert (!targ_die->die_definition);
7472   targ_die->die_definition = die;
7473 }
7474
7475 static inline dw_die_ref
7476 AT_ref (dw_attr_ref a)
7477 {
7478   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7479   return a->dw_attr_val.v.val_die_ref.die;
7480 }
7481
7482 static inline int
7483 AT_ref_external (dw_attr_ref a)
7484 {
7485   if (a && AT_class (a) == dw_val_class_die_ref)
7486     return a->dw_attr_val.v.val_die_ref.external;
7487
7488   return 0;
7489 }
7490
7491 static inline void
7492 set_AT_ref_external (dw_attr_ref a, int i)
7493 {
7494   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7495   a->dw_attr_val.v.val_die_ref.external = i;
7496 }
7497
7498 /* Add an FDE reference attribute value to a DIE.  */
7499
7500 static inline void
7501 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7502 {
7503   dw_attr_node attr;
7504
7505   attr.dw_attr = attr_kind;
7506   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7507   attr.dw_attr_val.v.val_fde_index = targ_fde;
7508   add_dwarf_attr (die, &attr);
7509 }
7510
7511 /* Add a location description attribute value to a DIE.  */
7512
7513 static inline void
7514 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7515 {
7516   dw_attr_node attr;
7517
7518   attr.dw_attr = attr_kind;
7519   attr.dw_attr_val.val_class = dw_val_class_loc;
7520   attr.dw_attr_val.v.val_loc = loc;
7521   add_dwarf_attr (die, &attr);
7522 }
7523
7524 static inline dw_loc_descr_ref
7525 AT_loc (dw_attr_ref a)
7526 {
7527   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7528   return a->dw_attr_val.v.val_loc;
7529 }
7530
7531 static inline void
7532 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7533 {
7534   dw_attr_node attr;
7535
7536   attr.dw_attr = attr_kind;
7537   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7538   attr.dw_attr_val.v.val_loc_list = loc_list;
7539   add_dwarf_attr (die, &attr);
7540   have_location_lists = true;
7541 }
7542
7543 static inline dw_loc_list_ref
7544 AT_loc_list (dw_attr_ref a)
7545 {
7546   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7547   return a->dw_attr_val.v.val_loc_list;
7548 }
7549
7550 static inline dw_loc_list_ref *
7551 AT_loc_list_ptr (dw_attr_ref a)
7552 {
7553   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7554   return &a->dw_attr_val.v.val_loc_list;
7555 }
7556
7557 /* Add an address constant attribute value to a DIE.  */
7558
7559 static inline void
7560 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7561 {
7562   dw_attr_node attr;
7563
7564   attr.dw_attr = attr_kind;
7565   attr.dw_attr_val.val_class = dw_val_class_addr;
7566   attr.dw_attr_val.v.val_addr = addr;
7567   add_dwarf_attr (die, &attr);
7568 }
7569
7570 /* Get the RTX from to an address DIE attribute.  */
7571
7572 static inline rtx
7573 AT_addr (dw_attr_ref a)
7574 {
7575   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7576   return a->dw_attr_val.v.val_addr;
7577 }
7578
7579 /* Add a file attribute value to a DIE.  */
7580
7581 static inline void
7582 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7583              struct dwarf_file_data *fd)
7584 {
7585   dw_attr_node attr;
7586
7587   attr.dw_attr = attr_kind;
7588   attr.dw_attr_val.val_class = dw_val_class_file;
7589   attr.dw_attr_val.v.val_file = fd;
7590   add_dwarf_attr (die, &attr);
7591 }
7592
7593 /* Get the dwarf_file_data from a file DIE attribute.  */
7594
7595 static inline struct dwarf_file_data *
7596 AT_file (dw_attr_ref a)
7597 {
7598   gcc_assert (a && AT_class (a) == dw_val_class_file);
7599   return a->dw_attr_val.v.val_file;
7600 }
7601
7602 /* Add a vms delta attribute value to a DIE.  */
7603
7604 static inline void
7605 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7606                   const char *lbl1, const char *lbl2)
7607 {
7608   dw_attr_node attr;
7609
7610   attr.dw_attr = attr_kind;
7611   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7612   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7613   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7614   add_dwarf_attr (die, &attr);
7615 }
7616
7617 /* Add a label identifier attribute value to a DIE.  */
7618
7619 static inline void
7620 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7621 {
7622   dw_attr_node attr;
7623
7624   attr.dw_attr = attr_kind;
7625   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7626   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7627   add_dwarf_attr (die, &attr);
7628 }
7629
7630 /* Add a section offset attribute value to a DIE, an offset into the
7631    debug_line section.  */
7632
7633 static inline void
7634 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7635                 const char *label)
7636 {
7637   dw_attr_node attr;
7638
7639   attr.dw_attr = attr_kind;
7640   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7641   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7642   add_dwarf_attr (die, &attr);
7643 }
7644
7645 /* Add a section offset attribute value to a DIE, an offset into the
7646    debug_macinfo section.  */
7647
7648 static inline void
7649 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7650                const char *label)
7651 {
7652   dw_attr_node attr;
7653
7654   attr.dw_attr = attr_kind;
7655   attr.dw_attr_val.val_class = dw_val_class_macptr;
7656   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7657   add_dwarf_attr (die, &attr);
7658 }
7659
7660 /* Add an offset attribute value to a DIE.  */
7661
7662 static inline void
7663 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7664                unsigned HOST_WIDE_INT offset)
7665 {
7666   dw_attr_node attr;
7667
7668   attr.dw_attr = attr_kind;
7669   attr.dw_attr_val.val_class = dw_val_class_offset;
7670   attr.dw_attr_val.v.val_offset = offset;
7671   add_dwarf_attr (die, &attr);
7672 }
7673
7674 /* Add an range_list attribute value to a DIE.  */
7675
7676 static void
7677 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7678                    long unsigned int offset)
7679 {
7680   dw_attr_node attr;
7681
7682   attr.dw_attr = attr_kind;
7683   attr.dw_attr_val.val_class = dw_val_class_range_list;
7684   attr.dw_attr_val.v.val_offset = offset;
7685   add_dwarf_attr (die, &attr);
7686 }
7687
7688 /* Return the start label of a delta attribute.  */
7689
7690 static inline const char *
7691 AT_vms_delta1 (dw_attr_ref a)
7692 {
7693   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7694   return a->dw_attr_val.v.val_vms_delta.lbl1;
7695 }
7696
7697 /* Return the end label of a delta attribute.  */
7698
7699 static inline const char *
7700 AT_vms_delta2 (dw_attr_ref a)
7701 {
7702   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7703   return a->dw_attr_val.v.val_vms_delta.lbl2;
7704 }
7705
7706 static inline const char *
7707 AT_lbl (dw_attr_ref a)
7708 {
7709   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7710                     || AT_class (a) == dw_val_class_lineptr
7711                     || AT_class (a) == dw_val_class_macptr));
7712   return a->dw_attr_val.v.val_lbl_id;
7713 }
7714
7715 /* Get the attribute of type attr_kind.  */
7716
7717 static dw_attr_ref
7718 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7719 {
7720   dw_attr_ref a;
7721   unsigned ix;
7722   dw_die_ref spec = NULL;
7723
7724   if (! die)
7725     return NULL;
7726
7727   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7728     if (a->dw_attr == attr_kind)
7729       return a;
7730     else if (a->dw_attr == DW_AT_specification
7731              || a->dw_attr == DW_AT_abstract_origin)
7732       spec = AT_ref (a);
7733
7734   if (spec)
7735     return get_AT (spec, attr_kind);
7736
7737   return NULL;
7738 }
7739
7740 /* Return the "low pc" attribute value, typically associated with a subprogram
7741    DIE.  Return null if the "low pc" attribute is either not present, or if it
7742    cannot be represented as an assembler label identifier.  */
7743
7744 static inline const char *
7745 get_AT_low_pc (dw_die_ref die)
7746 {
7747   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7748
7749   return a ? AT_lbl (a) : NULL;
7750 }
7751
7752 /* Return the "high pc" attribute value, typically associated with a subprogram
7753    DIE.  Return null if the "high pc" attribute is either not present, or if it
7754    cannot be represented as an assembler label identifier.  */
7755
7756 static inline const char *
7757 get_AT_hi_pc (dw_die_ref die)
7758 {
7759   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7760
7761   return a ? AT_lbl (a) : NULL;
7762 }
7763
7764 /* Return the value of the string attribute designated by ATTR_KIND, or
7765    NULL if it is not present.  */
7766
7767 static inline const char *
7768 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7769 {
7770   dw_attr_ref a = get_AT (die, attr_kind);
7771
7772   return a ? AT_string (a) : NULL;
7773 }
7774
7775 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7776    if it is not present.  */
7777
7778 static inline int
7779 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7780 {
7781   dw_attr_ref a = get_AT (die, attr_kind);
7782
7783   return a ? AT_flag (a) : 0;
7784 }
7785
7786 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7787    if it is not present.  */
7788
7789 static inline unsigned
7790 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7791 {
7792   dw_attr_ref a = get_AT (die, attr_kind);
7793
7794   return a ? AT_unsigned (a) : 0;
7795 }
7796
7797 static inline dw_die_ref
7798 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7799 {
7800   dw_attr_ref a = get_AT (die, attr_kind);
7801
7802   return a ? AT_ref (a) : NULL;
7803 }
7804
7805 static inline struct dwarf_file_data *
7806 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7807 {
7808   dw_attr_ref a = get_AT (die, attr_kind);
7809
7810   return a ? AT_file (a) : NULL;
7811 }
7812
7813 /* Return TRUE if the language is C++.  */
7814
7815 static inline bool
7816 is_cxx (void)
7817 {
7818   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7819
7820   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7821 }
7822
7823 /* Return TRUE if the language is Fortran.  */
7824
7825 static inline bool
7826 is_fortran (void)
7827 {
7828   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7829
7830   return (lang == DW_LANG_Fortran77
7831           || lang == DW_LANG_Fortran90
7832           || lang == DW_LANG_Fortran95);
7833 }
7834
7835 /* Return TRUE if the language is Ada.  */
7836
7837 static inline bool
7838 is_ada (void)
7839 {
7840   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7841
7842   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7843 }
7844
7845 /* Remove the specified attribute if present.  */
7846
7847 static void
7848 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7849 {
7850   dw_attr_ref a;
7851   unsigned ix;
7852
7853   if (! die)
7854     return;
7855
7856   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7857     if (a->dw_attr == attr_kind)
7858       {
7859         if (AT_class (a) == dw_val_class_str)
7860           if (a->dw_attr_val.v.val_str->refcount)
7861             a->dw_attr_val.v.val_str->refcount--;
7862
7863         /* VEC_ordered_remove should help reduce the number of abbrevs
7864            that are needed.  */
7865         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7866         return;
7867       }
7868 }
7869
7870 /* Remove CHILD from its parent.  PREV must have the property that
7871    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7872
7873 static void
7874 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7875 {
7876   gcc_assert (child->die_parent == prev->die_parent);
7877   gcc_assert (prev->die_sib == child);
7878   if (prev == child)
7879     {
7880       gcc_assert (child->die_parent->die_child == child);
7881       prev = NULL;
7882     }
7883   else
7884     prev->die_sib = child->die_sib;
7885   if (child->die_parent->die_child == child)
7886     child->die_parent->die_child = prev;
7887 }
7888
7889 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7890    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7891
7892 static void
7893 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7894 {
7895   dw_die_ref parent = old_child->die_parent;
7896
7897   gcc_assert (parent == prev->die_parent);
7898   gcc_assert (prev->die_sib == old_child);
7899
7900   new_child->die_parent = parent;
7901   if (prev == old_child)
7902     {
7903       gcc_assert (parent->die_child == old_child);
7904       new_child->die_sib = new_child;
7905     }
7906   else
7907     {
7908       prev->die_sib = new_child;
7909       new_child->die_sib = old_child->die_sib;
7910     }
7911   if (old_child->die_parent->die_child == old_child)
7912     old_child->die_parent->die_child = new_child;
7913 }
7914
7915 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7916
7917 static void
7918 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7919 {
7920   dw_die_ref c;
7921   new_parent->die_child = old_parent->die_child;
7922   old_parent->die_child = NULL;
7923   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7924 }
7925
7926 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7927    matches TAG.  */
7928
7929 static void
7930 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7931 {
7932   dw_die_ref c;
7933
7934   c = die->die_child;
7935   if (c) do {
7936     dw_die_ref prev = c;
7937     c = c->die_sib;
7938     while (c->die_tag == tag)
7939       {
7940         remove_child_with_prev (c, prev);
7941         /* Might have removed every child.  */
7942         if (c == c->die_sib)
7943           return;
7944         c = c->die_sib;
7945       }
7946   } while (c != die->die_child);
7947 }
7948
7949 /* Add a CHILD_DIE as the last child of DIE.  */
7950
7951 static void
7952 add_child_die (dw_die_ref die, dw_die_ref child_die)
7953 {
7954   /* FIXME this should probably be an assert.  */
7955   if (! die || ! child_die)
7956     return;
7957   gcc_assert (die != child_die);
7958
7959   child_die->die_parent = die;
7960   if (die->die_child)
7961     {
7962       child_die->die_sib = die->die_child->die_sib;
7963       die->die_child->die_sib = child_die;
7964     }
7965   else
7966     child_die->die_sib = child_die;
7967   die->die_child = child_die;
7968 }
7969
7970 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7971    is the specification, to the end of PARENT's list of children.
7972    This is done by removing and re-adding it.  */
7973
7974 static void
7975 splice_child_die (dw_die_ref parent, dw_die_ref child)
7976 {
7977   dw_die_ref p;
7978
7979   /* We want the declaration DIE from inside the class, not the
7980      specification DIE at toplevel.  */
7981   if (child->die_parent != parent)
7982     {
7983       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7984
7985       if (tmp)
7986         child = tmp;
7987     }
7988
7989   gcc_assert (child->die_parent == parent
7990               || (child->die_parent
7991                   == get_AT_ref (parent, DW_AT_specification)));
7992
7993   for (p = child->die_parent->die_child; ; p = p->die_sib)
7994     if (p->die_sib == child)
7995       {
7996         remove_child_with_prev (child, p);
7997         break;
7998       }
7999
8000   add_child_die (parent, child);
8001 }
8002
8003 /* Return a pointer to a newly created DIE node.  */
8004
8005 static inline dw_die_ref
8006 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8007 {
8008   dw_die_ref die = ggc_alloc_cleared_die_node ();
8009
8010   die->die_tag = tag_value;
8011
8012   if (parent_die != NULL)
8013     add_child_die (parent_die, die);
8014   else
8015     {
8016       limbo_die_node *limbo_node;
8017
8018       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8019       limbo_node->die = die;
8020       limbo_node->created_for = t;
8021       limbo_node->next = limbo_die_list;
8022       limbo_die_list = limbo_node;
8023     }
8024
8025   return die;
8026 }
8027
8028 /* Return the DIE associated with the given type specifier.  */
8029
8030 static inline dw_die_ref
8031 lookup_type_die (tree type)
8032 {
8033   return TYPE_SYMTAB_DIE (type);
8034 }
8035
8036 /* Equate a DIE to a given type specifier.  */
8037
8038 static inline void
8039 equate_type_number_to_die (tree type, dw_die_ref type_die)
8040 {
8041   TYPE_SYMTAB_DIE (type) = type_die;
8042 }
8043
8044 /* Returns a hash value for X (which really is a die_struct).  */
8045
8046 static hashval_t
8047 decl_die_table_hash (const void *x)
8048 {
8049   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8050 }
8051
8052 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8053
8054 static int
8055 decl_die_table_eq (const void *x, const void *y)
8056 {
8057   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8058 }
8059
8060 /* Return the DIE associated with a given declaration.  */
8061
8062 static inline dw_die_ref
8063 lookup_decl_die (tree decl)
8064 {
8065   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8066 }
8067
8068 /* Returns a hash value for X (which really is a var_loc_list).  */
8069
8070 static hashval_t
8071 decl_loc_table_hash (const void *x)
8072 {
8073   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8074 }
8075
8076 /* Return nonzero if decl_id of var_loc_list X is the same as
8077    UID of decl *Y.  */
8078
8079 static int
8080 decl_loc_table_eq (const void *x, const void *y)
8081 {
8082   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8083 }
8084
8085 /* Return the var_loc list associated with a given declaration.  */
8086
8087 static inline var_loc_list *
8088 lookup_decl_loc (const_tree decl)
8089 {
8090   if (!decl_loc_table)
8091     return NULL;
8092   return (var_loc_list *)
8093     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8094 }
8095
8096 /* Equate a DIE to a particular declaration.  */
8097
8098 static void
8099 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8100 {
8101   unsigned int decl_id = DECL_UID (decl);
8102   void **slot;
8103
8104   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8105   *slot = decl_die;
8106   decl_die->decl_id = decl_id;
8107 }
8108
8109 /* Return how many bits covers PIECE EXPR_LIST.  */
8110
8111 static int
8112 decl_piece_bitsize (rtx piece)
8113 {
8114   int ret = (int) GET_MODE (piece);
8115   if (ret)
8116     return ret;
8117   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8118               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8119   return INTVAL (XEXP (XEXP (piece, 0), 0));
8120 }
8121
8122 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8123
8124 static rtx *
8125 decl_piece_varloc_ptr (rtx piece)
8126 {
8127   if ((int) GET_MODE (piece))
8128     return &XEXP (piece, 0);
8129   else
8130     return &XEXP (XEXP (piece, 0), 1);
8131 }
8132
8133 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8134    Next is the chain of following piece nodes.  */
8135
8136 static rtx
8137 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8138 {
8139   if (bitsize <= (int) MAX_MACHINE_MODE)
8140     return alloc_EXPR_LIST (bitsize, loc_note, next);
8141   else
8142     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8143                                                GEN_INT (bitsize),
8144                                                loc_note), next);
8145 }
8146
8147 /* Return rtx that should be stored into loc field for
8148    LOC_NOTE and BITPOS/BITSIZE.  */
8149
8150 static rtx
8151 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8152                       HOST_WIDE_INT bitsize)
8153 {
8154   if (bitsize != -1)
8155     {
8156       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8157       if (bitpos != 0)
8158         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8159     }
8160   return loc_note;
8161 }
8162
8163 /* This function either modifies location piece list *DEST in
8164    place (if SRC and INNER is NULL), or copies location piece list
8165    *SRC to *DEST while modifying it.  Location BITPOS is modified
8166    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8167    not copied and if needed some padding around it is added.
8168    When modifying in place, DEST should point to EXPR_LIST where
8169    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8170    to the start of the whole list and INNER points to the EXPR_LIST
8171    where earlier pieces cover PIECE_BITPOS bits.  */
8172
8173 static void
8174 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8175                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8176                    HOST_WIDE_INT bitsize, rtx loc_note)
8177 {
8178   int diff;
8179   bool copy = inner != NULL;
8180
8181   if (copy)
8182     {
8183       /* First copy all nodes preceeding the current bitpos.  */
8184       while (src != inner)
8185         {
8186           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8187                                    decl_piece_bitsize (*src), NULL_RTX);
8188           dest = &XEXP (*dest, 1);
8189           src = &XEXP (*src, 1);
8190         }
8191     }
8192   /* Add padding if needed.  */
8193   if (bitpos != piece_bitpos)
8194     {
8195       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8196                                copy ? NULL_RTX : *dest);
8197       dest = &XEXP (*dest, 1);
8198     }
8199   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8200     {
8201       gcc_assert (!copy);
8202       /* A piece with correct bitpos and bitsize already exist,
8203          just update the location for it and return.  */
8204       *decl_piece_varloc_ptr (*dest) = loc_note;
8205       return;
8206     }
8207   /* Add the piece that changed.  */
8208   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8209   dest = &XEXP (*dest, 1);
8210   /* Skip over pieces that overlap it.  */
8211   diff = bitpos - piece_bitpos + bitsize;
8212   if (!copy)
8213     src = dest;
8214   while (diff > 0 && *src)
8215     {
8216       rtx piece = *src;
8217       diff -= decl_piece_bitsize (piece);
8218       if (copy)
8219         src = &XEXP (piece, 1);
8220       else
8221         {
8222           *src = XEXP (piece, 1);
8223           free_EXPR_LIST_node (piece);
8224         }
8225     }
8226   /* Add padding if needed.  */
8227   if (diff < 0 && *src)
8228     {
8229       if (!copy)
8230         dest = src;
8231       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8232       dest = &XEXP (*dest, 1);
8233     }
8234   if (!copy)
8235     return;
8236   /* Finally copy all nodes following it.  */
8237   while (*src)
8238     {
8239       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8240                                decl_piece_bitsize (*src), NULL_RTX);
8241       dest = &XEXP (*dest, 1);
8242       src = &XEXP (*src, 1);
8243     }
8244 }
8245
8246 /* Add a variable location node to the linked list for DECL.  */
8247
8248 static struct var_loc_node *
8249 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8250 {
8251   unsigned int decl_id;
8252   var_loc_list *temp;
8253   void **slot;
8254   struct var_loc_node *loc = NULL;
8255   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8256
8257   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8258     {
8259       tree realdecl = DECL_DEBUG_EXPR (decl);
8260       if (realdecl && handled_component_p (realdecl))
8261         {
8262           HOST_WIDE_INT maxsize;
8263           tree innerdecl;
8264           innerdecl
8265             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8266           if (!DECL_P (innerdecl)
8267               || DECL_IGNORED_P (innerdecl)
8268               || TREE_STATIC (innerdecl)
8269               || bitsize <= 0
8270               || bitpos + bitsize > 256
8271               || bitsize != maxsize)
8272             return NULL;
8273           decl = innerdecl;
8274         }
8275     }
8276
8277   decl_id = DECL_UID (decl);
8278   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8279   if (*slot == NULL)
8280     {
8281       temp = ggc_alloc_cleared_var_loc_list ();
8282       temp->decl_id = decl_id;
8283       *slot = temp;
8284     }
8285   else
8286     temp = (var_loc_list *) *slot;
8287
8288   if (temp->last)
8289     {
8290       struct var_loc_node *last = temp->last, *unused = NULL;
8291       rtx *piece_loc = NULL, last_loc_note;
8292       int piece_bitpos = 0;
8293       if (last->next)
8294         {
8295           last = last->next;
8296           gcc_assert (last->next == NULL);
8297         }
8298       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8299         {
8300           piece_loc = &last->loc;
8301           do
8302             {
8303               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8304               if (piece_bitpos + cur_bitsize > bitpos)
8305                 break;
8306               piece_bitpos += cur_bitsize;
8307               piece_loc = &XEXP (*piece_loc, 1);
8308             }
8309           while (*piece_loc);
8310         }
8311       /* TEMP->LAST here is either pointer to the last but one or
8312          last element in the chained list, LAST is pointer to the
8313          last element.  */
8314       if (label && strcmp (last->label, label) == 0)
8315         {
8316           /* For SRA optimized variables if there weren't any real
8317              insns since last note, just modify the last node.  */
8318           if (piece_loc != NULL)
8319             {
8320               adjust_piece_list (piece_loc, NULL, NULL,
8321                                  bitpos, piece_bitpos, bitsize, loc_note);
8322               return NULL;
8323             }
8324           /* If the last note doesn't cover any instructions, remove it.  */
8325           if (temp->last != last)
8326             {
8327               temp->last->next = NULL;
8328               unused = last;
8329               last = temp->last;
8330               gcc_assert (strcmp (last->label, label) != 0);
8331             }
8332           else
8333             {
8334               gcc_assert (temp->first == temp->last);
8335               memset (temp->last, '\0', sizeof (*temp->last));
8336               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8337               return temp->last;
8338             }
8339         }
8340       if (bitsize == -1 && NOTE_P (last->loc))
8341         last_loc_note = last->loc;
8342       else if (piece_loc != NULL
8343                && *piece_loc != NULL_RTX
8344                && piece_bitpos == bitpos
8345                && decl_piece_bitsize (*piece_loc) == bitsize)
8346         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8347       else
8348         last_loc_note = NULL_RTX;
8349       /* If the current location is the same as the end of the list,
8350          and either both or neither of the locations is uninitialized,
8351          we have nothing to do.  */
8352       if (last_loc_note == NULL_RTX
8353           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8354                             NOTE_VAR_LOCATION_LOC (loc_note)))
8355           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8356                != NOTE_VAR_LOCATION_STATUS (loc_note))
8357               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8358                    == VAR_INIT_STATUS_UNINITIALIZED)
8359                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8360                       == VAR_INIT_STATUS_UNINITIALIZED))))
8361         {
8362           /* Add LOC to the end of list and update LAST.  If the last
8363              element of the list has been removed above, reuse its
8364              memory for the new node, otherwise allocate a new one.  */
8365           if (unused)
8366             {
8367               loc = unused;
8368               memset (loc, '\0', sizeof (*loc));
8369             }
8370           else
8371             loc = ggc_alloc_cleared_var_loc_node ();
8372           if (bitsize == -1 || piece_loc == NULL)
8373             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8374           else
8375             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8376                                bitpos, piece_bitpos, bitsize, loc_note);
8377           last->next = loc;
8378           /* Ensure TEMP->LAST will point either to the new last but one
8379              element of the chain, or to the last element in it.  */
8380           if (last != temp->last)
8381             temp->last = last;
8382         }
8383       else if (unused)
8384         ggc_free (unused);
8385     }
8386   else
8387     {
8388       loc = ggc_alloc_cleared_var_loc_node ();
8389       temp->first = loc;
8390       temp->last = loc;
8391       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8392     }
8393   return loc;
8394 }
8395 \f
8396 /* Keep track of the number of spaces used to indent the
8397    output of the debugging routines that print the structure of
8398    the DIE internal representation.  */
8399 static int print_indent;
8400
8401 /* Indent the line the number of spaces given by print_indent.  */
8402
8403 static inline void
8404 print_spaces (FILE *outfile)
8405 {
8406   fprintf (outfile, "%*s", print_indent, "");
8407 }
8408
8409 /* Print a type signature in hex.  */
8410
8411 static inline void
8412 print_signature (FILE *outfile, char *sig)
8413 {
8414   int i;
8415
8416   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8417     fprintf (outfile, "%02x", sig[i] & 0xff);
8418 }
8419
8420 /* Print the information associated with a given DIE, and its children.
8421    This routine is a debugging aid only.  */
8422
8423 static void
8424 print_die (dw_die_ref die, FILE *outfile)
8425 {
8426   dw_attr_ref a;
8427   dw_die_ref c;
8428   unsigned ix;
8429
8430   print_spaces (outfile);
8431   fprintf (outfile, "DIE %4ld: %s\n",
8432            die->die_offset, dwarf_tag_name (die->die_tag));
8433   print_spaces (outfile);
8434   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8435   fprintf (outfile, " offset: %ld\n", die->die_offset);
8436   if (dwarf_version >= 4 && die->die_id.die_type_node)
8437     {
8438       print_spaces (outfile);
8439       fprintf (outfile, "  signature: ");
8440       print_signature (outfile, die->die_id.die_type_node->signature);
8441       fprintf (outfile, "\n");
8442     }
8443
8444   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8445     {
8446       print_spaces (outfile);
8447       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8448
8449       switch (AT_class (a))
8450         {
8451         case dw_val_class_addr:
8452           fprintf (outfile, "address");
8453           break;
8454         case dw_val_class_offset:
8455           fprintf (outfile, "offset");
8456           break;
8457         case dw_val_class_loc:
8458           fprintf (outfile, "location descriptor");
8459           break;
8460         case dw_val_class_loc_list:
8461           fprintf (outfile, "location list -> label:%s",
8462                    AT_loc_list (a)->ll_symbol);
8463           break;
8464         case dw_val_class_range_list:
8465           fprintf (outfile, "range list");
8466           break;
8467         case dw_val_class_const:
8468           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8469           break;
8470         case dw_val_class_unsigned_const:
8471           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8472           break;
8473         case dw_val_class_const_double:
8474           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8475                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8476                    a->dw_attr_val.v.val_double.high,
8477                    a->dw_attr_val.v.val_double.low);
8478           break;
8479         case dw_val_class_vec:
8480           fprintf (outfile, "floating-point or vector constant");
8481           break;
8482         case dw_val_class_flag:
8483           fprintf (outfile, "%u", AT_flag (a));
8484           break;
8485         case dw_val_class_die_ref:
8486           if (AT_ref (a) != NULL)
8487             {
8488               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8489                 {
8490                   fprintf (outfile, "die -> signature: ");
8491                   print_signature (outfile,
8492                                    AT_ref (a)->die_id.die_type_node->signature);
8493                 }
8494               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8495                 fprintf (outfile, "die -> label: %s",
8496                          AT_ref (a)->die_id.die_symbol);
8497               else
8498                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8499             }
8500           else
8501             fprintf (outfile, "die -> <null>");
8502           break;
8503         case dw_val_class_vms_delta:
8504           fprintf (outfile, "delta: @slotcount(%s-%s)",
8505                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8506           break;
8507         case dw_val_class_lbl_id:
8508         case dw_val_class_lineptr:
8509         case dw_val_class_macptr:
8510           fprintf (outfile, "label: %s", AT_lbl (a));
8511           break;
8512         case dw_val_class_str:
8513           if (AT_string (a) != NULL)
8514             fprintf (outfile, "\"%s\"", AT_string (a));
8515           else
8516             fprintf (outfile, "<null>");
8517           break;
8518         case dw_val_class_file:
8519           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8520                    AT_file (a)->emitted_number);
8521           break;
8522         case dw_val_class_data8:
8523           {
8524             int i;
8525
8526             for (i = 0; i < 8; i++)
8527               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8528             break;
8529           }
8530         default:
8531           break;
8532         }
8533
8534       fprintf (outfile, "\n");
8535     }
8536
8537   if (die->die_child != NULL)
8538     {
8539       print_indent += 4;
8540       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8541       print_indent -= 4;
8542     }
8543   if (print_indent == 0)
8544     fprintf (outfile, "\n");
8545 }
8546
8547 /* Print the contents of the source code line number correspondence table.
8548    This routine is a debugging aid only.  */
8549
8550 static void
8551 print_dwarf_line_table (FILE *outfile)
8552 {
8553   unsigned i;
8554   dw_line_info_ref line_info;
8555
8556   fprintf (outfile, "\n\nDWARF source line information\n");
8557   for (i = 1; i < line_info_table_in_use; i++)
8558     {
8559       line_info = &line_info_table[i];
8560       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8561                line_info->dw_file_num,
8562                line_info->dw_line_num);
8563     }
8564
8565   fprintf (outfile, "\n\n");
8566 }
8567
8568 /* Print the information collected for a given DIE.  */
8569
8570 DEBUG_FUNCTION void
8571 debug_dwarf_die (dw_die_ref die)
8572 {
8573   print_die (die, stderr);
8574 }
8575
8576 /* Print all DWARF information collected for the compilation unit.
8577    This routine is a debugging aid only.  */
8578
8579 DEBUG_FUNCTION void
8580 debug_dwarf (void)
8581 {
8582   print_indent = 0;
8583   print_die (comp_unit_die (), stderr);
8584   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8585     print_dwarf_line_table (stderr);
8586 }
8587 \f
8588 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8589    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8590    DIE that marks the start of the DIEs for this include file.  */
8591
8592 static dw_die_ref
8593 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8594 {
8595   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8596   dw_die_ref new_unit = gen_compile_unit_die (filename);
8597
8598   new_unit->die_sib = old_unit;
8599   return new_unit;
8600 }
8601
8602 /* Close an include-file CU and reopen the enclosing one.  */
8603
8604 static dw_die_ref
8605 pop_compile_unit (dw_die_ref old_unit)
8606 {
8607   dw_die_ref new_unit = old_unit->die_sib;
8608
8609   old_unit->die_sib = NULL;
8610   return new_unit;
8611 }
8612
8613 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8614 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8615
8616 /* Calculate the checksum of a location expression.  */
8617
8618 static inline void
8619 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8620 {
8621   int tem;
8622
8623   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8624   CHECKSUM (tem);
8625   CHECKSUM (loc->dw_loc_oprnd1);
8626   CHECKSUM (loc->dw_loc_oprnd2);
8627 }
8628
8629 /* Calculate the checksum of an attribute.  */
8630
8631 static void
8632 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8633 {
8634   dw_loc_descr_ref loc;
8635   rtx r;
8636
8637   CHECKSUM (at->dw_attr);
8638
8639   /* We don't care that this was compiled with a different compiler
8640      snapshot; if the output is the same, that's what matters.  */
8641   if (at->dw_attr == DW_AT_producer)
8642     return;
8643
8644   switch (AT_class (at))
8645     {
8646     case dw_val_class_const:
8647       CHECKSUM (at->dw_attr_val.v.val_int);
8648       break;
8649     case dw_val_class_unsigned_const:
8650       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8651       break;
8652     case dw_val_class_const_double:
8653       CHECKSUM (at->dw_attr_val.v.val_double);
8654       break;
8655     case dw_val_class_vec:
8656       CHECKSUM (at->dw_attr_val.v.val_vec);
8657       break;
8658     case dw_val_class_flag:
8659       CHECKSUM (at->dw_attr_val.v.val_flag);
8660       break;
8661     case dw_val_class_str:
8662       CHECKSUM_STRING (AT_string (at));
8663       break;
8664
8665     case dw_val_class_addr:
8666       r = AT_addr (at);
8667       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8668       CHECKSUM_STRING (XSTR (r, 0));
8669       break;
8670
8671     case dw_val_class_offset:
8672       CHECKSUM (at->dw_attr_val.v.val_offset);
8673       break;
8674
8675     case dw_val_class_loc:
8676       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8677         loc_checksum (loc, ctx);
8678       break;
8679
8680     case dw_val_class_die_ref:
8681       die_checksum (AT_ref (at), ctx, mark);
8682       break;
8683
8684     case dw_val_class_fde_ref:
8685     case dw_val_class_vms_delta:
8686     case dw_val_class_lbl_id:
8687     case dw_val_class_lineptr:
8688     case dw_val_class_macptr:
8689       break;
8690
8691     case dw_val_class_file:
8692       CHECKSUM_STRING (AT_file (at)->filename);
8693       break;
8694
8695     case dw_val_class_data8:
8696       CHECKSUM (at->dw_attr_val.v.val_data8);
8697       break;
8698
8699     default:
8700       break;
8701     }
8702 }
8703
8704 /* Calculate the checksum of a DIE.  */
8705
8706 static void
8707 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8708 {
8709   dw_die_ref c;
8710   dw_attr_ref a;
8711   unsigned ix;
8712
8713   /* To avoid infinite recursion.  */
8714   if (die->die_mark)
8715     {
8716       CHECKSUM (die->die_mark);
8717       return;
8718     }
8719   die->die_mark = ++(*mark);
8720
8721   CHECKSUM (die->die_tag);
8722
8723   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8724     attr_checksum (a, ctx, mark);
8725
8726   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8727 }
8728
8729 #undef CHECKSUM
8730 #undef CHECKSUM_STRING
8731
8732 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8733 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8734 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8735 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8736 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8737 #define CHECKSUM_ATTR(FOO) \
8738   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8739
8740 /* Calculate the checksum of a number in signed LEB128 format.  */
8741
8742 static void
8743 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8744 {
8745   unsigned char byte;
8746   bool more;
8747
8748   while (1)
8749     {
8750       byte = (value & 0x7f);
8751       value >>= 7;
8752       more = !((value == 0 && (byte & 0x40) == 0)
8753                 || (value == -1 && (byte & 0x40) != 0));
8754       if (more)
8755         byte |= 0x80;
8756       CHECKSUM (byte);
8757       if (!more)
8758         break;
8759     }
8760 }
8761
8762 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8763
8764 static void
8765 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8766 {
8767   while (1)
8768     {
8769       unsigned char byte = (value & 0x7f);
8770       value >>= 7;
8771       if (value != 0)
8772         /* More bytes to follow.  */
8773         byte |= 0x80;
8774       CHECKSUM (byte);
8775       if (value == 0)
8776         break;
8777     }
8778 }
8779
8780 /* Checksum the context of the DIE.  This adds the names of any
8781    surrounding namespaces or structures to the checksum.  */
8782
8783 static void
8784 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8785 {
8786   const char *name;
8787   dw_die_ref spec;
8788   int tag = die->die_tag;
8789
8790   if (tag != DW_TAG_namespace
8791       && tag != DW_TAG_structure_type
8792       && tag != DW_TAG_class_type)
8793     return;
8794
8795   name = get_AT_string (die, DW_AT_name);
8796
8797   spec = get_AT_ref (die, DW_AT_specification);
8798   if (spec != NULL)
8799     die = spec;
8800
8801   if (die->die_parent != NULL)
8802     checksum_die_context (die->die_parent, ctx);
8803
8804   CHECKSUM_ULEB128 ('C');
8805   CHECKSUM_ULEB128 (tag);
8806   if (name != NULL)
8807     CHECKSUM_STRING (name);
8808 }
8809
8810 /* Calculate the checksum of a location expression.  */
8811
8812 static inline void
8813 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8814 {
8815   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8816      were emitted as a DW_FORM_sdata instead of a location expression.  */
8817   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8818     {
8819       CHECKSUM_ULEB128 (DW_FORM_sdata);
8820       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8821       return;
8822     }
8823
8824   /* Otherwise, just checksum the raw location expression.  */
8825   while (loc != NULL)
8826     {
8827       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8828       CHECKSUM (loc->dw_loc_oprnd1);
8829       CHECKSUM (loc->dw_loc_oprnd2);
8830       loc = loc->dw_loc_next;
8831     }
8832 }
8833
8834 /* Calculate the checksum of an attribute.  */
8835
8836 static void
8837 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8838                        struct md5_ctx *ctx, int *mark)
8839 {
8840   dw_loc_descr_ref loc;
8841   rtx r;
8842
8843   if (AT_class (at) == dw_val_class_die_ref)
8844     {
8845       dw_die_ref target_die = AT_ref (at);
8846
8847       /* For pointer and reference types, we checksum only the (qualified)
8848          name of the target type (if there is a name).  For friend entries,
8849          we checksum only the (qualified) name of the target type or function.
8850          This allows the checksum to remain the same whether the target type
8851          is complete or not.  */
8852       if ((at->dw_attr == DW_AT_type
8853            && (tag == DW_TAG_pointer_type
8854                || tag == DW_TAG_reference_type
8855                || tag == DW_TAG_rvalue_reference_type
8856                || tag == DW_TAG_ptr_to_member_type))
8857           || (at->dw_attr == DW_AT_friend
8858               && tag == DW_TAG_friend))
8859         {
8860           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8861
8862           if (name_attr != NULL)
8863             {
8864               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8865
8866               if (decl == NULL)
8867                 decl = target_die;
8868               CHECKSUM_ULEB128 ('N');
8869               CHECKSUM_ULEB128 (at->dw_attr);
8870               if (decl->die_parent != NULL)
8871                 checksum_die_context (decl->die_parent, ctx);
8872               CHECKSUM_ULEB128 ('E');
8873               CHECKSUM_STRING (AT_string (name_attr));
8874               return;
8875             }
8876         }
8877
8878       /* For all other references to another DIE, we check to see if the
8879          target DIE has already been visited.  If it has, we emit a
8880          backward reference; if not, we descend recursively.  */
8881       if (target_die->die_mark > 0)
8882         {
8883           CHECKSUM_ULEB128 ('R');
8884           CHECKSUM_ULEB128 (at->dw_attr);
8885           CHECKSUM_ULEB128 (target_die->die_mark);
8886         }
8887       else
8888         {
8889           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8890
8891           if (decl == NULL)
8892             decl = target_die;
8893           target_die->die_mark = ++(*mark);
8894           CHECKSUM_ULEB128 ('T');
8895           CHECKSUM_ULEB128 (at->dw_attr);
8896           if (decl->die_parent != NULL)
8897             checksum_die_context (decl->die_parent, ctx);
8898           die_checksum_ordered (target_die, ctx, mark);
8899         }
8900       return;
8901     }
8902
8903   CHECKSUM_ULEB128 ('A');
8904   CHECKSUM_ULEB128 (at->dw_attr);
8905
8906   switch (AT_class (at))
8907     {
8908     case dw_val_class_const:
8909       CHECKSUM_ULEB128 (DW_FORM_sdata);
8910       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8911       break;
8912
8913     case dw_val_class_unsigned_const:
8914       CHECKSUM_ULEB128 (DW_FORM_sdata);
8915       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8916       break;
8917
8918     case dw_val_class_const_double:
8919       CHECKSUM_ULEB128 (DW_FORM_block);
8920       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8921       CHECKSUM (at->dw_attr_val.v.val_double);
8922       break;
8923
8924     case dw_val_class_vec:
8925       CHECKSUM_ULEB128 (DW_FORM_block);
8926       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8927       CHECKSUM (at->dw_attr_val.v.val_vec);
8928       break;
8929
8930     case dw_val_class_flag:
8931       CHECKSUM_ULEB128 (DW_FORM_flag);
8932       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8933       break;
8934
8935     case dw_val_class_str:
8936       CHECKSUM_ULEB128 (DW_FORM_string);
8937       CHECKSUM_STRING (AT_string (at));
8938       break;
8939
8940     case dw_val_class_addr:
8941       r = AT_addr (at);
8942       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8943       CHECKSUM_ULEB128 (DW_FORM_string);
8944       CHECKSUM_STRING (XSTR (r, 0));
8945       break;
8946
8947     case dw_val_class_offset:
8948       CHECKSUM_ULEB128 (DW_FORM_sdata);
8949       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8950       break;
8951
8952     case dw_val_class_loc:
8953       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8954         loc_checksum_ordered (loc, ctx);
8955       break;
8956
8957     case dw_val_class_fde_ref:
8958     case dw_val_class_lbl_id:
8959     case dw_val_class_lineptr:
8960     case dw_val_class_macptr:
8961       break;
8962
8963     case dw_val_class_file:
8964       CHECKSUM_ULEB128 (DW_FORM_string);
8965       CHECKSUM_STRING (AT_file (at)->filename);
8966       break;
8967
8968     case dw_val_class_data8:
8969       CHECKSUM (at->dw_attr_val.v.val_data8);
8970       break;
8971
8972     default:
8973       break;
8974     }
8975 }
8976
8977 struct checksum_attributes
8978 {
8979   dw_attr_ref at_name;
8980   dw_attr_ref at_type;
8981   dw_attr_ref at_friend;
8982   dw_attr_ref at_accessibility;
8983   dw_attr_ref at_address_class;
8984   dw_attr_ref at_allocated;
8985   dw_attr_ref at_artificial;
8986   dw_attr_ref at_associated;
8987   dw_attr_ref at_binary_scale;
8988   dw_attr_ref at_bit_offset;
8989   dw_attr_ref at_bit_size;
8990   dw_attr_ref at_bit_stride;
8991   dw_attr_ref at_byte_size;
8992   dw_attr_ref at_byte_stride;
8993   dw_attr_ref at_const_value;
8994   dw_attr_ref at_containing_type;
8995   dw_attr_ref at_count;
8996   dw_attr_ref at_data_location;
8997   dw_attr_ref at_data_member_location;
8998   dw_attr_ref at_decimal_scale;
8999   dw_attr_ref at_decimal_sign;
9000   dw_attr_ref at_default_value;
9001   dw_attr_ref at_digit_count;
9002   dw_attr_ref at_discr;
9003   dw_attr_ref at_discr_list;
9004   dw_attr_ref at_discr_value;
9005   dw_attr_ref at_encoding;
9006   dw_attr_ref at_endianity;
9007   dw_attr_ref at_explicit;
9008   dw_attr_ref at_is_optional;
9009   dw_attr_ref at_location;
9010   dw_attr_ref at_lower_bound;
9011   dw_attr_ref at_mutable;
9012   dw_attr_ref at_ordering;
9013   dw_attr_ref at_picture_string;
9014   dw_attr_ref at_prototyped;
9015   dw_attr_ref at_small;
9016   dw_attr_ref at_segment;
9017   dw_attr_ref at_string_length;
9018   dw_attr_ref at_threads_scaled;
9019   dw_attr_ref at_upper_bound;
9020   dw_attr_ref at_use_location;
9021   dw_attr_ref at_use_UTF8;
9022   dw_attr_ref at_variable_parameter;
9023   dw_attr_ref at_virtuality;
9024   dw_attr_ref at_visibility;
9025   dw_attr_ref at_vtable_elem_location;
9026 };
9027
9028 /* Collect the attributes that we will want to use for the checksum.  */
9029
9030 static void
9031 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9032 {
9033   dw_attr_ref a;
9034   unsigned ix;
9035
9036   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9037     {
9038       switch (a->dw_attr)
9039         {
9040         case DW_AT_name:
9041           attrs->at_name = a;
9042           break;
9043         case DW_AT_type:
9044           attrs->at_type = a;
9045           break;
9046         case DW_AT_friend:
9047           attrs->at_friend = a;
9048           break;
9049         case DW_AT_accessibility:
9050           attrs->at_accessibility = a;
9051           break;
9052         case DW_AT_address_class:
9053           attrs->at_address_class = a;
9054           break;
9055         case DW_AT_allocated:
9056           attrs->at_allocated = a;
9057           break;
9058         case DW_AT_artificial:
9059           attrs->at_artificial = a;
9060           break;
9061         case DW_AT_associated:
9062           attrs->at_associated = a;
9063           break;
9064         case DW_AT_binary_scale:
9065           attrs->at_binary_scale = a;
9066           break;
9067         case DW_AT_bit_offset:
9068           attrs->at_bit_offset = a;
9069           break;
9070         case DW_AT_bit_size:
9071           attrs->at_bit_size = a;
9072           break;
9073         case DW_AT_bit_stride:
9074           attrs->at_bit_stride = a;
9075           break;
9076         case DW_AT_byte_size:
9077           attrs->at_byte_size = a;
9078           break;
9079         case DW_AT_byte_stride:
9080           attrs->at_byte_stride = a;
9081           break;
9082         case DW_AT_const_value:
9083           attrs->at_const_value = a;
9084           break;
9085         case DW_AT_containing_type:
9086           attrs->at_containing_type = a;
9087           break;
9088         case DW_AT_count:
9089           attrs->at_count = a;
9090           break;
9091         case DW_AT_data_location:
9092           attrs->at_data_location = a;
9093           break;
9094         case DW_AT_data_member_location:
9095           attrs->at_data_member_location = a;
9096           break;
9097         case DW_AT_decimal_scale:
9098           attrs->at_decimal_scale = a;
9099           break;
9100         case DW_AT_decimal_sign:
9101           attrs->at_decimal_sign = a;
9102           break;
9103         case DW_AT_default_value:
9104           attrs->at_default_value = a;
9105           break;
9106         case DW_AT_digit_count:
9107           attrs->at_digit_count = a;
9108           break;
9109         case DW_AT_discr:
9110           attrs->at_discr = a;
9111           break;
9112         case DW_AT_discr_list:
9113           attrs->at_discr_list = a;
9114           break;
9115         case DW_AT_discr_value:
9116           attrs->at_discr_value = a;
9117           break;
9118         case DW_AT_encoding:
9119           attrs->at_encoding = a;
9120           break;
9121         case DW_AT_endianity:
9122           attrs->at_endianity = a;
9123           break;
9124         case DW_AT_explicit:
9125           attrs->at_explicit = a;
9126           break;
9127         case DW_AT_is_optional:
9128           attrs->at_is_optional = a;
9129           break;
9130         case DW_AT_location:
9131           attrs->at_location = a;
9132           break;
9133         case DW_AT_lower_bound:
9134           attrs->at_lower_bound = a;
9135           break;
9136         case DW_AT_mutable:
9137           attrs->at_mutable = a;
9138           break;
9139         case DW_AT_ordering:
9140           attrs->at_ordering = a;
9141           break;
9142         case DW_AT_picture_string:
9143           attrs->at_picture_string = a;
9144           break;
9145         case DW_AT_prototyped:
9146           attrs->at_prototyped = a;
9147           break;
9148         case DW_AT_small:
9149           attrs->at_small = a;
9150           break;
9151         case DW_AT_segment:
9152           attrs->at_segment = a;
9153           break;
9154         case DW_AT_string_length:
9155           attrs->at_string_length = a;
9156           break;
9157         case DW_AT_threads_scaled:
9158           attrs->at_threads_scaled = a;
9159           break;
9160         case DW_AT_upper_bound:
9161           attrs->at_upper_bound = a;
9162           break;
9163         case DW_AT_use_location:
9164           attrs->at_use_location = a;
9165           break;
9166         case DW_AT_use_UTF8:
9167           attrs->at_use_UTF8 = a;
9168           break;
9169         case DW_AT_variable_parameter:
9170           attrs->at_variable_parameter = a;
9171           break;
9172         case DW_AT_virtuality:
9173           attrs->at_virtuality = a;
9174           break;
9175         case DW_AT_visibility:
9176           attrs->at_visibility = a;
9177           break;
9178         case DW_AT_vtable_elem_location:
9179           attrs->at_vtable_elem_location = a;
9180           break;
9181         default:
9182           break;
9183         }
9184     }
9185 }
9186
9187 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9188
9189 static void
9190 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9191 {
9192   dw_die_ref c;
9193   dw_die_ref decl;
9194   struct checksum_attributes attrs;
9195
9196   CHECKSUM_ULEB128 ('D');
9197   CHECKSUM_ULEB128 (die->die_tag);
9198
9199   memset (&attrs, 0, sizeof (attrs));
9200
9201   decl = get_AT_ref (die, DW_AT_specification);
9202   if (decl != NULL)
9203     collect_checksum_attributes (&attrs, decl);
9204   collect_checksum_attributes (&attrs, die);
9205
9206   CHECKSUM_ATTR (attrs.at_name);
9207   CHECKSUM_ATTR (attrs.at_accessibility);
9208   CHECKSUM_ATTR (attrs.at_address_class);
9209   CHECKSUM_ATTR (attrs.at_allocated);
9210   CHECKSUM_ATTR (attrs.at_artificial);
9211   CHECKSUM_ATTR (attrs.at_associated);
9212   CHECKSUM_ATTR (attrs.at_binary_scale);
9213   CHECKSUM_ATTR (attrs.at_bit_offset);
9214   CHECKSUM_ATTR (attrs.at_bit_size);
9215   CHECKSUM_ATTR (attrs.at_bit_stride);
9216   CHECKSUM_ATTR (attrs.at_byte_size);
9217   CHECKSUM_ATTR (attrs.at_byte_stride);
9218   CHECKSUM_ATTR (attrs.at_const_value);
9219   CHECKSUM_ATTR (attrs.at_containing_type);
9220   CHECKSUM_ATTR (attrs.at_count);
9221   CHECKSUM_ATTR (attrs.at_data_location);
9222   CHECKSUM_ATTR (attrs.at_data_member_location);
9223   CHECKSUM_ATTR (attrs.at_decimal_scale);
9224   CHECKSUM_ATTR (attrs.at_decimal_sign);
9225   CHECKSUM_ATTR (attrs.at_default_value);
9226   CHECKSUM_ATTR (attrs.at_digit_count);
9227   CHECKSUM_ATTR (attrs.at_discr);
9228   CHECKSUM_ATTR (attrs.at_discr_list);
9229   CHECKSUM_ATTR (attrs.at_discr_value);
9230   CHECKSUM_ATTR (attrs.at_encoding);
9231   CHECKSUM_ATTR (attrs.at_endianity);
9232   CHECKSUM_ATTR (attrs.at_explicit);
9233   CHECKSUM_ATTR (attrs.at_is_optional);
9234   CHECKSUM_ATTR (attrs.at_location);
9235   CHECKSUM_ATTR (attrs.at_lower_bound);
9236   CHECKSUM_ATTR (attrs.at_mutable);
9237   CHECKSUM_ATTR (attrs.at_ordering);
9238   CHECKSUM_ATTR (attrs.at_picture_string);
9239   CHECKSUM_ATTR (attrs.at_prototyped);
9240   CHECKSUM_ATTR (attrs.at_small);
9241   CHECKSUM_ATTR (attrs.at_segment);
9242   CHECKSUM_ATTR (attrs.at_string_length);
9243   CHECKSUM_ATTR (attrs.at_threads_scaled);
9244   CHECKSUM_ATTR (attrs.at_upper_bound);
9245   CHECKSUM_ATTR (attrs.at_use_location);
9246   CHECKSUM_ATTR (attrs.at_use_UTF8);
9247   CHECKSUM_ATTR (attrs.at_variable_parameter);
9248   CHECKSUM_ATTR (attrs.at_virtuality);
9249   CHECKSUM_ATTR (attrs.at_visibility);
9250   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9251   CHECKSUM_ATTR (attrs.at_type);
9252   CHECKSUM_ATTR (attrs.at_friend);
9253
9254   /* Checksum the child DIEs, except for nested types and member functions.  */
9255   c = die->die_child;
9256   if (c) do {
9257     dw_attr_ref name_attr;
9258
9259     c = c->die_sib;
9260     name_attr = get_AT (c, DW_AT_name);
9261     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9262         && name_attr != NULL)
9263       {
9264         CHECKSUM_ULEB128 ('S');
9265         CHECKSUM_ULEB128 (c->die_tag);
9266         CHECKSUM_STRING (AT_string (name_attr));
9267       }
9268     else
9269       {
9270         /* Mark this DIE so it gets processed when unmarking.  */
9271         if (c->die_mark == 0)
9272           c->die_mark = -1;
9273         die_checksum_ordered (c, ctx, mark);
9274       }
9275   } while (c != die->die_child);
9276
9277   CHECKSUM_ULEB128 (0);
9278 }
9279
9280 #undef CHECKSUM
9281 #undef CHECKSUM_STRING
9282 #undef CHECKSUM_ATTR
9283 #undef CHECKSUM_LEB128
9284 #undef CHECKSUM_ULEB128
9285
9286 /* Generate the type signature for DIE.  This is computed by generating an
9287    MD5 checksum over the DIE's tag, its relevant attributes, and its
9288    children.  Attributes that are references to other DIEs are processed
9289    by recursion, using the MARK field to prevent infinite recursion.
9290    If the DIE is nested inside a namespace or another type, we also
9291    need to include that context in the signature.  The lower 64 bits
9292    of the resulting MD5 checksum comprise the signature.  */
9293
9294 static void
9295 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9296 {
9297   int mark;
9298   const char *name;
9299   unsigned char checksum[16];
9300   struct md5_ctx ctx;
9301   dw_die_ref decl;
9302
9303   name = get_AT_string (die, DW_AT_name);
9304   decl = get_AT_ref (die, DW_AT_specification);
9305
9306   /* First, compute a signature for just the type name (and its surrounding
9307      context, if any.  This is stored in the type unit DIE for link-time
9308      ODR (one-definition rule) checking.  */
9309
9310   if (is_cxx() && name != NULL)
9311     {
9312       md5_init_ctx (&ctx);
9313
9314       /* Checksum the names of surrounding namespaces and structures.  */
9315       if (decl != NULL && decl->die_parent != NULL)
9316         checksum_die_context (decl->die_parent, &ctx);
9317
9318       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9319       md5_process_bytes (name, strlen (name) + 1, &ctx);
9320       md5_finish_ctx (&ctx, checksum);
9321
9322       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9323     }
9324
9325   /* Next, compute the complete type signature.  */
9326
9327   md5_init_ctx (&ctx);
9328   mark = 1;
9329   die->die_mark = mark;
9330
9331   /* Checksum the names of surrounding namespaces and structures.  */
9332   if (decl != NULL && decl->die_parent != NULL)
9333     checksum_die_context (decl->die_parent, &ctx);
9334
9335   /* Checksum the DIE and its children.  */
9336   die_checksum_ordered (die, &ctx, &mark);
9337   unmark_all_dies (die);
9338   md5_finish_ctx (&ctx, checksum);
9339
9340   /* Store the signature in the type node and link the type DIE and the
9341      type node together.  */
9342   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9343           DWARF_TYPE_SIGNATURE_SIZE);
9344   die->die_id.die_type_node = type_node;
9345   type_node->type_die = die;
9346
9347   /* If the DIE is a specification, link its declaration to the type node
9348      as well.  */
9349   if (decl != NULL)
9350     decl->die_id.die_type_node = type_node;
9351 }
9352
9353 /* Do the location expressions look same?  */
9354 static inline int
9355 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9356 {
9357   return loc1->dw_loc_opc == loc2->dw_loc_opc
9358          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9359          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9360 }
9361
9362 /* Do the values look the same?  */
9363 static int
9364 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9365 {
9366   dw_loc_descr_ref loc1, loc2;
9367   rtx r1, r2;
9368
9369   if (v1->val_class != v2->val_class)
9370     return 0;
9371
9372   switch (v1->val_class)
9373     {
9374     case dw_val_class_const:
9375       return v1->v.val_int == v2->v.val_int;
9376     case dw_val_class_unsigned_const:
9377       return v1->v.val_unsigned == v2->v.val_unsigned;
9378     case dw_val_class_const_double:
9379       return v1->v.val_double.high == v2->v.val_double.high
9380              && v1->v.val_double.low == v2->v.val_double.low;
9381     case dw_val_class_vec:
9382       if (v1->v.val_vec.length != v2->v.val_vec.length
9383           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9384         return 0;
9385       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9386                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9387         return 0;
9388       return 1;
9389     case dw_val_class_flag:
9390       return v1->v.val_flag == v2->v.val_flag;
9391     case dw_val_class_str:
9392       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9393
9394     case dw_val_class_addr:
9395       r1 = v1->v.val_addr;
9396       r2 = v2->v.val_addr;
9397       if (GET_CODE (r1) != GET_CODE (r2))
9398         return 0;
9399       return !rtx_equal_p (r1, r2);
9400
9401     case dw_val_class_offset:
9402       return v1->v.val_offset == v2->v.val_offset;
9403
9404     case dw_val_class_loc:
9405       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9406            loc1 && loc2;
9407            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9408         if (!same_loc_p (loc1, loc2, mark))
9409           return 0;
9410       return !loc1 && !loc2;
9411
9412     case dw_val_class_die_ref:
9413       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9414
9415     case dw_val_class_fde_ref:
9416     case dw_val_class_vms_delta:
9417     case dw_val_class_lbl_id:
9418     case dw_val_class_lineptr:
9419     case dw_val_class_macptr:
9420       return 1;
9421
9422     case dw_val_class_file:
9423       return v1->v.val_file == v2->v.val_file;
9424
9425     case dw_val_class_data8:
9426       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9427
9428     default:
9429       return 1;
9430     }
9431 }
9432
9433 /* Do the attributes look the same?  */
9434
9435 static int
9436 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9437 {
9438   if (at1->dw_attr != at2->dw_attr)
9439     return 0;
9440
9441   /* We don't care that this was compiled with a different compiler
9442      snapshot; if the output is the same, that's what matters. */
9443   if (at1->dw_attr == DW_AT_producer)
9444     return 1;
9445
9446   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9447 }
9448
9449 /* Do the dies look the same?  */
9450
9451 static int
9452 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9453 {
9454   dw_die_ref c1, c2;
9455   dw_attr_ref a1;
9456   unsigned ix;
9457
9458   /* To avoid infinite recursion.  */
9459   if (die1->die_mark)
9460     return die1->die_mark == die2->die_mark;
9461   die1->die_mark = die2->die_mark = ++(*mark);
9462
9463   if (die1->die_tag != die2->die_tag)
9464     return 0;
9465
9466   if (VEC_length (dw_attr_node, die1->die_attr)
9467       != VEC_length (dw_attr_node, die2->die_attr))
9468     return 0;
9469
9470   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9471     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9472       return 0;
9473
9474   c1 = die1->die_child;
9475   c2 = die2->die_child;
9476   if (! c1)
9477     {
9478       if (c2)
9479         return 0;
9480     }
9481   else
9482     for (;;)
9483       {
9484         if (!same_die_p (c1, c2, mark))
9485           return 0;
9486         c1 = c1->die_sib;
9487         c2 = c2->die_sib;
9488         if (c1 == die1->die_child)
9489           {
9490             if (c2 == die2->die_child)
9491               break;
9492             else
9493               return 0;
9494           }
9495     }
9496
9497   return 1;
9498 }
9499
9500 /* Do the dies look the same?  Wrapper around same_die_p.  */
9501
9502 static int
9503 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9504 {
9505   int mark = 0;
9506   int ret = same_die_p (die1, die2, &mark);
9507
9508   unmark_all_dies (die1);
9509   unmark_all_dies (die2);
9510
9511   return ret;
9512 }
9513
9514 /* The prefix to attach to symbols on DIEs in the current comdat debug
9515    info section.  */
9516 static char *comdat_symbol_id;
9517
9518 /* The index of the current symbol within the current comdat CU.  */
9519 static unsigned int comdat_symbol_number;
9520
9521 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9522    children, and set comdat_symbol_id accordingly.  */
9523
9524 static void
9525 compute_section_prefix (dw_die_ref unit_die)
9526 {
9527   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9528   const char *base = die_name ? lbasename (die_name) : "anonymous";
9529   char *name = XALLOCAVEC (char, strlen (base) + 64);
9530   char *p;
9531   int i, mark;
9532   unsigned char checksum[16];
9533   struct md5_ctx ctx;
9534
9535   /* Compute the checksum of the DIE, then append part of it as hex digits to
9536      the name filename of the unit.  */
9537
9538   md5_init_ctx (&ctx);
9539   mark = 0;
9540   die_checksum (unit_die, &ctx, &mark);
9541   unmark_all_dies (unit_die);
9542   md5_finish_ctx (&ctx, checksum);
9543
9544   sprintf (name, "%s.", base);
9545   clean_symbol_name (name);
9546
9547   p = name + strlen (name);
9548   for (i = 0; i < 4; i++)
9549     {
9550       sprintf (p, "%.2x", checksum[i]);
9551       p += 2;
9552     }
9553
9554   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9555   comdat_symbol_number = 0;
9556 }
9557
9558 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9559
9560 static int
9561 is_type_die (dw_die_ref die)
9562 {
9563   switch (die->die_tag)
9564     {
9565     case DW_TAG_array_type:
9566     case DW_TAG_class_type:
9567     case DW_TAG_interface_type:
9568     case DW_TAG_enumeration_type:
9569     case DW_TAG_pointer_type:
9570     case DW_TAG_reference_type:
9571     case DW_TAG_rvalue_reference_type:
9572     case DW_TAG_string_type:
9573     case DW_TAG_structure_type:
9574     case DW_TAG_subroutine_type:
9575     case DW_TAG_union_type:
9576     case DW_TAG_ptr_to_member_type:
9577     case DW_TAG_set_type:
9578     case DW_TAG_subrange_type:
9579     case DW_TAG_base_type:
9580     case DW_TAG_const_type:
9581     case DW_TAG_file_type:
9582     case DW_TAG_packed_type:
9583     case DW_TAG_volatile_type:
9584     case DW_TAG_typedef:
9585       return 1;
9586     default:
9587       return 0;
9588     }
9589 }
9590
9591 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9592    Basically, we want to choose the bits that are likely to be shared between
9593    compilations (types) and leave out the bits that are specific to individual
9594    compilations (functions).  */
9595
9596 static int
9597 is_comdat_die (dw_die_ref c)
9598 {
9599   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9600      we do for stabs.  The advantage is a greater likelihood of sharing between
9601      objects that don't include headers in the same order (and therefore would
9602      put the base types in a different comdat).  jason 8/28/00 */
9603
9604   if (c->die_tag == DW_TAG_base_type)
9605     return 0;
9606
9607   if (c->die_tag == DW_TAG_pointer_type
9608       || c->die_tag == DW_TAG_reference_type
9609       || c->die_tag == DW_TAG_rvalue_reference_type
9610       || c->die_tag == DW_TAG_const_type
9611       || c->die_tag == DW_TAG_volatile_type)
9612     {
9613       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9614
9615       return t ? is_comdat_die (t) : 0;
9616     }
9617
9618   return is_type_die (c);
9619 }
9620
9621 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9622    compilation unit.  */
9623
9624 static int
9625 is_symbol_die (dw_die_ref c)
9626 {
9627   return (is_type_die (c)
9628           || is_declaration_die (c)
9629           || c->die_tag == DW_TAG_namespace
9630           || c->die_tag == DW_TAG_module);
9631 }
9632
9633 /* Returns true iff C is a compile-unit DIE.  */
9634
9635 static inline bool
9636 is_cu_die (dw_die_ref c)
9637 {
9638   return c && c->die_tag == DW_TAG_compile_unit;
9639 }
9640
9641 static char *
9642 gen_internal_sym (const char *prefix)
9643 {
9644   char buf[256];
9645
9646   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9647   return xstrdup (buf);
9648 }
9649
9650 /* Assign symbols to all worthy DIEs under DIE.  */
9651
9652 static void
9653 assign_symbol_names (dw_die_ref die)
9654 {
9655   dw_die_ref c;
9656
9657   if (is_symbol_die (die))
9658     {
9659       if (comdat_symbol_id)
9660         {
9661           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9662
9663           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9664                    comdat_symbol_id, comdat_symbol_number++);
9665           die->die_id.die_symbol = xstrdup (p);
9666         }
9667       else
9668         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9669     }
9670
9671   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9672 }
9673
9674 struct cu_hash_table_entry
9675 {
9676   dw_die_ref cu;
9677   unsigned min_comdat_num, max_comdat_num;
9678   struct cu_hash_table_entry *next;
9679 };
9680
9681 /* Routines to manipulate hash table of CUs.  */
9682 static hashval_t
9683 htab_cu_hash (const void *of)
9684 {
9685   const struct cu_hash_table_entry *const entry =
9686     (const struct cu_hash_table_entry *) of;
9687
9688   return htab_hash_string (entry->cu->die_id.die_symbol);
9689 }
9690
9691 static int
9692 htab_cu_eq (const void *of1, const void *of2)
9693 {
9694   const struct cu_hash_table_entry *const entry1 =
9695     (const struct cu_hash_table_entry *) of1;
9696   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9697
9698   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9699 }
9700
9701 static void
9702 htab_cu_del (void *what)
9703 {
9704   struct cu_hash_table_entry *next,
9705     *entry = (struct cu_hash_table_entry *) what;
9706
9707   while (entry)
9708     {
9709       next = entry->next;
9710       free (entry);
9711       entry = next;
9712     }
9713 }
9714
9715 /* Check whether we have already seen this CU and set up SYM_NUM
9716    accordingly.  */
9717 static int
9718 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9719 {
9720   struct cu_hash_table_entry dummy;
9721   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9722
9723   dummy.max_comdat_num = 0;
9724
9725   slot = (struct cu_hash_table_entry **)
9726     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9727         INSERT);
9728   entry = *slot;
9729
9730   for (; entry; last = entry, entry = entry->next)
9731     {
9732       if (same_die_p_wrap (cu, entry->cu))
9733         break;
9734     }
9735
9736   if (entry)
9737     {
9738       *sym_num = entry->min_comdat_num;
9739       return 1;
9740     }
9741
9742   entry = XCNEW (struct cu_hash_table_entry);
9743   entry->cu = cu;
9744   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9745   entry->next = *slot;
9746   *slot = entry;
9747
9748   return 0;
9749 }
9750
9751 /* Record SYM_NUM to record of CU in HTABLE.  */
9752 static void
9753 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9754 {
9755   struct cu_hash_table_entry **slot, *entry;
9756
9757   slot = (struct cu_hash_table_entry **)
9758     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9759         NO_INSERT);
9760   entry = *slot;
9761
9762   entry->max_comdat_num = sym_num;
9763 }
9764
9765 /* Traverse the DIE (which is always comp_unit_die), and set up
9766    additional compilation units for each of the include files we see
9767    bracketed by BINCL/EINCL.  */
9768
9769 static void
9770 break_out_includes (dw_die_ref die)
9771 {
9772   dw_die_ref c;
9773   dw_die_ref unit = NULL;
9774   limbo_die_node *node, **pnode;
9775   htab_t cu_hash_table;
9776
9777   c = die->die_child;
9778   if (c) do {
9779     dw_die_ref prev = c;
9780     c = c->die_sib;
9781     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9782            || (unit && is_comdat_die (c)))
9783       {
9784         dw_die_ref next = c->die_sib;
9785
9786         /* This DIE is for a secondary CU; remove it from the main one.  */
9787         remove_child_with_prev (c, prev);
9788
9789         if (c->die_tag == DW_TAG_GNU_BINCL)
9790           unit = push_new_compile_unit (unit, c);
9791         else if (c->die_tag == DW_TAG_GNU_EINCL)
9792           unit = pop_compile_unit (unit);
9793         else
9794           add_child_die (unit, c);
9795         c = next;
9796         if (c == die->die_child)
9797           break;
9798       }
9799   } while (c != die->die_child);
9800
9801 #if 0
9802   /* We can only use this in debugging, since the frontend doesn't check
9803      to make sure that we leave every include file we enter.  */
9804   gcc_assert (!unit);
9805 #endif
9806
9807   assign_symbol_names (die);
9808   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9809   for (node = limbo_die_list, pnode = &limbo_die_list;
9810        node;
9811        node = node->next)
9812     {
9813       int is_dupl;
9814
9815       compute_section_prefix (node->die);
9816       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9817                         &comdat_symbol_number);
9818       assign_symbol_names (node->die);
9819       if (is_dupl)
9820         *pnode = node->next;
9821       else
9822         {
9823           pnode = &node->next;
9824           record_comdat_symbol_number (node->die, cu_hash_table,
9825                 comdat_symbol_number);
9826         }
9827     }
9828   htab_delete (cu_hash_table);
9829 }
9830
9831 /* Return non-zero if this DIE is a declaration.  */
9832
9833 static int
9834 is_declaration_die (dw_die_ref die)
9835 {
9836   dw_attr_ref a;
9837   unsigned ix;
9838
9839   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9840     if (a->dw_attr == DW_AT_declaration)
9841       return 1;
9842
9843   return 0;
9844 }
9845
9846 /* Return non-zero if this DIE is nested inside a subprogram.  */
9847
9848 static int
9849 is_nested_in_subprogram (dw_die_ref die)
9850 {
9851   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9852
9853   if (decl == NULL)
9854     decl = die;
9855   return local_scope_p (decl);
9856 }
9857
9858 /* Return non-zero if this is a type DIE that should be moved to a
9859    COMDAT .debug_types section.  */
9860
9861 static int
9862 should_move_die_to_comdat (dw_die_ref die)
9863 {
9864   switch (die->die_tag)
9865     {
9866     case DW_TAG_class_type:
9867     case DW_TAG_structure_type:
9868     case DW_TAG_enumeration_type:
9869     case DW_TAG_union_type:
9870       /* Don't move declarations, inlined instances, or types nested in a
9871          subprogram.  */
9872       if (is_declaration_die (die)
9873           || get_AT (die, DW_AT_abstract_origin)
9874           || is_nested_in_subprogram (die))
9875         return 0;
9876       return 1;
9877     case DW_TAG_array_type:
9878     case DW_TAG_interface_type:
9879     case DW_TAG_pointer_type:
9880     case DW_TAG_reference_type:
9881     case DW_TAG_rvalue_reference_type:
9882     case DW_TAG_string_type:
9883     case DW_TAG_subroutine_type:
9884     case DW_TAG_ptr_to_member_type:
9885     case DW_TAG_set_type:
9886     case DW_TAG_subrange_type:
9887     case DW_TAG_base_type:
9888     case DW_TAG_const_type:
9889     case DW_TAG_file_type:
9890     case DW_TAG_packed_type:
9891     case DW_TAG_volatile_type:
9892     case DW_TAG_typedef:
9893     default:
9894       return 0;
9895     }
9896 }
9897
9898 /* Make a clone of DIE.  */
9899
9900 static dw_die_ref
9901 clone_die (dw_die_ref die)
9902 {
9903   dw_die_ref clone;
9904   dw_attr_ref a;
9905   unsigned ix;
9906
9907   clone = ggc_alloc_cleared_die_node ();
9908   clone->die_tag = die->die_tag;
9909
9910   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9911     add_dwarf_attr (clone, a);
9912
9913   return clone;
9914 }
9915
9916 /* Make a clone of the tree rooted at DIE.  */
9917
9918 static dw_die_ref
9919 clone_tree (dw_die_ref die)
9920 {
9921   dw_die_ref c;
9922   dw_die_ref clone = clone_die (die);
9923
9924   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9925
9926   return clone;
9927 }
9928
9929 /* Make a clone of DIE as a declaration.  */
9930
9931 static dw_die_ref
9932 clone_as_declaration (dw_die_ref die)
9933 {
9934   dw_die_ref clone;
9935   dw_die_ref decl;
9936   dw_attr_ref a;
9937   unsigned ix;
9938
9939   /* If the DIE is already a declaration, just clone it.  */
9940   if (is_declaration_die (die))
9941     return clone_die (die);
9942
9943   /* If the DIE is a specification, just clone its declaration DIE.  */
9944   decl = get_AT_ref (die, DW_AT_specification);
9945   if (decl != NULL)
9946     return clone_die (decl);
9947
9948   clone = ggc_alloc_cleared_die_node ();
9949   clone->die_tag = die->die_tag;
9950
9951   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9952     {
9953       /* We don't want to copy over all attributes.
9954          For example we don't want DW_AT_byte_size because otherwise we will no
9955          longer have a declaration and GDB will treat it as a definition.  */
9956
9957       switch (a->dw_attr)
9958         {
9959         case DW_AT_artificial:
9960         case DW_AT_containing_type:
9961         case DW_AT_external:
9962         case DW_AT_name:
9963         case DW_AT_type:
9964         case DW_AT_virtuality:
9965         case DW_AT_linkage_name:
9966         case DW_AT_MIPS_linkage_name:
9967           add_dwarf_attr (clone, a);
9968           break;
9969         case DW_AT_byte_size:
9970         default:
9971           break;
9972         }
9973     }
9974
9975   if (die->die_id.die_type_node)
9976     add_AT_die_ref (clone, DW_AT_signature, die);
9977
9978   add_AT_flag (clone, DW_AT_declaration, 1);
9979   return clone;
9980 }
9981
9982 /* Copy the declaration context to the new compile unit DIE.  This includes
9983    any surrounding namespace or type declarations.  If the DIE has an
9984    AT_specification attribute, it also includes attributes and children
9985    attached to the specification.  */
9986
9987 static void
9988 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9989 {
9990   dw_die_ref decl;
9991   dw_die_ref new_decl;
9992
9993   decl = get_AT_ref (die, DW_AT_specification);
9994   if (decl == NULL)
9995     decl = die;
9996   else
9997     {
9998       unsigned ix;
9999       dw_die_ref c;
10000       dw_attr_ref a;
10001
10002       /* Copy the type node pointer from the new DIE to the original
10003          declaration DIE so we can forward references later.  */
10004       decl->die_id.die_type_node = die->die_id.die_type_node;
10005
10006       remove_AT (die, DW_AT_specification);
10007
10008       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10009         {
10010           if (a->dw_attr != DW_AT_name
10011               && a->dw_attr != DW_AT_declaration
10012               && a->dw_attr != DW_AT_external)
10013             add_dwarf_attr (die, a);
10014         }
10015
10016       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10017     }
10018
10019   if (decl->die_parent != NULL
10020       && decl->die_parent->die_tag != DW_TAG_compile_unit
10021       && decl->die_parent->die_tag != DW_TAG_type_unit)
10022     {
10023       new_decl = copy_ancestor_tree (unit, decl, NULL);
10024       if (new_decl != NULL)
10025         {
10026           remove_AT (new_decl, DW_AT_signature);
10027           add_AT_specification (die, new_decl);
10028         }
10029     }
10030 }
10031
10032 /* Generate the skeleton ancestor tree for the given NODE, then clone
10033    the DIE and add the clone into the tree.  */
10034
10035 static void
10036 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10037 {
10038   if (node->new_die != NULL)
10039     return;
10040
10041   node->new_die = clone_as_declaration (node->old_die);
10042
10043   if (node->parent != NULL)
10044     {
10045       generate_skeleton_ancestor_tree (node->parent);
10046       add_child_die (node->parent->new_die, node->new_die);
10047     }
10048 }
10049
10050 /* Generate a skeleton tree of DIEs containing any declarations that are
10051    found in the original tree.  We traverse the tree looking for declaration
10052    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10053
10054 static void
10055 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10056 {
10057   skeleton_chain_node node;
10058   dw_die_ref c;
10059   dw_die_ref first;
10060   dw_die_ref prev = NULL;
10061   dw_die_ref next = NULL;
10062
10063   node.parent = parent;
10064
10065   first = c = parent->old_die->die_child;
10066   if (c)
10067     next = c->die_sib;
10068   if (c) do {
10069     if (prev == NULL || prev->die_sib == c)
10070       prev = c;
10071     c = next;
10072     next = (c == first ? NULL : c->die_sib);
10073     node.old_die = c;
10074     node.new_die = NULL;
10075     if (is_declaration_die (c))
10076       {
10077         /* Clone the existing DIE, move the original to the skeleton
10078            tree (which is in the main CU), and put the clone, with
10079            all the original's children, where the original came from.  */
10080         dw_die_ref clone = clone_die (c);
10081         move_all_children (c, clone);
10082
10083         replace_child (c, clone, prev);
10084         generate_skeleton_ancestor_tree (parent);
10085         add_child_die (parent->new_die, c);
10086         node.new_die = c;
10087         c = clone;
10088       }
10089     generate_skeleton_bottom_up (&node);
10090   } while (next != NULL);
10091 }
10092
10093 /* Wrapper function for generate_skeleton_bottom_up.  */
10094
10095 static dw_die_ref
10096 generate_skeleton (dw_die_ref die)
10097 {
10098   skeleton_chain_node node;
10099
10100   node.old_die = die;
10101   node.new_die = NULL;
10102   node.parent = NULL;
10103
10104   /* If this type definition is nested inside another type,
10105      always leave at least a declaration in its place.  */
10106   if (die->die_parent != NULL && is_type_die (die->die_parent))
10107     node.new_die = clone_as_declaration (die);
10108
10109   generate_skeleton_bottom_up (&node);
10110   return node.new_die;
10111 }
10112
10113 /* Remove the DIE from its parent, possibly replacing it with a cloned
10114    declaration.  The original DIE will be moved to a new compile unit
10115    so that existing references to it follow it to the new location.  If
10116    any of the original DIE's descendants is a declaration, we need to
10117    replace the original DIE with a skeleton tree and move the
10118    declarations back into the skeleton tree.  */
10119
10120 static dw_die_ref
10121 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10122 {
10123   dw_die_ref skeleton;
10124
10125   skeleton = generate_skeleton (child);
10126   if (skeleton == NULL)
10127     remove_child_with_prev (child, prev);
10128   else
10129     {
10130       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10131       replace_child (child, skeleton, prev);
10132     }
10133
10134   return skeleton;
10135 }
10136
10137 /* Traverse the DIE and set up additional .debug_types sections for each
10138    type worthy of being placed in a COMDAT section.  */
10139
10140 static void
10141 break_out_comdat_types (dw_die_ref die)
10142 {
10143   dw_die_ref c;
10144   dw_die_ref first;
10145   dw_die_ref prev = NULL;
10146   dw_die_ref next = NULL;
10147   dw_die_ref unit = NULL;
10148
10149   first = c = die->die_child;
10150   if (c)
10151     next = c->die_sib;
10152   if (c) do {
10153     if (prev == NULL || prev->die_sib == c)
10154       prev = c;
10155     c = next;
10156     next = (c == first ? NULL : c->die_sib);
10157     if (should_move_die_to_comdat (c))
10158       {
10159         dw_die_ref replacement;
10160         comdat_type_node_ref type_node;
10161
10162         /* Create a new type unit DIE as the root for the new tree, and
10163            add it to the list of comdat types.  */
10164         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10165         add_AT_unsigned (unit, DW_AT_language,
10166                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10167         type_node = ggc_alloc_cleared_comdat_type_node ();
10168         type_node->root_die = unit;
10169         type_node->next = comdat_type_list;
10170         comdat_type_list = type_node;
10171
10172         /* Generate the type signature.  */
10173         generate_type_signature (c, type_node);
10174
10175         /* Copy the declaration context, attributes, and children of the
10176            declaration into the new compile unit DIE.  */
10177         copy_declaration_context (unit, c);
10178
10179         /* Remove this DIE from the main CU.  */
10180         replacement = remove_child_or_replace_with_skeleton (c, prev);
10181
10182         /* Break out nested types into their own type units.  */
10183         break_out_comdat_types (c);
10184
10185         /* Add the DIE to the new compunit.  */
10186         add_child_die (unit, c);
10187
10188         if (replacement != NULL)
10189           c = replacement;
10190       }
10191     else if (c->die_tag == DW_TAG_namespace
10192              || c->die_tag == DW_TAG_class_type
10193              || c->die_tag == DW_TAG_structure_type
10194              || c->die_tag == DW_TAG_union_type)
10195       {
10196         /* Look for nested types that can be broken out.  */
10197         break_out_comdat_types (c);
10198       }
10199   } while (next != NULL);
10200 }
10201
10202 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10203
10204 struct decl_table_entry
10205 {
10206   dw_die_ref orig;
10207   dw_die_ref copy;
10208 };
10209
10210 /* Routines to manipulate hash table of copied declarations.  */
10211
10212 static hashval_t
10213 htab_decl_hash (const void *of)
10214 {
10215   const struct decl_table_entry *const entry =
10216     (const struct decl_table_entry *) of;
10217
10218   return htab_hash_pointer (entry->orig);
10219 }
10220
10221 static int
10222 htab_decl_eq (const void *of1, const void *of2)
10223 {
10224   const struct decl_table_entry *const entry1 =
10225     (const struct decl_table_entry *) of1;
10226   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10227
10228   return entry1->orig == entry2;
10229 }
10230
10231 static void
10232 htab_decl_del (void *what)
10233 {
10234   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10235
10236   free (entry);
10237 }
10238
10239 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10240    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10241    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10242    to check if the ancestor has already been copied into UNIT.  */
10243
10244 static dw_die_ref
10245 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10246 {
10247   dw_die_ref parent = die->die_parent;
10248   dw_die_ref new_parent = unit;
10249   dw_die_ref copy;
10250   void **slot = NULL;
10251   struct decl_table_entry *entry = NULL;
10252
10253   if (decl_table)
10254     {
10255       /* Check if the entry has already been copied to UNIT.  */
10256       slot = htab_find_slot_with_hash (decl_table, die,
10257                                        htab_hash_pointer (die), INSERT);
10258       if (*slot != HTAB_EMPTY_ENTRY)
10259         {
10260           entry = (struct decl_table_entry *) *slot;
10261           return entry->copy;
10262         }
10263
10264       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10265       entry = XCNEW (struct decl_table_entry);
10266       entry->orig = die;
10267       entry->copy = NULL;
10268       *slot = entry;
10269     }
10270
10271   if (parent != NULL)
10272     {
10273       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10274       if (spec != NULL)
10275         parent = spec;
10276       if (parent->die_tag != DW_TAG_compile_unit
10277           && parent->die_tag != DW_TAG_type_unit)
10278         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10279     }
10280
10281   copy = clone_as_declaration (die);
10282   add_child_die (new_parent, copy);
10283
10284   if (decl_table != NULL)
10285     {
10286       /* Record the pointer to the copy.  */
10287       entry->copy = copy;
10288     }
10289
10290   return copy;
10291 }
10292
10293 /* Walk the DIE and its children, looking for references to incomplete
10294    or trivial types that are unmarked (i.e., that are not in the current
10295    type_unit).  */
10296
10297 static void
10298 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10299 {
10300   dw_die_ref c;
10301   dw_attr_ref a;
10302   unsigned ix;
10303
10304   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10305     {
10306       if (AT_class (a) == dw_val_class_die_ref)
10307         {
10308           dw_die_ref targ = AT_ref (a);
10309           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10310           void **slot;
10311           struct decl_table_entry *entry;
10312
10313           if (targ->die_mark != 0 || type_node != NULL)
10314             continue;
10315
10316           slot = htab_find_slot_with_hash (decl_table, targ,
10317                                            htab_hash_pointer (targ), INSERT);
10318
10319           if (*slot != HTAB_EMPTY_ENTRY)
10320             {
10321               /* TARG has already been copied, so we just need to
10322                  modify the reference to point to the copy.  */
10323               entry = (struct decl_table_entry *) *slot;
10324               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10325             }
10326           else
10327             {
10328               dw_die_ref parent = unit;
10329               dw_die_ref copy = clone_tree (targ);
10330
10331               /* Make sure the cloned tree is marked as part of the
10332                  type unit.  */
10333               mark_dies (copy);
10334
10335               /* Record in DECL_TABLE that TARG has been copied.
10336                  Need to do this now, before the recursive call,
10337                  because DECL_TABLE may be expanded and SLOT
10338                  would no longer be a valid pointer.  */
10339               entry = XCNEW (struct decl_table_entry);
10340               entry->orig = targ;
10341               entry->copy = copy;
10342               *slot = entry;
10343
10344               /* If TARG has surrounding context, copy its ancestor tree
10345                  into the new type unit.  */
10346               if (targ->die_parent != NULL
10347                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10348                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10349                 parent = copy_ancestor_tree (unit, targ->die_parent,
10350                                              decl_table);
10351
10352               add_child_die (parent, copy);
10353               a->dw_attr_val.v.val_die_ref.die = copy;
10354
10355               /* Make sure the newly-copied DIE is walked.  If it was
10356                  installed in a previously-added context, it won't
10357                  get visited otherwise.  */
10358               if (parent != unit)
10359                 {
10360                   /* Find the highest point of the newly-added tree,
10361                      mark each node along the way, and walk from there.  */
10362                   parent->die_mark = 1;
10363                   while (parent->die_parent
10364                          && parent->die_parent->die_mark == 0)
10365                     {
10366                       parent = parent->die_parent;
10367                       parent->die_mark = 1;
10368                     }
10369                   copy_decls_walk (unit, parent, decl_table);
10370                 }
10371             }
10372         }
10373     }
10374
10375   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10376 }
10377
10378 /* Copy declarations for "unworthy" types into the new comdat section.
10379    Incomplete types, modified types, and certain other types aren't broken
10380    out into comdat sections of their own, so they don't have a signature,
10381    and we need to copy the declaration into the same section so that we
10382    don't have an external reference.  */
10383
10384 static void
10385 copy_decls_for_unworthy_types (dw_die_ref unit)
10386 {
10387   htab_t decl_table;
10388
10389   mark_dies (unit);
10390   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10391   copy_decls_walk (unit, unit, decl_table);
10392   htab_delete (decl_table);
10393   unmark_dies (unit);
10394 }
10395
10396 /* Traverse the DIE and add a sibling attribute if it may have the
10397    effect of speeding up access to siblings.  To save some space,
10398    avoid generating sibling attributes for DIE's without children.  */
10399
10400 static void
10401 add_sibling_attributes (dw_die_ref die)
10402 {
10403   dw_die_ref c;
10404
10405   if (! die->die_child)
10406     return;
10407
10408   if (die->die_parent && die != die->die_parent->die_child)
10409     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10410
10411   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10412 }
10413
10414 /* Output all location lists for the DIE and its children.  */
10415
10416 static void
10417 output_location_lists (dw_die_ref die)
10418 {
10419   dw_die_ref c;
10420   dw_attr_ref a;
10421   unsigned ix;
10422
10423   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10424     if (AT_class (a) == dw_val_class_loc_list)
10425       output_loc_list (AT_loc_list (a));
10426
10427   FOR_EACH_CHILD (die, c, output_location_lists (c));
10428 }
10429
10430 /* The format of each DIE (and its attribute value pairs) is encoded in an
10431    abbreviation table.  This routine builds the abbreviation table and assigns
10432    a unique abbreviation id for each abbreviation entry.  The children of each
10433    die are visited recursively.  */
10434
10435 static void
10436 build_abbrev_table (dw_die_ref die)
10437 {
10438   unsigned long abbrev_id;
10439   unsigned int n_alloc;
10440   dw_die_ref c;
10441   dw_attr_ref a;
10442   unsigned ix;
10443
10444   /* Scan the DIE references, and mark as external any that refer to
10445      DIEs from other CUs (i.e. those which are not marked).  */
10446   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10447     if (AT_class (a) == dw_val_class_die_ref
10448         && AT_ref (a)->die_mark == 0)
10449       {
10450         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10451         set_AT_ref_external (a, 1);
10452       }
10453
10454   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10455     {
10456       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10457       dw_attr_ref die_a, abbrev_a;
10458       unsigned ix;
10459       bool ok = true;
10460
10461       if (abbrev->die_tag != die->die_tag)
10462         continue;
10463       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10464         continue;
10465
10466       if (VEC_length (dw_attr_node, abbrev->die_attr)
10467           != VEC_length (dw_attr_node, die->die_attr))
10468         continue;
10469
10470       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10471         {
10472           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10473           if ((abbrev_a->dw_attr != die_a->dw_attr)
10474               || (value_format (abbrev_a) != value_format (die_a)))
10475             {
10476               ok = false;
10477               break;
10478             }
10479         }
10480       if (ok)
10481         break;
10482     }
10483
10484   if (abbrev_id >= abbrev_die_table_in_use)
10485     {
10486       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10487         {
10488           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10489           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10490                                             n_alloc);
10491
10492           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10493                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10494           abbrev_die_table_allocated = n_alloc;
10495         }
10496
10497       ++abbrev_die_table_in_use;
10498       abbrev_die_table[abbrev_id] = die;
10499     }
10500
10501   die->die_abbrev = abbrev_id;
10502   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10503 }
10504 \f
10505 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10506
10507 static int
10508 constant_size (unsigned HOST_WIDE_INT value)
10509 {
10510   int log;
10511
10512   if (value == 0)
10513     log = 0;
10514   else
10515     log = floor_log2 (value);
10516
10517   log = log / 8;
10518   log = 1 << (floor_log2 (log) + 1);
10519
10520   return log;
10521 }
10522
10523 /* Return the size of a DIE as it is represented in the
10524    .debug_info section.  */
10525
10526 static unsigned long
10527 size_of_die (dw_die_ref die)
10528 {
10529   unsigned long size = 0;
10530   dw_attr_ref a;
10531   unsigned ix;
10532
10533   size += size_of_uleb128 (die->die_abbrev);
10534   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10535     {
10536       switch (AT_class (a))
10537         {
10538         case dw_val_class_addr:
10539           size += DWARF2_ADDR_SIZE;
10540           break;
10541         case dw_val_class_offset:
10542           size += DWARF_OFFSET_SIZE;
10543           break;
10544         case dw_val_class_loc:
10545           {
10546             unsigned long lsize = size_of_locs (AT_loc (a));
10547
10548             /* Block length.  */
10549             if (dwarf_version >= 4)
10550               size += size_of_uleb128 (lsize);
10551             else
10552               size += constant_size (lsize);
10553             size += lsize;
10554           }
10555           break;
10556         case dw_val_class_loc_list:
10557           size += DWARF_OFFSET_SIZE;
10558           break;
10559         case dw_val_class_range_list:
10560           size += DWARF_OFFSET_SIZE;
10561           break;
10562         case dw_val_class_const:
10563           size += size_of_sleb128 (AT_int (a));
10564           break;
10565         case dw_val_class_unsigned_const:
10566           size += constant_size (AT_unsigned (a));
10567           break;
10568         case dw_val_class_const_double:
10569           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10570           if (HOST_BITS_PER_WIDE_INT >= 64)
10571             size++; /* block */
10572           break;
10573         case dw_val_class_vec:
10574           size += constant_size (a->dw_attr_val.v.val_vec.length
10575                                  * a->dw_attr_val.v.val_vec.elt_size)
10576                   + a->dw_attr_val.v.val_vec.length
10577                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10578           break;
10579         case dw_val_class_flag:
10580           if (dwarf_version >= 4)
10581             /* Currently all add_AT_flag calls pass in 1 as last argument,
10582                so DW_FORM_flag_present can be used.  If that ever changes,
10583                we'll need to use DW_FORM_flag and have some optimization
10584                in build_abbrev_table that will change those to
10585                DW_FORM_flag_present if it is set to 1 in all DIEs using
10586                the same abbrev entry.  */
10587             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10588           else
10589             size += 1;
10590           break;
10591         case dw_val_class_die_ref:
10592           if (AT_ref_external (a))
10593             {
10594               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10595                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10596                  is sized by target address length, whereas in DWARF3
10597                  it's always sized as an offset.  */
10598               if (dwarf_version >= 4)
10599                 size += DWARF_TYPE_SIGNATURE_SIZE;
10600               else if (dwarf_version == 2)
10601                 size += DWARF2_ADDR_SIZE;
10602               else
10603                 size += DWARF_OFFSET_SIZE;
10604             }
10605           else
10606             size += DWARF_OFFSET_SIZE;
10607           break;
10608         case dw_val_class_fde_ref:
10609           size += DWARF_OFFSET_SIZE;
10610           break;
10611         case dw_val_class_lbl_id:
10612           size += DWARF2_ADDR_SIZE;
10613           break;
10614         case dw_val_class_lineptr:
10615         case dw_val_class_macptr:
10616           size += DWARF_OFFSET_SIZE;
10617           break;
10618         case dw_val_class_str:
10619           if (AT_string_form (a) == DW_FORM_strp)
10620             size += DWARF_OFFSET_SIZE;
10621           else
10622             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10623           break;
10624         case dw_val_class_file:
10625           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10626           break;
10627         case dw_val_class_data8:
10628           size += 8;
10629           break;
10630         case dw_val_class_vms_delta:
10631           size += DWARF_OFFSET_SIZE;
10632           break;
10633         default:
10634           gcc_unreachable ();
10635         }
10636     }
10637
10638   return size;
10639 }
10640
10641 /* Size the debugging information associated with a given DIE.  Visits the
10642    DIE's children recursively.  Updates the global variable next_die_offset, on
10643    each time through.  Uses the current value of next_die_offset to update the
10644    die_offset field in each DIE.  */
10645
10646 static void
10647 calc_die_sizes (dw_die_ref die)
10648 {
10649   dw_die_ref c;
10650
10651   die->die_offset = next_die_offset;
10652   next_die_offset += size_of_die (die);
10653
10654   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10655
10656   if (die->die_child != NULL)
10657     /* Count the null byte used to terminate sibling lists.  */
10658     next_die_offset += 1;
10659 }
10660
10661 /* Set the marks for a die and its children.  We do this so
10662    that we know whether or not a reference needs to use FORM_ref_addr; only
10663    DIEs in the same CU will be marked.  We used to clear out the offset
10664    and use that as the flag, but ran into ordering problems.  */
10665
10666 static void
10667 mark_dies (dw_die_ref die)
10668 {
10669   dw_die_ref c;
10670
10671   gcc_assert (!die->die_mark);
10672
10673   die->die_mark = 1;
10674   FOR_EACH_CHILD (die, c, mark_dies (c));
10675 }
10676
10677 /* Clear the marks for a die and its children.  */
10678
10679 static void
10680 unmark_dies (dw_die_ref die)
10681 {
10682   dw_die_ref c;
10683
10684   if (dwarf_version < 4)
10685     gcc_assert (die->die_mark);
10686
10687   die->die_mark = 0;
10688   FOR_EACH_CHILD (die, c, unmark_dies (c));
10689 }
10690
10691 /* Clear the marks for a die, its children and referred dies.  */
10692
10693 static void
10694 unmark_all_dies (dw_die_ref die)
10695 {
10696   dw_die_ref c;
10697   dw_attr_ref a;
10698   unsigned ix;
10699
10700   if (!die->die_mark)
10701     return;
10702   die->die_mark = 0;
10703
10704   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10705
10706   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10707     if (AT_class (a) == dw_val_class_die_ref)
10708       unmark_all_dies (AT_ref (a));
10709 }
10710
10711 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10712    generated for the compilation unit.  */
10713
10714 static unsigned long
10715 size_of_pubnames (VEC (pubname_entry, gc) * names)
10716 {
10717   unsigned long size;
10718   unsigned i;
10719   pubname_ref p;
10720
10721   size = DWARF_PUBNAMES_HEADER_SIZE;
10722   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10723     if (names != pubtype_table
10724         || p->die->die_offset != 0
10725         || !flag_eliminate_unused_debug_types)
10726       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10727
10728   size += DWARF_OFFSET_SIZE;
10729   return size;
10730 }
10731
10732 /* Return the size of the information in the .debug_aranges section.  */
10733
10734 static unsigned long
10735 size_of_aranges (void)
10736 {
10737   unsigned long size;
10738
10739   size = DWARF_ARANGES_HEADER_SIZE;
10740
10741   /* Count the address/length pair for this compilation unit.  */
10742   if (text_section_used)
10743     size += 2 * DWARF2_ADDR_SIZE;
10744   if (cold_text_section_used)
10745     size += 2 * DWARF2_ADDR_SIZE;
10746   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10747
10748   /* Count the two zero words used to terminated the address range table.  */
10749   size += 2 * DWARF2_ADDR_SIZE;
10750   return size;
10751 }
10752 \f
10753 /* Select the encoding of an attribute value.  */
10754
10755 static enum dwarf_form
10756 value_format (dw_attr_ref a)
10757 {
10758   switch (a->dw_attr_val.val_class)
10759     {
10760     case dw_val_class_addr:
10761       /* Only very few attributes allow DW_FORM_addr.  */
10762       switch (a->dw_attr)
10763         {
10764         case DW_AT_low_pc:
10765         case DW_AT_high_pc:
10766         case DW_AT_entry_pc:
10767         case DW_AT_trampoline:
10768           return DW_FORM_addr;
10769         default:
10770           break;
10771         }
10772       switch (DWARF2_ADDR_SIZE)
10773         {
10774         case 1:
10775           return DW_FORM_data1;
10776         case 2:
10777           return DW_FORM_data2;
10778         case 4:
10779           return DW_FORM_data4;
10780         case 8:
10781           return DW_FORM_data8;
10782         default:
10783           gcc_unreachable ();
10784         }
10785     case dw_val_class_range_list:
10786     case dw_val_class_loc_list:
10787       if (dwarf_version >= 4)
10788         return DW_FORM_sec_offset;
10789       /* FALLTHRU */
10790     case dw_val_class_vms_delta:
10791     case dw_val_class_offset:
10792       switch (DWARF_OFFSET_SIZE)
10793         {
10794         case 4:
10795           return DW_FORM_data4;
10796         case 8:
10797           return DW_FORM_data8;
10798         default:
10799           gcc_unreachable ();
10800         }
10801     case dw_val_class_loc:
10802       if (dwarf_version >= 4)
10803         return DW_FORM_exprloc;
10804       switch (constant_size (size_of_locs (AT_loc (a))))
10805         {
10806         case 1:
10807           return DW_FORM_block1;
10808         case 2:
10809           return DW_FORM_block2;
10810         default:
10811           gcc_unreachable ();
10812         }
10813     case dw_val_class_const:
10814       return DW_FORM_sdata;
10815     case dw_val_class_unsigned_const:
10816       switch (constant_size (AT_unsigned (a)))
10817         {
10818         case 1:
10819           return DW_FORM_data1;
10820         case 2:
10821           return DW_FORM_data2;
10822         case 4:
10823           return DW_FORM_data4;
10824         case 8:
10825           return DW_FORM_data8;
10826         default:
10827           gcc_unreachable ();
10828         }
10829     case dw_val_class_const_double:
10830       switch (HOST_BITS_PER_WIDE_INT)
10831         {
10832         case 8:
10833           return DW_FORM_data2;
10834         case 16:
10835           return DW_FORM_data4;
10836         case 32:
10837           return DW_FORM_data8;
10838         case 64:
10839         default:
10840           return DW_FORM_block1;
10841         }
10842     case dw_val_class_vec:
10843       switch (constant_size (a->dw_attr_val.v.val_vec.length
10844                              * a->dw_attr_val.v.val_vec.elt_size))
10845         {
10846         case 1:
10847           return DW_FORM_block1;
10848         case 2:
10849           return DW_FORM_block2;
10850         case 4:
10851           return DW_FORM_block4;
10852         default:
10853           gcc_unreachable ();
10854         }
10855     case dw_val_class_flag:
10856       if (dwarf_version >= 4)
10857         {
10858           /* Currently all add_AT_flag calls pass in 1 as last argument,
10859              so DW_FORM_flag_present can be used.  If that ever changes,
10860              we'll need to use DW_FORM_flag and have some optimization
10861              in build_abbrev_table that will change those to
10862              DW_FORM_flag_present if it is set to 1 in all DIEs using
10863              the same abbrev entry.  */
10864           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10865           return DW_FORM_flag_present;
10866         }
10867       return DW_FORM_flag;
10868     case dw_val_class_die_ref:
10869       if (AT_ref_external (a))
10870         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10871       else
10872         return DW_FORM_ref;
10873     case dw_val_class_fde_ref:
10874       return DW_FORM_data;
10875     case dw_val_class_lbl_id:
10876       return DW_FORM_addr;
10877     case dw_val_class_lineptr:
10878     case dw_val_class_macptr:
10879       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10880     case dw_val_class_str:
10881       return AT_string_form (a);
10882     case dw_val_class_file:
10883       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10884         {
10885         case 1:
10886           return DW_FORM_data1;
10887         case 2:
10888           return DW_FORM_data2;
10889         case 4:
10890           return DW_FORM_data4;
10891         default:
10892           gcc_unreachable ();
10893         }
10894
10895     case dw_val_class_data8:
10896       return DW_FORM_data8;
10897
10898     default:
10899       gcc_unreachable ();
10900     }
10901 }
10902
10903 /* Output the encoding of an attribute value.  */
10904
10905 static void
10906 output_value_format (dw_attr_ref a)
10907 {
10908   enum dwarf_form form = value_format (a);
10909
10910   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10911 }
10912
10913 /* Output the .debug_abbrev section which defines the DIE abbreviation
10914    table.  */
10915
10916 static void
10917 output_abbrev_section (void)
10918 {
10919   unsigned long abbrev_id;
10920
10921   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10922     {
10923       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10924       unsigned ix;
10925       dw_attr_ref a_attr;
10926
10927       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10928       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10929                                    dwarf_tag_name (abbrev->die_tag));
10930
10931       if (abbrev->die_child != NULL)
10932         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10933       else
10934         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10935
10936       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10937            ix++)
10938         {
10939           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10940                                        dwarf_attr_name (a_attr->dw_attr));
10941           output_value_format (a_attr);
10942         }
10943
10944       dw2_asm_output_data (1, 0, NULL);
10945       dw2_asm_output_data (1, 0, NULL);
10946     }
10947
10948   /* Terminate the table.  */
10949   dw2_asm_output_data (1, 0, NULL);
10950 }
10951
10952 /* Output a symbol we can use to refer to this DIE from another CU.  */
10953
10954 static inline void
10955 output_die_symbol (dw_die_ref die)
10956 {
10957   char *sym = die->die_id.die_symbol;
10958
10959   if (sym == 0)
10960     return;
10961
10962   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10963     /* We make these global, not weak; if the target doesn't support
10964        .linkonce, it doesn't support combining the sections, so debugging
10965        will break.  */
10966     targetm.asm_out.globalize_label (asm_out_file, sym);
10967
10968   ASM_OUTPUT_LABEL (asm_out_file, sym);
10969 }
10970
10971 /* Return a new location list, given the begin and end range, and the
10972    expression.  */
10973
10974 static inline dw_loc_list_ref
10975 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10976               const char *section)
10977 {
10978   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10979
10980   retlist->begin = begin;
10981   retlist->end = end;
10982   retlist->expr = expr;
10983   retlist->section = section;
10984
10985   return retlist;
10986 }
10987
10988 /* Generate a new internal symbol for this location list node, if it
10989    hasn't got one yet.  */
10990
10991 static inline void
10992 gen_llsym (dw_loc_list_ref list)
10993 {
10994   gcc_assert (!list->ll_symbol);
10995   list->ll_symbol = gen_internal_sym ("LLST");
10996 }
10997
10998 /* Output the location list given to us.  */
10999
11000 static void
11001 output_loc_list (dw_loc_list_ref list_head)
11002 {
11003   dw_loc_list_ref curr = list_head;
11004
11005   if (list_head->emitted)
11006     return;
11007   list_head->emitted = true;
11008
11009   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11010
11011   /* Walk the location list, and output each range + expression.  */
11012   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11013     {
11014       unsigned long size;
11015       /* Don't output an entry that starts and ends at the same address.  */
11016       if (strcmp (curr->begin, curr->end) == 0)
11017         continue;
11018       if (!have_multiple_function_sections)
11019         {
11020           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11021                                 "Location list begin address (%s)",
11022                                 list_head->ll_symbol);
11023           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11024                                 "Location list end address (%s)",
11025                                 list_head->ll_symbol);
11026         }
11027       else
11028         {
11029           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11030                                "Location list begin address (%s)",
11031                                list_head->ll_symbol);
11032           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11033                                "Location list end address (%s)",
11034                                list_head->ll_symbol);
11035         }
11036       size = size_of_locs (curr->expr);
11037
11038       /* Output the block length for this list of location operations.  */
11039       gcc_assert (size <= 0xffff);
11040       dw2_asm_output_data (2, size, "%s", "Location expression size");
11041
11042       output_loc_sequence (curr->expr);
11043     }
11044
11045   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11046                        "Location list terminator begin (%s)",
11047                        list_head->ll_symbol);
11048   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11049                        "Location list terminator end (%s)",
11050                        list_head->ll_symbol);
11051 }
11052
11053 /* Output a type signature.  */
11054
11055 static inline void
11056 output_signature (const char *sig, const char *name)
11057 {
11058   int i;
11059
11060   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11061     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11062 }
11063
11064 /* Output the DIE and its attributes.  Called recursively to generate
11065    the definitions of each child DIE.  */
11066
11067 static void
11068 output_die (dw_die_ref die)
11069 {
11070   dw_attr_ref a;
11071   dw_die_ref c;
11072   unsigned long size;
11073   unsigned ix;
11074
11075   /* If someone in another CU might refer to us, set up a symbol for
11076      them to point to.  */
11077   if (dwarf_version < 4 && die->die_id.die_symbol)
11078     output_die_symbol (die);
11079
11080   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11081                                (unsigned long)die->die_offset,
11082                                dwarf_tag_name (die->die_tag));
11083
11084   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11085     {
11086       const char *name = dwarf_attr_name (a->dw_attr);
11087
11088       switch (AT_class (a))
11089         {
11090         case dw_val_class_addr:
11091           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11092           break;
11093
11094         case dw_val_class_offset:
11095           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11096                                "%s", name);
11097           break;
11098
11099         case dw_val_class_range_list:
11100           {
11101             char *p = strchr (ranges_section_label, '\0');
11102
11103             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11104                      a->dw_attr_val.v.val_offset);
11105             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11106                                    debug_ranges_section, "%s", name);
11107             *p = '\0';
11108           }
11109           break;
11110
11111         case dw_val_class_loc:
11112           size = size_of_locs (AT_loc (a));
11113
11114           /* Output the block length for this list of location operations.  */
11115           if (dwarf_version >= 4)
11116             dw2_asm_output_data_uleb128 (size, "%s", name);
11117           else
11118             dw2_asm_output_data (constant_size (size), size, "%s", name);
11119
11120           output_loc_sequence (AT_loc (a));
11121           break;
11122
11123         case dw_val_class_const:
11124           /* ??? It would be slightly more efficient to use a scheme like is
11125              used for unsigned constants below, but gdb 4.x does not sign
11126              extend.  Gdb 5.x does sign extend.  */
11127           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11128           break;
11129
11130         case dw_val_class_unsigned_const:
11131           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11132                                AT_unsigned (a), "%s", name);
11133           break;
11134
11135         case dw_val_class_const_double:
11136           {
11137             unsigned HOST_WIDE_INT first, second;
11138
11139             if (HOST_BITS_PER_WIDE_INT >= 64)
11140               dw2_asm_output_data (1,
11141                                    2 * HOST_BITS_PER_WIDE_INT
11142                                    / HOST_BITS_PER_CHAR,
11143                                    NULL);
11144
11145             if (WORDS_BIG_ENDIAN)
11146               {
11147                 first = a->dw_attr_val.v.val_double.high;
11148                 second = a->dw_attr_val.v.val_double.low;
11149               }
11150             else
11151               {
11152                 first = a->dw_attr_val.v.val_double.low;
11153                 second = a->dw_attr_val.v.val_double.high;
11154               }
11155
11156             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11157                                  first, name);
11158             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11159                                  second, NULL);
11160           }
11161           break;
11162
11163         case dw_val_class_vec:
11164           {
11165             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11166             unsigned int len = a->dw_attr_val.v.val_vec.length;
11167             unsigned int i;
11168             unsigned char *p;
11169
11170             dw2_asm_output_data (constant_size (len * elt_size),
11171                                  len * elt_size, "%s", name);
11172             if (elt_size > sizeof (HOST_WIDE_INT))
11173               {
11174                 elt_size /= 2;
11175                 len *= 2;
11176               }
11177             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11178                  i < len;
11179                  i++, p += elt_size)
11180               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11181                                    "fp or vector constant word %u", i);
11182             break;
11183           }
11184
11185         case dw_val_class_flag:
11186           if (dwarf_version >= 4)
11187             {
11188               /* Currently all add_AT_flag calls pass in 1 as last argument,
11189                  so DW_FORM_flag_present can be used.  If that ever changes,
11190                  we'll need to use DW_FORM_flag and have some optimization
11191                  in build_abbrev_table that will change those to
11192                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11193                  the same abbrev entry.  */
11194               gcc_assert (AT_flag (a) == 1);
11195               if (flag_debug_asm)
11196                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11197                          ASM_COMMENT_START, name);
11198               break;
11199             }
11200           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11201           break;
11202
11203         case dw_val_class_loc_list:
11204           {
11205             char *sym = AT_loc_list (a)->ll_symbol;
11206
11207             gcc_assert (sym);
11208             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11209                                    "%s", name);
11210           }
11211           break;
11212
11213         case dw_val_class_die_ref:
11214           if (AT_ref_external (a))
11215             {
11216               if (dwarf_version >= 4)
11217                 {
11218                   comdat_type_node_ref type_node =
11219                     AT_ref (a)->die_id.die_type_node;
11220
11221                   gcc_assert (type_node);
11222                   output_signature (type_node->signature, name);
11223                 }
11224               else
11225                 {
11226                   char *sym = AT_ref (a)->die_id.die_symbol;
11227                   int size;
11228
11229                   gcc_assert (sym);
11230                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11231                      length, whereas in DWARF3 it's always sized as an
11232                      offset.  */
11233                   if (dwarf_version == 2)
11234                     size = DWARF2_ADDR_SIZE;
11235                   else
11236                     size = DWARF_OFFSET_SIZE;
11237                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11238                                          name);
11239                 }
11240             }
11241           else
11242             {
11243               gcc_assert (AT_ref (a)->die_offset);
11244               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11245                                    "%s", name);
11246             }
11247           break;
11248
11249         case dw_val_class_fde_ref:
11250           {
11251             char l1[20];
11252
11253             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11254                                          a->dw_attr_val.v.val_fde_index * 2);
11255             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11256                                    "%s", name);
11257           }
11258           break;
11259
11260         case dw_val_class_vms_delta:
11261           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11262                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11263                                     "%s", name);
11264           break;
11265
11266         case dw_val_class_lbl_id:
11267           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11268           break;
11269
11270         case dw_val_class_lineptr:
11271           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11272                                  debug_line_section, "%s", name);
11273           break;
11274
11275         case dw_val_class_macptr:
11276           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11277                                  debug_macinfo_section, "%s", name);
11278           break;
11279
11280         case dw_val_class_str:
11281           if (AT_string_form (a) == DW_FORM_strp)
11282             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11283                                    a->dw_attr_val.v.val_str->label,
11284                                    debug_str_section,
11285                                    "%s: \"%s\"", name, AT_string (a));
11286           else
11287             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11288           break;
11289
11290         case dw_val_class_file:
11291           {
11292             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11293
11294             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11295                                  a->dw_attr_val.v.val_file->filename);
11296             break;
11297           }
11298
11299         case dw_val_class_data8:
11300           {
11301             int i;
11302
11303             for (i = 0; i < 8; i++)
11304               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11305                                    i == 0 ? "%s" : NULL, name);
11306             break;
11307           }
11308
11309         default:
11310           gcc_unreachable ();
11311         }
11312     }
11313
11314   FOR_EACH_CHILD (die, c, output_die (c));
11315
11316   /* Add null byte to terminate sibling list.  */
11317   if (die->die_child != NULL)
11318     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11319                          (unsigned long) die->die_offset);
11320 }
11321
11322 /* Output the compilation unit that appears at the beginning of the
11323    .debug_info section, and precedes the DIE descriptions.  */
11324
11325 static void
11326 output_compilation_unit_header (void)
11327 {
11328   int ver = dwarf_version;
11329
11330   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11331     dw2_asm_output_data (4, 0xffffffff,
11332       "Initial length escape value indicating 64-bit DWARF extension");
11333   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11334                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11335                        "Length of Compilation Unit Info");
11336   dw2_asm_output_data (2, ver, "DWARF version number");
11337   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11338                          debug_abbrev_section,
11339                          "Offset Into Abbrev. Section");
11340   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11341 }
11342
11343 /* Output the compilation unit DIE and its children.  */
11344
11345 static void
11346 output_comp_unit (dw_die_ref die, int output_if_empty)
11347 {
11348   const char *secname;
11349   char *oldsym, *tmp;
11350
11351   /* Unless we are outputting main CU, we may throw away empty ones.  */
11352   if (!output_if_empty && die->die_child == NULL)
11353     return;
11354
11355   /* Even if there are no children of this DIE, we must output the information
11356      about the compilation unit.  Otherwise, on an empty translation unit, we
11357      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11358      will then complain when examining the file.  First mark all the DIEs in
11359      this CU so we know which get local refs.  */
11360   mark_dies (die);
11361
11362   build_abbrev_table (die);
11363
11364   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11365   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11366   calc_die_sizes (die);
11367
11368   oldsym = die->die_id.die_symbol;
11369   if (oldsym)
11370     {
11371       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11372
11373       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11374       secname = tmp;
11375       die->die_id.die_symbol = NULL;
11376       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11377     }
11378   else
11379     {
11380       switch_to_section (debug_info_section);
11381       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11382       info_section_emitted = true;
11383     }
11384
11385   /* Output debugging information.  */
11386   output_compilation_unit_header ();
11387   output_die (die);
11388
11389   /* Leave the marks on the main CU, so we can check them in
11390      output_pubnames.  */
11391   if (oldsym)
11392     {
11393       unmark_dies (die);
11394       die->die_id.die_symbol = oldsym;
11395     }
11396 }
11397
11398 /* Output a comdat type unit DIE and its children.  */
11399
11400 static void
11401 output_comdat_type_unit (comdat_type_node *node)
11402 {
11403   const char *secname;
11404   char *tmp;
11405   int i;
11406 #if defined (OBJECT_FORMAT_ELF)
11407   tree comdat_key;
11408 #endif
11409
11410   /* First mark all the DIEs in this CU so we know which get local refs.  */
11411   mark_dies (node->root_die);
11412
11413   build_abbrev_table (node->root_die);
11414
11415   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11416   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11417   calc_die_sizes (node->root_die);
11418
11419 #if defined (OBJECT_FORMAT_ELF)
11420   secname = ".debug_types";
11421   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11422   sprintf (tmp, "wt.");
11423   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11424     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11425   comdat_key = get_identifier (tmp);
11426   targetm.asm_out.named_section (secname,
11427                                  SECTION_DEBUG | SECTION_LINKONCE,
11428                                  comdat_key);
11429 #else
11430   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11431   sprintf (tmp, ".gnu.linkonce.wt.");
11432   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11433     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11434   secname = tmp;
11435   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11436 #endif
11437
11438   /* Output debugging information.  */
11439   output_compilation_unit_header ();
11440   output_signature (node->signature, "Type Signature");
11441   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11442                        "Offset to Type DIE");
11443   output_die (node->root_die);
11444
11445   unmark_dies (node->root_die);
11446 }
11447
11448 /* Return the DWARF2/3 pubname associated with a decl.  */
11449
11450 static const char *
11451 dwarf2_name (tree decl, int scope)
11452 {
11453   if (DECL_NAMELESS (decl))
11454     return NULL;
11455   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11456 }
11457
11458 /* Add a new entry to .debug_pubnames if appropriate.  */
11459
11460 static void
11461 add_pubname_string (const char *str, dw_die_ref die)
11462 {
11463   if (targetm.want_debug_pub_sections)
11464     {
11465       pubname_entry e;
11466
11467       e.die = die;
11468       e.name = xstrdup (str);
11469       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11470     }
11471 }
11472
11473 static void
11474 add_pubname (tree decl, dw_die_ref die)
11475 {
11476   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11477     {
11478       const char *name = dwarf2_name (decl, 1);
11479       if (name)
11480         add_pubname_string (name, die);
11481     }
11482 }
11483
11484 /* Add a new entry to .debug_pubtypes if appropriate.  */
11485
11486 static void
11487 add_pubtype (tree decl, dw_die_ref die)
11488 {
11489   pubname_entry e;
11490
11491   if (!targetm.want_debug_pub_sections)
11492     return;
11493
11494   e.name = NULL;
11495   if ((TREE_PUBLIC (decl)
11496        || is_cu_die (die->die_parent))
11497       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11498     {
11499       e.die = die;
11500       if (TYPE_P (decl))
11501         {
11502           if (TYPE_NAME (decl))
11503             {
11504               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11505                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11506               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11507                        && DECL_NAME (TYPE_NAME (decl)))
11508                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11509               else
11510                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11511             }
11512         }
11513       else
11514         {
11515           e.name = dwarf2_name (decl, 1);
11516           if (e.name)
11517             e.name = xstrdup (e.name);
11518         }
11519
11520       /* If we don't have a name for the type, there's no point in adding
11521          it to the table.  */
11522       if (e.name && e.name[0] != '\0')
11523         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11524     }
11525 }
11526
11527 /* Output the public names table used to speed up access to externally
11528    visible names; or the public types table used to find type definitions.  */
11529
11530 static void
11531 output_pubnames (VEC (pubname_entry, gc) * names)
11532 {
11533   unsigned i;
11534   unsigned long pubnames_length = size_of_pubnames (names);
11535   pubname_ref pub;
11536
11537   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11538     dw2_asm_output_data (4, 0xffffffff,
11539       "Initial length escape value indicating 64-bit DWARF extension");
11540   if (names == pubname_table)
11541     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11542                          "Length of Public Names Info");
11543   else
11544     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11545                          "Length of Public Type Names Info");
11546   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11547   dw2_asm_output_data (2, 2, "DWARF Version");
11548   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11549                          debug_info_section,
11550                          "Offset of Compilation Unit Info");
11551   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11552                        "Compilation Unit Length");
11553
11554   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11555     {
11556       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11557       if (names == pubname_table)
11558         gcc_assert (pub->die->die_mark);
11559
11560       if (names != pubtype_table
11561           || pub->die->die_offset != 0
11562           || !flag_eliminate_unused_debug_types)
11563         {
11564           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11565                                "DIE offset");
11566
11567           dw2_asm_output_nstring (pub->name, -1, "external name");
11568         }
11569     }
11570
11571   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11572 }
11573
11574 /* Add a new entry to .debug_aranges if appropriate.  */
11575
11576 static void
11577 add_arange (tree decl, dw_die_ref die)
11578 {
11579   if (! DECL_SECTION_NAME (decl))
11580     return;
11581
11582   if (arange_table_in_use == arange_table_allocated)
11583     {
11584       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11585       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11586                                     arange_table_allocated);
11587       memset (arange_table + arange_table_in_use, 0,
11588               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11589     }
11590
11591   arange_table[arange_table_in_use++] = die;
11592 }
11593
11594 /* Output the information that goes into the .debug_aranges table.
11595    Namely, define the beginning and ending address range of the
11596    text section generated for this compilation unit.  */
11597
11598 static void
11599 output_aranges (void)
11600 {
11601   unsigned i;
11602   unsigned long aranges_length = size_of_aranges ();
11603
11604   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11605     dw2_asm_output_data (4, 0xffffffff,
11606       "Initial length escape value indicating 64-bit DWARF extension");
11607   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11608                        "Length of Address Ranges Info");
11609   /* Version number for aranges is still 2, even in DWARF3.  */
11610   dw2_asm_output_data (2, 2, "DWARF Version");
11611   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11612                          debug_info_section,
11613                          "Offset of Compilation Unit Info");
11614   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11615   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11616
11617   /* We need to align to twice the pointer size here.  */
11618   if (DWARF_ARANGES_PAD_SIZE)
11619     {
11620       /* Pad using a 2 byte words so that padding is correct for any
11621          pointer size.  */
11622       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11623                            2 * DWARF2_ADDR_SIZE);
11624       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11625         dw2_asm_output_data (2, 0, NULL);
11626     }
11627
11628   /* It is necessary not to output these entries if the sections were
11629      not used; if the sections were not used, the length will be 0 and
11630      the address may end up as 0 if the section is discarded by ld
11631      --gc-sections, leaving an invalid (0, 0) entry that can be
11632      confused with the terminator.  */
11633   if (text_section_used)
11634     {
11635       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11636       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11637                             text_section_label, "Length");
11638     }
11639   if (cold_text_section_used)
11640     {
11641       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11642                            "Address");
11643       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11644                             cold_text_section_label, "Length");
11645     }
11646
11647   for (i = 0; i < arange_table_in_use; i++)
11648     {
11649       dw_die_ref die = arange_table[i];
11650
11651       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11652       gcc_assert (die->die_mark);
11653
11654       if (die->die_tag == DW_TAG_subprogram)
11655         {
11656           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11657                                "Address");
11658           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11659                                 get_AT_low_pc (die), "Length");
11660         }
11661       else
11662         {
11663           /* A static variable; extract the symbol from DW_AT_location.
11664              Note that this code isn't currently hit, as we only emit
11665              aranges for functions (jason 9/23/99).  */
11666           dw_attr_ref a = get_AT (die, DW_AT_location);
11667           dw_loc_descr_ref loc;
11668
11669           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11670
11671           loc = AT_loc (a);
11672           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11673
11674           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11675                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11676           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11677                                get_AT_unsigned (die, DW_AT_byte_size),
11678                                "Length");
11679         }
11680     }
11681
11682   /* Output the terminator words.  */
11683   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11684   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11685 }
11686
11687 /* Add a new entry to .debug_ranges.  Return the offset at which it
11688    was placed.  */
11689
11690 static unsigned int
11691 add_ranges_num (int num)
11692 {
11693   unsigned int in_use = ranges_table_in_use;
11694
11695   if (in_use == ranges_table_allocated)
11696     {
11697       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11698       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11699                                     ranges_table_allocated);
11700       memset (ranges_table + ranges_table_in_use, 0,
11701               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11702     }
11703
11704   ranges_table[in_use].num = num;
11705   ranges_table_in_use = in_use + 1;
11706
11707   return in_use * 2 * DWARF2_ADDR_SIZE;
11708 }
11709
11710 /* Add a new entry to .debug_ranges corresponding to a block, or a
11711    range terminator if BLOCK is NULL.  */
11712
11713 static unsigned int
11714 add_ranges (const_tree block)
11715 {
11716   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11717 }
11718
11719 /* Add a new entry to .debug_ranges corresponding to a pair of
11720    labels.  */
11721
11722 static void
11723 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11724                       bool *added)
11725 {
11726   unsigned int in_use = ranges_by_label_in_use;
11727   unsigned int offset;
11728
11729   if (in_use == ranges_by_label_allocated)
11730     {
11731       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11732       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11733                                        ranges_by_label,
11734                                        ranges_by_label_allocated);
11735       memset (ranges_by_label + ranges_by_label_in_use, 0,
11736               RANGES_TABLE_INCREMENT
11737               * sizeof (struct dw_ranges_by_label_struct));
11738     }
11739
11740   ranges_by_label[in_use].begin = begin;
11741   ranges_by_label[in_use].end = end;
11742   ranges_by_label_in_use = in_use + 1;
11743
11744   offset = add_ranges_num (-(int)in_use - 1);
11745   if (!*added)
11746     {
11747       add_AT_range_list (die, DW_AT_ranges, offset);
11748       *added = true;
11749     }
11750 }
11751
11752 static void
11753 output_ranges (void)
11754 {
11755   unsigned i;
11756   static const char *const start_fmt = "Offset %#x";
11757   const char *fmt = start_fmt;
11758
11759   for (i = 0; i < ranges_table_in_use; i++)
11760     {
11761       int block_num = ranges_table[i].num;
11762
11763       if (block_num > 0)
11764         {
11765           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11766           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11767
11768           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11769           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11770
11771           /* If all code is in the text section, then the compilation
11772              unit base address defaults to DW_AT_low_pc, which is the
11773              base of the text section.  */
11774           if (!have_multiple_function_sections)
11775             {
11776               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11777                                     text_section_label,
11778                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11779               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11780                                     text_section_label, NULL);
11781             }
11782
11783           /* Otherwise, the compilation unit base address is zero,
11784              which allows us to use absolute addresses, and not worry
11785              about whether the target supports cross-section
11786              arithmetic.  */
11787           else
11788             {
11789               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11790                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11791               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11792             }
11793
11794           fmt = NULL;
11795         }
11796
11797       /* Negative block_num stands for an index into ranges_by_label.  */
11798       else if (block_num < 0)
11799         {
11800           int lab_idx = - block_num - 1;
11801
11802           if (!have_multiple_function_sections)
11803             {
11804               gcc_unreachable ();
11805 #if 0
11806               /* If we ever use add_ranges_by_labels () for a single
11807                  function section, all we have to do is to take out
11808                  the #if 0 above.  */
11809               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11810                                     ranges_by_label[lab_idx].begin,
11811                                     text_section_label,
11812                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11813               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11814                                     ranges_by_label[lab_idx].end,
11815                                     text_section_label, NULL);
11816 #endif
11817             }
11818           else
11819             {
11820               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11821                                    ranges_by_label[lab_idx].begin,
11822                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11823               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11824                                    ranges_by_label[lab_idx].end,
11825                                    NULL);
11826             }
11827         }
11828       else
11829         {
11830           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11831           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11832           fmt = start_fmt;
11833         }
11834     }
11835 }
11836
11837 /* Data structure containing information about input files.  */
11838 struct file_info
11839 {
11840   const char *path;     /* Complete file name.  */
11841   const char *fname;    /* File name part.  */
11842   int length;           /* Length of entire string.  */
11843   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11844   int dir_idx;          /* Index in directory table.  */
11845 };
11846
11847 /* Data structure containing information about directories with source
11848    files.  */
11849 struct dir_info
11850 {
11851   const char *path;     /* Path including directory name.  */
11852   int length;           /* Path length.  */
11853   int prefix;           /* Index of directory entry which is a prefix.  */
11854   int count;            /* Number of files in this directory.  */
11855   int dir_idx;          /* Index of directory used as base.  */
11856 };
11857
11858 /* Callback function for file_info comparison.  We sort by looking at
11859    the directories in the path.  */
11860
11861 static int
11862 file_info_cmp (const void *p1, const void *p2)
11863 {
11864   const struct file_info *const s1 = (const struct file_info *) p1;
11865   const struct file_info *const s2 = (const struct file_info *) p2;
11866   const unsigned char *cp1;
11867   const unsigned char *cp2;
11868
11869   /* Take care of file names without directories.  We need to make sure that
11870      we return consistent values to qsort since some will get confused if
11871      we return the same value when identical operands are passed in opposite
11872      orders.  So if neither has a directory, return 0 and otherwise return
11873      1 or -1 depending on which one has the directory.  */
11874   if ((s1->path == s1->fname || s2->path == s2->fname))
11875     return (s2->path == s2->fname) - (s1->path == s1->fname);
11876
11877   cp1 = (const unsigned char *) s1->path;
11878   cp2 = (const unsigned char *) s2->path;
11879
11880   while (1)
11881     {
11882       ++cp1;
11883       ++cp2;
11884       /* Reached the end of the first path?  If so, handle like above.  */
11885       if ((cp1 == (const unsigned char *) s1->fname)
11886           || (cp2 == (const unsigned char *) s2->fname))
11887         return ((cp2 == (const unsigned char *) s2->fname)
11888                 - (cp1 == (const unsigned char *) s1->fname));
11889
11890       /* Character of current path component the same?  */
11891       else if (*cp1 != *cp2)
11892         return *cp1 - *cp2;
11893     }
11894 }
11895
11896 struct file_name_acquire_data
11897 {
11898   struct file_info *files;
11899   int used_files;
11900   int max_files;
11901 };
11902
11903 /* Traversal function for the hash table.  */
11904
11905 static int
11906 file_name_acquire (void ** slot, void *data)
11907 {
11908   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11909   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11910   struct file_info *fi;
11911   const char *f;
11912
11913   gcc_assert (fnad->max_files >= d->emitted_number);
11914
11915   if (! d->emitted_number)
11916     return 1;
11917
11918   gcc_assert (fnad->max_files != fnad->used_files);
11919
11920   fi = fnad->files + fnad->used_files++;
11921
11922   /* Skip all leading "./".  */
11923   f = d->filename;
11924   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11925     f += 2;
11926
11927   /* Create a new array entry.  */
11928   fi->path = f;
11929   fi->length = strlen (f);
11930   fi->file_idx = d;
11931
11932   /* Search for the file name part.  */
11933   f = strrchr (f, DIR_SEPARATOR);
11934 #if defined (DIR_SEPARATOR_2)
11935   {
11936     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11937
11938     if (g != NULL)
11939       {
11940         if (f == NULL || f < g)
11941           f = g;
11942       }
11943   }
11944 #endif
11945
11946   fi->fname = f == NULL ? fi->path : f + 1;
11947   return 1;
11948 }
11949
11950 /* Output the directory table and the file name table.  We try to minimize
11951    the total amount of memory needed.  A heuristic is used to avoid large
11952    slowdowns with many input files.  */
11953
11954 static void
11955 output_file_names (void)
11956 {
11957   struct file_name_acquire_data fnad;
11958   int numfiles;
11959   struct file_info *files;
11960   struct dir_info *dirs;
11961   int *saved;
11962   int *savehere;
11963   int *backmap;
11964   int ndirs;
11965   int idx_offset;
11966   int i;
11967
11968   if (!last_emitted_file)
11969     {
11970       dw2_asm_output_data (1, 0, "End directory table");
11971       dw2_asm_output_data (1, 0, "End file name table");
11972       return;
11973     }
11974
11975   numfiles = last_emitted_file->emitted_number;
11976
11977   /* Allocate the various arrays we need.  */
11978   files = XALLOCAVEC (struct file_info, numfiles);
11979   dirs = XALLOCAVEC (struct dir_info, numfiles);
11980
11981   fnad.files = files;
11982   fnad.used_files = 0;
11983   fnad.max_files = numfiles;
11984   htab_traverse (file_table, file_name_acquire, &fnad);
11985   gcc_assert (fnad.used_files == fnad.max_files);
11986
11987   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11988
11989   /* Find all the different directories used.  */
11990   dirs[0].path = files[0].path;
11991   dirs[0].length = files[0].fname - files[0].path;
11992   dirs[0].prefix = -1;
11993   dirs[0].count = 1;
11994   dirs[0].dir_idx = 0;
11995   files[0].dir_idx = 0;
11996   ndirs = 1;
11997
11998   for (i = 1; i < numfiles; i++)
11999     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12000         && memcmp (dirs[ndirs - 1].path, files[i].path,
12001                    dirs[ndirs - 1].length) == 0)
12002       {
12003         /* Same directory as last entry.  */
12004         files[i].dir_idx = ndirs - 1;
12005         ++dirs[ndirs - 1].count;
12006       }
12007     else
12008       {
12009         int j;
12010
12011         /* This is a new directory.  */
12012         dirs[ndirs].path = files[i].path;
12013         dirs[ndirs].length = files[i].fname - files[i].path;
12014         dirs[ndirs].count = 1;
12015         dirs[ndirs].dir_idx = ndirs;
12016         files[i].dir_idx = ndirs;
12017
12018         /* Search for a prefix.  */
12019         dirs[ndirs].prefix = -1;
12020         for (j = 0; j < ndirs; j++)
12021           if (dirs[j].length < dirs[ndirs].length
12022               && dirs[j].length > 1
12023               && (dirs[ndirs].prefix == -1
12024                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12025               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12026             dirs[ndirs].prefix = j;
12027
12028         ++ndirs;
12029       }
12030
12031   /* Now to the actual work.  We have to find a subset of the directories which
12032      allow expressing the file name using references to the directory table
12033      with the least amount of characters.  We do not do an exhaustive search
12034      where we would have to check out every combination of every single
12035      possible prefix.  Instead we use a heuristic which provides nearly optimal
12036      results in most cases and never is much off.  */
12037   saved = XALLOCAVEC (int, ndirs);
12038   savehere = XALLOCAVEC (int, ndirs);
12039
12040   memset (saved, '\0', ndirs * sizeof (saved[0]));
12041   for (i = 0; i < ndirs; i++)
12042     {
12043       int j;
12044       int total;
12045
12046       /* We can always save some space for the current directory.  But this
12047          does not mean it will be enough to justify adding the directory.  */
12048       savehere[i] = dirs[i].length;
12049       total = (savehere[i] - saved[i]) * dirs[i].count;
12050
12051       for (j = i + 1; j < ndirs; j++)
12052         {
12053           savehere[j] = 0;
12054           if (saved[j] < dirs[i].length)
12055             {
12056               /* Determine whether the dirs[i] path is a prefix of the
12057                  dirs[j] path.  */
12058               int k;
12059
12060               k = dirs[j].prefix;
12061               while (k != -1 && k != (int) i)
12062                 k = dirs[k].prefix;
12063
12064               if (k == (int) i)
12065                 {
12066                   /* Yes it is.  We can possibly save some memory by
12067                      writing the filenames in dirs[j] relative to
12068                      dirs[i].  */
12069                   savehere[j] = dirs[i].length;
12070                   total += (savehere[j] - saved[j]) * dirs[j].count;
12071                 }
12072             }
12073         }
12074
12075       /* Check whether we can save enough to justify adding the dirs[i]
12076          directory.  */
12077       if (total > dirs[i].length + 1)
12078         {
12079           /* It's worthwhile adding.  */
12080           for (j = i; j < ndirs; j++)
12081             if (savehere[j] > 0)
12082               {
12083                 /* Remember how much we saved for this directory so far.  */
12084                 saved[j] = savehere[j];
12085
12086                 /* Remember the prefix directory.  */
12087                 dirs[j].dir_idx = i;
12088               }
12089         }
12090     }
12091
12092   /* Emit the directory name table.  */
12093   idx_offset = dirs[0].length > 0 ? 1 : 0;
12094   for (i = 1 - idx_offset; i < ndirs; i++)
12095     dw2_asm_output_nstring (dirs[i].path,
12096                             dirs[i].length
12097                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12098                             "Directory Entry: %#x", i + idx_offset);
12099
12100   dw2_asm_output_data (1, 0, "End directory table");
12101
12102   /* We have to emit them in the order of emitted_number since that's
12103      used in the debug info generation.  To do this efficiently we
12104      generate a back-mapping of the indices first.  */
12105   backmap = XALLOCAVEC (int, numfiles);
12106   for (i = 0; i < numfiles; i++)
12107     backmap[files[i].file_idx->emitted_number - 1] = i;
12108
12109   /* Now write all the file names.  */
12110   for (i = 0; i < numfiles; i++)
12111     {
12112       int file_idx = backmap[i];
12113       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12114
12115 #ifdef VMS_DEBUGGING_INFO
12116 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12117
12118       /* Setting these fields can lead to debugger miscomparisons,
12119          but VMS Debug requires them to be set correctly.  */
12120
12121       int ver;
12122       long long cdt;
12123       long siz;
12124       int maxfilelen = strlen (files[file_idx].path)
12125                                + dirs[dir_idx].length
12126                                + MAX_VMS_VERSION_LEN + 1;
12127       char *filebuf = XALLOCAVEC (char, maxfilelen);
12128
12129       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12130       snprintf (filebuf, maxfilelen, "%s;%d",
12131                 files[file_idx].path + dirs[dir_idx].length, ver);
12132
12133       dw2_asm_output_nstring
12134         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12135
12136       /* Include directory index.  */
12137       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12138
12139       /* Modification time.  */
12140       dw2_asm_output_data_uleb128
12141         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12142           ? cdt : 0,
12143          NULL);
12144
12145       /* File length in bytes.  */
12146       dw2_asm_output_data_uleb128
12147         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12148           ? siz : 0,
12149          NULL);
12150 #else
12151       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12152                               "File Entry: %#x", (unsigned) i + 1);
12153
12154       /* Include directory index.  */
12155       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12156
12157       /* Modification time.  */
12158       dw2_asm_output_data_uleb128 (0, NULL);
12159
12160       /* File length in bytes.  */
12161       dw2_asm_output_data_uleb128 (0, NULL);
12162 #endif /* VMS_DEBUGGING_INFO */
12163     }
12164
12165   dw2_asm_output_data (1, 0, "End file name table");
12166 }
12167
12168
12169 /* Output the source line number correspondence information.  This
12170    information goes into the .debug_line section.  */
12171
12172 static void
12173 output_line_info (void)
12174 {
12175   char l1[20], l2[20], p1[20], p2[20];
12176   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12177   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12178   unsigned opc;
12179   unsigned n_op_args;
12180   unsigned long lt_index;
12181   unsigned long current_line;
12182   long line_offset;
12183   long line_delta;
12184   unsigned long current_file;
12185   unsigned long function;
12186   int ver = dwarf_version;
12187
12188   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12189   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12190   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12191   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12192
12193   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12194     dw2_asm_output_data (4, 0xffffffff,
12195       "Initial length escape value indicating 64-bit DWARF extension");
12196   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12197                         "Length of Source Line Info");
12198   ASM_OUTPUT_LABEL (asm_out_file, l1);
12199
12200   dw2_asm_output_data (2, ver, "DWARF Version");
12201   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12202   ASM_OUTPUT_LABEL (asm_out_file, p1);
12203
12204   /* Define the architecture-dependent minimum instruction length (in
12205    bytes).  In this implementation of DWARF, this field is used for
12206    information purposes only.  Since GCC generates assembly language,
12207    we have no a priori knowledge of how many instruction bytes are
12208    generated for each source line, and therefore can use only the
12209    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12210    commands.  Accordingly, we fix this as `1', which is "correct
12211    enough" for all architectures, and don't let the target override.  */
12212   dw2_asm_output_data (1, 1,
12213                        "Minimum Instruction Length");
12214
12215   if (ver >= 4)
12216     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12217                          "Maximum Operations Per Instruction");
12218   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12219                        "Default is_stmt_start flag");
12220   dw2_asm_output_data (1, DWARF_LINE_BASE,
12221                        "Line Base Value (Special Opcodes)");
12222   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12223                        "Line Range Value (Special Opcodes)");
12224   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12225                        "Special Opcode Base");
12226
12227   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12228     {
12229       switch (opc)
12230         {
12231         case DW_LNS_advance_pc:
12232         case DW_LNS_advance_line:
12233         case DW_LNS_set_file:
12234         case DW_LNS_set_column:
12235         case DW_LNS_fixed_advance_pc:
12236           n_op_args = 1;
12237           break;
12238         default:
12239           n_op_args = 0;
12240           break;
12241         }
12242
12243       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12244                            opc, n_op_args);
12245     }
12246
12247   /* Write out the information about the files we use.  */
12248   output_file_names ();
12249   ASM_OUTPUT_LABEL (asm_out_file, p2);
12250
12251   /* We used to set the address register to the first location in the text
12252      section here, but that didn't accomplish anything since we already
12253      have a line note for the opening brace of the first function.  */
12254
12255   /* Generate the line number to PC correspondence table, encoded as
12256      a series of state machine operations.  */
12257   current_file = 1;
12258   current_line = 1;
12259
12260   if (cfun && in_cold_section_p)
12261     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12262   else
12263     strcpy (prev_line_label, text_section_label);
12264   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12265     {
12266       dw_line_info_ref line_info = &line_info_table[lt_index];
12267
12268 #if 0
12269       /* Disable this optimization for now; GDB wants to see two line notes
12270          at the beginning of a function so it can find the end of the
12271          prologue.  */
12272
12273       /* Don't emit anything for redundant notes.  Just updating the
12274          address doesn't accomplish anything, because we already assume
12275          that anything after the last address is this line.  */
12276       if (line_info->dw_line_num == current_line
12277           && line_info->dw_file_num == current_file)
12278         continue;
12279 #endif
12280
12281       /* Emit debug info for the address of the current line.
12282
12283          Unfortunately, we have little choice here currently, and must always
12284          use the most general form.  GCC does not know the address delta
12285          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12286          attributes which will give an upper bound on the address range.  We
12287          could perhaps use length attributes to determine when it is safe to
12288          use DW_LNS_fixed_advance_pc.  */
12289
12290       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12291       if (0)
12292         {
12293           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12294           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12295                                "DW_LNS_fixed_advance_pc");
12296           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12297         }
12298       else
12299         {
12300           /* This can handle any delta.  This takes
12301              4+DWARF2_ADDR_SIZE bytes.  */
12302           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12303           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12304           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12305           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12306         }
12307
12308       strcpy (prev_line_label, line_label);
12309
12310       /* Emit debug info for the source file of the current line, if
12311          different from the previous line.  */
12312       if (line_info->dw_file_num != current_file)
12313         {
12314           current_file = line_info->dw_file_num;
12315           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12316           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12317         }
12318
12319       /* Emit debug info for the current line number, choosing the encoding
12320          that uses the least amount of space.  */
12321       if (line_info->dw_line_num != current_line)
12322         {
12323           line_offset = line_info->dw_line_num - current_line;
12324           line_delta = line_offset - DWARF_LINE_BASE;
12325           current_line = line_info->dw_line_num;
12326           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12327             /* This can handle deltas from -10 to 234, using the current
12328                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12329                takes 1 byte.  */
12330             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12331                                  "line %lu", current_line);
12332           else
12333             {
12334               /* This can handle any delta.  This takes at least 4 bytes,
12335                  depending on the value being encoded.  */
12336               dw2_asm_output_data (1, DW_LNS_advance_line,
12337                                    "advance to line %lu", current_line);
12338               dw2_asm_output_data_sleb128 (line_offset, NULL);
12339               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12340             }
12341         }
12342       else
12343         /* We still need to start a new row, so output a copy insn.  */
12344         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12345     }
12346
12347   /* Emit debug info for the address of the end of the function.  */
12348   if (0)
12349     {
12350       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12351                            "DW_LNS_fixed_advance_pc");
12352       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12353     }
12354   else
12355     {
12356       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12357       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12358       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12359       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12360     }
12361
12362   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12363   dw2_asm_output_data_uleb128 (1, NULL);
12364   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12365
12366   function = 0;
12367   current_file = 1;
12368   current_line = 1;
12369   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12370     {
12371       dw_separate_line_info_ref line_info
12372         = &separate_line_info_table[lt_index];
12373
12374 #if 0
12375       /* Don't emit anything for redundant notes.  */
12376       if (line_info->dw_line_num == current_line
12377           && line_info->dw_file_num == current_file
12378           && line_info->function == function)
12379         goto cont;
12380 #endif
12381
12382       /* Emit debug info for the address of the current line.  If this is
12383          a new function, or the first line of a function, then we need
12384          to handle it differently.  */
12385       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12386                                    lt_index);
12387       if (function != line_info->function)
12388         {
12389           function = line_info->function;
12390
12391           /* Set the address register to the first line in the function.  */
12392           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12393           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12394           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12395           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12396         }
12397       else
12398         {
12399           /* ??? See the DW_LNS_advance_pc comment above.  */
12400           if (0)
12401             {
12402               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12403                                    "DW_LNS_fixed_advance_pc");
12404               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12405             }
12406           else
12407             {
12408               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12409               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12410               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12411               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12412             }
12413         }
12414
12415       strcpy (prev_line_label, line_label);
12416
12417       /* Emit debug info for the source file of the current line, if
12418          different from the previous line.  */
12419       if (line_info->dw_file_num != current_file)
12420         {
12421           current_file = line_info->dw_file_num;
12422           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12423           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12424         }
12425
12426       /* Emit debug info for the current line number, choosing the encoding
12427          that uses the least amount of space.  */
12428       if (line_info->dw_line_num != current_line)
12429         {
12430           line_offset = line_info->dw_line_num - current_line;
12431           line_delta = line_offset - DWARF_LINE_BASE;
12432           current_line = line_info->dw_line_num;
12433           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12434             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12435                                  "line %lu", current_line);
12436           else
12437             {
12438               dw2_asm_output_data (1, DW_LNS_advance_line,
12439                                    "advance to line %lu", current_line);
12440               dw2_asm_output_data_sleb128 (line_offset, NULL);
12441               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12442             }
12443         }
12444       else
12445         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12446
12447 #if 0
12448     cont:
12449 #endif
12450
12451       lt_index++;
12452
12453       /* If we're done with a function, end its sequence.  */
12454       if (lt_index == separate_line_info_table_in_use
12455           || separate_line_info_table[lt_index].function != function)
12456         {
12457           current_file = 1;
12458           current_line = 1;
12459
12460           /* Emit debug info for the address of the end of the function.  */
12461           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12462           if (0)
12463             {
12464               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12465                                    "DW_LNS_fixed_advance_pc");
12466               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12467             }
12468           else
12469             {
12470               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12471               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12472               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12473               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12474             }
12475
12476           /* Output the marker for the end of this sequence.  */
12477           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12478           dw2_asm_output_data_uleb128 (1, NULL);
12479           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12480         }
12481     }
12482
12483   /* Output the marker for the end of the line number info.  */
12484   ASM_OUTPUT_LABEL (asm_out_file, l2);
12485 }
12486
12487 /* Return the size of the .debug_dcall table for the compilation unit.  */
12488
12489 static unsigned long
12490 size_of_dcall_table (void)
12491 {
12492   unsigned long size;
12493   unsigned int i;
12494   dcall_entry *p;
12495   tree last_poc_decl = NULL;
12496
12497   /* Header:  version + debug info section pointer + pointer size.  */
12498   size = 2 + DWARF_OFFSET_SIZE + 1;
12499
12500   /* Each entry:  code label + DIE offset.  */
12501   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12502     {
12503       gcc_assert (p->targ_die != NULL);
12504       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12505       if (p->poc_decl != last_poc_decl)
12506         {
12507           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12508           gcc_assert (poc_die);
12509           last_poc_decl = p->poc_decl;
12510           if (poc_die)
12511             size += (DWARF_OFFSET_SIZE
12512                      + size_of_uleb128 (poc_die->die_offset));
12513         }
12514       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12515     }
12516
12517   return size;
12518 }
12519
12520 /* Output the direct call table used to disambiguate PC values when
12521    identical function have been merged.  */
12522
12523 static void
12524 output_dcall_table (void)
12525 {
12526   unsigned i;
12527   unsigned long dcall_length = size_of_dcall_table ();
12528   dcall_entry *p;
12529   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12530   tree last_poc_decl = NULL;
12531
12532   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12533     dw2_asm_output_data (4, 0xffffffff,
12534       "Initial length escape value indicating 64-bit DWARF extension");
12535   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12536                        "Length of Direct Call Table");
12537   dw2_asm_output_data (2, 4, "Version number");
12538   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12539                          debug_info_section,
12540                          "Offset of Compilation Unit Info");
12541   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12542
12543   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12544     {
12545       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12546       if (p->poc_decl != last_poc_decl)
12547         {
12548           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12549           last_poc_decl = p->poc_decl;
12550           if (poc_die)
12551             {
12552               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12553               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12554                                            "Caller DIE offset");
12555             }
12556         }
12557       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12558       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12559       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12560                                    "Callee DIE offset");
12561     }
12562 }
12563 \f
12564 /* Return the size of the .debug_vcall table for the compilation unit.  */
12565
12566 static unsigned long
12567 size_of_vcall_table (void)
12568 {
12569   unsigned long size;
12570   unsigned int i;
12571   vcall_entry *p;
12572
12573   /* Header:  version + pointer size.  */
12574   size = 2 + 1;
12575
12576   /* Each entry:  code label + vtable slot index.  */
12577   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12578     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12579
12580   return size;
12581 }
12582
12583 /* Output the virtual call table used to disambiguate PC values when
12584    identical function have been merged.  */
12585
12586 static void
12587 output_vcall_table (void)
12588 {
12589   unsigned i;
12590   unsigned long vcall_length = size_of_vcall_table ();
12591   vcall_entry *p;
12592   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12593
12594   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12595     dw2_asm_output_data (4, 0xffffffff,
12596       "Initial length escape value indicating 64-bit DWARF extension");
12597   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12598                        "Length of Virtual Call Table");
12599   dw2_asm_output_data (2, 4, "Version number");
12600   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12601
12602   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12603     {
12604       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12605       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12606       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12607     }
12608 }
12609 \f
12610 /* Given a pointer to a tree node for some base type, return a pointer to
12611    a DIE that describes the given type.
12612
12613    This routine must only be called for GCC type nodes that correspond to
12614    Dwarf base (fundamental) types.  */
12615
12616 static dw_die_ref
12617 base_type_die (tree type)
12618 {
12619   dw_die_ref base_type_result;
12620   enum dwarf_type encoding;
12621
12622   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12623     return 0;
12624
12625   /* If this is a subtype that should not be emitted as a subrange type,
12626      use the base type.  See subrange_type_for_debug_p.  */
12627   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12628     type = TREE_TYPE (type);
12629
12630   switch (TREE_CODE (type))
12631     {
12632     case INTEGER_TYPE:
12633       if ((dwarf_version >= 4 || !dwarf_strict)
12634           && TYPE_NAME (type)
12635           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12636           && DECL_IS_BUILTIN (TYPE_NAME (type))
12637           && DECL_NAME (TYPE_NAME (type)))
12638         {
12639           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12640           if (strcmp (name, "char16_t") == 0
12641               || strcmp (name, "char32_t") == 0)
12642             {
12643               encoding = DW_ATE_UTF;
12644               break;
12645             }
12646         }
12647       if (TYPE_STRING_FLAG (type))
12648         {
12649           if (TYPE_UNSIGNED (type))
12650             encoding = DW_ATE_unsigned_char;
12651           else
12652             encoding = DW_ATE_signed_char;
12653         }
12654       else if (TYPE_UNSIGNED (type))
12655         encoding = DW_ATE_unsigned;
12656       else
12657         encoding = DW_ATE_signed;
12658       break;
12659
12660     case REAL_TYPE:
12661       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12662         {
12663           if (dwarf_version >= 3 || !dwarf_strict)
12664             encoding = DW_ATE_decimal_float;
12665           else
12666             encoding = DW_ATE_lo_user;
12667         }
12668       else
12669         encoding = DW_ATE_float;
12670       break;
12671
12672     case FIXED_POINT_TYPE:
12673       if (!(dwarf_version >= 3 || !dwarf_strict))
12674         encoding = DW_ATE_lo_user;
12675       else if (TYPE_UNSIGNED (type))
12676         encoding = DW_ATE_unsigned_fixed;
12677       else
12678         encoding = DW_ATE_signed_fixed;
12679       break;
12680
12681       /* Dwarf2 doesn't know anything about complex ints, so use
12682          a user defined type for it.  */
12683     case COMPLEX_TYPE:
12684       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12685         encoding = DW_ATE_complex_float;
12686       else
12687         encoding = DW_ATE_lo_user;
12688       break;
12689
12690     case BOOLEAN_TYPE:
12691       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12692       encoding = DW_ATE_boolean;
12693       break;
12694
12695     default:
12696       /* No other TREE_CODEs are Dwarf fundamental types.  */
12697       gcc_unreachable ();
12698     }
12699
12700   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12701
12702   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12703                    int_size_in_bytes (type));
12704   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12705
12706   return base_type_result;
12707 }
12708
12709 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12710    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12711
12712 static inline int
12713 is_base_type (tree type)
12714 {
12715   switch (TREE_CODE (type))
12716     {
12717     case ERROR_MARK:
12718     case VOID_TYPE:
12719     case INTEGER_TYPE:
12720     case REAL_TYPE:
12721     case FIXED_POINT_TYPE:
12722     case COMPLEX_TYPE:
12723     case BOOLEAN_TYPE:
12724       return 1;
12725
12726     case ARRAY_TYPE:
12727     case RECORD_TYPE:
12728     case UNION_TYPE:
12729     case QUAL_UNION_TYPE:
12730     case ENUMERAL_TYPE:
12731     case FUNCTION_TYPE:
12732     case METHOD_TYPE:
12733     case POINTER_TYPE:
12734     case REFERENCE_TYPE:
12735     case NULLPTR_TYPE:
12736     case OFFSET_TYPE:
12737     case LANG_TYPE:
12738     case VECTOR_TYPE:
12739       return 0;
12740
12741     default:
12742       gcc_unreachable ();
12743     }
12744
12745   return 0;
12746 }
12747
12748 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12749    node, return the size in bits for the type if it is a constant, or else
12750    return the alignment for the type if the type's size is not constant, or
12751    else return BITS_PER_WORD if the type actually turns out to be an
12752    ERROR_MARK node.  */
12753
12754 static inline unsigned HOST_WIDE_INT
12755 simple_type_size_in_bits (const_tree type)
12756 {
12757   if (TREE_CODE (type) == ERROR_MARK)
12758     return BITS_PER_WORD;
12759   else if (TYPE_SIZE (type) == NULL_TREE)
12760     return 0;
12761   else if (host_integerp (TYPE_SIZE (type), 1))
12762     return tree_low_cst (TYPE_SIZE (type), 1);
12763   else
12764     return TYPE_ALIGN (type);
12765 }
12766
12767 /* Similarly, but return a double_int instead of UHWI.  */
12768
12769 static inline double_int
12770 double_int_type_size_in_bits (const_tree type)
12771 {
12772   if (TREE_CODE (type) == ERROR_MARK)
12773     return uhwi_to_double_int (BITS_PER_WORD);
12774   else if (TYPE_SIZE (type) == NULL_TREE)
12775     return double_int_zero;
12776   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12777     return tree_to_double_int (TYPE_SIZE (type));
12778   else
12779     return uhwi_to_double_int (TYPE_ALIGN (type));
12780 }
12781
12782 /*  Given a pointer to a tree node for a subrange type, return a pointer
12783     to a DIE that describes the given type.  */
12784
12785 static dw_die_ref
12786 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12787 {
12788   dw_die_ref subrange_die;
12789   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12790
12791   if (context_die == NULL)
12792     context_die = comp_unit_die ();
12793
12794   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12795
12796   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12797     {
12798       /* The size of the subrange type and its base type do not match,
12799          so we need to generate a size attribute for the subrange type.  */
12800       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12801     }
12802
12803   if (low)
12804     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12805   if (high)
12806     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12807
12808   return subrange_die;
12809 }
12810
12811 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12812    entry that chains various modifiers in front of the given type.  */
12813
12814 static dw_die_ref
12815 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12816                    dw_die_ref context_die)
12817 {
12818   enum tree_code code = TREE_CODE (type);
12819   dw_die_ref mod_type_die;
12820   dw_die_ref sub_die = NULL;
12821   tree item_type = NULL;
12822   tree qualified_type;
12823   tree name, low, high;
12824
12825   if (code == ERROR_MARK)
12826     return NULL;
12827
12828   /* See if we already have the appropriately qualified variant of
12829      this type.  */
12830   qualified_type
12831     = get_qualified_type (type,
12832                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12833                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12834
12835   if (qualified_type == sizetype
12836       && TYPE_NAME (qualified_type)
12837       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12838     {
12839       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12840
12841       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12842                            && TYPE_PRECISION (t)
12843                            == TYPE_PRECISION (qualified_type)
12844                            && TYPE_UNSIGNED (t)
12845                            == TYPE_UNSIGNED (qualified_type));
12846       qualified_type = t;
12847     }
12848
12849   /* If we do, then we can just use its DIE, if it exists.  */
12850   if (qualified_type)
12851     {
12852       mod_type_die = lookup_type_die (qualified_type);
12853       if (mod_type_die)
12854         return mod_type_die;
12855     }
12856
12857   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12858
12859   /* Handle C typedef types.  */
12860   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12861       && !DECL_ARTIFICIAL (name))
12862     {
12863       tree dtype = TREE_TYPE (name);
12864
12865       if (qualified_type == dtype)
12866         {
12867           /* For a named type, use the typedef.  */
12868           gen_type_die (qualified_type, context_die);
12869           return lookup_type_die (qualified_type);
12870         }
12871       else if (is_const_type < TYPE_READONLY (dtype)
12872                || is_volatile_type < TYPE_VOLATILE (dtype)
12873                || (is_const_type <= TYPE_READONLY (dtype)
12874                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12875                    && DECL_ORIGINAL_TYPE (name) != type))
12876         /* cv-unqualified version of named type.  Just use the unnamed
12877            type to which it refers.  */
12878         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12879                                   is_const_type, is_volatile_type,
12880                                   context_die);
12881       /* Else cv-qualified version of named type; fall through.  */
12882     }
12883
12884   if (is_const_type)
12885     {
12886       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12887       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12888     }
12889   else if (is_volatile_type)
12890     {
12891       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12892       sub_die = modified_type_die (type, 0, 0, context_die);
12893     }
12894   else if (code == POINTER_TYPE)
12895     {
12896       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12897       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12898                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12899       item_type = TREE_TYPE (type);
12900       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12901         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12902                          TYPE_ADDR_SPACE (item_type));
12903     }
12904   else if (code == REFERENCE_TYPE)
12905     {
12906       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12907         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12908                                 type);
12909       else
12910         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12911       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12912                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12913       item_type = TREE_TYPE (type);
12914       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12915         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12916                          TYPE_ADDR_SPACE (item_type));
12917     }
12918   else if (code == INTEGER_TYPE
12919            && TREE_TYPE (type) != NULL_TREE
12920            && subrange_type_for_debug_p (type, &low, &high))
12921     {
12922       mod_type_die = subrange_type_die (type, low, high, context_die);
12923       item_type = TREE_TYPE (type);
12924     }
12925   else if (is_base_type (type))
12926     mod_type_die = base_type_die (type);
12927   else
12928     {
12929       gen_type_die (type, context_die);
12930
12931       /* We have to get the type_main_variant here (and pass that to the
12932          `lookup_type_die' routine) because the ..._TYPE node we have
12933          might simply be a *copy* of some original type node (where the
12934          copy was created to help us keep track of typedef names) and
12935          that copy might have a different TYPE_UID from the original
12936          ..._TYPE node.  */
12937       if (TREE_CODE (type) != VECTOR_TYPE)
12938         return lookup_type_die (type_main_variant (type));
12939       else
12940         /* Vectors have the debugging information in the type,
12941            not the main variant.  */
12942         return lookup_type_die (type);
12943     }
12944
12945   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12946      don't output a DW_TAG_typedef, since there isn't one in the
12947      user's program; just attach a DW_AT_name to the type.
12948      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12949      if the base type already has the same name.  */
12950   if (name
12951       && ((TREE_CODE (name) != TYPE_DECL
12952            && (qualified_type == TYPE_MAIN_VARIANT (type)
12953                || (!is_const_type && !is_volatile_type)))
12954           || (TREE_CODE (name) == TYPE_DECL
12955               && TREE_TYPE (name) == qualified_type
12956               && DECL_NAME (name))))
12957     {
12958       if (TREE_CODE (name) == TYPE_DECL)
12959         /* Could just call add_name_and_src_coords_attributes here,
12960            but since this is a builtin type it doesn't have any
12961            useful source coordinates anyway.  */
12962         name = DECL_NAME (name);
12963       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12964     }
12965   /* This probably indicates a bug.  */
12966   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12967     add_name_attribute (mod_type_die, "__unknown__");
12968
12969   if (qualified_type)
12970     equate_type_number_to_die (qualified_type, mod_type_die);
12971
12972   if (item_type)
12973     /* We must do this after the equate_type_number_to_die call, in case
12974        this is a recursive type.  This ensures that the modified_type_die
12975        recursion will terminate even if the type is recursive.  Recursive
12976        types are possible in Ada.  */
12977     sub_die = modified_type_die (item_type,
12978                                  TYPE_READONLY (item_type),
12979                                  TYPE_VOLATILE (item_type),
12980                                  context_die);
12981
12982   if (sub_die != NULL)
12983     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12984
12985   return mod_type_die;
12986 }
12987
12988 /* Generate DIEs for the generic parameters of T.
12989    T must be either a generic type or a generic function.
12990    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12991
12992 static void
12993 gen_generic_params_dies (tree t)
12994 {
12995   tree parms, args;
12996   int parms_num, i;
12997   dw_die_ref die = NULL;
12998
12999   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13000     return;
13001
13002   if (TYPE_P (t))
13003     die = lookup_type_die (t);
13004   else if (DECL_P (t))
13005     die = lookup_decl_die (t);
13006
13007   gcc_assert (die);
13008
13009   parms = lang_hooks.get_innermost_generic_parms (t);
13010   if (!parms)
13011     /* T has no generic parameter. It means T is neither a generic type
13012        or function. End of story.  */
13013     return;
13014
13015   parms_num = TREE_VEC_LENGTH (parms);
13016   args = lang_hooks.get_innermost_generic_args (t);
13017   for (i = 0; i < parms_num; i++)
13018     {
13019       tree parm, arg, arg_pack_elems;
13020
13021       parm = TREE_VEC_ELT (parms, i);
13022       arg = TREE_VEC_ELT (args, i);
13023       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13024       gcc_assert (parm && TREE_VALUE (parm) && arg);
13025
13026       if (parm && TREE_VALUE (parm) && arg)
13027         {
13028           /* If PARM represents a template parameter pack,
13029              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13030              by DW_TAG_template_*_parameter DIEs for the argument
13031              pack elements of ARG. Note that ARG would then be
13032              an argument pack.  */
13033           if (arg_pack_elems)
13034             template_parameter_pack_die (TREE_VALUE (parm),
13035                                          arg_pack_elems,
13036                                          die);
13037           else
13038             generic_parameter_die (TREE_VALUE (parm), arg,
13039                                    true /* Emit DW_AT_name */, die);
13040         }
13041     }
13042 }
13043
13044 /* Create and return a DIE for PARM which should be
13045    the representation of a generic type parameter.
13046    For instance, in the C++ front end, PARM would be a template parameter.
13047    ARG is the argument to PARM.
13048    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13049    name of the PARM.
13050    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13051    as a child node.  */
13052
13053 static dw_die_ref
13054 generic_parameter_die (tree parm, tree arg,
13055                        bool emit_name_p,
13056                        dw_die_ref parent_die)
13057 {
13058   dw_die_ref tmpl_die = NULL;
13059   const char *name = NULL;
13060
13061   if (!parm || !DECL_NAME (parm) || !arg)
13062     return NULL;
13063
13064   /* We support non-type generic parameters and arguments,
13065      type generic parameters and arguments, as well as
13066      generic generic parameters (a.k.a. template template parameters in C++)
13067      and arguments.  */
13068   if (TREE_CODE (parm) == PARM_DECL)
13069     /* PARM is a nontype generic parameter  */
13070     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13071   else if (TREE_CODE (parm) == TYPE_DECL)
13072     /* PARM is a type generic parameter.  */
13073     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13074   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13075     /* PARM is a generic generic parameter.
13076        Its DIE is a GNU extension. It shall have a
13077        DW_AT_name attribute to represent the name of the template template
13078        parameter, and a DW_AT_GNU_template_name attribute to represent the
13079        name of the template template argument.  */
13080     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13081                         parent_die, parm);
13082   else
13083     gcc_unreachable ();
13084
13085   if (tmpl_die)
13086     {
13087       tree tmpl_type;
13088
13089       /* If PARM is a generic parameter pack, it means we are
13090          emitting debug info for a template argument pack element.
13091          In other terms, ARG is a template argument pack element.
13092          In that case, we don't emit any DW_AT_name attribute for
13093          the die.  */
13094       if (emit_name_p)
13095         {
13096           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13097           gcc_assert (name);
13098           add_AT_string (tmpl_die, DW_AT_name, name);
13099         }
13100
13101       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13102         {
13103           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13104              TMPL_DIE should have a child DW_AT_type attribute that is set
13105              to the type of the argument to PARM, which is ARG.
13106              If PARM is a type generic parameter, TMPL_DIE should have a
13107              child DW_AT_type that is set to ARG.  */
13108           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13109           add_type_attribute (tmpl_die, tmpl_type, 0,
13110                               TREE_THIS_VOLATILE (tmpl_type),
13111                               parent_die);
13112         }
13113       else
13114         {
13115           /* So TMPL_DIE is a DIE representing a
13116              a generic generic template parameter, a.k.a template template
13117              parameter in C++ and arg is a template.  */
13118
13119           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13120              to the name of the argument.  */
13121           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13122           if (name)
13123             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13124         }
13125
13126       if (TREE_CODE (parm) == PARM_DECL)
13127         /* So PARM is a non-type generic parameter.
13128            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13129            attribute of TMPL_DIE which value represents the value
13130            of ARG.
13131            We must be careful here:
13132            The value of ARG might reference some function decls.
13133            We might currently be emitting debug info for a generic
13134            type and types are emitted before function decls, we don't
13135            know if the function decls referenced by ARG will actually be
13136            emitted after cgraph computations.
13137            So must defer the generation of the DW_AT_const_value to
13138            after cgraph is ready.  */
13139         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13140     }
13141
13142   return tmpl_die;
13143 }
13144
13145 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13146    PARM_PACK must be a template parameter pack. The returned DIE
13147    will be child DIE of PARENT_DIE.  */
13148
13149 static dw_die_ref
13150 template_parameter_pack_die (tree parm_pack,
13151                              tree parm_pack_args,
13152                              dw_die_ref parent_die)
13153 {
13154   dw_die_ref die;
13155   int j;
13156
13157   gcc_assert (parent_die && parm_pack);
13158
13159   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13160   add_name_and_src_coords_attributes (die, parm_pack);
13161   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13162     generic_parameter_die (parm_pack,
13163                            TREE_VEC_ELT (parm_pack_args, j),
13164                            false /* Don't emit DW_AT_name */,
13165                            die);
13166   return die;
13167 }
13168
13169 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13170    an enumerated type.  */
13171
13172 static inline int
13173 type_is_enum (const_tree type)
13174 {
13175   return TREE_CODE (type) == ENUMERAL_TYPE;
13176 }
13177
13178 /* Return the DBX register number described by a given RTL node.  */
13179
13180 static unsigned int
13181 dbx_reg_number (const_rtx rtl)
13182 {
13183   unsigned regno = REGNO (rtl);
13184
13185   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13186
13187 #ifdef LEAF_REG_REMAP
13188   if (current_function_uses_only_leaf_regs)
13189     {
13190       int leaf_reg = LEAF_REG_REMAP (regno);
13191       if (leaf_reg != -1)
13192         regno = (unsigned) leaf_reg;
13193     }
13194 #endif
13195
13196   return DBX_REGISTER_NUMBER (regno);
13197 }
13198
13199 /* Optionally add a DW_OP_piece term to a location description expression.
13200    DW_OP_piece is only added if the location description expression already
13201    doesn't end with DW_OP_piece.  */
13202
13203 static void
13204 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13205 {
13206   dw_loc_descr_ref loc;
13207
13208   if (*list_head != NULL)
13209     {
13210       /* Find the end of the chain.  */
13211       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13212         ;
13213
13214       if (loc->dw_loc_opc != DW_OP_piece)
13215         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13216     }
13217 }
13218
13219 /* Return a location descriptor that designates a machine register or
13220    zero if there is none.  */
13221
13222 static dw_loc_descr_ref
13223 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13224 {
13225   rtx regs;
13226
13227   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13228     return 0;
13229
13230   /* We only use "frame base" when we're sure we're talking about the
13231      post-prologue local stack frame.  We do this by *not* running
13232      register elimination until this point, and recognizing the special
13233      argument pointer and soft frame pointer rtx's.
13234      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13235   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13236       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13237     {
13238       dw_loc_descr_ref result = NULL;
13239
13240       if (dwarf_version >= 4 || !dwarf_strict)
13241         {
13242           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13243           if (result)
13244             add_loc_descr (&result,
13245                            new_loc_descr (DW_OP_stack_value, 0, 0));
13246         }
13247       return result;
13248     }
13249
13250   regs = targetm.dwarf_register_span (rtl);
13251
13252   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13253     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13254   else
13255     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13256 }
13257
13258 /* Return a location descriptor that designates a machine register for
13259    a given hard register number.  */
13260
13261 static dw_loc_descr_ref
13262 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13263 {
13264   dw_loc_descr_ref reg_loc_descr;
13265
13266   if (regno <= 31)
13267     reg_loc_descr
13268       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13269   else
13270     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13271
13272   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13273     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13274
13275   return reg_loc_descr;
13276 }
13277
13278 /* Given an RTL of a register, return a location descriptor that
13279    designates a value that spans more than one register.  */
13280
13281 static dw_loc_descr_ref
13282 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13283                              enum var_init_status initialized)
13284 {
13285   int nregs, size, i;
13286   unsigned reg;
13287   dw_loc_descr_ref loc_result = NULL;
13288
13289   reg = REGNO (rtl);
13290 #ifdef LEAF_REG_REMAP
13291   if (current_function_uses_only_leaf_regs)
13292     {
13293       int leaf_reg = LEAF_REG_REMAP (reg);
13294       if (leaf_reg != -1)
13295         reg = (unsigned) leaf_reg;
13296     }
13297 #endif
13298   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13299   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13300
13301   /* Simple, contiguous registers.  */
13302   if (regs == NULL_RTX)
13303     {
13304       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13305
13306       loc_result = NULL;
13307       while (nregs--)
13308         {
13309           dw_loc_descr_ref t;
13310
13311           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13312                                       VAR_INIT_STATUS_INITIALIZED);
13313           add_loc_descr (&loc_result, t);
13314           add_loc_descr_op_piece (&loc_result, size);
13315           ++reg;
13316         }
13317       return loc_result;
13318     }
13319
13320   /* Now onto stupid register sets in non contiguous locations.  */
13321
13322   gcc_assert (GET_CODE (regs) == PARALLEL);
13323
13324   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13325   loc_result = NULL;
13326
13327   for (i = 0; i < XVECLEN (regs, 0); ++i)
13328     {
13329       dw_loc_descr_ref t;
13330
13331       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13332                                   VAR_INIT_STATUS_INITIALIZED);
13333       add_loc_descr (&loc_result, t);
13334       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13335       add_loc_descr_op_piece (&loc_result, size);
13336     }
13337
13338   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13339     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13340   return loc_result;
13341 }
13342
13343 /* Return a location descriptor that designates a constant.  */
13344
13345 static dw_loc_descr_ref
13346 int_loc_descriptor (HOST_WIDE_INT i)
13347 {
13348   enum dwarf_location_atom op;
13349
13350   /* Pick the smallest representation of a constant, rather than just
13351      defaulting to the LEB encoding.  */
13352   if (i >= 0)
13353     {
13354       if (i <= 31)
13355         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13356       else if (i <= 0xff)
13357         op = DW_OP_const1u;
13358       else if (i <= 0xffff)
13359         op = DW_OP_const2u;
13360       else if (HOST_BITS_PER_WIDE_INT == 32
13361                || i <= 0xffffffff)
13362         op = DW_OP_const4u;
13363       else
13364         op = DW_OP_constu;
13365     }
13366   else
13367     {
13368       if (i >= -0x80)
13369         op = DW_OP_const1s;
13370       else if (i >= -0x8000)
13371         op = DW_OP_const2s;
13372       else if (HOST_BITS_PER_WIDE_INT == 32
13373                || i >= -0x80000000)
13374         op = DW_OP_const4s;
13375       else
13376         op = DW_OP_consts;
13377     }
13378
13379   return new_loc_descr (op, i, 0);
13380 }
13381
13382 /* Return loc description representing "address" of integer value.
13383    This can appear only as toplevel expression.  */
13384
13385 static dw_loc_descr_ref
13386 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13387 {
13388   int litsize;
13389   dw_loc_descr_ref loc_result = NULL;
13390
13391   if (!(dwarf_version >= 4 || !dwarf_strict))
13392     return NULL;
13393
13394   if (i >= 0)
13395     {
13396       if (i <= 31)
13397         litsize = 1;
13398       else if (i <= 0xff)
13399         litsize = 2;
13400       else if (i <= 0xffff)
13401         litsize = 3;
13402       else if (HOST_BITS_PER_WIDE_INT == 32
13403                || i <= 0xffffffff)
13404         litsize = 5;
13405       else
13406         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13407     }
13408   else
13409     {
13410       if (i >= -0x80)
13411         litsize = 2;
13412       else if (i >= -0x8000)
13413         litsize = 3;
13414       else if (HOST_BITS_PER_WIDE_INT == 32
13415                || i >= -0x80000000)
13416         litsize = 5;
13417       else
13418         litsize = 1 + size_of_sleb128 (i);
13419     }
13420   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13421      is more compact.  For DW_OP_stack_value we need:
13422      litsize + 1 (DW_OP_stack_value)
13423      and for DW_OP_implicit_value:
13424      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13425   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13426     {
13427       loc_result = int_loc_descriptor (i);
13428       add_loc_descr (&loc_result,
13429                      new_loc_descr (DW_OP_stack_value, 0, 0));
13430       return loc_result;
13431     }
13432
13433   loc_result = new_loc_descr (DW_OP_implicit_value,
13434                               size, 0);
13435   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13436   loc_result->dw_loc_oprnd2.v.val_int = i;
13437   return loc_result;
13438 }
13439
13440 /* Return a location descriptor that designates a base+offset location.  */
13441
13442 static dw_loc_descr_ref
13443 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13444                  enum var_init_status initialized)
13445 {
13446   unsigned int regno;
13447   dw_loc_descr_ref result;
13448   dw_fde_ref fde = current_fde ();
13449
13450   /* We only use "frame base" when we're sure we're talking about the
13451      post-prologue local stack frame.  We do this by *not* running
13452      register elimination until this point, and recognizing the special
13453      argument pointer and soft frame pointer rtx's.  */
13454   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13455     {
13456       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13457
13458       if (elim != reg)
13459         {
13460           if (GET_CODE (elim) == PLUS)
13461             {
13462               offset += INTVAL (XEXP (elim, 1));
13463               elim = XEXP (elim, 0);
13464             }
13465           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13466                        && (elim == hard_frame_pointer_rtx
13467                            || elim == stack_pointer_rtx))
13468                       || elim == (frame_pointer_needed
13469                                   ? hard_frame_pointer_rtx
13470                                   : stack_pointer_rtx));
13471
13472           /* If drap register is used to align stack, use frame
13473              pointer + offset to access stack variables.  If stack
13474              is aligned without drap, use stack pointer + offset to
13475              access stack variables.  */
13476           if (crtl->stack_realign_tried
13477               && reg == frame_pointer_rtx)
13478             {
13479               int base_reg
13480                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13481                                       ? HARD_FRAME_POINTER_REGNUM
13482                                       : STACK_POINTER_REGNUM);
13483               return new_reg_loc_descr (base_reg, offset);
13484             }
13485
13486           offset += frame_pointer_fb_offset;
13487           return new_loc_descr (DW_OP_fbreg, offset, 0);
13488         }
13489     }
13490   else if (!optimize
13491            && fde
13492            && (fde->drap_reg == REGNO (reg)
13493                || fde->vdrap_reg == REGNO (reg)))
13494     {
13495       /* Use cfa+offset to represent the location of arguments passed
13496          on the stack when drap is used to align stack.
13497          Only do this when not optimizing, for optimized code var-tracking
13498          is supposed to track where the arguments live and the register
13499          used as vdrap or drap in some spot might be used for something
13500          else in other part of the routine.  */
13501       return new_loc_descr (DW_OP_fbreg, offset, 0);
13502     }
13503
13504   regno = dbx_reg_number (reg);
13505   if (regno <= 31)
13506     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13507                             offset, 0);
13508   else
13509     result = new_loc_descr (DW_OP_bregx, regno, offset);
13510
13511   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13512     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13513
13514   return result;
13515 }
13516
13517 /* Return true if this RTL expression describes a base+offset calculation.  */
13518
13519 static inline int
13520 is_based_loc (const_rtx rtl)
13521 {
13522   return (GET_CODE (rtl) == PLUS
13523           && ((REG_P (XEXP (rtl, 0))
13524                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13525                && CONST_INT_P (XEXP (rtl, 1)))));
13526 }
13527
13528 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13529    failed.  */
13530
13531 static dw_loc_descr_ref
13532 tls_mem_loc_descriptor (rtx mem)
13533 {
13534   tree base;
13535   dw_loc_descr_ref loc_result;
13536
13537   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13538     return NULL;
13539
13540   base = get_base_address (MEM_EXPR (mem));
13541   if (base == NULL
13542       || TREE_CODE (base) != VAR_DECL
13543       || !DECL_THREAD_LOCAL_P (base))
13544     return NULL;
13545
13546   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13547   if (loc_result == NULL)
13548     return NULL;
13549
13550   if (INTVAL (MEM_OFFSET (mem)))
13551     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13552
13553   return loc_result;
13554 }
13555
13556 /* Output debug info about reason why we failed to expand expression as dwarf
13557    expression.  */
13558
13559 static void
13560 expansion_failed (tree expr, rtx rtl, char const *reason)
13561 {
13562   if (dump_file && (dump_flags & TDF_DETAILS))
13563     {
13564       fprintf (dump_file, "Failed to expand as dwarf: ");
13565       if (expr)
13566         print_generic_expr (dump_file, expr, dump_flags);
13567       if (rtl)
13568         {
13569           fprintf (dump_file, "\n");
13570           print_rtl (dump_file, rtl);
13571         }
13572       fprintf (dump_file, "\nReason: %s\n", reason);
13573     }
13574 }
13575
13576 /* Helper function for const_ok_for_output, called either directly
13577    or via for_each_rtx.  */
13578
13579 static int
13580 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13581 {
13582   rtx rtl = *rtlp;
13583
13584   if (GET_CODE (rtl) == UNSPEC)
13585     {
13586       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13587          we can't express it in the debug info.  */
13588 #ifdef ENABLE_CHECKING
13589       /* Don't complain about TLS UNSPECs, those are just too hard to
13590          delegitimize.  */
13591       if (XVECLEN (rtl, 0) != 1
13592           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13593           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13594           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13595           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13596         inform (current_function_decl
13597                 ? DECL_SOURCE_LOCATION (current_function_decl)
13598                 : UNKNOWN_LOCATION,
13599                 "non-delegitimized UNSPEC %d found in variable location",
13600                 XINT (rtl, 1));
13601 #endif
13602       expansion_failed (NULL_TREE, rtl,
13603                         "UNSPEC hasn't been delegitimized.\n");
13604       return 1;
13605     }
13606
13607   if (GET_CODE (rtl) != SYMBOL_REF)
13608     return 0;
13609
13610   if (CONSTANT_POOL_ADDRESS_P (rtl))
13611     {
13612       bool marked;
13613       get_pool_constant_mark (rtl, &marked);
13614       /* If all references to this pool constant were optimized away,
13615          it was not output and thus we can't represent it.  */
13616       if (!marked)
13617         {
13618           expansion_failed (NULL_TREE, rtl,
13619                             "Constant was removed from constant pool.\n");
13620           return 1;
13621         }
13622     }
13623
13624   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13625     return 1;
13626
13627   /* Avoid references to external symbols in debug info, on several targets
13628      the linker might even refuse to link when linking a shared library,
13629      and in many other cases the relocations for .debug_info/.debug_loc are
13630      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13631      to be defined within the same shared library or executable are fine.  */
13632   if (SYMBOL_REF_EXTERNAL_P (rtl))
13633     {
13634       tree decl = SYMBOL_REF_DECL (rtl);
13635
13636       if (decl == NULL || !targetm.binds_local_p (decl))
13637         {
13638           expansion_failed (NULL_TREE, rtl,
13639                             "Symbol not defined in current TU.\n");
13640           return 1;
13641         }
13642     }
13643
13644   return 0;
13645 }
13646
13647 /* Return true if constant RTL can be emitted in DW_OP_addr or
13648    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13649    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13650
13651 static bool
13652 const_ok_for_output (rtx rtl)
13653 {
13654   if (GET_CODE (rtl) == SYMBOL_REF)
13655     return const_ok_for_output_1 (&rtl, NULL) == 0;
13656
13657   if (GET_CODE (rtl) == CONST)
13658     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13659
13660   return true;
13661 }
13662
13663 /* The following routine converts the RTL for a variable or parameter
13664    (resident in memory) into an equivalent Dwarf representation of a
13665    mechanism for getting the address of that same variable onto the top of a
13666    hypothetical "address evaluation" stack.
13667
13668    When creating memory location descriptors, we are effectively transforming
13669    the RTL for a memory-resident object into its Dwarf postfix expression
13670    equivalent.  This routine recursively descends an RTL tree, turning
13671    it into Dwarf postfix code as it goes.
13672
13673    MODE is the mode of the memory reference, needed to handle some
13674    autoincrement addressing modes.
13675
13676    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13677    location list for RTL.
13678
13679    Return 0 if we can't represent the location.  */
13680
13681 static dw_loc_descr_ref
13682 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13683                     enum var_init_status initialized)
13684 {
13685   dw_loc_descr_ref mem_loc_result = NULL;
13686   enum dwarf_location_atom op;
13687   dw_loc_descr_ref op0, op1;
13688
13689   /* Note that for a dynamically sized array, the location we will generate a
13690      description of here will be the lowest numbered location which is
13691      actually within the array.  That's *not* necessarily the same as the
13692      zeroth element of the array.  */
13693
13694   rtl = targetm.delegitimize_address (rtl);
13695
13696   switch (GET_CODE (rtl))
13697     {
13698     case POST_INC:
13699     case POST_DEC:
13700     case POST_MODIFY:
13701       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13702
13703     case SUBREG:
13704       /* The case of a subreg may arise when we have a local (register)
13705          variable or a formal (register) parameter which doesn't quite fill
13706          up an entire register.  For now, just assume that it is
13707          legitimate to make the Dwarf info refer to the whole register which
13708          contains the given subreg.  */
13709       if (!subreg_lowpart_p (rtl))
13710         break;
13711       rtl = SUBREG_REG (rtl);
13712       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13713         break;
13714       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13715         break;
13716       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13717       break;
13718
13719     case REG:
13720       /* Whenever a register number forms a part of the description of the
13721          method for calculating the (dynamic) address of a memory resident
13722          object, DWARF rules require the register number be referred to as
13723          a "base register".  This distinction is not based in any way upon
13724          what category of register the hardware believes the given register
13725          belongs to.  This is strictly DWARF terminology we're dealing with
13726          here. Note that in cases where the location of a memory-resident
13727          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13728          OP_CONST (0)) the actual DWARF location descriptor that we generate
13729          may just be OP_BASEREG (basereg).  This may look deceptively like
13730          the object in question was allocated to a register (rather than in
13731          memory) so DWARF consumers need to be aware of the subtle
13732          distinction between OP_REG and OP_BASEREG.  */
13733       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13734         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13735       else if (stack_realign_drap
13736                && crtl->drap_reg
13737                && crtl->args.internal_arg_pointer == rtl
13738                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13739         {
13740           /* If RTL is internal_arg_pointer, which has been optimized
13741              out, use DRAP instead.  */
13742           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13743                                             VAR_INIT_STATUS_INITIALIZED);
13744         }
13745       break;
13746
13747     case SIGN_EXTEND:
13748     case ZERO_EXTEND:
13749       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13750                                 VAR_INIT_STATUS_INITIALIZED);
13751       if (op0 == 0)
13752         break;
13753       else
13754         {
13755           int shift = DWARF2_ADDR_SIZE
13756                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13757           shift *= BITS_PER_UNIT;
13758           if (GET_CODE (rtl) == SIGN_EXTEND)
13759             op = DW_OP_shra;
13760           else
13761             op = DW_OP_shr;
13762           mem_loc_result = op0;
13763           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13764           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13765           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13766           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13767         }
13768       break;
13769
13770     case MEM:
13771       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13772                                            VAR_INIT_STATUS_INITIALIZED);
13773       if (mem_loc_result == NULL)
13774         mem_loc_result = tls_mem_loc_descriptor (rtl);
13775       if (mem_loc_result != 0)
13776         {
13777           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13778             {
13779               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13780               return 0;
13781             }
13782           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13783             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13784           else
13785             add_loc_descr (&mem_loc_result,
13786                            new_loc_descr (DW_OP_deref_size,
13787                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13788         }
13789       else
13790         {
13791           rtx new_rtl = avoid_constant_pool_reference (rtl);
13792           if (new_rtl != rtl)
13793             return mem_loc_descriptor (new_rtl, mode, initialized);
13794         }
13795       break;
13796
13797     case LO_SUM:
13798          rtl = XEXP (rtl, 1);
13799
13800       /* ... fall through ...  */
13801
13802     case LABEL_REF:
13803       /* Some ports can transform a symbol ref into a label ref, because
13804          the symbol ref is too far away and has to be dumped into a constant
13805          pool.  */
13806     case CONST:
13807     case SYMBOL_REF:
13808       if (GET_CODE (rtl) == SYMBOL_REF
13809           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13810         {
13811           dw_loc_descr_ref temp;
13812
13813           /* If this is not defined, we have no way to emit the data.  */
13814           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13815             break;
13816
13817           /* We used to emit DW_OP_addr here, but that's wrong, since
13818              DW_OP_addr should be relocated by the debug info consumer,
13819              while DW_OP_GNU_push_tls_address operand should not.  */
13820           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13821                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13822           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13823           temp->dw_loc_oprnd1.v.val_addr = rtl;
13824           temp->dtprel = true;
13825
13826           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13827           add_loc_descr (&mem_loc_result, temp);
13828
13829           break;
13830         }
13831
13832       if (!const_ok_for_output (rtl))
13833         break;
13834
13835     symref:
13836       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13837       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13838       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13839       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13840       break;
13841
13842     case CONCAT:
13843     case CONCATN:
13844     case VAR_LOCATION:
13845     case DEBUG_IMPLICIT_PTR:
13846       expansion_failed (NULL_TREE, rtl,
13847                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13848       return 0;
13849
13850     case PRE_MODIFY:
13851       /* Extract the PLUS expression nested inside and fall into
13852          PLUS code below.  */
13853       rtl = XEXP (rtl, 1);
13854       goto plus;
13855
13856     case PRE_INC:
13857     case PRE_DEC:
13858       /* Turn these into a PLUS expression and fall into the PLUS code
13859          below.  */
13860       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13861                           GEN_INT (GET_CODE (rtl) == PRE_INC
13862                                    ? GET_MODE_UNIT_SIZE (mode)
13863                                    : -GET_MODE_UNIT_SIZE (mode)));
13864
13865       /* ... fall through ...  */
13866
13867     case PLUS:
13868     plus:
13869       if (is_based_loc (rtl))
13870         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13871                                           INTVAL (XEXP (rtl, 1)),
13872                                           VAR_INIT_STATUS_INITIALIZED);
13873       else
13874         {
13875           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13876                                                VAR_INIT_STATUS_INITIALIZED);
13877           if (mem_loc_result == 0)
13878             break;
13879
13880           if (CONST_INT_P (XEXP (rtl, 1)))
13881             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13882           else
13883             {
13884               dw_loc_descr_ref mem_loc_result2
13885                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13886                                       VAR_INIT_STATUS_INITIALIZED);
13887               if (mem_loc_result2 == 0)
13888                 break;
13889               add_loc_descr (&mem_loc_result, mem_loc_result2);
13890               add_loc_descr (&mem_loc_result,
13891                              new_loc_descr (DW_OP_plus, 0, 0));
13892             }
13893         }
13894       break;
13895
13896     /* If a pseudo-reg is optimized away, it is possible for it to
13897        be replaced with a MEM containing a multiply or shift.  */
13898     case MINUS:
13899       op = DW_OP_minus;
13900       goto do_binop;
13901
13902     case MULT:
13903       op = DW_OP_mul;
13904       goto do_binop;
13905
13906     case DIV:
13907       op = DW_OP_div;
13908       goto do_binop;
13909
13910     case UMOD:
13911       op = DW_OP_mod;
13912       goto do_binop;
13913
13914     case ASHIFT:
13915       op = DW_OP_shl;
13916       goto do_binop;
13917
13918     case ASHIFTRT:
13919       op = DW_OP_shra;
13920       goto do_binop;
13921
13922     case LSHIFTRT:
13923       op = DW_OP_shr;
13924       goto do_binop;
13925
13926     case AND:
13927       op = DW_OP_and;
13928       goto do_binop;
13929
13930     case IOR:
13931       op = DW_OP_or;
13932       goto do_binop;
13933
13934     case XOR:
13935       op = DW_OP_xor;
13936       goto do_binop;
13937
13938     do_binop:
13939       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13940                                 VAR_INIT_STATUS_INITIALIZED);
13941       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13942                                 VAR_INIT_STATUS_INITIALIZED);
13943
13944       if (op0 == 0 || op1 == 0)
13945         break;
13946
13947       mem_loc_result = op0;
13948       add_loc_descr (&mem_loc_result, op1);
13949       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13950       break;
13951
13952     case MOD:
13953       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13954                                 VAR_INIT_STATUS_INITIALIZED);
13955       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13956                                 VAR_INIT_STATUS_INITIALIZED);
13957
13958       if (op0 == 0 || op1 == 0)
13959         break;
13960
13961       mem_loc_result = op0;
13962       add_loc_descr (&mem_loc_result, op1);
13963       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13964       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13965       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13966       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13967       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13968       break;
13969
13970     case NOT:
13971       op = DW_OP_not;
13972       goto do_unop;
13973
13974     case ABS:
13975       op = DW_OP_abs;
13976       goto do_unop;
13977
13978     case NEG:
13979       op = DW_OP_neg;
13980       goto do_unop;
13981
13982     do_unop:
13983       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13984                                 VAR_INIT_STATUS_INITIALIZED);
13985
13986       if (op0 == 0)
13987         break;
13988
13989       mem_loc_result = op0;
13990       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13991       break;
13992
13993     case CONST_INT:
13994       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13995       break;
13996
13997     case EQ:
13998       op = DW_OP_eq;
13999       goto do_scompare;
14000
14001     case GE:
14002       op = DW_OP_ge;
14003       goto do_scompare;
14004
14005     case GT:
14006       op = DW_OP_gt;
14007       goto do_scompare;
14008
14009     case LE:
14010       op = DW_OP_le;
14011       goto do_scompare;
14012
14013     case LT:
14014       op = DW_OP_lt;
14015       goto do_scompare;
14016
14017     case NE:
14018       op = DW_OP_ne;
14019       goto do_scompare;
14020
14021     do_scompare:
14022       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14023           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14024         break;
14025       else
14026         {
14027           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14028
14029           if (op_mode == VOIDmode)
14030             op_mode = GET_MODE (XEXP (rtl, 1));
14031           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14032             break;
14033
14034           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14035                                     VAR_INIT_STATUS_INITIALIZED);
14036           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14037                                     VAR_INIT_STATUS_INITIALIZED);
14038
14039           if (op0 == 0 || op1 == 0)
14040             break;
14041
14042           if (op_mode != VOIDmode
14043               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14044             {
14045               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14046               shift *= BITS_PER_UNIT;
14047               /* For eq/ne, if the operands are known to be zero-extended,
14048                  there is no need to do the fancy shifting up.  */
14049               if (op == DW_OP_eq || op == DW_OP_ne)
14050                 {
14051                   dw_loc_descr_ref last0, last1;
14052                   for (last0 = op0;
14053                        last0->dw_loc_next != NULL;
14054                        last0 = last0->dw_loc_next)
14055                     ;
14056                   for (last1 = op1;
14057                        last1->dw_loc_next != NULL;
14058                        last1 = last1->dw_loc_next)
14059                     ;
14060                   /* deref_size zero extends, and for constants we can check
14061                      whether they are zero extended or not.  */
14062                   if (((last0->dw_loc_opc == DW_OP_deref_size
14063                         && last0->dw_loc_oprnd1.v.val_int
14064                            <= GET_MODE_SIZE (op_mode))
14065                        || (CONST_INT_P (XEXP (rtl, 0))
14066                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14067                                == (INTVAL (XEXP (rtl, 0))
14068                                    & GET_MODE_MASK (op_mode))))
14069                       && ((last1->dw_loc_opc == DW_OP_deref_size
14070                            && last1->dw_loc_oprnd1.v.val_int
14071                               <= GET_MODE_SIZE (op_mode))
14072                           || (CONST_INT_P (XEXP (rtl, 1))
14073                               && (unsigned HOST_WIDE_INT)
14074                                  INTVAL (XEXP (rtl, 1))
14075                                  == (INTVAL (XEXP (rtl, 1))
14076                                      & GET_MODE_MASK (op_mode)))))
14077                     goto do_compare;
14078                 }
14079               add_loc_descr (&op0, int_loc_descriptor (shift));
14080               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14081               if (CONST_INT_P (XEXP (rtl, 1)))
14082                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14083               else
14084                 {
14085                   add_loc_descr (&op1, int_loc_descriptor (shift));
14086                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14087                 }
14088             }
14089         }
14090
14091     do_compare:
14092       mem_loc_result = op0;
14093       add_loc_descr (&mem_loc_result, op1);
14094       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14095       if (STORE_FLAG_VALUE != 1)
14096         {
14097           add_loc_descr (&mem_loc_result,
14098                          int_loc_descriptor (STORE_FLAG_VALUE));
14099           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14100         }
14101       break;
14102
14103     case GEU:
14104       op = DW_OP_ge;
14105       goto do_ucompare;
14106
14107     case GTU:
14108       op = DW_OP_gt;
14109       goto do_ucompare;
14110
14111     case LEU:
14112       op = DW_OP_le;
14113       goto do_ucompare;
14114
14115     case LTU:
14116       op = DW_OP_lt;
14117       goto do_ucompare;
14118
14119     do_ucompare:
14120       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14121           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14122         break;
14123       else
14124         {
14125           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14126
14127           if (op_mode == VOIDmode)
14128             op_mode = GET_MODE (XEXP (rtl, 1));
14129           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14130             break;
14131
14132           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14133                                     VAR_INIT_STATUS_INITIALIZED);
14134           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14135                                     VAR_INIT_STATUS_INITIALIZED);
14136
14137           if (op0 == 0 || op1 == 0)
14138             break;
14139
14140           if (op_mode != VOIDmode
14141               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14142             {
14143               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14144               dw_loc_descr_ref last0, last1;
14145               for (last0 = op0;
14146                    last0->dw_loc_next != NULL;
14147                    last0 = last0->dw_loc_next)
14148                 ;
14149               for (last1 = op1;
14150                    last1->dw_loc_next != NULL;
14151                    last1 = last1->dw_loc_next)
14152                 ;
14153               if (CONST_INT_P (XEXP (rtl, 0)))
14154                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14155               /* deref_size zero extends, so no need to mask it again.  */
14156               else if (last0->dw_loc_opc != DW_OP_deref_size
14157                        || last0->dw_loc_oprnd1.v.val_int
14158                           > GET_MODE_SIZE (op_mode))
14159                 {
14160                   add_loc_descr (&op0, int_loc_descriptor (mask));
14161                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14162                 }
14163               if (CONST_INT_P (XEXP (rtl, 1)))
14164                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14165               /* deref_size zero extends, so no need to mask it again.  */
14166               else if (last1->dw_loc_opc != DW_OP_deref_size
14167                        || last1->dw_loc_oprnd1.v.val_int
14168                           > GET_MODE_SIZE (op_mode))
14169                 {
14170                   add_loc_descr (&op1, int_loc_descriptor (mask));
14171                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14172                 }
14173             }
14174           else
14175             {
14176               HOST_WIDE_INT bias = 1;
14177               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14178               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14179               if (CONST_INT_P (XEXP (rtl, 1)))
14180                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14181                                           + INTVAL (XEXP (rtl, 1)));
14182               else
14183                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14184                                                     bias, 0));
14185             }
14186         }
14187       goto do_compare;
14188
14189     case SMIN:
14190     case SMAX:
14191     case UMIN:
14192     case UMAX:
14193       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14194           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14195           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14196         break;
14197
14198       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14199                                 VAR_INIT_STATUS_INITIALIZED);
14200       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14201                                 VAR_INIT_STATUS_INITIALIZED);
14202
14203       if (op0 == 0 || op1 == 0)
14204         break;
14205
14206       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14207       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14208       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14209       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14210         {
14211           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14212             {
14213               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14214               add_loc_descr (&op0, int_loc_descriptor (mask));
14215               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14216               add_loc_descr (&op1, int_loc_descriptor (mask));
14217               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14218             }
14219           else
14220             {
14221               HOST_WIDE_INT bias = 1;
14222               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14223               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14224               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14225             }
14226         }
14227       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14228         {
14229           int shift = DWARF2_ADDR_SIZE
14230                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14231           shift *= BITS_PER_UNIT;
14232           add_loc_descr (&op0, int_loc_descriptor (shift));
14233           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14234           add_loc_descr (&op1, int_loc_descriptor (shift));
14235           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14236         }
14237
14238       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14239         op = DW_OP_lt;
14240       else
14241         op = DW_OP_gt;
14242       mem_loc_result = op0;
14243       add_loc_descr (&mem_loc_result, op1);
14244       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14245       {
14246         dw_loc_descr_ref bra_node, drop_node;
14247
14248         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14249         add_loc_descr (&mem_loc_result, bra_node);
14250         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14251         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14252         add_loc_descr (&mem_loc_result, drop_node);
14253         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14254         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14255       }
14256       break;
14257
14258     case ZERO_EXTRACT:
14259     case SIGN_EXTRACT:
14260       if (CONST_INT_P (XEXP (rtl, 1))
14261           && CONST_INT_P (XEXP (rtl, 2))
14262           && ((unsigned) INTVAL (XEXP (rtl, 1))
14263               + (unsigned) INTVAL (XEXP (rtl, 2))
14264               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14265           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14266           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14267         {
14268           int shift, size;
14269           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14270                                     VAR_INIT_STATUS_INITIALIZED);
14271           if (op0 == 0)
14272             break;
14273           if (GET_CODE (rtl) == SIGN_EXTRACT)
14274             op = DW_OP_shra;
14275           else
14276             op = DW_OP_shr;
14277           mem_loc_result = op0;
14278           size = INTVAL (XEXP (rtl, 1));
14279           shift = INTVAL (XEXP (rtl, 2));
14280           if (BITS_BIG_ENDIAN)
14281             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14282                     - shift - size;
14283           if (shift + size != (int) DWARF2_ADDR_SIZE)
14284             {
14285               add_loc_descr (&mem_loc_result,
14286                              int_loc_descriptor (DWARF2_ADDR_SIZE
14287                                                  - shift - size));
14288               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14289             }
14290           if (size != (int) DWARF2_ADDR_SIZE)
14291             {
14292               add_loc_descr (&mem_loc_result,
14293                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14294               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14295             }
14296         }
14297       break;
14298
14299     case IF_THEN_ELSE:
14300       {
14301         dw_loc_descr_ref op2, bra_node, drop_node;
14302         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14303                                   VAR_INIT_STATUS_INITIALIZED);
14304         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14305                                   VAR_INIT_STATUS_INITIALIZED);
14306         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14307                                   VAR_INIT_STATUS_INITIALIZED);
14308         if (op0 == NULL || op1 == NULL || op2 == NULL)
14309           break;
14310
14311         mem_loc_result = op1;
14312         add_loc_descr (&mem_loc_result, op2);
14313         add_loc_descr (&mem_loc_result, op0);
14314         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14315         add_loc_descr (&mem_loc_result, bra_node);
14316         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14317         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14318         add_loc_descr (&mem_loc_result, drop_node);
14319         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14320         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14321       }
14322       break;
14323
14324     case COMPARE:
14325     case ROTATE:
14326     case ROTATERT:
14327     case TRUNCATE:
14328       /* In theory, we could implement the above.  */
14329       /* DWARF cannot represent the unsigned compare operations
14330          natively.  */
14331     case SS_MULT:
14332     case US_MULT:
14333     case SS_DIV:
14334     case US_DIV:
14335     case SS_PLUS:
14336     case US_PLUS:
14337     case SS_MINUS:
14338     case US_MINUS:
14339     case SS_NEG:
14340     case US_NEG:
14341     case SS_ABS:
14342     case SS_ASHIFT:
14343     case US_ASHIFT:
14344     case SS_TRUNCATE:
14345     case US_TRUNCATE:
14346     case UDIV:
14347     case UNORDERED:
14348     case ORDERED:
14349     case UNEQ:
14350     case UNGE:
14351     case UNGT:
14352     case UNLE:
14353     case UNLT:
14354     case LTGT:
14355     case FLOAT_EXTEND:
14356     case FLOAT_TRUNCATE:
14357     case FLOAT:
14358     case UNSIGNED_FLOAT:
14359     case FIX:
14360     case UNSIGNED_FIX:
14361     case FRACT_CONVERT:
14362     case UNSIGNED_FRACT_CONVERT:
14363     case SAT_FRACT:
14364     case UNSIGNED_SAT_FRACT:
14365     case SQRT:
14366     case BSWAP:
14367     case FFS:
14368     case CLZ:
14369     case CTZ:
14370     case POPCOUNT:
14371     case PARITY:
14372     case ASM_OPERANDS:
14373     case VEC_MERGE:
14374     case VEC_SELECT:
14375     case VEC_CONCAT:
14376     case VEC_DUPLICATE:
14377     case UNSPEC:
14378     case HIGH:
14379       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14380          can't express it in the debug info.  This can happen e.g. with some
14381          TLS UNSPECs.  */
14382       break;
14383
14384     case CONST_STRING:
14385       resolve_one_addr (&rtl, NULL);
14386       goto symref;
14387
14388     default:
14389 #ifdef ENABLE_CHECKING
14390       print_rtl (stderr, rtl);
14391       gcc_unreachable ();
14392 #else
14393       break;
14394 #endif
14395     }
14396
14397   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14398     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14399
14400   return mem_loc_result;
14401 }
14402
14403 /* Return a descriptor that describes the concatenation of two locations.
14404    This is typically a complex variable.  */
14405
14406 static dw_loc_descr_ref
14407 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14408 {
14409   dw_loc_descr_ref cc_loc_result = NULL;
14410   dw_loc_descr_ref x0_ref
14411     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14412   dw_loc_descr_ref x1_ref
14413     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14414
14415   if (x0_ref == 0 || x1_ref == 0)
14416     return 0;
14417
14418   cc_loc_result = x0_ref;
14419   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14420
14421   add_loc_descr (&cc_loc_result, x1_ref);
14422   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14423
14424   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14425     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14426
14427   return cc_loc_result;
14428 }
14429
14430 /* Return a descriptor that describes the concatenation of N
14431    locations.  */
14432
14433 static dw_loc_descr_ref
14434 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14435 {
14436   unsigned int i;
14437   dw_loc_descr_ref cc_loc_result = NULL;
14438   unsigned int n = XVECLEN (concatn, 0);
14439
14440   for (i = 0; i < n; ++i)
14441     {
14442       dw_loc_descr_ref ref;
14443       rtx x = XVECEXP (concatn, 0, i);
14444
14445       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14446       if (ref == NULL)
14447         return NULL;
14448
14449       add_loc_descr (&cc_loc_result, ref);
14450       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14451     }
14452
14453   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14454     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14455
14456   return cc_loc_result;
14457 }
14458
14459 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14460    for DEBUG_IMPLICIT_PTR RTL.  */
14461
14462 static dw_loc_descr_ref
14463 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14464 {
14465   dw_loc_descr_ref ret;
14466   dw_die_ref ref;
14467
14468   if (dwarf_strict)
14469     return NULL;
14470   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14471               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14472               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14473   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14474   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14475   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14476   if (ref)
14477     {
14478       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14479       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14480       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14481     }
14482   else
14483     {
14484       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14485       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14486     }
14487   return ret;
14488 }
14489
14490 /* Output a proper Dwarf location descriptor for a variable or parameter
14491    which is either allocated in a register or in a memory location.  For a
14492    register, we just generate an OP_REG and the register number.  For a
14493    memory location we provide a Dwarf postfix expression describing how to
14494    generate the (dynamic) address of the object onto the address stack.
14495
14496    MODE is mode of the decl if this loc_descriptor is going to be used in
14497    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14498    allowed, VOIDmode otherwise.
14499
14500    If we don't know how to describe it, return 0.  */
14501
14502 static dw_loc_descr_ref
14503 loc_descriptor (rtx rtl, enum machine_mode mode,
14504                 enum var_init_status initialized)
14505 {
14506   dw_loc_descr_ref loc_result = NULL;
14507
14508   switch (GET_CODE (rtl))
14509     {
14510     case SUBREG:
14511       /* The case of a subreg may arise when we have a local (register)
14512          variable or a formal (register) parameter which doesn't quite fill
14513          up an entire register.  For now, just assume that it is
14514          legitimate to make the Dwarf info refer to the whole register which
14515          contains the given subreg.  */
14516       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14517       break;
14518
14519     case REG:
14520       loc_result = reg_loc_descriptor (rtl, initialized);
14521       break;
14522
14523     case MEM:
14524       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14525                                        initialized);
14526       if (loc_result == NULL)
14527         loc_result = tls_mem_loc_descriptor (rtl);
14528       if (loc_result == NULL)
14529         {
14530           rtx new_rtl = avoid_constant_pool_reference (rtl);
14531           if (new_rtl != rtl)
14532             loc_result = loc_descriptor (new_rtl, mode, initialized);
14533         }
14534       break;
14535
14536     case CONCAT:
14537       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14538                                           initialized);
14539       break;
14540
14541     case CONCATN:
14542       loc_result = concatn_loc_descriptor (rtl, initialized);
14543       break;
14544
14545     case VAR_LOCATION:
14546       /* Single part.  */
14547       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14548         {
14549           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14550           if (GET_CODE (loc) == EXPR_LIST)
14551             loc = XEXP (loc, 0);
14552           loc_result = loc_descriptor (loc, mode, initialized);
14553           break;
14554         }
14555
14556       rtl = XEXP (rtl, 1);
14557       /* FALLTHRU */
14558
14559     case PARALLEL:
14560       {
14561         rtvec par_elems = XVEC (rtl, 0);
14562         int num_elem = GET_NUM_ELEM (par_elems);
14563         enum machine_mode mode;
14564         int i;
14565
14566         /* Create the first one, so we have something to add to.  */
14567         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14568                                      VOIDmode, initialized);
14569         if (loc_result == NULL)
14570           return NULL;
14571         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14572         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14573         for (i = 1; i < num_elem; i++)
14574           {
14575             dw_loc_descr_ref temp;
14576
14577             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14578                                    VOIDmode, initialized);
14579             if (temp == NULL)
14580               return NULL;
14581             add_loc_descr (&loc_result, temp);
14582             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14583             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14584           }
14585       }
14586       break;
14587
14588     case CONST_INT:
14589       if (mode != VOIDmode && mode != BLKmode)
14590         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14591                                                     INTVAL (rtl));
14592       break;
14593
14594     case CONST_DOUBLE:
14595       if (mode == VOIDmode)
14596         mode = GET_MODE (rtl);
14597
14598       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14599         {
14600           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14601
14602           /* Note that a CONST_DOUBLE rtx could represent either an integer
14603              or a floating-point constant.  A CONST_DOUBLE is used whenever
14604              the constant requires more than one word in order to be
14605              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14606           loc_result = new_loc_descr (DW_OP_implicit_value,
14607                                       GET_MODE_SIZE (mode), 0);
14608           if (SCALAR_FLOAT_MODE_P (mode))
14609             {
14610               unsigned int length = GET_MODE_SIZE (mode);
14611               unsigned char *array
14612                   = (unsigned char*) ggc_alloc_atomic (length);
14613
14614               insert_float (rtl, array);
14615               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14616               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14617               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14618               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14619             }
14620           else
14621             {
14622               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14623               loc_result->dw_loc_oprnd2.v.val_double
14624                 = rtx_to_double_int (rtl);
14625             }
14626         }
14627       break;
14628
14629     case CONST_VECTOR:
14630       if (mode == VOIDmode)
14631         mode = GET_MODE (rtl);
14632
14633       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14634         {
14635           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14636           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14637           unsigned char *array = (unsigned char *)
14638             ggc_alloc_atomic (length * elt_size);
14639           unsigned int i;
14640           unsigned char *p;
14641
14642           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14643           switch (GET_MODE_CLASS (mode))
14644             {
14645             case MODE_VECTOR_INT:
14646               for (i = 0, p = array; i < length; i++, p += elt_size)
14647                 {
14648                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14649                   double_int val = rtx_to_double_int (elt);
14650
14651                   if (elt_size <= sizeof (HOST_WIDE_INT))
14652                     insert_int (double_int_to_shwi (val), elt_size, p);
14653                   else
14654                     {
14655                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14656                       insert_double (val, p);
14657                     }
14658                 }
14659               break;
14660
14661             case MODE_VECTOR_FLOAT:
14662               for (i = 0, p = array; i < length; i++, p += elt_size)
14663                 {
14664                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14665                   insert_float (elt, p);
14666                 }
14667               break;
14668
14669             default:
14670               gcc_unreachable ();
14671             }
14672
14673           loc_result = new_loc_descr (DW_OP_implicit_value,
14674                                       length * elt_size, 0);
14675           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14676           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14677           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14678           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14679         }
14680       break;
14681
14682     case CONST:
14683       if (mode == VOIDmode
14684           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14685           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14686           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14687         {
14688           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14689           break;
14690         }
14691       /* FALLTHROUGH */
14692     case SYMBOL_REF:
14693       if (!const_ok_for_output (rtl))
14694         break;
14695     case LABEL_REF:
14696       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14697           && (dwarf_version >= 4 || !dwarf_strict))
14698         {
14699           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14700           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14701           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14702           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14703           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14704         }
14705       break;
14706
14707     case DEBUG_IMPLICIT_PTR:
14708       loc_result = implicit_ptr_descriptor (rtl, 0);
14709       break;
14710
14711     case PLUS:
14712       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14713           && CONST_INT_P (XEXP (rtl, 1)))
14714         {
14715           loc_result
14716             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14717           break;
14718         }
14719       /* FALLTHRU */
14720     default:
14721       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14722           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14723           && (dwarf_version >= 4 || !dwarf_strict))
14724         {
14725           /* Value expression.  */
14726           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14727           if (loc_result)
14728             add_loc_descr (&loc_result,
14729                            new_loc_descr (DW_OP_stack_value, 0, 0));
14730         }
14731       break;
14732     }
14733
14734   return loc_result;
14735 }
14736
14737 /* We need to figure out what section we should use as the base for the
14738    address ranges where a given location is valid.
14739    1. If this particular DECL has a section associated with it, use that.
14740    2. If this function has a section associated with it, use that.
14741    3. Otherwise, use the text section.
14742    XXX: If you split a variable across multiple sections, we won't notice.  */
14743
14744 static const char *
14745 secname_for_decl (const_tree decl)
14746 {
14747   const char *secname;
14748
14749   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14750     {
14751       tree sectree = DECL_SECTION_NAME (decl);
14752       secname = TREE_STRING_POINTER (sectree);
14753     }
14754   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14755     {
14756       tree sectree = DECL_SECTION_NAME (current_function_decl);
14757       secname = TREE_STRING_POINTER (sectree);
14758     }
14759   else if (cfun && in_cold_section_p)
14760     secname = crtl->subsections.cold_section_label;
14761   else
14762     secname = text_section_label;
14763
14764   return secname;
14765 }
14766
14767 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14768
14769 static bool
14770 decl_by_reference_p (tree decl)
14771 {
14772   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14773            || TREE_CODE (decl) == VAR_DECL)
14774           && DECL_BY_REFERENCE (decl));
14775 }
14776
14777 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14778    for VARLOC.  */
14779
14780 static dw_loc_descr_ref
14781 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14782                enum var_init_status initialized)
14783 {
14784   int have_address = 0;
14785   dw_loc_descr_ref descr;
14786   enum machine_mode mode;
14787
14788   if (want_address != 2)
14789     {
14790       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14791       /* Single part.  */
14792       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14793         {
14794           varloc = PAT_VAR_LOCATION_LOC (varloc);
14795           if (GET_CODE (varloc) == EXPR_LIST)
14796             varloc = XEXP (varloc, 0);
14797           mode = GET_MODE (varloc);
14798           if (MEM_P (varloc))
14799             {
14800               rtx addr = XEXP (varloc, 0);
14801               descr = mem_loc_descriptor (addr, mode, initialized);
14802               if (descr)
14803                 have_address = 1;
14804               else
14805                 {
14806                   rtx x = avoid_constant_pool_reference (varloc);
14807                   if (x != varloc)
14808                     descr = mem_loc_descriptor (x, mode, initialized);
14809                 }
14810             }
14811           else
14812             descr = mem_loc_descriptor (varloc, mode, initialized);
14813         }
14814       else
14815         return 0;
14816     }
14817   else
14818     {
14819       if (GET_CODE (varloc) == VAR_LOCATION)
14820         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14821       else
14822         mode = DECL_MODE (loc);
14823       descr = loc_descriptor (varloc, mode, initialized);
14824       have_address = 1;
14825     }
14826
14827   if (!descr)
14828     return 0;
14829
14830   if (want_address == 2 && !have_address
14831       && (dwarf_version >= 4 || !dwarf_strict))
14832     {
14833       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14834         {
14835           expansion_failed (loc, NULL_RTX,
14836                             "DWARF address size mismatch");
14837           return 0;
14838         }
14839       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14840       have_address = 1;
14841     }
14842   /* Show if we can't fill the request for an address.  */
14843   if (want_address && !have_address)
14844     {
14845       expansion_failed (loc, NULL_RTX,
14846                         "Want address and only have value");
14847       return 0;
14848     }
14849
14850   /* If we've got an address and don't want one, dereference.  */
14851   if (!want_address && have_address)
14852     {
14853       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14854       enum dwarf_location_atom op;
14855
14856       if (size > DWARF2_ADDR_SIZE || size == -1)
14857         {
14858           expansion_failed (loc, NULL_RTX,
14859                             "DWARF address size mismatch");
14860           return 0;
14861         }
14862       else if (size == DWARF2_ADDR_SIZE)
14863         op = DW_OP_deref;
14864       else
14865         op = DW_OP_deref_size;
14866
14867       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14868     }
14869
14870   return descr;
14871 }
14872
14873 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14874    if it is not possible.  */
14875
14876 static dw_loc_descr_ref
14877 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14878 {
14879   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14880     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14881   else if (dwarf_version >= 3 || !dwarf_strict)
14882     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14883   else
14884     return NULL;
14885 }
14886
14887 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14888    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14889
14890 static dw_loc_descr_ref
14891 dw_sra_loc_expr (tree decl, rtx loc)
14892 {
14893   rtx p;
14894   unsigned int padsize = 0;
14895   dw_loc_descr_ref descr, *descr_tail;
14896   unsigned HOST_WIDE_INT decl_size;
14897   rtx varloc;
14898   enum var_init_status initialized;
14899
14900   if (DECL_SIZE (decl) == NULL
14901       || !host_integerp (DECL_SIZE (decl), 1))
14902     return NULL;
14903
14904   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14905   descr = NULL;
14906   descr_tail = &descr;
14907
14908   for (p = loc; p; p = XEXP (p, 1))
14909     {
14910       unsigned int bitsize = decl_piece_bitsize (p);
14911       rtx loc_note = *decl_piece_varloc_ptr (p);
14912       dw_loc_descr_ref cur_descr;
14913       dw_loc_descr_ref *tail, last = NULL;
14914       unsigned int opsize = 0;
14915
14916       if (loc_note == NULL_RTX
14917           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14918         {
14919           padsize += bitsize;
14920           continue;
14921         }
14922       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14923       varloc = NOTE_VAR_LOCATION (loc_note);
14924       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14925       if (cur_descr == NULL)
14926         {
14927           padsize += bitsize;
14928           continue;
14929         }
14930
14931       /* Check that cur_descr either doesn't use
14932          DW_OP_*piece operations, or their sum is equal
14933          to bitsize.  Otherwise we can't embed it.  */
14934       for (tail = &cur_descr; *tail != NULL;
14935            tail = &(*tail)->dw_loc_next)
14936         if ((*tail)->dw_loc_opc == DW_OP_piece)
14937           {
14938             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14939                       * BITS_PER_UNIT;
14940             last = *tail;
14941           }
14942         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14943           {
14944             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14945             last = *tail;
14946           }
14947
14948       if (last != NULL && opsize != bitsize)
14949         {
14950           padsize += bitsize;
14951           continue;
14952         }
14953
14954       /* If there is a hole, add DW_OP_*piece after empty DWARF
14955          expression, which means that those bits are optimized out.  */
14956       if (padsize)
14957         {
14958           if (padsize > decl_size)
14959             return NULL;
14960           decl_size -= padsize;
14961           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14962           if (*descr_tail == NULL)
14963             return NULL;
14964           descr_tail = &(*descr_tail)->dw_loc_next;
14965           padsize = 0;
14966         }
14967       *descr_tail = cur_descr;
14968       descr_tail = tail;
14969       if (bitsize > decl_size)
14970         return NULL;
14971       decl_size -= bitsize;
14972       if (last == NULL)
14973         {
14974           HOST_WIDE_INT offset = 0;
14975           if (GET_CODE (varloc) == VAR_LOCATION
14976               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14977             {
14978               varloc = PAT_VAR_LOCATION_LOC (varloc);
14979               if (GET_CODE (varloc) == EXPR_LIST)
14980                 varloc = XEXP (varloc, 0);
14981             }
14982           do 
14983             {
14984               if (GET_CODE (varloc) == CONST
14985                   || GET_CODE (varloc) == SIGN_EXTEND
14986                   || GET_CODE (varloc) == ZERO_EXTEND)
14987                 varloc = XEXP (varloc, 0);
14988               else if (GET_CODE (varloc) == SUBREG)
14989                 varloc = SUBREG_REG (varloc);
14990               else
14991                 break;
14992             }
14993           while (1);
14994           /* DW_OP_bit_size offset should be zero for register
14995              or implicit location descriptions and empty location
14996              descriptions, but for memory addresses needs big endian
14997              adjustment.  */
14998           if (MEM_P (varloc))
14999             {
15000               unsigned HOST_WIDE_INT memsize
15001                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15002               if (memsize != bitsize)
15003                 {
15004                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15005                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15006                     return NULL;
15007                   if (memsize < bitsize)
15008                     return NULL;
15009                   if (BITS_BIG_ENDIAN)
15010                     offset = memsize - bitsize;
15011                 }
15012             }
15013
15014           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15015           if (*descr_tail == NULL)
15016             return NULL;
15017           descr_tail = &(*descr_tail)->dw_loc_next;
15018         }
15019     }
15020
15021   /* If there were any non-empty expressions, add padding till the end of
15022      the decl.  */
15023   if (descr != NULL && decl_size != 0)
15024     {
15025       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15026       if (*descr_tail == NULL)
15027         return NULL;
15028     }
15029   return descr;
15030 }
15031
15032 /* Return the dwarf representation of the location list LOC_LIST of
15033    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15034    function.  */
15035
15036 static dw_loc_list_ref
15037 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15038 {
15039   const char *endname, *secname;
15040   rtx varloc;
15041   enum var_init_status initialized;
15042   struct var_loc_node *node;
15043   dw_loc_descr_ref descr;
15044   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15045   dw_loc_list_ref list = NULL;
15046   dw_loc_list_ref *listp = &list;
15047
15048   /* Now that we know what section we are using for a base,
15049      actually construct the list of locations.
15050      The first location information is what is passed to the
15051      function that creates the location list, and the remaining
15052      locations just get added on to that list.
15053      Note that we only know the start address for a location
15054      (IE location changes), so to build the range, we use
15055      the range [current location start, next location start].
15056      This means we have to special case the last node, and generate
15057      a range of [last location start, end of function label].  */
15058
15059   secname = secname_for_decl (decl);
15060
15061   for (node = loc_list->first; node; node = node->next)
15062     if (GET_CODE (node->loc) == EXPR_LIST
15063         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15064       {
15065         if (GET_CODE (node->loc) == EXPR_LIST)
15066           {
15067             /* This requires DW_OP_{,bit_}piece, which is not usable
15068                inside DWARF expressions.  */
15069             if (want_address != 2)
15070               continue;
15071             descr = dw_sra_loc_expr (decl, node->loc);
15072             if (descr == NULL)
15073               continue;
15074           }
15075         else
15076           {
15077             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15078             varloc = NOTE_VAR_LOCATION (node->loc);
15079             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15080           }
15081         if (descr)
15082           {
15083             /* The variable has a location between NODE->LABEL and
15084                NODE->NEXT->LABEL.  */
15085             if (node->next)
15086               endname = node->next->label;
15087             /* If the variable has a location at the last label
15088                it keeps its location until the end of function.  */
15089             else if (!current_function_decl)
15090               endname = text_end_label;
15091             else
15092               {
15093                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15094                                              current_function_funcdef_no);
15095                 endname = ggc_strdup (label_id);
15096               }
15097
15098             *listp = new_loc_list (descr, node->label, endname, secname);
15099             listp = &(*listp)->dw_loc_next;
15100           }
15101       }
15102
15103   /* Try to avoid the overhead of a location list emitting a location
15104      expression instead, but only if we didn't have more than one
15105      location entry in the first place.  If some entries were not
15106      representable, we don't want to pretend a single entry that was
15107      applies to the entire scope in which the variable is
15108      available.  */
15109   if (list && loc_list->first->next)
15110     gen_llsym (list);
15111
15112   return list;
15113 }
15114
15115 /* Return if the loc_list has only single element and thus can be represented
15116    as location description.   */
15117
15118 static bool
15119 single_element_loc_list_p (dw_loc_list_ref list)
15120 {
15121   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15122   return !list->ll_symbol;
15123 }
15124
15125 /* To each location in list LIST add loc descr REF.  */
15126
15127 static void
15128 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15129 {
15130   dw_loc_descr_ref copy;
15131   add_loc_descr (&list->expr, ref);
15132   list = list->dw_loc_next;
15133   while (list)
15134     {
15135       copy = ggc_alloc_dw_loc_descr_node ();
15136       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15137       add_loc_descr (&list->expr, copy);
15138       while (copy->dw_loc_next)
15139         {
15140           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15141           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15142           copy->dw_loc_next = new_copy;
15143           copy = new_copy;
15144         }
15145       list = list->dw_loc_next;
15146     }
15147 }
15148
15149 /* Given two lists RET and LIST
15150    produce location list that is result of adding expression in LIST
15151    to expression in RET on each possition in program.
15152    Might be destructive on both RET and LIST.
15153
15154    TODO: We handle only simple cases of RET or LIST having at most one
15155    element. General case would inolve sorting the lists in program order
15156    and merging them that will need some additional work.
15157    Adding that will improve quality of debug info especially for SRA-ed
15158    structures.  */
15159
15160 static void
15161 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15162 {
15163   if (!list)
15164     return;
15165   if (!*ret)
15166     {
15167       *ret = list;
15168       return;
15169     }
15170   if (!list->dw_loc_next)
15171     {
15172       add_loc_descr_to_each (*ret, list->expr);
15173       return;
15174     }
15175   if (!(*ret)->dw_loc_next)
15176     {
15177       add_loc_descr_to_each (list, (*ret)->expr);
15178       *ret = list;
15179       return;
15180     }
15181   expansion_failed (NULL_TREE, NULL_RTX,
15182                     "Don't know how to merge two non-trivial"
15183                     " location lists.\n");
15184   *ret = NULL;
15185   return;
15186 }
15187
15188 /* LOC is constant expression.  Try a luck, look it up in constant
15189    pool and return its loc_descr of its address.  */
15190
15191 static dw_loc_descr_ref
15192 cst_pool_loc_descr (tree loc)
15193 {
15194   /* Get an RTL for this, if something has been emitted.  */
15195   rtx rtl = lookup_constant_def (loc);
15196   enum machine_mode mode;
15197
15198   if (!rtl || !MEM_P (rtl))
15199     {
15200       gcc_assert (!rtl);
15201       return 0;
15202     }
15203   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15204
15205   /* TODO: We might get more coverage if we was actually delaying expansion
15206      of all expressions till end of compilation when constant pools are fully
15207      populated.  */
15208   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15209     {
15210       expansion_failed (loc, NULL_RTX,
15211                         "CST value in contant pool but not marked.");
15212       return 0;
15213     }
15214   mode = GET_MODE (rtl);
15215   rtl = XEXP (rtl, 0);
15216   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15217 }
15218
15219 /* Return dw_loc_list representing address of addr_expr LOC
15220    by looking for innder INDIRECT_REF expression and turing it
15221    into simple arithmetics.  */
15222
15223 static dw_loc_list_ref
15224 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15225 {
15226   tree obj, offset;
15227   HOST_WIDE_INT bitsize, bitpos, bytepos;
15228   enum machine_mode mode;
15229   int volatilep;
15230   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15231   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15232
15233   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15234                              &bitsize, &bitpos, &offset, &mode,
15235                              &unsignedp, &volatilep, false);
15236   STRIP_NOPS (obj);
15237   if (bitpos % BITS_PER_UNIT)
15238     {
15239       expansion_failed (loc, NULL_RTX, "bitfield access");
15240       return 0;
15241     }
15242   if (!INDIRECT_REF_P (obj))
15243     {
15244       expansion_failed (obj,
15245                         NULL_RTX, "no indirect ref in inner refrence");
15246       return 0;
15247     }
15248   if (!offset && !bitpos)
15249     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15250   else if (toplev
15251            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15252            && (dwarf_version >= 4 || !dwarf_strict))
15253     {
15254       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15255       if (!list_ret)
15256         return 0;
15257       if (offset)
15258         {
15259           /* Variable offset.  */
15260           list_ret1 = loc_list_from_tree (offset, 0);
15261           if (list_ret1 == 0)
15262             return 0;
15263           add_loc_list (&list_ret, list_ret1);
15264           if (!list_ret)
15265             return 0;
15266           add_loc_descr_to_each (list_ret,
15267                                  new_loc_descr (DW_OP_plus, 0, 0));
15268         }
15269       bytepos = bitpos / BITS_PER_UNIT;
15270       if (bytepos > 0)
15271         add_loc_descr_to_each (list_ret,
15272                                new_loc_descr (DW_OP_plus_uconst,
15273                                               bytepos, 0));
15274       else if (bytepos < 0)
15275         loc_list_plus_const (list_ret, bytepos);
15276       add_loc_descr_to_each (list_ret,
15277                              new_loc_descr (DW_OP_stack_value, 0, 0));
15278     }
15279   return list_ret;
15280 }
15281
15282
15283 /* Generate Dwarf location list representing LOC.
15284    If WANT_ADDRESS is false, expression computing LOC will be computed
15285    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15286    if WANT_ADDRESS is 2, expression computing address useable in location
15287      will be returned (i.e. DW_OP_reg can be used
15288      to refer to register values).  */
15289
15290 static dw_loc_list_ref
15291 loc_list_from_tree (tree loc, int want_address)
15292 {
15293   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15294   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15295   int have_address = 0;
15296   enum dwarf_location_atom op;
15297
15298   /* ??? Most of the time we do not take proper care for sign/zero
15299      extending the values properly.  Hopefully this won't be a real
15300      problem...  */
15301
15302   switch (TREE_CODE (loc))
15303     {
15304     case ERROR_MARK:
15305       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15306       return 0;
15307
15308     case PLACEHOLDER_EXPR:
15309       /* This case involves extracting fields from an object to determine the
15310          position of other fields.  We don't try to encode this here.  The
15311          only user of this is Ada, which encodes the needed information using
15312          the names of types.  */
15313       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15314       return 0;
15315
15316     case CALL_EXPR:
15317       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15318       /* There are no opcodes for these operations.  */
15319       return 0;
15320
15321     case PREINCREMENT_EXPR:
15322     case PREDECREMENT_EXPR:
15323     case POSTINCREMENT_EXPR:
15324     case POSTDECREMENT_EXPR:
15325       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15326       /* There are no opcodes for these operations.  */
15327       return 0;
15328
15329     case ADDR_EXPR:
15330       /* If we already want an address, see if there is INDIRECT_REF inside
15331          e.g. for &this->field.  */
15332       if (want_address)
15333         {
15334           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15335                        (loc, want_address == 2);
15336           if (list_ret)
15337             have_address = 1;
15338           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15339                    && (ret = cst_pool_loc_descr (loc)))
15340             have_address = 1;
15341         }
15342         /* Otherwise, process the argument and look for the address.  */
15343       if (!list_ret && !ret)
15344         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15345       else
15346         {
15347           if (want_address)
15348             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15349           return NULL;
15350         }
15351       break;
15352
15353     case VAR_DECL:
15354       if (DECL_THREAD_LOCAL_P (loc))
15355         {
15356           rtx rtl;
15357           enum dwarf_location_atom first_op;
15358           enum dwarf_location_atom second_op;
15359           bool dtprel = false;
15360
15361           if (targetm.have_tls)
15362             {
15363               /* If this is not defined, we have no way to emit the
15364                  data.  */
15365               if (!targetm.asm_out.output_dwarf_dtprel)
15366                 return 0;
15367
15368                /* The way DW_OP_GNU_push_tls_address is specified, we
15369                   can only look up addresses of objects in the current
15370                   module.  We used DW_OP_addr as first op, but that's
15371                   wrong, because DW_OP_addr is relocated by the debug
15372                   info consumer, while DW_OP_GNU_push_tls_address
15373                   operand shouldn't be.  */
15374               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15375                 return 0;
15376               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15377               dtprel = true;
15378               second_op = DW_OP_GNU_push_tls_address;
15379             }
15380           else
15381             {
15382               if (!targetm.emutls.debug_form_tls_address
15383                   || !(dwarf_version >= 3 || !dwarf_strict))
15384                 return 0;
15385               /* We stuffed the control variable into the DECL_VALUE_EXPR
15386                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15387                  no longer appear in gimple code.  We used the control
15388                  variable in specific so that we could pick it up here.  */
15389               loc = DECL_VALUE_EXPR (loc);
15390               first_op = DW_OP_addr;
15391               second_op = DW_OP_form_tls_address;
15392             }
15393
15394           rtl = rtl_for_decl_location (loc);
15395           if (rtl == NULL_RTX)
15396             return 0;
15397
15398           if (!MEM_P (rtl))
15399             return 0;
15400           rtl = XEXP (rtl, 0);
15401           if (! CONSTANT_P (rtl))
15402             return 0;
15403
15404           ret = new_loc_descr (first_op, 0, 0);
15405           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15406           ret->dw_loc_oprnd1.v.val_addr = rtl;
15407           ret->dtprel = dtprel;
15408
15409           ret1 = new_loc_descr (second_op, 0, 0);
15410           add_loc_descr (&ret, ret1);
15411
15412           have_address = 1;
15413           break;
15414         }
15415       /* FALLTHRU */
15416
15417     case PARM_DECL:
15418       if (DECL_HAS_VALUE_EXPR_P (loc))
15419         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15420                                    want_address);
15421       /* FALLTHRU */
15422
15423     case RESULT_DECL:
15424     case FUNCTION_DECL:
15425       {
15426         rtx rtl;
15427         var_loc_list *loc_list = lookup_decl_loc (loc);
15428
15429         if (loc_list && loc_list->first)
15430           {
15431             list_ret = dw_loc_list (loc_list, loc, want_address);
15432             have_address = want_address != 0;
15433             break;
15434           }
15435         rtl = rtl_for_decl_location (loc);
15436         if (rtl == NULL_RTX)
15437           {
15438             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15439             return 0;
15440           }
15441         else if (CONST_INT_P (rtl))
15442           {
15443             HOST_WIDE_INT val = INTVAL (rtl);
15444             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15445               val &= GET_MODE_MASK (DECL_MODE (loc));
15446             ret = int_loc_descriptor (val);
15447           }
15448         else if (GET_CODE (rtl) == CONST_STRING)
15449           {
15450             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15451             return 0;
15452           }
15453         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15454           {
15455             ret = new_loc_descr (DW_OP_addr, 0, 0);
15456             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15457             ret->dw_loc_oprnd1.v.val_addr = rtl;
15458           }
15459         else
15460           {
15461             enum machine_mode mode;
15462
15463             /* Certain constructs can only be represented at top-level.  */
15464             if (want_address == 2)
15465               {
15466                 ret = loc_descriptor (rtl, VOIDmode,
15467                                       VAR_INIT_STATUS_INITIALIZED);
15468                 have_address = 1;
15469               }
15470             else
15471               {
15472                 mode = GET_MODE (rtl);
15473                 if (MEM_P (rtl))
15474                   {
15475                     rtl = XEXP (rtl, 0);
15476                     have_address = 1;
15477                   }
15478                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15479               }
15480             if (!ret)
15481               expansion_failed (loc, rtl,
15482                                 "failed to produce loc descriptor for rtl");
15483           }
15484       }
15485       break;
15486
15487     case MEM_REF:
15488       /* ??? FIXME.  */
15489       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15490         return 0;
15491       /* Fallthru.  */
15492     case INDIRECT_REF:
15493       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15494       have_address = 1;
15495       break;
15496
15497     case COMPOUND_EXPR:
15498       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15499
15500     CASE_CONVERT:
15501     case VIEW_CONVERT_EXPR:
15502     case SAVE_EXPR:
15503     case MODIFY_EXPR:
15504       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15505
15506     case COMPONENT_REF:
15507     case BIT_FIELD_REF:
15508     case ARRAY_REF:
15509     case ARRAY_RANGE_REF:
15510     case REALPART_EXPR:
15511     case IMAGPART_EXPR:
15512       {
15513         tree obj, offset;
15514         HOST_WIDE_INT bitsize, bitpos, bytepos;
15515         enum machine_mode mode;
15516         int volatilep;
15517         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15518
15519         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15520                                    &unsignedp, &volatilep, false);
15521
15522         gcc_assert (obj != loc);
15523
15524         list_ret = loc_list_from_tree (obj,
15525                                        want_address == 2
15526                                        && !bitpos && !offset ? 2 : 1);
15527         /* TODO: We can extract value of the small expression via shifting even
15528            for nonzero bitpos.  */
15529         if (list_ret == 0)
15530           return 0;
15531         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15532           {
15533             expansion_failed (loc, NULL_RTX,
15534                               "bitfield access");
15535             return 0;
15536           }
15537
15538         if (offset != NULL_TREE)
15539           {
15540             /* Variable offset.  */
15541             list_ret1 = loc_list_from_tree (offset, 0);
15542             if (list_ret1 == 0)
15543               return 0;
15544             add_loc_list (&list_ret, list_ret1);
15545             if (!list_ret)
15546               return 0;
15547             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15548           }
15549
15550         bytepos = bitpos / BITS_PER_UNIT;
15551         if (bytepos > 0)
15552           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15553         else if (bytepos < 0)
15554           loc_list_plus_const (list_ret, bytepos);
15555
15556         have_address = 1;
15557         break;
15558       }
15559
15560     case INTEGER_CST:
15561       if ((want_address || !host_integerp (loc, 0))
15562           && (ret = cst_pool_loc_descr (loc)))
15563         have_address = 1;
15564       else if (want_address == 2
15565                && host_integerp (loc, 0)
15566                && (ret = address_of_int_loc_descriptor
15567                            (int_size_in_bytes (TREE_TYPE (loc)),
15568                             tree_low_cst (loc, 0))))
15569         have_address = 1;
15570       else if (host_integerp (loc, 0))
15571         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15572       else
15573         {
15574           expansion_failed (loc, NULL_RTX,
15575                             "Integer operand is not host integer");
15576           return 0;
15577         }
15578       break;
15579
15580     case CONSTRUCTOR:
15581     case REAL_CST:
15582     case STRING_CST:
15583     case COMPLEX_CST:
15584       if ((ret = cst_pool_loc_descr (loc)))
15585         have_address = 1;
15586       else
15587       /* We can construct small constants here using int_loc_descriptor.  */
15588         expansion_failed (loc, NULL_RTX,
15589                           "constructor or constant not in constant pool");
15590       break;
15591
15592     case TRUTH_AND_EXPR:
15593     case TRUTH_ANDIF_EXPR:
15594     case BIT_AND_EXPR:
15595       op = DW_OP_and;
15596       goto do_binop;
15597
15598     case TRUTH_XOR_EXPR:
15599     case BIT_XOR_EXPR:
15600       op = DW_OP_xor;
15601       goto do_binop;
15602
15603     case TRUTH_OR_EXPR:
15604     case TRUTH_ORIF_EXPR:
15605     case BIT_IOR_EXPR:
15606       op = DW_OP_or;
15607       goto do_binop;
15608
15609     case FLOOR_DIV_EXPR:
15610     case CEIL_DIV_EXPR:
15611     case ROUND_DIV_EXPR:
15612     case TRUNC_DIV_EXPR:
15613       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15614         return 0;
15615       op = DW_OP_div;
15616       goto do_binop;
15617
15618     case MINUS_EXPR:
15619       op = DW_OP_minus;
15620       goto do_binop;
15621
15622     case FLOOR_MOD_EXPR:
15623     case CEIL_MOD_EXPR:
15624     case ROUND_MOD_EXPR:
15625     case TRUNC_MOD_EXPR:
15626       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15627         {
15628           op = DW_OP_mod;
15629           goto do_binop;
15630         }
15631       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15632       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15633       if (list_ret == 0 || list_ret1 == 0)
15634         return 0;
15635
15636       add_loc_list (&list_ret, list_ret1);
15637       if (list_ret == 0)
15638         return 0;
15639       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15640       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15641       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15642       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15643       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15644       break;
15645
15646     case MULT_EXPR:
15647       op = DW_OP_mul;
15648       goto do_binop;
15649
15650     case LSHIFT_EXPR:
15651       op = DW_OP_shl;
15652       goto do_binop;
15653
15654     case RSHIFT_EXPR:
15655       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15656       goto do_binop;
15657
15658     case POINTER_PLUS_EXPR:
15659     case PLUS_EXPR:
15660       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15661         {
15662           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15663           if (list_ret == 0)
15664             return 0;
15665
15666           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15667           break;
15668         }
15669
15670       op = DW_OP_plus;
15671       goto do_binop;
15672
15673     case LE_EXPR:
15674       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15675         return 0;
15676
15677       op = DW_OP_le;
15678       goto do_binop;
15679
15680     case GE_EXPR:
15681       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15682         return 0;
15683
15684       op = DW_OP_ge;
15685       goto do_binop;
15686
15687     case LT_EXPR:
15688       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15689         return 0;
15690
15691       op = DW_OP_lt;
15692       goto do_binop;
15693
15694     case GT_EXPR:
15695       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15696         return 0;
15697
15698       op = DW_OP_gt;
15699       goto do_binop;
15700
15701     case EQ_EXPR:
15702       op = DW_OP_eq;
15703       goto do_binop;
15704
15705     case NE_EXPR:
15706       op = DW_OP_ne;
15707       goto do_binop;
15708
15709     do_binop:
15710       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15711       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15712       if (list_ret == 0 || list_ret1 == 0)
15713         return 0;
15714
15715       add_loc_list (&list_ret, list_ret1);
15716       if (list_ret == 0)
15717         return 0;
15718       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15719       break;
15720
15721     case TRUTH_NOT_EXPR:
15722     case BIT_NOT_EXPR:
15723       op = DW_OP_not;
15724       goto do_unop;
15725
15726     case ABS_EXPR:
15727       op = DW_OP_abs;
15728       goto do_unop;
15729
15730     case NEGATE_EXPR:
15731       op = DW_OP_neg;
15732       goto do_unop;
15733
15734     do_unop:
15735       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15736       if (list_ret == 0)
15737         return 0;
15738
15739       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15740       break;
15741
15742     case MIN_EXPR:
15743     case MAX_EXPR:
15744       {
15745         const enum tree_code code =
15746           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15747
15748         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15749                       build2 (code, integer_type_node,
15750                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15751                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15752       }
15753
15754       /* ... fall through ...  */
15755
15756     case COND_EXPR:
15757       {
15758         dw_loc_descr_ref lhs
15759           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15760         dw_loc_list_ref rhs
15761           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15762         dw_loc_descr_ref bra_node, jump_node, tmp;
15763
15764         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15765         if (list_ret == 0 || lhs == 0 || rhs == 0)
15766           return 0;
15767
15768         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15769         add_loc_descr_to_each (list_ret, bra_node);
15770
15771         add_loc_list (&list_ret, rhs);
15772         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15773         add_loc_descr_to_each (list_ret, jump_node);
15774
15775         add_loc_descr_to_each (list_ret, lhs);
15776         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15777         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15778
15779         /* ??? Need a node to point the skip at.  Use a nop.  */
15780         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15781         add_loc_descr_to_each (list_ret, tmp);
15782         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15783         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15784       }
15785       break;
15786
15787     case FIX_TRUNC_EXPR:
15788       return 0;
15789
15790     default:
15791       /* Leave front-end specific codes as simply unknown.  This comes
15792          up, for instance, with the C STMT_EXPR.  */
15793       if ((unsigned int) TREE_CODE (loc)
15794           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15795         {
15796           expansion_failed (loc, NULL_RTX,
15797                             "language specific tree node");
15798           return 0;
15799         }
15800
15801 #ifdef ENABLE_CHECKING
15802       /* Otherwise this is a generic code; we should just lists all of
15803          these explicitly.  We forgot one.  */
15804       gcc_unreachable ();
15805 #else
15806       /* In a release build, we want to degrade gracefully: better to
15807          generate incomplete debugging information than to crash.  */
15808       return NULL;
15809 #endif
15810     }
15811
15812   if (!ret && !list_ret)
15813     return 0;
15814
15815   if (want_address == 2 && !have_address
15816       && (dwarf_version >= 4 || !dwarf_strict))
15817     {
15818       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15819         {
15820           expansion_failed (loc, NULL_RTX,
15821                             "DWARF address size mismatch");
15822           return 0;
15823         }
15824       if (ret)
15825         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15826       else
15827         add_loc_descr_to_each (list_ret,
15828                                new_loc_descr (DW_OP_stack_value, 0, 0));
15829       have_address = 1;
15830     }
15831   /* Show if we can't fill the request for an address.  */
15832   if (want_address && !have_address)
15833     {
15834       expansion_failed (loc, NULL_RTX,
15835                         "Want address and only have value");
15836       return 0;
15837     }
15838
15839   gcc_assert (!ret || !list_ret);
15840
15841   /* If we've got an address and don't want one, dereference.  */
15842   if (!want_address && have_address)
15843     {
15844       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15845
15846       if (size > DWARF2_ADDR_SIZE || size == -1)
15847         {
15848           expansion_failed (loc, NULL_RTX,
15849                             "DWARF address size mismatch");
15850           return 0;
15851         }
15852       else if (size == DWARF2_ADDR_SIZE)
15853         op = DW_OP_deref;
15854       else
15855         op = DW_OP_deref_size;
15856
15857       if (ret)
15858         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15859       else
15860         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15861     }
15862   if (ret)
15863     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15864
15865   return list_ret;
15866 }
15867
15868 /* Same as above but return only single location expression.  */
15869 static dw_loc_descr_ref
15870 loc_descriptor_from_tree (tree loc, int want_address)
15871 {
15872   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15873   if (!ret)
15874     return NULL;
15875   if (ret->dw_loc_next)
15876     {
15877       expansion_failed (loc, NULL_RTX,
15878                         "Location list where only loc descriptor needed");
15879       return NULL;
15880     }
15881   return ret->expr;
15882 }
15883
15884 /* Given a value, round it up to the lowest multiple of `boundary'
15885    which is not less than the value itself.  */
15886
15887 static inline HOST_WIDE_INT
15888 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15889 {
15890   return (((value + boundary - 1) / boundary) * boundary);
15891 }
15892
15893 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15894    pointer to the declared type for the relevant field variable, or return
15895    `integer_type_node' if the given node turns out to be an
15896    ERROR_MARK node.  */
15897
15898 static inline tree
15899 field_type (const_tree decl)
15900 {
15901   tree type;
15902
15903   if (TREE_CODE (decl) == ERROR_MARK)
15904     return integer_type_node;
15905
15906   type = DECL_BIT_FIELD_TYPE (decl);
15907   if (type == NULL_TREE)
15908     type = TREE_TYPE (decl);
15909
15910   return type;
15911 }
15912
15913 /* Given a pointer to a tree node, return the alignment in bits for
15914    it, or else return BITS_PER_WORD if the node actually turns out to
15915    be an ERROR_MARK node.  */
15916
15917 static inline unsigned
15918 simple_type_align_in_bits (const_tree type)
15919 {
15920   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15921 }
15922
15923 static inline unsigned
15924 simple_decl_align_in_bits (const_tree decl)
15925 {
15926   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15927 }
15928
15929 /* Return the result of rounding T up to ALIGN.  */
15930
15931 static inline double_int
15932 round_up_to_align (double_int t, unsigned int align)
15933 {
15934   double_int alignd = uhwi_to_double_int (align);
15935   t = double_int_add (t, alignd);
15936   t = double_int_add (t, double_int_minus_one);
15937   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15938   t = double_int_mul (t, alignd);
15939   return t;
15940 }
15941
15942 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15943    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15944    or return 0 if we are unable to determine what that offset is, either
15945    because the argument turns out to be a pointer to an ERROR_MARK node, or
15946    because the offset is actually variable.  (We can't handle the latter case
15947    just yet).  */
15948
15949 static HOST_WIDE_INT
15950 field_byte_offset (const_tree decl)
15951 {
15952   double_int object_offset_in_bits;
15953   double_int object_offset_in_bytes;
15954   double_int bitpos_int;
15955
15956   if (TREE_CODE (decl) == ERROR_MARK)
15957     return 0;
15958
15959   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15960
15961   /* We cannot yet cope with fields whose positions are variable, so
15962      for now, when we see such things, we simply return 0.  Someday, we may
15963      be able to handle such cases, but it will be damn difficult.  */
15964   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15965     return 0;
15966
15967   bitpos_int = tree_to_double_int (bit_position (decl));
15968
15969 #ifdef PCC_BITFIELD_TYPE_MATTERS
15970   if (PCC_BITFIELD_TYPE_MATTERS)
15971     {
15972       tree type;
15973       tree field_size_tree;
15974       double_int deepest_bitpos;
15975       double_int field_size_in_bits;
15976       unsigned int type_align_in_bits;
15977       unsigned int decl_align_in_bits;
15978       double_int type_size_in_bits;
15979
15980       type = field_type (decl);
15981       type_size_in_bits = double_int_type_size_in_bits (type);
15982       type_align_in_bits = simple_type_align_in_bits (type);
15983
15984       field_size_tree = DECL_SIZE (decl);
15985
15986       /* The size could be unspecified if there was an error, or for
15987          a flexible array member.  */
15988       if (!field_size_tree)
15989         field_size_tree = bitsize_zero_node;
15990
15991       /* If the size of the field is not constant, use the type size.  */
15992       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15993         field_size_in_bits = tree_to_double_int (field_size_tree);
15994       else
15995         field_size_in_bits = type_size_in_bits;
15996
15997       decl_align_in_bits = simple_decl_align_in_bits (decl);
15998
15999       /* The GCC front-end doesn't make any attempt to keep track of the
16000          starting bit offset (relative to the start of the containing
16001          structure type) of the hypothetical "containing object" for a
16002          bit-field.  Thus, when computing the byte offset value for the
16003          start of the "containing object" of a bit-field, we must deduce
16004          this information on our own. This can be rather tricky to do in
16005          some cases.  For example, handling the following structure type
16006          definition when compiling for an i386/i486 target (which only
16007          aligns long long's to 32-bit boundaries) can be very tricky:
16008
16009          struct S { int field1; long long field2:31; };
16010
16011          Fortunately, there is a simple rule-of-thumb which can be used
16012          in such cases.  When compiling for an i386/i486, GCC will
16013          allocate 8 bytes for the structure shown above.  It decides to
16014          do this based upon one simple rule for bit-field allocation.
16015          GCC allocates each "containing object" for each bit-field at
16016          the first (i.e. lowest addressed) legitimate alignment boundary
16017          (based upon the required minimum alignment for the declared
16018          type of the field) which it can possibly use, subject to the
16019          condition that there is still enough available space remaining
16020          in the containing object (when allocated at the selected point)
16021          to fully accommodate all of the bits of the bit-field itself.
16022
16023          This simple rule makes it obvious why GCC allocates 8 bytes for
16024          each object of the structure type shown above.  When looking
16025          for a place to allocate the "containing object" for `field2',
16026          the compiler simply tries to allocate a 64-bit "containing
16027          object" at each successive 32-bit boundary (starting at zero)
16028          until it finds a place to allocate that 64- bit field such that
16029          at least 31 contiguous (and previously unallocated) bits remain
16030          within that selected 64 bit field.  (As it turns out, for the
16031          example above, the compiler finds it is OK to allocate the
16032          "containing object" 64-bit field at bit-offset zero within the
16033          structure type.)
16034
16035          Here we attempt to work backwards from the limited set of facts
16036          we're given, and we try to deduce from those facts, where GCC
16037          must have believed that the containing object started (within
16038          the structure type). The value we deduce is then used (by the
16039          callers of this routine) to generate DW_AT_location and
16040          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16041          the case of DW_AT_location, regular fields as well).  */
16042
16043       /* Figure out the bit-distance from the start of the structure to
16044          the "deepest" bit of the bit-field.  */
16045       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16046
16047       /* This is the tricky part.  Use some fancy footwork to deduce
16048          where the lowest addressed bit of the containing object must
16049          be.  */
16050       object_offset_in_bits
16051         = double_int_sub (deepest_bitpos, type_size_in_bits);
16052
16053       /* Round up to type_align by default.  This works best for
16054          bitfields.  */
16055       object_offset_in_bits
16056         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16057
16058       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16059         {
16060           object_offset_in_bits
16061             = double_int_sub (deepest_bitpos, type_size_in_bits);
16062
16063           /* Round up to decl_align instead.  */
16064           object_offset_in_bits
16065             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16066         }
16067     }
16068   else
16069 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16070     object_offset_in_bits = bitpos_int;
16071
16072   object_offset_in_bytes
16073     = double_int_div (object_offset_in_bits,
16074                       uhwi_to_double_int (BITS_PER_UNIT), true,
16075                       TRUNC_DIV_EXPR);
16076   return double_int_to_shwi (object_offset_in_bytes);
16077 }
16078 \f
16079 /* The following routines define various Dwarf attributes and any data
16080    associated with them.  */
16081
16082 /* Add a location description attribute value to a DIE.
16083
16084    This emits location attributes suitable for whole variables and
16085    whole parameters.  Note that the location attributes for struct fields are
16086    generated by the routine `data_member_location_attribute' below.  */
16087
16088 static inline void
16089 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16090                              dw_loc_list_ref descr)
16091 {
16092   if (descr == 0)
16093     return;
16094   if (single_element_loc_list_p (descr))
16095     add_AT_loc (die, attr_kind, descr->expr);
16096   else
16097     add_AT_loc_list (die, attr_kind, descr);
16098 }
16099
16100 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16101
16102 static void
16103 add_accessibility_attribute (dw_die_ref die, tree decl)
16104 {
16105   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16106      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16107      the default has always been DW_ACCESS_public.  */
16108   if (TREE_PROTECTED (decl))
16109     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16110   else if (TREE_PRIVATE (decl))
16111     {
16112       if (dwarf_version == 2
16113           || die->die_parent == NULL
16114           || die->die_parent->die_tag != DW_TAG_class_type)
16115         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16116     }
16117   else if (dwarf_version > 2
16118            && die->die_parent
16119            && die->die_parent->die_tag == DW_TAG_class_type)
16120     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16121 }
16122
16123 /* Attach the specialized form of location attribute used for data members of
16124    struct and union types.  In the special case of a FIELD_DECL node which
16125    represents a bit-field, the "offset" part of this special location
16126    descriptor must indicate the distance in bytes from the lowest-addressed
16127    byte of the containing struct or union type to the lowest-addressed byte of
16128    the "containing object" for the bit-field.  (See the `field_byte_offset'
16129    function above).
16130
16131    For any given bit-field, the "containing object" is a hypothetical object
16132    (of some integral or enum type) within which the given bit-field lives.  The
16133    type of this hypothetical "containing object" is always the same as the
16134    declared type of the individual bit-field itself (for GCC anyway... the
16135    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16136    bytes) of the hypothetical "containing object" which will be given in the
16137    DW_AT_byte_size attribute for this bit-field.  (See the
16138    `byte_size_attribute' function below.)  It is also used when calculating the
16139    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16140    function below.)  */
16141
16142 static void
16143 add_data_member_location_attribute (dw_die_ref die, tree decl)
16144 {
16145   HOST_WIDE_INT offset;
16146   dw_loc_descr_ref loc_descr = 0;
16147
16148   if (TREE_CODE (decl) == TREE_BINFO)
16149     {
16150       /* We're working on the TAG_inheritance for a base class.  */
16151       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16152         {
16153           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16154              aren't at a fixed offset from all (sub)objects of the same
16155              type.  We need to extract the appropriate offset from our
16156              vtable.  The following dwarf expression means
16157
16158                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16159
16160              This is specific to the V3 ABI, of course.  */
16161
16162           dw_loc_descr_ref tmp;
16163
16164           /* Make a copy of the object address.  */
16165           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16166           add_loc_descr (&loc_descr, tmp);
16167
16168           /* Extract the vtable address.  */
16169           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16170           add_loc_descr (&loc_descr, tmp);
16171
16172           /* Calculate the address of the offset.  */
16173           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16174           gcc_assert (offset < 0);
16175
16176           tmp = int_loc_descriptor (-offset);
16177           add_loc_descr (&loc_descr, tmp);
16178           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16179           add_loc_descr (&loc_descr, tmp);
16180
16181           /* Extract the offset.  */
16182           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16183           add_loc_descr (&loc_descr, tmp);
16184
16185           /* Add it to the object address.  */
16186           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16187           add_loc_descr (&loc_descr, tmp);
16188         }
16189       else
16190         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16191     }
16192   else
16193     offset = field_byte_offset (decl);
16194
16195   if (! loc_descr)
16196     {
16197       if (dwarf_version > 2)
16198         {
16199           /* Don't need to output a location expression, just the constant. */
16200           if (offset < 0)
16201             add_AT_int (die, DW_AT_data_member_location, offset);
16202           else
16203             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16204           return;
16205         }
16206       else
16207         {
16208           enum dwarf_location_atom op;
16209
16210           /* The DWARF2 standard says that we should assume that the structure
16211              address is already on the stack, so we can specify a structure
16212              field address by using DW_OP_plus_uconst.  */
16213
16214 #ifdef MIPS_DEBUGGING_INFO
16215           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16216              operator correctly.  It works only if we leave the offset on the
16217              stack.  */
16218           op = DW_OP_constu;
16219 #else
16220           op = DW_OP_plus_uconst;
16221 #endif
16222
16223           loc_descr = new_loc_descr (op, offset, 0);
16224         }
16225     }
16226
16227   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16228 }
16229
16230 /* Writes integer values to dw_vec_const array.  */
16231
16232 static void
16233 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16234 {
16235   while (size != 0)
16236     {
16237       *dest++ = val & 0xff;
16238       val >>= 8;
16239       --size;
16240     }
16241 }
16242
16243 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16244
16245 static HOST_WIDE_INT
16246 extract_int (const unsigned char *src, unsigned int size)
16247 {
16248   HOST_WIDE_INT val = 0;
16249
16250   src += size;
16251   while (size != 0)
16252     {
16253       val <<= 8;
16254       val |= *--src & 0xff;
16255       --size;
16256     }
16257   return val;
16258 }
16259
16260 /* Writes double_int values to dw_vec_const array.  */
16261
16262 static void
16263 insert_double (double_int val, unsigned char *dest)
16264 {
16265   unsigned char *p0 = dest;
16266   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16267
16268   if (WORDS_BIG_ENDIAN)
16269     {
16270       p0 = p1;
16271       p1 = dest;
16272     }
16273
16274   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16275   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16276 }
16277
16278 /* Writes floating point values to dw_vec_const array.  */
16279
16280 static void
16281 insert_float (const_rtx rtl, unsigned char *array)
16282 {
16283   REAL_VALUE_TYPE rv;
16284   long val[4];
16285   int i;
16286
16287   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16288   real_to_target (val, &rv, GET_MODE (rtl));
16289
16290   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16291   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16292     {
16293       insert_int (val[i], 4, array);
16294       array += 4;
16295     }
16296 }
16297
16298 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16299    does not have a "location" either in memory or in a register.  These
16300    things can arise in GNU C when a constant is passed as an actual parameter
16301    to an inlined function.  They can also arise in C++ where declared
16302    constants do not necessarily get memory "homes".  */
16303
16304 static bool
16305 add_const_value_attribute (dw_die_ref die, rtx rtl)
16306 {
16307   switch (GET_CODE (rtl))
16308     {
16309     case CONST_INT:
16310       {
16311         HOST_WIDE_INT val = INTVAL (rtl);
16312
16313         if (val < 0)
16314           add_AT_int (die, DW_AT_const_value, val);
16315         else
16316           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16317       }
16318       return true;
16319
16320     case CONST_DOUBLE:
16321       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16322          floating-point constant.  A CONST_DOUBLE is used whenever the
16323          constant requires more than one word in order to be adequately
16324          represented.  */
16325       {
16326         enum machine_mode mode = GET_MODE (rtl);
16327
16328         if (SCALAR_FLOAT_MODE_P (mode))
16329           {
16330             unsigned int length = GET_MODE_SIZE (mode);
16331             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16332
16333             insert_float (rtl, array);
16334             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16335           }
16336         else
16337           add_AT_double (die, DW_AT_const_value,
16338                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16339       }
16340       return true;
16341
16342     case CONST_VECTOR:
16343       {
16344         enum machine_mode mode = GET_MODE (rtl);
16345         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16346         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16347         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16348           (length * elt_size);
16349         unsigned int i;
16350         unsigned char *p;
16351
16352         switch (GET_MODE_CLASS (mode))
16353           {
16354           case MODE_VECTOR_INT:
16355             for (i = 0, p = array; i < length; i++, p += elt_size)
16356               {
16357                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16358                 double_int val = rtx_to_double_int (elt);
16359
16360                 if (elt_size <= sizeof (HOST_WIDE_INT))
16361                   insert_int (double_int_to_shwi (val), elt_size, p);
16362                 else
16363                   {
16364                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16365                     insert_double (val, p);
16366                   }
16367               }
16368             break;
16369
16370           case MODE_VECTOR_FLOAT:
16371             for (i = 0, p = array; i < length; i++, p += elt_size)
16372               {
16373                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16374                 insert_float (elt, p);
16375               }
16376             break;
16377
16378           default:
16379             gcc_unreachable ();
16380           }
16381
16382         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16383       }
16384       return true;
16385
16386     case CONST_STRING:
16387       if (dwarf_version >= 4 || !dwarf_strict)
16388         {
16389           dw_loc_descr_ref loc_result;
16390           resolve_one_addr (&rtl, NULL);
16391         rtl_addr:
16392           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16393           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16394           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16395           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16396           add_AT_loc (die, DW_AT_location, loc_result);
16397           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16398           return true;
16399         }
16400       return false;
16401
16402     case CONST:
16403       if (CONSTANT_P (XEXP (rtl, 0)))
16404         return add_const_value_attribute (die, XEXP (rtl, 0));
16405       /* FALLTHROUGH */
16406     case SYMBOL_REF:
16407       if (!const_ok_for_output (rtl))
16408         return false;
16409     case LABEL_REF:
16410       if (dwarf_version >= 4 || !dwarf_strict)
16411         goto rtl_addr;
16412       return false;
16413
16414     case PLUS:
16415       /* In cases where an inlined instance of an inline function is passed
16416          the address of an `auto' variable (which is local to the caller) we
16417          can get a situation where the DECL_RTL of the artificial local
16418          variable (for the inlining) which acts as a stand-in for the
16419          corresponding formal parameter (of the inline function) will look
16420          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16421          exactly a compile-time constant expression, but it isn't the address
16422          of the (artificial) local variable either.  Rather, it represents the
16423          *value* which the artificial local variable always has during its
16424          lifetime.  We currently have no way to represent such quasi-constant
16425          values in Dwarf, so for now we just punt and generate nothing.  */
16426       return false;
16427
16428     case HIGH:
16429     case CONST_FIXED:
16430       return false;
16431
16432     case MEM:
16433       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16434           && MEM_READONLY_P (rtl)
16435           && GET_MODE (rtl) == BLKmode)
16436         {
16437           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16438           return true;
16439         }
16440       return false;
16441
16442     default:
16443       /* No other kinds of rtx should be possible here.  */
16444       gcc_unreachable ();
16445     }
16446   return false;
16447 }
16448
16449 /* Determine whether the evaluation of EXPR references any variables
16450    or functions which aren't otherwise used (and therefore may not be
16451    output).  */
16452 static tree
16453 reference_to_unused (tree * tp, int * walk_subtrees,
16454                      void * data ATTRIBUTE_UNUSED)
16455 {
16456   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16457     *walk_subtrees = 0;
16458
16459   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16460       && ! TREE_ASM_WRITTEN (*tp))
16461     return *tp;
16462   /* ???  The C++ FE emits debug information for using decls, so
16463      putting gcc_unreachable here falls over.  See PR31899.  For now
16464      be conservative.  */
16465   else if (!cgraph_global_info_ready
16466            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16467     return *tp;
16468   else if (TREE_CODE (*tp) == VAR_DECL)
16469     {
16470       struct varpool_node *node = varpool_get_node (*tp);
16471       if (!node || !node->needed)
16472         return *tp;
16473     }
16474   else if (TREE_CODE (*tp) == FUNCTION_DECL
16475            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16476     {
16477       /* The call graph machinery must have finished analyzing,
16478          optimizing and gimplifying the CU by now.
16479          So if *TP has no call graph node associated
16480          to it, it means *TP will not be emitted.  */
16481       if (!cgraph_get_node (*tp))
16482         return *tp;
16483     }
16484   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16485     return *tp;
16486
16487   return NULL_TREE;
16488 }
16489
16490 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16491    for use in a later add_const_value_attribute call.  */
16492
16493 static rtx
16494 rtl_for_decl_init (tree init, tree type)
16495 {
16496   rtx rtl = NULL_RTX;
16497
16498   /* If a variable is initialized with a string constant without embedded
16499      zeros, build CONST_STRING.  */
16500   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16501     {
16502       tree enttype = TREE_TYPE (type);
16503       tree domain = TYPE_DOMAIN (type);
16504       enum machine_mode mode = TYPE_MODE (enttype);
16505
16506       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16507           && domain
16508           && integer_zerop (TYPE_MIN_VALUE (domain))
16509           && compare_tree_int (TYPE_MAX_VALUE (domain),
16510                                TREE_STRING_LENGTH (init) - 1) == 0
16511           && ((size_t) TREE_STRING_LENGTH (init)
16512               == strlen (TREE_STRING_POINTER (init)) + 1))
16513         {
16514           rtl = gen_rtx_CONST_STRING (VOIDmode,
16515                                       ggc_strdup (TREE_STRING_POINTER (init)));
16516           rtl = gen_rtx_MEM (BLKmode, rtl);
16517           MEM_READONLY_P (rtl) = 1;
16518         }
16519     }
16520   /* Other aggregates, and complex values, could be represented using
16521      CONCAT: FIXME!  */
16522   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16523     ;
16524   /* Vectors only work if their mode is supported by the target.
16525      FIXME: generic vectors ought to work too.  */
16526   else if (TREE_CODE (type) == VECTOR_TYPE
16527            && !VECTOR_MODE_P (TYPE_MODE (type)))
16528     ;
16529   /* If the initializer is something that we know will expand into an
16530      immediate RTL constant, expand it now.  We must be careful not to
16531      reference variables which won't be output.  */
16532   else if (initializer_constant_valid_p (init, type)
16533            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16534     {
16535       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16536          possible.  */
16537       if (TREE_CODE (type) == VECTOR_TYPE)
16538         switch (TREE_CODE (init))
16539           {
16540           case VECTOR_CST:
16541             break;
16542           case CONSTRUCTOR:
16543             if (TREE_CONSTANT (init))
16544               {
16545                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16546                 bool constant_p = true;
16547                 tree value;
16548                 unsigned HOST_WIDE_INT ix;
16549
16550                 /* Even when ctor is constant, it might contain non-*_CST
16551                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16552                    belong into VECTOR_CST nodes.  */
16553                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16554                   if (!CONSTANT_CLASS_P (value))
16555                     {
16556                       constant_p = false;
16557                       break;
16558                     }
16559
16560                 if (constant_p)
16561                   {
16562                     init = build_vector_from_ctor (type, elts);
16563                     break;
16564                   }
16565               }
16566             /* FALLTHRU */
16567
16568           default:
16569             return NULL;
16570           }
16571
16572       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16573
16574       /* If expand_expr returns a MEM, it wasn't immediate.  */
16575       gcc_assert (!rtl || !MEM_P (rtl));
16576     }
16577
16578   return rtl;
16579 }
16580
16581 /* Generate RTL for the variable DECL to represent its location.  */
16582
16583 static rtx
16584 rtl_for_decl_location (tree decl)
16585 {
16586   rtx rtl;
16587
16588   /* Here we have to decide where we are going to say the parameter "lives"
16589      (as far as the debugger is concerned).  We only have a couple of
16590      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16591
16592      DECL_RTL normally indicates where the parameter lives during most of the
16593      activation of the function.  If optimization is enabled however, this
16594      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16595      that the parameter doesn't really live anywhere (as far as the code
16596      generation parts of GCC are concerned) during most of the function's
16597      activation.  That will happen (for example) if the parameter is never
16598      referenced within the function.
16599
16600      We could just generate a location descriptor here for all non-NULL
16601      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16602      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16603      where DECL_RTL is NULL or is a pseudo-reg.
16604
16605      Note however that we can only get away with using DECL_INCOMING_RTL as
16606      a backup substitute for DECL_RTL in certain limited cases.  In cases
16607      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16608      we can be sure that the parameter was passed using the same type as it is
16609      declared to have within the function, and that its DECL_INCOMING_RTL
16610      points us to a place where a value of that type is passed.
16611
16612      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16613      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16614      because in these cases DECL_INCOMING_RTL points us to a value of some
16615      type which is *different* from the type of the parameter itself.  Thus,
16616      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16617      such cases, the debugger would end up (for example) trying to fetch a
16618      `float' from a place which actually contains the first part of a
16619      `double'.  That would lead to really incorrect and confusing
16620      output at debug-time.
16621
16622      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16623      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16624      are a couple of exceptions however.  On little-endian machines we can
16625      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16626      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16627      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16628      when (on a little-endian machine) a non-prototyped function has a
16629      parameter declared to be of type `short' or `char'.  In such cases,
16630      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16631      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16632      passed `int' value.  If the debugger then uses that address to fetch
16633      a `short' or a `char' (on a little-endian machine) the result will be
16634      the correct data, so we allow for such exceptional cases below.
16635
16636      Note that our goal here is to describe the place where the given formal
16637      parameter lives during most of the function's activation (i.e. between the
16638      end of the prologue and the start of the epilogue).  We'll do that as best
16639      as we can. Note however that if the given formal parameter is modified
16640      sometime during the execution of the function, then a stack backtrace (at
16641      debug-time) will show the function as having been called with the *new*
16642      value rather than the value which was originally passed in.  This happens
16643      rarely enough that it is not a major problem, but it *is* a problem, and
16644      I'd like to fix it.
16645
16646      A future version of dwarf2out.c may generate two additional attributes for
16647      any given DW_TAG_formal_parameter DIE which will describe the "passed
16648      type" and the "passed location" for the given formal parameter in addition
16649      to the attributes we now generate to indicate the "declared type" and the
16650      "active location" for each parameter.  This additional set of attributes
16651      could be used by debuggers for stack backtraces. Separately, note that
16652      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16653      This happens (for example) for inlined-instances of inline function formal
16654      parameters which are never referenced.  This really shouldn't be
16655      happening.  All PARM_DECL nodes should get valid non-NULL
16656      DECL_INCOMING_RTL values.  FIXME.  */
16657
16658   /* Use DECL_RTL as the "location" unless we find something better.  */
16659   rtl = DECL_RTL_IF_SET (decl);
16660
16661   /* When generating abstract instances, ignore everything except
16662      constants, symbols living in memory, and symbols living in
16663      fixed registers.  */
16664   if (! reload_completed)
16665     {
16666       if (rtl
16667           && (CONSTANT_P (rtl)
16668               || (MEM_P (rtl)
16669                   && CONSTANT_P (XEXP (rtl, 0)))
16670               || (REG_P (rtl)
16671                   && TREE_CODE (decl) == VAR_DECL
16672                   && TREE_STATIC (decl))))
16673         {
16674           rtl = targetm.delegitimize_address (rtl);
16675           return rtl;
16676         }
16677       rtl = NULL_RTX;
16678     }
16679   else if (TREE_CODE (decl) == PARM_DECL)
16680     {
16681       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16682         {
16683           tree declared_type = TREE_TYPE (decl);
16684           tree passed_type = DECL_ARG_TYPE (decl);
16685           enum machine_mode dmode = TYPE_MODE (declared_type);
16686           enum machine_mode pmode = TYPE_MODE (passed_type);
16687
16688           /* This decl represents a formal parameter which was optimized out.
16689              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16690              all cases where (rtl == NULL_RTX) just below.  */
16691           if (dmode == pmode)
16692             rtl = DECL_INCOMING_RTL (decl);
16693           else if (SCALAR_INT_MODE_P (dmode)
16694                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16695                    && DECL_INCOMING_RTL (decl))
16696             {
16697               rtx inc = DECL_INCOMING_RTL (decl);
16698               if (REG_P (inc))
16699                 rtl = inc;
16700               else if (MEM_P (inc))
16701                 {
16702                   if (BYTES_BIG_ENDIAN)
16703                     rtl = adjust_address_nv (inc, dmode,
16704                                              GET_MODE_SIZE (pmode)
16705                                              - GET_MODE_SIZE (dmode));
16706                   else
16707                     rtl = inc;
16708                 }
16709             }
16710         }
16711
16712       /* If the parm was passed in registers, but lives on the stack, then
16713          make a big endian correction if the mode of the type of the
16714          parameter is not the same as the mode of the rtl.  */
16715       /* ??? This is the same series of checks that are made in dbxout.c before
16716          we reach the big endian correction code there.  It isn't clear if all
16717          of these checks are necessary here, but keeping them all is the safe
16718          thing to do.  */
16719       else if (MEM_P (rtl)
16720                && XEXP (rtl, 0) != const0_rtx
16721                && ! CONSTANT_P (XEXP (rtl, 0))
16722                /* Not passed in memory.  */
16723                && !MEM_P (DECL_INCOMING_RTL (decl))
16724                /* Not passed by invisible reference.  */
16725                && (!REG_P (XEXP (rtl, 0))
16726                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16727                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16728 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16729                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16730 #endif
16731                      )
16732                /* Big endian correction check.  */
16733                && BYTES_BIG_ENDIAN
16734                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16735                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16736                    < UNITS_PER_WORD))
16737         {
16738           int offset = (UNITS_PER_WORD
16739                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16740
16741           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16742                              plus_constant (XEXP (rtl, 0), offset));
16743         }
16744     }
16745   else if (TREE_CODE (decl) == VAR_DECL
16746            && rtl
16747            && MEM_P (rtl)
16748            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16749            && BYTES_BIG_ENDIAN)
16750     {
16751       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16752       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16753
16754       /* If a variable is declared "register" yet is smaller than
16755          a register, then if we store the variable to memory, it
16756          looks like we're storing a register-sized value, when in
16757          fact we are not.  We need to adjust the offset of the
16758          storage location to reflect the actual value's bytes,
16759          else gdb will not be able to display it.  */
16760       if (rsize > dsize)
16761         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16762                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16763     }
16764
16765   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16766      and will have been substituted directly into all expressions that use it.
16767      C does not have such a concept, but C++ and other languages do.  */
16768   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16769     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16770
16771   if (rtl)
16772     rtl = targetm.delegitimize_address (rtl);
16773
16774   /* If we don't look past the constant pool, we risk emitting a
16775      reference to a constant pool entry that isn't referenced from
16776      code, and thus is not emitted.  */
16777   if (rtl)
16778     rtl = avoid_constant_pool_reference (rtl);
16779
16780   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16781      in the current CU, resolve_addr will remove the expression referencing
16782      it.  */
16783   if (rtl == NULL_RTX
16784       && TREE_CODE (decl) == VAR_DECL
16785       && !DECL_EXTERNAL (decl)
16786       && TREE_STATIC (decl)
16787       && DECL_NAME (decl)
16788       && !DECL_HARD_REGISTER (decl)
16789       && DECL_MODE (decl) != VOIDmode)
16790     {
16791       rtl = make_decl_rtl_for_debug (decl);
16792       if (!MEM_P (rtl)
16793           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16794           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16795         rtl = NULL_RTX;
16796     }
16797
16798   return rtl;
16799 }
16800
16801 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16802    returned.  If so, the decl for the COMMON block is returned, and the
16803    value is the offset into the common block for the symbol.  */
16804
16805 static tree
16806 fortran_common (tree decl, HOST_WIDE_INT *value)
16807 {
16808   tree val_expr, cvar;
16809   enum machine_mode mode;
16810   HOST_WIDE_INT bitsize, bitpos;
16811   tree offset;
16812   int volatilep = 0, unsignedp = 0;
16813
16814   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16815      it does not have a value (the offset into the common area), or if it
16816      is thread local (as opposed to global) then it isn't common, and shouldn't
16817      be handled as such.  */
16818   if (TREE_CODE (decl) != VAR_DECL
16819       || !TREE_STATIC (decl)
16820       || !DECL_HAS_VALUE_EXPR_P (decl)
16821       || !is_fortran ())
16822     return NULL_TREE;
16823
16824   val_expr = DECL_VALUE_EXPR (decl);
16825   if (TREE_CODE (val_expr) != COMPONENT_REF)
16826     return NULL_TREE;
16827
16828   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16829                               &mode, &unsignedp, &volatilep, true);
16830
16831   if (cvar == NULL_TREE
16832       || TREE_CODE (cvar) != VAR_DECL
16833       || DECL_ARTIFICIAL (cvar)
16834       || !TREE_PUBLIC (cvar))
16835     return NULL_TREE;
16836
16837   *value = 0;
16838   if (offset != NULL)
16839     {
16840       if (!host_integerp (offset, 0))
16841         return NULL_TREE;
16842       *value = tree_low_cst (offset, 0);
16843     }
16844   if (bitpos != 0)
16845     *value += bitpos / BITS_PER_UNIT;
16846
16847   return cvar;
16848 }
16849
16850 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16851    data attribute for a variable or a parameter.  We generate the
16852    DW_AT_const_value attribute only in those cases where the given variable
16853    or parameter does not have a true "location" either in memory or in a
16854    register.  This can happen (for example) when a constant is passed as an
16855    actual argument in a call to an inline function.  (It's possible that
16856    these things can crop up in other ways also.)  Note that one type of
16857    constant value which can be passed into an inlined function is a constant
16858    pointer.  This can happen for example if an actual argument in an inlined
16859    function call evaluates to a compile-time constant address.  */
16860
16861 static bool
16862 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16863                                        enum dwarf_attribute attr)
16864 {
16865   rtx rtl;
16866   dw_loc_list_ref list;
16867   var_loc_list *loc_list;
16868
16869   if (TREE_CODE (decl) == ERROR_MARK)
16870     return false;
16871
16872   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16873               || TREE_CODE (decl) == RESULT_DECL);
16874
16875   /* Try to get some constant RTL for this decl, and use that as the value of
16876      the location.  */
16877
16878   rtl = rtl_for_decl_location (decl);
16879   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16880       && add_const_value_attribute (die, rtl))
16881     return true;
16882
16883   /* See if we have single element location list that is equivalent to
16884      a constant value.  That way we are better to use add_const_value_attribute
16885      rather than expanding constant value equivalent.  */
16886   loc_list = lookup_decl_loc (decl);
16887   if (loc_list
16888       && loc_list->first
16889       && loc_list->first->next == NULL
16890       && NOTE_P (loc_list->first->loc)
16891       && NOTE_VAR_LOCATION (loc_list->first->loc)
16892       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16893     {
16894       struct var_loc_node *node;
16895
16896       node = loc_list->first;
16897       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16898       if (GET_CODE (rtl) == EXPR_LIST)
16899         rtl = XEXP (rtl, 0);
16900       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16901           && add_const_value_attribute (die, rtl))
16902          return true;
16903     }
16904   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16905   if (list)
16906     {
16907       add_AT_location_description (die, attr, list);
16908       return true;
16909     }
16910   /* None of that worked, so it must not really have a location;
16911      try adding a constant value attribute from the DECL_INITIAL.  */
16912   return tree_add_const_value_attribute_for_decl (die, decl);
16913 }
16914
16915 /* Add VARIABLE and DIE into deferred locations list.  */
16916
16917 static void
16918 defer_location (tree variable, dw_die_ref die)
16919 {
16920   deferred_locations entry;
16921   entry.variable = variable;
16922   entry.die = die;
16923   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16924 }
16925
16926 /* Helper function for tree_add_const_value_attribute.  Natively encode
16927    initializer INIT into an array.  Return true if successful.  */
16928
16929 static bool
16930 native_encode_initializer (tree init, unsigned char *array, int size)
16931 {
16932   tree type;
16933
16934   if (init == NULL_TREE)
16935     return false;
16936
16937   STRIP_NOPS (init);
16938   switch (TREE_CODE (init))
16939     {
16940     case STRING_CST:
16941       type = TREE_TYPE (init);
16942       if (TREE_CODE (type) == ARRAY_TYPE)
16943         {
16944           tree enttype = TREE_TYPE (type);
16945           enum machine_mode mode = TYPE_MODE (enttype);
16946
16947           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16948             return false;
16949           if (int_size_in_bytes (type) != size)
16950             return false;
16951           if (size > TREE_STRING_LENGTH (init))
16952             {
16953               memcpy (array, TREE_STRING_POINTER (init),
16954                       TREE_STRING_LENGTH (init));
16955               memset (array + TREE_STRING_LENGTH (init),
16956                       '\0', size - TREE_STRING_LENGTH (init));
16957             }
16958           else
16959             memcpy (array, TREE_STRING_POINTER (init), size);
16960           return true;
16961         }
16962       return false;
16963     case CONSTRUCTOR:
16964       type = TREE_TYPE (init);
16965       if (int_size_in_bytes (type) != size)
16966         return false;
16967       if (TREE_CODE (type) == ARRAY_TYPE)
16968         {
16969           HOST_WIDE_INT min_index;
16970           unsigned HOST_WIDE_INT cnt;
16971           int curpos = 0, fieldsize;
16972           constructor_elt *ce;
16973
16974           if (TYPE_DOMAIN (type) == NULL_TREE
16975               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16976             return false;
16977
16978           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16979           if (fieldsize <= 0)
16980             return false;
16981
16982           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16983           memset (array, '\0', size);
16984           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16985             {
16986               tree val = ce->value;
16987               tree index = ce->index;
16988               int pos = curpos;
16989               if (index && TREE_CODE (index) == RANGE_EXPR)
16990                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16991                       * fieldsize;
16992               else if (index)
16993                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16994
16995               if (val)
16996                 {
16997                   STRIP_NOPS (val);
16998                   if (!native_encode_initializer (val, array + pos, fieldsize))
16999                     return false;
17000                 }
17001               curpos = pos + fieldsize;
17002               if (index && TREE_CODE (index) == RANGE_EXPR)
17003                 {
17004                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17005                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17006                   while (count > 0)
17007                     {
17008                       if (val)
17009                         memcpy (array + curpos, array + pos, fieldsize);
17010                       curpos += fieldsize;
17011                     }
17012                 }
17013               gcc_assert (curpos <= size);
17014             }
17015           return true;
17016         }
17017       else if (TREE_CODE (type) == RECORD_TYPE
17018                || TREE_CODE (type) == UNION_TYPE)
17019         {
17020           tree field = NULL_TREE;
17021           unsigned HOST_WIDE_INT cnt;
17022           constructor_elt *ce;
17023
17024           if (int_size_in_bytes (type) != size)
17025             return false;
17026
17027           if (TREE_CODE (type) == RECORD_TYPE)
17028             field = TYPE_FIELDS (type);
17029
17030           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17031             {
17032               tree val = ce->value;
17033               int pos, fieldsize;
17034
17035               if (ce->index != 0)
17036                 field = ce->index;
17037
17038               if (val)
17039                 STRIP_NOPS (val);
17040
17041               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17042                 return false;
17043
17044               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17045                   && TYPE_DOMAIN (TREE_TYPE (field))
17046                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17047                 return false;
17048               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17049                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17050                 return false;
17051               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17052               pos = int_byte_position (field);
17053               gcc_assert (pos + fieldsize <= size);
17054               if (val
17055                   && !native_encode_initializer (val, array + pos, fieldsize))
17056                 return false;
17057             }
17058           return true;
17059         }
17060       return false;
17061     case VIEW_CONVERT_EXPR:
17062     case NON_LVALUE_EXPR:
17063       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17064     default:
17065       return native_encode_expr (init, array, size) == size;
17066     }
17067 }
17068
17069 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17070    attribute is the const value T.  */
17071
17072 static bool
17073 tree_add_const_value_attribute (dw_die_ref die, tree t)
17074 {
17075   tree init;
17076   tree type = TREE_TYPE (t);
17077   rtx rtl;
17078
17079   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17080     return false;
17081
17082   init = t;
17083   gcc_assert (!DECL_P (init));
17084
17085   rtl = rtl_for_decl_init (init, type);
17086   if (rtl)
17087     return add_const_value_attribute (die, rtl);
17088   /* If the host and target are sane, try harder.  */
17089   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17090            && initializer_constant_valid_p (init, type))
17091     {
17092       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17093       if (size > 0 && (int) size == size)
17094         {
17095           unsigned char *array = (unsigned char *)
17096             ggc_alloc_cleared_atomic (size);
17097
17098           if (native_encode_initializer (init, array, size))
17099             {
17100               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17101               return true;
17102             }
17103         }
17104     }
17105   return false;
17106 }
17107
17108 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17109    attribute is the const value of T, where T is an integral constant
17110    variable with static storage duration
17111    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17112
17113 static bool
17114 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17115 {
17116
17117   if (!decl
17118       || (TREE_CODE (decl) != VAR_DECL
17119           && TREE_CODE (decl) != CONST_DECL))
17120     return false;
17121
17122     if (TREE_READONLY (decl)
17123         && ! TREE_THIS_VOLATILE (decl)
17124         && DECL_INITIAL (decl))
17125       /* OK */;
17126     else
17127       return false;
17128
17129   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17130   if (get_AT (var_die, DW_AT_const_value))
17131     return false;
17132
17133   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17134 }
17135
17136 /* Convert the CFI instructions for the current function into a
17137    location list.  This is used for DW_AT_frame_base when we targeting
17138    a dwarf2 consumer that does not support the dwarf3
17139    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17140    expressions.  */
17141
17142 static dw_loc_list_ref
17143 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17144 {
17145   dw_fde_ref fde;
17146   dw_loc_list_ref list, *list_tail;
17147   dw_cfi_ref cfi;
17148   dw_cfa_location last_cfa, next_cfa;
17149   const char *start_label, *last_label, *section;
17150   dw_cfa_location remember;
17151
17152   fde = current_fde ();
17153   gcc_assert (fde != NULL);
17154
17155   section = secname_for_decl (current_function_decl);
17156   list_tail = &list;
17157   list = NULL;
17158
17159   memset (&next_cfa, 0, sizeof (next_cfa));
17160   next_cfa.reg = INVALID_REGNUM;
17161   remember = next_cfa;
17162
17163   start_label = fde->dw_fde_begin;
17164
17165   /* ??? Bald assumption that the CIE opcode list does not contain
17166      advance opcodes.  */
17167   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17168     lookup_cfa_1 (cfi, &next_cfa, &remember);
17169
17170   last_cfa = next_cfa;
17171   last_label = start_label;
17172
17173   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17174     switch (cfi->dw_cfi_opc)
17175       {
17176       case DW_CFA_set_loc:
17177       case DW_CFA_advance_loc1:
17178       case DW_CFA_advance_loc2:
17179       case DW_CFA_advance_loc4:
17180         if (!cfa_equal_p (&last_cfa, &next_cfa))
17181           {
17182             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17183                                        start_label, last_label, section);
17184
17185             list_tail = &(*list_tail)->dw_loc_next;
17186             last_cfa = next_cfa;
17187             start_label = last_label;
17188           }
17189         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17190         break;
17191
17192       case DW_CFA_advance_loc:
17193         /* The encoding is complex enough that we should never emit this.  */
17194         gcc_unreachable ();
17195
17196       default:
17197         lookup_cfa_1 (cfi, &next_cfa, &remember);
17198         break;
17199       }
17200
17201   if (!cfa_equal_p (&last_cfa, &next_cfa))
17202     {
17203       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17204                                  start_label, last_label, section);
17205       list_tail = &(*list_tail)->dw_loc_next;
17206       start_label = last_label;
17207     }
17208
17209   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17210                              start_label, fde->dw_fde_end, section);
17211
17212   if (list && list->dw_loc_next)
17213     gen_llsym (list);
17214
17215   return list;
17216 }
17217
17218 /* Compute a displacement from the "steady-state frame pointer" to the
17219    frame base (often the same as the CFA), and store it in
17220    frame_pointer_fb_offset.  OFFSET is added to the displacement
17221    before the latter is negated.  */
17222
17223 static void
17224 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17225 {
17226   rtx reg, elim;
17227
17228 #ifdef FRAME_POINTER_CFA_OFFSET
17229   reg = frame_pointer_rtx;
17230   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17231 #else
17232   reg = arg_pointer_rtx;
17233   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17234 #endif
17235
17236   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17237   if (GET_CODE (elim) == PLUS)
17238     {
17239       offset += INTVAL (XEXP (elim, 1));
17240       elim = XEXP (elim, 0);
17241     }
17242
17243   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17244                && (elim == hard_frame_pointer_rtx
17245                    || elim == stack_pointer_rtx))
17246               || elim == (frame_pointer_needed
17247                           ? hard_frame_pointer_rtx
17248                           : stack_pointer_rtx));
17249
17250   frame_pointer_fb_offset = -offset;
17251 }
17252
17253 /* Generate a DW_AT_name attribute given some string value to be included as
17254    the value of the attribute.  */
17255
17256 static void
17257 add_name_attribute (dw_die_ref die, const char *name_string)
17258 {
17259   if (name_string != NULL && *name_string != 0)
17260     {
17261       if (demangle_name_func)
17262         name_string = (*demangle_name_func) (name_string);
17263
17264       add_AT_string (die, DW_AT_name, name_string);
17265     }
17266 }
17267
17268 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17269
17270 static void
17271 add_comp_dir_attribute (dw_die_ref die)
17272 {
17273   const char *wd = get_src_pwd ();
17274   char *wd1;
17275
17276   if (wd == NULL)
17277     return;
17278
17279   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17280     {
17281       int wdlen;
17282
17283       wdlen = strlen (wd);
17284       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17285       strcpy (wd1, wd);
17286       wd1 [wdlen] = DIR_SEPARATOR;
17287       wd1 [wdlen + 1] = 0;
17288       wd = wd1;
17289     }
17290
17291     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17292 }
17293
17294 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17295    default.  */
17296
17297 static int
17298 lower_bound_default (void)
17299 {
17300   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17301     {
17302     case DW_LANG_C:
17303     case DW_LANG_C89:
17304     case DW_LANG_C99:
17305     case DW_LANG_C_plus_plus:
17306     case DW_LANG_ObjC:
17307     case DW_LANG_ObjC_plus_plus:
17308     case DW_LANG_Java:
17309       return 0;
17310     case DW_LANG_Fortran77:
17311     case DW_LANG_Fortran90:
17312     case DW_LANG_Fortran95:
17313       return 1;
17314     case DW_LANG_UPC:
17315     case DW_LANG_D:
17316     case DW_LANG_Python:
17317       return dwarf_version >= 4 ? 0 : -1;
17318     case DW_LANG_Ada95:
17319     case DW_LANG_Ada83:
17320     case DW_LANG_Cobol74:
17321     case DW_LANG_Cobol85:
17322     case DW_LANG_Pascal83:
17323     case DW_LANG_Modula2:
17324     case DW_LANG_PLI:
17325       return dwarf_version >= 4 ? 1 : -1;
17326     default:
17327       return -1;
17328     }
17329 }
17330
17331 /* Given a tree node describing an array bound (either lower or upper) output
17332    a representation for that bound.  */
17333
17334 static void
17335 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17336 {
17337   switch (TREE_CODE (bound))
17338     {
17339     case ERROR_MARK:
17340       return;
17341
17342     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17343     case INTEGER_CST:
17344       {
17345         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17346         int dflt;
17347
17348         /* Use the default if possible.  */
17349         if (bound_attr == DW_AT_lower_bound
17350             && host_integerp (bound, 0)
17351             && (dflt = lower_bound_default ()) != -1
17352             && tree_low_cst (bound, 0) == dflt)
17353           ;
17354
17355         /* Otherwise represent the bound as an unsigned value with the
17356            precision of its type.  The precision and signedness of the
17357            type will be necessary to re-interpret it unambiguously.  */
17358         else if (prec < HOST_BITS_PER_WIDE_INT)
17359           {
17360             unsigned HOST_WIDE_INT mask
17361               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17362             add_AT_unsigned (subrange_die, bound_attr,
17363                              TREE_INT_CST_LOW (bound) & mask);
17364           }
17365         else if (prec == HOST_BITS_PER_WIDE_INT
17366                  || TREE_INT_CST_HIGH (bound) == 0)
17367           add_AT_unsigned (subrange_die, bound_attr,
17368                            TREE_INT_CST_LOW (bound));
17369         else
17370           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17371                          TREE_INT_CST_LOW (bound));
17372       }
17373       break;
17374
17375     CASE_CONVERT:
17376     case VIEW_CONVERT_EXPR:
17377       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17378       break;
17379
17380     case SAVE_EXPR:
17381       break;
17382
17383     case VAR_DECL:
17384     case PARM_DECL:
17385     case RESULT_DECL:
17386       {
17387         dw_die_ref decl_die = lookup_decl_die (bound);
17388
17389         /* ??? Can this happen, or should the variable have been bound
17390            first?  Probably it can, since I imagine that we try to create
17391            the types of parameters in the order in which they exist in
17392            the list, and won't have created a forward reference to a
17393            later parameter.  */
17394         if (decl_die != NULL)
17395           {
17396             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17397             break;
17398           }
17399       }
17400       /* FALLTHRU */
17401
17402     default:
17403       {
17404         /* Otherwise try to create a stack operation procedure to
17405            evaluate the value of the array bound.  */
17406
17407         dw_die_ref ctx, decl_die;
17408         dw_loc_list_ref list;
17409
17410         list = loc_list_from_tree (bound, 2);
17411         if (list == NULL || single_element_loc_list_p (list))
17412           {
17413             /* If DW_AT_*bound is not a reference nor constant, it is
17414                a DWARF expression rather than location description.
17415                For that loc_list_from_tree (bound, 0) is needed.
17416                If that fails to give a single element list,
17417                fall back to outputting this as a reference anyway.  */
17418             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17419             if (list2 && single_element_loc_list_p (list2))
17420               {
17421                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17422                 break;
17423               }
17424           }
17425         if (list == NULL)
17426           break;
17427
17428         if (current_function_decl == 0)
17429           ctx = comp_unit_die ();
17430         else
17431           ctx = lookup_decl_die (current_function_decl);
17432
17433         decl_die = new_die (DW_TAG_variable, ctx, bound);
17434         add_AT_flag (decl_die, DW_AT_artificial, 1);
17435         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17436         add_AT_location_description (decl_die, DW_AT_location, list);
17437         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17438         break;
17439       }
17440     }
17441 }
17442
17443 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17444    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17445    Note that the block of subscript information for an array type also
17446    includes information about the element type of the given array type.  */
17447
17448 static void
17449 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17450 {
17451   unsigned dimension_number;
17452   tree lower, upper;
17453   dw_die_ref subrange_die;
17454
17455   for (dimension_number = 0;
17456        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17457        type = TREE_TYPE (type), dimension_number++)
17458     {
17459       tree domain = TYPE_DOMAIN (type);
17460
17461       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17462         break;
17463
17464       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17465          and (in GNU C only) variable bounds.  Handle all three forms
17466          here.  */
17467       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17468       if (domain)
17469         {
17470           /* We have an array type with specified bounds.  */
17471           lower = TYPE_MIN_VALUE (domain);
17472           upper = TYPE_MAX_VALUE (domain);
17473
17474           /* Define the index type.  */
17475           if (TREE_TYPE (domain))
17476             {
17477               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17478                  TREE_TYPE field.  We can't emit debug info for this
17479                  because it is an unnamed integral type.  */
17480               if (TREE_CODE (domain) == INTEGER_TYPE
17481                   && TYPE_NAME (domain) == NULL_TREE
17482                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17483                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17484                 ;
17485               else
17486                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17487                                     type_die);
17488             }
17489
17490           /* ??? If upper is NULL, the array has unspecified length,
17491              but it does have a lower bound.  This happens with Fortran
17492                dimension arr(N:*)
17493              Since the debugger is definitely going to need to know N
17494              to produce useful results, go ahead and output the lower
17495              bound solo, and hope the debugger can cope.  */
17496
17497           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17498           if (upper)
17499             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17500         }
17501
17502       /* Otherwise we have an array type with an unspecified length.  The
17503          DWARF-2 spec does not say how to handle this; let's just leave out the
17504          bounds.  */
17505     }
17506 }
17507
17508 static void
17509 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17510 {
17511   unsigned size;
17512
17513   switch (TREE_CODE (tree_node))
17514     {
17515     case ERROR_MARK:
17516       size = 0;
17517       break;
17518     case ENUMERAL_TYPE:
17519     case RECORD_TYPE:
17520     case UNION_TYPE:
17521     case QUAL_UNION_TYPE:
17522       size = int_size_in_bytes (tree_node);
17523       break;
17524     case FIELD_DECL:
17525       /* For a data member of a struct or union, the DW_AT_byte_size is
17526          generally given as the number of bytes normally allocated for an
17527          object of the *declared* type of the member itself.  This is true
17528          even for bit-fields.  */
17529       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17530       break;
17531     default:
17532       gcc_unreachable ();
17533     }
17534
17535   /* Note that `size' might be -1 when we get to this point.  If it is, that
17536      indicates that the byte size of the entity in question is variable.  We
17537      have no good way of expressing this fact in Dwarf at the present time,
17538      so just let the -1 pass on through.  */
17539   add_AT_unsigned (die, DW_AT_byte_size, size);
17540 }
17541
17542 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17543    which specifies the distance in bits from the highest order bit of the
17544    "containing object" for the bit-field to the highest order bit of the
17545    bit-field itself.
17546
17547    For any given bit-field, the "containing object" is a hypothetical object
17548    (of some integral or enum type) within which the given bit-field lives.  The
17549    type of this hypothetical "containing object" is always the same as the
17550    declared type of the individual bit-field itself.  The determination of the
17551    exact location of the "containing object" for a bit-field is rather
17552    complicated.  It's handled by the `field_byte_offset' function (above).
17553
17554    Note that it is the size (in bytes) of the hypothetical "containing object"
17555    which will be given in the DW_AT_byte_size attribute for this bit-field.
17556    (See `byte_size_attribute' above).  */
17557
17558 static inline void
17559 add_bit_offset_attribute (dw_die_ref die, tree decl)
17560 {
17561   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17562   tree type = DECL_BIT_FIELD_TYPE (decl);
17563   HOST_WIDE_INT bitpos_int;
17564   HOST_WIDE_INT highest_order_object_bit_offset;
17565   HOST_WIDE_INT highest_order_field_bit_offset;
17566   HOST_WIDE_INT unsigned bit_offset;
17567
17568   /* Must be a field and a bit field.  */
17569   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17570
17571   /* We can't yet handle bit-fields whose offsets are variable, so if we
17572      encounter such things, just return without generating any attribute
17573      whatsoever.  Likewise for variable or too large size.  */
17574   if (! host_integerp (bit_position (decl), 0)
17575       || ! host_integerp (DECL_SIZE (decl), 1))
17576     return;
17577
17578   bitpos_int = int_bit_position (decl);
17579
17580   /* Note that the bit offset is always the distance (in bits) from the
17581      highest-order bit of the "containing object" to the highest-order bit of
17582      the bit-field itself.  Since the "high-order end" of any object or field
17583      is different on big-endian and little-endian machines, the computation
17584      below must take account of these differences.  */
17585   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17586   highest_order_field_bit_offset = bitpos_int;
17587
17588   if (! BYTES_BIG_ENDIAN)
17589     {
17590       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17591       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17592     }
17593
17594   bit_offset
17595     = (! BYTES_BIG_ENDIAN
17596        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17597        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17598
17599   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17600 }
17601
17602 /* For a FIELD_DECL node which represents a bit field, output an attribute
17603    which specifies the length in bits of the given field.  */
17604
17605 static inline void
17606 add_bit_size_attribute (dw_die_ref die, tree decl)
17607 {
17608   /* Must be a field and a bit field.  */
17609   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17610               && DECL_BIT_FIELD_TYPE (decl));
17611
17612   if (host_integerp (DECL_SIZE (decl), 1))
17613     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17614 }
17615
17616 /* If the compiled language is ANSI C, then add a 'prototyped'
17617    attribute, if arg types are given for the parameters of a function.  */
17618
17619 static inline void
17620 add_prototyped_attribute (dw_die_ref die, tree func_type)
17621 {
17622   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17623       && TYPE_ARG_TYPES (func_type) != NULL)
17624     add_AT_flag (die, DW_AT_prototyped, 1);
17625 }
17626
17627 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17628    by looking in either the type declaration or object declaration
17629    equate table.  */
17630
17631 static inline dw_die_ref
17632 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17633 {
17634   dw_die_ref origin_die = NULL;
17635
17636   if (TREE_CODE (origin) != FUNCTION_DECL)
17637     {
17638       /* We may have gotten separated from the block for the inlined
17639          function, if we're in an exception handler or some such; make
17640          sure that the abstract function has been written out.
17641
17642          Doing this for nested functions is wrong, however; functions are
17643          distinct units, and our context might not even be inline.  */
17644       tree fn = origin;
17645
17646       if (TYPE_P (fn))
17647         fn = TYPE_STUB_DECL (fn);
17648
17649       fn = decl_function_context (fn);
17650       if (fn)
17651         dwarf2out_abstract_function (fn);
17652     }
17653
17654   if (DECL_P (origin))
17655     origin_die = lookup_decl_die (origin);
17656   else if (TYPE_P (origin))
17657     origin_die = lookup_type_die (origin);
17658
17659   /* XXX: Functions that are never lowered don't always have correct block
17660      trees (in the case of java, they simply have no block tree, in some other
17661      languages).  For these functions, there is nothing we can really do to
17662      output correct debug info for inlined functions in all cases.  Rather
17663      than die, we'll just produce deficient debug info now, in that we will
17664      have variables without a proper abstract origin.  In the future, when all
17665      functions are lowered, we should re-add a gcc_assert (origin_die)
17666      here.  */
17667
17668   if (origin_die)
17669     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17670   return origin_die;
17671 }
17672
17673 /* We do not currently support the pure_virtual attribute.  */
17674
17675 static inline void
17676 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17677 {
17678   if (DECL_VINDEX (func_decl))
17679     {
17680       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17681
17682       if (host_integerp (DECL_VINDEX (func_decl), 0))
17683         add_AT_loc (die, DW_AT_vtable_elem_location,
17684                     new_loc_descr (DW_OP_constu,
17685                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17686                                    0));
17687
17688       /* GNU extension: Record what type this method came from originally.  */
17689       if (debug_info_level > DINFO_LEVEL_TERSE
17690           && DECL_CONTEXT (func_decl))
17691         add_AT_die_ref (die, DW_AT_containing_type,
17692                         lookup_type_die (DECL_CONTEXT (func_decl)));
17693     }
17694 }
17695 \f
17696 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17697    given decl.  This used to be a vendor extension until after DWARF 4
17698    standardized it.  */
17699
17700 static void
17701 add_linkage_attr (dw_die_ref die, tree decl)
17702 {
17703   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17704
17705   /* Mimic what assemble_name_raw does with a leading '*'.  */
17706   if (name[0] == '*')
17707     name = &name[1];
17708
17709   if (dwarf_version >= 4)
17710     add_AT_string (die, DW_AT_linkage_name, name);
17711   else
17712     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17713 }
17714
17715 /* Add source coordinate attributes for the given decl.  */
17716
17717 static void
17718 add_src_coords_attributes (dw_die_ref die, tree decl)
17719 {
17720   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17721
17722   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17723   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17724 }
17725
17726 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17727
17728 static void
17729 add_linkage_name (dw_die_ref die, tree decl)
17730 {
17731   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17732        && TREE_PUBLIC (decl)
17733        && !DECL_ABSTRACT (decl)
17734        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17735        && die->die_tag != DW_TAG_member)
17736     {
17737       /* Defer until we have an assembler name set.  */
17738       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17739         {
17740           limbo_die_node *asm_name;
17741
17742           asm_name = ggc_alloc_cleared_limbo_die_node ();
17743           asm_name->die = die;
17744           asm_name->created_for = decl;
17745           asm_name->next = deferred_asm_name;
17746           deferred_asm_name = asm_name;
17747         }
17748       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17749         add_linkage_attr (die, decl);
17750     }
17751 }
17752
17753 /* Add a DW_AT_name attribute and source coordinate attribute for the
17754    given decl, but only if it actually has a name.  */
17755
17756 static void
17757 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17758 {
17759   tree decl_name;
17760
17761   decl_name = DECL_NAME (decl);
17762   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17763     {
17764       const char *name = dwarf2_name (decl, 0);
17765       if (name)
17766         add_name_attribute (die, name);
17767       if (! DECL_ARTIFICIAL (decl))
17768         add_src_coords_attributes (die, decl);
17769
17770       add_linkage_name (die, decl);
17771     }
17772
17773 #ifdef VMS_DEBUGGING_INFO
17774   /* Get the function's name, as described by its RTL.  This may be different
17775      from the DECL_NAME name used in the source file.  */
17776   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17777     {
17778       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17779                    XEXP (DECL_RTL (decl), 0));
17780       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17781     }
17782 #endif /* VMS_DEBUGGING_INFO */
17783 }
17784
17785 #ifdef VMS_DEBUGGING_INFO
17786 /* Output the debug main pointer die for VMS */
17787
17788 void
17789 dwarf2out_vms_debug_main_pointer (void)
17790 {
17791   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17792   dw_die_ref die;
17793
17794   /* Allocate the VMS debug main subprogram die.  */
17795   die = ggc_alloc_cleared_die_node ();
17796   die->die_tag = DW_TAG_subprogram;
17797   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17798   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17799                                current_function_funcdef_no);
17800   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17801
17802   /* Make it the first child of comp_unit_die ().  */
17803   die->die_parent = comp_unit_die ();
17804   if (comp_unit_die ()->die_child)
17805     {
17806       die->die_sib = comp_unit_die ()->die_child->die_sib;
17807       comp_unit_die ()->die_child->die_sib = die;
17808     }
17809   else
17810     {
17811       die->die_sib = die;
17812       comp_unit_die ()->die_child = die;
17813     }
17814 }
17815 #endif /* VMS_DEBUGGING_INFO */
17816
17817 /* Push a new declaration scope.  */
17818
17819 static void
17820 push_decl_scope (tree scope)
17821 {
17822   VEC_safe_push (tree, gc, decl_scope_table, scope);
17823 }
17824
17825 /* Pop a declaration scope.  */
17826
17827 static inline void
17828 pop_decl_scope (void)
17829 {
17830   VEC_pop (tree, decl_scope_table);
17831 }
17832
17833 /* Return the DIE for the scope that immediately contains this type.
17834    Non-named types get global scope.  Named types nested in other
17835    types get their containing scope if it's open, or global scope
17836    otherwise.  All other types (i.e. function-local named types) get
17837    the current active scope.  */
17838
17839 static dw_die_ref
17840 scope_die_for (tree t, dw_die_ref context_die)
17841 {
17842   dw_die_ref scope_die = NULL;
17843   tree containing_scope;
17844   int i;
17845
17846   /* Non-types always go in the current scope.  */
17847   gcc_assert (TYPE_P (t));
17848
17849   containing_scope = TYPE_CONTEXT (t);
17850
17851   /* Use the containing namespace if it was passed in (for a declaration).  */
17852   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17853     {
17854       if (context_die == lookup_decl_die (containing_scope))
17855         /* OK */;
17856       else
17857         containing_scope = NULL_TREE;
17858     }
17859
17860   /* Ignore function type "scopes" from the C frontend.  They mean that
17861      a tagged type is local to a parmlist of a function declarator, but
17862      that isn't useful to DWARF.  */
17863   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17864     containing_scope = NULL_TREE;
17865
17866   if (SCOPE_FILE_SCOPE_P (containing_scope))
17867     scope_die = comp_unit_die ();
17868   else if (TYPE_P (containing_scope))
17869     {
17870       /* For types, we can just look up the appropriate DIE.  But
17871          first we check to see if we're in the middle of emitting it
17872          so we know where the new DIE should go.  */
17873       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17874         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17875           break;
17876
17877       if (i < 0)
17878         {
17879           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17880                       || TREE_ASM_WRITTEN (containing_scope));
17881           /*We are not in the middle of emitting the type
17882             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17883           scope_die = lookup_type_die (containing_scope);
17884
17885           /* If none of the current dies are suitable, we get file scope.  */
17886           if (scope_die == NULL)
17887             scope_die = comp_unit_die ();
17888         }
17889       else
17890         scope_die = lookup_type_die (containing_scope);
17891     }
17892   else
17893     scope_die = context_die;
17894
17895   return scope_die;
17896 }
17897
17898 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17899
17900 static inline int
17901 local_scope_p (dw_die_ref context_die)
17902 {
17903   for (; context_die; context_die = context_die->die_parent)
17904     if (context_die->die_tag == DW_TAG_inlined_subroutine
17905         || context_die->die_tag == DW_TAG_subprogram)
17906       return 1;
17907
17908   return 0;
17909 }
17910
17911 /* Returns nonzero if CONTEXT_DIE is a class.  */
17912
17913 static inline int
17914 class_scope_p (dw_die_ref context_die)
17915 {
17916   return (context_die
17917           && (context_die->die_tag == DW_TAG_structure_type
17918               || context_die->die_tag == DW_TAG_class_type
17919               || context_die->die_tag == DW_TAG_interface_type
17920               || context_die->die_tag == DW_TAG_union_type));
17921 }
17922
17923 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17924    whether or not to treat a DIE in this context as a declaration.  */
17925
17926 static inline int
17927 class_or_namespace_scope_p (dw_die_ref context_die)
17928 {
17929   return (class_scope_p (context_die)
17930           || (context_die && context_die->die_tag == DW_TAG_namespace));
17931 }
17932
17933 /* Many forms of DIEs require a "type description" attribute.  This
17934    routine locates the proper "type descriptor" die for the type given
17935    by 'type', and adds a DW_AT_type attribute below the given die.  */
17936
17937 static void
17938 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17939                     int decl_volatile, dw_die_ref context_die)
17940 {
17941   enum tree_code code  = TREE_CODE (type);
17942   dw_die_ref type_die  = NULL;
17943
17944   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17945      or fixed-point type, use the inner type.  This is because we have no
17946      support for unnamed types in base_type_die.  This can happen if this is
17947      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17948   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17949       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17950     type = TREE_TYPE (type), code = TREE_CODE (type);
17951
17952   if (code == ERROR_MARK
17953       /* Handle a special case.  For functions whose return type is void, we
17954          generate *no* type attribute.  (Note that no object may have type
17955          `void', so this only applies to function return types).  */
17956       || code == VOID_TYPE)
17957     return;
17958
17959   type_die = modified_type_die (type,
17960                                 decl_const || TYPE_READONLY (type),
17961                                 decl_volatile || TYPE_VOLATILE (type),
17962                                 context_die);
17963
17964   if (type_die != NULL)
17965     add_AT_die_ref (object_die, DW_AT_type, type_die);
17966 }
17967
17968 /* Given an object die, add the calling convention attribute for the
17969    function call type.  */
17970 static void
17971 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17972 {
17973   enum dwarf_calling_convention value = DW_CC_normal;
17974
17975   value = ((enum dwarf_calling_convention)
17976            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17977
17978   if (is_fortran ()
17979       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17980     {
17981       /* DWARF 2 doesn't provide a way to identify a program's source-level
17982         entry point.  DW_AT_calling_convention attributes are only meant
17983         to describe functions' calling conventions.  However, lacking a
17984         better way to signal the Fortran main program, we used this for 
17985         a long time, following existing custom.  Now, DWARF 4 has 
17986         DW_AT_main_subprogram, which we add below, but some tools still
17987         rely on the old way, which we thus keep.  */
17988       value = DW_CC_program;
17989
17990       if (dwarf_version >= 4 || !dwarf_strict)
17991         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
17992     }
17993
17994   /* Only add the attribute if the backend requests it, and
17995      is not DW_CC_normal.  */
17996   if (value && (value != DW_CC_normal))
17997     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17998 }
17999
18000 /* Given a tree pointer to a struct, class, union, or enum type node, return
18001    a pointer to the (string) tag name for the given type, or zero if the type
18002    was declared without a tag.  */
18003
18004 static const char *
18005 type_tag (const_tree type)
18006 {
18007   const char *name = 0;
18008
18009   if (TYPE_NAME (type) != 0)
18010     {
18011       tree t = 0;
18012
18013       /* Find the IDENTIFIER_NODE for the type name.  */
18014       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18015           && !TYPE_NAMELESS (type))
18016         t = TYPE_NAME (type);
18017
18018       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18019          a TYPE_DECL node, regardless of whether or not a `typedef' was
18020          involved.  */
18021       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18022                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18023         {
18024           /* We want to be extra verbose.  Don't call dwarf_name if
18025              DECL_NAME isn't set.  The default hook for decl_printable_name
18026              doesn't like that, and in this context it's correct to return
18027              0, instead of "<anonymous>" or the like.  */
18028           if (DECL_NAME (TYPE_NAME (type))
18029               && !DECL_NAMELESS (TYPE_NAME (type)))
18030             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18031         }
18032
18033       /* Now get the name as a string, or invent one.  */
18034       if (!name && t != 0)
18035         name = IDENTIFIER_POINTER (t);
18036     }
18037
18038   return (name == 0 || *name == '\0') ? 0 : name;
18039 }
18040
18041 /* Return the type associated with a data member, make a special check
18042    for bit field types.  */
18043
18044 static inline tree
18045 member_declared_type (const_tree member)
18046 {
18047   return (DECL_BIT_FIELD_TYPE (member)
18048           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18049 }
18050
18051 /* Get the decl's label, as described by its RTL. This may be different
18052    from the DECL_NAME name used in the source file.  */
18053
18054 #if 0
18055 static const char *
18056 decl_start_label (tree decl)
18057 {
18058   rtx x;
18059   const char *fnname;
18060
18061   x = DECL_RTL (decl);
18062   gcc_assert (MEM_P (x));
18063
18064   x = XEXP (x, 0);
18065   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18066
18067   fnname = XSTR (x, 0);
18068   return fnname;
18069 }
18070 #endif
18071 \f
18072 /* These routines generate the internal representation of the DIE's for
18073    the compilation unit.  Debugging information is collected by walking
18074    the declaration trees passed in from dwarf2out_decl().  */
18075
18076 static void
18077 gen_array_type_die (tree type, dw_die_ref context_die)
18078 {
18079   dw_die_ref scope_die = scope_die_for (type, context_die);
18080   dw_die_ref array_die;
18081
18082   /* GNU compilers represent multidimensional array types as sequences of one
18083      dimensional array types whose element types are themselves array types.
18084      We sometimes squish that down to a single array_type DIE with multiple
18085      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18086      say that we are allowed to do this kind of compression in C, because
18087      there is no difference between an array of arrays and a multidimensional
18088      array.  We don't do this for Ada to remain as close as possible to the
18089      actual representation, which is especially important against the language
18090      flexibilty wrt arrays of variable size.  */
18091
18092   bool collapse_nested_arrays = !is_ada ();
18093   tree element_type;
18094
18095   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18096      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18097   if (TYPE_STRING_FLAG (type)
18098       && TREE_CODE (type) == ARRAY_TYPE
18099       && is_fortran ()
18100       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18101     {
18102       HOST_WIDE_INT size;
18103
18104       array_die = new_die (DW_TAG_string_type, scope_die, type);
18105       add_name_attribute (array_die, type_tag (type));
18106       equate_type_number_to_die (type, array_die);
18107       size = int_size_in_bytes (type);
18108       if (size >= 0)
18109         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18110       else if (TYPE_DOMAIN (type) != NULL_TREE
18111                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18112                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18113         {
18114           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18115           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18116
18117           size = int_size_in_bytes (TREE_TYPE (szdecl));
18118           if (loc && size > 0)
18119             {
18120               add_AT_location_description (array_die, DW_AT_string_length, loc);
18121               if (size != DWARF2_ADDR_SIZE)
18122                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18123             }
18124         }
18125       return;
18126     }
18127
18128   /* ??? The SGI dwarf reader fails for array of array of enum types
18129      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18130      array type comes before the outer array type.  We thus call gen_type_die
18131      before we new_die and must prevent nested array types collapsing for this
18132      target.  */
18133
18134 #ifdef MIPS_DEBUGGING_INFO
18135   gen_type_die (TREE_TYPE (type), context_die);
18136   collapse_nested_arrays = false;
18137 #endif
18138
18139   array_die = new_die (DW_TAG_array_type, scope_die, type);
18140   add_name_attribute (array_die, type_tag (type));
18141   equate_type_number_to_die (type, array_die);
18142
18143   if (TREE_CODE (type) == VECTOR_TYPE)
18144     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18145
18146   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18147   if (is_fortran ()
18148       && TREE_CODE (type) == ARRAY_TYPE
18149       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18150       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18151     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18152
18153 #if 0
18154   /* We default the array ordering.  SDB will probably do
18155      the right things even if DW_AT_ordering is not present.  It's not even
18156      an issue until we start to get into multidimensional arrays anyway.  If
18157      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18158      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18159      and when we find out that we need to put these in, we will only do so
18160      for multidimensional arrays.  */
18161   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18162 #endif
18163
18164 #ifdef MIPS_DEBUGGING_INFO
18165   /* The SGI compilers handle arrays of unknown bound by setting
18166      AT_declaration and not emitting any subrange DIEs.  */
18167   if (TREE_CODE (type) == ARRAY_TYPE
18168       && ! TYPE_DOMAIN (type))
18169     add_AT_flag (array_die, DW_AT_declaration, 1);
18170   else
18171 #endif
18172   if (TREE_CODE (type) == VECTOR_TYPE)
18173     {
18174       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18175       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18176       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18177       add_bound_info (subrange_die, DW_AT_upper_bound,
18178                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18179     }
18180   else
18181     add_subscript_info (array_die, type, collapse_nested_arrays);
18182
18183   /* Add representation of the type of the elements of this array type and
18184      emit the corresponding DIE if we haven't done it already.  */
18185   element_type = TREE_TYPE (type);
18186   if (collapse_nested_arrays)
18187     while (TREE_CODE (element_type) == ARRAY_TYPE)
18188       {
18189         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18190           break;
18191         element_type = TREE_TYPE (element_type);
18192       }
18193
18194 #ifndef MIPS_DEBUGGING_INFO
18195   gen_type_die (element_type, context_die);
18196 #endif
18197
18198   add_type_attribute (array_die, element_type, 0, 0, context_die);
18199
18200   if (get_AT (array_die, DW_AT_name))
18201     add_pubtype (type, array_die);
18202 }
18203
18204 static dw_loc_descr_ref
18205 descr_info_loc (tree val, tree base_decl)
18206 {
18207   HOST_WIDE_INT size;
18208   dw_loc_descr_ref loc, loc2;
18209   enum dwarf_location_atom op;
18210
18211   if (val == base_decl)
18212     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18213
18214   switch (TREE_CODE (val))
18215     {
18216     CASE_CONVERT:
18217       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18218     case VAR_DECL:
18219       return loc_descriptor_from_tree (val, 0);
18220     case INTEGER_CST:
18221       if (host_integerp (val, 0))
18222         return int_loc_descriptor (tree_low_cst (val, 0));
18223       break;
18224     case INDIRECT_REF:
18225       size = int_size_in_bytes (TREE_TYPE (val));
18226       if (size < 0)
18227         break;
18228       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18229       if (!loc)
18230         break;
18231       if (size == DWARF2_ADDR_SIZE)
18232         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18233       else
18234         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18235       return loc;
18236     case POINTER_PLUS_EXPR:
18237     case PLUS_EXPR:
18238       if (host_integerp (TREE_OPERAND (val, 1), 1)
18239           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18240              < 16384)
18241         {
18242           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18243           if (!loc)
18244             break;
18245           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18246         }
18247       else
18248         {
18249           op = DW_OP_plus;
18250         do_binop:
18251           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18252           if (!loc)
18253             break;
18254           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18255           if (!loc2)
18256             break;
18257           add_loc_descr (&loc, loc2);
18258           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18259         }
18260       return loc;
18261     case MINUS_EXPR:
18262       op = DW_OP_minus;
18263       goto do_binop;
18264     case MULT_EXPR:
18265       op = DW_OP_mul;
18266       goto do_binop;
18267     case EQ_EXPR:
18268       op = DW_OP_eq;
18269       goto do_binop;
18270     case NE_EXPR:
18271       op = DW_OP_ne;
18272       goto do_binop;
18273     default:
18274       break;
18275     }
18276   return NULL;
18277 }
18278
18279 static void
18280 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18281                       tree val, tree base_decl)
18282 {
18283   dw_loc_descr_ref loc;
18284
18285   if (host_integerp (val, 0))
18286     {
18287       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18288       return;
18289     }
18290
18291   loc = descr_info_loc (val, base_decl);
18292   if (!loc)
18293     return;
18294
18295   add_AT_loc (die, attr, loc);
18296 }
18297
18298 /* This routine generates DIE for array with hidden descriptor, details
18299    are filled into *info by a langhook.  */
18300
18301 static void
18302 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18303                           dw_die_ref context_die)
18304 {
18305   dw_die_ref scope_die = scope_die_for (type, context_die);
18306   dw_die_ref array_die;
18307   int dim;
18308
18309   array_die = new_die (DW_TAG_array_type, scope_die, type);
18310   add_name_attribute (array_die, type_tag (type));
18311   equate_type_number_to_die (type, array_die);
18312
18313   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18314   if (is_fortran ()
18315       && info->ndimensions >= 2)
18316     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18317
18318   if (info->data_location)
18319     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18320                           info->base_decl);
18321   if (info->associated)
18322     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18323                           info->base_decl);
18324   if (info->allocated)
18325     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18326                           info->base_decl);
18327
18328   for (dim = 0; dim < info->ndimensions; dim++)
18329     {
18330       dw_die_ref subrange_die
18331         = new_die (DW_TAG_subrange_type, array_die, NULL);
18332
18333       if (info->dimen[dim].lower_bound)
18334         {
18335           /* If it is the default value, omit it.  */
18336           int dflt;
18337
18338           if (host_integerp (info->dimen[dim].lower_bound, 0)
18339               && (dflt = lower_bound_default ()) != -1
18340               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18341             ;
18342           else
18343             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18344                                   info->dimen[dim].lower_bound,
18345                                   info->base_decl);
18346         }
18347       if (info->dimen[dim].upper_bound)
18348         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18349                               info->dimen[dim].upper_bound,
18350                               info->base_decl);
18351       if (info->dimen[dim].stride)
18352         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18353                               info->dimen[dim].stride,
18354                               info->base_decl);
18355     }
18356
18357   gen_type_die (info->element_type, context_die);
18358   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18359
18360   if (get_AT (array_die, DW_AT_name))
18361     add_pubtype (type, array_die);
18362 }
18363
18364 #if 0
18365 static void
18366 gen_entry_point_die (tree decl, dw_die_ref context_die)
18367 {
18368   tree origin = decl_ultimate_origin (decl);
18369   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18370
18371   if (origin != NULL)
18372     add_abstract_origin_attribute (decl_die, origin);
18373   else
18374     {
18375       add_name_and_src_coords_attributes (decl_die, decl);
18376       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18377                           0, 0, context_die);
18378     }
18379
18380   if (DECL_ABSTRACT (decl))
18381     equate_decl_number_to_die (decl, decl_die);
18382   else
18383     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18384 }
18385 #endif
18386
18387 /* Walk through the list of incomplete types again, trying once more to
18388    emit full debugging info for them.  */
18389
18390 static void
18391 retry_incomplete_types (void)
18392 {
18393   int i;
18394
18395   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18396     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18397                                   DINFO_USAGE_DIR_USE))
18398       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18399 }
18400
18401 /* Determine what tag to use for a record type.  */
18402
18403 static enum dwarf_tag
18404 record_type_tag (tree type)
18405 {
18406   if (! lang_hooks.types.classify_record)
18407     return DW_TAG_structure_type;
18408
18409   switch (lang_hooks.types.classify_record (type))
18410     {
18411     case RECORD_IS_STRUCT:
18412       return DW_TAG_structure_type;
18413
18414     case RECORD_IS_CLASS:
18415       return DW_TAG_class_type;
18416
18417     case RECORD_IS_INTERFACE:
18418       if (dwarf_version >= 3 || !dwarf_strict)
18419         return DW_TAG_interface_type;
18420       return DW_TAG_structure_type;
18421
18422     default:
18423       gcc_unreachable ();
18424     }
18425 }
18426
18427 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18428    include all of the information about the enumeration values also. Each
18429    enumerated type name/value is listed as a child of the enumerated type
18430    DIE.  */
18431
18432 static dw_die_ref
18433 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18434 {
18435   dw_die_ref type_die = lookup_type_die (type);
18436
18437   if (type_die == NULL)
18438     {
18439       type_die = new_die (DW_TAG_enumeration_type,
18440                           scope_die_for (type, context_die), type);
18441       equate_type_number_to_die (type, type_die);
18442       add_name_attribute (type_die, type_tag (type));
18443       if (dwarf_version >= 4 || !dwarf_strict)
18444         {
18445           if (ENUM_IS_SCOPED (type))
18446             add_AT_flag (type_die, DW_AT_enum_class, 1);
18447           if (ENUM_IS_OPAQUE (type))
18448             add_AT_flag (type_die, DW_AT_declaration, 1);
18449         }
18450     }
18451   else if (! TYPE_SIZE (type))
18452     return type_die;
18453   else
18454     remove_AT (type_die, DW_AT_declaration);
18455
18456   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18457      given enum type is incomplete, do not generate the DW_AT_byte_size
18458      attribute or the DW_AT_element_list attribute.  */
18459   if (TYPE_SIZE (type))
18460     {
18461       tree link;
18462
18463       TREE_ASM_WRITTEN (type) = 1;
18464       add_byte_size_attribute (type_die, type);
18465       if (TYPE_STUB_DECL (type) != NULL_TREE)
18466         {
18467           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18468           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18469         }
18470
18471       /* If the first reference to this type was as the return type of an
18472          inline function, then it may not have a parent.  Fix this now.  */
18473       if (type_die->die_parent == NULL)
18474         add_child_die (scope_die_for (type, context_die), type_die);
18475
18476       for (link = TYPE_VALUES (type);
18477            link != NULL; link = TREE_CHAIN (link))
18478         {
18479           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18480           tree value = TREE_VALUE (link);
18481
18482           add_name_attribute (enum_die,
18483                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18484
18485           if (TREE_CODE (value) == CONST_DECL)
18486             value = DECL_INITIAL (value);
18487
18488           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18489             /* DWARF2 does not provide a way of indicating whether or
18490                not enumeration constants are signed or unsigned.  GDB
18491                always assumes the values are signed, so we output all
18492                values as if they were signed.  That means that
18493                enumeration constants with very large unsigned values
18494                will appear to have negative values in the debugger.  */
18495             add_AT_int (enum_die, DW_AT_const_value,
18496                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18497         }
18498     }
18499   else
18500     add_AT_flag (type_die, DW_AT_declaration, 1);
18501
18502   if (get_AT (type_die, DW_AT_name))
18503     add_pubtype (type, type_die);
18504
18505   return type_die;
18506 }
18507
18508 /* Generate a DIE to represent either a real live formal parameter decl or to
18509    represent just the type of some formal parameter position in some function
18510    type.
18511
18512    Note that this routine is a bit unusual because its argument may be a
18513    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18514    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18515    node.  If it's the former then this function is being called to output a
18516    DIE to represent a formal parameter object (or some inlining thereof).  If
18517    it's the latter, then this function is only being called to output a
18518    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18519    argument type of some subprogram type.
18520    If EMIT_NAME_P is true, name and source coordinate attributes
18521    are emitted.  */
18522
18523 static dw_die_ref
18524 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18525                           dw_die_ref context_die)
18526 {
18527   tree node_or_origin = node ? node : origin;
18528   tree ultimate_origin;
18529   dw_die_ref parm_die
18530     = new_die (DW_TAG_formal_parameter, context_die, node);
18531
18532   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18533     {
18534     case tcc_declaration:
18535       ultimate_origin = decl_ultimate_origin (node_or_origin);
18536       if (node || ultimate_origin)
18537         origin = ultimate_origin;
18538       if (origin != NULL)
18539         add_abstract_origin_attribute (parm_die, origin);
18540       else if (emit_name_p)
18541         add_name_and_src_coords_attributes (parm_die, node);
18542       if (origin == NULL
18543           || (! DECL_ABSTRACT (node_or_origin)
18544               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18545                                            decl_function_context
18546                                                             (node_or_origin))))
18547         {
18548           tree type = TREE_TYPE (node_or_origin);
18549           if (decl_by_reference_p (node_or_origin))
18550             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18551                                 context_die);
18552           else
18553             add_type_attribute (parm_die, type,
18554                                 TREE_READONLY (node_or_origin),
18555                                 TREE_THIS_VOLATILE (node_or_origin),
18556                                 context_die);
18557         }
18558       if (origin == NULL && DECL_ARTIFICIAL (node))
18559         add_AT_flag (parm_die, DW_AT_artificial, 1);
18560
18561       if (node && node != origin)
18562         equate_decl_number_to_die (node, parm_die);
18563       if (! DECL_ABSTRACT (node_or_origin))
18564         add_location_or_const_value_attribute (parm_die, node_or_origin,
18565                                                DW_AT_location);
18566
18567       break;
18568
18569     case tcc_type:
18570       /* We were called with some kind of a ..._TYPE node.  */
18571       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18572       break;
18573
18574     default:
18575       gcc_unreachable ();
18576     }
18577
18578   return parm_die;
18579 }
18580
18581 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18582    children DW_TAG_formal_parameter DIEs representing the arguments of the
18583    parameter pack.
18584
18585    PARM_PACK must be a function parameter pack.
18586    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18587    must point to the subsequent arguments of the function PACK_ARG belongs to.
18588    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18589    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18590    following the last one for which a DIE was generated.  */
18591
18592 static dw_die_ref
18593 gen_formal_parameter_pack_die  (tree parm_pack,
18594                                 tree pack_arg,
18595                                 dw_die_ref subr_die,
18596                                 tree *next_arg)
18597 {
18598   tree arg;
18599   dw_die_ref parm_pack_die;
18600
18601   gcc_assert (parm_pack
18602               && lang_hooks.function_parameter_pack_p (parm_pack)
18603               && subr_die);
18604
18605   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18606   add_src_coords_attributes (parm_pack_die, parm_pack);
18607
18608   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18609     {
18610       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18611                                                                  parm_pack))
18612         break;
18613       gen_formal_parameter_die (arg, NULL,
18614                                 false /* Don't emit name attribute.  */,
18615                                 parm_pack_die);
18616     }
18617   if (next_arg)
18618     *next_arg = arg;
18619   return parm_pack_die;
18620 }
18621
18622 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18623    at the end of an (ANSI prototyped) formal parameters list.  */
18624
18625 static void
18626 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18627 {
18628   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18629 }
18630
18631 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18632    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18633    parameters as specified in some function type specification (except for
18634    those which appear as part of a function *definition*).  */
18635
18636 static void
18637 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18638 {
18639   tree link;
18640   tree formal_type = NULL;
18641   tree first_parm_type;
18642   tree arg;
18643
18644   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18645     {
18646       arg = DECL_ARGUMENTS (function_or_method_type);
18647       function_or_method_type = TREE_TYPE (function_or_method_type);
18648     }
18649   else
18650     arg = NULL_TREE;
18651
18652   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18653
18654   /* Make our first pass over the list of formal parameter types and output a
18655      DW_TAG_formal_parameter DIE for each one.  */
18656   for (link = first_parm_type; link; )
18657     {
18658       dw_die_ref parm_die;
18659
18660       formal_type = TREE_VALUE (link);
18661       if (formal_type == void_type_node)
18662         break;
18663
18664       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18665       parm_die = gen_formal_parameter_die (formal_type, NULL,
18666                                            true /* Emit name attribute.  */,
18667                                            context_die);
18668       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18669           && link == first_parm_type)
18670         {
18671           add_AT_flag (parm_die, DW_AT_artificial, 1);
18672           if (dwarf_version >= 3 || !dwarf_strict)
18673             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18674         }
18675       else if (arg && DECL_ARTIFICIAL (arg))
18676         add_AT_flag (parm_die, DW_AT_artificial, 1);
18677
18678       link = TREE_CHAIN (link);
18679       if (arg)
18680         arg = DECL_CHAIN (arg);
18681     }
18682
18683   /* If this function type has an ellipsis, add a
18684      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18685   if (formal_type != void_type_node)
18686     gen_unspecified_parameters_die (function_or_method_type, context_die);
18687
18688   /* Make our second (and final) pass over the list of formal parameter types
18689      and output DIEs to represent those types (as necessary).  */
18690   for (link = TYPE_ARG_TYPES (function_or_method_type);
18691        link && TREE_VALUE (link);
18692        link = TREE_CHAIN (link))
18693     gen_type_die (TREE_VALUE (link), context_die);
18694 }
18695
18696 /* We want to generate the DIE for TYPE so that we can generate the
18697    die for MEMBER, which has been defined; we will need to refer back
18698    to the member declaration nested within TYPE.  If we're trying to
18699    generate minimal debug info for TYPE, processing TYPE won't do the
18700    trick; we need to attach the member declaration by hand.  */
18701
18702 static void
18703 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18704 {
18705   gen_type_die (type, context_die);
18706
18707   /* If we're trying to avoid duplicate debug info, we may not have
18708      emitted the member decl for this function.  Emit it now.  */
18709   if (TYPE_STUB_DECL (type)
18710       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18711       && ! lookup_decl_die (member))
18712     {
18713       dw_die_ref type_die;
18714       gcc_assert (!decl_ultimate_origin (member));
18715
18716       push_decl_scope (type);
18717       type_die = lookup_type_die (type);
18718       if (TREE_CODE (member) == FUNCTION_DECL)
18719         gen_subprogram_die (member, type_die);
18720       else if (TREE_CODE (member) == FIELD_DECL)
18721         {
18722           /* Ignore the nameless fields that are used to skip bits but handle
18723              C++ anonymous unions and structs.  */
18724           if (DECL_NAME (member) != NULL_TREE
18725               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18726               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18727             {
18728               gen_type_die (member_declared_type (member), type_die);
18729               gen_field_die (member, type_die);
18730             }
18731         }
18732       else
18733         gen_variable_die (member, NULL_TREE, type_die);
18734
18735       pop_decl_scope ();
18736     }
18737 }
18738
18739 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18740    may later generate inlined and/or out-of-line instances of.  */
18741
18742 static void
18743 dwarf2out_abstract_function (tree decl)
18744 {
18745   dw_die_ref old_die;
18746   tree save_fn;
18747   tree context;
18748   int was_abstract;
18749   htab_t old_decl_loc_table;
18750
18751   /* Make sure we have the actual abstract inline, not a clone.  */
18752   decl = DECL_ORIGIN (decl);
18753
18754   old_die = lookup_decl_die (decl);
18755   if (old_die && get_AT (old_die, DW_AT_inline))
18756     /* We've already generated the abstract instance.  */
18757     return;
18758
18759   /* We can be called while recursively when seeing block defining inlined subroutine
18760      DIE.  Be sure to not clobber the outer location table nor use it or we would
18761      get locations in abstract instantces.  */
18762   old_decl_loc_table = decl_loc_table;
18763   decl_loc_table = NULL;
18764
18765   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18766      we don't get confused by DECL_ABSTRACT.  */
18767   if (debug_info_level > DINFO_LEVEL_TERSE)
18768     {
18769       context = decl_class_context (decl);
18770       if (context)
18771         gen_type_die_for_member
18772           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18773     }
18774
18775   /* Pretend we've just finished compiling this function.  */
18776   save_fn = current_function_decl;
18777   current_function_decl = decl;
18778   push_cfun (DECL_STRUCT_FUNCTION (decl));
18779
18780   was_abstract = DECL_ABSTRACT (decl);
18781   set_decl_abstract_flags (decl, 1);
18782   dwarf2out_decl (decl);
18783   if (! was_abstract)
18784     set_decl_abstract_flags (decl, 0);
18785
18786   current_function_decl = save_fn;
18787   decl_loc_table = old_decl_loc_table;
18788   pop_cfun ();
18789 }
18790
18791 /* Helper function of premark_used_types() which gets called through
18792    htab_traverse.
18793
18794    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18795    marked as unused by prune_unused_types.  */
18796
18797 static int
18798 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18799 {
18800   tree type;
18801   dw_die_ref die;
18802
18803   type = (tree) *slot;
18804   die = lookup_type_die (type);
18805   if (die != NULL)
18806     die->die_perennial_p = 1;
18807   return 1;
18808 }
18809
18810 /* Helper function of premark_types_used_by_global_vars which gets called
18811    through htab_traverse.
18812
18813    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18814    marked as unused by prune_unused_types. The DIE of the type is marked
18815    only if the global variable using the type will actually be emitted.  */
18816
18817 static int
18818 premark_types_used_by_global_vars_helper (void **slot,
18819                                           void *data ATTRIBUTE_UNUSED)
18820 {
18821   struct types_used_by_vars_entry *entry;
18822   dw_die_ref die;
18823
18824   entry = (struct types_used_by_vars_entry *) *slot;
18825   gcc_assert (entry->type != NULL
18826               && entry->var_decl != NULL);
18827   die = lookup_type_die (entry->type);
18828   if (die)
18829     {
18830       /* Ask cgraph if the global variable really is to be emitted.
18831          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18832       struct varpool_node *node = varpool_get_node (entry->var_decl);
18833       if (node && node->needed)
18834         {
18835           die->die_perennial_p = 1;
18836           /* Keep the parent DIEs as well.  */
18837           while ((die = die->die_parent) && die->die_perennial_p == 0)
18838             die->die_perennial_p = 1;
18839         }
18840     }
18841   return 1;
18842 }
18843
18844 /* Mark all members of used_types_hash as perennial.  */
18845
18846 static void
18847 premark_used_types (void)
18848 {
18849   if (cfun && cfun->used_types_hash)
18850     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18851 }
18852
18853 /* Mark all members of types_used_by_vars_entry as perennial.  */
18854
18855 static void
18856 premark_types_used_by_global_vars (void)
18857 {
18858   if (types_used_by_vars_hash)
18859     htab_traverse (types_used_by_vars_hash,
18860                    premark_types_used_by_global_vars_helper, NULL);
18861 }
18862
18863 /* Generate a DIE to represent a declared function (either file-scope or
18864    block-local).  */
18865
18866 static void
18867 gen_subprogram_die (tree decl, dw_die_ref context_die)
18868 {
18869   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18870   tree origin = decl_ultimate_origin (decl);
18871   dw_die_ref subr_die;
18872   tree fn_arg_types;
18873   tree outer_scope;
18874   dw_die_ref old_die = lookup_decl_die (decl);
18875   int declaration = (current_function_decl != decl
18876                      || class_or_namespace_scope_p (context_die));
18877
18878   premark_used_types ();
18879
18880   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18881      started to generate the abstract instance of an inline, decided to output
18882      its containing class, and proceeded to emit the declaration of the inline
18883      from the member list for the class.  If so, DECLARATION takes priority;
18884      we'll get back to the abstract instance when done with the class.  */
18885
18886   /* The class-scope declaration DIE must be the primary DIE.  */
18887   if (origin && declaration && class_or_namespace_scope_p (context_die))
18888     {
18889       origin = NULL;
18890       gcc_assert (!old_die);
18891     }
18892
18893   /* Now that the C++ front end lazily declares artificial member fns, we
18894      might need to retrofit the declaration into its class.  */
18895   if (!declaration && !origin && !old_die
18896       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18897       && !class_or_namespace_scope_p (context_die)
18898       && debug_info_level > DINFO_LEVEL_TERSE)
18899     old_die = force_decl_die (decl);
18900
18901   if (origin != NULL)
18902     {
18903       gcc_assert (!declaration || local_scope_p (context_die));
18904
18905       /* Fixup die_parent for the abstract instance of a nested
18906          inline function.  */
18907       if (old_die && old_die->die_parent == NULL)
18908         add_child_die (context_die, old_die);
18909
18910       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18911       add_abstract_origin_attribute (subr_die, origin);
18912     }
18913   else if (old_die)
18914     {
18915       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18916       struct dwarf_file_data * file_index = lookup_filename (s.file);
18917
18918       if (!get_AT_flag (old_die, DW_AT_declaration)
18919           /* We can have a normal definition following an inline one in the
18920              case of redefinition of GNU C extern inlines.
18921              It seems reasonable to use AT_specification in this case.  */
18922           && !get_AT (old_die, DW_AT_inline))
18923         {
18924           /* Detect and ignore this case, where we are trying to output
18925              something we have already output.  */
18926           return;
18927         }
18928
18929       /* If the definition comes from the same place as the declaration,
18930          maybe use the old DIE.  We always want the DIE for this function
18931          that has the *_pc attributes to be under comp_unit_die so the
18932          debugger can find it.  We also need to do this for abstract
18933          instances of inlines, since the spec requires the out-of-line copy
18934          to have the same parent.  For local class methods, this doesn't
18935          apply; we just use the old DIE.  */
18936       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18937           && (DECL_ARTIFICIAL (decl)
18938               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18939                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18940                       == (unsigned) s.line))))
18941         {
18942           subr_die = old_die;
18943
18944           /* Clear out the declaration attribute and the formal parameters.
18945              Do not remove all children, because it is possible that this
18946              declaration die was forced using force_decl_die(). In such
18947              cases die that forced declaration die (e.g. TAG_imported_module)
18948              is one of the children that we do not want to remove.  */
18949           remove_AT (subr_die, DW_AT_declaration);
18950           remove_AT (subr_die, DW_AT_object_pointer);
18951           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18952         }
18953       else
18954         {
18955           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18956           add_AT_specification (subr_die, old_die);
18957           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18958             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18959           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18960             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18961         }
18962     }
18963   else
18964     {
18965       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18966
18967       if (TREE_PUBLIC (decl))
18968         add_AT_flag (subr_die, DW_AT_external, 1);
18969
18970       add_name_and_src_coords_attributes (subr_die, decl);
18971       if (debug_info_level > DINFO_LEVEL_TERSE)
18972         {
18973           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18974           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18975                               0, 0, context_die);
18976         }
18977
18978       add_pure_or_virtual_attribute (subr_die, decl);
18979       if (DECL_ARTIFICIAL (decl))
18980         add_AT_flag (subr_die, DW_AT_artificial, 1);
18981
18982       add_accessibility_attribute (subr_die, decl);
18983     }
18984
18985   if (declaration)
18986     {
18987       if (!old_die || !get_AT (old_die, DW_AT_inline))
18988         {
18989           add_AT_flag (subr_die, DW_AT_declaration, 1);
18990
18991           /* If this is an explicit function declaration then generate
18992              a DW_AT_explicit attribute.  */
18993           if (lang_hooks.decls.function_decl_explicit_p (decl)
18994               && (dwarf_version >= 3 || !dwarf_strict))
18995             add_AT_flag (subr_die, DW_AT_explicit, 1);
18996
18997           /* The first time we see a member function, it is in the context of
18998              the class to which it belongs.  We make sure of this by emitting
18999              the class first.  The next time is the definition, which is
19000              handled above.  The two may come from the same source text.
19001
19002              Note that force_decl_die() forces function declaration die. It is
19003              later reused to represent definition.  */
19004           equate_decl_number_to_die (decl, subr_die);
19005         }
19006     }
19007   else if (DECL_ABSTRACT (decl))
19008     {
19009       if (DECL_DECLARED_INLINE_P (decl))
19010         {
19011           if (cgraph_function_possibly_inlined_p (decl))
19012             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19013           else
19014             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19015         }
19016       else
19017         {
19018           if (cgraph_function_possibly_inlined_p (decl))
19019             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19020           else
19021             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19022         }
19023
19024       if (DECL_DECLARED_INLINE_P (decl)
19025           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19026         add_AT_flag (subr_die, DW_AT_artificial, 1);
19027
19028       equate_decl_number_to_die (decl, subr_die);
19029     }
19030   else if (!DECL_EXTERNAL (decl))
19031     {
19032       HOST_WIDE_INT cfa_fb_offset;
19033
19034       if (!old_die || !get_AT (old_die, DW_AT_inline))
19035         equate_decl_number_to_die (decl, subr_die);
19036
19037       if (!flag_reorder_blocks_and_partition)
19038         {
19039           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19040                                        current_function_funcdef_no);
19041           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19042           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19043                                        current_function_funcdef_no);
19044           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19045
19046 #if VMS_DEBUGGING_INFO
19047       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19048          Section 2.3 Prologue and Epilogue Attributes:
19049          When a breakpoint is set on entry to a function, it is generally
19050          desirable for execution to be suspended, not on the very first
19051          instruction of the function, but rather at a point after the
19052          function's frame has been set up, after any language defined local
19053          declaration processing has been completed, and before execution of
19054          the first statement of the function begins. Debuggers generally
19055          cannot properly determine where this point is.  Similarly for a
19056          breakpoint set on exit from a function. The prologue and epilogue
19057          attributes allow a compiler to communicate the location(s) to use.  */
19058
19059       {
19060         dw_fde_ref fde = &fde_table[current_funcdef_fde];
19061
19062         if (fde->dw_fde_vms_end_prologue)
19063           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19064             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19065
19066         if (fde->dw_fde_vms_begin_epilogue)
19067           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19068             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19069       }
19070 #endif
19071
19072           add_pubname (decl, subr_die);
19073           add_arange (decl, subr_die);
19074         }
19075       else
19076         {  /* Do nothing for now; maybe need to duplicate die, one for
19077               hot section and one for cold section, then use the hot/cold
19078               section begin/end labels to generate the aranges...  */
19079           /*
19080             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
19081             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
19082             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
19083             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
19084
19085             add_pubname (decl, subr_die);
19086             add_arange (decl, subr_die);
19087             add_arange (decl, subr_die);
19088            */
19089         }
19090
19091 #ifdef MIPS_DEBUGGING_INFO
19092       /* Add a reference to the FDE for this routine.  */
19093       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19094 #endif
19095
19096       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19097
19098       /* We define the "frame base" as the function's CFA.  This is more
19099          convenient for several reasons: (1) It's stable across the prologue
19100          and epilogue, which makes it better than just a frame pointer,
19101          (2) With dwarf3, there exists a one-byte encoding that allows us
19102          to reference the .debug_frame data by proxy, but failing that,
19103          (3) We can at least reuse the code inspection and interpretation
19104          code that determines the CFA position at various points in the
19105          function.  */
19106       if (dwarf_version >= 3)
19107         {
19108           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19109           add_AT_loc (subr_die, DW_AT_frame_base, op);
19110         }
19111       else
19112         {
19113           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19114           if (list->dw_loc_next)
19115             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19116           else
19117             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19118         }
19119
19120       /* Compute a displacement from the "steady-state frame pointer" to
19121          the CFA.  The former is what all stack slots and argument slots
19122          will reference in the rtl; the later is what we've told the
19123          debugger about.  We'll need to adjust all frame_base references
19124          by this displacement.  */
19125       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19126
19127       if (cfun->static_chain_decl)
19128         add_AT_location_description (subr_die, DW_AT_static_link,
19129                  loc_list_from_tree (cfun->static_chain_decl, 2));
19130     }
19131
19132   /* Generate child dies for template paramaters.  */
19133   if (debug_info_level > DINFO_LEVEL_TERSE)
19134     gen_generic_params_dies (decl);
19135
19136   /* Now output descriptions of the arguments for this function. This gets
19137      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19138      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19139      `...' at the end of the formal parameter list.  In order to find out if
19140      there was a trailing ellipsis or not, we must instead look at the type
19141      associated with the FUNCTION_DECL.  This will be a node of type
19142      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19143      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19144      an ellipsis at the end.  */
19145
19146   /* In the case where we are describing a mere function declaration, all we
19147      need to do here (and all we *can* do here) is to describe the *types* of
19148      its formal parameters.  */
19149   if (debug_info_level <= DINFO_LEVEL_TERSE)
19150     ;
19151   else if (declaration)
19152     gen_formal_types_die (decl, subr_die);
19153   else
19154     {
19155       /* Generate DIEs to represent all known formal parameters.  */
19156       tree parm = DECL_ARGUMENTS (decl);
19157       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19158       tree generic_decl_parm = generic_decl
19159                                 ? DECL_ARGUMENTS (generic_decl)
19160                                 : NULL;
19161
19162       /* Now we want to walk the list of parameters of the function and
19163          emit their relevant DIEs.
19164
19165          We consider the case of DECL being an instance of a generic function
19166          as well as it being a normal function.
19167
19168          If DECL is an instance of a generic function we walk the
19169          parameters of the generic function declaration _and_ the parameters of
19170          DECL itself. This is useful because we want to emit specific DIEs for
19171          function parameter packs and those are declared as part of the
19172          generic function declaration. In that particular case,
19173          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19174          That DIE has children DIEs representing the set of arguments
19175          of the pack. Note that the set of pack arguments can be empty.
19176          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19177          children DIE.
19178
19179          Otherwise, we just consider the parameters of DECL.  */
19180       while (generic_decl_parm || parm)
19181         {
19182           if (generic_decl_parm
19183               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19184             gen_formal_parameter_pack_die (generic_decl_parm,
19185                                            parm, subr_die,
19186                                            &parm);
19187           else if (parm)
19188             {
19189               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19190
19191               if (parm == DECL_ARGUMENTS (decl)
19192                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19193                   && parm_die
19194                   && (dwarf_version >= 3 || !dwarf_strict))
19195                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19196
19197               parm = DECL_CHAIN (parm);
19198             }
19199
19200           if (generic_decl_parm)
19201             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19202         }
19203
19204       /* Decide whether we need an unspecified_parameters DIE at the end.
19205          There are 2 more cases to do this for: 1) the ansi ... declaration -
19206          this is detectable when the end of the arg list is not a
19207          void_type_node 2) an unprototyped function declaration (not a
19208          definition).  This just means that we have no info about the
19209          parameters at all.  */
19210       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
19211       if (fn_arg_types != NULL)
19212         {
19213           /* This is the prototyped case, check for....  */
19214           if (stdarg_p (TREE_TYPE (decl)))
19215             gen_unspecified_parameters_die (decl, subr_die);
19216         }
19217       else if (DECL_INITIAL (decl) == NULL_TREE)
19218         gen_unspecified_parameters_die (decl, subr_die);
19219     }
19220
19221   /* Output Dwarf info for all of the stuff within the body of the function
19222      (if it has one - it may be just a declaration).  */
19223   outer_scope = DECL_INITIAL (decl);
19224
19225   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19226      a function.  This BLOCK actually represents the outermost binding contour
19227      for the function, i.e. the contour in which the function's formal
19228      parameters and labels get declared. Curiously, it appears that the front
19229      end doesn't actually put the PARM_DECL nodes for the current function onto
19230      the BLOCK_VARS list for this outer scope, but are strung off of the
19231      DECL_ARGUMENTS list for the function instead.
19232
19233      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19234      the LABEL_DECL nodes for the function however, and we output DWARF info
19235      for those in decls_for_scope.  Just within the `outer_scope' there will be
19236      a BLOCK node representing the function's outermost pair of curly braces,
19237      and any blocks used for the base and member initializers of a C++
19238      constructor function.  */
19239   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19240     {
19241       /* Emit a DW_TAG_variable DIE for a named return value.  */
19242       if (DECL_NAME (DECL_RESULT (decl)))
19243         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19244
19245       current_function_has_inlines = 0;
19246       decls_for_scope (outer_scope, subr_die, 0);
19247     }
19248   /* Add the calling convention attribute if requested.  */
19249   add_calling_convention_attribute (subr_die, decl);
19250
19251 }
19252
19253 /* Returns a hash value for X (which really is a die_struct).  */
19254
19255 static hashval_t
19256 common_block_die_table_hash (const void *x)
19257 {
19258   const_dw_die_ref d = (const_dw_die_ref) x;
19259   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19260 }
19261
19262 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19263    as decl_id and die_parent of die_struct Y.  */
19264
19265 static int
19266 common_block_die_table_eq (const void *x, const void *y)
19267 {
19268   const_dw_die_ref d = (const_dw_die_ref) x;
19269   const_dw_die_ref e = (const_dw_die_ref) y;
19270   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19271 }
19272
19273 /* Generate a DIE to represent a declared data object.
19274    Either DECL or ORIGIN must be non-null.  */
19275
19276 static void
19277 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19278 {
19279   HOST_WIDE_INT off;
19280   tree com_decl;
19281   tree decl_or_origin = decl ? decl : origin;
19282   tree ultimate_origin;
19283   dw_die_ref var_die;
19284   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19285   dw_die_ref origin_die;
19286   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19287                       || class_or_namespace_scope_p (context_die));
19288   bool specialization_p = false;
19289
19290   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19291   if (decl || ultimate_origin)
19292     origin = ultimate_origin;
19293   com_decl = fortran_common (decl_or_origin, &off);
19294
19295   /* Symbol in common gets emitted as a child of the common block, in the form
19296      of a data member.  */
19297   if (com_decl)
19298     {
19299       dw_die_ref com_die;
19300       dw_loc_list_ref loc;
19301       die_node com_die_arg;
19302
19303       var_die = lookup_decl_die (decl_or_origin);
19304       if (var_die)
19305         {
19306           if (get_AT (var_die, DW_AT_location) == NULL)
19307             {
19308               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19309               if (loc)
19310                 {
19311                   if (off)
19312                     {
19313                       /* Optimize the common case.  */
19314                       if (single_element_loc_list_p (loc)
19315                           && loc->expr->dw_loc_opc == DW_OP_addr
19316                           && loc->expr->dw_loc_next == NULL
19317                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19318                              == SYMBOL_REF)
19319                         loc->expr->dw_loc_oprnd1.v.val_addr
19320                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19321                         else
19322                           loc_list_plus_const (loc, off);
19323                     }
19324                   add_AT_location_description (var_die, DW_AT_location, loc);
19325                   remove_AT (var_die, DW_AT_declaration);
19326                 }
19327             }
19328           return;
19329         }
19330
19331       if (common_block_die_table == NULL)
19332         common_block_die_table
19333           = htab_create_ggc (10, common_block_die_table_hash,
19334                              common_block_die_table_eq, NULL);
19335
19336       com_die_arg.decl_id = DECL_UID (com_decl);
19337       com_die_arg.die_parent = context_die;
19338       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19339       loc = loc_list_from_tree (com_decl, 2);
19340       if (com_die == NULL)
19341         {
19342           const char *cnam
19343             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19344           void **slot;
19345
19346           com_die = new_die (DW_TAG_common_block, context_die, decl);
19347           add_name_and_src_coords_attributes (com_die, com_decl);
19348           if (loc)
19349             {
19350               add_AT_location_description (com_die, DW_AT_location, loc);
19351               /* Avoid sharing the same loc descriptor between
19352                  DW_TAG_common_block and DW_TAG_variable.  */
19353               loc = loc_list_from_tree (com_decl, 2);
19354             }
19355           else if (DECL_EXTERNAL (decl))
19356             add_AT_flag (com_die, DW_AT_declaration, 1);
19357           add_pubname_string (cnam, com_die); /* ??? needed? */
19358           com_die->decl_id = DECL_UID (com_decl);
19359           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19360           *slot = (void *) com_die;
19361         }
19362       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19363         {
19364           add_AT_location_description (com_die, DW_AT_location, loc);
19365           loc = loc_list_from_tree (com_decl, 2);
19366           remove_AT (com_die, DW_AT_declaration);
19367         }
19368       var_die = new_die (DW_TAG_variable, com_die, decl);
19369       add_name_and_src_coords_attributes (var_die, decl);
19370       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19371                           TREE_THIS_VOLATILE (decl), context_die);
19372       add_AT_flag (var_die, DW_AT_external, 1);
19373       if (loc)
19374         {
19375           if (off)
19376             {
19377               /* Optimize the common case.  */
19378               if (single_element_loc_list_p (loc)
19379                   && loc->expr->dw_loc_opc == DW_OP_addr
19380                   && loc->expr->dw_loc_next == NULL
19381                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19382                 loc->expr->dw_loc_oprnd1.v.val_addr
19383                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19384               else
19385                 loc_list_plus_const (loc, off);
19386             }
19387           add_AT_location_description (var_die, DW_AT_location, loc);
19388         }
19389       else if (DECL_EXTERNAL (decl))
19390         add_AT_flag (var_die, DW_AT_declaration, 1);
19391       equate_decl_number_to_die (decl, var_die);
19392       return;
19393     }
19394
19395   /* If the compiler emitted a definition for the DECL declaration
19396      and if we already emitted a DIE for it, don't emit a second
19397      DIE for it again. Allow re-declarations of DECLs that are
19398      inside functions, though.  */
19399   if (old_die && declaration && !local_scope_p (context_die))
19400     return;
19401
19402   /* For static data members, the declaration in the class is supposed
19403      to have DW_TAG_member tag; the specification should still be
19404      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19405   if (declaration && class_scope_p (context_die))
19406     var_die = new_die (DW_TAG_member, context_die, decl);
19407   else
19408     var_die = new_die (DW_TAG_variable, context_die, decl);
19409
19410   origin_die = NULL;
19411   if (origin != NULL)
19412     origin_die = add_abstract_origin_attribute (var_die, origin);
19413
19414   /* Loop unrolling can create multiple blocks that refer to the same
19415      static variable, so we must test for the DW_AT_declaration flag.
19416
19417      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19418      copy decls and set the DECL_ABSTRACT flag on them instead of
19419      sharing them.
19420
19421      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19422
19423      ??? The declare_in_namespace support causes us to get two DIEs for one
19424      variable, both of which are declarations.  We want to avoid considering
19425      one to be a specification, so we must test that this DIE is not a
19426      declaration.  */
19427   else if (old_die && TREE_STATIC (decl) && ! declaration
19428            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19429     {
19430       /* This is a definition of a C++ class level static.  */
19431       add_AT_specification (var_die, old_die);
19432       specialization_p = true;
19433       if (DECL_NAME (decl))
19434         {
19435           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19436           struct dwarf_file_data * file_index = lookup_filename (s.file);
19437
19438           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19439             add_AT_file (var_die, DW_AT_decl_file, file_index);
19440
19441           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19442             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19443
19444           if (old_die->die_tag == DW_TAG_member)
19445             add_linkage_name (var_die, decl);
19446         }
19447     }
19448   else
19449     add_name_and_src_coords_attributes (var_die, decl);
19450
19451   if ((origin == NULL && !specialization_p)
19452       || (origin != NULL
19453           && !DECL_ABSTRACT (decl_or_origin)
19454           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19455                                        decl_function_context
19456                                                         (decl_or_origin))))
19457     {
19458       tree type = TREE_TYPE (decl_or_origin);
19459
19460       if (decl_by_reference_p (decl_or_origin))
19461         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19462       else
19463         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19464                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19465     }
19466
19467   if (origin == NULL && !specialization_p)
19468     {
19469       if (TREE_PUBLIC (decl))
19470         add_AT_flag (var_die, DW_AT_external, 1);
19471
19472       if (DECL_ARTIFICIAL (decl))
19473         add_AT_flag (var_die, DW_AT_artificial, 1);
19474
19475       add_accessibility_attribute (var_die, decl);
19476     }
19477
19478   if (declaration)
19479     add_AT_flag (var_die, DW_AT_declaration, 1);
19480
19481   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19482     equate_decl_number_to_die (decl, var_die);
19483
19484   if (! declaration
19485       && (! DECL_ABSTRACT (decl_or_origin)
19486           /* Local static vars are shared between all clones/inlines,
19487              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19488              already set.  */
19489           || (TREE_CODE (decl_or_origin) == VAR_DECL
19490               && TREE_STATIC (decl_or_origin)
19491               && DECL_RTL_SET_P (decl_or_origin)))
19492       /* When abstract origin already has DW_AT_location attribute, no need
19493          to add it again.  */
19494       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19495     {
19496       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19497           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19498         defer_location (decl_or_origin, var_die);
19499       else
19500         add_location_or_const_value_attribute (var_die,
19501                                                decl_or_origin,
19502                                                DW_AT_location);
19503       add_pubname (decl_or_origin, var_die);
19504     }
19505   else
19506     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19507 }
19508
19509 /* Generate a DIE to represent a named constant.  */
19510
19511 static void
19512 gen_const_die (tree decl, dw_die_ref context_die)
19513 {
19514   dw_die_ref const_die;
19515   tree type = TREE_TYPE (decl);
19516
19517   const_die = new_die (DW_TAG_constant, context_die, decl);
19518   add_name_and_src_coords_attributes (const_die, decl);
19519   add_type_attribute (const_die, type, 1, 0, context_die);
19520   if (TREE_PUBLIC (decl))
19521     add_AT_flag (const_die, DW_AT_external, 1);
19522   if (DECL_ARTIFICIAL (decl))
19523     add_AT_flag (const_die, DW_AT_artificial, 1);
19524   tree_add_const_value_attribute_for_decl (const_die, decl);
19525 }
19526
19527 /* Generate a DIE to represent a label identifier.  */
19528
19529 static void
19530 gen_label_die (tree decl, dw_die_ref context_die)
19531 {
19532   tree origin = decl_ultimate_origin (decl);
19533   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19534   rtx insn;
19535   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19536
19537   if (origin != NULL)
19538     add_abstract_origin_attribute (lbl_die, origin);
19539   else
19540     add_name_and_src_coords_attributes (lbl_die, decl);
19541
19542   if (DECL_ABSTRACT (decl))
19543     equate_decl_number_to_die (decl, lbl_die);
19544   else
19545     {
19546       insn = DECL_RTL_IF_SET (decl);
19547
19548       /* Deleted labels are programmer specified labels which have been
19549          eliminated because of various optimizations.  We still emit them
19550          here so that it is possible to put breakpoints on them.  */
19551       if (insn
19552           && (LABEL_P (insn)
19553               || ((NOTE_P (insn)
19554                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19555         {
19556           /* When optimization is enabled (via -O) some parts of the compiler
19557              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19558              represent source-level labels which were explicitly declared by
19559              the user.  This really shouldn't be happening though, so catch
19560              it if it ever does happen.  */
19561           gcc_assert (!INSN_DELETED_P (insn));
19562
19563           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19564           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19565         }
19566     }
19567 }
19568
19569 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19570    attributes to the DIE for a block STMT, to describe where the inlined
19571    function was called from.  This is similar to add_src_coords_attributes.  */
19572
19573 static inline void
19574 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19575 {
19576   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19577
19578   if (dwarf_version >= 3 || !dwarf_strict)
19579     {
19580       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19581       add_AT_unsigned (die, DW_AT_call_line, s.line);
19582     }
19583 }
19584
19585
19586 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19587    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19588
19589 static inline void
19590 add_high_low_attributes (tree stmt, dw_die_ref die)
19591 {
19592   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19593
19594   if (BLOCK_FRAGMENT_CHAIN (stmt)
19595       && (dwarf_version >= 3 || !dwarf_strict))
19596     {
19597       tree chain;
19598
19599       if (inlined_function_outer_scope_p (stmt))
19600         {
19601           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19602                                        BLOCK_NUMBER (stmt));
19603           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19604         }
19605
19606       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19607
19608       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19609       do
19610         {
19611           add_ranges (chain);
19612           chain = BLOCK_FRAGMENT_CHAIN (chain);
19613         }
19614       while (chain);
19615       add_ranges (NULL);
19616     }
19617   else
19618     {
19619       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19620                                    BLOCK_NUMBER (stmt));
19621       add_AT_lbl_id (die, DW_AT_low_pc, label);
19622       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19623                                    BLOCK_NUMBER (stmt));
19624       add_AT_lbl_id (die, DW_AT_high_pc, label);
19625     }
19626 }
19627
19628 /* Generate a DIE for a lexical block.  */
19629
19630 static void
19631 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19632 {
19633   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19634
19635   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19636     add_high_low_attributes (stmt, stmt_die);
19637
19638   decls_for_scope (stmt, stmt_die, depth);
19639 }
19640
19641 /* Generate a DIE for an inlined subprogram.  */
19642
19643 static void
19644 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19645 {
19646   tree decl;
19647
19648   /* The instance of function that is effectively being inlined shall not
19649      be abstract.  */
19650   gcc_assert (! BLOCK_ABSTRACT (stmt));
19651
19652   decl = block_ultimate_origin (stmt);
19653
19654   /* Emit info for the abstract instance first, if we haven't yet.  We
19655      must emit this even if the block is abstract, otherwise when we
19656      emit the block below (or elsewhere), we may end up trying to emit
19657      a die whose origin die hasn't been emitted, and crashing.  */
19658   dwarf2out_abstract_function (decl);
19659
19660   if (! BLOCK_ABSTRACT (stmt))
19661     {
19662       dw_die_ref subr_die
19663         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19664
19665       add_abstract_origin_attribute (subr_die, decl);
19666       if (TREE_ASM_WRITTEN (stmt))
19667         add_high_low_attributes (stmt, subr_die);
19668       add_call_src_coords_attributes (stmt, subr_die);
19669
19670       decls_for_scope (stmt, subr_die, depth);
19671       current_function_has_inlines = 1;
19672     }
19673 }
19674
19675 /* Generate a DIE for a field in a record, or structure.  */
19676
19677 static void
19678 gen_field_die (tree decl, dw_die_ref context_die)
19679 {
19680   dw_die_ref decl_die;
19681
19682   if (TREE_TYPE (decl) == error_mark_node)
19683     return;
19684
19685   decl_die = new_die (DW_TAG_member, context_die, decl);
19686   add_name_and_src_coords_attributes (decl_die, decl);
19687   add_type_attribute (decl_die, member_declared_type (decl),
19688                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19689                       context_die);
19690
19691   if (DECL_BIT_FIELD_TYPE (decl))
19692     {
19693       add_byte_size_attribute (decl_die, decl);
19694       add_bit_size_attribute (decl_die, decl);
19695       add_bit_offset_attribute (decl_die, decl);
19696     }
19697
19698   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19699     add_data_member_location_attribute (decl_die, decl);
19700
19701   if (DECL_ARTIFICIAL (decl))
19702     add_AT_flag (decl_die, DW_AT_artificial, 1);
19703
19704   add_accessibility_attribute (decl_die, decl);
19705
19706   /* Equate decl number to die, so that we can look up this decl later on.  */
19707   equate_decl_number_to_die (decl, decl_die);
19708 }
19709
19710 #if 0
19711 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19712    Use modified_type_die instead.
19713    We keep this code here just in case these types of DIEs may be needed to
19714    represent certain things in other languages (e.g. Pascal) someday.  */
19715
19716 static void
19717 gen_pointer_type_die (tree type, dw_die_ref context_die)
19718 {
19719   dw_die_ref ptr_die
19720     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19721
19722   equate_type_number_to_die (type, ptr_die);
19723   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19724   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19725 }
19726
19727 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19728    Use modified_type_die instead.
19729    We keep this code here just in case these types of DIEs may be needed to
19730    represent certain things in other languages (e.g. Pascal) someday.  */
19731
19732 static void
19733 gen_reference_type_die (tree type, dw_die_ref context_die)
19734 {
19735   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19736
19737   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19738     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19739   else
19740     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19741
19742   equate_type_number_to_die (type, ref_die);
19743   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19744   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19745 }
19746 #endif
19747
19748 /* Generate a DIE for a pointer to a member type.  */
19749
19750 static void
19751 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19752 {
19753   dw_die_ref ptr_die
19754     = new_die (DW_TAG_ptr_to_member_type,
19755                scope_die_for (type, context_die), type);
19756
19757   equate_type_number_to_die (type, ptr_die);
19758   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19759                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19760   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19761 }
19762
19763 /* Generate the DIE for the compilation unit.  */
19764
19765 static dw_die_ref
19766 gen_compile_unit_die (const char *filename)
19767 {
19768   dw_die_ref die;
19769   char producer[250];
19770   const char *language_string = lang_hooks.name;
19771   int language;
19772
19773   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19774
19775   if (filename)
19776     {
19777       add_name_attribute (die, filename);
19778       /* Don't add cwd for <built-in>.  */
19779       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19780         add_comp_dir_attribute (die);
19781     }
19782
19783   sprintf (producer, "%s %s", language_string, version_string);
19784
19785 #ifdef MIPS_DEBUGGING_INFO
19786   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19787      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19788      not appear in the producer string, the debugger reaches the conclusion
19789      that the object file is stripped and has no debugging information.
19790      To get the MIPS/SGI debugger to believe that there is debugging
19791      information in the object file, we add a -g to the producer string.  */
19792   if (debug_info_level > DINFO_LEVEL_TERSE)
19793     strcat (producer, " -g");
19794 #endif
19795
19796   add_AT_string (die, DW_AT_producer, producer);
19797
19798   /* If our producer is LTO try to figure out a common language to use
19799      from the global list of translation units.  */
19800   if (strcmp (language_string, "GNU GIMPLE") == 0)
19801     {
19802       unsigned i;
19803       tree t;
19804       const char *common_lang = NULL;
19805
19806       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
19807         {
19808           if (!TRANSLATION_UNIT_LANGUAGE (t))
19809             continue;
19810           if (!common_lang)
19811             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19812           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19813             ;
19814           else if (strncmp (common_lang, "GNU C", 5) == 0
19815                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19816             /* Mixing C and C++ is ok, use C++ in that case.  */
19817             common_lang = "GNU C++";
19818           else
19819             {
19820               /* Fall back to C.  */
19821               common_lang = NULL;
19822               break;
19823             }
19824         }
19825
19826       if (common_lang)
19827         language_string = common_lang;
19828     }
19829
19830   language = DW_LANG_C89;
19831   if (strcmp (language_string, "GNU C++") == 0)
19832     language = DW_LANG_C_plus_plus;
19833   else if (strcmp (language_string, "GNU F77") == 0)
19834     language = DW_LANG_Fortran77;
19835   else if (strcmp (language_string, "GNU Pascal") == 0)
19836     language = DW_LANG_Pascal83;
19837   else if (dwarf_version >= 3 || !dwarf_strict)
19838     {
19839       if (strcmp (language_string, "GNU Ada") == 0)
19840         language = DW_LANG_Ada95;
19841       else if (strcmp (language_string, "GNU Fortran") == 0)
19842         language = DW_LANG_Fortran95;
19843       else if (strcmp (language_string, "GNU Java") == 0)
19844         language = DW_LANG_Java;
19845       else if (strcmp (language_string, "GNU Objective-C") == 0)
19846         language = DW_LANG_ObjC;
19847       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19848         language = DW_LANG_ObjC_plus_plus;
19849     }
19850
19851   add_AT_unsigned (die, DW_AT_language, language);
19852
19853   switch (language)
19854     {
19855     case DW_LANG_Fortran77:
19856     case DW_LANG_Fortran90:
19857     case DW_LANG_Fortran95:
19858       /* Fortran has case insensitive identifiers and the front-end
19859          lowercases everything.  */
19860       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19861       break;
19862     default:
19863       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19864       break;
19865     }
19866   return die;
19867 }
19868
19869 /* Generate the DIE for a base class.  */
19870
19871 static void
19872 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19873 {
19874   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19875
19876   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19877   add_data_member_location_attribute (die, binfo);
19878
19879   if (BINFO_VIRTUAL_P (binfo))
19880     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19881
19882   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19883      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19884      the default has always been DW_ACCESS_private.  */
19885   if (access == access_public_node)
19886     {
19887       if (dwarf_version == 2
19888           || context_die->die_tag == DW_TAG_class_type)
19889       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19890     }
19891   else if (access == access_protected_node)
19892     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19893   else if (dwarf_version > 2
19894            && context_die->die_tag != DW_TAG_class_type)
19895     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19896 }
19897
19898 /* Generate a DIE for a class member.  */
19899
19900 static void
19901 gen_member_die (tree type, dw_die_ref context_die)
19902 {
19903   tree member;
19904   tree binfo = TYPE_BINFO (type);
19905   dw_die_ref child;
19906
19907   /* If this is not an incomplete type, output descriptions of each of its
19908      members. Note that as we output the DIEs necessary to represent the
19909      members of this record or union type, we will also be trying to output
19910      DIEs to represent the *types* of those members. However the `type'
19911      function (above) will specifically avoid generating type DIEs for member
19912      types *within* the list of member DIEs for this (containing) type except
19913      for those types (of members) which are explicitly marked as also being
19914      members of this (containing) type themselves.  The g++ front- end can
19915      force any given type to be treated as a member of some other (containing)
19916      type by setting the TYPE_CONTEXT of the given (member) type to point to
19917      the TREE node representing the appropriate (containing) type.  */
19918
19919   /* First output info about the base classes.  */
19920   if (binfo)
19921     {
19922       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19923       int i;
19924       tree base;
19925
19926       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19927         gen_inheritance_die (base,
19928                              (accesses ? VEC_index (tree, accesses, i)
19929                               : access_public_node), context_die);
19930     }
19931
19932   /* Now output info about the data members and type members.  */
19933   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19934     {
19935       /* If we thought we were generating minimal debug info for TYPE
19936          and then changed our minds, some of the member declarations
19937          may have already been defined.  Don't define them again, but
19938          do put them in the right order.  */
19939
19940       child = lookup_decl_die (member);
19941       if (child)
19942         splice_child_die (context_die, child);
19943       else
19944         gen_decl_die (member, NULL, context_die);
19945     }
19946
19947   /* Now output info about the function members (if any).  */
19948   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19949     {
19950       /* Don't include clones in the member list.  */
19951       if (DECL_ABSTRACT_ORIGIN (member))
19952         continue;
19953
19954       child = lookup_decl_die (member);
19955       if (child)
19956         splice_child_die (context_die, child);
19957       else
19958         gen_decl_die (member, NULL, context_die);
19959     }
19960 }
19961
19962 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19963    is set, we pretend that the type was never defined, so we only get the
19964    member DIEs needed by later specification DIEs.  */
19965
19966 static void
19967 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19968                                 enum debug_info_usage usage)
19969 {
19970   dw_die_ref type_die = lookup_type_die (type);
19971   dw_die_ref scope_die = 0;
19972   int nested = 0;
19973   int complete = (TYPE_SIZE (type)
19974                   && (! TYPE_STUB_DECL (type)
19975                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19976   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19977   complete = complete && should_emit_struct_debug (type, usage);
19978
19979   if (type_die && ! complete)
19980     return;
19981
19982   if (TYPE_CONTEXT (type) != NULL_TREE
19983       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19984           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19985     nested = 1;
19986
19987   scope_die = scope_die_for (type, context_die);
19988
19989   if (! type_die || (nested && is_cu_die (scope_die)))
19990     /* First occurrence of type or toplevel definition of nested class.  */
19991     {
19992       dw_die_ref old_die = type_die;
19993
19994       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19995                           ? record_type_tag (type) : DW_TAG_union_type,
19996                           scope_die, type);
19997       equate_type_number_to_die (type, type_die);
19998       if (old_die)
19999         add_AT_specification (type_die, old_die);
20000       else
20001         add_name_attribute (type_die, type_tag (type));
20002     }
20003   else
20004     remove_AT (type_die, DW_AT_declaration);
20005
20006   /* Generate child dies for template paramaters.  */
20007   if (debug_info_level > DINFO_LEVEL_TERSE
20008       && COMPLETE_TYPE_P (type))
20009     gen_generic_params_dies (type);
20010
20011   /* If this type has been completed, then give it a byte_size attribute and
20012      then give a list of members.  */
20013   if (complete && !ns_decl)
20014     {
20015       /* Prevent infinite recursion in cases where the type of some member of
20016          this type is expressed in terms of this type itself.  */
20017       TREE_ASM_WRITTEN (type) = 1;
20018       add_byte_size_attribute (type_die, type);
20019       if (TYPE_STUB_DECL (type) != NULL_TREE)
20020         {
20021           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20022           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20023         }
20024
20025       /* If the first reference to this type was as the return type of an
20026          inline function, then it may not have a parent.  Fix this now.  */
20027       if (type_die->die_parent == NULL)
20028         add_child_die (scope_die, type_die);
20029
20030       push_decl_scope (type);
20031       gen_member_die (type, type_die);
20032       pop_decl_scope ();
20033
20034       /* GNU extension: Record what type our vtable lives in.  */
20035       if (TYPE_VFIELD (type))
20036         {
20037           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20038
20039           gen_type_die (vtype, context_die);
20040           add_AT_die_ref (type_die, DW_AT_containing_type,
20041                           lookup_type_die (vtype));
20042         }
20043     }
20044   else
20045     {
20046       add_AT_flag (type_die, DW_AT_declaration, 1);
20047
20048       /* We don't need to do this for function-local types.  */
20049       if (TYPE_STUB_DECL (type)
20050           && ! decl_function_context (TYPE_STUB_DECL (type)))
20051         VEC_safe_push (tree, gc, incomplete_types, type);
20052     }
20053
20054   if (get_AT (type_die, DW_AT_name))
20055     add_pubtype (type, type_die);
20056 }
20057
20058 /* Generate a DIE for a subroutine _type_.  */
20059
20060 static void
20061 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20062 {
20063   tree return_type = TREE_TYPE (type);
20064   dw_die_ref subr_die
20065     = new_die (DW_TAG_subroutine_type,
20066                scope_die_for (type, context_die), type);
20067
20068   equate_type_number_to_die (type, subr_die);
20069   add_prototyped_attribute (subr_die, type);
20070   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20071   gen_formal_types_die (type, subr_die);
20072
20073   if (get_AT (subr_die, DW_AT_name))
20074     add_pubtype (type, subr_die);
20075 }
20076
20077 /* Generate a DIE for a type definition.  */
20078
20079 static void
20080 gen_typedef_die (tree decl, dw_die_ref context_die)
20081 {
20082   dw_die_ref type_die;
20083   tree origin;
20084
20085   if (TREE_ASM_WRITTEN (decl))
20086     return;
20087
20088   TREE_ASM_WRITTEN (decl) = 1;
20089   type_die = new_die (DW_TAG_typedef, context_die, decl);
20090   origin = decl_ultimate_origin (decl);
20091   if (origin != NULL)
20092     add_abstract_origin_attribute (type_die, origin);
20093   else
20094     {
20095       tree type;
20096
20097       add_name_and_src_coords_attributes (type_die, decl);
20098       if (DECL_ORIGINAL_TYPE (decl))
20099         {
20100           type = DECL_ORIGINAL_TYPE (decl);
20101
20102           gcc_assert (type != TREE_TYPE (decl));
20103           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20104         }
20105       else
20106         {
20107           type = TREE_TYPE (decl);
20108
20109           if (is_naming_typedef_decl (TYPE_NAME (type)))
20110             {
20111               /* Here, we are in the case of decl being a typedef naming
20112                  an anonymous type, e.g:
20113                      typedef struct {...} foo;
20114                  In that case TREE_TYPE (decl) is not a typedef variant
20115                  type and TYPE_NAME of the anonymous type is set to the
20116                  TYPE_DECL of the typedef. This construct is emitted by
20117                  the C++ FE.
20118
20119                  TYPE is the anonymous struct named by the typedef
20120                  DECL. As we need the DW_AT_type attribute of the
20121                  DW_TAG_typedef to point to the DIE of TYPE, let's
20122                  generate that DIE right away. add_type_attribute
20123                  called below will then pick (via lookup_type_die) that
20124                  anonymous struct DIE.  */
20125               if (!TREE_ASM_WRITTEN (type))
20126                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20127             }
20128         }
20129
20130       add_type_attribute (type_die, type, TREE_READONLY (decl),
20131                           TREE_THIS_VOLATILE (decl), context_die);
20132
20133       if (is_naming_typedef_decl (decl))
20134         /* We want that all subsequent calls to lookup_type_die with
20135            TYPE in argument yield the DW_TAG_typedef we have just
20136            created.  */
20137         equate_type_number_to_die (type, type_die);
20138
20139       add_accessibility_attribute (type_die, decl);
20140     }
20141
20142   if (DECL_ABSTRACT (decl))
20143     equate_decl_number_to_die (decl, type_die);
20144
20145   if (get_AT (type_die, DW_AT_name))
20146     add_pubtype (decl, type_die);
20147 }
20148
20149 /* Generate a DIE for a struct, class, enum or union type.  */
20150
20151 static void
20152 gen_tagged_type_die (tree type,
20153                      dw_die_ref context_die,
20154                      enum debug_info_usage usage)
20155 {
20156   int need_pop;
20157
20158   if (type == NULL_TREE
20159       || !is_tagged_type (type))
20160     return;
20161
20162   /* If this is a nested type whose containing class hasn't been written
20163      out yet, writing it out will cover this one, too.  This does not apply
20164      to instantiations of member class templates; they need to be added to
20165      the containing class as they are generated.  FIXME: This hurts the
20166      idea of combining type decls from multiple TUs, since we can't predict
20167      what set of template instantiations we'll get.  */
20168   if (TYPE_CONTEXT (type)
20169       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20170       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20171     {
20172       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20173
20174       if (TREE_ASM_WRITTEN (type))
20175         return;
20176
20177       /* If that failed, attach ourselves to the stub.  */
20178       push_decl_scope (TYPE_CONTEXT (type));
20179       context_die = lookup_type_die (TYPE_CONTEXT (type));
20180       need_pop = 1;
20181     }
20182   else if (TYPE_CONTEXT (type) != NULL_TREE
20183            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20184     {
20185       /* If this type is local to a function that hasn't been written
20186          out yet, use a NULL context for now; it will be fixed up in
20187          decls_for_scope.  */
20188       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20189       need_pop = 0;
20190     }
20191   else
20192     {
20193       context_die = declare_in_namespace (type, context_die);
20194       need_pop = 0;
20195     }
20196
20197   if (TREE_CODE (type) == ENUMERAL_TYPE)
20198     {
20199       /* This might have been written out by the call to
20200          declare_in_namespace.  */
20201       if (!TREE_ASM_WRITTEN (type))
20202         gen_enumeration_type_die (type, context_die);
20203     }
20204   else
20205     gen_struct_or_union_type_die (type, context_die, usage);
20206
20207   if (need_pop)
20208     pop_decl_scope ();
20209
20210   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20211      it up if it is ever completed.  gen_*_type_die will set it for us
20212      when appropriate.  */
20213 }
20214
20215 /* Generate a type description DIE.  */
20216
20217 static void
20218 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20219                                 enum debug_info_usage usage)
20220 {
20221   struct array_descr_info info;
20222
20223   if (type == NULL_TREE || type == error_mark_node)
20224     return;
20225
20226   /* If TYPE is a typedef type variant, let's generate debug info
20227      for the parent typedef which TYPE is a type of.  */
20228   if (typedef_variant_p (type))
20229     {
20230       if (TREE_ASM_WRITTEN (type))
20231         return;
20232
20233       /* Prevent broken recursion; we can't hand off to the same type.  */
20234       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20235
20236       /* Use the DIE of the containing namespace as the parent DIE of
20237          the type description DIE we want to generate.  */
20238       if (DECL_CONTEXT (TYPE_NAME (type))
20239           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20240         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20241
20242       TREE_ASM_WRITTEN (type) = 1;
20243
20244       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20245       return;
20246     }
20247
20248   /* If type is an anonymous tagged type named by a typedef, let's
20249      generate debug info for the typedef.  */
20250   if (is_naming_typedef_decl (TYPE_NAME (type)))
20251     {
20252       /* Use the DIE of the containing namespace as the parent DIE of
20253          the type description DIE we want to generate.  */
20254       if (DECL_CONTEXT (TYPE_NAME (type))
20255           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20256         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20257       
20258       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20259       return;
20260     }
20261
20262   /* If this is an array type with hidden descriptor, handle it first.  */
20263   if (!TREE_ASM_WRITTEN (type)
20264       && lang_hooks.types.get_array_descr_info
20265       && lang_hooks.types.get_array_descr_info (type, &info)
20266       && (dwarf_version >= 3 || !dwarf_strict))
20267     {
20268       gen_descr_array_type_die (type, &info, context_die);
20269       TREE_ASM_WRITTEN (type) = 1;
20270       return;
20271     }
20272
20273   /* We are going to output a DIE to represent the unqualified version
20274      of this type (i.e. without any const or volatile qualifiers) so
20275      get the main variant (i.e. the unqualified version) of this type
20276      now.  (Vectors are special because the debugging info is in the
20277      cloned type itself).  */
20278   if (TREE_CODE (type) != VECTOR_TYPE)
20279     type = type_main_variant (type);
20280
20281   if (TREE_ASM_WRITTEN (type))
20282     return;
20283
20284   switch (TREE_CODE (type))
20285     {
20286     case ERROR_MARK:
20287       break;
20288
20289     case POINTER_TYPE:
20290     case REFERENCE_TYPE:
20291       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20292          ensures that the gen_type_die recursion will terminate even if the
20293          type is recursive.  Recursive types are possible in Ada.  */
20294       /* ??? We could perhaps do this for all types before the switch
20295          statement.  */
20296       TREE_ASM_WRITTEN (type) = 1;
20297
20298       /* For these types, all that is required is that we output a DIE (or a
20299          set of DIEs) to represent the "basis" type.  */
20300       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20301                                 DINFO_USAGE_IND_USE);
20302       break;
20303
20304     case OFFSET_TYPE:
20305       /* This code is used for C++ pointer-to-data-member types.
20306          Output a description of the relevant class type.  */
20307       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20308                                         DINFO_USAGE_IND_USE);
20309
20310       /* Output a description of the type of the object pointed to.  */
20311       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20312                                         DINFO_USAGE_IND_USE);
20313
20314       /* Now output a DIE to represent this pointer-to-data-member type
20315          itself.  */
20316       gen_ptr_to_mbr_type_die (type, context_die);
20317       break;
20318
20319     case FUNCTION_TYPE:
20320       /* Force out return type (in case it wasn't forced out already).  */
20321       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20322                                         DINFO_USAGE_DIR_USE);
20323       gen_subroutine_type_die (type, context_die);
20324       break;
20325
20326     case METHOD_TYPE:
20327       /* Force out return type (in case it wasn't forced out already).  */
20328       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20329                                         DINFO_USAGE_DIR_USE);
20330       gen_subroutine_type_die (type, context_die);
20331       break;
20332
20333     case ARRAY_TYPE:
20334       gen_array_type_die (type, context_die);
20335       break;
20336
20337     case VECTOR_TYPE:
20338       gen_array_type_die (type, context_die);
20339       break;
20340
20341     case ENUMERAL_TYPE:
20342     case RECORD_TYPE:
20343     case UNION_TYPE:
20344     case QUAL_UNION_TYPE:
20345       gen_tagged_type_die (type, context_die, usage);
20346       return;
20347
20348     case VOID_TYPE:
20349     case INTEGER_TYPE:
20350     case REAL_TYPE:
20351     case FIXED_POINT_TYPE:
20352     case COMPLEX_TYPE:
20353     case BOOLEAN_TYPE:
20354       /* No DIEs needed for fundamental types.  */
20355       break;
20356
20357     case NULLPTR_TYPE:
20358     case LANG_TYPE:
20359       /* Just use DW_TAG_unspecified_type.  */
20360       {
20361         dw_die_ref type_die = lookup_type_die (type);
20362         if (type_die == NULL)
20363           {
20364             tree name = TYPE_NAME (type);
20365             if (TREE_CODE (name) == TYPE_DECL)
20366               name = DECL_NAME (name);
20367             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20368             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20369             equate_type_number_to_die (type, type_die);
20370           }
20371       }
20372       break;
20373
20374     default:
20375       gcc_unreachable ();
20376     }
20377
20378   TREE_ASM_WRITTEN (type) = 1;
20379 }
20380
20381 static void
20382 gen_type_die (tree type, dw_die_ref context_die)
20383 {
20384   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20385 }
20386
20387 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20388    things which are local to the given block.  */
20389
20390 static void
20391 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20392 {
20393   int must_output_die = 0;
20394   bool inlined_func;
20395
20396   /* Ignore blocks that are NULL.  */
20397   if (stmt == NULL_TREE)
20398     return;
20399
20400   inlined_func = inlined_function_outer_scope_p (stmt);
20401
20402   /* If the block is one fragment of a non-contiguous block, do not
20403      process the variables, since they will have been done by the
20404      origin block.  Do process subblocks.  */
20405   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20406     {
20407       tree sub;
20408
20409       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20410         gen_block_die (sub, context_die, depth + 1);
20411
20412       return;
20413     }
20414
20415   /* Determine if we need to output any Dwarf DIEs at all to represent this
20416      block.  */
20417   if (inlined_func)
20418     /* The outer scopes for inlinings *must* always be represented.  We
20419        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20420     must_output_die = 1;
20421   else
20422     {
20423       /* Determine if this block directly contains any "significant"
20424          local declarations which we will need to output DIEs for.  */
20425       if (debug_info_level > DINFO_LEVEL_TERSE)
20426         /* We are not in terse mode so *any* local declaration counts
20427            as being a "significant" one.  */
20428         must_output_die = ((BLOCK_VARS (stmt) != NULL
20429                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20430                            && (TREE_USED (stmt)
20431                                || TREE_ASM_WRITTEN (stmt)
20432                                || BLOCK_ABSTRACT (stmt)));
20433       else if ((TREE_USED (stmt)
20434                 || TREE_ASM_WRITTEN (stmt)
20435                 || BLOCK_ABSTRACT (stmt))
20436                && !dwarf2out_ignore_block (stmt))
20437         must_output_die = 1;
20438     }
20439
20440   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20441      DIE for any block which contains no significant local declarations at
20442      all.  Rather, in such cases we just call `decls_for_scope' so that any
20443      needed Dwarf info for any sub-blocks will get properly generated. Note
20444      that in terse mode, our definition of what constitutes a "significant"
20445      local declaration gets restricted to include only inlined function
20446      instances and local (nested) function definitions.  */
20447   if (must_output_die)
20448     {
20449       if (inlined_func)
20450         {
20451           /* If STMT block is abstract, that means we have been called
20452              indirectly from dwarf2out_abstract_function.
20453              That function rightfully marks the descendent blocks (of
20454              the abstract function it is dealing with) as being abstract,
20455              precisely to prevent us from emitting any
20456              DW_TAG_inlined_subroutine DIE as a descendent
20457              of an abstract function instance. So in that case, we should
20458              not call gen_inlined_subroutine_die.
20459
20460              Later though, when cgraph asks dwarf2out to emit info
20461              for the concrete instance of the function decl into which
20462              the concrete instance of STMT got inlined, the later will lead
20463              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20464           if (! BLOCK_ABSTRACT (stmt))
20465             gen_inlined_subroutine_die (stmt, context_die, depth);
20466         }
20467       else
20468         gen_lexical_block_die (stmt, context_die, depth);
20469     }
20470   else
20471     decls_for_scope (stmt, context_die, depth);
20472 }
20473
20474 /* Process variable DECL (or variable with origin ORIGIN) within
20475    block STMT and add it to CONTEXT_DIE.  */
20476 static void
20477 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20478 {
20479   dw_die_ref die;
20480   tree decl_or_origin = decl ? decl : origin;
20481
20482   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20483     die = lookup_decl_die (decl_or_origin);
20484   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20485            && TYPE_DECL_IS_STUB (decl_or_origin))
20486     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20487   else
20488     die = NULL;
20489
20490   if (die != NULL && die->die_parent == NULL)
20491     add_child_die (context_die, die);
20492   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20493     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20494                                          stmt, context_die);
20495   else
20496     gen_decl_die (decl, origin, context_die);
20497 }
20498
20499 /* Generate all of the decls declared within a given scope and (recursively)
20500    all of its sub-blocks.  */
20501
20502 static void
20503 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20504 {
20505   tree decl;
20506   unsigned int i;
20507   tree subblocks;
20508
20509   /* Ignore NULL blocks.  */
20510   if (stmt == NULL_TREE)
20511     return;
20512
20513   /* Output the DIEs to represent all of the data objects and typedefs
20514      declared directly within this block but not within any nested
20515      sub-blocks.  Also, nested function and tag DIEs have been
20516      generated with a parent of NULL; fix that up now.  */
20517   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20518     process_scope_var (stmt, decl, NULL_TREE, context_die);
20519   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20520     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20521                        context_die);
20522
20523   /* If we're at -g1, we're not interested in subblocks.  */
20524   if (debug_info_level <= DINFO_LEVEL_TERSE)
20525     return;
20526
20527   /* Output the DIEs to represent all sub-blocks (and the items declared
20528      therein) of this block.  */
20529   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20530        subblocks != NULL;
20531        subblocks = BLOCK_CHAIN (subblocks))
20532     gen_block_die (subblocks, context_die, depth + 1);
20533 }
20534
20535 /* Is this a typedef we can avoid emitting?  */
20536
20537 static inline int
20538 is_redundant_typedef (const_tree decl)
20539 {
20540   if (TYPE_DECL_IS_STUB (decl))
20541     return 1;
20542
20543   if (DECL_ARTIFICIAL (decl)
20544       && DECL_CONTEXT (decl)
20545       && is_tagged_type (DECL_CONTEXT (decl))
20546       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20547       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20548     /* Also ignore the artificial member typedef for the class name.  */
20549     return 1;
20550
20551   return 0;
20552 }
20553
20554 /* Return TRUE if TYPE is a typedef that names a type for linkage
20555    purposes. This kind of typedefs is produced by the C++ FE for
20556    constructs like:
20557
20558    typedef struct {...} foo;
20559
20560    In that case, there is no typedef variant type produced for foo.
20561    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20562    struct type.  */
20563
20564 static bool
20565 is_naming_typedef_decl (const_tree decl)
20566 {
20567   if (decl == NULL_TREE
20568       || TREE_CODE (decl) != TYPE_DECL
20569       || !is_tagged_type (TREE_TYPE (decl))
20570       || DECL_IS_BUILTIN (decl)
20571       || is_redundant_typedef (decl)
20572       /* It looks like Ada produces TYPE_DECLs that are very similar
20573          to C++ naming typedefs but that have different
20574          semantics. Let's be specific to c++ for now.  */
20575       || !is_cxx ())
20576     return FALSE;
20577
20578   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20579           && TYPE_NAME (TREE_TYPE (decl)) == decl
20580           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20581               != TYPE_NAME (TREE_TYPE (decl))));
20582 }
20583
20584 /* Returns the DIE for a context.  */
20585
20586 static inline dw_die_ref
20587 get_context_die (tree context)
20588 {
20589   if (context)
20590     {
20591       /* Find die that represents this context.  */
20592       if (TYPE_P (context))
20593         return force_type_die (TYPE_MAIN_VARIANT (context));
20594       else
20595         return force_decl_die (context);
20596     }
20597   return comp_unit_die ();
20598 }
20599
20600 /* Returns the DIE for decl.  A DIE will always be returned.  */
20601
20602 static dw_die_ref
20603 force_decl_die (tree decl)
20604 {
20605   dw_die_ref decl_die;
20606   unsigned saved_external_flag;
20607   tree save_fn = NULL_TREE;
20608   decl_die = lookup_decl_die (decl);
20609   if (!decl_die)
20610     {
20611       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20612
20613       decl_die = lookup_decl_die (decl);
20614       if (decl_die)
20615         return decl_die;
20616
20617       switch (TREE_CODE (decl))
20618         {
20619         case FUNCTION_DECL:
20620           /* Clear current_function_decl, so that gen_subprogram_die thinks
20621              that this is a declaration. At this point, we just want to force
20622              declaration die.  */
20623           save_fn = current_function_decl;
20624           current_function_decl = NULL_TREE;
20625           gen_subprogram_die (decl, context_die);
20626           current_function_decl = save_fn;
20627           break;
20628
20629         case VAR_DECL:
20630           /* Set external flag to force declaration die. Restore it after
20631            gen_decl_die() call.  */
20632           saved_external_flag = DECL_EXTERNAL (decl);
20633           DECL_EXTERNAL (decl) = 1;
20634           gen_decl_die (decl, NULL, context_die);
20635           DECL_EXTERNAL (decl) = saved_external_flag;
20636           break;
20637
20638         case NAMESPACE_DECL:
20639           if (dwarf_version >= 3 || !dwarf_strict)
20640             dwarf2out_decl (decl);
20641           else
20642             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20643             decl_die = comp_unit_die ();
20644           break;
20645
20646         case TRANSLATION_UNIT_DECL:
20647           decl_die = comp_unit_die ();
20648           break;
20649
20650         default:
20651           gcc_unreachable ();
20652         }
20653
20654       /* We should be able to find the DIE now.  */
20655       if (!decl_die)
20656         decl_die = lookup_decl_die (decl);
20657       gcc_assert (decl_die);
20658     }
20659
20660   return decl_die;
20661 }
20662
20663 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20664    always returned.  */
20665
20666 static dw_die_ref
20667 force_type_die (tree type)
20668 {
20669   dw_die_ref type_die;
20670
20671   type_die = lookup_type_die (type);
20672   if (!type_die)
20673     {
20674       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20675
20676       type_die = modified_type_die (type, TYPE_READONLY (type),
20677                                     TYPE_VOLATILE (type), context_die);
20678       gcc_assert (type_die);
20679     }
20680   return type_die;
20681 }
20682
20683 /* Force out any required namespaces to be able to output DECL,
20684    and return the new context_die for it, if it's changed.  */
20685
20686 static dw_die_ref
20687 setup_namespace_context (tree thing, dw_die_ref context_die)
20688 {
20689   tree context = (DECL_P (thing)
20690                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20691   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20692     /* Force out the namespace.  */
20693     context_die = force_decl_die (context);
20694
20695   return context_die;
20696 }
20697
20698 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20699    type) within its namespace, if appropriate.
20700
20701    For compatibility with older debuggers, namespace DIEs only contain
20702    declarations; all definitions are emitted at CU scope.  */
20703
20704 static dw_die_ref
20705 declare_in_namespace (tree thing, dw_die_ref context_die)
20706 {
20707   dw_die_ref ns_context;
20708
20709   if (debug_info_level <= DINFO_LEVEL_TERSE)
20710     return context_die;
20711
20712   /* If this decl is from an inlined function, then don't try to emit it in its
20713      namespace, as we will get confused.  It would have already been emitted
20714      when the abstract instance of the inline function was emitted anyways.  */
20715   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20716     return context_die;
20717
20718   ns_context = setup_namespace_context (thing, context_die);
20719
20720   if (ns_context != context_die)
20721     {
20722       if (is_fortran ())
20723         return ns_context;
20724       if (DECL_P (thing))
20725         gen_decl_die (thing, NULL, ns_context);
20726       else
20727         gen_type_die (thing, ns_context);
20728     }
20729   return context_die;
20730 }
20731
20732 /* Generate a DIE for a namespace or namespace alias.  */
20733
20734 static void
20735 gen_namespace_die (tree decl, dw_die_ref context_die)
20736 {
20737   dw_die_ref namespace_die;
20738
20739   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20740      they are an alias of.  */
20741   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20742     {
20743       /* Output a real namespace or module.  */
20744       context_die = setup_namespace_context (decl, comp_unit_die ());
20745       namespace_die = new_die (is_fortran ()
20746                                ? DW_TAG_module : DW_TAG_namespace,
20747                                context_die, decl);
20748       /* For Fortran modules defined in different CU don't add src coords.  */
20749       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20750         {
20751           const char *name = dwarf2_name (decl, 0);
20752           if (name)
20753             add_name_attribute (namespace_die, name);
20754         }
20755       else
20756         add_name_and_src_coords_attributes (namespace_die, decl);
20757       if (DECL_EXTERNAL (decl))
20758         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20759       equate_decl_number_to_die (decl, namespace_die);
20760     }
20761   else
20762     {
20763       /* Output a namespace alias.  */
20764
20765       /* Force out the namespace we are an alias of, if necessary.  */
20766       dw_die_ref origin_die
20767         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20768
20769       if (DECL_FILE_SCOPE_P (decl)
20770           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20771         context_die = setup_namespace_context (decl, comp_unit_die ());
20772       /* Now create the namespace alias DIE.  */
20773       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20774       add_name_and_src_coords_attributes (namespace_die, decl);
20775       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20776       equate_decl_number_to_die (decl, namespace_die);
20777     }
20778 }
20779
20780 /* Generate Dwarf debug information for a decl described by DECL.
20781    The return value is currently only meaningful for PARM_DECLs,
20782    for all other decls it returns NULL.  */
20783
20784 static dw_die_ref
20785 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20786 {
20787   tree decl_or_origin = decl ? decl : origin;
20788   tree class_origin = NULL, ultimate_origin;
20789
20790   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20791     return NULL;
20792
20793   switch (TREE_CODE (decl_or_origin))
20794     {
20795     case ERROR_MARK:
20796       break;
20797
20798     case CONST_DECL:
20799       if (!is_fortran () && !is_ada ())
20800         {
20801           /* The individual enumerators of an enum type get output when we output
20802              the Dwarf representation of the relevant enum type itself.  */
20803           break;
20804         }
20805
20806       /* Emit its type.  */
20807       gen_type_die (TREE_TYPE (decl), context_die);
20808
20809       /* And its containing namespace.  */
20810       context_die = declare_in_namespace (decl, context_die);
20811
20812       gen_const_die (decl, context_die);
20813       break;
20814
20815     case FUNCTION_DECL:
20816       /* Don't output any DIEs to represent mere function declarations,
20817          unless they are class members or explicit block externs.  */
20818       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20819           && DECL_FILE_SCOPE_P (decl_or_origin)
20820           && (current_function_decl == NULL_TREE
20821               || DECL_ARTIFICIAL (decl_or_origin)))
20822         break;
20823
20824 #if 0
20825       /* FIXME */
20826       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20827          on local redeclarations of global functions.  That seems broken.  */
20828       if (current_function_decl != decl)
20829         /* This is only a declaration.  */;
20830 #endif
20831
20832       /* If we're emitting a clone, emit info for the abstract instance.  */
20833       if (origin || DECL_ORIGIN (decl) != decl)
20834         dwarf2out_abstract_function (origin
20835                                      ? DECL_ORIGIN (origin)
20836                                      : DECL_ABSTRACT_ORIGIN (decl));
20837
20838       /* If we're emitting an out-of-line copy of an inline function,
20839          emit info for the abstract instance and set up to refer to it.  */
20840       else if (cgraph_function_possibly_inlined_p (decl)
20841                && ! DECL_ABSTRACT (decl)
20842                && ! class_or_namespace_scope_p (context_die)
20843                /* dwarf2out_abstract_function won't emit a die if this is just
20844                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20845                   that case, because that works only if we have a die.  */
20846                && DECL_INITIAL (decl) != NULL_TREE)
20847         {
20848           dwarf2out_abstract_function (decl);
20849           set_decl_origin_self (decl);
20850         }
20851
20852       /* Otherwise we're emitting the primary DIE for this decl.  */
20853       else if (debug_info_level > DINFO_LEVEL_TERSE)
20854         {
20855           /* Before we describe the FUNCTION_DECL itself, make sure that we
20856              have its containing type.  */
20857           if (!origin)
20858             origin = decl_class_context (decl);
20859           if (origin != NULL_TREE)
20860             gen_type_die (origin, context_die);
20861
20862           /* And its return type.  */
20863           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20864
20865           /* And its virtual context.  */
20866           if (DECL_VINDEX (decl) != NULL_TREE)
20867             gen_type_die (DECL_CONTEXT (decl), context_die);
20868
20869           /* Make sure we have a member DIE for decl.  */
20870           if (origin != NULL_TREE)
20871             gen_type_die_for_member (origin, decl, context_die);
20872
20873           /* And its containing namespace.  */
20874           context_die = declare_in_namespace (decl, context_die);
20875         }
20876
20877       /* Now output a DIE to represent the function itself.  */
20878       if (decl)
20879         gen_subprogram_die (decl, context_die);
20880       break;
20881
20882     case TYPE_DECL:
20883       /* If we are in terse mode, don't generate any DIEs to represent any
20884          actual typedefs.  */
20885       if (debug_info_level <= DINFO_LEVEL_TERSE)
20886         break;
20887
20888       /* In the special case of a TYPE_DECL node representing the declaration
20889          of some type tag, if the given TYPE_DECL is marked as having been
20890          instantiated from some other (original) TYPE_DECL node (e.g. one which
20891          was generated within the original definition of an inline function) we
20892          used to generate a special (abbreviated) DW_TAG_structure_type,
20893          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20894          should be actually referencing those DIEs, as variable DIEs with that
20895          type would be emitted already in the abstract origin, so it was always
20896          removed during unused type prunning.  Don't add anything in this
20897          case.  */
20898       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20899         break;
20900
20901       if (is_redundant_typedef (decl))
20902         gen_type_die (TREE_TYPE (decl), context_die);
20903       else
20904         /* Output a DIE to represent the typedef itself.  */
20905         gen_typedef_die (decl, context_die);
20906       break;
20907
20908     case LABEL_DECL:
20909       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20910         gen_label_die (decl, context_die);
20911       break;
20912
20913     case VAR_DECL:
20914     case RESULT_DECL:
20915       /* If we are in terse mode, don't generate any DIEs to represent any
20916          variable declarations or definitions.  */
20917       if (debug_info_level <= DINFO_LEVEL_TERSE)
20918         break;
20919
20920       /* Output any DIEs that are needed to specify the type of this data
20921          object.  */
20922       if (decl_by_reference_p (decl_or_origin))
20923         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20924       else
20925         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20926
20927       /* And its containing type.  */
20928       class_origin = decl_class_context (decl_or_origin);
20929       if (class_origin != NULL_TREE)
20930         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20931
20932       /* And its containing namespace.  */
20933       context_die = declare_in_namespace (decl_or_origin, context_die);
20934
20935       /* Now output the DIE to represent the data object itself.  This gets
20936          complicated because of the possibility that the VAR_DECL really
20937          represents an inlined instance of a formal parameter for an inline
20938          function.  */
20939       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20940       if (ultimate_origin != NULL_TREE
20941           && TREE_CODE (ultimate_origin) == PARM_DECL)
20942         gen_formal_parameter_die (decl, origin,
20943                                   true /* Emit name attribute.  */,
20944                                   context_die);
20945       else
20946         gen_variable_die (decl, origin, context_die);
20947       break;
20948
20949     case FIELD_DECL:
20950       /* Ignore the nameless fields that are used to skip bits but handle C++
20951          anonymous unions and structs.  */
20952       if (DECL_NAME (decl) != NULL_TREE
20953           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20954           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20955         {
20956           gen_type_die (member_declared_type (decl), context_die);
20957           gen_field_die (decl, context_die);
20958         }
20959       break;
20960
20961     case PARM_DECL:
20962       if (DECL_BY_REFERENCE (decl_or_origin))
20963         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20964       else
20965         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20966       return gen_formal_parameter_die (decl, origin,
20967                                        true /* Emit name attribute.  */,
20968                                        context_die);
20969
20970     case NAMESPACE_DECL:
20971     case IMPORTED_DECL:
20972       if (dwarf_version >= 3 || !dwarf_strict)
20973         gen_namespace_die (decl, context_die);
20974       break;
20975
20976     default:
20977       /* Probably some frontend-internal decl.  Assume we don't care.  */
20978       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20979       break;
20980     }
20981
20982   return NULL;
20983 }
20984 \f
20985 /* Output debug information for global decl DECL.  Called from toplev.c after
20986    compilation proper has finished.  */
20987
20988 static void
20989 dwarf2out_global_decl (tree decl)
20990 {
20991   /* Output DWARF2 information for file-scope tentative data object
20992      declarations, file-scope (extern) function declarations (which
20993      had no corresponding body) and file-scope tagged type declarations
20994      and definitions which have not yet been forced out.  */
20995   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20996     dwarf2out_decl (decl);
20997 }
20998
20999 /* Output debug information for type decl DECL.  Called from toplev.c
21000    and from language front ends (to record built-in types).  */
21001 static void
21002 dwarf2out_type_decl (tree decl, int local)
21003 {
21004   if (!local)
21005     dwarf2out_decl (decl);
21006 }
21007
21008 /* Output debug information for imported module or decl DECL.
21009    NAME is non-NULL name in the lexical block if the decl has been renamed.
21010    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21011    that DECL belongs to.
21012    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21013 static void
21014 dwarf2out_imported_module_or_decl_1 (tree decl,
21015                                      tree name,
21016                                      tree lexical_block,
21017                                      dw_die_ref lexical_block_die)
21018 {
21019   expanded_location xloc;
21020   dw_die_ref imported_die = NULL;
21021   dw_die_ref at_import_die;
21022
21023   if (TREE_CODE (decl) == IMPORTED_DECL)
21024     {
21025       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21026       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21027       gcc_assert (decl);
21028     }
21029   else
21030     xloc = expand_location (input_location);
21031
21032   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21033     {
21034       at_import_die = force_type_die (TREE_TYPE (decl));
21035       /* For namespace N { typedef void T; } using N::T; base_type_die
21036          returns NULL, but DW_TAG_imported_declaration requires
21037          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21038       if (!at_import_die)
21039         {
21040           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21041           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21042           at_import_die = lookup_type_die (TREE_TYPE (decl));
21043           gcc_assert (at_import_die);
21044         }
21045     }
21046   else
21047     {
21048       at_import_die = lookup_decl_die (decl);
21049       if (!at_import_die)
21050         {
21051           /* If we're trying to avoid duplicate debug info, we may not have
21052              emitted the member decl for this field.  Emit it now.  */
21053           if (TREE_CODE (decl) == FIELD_DECL)
21054             {
21055               tree type = DECL_CONTEXT (decl);
21056
21057               if (TYPE_CONTEXT (type)
21058                   && TYPE_P (TYPE_CONTEXT (type))
21059                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21060                                                 DINFO_USAGE_DIR_USE))
21061                 return;
21062               gen_type_die_for_member (type, decl,
21063                                        get_context_die (TYPE_CONTEXT (type)));
21064             }
21065           at_import_die = force_decl_die (decl);
21066         }
21067     }
21068
21069   if (TREE_CODE (decl) == NAMESPACE_DECL)
21070     {
21071       if (dwarf_version >= 3 || !dwarf_strict)
21072         imported_die = new_die (DW_TAG_imported_module,
21073                                 lexical_block_die,
21074                                 lexical_block);
21075       else
21076         return;
21077     }
21078   else
21079     imported_die = new_die (DW_TAG_imported_declaration,
21080                             lexical_block_die,
21081                             lexical_block);
21082
21083   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21084   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21085   if (name)
21086     add_AT_string (imported_die, DW_AT_name,
21087                    IDENTIFIER_POINTER (name));
21088   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21089 }
21090
21091 /* Output debug information for imported module or decl DECL.
21092    NAME is non-NULL name in context if the decl has been renamed.
21093    CHILD is true if decl is one of the renamed decls as part of
21094    importing whole module.  */
21095
21096 static void
21097 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21098                                    bool child)
21099 {
21100   /* dw_die_ref at_import_die;  */
21101   dw_die_ref scope_die;
21102
21103   if (debug_info_level <= DINFO_LEVEL_TERSE)
21104     return;
21105
21106   gcc_assert (decl);
21107
21108   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21109      We need decl DIE for reference and scope die. First, get DIE for the decl
21110      itself.  */
21111
21112   /* Get the scope die for decl context. Use comp_unit_die for global module
21113      or decl. If die is not found for non globals, force new die.  */
21114   if (context
21115       && TYPE_P (context)
21116       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21117     return;
21118
21119   if (!(dwarf_version >= 3 || !dwarf_strict))
21120     return;
21121
21122   scope_die = get_context_die (context);
21123
21124   if (child)
21125     {
21126       gcc_assert (scope_die->die_child);
21127       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21128       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21129       scope_die = scope_die->die_child;
21130     }
21131
21132   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21133   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21134
21135 }
21136
21137 /* Write the debugging output for DECL.  */
21138
21139 void
21140 dwarf2out_decl (tree decl)
21141 {
21142   dw_die_ref context_die = comp_unit_die ();
21143
21144   switch (TREE_CODE (decl))
21145     {
21146     case ERROR_MARK:
21147       return;
21148
21149     case FUNCTION_DECL:
21150       /* What we would really like to do here is to filter out all mere
21151          file-scope declarations of file-scope functions which are never
21152          referenced later within this translation unit (and keep all of ones
21153          that *are* referenced later on) but we aren't clairvoyant, so we have
21154          no idea which functions will be referenced in the future (i.e. later
21155          on within the current translation unit). So here we just ignore all
21156          file-scope function declarations which are not also definitions.  If
21157          and when the debugger needs to know something about these functions,
21158          it will have to hunt around and find the DWARF information associated
21159          with the definition of the function.
21160
21161          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21162          nodes represent definitions and which ones represent mere
21163          declarations.  We have to check DECL_INITIAL instead. That's because
21164          the C front-end supports some weird semantics for "extern inline"
21165          function definitions.  These can get inlined within the current
21166          translation unit (and thus, we need to generate Dwarf info for their
21167          abstract instances so that the Dwarf info for the concrete inlined
21168          instances can have something to refer to) but the compiler never
21169          generates any out-of-lines instances of such things (despite the fact
21170          that they *are* definitions).
21171
21172          The important point is that the C front-end marks these "extern
21173          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21174          them anyway. Note that the C++ front-end also plays some similar games
21175          for inline function definitions appearing within include files which
21176          also contain `#pragma interface' pragmas.  */
21177       if (DECL_INITIAL (decl) == NULL_TREE)
21178         return;
21179
21180       /* If we're a nested function, initially use a parent of NULL; if we're
21181          a plain function, this will be fixed up in decls_for_scope.  If
21182          we're a method, it will be ignored, since we already have a DIE.  */
21183       if (decl_function_context (decl)
21184           /* But if we're in terse mode, we don't care about scope.  */
21185           && debug_info_level > DINFO_LEVEL_TERSE)
21186         context_die = NULL;
21187       break;
21188
21189     case VAR_DECL:
21190       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21191          declaration and if the declaration was never even referenced from
21192          within this entire compilation unit.  We suppress these DIEs in
21193          order to save space in the .debug section (by eliminating entries
21194          which are probably useless).  Note that we must not suppress
21195          block-local extern declarations (whether used or not) because that
21196          would screw-up the debugger's name lookup mechanism and cause it to
21197          miss things which really ought to be in scope at a given point.  */
21198       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21199         return;
21200
21201       /* For local statics lookup proper context die.  */
21202       if (TREE_STATIC (decl) && decl_function_context (decl))
21203         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21204
21205       /* If we are in terse mode, don't generate any DIEs to represent any
21206          variable declarations or definitions.  */
21207       if (debug_info_level <= DINFO_LEVEL_TERSE)
21208         return;
21209       break;
21210
21211     case CONST_DECL:
21212       if (debug_info_level <= DINFO_LEVEL_TERSE)
21213         return;
21214       if (!is_fortran () && !is_ada ())
21215         return;
21216       if (TREE_STATIC (decl) && decl_function_context (decl))
21217         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21218       break;
21219
21220     case NAMESPACE_DECL:
21221     case IMPORTED_DECL:
21222       if (debug_info_level <= DINFO_LEVEL_TERSE)
21223         return;
21224       if (lookup_decl_die (decl) != NULL)
21225         return;
21226       break;
21227
21228     case TYPE_DECL:
21229       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21230       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21231         return;
21232
21233       /* Don't bother trying to generate any DIEs to represent any of the
21234          normal built-in types for the language we are compiling.  */
21235       if (DECL_IS_BUILTIN (decl))
21236         return;
21237
21238       /* If we are in terse mode, don't generate any DIEs for types.  */
21239       if (debug_info_level <= DINFO_LEVEL_TERSE)
21240         return;
21241
21242       /* If we're a function-scope tag, initially use a parent of NULL;
21243          this will be fixed up in decls_for_scope.  */
21244       if (decl_function_context (decl))
21245         context_die = NULL;
21246
21247       break;
21248
21249     default:
21250       return;
21251     }
21252
21253   gen_decl_die (decl, NULL, context_die);
21254 }
21255
21256 /* Write the debugging output for DECL.  */
21257
21258 static void
21259 dwarf2out_function_decl (tree decl)
21260 {
21261   dwarf2out_decl (decl);
21262
21263   htab_empty (decl_loc_table);
21264 }
21265
21266 /* Output a marker (i.e. a label) for the beginning of the generated code for
21267    a lexical block.  */
21268
21269 static void
21270 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21271                        unsigned int blocknum)
21272 {
21273   switch_to_section (current_function_section ());
21274   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21275 }
21276
21277 /* Output a marker (i.e. a label) for the end of the generated code for a
21278    lexical block.  */
21279
21280 static void
21281 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21282 {
21283   switch_to_section (current_function_section ());
21284   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21285 }
21286
21287 /* Returns nonzero if it is appropriate not to emit any debugging
21288    information for BLOCK, because it doesn't contain any instructions.
21289
21290    Don't allow this for blocks with nested functions or local classes
21291    as we would end up with orphans, and in the presence of scheduling
21292    we may end up calling them anyway.  */
21293
21294 static bool
21295 dwarf2out_ignore_block (const_tree block)
21296 {
21297   tree decl;
21298   unsigned int i;
21299
21300   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21301     if (TREE_CODE (decl) == FUNCTION_DECL
21302         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21303       return 0;
21304   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21305     {
21306       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21307       if (TREE_CODE (decl) == FUNCTION_DECL
21308           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21309       return 0;
21310     }
21311
21312   return 1;
21313 }
21314
21315 /* Hash table routines for file_hash.  */
21316
21317 static int
21318 file_table_eq (const void *p1_p, const void *p2_p)
21319 {
21320   const struct dwarf_file_data *const p1 =
21321     (const struct dwarf_file_data *) p1_p;
21322   const char *const p2 = (const char *) p2_p;
21323   return strcmp (p1->filename, p2) == 0;
21324 }
21325
21326 static hashval_t
21327 file_table_hash (const void *p_p)
21328 {
21329   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21330   return htab_hash_string (p->filename);
21331 }
21332
21333 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21334    dwarf2out.c) and return its "index".  The index of each (known) filename is
21335    just a unique number which is associated with only that one filename.  We
21336    need such numbers for the sake of generating labels (in the .debug_sfnames
21337    section) and references to those files numbers (in the .debug_srcinfo
21338    and.debug_macinfo sections).  If the filename given as an argument is not
21339    found in our current list, add it to the list and assign it the next
21340    available unique index number.  In order to speed up searches, we remember
21341    the index of the filename was looked up last.  This handles the majority of
21342    all searches.  */
21343
21344 static struct dwarf_file_data *
21345 lookup_filename (const char *file_name)
21346 {
21347   void ** slot;
21348   struct dwarf_file_data * created;
21349
21350   /* Check to see if the file name that was searched on the previous
21351      call matches this file name.  If so, return the index.  */
21352   if (file_table_last_lookup
21353       && (file_name == file_table_last_lookup->filename
21354           || strcmp (file_table_last_lookup->filename, file_name) == 0))
21355     return file_table_last_lookup;
21356
21357   /* Didn't match the previous lookup, search the table.  */
21358   slot = htab_find_slot_with_hash (file_table, file_name,
21359                                    htab_hash_string (file_name), INSERT);
21360   if (*slot)
21361     return (struct dwarf_file_data *) *slot;
21362
21363   created = ggc_alloc_dwarf_file_data ();
21364   created->filename = file_name;
21365   created->emitted_number = 0;
21366   *slot = created;
21367   return created;
21368 }
21369
21370 /* If the assembler will construct the file table, then translate the compiler
21371    internal file table number into the assembler file table number, and emit
21372    a .file directive if we haven't already emitted one yet.  The file table
21373    numbers are different because we prune debug info for unused variables and
21374    types, which may include filenames.  */
21375
21376 static int
21377 maybe_emit_file (struct dwarf_file_data * fd)
21378 {
21379   if (! fd->emitted_number)
21380     {
21381       if (last_emitted_file)
21382         fd->emitted_number = last_emitted_file->emitted_number + 1;
21383       else
21384         fd->emitted_number = 1;
21385       last_emitted_file = fd;
21386
21387       if (DWARF2_ASM_LINE_DEBUG_INFO)
21388         {
21389           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21390           output_quoted_string (asm_out_file,
21391                                 remap_debug_filename (fd->filename));
21392           fputc ('\n', asm_out_file);
21393         }
21394     }
21395
21396   return fd->emitted_number;
21397 }
21398
21399 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21400    That generation should happen after function debug info has been
21401    generated. The value of the attribute is the constant value of ARG.  */
21402
21403 static void
21404 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21405 {
21406   die_arg_entry entry;
21407
21408   if (!die || !arg)
21409     return;
21410
21411   if (!tmpl_value_parm_die_table)
21412     tmpl_value_parm_die_table
21413       = VEC_alloc (die_arg_entry, gc, 32);
21414
21415   entry.die = die;
21416   entry.arg = arg;
21417   VEC_safe_push (die_arg_entry, gc,
21418                  tmpl_value_parm_die_table,
21419                  &entry);
21420 }
21421
21422 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21423    by append_entry_to_tmpl_value_parm_die_table. This function must
21424    be called after function DIEs have been generated.  */
21425
21426 static void
21427 gen_remaining_tmpl_value_param_die_attribute (void)
21428 {
21429   if (tmpl_value_parm_die_table)
21430     {
21431       unsigned i;
21432       die_arg_entry *e;
21433
21434       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21435         tree_add_const_value_attribute (e->die, e->arg);
21436     }
21437 }
21438
21439
21440 /* Replace DW_AT_name for the decl with name.  */
21441
21442 static void
21443 dwarf2out_set_name (tree decl, tree name)
21444 {
21445   dw_die_ref die;
21446   dw_attr_ref attr;
21447   const char *dname;
21448
21449   die = TYPE_SYMTAB_DIE (decl);
21450   if (!die)
21451     return;
21452
21453   dname = dwarf2_name (name, 0);
21454   if (!dname)
21455     return;
21456
21457   attr = get_AT (die, DW_AT_name);
21458   if (attr)
21459     {
21460       struct indirect_string_node *node;
21461
21462       node = find_AT_string (dname);
21463       /* replace the string.  */
21464       attr->dw_attr_val.v.val_str = node;
21465     }
21466
21467   else
21468     add_name_attribute (die, dname);
21469 }
21470
21471 /* Called by the final INSN scan whenever we see a direct function call.
21472    Make an entry into the direct call table, recording the point of call
21473    and a reference to the target function's debug entry.  */
21474
21475 static void
21476 dwarf2out_direct_call (tree targ)
21477 {
21478   dcall_entry e;
21479   tree origin = decl_ultimate_origin (targ);
21480
21481   /* If this is a clone, use the abstract origin as the target.  */
21482   if (origin)
21483     targ = origin;
21484
21485   e.poc_label_num = poc_label_num++;
21486   e.poc_decl = current_function_decl;
21487   e.targ_die = force_decl_die (targ);
21488   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21489
21490   /* Drop a label at the return point to mark the point of call.  */
21491   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21492 }
21493
21494 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21495
21496 static hashval_t
21497 vcall_insn_table_hash (const void *x)
21498 {
21499   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21500 }
21501
21502 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21503    insnd_uid of *Y.  */
21504
21505 static int
21506 vcall_insn_table_eq (const void *x, const void *y)
21507 {
21508   return (((const struct vcall_insn *) x)->insn_uid
21509           == ((const struct vcall_insn *) y)->insn_uid);
21510 }
21511
21512 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21513
21514 static void
21515 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21516 {
21517   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21518   struct vcall_insn **slot;
21519
21520   gcc_assert (item);
21521   item->insn_uid = insn_uid;
21522   item->vtable_slot = vtable_slot;
21523   slot = (struct vcall_insn **)
21524       htab_find_slot_with_hash (vcall_insn_table, &item,
21525                                 (hashval_t) insn_uid, INSERT);
21526   *slot = item;
21527 }
21528
21529 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21530
21531 static unsigned int
21532 lookup_vcall_insn (unsigned int insn_uid)
21533 {
21534   struct vcall_insn item;
21535   struct vcall_insn *p;
21536
21537   item.insn_uid = insn_uid;
21538   item.vtable_slot = 0;
21539   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21540                                                  (void *) &item,
21541                                                  (hashval_t) insn_uid);
21542   if (p == NULL)
21543     return (unsigned int) -1;
21544   return p->vtable_slot;
21545 }
21546
21547
21548 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21549    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21550    is the vtable slot index that we will need to put in the virtual call
21551    table later.  */
21552
21553 static void
21554 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21555 {
21556   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21557     {
21558       tree token = OBJ_TYPE_REF_TOKEN (addr);
21559       if (TREE_CODE (token) == INTEGER_CST)
21560         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21561     }
21562 }
21563
21564 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21565    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21566    with NEW_INSN.  */
21567
21568 static void
21569 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21570 {
21571   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21572
21573   if (vtable_slot != (unsigned int) -1)
21574     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21575 }
21576
21577 /* Called by the final INSN scan whenever we see a virtual function call.
21578    Make an entry into the virtual call table, recording the point of call
21579    and the slot index of the vtable entry used to call the virtual member
21580    function.  The slot index was associated with the INSN_UID during the
21581    lowering to RTL.  */
21582
21583 static void
21584 dwarf2out_virtual_call (int insn_uid)
21585 {
21586   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21587   vcall_entry e;
21588
21589   if (vtable_slot == (unsigned int) -1)
21590     return;
21591
21592   e.poc_label_num = poc_label_num++;
21593   e.vtable_slot = vtable_slot;
21594   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21595
21596   /* Drop a label at the return point to mark the point of call.  */
21597   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21598 }
21599
21600 /* Called by the final INSN scan whenever we see a var location.  We
21601    use it to drop labels in the right places, and throw the location in
21602    our lookup table.  */
21603
21604 static void
21605 dwarf2out_var_location (rtx loc_note)
21606 {
21607   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21608   struct var_loc_node *newloc;
21609   rtx next_real;
21610   static const char *last_label;
21611   static const char *last_postcall_label;
21612   static bool last_in_cold_section_p;
21613   tree decl;
21614
21615   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21616     return;
21617
21618   next_real = next_real_insn (loc_note);
21619   /* If there are no instructions which would be affected by this note,
21620      don't do anything.  */
21621   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21622     return;
21623
21624   /* If there were any real insns between note we processed last time
21625      and this note (or if it is the first note), clear
21626      last_{,postcall_}label so that they are not reused this time.  */
21627   if (last_var_location_insn == NULL_RTX
21628       || last_var_location_insn != next_real
21629       || last_in_cold_section_p != in_cold_section_p)
21630     {
21631       last_label = NULL;
21632       last_postcall_label = NULL;
21633     }
21634
21635   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21636   newloc = add_var_loc_to_decl (decl, loc_note,
21637                                 NOTE_DURING_CALL_P (loc_note)
21638                                 ? last_postcall_label : last_label);
21639   if (newloc == NULL)
21640     return;
21641
21642   /* If there were no real insns between note we processed last time
21643      and this note, use the label we emitted last time.  Otherwise
21644      create a new label and emit it.  */
21645   if (last_label == NULL)
21646     {
21647       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21648       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21649       loclabel_num++;
21650       last_label = ggc_strdup (loclabel);
21651     }
21652
21653   if (!NOTE_DURING_CALL_P (loc_note))
21654     newloc->label = last_label;
21655   else
21656     {
21657       if (!last_postcall_label)
21658         {
21659           sprintf (loclabel, "%s-1", last_label);
21660           last_postcall_label = ggc_strdup (loclabel);
21661         }
21662       newloc->label = last_postcall_label;
21663     }
21664
21665   last_var_location_insn = next_real;
21666   last_in_cold_section_p = in_cold_section_p;
21667 }
21668
21669 /* We need to reset the locations at the beginning of each
21670    function. We can't do this in the end_function hook, because the
21671    declarations that use the locations won't have been output when
21672    that hook is called.  Also compute have_multiple_function_sections here.  */
21673
21674 static void
21675 dwarf2out_begin_function (tree fun)
21676 {
21677   if (function_section (fun) != text_section)
21678     have_multiple_function_sections = true;
21679   else if (flag_reorder_blocks_and_partition && !cold_text_section)
21680     {
21681       gcc_assert (current_function_decl == fun);
21682       cold_text_section = unlikely_text_section ();
21683       switch_to_section (cold_text_section);
21684       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21685       switch_to_section (current_function_section ());
21686     }
21687
21688   dwarf2out_note_section_used ();
21689 }
21690
21691 /* Output a label to mark the beginning of a source code line entry
21692    and record information relating to this source line, in
21693    'line_info_table' for later output of the .debug_line section.  */
21694
21695 static void
21696 dwarf2out_source_line (unsigned int line, const char *filename,
21697                        int discriminator, bool is_stmt)
21698 {
21699   static bool last_is_stmt = true;
21700
21701   if (debug_info_level >= DINFO_LEVEL_NORMAL
21702       && line != 0)
21703     {
21704       int file_num = maybe_emit_file (lookup_filename (filename));
21705
21706       switch_to_section (current_function_section ());
21707
21708       /* If requested, emit something human-readable.  */
21709       if (flag_debug_asm)
21710         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21711                  filename, line);
21712
21713       if (DWARF2_ASM_LINE_DEBUG_INFO)
21714         {
21715           /* Emit the .loc directive understood by GNU as.  */
21716           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21717           if (is_stmt != last_is_stmt)
21718             {
21719               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21720               last_is_stmt = is_stmt;
21721             }
21722           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21723             fprintf (asm_out_file, " discriminator %d", discriminator);
21724           fputc ('\n', asm_out_file);
21725
21726           /* Indicate that line number info exists.  */
21727           line_info_table_in_use++;
21728         }
21729       else if (function_section (current_function_decl) != text_section)
21730         {
21731           dw_separate_line_info_ref line_info;
21732           targetm.asm_out.internal_label (asm_out_file,
21733                                           SEPARATE_LINE_CODE_LABEL,
21734                                           separate_line_info_table_in_use);
21735
21736           /* Expand the line info table if necessary.  */
21737           if (separate_line_info_table_in_use
21738               == separate_line_info_table_allocated)
21739             {
21740               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21741               separate_line_info_table
21742                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21743                                  separate_line_info_table,
21744                                  separate_line_info_table_allocated);
21745               memset (separate_line_info_table
21746                        + separate_line_info_table_in_use,
21747                       0,
21748                       (LINE_INFO_TABLE_INCREMENT
21749                        * sizeof (dw_separate_line_info_entry)));
21750             }
21751
21752           /* Add the new entry at the end of the line_info_table.  */
21753           line_info
21754             = &separate_line_info_table[separate_line_info_table_in_use++];
21755           line_info->dw_file_num = file_num;
21756           line_info->dw_line_num = line;
21757           line_info->function = current_function_funcdef_no;
21758         }
21759       else
21760         {
21761           dw_line_info_ref line_info;
21762
21763           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21764                                      line_info_table_in_use);
21765
21766           /* Expand the line info table if necessary.  */
21767           if (line_info_table_in_use == line_info_table_allocated)
21768             {
21769               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21770               line_info_table
21771                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21772                                  line_info_table_allocated);
21773               memset (line_info_table + line_info_table_in_use, 0,
21774                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21775             }
21776
21777           /* Add the new entry at the end of the line_info_table.  */
21778           line_info = &line_info_table[line_info_table_in_use++];
21779           line_info->dw_file_num = file_num;
21780           line_info->dw_line_num = line;
21781         }
21782     }
21783 }
21784
21785 /* Record the beginning of a new source file.  */
21786
21787 static void
21788 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21789 {
21790   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21791     {
21792       /* Record the beginning of the file for break_out_includes.  */
21793       dw_die_ref bincl_die;
21794
21795       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21796       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21797     }
21798
21799   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21800     {
21801       macinfo_entry e;
21802       e.code = DW_MACINFO_start_file;
21803       e.lineno = lineno;
21804       e.info = xstrdup (filename);
21805       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21806     }
21807 }
21808
21809 /* Record the end of a source file.  */
21810
21811 static void
21812 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21813 {
21814   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21815     /* Record the end of the file for break_out_includes.  */
21816     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21817
21818   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21819     {
21820       macinfo_entry e;
21821       e.code = DW_MACINFO_end_file;
21822       e.lineno = lineno;
21823       e.info = NULL;
21824       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21825     }
21826 }
21827
21828 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21829    the tail part of the directive line, i.e. the part which is past the
21830    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21831
21832 static void
21833 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21834                   const char *buffer ATTRIBUTE_UNUSED)
21835 {
21836   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21837     {
21838       macinfo_entry e;
21839       e.code = DW_MACINFO_define;
21840       e.lineno = lineno;
21841       e.info = xstrdup (buffer);;
21842       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21843     }
21844 }
21845
21846 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21847    the tail part of the directive line, i.e. the part which is past the
21848    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21849
21850 static void
21851 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21852                  const char *buffer ATTRIBUTE_UNUSED)
21853 {
21854   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21855     {
21856       macinfo_entry e;
21857       e.code = DW_MACINFO_undef;
21858       e.lineno = lineno;
21859       e.info = xstrdup (buffer);;
21860       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21861     }
21862 }
21863
21864 static void
21865 output_macinfo (void)
21866 {
21867   unsigned i;
21868   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
21869   macinfo_entry *ref;
21870
21871   if (! length)
21872     return;
21873
21874   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
21875     {
21876       switch (ref->code)
21877         {
21878           case DW_MACINFO_start_file:
21879             {
21880               int file_num = maybe_emit_file (lookup_filename (ref->info));
21881               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21882               dw2_asm_output_data_uleb128 
21883                         (ref->lineno, "Included from line number %lu", 
21884                                                 (unsigned long)ref->lineno);
21885               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21886             }
21887             break;
21888           case DW_MACINFO_end_file:
21889             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21890             break;
21891           case DW_MACINFO_define:
21892             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21893             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
21894                                                 (unsigned long)ref->lineno);
21895             dw2_asm_output_nstring (ref->info, -1, "The macro");
21896             break;
21897           case DW_MACINFO_undef:
21898             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21899             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21900                                                 (unsigned long)ref->lineno);
21901             dw2_asm_output_nstring (ref->info, -1, "The macro");
21902             break;
21903           default:
21904            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21905              ASM_COMMENT_START, (unsigned long)ref->code);
21906           break;
21907         }
21908     }
21909 }
21910
21911 /* Set up for Dwarf output at the start of compilation.  */
21912
21913 static void
21914 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21915 {
21916   /* Allocate the file_table.  */
21917   file_table = htab_create_ggc (50, file_table_hash,
21918                                 file_table_eq, NULL);
21919
21920   /* Allocate the decl_die_table.  */
21921   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21922                                     decl_die_table_eq, NULL);
21923
21924   /* Allocate the decl_loc_table.  */
21925   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21926                                     decl_loc_table_eq, NULL);
21927
21928   /* Allocate the initial hunk of the decl_scope_table.  */
21929   decl_scope_table = VEC_alloc (tree, gc, 256);
21930
21931   /* Allocate the initial hunk of the abbrev_die_table.  */
21932   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21933     (ABBREV_DIE_TABLE_INCREMENT);
21934   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21935   /* Zero-th entry is allocated, but unused.  */
21936   abbrev_die_table_in_use = 1;
21937
21938   /* Allocate the initial hunk of the line_info_table.  */
21939   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21940     (LINE_INFO_TABLE_INCREMENT);
21941   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21942
21943   /* Zero-th entry is allocated, but unused.  */
21944   line_info_table_in_use = 1;
21945
21946   /* Allocate the pubtypes and pubnames vectors.  */
21947   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21948   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21949
21950   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21951   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21952                                       vcall_insn_table_eq, NULL);
21953
21954   incomplete_types = VEC_alloc (tree, gc, 64);
21955
21956   used_rtx_array = VEC_alloc (rtx, gc, 32);
21957
21958   debug_info_section = get_section (DEBUG_INFO_SECTION,
21959                                     SECTION_DEBUG, NULL);
21960   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21961                                       SECTION_DEBUG, NULL);
21962   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21963                                        SECTION_DEBUG, NULL);
21964   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21965                                        SECTION_DEBUG, NULL);
21966   debug_line_section = get_section (DEBUG_LINE_SECTION,
21967                                     SECTION_DEBUG, NULL);
21968   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21969                                    SECTION_DEBUG, NULL);
21970   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21971                                         SECTION_DEBUG, NULL);
21972   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21973                                         SECTION_DEBUG, NULL);
21974   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21975                                      SECTION_DEBUG, NULL);
21976   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21977                                      SECTION_DEBUG, NULL);
21978   debug_str_section = get_section (DEBUG_STR_SECTION,
21979                                    DEBUG_STR_SECTION_FLAGS, NULL);
21980   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21981                                       SECTION_DEBUG, NULL);
21982   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21983                                      SECTION_DEBUG, NULL);
21984
21985   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21986   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21987                                DEBUG_ABBREV_SECTION_LABEL, 0);
21988   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21989   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21990                                COLD_TEXT_SECTION_LABEL, 0);
21991   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21992
21993   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21994                                DEBUG_INFO_SECTION_LABEL, 0);
21995   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21996                                DEBUG_LINE_SECTION_LABEL, 0);
21997   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21998                                DEBUG_RANGES_SECTION_LABEL, 0);
21999   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22000                                DEBUG_MACINFO_SECTION_LABEL, 0);
22001
22002   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22003     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22004
22005   switch_to_section (text_section);
22006   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22007 }
22008
22009 /* Called before cgraph_optimize starts outputtting functions, variables
22010    and toplevel asms into assembly.  */
22011
22012 static void
22013 dwarf2out_assembly_start (void)
22014 {
22015   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22016       && dwarf2out_do_cfi_asm ()
22017       && (!(flag_unwind_tables || flag_exceptions)
22018           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22019     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22020 }
22021
22022 /* A helper function for dwarf2out_finish called through
22023    htab_traverse.  Emit one queued .debug_str string.  */
22024
22025 static int
22026 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22027 {
22028   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22029
22030   if (node->label && node->refcount)
22031     {
22032       switch_to_section (debug_str_section);
22033       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22034       assemble_string (node->str, strlen (node->str) + 1);
22035     }
22036
22037   return 1;
22038 }
22039
22040 #if ENABLE_ASSERT_CHECKING
22041 /* Verify that all marks are clear.  */
22042
22043 static void
22044 verify_marks_clear (dw_die_ref die)
22045 {
22046   dw_die_ref c;
22047
22048   gcc_assert (! die->die_mark);
22049   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22050 }
22051 #endif /* ENABLE_ASSERT_CHECKING */
22052
22053 /* Clear the marks for a die and its children.
22054    Be cool if the mark isn't set.  */
22055
22056 static void
22057 prune_unmark_dies (dw_die_ref die)
22058 {
22059   dw_die_ref c;
22060
22061   if (die->die_mark)
22062     die->die_mark = 0;
22063   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22064 }
22065
22066 /* Given DIE that we're marking as used, find any other dies
22067    it references as attributes and mark them as used.  */
22068
22069 static void
22070 prune_unused_types_walk_attribs (dw_die_ref die)
22071 {
22072   dw_attr_ref a;
22073   unsigned ix;
22074
22075   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22076     {
22077       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22078         {
22079           /* A reference to another DIE.
22080              Make sure that it will get emitted.
22081              If it was broken out into a comdat group, don't follow it.  */
22082           if (dwarf_version < 4
22083               || a->dw_attr == DW_AT_specification
22084               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22085             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22086         }
22087       /* Set the string's refcount to 0 so that prune_unused_types_mark
22088          accounts properly for it.  */
22089       if (AT_class (a) == dw_val_class_str)
22090         a->dw_attr_val.v.val_str->refcount = 0;
22091     }
22092 }
22093
22094
22095 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22096    to DIE's children.  */
22097
22098 static void
22099 prune_unused_types_mark (dw_die_ref die, int dokids)
22100 {
22101   dw_die_ref c;
22102
22103   if (die->die_mark == 0)
22104     {
22105       /* We haven't done this node yet.  Mark it as used.  */
22106       die->die_mark = 1;
22107
22108       /* We also have to mark its parents as used.
22109          (But we don't want to mark our parents' kids due to this.)  */
22110       if (die->die_parent)
22111         prune_unused_types_mark (die->die_parent, 0);
22112
22113       /* Mark any referenced nodes.  */
22114       prune_unused_types_walk_attribs (die);
22115
22116       /* If this node is a specification,
22117          also mark the definition, if it exists.  */
22118       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22119         prune_unused_types_mark (die->die_definition, 1);
22120     }
22121
22122   if (dokids && die->die_mark != 2)
22123     {
22124       /* We need to walk the children, but haven't done so yet.
22125          Remember that we've walked the kids.  */
22126       die->die_mark = 2;
22127
22128       /* If this is an array type, we need to make sure our
22129          kids get marked, even if they're types.  If we're
22130          breaking out types into comdat sections, do this
22131          for all type definitions.  */
22132       if (die->die_tag == DW_TAG_array_type
22133           || (dwarf_version >= 4
22134               && is_type_die (die) && ! is_declaration_die (die)))
22135         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22136       else
22137         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22138     }
22139 }
22140
22141 /* For local classes, look if any static member functions were emitted
22142    and if so, mark them.  */
22143
22144 static void
22145 prune_unused_types_walk_local_classes (dw_die_ref die)
22146 {
22147   dw_die_ref c;
22148
22149   if (die->die_mark == 2)
22150     return;
22151
22152   switch (die->die_tag)
22153     {
22154     case DW_TAG_structure_type:
22155     case DW_TAG_union_type:
22156     case DW_TAG_class_type:
22157       break;
22158
22159     case DW_TAG_subprogram:
22160       if (!get_AT_flag (die, DW_AT_declaration)
22161           || die->die_definition != NULL)
22162         prune_unused_types_mark (die, 1);
22163       return;
22164
22165     default:
22166       return;
22167     }
22168
22169   /* Mark children.  */
22170   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22171 }
22172
22173 /* Walk the tree DIE and mark types that we actually use.  */
22174
22175 static void
22176 prune_unused_types_walk (dw_die_ref die)
22177 {
22178   dw_die_ref c;
22179
22180   /* Don't do anything if this node is already marked and
22181      children have been marked as well.  */
22182   if (die->die_mark == 2)
22183     return;
22184
22185   switch (die->die_tag)
22186     {
22187     case DW_TAG_structure_type:
22188     case DW_TAG_union_type:
22189     case DW_TAG_class_type:
22190       if (die->die_perennial_p)
22191         break;
22192
22193       for (c = die->die_parent; c; c = c->die_parent)
22194         if (c->die_tag == DW_TAG_subprogram)
22195           break;
22196
22197       /* Finding used static member functions inside of classes
22198          is needed just for local classes, because for other classes
22199          static member function DIEs with DW_AT_specification
22200          are emitted outside of the DW_TAG_*_type.  If we ever change
22201          it, we'd need to call this even for non-local classes.  */
22202       if (c)
22203         prune_unused_types_walk_local_classes (die);
22204
22205       /* It's a type node --- don't mark it.  */
22206       return;
22207
22208     case DW_TAG_const_type:
22209     case DW_TAG_packed_type:
22210     case DW_TAG_pointer_type:
22211     case DW_TAG_reference_type:
22212     case DW_TAG_rvalue_reference_type:
22213     case DW_TAG_volatile_type:
22214     case DW_TAG_typedef:
22215     case DW_TAG_array_type:
22216     case DW_TAG_interface_type:
22217     case DW_TAG_friend:
22218     case DW_TAG_variant_part:
22219     case DW_TAG_enumeration_type:
22220     case DW_TAG_subroutine_type:
22221     case DW_TAG_string_type:
22222     case DW_TAG_set_type:
22223     case DW_TAG_subrange_type:
22224     case DW_TAG_ptr_to_member_type:
22225     case DW_TAG_file_type:
22226       if (die->die_perennial_p)
22227         break;
22228
22229       /* It's a type node --- don't mark it.  */
22230       return;
22231
22232     default:
22233       /* Mark everything else.  */
22234       break;
22235   }
22236
22237   if (die->die_mark == 0)
22238     {
22239       die->die_mark = 1;
22240
22241       /* Now, mark any dies referenced from here.  */
22242       prune_unused_types_walk_attribs (die);
22243     }
22244
22245   die->die_mark = 2;
22246
22247   /* Mark children.  */
22248   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22249 }
22250
22251 /* Increment the string counts on strings referred to from DIE's
22252    attributes.  */
22253
22254 static void
22255 prune_unused_types_update_strings (dw_die_ref die)
22256 {
22257   dw_attr_ref a;
22258   unsigned ix;
22259
22260   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22261     if (AT_class (a) == dw_val_class_str)
22262       {
22263         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22264         s->refcount++;
22265         /* Avoid unnecessarily putting strings that are used less than
22266            twice in the hash table.  */
22267         if (s->refcount
22268             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22269           {
22270             void ** slot;
22271             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22272                                              htab_hash_string (s->str),
22273                                              INSERT);
22274             gcc_assert (*slot == NULL);
22275             *slot = s;
22276           }
22277       }
22278 }
22279
22280 /* Remove from the tree DIE any dies that aren't marked.  */
22281
22282 static void
22283 prune_unused_types_prune (dw_die_ref die)
22284 {
22285   dw_die_ref c;
22286
22287   gcc_assert (die->die_mark);
22288   prune_unused_types_update_strings (die);
22289
22290   if (! die->die_child)
22291     return;
22292
22293   c = die->die_child;
22294   do {
22295     dw_die_ref prev = c;
22296     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22297       if (c == die->die_child)
22298         {
22299           /* No marked children between 'prev' and the end of the list.  */
22300           if (prev == c)
22301             /* No marked children at all.  */
22302             die->die_child = NULL;
22303           else
22304             {
22305               prev->die_sib = c->die_sib;
22306               die->die_child = prev;
22307             }
22308           return;
22309         }
22310
22311     if (c != prev->die_sib)
22312       prev->die_sib = c;
22313     prune_unused_types_prune (c);
22314   } while (c != die->die_child);
22315 }
22316
22317 /* A helper function for dwarf2out_finish called through
22318    htab_traverse.  Clear .debug_str strings that we haven't already
22319    decided to emit.  */
22320
22321 static int
22322 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22323 {
22324   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22325
22326   if (!node->label || !node->refcount)
22327     htab_clear_slot (debug_str_hash, h);
22328
22329   return 1;
22330 }
22331
22332 /* Remove dies representing declarations that we never use.  */
22333
22334 static void
22335 prune_unused_types (void)
22336 {
22337   unsigned int i;
22338   limbo_die_node *node;
22339   comdat_type_node *ctnode;
22340   pubname_ref pub;
22341   dcall_entry *dcall;
22342
22343 #if ENABLE_ASSERT_CHECKING
22344   /* All the marks should already be clear.  */
22345   verify_marks_clear (comp_unit_die ());
22346   for (node = limbo_die_list; node; node = node->next)
22347     verify_marks_clear (node->die);
22348   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22349     verify_marks_clear (ctnode->root_die);
22350 #endif /* ENABLE_ASSERT_CHECKING */
22351
22352   /* Mark types that are used in global variables.  */
22353   premark_types_used_by_global_vars ();
22354
22355   /* Set the mark on nodes that are actually used.  */
22356   prune_unused_types_walk (comp_unit_die ());
22357   for (node = limbo_die_list; node; node = node->next)
22358     prune_unused_types_walk (node->die);
22359   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22360     {
22361       prune_unused_types_walk (ctnode->root_die);
22362       prune_unused_types_mark (ctnode->type_die, 1);
22363     }
22364
22365   /* Also set the mark on nodes referenced from the
22366      pubname_table or arange_table.  */
22367   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22368     prune_unused_types_mark (pub->die, 1);
22369   for (i = 0; i < arange_table_in_use; i++)
22370     prune_unused_types_mark (arange_table[i], 1);
22371
22372   /* Mark nodes referenced from the direct call table.  */
22373   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22374     prune_unused_types_mark (dcall->targ_die, 1);
22375
22376   /* Get rid of nodes that aren't marked; and update the string counts.  */
22377   if (debug_str_hash && debug_str_hash_forced)
22378     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22379   else if (debug_str_hash)
22380     htab_empty (debug_str_hash);
22381   prune_unused_types_prune (comp_unit_die ());
22382   for (node = limbo_die_list; node; node = node->next)
22383     prune_unused_types_prune (node->die);
22384   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22385     prune_unused_types_prune (ctnode->root_die);
22386
22387   /* Leave the marks clear.  */
22388   prune_unmark_dies (comp_unit_die ());
22389   for (node = limbo_die_list; node; node = node->next)
22390     prune_unmark_dies (node->die);
22391   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22392     prune_unmark_dies (ctnode->root_die);
22393 }
22394
22395 /* Set the parameter to true if there are any relative pathnames in
22396    the file table.  */
22397 static int
22398 file_table_relative_p (void ** slot, void *param)
22399 {
22400   bool *p = (bool *) param;
22401   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22402   if (!IS_ABSOLUTE_PATH (d->filename))
22403     {
22404       *p = true;
22405       return 0;
22406     }
22407   return 1;
22408 }
22409
22410 /* Routines to manipulate hash table of comdat type units.  */
22411
22412 static hashval_t
22413 htab_ct_hash (const void *of)
22414 {
22415   hashval_t h;
22416   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22417
22418   memcpy (&h, type_node->signature, sizeof (h));
22419   return h;
22420 }
22421
22422 static int
22423 htab_ct_eq (const void *of1, const void *of2)
22424 {
22425   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22426   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22427
22428   return (! memcmp (type_node_1->signature, type_node_2->signature,
22429                     DWARF_TYPE_SIGNATURE_SIZE));
22430 }
22431
22432 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22433    to the location it would have been added, should we know its
22434    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22435    probably improve compactness of debug info, removing equivalent
22436    abbrevs, and hide any differences caused by deferring the
22437    computation of the assembler name, triggered by e.g. PCH.  */
22438
22439 static inline void
22440 move_linkage_attr (dw_die_ref die)
22441 {
22442   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22443   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22444
22445   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22446               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22447
22448   while (--ix > 0)
22449     {
22450       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22451
22452       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22453         break;
22454     }
22455
22456   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22457     {
22458       VEC_pop (dw_attr_node, die->die_attr);
22459       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22460     }
22461 }
22462
22463 /* Helper function for resolve_addr, attempt to resolve
22464    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22465    SYMBOL_REFs refer to variables emitted in the current CU.  */
22466
22467 static int
22468 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22469 {
22470   rtx rtl = *addr;
22471
22472   if (GET_CODE (rtl) == CONST_STRING)
22473     {
22474       size_t len = strlen (XSTR (rtl, 0)) + 1;
22475       tree t = build_string (len, XSTR (rtl, 0));
22476       tree tlen = build_int_cst (NULL_TREE, len - 1);
22477       TREE_TYPE (t)
22478         = build_array_type (char_type_node, build_index_type (tlen));
22479       rtl = lookup_constant_def (t);
22480       if (!rtl || !MEM_P (rtl))
22481         return 1;
22482       rtl = XEXP (rtl, 0);
22483       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22484       *addr = rtl;
22485       return 0;
22486     }
22487
22488   if (GET_CODE (rtl) == SYMBOL_REF
22489       && SYMBOL_REF_DECL (rtl)
22490       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22491     return 1;
22492
22493   if (GET_CODE (rtl) == CONST
22494       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22495     return 1;
22496
22497   return 0;
22498 }
22499
22500 /* Helper function for resolve_addr, handle one location
22501    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22502    the location list couldn't be resolved.  */
22503
22504 static bool
22505 resolve_addr_in_expr (dw_loc_descr_ref loc)
22506 {
22507   for (; loc; loc = loc->dw_loc_next)
22508     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22509          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22510         || (loc->dw_loc_opc == DW_OP_implicit_value
22511             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22512             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22513       return false;
22514     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22515              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22516       {
22517         dw_die_ref ref
22518           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22519         if (ref == NULL)
22520           return false;
22521         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22522         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22523         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22524       }
22525   return true;
22526 }
22527
22528 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22529    an address in .rodata section if the string literal is emitted there,
22530    or remove the containing location list or replace DW_AT_const_value
22531    with DW_AT_location and empty location expression, if it isn't found
22532    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22533    to something that has been emitted in the current CU.  */
22534
22535 static void
22536 resolve_addr (dw_die_ref die)
22537 {
22538   dw_die_ref c;
22539   dw_attr_ref a;
22540   dw_loc_list_ref *curr;
22541   unsigned ix;
22542
22543   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22544     switch (AT_class (a))
22545       {
22546       case dw_val_class_loc_list:
22547         curr = AT_loc_list_ptr (a);
22548         while (*curr)
22549           {
22550             if (!resolve_addr_in_expr ((*curr)->expr))
22551               {
22552                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22553                 if (next && (*curr)->ll_symbol)
22554                   {
22555                     gcc_assert (!next->ll_symbol);
22556                     next->ll_symbol = (*curr)->ll_symbol;
22557                   }
22558                 *curr = next;
22559               }
22560             else
22561               curr = &(*curr)->dw_loc_next;
22562           }
22563         if (!AT_loc_list (a))
22564           {
22565             remove_AT (die, a->dw_attr);
22566             ix--;
22567           }
22568         break;
22569       case dw_val_class_loc:
22570         if (!resolve_addr_in_expr (AT_loc (a)))
22571           {
22572             remove_AT (die, a->dw_attr);
22573             ix--;
22574           }
22575         break;
22576       case dw_val_class_addr:
22577         if (a->dw_attr == DW_AT_const_value
22578             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22579           {
22580             remove_AT (die, a->dw_attr);
22581             ix--;
22582           }
22583         break;
22584       default:
22585         break;
22586       }
22587
22588   FOR_EACH_CHILD (die, c, resolve_addr (c));
22589 }
22590 \f
22591 /* Helper routines for optimize_location_lists.
22592    This pass tries to share identical local lists in .debug_loc
22593    section.  */
22594
22595 /* Iteratively hash operands of LOC opcode.  */
22596
22597 static inline hashval_t
22598 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22599 {
22600   dw_val_ref val1 = &loc->dw_loc_oprnd1;
22601   dw_val_ref val2 = &loc->dw_loc_oprnd2;
22602
22603   switch (loc->dw_loc_opc)
22604     {
22605     case DW_OP_const4u:
22606     case DW_OP_const8u:
22607       if (loc->dtprel)
22608         goto hash_addr;
22609       /* FALLTHRU */
22610     case DW_OP_const1u:
22611     case DW_OP_const1s:
22612     case DW_OP_const2u:
22613     case DW_OP_const2s:
22614     case DW_OP_const4s:
22615     case DW_OP_const8s:
22616     case DW_OP_constu:
22617     case DW_OP_consts:
22618     case DW_OP_pick:
22619     case DW_OP_plus_uconst:
22620     case DW_OP_breg0:
22621     case DW_OP_breg1:
22622     case DW_OP_breg2:
22623     case DW_OP_breg3:
22624     case DW_OP_breg4:
22625     case DW_OP_breg5:
22626     case DW_OP_breg6:
22627     case DW_OP_breg7:
22628     case DW_OP_breg8:
22629     case DW_OP_breg9:
22630     case DW_OP_breg10:
22631     case DW_OP_breg11:
22632     case DW_OP_breg12:
22633     case DW_OP_breg13:
22634     case DW_OP_breg14:
22635     case DW_OP_breg15:
22636     case DW_OP_breg16:
22637     case DW_OP_breg17:
22638     case DW_OP_breg18:
22639     case DW_OP_breg19:
22640     case DW_OP_breg20:
22641     case DW_OP_breg21:
22642     case DW_OP_breg22:
22643     case DW_OP_breg23:
22644     case DW_OP_breg24:
22645     case DW_OP_breg25:
22646     case DW_OP_breg26:
22647     case DW_OP_breg27:
22648     case DW_OP_breg28:
22649     case DW_OP_breg29:
22650     case DW_OP_breg30:
22651     case DW_OP_breg31:
22652     case DW_OP_regx:
22653     case DW_OP_fbreg:
22654     case DW_OP_piece:
22655     case DW_OP_deref_size:
22656     case DW_OP_xderef_size:
22657       hash = iterative_hash_object (val1->v.val_int, hash);
22658       break;
22659     case DW_OP_skip:
22660     case DW_OP_bra:
22661       {
22662         int offset;
22663
22664         gcc_assert (val1->val_class == dw_val_class_loc);
22665         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22666         hash = iterative_hash_object (offset, hash);
22667       }
22668       break;
22669     case DW_OP_implicit_value:
22670       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22671       switch (val2->val_class)
22672         {
22673         case dw_val_class_const:
22674           hash = iterative_hash_object (val2->v.val_int, hash);
22675           break;
22676         case dw_val_class_vec:
22677           {
22678             unsigned int elt_size = val2->v.val_vec.elt_size;
22679             unsigned int len = val2->v.val_vec.length;
22680
22681             hash = iterative_hash_object (elt_size, hash);
22682             hash = iterative_hash_object (len, hash);
22683             hash = iterative_hash (val2->v.val_vec.array,
22684                                    len * elt_size, hash);
22685           }
22686           break;
22687         case dw_val_class_const_double:
22688           hash = iterative_hash_object (val2->v.val_double.low, hash);
22689           hash = iterative_hash_object (val2->v.val_double.high, hash);
22690           break;
22691         case dw_val_class_addr:
22692           hash = iterative_hash_rtx (val2->v.val_addr, hash);
22693           break;
22694         default:
22695           gcc_unreachable ();
22696         }
22697       break;
22698     case DW_OP_bregx:
22699     case DW_OP_bit_piece:
22700       hash = iterative_hash_object (val1->v.val_int, hash);
22701       hash = iterative_hash_object (val2->v.val_int, hash);
22702       break;
22703     case DW_OP_addr:
22704     hash_addr:
22705       if (loc->dtprel)
22706         {
22707           unsigned char dtprel = 0xd1;
22708           hash = iterative_hash_object (dtprel, hash);
22709         }
22710       hash = iterative_hash_rtx (val1->v.val_addr, hash);
22711       break;
22712     case DW_OP_GNU_implicit_pointer:
22713       hash = iterative_hash_object (val2->v.val_int, hash);
22714       break;
22715
22716     default:
22717       /* Other codes have no operands.  */
22718       break;
22719     }
22720   return hash;
22721 }
22722
22723 /* Iteratively hash the whole DWARF location expression LOC.  */
22724
22725 static inline hashval_t
22726 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22727 {
22728   dw_loc_descr_ref l;
22729   bool sizes_computed = false;
22730   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22731   size_of_locs (loc);
22732
22733   for (l = loc; l != NULL; l = l->dw_loc_next)
22734     {
22735       enum dwarf_location_atom opc = l->dw_loc_opc;
22736       hash = iterative_hash_object (opc, hash);
22737       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22738         {
22739           size_of_locs (loc);
22740           sizes_computed = true;
22741         }
22742       hash = hash_loc_operands (l, hash);
22743     }
22744   return hash;
22745 }
22746
22747 /* Compute hash of the whole location list LIST_HEAD.  */
22748
22749 static inline void
22750 hash_loc_list (dw_loc_list_ref list_head)
22751 {
22752   dw_loc_list_ref curr = list_head;
22753   hashval_t hash = 0;
22754
22755   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22756     {
22757       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22758       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22759       if (curr->section)
22760         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22761                                hash);
22762       hash = hash_locs (curr->expr, hash);
22763     }
22764   list_head->hash = hash;
22765 }
22766
22767 /* Return true if X and Y opcodes have the same operands.  */
22768
22769 static inline bool
22770 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22771 {
22772   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22773   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22774   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22775   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22776
22777   switch (x->dw_loc_opc)
22778     {
22779     case DW_OP_const4u:
22780     case DW_OP_const8u:
22781       if (x->dtprel)
22782         goto hash_addr;
22783       /* FALLTHRU */
22784     case DW_OP_const1u:
22785     case DW_OP_const1s:
22786     case DW_OP_const2u:
22787     case DW_OP_const2s:
22788     case DW_OP_const4s:
22789     case DW_OP_const8s:
22790     case DW_OP_constu:
22791     case DW_OP_consts:
22792     case DW_OP_pick:
22793     case DW_OP_plus_uconst:
22794     case DW_OP_breg0:
22795     case DW_OP_breg1:
22796     case DW_OP_breg2:
22797     case DW_OP_breg3:
22798     case DW_OP_breg4:
22799     case DW_OP_breg5:
22800     case DW_OP_breg6:
22801     case DW_OP_breg7:
22802     case DW_OP_breg8:
22803     case DW_OP_breg9:
22804     case DW_OP_breg10:
22805     case DW_OP_breg11:
22806     case DW_OP_breg12:
22807     case DW_OP_breg13:
22808     case DW_OP_breg14:
22809     case DW_OP_breg15:
22810     case DW_OP_breg16:
22811     case DW_OP_breg17:
22812     case DW_OP_breg18:
22813     case DW_OP_breg19:
22814     case DW_OP_breg20:
22815     case DW_OP_breg21:
22816     case DW_OP_breg22:
22817     case DW_OP_breg23:
22818     case DW_OP_breg24:
22819     case DW_OP_breg25:
22820     case DW_OP_breg26:
22821     case DW_OP_breg27:
22822     case DW_OP_breg28:
22823     case DW_OP_breg29:
22824     case DW_OP_breg30:
22825     case DW_OP_breg31:
22826     case DW_OP_regx:
22827     case DW_OP_fbreg:
22828     case DW_OP_piece:
22829     case DW_OP_deref_size:
22830     case DW_OP_xderef_size:
22831       return valx1->v.val_int == valy1->v.val_int;
22832     case DW_OP_skip:
22833     case DW_OP_bra:
22834       gcc_assert (valx1->val_class == dw_val_class_loc
22835                   && valy1->val_class == dw_val_class_loc
22836                   && x->dw_loc_addr == y->dw_loc_addr);
22837       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22838     case DW_OP_implicit_value:
22839       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22840           || valx2->val_class != valy2->val_class)
22841         return false;
22842       switch (valx2->val_class)
22843         {
22844         case dw_val_class_const:
22845           return valx2->v.val_int == valy2->v.val_int;
22846         case dw_val_class_vec:
22847           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22848                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22849                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22850                             valx2->v.val_vec.elt_size
22851                             * valx2->v.val_vec.length) == 0;
22852         case dw_val_class_const_double:
22853           return valx2->v.val_double.low == valy2->v.val_double.low
22854                  && valx2->v.val_double.high == valy2->v.val_double.high;
22855         case dw_val_class_addr:
22856           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22857         default:
22858           gcc_unreachable ();
22859         }
22860     case DW_OP_bregx:
22861     case DW_OP_bit_piece:
22862       return valx1->v.val_int == valy1->v.val_int
22863              && valx2->v.val_int == valy2->v.val_int;
22864     case DW_OP_addr:
22865     hash_addr:
22866       return rtx_equal_p (valx1->v.val_addr, valx2->v.val_addr);
22867     case DW_OP_GNU_implicit_pointer:
22868       return valx1->val_class == dw_val_class_die_ref
22869              && valx1->val_class == valy1->val_class
22870              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22871              && valx2->v.val_int == valy2->v.val_int;
22872     default:
22873       /* Other codes have no operands.  */
22874       return true;
22875     }
22876 }
22877
22878 /* Return true if DWARF location expressions X and Y are the same.  */
22879
22880 static inline bool
22881 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22882 {
22883   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22884     if (x->dw_loc_opc != y->dw_loc_opc
22885         || x->dtprel != y->dtprel
22886         || !compare_loc_operands (x, y))
22887       break;
22888   return x == NULL && y == NULL;
22889 }
22890
22891 /* Return precomputed hash of location list X.  */
22892
22893 static hashval_t
22894 loc_list_hash (const void *x)
22895 {
22896   return ((const struct dw_loc_list_struct *) x)->hash;
22897 }
22898
22899 /* Return 1 if location lists X and Y are the same.  */
22900
22901 static int
22902 loc_list_eq (const void *x, const void *y)
22903 {
22904   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22905   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22906   if (a == b)
22907     return 1;
22908   if (a->hash != b->hash)
22909     return 0;
22910   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22911     if (strcmp (a->begin, b->begin) != 0
22912         || strcmp (a->end, b->end) != 0
22913         || (a->section == NULL) != (b->section == NULL)
22914         || (a->section && strcmp (a->section, b->section) != 0)
22915         || !compare_locs (a->expr, b->expr))
22916       break;
22917   return a == NULL && b == NULL;
22918 }
22919
22920 /* Recursively optimize location lists referenced from DIE
22921    children and share them whenever possible.  */
22922
22923 static void
22924 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22925 {
22926   dw_die_ref c;
22927   dw_attr_ref a;
22928   unsigned ix;
22929   void **slot;
22930
22931   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22932     if (AT_class (a) == dw_val_class_loc_list)
22933       {
22934         dw_loc_list_ref list = AT_loc_list (a);
22935         /* TODO: perform some optimizations here, before hashing
22936            it and storing into the hash table.  */
22937         hash_loc_list (list);
22938         slot = htab_find_slot_with_hash (htab, list, list->hash,
22939                                          INSERT);
22940         if (*slot == NULL)
22941           *slot = (void *) list;
22942         else
22943           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22944       }
22945
22946   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22947 }
22948
22949 /* Optimize location lists referenced from DIE
22950    children and share them whenever possible.  */
22951
22952 static void
22953 optimize_location_lists (dw_die_ref die)
22954 {
22955   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22956   optimize_location_lists_1 (die, htab);
22957   htab_delete (htab);
22958 }
22959 \f
22960 /* Output stuff that dwarf requires at the end of every file,
22961    and generate the DWARF-2 debugging info.  */
22962
22963 static void
22964 dwarf2out_finish (const char *filename)
22965 {
22966   limbo_die_node *node, *next_node;
22967   comdat_type_node *ctnode;
22968   htab_t comdat_type_table;
22969   unsigned int i;
22970
22971   gen_remaining_tmpl_value_param_die_attribute ();
22972
22973   /* Add the name for the main input file now.  We delayed this from
22974      dwarf2out_init to avoid complications with PCH.  */
22975   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22976   if (!IS_ABSOLUTE_PATH (filename))
22977     add_comp_dir_attribute (comp_unit_die ());
22978   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22979     {
22980       bool p = false;
22981       htab_traverse (file_table, file_table_relative_p, &p);
22982       if (p)
22983         add_comp_dir_attribute (comp_unit_die ());
22984     }
22985
22986   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22987     {
22988       add_location_or_const_value_attribute (
22989         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22990         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22991         DW_AT_location);
22992     }
22993
22994   /* Traverse the limbo die list, and add parent/child links.  The only
22995      dies without parents that should be here are concrete instances of
22996      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22997      For concrete instances, we can get the parent die from the abstract
22998      instance.  */
22999   for (node = limbo_die_list; node; node = next_node)
23000     {
23001       dw_die_ref die = node->die;
23002       next_node = node->next;
23003
23004       if (die->die_parent == NULL)
23005         {
23006           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23007
23008           if (origin)
23009             add_child_die (origin->die_parent, die);
23010           else if (is_cu_die (die))
23011             ;
23012           else if (seen_error ())
23013             /* It's OK to be confused by errors in the input.  */
23014             add_child_die (comp_unit_die (), die);
23015           else
23016             {
23017               /* In certain situations, the lexical block containing a
23018                  nested function can be optimized away, which results
23019                  in the nested function die being orphaned.  Likewise
23020                  with the return type of that nested function.  Force
23021                  this to be a child of the containing function.
23022
23023                  It may happen that even the containing function got fully
23024                  inlined and optimized out.  In that case we are lost and
23025                  assign the empty child.  This should not be big issue as
23026                  the function is likely unreachable too.  */
23027               tree context = NULL_TREE;
23028
23029               gcc_assert (node->created_for);
23030
23031               if (DECL_P (node->created_for))
23032                 context = DECL_CONTEXT (node->created_for);
23033               else if (TYPE_P (node->created_for))
23034                 context = TYPE_CONTEXT (node->created_for);
23035
23036               gcc_assert (context
23037                           && (TREE_CODE (context) == FUNCTION_DECL
23038                               || TREE_CODE (context) == NAMESPACE_DECL));
23039
23040               origin = lookup_decl_die (context);
23041               if (origin)
23042                 add_child_die (origin, die);
23043               else
23044                 add_child_die (comp_unit_die (), die);
23045             }
23046         }
23047     }
23048
23049   limbo_die_list = NULL;
23050
23051   resolve_addr (comp_unit_die ());
23052
23053   for (node = deferred_asm_name; node; node = node->next)
23054     {
23055       tree decl = node->created_for;
23056       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23057         {
23058           add_linkage_attr (node->die, decl);
23059           move_linkage_attr (node->die);
23060         }
23061     }
23062
23063   deferred_asm_name = NULL;
23064
23065   /* Walk through the list of incomplete types again, trying once more to
23066      emit full debugging info for them.  */
23067   retry_incomplete_types ();
23068
23069   if (flag_eliminate_unused_debug_types)
23070     prune_unused_types ();
23071
23072   /* Generate separate CUs for each of the include files we've seen.
23073      They will go into limbo_die_list.  */
23074   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23075     break_out_includes (comp_unit_die ());
23076
23077   /* Generate separate COMDAT sections for type DIEs. */
23078   if (dwarf_version >= 4)
23079     {
23080       break_out_comdat_types (comp_unit_die ());
23081
23082       /* Each new type_unit DIE was added to the limbo die list when created.
23083          Since these have all been added to comdat_type_list, clear the
23084          limbo die list.  */
23085       limbo_die_list = NULL;
23086
23087       /* For each new comdat type unit, copy declarations for incomplete
23088          types to make the new unit self-contained (i.e., no direct
23089          references to the main compile unit).  */
23090       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23091         copy_decls_for_unworthy_types (ctnode->root_die);
23092       copy_decls_for_unworthy_types (comp_unit_die ());
23093
23094       /* In the process of copying declarations from one unit to another,
23095          we may have left some declarations behind that are no longer
23096          referenced.  Prune them.  */
23097       prune_unused_types ();
23098     }
23099
23100   /* Traverse the DIE's and add add sibling attributes to those DIE's
23101      that have children.  */
23102   add_sibling_attributes (comp_unit_die ());
23103   for (node = limbo_die_list; node; node = node->next)
23104     add_sibling_attributes (node->die);
23105   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23106     add_sibling_attributes (ctnode->root_die);
23107
23108   /* Output a terminator label for the .text section.  */
23109   switch_to_section (text_section);
23110   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23111   if (cold_text_section)
23112     {
23113       switch_to_section (cold_text_section);
23114       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23115     }
23116
23117   /* We can only use the low/high_pc attributes if all of the code was
23118      in .text.  */
23119   if (!have_multiple_function_sections
23120       || !(dwarf_version >= 3 || !dwarf_strict))
23121     {
23122       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23123       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23124     }
23125
23126   else
23127     {
23128       unsigned fde_idx = 0;
23129       bool range_list_added = false;
23130
23131       /* We need to give .debug_loc and .debug_ranges an appropriate
23132          "base address".  Use zero so that these addresses become
23133          absolute.  Historically, we've emitted the unexpected
23134          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23135          Emit both to give time for other tools to adapt.  */
23136       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23137       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23138
23139       if (text_section_used)
23140         add_ranges_by_labels (comp_unit_die (), text_section_label,
23141                               text_end_label, &range_list_added);
23142       if (flag_reorder_blocks_and_partition && cold_text_section_used)
23143         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23144                               cold_end_label, &range_list_added);
23145
23146       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23147         {
23148           dw_fde_ref fde = &fde_table[fde_idx];
23149
23150           if (fde->dw_fde_switched_sections)
23151             {
23152               if (!fde->in_std_section)
23153                 add_ranges_by_labels (comp_unit_die (),
23154                                       fde->dw_fde_hot_section_label,
23155                                       fde->dw_fde_hot_section_end_label,
23156                                       &range_list_added);
23157               if (!fde->cold_in_std_section)
23158                 add_ranges_by_labels (comp_unit_die (),
23159                                       fde->dw_fde_unlikely_section_label,
23160                                       fde->dw_fde_unlikely_section_end_label,
23161                                       &range_list_added);
23162             }
23163           else if (!fde->in_std_section)
23164             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23165                                   fde->dw_fde_end, &range_list_added);
23166         }
23167
23168       if (range_list_added)
23169         add_ranges (NULL);
23170     }
23171
23172   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23173     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23174                     debug_line_section_label);
23175
23176   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23177     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23178
23179   if (have_location_lists)
23180     optimize_location_lists (comp_unit_die ());
23181
23182   /* Output all of the compilation units.  We put the main one last so that
23183      the offsets are available to output_pubnames.  */
23184   for (node = limbo_die_list; node; node = node->next)
23185     output_comp_unit (node->die, 0);
23186
23187   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23188   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23189     {
23190       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23191
23192       /* Don't output duplicate types.  */
23193       if (*slot != HTAB_EMPTY_ENTRY)
23194         continue;
23195
23196       /* Add a pointer to the line table for the main compilation unit
23197          so that the debugger can make sense of DW_AT_decl_file
23198          attributes.  */
23199       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23200         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23201                         debug_line_section_label);
23202
23203       output_comdat_type_unit (ctnode);
23204       *slot = ctnode;
23205     }
23206   htab_delete (comdat_type_table);
23207
23208   /* Output the main compilation unit if non-empty or if .debug_macinfo
23209      will be emitted.  */
23210   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23211
23212   /* Output the abbreviation table.  */
23213   switch_to_section (debug_abbrev_section);
23214   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23215   output_abbrev_section ();
23216
23217   /* Output location list section if necessary.  */
23218   if (have_location_lists)
23219     {
23220       /* Output the location lists info.  */
23221       switch_to_section (debug_loc_section);
23222       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23223                                    DEBUG_LOC_SECTION_LABEL, 0);
23224       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23225       output_location_lists (comp_unit_die ());
23226     }
23227
23228   /* Output public names table if necessary.  */
23229   if (!VEC_empty (pubname_entry, pubname_table))
23230     {
23231       gcc_assert (info_section_emitted);
23232       switch_to_section (debug_pubnames_section);
23233       output_pubnames (pubname_table);
23234     }
23235
23236   /* Output public types table if necessary.  */
23237   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23238      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23239      simply won't look for the section.  */
23240   if (!VEC_empty (pubname_entry, pubtype_table))
23241     {
23242       bool empty = false;
23243       
23244       if (flag_eliminate_unused_debug_types)
23245         {
23246           /* The pubtypes table might be emptied by pruning unused items.  */
23247           unsigned i;
23248           pubname_ref p;
23249           empty = true;
23250           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23251             if (p->die->die_offset != 0)
23252               {
23253                 empty = false;
23254                 break;
23255               }
23256         }
23257       if (!empty)
23258         {
23259           gcc_assert (info_section_emitted);
23260           switch_to_section (debug_pubtypes_section);
23261           output_pubnames (pubtype_table);
23262         }
23263     }
23264
23265   /* Output direct and virtual call tables if necessary.  */
23266   if (!VEC_empty (dcall_entry, dcall_table))
23267     {
23268       switch_to_section (debug_dcall_section);
23269       output_dcall_table ();
23270     }
23271   if (!VEC_empty (vcall_entry, vcall_table))
23272     {
23273       switch_to_section (debug_vcall_section);
23274       output_vcall_table ();
23275     }
23276
23277   /* Output the address range information.  We only put functions in the arange
23278      table, so don't write it out if we don't have any.  */
23279   if (fde_table_in_use)
23280     {
23281       switch_to_section (debug_aranges_section);
23282       output_aranges ();
23283     }
23284
23285   /* Output ranges section if necessary.  */
23286   if (ranges_table_in_use)
23287     {
23288       switch_to_section (debug_ranges_section);
23289       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23290       output_ranges ();
23291     }
23292
23293   /* Output the source line correspondence table.  We must do this
23294      even if there is no line information.  Otherwise, on an empty
23295      translation unit, we will generate a present, but empty,
23296      .debug_info section.  IRIX 6.5 `nm' will then complain when
23297      examining the file.  This is done late so that any filenames
23298      used by the debug_info section are marked as 'used'.  */
23299   switch_to_section (debug_line_section);
23300   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23301   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23302     output_line_info ();
23303
23304   /* Have to end the macro section.  */
23305   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23306     {
23307       switch_to_section (debug_macinfo_section);
23308       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23309       if (!VEC_empty (macinfo_entry, macinfo_table))
23310         output_macinfo ();
23311       dw2_asm_output_data (1, 0, "End compilation unit");
23312     }
23313
23314   /* If we emitted any DW_FORM_strp form attribute, output the string
23315      table too.  */
23316   if (debug_str_hash)
23317     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23318 }
23319
23320 #include "gt-dwarf2out.h"