OSDN Git Service

Fix for PR debug/45088
[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 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 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 dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6268 static void equate_type_number_to_die (tree, dw_die_ref);
6269 static hashval_t decl_die_table_hash (const void *);
6270 static int decl_die_table_eq (const void *, const void *);
6271 static dw_die_ref lookup_decl_die (tree);
6272 static hashval_t common_block_die_table_hash (const void *);
6273 static int common_block_die_table_eq (const void *, const void *);
6274 static hashval_t decl_loc_table_hash (const void *);
6275 static int decl_loc_table_eq (const void *, const void *);
6276 static var_loc_list *lookup_decl_loc (const_tree);
6277 static void equate_decl_number_to_die (tree, dw_die_ref);
6278 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6279 static void print_spaces (FILE *);
6280 static void print_die (dw_die_ref, FILE *);
6281 static void print_dwarf_line_table (FILE *);
6282 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6283 static dw_die_ref pop_compile_unit (dw_die_ref);
6284 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6285 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6286 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6287 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6288 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6289 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6290 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6291                                    struct md5_ctx *, int *);
6292 struct checksum_attributes;
6293 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6294 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6295 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6296 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6297 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6298 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6299 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6300 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6301 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6302 static void compute_section_prefix (dw_die_ref);
6303 static int is_type_die (dw_die_ref);
6304 static int is_comdat_die (dw_die_ref);
6305 static int is_symbol_die (dw_die_ref);
6306 static void assign_symbol_names (dw_die_ref);
6307 static void break_out_includes (dw_die_ref);
6308 static int is_declaration_die (dw_die_ref);
6309 static int should_move_die_to_comdat (dw_die_ref);
6310 static dw_die_ref clone_as_declaration (dw_die_ref);
6311 static dw_die_ref clone_die (dw_die_ref);
6312 static dw_die_ref clone_tree (dw_die_ref);
6313 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6314 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6315 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6316 static dw_die_ref generate_skeleton (dw_die_ref);
6317 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6318                                                          dw_die_ref);
6319 static void break_out_comdat_types (dw_die_ref);
6320 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6321 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6322 static void copy_decls_for_unworthy_types (dw_die_ref);
6323
6324 static hashval_t htab_cu_hash (const void *);
6325 static int htab_cu_eq (const void *, const void *);
6326 static void htab_cu_del (void *);
6327 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6328 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6329 static void add_sibling_attributes (dw_die_ref);
6330 static void build_abbrev_table (dw_die_ref);
6331 static void output_location_lists (dw_die_ref);
6332 static int constant_size (unsigned HOST_WIDE_INT);
6333 static unsigned long size_of_die (dw_die_ref);
6334 static void calc_die_sizes (dw_die_ref);
6335 static void mark_dies (dw_die_ref);
6336 static void unmark_dies (dw_die_ref);
6337 static void unmark_all_dies (dw_die_ref);
6338 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6339 static unsigned long size_of_aranges (void);
6340 static enum dwarf_form value_format (dw_attr_ref);
6341 static void output_value_format (dw_attr_ref);
6342 static void output_abbrev_section (void);
6343 static void output_die_symbol (dw_die_ref);
6344 static void output_die (dw_die_ref);
6345 static void output_compilation_unit_header (void);
6346 static void output_comp_unit (dw_die_ref, int);
6347 static void output_comdat_type_unit (comdat_type_node *);
6348 static const char *dwarf2_name (tree, int);
6349 static void add_pubname (tree, dw_die_ref);
6350 static void add_pubname_string (const char *, dw_die_ref);
6351 static void add_pubtype (tree, dw_die_ref);
6352 static void output_pubnames (VEC (pubname_entry,gc) *);
6353 static void add_arange (tree, dw_die_ref);
6354 static void output_aranges (void);
6355 static unsigned int add_ranges_num (int);
6356 static unsigned int add_ranges (const_tree);
6357 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6358                                   bool *);
6359 static void output_ranges (void);
6360 static void output_line_info (void);
6361 static void output_file_names (void);
6362 static dw_die_ref base_type_die (tree);
6363 static int is_base_type (tree);
6364 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6365 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6366 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6367 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6368 static int type_is_enum (const_tree);
6369 static unsigned int dbx_reg_number (const_rtx);
6370 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6371 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6372 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6373                                                 enum var_init_status);
6374 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6375                                                      enum var_init_status);
6376 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6377                                          enum var_init_status);
6378 static int is_based_loc (const_rtx);
6379 static int resolve_one_addr (rtx *, void *);
6380 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6381                                                enum var_init_status);
6382 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6383                                         enum var_init_status);
6384 static dw_loc_list_ref loc_list_from_tree (tree, int);
6385 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6386 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6387 static tree field_type (const_tree);
6388 static unsigned int simple_type_align_in_bits (const_tree);
6389 static unsigned int simple_decl_align_in_bits (const_tree);
6390 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6391 static HOST_WIDE_INT field_byte_offset (const_tree);
6392 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6393                                          dw_loc_list_ref);
6394 static void add_data_member_location_attribute (dw_die_ref, tree);
6395 static bool add_const_value_attribute (dw_die_ref, rtx);
6396 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6397 static void insert_double (double_int, unsigned char *);
6398 static void insert_float (const_rtx, unsigned char *);
6399 static rtx rtl_for_decl_location (tree);
6400 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6401                                                    enum dwarf_attribute);
6402 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6403 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6404 static void add_name_attribute (dw_die_ref, const char *);
6405 static void add_comp_dir_attribute (dw_die_ref);
6406 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6407 static void add_subscript_info (dw_die_ref, tree, bool);
6408 static void add_byte_size_attribute (dw_die_ref, tree);
6409 static void add_bit_offset_attribute (dw_die_ref, tree);
6410 static void add_bit_size_attribute (dw_die_ref, tree);
6411 static void add_prototyped_attribute (dw_die_ref, tree);
6412 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6413 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6414 static void add_src_coords_attributes (dw_die_ref, tree);
6415 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6416 static void push_decl_scope (tree);
6417 static void pop_decl_scope (void);
6418 static dw_die_ref scope_die_for (tree, dw_die_ref);
6419 static inline int local_scope_p (dw_die_ref);
6420 static inline int class_scope_p (dw_die_ref);
6421 static inline int class_or_namespace_scope_p (dw_die_ref);
6422 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6423 static void add_calling_convention_attribute (dw_die_ref, tree);
6424 static const char *type_tag (const_tree);
6425 static tree member_declared_type (const_tree);
6426 #if 0
6427 static const char *decl_start_label (tree);
6428 #endif
6429 static void gen_array_type_die (tree, dw_die_ref);
6430 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6431 #if 0
6432 static void gen_entry_point_die (tree, dw_die_ref);
6433 #endif
6434 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6435 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6436 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6437 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6438 static void gen_formal_types_die (tree, dw_die_ref);
6439 static void gen_subprogram_die (tree, dw_die_ref);
6440 static void gen_variable_die (tree, tree, dw_die_ref);
6441 static void gen_const_die (tree, dw_die_ref);
6442 static void gen_label_die (tree, dw_die_ref);
6443 static void gen_lexical_block_die (tree, dw_die_ref, int);
6444 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6445 static void gen_field_die (tree, dw_die_ref);
6446 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6447 static dw_die_ref gen_compile_unit_die (const char *);
6448 static void gen_inheritance_die (tree, tree, dw_die_ref);
6449 static void gen_member_die (tree, dw_die_ref);
6450 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6451                                                 enum debug_info_usage);
6452 static void gen_subroutine_type_die (tree, dw_die_ref);
6453 static void gen_typedef_die (tree, dw_die_ref);
6454 static void gen_type_die (tree, dw_die_ref);
6455 static void gen_block_die (tree, dw_die_ref, int);
6456 static void decls_for_scope (tree, dw_die_ref, int);
6457 static int is_redundant_typedef (const_tree);
6458 static bool is_naming_typedef_decl (const_tree);
6459 static inline dw_die_ref get_context_die (tree);
6460 static void gen_namespace_die (tree, dw_die_ref);
6461 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6462 static dw_die_ref force_decl_die (tree);
6463 static dw_die_ref force_type_die (tree);
6464 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6465 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6466 static struct dwarf_file_data * lookup_filename (const char *);
6467 static void retry_incomplete_types (void);
6468 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6469 static void gen_generic_params_dies (tree);
6470 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6471 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6472 static void splice_child_die (dw_die_ref, dw_die_ref);
6473 static int file_info_cmp (const void *, const void *);
6474 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6475                                      const char *, const char *);
6476 static void output_loc_list (dw_loc_list_ref);
6477 static char *gen_internal_sym (const char *);
6478
6479 static void prune_unmark_dies (dw_die_ref);
6480 static void prune_unused_types_mark (dw_die_ref, int);
6481 static void prune_unused_types_walk (dw_die_ref);
6482 static void prune_unused_types_walk_attribs (dw_die_ref);
6483 static void prune_unused_types_prune (dw_die_ref);
6484 static void prune_unused_types (void);
6485 static int maybe_emit_file (struct dwarf_file_data *fd);
6486 static inline const char *AT_vms_delta1 (dw_attr_ref);
6487 static inline const char *AT_vms_delta2 (dw_attr_ref);
6488 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6489                                      const char *, const char *);
6490 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6491 static void gen_remaining_tmpl_value_param_die_attribute (void);
6492
6493 /* Section names used to hold DWARF debugging information.  */
6494 #ifndef DEBUG_INFO_SECTION
6495 #define DEBUG_INFO_SECTION      ".debug_info"
6496 #endif
6497 #ifndef DEBUG_ABBREV_SECTION
6498 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6499 #endif
6500 #ifndef DEBUG_ARANGES_SECTION
6501 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6502 #endif
6503 #ifndef DEBUG_MACINFO_SECTION
6504 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6505 #endif
6506 #ifndef DEBUG_LINE_SECTION
6507 #define DEBUG_LINE_SECTION      ".debug_line"
6508 #endif
6509 #ifndef DEBUG_LOC_SECTION
6510 #define DEBUG_LOC_SECTION       ".debug_loc"
6511 #endif
6512 #ifndef DEBUG_PUBNAMES_SECTION
6513 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6514 #endif
6515 #ifndef DEBUG_PUBTYPES_SECTION
6516 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6517 #endif
6518 #ifndef DEBUG_DCALL_SECTION
6519 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6520 #endif
6521 #ifndef DEBUG_VCALL_SECTION
6522 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6523 #endif
6524 #ifndef DEBUG_STR_SECTION
6525 #define DEBUG_STR_SECTION       ".debug_str"
6526 #endif
6527 #ifndef DEBUG_RANGES_SECTION
6528 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6529 #endif
6530
6531 /* Standard ELF section names for compiled code and data.  */
6532 #ifndef TEXT_SECTION_NAME
6533 #define TEXT_SECTION_NAME       ".text"
6534 #endif
6535
6536 /* Section flags for .debug_str section.  */
6537 #define DEBUG_STR_SECTION_FLAGS \
6538   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6539    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6540    : SECTION_DEBUG)
6541
6542 /* Labels we insert at beginning sections we can reference instead of
6543    the section names themselves.  */
6544
6545 #ifndef TEXT_SECTION_LABEL
6546 #define TEXT_SECTION_LABEL              "Ltext"
6547 #endif
6548 #ifndef COLD_TEXT_SECTION_LABEL
6549 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6550 #endif
6551 #ifndef DEBUG_LINE_SECTION_LABEL
6552 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6553 #endif
6554 #ifndef DEBUG_INFO_SECTION_LABEL
6555 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6556 #endif
6557 #ifndef DEBUG_ABBREV_SECTION_LABEL
6558 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6559 #endif
6560 #ifndef DEBUG_LOC_SECTION_LABEL
6561 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6562 #endif
6563 #ifndef DEBUG_RANGES_SECTION_LABEL
6564 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6565 #endif
6566 #ifndef DEBUG_MACINFO_SECTION_LABEL
6567 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6568 #endif
6569
6570
6571 /* Definitions of defaults for formats and names of various special
6572    (artificial) labels which may be generated within this file (when the -g
6573    options is used and DWARF2_DEBUGGING_INFO is in effect.
6574    If necessary, these may be overridden from within the tm.h file, but
6575    typically, overriding these defaults is unnecessary.  */
6576
6577 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6578 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6579 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6580 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6581 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6582 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6583 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6584 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6585 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6586 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6587
6588 #ifndef TEXT_END_LABEL
6589 #define TEXT_END_LABEL          "Letext"
6590 #endif
6591 #ifndef COLD_END_LABEL
6592 #define COLD_END_LABEL          "Letext_cold"
6593 #endif
6594 #ifndef BLOCK_BEGIN_LABEL
6595 #define BLOCK_BEGIN_LABEL       "LBB"
6596 #endif
6597 #ifndef BLOCK_END_LABEL
6598 #define BLOCK_END_LABEL         "LBE"
6599 #endif
6600 #ifndef LINE_CODE_LABEL
6601 #define LINE_CODE_LABEL         "LM"
6602 #endif
6603 #ifndef SEPARATE_LINE_CODE_LABEL
6604 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6605 #endif
6606
6607 \f
6608 /* Return the root of the DIE's built for the current compilation unit.  */
6609 static dw_die_ref
6610 comp_unit_die (void)
6611 {
6612   if (!single_comp_unit_die)
6613     single_comp_unit_die = gen_compile_unit_die (NULL);
6614   return single_comp_unit_die;
6615 }
6616
6617 /* We allow a language front-end to designate a function that is to be
6618    called to "demangle" any name before it is put into a DIE.  */
6619
6620 static const char *(*demangle_name_func) (const char *);
6621
6622 void
6623 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6624 {
6625   demangle_name_func = func;
6626 }
6627
6628 /* Test if rtl node points to a pseudo register.  */
6629
6630 static inline int
6631 is_pseudo_reg (const_rtx rtl)
6632 {
6633   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6634           || (GET_CODE (rtl) == SUBREG
6635               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6636 }
6637
6638 /* Return a reference to a type, with its const and volatile qualifiers
6639    removed.  */
6640
6641 static inline tree
6642 type_main_variant (tree type)
6643 {
6644   type = TYPE_MAIN_VARIANT (type);
6645
6646   /* ??? There really should be only one main variant among any group of
6647      variants of a given type (and all of the MAIN_VARIANT values for all
6648      members of the group should point to that one type) but sometimes the C
6649      front-end messes this up for array types, so we work around that bug
6650      here.  */
6651   if (TREE_CODE (type) == ARRAY_TYPE)
6652     while (type != TYPE_MAIN_VARIANT (type))
6653       type = TYPE_MAIN_VARIANT (type);
6654
6655   return type;
6656 }
6657
6658 /* Return nonzero if the given type node represents a tagged type.  */
6659
6660 static inline int
6661 is_tagged_type (const_tree type)
6662 {
6663   enum tree_code code = TREE_CODE (type);
6664
6665   return (code == RECORD_TYPE || code == UNION_TYPE
6666           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6667 }
6668
6669 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6670
6671 static void
6672 get_ref_die_offset_label (char *label, dw_die_ref ref)
6673 {
6674   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6675 }
6676
6677 /* Convert a DIE tag into its string name.  */
6678
6679 static const char *
6680 dwarf_tag_name (unsigned int tag)
6681 {
6682   switch (tag)
6683     {
6684     case DW_TAG_padding:
6685       return "DW_TAG_padding";
6686     case DW_TAG_array_type:
6687       return "DW_TAG_array_type";
6688     case DW_TAG_class_type:
6689       return "DW_TAG_class_type";
6690     case DW_TAG_entry_point:
6691       return "DW_TAG_entry_point";
6692     case DW_TAG_enumeration_type:
6693       return "DW_TAG_enumeration_type";
6694     case DW_TAG_formal_parameter:
6695       return "DW_TAG_formal_parameter";
6696     case DW_TAG_imported_declaration:
6697       return "DW_TAG_imported_declaration";
6698     case DW_TAG_label:
6699       return "DW_TAG_label";
6700     case DW_TAG_lexical_block:
6701       return "DW_TAG_lexical_block";
6702     case DW_TAG_member:
6703       return "DW_TAG_member";
6704     case DW_TAG_pointer_type:
6705       return "DW_TAG_pointer_type";
6706     case DW_TAG_reference_type:
6707       return "DW_TAG_reference_type";
6708     case DW_TAG_compile_unit:
6709       return "DW_TAG_compile_unit";
6710     case DW_TAG_string_type:
6711       return "DW_TAG_string_type";
6712     case DW_TAG_structure_type:
6713       return "DW_TAG_structure_type";
6714     case DW_TAG_subroutine_type:
6715       return "DW_TAG_subroutine_type";
6716     case DW_TAG_typedef:
6717       return "DW_TAG_typedef";
6718     case DW_TAG_union_type:
6719       return "DW_TAG_union_type";
6720     case DW_TAG_unspecified_parameters:
6721       return "DW_TAG_unspecified_parameters";
6722     case DW_TAG_variant:
6723       return "DW_TAG_variant";
6724     case DW_TAG_common_block:
6725       return "DW_TAG_common_block";
6726     case DW_TAG_common_inclusion:
6727       return "DW_TAG_common_inclusion";
6728     case DW_TAG_inheritance:
6729       return "DW_TAG_inheritance";
6730     case DW_TAG_inlined_subroutine:
6731       return "DW_TAG_inlined_subroutine";
6732     case DW_TAG_module:
6733       return "DW_TAG_module";
6734     case DW_TAG_ptr_to_member_type:
6735       return "DW_TAG_ptr_to_member_type";
6736     case DW_TAG_set_type:
6737       return "DW_TAG_set_type";
6738     case DW_TAG_subrange_type:
6739       return "DW_TAG_subrange_type";
6740     case DW_TAG_with_stmt:
6741       return "DW_TAG_with_stmt";
6742     case DW_TAG_access_declaration:
6743       return "DW_TAG_access_declaration";
6744     case DW_TAG_base_type:
6745       return "DW_TAG_base_type";
6746     case DW_TAG_catch_block:
6747       return "DW_TAG_catch_block";
6748     case DW_TAG_const_type:
6749       return "DW_TAG_const_type";
6750     case DW_TAG_constant:
6751       return "DW_TAG_constant";
6752     case DW_TAG_enumerator:
6753       return "DW_TAG_enumerator";
6754     case DW_TAG_file_type:
6755       return "DW_TAG_file_type";
6756     case DW_TAG_friend:
6757       return "DW_TAG_friend";
6758     case DW_TAG_namelist:
6759       return "DW_TAG_namelist";
6760     case DW_TAG_namelist_item:
6761       return "DW_TAG_namelist_item";
6762     case DW_TAG_packed_type:
6763       return "DW_TAG_packed_type";
6764     case DW_TAG_subprogram:
6765       return "DW_TAG_subprogram";
6766     case DW_TAG_template_type_param:
6767       return "DW_TAG_template_type_param";
6768     case DW_TAG_template_value_param:
6769       return "DW_TAG_template_value_param";
6770     case DW_TAG_thrown_type:
6771       return "DW_TAG_thrown_type";
6772     case DW_TAG_try_block:
6773       return "DW_TAG_try_block";
6774     case DW_TAG_variant_part:
6775       return "DW_TAG_variant_part";
6776     case DW_TAG_variable:
6777       return "DW_TAG_variable";
6778     case DW_TAG_volatile_type:
6779       return "DW_TAG_volatile_type";
6780     case DW_TAG_dwarf_procedure:
6781       return "DW_TAG_dwarf_procedure";
6782     case DW_TAG_restrict_type:
6783       return "DW_TAG_restrict_type";
6784     case DW_TAG_interface_type:
6785       return "DW_TAG_interface_type";
6786     case DW_TAG_namespace:
6787       return "DW_TAG_namespace";
6788     case DW_TAG_imported_module:
6789       return "DW_TAG_imported_module";
6790     case DW_TAG_unspecified_type:
6791       return "DW_TAG_unspecified_type";
6792     case DW_TAG_partial_unit:
6793       return "DW_TAG_partial_unit";
6794     case DW_TAG_imported_unit:
6795       return "DW_TAG_imported_unit";
6796     case DW_TAG_condition:
6797       return "DW_TAG_condition";
6798     case DW_TAG_shared_type:
6799       return "DW_TAG_shared_type";
6800     case DW_TAG_type_unit:
6801       return "DW_TAG_type_unit";
6802     case DW_TAG_rvalue_reference_type:
6803       return "DW_TAG_rvalue_reference_type";
6804     case DW_TAG_template_alias:
6805       return "DW_TAG_template_alias";
6806     case DW_TAG_GNU_template_parameter_pack:
6807       return "DW_TAG_GNU_template_parameter_pack";
6808     case DW_TAG_GNU_formal_parameter_pack:
6809       return "DW_TAG_GNU_formal_parameter_pack";
6810     case DW_TAG_MIPS_loop:
6811       return "DW_TAG_MIPS_loop";
6812     case DW_TAG_format_label:
6813       return "DW_TAG_format_label";
6814     case DW_TAG_function_template:
6815       return "DW_TAG_function_template";
6816     case DW_TAG_class_template:
6817       return "DW_TAG_class_template";
6818     case DW_TAG_GNU_BINCL:
6819       return "DW_TAG_GNU_BINCL";
6820     case DW_TAG_GNU_EINCL:
6821       return "DW_TAG_GNU_EINCL";
6822     case DW_TAG_GNU_template_template_param:
6823       return "DW_TAG_GNU_template_template_param";
6824     default:
6825       return "DW_TAG_<unknown>";
6826     }
6827 }
6828
6829 /* Convert a DWARF attribute code into its string name.  */
6830
6831 static const char *
6832 dwarf_attr_name (unsigned int attr)
6833 {
6834   switch (attr)
6835     {
6836     case DW_AT_sibling:
6837       return "DW_AT_sibling";
6838     case DW_AT_location:
6839       return "DW_AT_location";
6840     case DW_AT_name:
6841       return "DW_AT_name";
6842     case DW_AT_ordering:
6843       return "DW_AT_ordering";
6844     case DW_AT_subscr_data:
6845       return "DW_AT_subscr_data";
6846     case DW_AT_byte_size:
6847       return "DW_AT_byte_size";
6848     case DW_AT_bit_offset:
6849       return "DW_AT_bit_offset";
6850     case DW_AT_bit_size:
6851       return "DW_AT_bit_size";
6852     case DW_AT_element_list:
6853       return "DW_AT_element_list";
6854     case DW_AT_stmt_list:
6855       return "DW_AT_stmt_list";
6856     case DW_AT_low_pc:
6857       return "DW_AT_low_pc";
6858     case DW_AT_high_pc:
6859       return "DW_AT_high_pc";
6860     case DW_AT_language:
6861       return "DW_AT_language";
6862     case DW_AT_member:
6863       return "DW_AT_member";
6864     case DW_AT_discr:
6865       return "DW_AT_discr";
6866     case DW_AT_discr_value:
6867       return "DW_AT_discr_value";
6868     case DW_AT_visibility:
6869       return "DW_AT_visibility";
6870     case DW_AT_import:
6871       return "DW_AT_import";
6872     case DW_AT_string_length:
6873       return "DW_AT_string_length";
6874     case DW_AT_common_reference:
6875       return "DW_AT_common_reference";
6876     case DW_AT_comp_dir:
6877       return "DW_AT_comp_dir";
6878     case DW_AT_const_value:
6879       return "DW_AT_const_value";
6880     case DW_AT_containing_type:
6881       return "DW_AT_containing_type";
6882     case DW_AT_default_value:
6883       return "DW_AT_default_value";
6884     case DW_AT_inline:
6885       return "DW_AT_inline";
6886     case DW_AT_is_optional:
6887       return "DW_AT_is_optional";
6888     case DW_AT_lower_bound:
6889       return "DW_AT_lower_bound";
6890     case DW_AT_producer:
6891       return "DW_AT_producer";
6892     case DW_AT_prototyped:
6893       return "DW_AT_prototyped";
6894     case DW_AT_return_addr:
6895       return "DW_AT_return_addr";
6896     case DW_AT_start_scope:
6897       return "DW_AT_start_scope";
6898     case DW_AT_bit_stride:
6899       return "DW_AT_bit_stride";
6900     case DW_AT_upper_bound:
6901       return "DW_AT_upper_bound";
6902     case DW_AT_abstract_origin:
6903       return "DW_AT_abstract_origin";
6904     case DW_AT_accessibility:
6905       return "DW_AT_accessibility";
6906     case DW_AT_address_class:
6907       return "DW_AT_address_class";
6908     case DW_AT_artificial:
6909       return "DW_AT_artificial";
6910     case DW_AT_base_types:
6911       return "DW_AT_base_types";
6912     case DW_AT_calling_convention:
6913       return "DW_AT_calling_convention";
6914     case DW_AT_count:
6915       return "DW_AT_count";
6916     case DW_AT_data_member_location:
6917       return "DW_AT_data_member_location";
6918     case DW_AT_decl_column:
6919       return "DW_AT_decl_column";
6920     case DW_AT_decl_file:
6921       return "DW_AT_decl_file";
6922     case DW_AT_decl_line:
6923       return "DW_AT_decl_line";
6924     case DW_AT_declaration:
6925       return "DW_AT_declaration";
6926     case DW_AT_discr_list:
6927       return "DW_AT_discr_list";
6928     case DW_AT_encoding:
6929       return "DW_AT_encoding";
6930     case DW_AT_external:
6931       return "DW_AT_external";
6932     case DW_AT_explicit:
6933       return "DW_AT_explicit";
6934     case DW_AT_frame_base:
6935       return "DW_AT_frame_base";
6936     case DW_AT_friend:
6937       return "DW_AT_friend";
6938     case DW_AT_identifier_case:
6939       return "DW_AT_identifier_case";
6940     case DW_AT_macro_info:
6941       return "DW_AT_macro_info";
6942     case DW_AT_namelist_items:
6943       return "DW_AT_namelist_items";
6944     case DW_AT_priority:
6945       return "DW_AT_priority";
6946     case DW_AT_segment:
6947       return "DW_AT_segment";
6948     case DW_AT_specification:
6949       return "DW_AT_specification";
6950     case DW_AT_static_link:
6951       return "DW_AT_static_link";
6952     case DW_AT_type:
6953       return "DW_AT_type";
6954     case DW_AT_use_location:
6955       return "DW_AT_use_location";
6956     case DW_AT_variable_parameter:
6957       return "DW_AT_variable_parameter";
6958     case DW_AT_virtuality:
6959       return "DW_AT_virtuality";
6960     case DW_AT_vtable_elem_location:
6961       return "DW_AT_vtable_elem_location";
6962
6963     case DW_AT_allocated:
6964       return "DW_AT_allocated";
6965     case DW_AT_associated:
6966       return "DW_AT_associated";
6967     case DW_AT_data_location:
6968       return "DW_AT_data_location";
6969     case DW_AT_byte_stride:
6970       return "DW_AT_byte_stride";
6971     case DW_AT_entry_pc:
6972       return "DW_AT_entry_pc";
6973     case DW_AT_use_UTF8:
6974       return "DW_AT_use_UTF8";
6975     case DW_AT_extension:
6976       return "DW_AT_extension";
6977     case DW_AT_ranges:
6978       return "DW_AT_ranges";
6979     case DW_AT_trampoline:
6980       return "DW_AT_trampoline";
6981     case DW_AT_call_column:
6982       return "DW_AT_call_column";
6983     case DW_AT_call_file:
6984       return "DW_AT_call_file";
6985     case DW_AT_call_line:
6986       return "DW_AT_call_line";
6987     case DW_AT_object_pointer:
6988       return "DW_AT_object_pointer";
6989
6990     case DW_AT_signature:
6991       return "DW_AT_signature";
6992     case DW_AT_main_subprogram:
6993       return "DW_AT_main_subprogram";
6994     case DW_AT_data_bit_offset:
6995       return "DW_AT_data_bit_offset";
6996     case DW_AT_const_expr:
6997       return "DW_AT_const_expr";
6998     case DW_AT_enum_class:
6999       return "DW_AT_enum_class";
7000     case DW_AT_linkage_name:
7001       return "DW_AT_linkage_name";
7002
7003     case DW_AT_MIPS_fde:
7004       return "DW_AT_MIPS_fde";
7005     case DW_AT_MIPS_loop_begin:
7006       return "DW_AT_MIPS_loop_begin";
7007     case DW_AT_MIPS_tail_loop_begin:
7008       return "DW_AT_MIPS_tail_loop_begin";
7009     case DW_AT_MIPS_epilog_begin:
7010       return "DW_AT_MIPS_epilog_begin";
7011 #if VMS_DEBUGGING_INFO
7012     case DW_AT_HP_prologue:
7013       return "DW_AT_HP_prologue";
7014 #else
7015     case DW_AT_MIPS_loop_unroll_factor:
7016       return "DW_AT_MIPS_loop_unroll_factor";
7017 #endif
7018     case DW_AT_MIPS_software_pipeline_depth:
7019       return "DW_AT_MIPS_software_pipeline_depth";
7020     case DW_AT_MIPS_linkage_name:
7021       return "DW_AT_MIPS_linkage_name";
7022 #if VMS_DEBUGGING_INFO
7023     case DW_AT_HP_epilogue:
7024       return "DW_AT_HP_epilogue";
7025 #else
7026     case DW_AT_MIPS_stride:
7027       return "DW_AT_MIPS_stride";
7028 #endif
7029     case DW_AT_MIPS_abstract_name:
7030       return "DW_AT_MIPS_abstract_name";
7031     case DW_AT_MIPS_clone_origin:
7032       return "DW_AT_MIPS_clone_origin";
7033     case DW_AT_MIPS_has_inlines:
7034       return "DW_AT_MIPS_has_inlines";
7035
7036     case DW_AT_sf_names:
7037       return "DW_AT_sf_names";
7038     case DW_AT_src_info:
7039       return "DW_AT_src_info";
7040     case DW_AT_mac_info:
7041       return "DW_AT_mac_info";
7042     case DW_AT_src_coords:
7043       return "DW_AT_src_coords";
7044     case DW_AT_body_begin:
7045       return "DW_AT_body_begin";
7046     case DW_AT_body_end:
7047       return "DW_AT_body_end";
7048     case DW_AT_GNU_vector:
7049       return "DW_AT_GNU_vector";
7050     case DW_AT_GNU_guarded_by:
7051       return "DW_AT_GNU_guarded_by";
7052     case DW_AT_GNU_pt_guarded_by:
7053       return "DW_AT_GNU_pt_guarded_by";
7054     case DW_AT_GNU_guarded:
7055       return "DW_AT_GNU_guarded";
7056     case DW_AT_GNU_pt_guarded:
7057       return "DW_AT_GNU_pt_guarded";
7058     case DW_AT_GNU_locks_excluded:
7059       return "DW_AT_GNU_locks_excluded";
7060     case DW_AT_GNU_exclusive_locks_required:
7061       return "DW_AT_GNU_exclusive_locks_required";
7062     case DW_AT_GNU_shared_locks_required:
7063       return "DW_AT_GNU_shared_locks_required";
7064     case DW_AT_GNU_odr_signature:
7065       return "DW_AT_GNU_odr_signature";
7066     case DW_AT_GNU_template_name:
7067       return "DW_AT_GNU_template_name";
7068
7069     case DW_AT_VMS_rtnbeg_pd_address:
7070       return "DW_AT_VMS_rtnbeg_pd_address";
7071
7072     default:
7073       return "DW_AT_<unknown>";
7074     }
7075 }
7076
7077 /* Convert a DWARF value form code into its string name.  */
7078
7079 static const char *
7080 dwarf_form_name (unsigned int form)
7081 {
7082   switch (form)
7083     {
7084     case DW_FORM_addr:
7085       return "DW_FORM_addr";
7086     case DW_FORM_block2:
7087       return "DW_FORM_block2";
7088     case DW_FORM_block4:
7089       return "DW_FORM_block4";
7090     case DW_FORM_data2:
7091       return "DW_FORM_data2";
7092     case DW_FORM_data4:
7093       return "DW_FORM_data4";
7094     case DW_FORM_data8:
7095       return "DW_FORM_data8";
7096     case DW_FORM_string:
7097       return "DW_FORM_string";
7098     case DW_FORM_block:
7099       return "DW_FORM_block";
7100     case DW_FORM_block1:
7101       return "DW_FORM_block1";
7102     case DW_FORM_data1:
7103       return "DW_FORM_data1";
7104     case DW_FORM_flag:
7105       return "DW_FORM_flag";
7106     case DW_FORM_sdata:
7107       return "DW_FORM_sdata";
7108     case DW_FORM_strp:
7109       return "DW_FORM_strp";
7110     case DW_FORM_udata:
7111       return "DW_FORM_udata";
7112     case DW_FORM_ref_addr:
7113       return "DW_FORM_ref_addr";
7114     case DW_FORM_ref1:
7115       return "DW_FORM_ref1";
7116     case DW_FORM_ref2:
7117       return "DW_FORM_ref2";
7118     case DW_FORM_ref4:
7119       return "DW_FORM_ref4";
7120     case DW_FORM_ref8:
7121       return "DW_FORM_ref8";
7122     case DW_FORM_ref_udata:
7123       return "DW_FORM_ref_udata";
7124     case DW_FORM_indirect:
7125       return "DW_FORM_indirect";
7126     case DW_FORM_sec_offset:
7127       return "DW_FORM_sec_offset";
7128     case DW_FORM_exprloc:
7129       return "DW_FORM_exprloc";
7130     case DW_FORM_flag_present:
7131       return "DW_FORM_flag_present";
7132     case DW_FORM_ref_sig8:
7133       return "DW_FORM_ref_sig8";
7134     default:
7135       return "DW_FORM_<unknown>";
7136     }
7137 }
7138 \f
7139 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7140    instance of an inlined instance of a decl which is local to an inline
7141    function, so we have to trace all of the way back through the origin chain
7142    to find out what sort of node actually served as the original seed for the
7143    given block.  */
7144
7145 static tree
7146 decl_ultimate_origin (const_tree decl)
7147 {
7148   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7149     return NULL_TREE;
7150
7151   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7152      nodes in the function to point to themselves; ignore that if
7153      we're trying to output the abstract instance of this function.  */
7154   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7155     return NULL_TREE;
7156
7157   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7158      most distant ancestor, this should never happen.  */
7159   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7160
7161   return DECL_ABSTRACT_ORIGIN (decl);
7162 }
7163
7164 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7165    of a virtual function may refer to a base class, so we check the 'this'
7166    parameter.  */
7167
7168 static tree
7169 decl_class_context (tree decl)
7170 {
7171   tree context = NULL_TREE;
7172
7173   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7174     context = DECL_CONTEXT (decl);
7175   else
7176     context = TYPE_MAIN_VARIANT
7177       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7178
7179   if (context && !TYPE_P (context))
7180     context = NULL_TREE;
7181
7182   return context;
7183 }
7184 \f
7185 /* Add an attribute/value pair to a DIE.  */
7186
7187 static inline void
7188 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7189 {
7190   /* Maybe this should be an assert?  */
7191   if (die == NULL)
7192     return;
7193
7194   if (die->die_attr == NULL)
7195     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7196   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7197 }
7198
7199 static inline enum dw_val_class
7200 AT_class (dw_attr_ref a)
7201 {
7202   return a->dw_attr_val.val_class;
7203 }
7204
7205 /* Add a flag value attribute to a DIE.  */
7206
7207 static inline void
7208 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7209 {
7210   dw_attr_node attr;
7211
7212   attr.dw_attr = attr_kind;
7213   attr.dw_attr_val.val_class = dw_val_class_flag;
7214   attr.dw_attr_val.v.val_flag = flag;
7215   add_dwarf_attr (die, &attr);
7216 }
7217
7218 static inline unsigned
7219 AT_flag (dw_attr_ref a)
7220 {
7221   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7222   return a->dw_attr_val.v.val_flag;
7223 }
7224
7225 /* Add a signed integer attribute value to a DIE.  */
7226
7227 static inline void
7228 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7229 {
7230   dw_attr_node attr;
7231
7232   attr.dw_attr = attr_kind;
7233   attr.dw_attr_val.val_class = dw_val_class_const;
7234   attr.dw_attr_val.v.val_int = int_val;
7235   add_dwarf_attr (die, &attr);
7236 }
7237
7238 static inline HOST_WIDE_INT
7239 AT_int (dw_attr_ref a)
7240 {
7241   gcc_assert (a && AT_class (a) == dw_val_class_const);
7242   return a->dw_attr_val.v.val_int;
7243 }
7244
7245 /* Add an unsigned integer attribute value to a DIE.  */
7246
7247 static inline void
7248 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7249                  unsigned HOST_WIDE_INT unsigned_val)
7250 {
7251   dw_attr_node attr;
7252
7253   attr.dw_attr = attr_kind;
7254   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7255   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7256   add_dwarf_attr (die, &attr);
7257 }
7258
7259 static inline unsigned HOST_WIDE_INT
7260 AT_unsigned (dw_attr_ref a)
7261 {
7262   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7263   return a->dw_attr_val.v.val_unsigned;
7264 }
7265
7266 /* Add an unsigned double integer attribute value to a DIE.  */
7267
7268 static inline void
7269 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7270                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7271 {
7272   dw_attr_node attr;
7273
7274   attr.dw_attr = attr_kind;
7275   attr.dw_attr_val.val_class = dw_val_class_const_double;
7276   attr.dw_attr_val.v.val_double.high = high;
7277   attr.dw_attr_val.v.val_double.low = low;
7278   add_dwarf_attr (die, &attr);
7279 }
7280
7281 /* Add a floating point attribute value to a DIE and return it.  */
7282
7283 static inline void
7284 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7285             unsigned int length, unsigned int elt_size, unsigned char *array)
7286 {
7287   dw_attr_node attr;
7288
7289   attr.dw_attr = attr_kind;
7290   attr.dw_attr_val.val_class = dw_val_class_vec;
7291   attr.dw_attr_val.v.val_vec.length = length;
7292   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7293   attr.dw_attr_val.v.val_vec.array = array;
7294   add_dwarf_attr (die, &attr);
7295 }
7296
7297 /* Add an 8-byte data attribute value to a DIE.  */
7298
7299 static inline void
7300 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7301               unsigned char data8[8])
7302 {
7303   dw_attr_node attr;
7304
7305   attr.dw_attr = attr_kind;
7306   attr.dw_attr_val.val_class = dw_val_class_data8;
7307   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7308   add_dwarf_attr (die, &attr);
7309 }
7310
7311 /* Hash and equality functions for debug_str_hash.  */
7312
7313 static hashval_t
7314 debug_str_do_hash (const void *x)
7315 {
7316   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7317 }
7318
7319 static int
7320 debug_str_eq (const void *x1, const void *x2)
7321 {
7322   return strcmp ((((const struct indirect_string_node *)x1)->str),
7323                  (const char *)x2) == 0;
7324 }
7325
7326 /* Add STR to the indirect string hash table.  */
7327
7328 static struct indirect_string_node *
7329 find_AT_string (const char *str)
7330 {
7331   struct indirect_string_node *node;
7332   void **slot;
7333
7334   if (! debug_str_hash)
7335     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7336                                       debug_str_eq, NULL);
7337
7338   slot = htab_find_slot_with_hash (debug_str_hash, str,
7339                                    htab_hash_string (str), INSERT);
7340   if (*slot == NULL)
7341     {
7342       node = ggc_alloc_cleared_indirect_string_node ();
7343       node->str = ggc_strdup (str);
7344       *slot = node;
7345     }
7346   else
7347     node = (struct indirect_string_node *) *slot;
7348
7349   node->refcount++;
7350   return node;
7351 }
7352
7353 /* Add a string attribute value to a DIE.  */
7354
7355 static inline void
7356 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7357 {
7358   dw_attr_node attr;
7359   struct indirect_string_node *node;
7360
7361   node = find_AT_string (str);
7362
7363   attr.dw_attr = attr_kind;
7364   attr.dw_attr_val.val_class = dw_val_class_str;
7365   attr.dw_attr_val.v.val_str = node;
7366   add_dwarf_attr (die, &attr);
7367 }
7368
7369 /* Create a label for an indirect string node, ensuring it is going to
7370    be output, unless its reference count goes down to zero.  */
7371
7372 static inline void
7373 gen_label_for_indirect_string (struct indirect_string_node *node)
7374 {
7375   char label[32];
7376
7377   if (node->label)
7378     return;
7379
7380   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7381   ++dw2_string_counter;
7382   node->label = xstrdup (label);
7383 }
7384
7385 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7386    debug string STR.  */
7387
7388 static inline rtx
7389 get_debug_string_label (const char *str)
7390 {
7391   struct indirect_string_node *node = find_AT_string (str);
7392
7393   debug_str_hash_forced = true;
7394
7395   gen_label_for_indirect_string (node);
7396
7397   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7398 }
7399
7400 static inline const char *
7401 AT_string (dw_attr_ref a)
7402 {
7403   gcc_assert (a && AT_class (a) == dw_val_class_str);
7404   return a->dw_attr_val.v.val_str->str;
7405 }
7406
7407 /* Find out whether a string should be output inline in DIE
7408    or out-of-line in .debug_str section.  */
7409
7410 static enum dwarf_form
7411 AT_string_form (dw_attr_ref a)
7412 {
7413   struct indirect_string_node *node;
7414   unsigned int len;
7415
7416   gcc_assert (a && AT_class (a) == dw_val_class_str);
7417
7418   node = a->dw_attr_val.v.val_str;
7419   if (node->form)
7420     return node->form;
7421
7422   len = strlen (node->str) + 1;
7423
7424   /* If the string is shorter or equal to the size of the reference, it is
7425      always better to put it inline.  */
7426   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7427     return node->form = DW_FORM_string;
7428
7429   /* If we cannot expect the linker to merge strings in .debug_str
7430      section, only put it into .debug_str if it is worth even in this
7431      single module.  */
7432   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7433       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7434       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7435     return node->form = DW_FORM_string;
7436
7437   gen_label_for_indirect_string (node);
7438
7439   return node->form = DW_FORM_strp;
7440 }
7441
7442 /* Add a DIE reference attribute value to a DIE.  */
7443
7444 static inline void
7445 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7446 {
7447   dw_attr_node attr;
7448
7449 #ifdef ENABLE_CHECKING
7450   gcc_assert (targ_die != NULL);
7451 #else
7452   /* With LTO we can end up trying to reference something we didn't create
7453      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7454   if (targ_die == NULL)
7455     return;
7456 #endif
7457
7458   attr.dw_attr = attr_kind;
7459   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7460   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7461   attr.dw_attr_val.v.val_die_ref.external = 0;
7462   add_dwarf_attr (die, &attr);
7463 }
7464
7465 /* Add an AT_specification attribute to a DIE, and also make the back
7466    pointer from the specification to the definition.  */
7467
7468 static inline void
7469 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7470 {
7471   add_AT_die_ref (die, DW_AT_specification, targ_die);
7472   gcc_assert (!targ_die->die_definition);
7473   targ_die->die_definition = die;
7474 }
7475
7476 static inline dw_die_ref
7477 AT_ref (dw_attr_ref a)
7478 {
7479   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7480   return a->dw_attr_val.v.val_die_ref.die;
7481 }
7482
7483 static inline int
7484 AT_ref_external (dw_attr_ref a)
7485 {
7486   if (a && AT_class (a) == dw_val_class_die_ref)
7487     return a->dw_attr_val.v.val_die_ref.external;
7488
7489   return 0;
7490 }
7491
7492 static inline void
7493 set_AT_ref_external (dw_attr_ref a, int i)
7494 {
7495   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7496   a->dw_attr_val.v.val_die_ref.external = i;
7497 }
7498
7499 /* Add an FDE reference attribute value to a DIE.  */
7500
7501 static inline void
7502 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7503 {
7504   dw_attr_node attr;
7505
7506   attr.dw_attr = attr_kind;
7507   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7508   attr.dw_attr_val.v.val_fde_index = targ_fde;
7509   add_dwarf_attr (die, &attr);
7510 }
7511
7512 /* Add a location description attribute value to a DIE.  */
7513
7514 static inline void
7515 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7516 {
7517   dw_attr_node attr;
7518
7519   attr.dw_attr = attr_kind;
7520   attr.dw_attr_val.val_class = dw_val_class_loc;
7521   attr.dw_attr_val.v.val_loc = loc;
7522   add_dwarf_attr (die, &attr);
7523 }
7524
7525 static inline dw_loc_descr_ref
7526 AT_loc (dw_attr_ref a)
7527 {
7528   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7529   return a->dw_attr_val.v.val_loc;
7530 }
7531
7532 static inline void
7533 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7534 {
7535   dw_attr_node attr;
7536
7537   attr.dw_attr = attr_kind;
7538   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7539   attr.dw_attr_val.v.val_loc_list = loc_list;
7540   add_dwarf_attr (die, &attr);
7541   have_location_lists = true;
7542 }
7543
7544 static inline dw_loc_list_ref
7545 AT_loc_list (dw_attr_ref a)
7546 {
7547   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7548   return a->dw_attr_val.v.val_loc_list;
7549 }
7550
7551 static inline dw_loc_list_ref *
7552 AT_loc_list_ptr (dw_attr_ref a)
7553 {
7554   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7555   return &a->dw_attr_val.v.val_loc_list;
7556 }
7557
7558 /* Add an address constant attribute value to a DIE.  */
7559
7560 static inline void
7561 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7562 {
7563   dw_attr_node attr;
7564
7565   attr.dw_attr = attr_kind;
7566   attr.dw_attr_val.val_class = dw_val_class_addr;
7567   attr.dw_attr_val.v.val_addr = addr;
7568   add_dwarf_attr (die, &attr);
7569 }
7570
7571 /* Get the RTX from to an address DIE attribute.  */
7572
7573 static inline rtx
7574 AT_addr (dw_attr_ref a)
7575 {
7576   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7577   return a->dw_attr_val.v.val_addr;
7578 }
7579
7580 /* Add a file attribute value to a DIE.  */
7581
7582 static inline void
7583 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7584              struct dwarf_file_data *fd)
7585 {
7586   dw_attr_node attr;
7587
7588   attr.dw_attr = attr_kind;
7589   attr.dw_attr_val.val_class = dw_val_class_file;
7590   attr.dw_attr_val.v.val_file = fd;
7591   add_dwarf_attr (die, &attr);
7592 }
7593
7594 /* Get the dwarf_file_data from a file DIE attribute.  */
7595
7596 static inline struct dwarf_file_data *
7597 AT_file (dw_attr_ref a)
7598 {
7599   gcc_assert (a && AT_class (a) == dw_val_class_file);
7600   return a->dw_attr_val.v.val_file;
7601 }
7602
7603 /* Add a vms delta attribute value to a DIE.  */
7604
7605 static inline void
7606 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7607                   const char *lbl1, const char *lbl2)
7608 {
7609   dw_attr_node attr;
7610
7611   attr.dw_attr = attr_kind;
7612   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7613   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7614   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7615   add_dwarf_attr (die, &attr);
7616 }
7617
7618 /* Add a label identifier attribute value to a DIE.  */
7619
7620 static inline void
7621 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7622 {
7623   dw_attr_node attr;
7624
7625   attr.dw_attr = attr_kind;
7626   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7627   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7628   add_dwarf_attr (die, &attr);
7629 }
7630
7631 /* Add a section offset attribute value to a DIE, an offset into the
7632    debug_line section.  */
7633
7634 static inline void
7635 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7636                 const char *label)
7637 {
7638   dw_attr_node attr;
7639
7640   attr.dw_attr = attr_kind;
7641   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7642   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7643   add_dwarf_attr (die, &attr);
7644 }
7645
7646 /* Add a section offset attribute value to a DIE, an offset into the
7647    debug_macinfo section.  */
7648
7649 static inline void
7650 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7651                const char *label)
7652 {
7653   dw_attr_node attr;
7654
7655   attr.dw_attr = attr_kind;
7656   attr.dw_attr_val.val_class = dw_val_class_macptr;
7657   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7658   add_dwarf_attr (die, &attr);
7659 }
7660
7661 /* Add an offset attribute value to a DIE.  */
7662
7663 static inline void
7664 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7665                unsigned HOST_WIDE_INT offset)
7666 {
7667   dw_attr_node attr;
7668
7669   attr.dw_attr = attr_kind;
7670   attr.dw_attr_val.val_class = dw_val_class_offset;
7671   attr.dw_attr_val.v.val_offset = offset;
7672   add_dwarf_attr (die, &attr);
7673 }
7674
7675 /* Add an range_list attribute value to a DIE.  */
7676
7677 static void
7678 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7679                    long unsigned int offset)
7680 {
7681   dw_attr_node attr;
7682
7683   attr.dw_attr = attr_kind;
7684   attr.dw_attr_val.val_class = dw_val_class_range_list;
7685   attr.dw_attr_val.v.val_offset = offset;
7686   add_dwarf_attr (die, &attr);
7687 }
7688
7689 /* Return the start label of a delta attribute.  */
7690
7691 static inline const char *
7692 AT_vms_delta1 (dw_attr_ref a)
7693 {
7694   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7695   return a->dw_attr_val.v.val_vms_delta.lbl1;
7696 }
7697
7698 /* Return the end label of a delta attribute.  */
7699
7700 static inline const char *
7701 AT_vms_delta2 (dw_attr_ref a)
7702 {
7703   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7704   return a->dw_attr_val.v.val_vms_delta.lbl2;
7705 }
7706
7707 static inline const char *
7708 AT_lbl (dw_attr_ref a)
7709 {
7710   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7711                     || AT_class (a) == dw_val_class_lineptr
7712                     || AT_class (a) == dw_val_class_macptr));
7713   return a->dw_attr_val.v.val_lbl_id;
7714 }
7715
7716 /* Get the attribute of type attr_kind.  */
7717
7718 static dw_attr_ref
7719 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7720 {
7721   dw_attr_ref a;
7722   unsigned ix;
7723   dw_die_ref spec = NULL;
7724
7725   if (! die)
7726     return NULL;
7727
7728   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7729     if (a->dw_attr == attr_kind)
7730       return a;
7731     else if (a->dw_attr == DW_AT_specification
7732              || a->dw_attr == DW_AT_abstract_origin)
7733       spec = AT_ref (a);
7734
7735   if (spec)
7736     return get_AT (spec, attr_kind);
7737
7738   return NULL;
7739 }
7740
7741 /* Return the "low pc" attribute value, typically associated with a subprogram
7742    DIE.  Return null if the "low pc" attribute is either not present, or if it
7743    cannot be represented as an assembler label identifier.  */
7744
7745 static inline const char *
7746 get_AT_low_pc (dw_die_ref die)
7747 {
7748   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7749
7750   return a ? AT_lbl (a) : NULL;
7751 }
7752
7753 /* Return the "high pc" attribute value, typically associated with a subprogram
7754    DIE.  Return null if the "high pc" attribute is either not present, or if it
7755    cannot be represented as an assembler label identifier.  */
7756
7757 static inline const char *
7758 get_AT_hi_pc (dw_die_ref die)
7759 {
7760   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7761
7762   return a ? AT_lbl (a) : NULL;
7763 }
7764
7765 /* Return the value of the string attribute designated by ATTR_KIND, or
7766    NULL if it is not present.  */
7767
7768 static inline const char *
7769 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7770 {
7771   dw_attr_ref a = get_AT (die, attr_kind);
7772
7773   return a ? AT_string (a) : NULL;
7774 }
7775
7776 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7777    if it is not present.  */
7778
7779 static inline int
7780 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7781 {
7782   dw_attr_ref a = get_AT (die, attr_kind);
7783
7784   return a ? AT_flag (a) : 0;
7785 }
7786
7787 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7788    if it is not present.  */
7789
7790 static inline unsigned
7791 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7792 {
7793   dw_attr_ref a = get_AT (die, attr_kind);
7794
7795   return a ? AT_unsigned (a) : 0;
7796 }
7797
7798 static inline dw_die_ref
7799 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7800 {
7801   dw_attr_ref a = get_AT (die, attr_kind);
7802
7803   return a ? AT_ref (a) : NULL;
7804 }
7805
7806 static inline struct dwarf_file_data *
7807 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7808 {
7809   dw_attr_ref a = get_AT (die, attr_kind);
7810
7811   return a ? AT_file (a) : NULL;
7812 }
7813
7814 /* Return TRUE if the language is C++.  */
7815
7816 static inline bool
7817 is_cxx (void)
7818 {
7819   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7820
7821   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7822 }
7823
7824 /* Return TRUE if the language is Fortran.  */
7825
7826 static inline bool
7827 is_fortran (void)
7828 {
7829   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7830
7831   return (lang == DW_LANG_Fortran77
7832           || lang == DW_LANG_Fortran90
7833           || lang == DW_LANG_Fortran95);
7834 }
7835
7836 /* Return TRUE if the language is Ada.  */
7837
7838 static inline bool
7839 is_ada (void)
7840 {
7841   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7842
7843   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7844 }
7845
7846 /* Remove the specified attribute if present.  */
7847
7848 static void
7849 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7850 {
7851   dw_attr_ref a;
7852   unsigned ix;
7853
7854   if (! die)
7855     return;
7856
7857   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7858     if (a->dw_attr == attr_kind)
7859       {
7860         if (AT_class (a) == dw_val_class_str)
7861           if (a->dw_attr_val.v.val_str->refcount)
7862             a->dw_attr_val.v.val_str->refcount--;
7863
7864         /* VEC_ordered_remove should help reduce the number of abbrevs
7865            that are needed.  */
7866         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7867         return;
7868       }
7869 }
7870
7871 /* Remove CHILD from its parent.  PREV must have the property that
7872    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7873
7874 static void
7875 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7876 {
7877   gcc_assert (child->die_parent == prev->die_parent);
7878   gcc_assert (prev->die_sib == child);
7879   if (prev == child)
7880     {
7881       gcc_assert (child->die_parent->die_child == child);
7882       prev = NULL;
7883     }
7884   else
7885     prev->die_sib = child->die_sib;
7886   if (child->die_parent->die_child == child)
7887     child->die_parent->die_child = prev;
7888 }
7889
7890 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7891    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7892
7893 static void
7894 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7895 {
7896   dw_die_ref parent = old_child->die_parent;
7897
7898   gcc_assert (parent == prev->die_parent);
7899   gcc_assert (prev->die_sib == old_child);
7900
7901   new_child->die_parent = parent;
7902   if (prev == old_child)
7903     {
7904       gcc_assert (parent->die_child == old_child);
7905       new_child->die_sib = new_child;
7906     }
7907   else
7908     {
7909       prev->die_sib = new_child;
7910       new_child->die_sib = old_child->die_sib;
7911     }
7912   if (old_child->die_parent->die_child == old_child)
7913     old_child->die_parent->die_child = new_child;
7914 }
7915
7916 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7917
7918 static void
7919 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7920 {
7921   dw_die_ref c;
7922   new_parent->die_child = old_parent->die_child;
7923   old_parent->die_child = NULL;
7924   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7925 }
7926
7927 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7928    matches TAG.  */
7929
7930 static void
7931 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7932 {
7933   dw_die_ref c;
7934
7935   c = die->die_child;
7936   if (c) do {
7937     dw_die_ref prev = c;
7938     c = c->die_sib;
7939     while (c->die_tag == tag)
7940       {
7941         remove_child_with_prev (c, prev);
7942         /* Might have removed every child.  */
7943         if (c == c->die_sib)
7944           return;
7945         c = c->die_sib;
7946       }
7947   } while (c != die->die_child);
7948 }
7949
7950 /* Add a CHILD_DIE as the last child of DIE.  */
7951
7952 static void
7953 add_child_die (dw_die_ref die, dw_die_ref child_die)
7954 {
7955   /* FIXME this should probably be an assert.  */
7956   if (! die || ! child_die)
7957     return;
7958   gcc_assert (die != child_die);
7959
7960   child_die->die_parent = die;
7961   if (die->die_child)
7962     {
7963       child_die->die_sib = die->die_child->die_sib;
7964       die->die_child->die_sib = child_die;
7965     }
7966   else
7967     child_die->die_sib = child_die;
7968   die->die_child = child_die;
7969 }
7970
7971 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7972    is the specification, to the end of PARENT's list of children.
7973    This is done by removing and re-adding it.  */
7974
7975 static void
7976 splice_child_die (dw_die_ref parent, dw_die_ref child)
7977 {
7978   dw_die_ref p;
7979
7980   /* We want the declaration DIE from inside the class, not the
7981      specification DIE at toplevel.  */
7982   if (child->die_parent != parent)
7983     {
7984       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7985
7986       if (tmp)
7987         child = tmp;
7988     }
7989
7990   gcc_assert (child->die_parent == parent
7991               || (child->die_parent
7992                   == get_AT_ref (parent, DW_AT_specification)));
7993
7994   for (p = child->die_parent->die_child; ; p = p->die_sib)
7995     if (p->die_sib == child)
7996       {
7997         remove_child_with_prev (child, p);
7998         break;
7999       }
8000
8001   add_child_die (parent, child);
8002 }
8003
8004 /* Return a pointer to a newly created DIE node.  */
8005
8006 static inline dw_die_ref
8007 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8008 {
8009   dw_die_ref die = ggc_alloc_cleared_die_node ();
8010
8011   die->die_tag = tag_value;
8012
8013   if (parent_die != NULL)
8014     add_child_die (parent_die, die);
8015   else
8016     {
8017       limbo_die_node *limbo_node;
8018
8019       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8020       limbo_node->die = die;
8021       limbo_node->created_for = t;
8022       limbo_node->next = limbo_die_list;
8023       limbo_die_list = limbo_node;
8024     }
8025
8026   return die;
8027 }
8028
8029 /* Return the DIE associated with the given type specifier.  */
8030
8031 static inline dw_die_ref
8032 lookup_type_die (tree type)
8033 {
8034   return TYPE_SYMTAB_DIE (type);
8035 }
8036
8037 /* Like lookup_type_die, but if type is an anonymous type named by a
8038    typedef[1], return the DIE of the anonymous type instead the one of
8039    the naming typedef.  This is because in gen_typedef_die, we did
8040    equate the anonymous struct named by the typedef with the DIE of
8041    the naming typedef. So by default, lookup_type_die on an anonymous
8042    struct yields the DIE of the naming typedef.
8043
8044    [1]: Read the comment of is_naming_typedef_decl to learn about what
8045    a naming typedef is.  */
8046
8047 static inline dw_die_ref
8048 lookup_type_die_strip_naming_typedef (tree type)
8049 {
8050   dw_die_ref die = lookup_type_die (type);
8051   if (TREE_CODE (type) == RECORD_TYPE
8052       && die->die_tag == DW_TAG_typedef
8053       && is_naming_typedef_decl (TYPE_NAME (type)))
8054     die = get_AT_ref (die, DW_AT_type);
8055   return die;
8056 }
8057
8058 /* Equate a DIE to a given type specifier.  */
8059
8060 static inline void
8061 equate_type_number_to_die (tree type, dw_die_ref type_die)
8062 {
8063   TYPE_SYMTAB_DIE (type) = type_die;
8064 }
8065
8066 /* Returns a hash value for X (which really is a die_struct).  */
8067
8068 static hashval_t
8069 decl_die_table_hash (const void *x)
8070 {
8071   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8072 }
8073
8074 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8075
8076 static int
8077 decl_die_table_eq (const void *x, const void *y)
8078 {
8079   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8080 }
8081
8082 /* Return the DIE associated with a given declaration.  */
8083
8084 static inline dw_die_ref
8085 lookup_decl_die (tree decl)
8086 {
8087   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8088 }
8089
8090 /* Returns a hash value for X (which really is a var_loc_list).  */
8091
8092 static hashval_t
8093 decl_loc_table_hash (const void *x)
8094 {
8095   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8096 }
8097
8098 /* Return nonzero if decl_id of var_loc_list X is the same as
8099    UID of decl *Y.  */
8100
8101 static int
8102 decl_loc_table_eq (const void *x, const void *y)
8103 {
8104   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8105 }
8106
8107 /* Return the var_loc list associated with a given declaration.  */
8108
8109 static inline var_loc_list *
8110 lookup_decl_loc (const_tree decl)
8111 {
8112   if (!decl_loc_table)
8113     return NULL;
8114   return (var_loc_list *)
8115     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8116 }
8117
8118 /* Equate a DIE to a particular declaration.  */
8119
8120 static void
8121 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8122 {
8123   unsigned int decl_id = DECL_UID (decl);
8124   void **slot;
8125
8126   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8127   *slot = decl_die;
8128   decl_die->decl_id = decl_id;
8129 }
8130
8131 /* Return how many bits covers PIECE EXPR_LIST.  */
8132
8133 static int
8134 decl_piece_bitsize (rtx piece)
8135 {
8136   int ret = (int) GET_MODE (piece);
8137   if (ret)
8138     return ret;
8139   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8140               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8141   return INTVAL (XEXP (XEXP (piece, 0), 0));
8142 }
8143
8144 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8145
8146 static rtx *
8147 decl_piece_varloc_ptr (rtx piece)
8148 {
8149   if ((int) GET_MODE (piece))
8150     return &XEXP (piece, 0);
8151   else
8152     return &XEXP (XEXP (piece, 0), 1);
8153 }
8154
8155 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8156    Next is the chain of following piece nodes.  */
8157
8158 static rtx
8159 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8160 {
8161   if (bitsize <= (int) MAX_MACHINE_MODE)
8162     return alloc_EXPR_LIST (bitsize, loc_note, next);
8163   else
8164     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8165                                                GEN_INT (bitsize),
8166                                                loc_note), next);
8167 }
8168
8169 /* Return rtx that should be stored into loc field for
8170    LOC_NOTE and BITPOS/BITSIZE.  */
8171
8172 static rtx
8173 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8174                       HOST_WIDE_INT bitsize)
8175 {
8176   if (bitsize != -1)
8177     {
8178       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8179       if (bitpos != 0)
8180         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8181     }
8182   return loc_note;
8183 }
8184
8185 /* This function either modifies location piece list *DEST in
8186    place (if SRC and INNER is NULL), or copies location piece list
8187    *SRC to *DEST while modifying it.  Location BITPOS is modified
8188    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8189    not copied and if needed some padding around it is added.
8190    When modifying in place, DEST should point to EXPR_LIST where
8191    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8192    to the start of the whole list and INNER points to the EXPR_LIST
8193    where earlier pieces cover PIECE_BITPOS bits.  */
8194
8195 static void
8196 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8197                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8198                    HOST_WIDE_INT bitsize, rtx loc_note)
8199 {
8200   int diff;
8201   bool copy = inner != NULL;
8202
8203   if (copy)
8204     {
8205       /* First copy all nodes preceeding the current bitpos.  */
8206       while (src != inner)
8207         {
8208           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8209                                    decl_piece_bitsize (*src), NULL_RTX);
8210           dest = &XEXP (*dest, 1);
8211           src = &XEXP (*src, 1);
8212         }
8213     }
8214   /* Add padding if needed.  */
8215   if (bitpos != piece_bitpos)
8216     {
8217       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8218                                copy ? NULL_RTX : *dest);
8219       dest = &XEXP (*dest, 1);
8220     }
8221   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8222     {
8223       gcc_assert (!copy);
8224       /* A piece with correct bitpos and bitsize already exist,
8225          just update the location for it and return.  */
8226       *decl_piece_varloc_ptr (*dest) = loc_note;
8227       return;
8228     }
8229   /* Add the piece that changed.  */
8230   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8231   dest = &XEXP (*dest, 1);
8232   /* Skip over pieces that overlap it.  */
8233   diff = bitpos - piece_bitpos + bitsize;
8234   if (!copy)
8235     src = dest;
8236   while (diff > 0 && *src)
8237     {
8238       rtx piece = *src;
8239       diff -= decl_piece_bitsize (piece);
8240       if (copy)
8241         src = &XEXP (piece, 1);
8242       else
8243         {
8244           *src = XEXP (piece, 1);
8245           free_EXPR_LIST_node (piece);
8246         }
8247     }
8248   /* Add padding if needed.  */
8249   if (diff < 0 && *src)
8250     {
8251       if (!copy)
8252         dest = src;
8253       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8254       dest = &XEXP (*dest, 1);
8255     }
8256   if (!copy)
8257     return;
8258   /* Finally copy all nodes following it.  */
8259   while (*src)
8260     {
8261       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8262                                decl_piece_bitsize (*src), NULL_RTX);
8263       dest = &XEXP (*dest, 1);
8264       src = &XEXP (*src, 1);
8265     }
8266 }
8267
8268 /* Add a variable location node to the linked list for DECL.  */
8269
8270 static struct var_loc_node *
8271 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8272 {
8273   unsigned int decl_id;
8274   var_loc_list *temp;
8275   void **slot;
8276   struct var_loc_node *loc = NULL;
8277   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8278
8279   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8280     {
8281       tree realdecl = DECL_DEBUG_EXPR (decl);
8282       if (realdecl && handled_component_p (realdecl))
8283         {
8284           HOST_WIDE_INT maxsize;
8285           tree innerdecl;
8286           innerdecl
8287             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8288           if (!DECL_P (innerdecl)
8289               || DECL_IGNORED_P (innerdecl)
8290               || TREE_STATIC (innerdecl)
8291               || bitsize <= 0
8292               || bitpos + bitsize > 256
8293               || bitsize != maxsize)
8294             return NULL;
8295           decl = innerdecl;
8296         }
8297     }
8298
8299   decl_id = DECL_UID (decl);
8300   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8301   if (*slot == NULL)
8302     {
8303       temp = ggc_alloc_cleared_var_loc_list ();
8304       temp->decl_id = decl_id;
8305       *slot = temp;
8306     }
8307   else
8308     temp = (var_loc_list *) *slot;
8309
8310   if (temp->last)
8311     {
8312       struct var_loc_node *last = temp->last, *unused = NULL;
8313       rtx *piece_loc = NULL, last_loc_note;
8314       int piece_bitpos = 0;
8315       if (last->next)
8316         {
8317           last = last->next;
8318           gcc_assert (last->next == NULL);
8319         }
8320       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8321         {
8322           piece_loc = &last->loc;
8323           do
8324             {
8325               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8326               if (piece_bitpos + cur_bitsize > bitpos)
8327                 break;
8328               piece_bitpos += cur_bitsize;
8329               piece_loc = &XEXP (*piece_loc, 1);
8330             }
8331           while (*piece_loc);
8332         }
8333       /* TEMP->LAST here is either pointer to the last but one or
8334          last element in the chained list, LAST is pointer to the
8335          last element.  */
8336       if (label && strcmp (last->label, label) == 0)
8337         {
8338           /* For SRA optimized variables if there weren't any real
8339              insns since last note, just modify the last node.  */
8340           if (piece_loc != NULL)
8341             {
8342               adjust_piece_list (piece_loc, NULL, NULL,
8343                                  bitpos, piece_bitpos, bitsize, loc_note);
8344               return NULL;
8345             }
8346           /* If the last note doesn't cover any instructions, remove it.  */
8347           if (temp->last != last)
8348             {
8349               temp->last->next = NULL;
8350               unused = last;
8351               last = temp->last;
8352               gcc_assert (strcmp (last->label, label) != 0);
8353             }
8354           else
8355             {
8356               gcc_assert (temp->first == temp->last);
8357               memset (temp->last, '\0', sizeof (*temp->last));
8358               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8359               return temp->last;
8360             }
8361         }
8362       if (bitsize == -1 && NOTE_P (last->loc))
8363         last_loc_note = last->loc;
8364       else if (piece_loc != NULL
8365                && *piece_loc != NULL_RTX
8366                && piece_bitpos == bitpos
8367                && decl_piece_bitsize (*piece_loc) == bitsize)
8368         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8369       else
8370         last_loc_note = NULL_RTX;
8371       /* If the current location is the same as the end of the list,
8372          and either both or neither of the locations is uninitialized,
8373          we have nothing to do.  */
8374       if (last_loc_note == NULL_RTX
8375           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8376                             NOTE_VAR_LOCATION_LOC (loc_note)))
8377           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8378                != NOTE_VAR_LOCATION_STATUS (loc_note))
8379               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8380                    == VAR_INIT_STATUS_UNINITIALIZED)
8381                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8382                       == VAR_INIT_STATUS_UNINITIALIZED))))
8383         {
8384           /* Add LOC to the end of list and update LAST.  If the last
8385              element of the list has been removed above, reuse its
8386              memory for the new node, otherwise allocate a new one.  */
8387           if (unused)
8388             {
8389               loc = unused;
8390               memset (loc, '\0', sizeof (*loc));
8391             }
8392           else
8393             loc = ggc_alloc_cleared_var_loc_node ();
8394           if (bitsize == -1 || piece_loc == NULL)
8395             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8396           else
8397             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8398                                bitpos, piece_bitpos, bitsize, loc_note);
8399           last->next = loc;
8400           /* Ensure TEMP->LAST will point either to the new last but one
8401              element of the chain, or to the last element in it.  */
8402           if (last != temp->last)
8403             temp->last = last;
8404         }
8405       else if (unused)
8406         ggc_free (unused);
8407     }
8408   else
8409     {
8410       loc = ggc_alloc_cleared_var_loc_node ();
8411       temp->first = loc;
8412       temp->last = loc;
8413       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8414     }
8415   return loc;
8416 }
8417 \f
8418 /* Keep track of the number of spaces used to indent the
8419    output of the debugging routines that print the structure of
8420    the DIE internal representation.  */
8421 static int print_indent;
8422
8423 /* Indent the line the number of spaces given by print_indent.  */
8424
8425 static inline void
8426 print_spaces (FILE *outfile)
8427 {
8428   fprintf (outfile, "%*s", print_indent, "");
8429 }
8430
8431 /* Print a type signature in hex.  */
8432
8433 static inline void
8434 print_signature (FILE *outfile, char *sig)
8435 {
8436   int i;
8437
8438   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8439     fprintf (outfile, "%02x", sig[i] & 0xff);
8440 }
8441
8442 /* Print the information associated with a given DIE, and its children.
8443    This routine is a debugging aid only.  */
8444
8445 static void
8446 print_die (dw_die_ref die, FILE *outfile)
8447 {
8448   dw_attr_ref a;
8449   dw_die_ref c;
8450   unsigned ix;
8451
8452   print_spaces (outfile);
8453   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8454            die->die_offset, dwarf_tag_name (die->die_tag),
8455            (void*) die);
8456   print_spaces (outfile);
8457   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8458   fprintf (outfile, " offset: %ld", die->die_offset);
8459   fprintf (outfile, " mark: %d\n", die->die_mark);
8460
8461   if (dwarf_version >= 4 && die->die_id.die_type_node)
8462     {
8463       print_spaces (outfile);
8464       fprintf (outfile, "  signature: ");
8465       print_signature (outfile, die->die_id.die_type_node->signature);
8466       fprintf (outfile, "\n");
8467     }
8468
8469   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8470     {
8471       print_spaces (outfile);
8472       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8473
8474       switch (AT_class (a))
8475         {
8476         case dw_val_class_addr:
8477           fprintf (outfile, "address");
8478           break;
8479         case dw_val_class_offset:
8480           fprintf (outfile, "offset");
8481           break;
8482         case dw_val_class_loc:
8483           fprintf (outfile, "location descriptor");
8484           break;
8485         case dw_val_class_loc_list:
8486           fprintf (outfile, "location list -> label:%s",
8487                    AT_loc_list (a)->ll_symbol);
8488           break;
8489         case dw_val_class_range_list:
8490           fprintf (outfile, "range list");
8491           break;
8492         case dw_val_class_const:
8493           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8494           break;
8495         case dw_val_class_unsigned_const:
8496           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8497           break;
8498         case dw_val_class_const_double:
8499           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8500                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8501                    a->dw_attr_val.v.val_double.high,
8502                    a->dw_attr_val.v.val_double.low);
8503           break;
8504         case dw_val_class_vec:
8505           fprintf (outfile, "floating-point or vector constant");
8506           break;
8507         case dw_val_class_flag:
8508           fprintf (outfile, "%u", AT_flag (a));
8509           break;
8510         case dw_val_class_die_ref:
8511           if (AT_ref (a) != NULL)
8512             {
8513               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8514                 {
8515                   fprintf (outfile, "die -> signature: ");
8516                   print_signature (outfile,
8517                                    AT_ref (a)->die_id.die_type_node->signature);
8518                 }
8519               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8520                 fprintf (outfile, "die -> label: %s",
8521                          AT_ref (a)->die_id.die_symbol);
8522               else
8523                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8524               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8525             }
8526           else
8527             fprintf (outfile, "die -> <null>");
8528           break;
8529         case dw_val_class_vms_delta:
8530           fprintf (outfile, "delta: @slotcount(%s-%s)",
8531                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8532           break;
8533         case dw_val_class_lbl_id:
8534         case dw_val_class_lineptr:
8535         case dw_val_class_macptr:
8536           fprintf (outfile, "label: %s", AT_lbl (a));
8537           break;
8538         case dw_val_class_str:
8539           if (AT_string (a) != NULL)
8540             fprintf (outfile, "\"%s\"", AT_string (a));
8541           else
8542             fprintf (outfile, "<null>");
8543           break;
8544         case dw_val_class_file:
8545           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8546                    AT_file (a)->emitted_number);
8547           break;
8548         case dw_val_class_data8:
8549           {
8550             int i;
8551
8552             for (i = 0; i < 8; i++)
8553               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8554             break;
8555           }
8556         default:
8557           break;
8558         }
8559
8560       fprintf (outfile, "\n");
8561     }
8562
8563   if (die->die_child != NULL)
8564     {
8565       print_indent += 4;
8566       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8567       print_indent -= 4;
8568     }
8569   if (print_indent == 0)
8570     fprintf (outfile, "\n");
8571 }
8572
8573 /* Print the contents of the source code line number correspondence table.
8574    This routine is a debugging aid only.  */
8575
8576 static void
8577 print_dwarf_line_table (FILE *outfile)
8578 {
8579   unsigned i;
8580   dw_line_info_ref line_info;
8581
8582   fprintf (outfile, "\n\nDWARF source line information\n");
8583   for (i = 1; i < line_info_table_in_use; i++)
8584     {
8585       line_info = &line_info_table[i];
8586       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8587                line_info->dw_file_num,
8588                line_info->dw_line_num);
8589     }
8590
8591   fprintf (outfile, "\n\n");
8592 }
8593
8594 /* Print the information collected for a given DIE.  */
8595
8596 DEBUG_FUNCTION void
8597 debug_dwarf_die (dw_die_ref die)
8598 {
8599   print_die (die, stderr);
8600 }
8601
8602 /* Print all DWARF information collected for the compilation unit.
8603    This routine is a debugging aid only.  */
8604
8605 DEBUG_FUNCTION void
8606 debug_dwarf (void)
8607 {
8608   print_indent = 0;
8609   print_die (comp_unit_die (), stderr);
8610   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8611     print_dwarf_line_table (stderr);
8612 }
8613 \f
8614 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8615    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8616    DIE that marks the start of the DIEs for this include file.  */
8617
8618 static dw_die_ref
8619 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8620 {
8621   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8622   dw_die_ref new_unit = gen_compile_unit_die (filename);
8623
8624   new_unit->die_sib = old_unit;
8625   return new_unit;
8626 }
8627
8628 /* Close an include-file CU and reopen the enclosing one.  */
8629
8630 static dw_die_ref
8631 pop_compile_unit (dw_die_ref old_unit)
8632 {
8633   dw_die_ref new_unit = old_unit->die_sib;
8634
8635   old_unit->die_sib = NULL;
8636   return new_unit;
8637 }
8638
8639 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8640 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8641
8642 /* Calculate the checksum of a location expression.  */
8643
8644 static inline void
8645 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8646 {
8647   int tem;
8648
8649   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8650   CHECKSUM (tem);
8651   CHECKSUM (loc->dw_loc_oprnd1);
8652   CHECKSUM (loc->dw_loc_oprnd2);
8653 }
8654
8655 /* Calculate the checksum of an attribute.  */
8656
8657 static void
8658 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8659 {
8660   dw_loc_descr_ref loc;
8661   rtx r;
8662
8663   CHECKSUM (at->dw_attr);
8664
8665   /* We don't care that this was compiled with a different compiler
8666      snapshot; if the output is the same, that's what matters.  */
8667   if (at->dw_attr == DW_AT_producer)
8668     return;
8669
8670   switch (AT_class (at))
8671     {
8672     case dw_val_class_const:
8673       CHECKSUM (at->dw_attr_val.v.val_int);
8674       break;
8675     case dw_val_class_unsigned_const:
8676       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8677       break;
8678     case dw_val_class_const_double:
8679       CHECKSUM (at->dw_attr_val.v.val_double);
8680       break;
8681     case dw_val_class_vec:
8682       CHECKSUM (at->dw_attr_val.v.val_vec);
8683       break;
8684     case dw_val_class_flag:
8685       CHECKSUM (at->dw_attr_val.v.val_flag);
8686       break;
8687     case dw_val_class_str:
8688       CHECKSUM_STRING (AT_string (at));
8689       break;
8690
8691     case dw_val_class_addr:
8692       r = AT_addr (at);
8693       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8694       CHECKSUM_STRING (XSTR (r, 0));
8695       break;
8696
8697     case dw_val_class_offset:
8698       CHECKSUM (at->dw_attr_val.v.val_offset);
8699       break;
8700
8701     case dw_val_class_loc:
8702       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8703         loc_checksum (loc, ctx);
8704       break;
8705
8706     case dw_val_class_die_ref:
8707       die_checksum (AT_ref (at), ctx, mark);
8708       break;
8709
8710     case dw_val_class_fde_ref:
8711     case dw_val_class_vms_delta:
8712     case dw_val_class_lbl_id:
8713     case dw_val_class_lineptr:
8714     case dw_val_class_macptr:
8715       break;
8716
8717     case dw_val_class_file:
8718       CHECKSUM_STRING (AT_file (at)->filename);
8719       break;
8720
8721     case dw_val_class_data8:
8722       CHECKSUM (at->dw_attr_val.v.val_data8);
8723       break;
8724
8725     default:
8726       break;
8727     }
8728 }
8729
8730 /* Calculate the checksum of a DIE.  */
8731
8732 static void
8733 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8734 {
8735   dw_die_ref c;
8736   dw_attr_ref a;
8737   unsigned ix;
8738
8739   /* To avoid infinite recursion.  */
8740   if (die->die_mark)
8741     {
8742       CHECKSUM (die->die_mark);
8743       return;
8744     }
8745   die->die_mark = ++(*mark);
8746
8747   CHECKSUM (die->die_tag);
8748
8749   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8750     attr_checksum (a, ctx, mark);
8751
8752   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8753 }
8754
8755 #undef CHECKSUM
8756 #undef CHECKSUM_STRING
8757
8758 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8759 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8760 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8761 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8762 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8763 #define CHECKSUM_ATTR(FOO) \
8764   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8765
8766 /* Calculate the checksum of a number in signed LEB128 format.  */
8767
8768 static void
8769 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8770 {
8771   unsigned char byte;
8772   bool more;
8773
8774   while (1)
8775     {
8776       byte = (value & 0x7f);
8777       value >>= 7;
8778       more = !((value == 0 && (byte & 0x40) == 0)
8779                 || (value == -1 && (byte & 0x40) != 0));
8780       if (more)
8781         byte |= 0x80;
8782       CHECKSUM (byte);
8783       if (!more)
8784         break;
8785     }
8786 }
8787
8788 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8789
8790 static void
8791 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8792 {
8793   while (1)
8794     {
8795       unsigned char byte = (value & 0x7f);
8796       value >>= 7;
8797       if (value != 0)
8798         /* More bytes to follow.  */
8799         byte |= 0x80;
8800       CHECKSUM (byte);
8801       if (value == 0)
8802         break;
8803     }
8804 }
8805
8806 /* Checksum the context of the DIE.  This adds the names of any
8807    surrounding namespaces or structures to the checksum.  */
8808
8809 static void
8810 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8811 {
8812   const char *name;
8813   dw_die_ref spec;
8814   int tag = die->die_tag;
8815
8816   if (tag != DW_TAG_namespace
8817       && tag != DW_TAG_structure_type
8818       && tag != DW_TAG_class_type)
8819     return;
8820
8821   name = get_AT_string (die, DW_AT_name);
8822
8823   spec = get_AT_ref (die, DW_AT_specification);
8824   if (spec != NULL)
8825     die = spec;
8826
8827   if (die->die_parent != NULL)
8828     checksum_die_context (die->die_parent, ctx);
8829
8830   CHECKSUM_ULEB128 ('C');
8831   CHECKSUM_ULEB128 (tag);
8832   if (name != NULL)
8833     CHECKSUM_STRING (name);
8834 }
8835
8836 /* Calculate the checksum of a location expression.  */
8837
8838 static inline void
8839 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8840 {
8841   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8842      were emitted as a DW_FORM_sdata instead of a location expression.  */
8843   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8844     {
8845       CHECKSUM_ULEB128 (DW_FORM_sdata);
8846       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8847       return;
8848     }
8849
8850   /* Otherwise, just checksum the raw location expression.  */
8851   while (loc != NULL)
8852     {
8853       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8854       CHECKSUM (loc->dw_loc_oprnd1);
8855       CHECKSUM (loc->dw_loc_oprnd2);
8856       loc = loc->dw_loc_next;
8857     }
8858 }
8859
8860 /* Calculate the checksum of an attribute.  */
8861
8862 static void
8863 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8864                        struct md5_ctx *ctx, int *mark)
8865 {
8866   dw_loc_descr_ref loc;
8867   rtx r;
8868
8869   if (AT_class (at) == dw_val_class_die_ref)
8870     {
8871       dw_die_ref target_die = AT_ref (at);
8872
8873       /* For pointer and reference types, we checksum only the (qualified)
8874          name of the target type (if there is a name).  For friend entries,
8875          we checksum only the (qualified) name of the target type or function.
8876          This allows the checksum to remain the same whether the target type
8877          is complete or not.  */
8878       if ((at->dw_attr == DW_AT_type
8879            && (tag == DW_TAG_pointer_type
8880                || tag == DW_TAG_reference_type
8881                || tag == DW_TAG_rvalue_reference_type
8882                || tag == DW_TAG_ptr_to_member_type))
8883           || (at->dw_attr == DW_AT_friend
8884               && tag == DW_TAG_friend))
8885         {
8886           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8887
8888           if (name_attr != NULL)
8889             {
8890               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8891
8892               if (decl == NULL)
8893                 decl = target_die;
8894               CHECKSUM_ULEB128 ('N');
8895               CHECKSUM_ULEB128 (at->dw_attr);
8896               if (decl->die_parent != NULL)
8897                 checksum_die_context (decl->die_parent, ctx);
8898               CHECKSUM_ULEB128 ('E');
8899               CHECKSUM_STRING (AT_string (name_attr));
8900               return;
8901             }
8902         }
8903
8904       /* For all other references to another DIE, we check to see if the
8905          target DIE has already been visited.  If it has, we emit a
8906          backward reference; if not, we descend recursively.  */
8907       if (target_die->die_mark > 0)
8908         {
8909           CHECKSUM_ULEB128 ('R');
8910           CHECKSUM_ULEB128 (at->dw_attr);
8911           CHECKSUM_ULEB128 (target_die->die_mark);
8912         }
8913       else
8914         {
8915           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8916
8917           if (decl == NULL)
8918             decl = target_die;
8919           target_die->die_mark = ++(*mark);
8920           CHECKSUM_ULEB128 ('T');
8921           CHECKSUM_ULEB128 (at->dw_attr);
8922           if (decl->die_parent != NULL)
8923             checksum_die_context (decl->die_parent, ctx);
8924           die_checksum_ordered (target_die, ctx, mark);
8925         }
8926       return;
8927     }
8928
8929   CHECKSUM_ULEB128 ('A');
8930   CHECKSUM_ULEB128 (at->dw_attr);
8931
8932   switch (AT_class (at))
8933     {
8934     case dw_val_class_const:
8935       CHECKSUM_ULEB128 (DW_FORM_sdata);
8936       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8937       break;
8938
8939     case dw_val_class_unsigned_const:
8940       CHECKSUM_ULEB128 (DW_FORM_sdata);
8941       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8942       break;
8943
8944     case dw_val_class_const_double:
8945       CHECKSUM_ULEB128 (DW_FORM_block);
8946       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8947       CHECKSUM (at->dw_attr_val.v.val_double);
8948       break;
8949
8950     case dw_val_class_vec:
8951       CHECKSUM_ULEB128 (DW_FORM_block);
8952       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8953       CHECKSUM (at->dw_attr_val.v.val_vec);
8954       break;
8955
8956     case dw_val_class_flag:
8957       CHECKSUM_ULEB128 (DW_FORM_flag);
8958       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8959       break;
8960
8961     case dw_val_class_str:
8962       CHECKSUM_ULEB128 (DW_FORM_string);
8963       CHECKSUM_STRING (AT_string (at));
8964       break;
8965
8966     case dw_val_class_addr:
8967       r = AT_addr (at);
8968       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8969       CHECKSUM_ULEB128 (DW_FORM_string);
8970       CHECKSUM_STRING (XSTR (r, 0));
8971       break;
8972
8973     case dw_val_class_offset:
8974       CHECKSUM_ULEB128 (DW_FORM_sdata);
8975       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8976       break;
8977
8978     case dw_val_class_loc:
8979       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8980         loc_checksum_ordered (loc, ctx);
8981       break;
8982
8983     case dw_val_class_fde_ref:
8984     case dw_val_class_lbl_id:
8985     case dw_val_class_lineptr:
8986     case dw_val_class_macptr:
8987       break;
8988
8989     case dw_val_class_file:
8990       CHECKSUM_ULEB128 (DW_FORM_string);
8991       CHECKSUM_STRING (AT_file (at)->filename);
8992       break;
8993
8994     case dw_val_class_data8:
8995       CHECKSUM (at->dw_attr_val.v.val_data8);
8996       break;
8997
8998     default:
8999       break;
9000     }
9001 }
9002
9003 struct checksum_attributes
9004 {
9005   dw_attr_ref at_name;
9006   dw_attr_ref at_type;
9007   dw_attr_ref at_friend;
9008   dw_attr_ref at_accessibility;
9009   dw_attr_ref at_address_class;
9010   dw_attr_ref at_allocated;
9011   dw_attr_ref at_artificial;
9012   dw_attr_ref at_associated;
9013   dw_attr_ref at_binary_scale;
9014   dw_attr_ref at_bit_offset;
9015   dw_attr_ref at_bit_size;
9016   dw_attr_ref at_bit_stride;
9017   dw_attr_ref at_byte_size;
9018   dw_attr_ref at_byte_stride;
9019   dw_attr_ref at_const_value;
9020   dw_attr_ref at_containing_type;
9021   dw_attr_ref at_count;
9022   dw_attr_ref at_data_location;
9023   dw_attr_ref at_data_member_location;
9024   dw_attr_ref at_decimal_scale;
9025   dw_attr_ref at_decimal_sign;
9026   dw_attr_ref at_default_value;
9027   dw_attr_ref at_digit_count;
9028   dw_attr_ref at_discr;
9029   dw_attr_ref at_discr_list;
9030   dw_attr_ref at_discr_value;
9031   dw_attr_ref at_encoding;
9032   dw_attr_ref at_endianity;
9033   dw_attr_ref at_explicit;
9034   dw_attr_ref at_is_optional;
9035   dw_attr_ref at_location;
9036   dw_attr_ref at_lower_bound;
9037   dw_attr_ref at_mutable;
9038   dw_attr_ref at_ordering;
9039   dw_attr_ref at_picture_string;
9040   dw_attr_ref at_prototyped;
9041   dw_attr_ref at_small;
9042   dw_attr_ref at_segment;
9043   dw_attr_ref at_string_length;
9044   dw_attr_ref at_threads_scaled;
9045   dw_attr_ref at_upper_bound;
9046   dw_attr_ref at_use_location;
9047   dw_attr_ref at_use_UTF8;
9048   dw_attr_ref at_variable_parameter;
9049   dw_attr_ref at_virtuality;
9050   dw_attr_ref at_visibility;
9051   dw_attr_ref at_vtable_elem_location;
9052 };
9053
9054 /* Collect the attributes that we will want to use for the checksum.  */
9055
9056 static void
9057 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9058 {
9059   dw_attr_ref a;
9060   unsigned ix;
9061
9062   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9063     {
9064       switch (a->dw_attr)
9065         {
9066         case DW_AT_name:
9067           attrs->at_name = a;
9068           break;
9069         case DW_AT_type:
9070           attrs->at_type = a;
9071           break;
9072         case DW_AT_friend:
9073           attrs->at_friend = a;
9074           break;
9075         case DW_AT_accessibility:
9076           attrs->at_accessibility = a;
9077           break;
9078         case DW_AT_address_class:
9079           attrs->at_address_class = a;
9080           break;
9081         case DW_AT_allocated:
9082           attrs->at_allocated = a;
9083           break;
9084         case DW_AT_artificial:
9085           attrs->at_artificial = a;
9086           break;
9087         case DW_AT_associated:
9088           attrs->at_associated = a;
9089           break;
9090         case DW_AT_binary_scale:
9091           attrs->at_binary_scale = a;
9092           break;
9093         case DW_AT_bit_offset:
9094           attrs->at_bit_offset = a;
9095           break;
9096         case DW_AT_bit_size:
9097           attrs->at_bit_size = a;
9098           break;
9099         case DW_AT_bit_stride:
9100           attrs->at_bit_stride = a;
9101           break;
9102         case DW_AT_byte_size:
9103           attrs->at_byte_size = a;
9104           break;
9105         case DW_AT_byte_stride:
9106           attrs->at_byte_stride = a;
9107           break;
9108         case DW_AT_const_value:
9109           attrs->at_const_value = a;
9110           break;
9111         case DW_AT_containing_type:
9112           attrs->at_containing_type = a;
9113           break;
9114         case DW_AT_count:
9115           attrs->at_count = a;
9116           break;
9117         case DW_AT_data_location:
9118           attrs->at_data_location = a;
9119           break;
9120         case DW_AT_data_member_location:
9121           attrs->at_data_member_location = a;
9122           break;
9123         case DW_AT_decimal_scale:
9124           attrs->at_decimal_scale = a;
9125           break;
9126         case DW_AT_decimal_sign:
9127           attrs->at_decimal_sign = a;
9128           break;
9129         case DW_AT_default_value:
9130           attrs->at_default_value = a;
9131           break;
9132         case DW_AT_digit_count:
9133           attrs->at_digit_count = a;
9134           break;
9135         case DW_AT_discr:
9136           attrs->at_discr = a;
9137           break;
9138         case DW_AT_discr_list:
9139           attrs->at_discr_list = a;
9140           break;
9141         case DW_AT_discr_value:
9142           attrs->at_discr_value = a;
9143           break;
9144         case DW_AT_encoding:
9145           attrs->at_encoding = a;
9146           break;
9147         case DW_AT_endianity:
9148           attrs->at_endianity = a;
9149           break;
9150         case DW_AT_explicit:
9151           attrs->at_explicit = a;
9152           break;
9153         case DW_AT_is_optional:
9154           attrs->at_is_optional = a;
9155           break;
9156         case DW_AT_location:
9157           attrs->at_location = a;
9158           break;
9159         case DW_AT_lower_bound:
9160           attrs->at_lower_bound = a;
9161           break;
9162         case DW_AT_mutable:
9163           attrs->at_mutable = a;
9164           break;
9165         case DW_AT_ordering:
9166           attrs->at_ordering = a;
9167           break;
9168         case DW_AT_picture_string:
9169           attrs->at_picture_string = a;
9170           break;
9171         case DW_AT_prototyped:
9172           attrs->at_prototyped = a;
9173           break;
9174         case DW_AT_small:
9175           attrs->at_small = a;
9176           break;
9177         case DW_AT_segment:
9178           attrs->at_segment = a;
9179           break;
9180         case DW_AT_string_length:
9181           attrs->at_string_length = a;
9182           break;
9183         case DW_AT_threads_scaled:
9184           attrs->at_threads_scaled = a;
9185           break;
9186         case DW_AT_upper_bound:
9187           attrs->at_upper_bound = a;
9188           break;
9189         case DW_AT_use_location:
9190           attrs->at_use_location = a;
9191           break;
9192         case DW_AT_use_UTF8:
9193           attrs->at_use_UTF8 = a;
9194           break;
9195         case DW_AT_variable_parameter:
9196           attrs->at_variable_parameter = a;
9197           break;
9198         case DW_AT_virtuality:
9199           attrs->at_virtuality = a;
9200           break;
9201         case DW_AT_visibility:
9202           attrs->at_visibility = a;
9203           break;
9204         case DW_AT_vtable_elem_location:
9205           attrs->at_vtable_elem_location = a;
9206           break;
9207         default:
9208           break;
9209         }
9210     }
9211 }
9212
9213 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9214
9215 static void
9216 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9217 {
9218   dw_die_ref c;
9219   dw_die_ref decl;
9220   struct checksum_attributes attrs;
9221
9222   CHECKSUM_ULEB128 ('D');
9223   CHECKSUM_ULEB128 (die->die_tag);
9224
9225   memset (&attrs, 0, sizeof (attrs));
9226
9227   decl = get_AT_ref (die, DW_AT_specification);
9228   if (decl != NULL)
9229     collect_checksum_attributes (&attrs, decl);
9230   collect_checksum_attributes (&attrs, die);
9231
9232   CHECKSUM_ATTR (attrs.at_name);
9233   CHECKSUM_ATTR (attrs.at_accessibility);
9234   CHECKSUM_ATTR (attrs.at_address_class);
9235   CHECKSUM_ATTR (attrs.at_allocated);
9236   CHECKSUM_ATTR (attrs.at_artificial);
9237   CHECKSUM_ATTR (attrs.at_associated);
9238   CHECKSUM_ATTR (attrs.at_binary_scale);
9239   CHECKSUM_ATTR (attrs.at_bit_offset);
9240   CHECKSUM_ATTR (attrs.at_bit_size);
9241   CHECKSUM_ATTR (attrs.at_bit_stride);
9242   CHECKSUM_ATTR (attrs.at_byte_size);
9243   CHECKSUM_ATTR (attrs.at_byte_stride);
9244   CHECKSUM_ATTR (attrs.at_const_value);
9245   CHECKSUM_ATTR (attrs.at_containing_type);
9246   CHECKSUM_ATTR (attrs.at_count);
9247   CHECKSUM_ATTR (attrs.at_data_location);
9248   CHECKSUM_ATTR (attrs.at_data_member_location);
9249   CHECKSUM_ATTR (attrs.at_decimal_scale);
9250   CHECKSUM_ATTR (attrs.at_decimal_sign);
9251   CHECKSUM_ATTR (attrs.at_default_value);
9252   CHECKSUM_ATTR (attrs.at_digit_count);
9253   CHECKSUM_ATTR (attrs.at_discr);
9254   CHECKSUM_ATTR (attrs.at_discr_list);
9255   CHECKSUM_ATTR (attrs.at_discr_value);
9256   CHECKSUM_ATTR (attrs.at_encoding);
9257   CHECKSUM_ATTR (attrs.at_endianity);
9258   CHECKSUM_ATTR (attrs.at_explicit);
9259   CHECKSUM_ATTR (attrs.at_is_optional);
9260   CHECKSUM_ATTR (attrs.at_location);
9261   CHECKSUM_ATTR (attrs.at_lower_bound);
9262   CHECKSUM_ATTR (attrs.at_mutable);
9263   CHECKSUM_ATTR (attrs.at_ordering);
9264   CHECKSUM_ATTR (attrs.at_picture_string);
9265   CHECKSUM_ATTR (attrs.at_prototyped);
9266   CHECKSUM_ATTR (attrs.at_small);
9267   CHECKSUM_ATTR (attrs.at_segment);
9268   CHECKSUM_ATTR (attrs.at_string_length);
9269   CHECKSUM_ATTR (attrs.at_threads_scaled);
9270   CHECKSUM_ATTR (attrs.at_upper_bound);
9271   CHECKSUM_ATTR (attrs.at_use_location);
9272   CHECKSUM_ATTR (attrs.at_use_UTF8);
9273   CHECKSUM_ATTR (attrs.at_variable_parameter);
9274   CHECKSUM_ATTR (attrs.at_virtuality);
9275   CHECKSUM_ATTR (attrs.at_visibility);
9276   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9277   CHECKSUM_ATTR (attrs.at_type);
9278   CHECKSUM_ATTR (attrs.at_friend);
9279
9280   /* Checksum the child DIEs, except for nested types and member functions.  */
9281   c = die->die_child;
9282   if (c) do {
9283     dw_attr_ref name_attr;
9284
9285     c = c->die_sib;
9286     name_attr = get_AT (c, DW_AT_name);
9287     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9288         && name_attr != NULL)
9289       {
9290         CHECKSUM_ULEB128 ('S');
9291         CHECKSUM_ULEB128 (c->die_tag);
9292         CHECKSUM_STRING (AT_string (name_attr));
9293       }
9294     else
9295       {
9296         /* Mark this DIE so it gets processed when unmarking.  */
9297         if (c->die_mark == 0)
9298           c->die_mark = -1;
9299         die_checksum_ordered (c, ctx, mark);
9300       }
9301   } while (c != die->die_child);
9302
9303   CHECKSUM_ULEB128 (0);
9304 }
9305
9306 #undef CHECKSUM
9307 #undef CHECKSUM_STRING
9308 #undef CHECKSUM_ATTR
9309 #undef CHECKSUM_LEB128
9310 #undef CHECKSUM_ULEB128
9311
9312 /* Generate the type signature for DIE.  This is computed by generating an
9313    MD5 checksum over the DIE's tag, its relevant attributes, and its
9314    children.  Attributes that are references to other DIEs are processed
9315    by recursion, using the MARK field to prevent infinite recursion.
9316    If the DIE is nested inside a namespace or another type, we also
9317    need to include that context in the signature.  The lower 64 bits
9318    of the resulting MD5 checksum comprise the signature.  */
9319
9320 static void
9321 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9322 {
9323   int mark;
9324   const char *name;
9325   unsigned char checksum[16];
9326   struct md5_ctx ctx;
9327   dw_die_ref decl;
9328
9329   name = get_AT_string (die, DW_AT_name);
9330   decl = get_AT_ref (die, DW_AT_specification);
9331
9332   /* First, compute a signature for just the type name (and its surrounding
9333      context, if any.  This is stored in the type unit DIE for link-time
9334      ODR (one-definition rule) checking.  */
9335
9336   if (is_cxx() && name != NULL)
9337     {
9338       md5_init_ctx (&ctx);
9339
9340       /* Checksum the names of surrounding namespaces and structures.  */
9341       if (decl != NULL && decl->die_parent != NULL)
9342         checksum_die_context (decl->die_parent, &ctx);
9343
9344       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9345       md5_process_bytes (name, strlen (name) + 1, &ctx);
9346       md5_finish_ctx (&ctx, checksum);
9347
9348       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9349     }
9350
9351   /* Next, compute the complete type signature.  */
9352
9353   md5_init_ctx (&ctx);
9354   mark = 1;
9355   die->die_mark = mark;
9356
9357   /* Checksum the names of surrounding namespaces and structures.  */
9358   if (decl != NULL && decl->die_parent != NULL)
9359     checksum_die_context (decl->die_parent, &ctx);
9360
9361   /* Checksum the DIE and its children.  */
9362   die_checksum_ordered (die, &ctx, &mark);
9363   unmark_all_dies (die);
9364   md5_finish_ctx (&ctx, checksum);
9365
9366   /* Store the signature in the type node and link the type DIE and the
9367      type node together.  */
9368   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9369           DWARF_TYPE_SIGNATURE_SIZE);
9370   die->die_id.die_type_node = type_node;
9371   type_node->type_die = die;
9372
9373   /* If the DIE is a specification, link its declaration to the type node
9374      as well.  */
9375   if (decl != NULL)
9376     decl->die_id.die_type_node = type_node;
9377 }
9378
9379 /* Do the location expressions look same?  */
9380 static inline int
9381 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9382 {
9383   return loc1->dw_loc_opc == loc2->dw_loc_opc
9384          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9385          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9386 }
9387
9388 /* Do the values look the same?  */
9389 static int
9390 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9391 {
9392   dw_loc_descr_ref loc1, loc2;
9393   rtx r1, r2;
9394
9395   if (v1->val_class != v2->val_class)
9396     return 0;
9397
9398   switch (v1->val_class)
9399     {
9400     case dw_val_class_const:
9401       return v1->v.val_int == v2->v.val_int;
9402     case dw_val_class_unsigned_const:
9403       return v1->v.val_unsigned == v2->v.val_unsigned;
9404     case dw_val_class_const_double:
9405       return v1->v.val_double.high == v2->v.val_double.high
9406              && v1->v.val_double.low == v2->v.val_double.low;
9407     case dw_val_class_vec:
9408       if (v1->v.val_vec.length != v2->v.val_vec.length
9409           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9410         return 0;
9411       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9412                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9413         return 0;
9414       return 1;
9415     case dw_val_class_flag:
9416       return v1->v.val_flag == v2->v.val_flag;
9417     case dw_val_class_str:
9418       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9419
9420     case dw_val_class_addr:
9421       r1 = v1->v.val_addr;
9422       r2 = v2->v.val_addr;
9423       if (GET_CODE (r1) != GET_CODE (r2))
9424         return 0;
9425       return !rtx_equal_p (r1, r2);
9426
9427     case dw_val_class_offset:
9428       return v1->v.val_offset == v2->v.val_offset;
9429
9430     case dw_val_class_loc:
9431       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9432            loc1 && loc2;
9433            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9434         if (!same_loc_p (loc1, loc2, mark))
9435           return 0;
9436       return !loc1 && !loc2;
9437
9438     case dw_val_class_die_ref:
9439       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9440
9441     case dw_val_class_fde_ref:
9442     case dw_val_class_vms_delta:
9443     case dw_val_class_lbl_id:
9444     case dw_val_class_lineptr:
9445     case dw_val_class_macptr:
9446       return 1;
9447
9448     case dw_val_class_file:
9449       return v1->v.val_file == v2->v.val_file;
9450
9451     case dw_val_class_data8:
9452       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9453
9454     default:
9455       return 1;
9456     }
9457 }
9458
9459 /* Do the attributes look the same?  */
9460
9461 static int
9462 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9463 {
9464   if (at1->dw_attr != at2->dw_attr)
9465     return 0;
9466
9467   /* We don't care that this was compiled with a different compiler
9468      snapshot; if the output is the same, that's what matters. */
9469   if (at1->dw_attr == DW_AT_producer)
9470     return 1;
9471
9472   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9473 }
9474
9475 /* Do the dies look the same?  */
9476
9477 static int
9478 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9479 {
9480   dw_die_ref c1, c2;
9481   dw_attr_ref a1;
9482   unsigned ix;
9483
9484   /* To avoid infinite recursion.  */
9485   if (die1->die_mark)
9486     return die1->die_mark == die2->die_mark;
9487   die1->die_mark = die2->die_mark = ++(*mark);
9488
9489   if (die1->die_tag != die2->die_tag)
9490     return 0;
9491
9492   if (VEC_length (dw_attr_node, die1->die_attr)
9493       != VEC_length (dw_attr_node, die2->die_attr))
9494     return 0;
9495
9496   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9497     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9498       return 0;
9499
9500   c1 = die1->die_child;
9501   c2 = die2->die_child;
9502   if (! c1)
9503     {
9504       if (c2)
9505         return 0;
9506     }
9507   else
9508     for (;;)
9509       {
9510         if (!same_die_p (c1, c2, mark))
9511           return 0;
9512         c1 = c1->die_sib;
9513         c2 = c2->die_sib;
9514         if (c1 == die1->die_child)
9515           {
9516             if (c2 == die2->die_child)
9517               break;
9518             else
9519               return 0;
9520           }
9521     }
9522
9523   return 1;
9524 }
9525
9526 /* Do the dies look the same?  Wrapper around same_die_p.  */
9527
9528 static int
9529 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9530 {
9531   int mark = 0;
9532   int ret = same_die_p (die1, die2, &mark);
9533
9534   unmark_all_dies (die1);
9535   unmark_all_dies (die2);
9536
9537   return ret;
9538 }
9539
9540 /* The prefix to attach to symbols on DIEs in the current comdat debug
9541    info section.  */
9542 static char *comdat_symbol_id;
9543
9544 /* The index of the current symbol within the current comdat CU.  */
9545 static unsigned int comdat_symbol_number;
9546
9547 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9548    children, and set comdat_symbol_id accordingly.  */
9549
9550 static void
9551 compute_section_prefix (dw_die_ref unit_die)
9552 {
9553   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9554   const char *base = die_name ? lbasename (die_name) : "anonymous";
9555   char *name = XALLOCAVEC (char, strlen (base) + 64);
9556   char *p;
9557   int i, mark;
9558   unsigned char checksum[16];
9559   struct md5_ctx ctx;
9560
9561   /* Compute the checksum of the DIE, then append part of it as hex digits to
9562      the name filename of the unit.  */
9563
9564   md5_init_ctx (&ctx);
9565   mark = 0;
9566   die_checksum (unit_die, &ctx, &mark);
9567   unmark_all_dies (unit_die);
9568   md5_finish_ctx (&ctx, checksum);
9569
9570   sprintf (name, "%s.", base);
9571   clean_symbol_name (name);
9572
9573   p = name + strlen (name);
9574   for (i = 0; i < 4; i++)
9575     {
9576       sprintf (p, "%.2x", checksum[i]);
9577       p += 2;
9578     }
9579
9580   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9581   comdat_symbol_number = 0;
9582 }
9583
9584 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9585
9586 static int
9587 is_type_die (dw_die_ref die)
9588 {
9589   switch (die->die_tag)
9590     {
9591     case DW_TAG_array_type:
9592     case DW_TAG_class_type:
9593     case DW_TAG_interface_type:
9594     case DW_TAG_enumeration_type:
9595     case DW_TAG_pointer_type:
9596     case DW_TAG_reference_type:
9597     case DW_TAG_rvalue_reference_type:
9598     case DW_TAG_string_type:
9599     case DW_TAG_structure_type:
9600     case DW_TAG_subroutine_type:
9601     case DW_TAG_union_type:
9602     case DW_TAG_ptr_to_member_type:
9603     case DW_TAG_set_type:
9604     case DW_TAG_subrange_type:
9605     case DW_TAG_base_type:
9606     case DW_TAG_const_type:
9607     case DW_TAG_file_type:
9608     case DW_TAG_packed_type:
9609     case DW_TAG_volatile_type:
9610     case DW_TAG_typedef:
9611       return 1;
9612     default:
9613       return 0;
9614     }
9615 }
9616
9617 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9618    Basically, we want to choose the bits that are likely to be shared between
9619    compilations (types) and leave out the bits that are specific to individual
9620    compilations (functions).  */
9621
9622 static int
9623 is_comdat_die (dw_die_ref c)
9624 {
9625   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9626      we do for stabs.  The advantage is a greater likelihood of sharing between
9627      objects that don't include headers in the same order (and therefore would
9628      put the base types in a different comdat).  jason 8/28/00 */
9629
9630   if (c->die_tag == DW_TAG_base_type)
9631     return 0;
9632
9633   if (c->die_tag == DW_TAG_pointer_type
9634       || c->die_tag == DW_TAG_reference_type
9635       || c->die_tag == DW_TAG_rvalue_reference_type
9636       || c->die_tag == DW_TAG_const_type
9637       || c->die_tag == DW_TAG_volatile_type)
9638     {
9639       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9640
9641       return t ? is_comdat_die (t) : 0;
9642     }
9643
9644   return is_type_die (c);
9645 }
9646
9647 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9648    compilation unit.  */
9649
9650 static int
9651 is_symbol_die (dw_die_ref c)
9652 {
9653   return (is_type_die (c)
9654           || is_declaration_die (c)
9655           || c->die_tag == DW_TAG_namespace
9656           || c->die_tag == DW_TAG_module);
9657 }
9658
9659 /* Returns true iff C is a compile-unit DIE.  */
9660
9661 static inline bool
9662 is_cu_die (dw_die_ref c)
9663 {
9664   return c && c->die_tag == DW_TAG_compile_unit;
9665 }
9666
9667 static char *
9668 gen_internal_sym (const char *prefix)
9669 {
9670   char buf[256];
9671
9672   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9673   return xstrdup (buf);
9674 }
9675
9676 /* Assign symbols to all worthy DIEs under DIE.  */
9677
9678 static void
9679 assign_symbol_names (dw_die_ref die)
9680 {
9681   dw_die_ref c;
9682
9683   if (is_symbol_die (die))
9684     {
9685       if (comdat_symbol_id)
9686         {
9687           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9688
9689           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9690                    comdat_symbol_id, comdat_symbol_number++);
9691           die->die_id.die_symbol = xstrdup (p);
9692         }
9693       else
9694         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9695     }
9696
9697   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9698 }
9699
9700 struct cu_hash_table_entry
9701 {
9702   dw_die_ref cu;
9703   unsigned min_comdat_num, max_comdat_num;
9704   struct cu_hash_table_entry *next;
9705 };
9706
9707 /* Routines to manipulate hash table of CUs.  */
9708 static hashval_t
9709 htab_cu_hash (const void *of)
9710 {
9711   const struct cu_hash_table_entry *const entry =
9712     (const struct cu_hash_table_entry *) of;
9713
9714   return htab_hash_string (entry->cu->die_id.die_symbol);
9715 }
9716
9717 static int
9718 htab_cu_eq (const void *of1, const void *of2)
9719 {
9720   const struct cu_hash_table_entry *const entry1 =
9721     (const struct cu_hash_table_entry *) of1;
9722   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9723
9724   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9725 }
9726
9727 static void
9728 htab_cu_del (void *what)
9729 {
9730   struct cu_hash_table_entry *next,
9731     *entry = (struct cu_hash_table_entry *) what;
9732
9733   while (entry)
9734     {
9735       next = entry->next;
9736       free (entry);
9737       entry = next;
9738     }
9739 }
9740
9741 /* Check whether we have already seen this CU and set up SYM_NUM
9742    accordingly.  */
9743 static int
9744 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9745 {
9746   struct cu_hash_table_entry dummy;
9747   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9748
9749   dummy.max_comdat_num = 0;
9750
9751   slot = (struct cu_hash_table_entry **)
9752     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9753         INSERT);
9754   entry = *slot;
9755
9756   for (; entry; last = entry, entry = entry->next)
9757     {
9758       if (same_die_p_wrap (cu, entry->cu))
9759         break;
9760     }
9761
9762   if (entry)
9763     {
9764       *sym_num = entry->min_comdat_num;
9765       return 1;
9766     }
9767
9768   entry = XCNEW (struct cu_hash_table_entry);
9769   entry->cu = cu;
9770   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9771   entry->next = *slot;
9772   *slot = entry;
9773
9774   return 0;
9775 }
9776
9777 /* Record SYM_NUM to record of CU in HTABLE.  */
9778 static void
9779 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9780 {
9781   struct cu_hash_table_entry **slot, *entry;
9782
9783   slot = (struct cu_hash_table_entry **)
9784     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9785         NO_INSERT);
9786   entry = *slot;
9787
9788   entry->max_comdat_num = sym_num;
9789 }
9790
9791 /* Traverse the DIE (which is always comp_unit_die), and set up
9792    additional compilation units for each of the include files we see
9793    bracketed by BINCL/EINCL.  */
9794
9795 static void
9796 break_out_includes (dw_die_ref die)
9797 {
9798   dw_die_ref c;
9799   dw_die_ref unit = NULL;
9800   limbo_die_node *node, **pnode;
9801   htab_t cu_hash_table;
9802
9803   c = die->die_child;
9804   if (c) do {
9805     dw_die_ref prev = c;
9806     c = c->die_sib;
9807     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9808            || (unit && is_comdat_die (c)))
9809       {
9810         dw_die_ref next = c->die_sib;
9811
9812         /* This DIE is for a secondary CU; remove it from the main one.  */
9813         remove_child_with_prev (c, prev);
9814
9815         if (c->die_tag == DW_TAG_GNU_BINCL)
9816           unit = push_new_compile_unit (unit, c);
9817         else if (c->die_tag == DW_TAG_GNU_EINCL)
9818           unit = pop_compile_unit (unit);
9819         else
9820           add_child_die (unit, c);
9821         c = next;
9822         if (c == die->die_child)
9823           break;
9824       }
9825   } while (c != die->die_child);
9826
9827 #if 0
9828   /* We can only use this in debugging, since the frontend doesn't check
9829      to make sure that we leave every include file we enter.  */
9830   gcc_assert (!unit);
9831 #endif
9832
9833   assign_symbol_names (die);
9834   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9835   for (node = limbo_die_list, pnode = &limbo_die_list;
9836        node;
9837        node = node->next)
9838     {
9839       int is_dupl;
9840
9841       compute_section_prefix (node->die);
9842       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9843                         &comdat_symbol_number);
9844       assign_symbol_names (node->die);
9845       if (is_dupl)
9846         *pnode = node->next;
9847       else
9848         {
9849           pnode = &node->next;
9850           record_comdat_symbol_number (node->die, cu_hash_table,
9851                 comdat_symbol_number);
9852         }
9853     }
9854   htab_delete (cu_hash_table);
9855 }
9856
9857 /* Return non-zero if this DIE is a declaration.  */
9858
9859 static int
9860 is_declaration_die (dw_die_ref die)
9861 {
9862   dw_attr_ref a;
9863   unsigned ix;
9864
9865   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9866     if (a->dw_attr == DW_AT_declaration)
9867       return 1;
9868
9869   return 0;
9870 }
9871
9872 /* Return non-zero if this DIE is nested inside a subprogram.  */
9873
9874 static int
9875 is_nested_in_subprogram (dw_die_ref die)
9876 {
9877   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9878
9879   if (decl == NULL)
9880     decl = die;
9881   return local_scope_p (decl);
9882 }
9883
9884 /* Return non-zero if this is a type DIE that should be moved to a
9885    COMDAT .debug_types section.  */
9886
9887 static int
9888 should_move_die_to_comdat (dw_die_ref die)
9889 {
9890   switch (die->die_tag)
9891     {
9892     case DW_TAG_class_type:
9893     case DW_TAG_structure_type:
9894     case DW_TAG_enumeration_type:
9895     case DW_TAG_union_type:
9896       /* Don't move declarations, inlined instances, or types nested in a
9897          subprogram.  */
9898       if (is_declaration_die (die)
9899           || get_AT (die, DW_AT_abstract_origin)
9900           || is_nested_in_subprogram (die))
9901         return 0;
9902       return 1;
9903     case DW_TAG_array_type:
9904     case DW_TAG_interface_type:
9905     case DW_TAG_pointer_type:
9906     case DW_TAG_reference_type:
9907     case DW_TAG_rvalue_reference_type:
9908     case DW_TAG_string_type:
9909     case DW_TAG_subroutine_type:
9910     case DW_TAG_ptr_to_member_type:
9911     case DW_TAG_set_type:
9912     case DW_TAG_subrange_type:
9913     case DW_TAG_base_type:
9914     case DW_TAG_const_type:
9915     case DW_TAG_file_type:
9916     case DW_TAG_packed_type:
9917     case DW_TAG_volatile_type:
9918     case DW_TAG_typedef:
9919     default:
9920       return 0;
9921     }
9922 }
9923
9924 /* Make a clone of DIE.  */
9925
9926 static dw_die_ref
9927 clone_die (dw_die_ref die)
9928 {
9929   dw_die_ref clone;
9930   dw_attr_ref a;
9931   unsigned ix;
9932
9933   clone = ggc_alloc_cleared_die_node ();
9934   clone->die_tag = die->die_tag;
9935
9936   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9937     add_dwarf_attr (clone, a);
9938
9939   return clone;
9940 }
9941
9942 /* Make a clone of the tree rooted at DIE.  */
9943
9944 static dw_die_ref
9945 clone_tree (dw_die_ref die)
9946 {
9947   dw_die_ref c;
9948   dw_die_ref clone = clone_die (die);
9949
9950   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9951
9952   return clone;
9953 }
9954
9955 /* Make a clone of DIE as a declaration.  */
9956
9957 static dw_die_ref
9958 clone_as_declaration (dw_die_ref die)
9959 {
9960   dw_die_ref clone;
9961   dw_die_ref decl;
9962   dw_attr_ref a;
9963   unsigned ix;
9964
9965   /* If the DIE is already a declaration, just clone it.  */
9966   if (is_declaration_die (die))
9967     return clone_die (die);
9968
9969   /* If the DIE is a specification, just clone its declaration DIE.  */
9970   decl = get_AT_ref (die, DW_AT_specification);
9971   if (decl != NULL)
9972     return clone_die (decl);
9973
9974   clone = ggc_alloc_cleared_die_node ();
9975   clone->die_tag = die->die_tag;
9976
9977   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9978     {
9979       /* We don't want to copy over all attributes.
9980          For example we don't want DW_AT_byte_size because otherwise we will no
9981          longer have a declaration and GDB will treat it as a definition.  */
9982
9983       switch (a->dw_attr)
9984         {
9985         case DW_AT_artificial:
9986         case DW_AT_containing_type:
9987         case DW_AT_external:
9988         case DW_AT_name:
9989         case DW_AT_type:
9990         case DW_AT_virtuality:
9991         case DW_AT_linkage_name:
9992         case DW_AT_MIPS_linkage_name:
9993           add_dwarf_attr (clone, a);
9994           break;
9995         case DW_AT_byte_size:
9996         default:
9997           break;
9998         }
9999     }
10000
10001   if (die->die_id.die_type_node)
10002     add_AT_die_ref (clone, DW_AT_signature, die);
10003
10004   add_AT_flag (clone, DW_AT_declaration, 1);
10005   return clone;
10006 }
10007
10008 /* Copy the declaration context to the new compile unit DIE.  This includes
10009    any surrounding namespace or type declarations.  If the DIE has an
10010    AT_specification attribute, it also includes attributes and children
10011    attached to the specification.  */
10012
10013 static void
10014 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10015 {
10016   dw_die_ref decl;
10017   dw_die_ref new_decl;
10018
10019   decl = get_AT_ref (die, DW_AT_specification);
10020   if (decl == NULL)
10021     decl = die;
10022   else
10023     {
10024       unsigned ix;
10025       dw_die_ref c;
10026       dw_attr_ref a;
10027
10028       /* Copy the type node pointer from the new DIE to the original
10029          declaration DIE so we can forward references later.  */
10030       decl->die_id.die_type_node = die->die_id.die_type_node;
10031
10032       remove_AT (die, DW_AT_specification);
10033
10034       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10035         {
10036           if (a->dw_attr != DW_AT_name
10037               && a->dw_attr != DW_AT_declaration
10038               && a->dw_attr != DW_AT_external)
10039             add_dwarf_attr (die, a);
10040         }
10041
10042       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10043     }
10044
10045   if (decl->die_parent != NULL
10046       && decl->die_parent->die_tag != DW_TAG_compile_unit
10047       && decl->die_parent->die_tag != DW_TAG_type_unit)
10048     {
10049       new_decl = copy_ancestor_tree (unit, decl, NULL);
10050       if (new_decl != NULL)
10051         {
10052           remove_AT (new_decl, DW_AT_signature);
10053           add_AT_specification (die, new_decl);
10054         }
10055     }
10056 }
10057
10058 /* Generate the skeleton ancestor tree for the given NODE, then clone
10059    the DIE and add the clone into the tree.  */
10060
10061 static void
10062 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10063 {
10064   if (node->new_die != NULL)
10065     return;
10066
10067   node->new_die = clone_as_declaration (node->old_die);
10068
10069   if (node->parent != NULL)
10070     {
10071       generate_skeleton_ancestor_tree (node->parent);
10072       add_child_die (node->parent->new_die, node->new_die);
10073     }
10074 }
10075
10076 /* Generate a skeleton tree of DIEs containing any declarations that are
10077    found in the original tree.  We traverse the tree looking for declaration
10078    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10079
10080 static void
10081 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10082 {
10083   skeleton_chain_node node;
10084   dw_die_ref c;
10085   dw_die_ref first;
10086   dw_die_ref prev = NULL;
10087   dw_die_ref next = NULL;
10088
10089   node.parent = parent;
10090
10091   first = c = parent->old_die->die_child;
10092   if (c)
10093     next = c->die_sib;
10094   if (c) do {
10095     if (prev == NULL || prev->die_sib == c)
10096       prev = c;
10097     c = next;
10098     next = (c == first ? NULL : c->die_sib);
10099     node.old_die = c;
10100     node.new_die = NULL;
10101     if (is_declaration_die (c))
10102       {
10103         /* Clone the existing DIE, move the original to the skeleton
10104            tree (which is in the main CU), and put the clone, with
10105            all the original's children, where the original came from.  */
10106         dw_die_ref clone = clone_die (c);
10107         move_all_children (c, clone);
10108
10109         replace_child (c, clone, prev);
10110         generate_skeleton_ancestor_tree (parent);
10111         add_child_die (parent->new_die, c);
10112         node.new_die = c;
10113         c = clone;
10114       }
10115     generate_skeleton_bottom_up (&node);
10116   } while (next != NULL);
10117 }
10118
10119 /* Wrapper function for generate_skeleton_bottom_up.  */
10120
10121 static dw_die_ref
10122 generate_skeleton (dw_die_ref die)
10123 {
10124   skeleton_chain_node node;
10125
10126   node.old_die = die;
10127   node.new_die = NULL;
10128   node.parent = NULL;
10129
10130   /* If this type definition is nested inside another type,
10131      always leave at least a declaration in its place.  */
10132   if (die->die_parent != NULL && is_type_die (die->die_parent))
10133     node.new_die = clone_as_declaration (die);
10134
10135   generate_skeleton_bottom_up (&node);
10136   return node.new_die;
10137 }
10138
10139 /* Remove the DIE from its parent, possibly replacing it with a cloned
10140    declaration.  The original DIE will be moved to a new compile unit
10141    so that existing references to it follow it to the new location.  If
10142    any of the original DIE's descendants is a declaration, we need to
10143    replace the original DIE with a skeleton tree and move the
10144    declarations back into the skeleton tree.  */
10145
10146 static dw_die_ref
10147 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10148 {
10149   dw_die_ref skeleton;
10150
10151   skeleton = generate_skeleton (child);
10152   if (skeleton == NULL)
10153     remove_child_with_prev (child, prev);
10154   else
10155     {
10156       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10157       replace_child (child, skeleton, prev);
10158     }
10159
10160   return skeleton;
10161 }
10162
10163 /* Traverse the DIE and set up additional .debug_types sections for each
10164    type worthy of being placed in a COMDAT section.  */
10165
10166 static void
10167 break_out_comdat_types (dw_die_ref die)
10168 {
10169   dw_die_ref c;
10170   dw_die_ref first;
10171   dw_die_ref prev = NULL;
10172   dw_die_ref next = NULL;
10173   dw_die_ref unit = NULL;
10174
10175   first = c = die->die_child;
10176   if (c)
10177     next = c->die_sib;
10178   if (c) do {
10179     if (prev == NULL || prev->die_sib == c)
10180       prev = c;
10181     c = next;
10182     next = (c == first ? NULL : c->die_sib);
10183     if (should_move_die_to_comdat (c))
10184       {
10185         dw_die_ref replacement;
10186         comdat_type_node_ref type_node;
10187
10188         /* Create a new type unit DIE as the root for the new tree, and
10189            add it to the list of comdat types.  */
10190         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10191         add_AT_unsigned (unit, DW_AT_language,
10192                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10193         type_node = ggc_alloc_cleared_comdat_type_node ();
10194         type_node->root_die = unit;
10195         type_node->next = comdat_type_list;
10196         comdat_type_list = type_node;
10197
10198         /* Generate the type signature.  */
10199         generate_type_signature (c, type_node);
10200
10201         /* Copy the declaration context, attributes, and children of the
10202            declaration into the new compile unit DIE.  */
10203         copy_declaration_context (unit, c);
10204
10205         /* Remove this DIE from the main CU.  */
10206         replacement = remove_child_or_replace_with_skeleton (c, prev);
10207
10208         /* Break out nested types into their own type units.  */
10209         break_out_comdat_types (c);
10210
10211         /* Add the DIE to the new compunit.  */
10212         add_child_die (unit, c);
10213
10214         if (replacement != NULL)
10215           c = replacement;
10216       }
10217     else if (c->die_tag == DW_TAG_namespace
10218              || c->die_tag == DW_TAG_class_type
10219              || c->die_tag == DW_TAG_structure_type
10220              || c->die_tag == DW_TAG_union_type)
10221       {
10222         /* Look for nested types that can be broken out.  */
10223         break_out_comdat_types (c);
10224       }
10225   } while (next != NULL);
10226 }
10227
10228 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10229
10230 struct decl_table_entry
10231 {
10232   dw_die_ref orig;
10233   dw_die_ref copy;
10234 };
10235
10236 /* Routines to manipulate hash table of copied declarations.  */
10237
10238 static hashval_t
10239 htab_decl_hash (const void *of)
10240 {
10241   const struct decl_table_entry *const entry =
10242     (const struct decl_table_entry *) of;
10243
10244   return htab_hash_pointer (entry->orig);
10245 }
10246
10247 static int
10248 htab_decl_eq (const void *of1, const void *of2)
10249 {
10250   const struct decl_table_entry *const entry1 =
10251     (const struct decl_table_entry *) of1;
10252   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10253
10254   return entry1->orig == entry2;
10255 }
10256
10257 static void
10258 htab_decl_del (void *what)
10259 {
10260   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10261
10262   free (entry);
10263 }
10264
10265 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10266    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10267    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10268    to check if the ancestor has already been copied into UNIT.  */
10269
10270 static dw_die_ref
10271 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10272 {
10273   dw_die_ref parent = die->die_parent;
10274   dw_die_ref new_parent = unit;
10275   dw_die_ref copy;
10276   void **slot = NULL;
10277   struct decl_table_entry *entry = NULL;
10278
10279   if (decl_table)
10280     {
10281       /* Check if the entry has already been copied to UNIT.  */
10282       slot = htab_find_slot_with_hash (decl_table, die,
10283                                        htab_hash_pointer (die), INSERT);
10284       if (*slot != HTAB_EMPTY_ENTRY)
10285         {
10286           entry = (struct decl_table_entry *) *slot;
10287           return entry->copy;
10288         }
10289
10290       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10291       entry = XCNEW (struct decl_table_entry);
10292       entry->orig = die;
10293       entry->copy = NULL;
10294       *slot = entry;
10295     }
10296
10297   if (parent != NULL)
10298     {
10299       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10300       if (spec != NULL)
10301         parent = spec;
10302       if (parent->die_tag != DW_TAG_compile_unit
10303           && parent->die_tag != DW_TAG_type_unit)
10304         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10305     }
10306
10307   copy = clone_as_declaration (die);
10308   add_child_die (new_parent, copy);
10309
10310   if (decl_table != NULL)
10311     {
10312       /* Record the pointer to the copy.  */
10313       entry->copy = copy;
10314     }
10315
10316   return copy;
10317 }
10318
10319 /* Walk the DIE and its children, looking for references to incomplete
10320    or trivial types that are unmarked (i.e., that are not in the current
10321    type_unit).  */
10322
10323 static void
10324 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10325 {
10326   dw_die_ref c;
10327   dw_attr_ref a;
10328   unsigned ix;
10329
10330   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10331     {
10332       if (AT_class (a) == dw_val_class_die_ref)
10333         {
10334           dw_die_ref targ = AT_ref (a);
10335           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10336           void **slot;
10337           struct decl_table_entry *entry;
10338
10339           if (targ->die_mark != 0 || type_node != NULL)
10340             continue;
10341
10342           slot = htab_find_slot_with_hash (decl_table, targ,
10343                                            htab_hash_pointer (targ), INSERT);
10344
10345           if (*slot != HTAB_EMPTY_ENTRY)
10346             {
10347               /* TARG has already been copied, so we just need to
10348                  modify the reference to point to the copy.  */
10349               entry = (struct decl_table_entry *) *slot;
10350               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10351             }
10352           else
10353             {
10354               dw_die_ref parent = unit;
10355               dw_die_ref copy = clone_tree (targ);
10356
10357               /* Make sure the cloned tree is marked as part of the
10358                  type unit.  */
10359               mark_dies (copy);
10360
10361               /* Record in DECL_TABLE that TARG has been copied.
10362                  Need to do this now, before the recursive call,
10363                  because DECL_TABLE may be expanded and SLOT
10364                  would no longer be a valid pointer.  */
10365               entry = XCNEW (struct decl_table_entry);
10366               entry->orig = targ;
10367               entry->copy = copy;
10368               *slot = entry;
10369
10370               /* If TARG has surrounding context, copy its ancestor tree
10371                  into the new type unit.  */
10372               if (targ->die_parent != NULL
10373                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10374                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10375                 parent = copy_ancestor_tree (unit, targ->die_parent,
10376                                              decl_table);
10377
10378               add_child_die (parent, copy);
10379               a->dw_attr_val.v.val_die_ref.die = copy;
10380
10381               /* Make sure the newly-copied DIE is walked.  If it was
10382                  installed in a previously-added context, it won't
10383                  get visited otherwise.  */
10384               if (parent != unit)
10385                 {
10386                   /* Find the highest point of the newly-added tree,
10387                      mark each node along the way, and walk from there.  */
10388                   parent->die_mark = 1;
10389                   while (parent->die_parent
10390                          && parent->die_parent->die_mark == 0)
10391                     {
10392                       parent = parent->die_parent;
10393                       parent->die_mark = 1;
10394                     }
10395                   copy_decls_walk (unit, parent, decl_table);
10396                 }
10397             }
10398         }
10399     }
10400
10401   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10402 }
10403
10404 /* Copy declarations for "unworthy" types into the new comdat section.
10405    Incomplete types, modified types, and certain other types aren't broken
10406    out into comdat sections of their own, so they don't have a signature,
10407    and we need to copy the declaration into the same section so that we
10408    don't have an external reference.  */
10409
10410 static void
10411 copy_decls_for_unworthy_types (dw_die_ref unit)
10412 {
10413   htab_t decl_table;
10414
10415   mark_dies (unit);
10416   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10417   copy_decls_walk (unit, unit, decl_table);
10418   htab_delete (decl_table);
10419   unmark_dies (unit);
10420 }
10421
10422 /* Traverse the DIE and add a sibling attribute if it may have the
10423    effect of speeding up access to siblings.  To save some space,
10424    avoid generating sibling attributes for DIE's without children.  */
10425
10426 static void
10427 add_sibling_attributes (dw_die_ref die)
10428 {
10429   dw_die_ref c;
10430
10431   if (! die->die_child)
10432     return;
10433
10434   if (die->die_parent && die != die->die_parent->die_child)
10435     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10436
10437   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10438 }
10439
10440 /* Output all location lists for the DIE and its children.  */
10441
10442 static void
10443 output_location_lists (dw_die_ref die)
10444 {
10445   dw_die_ref c;
10446   dw_attr_ref a;
10447   unsigned ix;
10448
10449   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10450     if (AT_class (a) == dw_val_class_loc_list)
10451       output_loc_list (AT_loc_list (a));
10452
10453   FOR_EACH_CHILD (die, c, output_location_lists (c));
10454 }
10455
10456 /* The format of each DIE (and its attribute value pairs) is encoded in an
10457    abbreviation table.  This routine builds the abbreviation table and assigns
10458    a unique abbreviation id for each abbreviation entry.  The children of each
10459    die are visited recursively.  */
10460
10461 static void
10462 build_abbrev_table (dw_die_ref die)
10463 {
10464   unsigned long abbrev_id;
10465   unsigned int n_alloc;
10466   dw_die_ref c;
10467   dw_attr_ref a;
10468   unsigned ix;
10469
10470   /* Scan the DIE references, and mark as external any that refer to
10471      DIEs from other CUs (i.e. those which are not marked).  */
10472   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10473     if (AT_class (a) == dw_val_class_die_ref
10474         && AT_ref (a)->die_mark == 0)
10475       {
10476         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10477         set_AT_ref_external (a, 1);
10478       }
10479
10480   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10481     {
10482       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10483       dw_attr_ref die_a, abbrev_a;
10484       unsigned ix;
10485       bool ok = true;
10486
10487       if (abbrev->die_tag != die->die_tag)
10488         continue;
10489       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10490         continue;
10491
10492       if (VEC_length (dw_attr_node, abbrev->die_attr)
10493           != VEC_length (dw_attr_node, die->die_attr))
10494         continue;
10495
10496       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10497         {
10498           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10499           if ((abbrev_a->dw_attr != die_a->dw_attr)
10500               || (value_format (abbrev_a) != value_format (die_a)))
10501             {
10502               ok = false;
10503               break;
10504             }
10505         }
10506       if (ok)
10507         break;
10508     }
10509
10510   if (abbrev_id >= abbrev_die_table_in_use)
10511     {
10512       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10513         {
10514           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10515           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10516                                             n_alloc);
10517
10518           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10519                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10520           abbrev_die_table_allocated = n_alloc;
10521         }
10522
10523       ++abbrev_die_table_in_use;
10524       abbrev_die_table[abbrev_id] = die;
10525     }
10526
10527   die->die_abbrev = abbrev_id;
10528   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10529 }
10530 \f
10531 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10532
10533 static int
10534 constant_size (unsigned HOST_WIDE_INT value)
10535 {
10536   int log;
10537
10538   if (value == 0)
10539     log = 0;
10540   else
10541     log = floor_log2 (value);
10542
10543   log = log / 8;
10544   log = 1 << (floor_log2 (log) + 1);
10545
10546   return log;
10547 }
10548
10549 /* Return the size of a DIE as it is represented in the
10550    .debug_info section.  */
10551
10552 static unsigned long
10553 size_of_die (dw_die_ref die)
10554 {
10555   unsigned long size = 0;
10556   dw_attr_ref a;
10557   unsigned ix;
10558
10559   size += size_of_uleb128 (die->die_abbrev);
10560   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10561     {
10562       switch (AT_class (a))
10563         {
10564         case dw_val_class_addr:
10565           size += DWARF2_ADDR_SIZE;
10566           break;
10567         case dw_val_class_offset:
10568           size += DWARF_OFFSET_SIZE;
10569           break;
10570         case dw_val_class_loc:
10571           {
10572             unsigned long lsize = size_of_locs (AT_loc (a));
10573
10574             /* Block length.  */
10575             if (dwarf_version >= 4)
10576               size += size_of_uleb128 (lsize);
10577             else
10578               size += constant_size (lsize);
10579             size += lsize;
10580           }
10581           break;
10582         case dw_val_class_loc_list:
10583           size += DWARF_OFFSET_SIZE;
10584           break;
10585         case dw_val_class_range_list:
10586           size += DWARF_OFFSET_SIZE;
10587           break;
10588         case dw_val_class_const:
10589           size += size_of_sleb128 (AT_int (a));
10590           break;
10591         case dw_val_class_unsigned_const:
10592           size += constant_size (AT_unsigned (a));
10593           break;
10594         case dw_val_class_const_double:
10595           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10596           if (HOST_BITS_PER_WIDE_INT >= 64)
10597             size++; /* block */
10598           break;
10599         case dw_val_class_vec:
10600           size += constant_size (a->dw_attr_val.v.val_vec.length
10601                                  * a->dw_attr_val.v.val_vec.elt_size)
10602                   + a->dw_attr_val.v.val_vec.length
10603                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10604           break;
10605         case dw_val_class_flag:
10606           if (dwarf_version >= 4)
10607             /* Currently all add_AT_flag calls pass in 1 as last argument,
10608                so DW_FORM_flag_present can be used.  If that ever changes,
10609                we'll need to use DW_FORM_flag and have some optimization
10610                in build_abbrev_table that will change those to
10611                DW_FORM_flag_present if it is set to 1 in all DIEs using
10612                the same abbrev entry.  */
10613             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10614           else
10615             size += 1;
10616           break;
10617         case dw_val_class_die_ref:
10618           if (AT_ref_external (a))
10619             {
10620               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10621                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10622                  is sized by target address length, whereas in DWARF3
10623                  it's always sized as an offset.  */
10624               if (dwarf_version >= 4)
10625                 size += DWARF_TYPE_SIGNATURE_SIZE;
10626               else if (dwarf_version == 2)
10627                 size += DWARF2_ADDR_SIZE;
10628               else
10629                 size += DWARF_OFFSET_SIZE;
10630             }
10631           else
10632             size += DWARF_OFFSET_SIZE;
10633           break;
10634         case dw_val_class_fde_ref:
10635           size += DWARF_OFFSET_SIZE;
10636           break;
10637         case dw_val_class_lbl_id:
10638           size += DWARF2_ADDR_SIZE;
10639           break;
10640         case dw_val_class_lineptr:
10641         case dw_val_class_macptr:
10642           size += DWARF_OFFSET_SIZE;
10643           break;
10644         case dw_val_class_str:
10645           if (AT_string_form (a) == DW_FORM_strp)
10646             size += DWARF_OFFSET_SIZE;
10647           else
10648             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10649           break;
10650         case dw_val_class_file:
10651           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10652           break;
10653         case dw_val_class_data8:
10654           size += 8;
10655           break;
10656         case dw_val_class_vms_delta:
10657           size += DWARF_OFFSET_SIZE;
10658           break;
10659         default:
10660           gcc_unreachable ();
10661         }
10662     }
10663
10664   return size;
10665 }
10666
10667 /* Size the debugging information associated with a given DIE.  Visits the
10668    DIE's children recursively.  Updates the global variable next_die_offset, on
10669    each time through.  Uses the current value of next_die_offset to update the
10670    die_offset field in each DIE.  */
10671
10672 static void
10673 calc_die_sizes (dw_die_ref die)
10674 {
10675   dw_die_ref c;
10676
10677   die->die_offset = next_die_offset;
10678   next_die_offset += size_of_die (die);
10679
10680   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10681
10682   if (die->die_child != NULL)
10683     /* Count the null byte used to terminate sibling lists.  */
10684     next_die_offset += 1;
10685 }
10686
10687 /* Set the marks for a die and its children.  We do this so
10688    that we know whether or not a reference needs to use FORM_ref_addr; only
10689    DIEs in the same CU will be marked.  We used to clear out the offset
10690    and use that as the flag, but ran into ordering problems.  */
10691
10692 static void
10693 mark_dies (dw_die_ref die)
10694 {
10695   dw_die_ref c;
10696
10697   gcc_assert (!die->die_mark);
10698
10699   die->die_mark = 1;
10700   FOR_EACH_CHILD (die, c, mark_dies (c));
10701 }
10702
10703 /* Clear the marks for a die and its children.  */
10704
10705 static void
10706 unmark_dies (dw_die_ref die)
10707 {
10708   dw_die_ref c;
10709
10710   if (dwarf_version < 4)
10711     gcc_assert (die->die_mark);
10712
10713   die->die_mark = 0;
10714   FOR_EACH_CHILD (die, c, unmark_dies (c));
10715 }
10716
10717 /* Clear the marks for a die, its children and referred dies.  */
10718
10719 static void
10720 unmark_all_dies (dw_die_ref die)
10721 {
10722   dw_die_ref c;
10723   dw_attr_ref a;
10724   unsigned ix;
10725
10726   if (!die->die_mark)
10727     return;
10728   die->die_mark = 0;
10729
10730   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10731
10732   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10733     if (AT_class (a) == dw_val_class_die_ref)
10734       unmark_all_dies (AT_ref (a));
10735 }
10736
10737 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10738    generated for the compilation unit.  */
10739
10740 static unsigned long
10741 size_of_pubnames (VEC (pubname_entry, gc) * names)
10742 {
10743   unsigned long size;
10744   unsigned i;
10745   pubname_ref p;
10746
10747   size = DWARF_PUBNAMES_HEADER_SIZE;
10748   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10749     if (names != pubtype_table
10750         || p->die->die_offset != 0
10751         || !flag_eliminate_unused_debug_types)
10752       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10753
10754   size += DWARF_OFFSET_SIZE;
10755   return size;
10756 }
10757
10758 /* Return the size of the information in the .debug_aranges section.  */
10759
10760 static unsigned long
10761 size_of_aranges (void)
10762 {
10763   unsigned long size;
10764
10765   size = DWARF_ARANGES_HEADER_SIZE;
10766
10767   /* Count the address/length pair for this compilation unit.  */
10768   if (text_section_used)
10769     size += 2 * DWARF2_ADDR_SIZE;
10770   if (cold_text_section_used)
10771     size += 2 * DWARF2_ADDR_SIZE;
10772   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10773
10774   /* Count the two zero words used to terminated the address range table.  */
10775   size += 2 * DWARF2_ADDR_SIZE;
10776   return size;
10777 }
10778 \f
10779 /* Select the encoding of an attribute value.  */
10780
10781 static enum dwarf_form
10782 value_format (dw_attr_ref a)
10783 {
10784   switch (a->dw_attr_val.val_class)
10785     {
10786     case dw_val_class_addr:
10787       /* Only very few attributes allow DW_FORM_addr.  */
10788       switch (a->dw_attr)
10789         {
10790         case DW_AT_low_pc:
10791         case DW_AT_high_pc:
10792         case DW_AT_entry_pc:
10793         case DW_AT_trampoline:
10794           return DW_FORM_addr;
10795         default:
10796           break;
10797         }
10798       switch (DWARF2_ADDR_SIZE)
10799         {
10800         case 1:
10801           return DW_FORM_data1;
10802         case 2:
10803           return DW_FORM_data2;
10804         case 4:
10805           return DW_FORM_data4;
10806         case 8:
10807           return DW_FORM_data8;
10808         default:
10809           gcc_unreachable ();
10810         }
10811     case dw_val_class_range_list:
10812     case dw_val_class_loc_list:
10813       if (dwarf_version >= 4)
10814         return DW_FORM_sec_offset;
10815       /* FALLTHRU */
10816     case dw_val_class_vms_delta:
10817     case dw_val_class_offset:
10818       switch (DWARF_OFFSET_SIZE)
10819         {
10820         case 4:
10821           return DW_FORM_data4;
10822         case 8:
10823           return DW_FORM_data8;
10824         default:
10825           gcc_unreachable ();
10826         }
10827     case dw_val_class_loc:
10828       if (dwarf_version >= 4)
10829         return DW_FORM_exprloc;
10830       switch (constant_size (size_of_locs (AT_loc (a))))
10831         {
10832         case 1:
10833           return DW_FORM_block1;
10834         case 2:
10835           return DW_FORM_block2;
10836         default:
10837           gcc_unreachable ();
10838         }
10839     case dw_val_class_const:
10840       return DW_FORM_sdata;
10841     case dw_val_class_unsigned_const:
10842       switch (constant_size (AT_unsigned (a)))
10843         {
10844         case 1:
10845           return DW_FORM_data1;
10846         case 2:
10847           return DW_FORM_data2;
10848         case 4:
10849           return DW_FORM_data4;
10850         case 8:
10851           return DW_FORM_data8;
10852         default:
10853           gcc_unreachable ();
10854         }
10855     case dw_val_class_const_double:
10856       switch (HOST_BITS_PER_WIDE_INT)
10857         {
10858         case 8:
10859           return DW_FORM_data2;
10860         case 16:
10861           return DW_FORM_data4;
10862         case 32:
10863           return DW_FORM_data8;
10864         case 64:
10865         default:
10866           return DW_FORM_block1;
10867         }
10868     case dw_val_class_vec:
10869       switch (constant_size (a->dw_attr_val.v.val_vec.length
10870                              * a->dw_attr_val.v.val_vec.elt_size))
10871         {
10872         case 1:
10873           return DW_FORM_block1;
10874         case 2:
10875           return DW_FORM_block2;
10876         case 4:
10877           return DW_FORM_block4;
10878         default:
10879           gcc_unreachable ();
10880         }
10881     case dw_val_class_flag:
10882       if (dwarf_version >= 4)
10883         {
10884           /* Currently all add_AT_flag calls pass in 1 as last argument,
10885              so DW_FORM_flag_present can be used.  If that ever changes,
10886              we'll need to use DW_FORM_flag and have some optimization
10887              in build_abbrev_table that will change those to
10888              DW_FORM_flag_present if it is set to 1 in all DIEs using
10889              the same abbrev entry.  */
10890           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10891           return DW_FORM_flag_present;
10892         }
10893       return DW_FORM_flag;
10894     case dw_val_class_die_ref:
10895       if (AT_ref_external (a))
10896         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10897       else
10898         return DW_FORM_ref;
10899     case dw_val_class_fde_ref:
10900       return DW_FORM_data;
10901     case dw_val_class_lbl_id:
10902       return DW_FORM_addr;
10903     case dw_val_class_lineptr:
10904     case dw_val_class_macptr:
10905       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10906     case dw_val_class_str:
10907       return AT_string_form (a);
10908     case dw_val_class_file:
10909       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10910         {
10911         case 1:
10912           return DW_FORM_data1;
10913         case 2:
10914           return DW_FORM_data2;
10915         case 4:
10916           return DW_FORM_data4;
10917         default:
10918           gcc_unreachable ();
10919         }
10920
10921     case dw_val_class_data8:
10922       return DW_FORM_data8;
10923
10924     default:
10925       gcc_unreachable ();
10926     }
10927 }
10928
10929 /* Output the encoding of an attribute value.  */
10930
10931 static void
10932 output_value_format (dw_attr_ref a)
10933 {
10934   enum dwarf_form form = value_format (a);
10935
10936   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10937 }
10938
10939 /* Output the .debug_abbrev section which defines the DIE abbreviation
10940    table.  */
10941
10942 static void
10943 output_abbrev_section (void)
10944 {
10945   unsigned long abbrev_id;
10946
10947   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10948     {
10949       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10950       unsigned ix;
10951       dw_attr_ref a_attr;
10952
10953       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10954       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10955                                    dwarf_tag_name (abbrev->die_tag));
10956
10957       if (abbrev->die_child != NULL)
10958         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10959       else
10960         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10961
10962       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10963            ix++)
10964         {
10965           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10966                                        dwarf_attr_name (a_attr->dw_attr));
10967           output_value_format (a_attr);
10968         }
10969
10970       dw2_asm_output_data (1, 0, NULL);
10971       dw2_asm_output_data (1, 0, NULL);
10972     }
10973
10974   /* Terminate the table.  */
10975   dw2_asm_output_data (1, 0, NULL);
10976 }
10977
10978 /* Output a symbol we can use to refer to this DIE from another CU.  */
10979
10980 static inline void
10981 output_die_symbol (dw_die_ref die)
10982 {
10983   char *sym = die->die_id.die_symbol;
10984
10985   if (sym == 0)
10986     return;
10987
10988   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10989     /* We make these global, not weak; if the target doesn't support
10990        .linkonce, it doesn't support combining the sections, so debugging
10991        will break.  */
10992     targetm.asm_out.globalize_label (asm_out_file, sym);
10993
10994   ASM_OUTPUT_LABEL (asm_out_file, sym);
10995 }
10996
10997 /* Return a new location list, given the begin and end range, and the
10998    expression.  */
10999
11000 static inline dw_loc_list_ref
11001 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11002               const char *section)
11003 {
11004   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11005
11006   retlist->begin = begin;
11007   retlist->end = end;
11008   retlist->expr = expr;
11009   retlist->section = section;
11010
11011   return retlist;
11012 }
11013
11014 /* Generate a new internal symbol for this location list node, if it
11015    hasn't got one yet.  */
11016
11017 static inline void
11018 gen_llsym (dw_loc_list_ref list)
11019 {
11020   gcc_assert (!list->ll_symbol);
11021   list->ll_symbol = gen_internal_sym ("LLST");
11022 }
11023
11024 /* Output the location list given to us.  */
11025
11026 static void
11027 output_loc_list (dw_loc_list_ref list_head)
11028 {
11029   dw_loc_list_ref curr = list_head;
11030
11031   if (list_head->emitted)
11032     return;
11033   list_head->emitted = true;
11034
11035   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11036
11037   /* Walk the location list, and output each range + expression.  */
11038   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11039     {
11040       unsigned long size;
11041       /* Don't output an entry that starts and ends at the same address.  */
11042       if (strcmp (curr->begin, curr->end) == 0)
11043         continue;
11044       if (!have_multiple_function_sections)
11045         {
11046           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11047                                 "Location list begin address (%s)",
11048                                 list_head->ll_symbol);
11049           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11050                                 "Location list end address (%s)",
11051                                 list_head->ll_symbol);
11052         }
11053       else
11054         {
11055           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11056                                "Location list begin address (%s)",
11057                                list_head->ll_symbol);
11058           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11059                                "Location list end address (%s)",
11060                                list_head->ll_symbol);
11061         }
11062       size = size_of_locs (curr->expr);
11063
11064       /* Output the block length for this list of location operations.  */
11065       gcc_assert (size <= 0xffff);
11066       dw2_asm_output_data (2, size, "%s", "Location expression size");
11067
11068       output_loc_sequence (curr->expr);
11069     }
11070
11071   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11072                        "Location list terminator begin (%s)",
11073                        list_head->ll_symbol);
11074   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11075                        "Location list terminator end (%s)",
11076                        list_head->ll_symbol);
11077 }
11078
11079 /* Output a type signature.  */
11080
11081 static inline void
11082 output_signature (const char *sig, const char *name)
11083 {
11084   int i;
11085
11086   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11087     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11088 }
11089
11090 /* Output the DIE and its attributes.  Called recursively to generate
11091    the definitions of each child DIE.  */
11092
11093 static void
11094 output_die (dw_die_ref die)
11095 {
11096   dw_attr_ref a;
11097   dw_die_ref c;
11098   unsigned long size;
11099   unsigned ix;
11100
11101   /* If someone in another CU might refer to us, set up a symbol for
11102      them to point to.  */
11103   if (dwarf_version < 4 && die->die_id.die_symbol)
11104     output_die_symbol (die);
11105
11106   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11107                                (unsigned long)die->die_offset,
11108                                dwarf_tag_name (die->die_tag));
11109
11110   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11111     {
11112       const char *name = dwarf_attr_name (a->dw_attr);
11113
11114       switch (AT_class (a))
11115         {
11116         case dw_val_class_addr:
11117           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11118           break;
11119
11120         case dw_val_class_offset:
11121           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11122                                "%s", name);
11123           break;
11124
11125         case dw_val_class_range_list:
11126           {
11127             char *p = strchr (ranges_section_label, '\0');
11128
11129             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11130                      a->dw_attr_val.v.val_offset);
11131             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11132                                    debug_ranges_section, "%s", name);
11133             *p = '\0';
11134           }
11135           break;
11136
11137         case dw_val_class_loc:
11138           size = size_of_locs (AT_loc (a));
11139
11140           /* Output the block length for this list of location operations.  */
11141           if (dwarf_version >= 4)
11142             dw2_asm_output_data_uleb128 (size, "%s", name);
11143           else
11144             dw2_asm_output_data (constant_size (size), size, "%s", name);
11145
11146           output_loc_sequence (AT_loc (a));
11147           break;
11148
11149         case dw_val_class_const:
11150           /* ??? It would be slightly more efficient to use a scheme like is
11151              used for unsigned constants below, but gdb 4.x does not sign
11152              extend.  Gdb 5.x does sign extend.  */
11153           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11154           break;
11155
11156         case dw_val_class_unsigned_const:
11157           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11158                                AT_unsigned (a), "%s", name);
11159           break;
11160
11161         case dw_val_class_const_double:
11162           {
11163             unsigned HOST_WIDE_INT first, second;
11164
11165             if (HOST_BITS_PER_WIDE_INT >= 64)
11166               dw2_asm_output_data (1,
11167                                    2 * HOST_BITS_PER_WIDE_INT
11168                                    / HOST_BITS_PER_CHAR,
11169                                    NULL);
11170
11171             if (WORDS_BIG_ENDIAN)
11172               {
11173                 first = a->dw_attr_val.v.val_double.high;
11174                 second = a->dw_attr_val.v.val_double.low;
11175               }
11176             else
11177               {
11178                 first = a->dw_attr_val.v.val_double.low;
11179                 second = a->dw_attr_val.v.val_double.high;
11180               }
11181
11182             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11183                                  first, name);
11184             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11185                                  second, NULL);
11186           }
11187           break;
11188
11189         case dw_val_class_vec:
11190           {
11191             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11192             unsigned int len = a->dw_attr_val.v.val_vec.length;
11193             unsigned int i;
11194             unsigned char *p;
11195
11196             dw2_asm_output_data (constant_size (len * elt_size),
11197                                  len * elt_size, "%s", name);
11198             if (elt_size > sizeof (HOST_WIDE_INT))
11199               {
11200                 elt_size /= 2;
11201                 len *= 2;
11202               }
11203             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11204                  i < len;
11205                  i++, p += elt_size)
11206               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11207                                    "fp or vector constant word %u", i);
11208             break;
11209           }
11210
11211         case dw_val_class_flag:
11212           if (dwarf_version >= 4)
11213             {
11214               /* Currently all add_AT_flag calls pass in 1 as last argument,
11215                  so DW_FORM_flag_present can be used.  If that ever changes,
11216                  we'll need to use DW_FORM_flag and have some optimization
11217                  in build_abbrev_table that will change those to
11218                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11219                  the same abbrev entry.  */
11220               gcc_assert (AT_flag (a) == 1);
11221               if (flag_debug_asm)
11222                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11223                          ASM_COMMENT_START, name);
11224               break;
11225             }
11226           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11227           break;
11228
11229         case dw_val_class_loc_list:
11230           {
11231             char *sym = AT_loc_list (a)->ll_symbol;
11232
11233             gcc_assert (sym);
11234             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11235                                    "%s", name);
11236           }
11237           break;
11238
11239         case dw_val_class_die_ref:
11240           if (AT_ref_external (a))
11241             {
11242               if (dwarf_version >= 4)
11243                 {
11244                   comdat_type_node_ref type_node =
11245                     AT_ref (a)->die_id.die_type_node;
11246
11247                   gcc_assert (type_node);
11248                   output_signature (type_node->signature, name);
11249                 }
11250               else
11251                 {
11252                   char *sym = AT_ref (a)->die_id.die_symbol;
11253                   int size;
11254
11255                   gcc_assert (sym);
11256                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11257                      length, whereas in DWARF3 it's always sized as an
11258                      offset.  */
11259                   if (dwarf_version == 2)
11260                     size = DWARF2_ADDR_SIZE;
11261                   else
11262                     size = DWARF_OFFSET_SIZE;
11263                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11264                                          name);
11265                 }
11266             }
11267           else
11268             {
11269               gcc_assert (AT_ref (a)->die_offset);
11270               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11271                                    "%s", name);
11272             }
11273           break;
11274
11275         case dw_val_class_fde_ref:
11276           {
11277             char l1[20];
11278
11279             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11280                                          a->dw_attr_val.v.val_fde_index * 2);
11281             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11282                                    "%s", name);
11283           }
11284           break;
11285
11286         case dw_val_class_vms_delta:
11287           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11288                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11289                                     "%s", name);
11290           break;
11291
11292         case dw_val_class_lbl_id:
11293           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11294           break;
11295
11296         case dw_val_class_lineptr:
11297           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11298                                  debug_line_section, "%s", name);
11299           break;
11300
11301         case dw_val_class_macptr:
11302           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11303                                  debug_macinfo_section, "%s", name);
11304           break;
11305
11306         case dw_val_class_str:
11307           if (AT_string_form (a) == DW_FORM_strp)
11308             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11309                                    a->dw_attr_val.v.val_str->label,
11310                                    debug_str_section,
11311                                    "%s: \"%s\"", name, AT_string (a));
11312           else
11313             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11314           break;
11315
11316         case dw_val_class_file:
11317           {
11318             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11319
11320             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11321                                  a->dw_attr_val.v.val_file->filename);
11322             break;
11323           }
11324
11325         case dw_val_class_data8:
11326           {
11327             int i;
11328
11329             for (i = 0; i < 8; i++)
11330               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11331                                    i == 0 ? "%s" : NULL, name);
11332             break;
11333           }
11334
11335         default:
11336           gcc_unreachable ();
11337         }
11338     }
11339
11340   FOR_EACH_CHILD (die, c, output_die (c));
11341
11342   /* Add null byte to terminate sibling list.  */
11343   if (die->die_child != NULL)
11344     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11345                          (unsigned long) die->die_offset);
11346 }
11347
11348 /* Output the compilation unit that appears at the beginning of the
11349    .debug_info section, and precedes the DIE descriptions.  */
11350
11351 static void
11352 output_compilation_unit_header (void)
11353 {
11354   int ver = dwarf_version;
11355
11356   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11357     dw2_asm_output_data (4, 0xffffffff,
11358       "Initial length escape value indicating 64-bit DWARF extension");
11359   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11360                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11361                        "Length of Compilation Unit Info");
11362   dw2_asm_output_data (2, ver, "DWARF version number");
11363   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11364                          debug_abbrev_section,
11365                          "Offset Into Abbrev. Section");
11366   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11367 }
11368
11369 /* Output the compilation unit DIE and its children.  */
11370
11371 static void
11372 output_comp_unit (dw_die_ref die, int output_if_empty)
11373 {
11374   const char *secname;
11375   char *oldsym, *tmp;
11376
11377   /* Unless we are outputting main CU, we may throw away empty ones.  */
11378   if (!output_if_empty && die->die_child == NULL)
11379     return;
11380
11381   /* Even if there are no children of this DIE, we must output the information
11382      about the compilation unit.  Otherwise, on an empty translation unit, we
11383      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11384      will then complain when examining the file.  First mark all the DIEs in
11385      this CU so we know which get local refs.  */
11386   mark_dies (die);
11387
11388   build_abbrev_table (die);
11389
11390   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11391   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11392   calc_die_sizes (die);
11393
11394   oldsym = die->die_id.die_symbol;
11395   if (oldsym)
11396     {
11397       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11398
11399       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11400       secname = tmp;
11401       die->die_id.die_symbol = NULL;
11402       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11403     }
11404   else
11405     {
11406       switch_to_section (debug_info_section);
11407       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11408       info_section_emitted = true;
11409     }
11410
11411   /* Output debugging information.  */
11412   output_compilation_unit_header ();
11413   output_die (die);
11414
11415   /* Leave the marks on the main CU, so we can check them in
11416      output_pubnames.  */
11417   if (oldsym)
11418     {
11419       unmark_dies (die);
11420       die->die_id.die_symbol = oldsym;
11421     }
11422 }
11423
11424 /* Output a comdat type unit DIE and its children.  */
11425
11426 static void
11427 output_comdat_type_unit (comdat_type_node *node)
11428 {
11429   const char *secname;
11430   char *tmp;
11431   int i;
11432 #if defined (OBJECT_FORMAT_ELF)
11433   tree comdat_key;
11434 #endif
11435
11436   /* First mark all the DIEs in this CU so we know which get local refs.  */
11437   mark_dies (node->root_die);
11438
11439   build_abbrev_table (node->root_die);
11440
11441   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11442   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11443   calc_die_sizes (node->root_die);
11444
11445 #if defined (OBJECT_FORMAT_ELF)
11446   secname = ".debug_types";
11447   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11448   sprintf (tmp, "wt.");
11449   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11450     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11451   comdat_key = get_identifier (tmp);
11452   targetm.asm_out.named_section (secname,
11453                                  SECTION_DEBUG | SECTION_LINKONCE,
11454                                  comdat_key);
11455 #else
11456   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11457   sprintf (tmp, ".gnu.linkonce.wt.");
11458   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11459     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11460   secname = tmp;
11461   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11462 #endif
11463
11464   /* Output debugging information.  */
11465   output_compilation_unit_header ();
11466   output_signature (node->signature, "Type Signature");
11467   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11468                        "Offset to Type DIE");
11469   output_die (node->root_die);
11470
11471   unmark_dies (node->root_die);
11472 }
11473
11474 /* Return the DWARF2/3 pubname associated with a decl.  */
11475
11476 static const char *
11477 dwarf2_name (tree decl, int scope)
11478 {
11479   if (DECL_NAMELESS (decl))
11480     return NULL;
11481   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11482 }
11483
11484 /* Add a new entry to .debug_pubnames if appropriate.  */
11485
11486 static void
11487 add_pubname_string (const char *str, dw_die_ref die)
11488 {
11489   if (targetm.want_debug_pub_sections)
11490     {
11491       pubname_entry e;
11492
11493       e.die = die;
11494       e.name = xstrdup (str);
11495       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11496     }
11497 }
11498
11499 static void
11500 add_pubname (tree decl, dw_die_ref die)
11501 {
11502   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11503     {
11504       const char *name = dwarf2_name (decl, 1);
11505       if (name)
11506         add_pubname_string (name, die);
11507     }
11508 }
11509
11510 /* Add a new entry to .debug_pubtypes if appropriate.  */
11511
11512 static void
11513 add_pubtype (tree decl, dw_die_ref die)
11514 {
11515   pubname_entry e;
11516
11517   if (!targetm.want_debug_pub_sections)
11518     return;
11519
11520   e.name = NULL;
11521   if ((TREE_PUBLIC (decl)
11522        || is_cu_die (die->die_parent))
11523       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11524     {
11525       e.die = die;
11526       if (TYPE_P (decl))
11527         {
11528           if (TYPE_NAME (decl))
11529             {
11530               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11531                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11532               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11533                        && DECL_NAME (TYPE_NAME (decl)))
11534                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11535               else
11536                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11537             }
11538         }
11539       else
11540         {
11541           e.name = dwarf2_name (decl, 1);
11542           if (e.name)
11543             e.name = xstrdup (e.name);
11544         }
11545
11546       /* If we don't have a name for the type, there's no point in adding
11547          it to the table.  */
11548       if (e.name && e.name[0] != '\0')
11549         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11550     }
11551 }
11552
11553 /* Output the public names table used to speed up access to externally
11554    visible names; or the public types table used to find type definitions.  */
11555
11556 static void
11557 output_pubnames (VEC (pubname_entry, gc) * names)
11558 {
11559   unsigned i;
11560   unsigned long pubnames_length = size_of_pubnames (names);
11561   pubname_ref pub;
11562
11563   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11564     dw2_asm_output_data (4, 0xffffffff,
11565       "Initial length escape value indicating 64-bit DWARF extension");
11566   if (names == pubname_table)
11567     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11568                          "Length of Public Names Info");
11569   else
11570     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11571                          "Length of Public Type Names Info");
11572   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11573   dw2_asm_output_data (2, 2, "DWARF Version");
11574   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11575                          debug_info_section,
11576                          "Offset of Compilation Unit Info");
11577   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11578                        "Compilation Unit Length");
11579
11580   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11581     {
11582       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11583       if (names == pubname_table)
11584         gcc_assert (pub->die->die_mark);
11585
11586       if (names != pubtype_table
11587           || pub->die->die_offset != 0
11588           || !flag_eliminate_unused_debug_types)
11589         {
11590           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11591                                "DIE offset");
11592
11593           dw2_asm_output_nstring (pub->name, -1, "external name");
11594         }
11595     }
11596
11597   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11598 }
11599
11600 /* Add a new entry to .debug_aranges if appropriate.  */
11601
11602 static void
11603 add_arange (tree decl, dw_die_ref die)
11604 {
11605   if (! DECL_SECTION_NAME (decl))
11606     return;
11607
11608   if (arange_table_in_use == arange_table_allocated)
11609     {
11610       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11611       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11612                                     arange_table_allocated);
11613       memset (arange_table + arange_table_in_use, 0,
11614               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11615     }
11616
11617   arange_table[arange_table_in_use++] = die;
11618 }
11619
11620 /* Output the information that goes into the .debug_aranges table.
11621    Namely, define the beginning and ending address range of the
11622    text section generated for this compilation unit.  */
11623
11624 static void
11625 output_aranges (void)
11626 {
11627   unsigned i;
11628   unsigned long aranges_length = size_of_aranges ();
11629
11630   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11631     dw2_asm_output_data (4, 0xffffffff,
11632       "Initial length escape value indicating 64-bit DWARF extension");
11633   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11634                        "Length of Address Ranges Info");
11635   /* Version number for aranges is still 2, even in DWARF3.  */
11636   dw2_asm_output_data (2, 2, "DWARF Version");
11637   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11638                          debug_info_section,
11639                          "Offset of Compilation Unit Info");
11640   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11641   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11642
11643   /* We need to align to twice the pointer size here.  */
11644   if (DWARF_ARANGES_PAD_SIZE)
11645     {
11646       /* Pad using a 2 byte words so that padding is correct for any
11647          pointer size.  */
11648       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11649                            2 * DWARF2_ADDR_SIZE);
11650       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11651         dw2_asm_output_data (2, 0, NULL);
11652     }
11653
11654   /* It is necessary not to output these entries if the sections were
11655      not used; if the sections were not used, the length will be 0 and
11656      the address may end up as 0 if the section is discarded by ld
11657      --gc-sections, leaving an invalid (0, 0) entry that can be
11658      confused with the terminator.  */
11659   if (text_section_used)
11660     {
11661       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11662       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11663                             text_section_label, "Length");
11664     }
11665   if (cold_text_section_used)
11666     {
11667       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11668                            "Address");
11669       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11670                             cold_text_section_label, "Length");
11671     }
11672
11673   for (i = 0; i < arange_table_in_use; i++)
11674     {
11675       dw_die_ref die = arange_table[i];
11676
11677       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11678       gcc_assert (die->die_mark);
11679
11680       if (die->die_tag == DW_TAG_subprogram)
11681         {
11682           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11683                                "Address");
11684           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11685                                 get_AT_low_pc (die), "Length");
11686         }
11687       else
11688         {
11689           /* A static variable; extract the symbol from DW_AT_location.
11690              Note that this code isn't currently hit, as we only emit
11691              aranges for functions (jason 9/23/99).  */
11692           dw_attr_ref a = get_AT (die, DW_AT_location);
11693           dw_loc_descr_ref loc;
11694
11695           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11696
11697           loc = AT_loc (a);
11698           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11699
11700           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11701                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11702           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11703                                get_AT_unsigned (die, DW_AT_byte_size),
11704                                "Length");
11705         }
11706     }
11707
11708   /* Output the terminator words.  */
11709   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11710   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11711 }
11712
11713 /* Add a new entry to .debug_ranges.  Return the offset at which it
11714    was placed.  */
11715
11716 static unsigned int
11717 add_ranges_num (int num)
11718 {
11719   unsigned int in_use = ranges_table_in_use;
11720
11721   if (in_use == ranges_table_allocated)
11722     {
11723       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11724       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11725                                     ranges_table_allocated);
11726       memset (ranges_table + ranges_table_in_use, 0,
11727               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11728     }
11729
11730   ranges_table[in_use].num = num;
11731   ranges_table_in_use = in_use + 1;
11732
11733   return in_use * 2 * DWARF2_ADDR_SIZE;
11734 }
11735
11736 /* Add a new entry to .debug_ranges corresponding to a block, or a
11737    range terminator if BLOCK is NULL.  */
11738
11739 static unsigned int
11740 add_ranges (const_tree block)
11741 {
11742   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11743 }
11744
11745 /* Add a new entry to .debug_ranges corresponding to a pair of
11746    labels.  */
11747
11748 static void
11749 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11750                       bool *added)
11751 {
11752   unsigned int in_use = ranges_by_label_in_use;
11753   unsigned int offset;
11754
11755   if (in_use == ranges_by_label_allocated)
11756     {
11757       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11758       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11759                                        ranges_by_label,
11760                                        ranges_by_label_allocated);
11761       memset (ranges_by_label + ranges_by_label_in_use, 0,
11762               RANGES_TABLE_INCREMENT
11763               * sizeof (struct dw_ranges_by_label_struct));
11764     }
11765
11766   ranges_by_label[in_use].begin = begin;
11767   ranges_by_label[in_use].end = end;
11768   ranges_by_label_in_use = in_use + 1;
11769
11770   offset = add_ranges_num (-(int)in_use - 1);
11771   if (!*added)
11772     {
11773       add_AT_range_list (die, DW_AT_ranges, offset);
11774       *added = true;
11775     }
11776 }
11777
11778 static void
11779 output_ranges (void)
11780 {
11781   unsigned i;
11782   static const char *const start_fmt = "Offset %#x";
11783   const char *fmt = start_fmt;
11784
11785   for (i = 0; i < ranges_table_in_use; i++)
11786     {
11787       int block_num = ranges_table[i].num;
11788
11789       if (block_num > 0)
11790         {
11791           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11792           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11793
11794           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11795           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11796
11797           /* If all code is in the text section, then the compilation
11798              unit base address defaults to DW_AT_low_pc, which is the
11799              base of the text section.  */
11800           if (!have_multiple_function_sections)
11801             {
11802               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11803                                     text_section_label,
11804                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11805               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11806                                     text_section_label, NULL);
11807             }
11808
11809           /* Otherwise, the compilation unit base address is zero,
11810              which allows us to use absolute addresses, and not worry
11811              about whether the target supports cross-section
11812              arithmetic.  */
11813           else
11814             {
11815               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11816                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11817               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11818             }
11819
11820           fmt = NULL;
11821         }
11822
11823       /* Negative block_num stands for an index into ranges_by_label.  */
11824       else if (block_num < 0)
11825         {
11826           int lab_idx = - block_num - 1;
11827
11828           if (!have_multiple_function_sections)
11829             {
11830               gcc_unreachable ();
11831 #if 0
11832               /* If we ever use add_ranges_by_labels () for a single
11833                  function section, all we have to do is to take out
11834                  the #if 0 above.  */
11835               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11836                                     ranges_by_label[lab_idx].begin,
11837                                     text_section_label,
11838                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11839               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11840                                     ranges_by_label[lab_idx].end,
11841                                     text_section_label, NULL);
11842 #endif
11843             }
11844           else
11845             {
11846               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11847                                    ranges_by_label[lab_idx].begin,
11848                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11849               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11850                                    ranges_by_label[lab_idx].end,
11851                                    NULL);
11852             }
11853         }
11854       else
11855         {
11856           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11857           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11858           fmt = start_fmt;
11859         }
11860     }
11861 }
11862
11863 /* Data structure containing information about input files.  */
11864 struct file_info
11865 {
11866   const char *path;     /* Complete file name.  */
11867   const char *fname;    /* File name part.  */
11868   int length;           /* Length of entire string.  */
11869   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11870   int dir_idx;          /* Index in directory table.  */
11871 };
11872
11873 /* Data structure containing information about directories with source
11874    files.  */
11875 struct dir_info
11876 {
11877   const char *path;     /* Path including directory name.  */
11878   int length;           /* Path length.  */
11879   int prefix;           /* Index of directory entry which is a prefix.  */
11880   int count;            /* Number of files in this directory.  */
11881   int dir_idx;          /* Index of directory used as base.  */
11882 };
11883
11884 /* Callback function for file_info comparison.  We sort by looking at
11885    the directories in the path.  */
11886
11887 static int
11888 file_info_cmp (const void *p1, const void *p2)
11889 {
11890   const struct file_info *const s1 = (const struct file_info *) p1;
11891   const struct file_info *const s2 = (const struct file_info *) p2;
11892   const unsigned char *cp1;
11893   const unsigned char *cp2;
11894
11895   /* Take care of file names without directories.  We need to make sure that
11896      we return consistent values to qsort since some will get confused if
11897      we return the same value when identical operands are passed in opposite
11898      orders.  So if neither has a directory, return 0 and otherwise return
11899      1 or -1 depending on which one has the directory.  */
11900   if ((s1->path == s1->fname || s2->path == s2->fname))
11901     return (s2->path == s2->fname) - (s1->path == s1->fname);
11902
11903   cp1 = (const unsigned char *) s1->path;
11904   cp2 = (const unsigned char *) s2->path;
11905
11906   while (1)
11907     {
11908       ++cp1;
11909       ++cp2;
11910       /* Reached the end of the first path?  If so, handle like above.  */
11911       if ((cp1 == (const unsigned char *) s1->fname)
11912           || (cp2 == (const unsigned char *) s2->fname))
11913         return ((cp2 == (const unsigned char *) s2->fname)
11914                 - (cp1 == (const unsigned char *) s1->fname));
11915
11916       /* Character of current path component the same?  */
11917       else if (*cp1 != *cp2)
11918         return *cp1 - *cp2;
11919     }
11920 }
11921
11922 struct file_name_acquire_data
11923 {
11924   struct file_info *files;
11925   int used_files;
11926   int max_files;
11927 };
11928
11929 /* Traversal function for the hash table.  */
11930
11931 static int
11932 file_name_acquire (void ** slot, void *data)
11933 {
11934   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11935   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11936   struct file_info *fi;
11937   const char *f;
11938
11939   gcc_assert (fnad->max_files >= d->emitted_number);
11940
11941   if (! d->emitted_number)
11942     return 1;
11943
11944   gcc_assert (fnad->max_files != fnad->used_files);
11945
11946   fi = fnad->files + fnad->used_files++;
11947
11948   /* Skip all leading "./".  */
11949   f = d->filename;
11950   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11951     f += 2;
11952
11953   /* Create a new array entry.  */
11954   fi->path = f;
11955   fi->length = strlen (f);
11956   fi->file_idx = d;
11957
11958   /* Search for the file name part.  */
11959   f = strrchr (f, DIR_SEPARATOR);
11960 #if defined (DIR_SEPARATOR_2)
11961   {
11962     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11963
11964     if (g != NULL)
11965       {
11966         if (f == NULL || f < g)
11967           f = g;
11968       }
11969   }
11970 #endif
11971
11972   fi->fname = f == NULL ? fi->path : f + 1;
11973   return 1;
11974 }
11975
11976 /* Output the directory table and the file name table.  We try to minimize
11977    the total amount of memory needed.  A heuristic is used to avoid large
11978    slowdowns with many input files.  */
11979
11980 static void
11981 output_file_names (void)
11982 {
11983   struct file_name_acquire_data fnad;
11984   int numfiles;
11985   struct file_info *files;
11986   struct dir_info *dirs;
11987   int *saved;
11988   int *savehere;
11989   int *backmap;
11990   int ndirs;
11991   int idx_offset;
11992   int i;
11993
11994   if (!last_emitted_file)
11995     {
11996       dw2_asm_output_data (1, 0, "End directory table");
11997       dw2_asm_output_data (1, 0, "End file name table");
11998       return;
11999     }
12000
12001   numfiles = last_emitted_file->emitted_number;
12002
12003   /* Allocate the various arrays we need.  */
12004   files = XALLOCAVEC (struct file_info, numfiles);
12005   dirs = XALLOCAVEC (struct dir_info, numfiles);
12006
12007   fnad.files = files;
12008   fnad.used_files = 0;
12009   fnad.max_files = numfiles;
12010   htab_traverse (file_table, file_name_acquire, &fnad);
12011   gcc_assert (fnad.used_files == fnad.max_files);
12012
12013   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12014
12015   /* Find all the different directories used.  */
12016   dirs[0].path = files[0].path;
12017   dirs[0].length = files[0].fname - files[0].path;
12018   dirs[0].prefix = -1;
12019   dirs[0].count = 1;
12020   dirs[0].dir_idx = 0;
12021   files[0].dir_idx = 0;
12022   ndirs = 1;
12023
12024   for (i = 1; i < numfiles; i++)
12025     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12026         && memcmp (dirs[ndirs - 1].path, files[i].path,
12027                    dirs[ndirs - 1].length) == 0)
12028       {
12029         /* Same directory as last entry.  */
12030         files[i].dir_idx = ndirs - 1;
12031         ++dirs[ndirs - 1].count;
12032       }
12033     else
12034       {
12035         int j;
12036
12037         /* This is a new directory.  */
12038         dirs[ndirs].path = files[i].path;
12039         dirs[ndirs].length = files[i].fname - files[i].path;
12040         dirs[ndirs].count = 1;
12041         dirs[ndirs].dir_idx = ndirs;
12042         files[i].dir_idx = ndirs;
12043
12044         /* Search for a prefix.  */
12045         dirs[ndirs].prefix = -1;
12046         for (j = 0; j < ndirs; j++)
12047           if (dirs[j].length < dirs[ndirs].length
12048               && dirs[j].length > 1
12049               && (dirs[ndirs].prefix == -1
12050                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12051               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12052             dirs[ndirs].prefix = j;
12053
12054         ++ndirs;
12055       }
12056
12057   /* Now to the actual work.  We have to find a subset of the directories which
12058      allow expressing the file name using references to the directory table
12059      with the least amount of characters.  We do not do an exhaustive search
12060      where we would have to check out every combination of every single
12061      possible prefix.  Instead we use a heuristic which provides nearly optimal
12062      results in most cases and never is much off.  */
12063   saved = XALLOCAVEC (int, ndirs);
12064   savehere = XALLOCAVEC (int, ndirs);
12065
12066   memset (saved, '\0', ndirs * sizeof (saved[0]));
12067   for (i = 0; i < ndirs; i++)
12068     {
12069       int j;
12070       int total;
12071
12072       /* We can always save some space for the current directory.  But this
12073          does not mean it will be enough to justify adding the directory.  */
12074       savehere[i] = dirs[i].length;
12075       total = (savehere[i] - saved[i]) * dirs[i].count;
12076
12077       for (j = i + 1; j < ndirs; j++)
12078         {
12079           savehere[j] = 0;
12080           if (saved[j] < dirs[i].length)
12081             {
12082               /* Determine whether the dirs[i] path is a prefix of the
12083                  dirs[j] path.  */
12084               int k;
12085
12086               k = dirs[j].prefix;
12087               while (k != -1 && k != (int) i)
12088                 k = dirs[k].prefix;
12089
12090               if (k == (int) i)
12091                 {
12092                   /* Yes it is.  We can possibly save some memory by
12093                      writing the filenames in dirs[j] relative to
12094                      dirs[i].  */
12095                   savehere[j] = dirs[i].length;
12096                   total += (savehere[j] - saved[j]) * dirs[j].count;
12097                 }
12098             }
12099         }
12100
12101       /* Check whether we can save enough to justify adding the dirs[i]
12102          directory.  */
12103       if (total > dirs[i].length + 1)
12104         {
12105           /* It's worthwhile adding.  */
12106           for (j = i; j < ndirs; j++)
12107             if (savehere[j] > 0)
12108               {
12109                 /* Remember how much we saved for this directory so far.  */
12110                 saved[j] = savehere[j];
12111
12112                 /* Remember the prefix directory.  */
12113                 dirs[j].dir_idx = i;
12114               }
12115         }
12116     }
12117
12118   /* Emit the directory name table.  */
12119   idx_offset = dirs[0].length > 0 ? 1 : 0;
12120   for (i = 1 - idx_offset; i < ndirs; i++)
12121     dw2_asm_output_nstring (dirs[i].path,
12122                             dirs[i].length
12123                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12124                             "Directory Entry: %#x", i + idx_offset);
12125
12126   dw2_asm_output_data (1, 0, "End directory table");
12127
12128   /* We have to emit them in the order of emitted_number since that's
12129      used in the debug info generation.  To do this efficiently we
12130      generate a back-mapping of the indices first.  */
12131   backmap = XALLOCAVEC (int, numfiles);
12132   for (i = 0; i < numfiles; i++)
12133     backmap[files[i].file_idx->emitted_number - 1] = i;
12134
12135   /* Now write all the file names.  */
12136   for (i = 0; i < numfiles; i++)
12137     {
12138       int file_idx = backmap[i];
12139       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12140
12141 #ifdef VMS_DEBUGGING_INFO
12142 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12143
12144       /* Setting these fields can lead to debugger miscomparisons,
12145          but VMS Debug requires them to be set correctly.  */
12146
12147       int ver;
12148       long long cdt;
12149       long siz;
12150       int maxfilelen = strlen (files[file_idx].path)
12151                                + dirs[dir_idx].length
12152                                + MAX_VMS_VERSION_LEN + 1;
12153       char *filebuf = XALLOCAVEC (char, maxfilelen);
12154
12155       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12156       snprintf (filebuf, maxfilelen, "%s;%d",
12157                 files[file_idx].path + dirs[dir_idx].length, ver);
12158
12159       dw2_asm_output_nstring
12160         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12161
12162       /* Include directory index.  */
12163       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12164
12165       /* Modification time.  */
12166       dw2_asm_output_data_uleb128
12167         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12168           ? cdt : 0,
12169          NULL);
12170
12171       /* File length in bytes.  */
12172       dw2_asm_output_data_uleb128
12173         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12174           ? siz : 0,
12175          NULL);
12176 #else
12177       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12178                               "File Entry: %#x", (unsigned) i + 1);
12179
12180       /* Include directory index.  */
12181       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12182
12183       /* Modification time.  */
12184       dw2_asm_output_data_uleb128 (0, NULL);
12185
12186       /* File length in bytes.  */
12187       dw2_asm_output_data_uleb128 (0, NULL);
12188 #endif /* VMS_DEBUGGING_INFO */
12189     }
12190
12191   dw2_asm_output_data (1, 0, "End file name table");
12192 }
12193
12194
12195 /* Output the source line number correspondence information.  This
12196    information goes into the .debug_line section.  */
12197
12198 static void
12199 output_line_info (void)
12200 {
12201   char l1[20], l2[20], p1[20], p2[20];
12202   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12203   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12204   unsigned opc;
12205   unsigned n_op_args;
12206   unsigned long lt_index;
12207   unsigned long current_line;
12208   long line_offset;
12209   long line_delta;
12210   unsigned long current_file;
12211   unsigned long function;
12212   int ver = dwarf_version;
12213
12214   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12215   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12216   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12217   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12218
12219   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12220     dw2_asm_output_data (4, 0xffffffff,
12221       "Initial length escape value indicating 64-bit DWARF extension");
12222   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12223                         "Length of Source Line Info");
12224   ASM_OUTPUT_LABEL (asm_out_file, l1);
12225
12226   dw2_asm_output_data (2, ver, "DWARF Version");
12227   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12228   ASM_OUTPUT_LABEL (asm_out_file, p1);
12229
12230   /* Define the architecture-dependent minimum instruction length (in
12231    bytes).  In this implementation of DWARF, this field is used for
12232    information purposes only.  Since GCC generates assembly language,
12233    we have no a priori knowledge of how many instruction bytes are
12234    generated for each source line, and therefore can use only the
12235    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12236    commands.  Accordingly, we fix this as `1', which is "correct
12237    enough" for all architectures, and don't let the target override.  */
12238   dw2_asm_output_data (1, 1,
12239                        "Minimum Instruction Length");
12240
12241   if (ver >= 4)
12242     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12243                          "Maximum Operations Per Instruction");
12244   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12245                        "Default is_stmt_start flag");
12246   dw2_asm_output_data (1, DWARF_LINE_BASE,
12247                        "Line Base Value (Special Opcodes)");
12248   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12249                        "Line Range Value (Special Opcodes)");
12250   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12251                        "Special Opcode Base");
12252
12253   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12254     {
12255       switch (opc)
12256         {
12257         case DW_LNS_advance_pc:
12258         case DW_LNS_advance_line:
12259         case DW_LNS_set_file:
12260         case DW_LNS_set_column:
12261         case DW_LNS_fixed_advance_pc:
12262           n_op_args = 1;
12263           break;
12264         default:
12265           n_op_args = 0;
12266           break;
12267         }
12268
12269       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12270                            opc, n_op_args);
12271     }
12272
12273   /* Write out the information about the files we use.  */
12274   output_file_names ();
12275   ASM_OUTPUT_LABEL (asm_out_file, p2);
12276
12277   /* We used to set the address register to the first location in the text
12278      section here, but that didn't accomplish anything since we already
12279      have a line note for the opening brace of the first function.  */
12280
12281   /* Generate the line number to PC correspondence table, encoded as
12282      a series of state machine operations.  */
12283   current_file = 1;
12284   current_line = 1;
12285
12286   if (cfun && in_cold_section_p)
12287     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12288   else
12289     strcpy (prev_line_label, text_section_label);
12290   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12291     {
12292       dw_line_info_ref line_info = &line_info_table[lt_index];
12293
12294 #if 0
12295       /* Disable this optimization for now; GDB wants to see two line notes
12296          at the beginning of a function so it can find the end of the
12297          prologue.  */
12298
12299       /* Don't emit anything for redundant notes.  Just updating the
12300          address doesn't accomplish anything, because we already assume
12301          that anything after the last address is this line.  */
12302       if (line_info->dw_line_num == current_line
12303           && line_info->dw_file_num == current_file)
12304         continue;
12305 #endif
12306
12307       /* Emit debug info for the address of the current line.
12308
12309          Unfortunately, we have little choice here currently, and must always
12310          use the most general form.  GCC does not know the address delta
12311          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12312          attributes which will give an upper bound on the address range.  We
12313          could perhaps use length attributes to determine when it is safe to
12314          use DW_LNS_fixed_advance_pc.  */
12315
12316       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12317       if (0)
12318         {
12319           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12320           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12321                                "DW_LNS_fixed_advance_pc");
12322           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12323         }
12324       else
12325         {
12326           /* This can handle any delta.  This takes
12327              4+DWARF2_ADDR_SIZE bytes.  */
12328           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12329           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12330           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12331           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12332         }
12333
12334       strcpy (prev_line_label, line_label);
12335
12336       /* Emit debug info for the source file of the current line, if
12337          different from the previous line.  */
12338       if (line_info->dw_file_num != current_file)
12339         {
12340           current_file = line_info->dw_file_num;
12341           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12342           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12343         }
12344
12345       /* Emit debug info for the current line number, choosing the encoding
12346          that uses the least amount of space.  */
12347       if (line_info->dw_line_num != current_line)
12348         {
12349           line_offset = line_info->dw_line_num - current_line;
12350           line_delta = line_offset - DWARF_LINE_BASE;
12351           current_line = line_info->dw_line_num;
12352           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12353             /* This can handle deltas from -10 to 234, using the current
12354                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12355                takes 1 byte.  */
12356             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12357                                  "line %lu", current_line);
12358           else
12359             {
12360               /* This can handle any delta.  This takes at least 4 bytes,
12361                  depending on the value being encoded.  */
12362               dw2_asm_output_data (1, DW_LNS_advance_line,
12363                                    "advance to line %lu", current_line);
12364               dw2_asm_output_data_sleb128 (line_offset, NULL);
12365               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12366             }
12367         }
12368       else
12369         /* We still need to start a new row, so output a copy insn.  */
12370         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12371     }
12372
12373   /* Emit debug info for the address of the end of the function.  */
12374   if (0)
12375     {
12376       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12377                            "DW_LNS_fixed_advance_pc");
12378       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12379     }
12380   else
12381     {
12382       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12383       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12384       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12385       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12386     }
12387
12388   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12389   dw2_asm_output_data_uleb128 (1, NULL);
12390   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12391
12392   function = 0;
12393   current_file = 1;
12394   current_line = 1;
12395   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12396     {
12397       dw_separate_line_info_ref line_info
12398         = &separate_line_info_table[lt_index];
12399
12400 #if 0
12401       /* Don't emit anything for redundant notes.  */
12402       if (line_info->dw_line_num == current_line
12403           && line_info->dw_file_num == current_file
12404           && line_info->function == function)
12405         goto cont;
12406 #endif
12407
12408       /* Emit debug info for the address of the current line.  If this is
12409          a new function, or the first line of a function, then we need
12410          to handle it differently.  */
12411       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12412                                    lt_index);
12413       if (function != line_info->function)
12414         {
12415           function = line_info->function;
12416
12417           /* Set the address register to the first line in the function.  */
12418           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12419           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12420           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12421           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12422         }
12423       else
12424         {
12425           /* ??? See the DW_LNS_advance_pc comment above.  */
12426           if (0)
12427             {
12428               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12429                                    "DW_LNS_fixed_advance_pc");
12430               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12431             }
12432           else
12433             {
12434               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12435               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12436               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12437               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12438             }
12439         }
12440
12441       strcpy (prev_line_label, line_label);
12442
12443       /* Emit debug info for the source file of the current line, if
12444          different from the previous line.  */
12445       if (line_info->dw_file_num != current_file)
12446         {
12447           current_file = line_info->dw_file_num;
12448           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12449           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12450         }
12451
12452       /* Emit debug info for the current line number, choosing the encoding
12453          that uses the least amount of space.  */
12454       if (line_info->dw_line_num != current_line)
12455         {
12456           line_offset = line_info->dw_line_num - current_line;
12457           line_delta = line_offset - DWARF_LINE_BASE;
12458           current_line = line_info->dw_line_num;
12459           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12460             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12461                                  "line %lu", current_line);
12462           else
12463             {
12464               dw2_asm_output_data (1, DW_LNS_advance_line,
12465                                    "advance to line %lu", current_line);
12466               dw2_asm_output_data_sleb128 (line_offset, NULL);
12467               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12468             }
12469         }
12470       else
12471         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12472
12473 #if 0
12474     cont:
12475 #endif
12476
12477       lt_index++;
12478
12479       /* If we're done with a function, end its sequence.  */
12480       if (lt_index == separate_line_info_table_in_use
12481           || separate_line_info_table[lt_index].function != function)
12482         {
12483           current_file = 1;
12484           current_line = 1;
12485
12486           /* Emit debug info for the address of the end of the function.  */
12487           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12488           if (0)
12489             {
12490               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12491                                    "DW_LNS_fixed_advance_pc");
12492               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12493             }
12494           else
12495             {
12496               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12497               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12498               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12499               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12500             }
12501
12502           /* Output the marker for the end of this sequence.  */
12503           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12504           dw2_asm_output_data_uleb128 (1, NULL);
12505           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12506         }
12507     }
12508
12509   /* Output the marker for the end of the line number info.  */
12510   ASM_OUTPUT_LABEL (asm_out_file, l2);
12511 }
12512
12513 /* Return the size of the .debug_dcall table for the compilation unit.  */
12514
12515 static unsigned long
12516 size_of_dcall_table (void)
12517 {
12518   unsigned long size;
12519   unsigned int i;
12520   dcall_entry *p;
12521   tree last_poc_decl = NULL;
12522
12523   /* Header:  version + debug info section pointer + pointer size.  */
12524   size = 2 + DWARF_OFFSET_SIZE + 1;
12525
12526   /* Each entry:  code label + DIE offset.  */
12527   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12528     {
12529       gcc_assert (p->targ_die != NULL);
12530       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12531       if (p->poc_decl != last_poc_decl)
12532         {
12533           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12534           gcc_assert (poc_die);
12535           last_poc_decl = p->poc_decl;
12536           if (poc_die)
12537             size += (DWARF_OFFSET_SIZE
12538                      + size_of_uleb128 (poc_die->die_offset));
12539         }
12540       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12541     }
12542
12543   return size;
12544 }
12545
12546 /* Output the direct call table used to disambiguate PC values when
12547    identical function have been merged.  */
12548
12549 static void
12550 output_dcall_table (void)
12551 {
12552   unsigned i;
12553   unsigned long dcall_length = size_of_dcall_table ();
12554   dcall_entry *p;
12555   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12556   tree last_poc_decl = NULL;
12557
12558   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12559     dw2_asm_output_data (4, 0xffffffff,
12560       "Initial length escape value indicating 64-bit DWARF extension");
12561   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12562                        "Length of Direct Call Table");
12563   dw2_asm_output_data (2, 4, "Version number");
12564   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12565                          debug_info_section,
12566                          "Offset of Compilation Unit Info");
12567   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12568
12569   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12570     {
12571       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12572       if (p->poc_decl != last_poc_decl)
12573         {
12574           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12575           last_poc_decl = p->poc_decl;
12576           if (poc_die)
12577             {
12578               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12579               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12580                                            "Caller DIE offset");
12581             }
12582         }
12583       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12584       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12585       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12586                                    "Callee DIE offset");
12587     }
12588 }
12589 \f
12590 /* Return the size of the .debug_vcall table for the compilation unit.  */
12591
12592 static unsigned long
12593 size_of_vcall_table (void)
12594 {
12595   unsigned long size;
12596   unsigned int i;
12597   vcall_entry *p;
12598
12599   /* Header:  version + pointer size.  */
12600   size = 2 + 1;
12601
12602   /* Each entry:  code label + vtable slot index.  */
12603   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12604     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12605
12606   return size;
12607 }
12608
12609 /* Output the virtual call table used to disambiguate PC values when
12610    identical function have been merged.  */
12611
12612 static void
12613 output_vcall_table (void)
12614 {
12615   unsigned i;
12616   unsigned long vcall_length = size_of_vcall_table ();
12617   vcall_entry *p;
12618   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12619
12620   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12621     dw2_asm_output_data (4, 0xffffffff,
12622       "Initial length escape value indicating 64-bit DWARF extension");
12623   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12624                        "Length of Virtual Call Table");
12625   dw2_asm_output_data (2, 4, "Version number");
12626   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12627
12628   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12629     {
12630       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12631       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12632       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12633     }
12634 }
12635 \f
12636 /* Given a pointer to a tree node for some base type, return a pointer to
12637    a DIE that describes the given type.
12638
12639    This routine must only be called for GCC type nodes that correspond to
12640    Dwarf base (fundamental) types.  */
12641
12642 static dw_die_ref
12643 base_type_die (tree type)
12644 {
12645   dw_die_ref base_type_result;
12646   enum dwarf_type encoding;
12647
12648   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12649     return 0;
12650
12651   /* If this is a subtype that should not be emitted as a subrange type,
12652      use the base type.  See subrange_type_for_debug_p.  */
12653   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12654     type = TREE_TYPE (type);
12655
12656   switch (TREE_CODE (type))
12657     {
12658     case INTEGER_TYPE:
12659       if ((dwarf_version >= 4 || !dwarf_strict)
12660           && TYPE_NAME (type)
12661           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12662           && DECL_IS_BUILTIN (TYPE_NAME (type))
12663           && DECL_NAME (TYPE_NAME (type)))
12664         {
12665           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12666           if (strcmp (name, "char16_t") == 0
12667               || strcmp (name, "char32_t") == 0)
12668             {
12669               encoding = DW_ATE_UTF;
12670               break;
12671             }
12672         }
12673       if (TYPE_STRING_FLAG (type))
12674         {
12675           if (TYPE_UNSIGNED (type))
12676             encoding = DW_ATE_unsigned_char;
12677           else
12678             encoding = DW_ATE_signed_char;
12679         }
12680       else if (TYPE_UNSIGNED (type))
12681         encoding = DW_ATE_unsigned;
12682       else
12683         encoding = DW_ATE_signed;
12684       break;
12685
12686     case REAL_TYPE:
12687       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12688         {
12689           if (dwarf_version >= 3 || !dwarf_strict)
12690             encoding = DW_ATE_decimal_float;
12691           else
12692             encoding = DW_ATE_lo_user;
12693         }
12694       else
12695         encoding = DW_ATE_float;
12696       break;
12697
12698     case FIXED_POINT_TYPE:
12699       if (!(dwarf_version >= 3 || !dwarf_strict))
12700         encoding = DW_ATE_lo_user;
12701       else if (TYPE_UNSIGNED (type))
12702         encoding = DW_ATE_unsigned_fixed;
12703       else
12704         encoding = DW_ATE_signed_fixed;
12705       break;
12706
12707       /* Dwarf2 doesn't know anything about complex ints, so use
12708          a user defined type for it.  */
12709     case COMPLEX_TYPE:
12710       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12711         encoding = DW_ATE_complex_float;
12712       else
12713         encoding = DW_ATE_lo_user;
12714       break;
12715
12716     case BOOLEAN_TYPE:
12717       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12718       encoding = DW_ATE_boolean;
12719       break;
12720
12721     default:
12722       /* No other TREE_CODEs are Dwarf fundamental types.  */
12723       gcc_unreachable ();
12724     }
12725
12726   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12727
12728   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12729                    int_size_in_bytes (type));
12730   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12731
12732   return base_type_result;
12733 }
12734
12735 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12736    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12737
12738 static inline int
12739 is_base_type (tree type)
12740 {
12741   switch (TREE_CODE (type))
12742     {
12743     case ERROR_MARK:
12744     case VOID_TYPE:
12745     case INTEGER_TYPE:
12746     case REAL_TYPE:
12747     case FIXED_POINT_TYPE:
12748     case COMPLEX_TYPE:
12749     case BOOLEAN_TYPE:
12750       return 1;
12751
12752     case ARRAY_TYPE:
12753     case RECORD_TYPE:
12754     case UNION_TYPE:
12755     case QUAL_UNION_TYPE:
12756     case ENUMERAL_TYPE:
12757     case FUNCTION_TYPE:
12758     case METHOD_TYPE:
12759     case POINTER_TYPE:
12760     case REFERENCE_TYPE:
12761     case NULLPTR_TYPE:
12762     case OFFSET_TYPE:
12763     case LANG_TYPE:
12764     case VECTOR_TYPE:
12765       return 0;
12766
12767     default:
12768       gcc_unreachable ();
12769     }
12770
12771   return 0;
12772 }
12773
12774 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12775    node, return the size in bits for the type if it is a constant, or else
12776    return the alignment for the type if the type's size is not constant, or
12777    else return BITS_PER_WORD if the type actually turns out to be an
12778    ERROR_MARK node.  */
12779
12780 static inline unsigned HOST_WIDE_INT
12781 simple_type_size_in_bits (const_tree type)
12782 {
12783   if (TREE_CODE (type) == ERROR_MARK)
12784     return BITS_PER_WORD;
12785   else if (TYPE_SIZE (type) == NULL_TREE)
12786     return 0;
12787   else if (host_integerp (TYPE_SIZE (type), 1))
12788     return tree_low_cst (TYPE_SIZE (type), 1);
12789   else
12790     return TYPE_ALIGN (type);
12791 }
12792
12793 /* Similarly, but return a double_int instead of UHWI.  */
12794
12795 static inline double_int
12796 double_int_type_size_in_bits (const_tree type)
12797 {
12798   if (TREE_CODE (type) == ERROR_MARK)
12799     return uhwi_to_double_int (BITS_PER_WORD);
12800   else if (TYPE_SIZE (type) == NULL_TREE)
12801     return double_int_zero;
12802   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12803     return tree_to_double_int (TYPE_SIZE (type));
12804   else
12805     return uhwi_to_double_int (TYPE_ALIGN (type));
12806 }
12807
12808 /*  Given a pointer to a tree node for a subrange type, return a pointer
12809     to a DIE that describes the given type.  */
12810
12811 static dw_die_ref
12812 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12813 {
12814   dw_die_ref subrange_die;
12815   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12816
12817   if (context_die == NULL)
12818     context_die = comp_unit_die ();
12819
12820   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12821
12822   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12823     {
12824       /* The size of the subrange type and its base type do not match,
12825          so we need to generate a size attribute for the subrange type.  */
12826       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12827     }
12828
12829   if (low)
12830     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12831   if (high)
12832     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12833
12834   return subrange_die;
12835 }
12836
12837 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12838    entry that chains various modifiers in front of the given type.  */
12839
12840 static dw_die_ref
12841 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12842                    dw_die_ref context_die)
12843 {
12844   enum tree_code code = TREE_CODE (type);
12845   dw_die_ref mod_type_die;
12846   dw_die_ref sub_die = NULL;
12847   tree item_type = NULL;
12848   tree qualified_type;
12849   tree name, low, high;
12850
12851   if (code == ERROR_MARK)
12852     return NULL;
12853
12854   /* See if we already have the appropriately qualified variant of
12855      this type.  */
12856   qualified_type
12857     = get_qualified_type (type,
12858                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12859                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12860
12861   if (qualified_type == sizetype
12862       && TYPE_NAME (qualified_type)
12863       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12864     {
12865       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12866
12867       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12868                            && TYPE_PRECISION (t)
12869                            == TYPE_PRECISION (qualified_type)
12870                            && TYPE_UNSIGNED (t)
12871                            == TYPE_UNSIGNED (qualified_type));
12872       qualified_type = t;
12873     }
12874
12875   /* If we do, then we can just use its DIE, if it exists.  */
12876   if (qualified_type)
12877     {
12878       mod_type_die = lookup_type_die (qualified_type);
12879       if (mod_type_die)
12880         return mod_type_die;
12881     }
12882
12883   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12884
12885   /* Handle C typedef types.  */
12886   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12887       && !DECL_ARTIFICIAL (name))
12888     {
12889       tree dtype = TREE_TYPE (name);
12890
12891       if (qualified_type == dtype)
12892         {
12893           /* For a named type, use the typedef.  */
12894           gen_type_die (qualified_type, context_die);
12895           return lookup_type_die (qualified_type);
12896         }
12897       else if (is_const_type < TYPE_READONLY (dtype)
12898                || is_volatile_type < TYPE_VOLATILE (dtype)
12899                || (is_const_type <= TYPE_READONLY (dtype)
12900                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12901                    && DECL_ORIGINAL_TYPE (name) != type))
12902         /* cv-unqualified version of named type.  Just use the unnamed
12903            type to which it refers.  */
12904         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12905                                   is_const_type, is_volatile_type,
12906                                   context_die);
12907       /* Else cv-qualified version of named type; fall through.  */
12908     }
12909
12910   if (is_const_type
12911       /* If both is_const_type and is_volatile_type, prefer the path
12912          which leads to a qualified type.  */
12913       && (!is_volatile_type
12914           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12915           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12916     {
12917       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12918       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12919     }
12920   else if (is_volatile_type)
12921     {
12922       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12923       sub_die = modified_type_die (type, is_const_type, 0, context_die);
12924     }
12925   else if (code == POINTER_TYPE)
12926     {
12927       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12928       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12929                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12930       item_type = TREE_TYPE (type);
12931       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12932         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12933                          TYPE_ADDR_SPACE (item_type));
12934     }
12935   else if (code == REFERENCE_TYPE)
12936     {
12937       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12938         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12939                                 type);
12940       else
12941         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12942       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12943                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12944       item_type = TREE_TYPE (type);
12945       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12946         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12947                          TYPE_ADDR_SPACE (item_type));
12948     }
12949   else if (code == INTEGER_TYPE
12950            && TREE_TYPE (type) != NULL_TREE
12951            && subrange_type_for_debug_p (type, &low, &high))
12952     {
12953       mod_type_die = subrange_type_die (type, low, high, context_die);
12954       item_type = TREE_TYPE (type);
12955     }
12956   else if (is_base_type (type))
12957     mod_type_die = base_type_die (type);
12958   else
12959     {
12960       gen_type_die (type, context_die);
12961
12962       /* We have to get the type_main_variant here (and pass that to the
12963          `lookup_type_die' routine) because the ..._TYPE node we have
12964          might simply be a *copy* of some original type node (where the
12965          copy was created to help us keep track of typedef names) and
12966          that copy might have a different TYPE_UID from the original
12967          ..._TYPE node.  */
12968       if (TREE_CODE (type) != VECTOR_TYPE)
12969         return lookup_type_die (type_main_variant (type));
12970       else
12971         /* Vectors have the debugging information in the type,
12972            not the main variant.  */
12973         return lookup_type_die (type);
12974     }
12975
12976   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12977      don't output a DW_TAG_typedef, since there isn't one in the
12978      user's program; just attach a DW_AT_name to the type.
12979      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12980      if the base type already has the same name.  */
12981   if (name
12982       && ((TREE_CODE (name) != TYPE_DECL
12983            && (qualified_type == TYPE_MAIN_VARIANT (type)
12984                || (!is_const_type && !is_volatile_type)))
12985           || (TREE_CODE (name) == TYPE_DECL
12986               && TREE_TYPE (name) == qualified_type
12987               && DECL_NAME (name))))
12988     {
12989       if (TREE_CODE (name) == TYPE_DECL)
12990         /* Could just call add_name_and_src_coords_attributes here,
12991            but since this is a builtin type it doesn't have any
12992            useful source coordinates anyway.  */
12993         name = DECL_NAME (name);
12994       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12995     }
12996   /* This probably indicates a bug.  */
12997   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12998     add_name_attribute (mod_type_die, "__unknown__");
12999
13000   if (qualified_type)
13001     equate_type_number_to_die (qualified_type, mod_type_die);
13002
13003   if (item_type)
13004     /* We must do this after the equate_type_number_to_die call, in case
13005        this is a recursive type.  This ensures that the modified_type_die
13006        recursion will terminate even if the type is recursive.  Recursive
13007        types are possible in Ada.  */
13008     sub_die = modified_type_die (item_type,
13009                                  TYPE_READONLY (item_type),
13010                                  TYPE_VOLATILE (item_type),
13011                                  context_die);
13012
13013   if (sub_die != NULL)
13014     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13015
13016   return mod_type_die;
13017 }
13018
13019 /* Generate DIEs for the generic parameters of T.
13020    T must be either a generic type or a generic function.
13021    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13022
13023 static void
13024 gen_generic_params_dies (tree t)
13025 {
13026   tree parms, args;
13027   int parms_num, i;
13028   dw_die_ref die = NULL;
13029
13030   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13031     return;
13032
13033   if (TYPE_P (t))
13034     die = lookup_type_die (t);
13035   else if (DECL_P (t))
13036     die = lookup_decl_die (t);
13037
13038   gcc_assert (die);
13039
13040   parms = lang_hooks.get_innermost_generic_parms (t);
13041   if (!parms)
13042     /* T has no generic parameter. It means T is neither a generic type
13043        or function. End of story.  */
13044     return;
13045
13046   parms_num = TREE_VEC_LENGTH (parms);
13047   args = lang_hooks.get_innermost_generic_args (t);
13048   for (i = 0; i < parms_num; i++)
13049     {
13050       tree parm, arg, arg_pack_elems;
13051
13052       parm = TREE_VEC_ELT (parms, i);
13053       arg = TREE_VEC_ELT (args, i);
13054       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13055       gcc_assert (parm && TREE_VALUE (parm) && arg);
13056
13057       if (parm && TREE_VALUE (parm) && arg)
13058         {
13059           /* If PARM represents a template parameter pack,
13060              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13061              by DW_TAG_template_*_parameter DIEs for the argument
13062              pack elements of ARG. Note that ARG would then be
13063              an argument pack.  */
13064           if (arg_pack_elems)
13065             template_parameter_pack_die (TREE_VALUE (parm),
13066                                          arg_pack_elems,
13067                                          die);
13068           else
13069             generic_parameter_die (TREE_VALUE (parm), arg,
13070                                    true /* Emit DW_AT_name */, die);
13071         }
13072     }
13073 }
13074
13075 /* Create and return a DIE for PARM which should be
13076    the representation of a generic type parameter.
13077    For instance, in the C++ front end, PARM would be a template parameter.
13078    ARG is the argument to PARM.
13079    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13080    name of the PARM.
13081    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13082    as a child node.  */
13083
13084 static dw_die_ref
13085 generic_parameter_die (tree parm, tree arg,
13086                        bool emit_name_p,
13087                        dw_die_ref parent_die)
13088 {
13089   dw_die_ref tmpl_die = NULL;
13090   const char *name = NULL;
13091
13092   if (!parm || !DECL_NAME (parm) || !arg)
13093     return NULL;
13094
13095   /* We support non-type generic parameters and arguments,
13096      type generic parameters and arguments, as well as
13097      generic generic parameters (a.k.a. template template parameters in C++)
13098      and arguments.  */
13099   if (TREE_CODE (parm) == PARM_DECL)
13100     /* PARM is a nontype generic parameter  */
13101     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13102   else if (TREE_CODE (parm) == TYPE_DECL)
13103     /* PARM is a type generic parameter.  */
13104     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13105   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13106     /* PARM is a generic generic parameter.
13107        Its DIE is a GNU extension. It shall have a
13108        DW_AT_name attribute to represent the name of the template template
13109        parameter, and a DW_AT_GNU_template_name attribute to represent the
13110        name of the template template argument.  */
13111     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13112                         parent_die, parm);
13113   else
13114     gcc_unreachable ();
13115
13116   if (tmpl_die)
13117     {
13118       tree tmpl_type;
13119
13120       /* If PARM is a generic parameter pack, it means we are
13121          emitting debug info for a template argument pack element.
13122          In other terms, ARG is a template argument pack element.
13123          In that case, we don't emit any DW_AT_name attribute for
13124          the die.  */
13125       if (emit_name_p)
13126         {
13127           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13128           gcc_assert (name);
13129           add_AT_string (tmpl_die, DW_AT_name, name);
13130         }
13131
13132       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13133         {
13134           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13135              TMPL_DIE should have a child DW_AT_type attribute that is set
13136              to the type of the argument to PARM, which is ARG.
13137              If PARM is a type generic parameter, TMPL_DIE should have a
13138              child DW_AT_type that is set to ARG.  */
13139           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13140           add_type_attribute (tmpl_die, tmpl_type, 0,
13141                               TREE_THIS_VOLATILE (tmpl_type),
13142                               parent_die);
13143         }
13144       else
13145         {
13146           /* So TMPL_DIE is a DIE representing a
13147              a generic generic template parameter, a.k.a template template
13148              parameter in C++ and arg is a template.  */
13149
13150           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13151              to the name of the argument.  */
13152           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13153           if (name)
13154             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13155         }
13156
13157       if (TREE_CODE (parm) == PARM_DECL)
13158         /* So PARM is a non-type generic parameter.
13159            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13160            attribute of TMPL_DIE which value represents the value
13161            of ARG.
13162            We must be careful here:
13163            The value of ARG might reference some function decls.
13164            We might currently be emitting debug info for a generic
13165            type and types are emitted before function decls, we don't
13166            know if the function decls referenced by ARG will actually be
13167            emitted after cgraph computations.
13168            So must defer the generation of the DW_AT_const_value to
13169            after cgraph is ready.  */
13170         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13171     }
13172
13173   return tmpl_die;
13174 }
13175
13176 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13177    PARM_PACK must be a template parameter pack. The returned DIE
13178    will be child DIE of PARENT_DIE.  */
13179
13180 static dw_die_ref
13181 template_parameter_pack_die (tree parm_pack,
13182                              tree parm_pack_args,
13183                              dw_die_ref parent_die)
13184 {
13185   dw_die_ref die;
13186   int j;
13187
13188   gcc_assert (parent_die && parm_pack);
13189
13190   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13191   add_name_and_src_coords_attributes (die, parm_pack);
13192   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13193     generic_parameter_die (parm_pack,
13194                            TREE_VEC_ELT (parm_pack_args, j),
13195                            false /* Don't emit DW_AT_name */,
13196                            die);
13197   return die;
13198 }
13199
13200 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13201    an enumerated type.  */
13202
13203 static inline int
13204 type_is_enum (const_tree type)
13205 {
13206   return TREE_CODE (type) == ENUMERAL_TYPE;
13207 }
13208
13209 /* Return the DBX register number described by a given RTL node.  */
13210
13211 static unsigned int
13212 dbx_reg_number (const_rtx rtl)
13213 {
13214   unsigned regno = REGNO (rtl);
13215
13216   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13217
13218 #ifdef LEAF_REG_REMAP
13219   if (current_function_uses_only_leaf_regs)
13220     {
13221       int leaf_reg = LEAF_REG_REMAP (regno);
13222       if (leaf_reg != -1)
13223         regno = (unsigned) leaf_reg;
13224     }
13225 #endif
13226
13227   return DBX_REGISTER_NUMBER (regno);
13228 }
13229
13230 /* Optionally add a DW_OP_piece term to a location description expression.
13231    DW_OP_piece is only added if the location description expression already
13232    doesn't end with DW_OP_piece.  */
13233
13234 static void
13235 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13236 {
13237   dw_loc_descr_ref loc;
13238
13239   if (*list_head != NULL)
13240     {
13241       /* Find the end of the chain.  */
13242       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13243         ;
13244
13245       if (loc->dw_loc_opc != DW_OP_piece)
13246         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13247     }
13248 }
13249
13250 /* Return a location descriptor that designates a machine register or
13251    zero if there is none.  */
13252
13253 static dw_loc_descr_ref
13254 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13255 {
13256   rtx regs;
13257
13258   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13259     return 0;
13260
13261   /* We only use "frame base" when we're sure we're talking about the
13262      post-prologue local stack frame.  We do this by *not* running
13263      register elimination until this point, and recognizing the special
13264      argument pointer and soft frame pointer rtx's.
13265      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13266   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13267       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13268     {
13269       dw_loc_descr_ref result = NULL;
13270
13271       if (dwarf_version >= 4 || !dwarf_strict)
13272         {
13273           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13274           if (result)
13275             add_loc_descr (&result,
13276                            new_loc_descr (DW_OP_stack_value, 0, 0));
13277         }
13278       return result;
13279     }
13280
13281   regs = targetm.dwarf_register_span (rtl);
13282
13283   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13284     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13285   else
13286     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13287 }
13288
13289 /* Return a location descriptor that designates a machine register for
13290    a given hard register number.  */
13291
13292 static dw_loc_descr_ref
13293 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13294 {
13295   dw_loc_descr_ref reg_loc_descr;
13296
13297   if (regno <= 31)
13298     reg_loc_descr
13299       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13300   else
13301     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13302
13303   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13304     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13305
13306   return reg_loc_descr;
13307 }
13308
13309 /* Given an RTL of a register, return a location descriptor that
13310    designates a value that spans more than one register.  */
13311
13312 static dw_loc_descr_ref
13313 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13314                              enum var_init_status initialized)
13315 {
13316   int nregs, size, i;
13317   unsigned reg;
13318   dw_loc_descr_ref loc_result = NULL;
13319
13320   reg = REGNO (rtl);
13321 #ifdef LEAF_REG_REMAP
13322   if (current_function_uses_only_leaf_regs)
13323     {
13324       int leaf_reg = LEAF_REG_REMAP (reg);
13325       if (leaf_reg != -1)
13326         reg = (unsigned) leaf_reg;
13327     }
13328 #endif
13329   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13330   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13331
13332   /* Simple, contiguous registers.  */
13333   if (regs == NULL_RTX)
13334     {
13335       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13336
13337       loc_result = NULL;
13338       while (nregs--)
13339         {
13340           dw_loc_descr_ref t;
13341
13342           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13343                                       VAR_INIT_STATUS_INITIALIZED);
13344           add_loc_descr (&loc_result, t);
13345           add_loc_descr_op_piece (&loc_result, size);
13346           ++reg;
13347         }
13348       return loc_result;
13349     }
13350
13351   /* Now onto stupid register sets in non contiguous locations.  */
13352
13353   gcc_assert (GET_CODE (regs) == PARALLEL);
13354
13355   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13356   loc_result = NULL;
13357
13358   for (i = 0; i < XVECLEN (regs, 0); ++i)
13359     {
13360       dw_loc_descr_ref t;
13361
13362       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13363                                   VAR_INIT_STATUS_INITIALIZED);
13364       add_loc_descr (&loc_result, t);
13365       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13366       add_loc_descr_op_piece (&loc_result, size);
13367     }
13368
13369   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13370     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13371   return loc_result;
13372 }
13373
13374 /* Return a location descriptor that designates a constant.  */
13375
13376 static dw_loc_descr_ref
13377 int_loc_descriptor (HOST_WIDE_INT i)
13378 {
13379   enum dwarf_location_atom op;
13380
13381   /* Pick the smallest representation of a constant, rather than just
13382      defaulting to the LEB encoding.  */
13383   if (i >= 0)
13384     {
13385       if (i <= 31)
13386         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13387       else if (i <= 0xff)
13388         op = DW_OP_const1u;
13389       else if (i <= 0xffff)
13390         op = DW_OP_const2u;
13391       else if (HOST_BITS_PER_WIDE_INT == 32
13392                || i <= 0xffffffff)
13393         op = DW_OP_const4u;
13394       else
13395         op = DW_OP_constu;
13396     }
13397   else
13398     {
13399       if (i >= -0x80)
13400         op = DW_OP_const1s;
13401       else if (i >= -0x8000)
13402         op = DW_OP_const2s;
13403       else if (HOST_BITS_PER_WIDE_INT == 32
13404                || i >= -0x80000000)
13405         op = DW_OP_const4s;
13406       else
13407         op = DW_OP_consts;
13408     }
13409
13410   return new_loc_descr (op, i, 0);
13411 }
13412
13413 /* Return loc description representing "address" of integer value.
13414    This can appear only as toplevel expression.  */
13415
13416 static dw_loc_descr_ref
13417 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13418 {
13419   int litsize;
13420   dw_loc_descr_ref loc_result = NULL;
13421
13422   if (!(dwarf_version >= 4 || !dwarf_strict))
13423     return NULL;
13424
13425   if (i >= 0)
13426     {
13427       if (i <= 31)
13428         litsize = 1;
13429       else if (i <= 0xff)
13430         litsize = 2;
13431       else if (i <= 0xffff)
13432         litsize = 3;
13433       else if (HOST_BITS_PER_WIDE_INT == 32
13434                || i <= 0xffffffff)
13435         litsize = 5;
13436       else
13437         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13438     }
13439   else
13440     {
13441       if (i >= -0x80)
13442         litsize = 2;
13443       else if (i >= -0x8000)
13444         litsize = 3;
13445       else if (HOST_BITS_PER_WIDE_INT == 32
13446                || i >= -0x80000000)
13447         litsize = 5;
13448       else
13449         litsize = 1 + size_of_sleb128 (i);
13450     }
13451   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13452      is more compact.  For DW_OP_stack_value we need:
13453      litsize + 1 (DW_OP_stack_value)
13454      and for DW_OP_implicit_value:
13455      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13456   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13457     {
13458       loc_result = int_loc_descriptor (i);
13459       add_loc_descr (&loc_result,
13460                      new_loc_descr (DW_OP_stack_value, 0, 0));
13461       return loc_result;
13462     }
13463
13464   loc_result = new_loc_descr (DW_OP_implicit_value,
13465                               size, 0);
13466   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13467   loc_result->dw_loc_oprnd2.v.val_int = i;
13468   return loc_result;
13469 }
13470
13471 /* Return a location descriptor that designates a base+offset location.  */
13472
13473 static dw_loc_descr_ref
13474 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13475                  enum var_init_status initialized)
13476 {
13477   unsigned int regno;
13478   dw_loc_descr_ref result;
13479   dw_fde_ref fde = current_fde ();
13480
13481   /* We only use "frame base" when we're sure we're talking about the
13482      post-prologue local stack frame.  We do this by *not* running
13483      register elimination until this point, and recognizing the special
13484      argument pointer and soft frame pointer rtx's.  */
13485   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13486     {
13487       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13488
13489       if (elim != reg)
13490         {
13491           if (GET_CODE (elim) == PLUS)
13492             {
13493               offset += INTVAL (XEXP (elim, 1));
13494               elim = XEXP (elim, 0);
13495             }
13496           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13497                        && (elim == hard_frame_pointer_rtx
13498                            || elim == stack_pointer_rtx))
13499                       || elim == (frame_pointer_needed
13500                                   ? hard_frame_pointer_rtx
13501                                   : stack_pointer_rtx));
13502
13503           /* If drap register is used to align stack, use frame
13504              pointer + offset to access stack variables.  If stack
13505              is aligned without drap, use stack pointer + offset to
13506              access stack variables.  */
13507           if (crtl->stack_realign_tried
13508               && reg == frame_pointer_rtx)
13509             {
13510               int base_reg
13511                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13512                                       ? HARD_FRAME_POINTER_REGNUM
13513                                       : STACK_POINTER_REGNUM);
13514               return new_reg_loc_descr (base_reg, offset);
13515             }
13516
13517           offset += frame_pointer_fb_offset;
13518           return new_loc_descr (DW_OP_fbreg, offset, 0);
13519         }
13520     }
13521   else if (!optimize
13522            && fde
13523            && (fde->drap_reg == REGNO (reg)
13524                || fde->vdrap_reg == REGNO (reg)))
13525     {
13526       /* Use cfa+offset to represent the location of arguments passed
13527          on the stack when drap is used to align stack.
13528          Only do this when not optimizing, for optimized code var-tracking
13529          is supposed to track where the arguments live and the register
13530          used as vdrap or drap in some spot might be used for something
13531          else in other part of the routine.  */
13532       return new_loc_descr (DW_OP_fbreg, offset, 0);
13533     }
13534
13535   regno = dbx_reg_number (reg);
13536   if (regno <= 31)
13537     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13538                             offset, 0);
13539   else
13540     result = new_loc_descr (DW_OP_bregx, regno, offset);
13541
13542   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13543     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13544
13545   return result;
13546 }
13547
13548 /* Return true if this RTL expression describes a base+offset calculation.  */
13549
13550 static inline int
13551 is_based_loc (const_rtx rtl)
13552 {
13553   return (GET_CODE (rtl) == PLUS
13554           && ((REG_P (XEXP (rtl, 0))
13555                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13556                && CONST_INT_P (XEXP (rtl, 1)))));
13557 }
13558
13559 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13560    failed.  */
13561
13562 static dw_loc_descr_ref
13563 tls_mem_loc_descriptor (rtx mem)
13564 {
13565   tree base;
13566   dw_loc_descr_ref loc_result;
13567
13568   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13569     return NULL;
13570
13571   base = get_base_address (MEM_EXPR (mem));
13572   if (base == NULL
13573       || TREE_CODE (base) != VAR_DECL
13574       || !DECL_THREAD_LOCAL_P (base))
13575     return NULL;
13576
13577   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13578   if (loc_result == NULL)
13579     return NULL;
13580
13581   if (INTVAL (MEM_OFFSET (mem)))
13582     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13583
13584   return loc_result;
13585 }
13586
13587 /* Output debug info about reason why we failed to expand expression as dwarf
13588    expression.  */
13589
13590 static void
13591 expansion_failed (tree expr, rtx rtl, char const *reason)
13592 {
13593   if (dump_file && (dump_flags & TDF_DETAILS))
13594     {
13595       fprintf (dump_file, "Failed to expand as dwarf: ");
13596       if (expr)
13597         print_generic_expr (dump_file, expr, dump_flags);
13598       if (rtl)
13599         {
13600           fprintf (dump_file, "\n");
13601           print_rtl (dump_file, rtl);
13602         }
13603       fprintf (dump_file, "\nReason: %s\n", reason);
13604     }
13605 }
13606
13607 /* Helper function for const_ok_for_output, called either directly
13608    or via for_each_rtx.  */
13609
13610 static int
13611 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13612 {
13613   rtx rtl = *rtlp;
13614
13615   if (GET_CODE (rtl) == UNSPEC)
13616     {
13617       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13618          we can't express it in the debug info.  */
13619 #ifdef ENABLE_CHECKING
13620       /* Don't complain about TLS UNSPECs, those are just too hard to
13621          delegitimize.  */
13622       if (XVECLEN (rtl, 0) != 1
13623           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13624           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13625           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13626           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13627         inform (current_function_decl
13628                 ? DECL_SOURCE_LOCATION (current_function_decl)
13629                 : UNKNOWN_LOCATION,
13630                 "non-delegitimized UNSPEC %d found in variable location",
13631                 XINT (rtl, 1));
13632 #endif
13633       expansion_failed (NULL_TREE, rtl,
13634                         "UNSPEC hasn't been delegitimized.\n");
13635       return 1;
13636     }
13637
13638   if (GET_CODE (rtl) != SYMBOL_REF)
13639     return 0;
13640
13641   if (CONSTANT_POOL_ADDRESS_P (rtl))
13642     {
13643       bool marked;
13644       get_pool_constant_mark (rtl, &marked);
13645       /* If all references to this pool constant were optimized away,
13646          it was not output and thus we can't represent it.  */
13647       if (!marked)
13648         {
13649           expansion_failed (NULL_TREE, rtl,
13650                             "Constant was removed from constant pool.\n");
13651           return 1;
13652         }
13653     }
13654
13655   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13656     return 1;
13657
13658   /* Avoid references to external symbols in debug info, on several targets
13659      the linker might even refuse to link when linking a shared library,
13660      and in many other cases the relocations for .debug_info/.debug_loc are
13661      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13662      to be defined within the same shared library or executable are fine.  */
13663   if (SYMBOL_REF_EXTERNAL_P (rtl))
13664     {
13665       tree decl = SYMBOL_REF_DECL (rtl);
13666
13667       if (decl == NULL || !targetm.binds_local_p (decl))
13668         {
13669           expansion_failed (NULL_TREE, rtl,
13670                             "Symbol not defined in current TU.\n");
13671           return 1;
13672         }
13673     }
13674
13675   return 0;
13676 }
13677
13678 /* Return true if constant RTL can be emitted in DW_OP_addr or
13679    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13680    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13681
13682 static bool
13683 const_ok_for_output (rtx rtl)
13684 {
13685   if (GET_CODE (rtl) == SYMBOL_REF)
13686     return const_ok_for_output_1 (&rtl, NULL) == 0;
13687
13688   if (GET_CODE (rtl) == CONST)
13689     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13690
13691   return true;
13692 }
13693
13694 /* The following routine converts the RTL for a variable or parameter
13695    (resident in memory) into an equivalent Dwarf representation of a
13696    mechanism for getting the address of that same variable onto the top of a
13697    hypothetical "address evaluation" stack.
13698
13699    When creating memory location descriptors, we are effectively transforming
13700    the RTL for a memory-resident object into its Dwarf postfix expression
13701    equivalent.  This routine recursively descends an RTL tree, turning
13702    it into Dwarf postfix code as it goes.
13703
13704    MODE is the mode of the memory reference, needed to handle some
13705    autoincrement addressing modes.
13706
13707    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13708    location list for RTL.
13709
13710    Return 0 if we can't represent the location.  */
13711
13712 static dw_loc_descr_ref
13713 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13714                     enum var_init_status initialized)
13715 {
13716   dw_loc_descr_ref mem_loc_result = NULL;
13717   enum dwarf_location_atom op;
13718   dw_loc_descr_ref op0, op1;
13719
13720   /* Note that for a dynamically sized array, the location we will generate a
13721      description of here will be the lowest numbered location which is
13722      actually within the array.  That's *not* necessarily the same as the
13723      zeroth element of the array.  */
13724
13725   rtl = targetm.delegitimize_address (rtl);
13726
13727   switch (GET_CODE (rtl))
13728     {
13729     case POST_INC:
13730     case POST_DEC:
13731     case POST_MODIFY:
13732       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13733
13734     case SUBREG:
13735       /* The case of a subreg may arise when we have a local (register)
13736          variable or a formal (register) parameter which doesn't quite fill
13737          up an entire register.  For now, just assume that it is
13738          legitimate to make the Dwarf info refer to the whole register which
13739          contains the given subreg.  */
13740       if (!subreg_lowpart_p (rtl))
13741         break;
13742       rtl = SUBREG_REG (rtl);
13743       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13744         break;
13745       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13746         break;
13747       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13748       break;
13749
13750     case REG:
13751       /* Whenever a register number forms a part of the description of the
13752          method for calculating the (dynamic) address of a memory resident
13753          object, DWARF rules require the register number be referred to as
13754          a "base register".  This distinction is not based in any way upon
13755          what category of register the hardware believes the given register
13756          belongs to.  This is strictly DWARF terminology we're dealing with
13757          here. Note that in cases where the location of a memory-resident
13758          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13759          OP_CONST (0)) the actual DWARF location descriptor that we generate
13760          may just be OP_BASEREG (basereg).  This may look deceptively like
13761          the object in question was allocated to a register (rather than in
13762          memory) so DWARF consumers need to be aware of the subtle
13763          distinction between OP_REG and OP_BASEREG.  */
13764       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13765         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13766       else if (stack_realign_drap
13767                && crtl->drap_reg
13768                && crtl->args.internal_arg_pointer == rtl
13769                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13770         {
13771           /* If RTL is internal_arg_pointer, which has been optimized
13772              out, use DRAP instead.  */
13773           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13774                                             VAR_INIT_STATUS_INITIALIZED);
13775         }
13776       break;
13777
13778     case SIGN_EXTEND:
13779     case ZERO_EXTEND:
13780       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13781                                 VAR_INIT_STATUS_INITIALIZED);
13782       if (op0 == 0)
13783         break;
13784       else
13785         {
13786           int shift = DWARF2_ADDR_SIZE
13787                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13788           shift *= BITS_PER_UNIT;
13789           if (GET_CODE (rtl) == SIGN_EXTEND)
13790             op = DW_OP_shra;
13791           else
13792             op = DW_OP_shr;
13793           mem_loc_result = op0;
13794           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13795           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13796           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13797           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13798         }
13799       break;
13800
13801     case MEM:
13802       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13803                                            VAR_INIT_STATUS_INITIALIZED);
13804       if (mem_loc_result == NULL)
13805         mem_loc_result = tls_mem_loc_descriptor (rtl);
13806       if (mem_loc_result != 0)
13807         {
13808           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13809             {
13810               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13811               return 0;
13812             }
13813           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13814             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13815           else
13816             add_loc_descr (&mem_loc_result,
13817                            new_loc_descr (DW_OP_deref_size,
13818                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13819         }
13820       else
13821         {
13822           rtx new_rtl = avoid_constant_pool_reference (rtl);
13823           if (new_rtl != rtl)
13824             return mem_loc_descriptor (new_rtl, mode, initialized);
13825         }
13826       break;
13827
13828     case LO_SUM:
13829          rtl = XEXP (rtl, 1);
13830
13831       /* ... fall through ...  */
13832
13833     case LABEL_REF:
13834       /* Some ports can transform a symbol ref into a label ref, because
13835          the symbol ref is too far away and has to be dumped into a constant
13836          pool.  */
13837     case CONST:
13838     case SYMBOL_REF:
13839       if (GET_CODE (rtl) == SYMBOL_REF
13840           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13841         {
13842           dw_loc_descr_ref temp;
13843
13844           /* If this is not defined, we have no way to emit the data.  */
13845           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13846             break;
13847
13848           /* We used to emit DW_OP_addr here, but that's wrong, since
13849              DW_OP_addr should be relocated by the debug info consumer,
13850              while DW_OP_GNU_push_tls_address operand should not.  */
13851           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13852                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13853           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13854           temp->dw_loc_oprnd1.v.val_addr = rtl;
13855           temp->dtprel = true;
13856
13857           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13858           add_loc_descr (&mem_loc_result, temp);
13859
13860           break;
13861         }
13862
13863       if (!const_ok_for_output (rtl))
13864         break;
13865
13866     symref:
13867       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13868       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13869       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13870       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13871       break;
13872
13873     case CONCAT:
13874     case CONCATN:
13875     case VAR_LOCATION:
13876     case DEBUG_IMPLICIT_PTR:
13877       expansion_failed (NULL_TREE, rtl,
13878                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13879       return 0;
13880
13881     case PRE_MODIFY:
13882       /* Extract the PLUS expression nested inside and fall into
13883          PLUS code below.  */
13884       rtl = XEXP (rtl, 1);
13885       goto plus;
13886
13887     case PRE_INC:
13888     case PRE_DEC:
13889       /* Turn these into a PLUS expression and fall into the PLUS code
13890          below.  */
13891       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13892                           GEN_INT (GET_CODE (rtl) == PRE_INC
13893                                    ? GET_MODE_UNIT_SIZE (mode)
13894                                    : -GET_MODE_UNIT_SIZE (mode)));
13895
13896       /* ... fall through ...  */
13897
13898     case PLUS:
13899     plus:
13900       if (is_based_loc (rtl))
13901         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13902                                           INTVAL (XEXP (rtl, 1)),
13903                                           VAR_INIT_STATUS_INITIALIZED);
13904       else
13905         {
13906           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13907                                                VAR_INIT_STATUS_INITIALIZED);
13908           if (mem_loc_result == 0)
13909             break;
13910
13911           if (CONST_INT_P (XEXP (rtl, 1)))
13912             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13913           else
13914             {
13915               dw_loc_descr_ref mem_loc_result2
13916                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13917                                       VAR_INIT_STATUS_INITIALIZED);
13918               if (mem_loc_result2 == 0)
13919                 break;
13920               add_loc_descr (&mem_loc_result, mem_loc_result2);
13921               add_loc_descr (&mem_loc_result,
13922                              new_loc_descr (DW_OP_plus, 0, 0));
13923             }
13924         }
13925       break;
13926
13927     /* If a pseudo-reg is optimized away, it is possible for it to
13928        be replaced with a MEM containing a multiply or shift.  */
13929     case MINUS:
13930       op = DW_OP_minus;
13931       goto do_binop;
13932
13933     case MULT:
13934       op = DW_OP_mul;
13935       goto do_binop;
13936
13937     case DIV:
13938       op = DW_OP_div;
13939       goto do_binop;
13940
13941     case UMOD:
13942       op = DW_OP_mod;
13943       goto do_binop;
13944
13945     case ASHIFT:
13946       op = DW_OP_shl;
13947       goto do_binop;
13948
13949     case ASHIFTRT:
13950       op = DW_OP_shra;
13951       goto do_binop;
13952
13953     case LSHIFTRT:
13954       op = DW_OP_shr;
13955       goto do_binop;
13956
13957     case AND:
13958       op = DW_OP_and;
13959       goto do_binop;
13960
13961     case IOR:
13962       op = DW_OP_or;
13963       goto do_binop;
13964
13965     case XOR:
13966       op = DW_OP_xor;
13967       goto do_binop;
13968
13969     do_binop:
13970       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13971                                 VAR_INIT_STATUS_INITIALIZED);
13972       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13973                                 VAR_INIT_STATUS_INITIALIZED);
13974
13975       if (op0 == 0 || op1 == 0)
13976         break;
13977
13978       mem_loc_result = op0;
13979       add_loc_descr (&mem_loc_result, op1);
13980       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13981       break;
13982
13983     case MOD:
13984       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13985                                 VAR_INIT_STATUS_INITIALIZED);
13986       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13987                                 VAR_INIT_STATUS_INITIALIZED);
13988
13989       if (op0 == 0 || op1 == 0)
13990         break;
13991
13992       mem_loc_result = op0;
13993       add_loc_descr (&mem_loc_result, op1);
13994       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13995       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13996       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13997       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13998       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13999       break;
14000
14001     case NOT:
14002       op = DW_OP_not;
14003       goto do_unop;
14004
14005     case ABS:
14006       op = DW_OP_abs;
14007       goto do_unop;
14008
14009     case NEG:
14010       op = DW_OP_neg;
14011       goto do_unop;
14012
14013     do_unop:
14014       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14015                                 VAR_INIT_STATUS_INITIALIZED);
14016
14017       if (op0 == 0)
14018         break;
14019
14020       mem_loc_result = op0;
14021       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14022       break;
14023
14024     case CONST_INT:
14025       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14026       break;
14027
14028     case EQ:
14029       op = DW_OP_eq;
14030       goto do_scompare;
14031
14032     case GE:
14033       op = DW_OP_ge;
14034       goto do_scompare;
14035
14036     case GT:
14037       op = DW_OP_gt;
14038       goto do_scompare;
14039
14040     case LE:
14041       op = DW_OP_le;
14042       goto do_scompare;
14043
14044     case LT:
14045       op = DW_OP_lt;
14046       goto do_scompare;
14047
14048     case NE:
14049       op = DW_OP_ne;
14050       goto do_scompare;
14051
14052     do_scompare:
14053       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14054           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14055         break;
14056       else
14057         {
14058           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14059
14060           if (op_mode == VOIDmode)
14061             op_mode = GET_MODE (XEXP (rtl, 1));
14062           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14063             break;
14064
14065           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14066                                     VAR_INIT_STATUS_INITIALIZED);
14067           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14068                                     VAR_INIT_STATUS_INITIALIZED);
14069
14070           if (op0 == 0 || op1 == 0)
14071             break;
14072
14073           if (op_mode != VOIDmode
14074               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14075             {
14076               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14077               shift *= BITS_PER_UNIT;
14078               /* For eq/ne, if the operands are known to be zero-extended,
14079                  there is no need to do the fancy shifting up.  */
14080               if (op == DW_OP_eq || op == DW_OP_ne)
14081                 {
14082                   dw_loc_descr_ref last0, last1;
14083                   for (last0 = op0;
14084                        last0->dw_loc_next != NULL;
14085                        last0 = last0->dw_loc_next)
14086                     ;
14087                   for (last1 = op1;
14088                        last1->dw_loc_next != NULL;
14089                        last1 = last1->dw_loc_next)
14090                     ;
14091                   /* deref_size zero extends, and for constants we can check
14092                      whether they are zero extended or not.  */
14093                   if (((last0->dw_loc_opc == DW_OP_deref_size
14094                         && last0->dw_loc_oprnd1.v.val_int
14095                            <= GET_MODE_SIZE (op_mode))
14096                        || (CONST_INT_P (XEXP (rtl, 0))
14097                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14098                                == (INTVAL (XEXP (rtl, 0))
14099                                    & GET_MODE_MASK (op_mode))))
14100                       && ((last1->dw_loc_opc == DW_OP_deref_size
14101                            && last1->dw_loc_oprnd1.v.val_int
14102                               <= GET_MODE_SIZE (op_mode))
14103                           || (CONST_INT_P (XEXP (rtl, 1))
14104                               && (unsigned HOST_WIDE_INT)
14105                                  INTVAL (XEXP (rtl, 1))
14106                                  == (INTVAL (XEXP (rtl, 1))
14107                                      & GET_MODE_MASK (op_mode)))))
14108                     goto do_compare;
14109                 }
14110               add_loc_descr (&op0, int_loc_descriptor (shift));
14111               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14112               if (CONST_INT_P (XEXP (rtl, 1)))
14113                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14114               else
14115                 {
14116                   add_loc_descr (&op1, int_loc_descriptor (shift));
14117                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14118                 }
14119             }
14120         }
14121
14122     do_compare:
14123       mem_loc_result = op0;
14124       add_loc_descr (&mem_loc_result, op1);
14125       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14126       if (STORE_FLAG_VALUE != 1)
14127         {
14128           add_loc_descr (&mem_loc_result,
14129                          int_loc_descriptor (STORE_FLAG_VALUE));
14130           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14131         }
14132       break;
14133
14134     case GEU:
14135       op = DW_OP_ge;
14136       goto do_ucompare;
14137
14138     case GTU:
14139       op = DW_OP_gt;
14140       goto do_ucompare;
14141
14142     case LEU:
14143       op = DW_OP_le;
14144       goto do_ucompare;
14145
14146     case LTU:
14147       op = DW_OP_lt;
14148       goto do_ucompare;
14149
14150     do_ucompare:
14151       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14152           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14153         break;
14154       else
14155         {
14156           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14157
14158           if (op_mode == VOIDmode)
14159             op_mode = GET_MODE (XEXP (rtl, 1));
14160           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14161             break;
14162
14163           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14164                                     VAR_INIT_STATUS_INITIALIZED);
14165           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14166                                     VAR_INIT_STATUS_INITIALIZED);
14167
14168           if (op0 == 0 || op1 == 0)
14169             break;
14170
14171           if (op_mode != VOIDmode
14172               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14173             {
14174               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14175               dw_loc_descr_ref last0, last1;
14176               for (last0 = op0;
14177                    last0->dw_loc_next != NULL;
14178                    last0 = last0->dw_loc_next)
14179                 ;
14180               for (last1 = op1;
14181                    last1->dw_loc_next != NULL;
14182                    last1 = last1->dw_loc_next)
14183                 ;
14184               if (CONST_INT_P (XEXP (rtl, 0)))
14185                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14186               /* deref_size zero extends, so no need to mask it again.  */
14187               else if (last0->dw_loc_opc != DW_OP_deref_size
14188                        || last0->dw_loc_oprnd1.v.val_int
14189                           > GET_MODE_SIZE (op_mode))
14190                 {
14191                   add_loc_descr (&op0, int_loc_descriptor (mask));
14192                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14193                 }
14194               if (CONST_INT_P (XEXP (rtl, 1)))
14195                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14196               /* deref_size zero extends, so no need to mask it again.  */
14197               else if (last1->dw_loc_opc != DW_OP_deref_size
14198                        || last1->dw_loc_oprnd1.v.val_int
14199                           > GET_MODE_SIZE (op_mode))
14200                 {
14201                   add_loc_descr (&op1, int_loc_descriptor (mask));
14202                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14203                 }
14204             }
14205           else
14206             {
14207               HOST_WIDE_INT bias = 1;
14208               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14209               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14210               if (CONST_INT_P (XEXP (rtl, 1)))
14211                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14212                                           + INTVAL (XEXP (rtl, 1)));
14213               else
14214                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14215                                                     bias, 0));
14216             }
14217         }
14218       goto do_compare;
14219
14220     case SMIN:
14221     case SMAX:
14222     case UMIN:
14223     case UMAX:
14224       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14225           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14226           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14227         break;
14228
14229       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14230                                 VAR_INIT_STATUS_INITIALIZED);
14231       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14232                                 VAR_INIT_STATUS_INITIALIZED);
14233
14234       if (op0 == 0 || op1 == 0)
14235         break;
14236
14237       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14238       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14239       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14240       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14241         {
14242           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14243             {
14244               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14245               add_loc_descr (&op0, int_loc_descriptor (mask));
14246               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14247               add_loc_descr (&op1, int_loc_descriptor (mask));
14248               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14249             }
14250           else
14251             {
14252               HOST_WIDE_INT bias = 1;
14253               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14254               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14255               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14256             }
14257         }
14258       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14259         {
14260           int shift = DWARF2_ADDR_SIZE
14261                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14262           shift *= BITS_PER_UNIT;
14263           add_loc_descr (&op0, int_loc_descriptor (shift));
14264           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14265           add_loc_descr (&op1, int_loc_descriptor (shift));
14266           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14267         }
14268
14269       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14270         op = DW_OP_lt;
14271       else
14272         op = DW_OP_gt;
14273       mem_loc_result = op0;
14274       add_loc_descr (&mem_loc_result, op1);
14275       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14276       {
14277         dw_loc_descr_ref bra_node, drop_node;
14278
14279         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14280         add_loc_descr (&mem_loc_result, bra_node);
14281         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14282         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14283         add_loc_descr (&mem_loc_result, drop_node);
14284         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14285         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14286       }
14287       break;
14288
14289     case ZERO_EXTRACT:
14290     case SIGN_EXTRACT:
14291       if (CONST_INT_P (XEXP (rtl, 1))
14292           && CONST_INT_P (XEXP (rtl, 2))
14293           && ((unsigned) INTVAL (XEXP (rtl, 1))
14294               + (unsigned) INTVAL (XEXP (rtl, 2))
14295               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14296           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14297           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14298         {
14299           int shift, size;
14300           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14301                                     VAR_INIT_STATUS_INITIALIZED);
14302           if (op0 == 0)
14303             break;
14304           if (GET_CODE (rtl) == SIGN_EXTRACT)
14305             op = DW_OP_shra;
14306           else
14307             op = DW_OP_shr;
14308           mem_loc_result = op0;
14309           size = INTVAL (XEXP (rtl, 1));
14310           shift = INTVAL (XEXP (rtl, 2));
14311           if (BITS_BIG_ENDIAN)
14312             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14313                     - shift - size;
14314           if (shift + size != (int) DWARF2_ADDR_SIZE)
14315             {
14316               add_loc_descr (&mem_loc_result,
14317                              int_loc_descriptor (DWARF2_ADDR_SIZE
14318                                                  - shift - size));
14319               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14320             }
14321           if (size != (int) DWARF2_ADDR_SIZE)
14322             {
14323               add_loc_descr (&mem_loc_result,
14324                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14325               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14326             }
14327         }
14328       break;
14329
14330     case IF_THEN_ELSE:
14331       {
14332         dw_loc_descr_ref op2, bra_node, drop_node;
14333         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14334                                   VAR_INIT_STATUS_INITIALIZED);
14335         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14336                                   VAR_INIT_STATUS_INITIALIZED);
14337         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14338                                   VAR_INIT_STATUS_INITIALIZED);
14339         if (op0 == NULL || op1 == NULL || op2 == NULL)
14340           break;
14341
14342         mem_loc_result = op1;
14343         add_loc_descr (&mem_loc_result, op2);
14344         add_loc_descr (&mem_loc_result, op0);
14345         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14346         add_loc_descr (&mem_loc_result, bra_node);
14347         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14348         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14349         add_loc_descr (&mem_loc_result, drop_node);
14350         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14351         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14352       }
14353       break;
14354
14355     case COMPARE:
14356     case ROTATE:
14357     case ROTATERT:
14358     case TRUNCATE:
14359       /* In theory, we could implement the above.  */
14360       /* DWARF cannot represent the unsigned compare operations
14361          natively.  */
14362     case SS_MULT:
14363     case US_MULT:
14364     case SS_DIV:
14365     case US_DIV:
14366     case SS_PLUS:
14367     case US_PLUS:
14368     case SS_MINUS:
14369     case US_MINUS:
14370     case SS_NEG:
14371     case US_NEG:
14372     case SS_ABS:
14373     case SS_ASHIFT:
14374     case US_ASHIFT:
14375     case SS_TRUNCATE:
14376     case US_TRUNCATE:
14377     case UDIV:
14378     case UNORDERED:
14379     case ORDERED:
14380     case UNEQ:
14381     case UNGE:
14382     case UNGT:
14383     case UNLE:
14384     case UNLT:
14385     case LTGT:
14386     case FLOAT_EXTEND:
14387     case FLOAT_TRUNCATE:
14388     case FLOAT:
14389     case UNSIGNED_FLOAT:
14390     case FIX:
14391     case UNSIGNED_FIX:
14392     case FRACT_CONVERT:
14393     case UNSIGNED_FRACT_CONVERT:
14394     case SAT_FRACT:
14395     case UNSIGNED_SAT_FRACT:
14396     case SQRT:
14397     case BSWAP:
14398     case FFS:
14399     case CLZ:
14400     case CTZ:
14401     case POPCOUNT:
14402     case PARITY:
14403     case ASM_OPERANDS:
14404     case VEC_MERGE:
14405     case VEC_SELECT:
14406     case VEC_CONCAT:
14407     case VEC_DUPLICATE:
14408     case UNSPEC:
14409     case HIGH:
14410       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14411          can't express it in the debug info.  This can happen e.g. with some
14412          TLS UNSPECs.  */
14413       break;
14414
14415     case CONST_STRING:
14416       resolve_one_addr (&rtl, NULL);
14417       goto symref;
14418
14419     default:
14420 #ifdef ENABLE_CHECKING
14421       print_rtl (stderr, rtl);
14422       gcc_unreachable ();
14423 #else
14424       break;
14425 #endif
14426     }
14427
14428   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14429     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14430
14431   return mem_loc_result;
14432 }
14433
14434 /* Return a descriptor that describes the concatenation of two locations.
14435    This is typically a complex variable.  */
14436
14437 static dw_loc_descr_ref
14438 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14439 {
14440   dw_loc_descr_ref cc_loc_result = NULL;
14441   dw_loc_descr_ref x0_ref
14442     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14443   dw_loc_descr_ref x1_ref
14444     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14445
14446   if (x0_ref == 0 || x1_ref == 0)
14447     return 0;
14448
14449   cc_loc_result = x0_ref;
14450   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14451
14452   add_loc_descr (&cc_loc_result, x1_ref);
14453   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14454
14455   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14456     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14457
14458   return cc_loc_result;
14459 }
14460
14461 /* Return a descriptor that describes the concatenation of N
14462    locations.  */
14463
14464 static dw_loc_descr_ref
14465 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14466 {
14467   unsigned int i;
14468   dw_loc_descr_ref cc_loc_result = NULL;
14469   unsigned int n = XVECLEN (concatn, 0);
14470
14471   for (i = 0; i < n; ++i)
14472     {
14473       dw_loc_descr_ref ref;
14474       rtx x = XVECEXP (concatn, 0, i);
14475
14476       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14477       if (ref == NULL)
14478         return NULL;
14479
14480       add_loc_descr (&cc_loc_result, ref);
14481       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14482     }
14483
14484   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14485     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14486
14487   return cc_loc_result;
14488 }
14489
14490 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14491    for DEBUG_IMPLICIT_PTR RTL.  */
14492
14493 static dw_loc_descr_ref
14494 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14495 {
14496   dw_loc_descr_ref ret;
14497   dw_die_ref ref;
14498
14499   if (dwarf_strict)
14500     return NULL;
14501   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14502               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14503               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14504   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14505   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14506   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14507   if (ref)
14508     {
14509       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14510       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14511       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14512     }
14513   else
14514     {
14515       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14516       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14517     }
14518   return ret;
14519 }
14520
14521 /* Output a proper Dwarf location descriptor for a variable or parameter
14522    which is either allocated in a register or in a memory location.  For a
14523    register, we just generate an OP_REG and the register number.  For a
14524    memory location we provide a Dwarf postfix expression describing how to
14525    generate the (dynamic) address of the object onto the address stack.
14526
14527    MODE is mode of the decl if this loc_descriptor is going to be used in
14528    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14529    allowed, VOIDmode otherwise.
14530
14531    If we don't know how to describe it, return 0.  */
14532
14533 static dw_loc_descr_ref
14534 loc_descriptor (rtx rtl, enum machine_mode mode,
14535                 enum var_init_status initialized)
14536 {
14537   dw_loc_descr_ref loc_result = NULL;
14538
14539   switch (GET_CODE (rtl))
14540     {
14541     case SUBREG:
14542       /* The case of a subreg may arise when we have a local (register)
14543          variable or a formal (register) parameter which doesn't quite fill
14544          up an entire register.  For now, just assume that it is
14545          legitimate to make the Dwarf info refer to the whole register which
14546          contains the given subreg.  */
14547       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14548       break;
14549
14550     case REG:
14551       loc_result = reg_loc_descriptor (rtl, initialized);
14552       break;
14553
14554     case MEM:
14555       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14556                                        initialized);
14557       if (loc_result == NULL)
14558         loc_result = tls_mem_loc_descriptor (rtl);
14559       if (loc_result == NULL)
14560         {
14561           rtx new_rtl = avoid_constant_pool_reference (rtl);
14562           if (new_rtl != rtl)
14563             loc_result = loc_descriptor (new_rtl, mode, initialized);
14564         }
14565       break;
14566
14567     case CONCAT:
14568       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14569                                           initialized);
14570       break;
14571
14572     case CONCATN:
14573       loc_result = concatn_loc_descriptor (rtl, initialized);
14574       break;
14575
14576     case VAR_LOCATION:
14577       /* Single part.  */
14578       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14579         {
14580           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14581           if (GET_CODE (loc) == EXPR_LIST)
14582             loc = XEXP (loc, 0);
14583           loc_result = loc_descriptor (loc, mode, initialized);
14584           break;
14585         }
14586
14587       rtl = XEXP (rtl, 1);
14588       /* FALLTHRU */
14589
14590     case PARALLEL:
14591       {
14592         rtvec par_elems = XVEC (rtl, 0);
14593         int num_elem = GET_NUM_ELEM (par_elems);
14594         enum machine_mode mode;
14595         int i;
14596
14597         /* Create the first one, so we have something to add to.  */
14598         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14599                                      VOIDmode, initialized);
14600         if (loc_result == NULL)
14601           return NULL;
14602         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14603         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14604         for (i = 1; i < num_elem; i++)
14605           {
14606             dw_loc_descr_ref temp;
14607
14608             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14609                                    VOIDmode, initialized);
14610             if (temp == NULL)
14611               return NULL;
14612             add_loc_descr (&loc_result, temp);
14613             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14614             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14615           }
14616       }
14617       break;
14618
14619     case CONST_INT:
14620       if (mode != VOIDmode && mode != BLKmode)
14621         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14622                                                     INTVAL (rtl));
14623       break;
14624
14625     case CONST_DOUBLE:
14626       if (mode == VOIDmode)
14627         mode = GET_MODE (rtl);
14628
14629       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14630         {
14631           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14632
14633           /* Note that a CONST_DOUBLE rtx could represent either an integer
14634              or a floating-point constant.  A CONST_DOUBLE is used whenever
14635              the constant requires more than one word in order to be
14636              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14637           loc_result = new_loc_descr (DW_OP_implicit_value,
14638                                       GET_MODE_SIZE (mode), 0);
14639           if (SCALAR_FLOAT_MODE_P (mode))
14640             {
14641               unsigned int length = GET_MODE_SIZE (mode);
14642               unsigned char *array
14643                   = (unsigned char*) ggc_alloc_atomic (length);
14644
14645               insert_float (rtl, array);
14646               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14647               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14648               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14649               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14650             }
14651           else
14652             {
14653               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14654               loc_result->dw_loc_oprnd2.v.val_double
14655                 = rtx_to_double_int (rtl);
14656             }
14657         }
14658       break;
14659
14660     case CONST_VECTOR:
14661       if (mode == VOIDmode)
14662         mode = GET_MODE (rtl);
14663
14664       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14665         {
14666           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14667           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14668           unsigned char *array = (unsigned char *)
14669             ggc_alloc_atomic (length * elt_size);
14670           unsigned int i;
14671           unsigned char *p;
14672
14673           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14674           switch (GET_MODE_CLASS (mode))
14675             {
14676             case MODE_VECTOR_INT:
14677               for (i = 0, p = array; i < length; i++, p += elt_size)
14678                 {
14679                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14680                   double_int val = rtx_to_double_int (elt);
14681
14682                   if (elt_size <= sizeof (HOST_WIDE_INT))
14683                     insert_int (double_int_to_shwi (val), elt_size, p);
14684                   else
14685                     {
14686                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14687                       insert_double (val, p);
14688                     }
14689                 }
14690               break;
14691
14692             case MODE_VECTOR_FLOAT:
14693               for (i = 0, p = array; i < length; i++, p += elt_size)
14694                 {
14695                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14696                   insert_float (elt, p);
14697                 }
14698               break;
14699
14700             default:
14701               gcc_unreachable ();
14702             }
14703
14704           loc_result = new_loc_descr (DW_OP_implicit_value,
14705                                       length * elt_size, 0);
14706           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14707           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14708           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14709           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14710         }
14711       break;
14712
14713     case CONST:
14714       if (mode == VOIDmode
14715           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14716           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14717           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14718         {
14719           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14720           break;
14721         }
14722       /* FALLTHROUGH */
14723     case SYMBOL_REF:
14724       if (!const_ok_for_output (rtl))
14725         break;
14726     case LABEL_REF:
14727       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14728           && (dwarf_version >= 4 || !dwarf_strict))
14729         {
14730           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14731           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14732           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14733           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14734           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14735         }
14736       break;
14737
14738     case DEBUG_IMPLICIT_PTR:
14739       loc_result = implicit_ptr_descriptor (rtl, 0);
14740       break;
14741
14742     case PLUS:
14743       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14744           && CONST_INT_P (XEXP (rtl, 1)))
14745         {
14746           loc_result
14747             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14748           break;
14749         }
14750       /* FALLTHRU */
14751     default:
14752       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14753           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14754           && (dwarf_version >= 4 || !dwarf_strict))
14755         {
14756           /* Value expression.  */
14757           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14758           if (loc_result)
14759             add_loc_descr (&loc_result,
14760                            new_loc_descr (DW_OP_stack_value, 0, 0));
14761         }
14762       break;
14763     }
14764
14765   return loc_result;
14766 }
14767
14768 /* We need to figure out what section we should use as the base for the
14769    address ranges where a given location is valid.
14770    1. If this particular DECL has a section associated with it, use that.
14771    2. If this function has a section associated with it, use that.
14772    3. Otherwise, use the text section.
14773    XXX: If you split a variable across multiple sections, we won't notice.  */
14774
14775 static const char *
14776 secname_for_decl (const_tree decl)
14777 {
14778   const char *secname;
14779
14780   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14781     {
14782       tree sectree = DECL_SECTION_NAME (decl);
14783       secname = TREE_STRING_POINTER (sectree);
14784     }
14785   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14786     {
14787       tree sectree = DECL_SECTION_NAME (current_function_decl);
14788       secname = TREE_STRING_POINTER (sectree);
14789     }
14790   else if (cfun && in_cold_section_p)
14791     secname = crtl->subsections.cold_section_label;
14792   else
14793     secname = text_section_label;
14794
14795   return secname;
14796 }
14797
14798 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14799
14800 static bool
14801 decl_by_reference_p (tree decl)
14802 {
14803   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14804            || TREE_CODE (decl) == VAR_DECL)
14805           && DECL_BY_REFERENCE (decl));
14806 }
14807
14808 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14809    for VARLOC.  */
14810
14811 static dw_loc_descr_ref
14812 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14813                enum var_init_status initialized)
14814 {
14815   int have_address = 0;
14816   dw_loc_descr_ref descr;
14817   enum machine_mode mode;
14818
14819   if (want_address != 2)
14820     {
14821       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14822       /* Single part.  */
14823       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14824         {
14825           varloc = PAT_VAR_LOCATION_LOC (varloc);
14826           if (GET_CODE (varloc) == EXPR_LIST)
14827             varloc = XEXP (varloc, 0);
14828           mode = GET_MODE (varloc);
14829           if (MEM_P (varloc))
14830             {
14831               rtx addr = XEXP (varloc, 0);
14832               descr = mem_loc_descriptor (addr, mode, initialized);
14833               if (descr)
14834                 have_address = 1;
14835               else
14836                 {
14837                   rtx x = avoid_constant_pool_reference (varloc);
14838                   if (x != varloc)
14839                     descr = mem_loc_descriptor (x, mode, initialized);
14840                 }
14841             }
14842           else
14843             descr = mem_loc_descriptor (varloc, mode, initialized);
14844         }
14845       else
14846         return 0;
14847     }
14848   else
14849     {
14850       if (GET_CODE (varloc) == VAR_LOCATION)
14851         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14852       else
14853         mode = DECL_MODE (loc);
14854       descr = loc_descriptor (varloc, mode, initialized);
14855       have_address = 1;
14856     }
14857
14858   if (!descr)
14859     return 0;
14860
14861   if (want_address == 2 && !have_address
14862       && (dwarf_version >= 4 || !dwarf_strict))
14863     {
14864       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14865         {
14866           expansion_failed (loc, NULL_RTX,
14867                             "DWARF address size mismatch");
14868           return 0;
14869         }
14870       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14871       have_address = 1;
14872     }
14873   /* Show if we can't fill the request for an address.  */
14874   if (want_address && !have_address)
14875     {
14876       expansion_failed (loc, NULL_RTX,
14877                         "Want address and only have value");
14878       return 0;
14879     }
14880
14881   /* If we've got an address and don't want one, dereference.  */
14882   if (!want_address && have_address)
14883     {
14884       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14885       enum dwarf_location_atom op;
14886
14887       if (size > DWARF2_ADDR_SIZE || size == -1)
14888         {
14889           expansion_failed (loc, NULL_RTX,
14890                             "DWARF address size mismatch");
14891           return 0;
14892         }
14893       else if (size == DWARF2_ADDR_SIZE)
14894         op = DW_OP_deref;
14895       else
14896         op = DW_OP_deref_size;
14897
14898       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14899     }
14900
14901   return descr;
14902 }
14903
14904 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14905    if it is not possible.  */
14906
14907 static dw_loc_descr_ref
14908 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14909 {
14910   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14911     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14912   else if (dwarf_version >= 3 || !dwarf_strict)
14913     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14914   else
14915     return NULL;
14916 }
14917
14918 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14919    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14920
14921 static dw_loc_descr_ref
14922 dw_sra_loc_expr (tree decl, rtx loc)
14923 {
14924   rtx p;
14925   unsigned int padsize = 0;
14926   dw_loc_descr_ref descr, *descr_tail;
14927   unsigned HOST_WIDE_INT decl_size;
14928   rtx varloc;
14929   enum var_init_status initialized;
14930
14931   if (DECL_SIZE (decl) == NULL
14932       || !host_integerp (DECL_SIZE (decl), 1))
14933     return NULL;
14934
14935   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14936   descr = NULL;
14937   descr_tail = &descr;
14938
14939   for (p = loc; p; p = XEXP (p, 1))
14940     {
14941       unsigned int bitsize = decl_piece_bitsize (p);
14942       rtx loc_note = *decl_piece_varloc_ptr (p);
14943       dw_loc_descr_ref cur_descr;
14944       dw_loc_descr_ref *tail, last = NULL;
14945       unsigned int opsize = 0;
14946
14947       if (loc_note == NULL_RTX
14948           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14949         {
14950           padsize += bitsize;
14951           continue;
14952         }
14953       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14954       varloc = NOTE_VAR_LOCATION (loc_note);
14955       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14956       if (cur_descr == NULL)
14957         {
14958           padsize += bitsize;
14959           continue;
14960         }
14961
14962       /* Check that cur_descr either doesn't use
14963          DW_OP_*piece operations, or their sum is equal
14964          to bitsize.  Otherwise we can't embed it.  */
14965       for (tail = &cur_descr; *tail != NULL;
14966            tail = &(*tail)->dw_loc_next)
14967         if ((*tail)->dw_loc_opc == DW_OP_piece)
14968           {
14969             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14970                       * BITS_PER_UNIT;
14971             last = *tail;
14972           }
14973         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14974           {
14975             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14976             last = *tail;
14977           }
14978
14979       if (last != NULL && opsize != bitsize)
14980         {
14981           padsize += bitsize;
14982           continue;
14983         }
14984
14985       /* If there is a hole, add DW_OP_*piece after empty DWARF
14986          expression, which means that those bits are optimized out.  */
14987       if (padsize)
14988         {
14989           if (padsize > decl_size)
14990             return NULL;
14991           decl_size -= padsize;
14992           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14993           if (*descr_tail == NULL)
14994             return NULL;
14995           descr_tail = &(*descr_tail)->dw_loc_next;
14996           padsize = 0;
14997         }
14998       *descr_tail = cur_descr;
14999       descr_tail = tail;
15000       if (bitsize > decl_size)
15001         return NULL;
15002       decl_size -= bitsize;
15003       if (last == NULL)
15004         {
15005           HOST_WIDE_INT offset = 0;
15006           if (GET_CODE (varloc) == VAR_LOCATION
15007               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15008             {
15009               varloc = PAT_VAR_LOCATION_LOC (varloc);
15010               if (GET_CODE (varloc) == EXPR_LIST)
15011                 varloc = XEXP (varloc, 0);
15012             }
15013           do 
15014             {
15015               if (GET_CODE (varloc) == CONST
15016                   || GET_CODE (varloc) == SIGN_EXTEND
15017                   || GET_CODE (varloc) == ZERO_EXTEND)
15018                 varloc = XEXP (varloc, 0);
15019               else if (GET_CODE (varloc) == SUBREG)
15020                 varloc = SUBREG_REG (varloc);
15021               else
15022                 break;
15023             }
15024           while (1);
15025           /* DW_OP_bit_size offset should be zero for register
15026              or implicit location descriptions and empty location
15027              descriptions, but for memory addresses needs big endian
15028              adjustment.  */
15029           if (MEM_P (varloc))
15030             {
15031               unsigned HOST_WIDE_INT memsize
15032                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15033               if (memsize != bitsize)
15034                 {
15035                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15036                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15037                     return NULL;
15038                   if (memsize < bitsize)
15039                     return NULL;
15040                   if (BITS_BIG_ENDIAN)
15041                     offset = memsize - bitsize;
15042                 }
15043             }
15044
15045           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15046           if (*descr_tail == NULL)
15047             return NULL;
15048           descr_tail = &(*descr_tail)->dw_loc_next;
15049         }
15050     }
15051
15052   /* If there were any non-empty expressions, add padding till the end of
15053      the decl.  */
15054   if (descr != NULL && decl_size != 0)
15055     {
15056       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15057       if (*descr_tail == NULL)
15058         return NULL;
15059     }
15060   return descr;
15061 }
15062
15063 /* Return the dwarf representation of the location list LOC_LIST of
15064    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15065    function.  */
15066
15067 static dw_loc_list_ref
15068 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15069 {
15070   const char *endname, *secname;
15071   rtx varloc;
15072   enum var_init_status initialized;
15073   struct var_loc_node *node;
15074   dw_loc_descr_ref descr;
15075   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15076   dw_loc_list_ref list = NULL;
15077   dw_loc_list_ref *listp = &list;
15078
15079   /* Now that we know what section we are using for a base,
15080      actually construct the list of locations.
15081      The first location information is what is passed to the
15082      function that creates the location list, and the remaining
15083      locations just get added on to that list.
15084      Note that we only know the start address for a location
15085      (IE location changes), so to build the range, we use
15086      the range [current location start, next location start].
15087      This means we have to special case the last node, and generate
15088      a range of [last location start, end of function label].  */
15089
15090   secname = secname_for_decl (decl);
15091
15092   for (node = loc_list->first; node; node = node->next)
15093     if (GET_CODE (node->loc) == EXPR_LIST
15094         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15095       {
15096         if (GET_CODE (node->loc) == EXPR_LIST)
15097           {
15098             /* This requires DW_OP_{,bit_}piece, which is not usable
15099                inside DWARF expressions.  */
15100             if (want_address != 2)
15101               continue;
15102             descr = dw_sra_loc_expr (decl, node->loc);
15103             if (descr == NULL)
15104               continue;
15105           }
15106         else
15107           {
15108             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15109             varloc = NOTE_VAR_LOCATION (node->loc);
15110             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15111           }
15112         if (descr)
15113           {
15114             /* The variable has a location between NODE->LABEL and
15115                NODE->NEXT->LABEL.  */
15116             if (node->next)
15117               endname = node->next->label;
15118             /* If the variable has a location at the last label
15119                it keeps its location until the end of function.  */
15120             else if (!current_function_decl)
15121               endname = text_end_label;
15122             else
15123               {
15124                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15125                                              current_function_funcdef_no);
15126                 endname = ggc_strdup (label_id);
15127               }
15128
15129             *listp = new_loc_list (descr, node->label, endname, secname);
15130             listp = &(*listp)->dw_loc_next;
15131           }
15132       }
15133
15134   /* Try to avoid the overhead of a location list emitting a location
15135      expression instead, but only if we didn't have more than one
15136      location entry in the first place.  If some entries were not
15137      representable, we don't want to pretend a single entry that was
15138      applies to the entire scope in which the variable is
15139      available.  */
15140   if (list && loc_list->first->next)
15141     gen_llsym (list);
15142
15143   return list;
15144 }
15145
15146 /* Return if the loc_list has only single element and thus can be represented
15147    as location description.   */
15148
15149 static bool
15150 single_element_loc_list_p (dw_loc_list_ref list)
15151 {
15152   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15153   return !list->ll_symbol;
15154 }
15155
15156 /* To each location in list LIST add loc descr REF.  */
15157
15158 static void
15159 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15160 {
15161   dw_loc_descr_ref copy;
15162   add_loc_descr (&list->expr, ref);
15163   list = list->dw_loc_next;
15164   while (list)
15165     {
15166       copy = ggc_alloc_dw_loc_descr_node ();
15167       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15168       add_loc_descr (&list->expr, copy);
15169       while (copy->dw_loc_next)
15170         {
15171           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15172           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15173           copy->dw_loc_next = new_copy;
15174           copy = new_copy;
15175         }
15176       list = list->dw_loc_next;
15177     }
15178 }
15179
15180 /* Given two lists RET and LIST
15181    produce location list that is result of adding expression in LIST
15182    to expression in RET on each possition in program.
15183    Might be destructive on both RET and LIST.
15184
15185    TODO: We handle only simple cases of RET or LIST having at most one
15186    element. General case would inolve sorting the lists in program order
15187    and merging them that will need some additional work.
15188    Adding that will improve quality of debug info especially for SRA-ed
15189    structures.  */
15190
15191 static void
15192 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15193 {
15194   if (!list)
15195     return;
15196   if (!*ret)
15197     {
15198       *ret = list;
15199       return;
15200     }
15201   if (!list->dw_loc_next)
15202     {
15203       add_loc_descr_to_each (*ret, list->expr);
15204       return;
15205     }
15206   if (!(*ret)->dw_loc_next)
15207     {
15208       add_loc_descr_to_each (list, (*ret)->expr);
15209       *ret = list;
15210       return;
15211     }
15212   expansion_failed (NULL_TREE, NULL_RTX,
15213                     "Don't know how to merge two non-trivial"
15214                     " location lists.\n");
15215   *ret = NULL;
15216   return;
15217 }
15218
15219 /* LOC is constant expression.  Try a luck, look it up in constant
15220    pool and return its loc_descr of its address.  */
15221
15222 static dw_loc_descr_ref
15223 cst_pool_loc_descr (tree loc)
15224 {
15225   /* Get an RTL for this, if something has been emitted.  */
15226   rtx rtl = lookup_constant_def (loc);
15227   enum machine_mode mode;
15228
15229   if (!rtl || !MEM_P (rtl))
15230     {
15231       gcc_assert (!rtl);
15232       return 0;
15233     }
15234   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15235
15236   /* TODO: We might get more coverage if we was actually delaying expansion
15237      of all expressions till end of compilation when constant pools are fully
15238      populated.  */
15239   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15240     {
15241       expansion_failed (loc, NULL_RTX,
15242                         "CST value in contant pool but not marked.");
15243       return 0;
15244     }
15245   mode = GET_MODE (rtl);
15246   rtl = XEXP (rtl, 0);
15247   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15248 }
15249
15250 /* Return dw_loc_list representing address of addr_expr LOC
15251    by looking for innder INDIRECT_REF expression and turing it
15252    into simple arithmetics.  */
15253
15254 static dw_loc_list_ref
15255 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15256 {
15257   tree obj, offset;
15258   HOST_WIDE_INT bitsize, bitpos, bytepos;
15259   enum machine_mode mode;
15260   int volatilep;
15261   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15262   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15263
15264   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15265                              &bitsize, &bitpos, &offset, &mode,
15266                              &unsignedp, &volatilep, false);
15267   STRIP_NOPS (obj);
15268   if (bitpos % BITS_PER_UNIT)
15269     {
15270       expansion_failed (loc, NULL_RTX, "bitfield access");
15271       return 0;
15272     }
15273   if (!INDIRECT_REF_P (obj))
15274     {
15275       expansion_failed (obj,
15276                         NULL_RTX, "no indirect ref in inner refrence");
15277       return 0;
15278     }
15279   if (!offset && !bitpos)
15280     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15281   else if (toplev
15282            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15283            && (dwarf_version >= 4 || !dwarf_strict))
15284     {
15285       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15286       if (!list_ret)
15287         return 0;
15288       if (offset)
15289         {
15290           /* Variable offset.  */
15291           list_ret1 = loc_list_from_tree (offset, 0);
15292           if (list_ret1 == 0)
15293             return 0;
15294           add_loc_list (&list_ret, list_ret1);
15295           if (!list_ret)
15296             return 0;
15297           add_loc_descr_to_each (list_ret,
15298                                  new_loc_descr (DW_OP_plus, 0, 0));
15299         }
15300       bytepos = bitpos / BITS_PER_UNIT;
15301       if (bytepos > 0)
15302         add_loc_descr_to_each (list_ret,
15303                                new_loc_descr (DW_OP_plus_uconst,
15304                                               bytepos, 0));
15305       else if (bytepos < 0)
15306         loc_list_plus_const (list_ret, bytepos);
15307       add_loc_descr_to_each (list_ret,
15308                              new_loc_descr (DW_OP_stack_value, 0, 0));
15309     }
15310   return list_ret;
15311 }
15312
15313
15314 /* Generate Dwarf location list representing LOC.
15315    If WANT_ADDRESS is false, expression computing LOC will be computed
15316    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15317    if WANT_ADDRESS is 2, expression computing address useable in location
15318      will be returned (i.e. DW_OP_reg can be used
15319      to refer to register values).  */
15320
15321 static dw_loc_list_ref
15322 loc_list_from_tree (tree loc, int want_address)
15323 {
15324   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15325   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15326   int have_address = 0;
15327   enum dwarf_location_atom op;
15328
15329   /* ??? Most of the time we do not take proper care for sign/zero
15330      extending the values properly.  Hopefully this won't be a real
15331      problem...  */
15332
15333   switch (TREE_CODE (loc))
15334     {
15335     case ERROR_MARK:
15336       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15337       return 0;
15338
15339     case PLACEHOLDER_EXPR:
15340       /* This case involves extracting fields from an object to determine the
15341          position of other fields.  We don't try to encode this here.  The
15342          only user of this is Ada, which encodes the needed information using
15343          the names of types.  */
15344       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15345       return 0;
15346
15347     case CALL_EXPR:
15348       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15349       /* There are no opcodes for these operations.  */
15350       return 0;
15351
15352     case PREINCREMENT_EXPR:
15353     case PREDECREMENT_EXPR:
15354     case POSTINCREMENT_EXPR:
15355     case POSTDECREMENT_EXPR:
15356       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15357       /* There are no opcodes for these operations.  */
15358       return 0;
15359
15360     case ADDR_EXPR:
15361       /* If we already want an address, see if there is INDIRECT_REF inside
15362          e.g. for &this->field.  */
15363       if (want_address)
15364         {
15365           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15366                        (loc, want_address == 2);
15367           if (list_ret)
15368             have_address = 1;
15369           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15370                    && (ret = cst_pool_loc_descr (loc)))
15371             have_address = 1;
15372         }
15373         /* Otherwise, process the argument and look for the address.  */
15374       if (!list_ret && !ret)
15375         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15376       else
15377         {
15378           if (want_address)
15379             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15380           return NULL;
15381         }
15382       break;
15383
15384     case VAR_DECL:
15385       if (DECL_THREAD_LOCAL_P (loc))
15386         {
15387           rtx rtl;
15388           enum dwarf_location_atom first_op;
15389           enum dwarf_location_atom second_op;
15390           bool dtprel = false;
15391
15392           if (targetm.have_tls)
15393             {
15394               /* If this is not defined, we have no way to emit the
15395                  data.  */
15396               if (!targetm.asm_out.output_dwarf_dtprel)
15397                 return 0;
15398
15399                /* The way DW_OP_GNU_push_tls_address is specified, we
15400                   can only look up addresses of objects in the current
15401                   module.  We used DW_OP_addr as first op, but that's
15402                   wrong, because DW_OP_addr is relocated by the debug
15403                   info consumer, while DW_OP_GNU_push_tls_address
15404                   operand shouldn't be.  */
15405               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15406                 return 0;
15407               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15408               dtprel = true;
15409               second_op = DW_OP_GNU_push_tls_address;
15410             }
15411           else
15412             {
15413               if (!targetm.emutls.debug_form_tls_address
15414                   || !(dwarf_version >= 3 || !dwarf_strict))
15415                 return 0;
15416               /* We stuffed the control variable into the DECL_VALUE_EXPR
15417                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15418                  no longer appear in gimple code.  We used the control
15419                  variable in specific so that we could pick it up here.  */
15420               loc = DECL_VALUE_EXPR (loc);
15421               first_op = DW_OP_addr;
15422               second_op = DW_OP_form_tls_address;
15423             }
15424
15425           rtl = rtl_for_decl_location (loc);
15426           if (rtl == NULL_RTX)
15427             return 0;
15428
15429           if (!MEM_P (rtl))
15430             return 0;
15431           rtl = XEXP (rtl, 0);
15432           if (! CONSTANT_P (rtl))
15433             return 0;
15434
15435           ret = new_loc_descr (first_op, 0, 0);
15436           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15437           ret->dw_loc_oprnd1.v.val_addr = rtl;
15438           ret->dtprel = dtprel;
15439
15440           ret1 = new_loc_descr (second_op, 0, 0);
15441           add_loc_descr (&ret, ret1);
15442
15443           have_address = 1;
15444           break;
15445         }
15446       /* FALLTHRU */
15447
15448     case PARM_DECL:
15449       if (DECL_HAS_VALUE_EXPR_P (loc))
15450         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15451                                    want_address);
15452       /* FALLTHRU */
15453
15454     case RESULT_DECL:
15455     case FUNCTION_DECL:
15456       {
15457         rtx rtl;
15458         var_loc_list *loc_list = lookup_decl_loc (loc);
15459
15460         if (loc_list && loc_list->first)
15461           {
15462             list_ret = dw_loc_list (loc_list, loc, want_address);
15463             have_address = want_address != 0;
15464             break;
15465           }
15466         rtl = rtl_for_decl_location (loc);
15467         if (rtl == NULL_RTX)
15468           {
15469             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15470             return 0;
15471           }
15472         else if (CONST_INT_P (rtl))
15473           {
15474             HOST_WIDE_INT val = INTVAL (rtl);
15475             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15476               val &= GET_MODE_MASK (DECL_MODE (loc));
15477             ret = int_loc_descriptor (val);
15478           }
15479         else if (GET_CODE (rtl) == CONST_STRING)
15480           {
15481             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15482             return 0;
15483           }
15484         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15485           {
15486             ret = new_loc_descr (DW_OP_addr, 0, 0);
15487             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15488             ret->dw_loc_oprnd1.v.val_addr = rtl;
15489           }
15490         else
15491           {
15492             enum machine_mode mode;
15493
15494             /* Certain constructs can only be represented at top-level.  */
15495             if (want_address == 2)
15496               {
15497                 ret = loc_descriptor (rtl, VOIDmode,
15498                                       VAR_INIT_STATUS_INITIALIZED);
15499                 have_address = 1;
15500               }
15501             else
15502               {
15503                 mode = GET_MODE (rtl);
15504                 if (MEM_P (rtl))
15505                   {
15506                     rtl = XEXP (rtl, 0);
15507                     have_address = 1;
15508                   }
15509                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15510               }
15511             if (!ret)
15512               expansion_failed (loc, rtl,
15513                                 "failed to produce loc descriptor for rtl");
15514           }
15515       }
15516       break;
15517
15518     case MEM_REF:
15519       /* ??? FIXME.  */
15520       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15521         return 0;
15522       /* Fallthru.  */
15523     case INDIRECT_REF:
15524       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15525       have_address = 1;
15526       break;
15527
15528     case COMPOUND_EXPR:
15529       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15530
15531     CASE_CONVERT:
15532     case VIEW_CONVERT_EXPR:
15533     case SAVE_EXPR:
15534     case MODIFY_EXPR:
15535       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15536
15537     case COMPONENT_REF:
15538     case BIT_FIELD_REF:
15539     case ARRAY_REF:
15540     case ARRAY_RANGE_REF:
15541     case REALPART_EXPR:
15542     case IMAGPART_EXPR:
15543       {
15544         tree obj, offset;
15545         HOST_WIDE_INT bitsize, bitpos, bytepos;
15546         enum machine_mode mode;
15547         int volatilep;
15548         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15549
15550         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15551                                    &unsignedp, &volatilep, false);
15552
15553         gcc_assert (obj != loc);
15554
15555         list_ret = loc_list_from_tree (obj,
15556                                        want_address == 2
15557                                        && !bitpos && !offset ? 2 : 1);
15558         /* TODO: We can extract value of the small expression via shifting even
15559            for nonzero bitpos.  */
15560         if (list_ret == 0)
15561           return 0;
15562         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15563           {
15564             expansion_failed (loc, NULL_RTX,
15565                               "bitfield access");
15566             return 0;
15567           }
15568
15569         if (offset != NULL_TREE)
15570           {
15571             /* Variable offset.  */
15572             list_ret1 = loc_list_from_tree (offset, 0);
15573             if (list_ret1 == 0)
15574               return 0;
15575             add_loc_list (&list_ret, list_ret1);
15576             if (!list_ret)
15577               return 0;
15578             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15579           }
15580
15581         bytepos = bitpos / BITS_PER_UNIT;
15582         if (bytepos > 0)
15583           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15584         else if (bytepos < 0)
15585           loc_list_plus_const (list_ret, bytepos);
15586
15587         have_address = 1;
15588         break;
15589       }
15590
15591     case INTEGER_CST:
15592       if ((want_address || !host_integerp (loc, 0))
15593           && (ret = cst_pool_loc_descr (loc)))
15594         have_address = 1;
15595       else if (want_address == 2
15596                && host_integerp (loc, 0)
15597                && (ret = address_of_int_loc_descriptor
15598                            (int_size_in_bytes (TREE_TYPE (loc)),
15599                             tree_low_cst (loc, 0))))
15600         have_address = 1;
15601       else if (host_integerp (loc, 0))
15602         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15603       else
15604         {
15605           expansion_failed (loc, NULL_RTX,
15606                             "Integer operand is not host integer");
15607           return 0;
15608         }
15609       break;
15610
15611     case CONSTRUCTOR:
15612     case REAL_CST:
15613     case STRING_CST:
15614     case COMPLEX_CST:
15615       if ((ret = cst_pool_loc_descr (loc)))
15616         have_address = 1;
15617       else
15618       /* We can construct small constants here using int_loc_descriptor.  */
15619         expansion_failed (loc, NULL_RTX,
15620                           "constructor or constant not in constant pool");
15621       break;
15622
15623     case TRUTH_AND_EXPR:
15624     case TRUTH_ANDIF_EXPR:
15625     case BIT_AND_EXPR:
15626       op = DW_OP_and;
15627       goto do_binop;
15628
15629     case TRUTH_XOR_EXPR:
15630     case BIT_XOR_EXPR:
15631       op = DW_OP_xor;
15632       goto do_binop;
15633
15634     case TRUTH_OR_EXPR:
15635     case TRUTH_ORIF_EXPR:
15636     case BIT_IOR_EXPR:
15637       op = DW_OP_or;
15638       goto do_binop;
15639
15640     case FLOOR_DIV_EXPR:
15641     case CEIL_DIV_EXPR:
15642     case ROUND_DIV_EXPR:
15643     case TRUNC_DIV_EXPR:
15644       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15645         return 0;
15646       op = DW_OP_div;
15647       goto do_binop;
15648
15649     case MINUS_EXPR:
15650       op = DW_OP_minus;
15651       goto do_binop;
15652
15653     case FLOOR_MOD_EXPR:
15654     case CEIL_MOD_EXPR:
15655     case ROUND_MOD_EXPR:
15656     case TRUNC_MOD_EXPR:
15657       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15658         {
15659           op = DW_OP_mod;
15660           goto do_binop;
15661         }
15662       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15663       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15664       if (list_ret == 0 || list_ret1 == 0)
15665         return 0;
15666
15667       add_loc_list (&list_ret, list_ret1);
15668       if (list_ret == 0)
15669         return 0;
15670       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15671       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15672       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15673       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15674       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15675       break;
15676
15677     case MULT_EXPR:
15678       op = DW_OP_mul;
15679       goto do_binop;
15680
15681     case LSHIFT_EXPR:
15682       op = DW_OP_shl;
15683       goto do_binop;
15684
15685     case RSHIFT_EXPR:
15686       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15687       goto do_binop;
15688
15689     case POINTER_PLUS_EXPR:
15690     case PLUS_EXPR:
15691       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15692         {
15693           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15694           if (list_ret == 0)
15695             return 0;
15696
15697           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15698           break;
15699         }
15700
15701       op = DW_OP_plus;
15702       goto do_binop;
15703
15704     case LE_EXPR:
15705       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15706         return 0;
15707
15708       op = DW_OP_le;
15709       goto do_binop;
15710
15711     case GE_EXPR:
15712       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15713         return 0;
15714
15715       op = DW_OP_ge;
15716       goto do_binop;
15717
15718     case LT_EXPR:
15719       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15720         return 0;
15721
15722       op = DW_OP_lt;
15723       goto do_binop;
15724
15725     case GT_EXPR:
15726       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15727         return 0;
15728
15729       op = DW_OP_gt;
15730       goto do_binop;
15731
15732     case EQ_EXPR:
15733       op = DW_OP_eq;
15734       goto do_binop;
15735
15736     case NE_EXPR:
15737       op = DW_OP_ne;
15738       goto do_binop;
15739
15740     do_binop:
15741       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15742       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15743       if (list_ret == 0 || list_ret1 == 0)
15744         return 0;
15745
15746       add_loc_list (&list_ret, list_ret1);
15747       if (list_ret == 0)
15748         return 0;
15749       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15750       break;
15751
15752     case TRUTH_NOT_EXPR:
15753     case BIT_NOT_EXPR:
15754       op = DW_OP_not;
15755       goto do_unop;
15756
15757     case ABS_EXPR:
15758       op = DW_OP_abs;
15759       goto do_unop;
15760
15761     case NEGATE_EXPR:
15762       op = DW_OP_neg;
15763       goto do_unop;
15764
15765     do_unop:
15766       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15767       if (list_ret == 0)
15768         return 0;
15769
15770       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15771       break;
15772
15773     case MIN_EXPR:
15774     case MAX_EXPR:
15775       {
15776         const enum tree_code code =
15777           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15778
15779         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15780                       build2 (code, integer_type_node,
15781                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15782                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15783       }
15784
15785       /* ... fall through ...  */
15786
15787     case COND_EXPR:
15788       {
15789         dw_loc_descr_ref lhs
15790           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15791         dw_loc_list_ref rhs
15792           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15793         dw_loc_descr_ref bra_node, jump_node, tmp;
15794
15795         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15796         if (list_ret == 0 || lhs == 0 || rhs == 0)
15797           return 0;
15798
15799         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15800         add_loc_descr_to_each (list_ret, bra_node);
15801
15802         add_loc_list (&list_ret, rhs);
15803         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15804         add_loc_descr_to_each (list_ret, jump_node);
15805
15806         add_loc_descr_to_each (list_ret, lhs);
15807         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15808         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15809
15810         /* ??? Need a node to point the skip at.  Use a nop.  */
15811         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15812         add_loc_descr_to_each (list_ret, tmp);
15813         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15814         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15815       }
15816       break;
15817
15818     case FIX_TRUNC_EXPR:
15819       return 0;
15820
15821     default:
15822       /* Leave front-end specific codes as simply unknown.  This comes
15823          up, for instance, with the C STMT_EXPR.  */
15824       if ((unsigned int) TREE_CODE (loc)
15825           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15826         {
15827           expansion_failed (loc, NULL_RTX,
15828                             "language specific tree node");
15829           return 0;
15830         }
15831
15832 #ifdef ENABLE_CHECKING
15833       /* Otherwise this is a generic code; we should just lists all of
15834          these explicitly.  We forgot one.  */
15835       gcc_unreachable ();
15836 #else
15837       /* In a release build, we want to degrade gracefully: better to
15838          generate incomplete debugging information than to crash.  */
15839       return NULL;
15840 #endif
15841     }
15842
15843   if (!ret && !list_ret)
15844     return 0;
15845
15846   if (want_address == 2 && !have_address
15847       && (dwarf_version >= 4 || !dwarf_strict))
15848     {
15849       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15850         {
15851           expansion_failed (loc, NULL_RTX,
15852                             "DWARF address size mismatch");
15853           return 0;
15854         }
15855       if (ret)
15856         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15857       else
15858         add_loc_descr_to_each (list_ret,
15859                                new_loc_descr (DW_OP_stack_value, 0, 0));
15860       have_address = 1;
15861     }
15862   /* Show if we can't fill the request for an address.  */
15863   if (want_address && !have_address)
15864     {
15865       expansion_failed (loc, NULL_RTX,
15866                         "Want address and only have value");
15867       return 0;
15868     }
15869
15870   gcc_assert (!ret || !list_ret);
15871
15872   /* If we've got an address and don't want one, dereference.  */
15873   if (!want_address && have_address)
15874     {
15875       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15876
15877       if (size > DWARF2_ADDR_SIZE || size == -1)
15878         {
15879           expansion_failed (loc, NULL_RTX,
15880                             "DWARF address size mismatch");
15881           return 0;
15882         }
15883       else if (size == DWARF2_ADDR_SIZE)
15884         op = DW_OP_deref;
15885       else
15886         op = DW_OP_deref_size;
15887
15888       if (ret)
15889         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15890       else
15891         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15892     }
15893   if (ret)
15894     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15895
15896   return list_ret;
15897 }
15898
15899 /* Same as above but return only single location expression.  */
15900 static dw_loc_descr_ref
15901 loc_descriptor_from_tree (tree loc, int want_address)
15902 {
15903   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15904   if (!ret)
15905     return NULL;
15906   if (ret->dw_loc_next)
15907     {
15908       expansion_failed (loc, NULL_RTX,
15909                         "Location list where only loc descriptor needed");
15910       return NULL;
15911     }
15912   return ret->expr;
15913 }
15914
15915 /* Given a value, round it up to the lowest multiple of `boundary'
15916    which is not less than the value itself.  */
15917
15918 static inline HOST_WIDE_INT
15919 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15920 {
15921   return (((value + boundary - 1) / boundary) * boundary);
15922 }
15923
15924 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15925    pointer to the declared type for the relevant field variable, or return
15926    `integer_type_node' if the given node turns out to be an
15927    ERROR_MARK node.  */
15928
15929 static inline tree
15930 field_type (const_tree decl)
15931 {
15932   tree type;
15933
15934   if (TREE_CODE (decl) == ERROR_MARK)
15935     return integer_type_node;
15936
15937   type = DECL_BIT_FIELD_TYPE (decl);
15938   if (type == NULL_TREE)
15939     type = TREE_TYPE (decl);
15940
15941   return type;
15942 }
15943
15944 /* Given a pointer to a tree node, return the alignment in bits for
15945    it, or else return BITS_PER_WORD if the node actually turns out to
15946    be an ERROR_MARK node.  */
15947
15948 static inline unsigned
15949 simple_type_align_in_bits (const_tree type)
15950 {
15951   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15952 }
15953
15954 static inline unsigned
15955 simple_decl_align_in_bits (const_tree decl)
15956 {
15957   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15958 }
15959
15960 /* Return the result of rounding T up to ALIGN.  */
15961
15962 static inline double_int
15963 round_up_to_align (double_int t, unsigned int align)
15964 {
15965   double_int alignd = uhwi_to_double_int (align);
15966   t = double_int_add (t, alignd);
15967   t = double_int_add (t, double_int_minus_one);
15968   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15969   t = double_int_mul (t, alignd);
15970   return t;
15971 }
15972
15973 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15974    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15975    or return 0 if we are unable to determine what that offset is, either
15976    because the argument turns out to be a pointer to an ERROR_MARK node, or
15977    because the offset is actually variable.  (We can't handle the latter case
15978    just yet).  */
15979
15980 static HOST_WIDE_INT
15981 field_byte_offset (const_tree decl)
15982 {
15983   double_int object_offset_in_bits;
15984   double_int object_offset_in_bytes;
15985   double_int bitpos_int;
15986
15987   if (TREE_CODE (decl) == ERROR_MARK)
15988     return 0;
15989
15990   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15991
15992   /* We cannot yet cope with fields whose positions are variable, so
15993      for now, when we see such things, we simply return 0.  Someday, we may
15994      be able to handle such cases, but it will be damn difficult.  */
15995   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15996     return 0;
15997
15998   bitpos_int = tree_to_double_int (bit_position (decl));
15999
16000 #ifdef PCC_BITFIELD_TYPE_MATTERS
16001   if (PCC_BITFIELD_TYPE_MATTERS)
16002     {
16003       tree type;
16004       tree field_size_tree;
16005       double_int deepest_bitpos;
16006       double_int field_size_in_bits;
16007       unsigned int type_align_in_bits;
16008       unsigned int decl_align_in_bits;
16009       double_int type_size_in_bits;
16010
16011       type = field_type (decl);
16012       type_size_in_bits = double_int_type_size_in_bits (type);
16013       type_align_in_bits = simple_type_align_in_bits (type);
16014
16015       field_size_tree = DECL_SIZE (decl);
16016
16017       /* The size could be unspecified if there was an error, or for
16018          a flexible array member.  */
16019       if (!field_size_tree)
16020         field_size_tree = bitsize_zero_node;
16021
16022       /* If the size of the field is not constant, use the type size.  */
16023       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16024         field_size_in_bits = tree_to_double_int (field_size_tree);
16025       else
16026         field_size_in_bits = type_size_in_bits;
16027
16028       decl_align_in_bits = simple_decl_align_in_bits (decl);
16029
16030       /* The GCC front-end doesn't make any attempt to keep track of the
16031          starting bit offset (relative to the start of the containing
16032          structure type) of the hypothetical "containing object" for a
16033          bit-field.  Thus, when computing the byte offset value for the
16034          start of the "containing object" of a bit-field, we must deduce
16035          this information on our own. This can be rather tricky to do in
16036          some cases.  For example, handling the following structure type
16037          definition when compiling for an i386/i486 target (which only
16038          aligns long long's to 32-bit boundaries) can be very tricky:
16039
16040          struct S { int field1; long long field2:31; };
16041
16042          Fortunately, there is a simple rule-of-thumb which can be used
16043          in such cases.  When compiling for an i386/i486, GCC will
16044          allocate 8 bytes for the structure shown above.  It decides to
16045          do this based upon one simple rule for bit-field allocation.
16046          GCC allocates each "containing object" for each bit-field at
16047          the first (i.e. lowest addressed) legitimate alignment boundary
16048          (based upon the required minimum alignment for the declared
16049          type of the field) which it can possibly use, subject to the
16050          condition that there is still enough available space remaining
16051          in the containing object (when allocated at the selected point)
16052          to fully accommodate all of the bits of the bit-field itself.
16053
16054          This simple rule makes it obvious why GCC allocates 8 bytes for
16055          each object of the structure type shown above.  When looking
16056          for a place to allocate the "containing object" for `field2',
16057          the compiler simply tries to allocate a 64-bit "containing
16058          object" at each successive 32-bit boundary (starting at zero)
16059          until it finds a place to allocate that 64- bit field such that
16060          at least 31 contiguous (and previously unallocated) bits remain
16061          within that selected 64 bit field.  (As it turns out, for the
16062          example above, the compiler finds it is OK to allocate the
16063          "containing object" 64-bit field at bit-offset zero within the
16064          structure type.)
16065
16066          Here we attempt to work backwards from the limited set of facts
16067          we're given, and we try to deduce from those facts, where GCC
16068          must have believed that the containing object started (within
16069          the structure type). The value we deduce is then used (by the
16070          callers of this routine) to generate DW_AT_location and
16071          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16072          the case of DW_AT_location, regular fields as well).  */
16073
16074       /* Figure out the bit-distance from the start of the structure to
16075          the "deepest" bit of the bit-field.  */
16076       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16077
16078       /* This is the tricky part.  Use some fancy footwork to deduce
16079          where the lowest addressed bit of the containing object must
16080          be.  */
16081       object_offset_in_bits
16082         = double_int_sub (deepest_bitpos, type_size_in_bits);
16083
16084       /* Round up to type_align by default.  This works best for
16085          bitfields.  */
16086       object_offset_in_bits
16087         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16088
16089       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16090         {
16091           object_offset_in_bits
16092             = double_int_sub (deepest_bitpos, type_size_in_bits);
16093
16094           /* Round up to decl_align instead.  */
16095           object_offset_in_bits
16096             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16097         }
16098     }
16099   else
16100 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16101     object_offset_in_bits = bitpos_int;
16102
16103   object_offset_in_bytes
16104     = double_int_div (object_offset_in_bits,
16105                       uhwi_to_double_int (BITS_PER_UNIT), true,
16106                       TRUNC_DIV_EXPR);
16107   return double_int_to_shwi (object_offset_in_bytes);
16108 }
16109 \f
16110 /* The following routines define various Dwarf attributes and any data
16111    associated with them.  */
16112
16113 /* Add a location description attribute value to a DIE.
16114
16115    This emits location attributes suitable for whole variables and
16116    whole parameters.  Note that the location attributes for struct fields are
16117    generated by the routine `data_member_location_attribute' below.  */
16118
16119 static inline void
16120 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16121                              dw_loc_list_ref descr)
16122 {
16123   if (descr == 0)
16124     return;
16125   if (single_element_loc_list_p (descr))
16126     add_AT_loc (die, attr_kind, descr->expr);
16127   else
16128     add_AT_loc_list (die, attr_kind, descr);
16129 }
16130
16131 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16132
16133 static void
16134 add_accessibility_attribute (dw_die_ref die, tree decl)
16135 {
16136   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16137      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16138      the default has always been DW_ACCESS_public.  */
16139   if (TREE_PROTECTED (decl))
16140     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16141   else if (TREE_PRIVATE (decl))
16142     {
16143       if (dwarf_version == 2
16144           || die->die_parent == NULL
16145           || die->die_parent->die_tag != DW_TAG_class_type)
16146         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16147     }
16148   else if (dwarf_version > 2
16149            && die->die_parent
16150            && die->die_parent->die_tag == DW_TAG_class_type)
16151     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16152 }
16153
16154 /* Attach the specialized form of location attribute used for data members of
16155    struct and union types.  In the special case of a FIELD_DECL node which
16156    represents a bit-field, the "offset" part of this special location
16157    descriptor must indicate the distance in bytes from the lowest-addressed
16158    byte of the containing struct or union type to the lowest-addressed byte of
16159    the "containing object" for the bit-field.  (See the `field_byte_offset'
16160    function above).
16161
16162    For any given bit-field, the "containing object" is a hypothetical object
16163    (of some integral or enum type) within which the given bit-field lives.  The
16164    type of this hypothetical "containing object" is always the same as the
16165    declared type of the individual bit-field itself (for GCC anyway... the
16166    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16167    bytes) of the hypothetical "containing object" which will be given in the
16168    DW_AT_byte_size attribute for this bit-field.  (See the
16169    `byte_size_attribute' function below.)  It is also used when calculating the
16170    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16171    function below.)  */
16172
16173 static void
16174 add_data_member_location_attribute (dw_die_ref die, tree decl)
16175 {
16176   HOST_WIDE_INT offset;
16177   dw_loc_descr_ref loc_descr = 0;
16178
16179   if (TREE_CODE (decl) == TREE_BINFO)
16180     {
16181       /* We're working on the TAG_inheritance for a base class.  */
16182       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16183         {
16184           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16185              aren't at a fixed offset from all (sub)objects of the same
16186              type.  We need to extract the appropriate offset from our
16187              vtable.  The following dwarf expression means
16188
16189                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16190
16191              This is specific to the V3 ABI, of course.  */
16192
16193           dw_loc_descr_ref tmp;
16194
16195           /* Make a copy of the object address.  */
16196           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16197           add_loc_descr (&loc_descr, tmp);
16198
16199           /* Extract the vtable address.  */
16200           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16201           add_loc_descr (&loc_descr, tmp);
16202
16203           /* Calculate the address of the offset.  */
16204           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16205           gcc_assert (offset < 0);
16206
16207           tmp = int_loc_descriptor (-offset);
16208           add_loc_descr (&loc_descr, tmp);
16209           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16210           add_loc_descr (&loc_descr, tmp);
16211
16212           /* Extract the offset.  */
16213           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16214           add_loc_descr (&loc_descr, tmp);
16215
16216           /* Add it to the object address.  */
16217           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16218           add_loc_descr (&loc_descr, tmp);
16219         }
16220       else
16221         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16222     }
16223   else
16224     offset = field_byte_offset (decl);
16225
16226   if (! loc_descr)
16227     {
16228       if (dwarf_version > 2)
16229         {
16230           /* Don't need to output a location expression, just the constant. */
16231           if (offset < 0)
16232             add_AT_int (die, DW_AT_data_member_location, offset);
16233           else
16234             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16235           return;
16236         }
16237       else
16238         {
16239           enum dwarf_location_atom op;
16240
16241           /* The DWARF2 standard says that we should assume that the structure
16242              address is already on the stack, so we can specify a structure
16243              field address by using DW_OP_plus_uconst.  */
16244
16245 #ifdef MIPS_DEBUGGING_INFO
16246           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16247              operator correctly.  It works only if we leave the offset on the
16248              stack.  */
16249           op = DW_OP_constu;
16250 #else
16251           op = DW_OP_plus_uconst;
16252 #endif
16253
16254           loc_descr = new_loc_descr (op, offset, 0);
16255         }
16256     }
16257
16258   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16259 }
16260
16261 /* Writes integer values to dw_vec_const array.  */
16262
16263 static void
16264 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16265 {
16266   while (size != 0)
16267     {
16268       *dest++ = val & 0xff;
16269       val >>= 8;
16270       --size;
16271     }
16272 }
16273
16274 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16275
16276 static HOST_WIDE_INT
16277 extract_int (const unsigned char *src, unsigned int size)
16278 {
16279   HOST_WIDE_INT val = 0;
16280
16281   src += size;
16282   while (size != 0)
16283     {
16284       val <<= 8;
16285       val |= *--src & 0xff;
16286       --size;
16287     }
16288   return val;
16289 }
16290
16291 /* Writes double_int values to dw_vec_const array.  */
16292
16293 static void
16294 insert_double (double_int val, unsigned char *dest)
16295 {
16296   unsigned char *p0 = dest;
16297   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16298
16299   if (WORDS_BIG_ENDIAN)
16300     {
16301       p0 = p1;
16302       p1 = dest;
16303     }
16304
16305   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16306   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16307 }
16308
16309 /* Writes floating point values to dw_vec_const array.  */
16310
16311 static void
16312 insert_float (const_rtx rtl, unsigned char *array)
16313 {
16314   REAL_VALUE_TYPE rv;
16315   long val[4];
16316   int i;
16317
16318   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16319   real_to_target (val, &rv, GET_MODE (rtl));
16320
16321   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16322   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16323     {
16324       insert_int (val[i], 4, array);
16325       array += 4;
16326     }
16327 }
16328
16329 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16330    does not have a "location" either in memory or in a register.  These
16331    things can arise in GNU C when a constant is passed as an actual parameter
16332    to an inlined function.  They can also arise in C++ where declared
16333    constants do not necessarily get memory "homes".  */
16334
16335 static bool
16336 add_const_value_attribute (dw_die_ref die, rtx rtl)
16337 {
16338   switch (GET_CODE (rtl))
16339     {
16340     case CONST_INT:
16341       {
16342         HOST_WIDE_INT val = INTVAL (rtl);
16343
16344         if (val < 0)
16345           add_AT_int (die, DW_AT_const_value, val);
16346         else
16347           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16348       }
16349       return true;
16350
16351     case CONST_DOUBLE:
16352       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16353          floating-point constant.  A CONST_DOUBLE is used whenever the
16354          constant requires more than one word in order to be adequately
16355          represented.  */
16356       {
16357         enum machine_mode mode = GET_MODE (rtl);
16358
16359         if (SCALAR_FLOAT_MODE_P (mode))
16360           {
16361             unsigned int length = GET_MODE_SIZE (mode);
16362             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16363
16364             insert_float (rtl, array);
16365             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16366           }
16367         else
16368           add_AT_double (die, DW_AT_const_value,
16369                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16370       }
16371       return true;
16372
16373     case CONST_VECTOR:
16374       {
16375         enum machine_mode mode = GET_MODE (rtl);
16376         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16377         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16378         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16379           (length * elt_size);
16380         unsigned int i;
16381         unsigned char *p;
16382
16383         switch (GET_MODE_CLASS (mode))
16384           {
16385           case MODE_VECTOR_INT:
16386             for (i = 0, p = array; i < length; i++, p += elt_size)
16387               {
16388                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16389                 double_int val = rtx_to_double_int (elt);
16390
16391                 if (elt_size <= sizeof (HOST_WIDE_INT))
16392                   insert_int (double_int_to_shwi (val), elt_size, p);
16393                 else
16394                   {
16395                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16396                     insert_double (val, p);
16397                   }
16398               }
16399             break;
16400
16401           case MODE_VECTOR_FLOAT:
16402             for (i = 0, p = array; i < length; i++, p += elt_size)
16403               {
16404                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16405                 insert_float (elt, p);
16406               }
16407             break;
16408
16409           default:
16410             gcc_unreachable ();
16411           }
16412
16413         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16414       }
16415       return true;
16416
16417     case CONST_STRING:
16418       if (dwarf_version >= 4 || !dwarf_strict)
16419         {
16420           dw_loc_descr_ref loc_result;
16421           resolve_one_addr (&rtl, NULL);
16422         rtl_addr:
16423           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16424           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16425           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16426           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16427           add_AT_loc (die, DW_AT_location, loc_result);
16428           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16429           return true;
16430         }
16431       return false;
16432
16433     case CONST:
16434       if (CONSTANT_P (XEXP (rtl, 0)))
16435         return add_const_value_attribute (die, XEXP (rtl, 0));
16436       /* FALLTHROUGH */
16437     case SYMBOL_REF:
16438       if (!const_ok_for_output (rtl))
16439         return false;
16440     case LABEL_REF:
16441       if (dwarf_version >= 4 || !dwarf_strict)
16442         goto rtl_addr;
16443       return false;
16444
16445     case PLUS:
16446       /* In cases where an inlined instance of an inline function is passed
16447          the address of an `auto' variable (which is local to the caller) we
16448          can get a situation where the DECL_RTL of the artificial local
16449          variable (for the inlining) which acts as a stand-in for the
16450          corresponding formal parameter (of the inline function) will look
16451          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16452          exactly a compile-time constant expression, but it isn't the address
16453          of the (artificial) local variable either.  Rather, it represents the
16454          *value* which the artificial local variable always has during its
16455          lifetime.  We currently have no way to represent such quasi-constant
16456          values in Dwarf, so for now we just punt and generate nothing.  */
16457       return false;
16458
16459     case HIGH:
16460     case CONST_FIXED:
16461       return false;
16462
16463     case MEM:
16464       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16465           && MEM_READONLY_P (rtl)
16466           && GET_MODE (rtl) == BLKmode)
16467         {
16468           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16469           return true;
16470         }
16471       return false;
16472
16473     default:
16474       /* No other kinds of rtx should be possible here.  */
16475       gcc_unreachable ();
16476     }
16477   return false;
16478 }
16479
16480 /* Determine whether the evaluation of EXPR references any variables
16481    or functions which aren't otherwise used (and therefore may not be
16482    output).  */
16483 static tree
16484 reference_to_unused (tree * tp, int * walk_subtrees,
16485                      void * data ATTRIBUTE_UNUSED)
16486 {
16487   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16488     *walk_subtrees = 0;
16489
16490   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16491       && ! TREE_ASM_WRITTEN (*tp))
16492     return *tp;
16493   /* ???  The C++ FE emits debug information for using decls, so
16494      putting gcc_unreachable here falls over.  See PR31899.  For now
16495      be conservative.  */
16496   else if (!cgraph_global_info_ready
16497            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16498     return *tp;
16499   else if (TREE_CODE (*tp) == VAR_DECL)
16500     {
16501       struct varpool_node *node = varpool_get_node (*tp);
16502       if (!node || !node->needed)
16503         return *tp;
16504     }
16505   else if (TREE_CODE (*tp) == FUNCTION_DECL
16506            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16507     {
16508       /* The call graph machinery must have finished analyzing,
16509          optimizing and gimplifying the CU by now.
16510          So if *TP has no call graph node associated
16511          to it, it means *TP will not be emitted.  */
16512       if (!cgraph_get_node (*tp))
16513         return *tp;
16514     }
16515   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16516     return *tp;
16517
16518   return NULL_TREE;
16519 }
16520
16521 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16522    for use in a later add_const_value_attribute call.  */
16523
16524 static rtx
16525 rtl_for_decl_init (tree init, tree type)
16526 {
16527   rtx rtl = NULL_RTX;
16528
16529   /* If a variable is initialized with a string constant without embedded
16530      zeros, build CONST_STRING.  */
16531   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16532     {
16533       tree enttype = TREE_TYPE (type);
16534       tree domain = TYPE_DOMAIN (type);
16535       enum machine_mode mode = TYPE_MODE (enttype);
16536
16537       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16538           && domain
16539           && integer_zerop (TYPE_MIN_VALUE (domain))
16540           && compare_tree_int (TYPE_MAX_VALUE (domain),
16541                                TREE_STRING_LENGTH (init) - 1) == 0
16542           && ((size_t) TREE_STRING_LENGTH (init)
16543               == strlen (TREE_STRING_POINTER (init)) + 1))
16544         {
16545           rtl = gen_rtx_CONST_STRING (VOIDmode,
16546                                       ggc_strdup (TREE_STRING_POINTER (init)));
16547           rtl = gen_rtx_MEM (BLKmode, rtl);
16548           MEM_READONLY_P (rtl) = 1;
16549         }
16550     }
16551   /* Other aggregates, and complex values, could be represented using
16552      CONCAT: FIXME!  */
16553   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16554     ;
16555   /* Vectors only work if their mode is supported by the target.
16556      FIXME: generic vectors ought to work too.  */
16557   else if (TREE_CODE (type) == VECTOR_TYPE
16558            && !VECTOR_MODE_P (TYPE_MODE (type)))
16559     ;
16560   /* If the initializer is something that we know will expand into an
16561      immediate RTL constant, expand it now.  We must be careful not to
16562      reference variables which won't be output.  */
16563   else if (initializer_constant_valid_p (init, type)
16564            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16565     {
16566       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16567          possible.  */
16568       if (TREE_CODE (type) == VECTOR_TYPE)
16569         switch (TREE_CODE (init))
16570           {
16571           case VECTOR_CST:
16572             break;
16573           case CONSTRUCTOR:
16574             if (TREE_CONSTANT (init))
16575               {
16576                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16577                 bool constant_p = true;
16578                 tree value;
16579                 unsigned HOST_WIDE_INT ix;
16580
16581                 /* Even when ctor is constant, it might contain non-*_CST
16582                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16583                    belong into VECTOR_CST nodes.  */
16584                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16585                   if (!CONSTANT_CLASS_P (value))
16586                     {
16587                       constant_p = false;
16588                       break;
16589                     }
16590
16591                 if (constant_p)
16592                   {
16593                     init = build_vector_from_ctor (type, elts);
16594                     break;
16595                   }
16596               }
16597             /* FALLTHRU */
16598
16599           default:
16600             return NULL;
16601           }
16602
16603       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16604
16605       /* If expand_expr returns a MEM, it wasn't immediate.  */
16606       gcc_assert (!rtl || !MEM_P (rtl));
16607     }
16608
16609   return rtl;
16610 }
16611
16612 /* Generate RTL for the variable DECL to represent its location.  */
16613
16614 static rtx
16615 rtl_for_decl_location (tree decl)
16616 {
16617   rtx rtl;
16618
16619   /* Here we have to decide where we are going to say the parameter "lives"
16620      (as far as the debugger is concerned).  We only have a couple of
16621      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16622
16623      DECL_RTL normally indicates where the parameter lives during most of the
16624      activation of the function.  If optimization is enabled however, this
16625      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16626      that the parameter doesn't really live anywhere (as far as the code
16627      generation parts of GCC are concerned) during most of the function's
16628      activation.  That will happen (for example) if the parameter is never
16629      referenced within the function.
16630
16631      We could just generate a location descriptor here for all non-NULL
16632      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16633      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16634      where DECL_RTL is NULL or is a pseudo-reg.
16635
16636      Note however that we can only get away with using DECL_INCOMING_RTL as
16637      a backup substitute for DECL_RTL in certain limited cases.  In cases
16638      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16639      we can be sure that the parameter was passed using the same type as it is
16640      declared to have within the function, and that its DECL_INCOMING_RTL
16641      points us to a place where a value of that type is passed.
16642
16643      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16644      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16645      because in these cases DECL_INCOMING_RTL points us to a value of some
16646      type which is *different* from the type of the parameter itself.  Thus,
16647      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16648      such cases, the debugger would end up (for example) trying to fetch a
16649      `float' from a place which actually contains the first part of a
16650      `double'.  That would lead to really incorrect and confusing
16651      output at debug-time.
16652
16653      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16654      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16655      are a couple of exceptions however.  On little-endian machines we can
16656      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16657      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16658      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16659      when (on a little-endian machine) a non-prototyped function has a
16660      parameter declared to be of type `short' or `char'.  In such cases,
16661      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16662      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16663      passed `int' value.  If the debugger then uses that address to fetch
16664      a `short' or a `char' (on a little-endian machine) the result will be
16665      the correct data, so we allow for such exceptional cases below.
16666
16667      Note that our goal here is to describe the place where the given formal
16668      parameter lives during most of the function's activation (i.e. between the
16669      end of the prologue and the start of the epilogue).  We'll do that as best
16670      as we can. Note however that if the given formal parameter is modified
16671      sometime during the execution of the function, then a stack backtrace (at
16672      debug-time) will show the function as having been called with the *new*
16673      value rather than the value which was originally passed in.  This happens
16674      rarely enough that it is not a major problem, but it *is* a problem, and
16675      I'd like to fix it.
16676
16677      A future version of dwarf2out.c may generate two additional attributes for
16678      any given DW_TAG_formal_parameter DIE which will describe the "passed
16679      type" and the "passed location" for the given formal parameter in addition
16680      to the attributes we now generate to indicate the "declared type" and the
16681      "active location" for each parameter.  This additional set of attributes
16682      could be used by debuggers for stack backtraces. Separately, note that
16683      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16684      This happens (for example) for inlined-instances of inline function formal
16685      parameters which are never referenced.  This really shouldn't be
16686      happening.  All PARM_DECL nodes should get valid non-NULL
16687      DECL_INCOMING_RTL values.  FIXME.  */
16688
16689   /* Use DECL_RTL as the "location" unless we find something better.  */
16690   rtl = DECL_RTL_IF_SET (decl);
16691
16692   /* When generating abstract instances, ignore everything except
16693      constants, symbols living in memory, and symbols living in
16694      fixed registers.  */
16695   if (! reload_completed)
16696     {
16697       if (rtl
16698           && (CONSTANT_P (rtl)
16699               || (MEM_P (rtl)
16700                   && CONSTANT_P (XEXP (rtl, 0)))
16701               || (REG_P (rtl)
16702                   && TREE_CODE (decl) == VAR_DECL
16703                   && TREE_STATIC (decl))))
16704         {
16705           rtl = targetm.delegitimize_address (rtl);
16706           return rtl;
16707         }
16708       rtl = NULL_RTX;
16709     }
16710   else if (TREE_CODE (decl) == PARM_DECL)
16711     {
16712       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16713         {
16714           tree declared_type = TREE_TYPE (decl);
16715           tree passed_type = DECL_ARG_TYPE (decl);
16716           enum machine_mode dmode = TYPE_MODE (declared_type);
16717           enum machine_mode pmode = TYPE_MODE (passed_type);
16718
16719           /* This decl represents a formal parameter which was optimized out.
16720              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16721              all cases where (rtl == NULL_RTX) just below.  */
16722           if (dmode == pmode)
16723             rtl = DECL_INCOMING_RTL (decl);
16724           else if (SCALAR_INT_MODE_P (dmode)
16725                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16726                    && DECL_INCOMING_RTL (decl))
16727             {
16728               rtx inc = DECL_INCOMING_RTL (decl);
16729               if (REG_P (inc))
16730                 rtl = inc;
16731               else if (MEM_P (inc))
16732                 {
16733                   if (BYTES_BIG_ENDIAN)
16734                     rtl = adjust_address_nv (inc, dmode,
16735                                              GET_MODE_SIZE (pmode)
16736                                              - GET_MODE_SIZE (dmode));
16737                   else
16738                     rtl = inc;
16739                 }
16740             }
16741         }
16742
16743       /* If the parm was passed in registers, but lives on the stack, then
16744          make a big endian correction if the mode of the type of the
16745          parameter is not the same as the mode of the rtl.  */
16746       /* ??? This is the same series of checks that are made in dbxout.c before
16747          we reach the big endian correction code there.  It isn't clear if all
16748          of these checks are necessary here, but keeping them all is the safe
16749          thing to do.  */
16750       else if (MEM_P (rtl)
16751                && XEXP (rtl, 0) != const0_rtx
16752                && ! CONSTANT_P (XEXP (rtl, 0))
16753                /* Not passed in memory.  */
16754                && !MEM_P (DECL_INCOMING_RTL (decl))
16755                /* Not passed by invisible reference.  */
16756                && (!REG_P (XEXP (rtl, 0))
16757                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16758                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16759 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16760                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16761 #endif
16762                      )
16763                /* Big endian correction check.  */
16764                && BYTES_BIG_ENDIAN
16765                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16766                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16767                    < UNITS_PER_WORD))
16768         {
16769           int offset = (UNITS_PER_WORD
16770                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16771
16772           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16773                              plus_constant (XEXP (rtl, 0), offset));
16774         }
16775     }
16776   else if (TREE_CODE (decl) == VAR_DECL
16777            && rtl
16778            && MEM_P (rtl)
16779            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16780            && BYTES_BIG_ENDIAN)
16781     {
16782       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16783       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16784
16785       /* If a variable is declared "register" yet is smaller than
16786          a register, then if we store the variable to memory, it
16787          looks like we're storing a register-sized value, when in
16788          fact we are not.  We need to adjust the offset of the
16789          storage location to reflect the actual value's bytes,
16790          else gdb will not be able to display it.  */
16791       if (rsize > dsize)
16792         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16793                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16794     }
16795
16796   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16797      and will have been substituted directly into all expressions that use it.
16798      C does not have such a concept, but C++ and other languages do.  */
16799   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16800     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16801
16802   if (rtl)
16803     rtl = targetm.delegitimize_address (rtl);
16804
16805   /* If we don't look past the constant pool, we risk emitting a
16806      reference to a constant pool entry that isn't referenced from
16807      code, and thus is not emitted.  */
16808   if (rtl)
16809     rtl = avoid_constant_pool_reference (rtl);
16810
16811   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16812      in the current CU, resolve_addr will remove the expression referencing
16813      it.  */
16814   if (rtl == NULL_RTX
16815       && TREE_CODE (decl) == VAR_DECL
16816       && !DECL_EXTERNAL (decl)
16817       && TREE_STATIC (decl)
16818       && DECL_NAME (decl)
16819       && !DECL_HARD_REGISTER (decl)
16820       && DECL_MODE (decl) != VOIDmode)
16821     {
16822       rtl = make_decl_rtl_for_debug (decl);
16823       if (!MEM_P (rtl)
16824           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16825           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16826         rtl = NULL_RTX;
16827     }
16828
16829   return rtl;
16830 }
16831
16832 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16833    returned.  If so, the decl for the COMMON block is returned, and the
16834    value is the offset into the common block for the symbol.  */
16835
16836 static tree
16837 fortran_common (tree decl, HOST_WIDE_INT *value)
16838 {
16839   tree val_expr, cvar;
16840   enum machine_mode mode;
16841   HOST_WIDE_INT bitsize, bitpos;
16842   tree offset;
16843   int volatilep = 0, unsignedp = 0;
16844
16845   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16846      it does not have a value (the offset into the common area), or if it
16847      is thread local (as opposed to global) then it isn't common, and shouldn't
16848      be handled as such.  */
16849   if (TREE_CODE (decl) != VAR_DECL
16850       || !TREE_STATIC (decl)
16851       || !DECL_HAS_VALUE_EXPR_P (decl)
16852       || !is_fortran ())
16853     return NULL_TREE;
16854
16855   val_expr = DECL_VALUE_EXPR (decl);
16856   if (TREE_CODE (val_expr) != COMPONENT_REF)
16857     return NULL_TREE;
16858
16859   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16860                               &mode, &unsignedp, &volatilep, true);
16861
16862   if (cvar == NULL_TREE
16863       || TREE_CODE (cvar) != VAR_DECL
16864       || DECL_ARTIFICIAL (cvar)
16865       || !TREE_PUBLIC (cvar))
16866     return NULL_TREE;
16867
16868   *value = 0;
16869   if (offset != NULL)
16870     {
16871       if (!host_integerp (offset, 0))
16872         return NULL_TREE;
16873       *value = tree_low_cst (offset, 0);
16874     }
16875   if (bitpos != 0)
16876     *value += bitpos / BITS_PER_UNIT;
16877
16878   return cvar;
16879 }
16880
16881 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16882    data attribute for a variable or a parameter.  We generate the
16883    DW_AT_const_value attribute only in those cases where the given variable
16884    or parameter does not have a true "location" either in memory or in a
16885    register.  This can happen (for example) when a constant is passed as an
16886    actual argument in a call to an inline function.  (It's possible that
16887    these things can crop up in other ways also.)  Note that one type of
16888    constant value which can be passed into an inlined function is a constant
16889    pointer.  This can happen for example if an actual argument in an inlined
16890    function call evaluates to a compile-time constant address.  */
16891
16892 static bool
16893 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16894                                        enum dwarf_attribute attr)
16895 {
16896   rtx rtl;
16897   dw_loc_list_ref list;
16898   var_loc_list *loc_list;
16899
16900   if (TREE_CODE (decl) == ERROR_MARK)
16901     return false;
16902
16903   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16904               || TREE_CODE (decl) == RESULT_DECL);
16905
16906   /* Try to get some constant RTL for this decl, and use that as the value of
16907      the location.  */
16908
16909   rtl = rtl_for_decl_location (decl);
16910   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16911       && add_const_value_attribute (die, rtl))
16912     return true;
16913
16914   /* See if we have single element location list that is equivalent to
16915      a constant value.  That way we are better to use add_const_value_attribute
16916      rather than expanding constant value equivalent.  */
16917   loc_list = lookup_decl_loc (decl);
16918   if (loc_list
16919       && loc_list->first
16920       && loc_list->first->next == NULL
16921       && NOTE_P (loc_list->first->loc)
16922       && NOTE_VAR_LOCATION (loc_list->first->loc)
16923       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16924     {
16925       struct var_loc_node *node;
16926
16927       node = loc_list->first;
16928       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16929       if (GET_CODE (rtl) == EXPR_LIST)
16930         rtl = XEXP (rtl, 0);
16931       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16932           && add_const_value_attribute (die, rtl))
16933          return true;
16934     }
16935   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16936   if (list)
16937     {
16938       add_AT_location_description (die, attr, list);
16939       return true;
16940     }
16941   /* None of that worked, so it must not really have a location;
16942      try adding a constant value attribute from the DECL_INITIAL.  */
16943   return tree_add_const_value_attribute_for_decl (die, decl);
16944 }
16945
16946 /* Add VARIABLE and DIE into deferred locations list.  */
16947
16948 static void
16949 defer_location (tree variable, dw_die_ref die)
16950 {
16951   deferred_locations entry;
16952   entry.variable = variable;
16953   entry.die = die;
16954   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16955 }
16956
16957 /* Helper function for tree_add_const_value_attribute.  Natively encode
16958    initializer INIT into an array.  Return true if successful.  */
16959
16960 static bool
16961 native_encode_initializer (tree init, unsigned char *array, int size)
16962 {
16963   tree type;
16964
16965   if (init == NULL_TREE)
16966     return false;
16967
16968   STRIP_NOPS (init);
16969   switch (TREE_CODE (init))
16970     {
16971     case STRING_CST:
16972       type = TREE_TYPE (init);
16973       if (TREE_CODE (type) == ARRAY_TYPE)
16974         {
16975           tree enttype = TREE_TYPE (type);
16976           enum machine_mode mode = TYPE_MODE (enttype);
16977
16978           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16979             return false;
16980           if (int_size_in_bytes (type) != size)
16981             return false;
16982           if (size > TREE_STRING_LENGTH (init))
16983             {
16984               memcpy (array, TREE_STRING_POINTER (init),
16985                       TREE_STRING_LENGTH (init));
16986               memset (array + TREE_STRING_LENGTH (init),
16987                       '\0', size - TREE_STRING_LENGTH (init));
16988             }
16989           else
16990             memcpy (array, TREE_STRING_POINTER (init), size);
16991           return true;
16992         }
16993       return false;
16994     case CONSTRUCTOR:
16995       type = TREE_TYPE (init);
16996       if (int_size_in_bytes (type) != size)
16997         return false;
16998       if (TREE_CODE (type) == ARRAY_TYPE)
16999         {
17000           HOST_WIDE_INT min_index;
17001           unsigned HOST_WIDE_INT cnt;
17002           int curpos = 0, fieldsize;
17003           constructor_elt *ce;
17004
17005           if (TYPE_DOMAIN (type) == NULL_TREE
17006               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17007             return false;
17008
17009           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17010           if (fieldsize <= 0)
17011             return false;
17012
17013           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17014           memset (array, '\0', size);
17015           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17016             {
17017               tree val = ce->value;
17018               tree index = ce->index;
17019               int pos = curpos;
17020               if (index && TREE_CODE (index) == RANGE_EXPR)
17021                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17022                       * fieldsize;
17023               else if (index)
17024                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17025
17026               if (val)
17027                 {
17028                   STRIP_NOPS (val);
17029                   if (!native_encode_initializer (val, array + pos, fieldsize))
17030                     return false;
17031                 }
17032               curpos = pos + fieldsize;
17033               if (index && TREE_CODE (index) == RANGE_EXPR)
17034                 {
17035                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17036                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17037                   while (count > 0)
17038                     {
17039                       if (val)
17040                         memcpy (array + curpos, array + pos, fieldsize);
17041                       curpos += fieldsize;
17042                     }
17043                 }
17044               gcc_assert (curpos <= size);
17045             }
17046           return true;
17047         }
17048       else if (TREE_CODE (type) == RECORD_TYPE
17049                || TREE_CODE (type) == UNION_TYPE)
17050         {
17051           tree field = NULL_TREE;
17052           unsigned HOST_WIDE_INT cnt;
17053           constructor_elt *ce;
17054
17055           if (int_size_in_bytes (type) != size)
17056             return false;
17057
17058           if (TREE_CODE (type) == RECORD_TYPE)
17059             field = TYPE_FIELDS (type);
17060
17061           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17062             {
17063               tree val = ce->value;
17064               int pos, fieldsize;
17065
17066               if (ce->index != 0)
17067                 field = ce->index;
17068
17069               if (val)
17070                 STRIP_NOPS (val);
17071
17072               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17073                 return false;
17074
17075               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17076                   && TYPE_DOMAIN (TREE_TYPE (field))
17077                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17078                 return false;
17079               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17080                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17081                 return false;
17082               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17083               pos = int_byte_position (field);
17084               gcc_assert (pos + fieldsize <= size);
17085               if (val
17086                   && !native_encode_initializer (val, array + pos, fieldsize))
17087                 return false;
17088             }
17089           return true;
17090         }
17091       return false;
17092     case VIEW_CONVERT_EXPR:
17093     case NON_LVALUE_EXPR:
17094       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17095     default:
17096       return native_encode_expr (init, array, size) == size;
17097     }
17098 }
17099
17100 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17101    attribute is the const value T.  */
17102
17103 static bool
17104 tree_add_const_value_attribute (dw_die_ref die, tree t)
17105 {
17106   tree init;
17107   tree type = TREE_TYPE (t);
17108   rtx rtl;
17109
17110   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17111     return false;
17112
17113   init = t;
17114   gcc_assert (!DECL_P (init));
17115
17116   rtl = rtl_for_decl_init (init, type);
17117   if (rtl)
17118     return add_const_value_attribute (die, rtl);
17119   /* If the host and target are sane, try harder.  */
17120   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17121            && initializer_constant_valid_p (init, type))
17122     {
17123       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17124       if (size > 0 && (int) size == size)
17125         {
17126           unsigned char *array = (unsigned char *)
17127             ggc_alloc_cleared_atomic (size);
17128
17129           if (native_encode_initializer (init, array, size))
17130             {
17131               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17132               return true;
17133             }
17134         }
17135     }
17136   return false;
17137 }
17138
17139 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17140    attribute is the const value of T, where T is an integral constant
17141    variable with static storage duration
17142    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17143
17144 static bool
17145 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17146 {
17147
17148   if (!decl
17149       || (TREE_CODE (decl) != VAR_DECL
17150           && TREE_CODE (decl) != CONST_DECL))
17151     return false;
17152
17153     if (TREE_READONLY (decl)
17154         && ! TREE_THIS_VOLATILE (decl)
17155         && DECL_INITIAL (decl))
17156       /* OK */;
17157     else
17158       return false;
17159
17160   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17161   if (get_AT (var_die, DW_AT_const_value))
17162     return false;
17163
17164   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17165 }
17166
17167 /* Convert the CFI instructions for the current function into a
17168    location list.  This is used for DW_AT_frame_base when we targeting
17169    a dwarf2 consumer that does not support the dwarf3
17170    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17171    expressions.  */
17172
17173 static dw_loc_list_ref
17174 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17175 {
17176   dw_fde_ref fde;
17177   dw_loc_list_ref list, *list_tail;
17178   dw_cfi_ref cfi;
17179   dw_cfa_location last_cfa, next_cfa;
17180   const char *start_label, *last_label, *section;
17181   dw_cfa_location remember;
17182
17183   fde = current_fde ();
17184   gcc_assert (fde != NULL);
17185
17186   section = secname_for_decl (current_function_decl);
17187   list_tail = &list;
17188   list = NULL;
17189
17190   memset (&next_cfa, 0, sizeof (next_cfa));
17191   next_cfa.reg = INVALID_REGNUM;
17192   remember = next_cfa;
17193
17194   start_label = fde->dw_fde_begin;
17195
17196   /* ??? Bald assumption that the CIE opcode list does not contain
17197      advance opcodes.  */
17198   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17199     lookup_cfa_1 (cfi, &next_cfa, &remember);
17200
17201   last_cfa = next_cfa;
17202   last_label = start_label;
17203
17204   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17205     switch (cfi->dw_cfi_opc)
17206       {
17207       case DW_CFA_set_loc:
17208       case DW_CFA_advance_loc1:
17209       case DW_CFA_advance_loc2:
17210       case DW_CFA_advance_loc4:
17211         if (!cfa_equal_p (&last_cfa, &next_cfa))
17212           {
17213             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17214                                        start_label, last_label, section);
17215
17216             list_tail = &(*list_tail)->dw_loc_next;
17217             last_cfa = next_cfa;
17218             start_label = last_label;
17219           }
17220         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17221         break;
17222
17223       case DW_CFA_advance_loc:
17224         /* The encoding is complex enough that we should never emit this.  */
17225         gcc_unreachable ();
17226
17227       default:
17228         lookup_cfa_1 (cfi, &next_cfa, &remember);
17229         break;
17230       }
17231
17232   if (!cfa_equal_p (&last_cfa, &next_cfa))
17233     {
17234       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17235                                  start_label, last_label, section);
17236       list_tail = &(*list_tail)->dw_loc_next;
17237       start_label = last_label;
17238     }
17239
17240   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17241                              start_label, fde->dw_fde_end, section);
17242
17243   if (list && list->dw_loc_next)
17244     gen_llsym (list);
17245
17246   return list;
17247 }
17248
17249 /* Compute a displacement from the "steady-state frame pointer" to the
17250    frame base (often the same as the CFA), and store it in
17251    frame_pointer_fb_offset.  OFFSET is added to the displacement
17252    before the latter is negated.  */
17253
17254 static void
17255 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17256 {
17257   rtx reg, elim;
17258
17259 #ifdef FRAME_POINTER_CFA_OFFSET
17260   reg = frame_pointer_rtx;
17261   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17262 #else
17263   reg = arg_pointer_rtx;
17264   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17265 #endif
17266
17267   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17268   if (GET_CODE (elim) == PLUS)
17269     {
17270       offset += INTVAL (XEXP (elim, 1));
17271       elim = XEXP (elim, 0);
17272     }
17273
17274   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17275                && (elim == hard_frame_pointer_rtx
17276                    || elim == stack_pointer_rtx))
17277               || elim == (frame_pointer_needed
17278                           ? hard_frame_pointer_rtx
17279                           : stack_pointer_rtx));
17280
17281   frame_pointer_fb_offset = -offset;
17282 }
17283
17284 /* Generate a DW_AT_name attribute given some string value to be included as
17285    the value of the attribute.  */
17286
17287 static void
17288 add_name_attribute (dw_die_ref die, const char *name_string)
17289 {
17290   if (name_string != NULL && *name_string != 0)
17291     {
17292       if (demangle_name_func)
17293         name_string = (*demangle_name_func) (name_string);
17294
17295       add_AT_string (die, DW_AT_name, name_string);
17296     }
17297 }
17298
17299 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17300
17301 static void
17302 add_comp_dir_attribute (dw_die_ref die)
17303 {
17304   const char *wd = get_src_pwd ();
17305   char *wd1;
17306
17307   if (wd == NULL)
17308     return;
17309
17310   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17311     {
17312       int wdlen;
17313
17314       wdlen = strlen (wd);
17315       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17316       strcpy (wd1, wd);
17317       wd1 [wdlen] = DIR_SEPARATOR;
17318       wd1 [wdlen + 1] = 0;
17319       wd = wd1;
17320     }
17321
17322     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17323 }
17324
17325 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17326    default.  */
17327
17328 static int
17329 lower_bound_default (void)
17330 {
17331   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17332     {
17333     case DW_LANG_C:
17334     case DW_LANG_C89:
17335     case DW_LANG_C99:
17336     case DW_LANG_C_plus_plus:
17337     case DW_LANG_ObjC:
17338     case DW_LANG_ObjC_plus_plus:
17339     case DW_LANG_Java:
17340       return 0;
17341     case DW_LANG_Fortran77:
17342     case DW_LANG_Fortran90:
17343     case DW_LANG_Fortran95:
17344       return 1;
17345     case DW_LANG_UPC:
17346     case DW_LANG_D:
17347     case DW_LANG_Python:
17348       return dwarf_version >= 4 ? 0 : -1;
17349     case DW_LANG_Ada95:
17350     case DW_LANG_Ada83:
17351     case DW_LANG_Cobol74:
17352     case DW_LANG_Cobol85:
17353     case DW_LANG_Pascal83:
17354     case DW_LANG_Modula2:
17355     case DW_LANG_PLI:
17356       return dwarf_version >= 4 ? 1 : -1;
17357     default:
17358       return -1;
17359     }
17360 }
17361
17362 /* Given a tree node describing an array bound (either lower or upper) output
17363    a representation for that bound.  */
17364
17365 static void
17366 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17367 {
17368   switch (TREE_CODE (bound))
17369     {
17370     case ERROR_MARK:
17371       return;
17372
17373     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17374     case INTEGER_CST:
17375       {
17376         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17377         int dflt;
17378
17379         /* Use the default if possible.  */
17380         if (bound_attr == DW_AT_lower_bound
17381             && host_integerp (bound, 0)
17382             && (dflt = lower_bound_default ()) != -1
17383             && tree_low_cst (bound, 0) == dflt)
17384           ;
17385
17386         /* Otherwise represent the bound as an unsigned value with the
17387            precision of its type.  The precision and signedness of the
17388            type will be necessary to re-interpret it unambiguously.  */
17389         else if (prec < HOST_BITS_PER_WIDE_INT)
17390           {
17391             unsigned HOST_WIDE_INT mask
17392               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17393             add_AT_unsigned (subrange_die, bound_attr,
17394                              TREE_INT_CST_LOW (bound) & mask);
17395           }
17396         else if (prec == HOST_BITS_PER_WIDE_INT
17397                  || TREE_INT_CST_HIGH (bound) == 0)
17398           add_AT_unsigned (subrange_die, bound_attr,
17399                            TREE_INT_CST_LOW (bound));
17400         else
17401           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17402                          TREE_INT_CST_LOW (bound));
17403       }
17404       break;
17405
17406     CASE_CONVERT:
17407     case VIEW_CONVERT_EXPR:
17408       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17409       break;
17410
17411     case SAVE_EXPR:
17412       break;
17413
17414     case VAR_DECL:
17415     case PARM_DECL:
17416     case RESULT_DECL:
17417       {
17418         dw_die_ref decl_die = lookup_decl_die (bound);
17419
17420         /* ??? Can this happen, or should the variable have been bound
17421            first?  Probably it can, since I imagine that we try to create
17422            the types of parameters in the order in which they exist in
17423            the list, and won't have created a forward reference to a
17424            later parameter.  */
17425         if (decl_die != NULL)
17426           {
17427             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17428             break;
17429           }
17430       }
17431       /* FALLTHRU */
17432
17433     default:
17434       {
17435         /* Otherwise try to create a stack operation procedure to
17436            evaluate the value of the array bound.  */
17437
17438         dw_die_ref ctx, decl_die;
17439         dw_loc_list_ref list;
17440
17441         list = loc_list_from_tree (bound, 2);
17442         if (list == NULL || single_element_loc_list_p (list))
17443           {
17444             /* If DW_AT_*bound is not a reference nor constant, it is
17445                a DWARF expression rather than location description.
17446                For that loc_list_from_tree (bound, 0) is needed.
17447                If that fails to give a single element list,
17448                fall back to outputting this as a reference anyway.  */
17449             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17450             if (list2 && single_element_loc_list_p (list2))
17451               {
17452                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17453                 break;
17454               }
17455           }
17456         if (list == NULL)
17457           break;
17458
17459         if (current_function_decl == 0)
17460           ctx = comp_unit_die ();
17461         else
17462           ctx = lookup_decl_die (current_function_decl);
17463
17464         decl_die = new_die (DW_TAG_variable, ctx, bound);
17465         add_AT_flag (decl_die, DW_AT_artificial, 1);
17466         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17467         add_AT_location_description (decl_die, DW_AT_location, list);
17468         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17469         break;
17470       }
17471     }
17472 }
17473
17474 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17475    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17476    Note that the block of subscript information for an array type also
17477    includes information about the element type of the given array type.  */
17478
17479 static void
17480 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17481 {
17482   unsigned dimension_number;
17483   tree lower, upper;
17484   dw_die_ref subrange_die;
17485
17486   for (dimension_number = 0;
17487        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17488        type = TREE_TYPE (type), dimension_number++)
17489     {
17490       tree domain = TYPE_DOMAIN (type);
17491
17492       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17493         break;
17494
17495       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17496          and (in GNU C only) variable bounds.  Handle all three forms
17497          here.  */
17498       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17499       if (domain)
17500         {
17501           /* We have an array type with specified bounds.  */
17502           lower = TYPE_MIN_VALUE (domain);
17503           upper = TYPE_MAX_VALUE (domain);
17504
17505           /* Define the index type.  */
17506           if (TREE_TYPE (domain))
17507             {
17508               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17509                  TREE_TYPE field.  We can't emit debug info for this
17510                  because it is an unnamed integral type.  */
17511               if (TREE_CODE (domain) == INTEGER_TYPE
17512                   && TYPE_NAME (domain) == NULL_TREE
17513                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17514                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17515                 ;
17516               else
17517                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17518                                     type_die);
17519             }
17520
17521           /* ??? If upper is NULL, the array has unspecified length,
17522              but it does have a lower bound.  This happens with Fortran
17523                dimension arr(N:*)
17524              Since the debugger is definitely going to need to know N
17525              to produce useful results, go ahead and output the lower
17526              bound solo, and hope the debugger can cope.  */
17527
17528           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17529           if (upper)
17530             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17531         }
17532
17533       /* Otherwise we have an array type with an unspecified length.  The
17534          DWARF-2 spec does not say how to handle this; let's just leave out the
17535          bounds.  */
17536     }
17537 }
17538
17539 static void
17540 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17541 {
17542   unsigned size;
17543
17544   switch (TREE_CODE (tree_node))
17545     {
17546     case ERROR_MARK:
17547       size = 0;
17548       break;
17549     case ENUMERAL_TYPE:
17550     case RECORD_TYPE:
17551     case UNION_TYPE:
17552     case QUAL_UNION_TYPE:
17553       size = int_size_in_bytes (tree_node);
17554       break;
17555     case FIELD_DECL:
17556       /* For a data member of a struct or union, the DW_AT_byte_size is
17557          generally given as the number of bytes normally allocated for an
17558          object of the *declared* type of the member itself.  This is true
17559          even for bit-fields.  */
17560       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17561       break;
17562     default:
17563       gcc_unreachable ();
17564     }
17565
17566   /* Note that `size' might be -1 when we get to this point.  If it is, that
17567      indicates that the byte size of the entity in question is variable.  We
17568      have no good way of expressing this fact in Dwarf at the present time,
17569      so just let the -1 pass on through.  */
17570   add_AT_unsigned (die, DW_AT_byte_size, size);
17571 }
17572
17573 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17574    which specifies the distance in bits from the highest order bit of the
17575    "containing object" for the bit-field to the highest order bit of the
17576    bit-field itself.
17577
17578    For any given bit-field, the "containing object" is a hypothetical object
17579    (of some integral or enum type) within which the given bit-field lives.  The
17580    type of this hypothetical "containing object" is always the same as the
17581    declared type of the individual bit-field itself.  The determination of the
17582    exact location of the "containing object" for a bit-field is rather
17583    complicated.  It's handled by the `field_byte_offset' function (above).
17584
17585    Note that it is the size (in bytes) of the hypothetical "containing object"
17586    which will be given in the DW_AT_byte_size attribute for this bit-field.
17587    (See `byte_size_attribute' above).  */
17588
17589 static inline void
17590 add_bit_offset_attribute (dw_die_ref die, tree decl)
17591 {
17592   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17593   tree type = DECL_BIT_FIELD_TYPE (decl);
17594   HOST_WIDE_INT bitpos_int;
17595   HOST_WIDE_INT highest_order_object_bit_offset;
17596   HOST_WIDE_INT highest_order_field_bit_offset;
17597   HOST_WIDE_INT unsigned bit_offset;
17598
17599   /* Must be a field and a bit field.  */
17600   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17601
17602   /* We can't yet handle bit-fields whose offsets are variable, so if we
17603      encounter such things, just return without generating any attribute
17604      whatsoever.  Likewise for variable or too large size.  */
17605   if (! host_integerp (bit_position (decl), 0)
17606       || ! host_integerp (DECL_SIZE (decl), 1))
17607     return;
17608
17609   bitpos_int = int_bit_position (decl);
17610
17611   /* Note that the bit offset is always the distance (in bits) from the
17612      highest-order bit of the "containing object" to the highest-order bit of
17613      the bit-field itself.  Since the "high-order end" of any object or field
17614      is different on big-endian and little-endian machines, the computation
17615      below must take account of these differences.  */
17616   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17617   highest_order_field_bit_offset = bitpos_int;
17618
17619   if (! BYTES_BIG_ENDIAN)
17620     {
17621       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17622       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17623     }
17624
17625   bit_offset
17626     = (! BYTES_BIG_ENDIAN
17627        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17628        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17629
17630   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17631 }
17632
17633 /* For a FIELD_DECL node which represents a bit field, output an attribute
17634    which specifies the length in bits of the given field.  */
17635
17636 static inline void
17637 add_bit_size_attribute (dw_die_ref die, tree decl)
17638 {
17639   /* Must be a field and a bit field.  */
17640   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17641               && DECL_BIT_FIELD_TYPE (decl));
17642
17643   if (host_integerp (DECL_SIZE (decl), 1))
17644     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17645 }
17646
17647 /* If the compiled language is ANSI C, then add a 'prototyped'
17648    attribute, if arg types are given for the parameters of a function.  */
17649
17650 static inline void
17651 add_prototyped_attribute (dw_die_ref die, tree func_type)
17652 {
17653   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17654       && TYPE_ARG_TYPES (func_type) != NULL)
17655     add_AT_flag (die, DW_AT_prototyped, 1);
17656 }
17657
17658 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17659    by looking in either the type declaration or object declaration
17660    equate table.  */
17661
17662 static inline dw_die_ref
17663 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17664 {
17665   dw_die_ref origin_die = NULL;
17666
17667   if (TREE_CODE (origin) != FUNCTION_DECL)
17668     {
17669       /* We may have gotten separated from the block for the inlined
17670          function, if we're in an exception handler or some such; make
17671          sure that the abstract function has been written out.
17672
17673          Doing this for nested functions is wrong, however; functions are
17674          distinct units, and our context might not even be inline.  */
17675       tree fn = origin;
17676
17677       if (TYPE_P (fn))
17678         fn = TYPE_STUB_DECL (fn);
17679
17680       fn = decl_function_context (fn);
17681       if (fn)
17682         dwarf2out_abstract_function (fn);
17683     }
17684
17685   if (DECL_P (origin))
17686     origin_die = lookup_decl_die (origin);
17687   else if (TYPE_P (origin))
17688     origin_die = lookup_type_die (origin);
17689
17690   /* XXX: Functions that are never lowered don't always have correct block
17691      trees (in the case of java, they simply have no block tree, in some other
17692      languages).  For these functions, there is nothing we can really do to
17693      output correct debug info for inlined functions in all cases.  Rather
17694      than die, we'll just produce deficient debug info now, in that we will
17695      have variables without a proper abstract origin.  In the future, when all
17696      functions are lowered, we should re-add a gcc_assert (origin_die)
17697      here.  */
17698
17699   if (origin_die)
17700     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17701   return origin_die;
17702 }
17703
17704 /* We do not currently support the pure_virtual attribute.  */
17705
17706 static inline void
17707 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17708 {
17709   if (DECL_VINDEX (func_decl))
17710     {
17711       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17712
17713       if (host_integerp (DECL_VINDEX (func_decl), 0))
17714         add_AT_loc (die, DW_AT_vtable_elem_location,
17715                     new_loc_descr (DW_OP_constu,
17716                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17717                                    0));
17718
17719       /* GNU extension: Record what type this method came from originally.  */
17720       if (debug_info_level > DINFO_LEVEL_TERSE
17721           && DECL_CONTEXT (func_decl))
17722         add_AT_die_ref (die, DW_AT_containing_type,
17723                         lookup_type_die (DECL_CONTEXT (func_decl)));
17724     }
17725 }
17726 \f
17727 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17728    given decl.  This used to be a vendor extension until after DWARF 4
17729    standardized it.  */
17730
17731 static void
17732 add_linkage_attr (dw_die_ref die, tree decl)
17733 {
17734   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17735
17736   /* Mimic what assemble_name_raw does with a leading '*'.  */
17737   if (name[0] == '*')
17738     name = &name[1];
17739
17740   if (dwarf_version >= 4)
17741     add_AT_string (die, DW_AT_linkage_name, name);
17742   else
17743     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17744 }
17745
17746 /* Add source coordinate attributes for the given decl.  */
17747
17748 static void
17749 add_src_coords_attributes (dw_die_ref die, tree decl)
17750 {
17751   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17752
17753   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17754   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17755 }
17756
17757 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17758
17759 static void
17760 add_linkage_name (dw_die_ref die, tree decl)
17761 {
17762   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17763        && TREE_PUBLIC (decl)
17764        && !DECL_ABSTRACT (decl)
17765        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17766        && die->die_tag != DW_TAG_member)
17767     {
17768       /* Defer until we have an assembler name set.  */
17769       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17770         {
17771           limbo_die_node *asm_name;
17772
17773           asm_name = ggc_alloc_cleared_limbo_die_node ();
17774           asm_name->die = die;
17775           asm_name->created_for = decl;
17776           asm_name->next = deferred_asm_name;
17777           deferred_asm_name = asm_name;
17778         }
17779       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17780         add_linkage_attr (die, decl);
17781     }
17782 }
17783
17784 /* Add a DW_AT_name attribute and source coordinate attribute for the
17785    given decl, but only if it actually has a name.  */
17786
17787 static void
17788 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17789 {
17790   tree decl_name;
17791
17792   decl_name = DECL_NAME (decl);
17793   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17794     {
17795       const char *name = dwarf2_name (decl, 0);
17796       if (name)
17797         add_name_attribute (die, name);
17798       if (! DECL_ARTIFICIAL (decl))
17799         add_src_coords_attributes (die, decl);
17800
17801       add_linkage_name (die, decl);
17802     }
17803
17804 #ifdef VMS_DEBUGGING_INFO
17805   /* Get the function's name, as described by its RTL.  This may be different
17806      from the DECL_NAME name used in the source file.  */
17807   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17808     {
17809       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17810                    XEXP (DECL_RTL (decl), 0));
17811       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17812     }
17813 #endif /* VMS_DEBUGGING_INFO */
17814 }
17815
17816 #ifdef VMS_DEBUGGING_INFO
17817 /* Output the debug main pointer die for VMS */
17818
17819 void
17820 dwarf2out_vms_debug_main_pointer (void)
17821 {
17822   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17823   dw_die_ref die;
17824
17825   /* Allocate the VMS debug main subprogram die.  */
17826   die = ggc_alloc_cleared_die_node ();
17827   die->die_tag = DW_TAG_subprogram;
17828   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17829   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17830                                current_function_funcdef_no);
17831   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17832
17833   /* Make it the first child of comp_unit_die ().  */
17834   die->die_parent = comp_unit_die ();
17835   if (comp_unit_die ()->die_child)
17836     {
17837       die->die_sib = comp_unit_die ()->die_child->die_sib;
17838       comp_unit_die ()->die_child->die_sib = die;
17839     }
17840   else
17841     {
17842       die->die_sib = die;
17843       comp_unit_die ()->die_child = die;
17844     }
17845 }
17846 #endif /* VMS_DEBUGGING_INFO */
17847
17848 /* Push a new declaration scope.  */
17849
17850 static void
17851 push_decl_scope (tree scope)
17852 {
17853   VEC_safe_push (tree, gc, decl_scope_table, scope);
17854 }
17855
17856 /* Pop a declaration scope.  */
17857
17858 static inline void
17859 pop_decl_scope (void)
17860 {
17861   VEC_pop (tree, decl_scope_table);
17862 }
17863
17864 /* Return the DIE for the scope that immediately contains this type.
17865    Non-named types get global scope.  Named types nested in other
17866    types get their containing scope if it's open, or global scope
17867    otherwise.  All other types (i.e. function-local named types) get
17868    the current active scope.  */
17869
17870 static dw_die_ref
17871 scope_die_for (tree t, dw_die_ref context_die)
17872 {
17873   dw_die_ref scope_die = NULL;
17874   tree containing_scope;
17875   int i;
17876
17877   /* Non-types always go in the current scope.  */
17878   gcc_assert (TYPE_P (t));
17879
17880   containing_scope = TYPE_CONTEXT (t);
17881
17882   /* Use the containing namespace if it was passed in (for a declaration).  */
17883   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17884     {
17885       if (context_die == lookup_decl_die (containing_scope))
17886         /* OK */;
17887       else
17888         containing_scope = NULL_TREE;
17889     }
17890
17891   /* Ignore function type "scopes" from the C frontend.  They mean that
17892      a tagged type is local to a parmlist of a function declarator, but
17893      that isn't useful to DWARF.  */
17894   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17895     containing_scope = NULL_TREE;
17896
17897   if (SCOPE_FILE_SCOPE_P (containing_scope))
17898     scope_die = comp_unit_die ();
17899   else if (TYPE_P (containing_scope))
17900     {
17901       /* For types, we can just look up the appropriate DIE.  But
17902          first we check to see if we're in the middle of emitting it
17903          so we know where the new DIE should go.  */
17904       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17905         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17906           break;
17907
17908       if (i < 0)
17909         {
17910           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17911                       || TREE_ASM_WRITTEN (containing_scope));
17912           /*We are not in the middle of emitting the type
17913             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17914           scope_die = lookup_type_die (containing_scope);
17915
17916           /* If none of the current dies are suitable, we get file scope.  */
17917           if (scope_die == NULL)
17918             scope_die = comp_unit_die ();
17919         }
17920       else
17921         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17922     }
17923   else
17924     scope_die = context_die;
17925
17926   return scope_die;
17927 }
17928
17929 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17930
17931 static inline int
17932 local_scope_p (dw_die_ref context_die)
17933 {
17934   for (; context_die; context_die = context_die->die_parent)
17935     if (context_die->die_tag == DW_TAG_inlined_subroutine
17936         || context_die->die_tag == DW_TAG_subprogram)
17937       return 1;
17938
17939   return 0;
17940 }
17941
17942 /* Returns nonzero if CONTEXT_DIE is a class.  */
17943
17944 static inline int
17945 class_scope_p (dw_die_ref context_die)
17946 {
17947   return (context_die
17948           && (context_die->die_tag == DW_TAG_structure_type
17949               || context_die->die_tag == DW_TAG_class_type
17950               || context_die->die_tag == DW_TAG_interface_type
17951               || context_die->die_tag == DW_TAG_union_type));
17952 }
17953
17954 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17955    whether or not to treat a DIE in this context as a declaration.  */
17956
17957 static inline int
17958 class_or_namespace_scope_p (dw_die_ref context_die)
17959 {
17960   return (class_scope_p (context_die)
17961           || (context_die && context_die->die_tag == DW_TAG_namespace));
17962 }
17963
17964 /* Many forms of DIEs require a "type description" attribute.  This
17965    routine locates the proper "type descriptor" die for the type given
17966    by 'type', and adds a DW_AT_type attribute below the given die.  */
17967
17968 static void
17969 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17970                     int decl_volatile, dw_die_ref context_die)
17971 {
17972   enum tree_code code  = TREE_CODE (type);
17973   dw_die_ref type_die  = NULL;
17974
17975   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17976      or fixed-point type, use the inner type.  This is because we have no
17977      support for unnamed types in base_type_die.  This can happen if this is
17978      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17979   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17980       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17981     type = TREE_TYPE (type), code = TREE_CODE (type);
17982
17983   if (code == ERROR_MARK
17984       /* Handle a special case.  For functions whose return type is void, we
17985          generate *no* type attribute.  (Note that no object may have type
17986          `void', so this only applies to function return types).  */
17987       || code == VOID_TYPE)
17988     return;
17989
17990   type_die = modified_type_die (type,
17991                                 decl_const || TYPE_READONLY (type),
17992                                 decl_volatile || TYPE_VOLATILE (type),
17993                                 context_die);
17994
17995   if (type_die != NULL)
17996     add_AT_die_ref (object_die, DW_AT_type, type_die);
17997 }
17998
17999 /* Given an object die, add the calling convention attribute for the
18000    function call type.  */
18001 static void
18002 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18003 {
18004   enum dwarf_calling_convention value = DW_CC_normal;
18005
18006   value = ((enum dwarf_calling_convention)
18007            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18008
18009   if (is_fortran ()
18010       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18011     {
18012       /* DWARF 2 doesn't provide a way to identify a program's source-level
18013         entry point.  DW_AT_calling_convention attributes are only meant
18014         to describe functions' calling conventions.  However, lacking a
18015         better way to signal the Fortran main program, we used this for 
18016         a long time, following existing custom.  Now, DWARF 4 has 
18017         DW_AT_main_subprogram, which we add below, but some tools still
18018         rely on the old way, which we thus keep.  */
18019       value = DW_CC_program;
18020
18021       if (dwarf_version >= 4 || !dwarf_strict)
18022         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18023     }
18024
18025   /* Only add the attribute if the backend requests it, and
18026      is not DW_CC_normal.  */
18027   if (value && (value != DW_CC_normal))
18028     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18029 }
18030
18031 /* Given a tree pointer to a struct, class, union, or enum type node, return
18032    a pointer to the (string) tag name for the given type, or zero if the type
18033    was declared without a tag.  */
18034
18035 static const char *
18036 type_tag (const_tree type)
18037 {
18038   const char *name = 0;
18039
18040   if (TYPE_NAME (type) != 0)
18041     {
18042       tree t = 0;
18043
18044       /* Find the IDENTIFIER_NODE for the type name.  */
18045       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18046           && !TYPE_NAMELESS (type))
18047         t = TYPE_NAME (type);
18048
18049       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18050          a TYPE_DECL node, regardless of whether or not a `typedef' was
18051          involved.  */
18052       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18053                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18054         {
18055           /* We want to be extra verbose.  Don't call dwarf_name if
18056              DECL_NAME isn't set.  The default hook for decl_printable_name
18057              doesn't like that, and in this context it's correct to return
18058              0, instead of "<anonymous>" or the like.  */
18059           if (DECL_NAME (TYPE_NAME (type))
18060               && !DECL_NAMELESS (TYPE_NAME (type)))
18061             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18062         }
18063
18064       /* Now get the name as a string, or invent one.  */
18065       if (!name && t != 0)
18066         name = IDENTIFIER_POINTER (t);
18067     }
18068
18069   return (name == 0 || *name == '\0') ? 0 : name;
18070 }
18071
18072 /* Return the type associated with a data member, make a special check
18073    for bit field types.  */
18074
18075 static inline tree
18076 member_declared_type (const_tree member)
18077 {
18078   return (DECL_BIT_FIELD_TYPE (member)
18079           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18080 }
18081
18082 /* Get the decl's label, as described by its RTL. This may be different
18083    from the DECL_NAME name used in the source file.  */
18084
18085 #if 0
18086 static const char *
18087 decl_start_label (tree decl)
18088 {
18089   rtx x;
18090   const char *fnname;
18091
18092   x = DECL_RTL (decl);
18093   gcc_assert (MEM_P (x));
18094
18095   x = XEXP (x, 0);
18096   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18097
18098   fnname = XSTR (x, 0);
18099   return fnname;
18100 }
18101 #endif
18102 \f
18103 /* These routines generate the internal representation of the DIE's for
18104    the compilation unit.  Debugging information is collected by walking
18105    the declaration trees passed in from dwarf2out_decl().  */
18106
18107 static void
18108 gen_array_type_die (tree type, dw_die_ref context_die)
18109 {
18110   dw_die_ref scope_die = scope_die_for (type, context_die);
18111   dw_die_ref array_die;
18112
18113   /* GNU compilers represent multidimensional array types as sequences of one
18114      dimensional array types whose element types are themselves array types.
18115      We sometimes squish that down to a single array_type DIE with multiple
18116      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18117      say that we are allowed to do this kind of compression in C, because
18118      there is no difference between an array of arrays and a multidimensional
18119      array.  We don't do this for Ada to remain as close as possible to the
18120      actual representation, which is especially important against the language
18121      flexibilty wrt arrays of variable size.  */
18122
18123   bool collapse_nested_arrays = !is_ada ();
18124   tree element_type;
18125
18126   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18127      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18128   if (TYPE_STRING_FLAG (type)
18129       && TREE_CODE (type) == ARRAY_TYPE
18130       && is_fortran ()
18131       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18132     {
18133       HOST_WIDE_INT size;
18134
18135       array_die = new_die (DW_TAG_string_type, scope_die, type);
18136       add_name_attribute (array_die, type_tag (type));
18137       equate_type_number_to_die (type, array_die);
18138       size = int_size_in_bytes (type);
18139       if (size >= 0)
18140         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18141       else if (TYPE_DOMAIN (type) != NULL_TREE
18142                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18143                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18144         {
18145           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18146           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18147
18148           size = int_size_in_bytes (TREE_TYPE (szdecl));
18149           if (loc && size > 0)
18150             {
18151               add_AT_location_description (array_die, DW_AT_string_length, loc);
18152               if (size != DWARF2_ADDR_SIZE)
18153                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18154             }
18155         }
18156       return;
18157     }
18158
18159   /* ??? The SGI dwarf reader fails for array of array of enum types
18160      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18161      array type comes before the outer array type.  We thus call gen_type_die
18162      before we new_die and must prevent nested array types collapsing for this
18163      target.  */
18164
18165 #ifdef MIPS_DEBUGGING_INFO
18166   gen_type_die (TREE_TYPE (type), context_die);
18167   collapse_nested_arrays = false;
18168 #endif
18169
18170   array_die = new_die (DW_TAG_array_type, scope_die, type);
18171   add_name_attribute (array_die, type_tag (type));
18172   equate_type_number_to_die (type, array_die);
18173
18174   if (TREE_CODE (type) == VECTOR_TYPE)
18175     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18176
18177   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18178   if (is_fortran ()
18179       && TREE_CODE (type) == ARRAY_TYPE
18180       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18181       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18182     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18183
18184 #if 0
18185   /* We default the array ordering.  SDB will probably do
18186      the right things even if DW_AT_ordering is not present.  It's not even
18187      an issue until we start to get into multidimensional arrays anyway.  If
18188      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18189      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18190      and when we find out that we need to put these in, we will only do so
18191      for multidimensional arrays.  */
18192   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18193 #endif
18194
18195 #ifdef MIPS_DEBUGGING_INFO
18196   /* The SGI compilers handle arrays of unknown bound by setting
18197      AT_declaration and not emitting any subrange DIEs.  */
18198   if (TREE_CODE (type) == ARRAY_TYPE
18199       && ! TYPE_DOMAIN (type))
18200     add_AT_flag (array_die, DW_AT_declaration, 1);
18201   else
18202 #endif
18203   if (TREE_CODE (type) == VECTOR_TYPE)
18204     {
18205       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18206       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18207       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18208       add_bound_info (subrange_die, DW_AT_upper_bound,
18209                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18210     }
18211   else
18212     add_subscript_info (array_die, type, collapse_nested_arrays);
18213
18214   /* Add representation of the type of the elements of this array type and
18215      emit the corresponding DIE if we haven't done it already.  */
18216   element_type = TREE_TYPE (type);
18217   if (collapse_nested_arrays)
18218     while (TREE_CODE (element_type) == ARRAY_TYPE)
18219       {
18220         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18221           break;
18222         element_type = TREE_TYPE (element_type);
18223       }
18224
18225 #ifndef MIPS_DEBUGGING_INFO
18226   gen_type_die (element_type, context_die);
18227 #endif
18228
18229   add_type_attribute (array_die, element_type, 0, 0, context_die);
18230
18231   if (get_AT (array_die, DW_AT_name))
18232     add_pubtype (type, array_die);
18233 }
18234
18235 static dw_loc_descr_ref
18236 descr_info_loc (tree val, tree base_decl)
18237 {
18238   HOST_WIDE_INT size;
18239   dw_loc_descr_ref loc, loc2;
18240   enum dwarf_location_atom op;
18241
18242   if (val == base_decl)
18243     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18244
18245   switch (TREE_CODE (val))
18246     {
18247     CASE_CONVERT:
18248       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18249     case VAR_DECL:
18250       return loc_descriptor_from_tree (val, 0);
18251     case INTEGER_CST:
18252       if (host_integerp (val, 0))
18253         return int_loc_descriptor (tree_low_cst (val, 0));
18254       break;
18255     case INDIRECT_REF:
18256       size = int_size_in_bytes (TREE_TYPE (val));
18257       if (size < 0)
18258         break;
18259       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18260       if (!loc)
18261         break;
18262       if (size == DWARF2_ADDR_SIZE)
18263         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18264       else
18265         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18266       return loc;
18267     case POINTER_PLUS_EXPR:
18268     case PLUS_EXPR:
18269       if (host_integerp (TREE_OPERAND (val, 1), 1)
18270           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18271              < 16384)
18272         {
18273           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18274           if (!loc)
18275             break;
18276           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18277         }
18278       else
18279         {
18280           op = DW_OP_plus;
18281         do_binop:
18282           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18283           if (!loc)
18284             break;
18285           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18286           if (!loc2)
18287             break;
18288           add_loc_descr (&loc, loc2);
18289           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18290         }
18291       return loc;
18292     case MINUS_EXPR:
18293       op = DW_OP_minus;
18294       goto do_binop;
18295     case MULT_EXPR:
18296       op = DW_OP_mul;
18297       goto do_binop;
18298     case EQ_EXPR:
18299       op = DW_OP_eq;
18300       goto do_binop;
18301     case NE_EXPR:
18302       op = DW_OP_ne;
18303       goto do_binop;
18304     default:
18305       break;
18306     }
18307   return NULL;
18308 }
18309
18310 static void
18311 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18312                       tree val, tree base_decl)
18313 {
18314   dw_loc_descr_ref loc;
18315
18316   if (host_integerp (val, 0))
18317     {
18318       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18319       return;
18320     }
18321
18322   loc = descr_info_loc (val, base_decl);
18323   if (!loc)
18324     return;
18325
18326   add_AT_loc (die, attr, loc);
18327 }
18328
18329 /* This routine generates DIE for array with hidden descriptor, details
18330    are filled into *info by a langhook.  */
18331
18332 static void
18333 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18334                           dw_die_ref context_die)
18335 {
18336   dw_die_ref scope_die = scope_die_for (type, context_die);
18337   dw_die_ref array_die;
18338   int dim;
18339
18340   array_die = new_die (DW_TAG_array_type, scope_die, type);
18341   add_name_attribute (array_die, type_tag (type));
18342   equate_type_number_to_die (type, array_die);
18343
18344   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18345   if (is_fortran ()
18346       && info->ndimensions >= 2)
18347     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18348
18349   if (info->data_location)
18350     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18351                           info->base_decl);
18352   if (info->associated)
18353     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18354                           info->base_decl);
18355   if (info->allocated)
18356     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18357                           info->base_decl);
18358
18359   for (dim = 0; dim < info->ndimensions; dim++)
18360     {
18361       dw_die_ref subrange_die
18362         = new_die (DW_TAG_subrange_type, array_die, NULL);
18363
18364       if (info->dimen[dim].lower_bound)
18365         {
18366           /* If it is the default value, omit it.  */
18367           int dflt;
18368
18369           if (host_integerp (info->dimen[dim].lower_bound, 0)
18370               && (dflt = lower_bound_default ()) != -1
18371               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18372             ;
18373           else
18374             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18375                                   info->dimen[dim].lower_bound,
18376                                   info->base_decl);
18377         }
18378       if (info->dimen[dim].upper_bound)
18379         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18380                               info->dimen[dim].upper_bound,
18381                               info->base_decl);
18382       if (info->dimen[dim].stride)
18383         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18384                               info->dimen[dim].stride,
18385                               info->base_decl);
18386     }
18387
18388   gen_type_die (info->element_type, context_die);
18389   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18390
18391   if (get_AT (array_die, DW_AT_name))
18392     add_pubtype (type, array_die);
18393 }
18394
18395 #if 0
18396 static void
18397 gen_entry_point_die (tree decl, dw_die_ref context_die)
18398 {
18399   tree origin = decl_ultimate_origin (decl);
18400   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18401
18402   if (origin != NULL)
18403     add_abstract_origin_attribute (decl_die, origin);
18404   else
18405     {
18406       add_name_and_src_coords_attributes (decl_die, decl);
18407       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18408                           0, 0, context_die);
18409     }
18410
18411   if (DECL_ABSTRACT (decl))
18412     equate_decl_number_to_die (decl, decl_die);
18413   else
18414     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18415 }
18416 #endif
18417
18418 /* Walk through the list of incomplete types again, trying once more to
18419    emit full debugging info for them.  */
18420
18421 static void
18422 retry_incomplete_types (void)
18423 {
18424   int i;
18425
18426   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18427     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18428                                   DINFO_USAGE_DIR_USE))
18429       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18430 }
18431
18432 /* Determine what tag to use for a record type.  */
18433
18434 static enum dwarf_tag
18435 record_type_tag (tree type)
18436 {
18437   if (! lang_hooks.types.classify_record)
18438     return DW_TAG_structure_type;
18439
18440   switch (lang_hooks.types.classify_record (type))
18441     {
18442     case RECORD_IS_STRUCT:
18443       return DW_TAG_structure_type;
18444
18445     case RECORD_IS_CLASS:
18446       return DW_TAG_class_type;
18447
18448     case RECORD_IS_INTERFACE:
18449       if (dwarf_version >= 3 || !dwarf_strict)
18450         return DW_TAG_interface_type;
18451       return DW_TAG_structure_type;
18452
18453     default:
18454       gcc_unreachable ();
18455     }
18456 }
18457
18458 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18459    include all of the information about the enumeration values also. Each
18460    enumerated type name/value is listed as a child of the enumerated type
18461    DIE.  */
18462
18463 static dw_die_ref
18464 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18465 {
18466   dw_die_ref type_die = lookup_type_die (type);
18467
18468   if (type_die == NULL)
18469     {
18470       type_die = new_die (DW_TAG_enumeration_type,
18471                           scope_die_for (type, context_die), type);
18472       equate_type_number_to_die (type, type_die);
18473       add_name_attribute (type_die, type_tag (type));
18474       if (dwarf_version >= 4 || !dwarf_strict)
18475         {
18476           if (ENUM_IS_SCOPED (type))
18477             add_AT_flag (type_die, DW_AT_enum_class, 1);
18478           if (ENUM_IS_OPAQUE (type))
18479             add_AT_flag (type_die, DW_AT_declaration, 1);
18480         }
18481     }
18482   else if (! TYPE_SIZE (type))
18483     return type_die;
18484   else
18485     remove_AT (type_die, DW_AT_declaration);
18486
18487   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18488      given enum type is incomplete, do not generate the DW_AT_byte_size
18489      attribute or the DW_AT_element_list attribute.  */
18490   if (TYPE_SIZE (type))
18491     {
18492       tree link;
18493
18494       TREE_ASM_WRITTEN (type) = 1;
18495       add_byte_size_attribute (type_die, type);
18496       if (TYPE_STUB_DECL (type) != NULL_TREE)
18497         {
18498           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18499           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18500         }
18501
18502       /* If the first reference to this type was as the return type of an
18503          inline function, then it may not have a parent.  Fix this now.  */
18504       if (type_die->die_parent == NULL)
18505         add_child_die (scope_die_for (type, context_die), type_die);
18506
18507       for (link = TYPE_VALUES (type);
18508            link != NULL; link = TREE_CHAIN (link))
18509         {
18510           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18511           tree value = TREE_VALUE (link);
18512
18513           add_name_attribute (enum_die,
18514                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18515
18516           if (TREE_CODE (value) == CONST_DECL)
18517             value = DECL_INITIAL (value);
18518
18519           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18520             /* DWARF2 does not provide a way of indicating whether or
18521                not enumeration constants are signed or unsigned.  GDB
18522                always assumes the values are signed, so we output all
18523                values as if they were signed.  That means that
18524                enumeration constants with very large unsigned values
18525                will appear to have negative values in the debugger.  */
18526             add_AT_int (enum_die, DW_AT_const_value,
18527                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18528         }
18529     }
18530   else
18531     add_AT_flag (type_die, DW_AT_declaration, 1);
18532
18533   if (get_AT (type_die, DW_AT_name))
18534     add_pubtype (type, type_die);
18535
18536   return type_die;
18537 }
18538
18539 /* Generate a DIE to represent either a real live formal parameter decl or to
18540    represent just the type of some formal parameter position in some function
18541    type.
18542
18543    Note that this routine is a bit unusual because its argument may be a
18544    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18545    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18546    node.  If it's the former then this function is being called to output a
18547    DIE to represent a formal parameter object (or some inlining thereof).  If
18548    it's the latter, then this function is only being called to output a
18549    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18550    argument type of some subprogram type.
18551    If EMIT_NAME_P is true, name and source coordinate attributes
18552    are emitted.  */
18553
18554 static dw_die_ref
18555 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18556                           dw_die_ref context_die)
18557 {
18558   tree node_or_origin = node ? node : origin;
18559   tree ultimate_origin;
18560   dw_die_ref parm_die
18561     = new_die (DW_TAG_formal_parameter, context_die, node);
18562
18563   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18564     {
18565     case tcc_declaration:
18566       ultimate_origin = decl_ultimate_origin (node_or_origin);
18567       if (node || ultimate_origin)
18568         origin = ultimate_origin;
18569       if (origin != NULL)
18570         add_abstract_origin_attribute (parm_die, origin);
18571       else if (emit_name_p)
18572         add_name_and_src_coords_attributes (parm_die, node);
18573       if (origin == NULL
18574           || (! DECL_ABSTRACT (node_or_origin)
18575               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18576                                            decl_function_context
18577                                                             (node_or_origin))))
18578         {
18579           tree type = TREE_TYPE (node_or_origin);
18580           if (decl_by_reference_p (node_or_origin))
18581             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18582                                 context_die);
18583           else
18584             add_type_attribute (parm_die, type,
18585                                 TREE_READONLY (node_or_origin),
18586                                 TREE_THIS_VOLATILE (node_or_origin),
18587                                 context_die);
18588         }
18589       if (origin == NULL && DECL_ARTIFICIAL (node))
18590         add_AT_flag (parm_die, DW_AT_artificial, 1);
18591
18592       if (node && node != origin)
18593         equate_decl_number_to_die (node, parm_die);
18594       if (! DECL_ABSTRACT (node_or_origin))
18595         add_location_or_const_value_attribute (parm_die, node_or_origin,
18596                                                DW_AT_location);
18597
18598       break;
18599
18600     case tcc_type:
18601       /* We were called with some kind of a ..._TYPE node.  */
18602       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18603       break;
18604
18605     default:
18606       gcc_unreachable ();
18607     }
18608
18609   return parm_die;
18610 }
18611
18612 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18613    children DW_TAG_formal_parameter DIEs representing the arguments of the
18614    parameter pack.
18615
18616    PARM_PACK must be a function parameter pack.
18617    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18618    must point to the subsequent arguments of the function PACK_ARG belongs to.
18619    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18620    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18621    following the last one for which a DIE was generated.  */
18622
18623 static dw_die_ref
18624 gen_formal_parameter_pack_die  (tree parm_pack,
18625                                 tree pack_arg,
18626                                 dw_die_ref subr_die,
18627                                 tree *next_arg)
18628 {
18629   tree arg;
18630   dw_die_ref parm_pack_die;
18631
18632   gcc_assert (parm_pack
18633               && lang_hooks.function_parameter_pack_p (parm_pack)
18634               && subr_die);
18635
18636   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18637   add_src_coords_attributes (parm_pack_die, parm_pack);
18638
18639   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18640     {
18641       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18642                                                                  parm_pack))
18643         break;
18644       gen_formal_parameter_die (arg, NULL,
18645                                 false /* Don't emit name attribute.  */,
18646                                 parm_pack_die);
18647     }
18648   if (next_arg)
18649     *next_arg = arg;
18650   return parm_pack_die;
18651 }
18652
18653 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18654    at the end of an (ANSI prototyped) formal parameters list.  */
18655
18656 static void
18657 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18658 {
18659   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18660 }
18661
18662 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18663    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18664    parameters as specified in some function type specification (except for
18665    those which appear as part of a function *definition*).  */
18666
18667 static void
18668 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18669 {
18670   tree link;
18671   tree formal_type = NULL;
18672   tree first_parm_type;
18673   tree arg;
18674
18675   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18676     {
18677       arg = DECL_ARGUMENTS (function_or_method_type);
18678       function_or_method_type = TREE_TYPE (function_or_method_type);
18679     }
18680   else
18681     arg = NULL_TREE;
18682
18683   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18684
18685   /* Make our first pass over the list of formal parameter types and output a
18686      DW_TAG_formal_parameter DIE for each one.  */
18687   for (link = first_parm_type; link; )
18688     {
18689       dw_die_ref parm_die;
18690
18691       formal_type = TREE_VALUE (link);
18692       if (formal_type == void_type_node)
18693         break;
18694
18695       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18696       parm_die = gen_formal_parameter_die (formal_type, NULL,
18697                                            true /* Emit name attribute.  */,
18698                                            context_die);
18699       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18700           && link == first_parm_type)
18701         {
18702           add_AT_flag (parm_die, DW_AT_artificial, 1);
18703           if (dwarf_version >= 3 || !dwarf_strict)
18704             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18705         }
18706       else if (arg && DECL_ARTIFICIAL (arg))
18707         add_AT_flag (parm_die, DW_AT_artificial, 1);
18708
18709       link = TREE_CHAIN (link);
18710       if (arg)
18711         arg = DECL_CHAIN (arg);
18712     }
18713
18714   /* If this function type has an ellipsis, add a
18715      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18716   if (formal_type != void_type_node)
18717     gen_unspecified_parameters_die (function_or_method_type, context_die);
18718
18719   /* Make our second (and final) pass over the list of formal parameter types
18720      and output DIEs to represent those types (as necessary).  */
18721   for (link = TYPE_ARG_TYPES (function_or_method_type);
18722        link && TREE_VALUE (link);
18723        link = TREE_CHAIN (link))
18724     gen_type_die (TREE_VALUE (link), context_die);
18725 }
18726
18727 /* We want to generate the DIE for TYPE so that we can generate the
18728    die for MEMBER, which has been defined; we will need to refer back
18729    to the member declaration nested within TYPE.  If we're trying to
18730    generate minimal debug info for TYPE, processing TYPE won't do the
18731    trick; we need to attach the member declaration by hand.  */
18732
18733 static void
18734 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18735 {
18736   gen_type_die (type, context_die);
18737
18738   /* If we're trying to avoid duplicate debug info, we may not have
18739      emitted the member decl for this function.  Emit it now.  */
18740   if (TYPE_STUB_DECL (type)
18741       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18742       && ! lookup_decl_die (member))
18743     {
18744       dw_die_ref type_die;
18745       gcc_assert (!decl_ultimate_origin (member));
18746
18747       push_decl_scope (type);
18748       type_die = lookup_type_die_strip_naming_typedef (type);
18749       if (TREE_CODE (member) == FUNCTION_DECL)
18750         gen_subprogram_die (member, type_die);
18751       else if (TREE_CODE (member) == FIELD_DECL)
18752         {
18753           /* Ignore the nameless fields that are used to skip bits but handle
18754              C++ anonymous unions and structs.  */
18755           if (DECL_NAME (member) != NULL_TREE
18756               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18757               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18758             {
18759               gen_type_die (member_declared_type (member), type_die);
18760               gen_field_die (member, type_die);
18761             }
18762         }
18763       else
18764         gen_variable_die (member, NULL_TREE, type_die);
18765
18766       pop_decl_scope ();
18767     }
18768 }
18769
18770 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18771    may later generate inlined and/or out-of-line instances of.  */
18772
18773 static void
18774 dwarf2out_abstract_function (tree decl)
18775 {
18776   dw_die_ref old_die;
18777   tree save_fn;
18778   tree context;
18779   int was_abstract;
18780   htab_t old_decl_loc_table;
18781
18782   /* Make sure we have the actual abstract inline, not a clone.  */
18783   decl = DECL_ORIGIN (decl);
18784
18785   old_die = lookup_decl_die (decl);
18786   if (old_die && get_AT (old_die, DW_AT_inline))
18787     /* We've already generated the abstract instance.  */
18788     return;
18789
18790   /* We can be called while recursively when seeing block defining inlined subroutine
18791      DIE.  Be sure to not clobber the outer location table nor use it or we would
18792      get locations in abstract instantces.  */
18793   old_decl_loc_table = decl_loc_table;
18794   decl_loc_table = NULL;
18795
18796   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18797      we don't get confused by DECL_ABSTRACT.  */
18798   if (debug_info_level > DINFO_LEVEL_TERSE)
18799     {
18800       context = decl_class_context (decl);
18801       if (context)
18802         gen_type_die_for_member
18803           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18804     }
18805
18806   /* Pretend we've just finished compiling this function.  */
18807   save_fn = current_function_decl;
18808   current_function_decl = decl;
18809   push_cfun (DECL_STRUCT_FUNCTION (decl));
18810
18811   was_abstract = DECL_ABSTRACT (decl);
18812   set_decl_abstract_flags (decl, 1);
18813   dwarf2out_decl (decl);
18814   if (! was_abstract)
18815     set_decl_abstract_flags (decl, 0);
18816
18817   current_function_decl = save_fn;
18818   decl_loc_table = old_decl_loc_table;
18819   pop_cfun ();
18820 }
18821
18822 /* Helper function of premark_used_types() which gets called through
18823    htab_traverse.
18824
18825    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18826    marked as unused by prune_unused_types.  */
18827
18828 static int
18829 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18830 {
18831   tree type;
18832   dw_die_ref die;
18833
18834   type = (tree) *slot;
18835   die = lookup_type_die (type);
18836   if (die != NULL)
18837     die->die_perennial_p = 1;
18838   return 1;
18839 }
18840
18841 /* Helper function of premark_types_used_by_global_vars which gets called
18842    through htab_traverse.
18843
18844    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18845    marked as unused by prune_unused_types. The DIE of the type is marked
18846    only if the global variable using the type will actually be emitted.  */
18847
18848 static int
18849 premark_types_used_by_global_vars_helper (void **slot,
18850                                           void *data ATTRIBUTE_UNUSED)
18851 {
18852   struct types_used_by_vars_entry *entry;
18853   dw_die_ref die;
18854
18855   entry = (struct types_used_by_vars_entry *) *slot;
18856   gcc_assert (entry->type != NULL
18857               && entry->var_decl != NULL);
18858   die = lookup_type_die (entry->type);
18859   if (die)
18860     {
18861       /* Ask cgraph if the global variable really is to be emitted.
18862          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18863       struct varpool_node *node = varpool_get_node (entry->var_decl);
18864       if (node && node->needed)
18865         {
18866           die->die_perennial_p = 1;
18867           /* Keep the parent DIEs as well.  */
18868           while ((die = die->die_parent) && die->die_perennial_p == 0)
18869             die->die_perennial_p = 1;
18870         }
18871     }
18872   return 1;
18873 }
18874
18875 /* Mark all members of used_types_hash as perennial.  */
18876
18877 static void
18878 premark_used_types (void)
18879 {
18880   if (cfun && cfun->used_types_hash)
18881     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18882 }
18883
18884 /* Mark all members of types_used_by_vars_entry as perennial.  */
18885
18886 static void
18887 premark_types_used_by_global_vars (void)
18888 {
18889   if (types_used_by_vars_hash)
18890     htab_traverse (types_used_by_vars_hash,
18891                    premark_types_used_by_global_vars_helper, NULL);
18892 }
18893
18894 /* Generate a DIE to represent a declared function (either file-scope or
18895    block-local).  */
18896
18897 static void
18898 gen_subprogram_die (tree decl, dw_die_ref context_die)
18899 {
18900   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18901   tree origin = decl_ultimate_origin (decl);
18902   dw_die_ref subr_die;
18903   tree fn_arg_types;
18904   tree outer_scope;
18905   dw_die_ref old_die = lookup_decl_die (decl);
18906   int declaration = (current_function_decl != decl
18907                      || class_or_namespace_scope_p (context_die));
18908
18909   premark_used_types ();
18910
18911   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18912      started to generate the abstract instance of an inline, decided to output
18913      its containing class, and proceeded to emit the declaration of the inline
18914      from the member list for the class.  If so, DECLARATION takes priority;
18915      we'll get back to the abstract instance when done with the class.  */
18916
18917   /* The class-scope declaration DIE must be the primary DIE.  */
18918   if (origin && declaration && class_or_namespace_scope_p (context_die))
18919     {
18920       origin = NULL;
18921       gcc_assert (!old_die);
18922     }
18923
18924   /* Now that the C++ front end lazily declares artificial member fns, we
18925      might need to retrofit the declaration into its class.  */
18926   if (!declaration && !origin && !old_die
18927       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18928       && !class_or_namespace_scope_p (context_die)
18929       && debug_info_level > DINFO_LEVEL_TERSE)
18930     old_die = force_decl_die (decl);
18931
18932   if (origin != NULL)
18933     {
18934       gcc_assert (!declaration || local_scope_p (context_die));
18935
18936       /* Fixup die_parent for the abstract instance of a nested
18937          inline function.  */
18938       if (old_die && old_die->die_parent == NULL)
18939         add_child_die (context_die, old_die);
18940
18941       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18942       add_abstract_origin_attribute (subr_die, origin);
18943     }
18944   else if (old_die)
18945     {
18946       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18947       struct dwarf_file_data * file_index = lookup_filename (s.file);
18948
18949       if (!get_AT_flag (old_die, DW_AT_declaration)
18950           /* We can have a normal definition following an inline one in the
18951              case of redefinition of GNU C extern inlines.
18952              It seems reasonable to use AT_specification in this case.  */
18953           && !get_AT (old_die, DW_AT_inline))
18954         {
18955           /* Detect and ignore this case, where we are trying to output
18956              something we have already output.  */
18957           return;
18958         }
18959
18960       /* If the definition comes from the same place as the declaration,
18961          maybe use the old DIE.  We always want the DIE for this function
18962          that has the *_pc attributes to be under comp_unit_die so the
18963          debugger can find it.  We also need to do this for abstract
18964          instances of inlines, since the spec requires the out-of-line copy
18965          to have the same parent.  For local class methods, this doesn't
18966          apply; we just use the old DIE.  */
18967       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
18968           && (DECL_ARTIFICIAL (decl)
18969               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18970                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18971                       == (unsigned) s.line))))
18972         {
18973           subr_die = old_die;
18974
18975           /* Clear out the declaration attribute and the formal parameters.
18976              Do not remove all children, because it is possible that this
18977              declaration die was forced using force_decl_die(). In such
18978              cases die that forced declaration die (e.g. TAG_imported_module)
18979              is one of the children that we do not want to remove.  */
18980           remove_AT (subr_die, DW_AT_declaration);
18981           remove_AT (subr_die, DW_AT_object_pointer);
18982           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18983         }
18984       else
18985         {
18986           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18987           add_AT_specification (subr_die, old_die);
18988           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18989             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18990           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18991             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18992         }
18993     }
18994   else
18995     {
18996       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18997
18998       if (TREE_PUBLIC (decl))
18999         add_AT_flag (subr_die, DW_AT_external, 1);
19000
19001       add_name_and_src_coords_attributes (subr_die, decl);
19002       if (debug_info_level > DINFO_LEVEL_TERSE)
19003         {
19004           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19005           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19006                               0, 0, context_die);
19007         }
19008
19009       add_pure_or_virtual_attribute (subr_die, decl);
19010       if (DECL_ARTIFICIAL (decl))
19011         add_AT_flag (subr_die, DW_AT_artificial, 1);
19012
19013       add_accessibility_attribute (subr_die, decl);
19014     }
19015
19016   if (declaration)
19017     {
19018       if (!old_die || !get_AT (old_die, DW_AT_inline))
19019         {
19020           add_AT_flag (subr_die, DW_AT_declaration, 1);
19021
19022           /* If this is an explicit function declaration then generate
19023              a DW_AT_explicit attribute.  */
19024           if (lang_hooks.decls.function_decl_explicit_p (decl)
19025               && (dwarf_version >= 3 || !dwarf_strict))
19026             add_AT_flag (subr_die, DW_AT_explicit, 1);
19027
19028           /* The first time we see a member function, it is in the context of
19029              the class to which it belongs.  We make sure of this by emitting
19030              the class first.  The next time is the definition, which is
19031              handled above.  The two may come from the same source text.
19032
19033              Note that force_decl_die() forces function declaration die. It is
19034              later reused to represent definition.  */
19035           equate_decl_number_to_die (decl, subr_die);
19036         }
19037     }
19038   else if (DECL_ABSTRACT (decl))
19039     {
19040       if (DECL_DECLARED_INLINE_P (decl))
19041         {
19042           if (cgraph_function_possibly_inlined_p (decl))
19043             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19044           else
19045             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19046         }
19047       else
19048         {
19049           if (cgraph_function_possibly_inlined_p (decl))
19050             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19051           else
19052             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19053         }
19054
19055       if (DECL_DECLARED_INLINE_P (decl)
19056           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19057         add_AT_flag (subr_die, DW_AT_artificial, 1);
19058
19059       equate_decl_number_to_die (decl, subr_die);
19060     }
19061   else if (!DECL_EXTERNAL (decl))
19062     {
19063       HOST_WIDE_INT cfa_fb_offset;
19064
19065       if (!old_die || !get_AT (old_die, DW_AT_inline))
19066         equate_decl_number_to_die (decl, subr_die);
19067
19068       if (!flag_reorder_blocks_and_partition)
19069         {
19070           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19071                                        current_function_funcdef_no);
19072           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19073           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19074                                        current_function_funcdef_no);
19075           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19076
19077 #if VMS_DEBUGGING_INFO
19078       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19079          Section 2.3 Prologue and Epilogue Attributes:
19080          When a breakpoint is set on entry to a function, it is generally
19081          desirable for execution to be suspended, not on the very first
19082          instruction of the function, but rather at a point after the
19083          function's frame has been set up, after any language defined local
19084          declaration processing has been completed, and before execution of
19085          the first statement of the function begins. Debuggers generally
19086          cannot properly determine where this point is.  Similarly for a
19087          breakpoint set on exit from a function. The prologue and epilogue
19088          attributes allow a compiler to communicate the location(s) to use.  */
19089
19090       {
19091         dw_fde_ref fde = &fde_table[current_funcdef_fde];
19092
19093         if (fde->dw_fde_vms_end_prologue)
19094           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19095             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19096
19097         if (fde->dw_fde_vms_begin_epilogue)
19098           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19099             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19100       }
19101 #endif
19102
19103           add_pubname (decl, subr_die);
19104           add_arange (decl, subr_die);
19105         }
19106       else
19107         {  /* Do nothing for now; maybe need to duplicate die, one for
19108               hot section and one for cold section, then use the hot/cold
19109               section begin/end labels to generate the aranges...  */
19110           /*
19111             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
19112             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
19113             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
19114             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
19115
19116             add_pubname (decl, subr_die);
19117             add_arange (decl, subr_die);
19118             add_arange (decl, subr_die);
19119            */
19120         }
19121
19122 #ifdef MIPS_DEBUGGING_INFO
19123       /* Add a reference to the FDE for this routine.  */
19124       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19125 #endif
19126
19127       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19128
19129       /* We define the "frame base" as the function's CFA.  This is more
19130          convenient for several reasons: (1) It's stable across the prologue
19131          and epilogue, which makes it better than just a frame pointer,
19132          (2) With dwarf3, there exists a one-byte encoding that allows us
19133          to reference the .debug_frame data by proxy, but failing that,
19134          (3) We can at least reuse the code inspection and interpretation
19135          code that determines the CFA position at various points in the
19136          function.  */
19137       if (dwarf_version >= 3)
19138         {
19139           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19140           add_AT_loc (subr_die, DW_AT_frame_base, op);
19141         }
19142       else
19143         {
19144           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19145           if (list->dw_loc_next)
19146             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19147           else
19148             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19149         }
19150
19151       /* Compute a displacement from the "steady-state frame pointer" to
19152          the CFA.  The former is what all stack slots and argument slots
19153          will reference in the rtl; the later is what we've told the
19154          debugger about.  We'll need to adjust all frame_base references
19155          by this displacement.  */
19156       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19157
19158       if (cfun->static_chain_decl)
19159         add_AT_location_description (subr_die, DW_AT_static_link,
19160                  loc_list_from_tree (cfun->static_chain_decl, 2));
19161     }
19162
19163   /* Generate child dies for template paramaters.  */
19164   if (debug_info_level > DINFO_LEVEL_TERSE)
19165     gen_generic_params_dies (decl);
19166
19167   /* Now output descriptions of the arguments for this function. This gets
19168      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19169      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19170      `...' at the end of the formal parameter list.  In order to find out if
19171      there was a trailing ellipsis or not, we must instead look at the type
19172      associated with the FUNCTION_DECL.  This will be a node of type
19173      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19174      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19175      an ellipsis at the end.  */
19176
19177   /* In the case where we are describing a mere function declaration, all we
19178      need to do here (and all we *can* do here) is to describe the *types* of
19179      its formal parameters.  */
19180   if (debug_info_level <= DINFO_LEVEL_TERSE)
19181     ;
19182   else if (declaration)
19183     gen_formal_types_die (decl, subr_die);
19184   else
19185     {
19186       /* Generate DIEs to represent all known formal parameters.  */
19187       tree parm = DECL_ARGUMENTS (decl);
19188       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19189       tree generic_decl_parm = generic_decl
19190                                 ? DECL_ARGUMENTS (generic_decl)
19191                                 : NULL;
19192
19193       /* Now we want to walk the list of parameters of the function and
19194          emit their relevant DIEs.
19195
19196          We consider the case of DECL being an instance of a generic function
19197          as well as it being a normal function.
19198
19199          If DECL is an instance of a generic function we walk the
19200          parameters of the generic function declaration _and_ the parameters of
19201          DECL itself. This is useful because we want to emit specific DIEs for
19202          function parameter packs and those are declared as part of the
19203          generic function declaration. In that particular case,
19204          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19205          That DIE has children DIEs representing the set of arguments
19206          of the pack. Note that the set of pack arguments can be empty.
19207          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19208          children DIE.
19209
19210          Otherwise, we just consider the parameters of DECL.  */
19211       while (generic_decl_parm || parm)
19212         {
19213           if (generic_decl_parm
19214               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19215             gen_formal_parameter_pack_die (generic_decl_parm,
19216                                            parm, subr_die,
19217                                            &parm);
19218           else if (parm)
19219             {
19220               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19221
19222               if (parm == DECL_ARGUMENTS (decl)
19223                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19224                   && parm_die
19225                   && (dwarf_version >= 3 || !dwarf_strict))
19226                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19227
19228               parm = DECL_CHAIN (parm);
19229             }
19230
19231           if (generic_decl_parm)
19232             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19233         }
19234
19235       /* Decide whether we need an unspecified_parameters DIE at the end.
19236          There are 2 more cases to do this for: 1) the ansi ... declaration -
19237          this is detectable when the end of the arg list is not a
19238          void_type_node 2) an unprototyped function declaration (not a
19239          definition).  This just means that we have no info about the
19240          parameters at all.  */
19241       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
19242       if (fn_arg_types != NULL)
19243         {
19244           /* This is the prototyped case, check for....  */
19245           if (stdarg_p (TREE_TYPE (decl)))
19246             gen_unspecified_parameters_die (decl, subr_die);
19247         }
19248       else if (DECL_INITIAL (decl) == NULL_TREE)
19249         gen_unspecified_parameters_die (decl, subr_die);
19250     }
19251
19252   /* Output Dwarf info for all of the stuff within the body of the function
19253      (if it has one - it may be just a declaration).  */
19254   outer_scope = DECL_INITIAL (decl);
19255
19256   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19257      a function.  This BLOCK actually represents the outermost binding contour
19258      for the function, i.e. the contour in which the function's formal
19259      parameters and labels get declared. Curiously, it appears that the front
19260      end doesn't actually put the PARM_DECL nodes for the current function onto
19261      the BLOCK_VARS list for this outer scope, but are strung off of the
19262      DECL_ARGUMENTS list for the function instead.
19263
19264      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19265      the LABEL_DECL nodes for the function however, and we output DWARF info
19266      for those in decls_for_scope.  Just within the `outer_scope' there will be
19267      a BLOCK node representing the function's outermost pair of curly braces,
19268      and any blocks used for the base and member initializers of a C++
19269      constructor function.  */
19270   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19271     {
19272       /* Emit a DW_TAG_variable DIE for a named return value.  */
19273       if (DECL_NAME (DECL_RESULT (decl)))
19274         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19275
19276       current_function_has_inlines = 0;
19277       decls_for_scope (outer_scope, subr_die, 0);
19278     }
19279   /* Add the calling convention attribute if requested.  */
19280   add_calling_convention_attribute (subr_die, decl);
19281
19282 }
19283
19284 /* Returns a hash value for X (which really is a die_struct).  */
19285
19286 static hashval_t
19287 common_block_die_table_hash (const void *x)
19288 {
19289   const_dw_die_ref d = (const_dw_die_ref) x;
19290   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19291 }
19292
19293 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19294    as decl_id and die_parent of die_struct Y.  */
19295
19296 static int
19297 common_block_die_table_eq (const void *x, const void *y)
19298 {
19299   const_dw_die_ref d = (const_dw_die_ref) x;
19300   const_dw_die_ref e = (const_dw_die_ref) y;
19301   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19302 }
19303
19304 /* Generate a DIE to represent a declared data object.
19305    Either DECL or ORIGIN must be non-null.  */
19306
19307 static void
19308 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19309 {
19310   HOST_WIDE_INT off;
19311   tree com_decl;
19312   tree decl_or_origin = decl ? decl : origin;
19313   tree ultimate_origin;
19314   dw_die_ref var_die;
19315   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19316   dw_die_ref origin_die;
19317   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19318                       || class_or_namespace_scope_p (context_die));
19319   bool specialization_p = false;
19320
19321   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19322   if (decl || ultimate_origin)
19323     origin = ultimate_origin;
19324   com_decl = fortran_common (decl_or_origin, &off);
19325
19326   /* Symbol in common gets emitted as a child of the common block, in the form
19327      of a data member.  */
19328   if (com_decl)
19329     {
19330       dw_die_ref com_die;
19331       dw_loc_list_ref loc;
19332       die_node com_die_arg;
19333
19334       var_die = lookup_decl_die (decl_or_origin);
19335       if (var_die)
19336         {
19337           if (get_AT (var_die, DW_AT_location) == NULL)
19338             {
19339               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19340               if (loc)
19341                 {
19342                   if (off)
19343                     {
19344                       /* Optimize the common case.  */
19345                       if (single_element_loc_list_p (loc)
19346                           && loc->expr->dw_loc_opc == DW_OP_addr
19347                           && loc->expr->dw_loc_next == NULL
19348                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19349                              == SYMBOL_REF)
19350                         loc->expr->dw_loc_oprnd1.v.val_addr
19351                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19352                         else
19353                           loc_list_plus_const (loc, off);
19354                     }
19355                   add_AT_location_description (var_die, DW_AT_location, loc);
19356                   remove_AT (var_die, DW_AT_declaration);
19357                 }
19358             }
19359           return;
19360         }
19361
19362       if (common_block_die_table == NULL)
19363         common_block_die_table
19364           = htab_create_ggc (10, common_block_die_table_hash,
19365                              common_block_die_table_eq, NULL);
19366
19367       com_die_arg.decl_id = DECL_UID (com_decl);
19368       com_die_arg.die_parent = context_die;
19369       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19370       loc = loc_list_from_tree (com_decl, 2);
19371       if (com_die == NULL)
19372         {
19373           const char *cnam
19374             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19375           void **slot;
19376
19377           com_die = new_die (DW_TAG_common_block, context_die, decl);
19378           add_name_and_src_coords_attributes (com_die, com_decl);
19379           if (loc)
19380             {
19381               add_AT_location_description (com_die, DW_AT_location, loc);
19382               /* Avoid sharing the same loc descriptor between
19383                  DW_TAG_common_block and DW_TAG_variable.  */
19384               loc = loc_list_from_tree (com_decl, 2);
19385             }
19386           else if (DECL_EXTERNAL (decl))
19387             add_AT_flag (com_die, DW_AT_declaration, 1);
19388           add_pubname_string (cnam, com_die); /* ??? needed? */
19389           com_die->decl_id = DECL_UID (com_decl);
19390           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19391           *slot = (void *) com_die;
19392         }
19393       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19394         {
19395           add_AT_location_description (com_die, DW_AT_location, loc);
19396           loc = loc_list_from_tree (com_decl, 2);
19397           remove_AT (com_die, DW_AT_declaration);
19398         }
19399       var_die = new_die (DW_TAG_variable, com_die, decl);
19400       add_name_and_src_coords_attributes (var_die, decl);
19401       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19402                           TREE_THIS_VOLATILE (decl), context_die);
19403       add_AT_flag (var_die, DW_AT_external, 1);
19404       if (loc)
19405         {
19406           if (off)
19407             {
19408               /* Optimize the common case.  */
19409               if (single_element_loc_list_p (loc)
19410                   && loc->expr->dw_loc_opc == DW_OP_addr
19411                   && loc->expr->dw_loc_next == NULL
19412                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19413                 loc->expr->dw_loc_oprnd1.v.val_addr
19414                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19415               else
19416                 loc_list_plus_const (loc, off);
19417             }
19418           add_AT_location_description (var_die, DW_AT_location, loc);
19419         }
19420       else if (DECL_EXTERNAL (decl))
19421         add_AT_flag (var_die, DW_AT_declaration, 1);
19422       equate_decl_number_to_die (decl, var_die);
19423       return;
19424     }
19425
19426   /* If the compiler emitted a definition for the DECL declaration
19427      and if we already emitted a DIE for it, don't emit a second
19428      DIE for it again. Allow re-declarations of DECLs that are
19429      inside functions, though.  */
19430   if (old_die && declaration && !local_scope_p (context_die))
19431     return;
19432
19433   /* For static data members, the declaration in the class is supposed
19434      to have DW_TAG_member tag; the specification should still be
19435      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19436   if (declaration && class_scope_p (context_die))
19437     var_die = new_die (DW_TAG_member, context_die, decl);
19438   else
19439     var_die = new_die (DW_TAG_variable, context_die, decl);
19440
19441   origin_die = NULL;
19442   if (origin != NULL)
19443     origin_die = add_abstract_origin_attribute (var_die, origin);
19444
19445   /* Loop unrolling can create multiple blocks that refer to the same
19446      static variable, so we must test for the DW_AT_declaration flag.
19447
19448      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19449      copy decls and set the DECL_ABSTRACT flag on them instead of
19450      sharing them.
19451
19452      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19453
19454      ??? The declare_in_namespace support causes us to get two DIEs for one
19455      variable, both of which are declarations.  We want to avoid considering
19456      one to be a specification, so we must test that this DIE is not a
19457      declaration.  */
19458   else if (old_die && TREE_STATIC (decl) && ! declaration
19459            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19460     {
19461       /* This is a definition of a C++ class level static.  */
19462       add_AT_specification (var_die, old_die);
19463       specialization_p = true;
19464       if (DECL_NAME (decl))
19465         {
19466           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19467           struct dwarf_file_data * file_index = lookup_filename (s.file);
19468
19469           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19470             add_AT_file (var_die, DW_AT_decl_file, file_index);
19471
19472           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19473             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19474
19475           if (old_die->die_tag == DW_TAG_member)
19476             add_linkage_name (var_die, decl);
19477         }
19478     }
19479   else
19480     add_name_and_src_coords_attributes (var_die, decl);
19481
19482   if ((origin == NULL && !specialization_p)
19483       || (origin != NULL
19484           && !DECL_ABSTRACT (decl_or_origin)
19485           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19486                                        decl_function_context
19487                                                         (decl_or_origin))))
19488     {
19489       tree type = TREE_TYPE (decl_or_origin);
19490
19491       if (decl_by_reference_p (decl_or_origin))
19492         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19493       else
19494         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19495                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19496     }
19497
19498   if (origin == NULL && !specialization_p)
19499     {
19500       if (TREE_PUBLIC (decl))
19501         add_AT_flag (var_die, DW_AT_external, 1);
19502
19503       if (DECL_ARTIFICIAL (decl))
19504         add_AT_flag (var_die, DW_AT_artificial, 1);
19505
19506       add_accessibility_attribute (var_die, decl);
19507     }
19508
19509   if (declaration)
19510     add_AT_flag (var_die, DW_AT_declaration, 1);
19511
19512   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19513     equate_decl_number_to_die (decl, var_die);
19514
19515   if (! declaration
19516       && (! DECL_ABSTRACT (decl_or_origin)
19517           /* Local static vars are shared between all clones/inlines,
19518              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19519              already set.  */
19520           || (TREE_CODE (decl_or_origin) == VAR_DECL
19521               && TREE_STATIC (decl_or_origin)
19522               && DECL_RTL_SET_P (decl_or_origin)))
19523       /* When abstract origin already has DW_AT_location attribute, no need
19524          to add it again.  */
19525       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19526     {
19527       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19528           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19529         defer_location (decl_or_origin, var_die);
19530       else
19531         add_location_or_const_value_attribute (var_die,
19532                                                decl_or_origin,
19533                                                DW_AT_location);
19534       add_pubname (decl_or_origin, var_die);
19535     }
19536   else
19537     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19538 }
19539
19540 /* Generate a DIE to represent a named constant.  */
19541
19542 static void
19543 gen_const_die (tree decl, dw_die_ref context_die)
19544 {
19545   dw_die_ref const_die;
19546   tree type = TREE_TYPE (decl);
19547
19548   const_die = new_die (DW_TAG_constant, context_die, decl);
19549   add_name_and_src_coords_attributes (const_die, decl);
19550   add_type_attribute (const_die, type, 1, 0, context_die);
19551   if (TREE_PUBLIC (decl))
19552     add_AT_flag (const_die, DW_AT_external, 1);
19553   if (DECL_ARTIFICIAL (decl))
19554     add_AT_flag (const_die, DW_AT_artificial, 1);
19555   tree_add_const_value_attribute_for_decl (const_die, decl);
19556 }
19557
19558 /* Generate a DIE to represent a label identifier.  */
19559
19560 static void
19561 gen_label_die (tree decl, dw_die_ref context_die)
19562 {
19563   tree origin = decl_ultimate_origin (decl);
19564   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19565   rtx insn;
19566   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19567
19568   if (origin != NULL)
19569     add_abstract_origin_attribute (lbl_die, origin);
19570   else
19571     add_name_and_src_coords_attributes (lbl_die, decl);
19572
19573   if (DECL_ABSTRACT (decl))
19574     equate_decl_number_to_die (decl, lbl_die);
19575   else
19576     {
19577       insn = DECL_RTL_IF_SET (decl);
19578
19579       /* Deleted labels are programmer specified labels which have been
19580          eliminated because of various optimizations.  We still emit them
19581          here so that it is possible to put breakpoints on them.  */
19582       if (insn
19583           && (LABEL_P (insn)
19584               || ((NOTE_P (insn)
19585                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19586         {
19587           /* When optimization is enabled (via -O) some parts of the compiler
19588              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19589              represent source-level labels which were explicitly declared by
19590              the user.  This really shouldn't be happening though, so catch
19591              it if it ever does happen.  */
19592           gcc_assert (!INSN_DELETED_P (insn));
19593
19594           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19595           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19596         }
19597     }
19598 }
19599
19600 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19601    attributes to the DIE for a block STMT, to describe where the inlined
19602    function was called from.  This is similar to add_src_coords_attributes.  */
19603
19604 static inline void
19605 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19606 {
19607   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19608
19609   if (dwarf_version >= 3 || !dwarf_strict)
19610     {
19611       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19612       add_AT_unsigned (die, DW_AT_call_line, s.line);
19613     }
19614 }
19615
19616
19617 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19618    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19619
19620 static inline void
19621 add_high_low_attributes (tree stmt, dw_die_ref die)
19622 {
19623   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19624
19625   if (BLOCK_FRAGMENT_CHAIN (stmt)
19626       && (dwarf_version >= 3 || !dwarf_strict))
19627     {
19628       tree chain;
19629
19630       if (inlined_function_outer_scope_p (stmt))
19631         {
19632           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19633                                        BLOCK_NUMBER (stmt));
19634           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19635         }
19636
19637       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19638
19639       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19640       do
19641         {
19642           add_ranges (chain);
19643           chain = BLOCK_FRAGMENT_CHAIN (chain);
19644         }
19645       while (chain);
19646       add_ranges (NULL);
19647     }
19648   else
19649     {
19650       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19651                                    BLOCK_NUMBER (stmt));
19652       add_AT_lbl_id (die, DW_AT_low_pc, label);
19653       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19654                                    BLOCK_NUMBER (stmt));
19655       add_AT_lbl_id (die, DW_AT_high_pc, label);
19656     }
19657 }
19658
19659 /* Generate a DIE for a lexical block.  */
19660
19661 static void
19662 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19663 {
19664   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19665
19666   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19667     add_high_low_attributes (stmt, stmt_die);
19668
19669   decls_for_scope (stmt, stmt_die, depth);
19670 }
19671
19672 /* Generate a DIE for an inlined subprogram.  */
19673
19674 static void
19675 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19676 {
19677   tree decl;
19678
19679   /* The instance of function that is effectively being inlined shall not
19680      be abstract.  */
19681   gcc_assert (! BLOCK_ABSTRACT (stmt));
19682
19683   decl = block_ultimate_origin (stmt);
19684
19685   /* Emit info for the abstract instance first, if we haven't yet.  We
19686      must emit this even if the block is abstract, otherwise when we
19687      emit the block below (or elsewhere), we may end up trying to emit
19688      a die whose origin die hasn't been emitted, and crashing.  */
19689   dwarf2out_abstract_function (decl);
19690
19691   if (! BLOCK_ABSTRACT (stmt))
19692     {
19693       dw_die_ref subr_die
19694         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19695
19696       add_abstract_origin_attribute (subr_die, decl);
19697       if (TREE_ASM_WRITTEN (stmt))
19698         add_high_low_attributes (stmt, subr_die);
19699       add_call_src_coords_attributes (stmt, subr_die);
19700
19701       decls_for_scope (stmt, subr_die, depth);
19702       current_function_has_inlines = 1;
19703     }
19704 }
19705
19706 /* Generate a DIE for a field in a record, or structure.  */
19707
19708 static void
19709 gen_field_die (tree decl, dw_die_ref context_die)
19710 {
19711   dw_die_ref decl_die;
19712
19713   if (TREE_TYPE (decl) == error_mark_node)
19714     return;
19715
19716   decl_die = new_die (DW_TAG_member, context_die, decl);
19717   add_name_and_src_coords_attributes (decl_die, decl);
19718   add_type_attribute (decl_die, member_declared_type (decl),
19719                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19720                       context_die);
19721
19722   if (DECL_BIT_FIELD_TYPE (decl))
19723     {
19724       add_byte_size_attribute (decl_die, decl);
19725       add_bit_size_attribute (decl_die, decl);
19726       add_bit_offset_attribute (decl_die, decl);
19727     }
19728
19729   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19730     add_data_member_location_attribute (decl_die, decl);
19731
19732   if (DECL_ARTIFICIAL (decl))
19733     add_AT_flag (decl_die, DW_AT_artificial, 1);
19734
19735   add_accessibility_attribute (decl_die, decl);
19736
19737   /* Equate decl number to die, so that we can look up this decl later on.  */
19738   equate_decl_number_to_die (decl, decl_die);
19739 }
19740
19741 #if 0
19742 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19743    Use modified_type_die instead.
19744    We keep this code here just in case these types of DIEs may be needed to
19745    represent certain things in other languages (e.g. Pascal) someday.  */
19746
19747 static void
19748 gen_pointer_type_die (tree type, dw_die_ref context_die)
19749 {
19750   dw_die_ref ptr_die
19751     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19752
19753   equate_type_number_to_die (type, ptr_die);
19754   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19755   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19756 }
19757
19758 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19759    Use modified_type_die instead.
19760    We keep this code here just in case these types of DIEs may be needed to
19761    represent certain things in other languages (e.g. Pascal) someday.  */
19762
19763 static void
19764 gen_reference_type_die (tree type, dw_die_ref context_die)
19765 {
19766   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19767
19768   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19769     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19770   else
19771     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19772
19773   equate_type_number_to_die (type, ref_die);
19774   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19775   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19776 }
19777 #endif
19778
19779 /* Generate a DIE for a pointer to a member type.  */
19780
19781 static void
19782 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19783 {
19784   dw_die_ref ptr_die
19785     = new_die (DW_TAG_ptr_to_member_type,
19786                scope_die_for (type, context_die), type);
19787
19788   equate_type_number_to_die (type, ptr_die);
19789   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19790                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19791   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19792 }
19793
19794 /* Generate the DIE for the compilation unit.  */
19795
19796 static dw_die_ref
19797 gen_compile_unit_die (const char *filename)
19798 {
19799   dw_die_ref die;
19800   char producer[250];
19801   const char *language_string = lang_hooks.name;
19802   int language;
19803
19804   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19805
19806   if (filename)
19807     {
19808       add_name_attribute (die, filename);
19809       /* Don't add cwd for <built-in>.  */
19810       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19811         add_comp_dir_attribute (die);
19812     }
19813
19814   sprintf (producer, "%s %s", language_string, version_string);
19815
19816 #ifdef MIPS_DEBUGGING_INFO
19817   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19818      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19819      not appear in the producer string, the debugger reaches the conclusion
19820      that the object file is stripped and has no debugging information.
19821      To get the MIPS/SGI debugger to believe that there is debugging
19822      information in the object file, we add a -g to the producer string.  */
19823   if (debug_info_level > DINFO_LEVEL_TERSE)
19824     strcat (producer, " -g");
19825 #endif
19826
19827   add_AT_string (die, DW_AT_producer, producer);
19828
19829   /* If our producer is LTO try to figure out a common language to use
19830      from the global list of translation units.  */
19831   if (strcmp (language_string, "GNU GIMPLE") == 0)
19832     {
19833       unsigned i;
19834       tree t;
19835       const char *common_lang = NULL;
19836
19837       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
19838         {
19839           if (!TRANSLATION_UNIT_LANGUAGE (t))
19840             continue;
19841           if (!common_lang)
19842             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
19843           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
19844             ;
19845           else if (strncmp (common_lang, "GNU C", 5) == 0
19846                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
19847             /* Mixing C and C++ is ok, use C++ in that case.  */
19848             common_lang = "GNU C++";
19849           else
19850             {
19851               /* Fall back to C.  */
19852               common_lang = NULL;
19853               break;
19854             }
19855         }
19856
19857       if (common_lang)
19858         language_string = common_lang;
19859     }
19860
19861   language = DW_LANG_C89;
19862   if (strcmp (language_string, "GNU C++") == 0)
19863     language = DW_LANG_C_plus_plus;
19864   else if (strcmp (language_string, "GNU F77") == 0)
19865     language = DW_LANG_Fortran77;
19866   else if (strcmp (language_string, "GNU Pascal") == 0)
19867     language = DW_LANG_Pascal83;
19868   else if (dwarf_version >= 3 || !dwarf_strict)
19869     {
19870       if (strcmp (language_string, "GNU Ada") == 0)
19871         language = DW_LANG_Ada95;
19872       else if (strcmp (language_string, "GNU Fortran") == 0)
19873         language = DW_LANG_Fortran95;
19874       else if (strcmp (language_string, "GNU Java") == 0)
19875         language = DW_LANG_Java;
19876       else if (strcmp (language_string, "GNU Objective-C") == 0)
19877         language = DW_LANG_ObjC;
19878       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19879         language = DW_LANG_ObjC_plus_plus;
19880     }
19881
19882   add_AT_unsigned (die, DW_AT_language, language);
19883
19884   switch (language)
19885     {
19886     case DW_LANG_Fortran77:
19887     case DW_LANG_Fortran90:
19888     case DW_LANG_Fortran95:
19889       /* Fortran has case insensitive identifiers and the front-end
19890          lowercases everything.  */
19891       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19892       break;
19893     default:
19894       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19895       break;
19896     }
19897   return die;
19898 }
19899
19900 /* Generate the DIE for a base class.  */
19901
19902 static void
19903 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19904 {
19905   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19906
19907   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19908   add_data_member_location_attribute (die, binfo);
19909
19910   if (BINFO_VIRTUAL_P (binfo))
19911     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19912
19913   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
19914      children, otherwise the default is DW_ACCESS_public.  In DWARF2
19915      the default has always been DW_ACCESS_private.  */
19916   if (access == access_public_node)
19917     {
19918       if (dwarf_version == 2
19919           || context_die->die_tag == DW_TAG_class_type)
19920       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19921     }
19922   else if (access == access_protected_node)
19923     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19924   else if (dwarf_version > 2
19925            && context_die->die_tag != DW_TAG_class_type)
19926     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
19927 }
19928
19929 /* Generate a DIE for a class member.  */
19930
19931 static void
19932 gen_member_die (tree type, dw_die_ref context_die)
19933 {
19934   tree member;
19935   tree binfo = TYPE_BINFO (type);
19936   dw_die_ref child;
19937
19938   /* If this is not an incomplete type, output descriptions of each of its
19939      members. Note that as we output the DIEs necessary to represent the
19940      members of this record or union type, we will also be trying to output
19941      DIEs to represent the *types* of those members. However the `type'
19942      function (above) will specifically avoid generating type DIEs for member
19943      types *within* the list of member DIEs for this (containing) type except
19944      for those types (of members) which are explicitly marked as also being
19945      members of this (containing) type themselves.  The g++ front- end can
19946      force any given type to be treated as a member of some other (containing)
19947      type by setting the TYPE_CONTEXT of the given (member) type to point to
19948      the TREE node representing the appropriate (containing) type.  */
19949
19950   /* First output info about the base classes.  */
19951   if (binfo)
19952     {
19953       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19954       int i;
19955       tree base;
19956
19957       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19958         gen_inheritance_die (base,
19959                              (accesses ? VEC_index (tree, accesses, i)
19960                               : access_public_node), context_die);
19961     }
19962
19963   /* Now output info about the data members and type members.  */
19964   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19965     {
19966       /* If we thought we were generating minimal debug info for TYPE
19967          and then changed our minds, some of the member declarations
19968          may have already been defined.  Don't define them again, but
19969          do put them in the right order.  */
19970
19971       child = lookup_decl_die (member);
19972       if (child)
19973         splice_child_die (context_die, child);
19974       else
19975         gen_decl_die (member, NULL, context_die);
19976     }
19977
19978   /* Now output info about the function members (if any).  */
19979   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19980     {
19981       /* Don't include clones in the member list.  */
19982       if (DECL_ABSTRACT_ORIGIN (member))
19983         continue;
19984
19985       child = lookup_decl_die (member);
19986       if (child)
19987         splice_child_die (context_die, child);
19988       else
19989         gen_decl_die (member, NULL, context_die);
19990     }
19991 }
19992
19993 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19994    is set, we pretend that the type was never defined, so we only get the
19995    member DIEs needed by later specification DIEs.  */
19996
19997 static void
19998 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19999                                 enum debug_info_usage usage)
20000 {
20001   dw_die_ref type_die = lookup_type_die (type);
20002   dw_die_ref scope_die = 0;
20003   int nested = 0;
20004   int complete = (TYPE_SIZE (type)
20005                   && (! TYPE_STUB_DECL (type)
20006                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20007   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20008   complete = complete && should_emit_struct_debug (type, usage);
20009
20010   if (type_die && ! complete)
20011     return;
20012
20013   if (TYPE_CONTEXT (type) != NULL_TREE
20014       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20015           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20016     nested = 1;
20017
20018   scope_die = scope_die_for (type, context_die);
20019
20020   if (! type_die || (nested && is_cu_die (scope_die)))
20021     /* First occurrence of type or toplevel definition of nested class.  */
20022     {
20023       dw_die_ref old_die = type_die;
20024
20025       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20026                           ? record_type_tag (type) : DW_TAG_union_type,
20027                           scope_die, type);
20028       equate_type_number_to_die (type, type_die);
20029       if (old_die)
20030         add_AT_specification (type_die, old_die);
20031       else
20032         add_name_attribute (type_die, type_tag (type));
20033     }
20034   else
20035     remove_AT (type_die, DW_AT_declaration);
20036
20037   /* Generate child dies for template paramaters.  */
20038   if (debug_info_level > DINFO_LEVEL_TERSE
20039       && COMPLETE_TYPE_P (type))
20040     gen_generic_params_dies (type);
20041
20042   /* If this type has been completed, then give it a byte_size attribute and
20043      then give a list of members.  */
20044   if (complete && !ns_decl)
20045     {
20046       /* Prevent infinite recursion in cases where the type of some member of
20047          this type is expressed in terms of this type itself.  */
20048       TREE_ASM_WRITTEN (type) = 1;
20049       add_byte_size_attribute (type_die, type);
20050       if (TYPE_STUB_DECL (type) != NULL_TREE)
20051         {
20052           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20053           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20054         }
20055
20056       /* If the first reference to this type was as the return type of an
20057          inline function, then it may not have a parent.  Fix this now.  */
20058       if (type_die->die_parent == NULL)
20059         add_child_die (scope_die, type_die);
20060
20061       push_decl_scope (type);
20062       gen_member_die (type, type_die);
20063       pop_decl_scope ();
20064
20065       /* GNU extension: Record what type our vtable lives in.  */
20066       if (TYPE_VFIELD (type))
20067         {
20068           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20069
20070           gen_type_die (vtype, context_die);
20071           add_AT_die_ref (type_die, DW_AT_containing_type,
20072                           lookup_type_die (vtype));
20073         }
20074     }
20075   else
20076     {
20077       add_AT_flag (type_die, DW_AT_declaration, 1);
20078
20079       /* We don't need to do this for function-local types.  */
20080       if (TYPE_STUB_DECL (type)
20081           && ! decl_function_context (TYPE_STUB_DECL (type)))
20082         VEC_safe_push (tree, gc, incomplete_types, type);
20083     }
20084
20085   if (get_AT (type_die, DW_AT_name))
20086     add_pubtype (type, type_die);
20087 }
20088
20089 /* Generate a DIE for a subroutine _type_.  */
20090
20091 static void
20092 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20093 {
20094   tree return_type = TREE_TYPE (type);
20095   dw_die_ref subr_die
20096     = new_die (DW_TAG_subroutine_type,
20097                scope_die_for (type, context_die), type);
20098
20099   equate_type_number_to_die (type, subr_die);
20100   add_prototyped_attribute (subr_die, type);
20101   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20102   gen_formal_types_die (type, subr_die);
20103
20104   if (get_AT (subr_die, DW_AT_name))
20105     add_pubtype (type, subr_die);
20106 }
20107
20108 /* Generate a DIE for a type definition.  */
20109
20110 static void
20111 gen_typedef_die (tree decl, dw_die_ref context_die)
20112 {
20113   dw_die_ref type_die;
20114   tree origin;
20115
20116   if (TREE_ASM_WRITTEN (decl))
20117     return;
20118
20119   TREE_ASM_WRITTEN (decl) = 1;
20120   type_die = new_die (DW_TAG_typedef, context_die, decl);
20121   origin = decl_ultimate_origin (decl);
20122   if (origin != NULL)
20123     add_abstract_origin_attribute (type_die, origin);
20124   else
20125     {
20126       tree type;
20127
20128       add_name_and_src_coords_attributes (type_die, decl);
20129       if (DECL_ORIGINAL_TYPE (decl))
20130         {
20131           type = DECL_ORIGINAL_TYPE (decl);
20132
20133           gcc_assert (type != TREE_TYPE (decl));
20134           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20135         }
20136       else
20137         {
20138           type = TREE_TYPE (decl);
20139
20140           if (is_naming_typedef_decl (TYPE_NAME (type)))
20141             {
20142               /* Here, we are in the case of decl being a typedef naming
20143                  an anonymous type, e.g:
20144                      typedef struct {...} foo;
20145                  In that case TREE_TYPE (decl) is not a typedef variant
20146                  type and TYPE_NAME of the anonymous type is set to the
20147                  TYPE_DECL of the typedef. This construct is emitted by
20148                  the C++ FE.
20149
20150                  TYPE is the anonymous struct named by the typedef
20151                  DECL. As we need the DW_AT_type attribute of the
20152                  DW_TAG_typedef to point to the DIE of TYPE, let's
20153                  generate that DIE right away. add_type_attribute
20154                  called below will then pick (via lookup_type_die) that
20155                  anonymous struct DIE.  */
20156               if (!TREE_ASM_WRITTEN (type))
20157                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20158             }
20159         }
20160
20161       add_type_attribute (type_die, type, TREE_READONLY (decl),
20162                           TREE_THIS_VOLATILE (decl), context_die);
20163
20164       if (is_naming_typedef_decl (decl))
20165         /* We want that all subsequent calls to lookup_type_die with
20166            TYPE in argument yield the DW_TAG_typedef we have just
20167            created.  */
20168         equate_type_number_to_die (type, type_die);
20169
20170       add_accessibility_attribute (type_die, decl);
20171     }
20172
20173   if (DECL_ABSTRACT (decl))
20174     equate_decl_number_to_die (decl, type_die);
20175
20176   if (get_AT (type_die, DW_AT_name))
20177     add_pubtype (decl, type_die);
20178 }
20179
20180 /* Generate a DIE for a struct, class, enum or union type.  */
20181
20182 static void
20183 gen_tagged_type_die (tree type,
20184                      dw_die_ref context_die,
20185                      enum debug_info_usage usage)
20186 {
20187   int need_pop;
20188
20189   if (type == NULL_TREE
20190       || !is_tagged_type (type))
20191     return;
20192
20193   /* If this is a nested type whose containing class hasn't been written
20194      out yet, writing it out will cover this one, too.  This does not apply
20195      to instantiations of member class templates; they need to be added to
20196      the containing class as they are generated.  FIXME: This hurts the
20197      idea of combining type decls from multiple TUs, since we can't predict
20198      what set of template instantiations we'll get.  */
20199   if (TYPE_CONTEXT (type)
20200       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20201       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20202     {
20203       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20204
20205       if (TREE_ASM_WRITTEN (type))
20206         return;
20207
20208       /* If that failed, attach ourselves to the stub.  */
20209       push_decl_scope (TYPE_CONTEXT (type));
20210       context_die = lookup_type_die (TYPE_CONTEXT (type));
20211       need_pop = 1;
20212     }
20213   else if (TYPE_CONTEXT (type) != NULL_TREE
20214            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20215     {
20216       /* If this type is local to a function that hasn't been written
20217          out yet, use a NULL context for now; it will be fixed up in
20218          decls_for_scope.  */
20219       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20220       /* A declaration DIE doesn't count; nested types need to go in the
20221          specification.  */
20222       if (context_die && is_declaration_die (context_die))
20223         context_die = NULL;
20224       need_pop = 0;
20225     }
20226   else
20227     {
20228       context_die = declare_in_namespace (type, context_die);
20229       need_pop = 0;
20230     }
20231
20232   if (TREE_CODE (type) == ENUMERAL_TYPE)
20233     {
20234       /* This might have been written out by the call to
20235          declare_in_namespace.  */
20236       if (!TREE_ASM_WRITTEN (type))
20237         gen_enumeration_type_die (type, context_die);
20238     }
20239   else
20240     gen_struct_or_union_type_die (type, context_die, usage);
20241
20242   if (need_pop)
20243     pop_decl_scope ();
20244
20245   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20246      it up if it is ever completed.  gen_*_type_die will set it for us
20247      when appropriate.  */
20248 }
20249
20250 /* Generate a type description DIE.  */
20251
20252 static void
20253 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20254                          enum debug_info_usage usage)
20255 {
20256   struct array_descr_info info;
20257
20258   if (type == NULL_TREE || type == error_mark_node)
20259     return;
20260
20261   if (TYPE_NAME (type) != NULL_TREE
20262       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20263       && is_redundant_typedef (TYPE_NAME (type))
20264       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20265     /* The DECL of this type is a typedef we don't want to emit debug
20266        info for but we want debug info for its underlying typedef.
20267        This can happen for e.g, the injected-class-name of a C++
20268        type.  */
20269     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20270
20271   /* If TYPE is a typedef type variant, let's generate debug info
20272      for the parent typedef which TYPE is a type of.  */
20273   if (typedef_variant_p (type))
20274     {
20275       if (TREE_ASM_WRITTEN (type))
20276         return;
20277
20278       /* Prevent broken recursion; we can't hand off to the same type.  */
20279       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20280
20281       /* Use the DIE of the containing namespace as the parent DIE of
20282          the type description DIE we want to generate.  */
20283       if (DECL_CONTEXT (TYPE_NAME (type))
20284           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20285         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20286
20287       TREE_ASM_WRITTEN (type) = 1;
20288
20289       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20290       return;
20291     }
20292
20293   /* If type is an anonymous tagged type named by a typedef, let's
20294      generate debug info for the typedef.  */
20295   if (is_naming_typedef_decl (TYPE_NAME (type)))
20296     {
20297       /* Use the DIE of the containing namespace as the parent DIE of
20298          the type description DIE we want to generate.  */
20299       if (DECL_CONTEXT (TYPE_NAME (type))
20300           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20301         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20302       
20303       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20304       return;
20305     }
20306
20307   /* If this is an array type with hidden descriptor, handle it first.  */
20308   if (!TREE_ASM_WRITTEN (type)
20309       && lang_hooks.types.get_array_descr_info
20310       && lang_hooks.types.get_array_descr_info (type, &info)
20311       && (dwarf_version >= 3 || !dwarf_strict))
20312     {
20313       gen_descr_array_type_die (type, &info, context_die);
20314       TREE_ASM_WRITTEN (type) = 1;
20315       return;
20316     }
20317
20318   /* We are going to output a DIE to represent the unqualified version
20319      of this type (i.e. without any const or volatile qualifiers) so
20320      get the main variant (i.e. the unqualified version) of this type
20321      now.  (Vectors are special because the debugging info is in the
20322      cloned type itself).  */
20323   if (TREE_CODE (type) != VECTOR_TYPE)
20324     type = type_main_variant (type);
20325
20326   if (TREE_ASM_WRITTEN (type))
20327     return;
20328
20329   switch (TREE_CODE (type))
20330     {
20331     case ERROR_MARK:
20332       break;
20333
20334     case POINTER_TYPE:
20335     case REFERENCE_TYPE:
20336       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20337          ensures that the gen_type_die recursion will terminate even if the
20338          type is recursive.  Recursive types are possible in Ada.  */
20339       /* ??? We could perhaps do this for all types before the switch
20340          statement.  */
20341       TREE_ASM_WRITTEN (type) = 1;
20342
20343       /* For these types, all that is required is that we output a DIE (or a
20344          set of DIEs) to represent the "basis" type.  */
20345       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20346                                 DINFO_USAGE_IND_USE);
20347       break;
20348
20349     case OFFSET_TYPE:
20350       /* This code is used for C++ pointer-to-data-member types.
20351          Output a description of the relevant class type.  */
20352       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20353                                         DINFO_USAGE_IND_USE);
20354
20355       /* Output a description of the type of the object pointed to.  */
20356       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20357                                         DINFO_USAGE_IND_USE);
20358
20359       /* Now output a DIE to represent this pointer-to-data-member type
20360          itself.  */
20361       gen_ptr_to_mbr_type_die (type, context_die);
20362       break;
20363
20364     case FUNCTION_TYPE:
20365       /* Force out return type (in case it wasn't forced out already).  */
20366       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20367                                         DINFO_USAGE_DIR_USE);
20368       gen_subroutine_type_die (type, context_die);
20369       break;
20370
20371     case METHOD_TYPE:
20372       /* Force out return type (in case it wasn't forced out already).  */
20373       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20374                                         DINFO_USAGE_DIR_USE);
20375       gen_subroutine_type_die (type, context_die);
20376       break;
20377
20378     case ARRAY_TYPE:
20379       gen_array_type_die (type, context_die);
20380       break;
20381
20382     case VECTOR_TYPE:
20383       gen_array_type_die (type, context_die);
20384       break;
20385
20386     case ENUMERAL_TYPE:
20387     case RECORD_TYPE:
20388     case UNION_TYPE:
20389     case QUAL_UNION_TYPE:
20390       gen_tagged_type_die (type, context_die, usage);
20391       return;
20392
20393     case VOID_TYPE:
20394     case INTEGER_TYPE:
20395     case REAL_TYPE:
20396     case FIXED_POINT_TYPE:
20397     case COMPLEX_TYPE:
20398     case BOOLEAN_TYPE:
20399       /* No DIEs needed for fundamental types.  */
20400       break;
20401
20402     case NULLPTR_TYPE:
20403     case LANG_TYPE:
20404       /* Just use DW_TAG_unspecified_type.  */
20405       {
20406         dw_die_ref type_die = lookup_type_die (type);
20407         if (type_die == NULL)
20408           {
20409             tree name = TYPE_NAME (type);
20410             if (TREE_CODE (name) == TYPE_DECL)
20411               name = DECL_NAME (name);
20412             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20413             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20414             equate_type_number_to_die (type, type_die);
20415           }
20416       }
20417       break;
20418
20419     default:
20420       gcc_unreachable ();
20421     }
20422
20423   TREE_ASM_WRITTEN (type) = 1;
20424 }
20425
20426 static void
20427 gen_type_die (tree type, dw_die_ref context_die)
20428 {
20429   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20430 }
20431
20432 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20433    things which are local to the given block.  */
20434
20435 static void
20436 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20437 {
20438   int must_output_die = 0;
20439   bool inlined_func;
20440
20441   /* Ignore blocks that are NULL.  */
20442   if (stmt == NULL_TREE)
20443     return;
20444
20445   inlined_func = inlined_function_outer_scope_p (stmt);
20446
20447   /* If the block is one fragment of a non-contiguous block, do not
20448      process the variables, since they will have been done by the
20449      origin block.  Do process subblocks.  */
20450   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20451     {
20452       tree sub;
20453
20454       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20455         gen_block_die (sub, context_die, depth + 1);
20456
20457       return;
20458     }
20459
20460   /* Determine if we need to output any Dwarf DIEs at all to represent this
20461      block.  */
20462   if (inlined_func)
20463     /* The outer scopes for inlinings *must* always be represented.  We
20464        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20465     must_output_die = 1;
20466   else
20467     {
20468       /* Determine if this block directly contains any "significant"
20469          local declarations which we will need to output DIEs for.  */
20470       if (debug_info_level > DINFO_LEVEL_TERSE)
20471         /* We are not in terse mode so *any* local declaration counts
20472            as being a "significant" one.  */
20473         must_output_die = ((BLOCK_VARS (stmt) != NULL
20474                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20475                            && (TREE_USED (stmt)
20476                                || TREE_ASM_WRITTEN (stmt)
20477                                || BLOCK_ABSTRACT (stmt)));
20478       else if ((TREE_USED (stmt)
20479                 || TREE_ASM_WRITTEN (stmt)
20480                 || BLOCK_ABSTRACT (stmt))
20481                && !dwarf2out_ignore_block (stmt))
20482         must_output_die = 1;
20483     }
20484
20485   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20486      DIE for any block which contains no significant local declarations at
20487      all.  Rather, in such cases we just call `decls_for_scope' so that any
20488      needed Dwarf info for any sub-blocks will get properly generated. Note
20489      that in terse mode, our definition of what constitutes a "significant"
20490      local declaration gets restricted to include only inlined function
20491      instances and local (nested) function definitions.  */
20492   if (must_output_die)
20493     {
20494       if (inlined_func)
20495         {
20496           /* If STMT block is abstract, that means we have been called
20497              indirectly from dwarf2out_abstract_function.
20498              That function rightfully marks the descendent blocks (of
20499              the abstract function it is dealing with) as being abstract,
20500              precisely to prevent us from emitting any
20501              DW_TAG_inlined_subroutine DIE as a descendent
20502              of an abstract function instance. So in that case, we should
20503              not call gen_inlined_subroutine_die.
20504
20505              Later though, when cgraph asks dwarf2out to emit info
20506              for the concrete instance of the function decl into which
20507              the concrete instance of STMT got inlined, the later will lead
20508              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20509           if (! BLOCK_ABSTRACT (stmt))
20510             gen_inlined_subroutine_die (stmt, context_die, depth);
20511         }
20512       else
20513         gen_lexical_block_die (stmt, context_die, depth);
20514     }
20515   else
20516     decls_for_scope (stmt, context_die, depth);
20517 }
20518
20519 /* Process variable DECL (or variable with origin ORIGIN) within
20520    block STMT and add it to CONTEXT_DIE.  */
20521 static void
20522 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20523 {
20524   dw_die_ref die;
20525   tree decl_or_origin = decl ? decl : origin;
20526
20527   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20528     die = lookup_decl_die (decl_or_origin);
20529   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20530            && TYPE_DECL_IS_STUB (decl_or_origin))
20531     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20532   else
20533     die = NULL;
20534
20535   if (die != NULL && die->die_parent == NULL)
20536     add_child_die (context_die, die);
20537   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20538     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20539                                          stmt, context_die);
20540   else
20541     gen_decl_die (decl, origin, context_die);
20542 }
20543
20544 /* Generate all of the decls declared within a given scope and (recursively)
20545    all of its sub-blocks.  */
20546
20547 static void
20548 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20549 {
20550   tree decl;
20551   unsigned int i;
20552   tree subblocks;
20553
20554   /* Ignore NULL blocks.  */
20555   if (stmt == NULL_TREE)
20556     return;
20557
20558   /* Output the DIEs to represent all of the data objects and typedefs
20559      declared directly within this block but not within any nested
20560      sub-blocks.  Also, nested function and tag DIEs have been
20561      generated with a parent of NULL; fix that up now.  */
20562   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20563     process_scope_var (stmt, decl, NULL_TREE, context_die);
20564   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20565     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20566                        context_die);
20567
20568   /* If we're at -g1, we're not interested in subblocks.  */
20569   if (debug_info_level <= DINFO_LEVEL_TERSE)
20570     return;
20571
20572   /* Output the DIEs to represent all sub-blocks (and the items declared
20573      therein) of this block.  */
20574   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20575        subblocks != NULL;
20576        subblocks = BLOCK_CHAIN (subblocks))
20577     gen_block_die (subblocks, context_die, depth + 1);
20578 }
20579
20580 /* Is this a typedef we can avoid emitting?  */
20581
20582 static inline int
20583 is_redundant_typedef (const_tree decl)
20584 {
20585   if (TYPE_DECL_IS_STUB (decl))
20586     return 1;
20587
20588   if (DECL_ARTIFICIAL (decl)
20589       && DECL_CONTEXT (decl)
20590       && is_tagged_type (DECL_CONTEXT (decl))
20591       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20592       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20593     /* Also ignore the artificial member typedef for the class name.  */
20594     return 1;
20595
20596   return 0;
20597 }
20598
20599 /* Return TRUE if TYPE is a typedef that names a type for linkage
20600    purposes. This kind of typedefs is produced by the C++ FE for
20601    constructs like:
20602
20603    typedef struct {...} foo;
20604
20605    In that case, there is no typedef variant type produced for foo.
20606    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20607    struct type.  */
20608
20609 static bool
20610 is_naming_typedef_decl (const_tree decl)
20611 {
20612   if (decl == NULL_TREE
20613       || TREE_CODE (decl) != TYPE_DECL
20614       || !is_tagged_type (TREE_TYPE (decl))
20615       || DECL_IS_BUILTIN (decl)
20616       || is_redundant_typedef (decl)
20617       /* It looks like Ada produces TYPE_DECLs that are very similar
20618          to C++ naming typedefs but that have different
20619          semantics. Let's be specific to c++ for now.  */
20620       || !is_cxx ())
20621     return FALSE;
20622
20623   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20624           && TYPE_NAME (TREE_TYPE (decl)) == decl
20625           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20626               != TYPE_NAME (TREE_TYPE (decl))));
20627 }
20628
20629 /* Returns the DIE for a context.  */
20630
20631 static inline dw_die_ref
20632 get_context_die (tree context)
20633 {
20634   if (context)
20635     {
20636       /* Find die that represents this context.  */
20637       if (TYPE_P (context))
20638         return force_type_die (TYPE_MAIN_VARIANT (context));
20639       else
20640         return force_decl_die (context);
20641     }
20642   return comp_unit_die ();
20643 }
20644
20645 /* Returns the DIE for decl.  A DIE will always be returned.  */
20646
20647 static dw_die_ref
20648 force_decl_die (tree decl)
20649 {
20650   dw_die_ref decl_die;
20651   unsigned saved_external_flag;
20652   tree save_fn = NULL_TREE;
20653   decl_die = lookup_decl_die (decl);
20654   if (!decl_die)
20655     {
20656       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20657
20658       decl_die = lookup_decl_die (decl);
20659       if (decl_die)
20660         return decl_die;
20661
20662       switch (TREE_CODE (decl))
20663         {
20664         case FUNCTION_DECL:
20665           /* Clear current_function_decl, so that gen_subprogram_die thinks
20666              that this is a declaration. At this point, we just want to force
20667              declaration die.  */
20668           save_fn = current_function_decl;
20669           current_function_decl = NULL_TREE;
20670           gen_subprogram_die (decl, context_die);
20671           current_function_decl = save_fn;
20672           break;
20673
20674         case VAR_DECL:
20675           /* Set external flag to force declaration die. Restore it after
20676            gen_decl_die() call.  */
20677           saved_external_flag = DECL_EXTERNAL (decl);
20678           DECL_EXTERNAL (decl) = 1;
20679           gen_decl_die (decl, NULL, context_die);
20680           DECL_EXTERNAL (decl) = saved_external_flag;
20681           break;
20682
20683         case NAMESPACE_DECL:
20684           if (dwarf_version >= 3 || !dwarf_strict)
20685             dwarf2out_decl (decl);
20686           else
20687             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20688             decl_die = comp_unit_die ();
20689           break;
20690
20691         case TRANSLATION_UNIT_DECL:
20692           decl_die = comp_unit_die ();
20693           break;
20694
20695         default:
20696           gcc_unreachable ();
20697         }
20698
20699       /* We should be able to find the DIE now.  */
20700       if (!decl_die)
20701         decl_die = lookup_decl_die (decl);
20702       gcc_assert (decl_die);
20703     }
20704
20705   return decl_die;
20706 }
20707
20708 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20709    always returned.  */
20710
20711 static dw_die_ref
20712 force_type_die (tree type)
20713 {
20714   dw_die_ref type_die;
20715
20716   type_die = lookup_type_die (type);
20717   if (!type_die)
20718     {
20719       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20720
20721       type_die = modified_type_die (type, TYPE_READONLY (type),
20722                                     TYPE_VOLATILE (type), context_die);
20723       gcc_assert (type_die);
20724     }
20725   return type_die;
20726 }
20727
20728 /* Force out any required namespaces to be able to output DECL,
20729    and return the new context_die for it, if it's changed.  */
20730
20731 static dw_die_ref
20732 setup_namespace_context (tree thing, dw_die_ref context_die)
20733 {
20734   tree context = (DECL_P (thing)
20735                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20736   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20737     /* Force out the namespace.  */
20738     context_die = force_decl_die (context);
20739
20740   return context_die;
20741 }
20742
20743 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20744    type) within its namespace, if appropriate.
20745
20746    For compatibility with older debuggers, namespace DIEs only contain
20747    declarations; all definitions are emitted at CU scope.  */
20748
20749 static dw_die_ref
20750 declare_in_namespace (tree thing, dw_die_ref context_die)
20751 {
20752   dw_die_ref ns_context;
20753
20754   if (debug_info_level <= DINFO_LEVEL_TERSE)
20755     return context_die;
20756
20757   /* If this decl is from an inlined function, then don't try to emit it in its
20758      namespace, as we will get confused.  It would have already been emitted
20759      when the abstract instance of the inline function was emitted anyways.  */
20760   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20761     return context_die;
20762
20763   ns_context = setup_namespace_context (thing, context_die);
20764
20765   if (ns_context != context_die)
20766     {
20767       if (is_fortran ())
20768         return ns_context;
20769       if (DECL_P (thing))
20770         gen_decl_die (thing, NULL, ns_context);
20771       else
20772         gen_type_die (thing, ns_context);
20773     }
20774   return context_die;
20775 }
20776
20777 /* Generate a DIE for a namespace or namespace alias.  */
20778
20779 static void
20780 gen_namespace_die (tree decl, dw_die_ref context_die)
20781 {
20782   dw_die_ref namespace_die;
20783
20784   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20785      they are an alias of.  */
20786   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20787     {
20788       /* Output a real namespace or module.  */
20789       context_die = setup_namespace_context (decl, comp_unit_die ());
20790       namespace_die = new_die (is_fortran ()
20791                                ? DW_TAG_module : DW_TAG_namespace,
20792                                context_die, decl);
20793       /* For Fortran modules defined in different CU don't add src coords.  */
20794       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20795         {
20796           const char *name = dwarf2_name (decl, 0);
20797           if (name)
20798             add_name_attribute (namespace_die, name);
20799         }
20800       else
20801         add_name_and_src_coords_attributes (namespace_die, decl);
20802       if (DECL_EXTERNAL (decl))
20803         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20804       equate_decl_number_to_die (decl, namespace_die);
20805     }
20806   else
20807     {
20808       /* Output a namespace alias.  */
20809
20810       /* Force out the namespace we are an alias of, if necessary.  */
20811       dw_die_ref origin_die
20812         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20813
20814       if (DECL_FILE_SCOPE_P (decl)
20815           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20816         context_die = setup_namespace_context (decl, comp_unit_die ());
20817       /* Now create the namespace alias DIE.  */
20818       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20819       add_name_and_src_coords_attributes (namespace_die, decl);
20820       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20821       equate_decl_number_to_die (decl, namespace_die);
20822     }
20823 }
20824
20825 /* Generate Dwarf debug information for a decl described by DECL.
20826    The return value is currently only meaningful for PARM_DECLs,
20827    for all other decls it returns NULL.  */
20828
20829 static dw_die_ref
20830 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20831 {
20832   tree decl_or_origin = decl ? decl : origin;
20833   tree class_origin = NULL, ultimate_origin;
20834
20835   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20836     return NULL;
20837
20838   switch (TREE_CODE (decl_or_origin))
20839     {
20840     case ERROR_MARK:
20841       break;
20842
20843     case CONST_DECL:
20844       if (!is_fortran () && !is_ada ())
20845         {
20846           /* The individual enumerators of an enum type get output when we output
20847              the Dwarf representation of the relevant enum type itself.  */
20848           break;
20849         }
20850
20851       /* Emit its type.  */
20852       gen_type_die (TREE_TYPE (decl), context_die);
20853
20854       /* And its containing namespace.  */
20855       context_die = declare_in_namespace (decl, context_die);
20856
20857       gen_const_die (decl, context_die);
20858       break;
20859
20860     case FUNCTION_DECL:
20861       /* Don't output any DIEs to represent mere function declarations,
20862          unless they are class members or explicit block externs.  */
20863       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20864           && DECL_FILE_SCOPE_P (decl_or_origin)
20865           && (current_function_decl == NULL_TREE
20866               || DECL_ARTIFICIAL (decl_or_origin)))
20867         break;
20868
20869 #if 0
20870       /* FIXME */
20871       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20872          on local redeclarations of global functions.  That seems broken.  */
20873       if (current_function_decl != decl)
20874         /* This is only a declaration.  */;
20875 #endif
20876
20877       /* If we're emitting a clone, emit info for the abstract instance.  */
20878       if (origin || DECL_ORIGIN (decl) != decl)
20879         dwarf2out_abstract_function (origin
20880                                      ? DECL_ORIGIN (origin)
20881                                      : DECL_ABSTRACT_ORIGIN (decl));
20882
20883       /* If we're emitting an out-of-line copy of an inline function,
20884          emit info for the abstract instance and set up to refer to it.  */
20885       else if (cgraph_function_possibly_inlined_p (decl)
20886                && ! DECL_ABSTRACT (decl)
20887                && ! class_or_namespace_scope_p (context_die)
20888                /* dwarf2out_abstract_function won't emit a die if this is just
20889                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20890                   that case, because that works only if we have a die.  */
20891                && DECL_INITIAL (decl) != NULL_TREE)
20892         {
20893           dwarf2out_abstract_function (decl);
20894           set_decl_origin_self (decl);
20895         }
20896
20897       /* Otherwise we're emitting the primary DIE for this decl.  */
20898       else if (debug_info_level > DINFO_LEVEL_TERSE)
20899         {
20900           /* Before we describe the FUNCTION_DECL itself, make sure that we
20901              have its containing type.  */
20902           if (!origin)
20903             origin = decl_class_context (decl);
20904           if (origin != NULL_TREE)
20905             gen_type_die (origin, context_die);
20906
20907           /* And its return type.  */
20908           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20909
20910           /* And its virtual context.  */
20911           if (DECL_VINDEX (decl) != NULL_TREE)
20912             gen_type_die (DECL_CONTEXT (decl), context_die);
20913
20914           /* Make sure we have a member DIE for decl.  */
20915           if (origin != NULL_TREE)
20916             gen_type_die_for_member (origin, decl, context_die);
20917
20918           /* And its containing namespace.  */
20919           context_die = declare_in_namespace (decl, context_die);
20920         }
20921
20922       /* Now output a DIE to represent the function itself.  */
20923       if (decl)
20924         gen_subprogram_die (decl, context_die);
20925       break;
20926
20927     case TYPE_DECL:
20928       /* If we are in terse mode, don't generate any DIEs to represent any
20929          actual typedefs.  */
20930       if (debug_info_level <= DINFO_LEVEL_TERSE)
20931         break;
20932
20933       /* In the special case of a TYPE_DECL node representing the declaration
20934          of some type tag, if the given TYPE_DECL is marked as having been
20935          instantiated from some other (original) TYPE_DECL node (e.g. one which
20936          was generated within the original definition of an inline function) we
20937          used to generate a special (abbreviated) DW_TAG_structure_type,
20938          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20939          should be actually referencing those DIEs, as variable DIEs with that
20940          type would be emitted already in the abstract origin, so it was always
20941          removed during unused type prunning.  Don't add anything in this
20942          case.  */
20943       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20944         break;
20945
20946       if (is_redundant_typedef (decl))
20947         gen_type_die (TREE_TYPE (decl), context_die);
20948       else
20949         /* Output a DIE to represent the typedef itself.  */
20950         gen_typedef_die (decl, context_die);
20951       break;
20952
20953     case LABEL_DECL:
20954       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20955         gen_label_die (decl, context_die);
20956       break;
20957
20958     case VAR_DECL:
20959     case RESULT_DECL:
20960       /* If we are in terse mode, don't generate any DIEs to represent any
20961          variable declarations or definitions.  */
20962       if (debug_info_level <= DINFO_LEVEL_TERSE)
20963         break;
20964
20965       /* Output any DIEs that are needed to specify the type of this data
20966          object.  */
20967       if (decl_by_reference_p (decl_or_origin))
20968         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20969       else
20970         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20971
20972       /* And its containing type.  */
20973       class_origin = decl_class_context (decl_or_origin);
20974       if (class_origin != NULL_TREE)
20975         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20976
20977       /* And its containing namespace.  */
20978       context_die = declare_in_namespace (decl_or_origin, context_die);
20979
20980       /* Now output the DIE to represent the data object itself.  This gets
20981          complicated because of the possibility that the VAR_DECL really
20982          represents an inlined instance of a formal parameter for an inline
20983          function.  */
20984       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20985       if (ultimate_origin != NULL_TREE
20986           && TREE_CODE (ultimate_origin) == PARM_DECL)
20987         gen_formal_parameter_die (decl, origin,
20988                                   true /* Emit name attribute.  */,
20989                                   context_die);
20990       else
20991         gen_variable_die (decl, origin, context_die);
20992       break;
20993
20994     case FIELD_DECL:
20995       /* Ignore the nameless fields that are used to skip bits but handle C++
20996          anonymous unions and structs.  */
20997       if (DECL_NAME (decl) != NULL_TREE
20998           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20999           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21000         {
21001           gen_type_die (member_declared_type (decl), context_die);
21002           gen_field_die (decl, context_die);
21003         }
21004       break;
21005
21006     case PARM_DECL:
21007       if (DECL_BY_REFERENCE (decl_or_origin))
21008         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21009       else
21010         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21011       return gen_formal_parameter_die (decl, origin,
21012                                        true /* Emit name attribute.  */,
21013                                        context_die);
21014
21015     case NAMESPACE_DECL:
21016     case IMPORTED_DECL:
21017       if (dwarf_version >= 3 || !dwarf_strict)
21018         gen_namespace_die (decl, context_die);
21019       break;
21020
21021     default:
21022       /* Probably some frontend-internal decl.  Assume we don't care.  */
21023       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21024       break;
21025     }
21026
21027   return NULL;
21028 }
21029 \f
21030 /* Output debug information for global decl DECL.  Called from toplev.c after
21031    compilation proper has finished.  */
21032
21033 static void
21034 dwarf2out_global_decl (tree decl)
21035 {
21036   /* Output DWARF2 information for file-scope tentative data object
21037      declarations, file-scope (extern) function declarations (which
21038      had no corresponding body) and file-scope tagged type declarations
21039      and definitions which have not yet been forced out.  */
21040   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21041     dwarf2out_decl (decl);
21042 }
21043
21044 /* Output debug information for type decl DECL.  Called from toplev.c
21045    and from language front ends (to record built-in types).  */
21046 static void
21047 dwarf2out_type_decl (tree decl, int local)
21048 {
21049   if (!local)
21050     dwarf2out_decl (decl);
21051 }
21052
21053 /* Output debug information for imported module or decl DECL.
21054    NAME is non-NULL name in the lexical block if the decl has been renamed.
21055    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21056    that DECL belongs to.
21057    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21058 static void
21059 dwarf2out_imported_module_or_decl_1 (tree decl,
21060                                      tree name,
21061                                      tree lexical_block,
21062                                      dw_die_ref lexical_block_die)
21063 {
21064   expanded_location xloc;
21065   dw_die_ref imported_die = NULL;
21066   dw_die_ref at_import_die;
21067
21068   if (TREE_CODE (decl) == IMPORTED_DECL)
21069     {
21070       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21071       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21072       gcc_assert (decl);
21073     }
21074   else
21075     xloc = expand_location (input_location);
21076
21077   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21078     {
21079       at_import_die = force_type_die (TREE_TYPE (decl));
21080       /* For namespace N { typedef void T; } using N::T; base_type_die
21081          returns NULL, but DW_TAG_imported_declaration requires
21082          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21083       if (!at_import_die)
21084         {
21085           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21086           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21087           at_import_die = lookup_type_die (TREE_TYPE (decl));
21088           gcc_assert (at_import_die);
21089         }
21090     }
21091   else
21092     {
21093       at_import_die = lookup_decl_die (decl);
21094       if (!at_import_die)
21095         {
21096           /* If we're trying to avoid duplicate debug info, we may not have
21097              emitted the member decl for this field.  Emit it now.  */
21098           if (TREE_CODE (decl) == FIELD_DECL)
21099             {
21100               tree type = DECL_CONTEXT (decl);
21101
21102               if (TYPE_CONTEXT (type)
21103                   && TYPE_P (TYPE_CONTEXT (type))
21104                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21105                                                 DINFO_USAGE_DIR_USE))
21106                 return;
21107               gen_type_die_for_member (type, decl,
21108                                        get_context_die (TYPE_CONTEXT (type)));
21109             }
21110           at_import_die = force_decl_die (decl);
21111         }
21112     }
21113
21114   if (TREE_CODE (decl) == NAMESPACE_DECL)
21115     {
21116       if (dwarf_version >= 3 || !dwarf_strict)
21117         imported_die = new_die (DW_TAG_imported_module,
21118                                 lexical_block_die,
21119                                 lexical_block);
21120       else
21121         return;
21122     }
21123   else
21124     imported_die = new_die (DW_TAG_imported_declaration,
21125                             lexical_block_die,
21126                             lexical_block);
21127
21128   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21129   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21130   if (name)
21131     add_AT_string (imported_die, DW_AT_name,
21132                    IDENTIFIER_POINTER (name));
21133   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21134 }
21135
21136 /* Output debug information for imported module or decl DECL.
21137    NAME is non-NULL name in context if the decl has been renamed.
21138    CHILD is true if decl is one of the renamed decls as part of
21139    importing whole module.  */
21140
21141 static void
21142 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21143                                    bool child)
21144 {
21145   /* dw_die_ref at_import_die;  */
21146   dw_die_ref scope_die;
21147
21148   if (debug_info_level <= DINFO_LEVEL_TERSE)
21149     return;
21150
21151   gcc_assert (decl);
21152
21153   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21154      We need decl DIE for reference and scope die. First, get DIE for the decl
21155      itself.  */
21156
21157   /* Get the scope die for decl context. Use comp_unit_die for global module
21158      or decl. If die is not found for non globals, force new die.  */
21159   if (context
21160       && TYPE_P (context)
21161       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21162     return;
21163
21164   if (!(dwarf_version >= 3 || !dwarf_strict))
21165     return;
21166
21167   scope_die = get_context_die (context);
21168
21169   if (child)
21170     {
21171       gcc_assert (scope_die->die_child);
21172       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21173       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21174       scope_die = scope_die->die_child;
21175     }
21176
21177   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21178   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21179
21180 }
21181
21182 /* Write the debugging output for DECL.  */
21183
21184 void
21185 dwarf2out_decl (tree decl)
21186 {
21187   dw_die_ref context_die = comp_unit_die ();
21188
21189   switch (TREE_CODE (decl))
21190     {
21191     case ERROR_MARK:
21192       return;
21193
21194     case FUNCTION_DECL:
21195       /* What we would really like to do here is to filter out all mere
21196          file-scope declarations of file-scope functions which are never
21197          referenced later within this translation unit (and keep all of ones
21198          that *are* referenced later on) but we aren't clairvoyant, so we have
21199          no idea which functions will be referenced in the future (i.e. later
21200          on within the current translation unit). So here we just ignore all
21201          file-scope function declarations which are not also definitions.  If
21202          and when the debugger needs to know something about these functions,
21203          it will have to hunt around and find the DWARF information associated
21204          with the definition of the function.
21205
21206          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21207          nodes represent definitions and which ones represent mere
21208          declarations.  We have to check DECL_INITIAL instead. That's because
21209          the C front-end supports some weird semantics for "extern inline"
21210          function definitions.  These can get inlined within the current
21211          translation unit (and thus, we need to generate Dwarf info for their
21212          abstract instances so that the Dwarf info for the concrete inlined
21213          instances can have something to refer to) but the compiler never
21214          generates any out-of-lines instances of such things (despite the fact
21215          that they *are* definitions).
21216
21217          The important point is that the C front-end marks these "extern
21218          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21219          them anyway. Note that the C++ front-end also plays some similar games
21220          for inline function definitions appearing within include files which
21221          also contain `#pragma interface' pragmas.  */
21222       if (DECL_INITIAL (decl) == NULL_TREE)
21223         return;
21224
21225       /* If we're a nested function, initially use a parent of NULL; if we're
21226          a plain function, this will be fixed up in decls_for_scope.  If
21227          we're a method, it will be ignored, since we already have a DIE.  */
21228       if (decl_function_context (decl)
21229           /* But if we're in terse mode, we don't care about scope.  */
21230           && debug_info_level > DINFO_LEVEL_TERSE)
21231         context_die = NULL;
21232       break;
21233
21234     case VAR_DECL:
21235       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21236          declaration and if the declaration was never even referenced from
21237          within this entire compilation unit.  We suppress these DIEs in
21238          order to save space in the .debug section (by eliminating entries
21239          which are probably useless).  Note that we must not suppress
21240          block-local extern declarations (whether used or not) because that
21241          would screw-up the debugger's name lookup mechanism and cause it to
21242          miss things which really ought to be in scope at a given point.  */
21243       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21244         return;
21245
21246       /* For local statics lookup proper context die.  */
21247       if (TREE_STATIC (decl) && decl_function_context (decl))
21248         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21249
21250       /* If we are in terse mode, don't generate any DIEs to represent any
21251          variable declarations or definitions.  */
21252       if (debug_info_level <= DINFO_LEVEL_TERSE)
21253         return;
21254       break;
21255
21256     case CONST_DECL:
21257       if (debug_info_level <= DINFO_LEVEL_TERSE)
21258         return;
21259       if (!is_fortran () && !is_ada ())
21260         return;
21261       if (TREE_STATIC (decl) && decl_function_context (decl))
21262         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21263       break;
21264
21265     case NAMESPACE_DECL:
21266     case IMPORTED_DECL:
21267       if (debug_info_level <= DINFO_LEVEL_TERSE)
21268         return;
21269       if (lookup_decl_die (decl) != NULL)
21270         return;
21271       break;
21272
21273     case TYPE_DECL:
21274       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21275       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21276         return;
21277
21278       /* Don't bother trying to generate any DIEs to represent any of the
21279          normal built-in types for the language we are compiling.  */
21280       if (DECL_IS_BUILTIN (decl))
21281         return;
21282
21283       /* If we are in terse mode, don't generate any DIEs for types.  */
21284       if (debug_info_level <= DINFO_LEVEL_TERSE)
21285         return;
21286
21287       /* If we're a function-scope tag, initially use a parent of NULL;
21288          this will be fixed up in decls_for_scope.  */
21289       if (decl_function_context (decl))
21290         context_die = NULL;
21291
21292       break;
21293
21294     default:
21295       return;
21296     }
21297
21298   gen_decl_die (decl, NULL, context_die);
21299 }
21300
21301 /* Write the debugging output for DECL.  */
21302
21303 static void
21304 dwarf2out_function_decl (tree decl)
21305 {
21306   dwarf2out_decl (decl);
21307
21308   htab_empty (decl_loc_table);
21309 }
21310
21311 /* Output a marker (i.e. a label) for the beginning of the generated code for
21312    a lexical block.  */
21313
21314 static void
21315 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21316                        unsigned int blocknum)
21317 {
21318   switch_to_section (current_function_section ());
21319   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21320 }
21321
21322 /* Output a marker (i.e. a label) for the end of the generated code for a
21323    lexical block.  */
21324
21325 static void
21326 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21327 {
21328   switch_to_section (current_function_section ());
21329   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21330 }
21331
21332 /* Returns nonzero if it is appropriate not to emit any debugging
21333    information for BLOCK, because it doesn't contain any instructions.
21334
21335    Don't allow this for blocks with nested functions or local classes
21336    as we would end up with orphans, and in the presence of scheduling
21337    we may end up calling them anyway.  */
21338
21339 static bool
21340 dwarf2out_ignore_block (const_tree block)
21341 {
21342   tree decl;
21343   unsigned int i;
21344
21345   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21346     if (TREE_CODE (decl) == FUNCTION_DECL
21347         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21348       return 0;
21349   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21350     {
21351       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21352       if (TREE_CODE (decl) == FUNCTION_DECL
21353           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21354       return 0;
21355     }
21356
21357   return 1;
21358 }
21359
21360 /* Hash table routines for file_hash.  */
21361
21362 static int
21363 file_table_eq (const void *p1_p, const void *p2_p)
21364 {
21365   const struct dwarf_file_data *const p1 =
21366     (const struct dwarf_file_data *) p1_p;
21367   const char *const p2 = (const char *) p2_p;
21368   return strcmp (p1->filename, p2) == 0;
21369 }
21370
21371 static hashval_t
21372 file_table_hash (const void *p_p)
21373 {
21374   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21375   return htab_hash_string (p->filename);
21376 }
21377
21378 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21379    dwarf2out.c) and return its "index".  The index of each (known) filename is
21380    just a unique number which is associated with only that one filename.  We
21381    need such numbers for the sake of generating labels (in the .debug_sfnames
21382    section) and references to those files numbers (in the .debug_srcinfo
21383    and.debug_macinfo sections).  If the filename given as an argument is not
21384    found in our current list, add it to the list and assign it the next
21385    available unique index number.  In order to speed up searches, we remember
21386    the index of the filename was looked up last.  This handles the majority of
21387    all searches.  */
21388
21389 static struct dwarf_file_data *
21390 lookup_filename (const char *file_name)
21391 {
21392   void ** slot;
21393   struct dwarf_file_data * created;
21394
21395   /* Check to see if the file name that was searched on the previous
21396      call matches this file name.  If so, return the index.  */
21397   if (file_table_last_lookup
21398       && (file_name == file_table_last_lookup->filename
21399           || strcmp (file_table_last_lookup->filename, file_name) == 0))
21400     return file_table_last_lookup;
21401
21402   /* Didn't match the previous lookup, search the table.  */
21403   slot = htab_find_slot_with_hash (file_table, file_name,
21404                                    htab_hash_string (file_name), INSERT);
21405   if (*slot)
21406     return (struct dwarf_file_data *) *slot;
21407
21408   created = ggc_alloc_dwarf_file_data ();
21409   created->filename = file_name;
21410   created->emitted_number = 0;
21411   *slot = created;
21412   return created;
21413 }
21414
21415 /* If the assembler will construct the file table, then translate the compiler
21416    internal file table number into the assembler file table number, and emit
21417    a .file directive if we haven't already emitted one yet.  The file table
21418    numbers are different because we prune debug info for unused variables and
21419    types, which may include filenames.  */
21420
21421 static int
21422 maybe_emit_file (struct dwarf_file_data * fd)
21423 {
21424   if (! fd->emitted_number)
21425     {
21426       if (last_emitted_file)
21427         fd->emitted_number = last_emitted_file->emitted_number + 1;
21428       else
21429         fd->emitted_number = 1;
21430       last_emitted_file = fd;
21431
21432       if (DWARF2_ASM_LINE_DEBUG_INFO)
21433         {
21434           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21435           output_quoted_string (asm_out_file,
21436                                 remap_debug_filename (fd->filename));
21437           fputc ('\n', asm_out_file);
21438         }
21439     }
21440
21441   return fd->emitted_number;
21442 }
21443
21444 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21445    That generation should happen after function debug info has been
21446    generated. The value of the attribute is the constant value of ARG.  */
21447
21448 static void
21449 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21450 {
21451   die_arg_entry entry;
21452
21453   if (!die || !arg)
21454     return;
21455
21456   if (!tmpl_value_parm_die_table)
21457     tmpl_value_parm_die_table
21458       = VEC_alloc (die_arg_entry, gc, 32);
21459
21460   entry.die = die;
21461   entry.arg = arg;
21462   VEC_safe_push (die_arg_entry, gc,
21463                  tmpl_value_parm_die_table,
21464                  &entry);
21465 }
21466
21467 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21468    by append_entry_to_tmpl_value_parm_die_table. This function must
21469    be called after function DIEs have been generated.  */
21470
21471 static void
21472 gen_remaining_tmpl_value_param_die_attribute (void)
21473 {
21474   if (tmpl_value_parm_die_table)
21475     {
21476       unsigned i;
21477       die_arg_entry *e;
21478
21479       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21480         tree_add_const_value_attribute (e->die, e->arg);
21481     }
21482 }
21483
21484
21485 /* Replace DW_AT_name for the decl with name.  */
21486
21487 static void
21488 dwarf2out_set_name (tree decl, tree name)
21489 {
21490   dw_die_ref die;
21491   dw_attr_ref attr;
21492   const char *dname;
21493
21494   die = TYPE_SYMTAB_DIE (decl);
21495   if (!die)
21496     return;
21497
21498   dname = dwarf2_name (name, 0);
21499   if (!dname)
21500     return;
21501
21502   attr = get_AT (die, DW_AT_name);
21503   if (attr)
21504     {
21505       struct indirect_string_node *node;
21506
21507       node = find_AT_string (dname);
21508       /* replace the string.  */
21509       attr->dw_attr_val.v.val_str = node;
21510     }
21511
21512   else
21513     add_name_attribute (die, dname);
21514 }
21515
21516 /* Called by the final INSN scan whenever we see a direct function call.
21517    Make an entry into the direct call table, recording the point of call
21518    and a reference to the target function's debug entry.  */
21519
21520 static void
21521 dwarf2out_direct_call (tree targ)
21522 {
21523   dcall_entry e;
21524   tree origin = decl_ultimate_origin (targ);
21525
21526   /* If this is a clone, use the abstract origin as the target.  */
21527   if (origin)
21528     targ = origin;
21529
21530   e.poc_label_num = poc_label_num++;
21531   e.poc_decl = current_function_decl;
21532   e.targ_die = force_decl_die (targ);
21533   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21534
21535   /* Drop a label at the return point to mark the point of call.  */
21536   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21537 }
21538
21539 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21540
21541 static hashval_t
21542 vcall_insn_table_hash (const void *x)
21543 {
21544   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21545 }
21546
21547 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21548    insnd_uid of *Y.  */
21549
21550 static int
21551 vcall_insn_table_eq (const void *x, const void *y)
21552 {
21553   return (((const struct vcall_insn *) x)->insn_uid
21554           == ((const struct vcall_insn *) y)->insn_uid);
21555 }
21556
21557 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21558
21559 static void
21560 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21561 {
21562   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21563   struct vcall_insn **slot;
21564
21565   gcc_assert (item);
21566   item->insn_uid = insn_uid;
21567   item->vtable_slot = vtable_slot;
21568   slot = (struct vcall_insn **)
21569       htab_find_slot_with_hash (vcall_insn_table, &item,
21570                                 (hashval_t) insn_uid, INSERT);
21571   *slot = item;
21572 }
21573
21574 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21575
21576 static unsigned int
21577 lookup_vcall_insn (unsigned int insn_uid)
21578 {
21579   struct vcall_insn item;
21580   struct vcall_insn *p;
21581
21582   item.insn_uid = insn_uid;
21583   item.vtable_slot = 0;
21584   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21585                                                  (void *) &item,
21586                                                  (hashval_t) insn_uid);
21587   if (p == NULL)
21588     return (unsigned int) -1;
21589   return p->vtable_slot;
21590 }
21591
21592
21593 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21594    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21595    is the vtable slot index that we will need to put in the virtual call
21596    table later.  */
21597
21598 static void
21599 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21600 {
21601   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21602     {
21603       tree token = OBJ_TYPE_REF_TOKEN (addr);
21604       if (TREE_CODE (token) == INTEGER_CST)
21605         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21606     }
21607 }
21608
21609 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21610    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21611    with NEW_INSN.  */
21612
21613 static void
21614 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21615 {
21616   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21617
21618   if (vtable_slot != (unsigned int) -1)
21619     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21620 }
21621
21622 /* Called by the final INSN scan whenever we see a virtual function call.
21623    Make an entry into the virtual call table, recording the point of call
21624    and the slot index of the vtable entry used to call the virtual member
21625    function.  The slot index was associated with the INSN_UID during the
21626    lowering to RTL.  */
21627
21628 static void
21629 dwarf2out_virtual_call (int insn_uid)
21630 {
21631   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21632   vcall_entry e;
21633
21634   if (vtable_slot == (unsigned int) -1)
21635     return;
21636
21637   e.poc_label_num = poc_label_num++;
21638   e.vtable_slot = vtable_slot;
21639   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21640
21641   /* Drop a label at the return point to mark the point of call.  */
21642   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21643 }
21644
21645 /* Called by the final INSN scan whenever we see a var location.  We
21646    use it to drop labels in the right places, and throw the location in
21647    our lookup table.  */
21648
21649 static void
21650 dwarf2out_var_location (rtx loc_note)
21651 {
21652   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21653   struct var_loc_node *newloc;
21654   rtx next_real;
21655   static const char *last_label;
21656   static const char *last_postcall_label;
21657   static bool last_in_cold_section_p;
21658   tree decl;
21659
21660   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21661     return;
21662
21663   next_real = next_real_insn (loc_note);
21664   /* If there are no instructions which would be affected by this note,
21665      don't do anything.  */
21666   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21667     return;
21668
21669   /* If there were any real insns between note we processed last time
21670      and this note (or if it is the first note), clear
21671      last_{,postcall_}label so that they are not reused this time.  */
21672   if (last_var_location_insn == NULL_RTX
21673       || last_var_location_insn != next_real
21674       || last_in_cold_section_p != in_cold_section_p)
21675     {
21676       last_label = NULL;
21677       last_postcall_label = NULL;
21678     }
21679
21680   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21681   newloc = add_var_loc_to_decl (decl, loc_note,
21682                                 NOTE_DURING_CALL_P (loc_note)
21683                                 ? last_postcall_label : last_label);
21684   if (newloc == NULL)
21685     return;
21686
21687   /* If there were no real insns between note we processed last time
21688      and this note, use the label we emitted last time.  Otherwise
21689      create a new label and emit it.  */
21690   if (last_label == NULL)
21691     {
21692       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21693       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21694       loclabel_num++;
21695       last_label = ggc_strdup (loclabel);
21696     }
21697
21698   if (!NOTE_DURING_CALL_P (loc_note))
21699     newloc->label = last_label;
21700   else
21701     {
21702       if (!last_postcall_label)
21703         {
21704           sprintf (loclabel, "%s-1", last_label);
21705           last_postcall_label = ggc_strdup (loclabel);
21706         }
21707       newloc->label = last_postcall_label;
21708     }
21709
21710   last_var_location_insn = next_real;
21711   last_in_cold_section_p = in_cold_section_p;
21712 }
21713
21714 /* We need to reset the locations at the beginning of each
21715    function. We can't do this in the end_function hook, because the
21716    declarations that use the locations won't have been output when
21717    that hook is called.  Also compute have_multiple_function_sections here.  */
21718
21719 static void
21720 dwarf2out_begin_function (tree fun)
21721 {
21722   if (function_section (fun) != text_section)
21723     have_multiple_function_sections = true;
21724   else if (flag_reorder_blocks_and_partition && !cold_text_section)
21725     {
21726       gcc_assert (current_function_decl == fun);
21727       cold_text_section = unlikely_text_section ();
21728       switch_to_section (cold_text_section);
21729       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21730       switch_to_section (current_function_section ());
21731     }
21732
21733   dwarf2out_note_section_used ();
21734 }
21735
21736 /* Output a label to mark the beginning of a source code line entry
21737    and record information relating to this source line, in
21738    'line_info_table' for later output of the .debug_line section.  */
21739
21740 static void
21741 dwarf2out_source_line (unsigned int line, const char *filename,
21742                        int discriminator, bool is_stmt)
21743 {
21744   static bool last_is_stmt = true;
21745
21746   if (debug_info_level >= DINFO_LEVEL_NORMAL
21747       && line != 0)
21748     {
21749       int file_num = maybe_emit_file (lookup_filename (filename));
21750
21751       switch_to_section (current_function_section ());
21752
21753       /* If requested, emit something human-readable.  */
21754       if (flag_debug_asm)
21755         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21756                  filename, line);
21757
21758       if (DWARF2_ASM_LINE_DEBUG_INFO)
21759         {
21760           /* Emit the .loc directive understood by GNU as.  */
21761           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21762           if (is_stmt != last_is_stmt)
21763             {
21764               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21765               last_is_stmt = is_stmt;
21766             }
21767           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21768             fprintf (asm_out_file, " discriminator %d", discriminator);
21769           fputc ('\n', asm_out_file);
21770
21771           /* Indicate that line number info exists.  */
21772           line_info_table_in_use++;
21773         }
21774       else if (function_section (current_function_decl) != text_section)
21775         {
21776           dw_separate_line_info_ref line_info;
21777           targetm.asm_out.internal_label (asm_out_file,
21778                                           SEPARATE_LINE_CODE_LABEL,
21779                                           separate_line_info_table_in_use);
21780
21781           /* Expand the line info table if necessary.  */
21782           if (separate_line_info_table_in_use
21783               == separate_line_info_table_allocated)
21784             {
21785               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21786               separate_line_info_table
21787                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21788                                  separate_line_info_table,
21789                                  separate_line_info_table_allocated);
21790               memset (separate_line_info_table
21791                        + separate_line_info_table_in_use,
21792                       0,
21793                       (LINE_INFO_TABLE_INCREMENT
21794                        * sizeof (dw_separate_line_info_entry)));
21795             }
21796
21797           /* Add the new entry at the end of the line_info_table.  */
21798           line_info
21799             = &separate_line_info_table[separate_line_info_table_in_use++];
21800           line_info->dw_file_num = file_num;
21801           line_info->dw_line_num = line;
21802           line_info->function = current_function_funcdef_no;
21803         }
21804       else
21805         {
21806           dw_line_info_ref line_info;
21807
21808           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21809                                      line_info_table_in_use);
21810
21811           /* Expand the line info table if necessary.  */
21812           if (line_info_table_in_use == line_info_table_allocated)
21813             {
21814               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21815               line_info_table
21816                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21817                                  line_info_table_allocated);
21818               memset (line_info_table + line_info_table_in_use, 0,
21819                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21820             }
21821
21822           /* Add the new entry at the end of the line_info_table.  */
21823           line_info = &line_info_table[line_info_table_in_use++];
21824           line_info->dw_file_num = file_num;
21825           line_info->dw_line_num = line;
21826         }
21827     }
21828 }
21829
21830 /* Record the beginning of a new source file.  */
21831
21832 static void
21833 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21834 {
21835   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21836     {
21837       /* Record the beginning of the file for break_out_includes.  */
21838       dw_die_ref bincl_die;
21839
21840       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21841       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21842     }
21843
21844   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21845     {
21846       macinfo_entry e;
21847       e.code = DW_MACINFO_start_file;
21848       e.lineno = lineno;
21849       e.info = xstrdup (filename);
21850       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21851     }
21852 }
21853
21854 /* Record the end of a source file.  */
21855
21856 static void
21857 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21858 {
21859   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21860     /* Record the end of the file for break_out_includes.  */
21861     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21862
21863   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21864     {
21865       macinfo_entry e;
21866       e.code = DW_MACINFO_end_file;
21867       e.lineno = lineno;
21868       e.info = NULL;
21869       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21870     }
21871 }
21872
21873 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21874    the tail part of the directive line, i.e. the part which is past the
21875    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21876
21877 static void
21878 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21879                   const char *buffer ATTRIBUTE_UNUSED)
21880 {
21881   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21882     {
21883       macinfo_entry e;
21884       e.code = DW_MACINFO_define;
21885       e.lineno = lineno;
21886       e.info = xstrdup (buffer);;
21887       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21888     }
21889 }
21890
21891 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21892    the tail part of the directive line, i.e. the part which is past the
21893    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21894
21895 static void
21896 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21897                  const char *buffer ATTRIBUTE_UNUSED)
21898 {
21899   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21900     {
21901       macinfo_entry e;
21902       e.code = DW_MACINFO_undef;
21903       e.lineno = lineno;
21904       e.info = xstrdup (buffer);;
21905       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
21906     }
21907 }
21908
21909 static void
21910 output_macinfo (void)
21911 {
21912   unsigned i;
21913   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
21914   macinfo_entry *ref;
21915
21916   if (! length)
21917     return;
21918
21919   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
21920     {
21921       switch (ref->code)
21922         {
21923           case DW_MACINFO_start_file:
21924             {
21925               int file_num = maybe_emit_file (lookup_filename (ref->info));
21926               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21927               dw2_asm_output_data_uleb128 
21928                         (ref->lineno, "Included from line number %lu", 
21929                                                 (unsigned long)ref->lineno);
21930               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21931             }
21932             break;
21933           case DW_MACINFO_end_file:
21934             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21935             break;
21936           case DW_MACINFO_define:
21937             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21938             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
21939                                                 (unsigned long)ref->lineno);
21940             dw2_asm_output_nstring (ref->info, -1, "The macro");
21941             break;
21942           case DW_MACINFO_undef:
21943             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21944             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21945                                                 (unsigned long)ref->lineno);
21946             dw2_asm_output_nstring (ref->info, -1, "The macro");
21947             break;
21948           default:
21949            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21950              ASM_COMMENT_START, (unsigned long)ref->code);
21951           break;
21952         }
21953     }
21954 }
21955
21956 /* Set up for Dwarf output at the start of compilation.  */
21957
21958 static void
21959 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21960 {
21961   /* Allocate the file_table.  */
21962   file_table = htab_create_ggc (50, file_table_hash,
21963                                 file_table_eq, NULL);
21964
21965   /* Allocate the decl_die_table.  */
21966   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21967                                     decl_die_table_eq, NULL);
21968
21969   /* Allocate the decl_loc_table.  */
21970   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21971                                     decl_loc_table_eq, NULL);
21972
21973   /* Allocate the initial hunk of the decl_scope_table.  */
21974   decl_scope_table = VEC_alloc (tree, gc, 256);
21975
21976   /* Allocate the initial hunk of the abbrev_die_table.  */
21977   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21978     (ABBREV_DIE_TABLE_INCREMENT);
21979   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21980   /* Zero-th entry is allocated, but unused.  */
21981   abbrev_die_table_in_use = 1;
21982
21983   /* Allocate the initial hunk of the line_info_table.  */
21984   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21985     (LINE_INFO_TABLE_INCREMENT);
21986   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21987
21988   /* Zero-th entry is allocated, but unused.  */
21989   line_info_table_in_use = 1;
21990
21991   /* Allocate the pubtypes and pubnames vectors.  */
21992   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21993   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21994
21995   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21996   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21997                                       vcall_insn_table_eq, NULL);
21998
21999   incomplete_types = VEC_alloc (tree, gc, 64);
22000
22001   used_rtx_array = VEC_alloc (rtx, gc, 32);
22002
22003   debug_info_section = get_section (DEBUG_INFO_SECTION,
22004                                     SECTION_DEBUG, NULL);
22005   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22006                                       SECTION_DEBUG, NULL);
22007   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22008                                        SECTION_DEBUG, NULL);
22009   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22010                                        SECTION_DEBUG, NULL);
22011   debug_line_section = get_section (DEBUG_LINE_SECTION,
22012                                     SECTION_DEBUG, NULL);
22013   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22014                                    SECTION_DEBUG, NULL);
22015   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22016                                         SECTION_DEBUG, NULL);
22017   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22018                                         SECTION_DEBUG, NULL);
22019   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
22020                                      SECTION_DEBUG, NULL);
22021   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
22022                                      SECTION_DEBUG, NULL);
22023   debug_str_section = get_section (DEBUG_STR_SECTION,
22024                                    DEBUG_STR_SECTION_FLAGS, NULL);
22025   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22026                                       SECTION_DEBUG, NULL);
22027   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22028                                      SECTION_DEBUG, NULL);
22029
22030   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22031   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22032                                DEBUG_ABBREV_SECTION_LABEL, 0);
22033   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22034   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22035                                COLD_TEXT_SECTION_LABEL, 0);
22036   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22037
22038   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22039                                DEBUG_INFO_SECTION_LABEL, 0);
22040   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22041                                DEBUG_LINE_SECTION_LABEL, 0);
22042   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22043                                DEBUG_RANGES_SECTION_LABEL, 0);
22044   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22045                                DEBUG_MACINFO_SECTION_LABEL, 0);
22046
22047   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22048     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22049
22050   switch_to_section (text_section);
22051   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22052 }
22053
22054 /* Called before cgraph_optimize starts outputtting functions, variables
22055    and toplevel asms into assembly.  */
22056
22057 static void
22058 dwarf2out_assembly_start (void)
22059 {
22060   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22061       && dwarf2out_do_cfi_asm ()
22062       && (!(flag_unwind_tables || flag_exceptions)
22063           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22064     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22065 }
22066
22067 /* A helper function for dwarf2out_finish called through
22068    htab_traverse.  Emit one queued .debug_str string.  */
22069
22070 static int
22071 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22072 {
22073   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22074
22075   if (node->label && node->refcount)
22076     {
22077       switch_to_section (debug_str_section);
22078       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22079       assemble_string (node->str, strlen (node->str) + 1);
22080     }
22081
22082   return 1;
22083 }
22084
22085 #if ENABLE_ASSERT_CHECKING
22086 /* Verify that all marks are clear.  */
22087
22088 static void
22089 verify_marks_clear (dw_die_ref die)
22090 {
22091   dw_die_ref c;
22092
22093   gcc_assert (! die->die_mark);
22094   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22095 }
22096 #endif /* ENABLE_ASSERT_CHECKING */
22097
22098 /* Clear the marks for a die and its children.
22099    Be cool if the mark isn't set.  */
22100
22101 static void
22102 prune_unmark_dies (dw_die_ref die)
22103 {
22104   dw_die_ref c;
22105
22106   if (die->die_mark)
22107     die->die_mark = 0;
22108   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22109 }
22110
22111 /* Given DIE that we're marking as used, find any other dies
22112    it references as attributes and mark them as used.  */
22113
22114 static void
22115 prune_unused_types_walk_attribs (dw_die_ref die)
22116 {
22117   dw_attr_ref a;
22118   unsigned ix;
22119
22120   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22121     {
22122       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22123         {
22124           /* A reference to another DIE.
22125              Make sure that it will get emitted.
22126              If it was broken out into a comdat group, don't follow it.  */
22127           if (dwarf_version < 4
22128               || a->dw_attr == DW_AT_specification
22129               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22130             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22131         }
22132       /* Set the string's refcount to 0 so that prune_unused_types_mark
22133          accounts properly for it.  */
22134       if (AT_class (a) == dw_val_class_str)
22135         a->dw_attr_val.v.val_str->refcount = 0;
22136     }
22137 }
22138
22139
22140 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22141    to DIE's children.  */
22142
22143 static void
22144 prune_unused_types_mark (dw_die_ref die, int dokids)
22145 {
22146   dw_die_ref c;
22147
22148   if (die->die_mark == 0)
22149     {
22150       /* We haven't done this node yet.  Mark it as used.  */
22151       die->die_mark = 1;
22152
22153       /* We also have to mark its parents as used.
22154          (But we don't want to mark our parents' kids due to this.)  */
22155       if (die->die_parent)
22156         prune_unused_types_mark (die->die_parent, 0);
22157
22158       /* Mark any referenced nodes.  */
22159       prune_unused_types_walk_attribs (die);
22160
22161       /* If this node is a specification,
22162          also mark the definition, if it exists.  */
22163       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22164         prune_unused_types_mark (die->die_definition, 1);
22165     }
22166
22167   if (dokids && die->die_mark != 2)
22168     {
22169       /* We need to walk the children, but haven't done so yet.
22170          Remember that we've walked the kids.  */
22171       die->die_mark = 2;
22172
22173       /* If this is an array type, we need to make sure our
22174          kids get marked, even if they're types.  If we're
22175          breaking out types into comdat sections, do this
22176          for all type definitions.  */
22177       if (die->die_tag == DW_TAG_array_type
22178           || (dwarf_version >= 4
22179               && is_type_die (die) && ! is_declaration_die (die)))
22180         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22181       else
22182         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22183     }
22184 }
22185
22186 /* For local classes, look if any static member functions were emitted
22187    and if so, mark them.  */
22188
22189 static void
22190 prune_unused_types_walk_local_classes (dw_die_ref die)
22191 {
22192   dw_die_ref c;
22193
22194   if (die->die_mark == 2)
22195     return;
22196
22197   switch (die->die_tag)
22198     {
22199     case DW_TAG_structure_type:
22200     case DW_TAG_union_type:
22201     case DW_TAG_class_type:
22202       break;
22203
22204     case DW_TAG_subprogram:
22205       if (!get_AT_flag (die, DW_AT_declaration)
22206           || die->die_definition != NULL)
22207         prune_unused_types_mark (die, 1);
22208       return;
22209
22210     default:
22211       return;
22212     }
22213
22214   /* Mark children.  */
22215   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22216 }
22217
22218 /* Walk the tree DIE and mark types that we actually use.  */
22219
22220 static void
22221 prune_unused_types_walk (dw_die_ref die)
22222 {
22223   dw_die_ref c;
22224
22225   /* Don't do anything if this node is already marked and
22226      children have been marked as well.  */
22227   if (die->die_mark == 2)
22228     return;
22229
22230   switch (die->die_tag)
22231     {
22232     case DW_TAG_structure_type:
22233     case DW_TAG_union_type:
22234     case DW_TAG_class_type:
22235       if (die->die_perennial_p)
22236         break;
22237
22238       for (c = die->die_parent; c; c = c->die_parent)
22239         if (c->die_tag == DW_TAG_subprogram)
22240           break;
22241
22242       /* Finding used static member functions inside of classes
22243          is needed just for local classes, because for other classes
22244          static member function DIEs with DW_AT_specification
22245          are emitted outside of the DW_TAG_*_type.  If we ever change
22246          it, we'd need to call this even for non-local classes.  */
22247       if (c)
22248         prune_unused_types_walk_local_classes (die);
22249
22250       /* It's a type node --- don't mark it.  */
22251       return;
22252
22253     case DW_TAG_const_type:
22254     case DW_TAG_packed_type:
22255     case DW_TAG_pointer_type:
22256     case DW_TAG_reference_type:
22257     case DW_TAG_rvalue_reference_type:
22258     case DW_TAG_volatile_type:
22259     case DW_TAG_typedef:
22260     case DW_TAG_array_type:
22261     case DW_TAG_interface_type:
22262     case DW_TAG_friend:
22263     case DW_TAG_variant_part:
22264     case DW_TAG_enumeration_type:
22265     case DW_TAG_subroutine_type:
22266     case DW_TAG_string_type:
22267     case DW_TAG_set_type:
22268     case DW_TAG_subrange_type:
22269     case DW_TAG_ptr_to_member_type:
22270     case DW_TAG_file_type:
22271       if (die->die_perennial_p)
22272         break;
22273
22274       /* It's a type node --- don't mark it.  */
22275       return;
22276
22277     default:
22278       /* Mark everything else.  */
22279       break;
22280   }
22281
22282   if (die->die_mark == 0)
22283     {
22284       die->die_mark = 1;
22285
22286       /* Now, mark any dies referenced from here.  */
22287       prune_unused_types_walk_attribs (die);
22288     }
22289
22290   die->die_mark = 2;
22291
22292   /* Mark children.  */
22293   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22294 }
22295
22296 /* Increment the string counts on strings referred to from DIE's
22297    attributes.  */
22298
22299 static void
22300 prune_unused_types_update_strings (dw_die_ref die)
22301 {
22302   dw_attr_ref a;
22303   unsigned ix;
22304
22305   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22306     if (AT_class (a) == dw_val_class_str)
22307       {
22308         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22309         s->refcount++;
22310         /* Avoid unnecessarily putting strings that are used less than
22311            twice in the hash table.  */
22312         if (s->refcount
22313             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22314           {
22315             void ** slot;
22316             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22317                                              htab_hash_string (s->str),
22318                                              INSERT);
22319             gcc_assert (*slot == NULL);
22320             *slot = s;
22321           }
22322       }
22323 }
22324
22325 /* Remove from the tree DIE any dies that aren't marked.  */
22326
22327 static void
22328 prune_unused_types_prune (dw_die_ref die)
22329 {
22330   dw_die_ref c;
22331
22332   gcc_assert (die->die_mark);
22333   prune_unused_types_update_strings (die);
22334
22335   if (! die->die_child)
22336     return;
22337
22338   c = die->die_child;
22339   do {
22340     dw_die_ref prev = c;
22341     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22342       if (c == die->die_child)
22343         {
22344           /* No marked children between 'prev' and the end of the list.  */
22345           if (prev == c)
22346             /* No marked children at all.  */
22347             die->die_child = NULL;
22348           else
22349             {
22350               prev->die_sib = c->die_sib;
22351               die->die_child = prev;
22352             }
22353           return;
22354         }
22355
22356     if (c != prev->die_sib)
22357       prev->die_sib = c;
22358     prune_unused_types_prune (c);
22359   } while (c != die->die_child);
22360 }
22361
22362 /* A helper function for dwarf2out_finish called through
22363    htab_traverse.  Clear .debug_str strings that we haven't already
22364    decided to emit.  */
22365
22366 static int
22367 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22368 {
22369   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22370
22371   if (!node->label || !node->refcount)
22372     htab_clear_slot (debug_str_hash, h);
22373
22374   return 1;
22375 }
22376
22377 /* Remove dies representing declarations that we never use.  */
22378
22379 static void
22380 prune_unused_types (void)
22381 {
22382   unsigned int i;
22383   limbo_die_node *node;
22384   comdat_type_node *ctnode;
22385   pubname_ref pub;
22386   dcall_entry *dcall;
22387
22388 #if ENABLE_ASSERT_CHECKING
22389   /* All the marks should already be clear.  */
22390   verify_marks_clear (comp_unit_die ());
22391   for (node = limbo_die_list; node; node = node->next)
22392     verify_marks_clear (node->die);
22393   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22394     verify_marks_clear (ctnode->root_die);
22395 #endif /* ENABLE_ASSERT_CHECKING */
22396
22397   /* Mark types that are used in global variables.  */
22398   premark_types_used_by_global_vars ();
22399
22400   /* Set the mark on nodes that are actually used.  */
22401   prune_unused_types_walk (comp_unit_die ());
22402   for (node = limbo_die_list; node; node = node->next)
22403     prune_unused_types_walk (node->die);
22404   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22405     {
22406       prune_unused_types_walk (ctnode->root_die);
22407       prune_unused_types_mark (ctnode->type_die, 1);
22408     }
22409
22410   /* Also set the mark on nodes referenced from the
22411      pubname_table or arange_table.  */
22412   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22413     prune_unused_types_mark (pub->die, 1);
22414   for (i = 0; i < arange_table_in_use; i++)
22415     prune_unused_types_mark (arange_table[i], 1);
22416
22417   /* Mark nodes referenced from the direct call table.  */
22418   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
22419     prune_unused_types_mark (dcall->targ_die, 1);
22420
22421   /* Get rid of nodes that aren't marked; and update the string counts.  */
22422   if (debug_str_hash && debug_str_hash_forced)
22423     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22424   else if (debug_str_hash)
22425     htab_empty (debug_str_hash);
22426   prune_unused_types_prune (comp_unit_die ());
22427   for (node = limbo_die_list; node; node = node->next)
22428     prune_unused_types_prune (node->die);
22429   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22430     prune_unused_types_prune (ctnode->root_die);
22431
22432   /* Leave the marks clear.  */
22433   prune_unmark_dies (comp_unit_die ());
22434   for (node = limbo_die_list; node; node = node->next)
22435     prune_unmark_dies (node->die);
22436   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22437     prune_unmark_dies (ctnode->root_die);
22438 }
22439
22440 /* Set the parameter to true if there are any relative pathnames in
22441    the file table.  */
22442 static int
22443 file_table_relative_p (void ** slot, void *param)
22444 {
22445   bool *p = (bool *) param;
22446   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22447   if (!IS_ABSOLUTE_PATH (d->filename))
22448     {
22449       *p = true;
22450       return 0;
22451     }
22452   return 1;
22453 }
22454
22455 /* Routines to manipulate hash table of comdat type units.  */
22456
22457 static hashval_t
22458 htab_ct_hash (const void *of)
22459 {
22460   hashval_t h;
22461   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22462
22463   memcpy (&h, type_node->signature, sizeof (h));
22464   return h;
22465 }
22466
22467 static int
22468 htab_ct_eq (const void *of1, const void *of2)
22469 {
22470   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22471   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22472
22473   return (! memcmp (type_node_1->signature, type_node_2->signature,
22474                     DWARF_TYPE_SIGNATURE_SIZE));
22475 }
22476
22477 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22478    to the location it would have been added, should we know its
22479    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22480    probably improve compactness of debug info, removing equivalent
22481    abbrevs, and hide any differences caused by deferring the
22482    computation of the assembler name, triggered by e.g. PCH.  */
22483
22484 static inline void
22485 move_linkage_attr (dw_die_ref die)
22486 {
22487   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22488   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22489
22490   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22491               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22492
22493   while (--ix > 0)
22494     {
22495       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22496
22497       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22498         break;
22499     }
22500
22501   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22502     {
22503       VEC_pop (dw_attr_node, die->die_attr);
22504       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22505     }
22506 }
22507
22508 /* Helper function for resolve_addr, attempt to resolve
22509    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22510    SYMBOL_REFs refer to variables emitted in the current CU.  */
22511
22512 static int
22513 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22514 {
22515   rtx rtl = *addr;
22516
22517   if (GET_CODE (rtl) == CONST_STRING)
22518     {
22519       size_t len = strlen (XSTR (rtl, 0)) + 1;
22520       tree t = build_string (len, XSTR (rtl, 0));
22521       tree tlen = build_int_cst (NULL_TREE, len - 1);
22522       TREE_TYPE (t)
22523         = build_array_type (char_type_node, build_index_type (tlen));
22524       rtl = lookup_constant_def (t);
22525       if (!rtl || !MEM_P (rtl))
22526         return 1;
22527       rtl = XEXP (rtl, 0);
22528       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22529       *addr = rtl;
22530       return 0;
22531     }
22532
22533   if (GET_CODE (rtl) == SYMBOL_REF
22534       && SYMBOL_REF_DECL (rtl)
22535       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22536     return 1;
22537
22538   if (GET_CODE (rtl) == CONST
22539       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22540     return 1;
22541
22542   return 0;
22543 }
22544
22545 /* Helper function for resolve_addr, handle one location
22546    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22547    the location list couldn't be resolved.  */
22548
22549 static bool
22550 resolve_addr_in_expr (dw_loc_descr_ref loc)
22551 {
22552   for (; loc; loc = loc->dw_loc_next)
22553     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22554          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22555         || (loc->dw_loc_opc == DW_OP_implicit_value
22556             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22557             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22558       return false;
22559     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22560              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22561       {
22562         dw_die_ref ref
22563           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22564         if (ref == NULL)
22565           return false;
22566         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22567         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22568         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22569       }
22570   return true;
22571 }
22572
22573 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22574    an address in .rodata section if the string literal is emitted there,
22575    or remove the containing location list or replace DW_AT_const_value
22576    with DW_AT_location and empty location expression, if it isn't found
22577    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22578    to something that has been emitted in the current CU.  */
22579
22580 static void
22581 resolve_addr (dw_die_ref die)
22582 {
22583   dw_die_ref c;
22584   dw_attr_ref a;
22585   dw_loc_list_ref *curr;
22586   unsigned ix;
22587
22588   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22589     switch (AT_class (a))
22590       {
22591       case dw_val_class_loc_list:
22592         curr = AT_loc_list_ptr (a);
22593         while (*curr)
22594           {
22595             if (!resolve_addr_in_expr ((*curr)->expr))
22596               {
22597                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22598                 if (next && (*curr)->ll_symbol)
22599                   {
22600                     gcc_assert (!next->ll_symbol);
22601                     next->ll_symbol = (*curr)->ll_symbol;
22602                   }
22603                 *curr = next;
22604               }
22605             else
22606               curr = &(*curr)->dw_loc_next;
22607           }
22608         if (!AT_loc_list (a))
22609           {
22610             remove_AT (die, a->dw_attr);
22611             ix--;
22612           }
22613         break;
22614       case dw_val_class_loc:
22615         if (!resolve_addr_in_expr (AT_loc (a)))
22616           {
22617             remove_AT (die, a->dw_attr);
22618             ix--;
22619           }
22620         break;
22621       case dw_val_class_addr:
22622         if (a->dw_attr == DW_AT_const_value
22623             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22624           {
22625             remove_AT (die, a->dw_attr);
22626             ix--;
22627           }
22628         break;
22629       default:
22630         break;
22631       }
22632
22633   FOR_EACH_CHILD (die, c, resolve_addr (c));
22634 }
22635 \f
22636 /* Helper routines for optimize_location_lists.
22637    This pass tries to share identical local lists in .debug_loc
22638    section.  */
22639
22640 /* Iteratively hash operands of LOC opcode.  */
22641
22642 static inline hashval_t
22643 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22644 {
22645   dw_val_ref val1 = &loc->dw_loc_oprnd1;
22646   dw_val_ref val2 = &loc->dw_loc_oprnd2;
22647
22648   switch (loc->dw_loc_opc)
22649     {
22650     case DW_OP_const4u:
22651     case DW_OP_const8u:
22652       if (loc->dtprel)
22653         goto hash_addr;
22654       /* FALLTHRU */
22655     case DW_OP_const1u:
22656     case DW_OP_const1s:
22657     case DW_OP_const2u:
22658     case DW_OP_const2s:
22659     case DW_OP_const4s:
22660     case DW_OP_const8s:
22661     case DW_OP_constu:
22662     case DW_OP_consts:
22663     case DW_OP_pick:
22664     case DW_OP_plus_uconst:
22665     case DW_OP_breg0:
22666     case DW_OP_breg1:
22667     case DW_OP_breg2:
22668     case DW_OP_breg3:
22669     case DW_OP_breg4:
22670     case DW_OP_breg5:
22671     case DW_OP_breg6:
22672     case DW_OP_breg7:
22673     case DW_OP_breg8:
22674     case DW_OP_breg9:
22675     case DW_OP_breg10:
22676     case DW_OP_breg11:
22677     case DW_OP_breg12:
22678     case DW_OP_breg13:
22679     case DW_OP_breg14:
22680     case DW_OP_breg15:
22681     case DW_OP_breg16:
22682     case DW_OP_breg17:
22683     case DW_OP_breg18:
22684     case DW_OP_breg19:
22685     case DW_OP_breg20:
22686     case DW_OP_breg21:
22687     case DW_OP_breg22:
22688     case DW_OP_breg23:
22689     case DW_OP_breg24:
22690     case DW_OP_breg25:
22691     case DW_OP_breg26:
22692     case DW_OP_breg27:
22693     case DW_OP_breg28:
22694     case DW_OP_breg29:
22695     case DW_OP_breg30:
22696     case DW_OP_breg31:
22697     case DW_OP_regx:
22698     case DW_OP_fbreg:
22699     case DW_OP_piece:
22700     case DW_OP_deref_size:
22701     case DW_OP_xderef_size:
22702       hash = iterative_hash_object (val1->v.val_int, hash);
22703       break;
22704     case DW_OP_skip:
22705     case DW_OP_bra:
22706       {
22707         int offset;
22708
22709         gcc_assert (val1->val_class == dw_val_class_loc);
22710         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22711         hash = iterative_hash_object (offset, hash);
22712       }
22713       break;
22714     case DW_OP_implicit_value:
22715       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22716       switch (val2->val_class)
22717         {
22718         case dw_val_class_const:
22719           hash = iterative_hash_object (val2->v.val_int, hash);
22720           break;
22721         case dw_val_class_vec:
22722           {
22723             unsigned int elt_size = val2->v.val_vec.elt_size;
22724             unsigned int len = val2->v.val_vec.length;
22725
22726             hash = iterative_hash_object (elt_size, hash);
22727             hash = iterative_hash_object (len, hash);
22728             hash = iterative_hash (val2->v.val_vec.array,
22729                                    len * elt_size, hash);
22730           }
22731           break;
22732         case dw_val_class_const_double:
22733           hash = iterative_hash_object (val2->v.val_double.low, hash);
22734           hash = iterative_hash_object (val2->v.val_double.high, hash);
22735           break;
22736         case dw_val_class_addr:
22737           hash = iterative_hash_rtx (val2->v.val_addr, hash);
22738           break;
22739         default:
22740           gcc_unreachable ();
22741         }
22742       break;
22743     case DW_OP_bregx:
22744     case DW_OP_bit_piece:
22745       hash = iterative_hash_object (val1->v.val_int, hash);
22746       hash = iterative_hash_object (val2->v.val_int, hash);
22747       break;
22748     case DW_OP_addr:
22749     hash_addr:
22750       if (loc->dtprel)
22751         {
22752           unsigned char dtprel = 0xd1;
22753           hash = iterative_hash_object (dtprel, hash);
22754         }
22755       hash = iterative_hash_rtx (val1->v.val_addr, hash);
22756       break;
22757     case DW_OP_GNU_implicit_pointer:
22758       hash = iterative_hash_object (val2->v.val_int, hash);
22759       break;
22760
22761     default:
22762       /* Other codes have no operands.  */
22763       break;
22764     }
22765   return hash;
22766 }
22767
22768 /* Iteratively hash the whole DWARF location expression LOC.  */
22769
22770 static inline hashval_t
22771 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22772 {
22773   dw_loc_descr_ref l;
22774   bool sizes_computed = false;
22775   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22776   size_of_locs (loc);
22777
22778   for (l = loc; l != NULL; l = l->dw_loc_next)
22779     {
22780       enum dwarf_location_atom opc = l->dw_loc_opc;
22781       hash = iterative_hash_object (opc, hash);
22782       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22783         {
22784           size_of_locs (loc);
22785           sizes_computed = true;
22786         }
22787       hash = hash_loc_operands (l, hash);
22788     }
22789   return hash;
22790 }
22791
22792 /* Compute hash of the whole location list LIST_HEAD.  */
22793
22794 static inline void
22795 hash_loc_list (dw_loc_list_ref list_head)
22796 {
22797   dw_loc_list_ref curr = list_head;
22798   hashval_t hash = 0;
22799
22800   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22801     {
22802       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22803       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22804       if (curr->section)
22805         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22806                                hash);
22807       hash = hash_locs (curr->expr, hash);
22808     }
22809   list_head->hash = hash;
22810 }
22811
22812 /* Return true if X and Y opcodes have the same operands.  */
22813
22814 static inline bool
22815 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22816 {
22817   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22818   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22819   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22820   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22821
22822   switch (x->dw_loc_opc)
22823     {
22824     case DW_OP_const4u:
22825     case DW_OP_const8u:
22826       if (x->dtprel)
22827         goto hash_addr;
22828       /* FALLTHRU */
22829     case DW_OP_const1u:
22830     case DW_OP_const1s:
22831     case DW_OP_const2u:
22832     case DW_OP_const2s:
22833     case DW_OP_const4s:
22834     case DW_OP_const8s:
22835     case DW_OP_constu:
22836     case DW_OP_consts:
22837     case DW_OP_pick:
22838     case DW_OP_plus_uconst:
22839     case DW_OP_breg0:
22840     case DW_OP_breg1:
22841     case DW_OP_breg2:
22842     case DW_OP_breg3:
22843     case DW_OP_breg4:
22844     case DW_OP_breg5:
22845     case DW_OP_breg6:
22846     case DW_OP_breg7:
22847     case DW_OP_breg8:
22848     case DW_OP_breg9:
22849     case DW_OP_breg10:
22850     case DW_OP_breg11:
22851     case DW_OP_breg12:
22852     case DW_OP_breg13:
22853     case DW_OP_breg14:
22854     case DW_OP_breg15:
22855     case DW_OP_breg16:
22856     case DW_OP_breg17:
22857     case DW_OP_breg18:
22858     case DW_OP_breg19:
22859     case DW_OP_breg20:
22860     case DW_OP_breg21:
22861     case DW_OP_breg22:
22862     case DW_OP_breg23:
22863     case DW_OP_breg24:
22864     case DW_OP_breg25:
22865     case DW_OP_breg26:
22866     case DW_OP_breg27:
22867     case DW_OP_breg28:
22868     case DW_OP_breg29:
22869     case DW_OP_breg30:
22870     case DW_OP_breg31:
22871     case DW_OP_regx:
22872     case DW_OP_fbreg:
22873     case DW_OP_piece:
22874     case DW_OP_deref_size:
22875     case DW_OP_xderef_size:
22876       return valx1->v.val_int == valy1->v.val_int;
22877     case DW_OP_skip:
22878     case DW_OP_bra:
22879       gcc_assert (valx1->val_class == dw_val_class_loc
22880                   && valy1->val_class == dw_val_class_loc
22881                   && x->dw_loc_addr == y->dw_loc_addr);
22882       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22883     case DW_OP_implicit_value:
22884       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22885           || valx2->val_class != valy2->val_class)
22886         return false;
22887       switch (valx2->val_class)
22888         {
22889         case dw_val_class_const:
22890           return valx2->v.val_int == valy2->v.val_int;
22891         case dw_val_class_vec:
22892           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22893                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22894                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22895                             valx2->v.val_vec.elt_size
22896                             * valx2->v.val_vec.length) == 0;
22897         case dw_val_class_const_double:
22898           return valx2->v.val_double.low == valy2->v.val_double.low
22899                  && valx2->v.val_double.high == valy2->v.val_double.high;
22900         case dw_val_class_addr:
22901           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22902         default:
22903           gcc_unreachable ();
22904         }
22905     case DW_OP_bregx:
22906     case DW_OP_bit_piece:
22907       return valx1->v.val_int == valy1->v.val_int
22908              && valx2->v.val_int == valy2->v.val_int;
22909     case DW_OP_addr:
22910     hash_addr:
22911       return rtx_equal_p (valx1->v.val_addr, valx2->v.val_addr);
22912     case DW_OP_GNU_implicit_pointer:
22913       return valx1->val_class == dw_val_class_die_ref
22914              && valx1->val_class == valy1->val_class
22915              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22916              && valx2->v.val_int == valy2->v.val_int;
22917     default:
22918       /* Other codes have no operands.  */
22919       return true;
22920     }
22921 }
22922
22923 /* Return true if DWARF location expressions X and Y are the same.  */
22924
22925 static inline bool
22926 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22927 {
22928   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22929     if (x->dw_loc_opc != y->dw_loc_opc
22930         || x->dtprel != y->dtprel
22931         || !compare_loc_operands (x, y))
22932       break;
22933   return x == NULL && y == NULL;
22934 }
22935
22936 /* Return precomputed hash of location list X.  */
22937
22938 static hashval_t
22939 loc_list_hash (const void *x)
22940 {
22941   return ((const struct dw_loc_list_struct *) x)->hash;
22942 }
22943
22944 /* Return 1 if location lists X and Y are the same.  */
22945
22946 static int
22947 loc_list_eq (const void *x, const void *y)
22948 {
22949   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22950   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22951   if (a == b)
22952     return 1;
22953   if (a->hash != b->hash)
22954     return 0;
22955   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22956     if (strcmp (a->begin, b->begin) != 0
22957         || strcmp (a->end, b->end) != 0
22958         || (a->section == NULL) != (b->section == NULL)
22959         || (a->section && strcmp (a->section, b->section) != 0)
22960         || !compare_locs (a->expr, b->expr))
22961       break;
22962   return a == NULL && b == NULL;
22963 }
22964
22965 /* Recursively optimize location lists referenced from DIE
22966    children and share them whenever possible.  */
22967
22968 static void
22969 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22970 {
22971   dw_die_ref c;
22972   dw_attr_ref a;
22973   unsigned ix;
22974   void **slot;
22975
22976   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22977     if (AT_class (a) == dw_val_class_loc_list)
22978       {
22979         dw_loc_list_ref list = AT_loc_list (a);
22980         /* TODO: perform some optimizations here, before hashing
22981            it and storing into the hash table.  */
22982         hash_loc_list (list);
22983         slot = htab_find_slot_with_hash (htab, list, list->hash,
22984                                          INSERT);
22985         if (*slot == NULL)
22986           *slot = (void *) list;
22987         else
22988           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22989       }
22990
22991   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22992 }
22993
22994 /* Optimize location lists referenced from DIE
22995    children and share them whenever possible.  */
22996
22997 static void
22998 optimize_location_lists (dw_die_ref die)
22999 {
23000   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23001   optimize_location_lists_1 (die, htab);
23002   htab_delete (htab);
23003 }
23004 \f
23005 /* Output stuff that dwarf requires at the end of every file,
23006    and generate the DWARF-2 debugging info.  */
23007
23008 static void
23009 dwarf2out_finish (const char *filename)
23010 {
23011   limbo_die_node *node, *next_node;
23012   comdat_type_node *ctnode;
23013   htab_t comdat_type_table;
23014   unsigned int i;
23015
23016   gen_remaining_tmpl_value_param_die_attribute ();
23017
23018   /* Add the name for the main input file now.  We delayed this from
23019      dwarf2out_init to avoid complications with PCH.  */
23020   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23021   if (!IS_ABSOLUTE_PATH (filename))
23022     add_comp_dir_attribute (comp_unit_die ());
23023   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23024     {
23025       bool p = false;
23026       htab_traverse (file_table, file_table_relative_p, &p);
23027       if (p)
23028         add_comp_dir_attribute (comp_unit_die ());
23029     }
23030
23031   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23032     {
23033       add_location_or_const_value_attribute (
23034         VEC_index (deferred_locations, deferred_locations_list, i)->die,
23035         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23036         DW_AT_location);
23037     }
23038
23039   /* Traverse the limbo die list, and add parent/child links.  The only
23040      dies without parents that should be here are concrete instances of
23041      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23042      For concrete instances, we can get the parent die from the abstract
23043      instance.  */
23044   for (node = limbo_die_list; node; node = next_node)
23045     {
23046       dw_die_ref die = node->die;
23047       next_node = node->next;
23048
23049       if (die->die_parent == NULL)
23050         {
23051           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23052
23053           if (origin)
23054             add_child_die (origin->die_parent, die);
23055           else if (is_cu_die (die))
23056             ;
23057           else if (seen_error ())
23058             /* It's OK to be confused by errors in the input.  */
23059             add_child_die (comp_unit_die (), die);
23060           else
23061             {
23062               /* In certain situations, the lexical block containing a
23063                  nested function can be optimized away, which results
23064                  in the nested function die being orphaned.  Likewise
23065                  with the return type of that nested function.  Force
23066                  this to be a child of the containing function.
23067
23068                  It may happen that even the containing function got fully
23069                  inlined and optimized out.  In that case we are lost and
23070                  assign the empty child.  This should not be big issue as
23071                  the function is likely unreachable too.  */
23072               tree context = NULL_TREE;
23073
23074               gcc_assert (node->created_for);
23075
23076               if (DECL_P (node->created_for))
23077                 context = DECL_CONTEXT (node->created_for);
23078               else if (TYPE_P (node->created_for))
23079                 context = TYPE_CONTEXT (node->created_for);
23080
23081               gcc_assert (context
23082                           && (TREE_CODE (context) == FUNCTION_DECL
23083                               || TREE_CODE (context) == NAMESPACE_DECL));
23084
23085               origin = lookup_decl_die (context);
23086               if (origin)
23087                 add_child_die (origin, die);
23088               else
23089                 add_child_die (comp_unit_die (), die);
23090             }
23091         }
23092     }
23093
23094   limbo_die_list = NULL;
23095
23096   resolve_addr (comp_unit_die ());
23097
23098   for (node = deferred_asm_name; node; node = node->next)
23099     {
23100       tree decl = node->created_for;
23101       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23102         {
23103           add_linkage_attr (node->die, decl);
23104           move_linkage_attr (node->die);
23105         }
23106     }
23107
23108   deferred_asm_name = NULL;
23109
23110   /* Walk through the list of incomplete types again, trying once more to
23111      emit full debugging info for them.  */
23112   retry_incomplete_types ();
23113
23114   if (flag_eliminate_unused_debug_types)
23115     prune_unused_types ();
23116
23117   /* Generate separate CUs for each of the include files we've seen.
23118      They will go into limbo_die_list.  */
23119   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23120     break_out_includes (comp_unit_die ());
23121
23122   /* Generate separate COMDAT sections for type DIEs. */
23123   if (dwarf_version >= 4)
23124     {
23125       break_out_comdat_types (comp_unit_die ());
23126
23127       /* Each new type_unit DIE was added to the limbo die list when created.
23128          Since these have all been added to comdat_type_list, clear the
23129          limbo die list.  */
23130       limbo_die_list = NULL;
23131
23132       /* For each new comdat type unit, copy declarations for incomplete
23133          types to make the new unit self-contained (i.e., no direct
23134          references to the main compile unit).  */
23135       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23136         copy_decls_for_unworthy_types (ctnode->root_die);
23137       copy_decls_for_unworthy_types (comp_unit_die ());
23138
23139       /* In the process of copying declarations from one unit to another,
23140          we may have left some declarations behind that are no longer
23141          referenced.  Prune them.  */
23142       prune_unused_types ();
23143     }
23144
23145   /* Traverse the DIE's and add add sibling attributes to those DIE's
23146      that have children.  */
23147   add_sibling_attributes (comp_unit_die ());
23148   for (node = limbo_die_list; node; node = node->next)
23149     add_sibling_attributes (node->die);
23150   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23151     add_sibling_attributes (ctnode->root_die);
23152
23153   /* Output a terminator label for the .text section.  */
23154   switch_to_section (text_section);
23155   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23156   if (cold_text_section)
23157     {
23158       switch_to_section (cold_text_section);
23159       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23160     }
23161
23162   /* We can only use the low/high_pc attributes if all of the code was
23163      in .text.  */
23164   if (!have_multiple_function_sections
23165       || !(dwarf_version >= 3 || !dwarf_strict))
23166     {
23167       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23168       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23169     }
23170
23171   else
23172     {
23173       unsigned fde_idx = 0;
23174       bool range_list_added = false;
23175
23176       /* We need to give .debug_loc and .debug_ranges an appropriate
23177          "base address".  Use zero so that these addresses become
23178          absolute.  Historically, we've emitted the unexpected
23179          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23180          Emit both to give time for other tools to adapt.  */
23181       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23182       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23183
23184       if (text_section_used)
23185         add_ranges_by_labels (comp_unit_die (), text_section_label,
23186                               text_end_label, &range_list_added);
23187       if (flag_reorder_blocks_and_partition && cold_text_section_used)
23188         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23189                               cold_end_label, &range_list_added);
23190
23191       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23192         {
23193           dw_fde_ref fde = &fde_table[fde_idx];
23194
23195           if (fde->dw_fde_switched_sections)
23196             {
23197               if (!fde->in_std_section)
23198                 add_ranges_by_labels (comp_unit_die (),
23199                                       fde->dw_fde_hot_section_label,
23200                                       fde->dw_fde_hot_section_end_label,
23201                                       &range_list_added);
23202               if (!fde->cold_in_std_section)
23203                 add_ranges_by_labels (comp_unit_die (),
23204                                       fde->dw_fde_unlikely_section_label,
23205                                       fde->dw_fde_unlikely_section_end_label,
23206                                       &range_list_added);
23207             }
23208           else if (!fde->in_std_section)
23209             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23210                                   fde->dw_fde_end, &range_list_added);
23211         }
23212
23213       if (range_list_added)
23214         add_ranges (NULL);
23215     }
23216
23217   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23218     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23219                     debug_line_section_label);
23220
23221   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23222     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23223
23224   if (have_location_lists)
23225     optimize_location_lists (comp_unit_die ());
23226
23227   /* Output all of the compilation units.  We put the main one last so that
23228      the offsets are available to output_pubnames.  */
23229   for (node = limbo_die_list; node; node = node->next)
23230     output_comp_unit (node->die, 0);
23231
23232   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23233   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23234     {
23235       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23236
23237       /* Don't output duplicate types.  */
23238       if (*slot != HTAB_EMPTY_ENTRY)
23239         continue;
23240
23241       /* Add a pointer to the line table for the main compilation unit
23242          so that the debugger can make sense of DW_AT_decl_file
23243          attributes.  */
23244       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23245         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23246                         debug_line_section_label);
23247
23248       output_comdat_type_unit (ctnode);
23249       *slot = ctnode;
23250     }
23251   htab_delete (comdat_type_table);
23252
23253   /* Output the main compilation unit if non-empty or if .debug_macinfo
23254      will be emitted.  */
23255   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23256
23257   /* Output the abbreviation table.  */
23258   switch_to_section (debug_abbrev_section);
23259   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23260   output_abbrev_section ();
23261
23262   /* Output location list section if necessary.  */
23263   if (have_location_lists)
23264     {
23265       /* Output the location lists info.  */
23266       switch_to_section (debug_loc_section);
23267       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23268                                    DEBUG_LOC_SECTION_LABEL, 0);
23269       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23270       output_location_lists (comp_unit_die ());
23271     }
23272
23273   /* Output public names table if necessary.  */
23274   if (!VEC_empty (pubname_entry, pubname_table))
23275     {
23276       gcc_assert (info_section_emitted);
23277       switch_to_section (debug_pubnames_section);
23278       output_pubnames (pubname_table);
23279     }
23280
23281   /* Output public types table if necessary.  */
23282   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23283      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23284      simply won't look for the section.  */
23285   if (!VEC_empty (pubname_entry, pubtype_table))
23286     {
23287       bool empty = false;
23288       
23289       if (flag_eliminate_unused_debug_types)
23290         {
23291           /* The pubtypes table might be emptied by pruning unused items.  */
23292           unsigned i;
23293           pubname_ref p;
23294           empty = true;
23295           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23296             if (p->die->die_offset != 0)
23297               {
23298                 empty = false;
23299                 break;
23300               }
23301         }
23302       if (!empty)
23303         {
23304           gcc_assert (info_section_emitted);
23305           switch_to_section (debug_pubtypes_section);
23306           output_pubnames (pubtype_table);
23307         }
23308     }
23309
23310   /* Output direct and virtual call tables if necessary.  */
23311   if (!VEC_empty (dcall_entry, dcall_table))
23312     {
23313       switch_to_section (debug_dcall_section);
23314       output_dcall_table ();
23315     }
23316   if (!VEC_empty (vcall_entry, vcall_table))
23317     {
23318       switch_to_section (debug_vcall_section);
23319       output_vcall_table ();
23320     }
23321
23322   /* Output the address range information.  We only put functions in the arange
23323      table, so don't write it out if we don't have any.  */
23324   if (fde_table_in_use)
23325     {
23326       switch_to_section (debug_aranges_section);
23327       output_aranges ();
23328     }
23329
23330   /* Output ranges section if necessary.  */
23331   if (ranges_table_in_use)
23332     {
23333       switch_to_section (debug_ranges_section);
23334       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23335       output_ranges ();
23336     }
23337
23338   /* Output the source line correspondence table.  We must do this
23339      even if there is no line information.  Otherwise, on an empty
23340      translation unit, we will generate a present, but empty,
23341      .debug_info section.  IRIX 6.5 `nm' will then complain when
23342      examining the file.  This is done late so that any filenames
23343      used by the debug_info section are marked as 'used'.  */
23344   switch_to_section (debug_line_section);
23345   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23346   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23347     output_line_info ();
23348
23349   /* Have to end the macro section.  */
23350   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23351     {
23352       switch_to_section (debug_macinfo_section);
23353       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23354       if (!VEC_empty (macinfo_entry, macinfo_table))
23355         output_macinfo ();
23356       dw2_asm_output_data (1, 0, "End compilation unit");
23357     }
23358
23359   /* If we emitted any DW_FORM_strp form attribute, output the string
23360      table too.  */
23361   if (debug_str_hash)
23362     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23363 }
23364
23365 #include "gt-dwarf2out.h"