c1c22b46323f7be7d2df06f18172249a5c4af2b2
[platform/upstream/linaro-gcc.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, 2011
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 #include "cfglayout.h"
96
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
99
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102
103 /* Define this macro to be a nonzero value if the directory specifications
104     which are output in the debug info should end with a separator.  */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107    from generating indirect strings in DWARF2 debug information, for instance
108    if your target is stuck with an old version of GDB that is unable to
109    process them properly or uses VMS Debug.  */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
115
116 /* ??? Poison these here until it can be done generically.  They've been
117    totally replaced in this file; make sure it stays that way.  */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
123
124 #ifndef INCOMING_RETURN_ADDR_RTX
125 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
126 #endif
127
128 /* Map register numbers held in the call frame info that gcc has
129    collected using DWARF_FRAME_REGNUM to those that should be output in
130    .debug_frame and .eh_frame.  */
131 #ifndef DWARF2_FRAME_REG_OUT
132 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
133 #endif
134
135 /* Save the result of dwarf2out_do_frame across PCH.  */
136 static GTY(()) bool saved_do_cfi_asm = 0;
137
138 /* Decide whether we want to emit frame unwind information for the current
139    translation unit.  */
140
141 int
142 dwarf2out_do_frame (void)
143 {
144   /* We want to emit correct CFA location expressions or lists, so we
145      have to return true if we're going to output debug info, even if
146      we're not going to output frame or unwind info.  */
147   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
148     return true;
149
150   if (saved_do_cfi_asm)
151     return true;
152
153   if (targetm.debug_unwind_info () == UI_DWARF2)
154     return true;
155
156   if ((flag_unwind_tables || flag_exceptions)
157       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
158     return true;
159
160   return false;
161 }
162
163 /* Decide whether to emit frame unwind via assembler directives.  */
164
165 int
166 dwarf2out_do_cfi_asm (void)
167 {
168   int enc;
169
170 #ifdef MIPS_DEBUGGING_INFO
171   return false;
172 #endif
173   if (saved_do_cfi_asm)
174     return true;
175   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
176     return false;
177   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
178     return false;
179
180   /* Make sure the personality encoding is one the assembler can support.
181      In particular, aligned addresses can't be handled.  */
182   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
183   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184     return false;
185   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
186   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
187     return false;
188
189   /* If we can't get the assembler to emit only .debug_frame, and we don't need
190      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
191   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
192       && !flag_unwind_tables && !flag_exceptions
193       && targetm.except_unwind_info (&global_options) != UI_DWARF2)
194     return false;
195
196   saved_do_cfi_asm = true;
197   return true;
198 }
199
200 /* The size of the target's pointer type.  */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
204
205 /* Array of RTXes referenced by the debugging information, which therefore
206    must be kept around forever.  */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
208
209 /* A pointer to the base of a list of incomplete types which might be
210    completed at some later time.  incomplete_types_list needs to be a
211    VEC(tree,gc) because we want to tell the garbage collector about
212    it.  */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
214
215 /* A pointer to the base of a table of references to declaration
216    scopes.  This table is a display which tracks the nesting
217    of declaration scopes at the current scope and containing
218    scopes.  This table is used to find the proper place to
219    define type declaration DIE's.  */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
221
222 /* Pointers to various DWARF2 sections.  */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_str_section;
232 static GTY(()) section *debug_ranges_section;
233 static GTY(()) section *debug_frame_section;
234
235 /* Personality decl of current unit.  Used only when assembler does not support
236    personality CFI.  */
237 static GTY(()) rtx current_unit_personality;
238
239 /* How to start an assembler comment.  */
240 #ifndef ASM_COMMENT_START
241 #define ASM_COMMENT_START ";#"
242 #endif
243
244 typedef struct dw_cfi_struct *dw_cfi_ref;
245 typedef struct dw_fde_struct *dw_fde_ref;
246 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
247
248 /* Call frames are described using a sequence of Call Frame
249    Information instructions.  The register number, offset
250    and address fields are provided as possible operands;
251    their use is selected by the opcode field.  */
252
253 enum dw_cfi_oprnd_type {
254   dw_cfi_oprnd_unused,
255   dw_cfi_oprnd_reg_num,
256   dw_cfi_oprnd_offset,
257   dw_cfi_oprnd_addr,
258   dw_cfi_oprnd_loc
259 };
260
261 typedef union GTY(()) dw_cfi_oprnd_struct {
262   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
263   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
264   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
265   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
266 }
267 dw_cfi_oprnd;
268
269 typedef struct GTY(()) dw_cfi_struct {
270   enum dwarf_call_frame_info dw_cfi_opc;
271   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
272     dw_cfi_oprnd1;
273   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
274     dw_cfi_oprnd2;
275 }
276 dw_cfi_node;
277
278 DEF_VEC_P (dw_cfi_ref);
279 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
280 DEF_VEC_ALLOC_P (dw_cfi_ref, gc);
281
282 typedef VEC(dw_cfi_ref, gc) *cfi_vec;
283
284 /* This is how we define the location of the CFA. We use to handle it
285    as REG + OFFSET all the time,  but now it can be more complex.
286    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
287    Instead of passing around REG and OFFSET, we pass a copy
288    of this structure.  */
289 typedef struct cfa_loc {
290   HOST_WIDE_INT offset;
291   HOST_WIDE_INT base_offset;
292   unsigned int reg;
293   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
294   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
295 } dw_cfa_location;
296
297 /* All call frame descriptions (FDE's) in the GCC generated DWARF
298    refer to a single Common Information Entry (CIE), defined at
299    the beginning of the .debug_frame section.  This use of a single
300    CIE obviates the need to keep track of multiple CIE's
301    in the DWARF generation routines below.  */
302
303 typedef struct GTY(()) dw_fde_struct {
304   tree decl;
305   const char *dw_fde_begin;
306   const char *dw_fde_current_label;
307   const char *dw_fde_end;
308   const char *dw_fde_vms_end_prologue;
309   const char *dw_fde_vms_begin_epilogue;
310   const char *dw_fde_second_begin;
311   const char *dw_fde_second_end;
312   cfi_vec dw_fde_cfi;
313   int dw_fde_switch_cfi_index; /* Last CFI before switching sections.  */
314   HOST_WIDE_INT stack_realignment;
315   unsigned funcdef_number;
316   /* Dynamic realign argument pointer register.  */
317   unsigned int drap_reg;
318   /* Virtual dynamic realign argument pointer register.  */
319   unsigned int vdrap_reg;
320   /* These 3 flags are copied from rtl_data in function.h.  */
321   unsigned all_throwers_are_sibcalls : 1;
322   unsigned uses_eh_lsda : 1;
323   unsigned nothrow : 1;
324   /* Whether we did stack realign in this call frame.  */
325   unsigned stack_realign : 1;
326   /* Whether dynamic realign argument pointer register has been saved.  */
327   unsigned drap_reg_saved: 1;
328   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
329   unsigned in_std_section : 1;
330   /* True iff dw_fde_second_begin label is in text_section or
331      cold_text_section.  */
332   unsigned second_in_std_section : 1;
333 }
334 dw_fde_node;
335
336 /* Maximum size (in bytes) of an artificially generated label.  */
337 #define MAX_ARTIFICIAL_LABEL_BYTES      30
338
339 /* The size of addresses as they appear in the Dwarf 2 data.
340    Some architectures use word addresses to refer to code locations,
341    but Dwarf 2 info always uses byte addresses.  On such machines,
342    Dwarf 2 addresses need to be larger than the architecture's
343    pointers.  */
344 #ifndef DWARF2_ADDR_SIZE
345 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
346 #endif
347
348 /* The size in bytes of a DWARF field indicating an offset or length
349    relative to a debug info section, specified to be 4 bytes in the
350    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
351    as PTR_SIZE.  */
352
353 #ifndef DWARF_OFFSET_SIZE
354 #define DWARF_OFFSET_SIZE 4
355 #endif
356
357 /* The size in bytes of a DWARF 4 type signature.  */
358
359 #ifndef DWARF_TYPE_SIGNATURE_SIZE
360 #define DWARF_TYPE_SIGNATURE_SIZE 8
361 #endif
362
363 /* According to the (draft) DWARF 3 specification, the initial length
364    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
365    bytes are 0xffffffff, followed by the length stored in the next 8
366    bytes.
367
368    However, the SGI/MIPS ABI uses an initial length which is equal to
369    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
370
371 #ifndef DWARF_INITIAL_LENGTH_SIZE
372 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
373 #endif
374
375 /* Round SIZE up to the nearest BOUNDARY.  */
376 #define DWARF_ROUND(SIZE,BOUNDARY) \
377   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
378
379 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
380 #ifndef DWARF_CIE_DATA_ALIGNMENT
381 #ifdef STACK_GROWS_DOWNWARD
382 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
383 #else
384 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
385 #endif
386 #endif
387
388 /* CIE identifier.  */
389 #if HOST_BITS_PER_WIDE_INT >= 64
390 #define DWARF_CIE_ID \
391   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
392 #else
393 #define DWARF_CIE_ID DW_CIE_ID
394 #endif
395
396 /* A pointer to the base of a table that contains frame description
397    information for each routine.  */
398 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
399
400 /* Number of elements currently allocated for fde_table.  */
401 static GTY(()) unsigned fde_table_allocated;
402
403 /* Number of elements in fde_table currently in use.  */
404 static GTY(()) unsigned fde_table_in_use;
405
406 /* Size (in elements) of increments by which we may expand the
407    fde_table.  */
408 #define FDE_TABLE_INCREMENT 256
409
410 /* Get the current fde_table entry we should use.  */
411
412 static inline dw_fde_ref
413 current_fde (void)
414 {
415   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
416 }
417
418 /* A vector of call frame insns for the CIE.  */
419 static GTY(()) cfi_vec cie_cfi_vec;
420
421 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
422    attribute that accelerates the lookup of the FDE associated
423    with the subprogram.  This variable holds the table index of the FDE
424    associated with the current function (body) definition.  */
425 static unsigned current_funcdef_fde;
426
427 struct GTY(()) indirect_string_node {
428   const char *str;
429   unsigned int refcount;
430   enum dwarf_form form;
431   char *label;
432 };
433
434 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
435
436 static GTY(()) int dw2_string_counter;
437 static GTY(()) unsigned long dwarf2out_cfi_label_num;
438
439 /* True if the compilation unit places functions in more than one section.  */
440 static GTY(()) bool have_multiple_function_sections = false;
441
442 /* Whether the default text and cold text sections have been used at all.  */
443
444 static GTY(()) bool text_section_used = false;
445 static GTY(()) bool cold_text_section_used = false;
446
447 /* The default cold text section.  */
448 static GTY(()) section *cold_text_section;
449
450 /* Forward declarations for functions defined in this file.  */
451
452 static char *stripattributes (const char *);
453 static const char *dwarf_cfi_name (unsigned);
454 static dw_cfi_ref new_cfi (void);
455 static void add_cfi (cfi_vec *, dw_cfi_ref);
456 static void add_fde_cfi (const char *, dw_cfi_ref);
457 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
458 static void lookup_cfa (dw_cfa_location *);
459 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
460 static void initial_return_save (rtx);
461 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
462                                           HOST_WIDE_INT);
463 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
464 static void output_cfi_directive (dw_cfi_ref);
465 static void output_call_frame_info (int);
466 static void dwarf2out_note_section_used (void);
467 static bool clobbers_queued_reg_save (const_rtx);
468 static void dwarf2out_frame_debug_expr (rtx, const char *);
469
470 /* Support for complex CFA locations.  */
471 static void output_cfa_loc (dw_cfi_ref, int);
472 static void output_cfa_loc_raw (dw_cfi_ref);
473 static void get_cfa_from_loc_descr (dw_cfa_location *,
474                                     struct dw_loc_descr_struct *);
475 static struct dw_loc_descr_struct *build_cfa_loc
476   (dw_cfa_location *, HOST_WIDE_INT);
477 static struct dw_loc_descr_struct *build_cfa_aligned_loc
478   (HOST_WIDE_INT, HOST_WIDE_INT);
479 static void def_cfa_1 (const char *, dw_cfa_location *);
480 static struct dw_loc_descr_struct *mem_loc_descriptor
481   (rtx, enum machine_mode mode, enum machine_mode mem_mode,
482    enum var_init_status);
483
484 /* How to start an assembler comment.  */
485 #ifndef ASM_COMMENT_START
486 #define ASM_COMMENT_START ";#"
487 #endif
488
489 /* Data and reference forms for relocatable data.  */
490 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
491 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
492
493 #ifndef DEBUG_FRAME_SECTION
494 #define DEBUG_FRAME_SECTION     ".debug_frame"
495 #endif
496
497 #ifndef FUNC_BEGIN_LABEL
498 #define FUNC_BEGIN_LABEL        "LFB"
499 #endif
500
501 #ifndef FUNC_END_LABEL
502 #define FUNC_END_LABEL          "LFE"
503 #endif
504
505 #ifndef PROLOGUE_END_LABEL
506 #define PROLOGUE_END_LABEL      "LPE"
507 #endif
508
509 #ifndef EPILOGUE_BEGIN_LABEL
510 #define EPILOGUE_BEGIN_LABEL    "LEB"
511 #endif
512
513 #ifndef FRAME_BEGIN_LABEL
514 #define FRAME_BEGIN_LABEL       "Lframe"
515 #endif
516 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
517 #define CIE_END_LABEL           "LECIE"
518 #define FDE_LABEL               "LSFDE"
519 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
520 #define FDE_END_LABEL           "LEFDE"
521 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
522 #define LINE_NUMBER_END_LABEL   "LELT"
523 #define LN_PROLOG_AS_LABEL      "LASLTP"
524 #define LN_PROLOG_END_LABEL     "LELTP"
525 #define DIE_LABEL_PREFIX        "DW"
526
527 /* The DWARF 2 CFA column which tracks the return address.  Normally this
528    is the column for PC, or the first column after all of the hard
529    registers.  */
530 #ifndef DWARF_FRAME_RETURN_COLUMN
531 #ifdef PC_REGNUM
532 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
533 #else
534 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
535 #endif
536 #endif
537
538 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
539    default, we just provide columns for all registers.  */
540 #ifndef DWARF_FRAME_REGNUM
541 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
542 #endif
543 \f
544 /* Match the base name of a file to the base name of a compilation unit. */
545
546 static int
547 matches_main_base (const char *path)
548 {
549   /* Cache the last query. */
550   static const char *last_path = NULL;
551   static int last_match = 0;
552   if (path != last_path)
553     {
554       const char *base;
555       int length = base_of_path (path, &base);
556       last_path = path;
557       last_match = (length == main_input_baselength
558                     && memcmp (base, main_input_basename, length) == 0);
559     }
560   return last_match;
561 }
562
563 #ifdef DEBUG_DEBUG_STRUCT
564
565 static int
566 dump_struct_debug (tree type, enum debug_info_usage usage,
567                    enum debug_struct_file criterion, int generic,
568                    int matches, int result)
569 {
570   /* Find the type name. */
571   tree type_decl = TYPE_STUB_DECL (type);
572   tree t = type_decl;
573   const char *name = 0;
574   if (TREE_CODE (t) == TYPE_DECL)
575     t = DECL_NAME (t);
576   if (t)
577     name = IDENTIFIER_POINTER (t);
578
579   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
580            criterion,
581            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
582            matches ? "bas" : "hdr",
583            generic ? "gen" : "ord",
584            usage == DINFO_USAGE_DFN ? ";" :
585              usage == DINFO_USAGE_DIR_USE ? "." : "*",
586            result,
587            (void*) type_decl, name);
588   return result;
589 }
590 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
591   dump_struct_debug (type, usage, criterion, generic, matches, result)
592
593 #else
594
595 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
596   (result)
597
598 #endif
599
600 static bool
601 should_emit_struct_debug (tree type, enum debug_info_usage usage)
602 {
603   enum debug_struct_file criterion;
604   tree type_decl;
605   bool generic = lang_hooks.types.generic_p (type);
606
607   if (generic)
608     criterion = debug_struct_generic[usage];
609   else
610     criterion = debug_struct_ordinary[usage];
611
612   if (criterion == DINFO_STRUCT_FILE_NONE)
613     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
614   if (criterion == DINFO_STRUCT_FILE_ANY)
615     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
616
617   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
618
619   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
620     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
621
622   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
623     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
624   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
625 }
626 \f
627 /* Hook used by __throw.  */
628
629 rtx
630 expand_builtin_dwarf_sp_column (void)
631 {
632   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
633   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
634 }
635
636 /* Return a pointer to a copy of the section string name S with all
637    attributes stripped off, and an asterisk prepended (for assemble_name).  */
638
639 static inline char *
640 stripattributes (const char *s)
641 {
642   char *stripped = XNEWVEC (char, strlen (s) + 2);
643   char *p = stripped;
644
645   *p++ = '*';
646
647   while (*s && *s != ',')
648     *p++ = *s++;
649
650   *p = '\0';
651   return stripped;
652 }
653
654 /* MEM is a memory reference for the register size table, each element of
655    which has mode MODE.  Initialize column C as a return address column.  */
656
657 static void
658 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
659 {
660   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
661   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
662   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
663 }
664
665 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
666
667 static inline HOST_WIDE_INT
668 div_data_align (HOST_WIDE_INT off)
669 {
670   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
671   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
672   return r;
673 }
674
675 /* Return true if we need a signed version of a given opcode
676    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
677
678 static inline bool
679 need_data_align_sf_opcode (HOST_WIDE_INT off)
680 {
681   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
682 }
683
684 /* Generate code to initialize the register size table.  */
685
686 void
687 expand_builtin_init_dwarf_reg_sizes (tree address)
688 {
689   unsigned int i;
690   enum machine_mode mode = TYPE_MODE (char_type_node);
691   rtx addr = expand_normal (address);
692   rtx mem = gen_rtx_MEM (BLKmode, addr);
693   bool wrote_return_column = false;
694
695   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
696     {
697       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
698
699       if (rnum < DWARF_FRAME_REGISTERS)
700         {
701           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
702           enum machine_mode save_mode = reg_raw_mode[i];
703           HOST_WIDE_INT size;
704
705           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
706             save_mode = choose_hard_reg_mode (i, 1, true);
707           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
708             {
709               if (save_mode == VOIDmode)
710                 continue;
711               wrote_return_column = true;
712             }
713           size = GET_MODE_SIZE (save_mode);
714           if (offset < 0)
715             continue;
716
717           emit_move_insn (adjust_address (mem, mode, offset),
718                           gen_int_mode (size, mode));
719         }
720     }
721
722   if (!wrote_return_column)
723     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
724
725 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
726   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
727 #endif
728
729   targetm.init_dwarf_reg_sizes_extra (address);
730 }
731
732 /* Convert a DWARF call frame info. operation to its string name */
733
734 static const char *
735 dwarf_cfi_name (unsigned int cfi_opc)
736 {
737   switch (cfi_opc)
738     {
739     case DW_CFA_advance_loc:
740       return "DW_CFA_advance_loc";
741     case DW_CFA_offset:
742       return "DW_CFA_offset";
743     case DW_CFA_restore:
744       return "DW_CFA_restore";
745     case DW_CFA_nop:
746       return "DW_CFA_nop";
747     case DW_CFA_set_loc:
748       return "DW_CFA_set_loc";
749     case DW_CFA_advance_loc1:
750       return "DW_CFA_advance_loc1";
751     case DW_CFA_advance_loc2:
752       return "DW_CFA_advance_loc2";
753     case DW_CFA_advance_loc4:
754       return "DW_CFA_advance_loc4";
755     case DW_CFA_offset_extended:
756       return "DW_CFA_offset_extended";
757     case DW_CFA_restore_extended:
758       return "DW_CFA_restore_extended";
759     case DW_CFA_undefined:
760       return "DW_CFA_undefined";
761     case DW_CFA_same_value:
762       return "DW_CFA_same_value";
763     case DW_CFA_register:
764       return "DW_CFA_register";
765     case DW_CFA_remember_state:
766       return "DW_CFA_remember_state";
767     case DW_CFA_restore_state:
768       return "DW_CFA_restore_state";
769     case DW_CFA_def_cfa:
770       return "DW_CFA_def_cfa";
771     case DW_CFA_def_cfa_register:
772       return "DW_CFA_def_cfa_register";
773     case DW_CFA_def_cfa_offset:
774       return "DW_CFA_def_cfa_offset";
775
776     /* DWARF 3 */
777     case DW_CFA_def_cfa_expression:
778       return "DW_CFA_def_cfa_expression";
779     case DW_CFA_expression:
780       return "DW_CFA_expression";
781     case DW_CFA_offset_extended_sf:
782       return "DW_CFA_offset_extended_sf";
783     case DW_CFA_def_cfa_sf:
784       return "DW_CFA_def_cfa_sf";
785     case DW_CFA_def_cfa_offset_sf:
786       return "DW_CFA_def_cfa_offset_sf";
787
788     /* SGI/MIPS specific */
789     case DW_CFA_MIPS_advance_loc8:
790       return "DW_CFA_MIPS_advance_loc8";
791
792     /* GNU extensions */
793     case DW_CFA_GNU_window_save:
794       return "DW_CFA_GNU_window_save";
795     case DW_CFA_GNU_args_size:
796       return "DW_CFA_GNU_args_size";
797     case DW_CFA_GNU_negative_offset_extended:
798       return "DW_CFA_GNU_negative_offset_extended";
799
800     default:
801       return "DW_CFA_<unknown>";
802     }
803 }
804
805 /* Return a pointer to a newly allocated Call Frame Instruction.  */
806
807 static inline dw_cfi_ref
808 new_cfi (void)
809 {
810   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
811
812   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
813   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
814
815   return cfi;
816 }
817
818 /* Add a Call Frame Instruction to list of instructions.  */
819
820 static inline void
821 add_cfi (cfi_vec *vec, dw_cfi_ref cfi)
822 {
823   dw_fde_ref fde = current_fde ();
824
825   /* When DRAP is used, CFA is defined with an expression.  Redefine
826      CFA may lead to a different CFA value.   */
827   /* ??? Of course, this heuristic fails when we're annotating epilogues,
828      because of course we'll always want to redefine the CFA back to the
829      stack pointer on the way out.  Where should we move this check?  */
830   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
831     switch (cfi->dw_cfi_opc)
832       {
833         case DW_CFA_def_cfa_register:
834         case DW_CFA_def_cfa_offset:
835         case DW_CFA_def_cfa_offset_sf:
836         case DW_CFA_def_cfa:
837         case DW_CFA_def_cfa_sf:
838           gcc_unreachable ();
839
840         default:
841           break;
842       }
843
844   VEC_safe_push (dw_cfi_ref, gc, *vec, cfi);
845 }
846
847 /* Generate a new label for the CFI info to refer to.  FORCE is true
848    if a label needs to be output even when using .cfi_* directives.  */
849
850 char *
851 dwarf2out_cfi_label (bool force)
852 {
853   static char label[20];
854
855   if (!force && dwarf2out_do_cfi_asm ())
856     {
857       /* In this case, we will be emitting the asm directive instead of
858          the label, so just return a placeholder to keep the rest of the
859          interfaces happy.  */
860       strcpy (label, "<do not output>");
861     }
862   else
863     {
864       int num = dwarf2out_cfi_label_num++;
865       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
866       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
867     }
868
869   return label;
870 }
871
872 /* True if remember_state should be emitted before following CFI directive.  */
873 static bool emit_cfa_remember;
874
875 /* True if any CFI directives were emitted at the current insn.  */
876 static bool any_cfis_emitted;
877
878 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
879    or to the CIE if LABEL is NULL.  */
880
881 static void
882 add_fde_cfi (const char *label, dw_cfi_ref cfi)
883 {
884   cfi_vec *vec;
885
886   if (cie_cfi_vec == NULL)
887     cie_cfi_vec = VEC_alloc (dw_cfi_ref, gc, 20);
888
889   vec = &cie_cfi_vec;
890
891   if (emit_cfa_remember)
892     {
893       dw_cfi_ref cfi_remember;
894
895       /* Emit the state save.  */
896       emit_cfa_remember = false;
897       cfi_remember = new_cfi ();
898       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
899       add_fde_cfi (label, cfi_remember);
900     }
901
902   if (dwarf2out_do_cfi_asm ())
903     {
904       if (label)
905         {
906           dw_fde_ref fde = current_fde ();
907
908           gcc_assert (fde != NULL);
909
910           /* We still have to add the cfi to the list so that lookup_cfa
911              works later on.  When -g2 and above we even need to force
912              emitting of CFI labels and add to list a DW_CFA_set_loc for
913              convert_cfa_to_fb_loc_list purposes.  If we're generating
914              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
915              convert_cfa_to_fb_loc_list.  */
916           if (dwarf_version == 2
917               && debug_info_level > DINFO_LEVEL_TERSE
918               && (write_symbols == DWARF2_DEBUG
919                   || write_symbols == VMS_AND_DWARF2_DEBUG))
920             {
921               switch (cfi->dw_cfi_opc)
922                 {
923                 case DW_CFA_def_cfa_offset:
924                 case DW_CFA_def_cfa_offset_sf:
925                 case DW_CFA_def_cfa_register:
926                 case DW_CFA_def_cfa:
927                 case DW_CFA_def_cfa_sf:
928                 case DW_CFA_def_cfa_expression:
929                 case DW_CFA_restore_state:
930                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
931                     label = dwarf2out_cfi_label (true);
932
933                   if (fde->dw_fde_current_label == NULL
934                       || strcmp (label, fde->dw_fde_current_label) != 0)
935                     {
936                       dw_cfi_ref xcfi;
937
938                       label = xstrdup (label);
939
940                       /* Set the location counter to the new label.  */
941                       xcfi = new_cfi ();
942                       /* It doesn't metter whether DW_CFA_set_loc
943                          or DW_CFA_advance_loc4 is added here, those aren't
944                          emitted into assembly, only looked up by
945                          convert_cfa_to_fb_loc_list.  */
946                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
947                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
948                       add_cfi (&fde->dw_fde_cfi, xcfi);
949                       fde->dw_fde_current_label = label;
950                     }
951                   break;
952                 default:
953                   break;
954                 }
955             }
956
957           output_cfi_directive (cfi);
958
959           vec = &fde->dw_fde_cfi;
960           any_cfis_emitted = true;
961         }
962       /* ??? If this is a CFI for the CIE, we don't emit.  This
963          assumes that the standard CIE contents that the assembler
964          uses matches the standard CIE contents that the compiler
965          uses.  This is probably a bad assumption.  I'm not quite
966          sure how to address this for now.  */
967     }
968   else if (label)
969     {
970       dw_fde_ref fde = current_fde ();
971
972       gcc_assert (fde != NULL);
973
974       if (*label == 0)
975         label = dwarf2out_cfi_label (false);
976
977       if (fde->dw_fde_current_label == NULL
978           || strcmp (label, fde->dw_fde_current_label) != 0)
979         {
980           dw_cfi_ref xcfi;
981
982           label = xstrdup (label);
983
984           /* Set the location counter to the new label.  */
985           xcfi = new_cfi ();
986           /* If we have a current label, advance from there, otherwise
987              set the location directly using set_loc.  */
988           xcfi->dw_cfi_opc = fde->dw_fde_current_label
989                              ? DW_CFA_advance_loc4
990                              : DW_CFA_set_loc;
991           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
992           add_cfi (&fde->dw_fde_cfi, xcfi);
993
994           fde->dw_fde_current_label = label;
995         }
996
997       vec = &fde->dw_fde_cfi;
998       any_cfis_emitted = true;
999     }
1000
1001   add_cfi (vec, cfi);
1002 }
1003
1004 /* Subroutine of lookup_cfa.  */
1005
1006 static void
1007 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1008 {
1009   switch (cfi->dw_cfi_opc)
1010     {
1011     case DW_CFA_def_cfa_offset:
1012     case DW_CFA_def_cfa_offset_sf:
1013       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1014       break;
1015     case DW_CFA_def_cfa_register:
1016       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1017       break;
1018     case DW_CFA_def_cfa:
1019     case DW_CFA_def_cfa_sf:
1020       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1021       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1022       break;
1023     case DW_CFA_def_cfa_expression:
1024       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1025       break;
1026
1027     case DW_CFA_remember_state:
1028       gcc_assert (!remember->in_use);
1029       *remember = *loc;
1030       remember->in_use = 1;
1031       break;
1032     case DW_CFA_restore_state:
1033       gcc_assert (remember->in_use);
1034       *loc = *remember;
1035       remember->in_use = 0;
1036       break;
1037
1038     default:
1039       break;
1040     }
1041 }
1042
1043 /* Find the previous value for the CFA.  */
1044
1045 static void
1046 lookup_cfa (dw_cfa_location *loc)
1047 {
1048   int ix;
1049   dw_cfi_ref cfi;
1050   dw_fde_ref fde;
1051   dw_cfa_location remember;
1052
1053   memset (loc, 0, sizeof (*loc));
1054   loc->reg = INVALID_REGNUM;
1055   remember = *loc;
1056
1057   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
1058     lookup_cfa_1 (cfi, loc, &remember);
1059
1060   fde = current_fde ();
1061   if (fde)
1062     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
1063       lookup_cfa_1 (cfi, loc, &remember);
1064 }
1065
1066 /* The current rule for calculating the DWARF2 canonical frame address.  */
1067 static dw_cfa_location cfa;
1068
1069 /* The register used for saving registers to the stack, and its offset
1070    from the CFA.  */
1071 static dw_cfa_location cfa_store;
1072
1073 /* The current save location around an epilogue.  */
1074 static dw_cfa_location cfa_remember;
1075
1076 /* The running total of the size of arguments pushed onto the stack.  */
1077 static HOST_WIDE_INT args_size;
1078
1079 /* The last args_size we actually output.  */
1080 static HOST_WIDE_INT old_args_size;
1081
1082 /* Entry point to update the canonical frame address (CFA).
1083    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1084    calculated from REG+OFFSET.  */
1085
1086 void
1087 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1088 {
1089   dw_cfa_location loc;
1090   loc.indirect = 0;
1091   loc.base_offset = 0;
1092   loc.reg = reg;
1093   loc.offset = offset;
1094   def_cfa_1 (label, &loc);
1095 }
1096
1097 /* Determine if two dw_cfa_location structures define the same data.  */
1098
1099 static bool
1100 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1101 {
1102   return (loc1->reg == loc2->reg
1103           && loc1->offset == loc2->offset
1104           && loc1->indirect == loc2->indirect
1105           && (loc1->indirect == 0
1106               || loc1->base_offset == loc2->base_offset));
1107 }
1108
1109 /* This routine does the actual work.  The CFA is now calculated from
1110    the dw_cfa_location structure.  */
1111
1112 static void
1113 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1114 {
1115   dw_cfi_ref cfi;
1116   dw_cfa_location old_cfa, loc;
1117
1118   cfa = *loc_p;
1119   loc = *loc_p;
1120
1121   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1122     cfa_store.offset = loc.offset;
1123
1124   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1125   lookup_cfa (&old_cfa);
1126
1127   /* If nothing changed, no need to issue any call frame instructions.  */
1128   if (cfa_equal_p (&loc, &old_cfa))
1129     return;
1130
1131   cfi = new_cfi ();
1132
1133   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1134     {
1135       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1136          the CFA register did not change but the offset did.  The data
1137          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1138          in the assembler via the .cfi_def_cfa_offset directive.  */
1139       if (loc.offset < 0)
1140         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1141       else
1142         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1143       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1144     }
1145
1146 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1147   else if (loc.offset == old_cfa.offset
1148            && old_cfa.reg != INVALID_REGNUM
1149            && !loc.indirect
1150            && !old_cfa.indirect)
1151     {
1152       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1153          indicating the CFA register has changed to <register> but the
1154          offset has not changed.  */
1155       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1156       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1157     }
1158 #endif
1159
1160   else if (loc.indirect == 0)
1161     {
1162       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1163          indicating the CFA register has changed to <register> with
1164          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1165          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1166          directive.  */
1167       if (loc.offset < 0)
1168         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1169       else
1170         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1171       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1172       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1173     }
1174   else
1175     {
1176       /* Construct a DW_CFA_def_cfa_expression instruction to
1177          calculate the CFA using a full location expression since no
1178          register-offset pair is available.  */
1179       struct dw_loc_descr_struct *loc_list;
1180
1181       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1182       loc_list = build_cfa_loc (&loc, 0);
1183       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1184     }
1185
1186   add_fde_cfi (label, cfi);
1187 }
1188
1189 /* Add the CFI for saving a register.  REG is the CFA column number.
1190    LABEL is passed to add_fde_cfi.
1191    If SREG is -1, the register is saved at OFFSET from the CFA;
1192    otherwise it is saved in SREG.  */
1193
1194 static void
1195 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1196 {
1197   dw_cfi_ref cfi = new_cfi ();
1198   dw_fde_ref fde = current_fde ();
1199
1200   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1201
1202   /* When stack is aligned, store REG using DW_CFA_expression with
1203      FP.  */
1204   if (fde
1205       && fde->stack_realign
1206       && sreg == INVALID_REGNUM)
1207     {
1208       cfi->dw_cfi_opc = DW_CFA_expression;
1209       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1210       cfi->dw_cfi_oprnd2.dw_cfi_loc
1211         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1212     }
1213   else if (sreg == INVALID_REGNUM)
1214     {
1215       if (need_data_align_sf_opcode (offset))
1216         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1217       else if (reg & ~0x3f)
1218         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1219       else
1220         cfi->dw_cfi_opc = DW_CFA_offset;
1221       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1222     }
1223   else if (sreg == reg)
1224     cfi->dw_cfi_opc = DW_CFA_same_value;
1225   else
1226     {
1227       cfi->dw_cfi_opc = DW_CFA_register;
1228       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1229     }
1230
1231   add_fde_cfi (label, cfi);
1232 }
1233
1234 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1235    This CFI tells the unwinder that it needs to restore the window registers
1236    from the previous frame's window save area.
1237
1238    ??? Perhaps we should note in the CIE where windows are saved (instead of
1239    assuming 0(cfa)) and what registers are in the window.  */
1240
1241 void
1242 dwarf2out_window_save (const char *label)
1243 {
1244   dw_cfi_ref cfi = new_cfi ();
1245
1246   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1247   add_fde_cfi (label, cfi);
1248 }
1249
1250 /* Entry point for saving a register to the stack.  REG is the GCC register
1251    number.  LABEL and OFFSET are passed to reg_save.  */
1252
1253 void
1254 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1255 {
1256   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1257 }
1258
1259 /* Entry point for saving the return address in the stack.
1260    LABEL and OFFSET are passed to reg_save.  */
1261
1262 void
1263 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1264 {
1265   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1266 }
1267
1268 /* Entry point for saving the return address in a register.
1269    LABEL and SREG are passed to reg_save.  */
1270
1271 void
1272 dwarf2out_return_reg (const char *label, unsigned int sreg)
1273 {
1274   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1275 }
1276
1277 /* Record the initial position of the return address.  RTL is
1278    INCOMING_RETURN_ADDR_RTX.  */
1279
1280 static void
1281 initial_return_save (rtx rtl)
1282 {
1283   unsigned int reg = INVALID_REGNUM;
1284   HOST_WIDE_INT offset = 0;
1285
1286   switch (GET_CODE (rtl))
1287     {
1288     case REG:
1289       /* RA is in a register.  */
1290       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1291       break;
1292
1293     case MEM:
1294       /* RA is on the stack.  */
1295       rtl = XEXP (rtl, 0);
1296       switch (GET_CODE (rtl))
1297         {
1298         case REG:
1299           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1300           offset = 0;
1301           break;
1302
1303         case PLUS:
1304           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1305           offset = INTVAL (XEXP (rtl, 1));
1306           break;
1307
1308         case MINUS:
1309           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1310           offset = -INTVAL (XEXP (rtl, 1));
1311           break;
1312
1313         default:
1314           gcc_unreachable ();
1315         }
1316
1317       break;
1318
1319     case PLUS:
1320       /* The return address is at some offset from any value we can
1321          actually load.  For instance, on the SPARC it is in %i7+8. Just
1322          ignore the offset for now; it doesn't matter for unwinding frames.  */
1323       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1324       initial_return_save (XEXP (rtl, 0));
1325       return;
1326
1327     default:
1328       gcc_unreachable ();
1329     }
1330
1331   if (reg != DWARF_FRAME_RETURN_COLUMN)
1332     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1333 }
1334
1335 /* Given a SET, calculate the amount of stack adjustment it
1336    contains.  */
1337
1338 static HOST_WIDE_INT
1339 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1340                      HOST_WIDE_INT cur_offset)
1341 {
1342   const_rtx src = SET_SRC (pattern);
1343   const_rtx dest = SET_DEST (pattern);
1344   HOST_WIDE_INT offset = 0;
1345   enum rtx_code code;
1346
1347   if (dest == stack_pointer_rtx)
1348     {
1349       code = GET_CODE (src);
1350
1351       /* Assume (set (reg sp) (reg whatever)) sets args_size
1352          level to 0.  */
1353       if (code == REG && src != stack_pointer_rtx)
1354         {
1355           offset = -cur_args_size;
1356 #ifndef STACK_GROWS_DOWNWARD
1357           offset = -offset;
1358 #endif
1359           return offset - cur_offset;
1360         }
1361
1362       if (! (code == PLUS || code == MINUS)
1363           || XEXP (src, 0) != stack_pointer_rtx
1364           || !CONST_INT_P (XEXP (src, 1)))
1365         return 0;
1366
1367       /* (set (reg sp) (plus (reg sp) (const_int))) */
1368       offset = INTVAL (XEXP (src, 1));
1369       if (code == PLUS)
1370         offset = -offset;
1371       return offset;
1372     }
1373
1374   if (MEM_P (src) && !MEM_P (dest))
1375     dest = src;
1376   if (MEM_P (dest))
1377     {
1378       /* (set (mem (pre_dec (reg sp))) (foo)) */
1379       src = XEXP (dest, 0);
1380       code = GET_CODE (src);
1381
1382       switch (code)
1383         {
1384         case PRE_MODIFY:
1385         case POST_MODIFY:
1386           if (XEXP (src, 0) == stack_pointer_rtx)
1387             {
1388               rtx val = XEXP (XEXP (src, 1), 1);
1389               /* We handle only adjustments by constant amount.  */
1390               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1391                           && CONST_INT_P (val));
1392               offset = -INTVAL (val);
1393               break;
1394             }
1395           return 0;
1396
1397         case PRE_DEC:
1398         case POST_DEC:
1399           if (XEXP (src, 0) == stack_pointer_rtx)
1400             {
1401               offset = GET_MODE_SIZE (GET_MODE (dest));
1402               break;
1403             }
1404           return 0;
1405
1406         case PRE_INC:
1407         case POST_INC:
1408           if (XEXP (src, 0) == stack_pointer_rtx)
1409             {
1410               offset = -GET_MODE_SIZE (GET_MODE (dest));
1411               break;
1412             }
1413           return 0;
1414
1415         default:
1416           return 0;
1417         }
1418     }
1419   else
1420     return 0;
1421
1422   return offset;
1423 }
1424
1425 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1426    indexed by INSN_UID.  */
1427
1428 static HOST_WIDE_INT *barrier_args_size;
1429
1430 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1431
1432 static HOST_WIDE_INT
1433 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1434                              VEC (rtx, heap) **next)
1435 {
1436   HOST_WIDE_INT offset = 0;
1437   int i;
1438
1439   if (! RTX_FRAME_RELATED_P (insn))
1440     {
1441       if (prologue_epilogue_contains (insn))
1442         /* Nothing */;
1443       else if (GET_CODE (PATTERN (insn)) == SET)
1444         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1445       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1446                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1447         {
1448           /* There may be stack adjustments inside compound insns.  Search
1449              for them.  */
1450           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1451             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1452               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1453                                              cur_args_size, offset);
1454         }
1455     }
1456   else
1457     {
1458       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1459
1460       if (expr)
1461         {
1462           expr = XEXP (expr, 0);
1463           if (GET_CODE (expr) == PARALLEL
1464               || GET_CODE (expr) == SEQUENCE)
1465             for (i = 1; i < XVECLEN (expr, 0); i++)
1466               {
1467                 rtx elem = XVECEXP (expr, 0, i);
1468
1469                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1470                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1471               }
1472         }
1473     }
1474
1475 #ifndef STACK_GROWS_DOWNWARD
1476   offset = -offset;
1477 #endif
1478
1479   cur_args_size += offset;
1480   if (cur_args_size < 0)
1481     cur_args_size = 0;
1482
1483   if (JUMP_P (insn))
1484     {
1485       rtx dest = JUMP_LABEL (insn);
1486
1487       if (dest)
1488         {
1489           if (barrier_args_size [INSN_UID (dest)] < 0)
1490             {
1491               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1492               VEC_safe_push (rtx, heap, *next, dest);
1493             }
1494         }
1495     }
1496
1497   return cur_args_size;
1498 }
1499
1500 /* Walk the whole function and compute args_size on BARRIERs.  */
1501
1502 static void
1503 compute_barrier_args_size (void)
1504 {
1505   int max_uid = get_max_uid (), i;
1506   rtx insn;
1507   VEC (rtx, heap) *worklist, *next, *tmp;
1508
1509   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1510   for (i = 0; i < max_uid; i++)
1511     barrier_args_size[i] = -1;
1512
1513   worklist = VEC_alloc (rtx, heap, 20);
1514   next = VEC_alloc (rtx, heap, 20);
1515   insn = get_insns ();
1516   barrier_args_size[INSN_UID (insn)] = 0;
1517   VEC_quick_push (rtx, worklist, insn);
1518   for (;;)
1519     {
1520       while (!VEC_empty (rtx, worklist))
1521         {
1522           rtx prev, body, first_insn;
1523           HOST_WIDE_INT cur_args_size;
1524
1525           first_insn = insn = VEC_pop (rtx, worklist);
1526           cur_args_size = barrier_args_size[INSN_UID (insn)];
1527           prev = prev_nonnote_insn (insn);
1528           if (prev && BARRIER_P (prev))
1529             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1530
1531           for (; insn; insn = NEXT_INSN (insn))
1532             {
1533               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1534                 continue;
1535               if (BARRIER_P (insn))
1536                 break;
1537
1538               if (LABEL_P (insn))
1539                 {
1540                   if (insn == first_insn)
1541                     continue;
1542                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1543                     {
1544                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1545                       continue;
1546                     }
1547                   else
1548                     {
1549                       /* The insns starting with this label have been
1550                          already scanned or are in the worklist.  */
1551                       break;
1552                     }
1553                 }
1554
1555               body = PATTERN (insn);
1556               if (GET_CODE (body) == SEQUENCE)
1557                 {
1558                   HOST_WIDE_INT dest_args_size = cur_args_size;
1559                   for (i = 1; i < XVECLEN (body, 0); i++)
1560                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1561                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1562                       dest_args_size
1563                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1564                                                        dest_args_size, &next);
1565                     else
1566                       cur_args_size
1567                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1568                                                        cur_args_size, &next);
1569
1570                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1571                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1572                                                  dest_args_size, &next);
1573                   else
1574                     cur_args_size
1575                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1576                                                      cur_args_size, &next);
1577                 }
1578               else
1579                 cur_args_size
1580                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1581             }
1582         }
1583
1584       if (VEC_empty (rtx, next))
1585         break;
1586
1587       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1588       tmp = next;
1589       next = worklist;
1590       worklist = tmp;
1591       VEC_truncate (rtx, next, 0);
1592     }
1593
1594   VEC_free (rtx, heap, worklist);
1595   VEC_free (rtx, heap, next);
1596 }
1597
1598 /* Add a CFI to update the running total of the size of arguments
1599    pushed onto the stack.  */
1600
1601 static void
1602 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1603 {
1604   dw_cfi_ref cfi;
1605
1606   if (size == old_args_size)
1607     return;
1608
1609   old_args_size = size;
1610
1611   cfi = new_cfi ();
1612   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1613   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1614   add_fde_cfi (label, cfi);
1615 }
1616
1617 /* Record a stack adjustment of OFFSET bytes.  */
1618
1619 static void
1620 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1621 {
1622   if (cfa.reg == STACK_POINTER_REGNUM)
1623     cfa.offset += offset;
1624
1625   if (cfa_store.reg == STACK_POINTER_REGNUM)
1626     cfa_store.offset += offset;
1627
1628   if (ACCUMULATE_OUTGOING_ARGS)
1629     return;
1630
1631 #ifndef STACK_GROWS_DOWNWARD
1632   offset = -offset;
1633 #endif
1634
1635   args_size += offset;
1636   if (args_size < 0)
1637     args_size = 0;
1638
1639   def_cfa_1 (label, &cfa);
1640   if (flag_asynchronous_unwind_tables)
1641     dwarf2out_args_size (label, args_size);
1642 }
1643
1644 /* Check INSN to see if it looks like a push or a stack adjustment, and
1645    make a note of it if it does.  EH uses this information to find out
1646    how much extra space it needs to pop off the stack.  */
1647
1648 static void
1649 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1650 {
1651   HOST_WIDE_INT offset;
1652   const char *label;
1653   int i;
1654
1655   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1656      with this function.  Proper support would require all frame-related
1657      insns to be marked, and to be able to handle saving state around
1658      epilogues textually in the middle of the function.  */
1659   if (prologue_epilogue_contains (insn))
1660     return;
1661
1662   /* If INSN is an instruction from target of an annulled branch, the
1663      effects are for the target only and so current argument size
1664      shouldn't change at all.  */
1665   if (final_sequence
1666       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1667       && INSN_FROM_TARGET_P (insn))
1668     return;
1669
1670   /* If only calls can throw, and we have a frame pointer,
1671      save up adjustments until we see the CALL_INSN.  */
1672   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1673     {
1674       if (CALL_P (insn) && !after_p)
1675         {
1676           /* Extract the size of the args from the CALL rtx itself.  */
1677           insn = PATTERN (insn);
1678           if (GET_CODE (insn) == PARALLEL)
1679             insn = XVECEXP (insn, 0, 0);
1680           if (GET_CODE (insn) == SET)
1681             insn = SET_SRC (insn);
1682           gcc_assert (GET_CODE (insn) == CALL);
1683           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1684         }
1685       return;
1686     }
1687
1688   if (CALL_P (insn) && !after_p)
1689     {
1690       if (!flag_asynchronous_unwind_tables)
1691         dwarf2out_args_size ("", args_size);
1692       return;
1693     }
1694   else if (BARRIER_P (insn))
1695     {
1696       /* Don't call compute_barrier_args_size () if the only
1697          BARRIER is at the end of function.  */
1698       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1699         compute_barrier_args_size ();
1700       if (barrier_args_size == NULL)
1701         offset = 0;
1702       else
1703         {
1704           offset = barrier_args_size[INSN_UID (insn)];
1705           if (offset < 0)
1706             offset = 0;
1707         }
1708
1709       offset -= args_size;
1710 #ifndef STACK_GROWS_DOWNWARD
1711       offset = -offset;
1712 #endif
1713     }
1714   else if (GET_CODE (PATTERN (insn)) == SET)
1715     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1716   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1717            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1718     {
1719       /* There may be stack adjustments inside compound insns.  Search
1720          for them.  */
1721       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1722         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1723           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1724                                          args_size, offset);
1725     }
1726   else
1727     return;
1728
1729   if (offset == 0)
1730     return;
1731
1732   label = dwarf2out_cfi_label (false);
1733   dwarf2out_stack_adjust (offset, label);
1734 }
1735
1736 /* We delay emitting a register save until either (a) we reach the end
1737    of the prologue or (b) the register is clobbered.  This clusters
1738    register saves so that there are fewer pc advances.  */
1739
1740 struct GTY(()) queued_reg_save {
1741   struct queued_reg_save *next;
1742   rtx reg;
1743   HOST_WIDE_INT cfa_offset;
1744   rtx saved_reg;
1745 };
1746
1747 static GTY(()) struct queued_reg_save *queued_reg_saves;
1748
1749 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1750 struct GTY(()) reg_saved_in_data {
1751   rtx orig_reg;
1752   rtx saved_in_reg;
1753 };
1754
1755 /* A list of registers saved in other registers.
1756    The list intentionally has a small maximum capacity of 4; if your
1757    port needs more than that, you might consider implementing a
1758    more efficient data structure.  */
1759 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1760 static GTY(()) size_t num_regs_saved_in_regs;
1761
1762 static const char *last_reg_save_label;
1763
1764 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1765    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1766
1767 static void
1768 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1769 {
1770   struct queued_reg_save *q;
1771
1772   /* Duplicates waste space, but it's also necessary to remove them
1773      for correctness, since the queue gets output in reverse
1774      order.  */
1775   for (q = queued_reg_saves; q != NULL; q = q->next)
1776     if (REGNO (q->reg) == REGNO (reg))
1777       break;
1778
1779   if (q == NULL)
1780     {
1781       q = ggc_alloc_queued_reg_save ();
1782       q->next = queued_reg_saves;
1783       queued_reg_saves = q;
1784     }
1785
1786   q->reg = reg;
1787   q->cfa_offset = offset;
1788   q->saved_reg = sreg;
1789
1790   last_reg_save_label = label;
1791 }
1792
1793 /* Output all the entries in QUEUED_REG_SAVES.  */
1794
1795 void
1796 dwarf2out_flush_queued_reg_saves (void)
1797 {
1798   struct queued_reg_save *q;
1799
1800   for (q = queued_reg_saves; q; q = q->next)
1801     {
1802       size_t i;
1803       unsigned int reg, sreg;
1804
1805       for (i = 0; i < num_regs_saved_in_regs; i++)
1806         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1807           break;
1808       if (q->saved_reg && i == num_regs_saved_in_regs)
1809         {
1810           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1811           num_regs_saved_in_regs++;
1812         }
1813       if (i != num_regs_saved_in_regs)
1814         {
1815           regs_saved_in_regs[i].orig_reg = q->reg;
1816           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1817         }
1818
1819       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1820       if (q->saved_reg)
1821         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1822       else
1823         sreg = INVALID_REGNUM;
1824       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1825     }
1826
1827   queued_reg_saves = NULL;
1828   last_reg_save_label = NULL;
1829 }
1830
1831 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1832    location for?  Or, does it clobber a register which we've previously
1833    said that some other register is saved in, and for which we now
1834    have a new location for?  */
1835
1836 static bool
1837 clobbers_queued_reg_save (const_rtx insn)
1838 {
1839   struct queued_reg_save *q;
1840
1841   for (q = queued_reg_saves; q; q = q->next)
1842     {
1843       size_t i;
1844       if (modified_in_p (q->reg, insn))
1845         return true;
1846       for (i = 0; i < num_regs_saved_in_regs; i++)
1847         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1848             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1849           return true;
1850     }
1851
1852   return false;
1853 }
1854
1855 /* Entry point for saving the first register into the second.  */
1856
1857 void
1858 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1859 {
1860   size_t i;
1861   unsigned int regno, sregno;
1862
1863   for (i = 0; i < num_regs_saved_in_regs; i++)
1864     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1865       break;
1866   if (i == num_regs_saved_in_regs)
1867     {
1868       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1869       num_regs_saved_in_regs++;
1870     }
1871   regs_saved_in_regs[i].orig_reg = reg;
1872   regs_saved_in_regs[i].saved_in_reg = sreg;
1873
1874   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1875   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1876   reg_save (label, regno, sregno, 0);
1877 }
1878
1879 /* What register, if any, is currently saved in REG?  */
1880
1881 static rtx
1882 reg_saved_in (rtx reg)
1883 {
1884   unsigned int regn = REGNO (reg);
1885   size_t i;
1886   struct queued_reg_save *q;
1887
1888   for (q = queued_reg_saves; q; q = q->next)
1889     if (q->saved_reg && regn == REGNO (q->saved_reg))
1890       return q->reg;
1891
1892   for (i = 0; i < num_regs_saved_in_regs; i++)
1893     if (regs_saved_in_regs[i].saved_in_reg
1894         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1895       return regs_saved_in_regs[i].orig_reg;
1896
1897   return NULL_RTX;
1898 }
1899
1900
1901 /* A temporary register holding an integral value used in adjusting SP
1902    or setting up the store_reg.  The "offset" field holds the integer
1903    value, not an offset.  */
1904 static dw_cfa_location cfa_temp;
1905
1906 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1907
1908 static void
1909 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1910 {
1911   memset (&cfa, 0, sizeof (cfa));
1912
1913   switch (GET_CODE (pat))
1914     {
1915     case PLUS:
1916       cfa.reg = REGNO (XEXP (pat, 0));
1917       cfa.offset = INTVAL (XEXP (pat, 1));
1918       break;
1919
1920     case REG:
1921       cfa.reg = REGNO (pat);
1922       break;
1923
1924     case MEM:
1925       cfa.indirect = 1;
1926       pat = XEXP (pat, 0);
1927       if (GET_CODE (pat) == PLUS)
1928         {
1929           cfa.base_offset = INTVAL (XEXP (pat, 1));
1930           pat = XEXP (pat, 0);
1931         }
1932       cfa.reg = REGNO (pat);
1933       break;
1934
1935     default:
1936       /* Recurse and define an expression.  */
1937       gcc_unreachable ();
1938     }
1939
1940   def_cfa_1 (label, &cfa);
1941 }
1942
1943 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1944
1945 static void
1946 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1947 {
1948   rtx src, dest;
1949
1950   gcc_assert (GET_CODE (pat) == SET);
1951   dest = XEXP (pat, 0);
1952   src = XEXP (pat, 1);
1953
1954   switch (GET_CODE (src))
1955     {
1956     case PLUS:
1957       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1958       cfa.offset -= INTVAL (XEXP (src, 1));
1959       break;
1960
1961     case REG:
1962         break;
1963
1964     default:
1965         gcc_unreachable ();
1966     }
1967
1968   cfa.reg = REGNO (dest);
1969   gcc_assert (cfa.indirect == 0);
1970
1971   def_cfa_1 (label, &cfa);
1972 }
1973
1974 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1975
1976 static void
1977 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1978 {
1979   HOST_WIDE_INT offset;
1980   rtx src, addr, span;
1981
1982   src = XEXP (set, 1);
1983   addr = XEXP (set, 0);
1984   gcc_assert (MEM_P (addr));
1985   addr = XEXP (addr, 0);
1986
1987   /* As documented, only consider extremely simple addresses.  */
1988   switch (GET_CODE (addr))
1989     {
1990     case REG:
1991       gcc_assert (REGNO (addr) == cfa.reg);
1992       offset = -cfa.offset;
1993       break;
1994     case PLUS:
1995       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1996       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1997       break;
1998     default:
1999       gcc_unreachable ();
2000     }
2001
2002   span = targetm.dwarf_register_span (src);
2003
2004   /* ??? We'd like to use queue_reg_save, but we need to come up with
2005      a different flushing heuristic for epilogues.  */
2006   if (!span)
2007     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2008   else
2009     {
2010       /* We have a PARALLEL describing where the contents of SRC live.
2011          Queue register saves for each piece of the PARALLEL.  */
2012       int par_index;
2013       int limit;
2014       HOST_WIDE_INT span_offset = offset;
2015
2016       gcc_assert (GET_CODE (span) == PARALLEL);
2017
2018       limit = XVECLEN (span, 0);
2019       for (par_index = 0; par_index < limit; par_index++)
2020         {
2021           rtx elem = XVECEXP (span, 0, par_index);
2022
2023           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2024                     INVALID_REGNUM, span_offset);
2025           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2026         }
2027     }
2028 }
2029
2030 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2031
2032 static void
2033 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2034 {
2035   rtx src, dest;
2036   unsigned sregno, dregno;
2037
2038   src = XEXP (set, 1);
2039   dest = XEXP (set, 0);
2040
2041   if (src == pc_rtx)
2042     sregno = DWARF_FRAME_RETURN_COLUMN;
2043   else
2044     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2045
2046   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2047
2048   /* ??? We'd like to use queue_reg_save, but we need to come up with
2049      a different flushing heuristic for epilogues.  */
2050   reg_save (label, sregno, dregno, 0);
2051 }
2052
2053 /* Helper function to get mode of MEM's address.  */
2054
2055 static inline enum machine_mode
2056 get_address_mode (rtx mem)
2057 {
2058   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
2059   if (mode != VOIDmode)
2060     return mode;
2061   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
2062 }
2063
2064 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2065
2066 static void
2067 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2068 {
2069   rtx src, dest, span;
2070   dw_cfi_ref cfi = new_cfi ();
2071
2072   dest = SET_DEST (set);
2073   src = SET_SRC (set);
2074
2075   gcc_assert (REG_P (src));
2076   gcc_assert (MEM_P (dest));
2077
2078   span = targetm.dwarf_register_span (src);
2079   gcc_assert (!span);
2080
2081   cfi->dw_cfi_opc = DW_CFA_expression;
2082   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2083   cfi->dw_cfi_oprnd2.dw_cfi_loc
2084     = mem_loc_descriptor (XEXP (dest, 0), get_address_mode (dest),
2085                           GET_MODE (dest), VAR_INIT_STATUS_INITIALIZED);
2086
2087   /* ??? We'd like to use queue_reg_save, were the interface different,
2088      and, as above, we could manage flushing for epilogues.  */
2089   add_fde_cfi (label, cfi);
2090 }
2091
2092 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2093
2094 static void
2095 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2096 {
2097   dw_cfi_ref cfi = new_cfi ();
2098   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2099
2100   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2101   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2102
2103   add_fde_cfi (label, cfi);
2104 }
2105
2106 /* Record call frame debugging information for an expression EXPR,
2107    which either sets SP or FP (adjusting how we calculate the frame
2108    address) or saves a register to the stack or another register.
2109    LABEL indicates the address of EXPR.
2110
2111    This function encodes a state machine mapping rtxes to actions on
2112    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2113    users need not read the source code.
2114
2115   The High-Level Picture
2116
2117   Changes in the register we use to calculate the CFA: Currently we
2118   assume that if you copy the CFA register into another register, we
2119   should take the other one as the new CFA register; this seems to
2120   work pretty well.  If it's wrong for some target, it's simple
2121   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2122
2123   Changes in the register we use for saving registers to the stack:
2124   This is usually SP, but not always.  Again, we deduce that if you
2125   copy SP into another register (and SP is not the CFA register),
2126   then the new register is the one we will be using for register
2127   saves.  This also seems to work.
2128
2129   Register saves: There's not much guesswork about this one; if
2130   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2131   register save, and the register used to calculate the destination
2132   had better be the one we think we're using for this purpose.
2133   It's also assumed that a copy from a call-saved register to another
2134   register is saving that register if RTX_FRAME_RELATED_P is set on
2135   that instruction.  If the copy is from a call-saved register to
2136   the *same* register, that means that the register is now the same
2137   value as in the caller.
2138
2139   Except: If the register being saved is the CFA register, and the
2140   offset is nonzero, we are saving the CFA, so we assume we have to
2141   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2142   the intent is to save the value of SP from the previous frame.
2143
2144   In addition, if a register has previously been saved to a different
2145   register,
2146
2147   Invariants / Summaries of Rules
2148
2149   cfa          current rule for calculating the CFA.  It usually
2150                consists of a register and an offset.
2151   cfa_store    register used by prologue code to save things to the stack
2152                cfa_store.offset is the offset from the value of
2153                cfa_store.reg to the actual CFA
2154   cfa_temp     register holding an integral value.  cfa_temp.offset
2155                stores the value, which will be used to adjust the
2156                stack pointer.  cfa_temp is also used like cfa_store,
2157                to track stores to the stack via fp or a temp reg.
2158
2159   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2160                with cfa.reg as the first operand changes the cfa.reg and its
2161                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2162                cfa_temp.offset.
2163
2164   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2165                expression yielding a constant.  This sets cfa_temp.reg
2166                and cfa_temp.offset.
2167
2168   Rule 5:      Create a new register cfa_store used to save items to the
2169                stack.
2170
2171   Rules 10-14: Save a register to the stack.  Define offset as the
2172                difference of the original location and cfa_store's
2173                location (or cfa_temp's location if cfa_temp is used).
2174
2175   Rules 16-20: If AND operation happens on sp in prologue, we assume
2176                stack is realigned.  We will use a group of DW_OP_XXX
2177                expressions to represent the location of the stored
2178                register instead of CFA+offset.
2179
2180   The Rules
2181
2182   "{a,b}" indicates a choice of a xor b.
2183   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2184
2185   Rule 1:
2186   (set <reg1> <reg2>:cfa.reg)
2187   effects: cfa.reg = <reg1>
2188            cfa.offset unchanged
2189            cfa_temp.reg = <reg1>
2190            cfa_temp.offset = cfa.offset
2191
2192   Rule 2:
2193   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2194                               {<const_int>,<reg>:cfa_temp.reg}))
2195   effects: cfa.reg = sp if fp used
2196            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2197            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2198              if cfa_store.reg==sp
2199
2200   Rule 3:
2201   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2202   effects: cfa.reg = fp
2203            cfa_offset += +/- <const_int>
2204
2205   Rule 4:
2206   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2207   constraints: <reg1> != fp
2208                <reg1> != sp
2209   effects: cfa.reg = <reg1>
2210            cfa_temp.reg = <reg1>
2211            cfa_temp.offset = cfa.offset
2212
2213   Rule 5:
2214   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2215   constraints: <reg1> != fp
2216                <reg1> != sp
2217   effects: cfa_store.reg = <reg1>
2218            cfa_store.offset = cfa.offset - cfa_temp.offset
2219
2220   Rule 6:
2221   (set <reg> <const_int>)
2222   effects: cfa_temp.reg = <reg>
2223            cfa_temp.offset = <const_int>
2224
2225   Rule 7:
2226   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2227   effects: cfa_temp.reg = <reg1>
2228            cfa_temp.offset |= <const_int>
2229
2230   Rule 8:
2231   (set <reg> (high <exp>))
2232   effects: none
2233
2234   Rule 9:
2235   (set <reg> (lo_sum <exp> <const_int>))
2236   effects: cfa_temp.reg = <reg>
2237            cfa_temp.offset = <const_int>
2238
2239   Rule 10:
2240   (set (mem ({pre,post}_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2241   effects: cfa_store.offset -= <const_int>
2242            cfa.offset = cfa_store.offset if cfa.reg == sp
2243            cfa.reg = sp
2244            cfa.base_offset = -cfa_store.offset
2245
2246   Rule 11:
2247   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2248   effects: cfa_store.offset += -/+ mode_size(mem)
2249            cfa.offset = cfa_store.offset if cfa.reg == sp
2250            cfa.reg = sp
2251            cfa.base_offset = -cfa_store.offset
2252
2253   Rule 12:
2254   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2255
2256        <reg2>)
2257   effects: cfa.reg = <reg1>
2258            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2259
2260   Rule 13:
2261   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2262   effects: cfa.reg = <reg1>
2263            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2264
2265   Rule 14:
2266   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2267   effects: cfa.reg = <reg1>
2268            cfa.base_offset = -cfa_temp.offset
2269            cfa_temp.offset -= mode_size(mem)
2270
2271   Rule 15:
2272   (set <reg> {unspec, unspec_volatile})
2273   effects: target-dependent
2274
2275   Rule 16:
2276   (set sp (and: sp <const_int>))
2277   constraints: cfa_store.reg == sp
2278   effects: current_fde.stack_realign = 1
2279            cfa_store.offset = 0
2280            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2281
2282   Rule 17:
2283   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2284   effects: cfa_store.offset += -/+ mode_size(mem)
2285
2286   Rule 18:
2287   (set (mem ({pre_inc, pre_dec} sp)) fp)
2288   constraints: fde->stack_realign == 1
2289   effects: cfa_store.offset = 0
2290            cfa.reg != HARD_FRAME_POINTER_REGNUM
2291
2292   Rule 19:
2293   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2294   constraints: fde->stack_realign == 1
2295                && cfa.offset == 0
2296                && cfa.indirect == 0
2297                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2298   effects: Use DW_CFA_def_cfa_expression to define cfa
2299            cfa.reg == fde->drap_reg  */
2300
2301 static void
2302 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2303 {
2304   rtx src, dest, span;
2305   HOST_WIDE_INT offset;
2306   dw_fde_ref fde;
2307
2308   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2309      the PARALLEL independently. The first element is always processed if
2310      it is a SET. This is for backward compatibility.   Other elements
2311      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2312      flag is set in them.  */
2313   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2314     {
2315       int par_index;
2316       int limit = XVECLEN (expr, 0);
2317       rtx elem;
2318
2319       /* PARALLELs have strict read-modify-write semantics, so we
2320          ought to evaluate every rvalue before changing any lvalue.
2321          It's cumbersome to do that in general, but there's an
2322          easy approximation that is enough for all current users:
2323          handle register saves before register assignments.  */
2324       if (GET_CODE (expr) == PARALLEL)
2325         for (par_index = 0; par_index < limit; par_index++)
2326           {
2327             elem = XVECEXP (expr, 0, par_index);
2328             if (GET_CODE (elem) == SET
2329                 && MEM_P (SET_DEST (elem))
2330                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2331               dwarf2out_frame_debug_expr (elem, label);
2332           }
2333
2334       for (par_index = 0; par_index < limit; par_index++)
2335         {
2336           elem = XVECEXP (expr, 0, par_index);
2337           if (GET_CODE (elem) == SET
2338               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2339               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2340             dwarf2out_frame_debug_expr (elem, label);
2341           else if (GET_CODE (elem) == SET
2342                    && par_index != 0
2343                    && !RTX_FRAME_RELATED_P (elem))
2344             {
2345               /* Stack adjustment combining might combine some post-prologue
2346                  stack adjustment into a prologue stack adjustment.  */
2347               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2348
2349               if (offset != 0)
2350                 dwarf2out_stack_adjust (offset, label);
2351             }
2352         }
2353       return;
2354     }
2355
2356   gcc_assert (GET_CODE (expr) == SET);
2357
2358   src = SET_SRC (expr);
2359   dest = SET_DEST (expr);
2360
2361   if (REG_P (src))
2362     {
2363       rtx rsi = reg_saved_in (src);
2364       if (rsi)
2365         src = rsi;
2366     }
2367
2368   fde = current_fde ();
2369
2370   switch (GET_CODE (dest))
2371     {
2372     case REG:
2373       switch (GET_CODE (src))
2374         {
2375           /* Setting FP from SP.  */
2376         case REG:
2377           if (cfa.reg == (unsigned) REGNO (src))
2378             {
2379               /* Rule 1 */
2380               /* Update the CFA rule wrt SP or FP.  Make sure src is
2381                  relative to the current CFA register.
2382
2383                  We used to require that dest be either SP or FP, but the
2384                  ARM copies SP to a temporary register, and from there to
2385                  FP.  So we just rely on the backends to only set
2386                  RTX_FRAME_RELATED_P on appropriate insns.  */
2387               cfa.reg = REGNO (dest);
2388               cfa_temp.reg = cfa.reg;
2389               cfa_temp.offset = cfa.offset;
2390             }
2391           else
2392             {
2393               /* Saving a register in a register.  */
2394               gcc_assert (!fixed_regs [REGNO (dest)]
2395                           /* For the SPARC and its register window.  */
2396                           || (DWARF_FRAME_REGNUM (REGNO (src))
2397                               == DWARF_FRAME_RETURN_COLUMN));
2398
2399               /* After stack is aligned, we can only save SP in FP
2400                  if drap register is used.  In this case, we have
2401                  to restore stack pointer with the CFA value and we
2402                  don't generate this DWARF information.  */
2403               if (fde
2404                   && fde->stack_realign
2405                   && REGNO (src) == STACK_POINTER_REGNUM)
2406                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2407                             && fde->drap_reg != INVALID_REGNUM
2408                             && cfa.reg != REGNO (src));
2409               else
2410                 queue_reg_save (label, src, dest, 0);
2411             }
2412           break;
2413
2414         case PLUS:
2415         case MINUS:
2416         case LO_SUM:
2417           if (dest == stack_pointer_rtx)
2418             {
2419               /* Rule 2 */
2420               /* Adjusting SP.  */
2421               switch (GET_CODE (XEXP (src, 1)))
2422                 {
2423                 case CONST_INT:
2424                   offset = INTVAL (XEXP (src, 1));
2425                   break;
2426                 case REG:
2427                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2428                               == cfa_temp.reg);
2429                   offset = cfa_temp.offset;
2430                   break;
2431                 default:
2432                   gcc_unreachable ();
2433                 }
2434
2435               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2436                 {
2437                   /* Restoring SP from FP in the epilogue.  */
2438                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2439                   cfa.reg = STACK_POINTER_REGNUM;
2440                 }
2441               else if (GET_CODE (src) == LO_SUM)
2442                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2443                 ;
2444               else
2445                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2446
2447               if (GET_CODE (src) != MINUS)
2448                 offset = -offset;
2449               if (cfa.reg == STACK_POINTER_REGNUM)
2450                 cfa.offset += offset;
2451               if (cfa_store.reg == STACK_POINTER_REGNUM)
2452                 cfa_store.offset += offset;
2453             }
2454           else if (dest == hard_frame_pointer_rtx)
2455             {
2456               /* Rule 3 */
2457               /* Either setting the FP from an offset of the SP,
2458                  or adjusting the FP */
2459               gcc_assert (frame_pointer_needed);
2460
2461               gcc_assert (REG_P (XEXP (src, 0))
2462                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2463                           && CONST_INT_P (XEXP (src, 1)));
2464               offset = INTVAL (XEXP (src, 1));
2465               if (GET_CODE (src) != MINUS)
2466                 offset = -offset;
2467               cfa.offset += offset;
2468               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2469             }
2470           else
2471             {
2472               gcc_assert (GET_CODE (src) != MINUS);
2473
2474               /* Rule 4 */
2475               if (REG_P (XEXP (src, 0))
2476                   && REGNO (XEXP (src, 0)) == cfa.reg
2477                   && CONST_INT_P (XEXP (src, 1)))
2478                 {
2479                   /* Setting a temporary CFA register that will be copied
2480                      into the FP later on.  */
2481                   offset = - INTVAL (XEXP (src, 1));
2482                   cfa.offset += offset;
2483                   cfa.reg = REGNO (dest);
2484                   /* Or used to save regs to the stack.  */
2485                   cfa_temp.reg = cfa.reg;
2486                   cfa_temp.offset = cfa.offset;
2487                 }
2488
2489               /* Rule 5 */
2490               else if (REG_P (XEXP (src, 0))
2491                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2492                        && XEXP (src, 1) == stack_pointer_rtx)
2493                 {
2494                   /* Setting a scratch register that we will use instead
2495                      of SP for saving registers to the stack.  */
2496                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2497                   cfa_store.reg = REGNO (dest);
2498                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2499                 }
2500
2501               /* Rule 9 */
2502               else if (GET_CODE (src) == LO_SUM
2503                        && CONST_INT_P (XEXP (src, 1)))
2504                 {
2505                   cfa_temp.reg = REGNO (dest);
2506                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2507                 }
2508               else
2509                 gcc_unreachable ();
2510             }
2511           break;
2512
2513           /* Rule 6 */
2514         case CONST_INT:
2515           cfa_temp.reg = REGNO (dest);
2516           cfa_temp.offset = INTVAL (src);
2517           break;
2518
2519           /* Rule 7 */
2520         case IOR:
2521           gcc_assert (REG_P (XEXP (src, 0))
2522                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2523                       && CONST_INT_P (XEXP (src, 1)));
2524
2525           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2526             cfa_temp.reg = REGNO (dest);
2527           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2528           break;
2529
2530           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2531              which will fill in all of the bits.  */
2532           /* Rule 8 */
2533         case HIGH:
2534           break;
2535
2536           /* Rule 15 */
2537         case UNSPEC:
2538         case UNSPEC_VOLATILE:
2539           gcc_assert (targetm.dwarf_handle_frame_unspec);
2540           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2541           return;
2542
2543           /* Rule 16 */
2544         case AND:
2545           /* If this AND operation happens on stack pointer in prologue,
2546              we assume the stack is realigned and we extract the
2547              alignment.  */
2548           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2549             {
2550               /* We interpret reg_save differently with stack_realign set.
2551                  Thus we must flush whatever we have queued first.  */
2552               dwarf2out_flush_queued_reg_saves ();
2553
2554               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2555               fde->stack_realign = 1;
2556               fde->stack_realignment = INTVAL (XEXP (src, 1));
2557               cfa_store.offset = 0;
2558
2559               if (cfa.reg != STACK_POINTER_REGNUM
2560                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2561                 fde->drap_reg = cfa.reg;
2562             }
2563           return;
2564
2565         default:
2566           gcc_unreachable ();
2567         }
2568
2569       def_cfa_1 (label, &cfa);
2570       break;
2571
2572     case MEM:
2573
2574       /* Saving a register to the stack.  Make sure dest is relative to the
2575          CFA register.  */
2576       switch (GET_CODE (XEXP (dest, 0)))
2577         {
2578           /* Rule 10 */
2579           /* With a push.  */
2580         case PRE_MODIFY:
2581         case POST_MODIFY:
2582           /* We can't handle variable size modifications.  */
2583           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2584                       == CONST_INT);
2585           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2586
2587           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2588                       && cfa_store.reg == STACK_POINTER_REGNUM);
2589
2590           cfa_store.offset += offset;
2591           if (cfa.reg == STACK_POINTER_REGNUM)
2592             cfa.offset = cfa_store.offset;
2593
2594           if (GET_CODE (XEXP (dest, 0)) == POST_MODIFY)
2595             offset -= cfa_store.offset;
2596           else
2597             offset = -cfa_store.offset;
2598           break;
2599
2600           /* Rule 11 */
2601         case PRE_INC:
2602         case PRE_DEC:
2603         case POST_DEC:
2604           offset = GET_MODE_SIZE (GET_MODE (dest));
2605           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2606             offset = -offset;
2607
2608           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2609                        == STACK_POINTER_REGNUM)
2610                       && cfa_store.reg == STACK_POINTER_REGNUM);
2611
2612           cfa_store.offset += offset;
2613
2614           /* Rule 18: If stack is aligned, we will use FP as a
2615              reference to represent the address of the stored
2616              regiser.  */
2617           if (fde
2618               && fde->stack_realign
2619               && src == hard_frame_pointer_rtx)
2620             {
2621               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2622               cfa_store.offset = 0;
2623             }
2624
2625           if (cfa.reg == STACK_POINTER_REGNUM)
2626             cfa.offset = cfa_store.offset;
2627
2628           if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2629             offset += -cfa_store.offset;
2630           else
2631             offset = -cfa_store.offset;
2632           break;
2633
2634           /* Rule 12 */
2635           /* With an offset.  */
2636         case PLUS:
2637         case MINUS:
2638         case LO_SUM:
2639           {
2640             int regno;
2641
2642             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2643                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2644             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2645             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2646               offset = -offset;
2647
2648             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2649
2650             if (cfa.reg == (unsigned) regno)
2651               offset -= cfa.offset;
2652             else if (cfa_store.reg == (unsigned) regno)
2653               offset -= cfa_store.offset;
2654             else
2655               {
2656                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2657                 offset -= cfa_temp.offset;
2658               }
2659           }
2660           break;
2661
2662           /* Rule 13 */
2663           /* Without an offset.  */
2664         case REG:
2665           {
2666             int regno = REGNO (XEXP (dest, 0));
2667
2668             if (cfa.reg == (unsigned) regno)
2669               offset = -cfa.offset;
2670             else if (cfa_store.reg == (unsigned) regno)
2671               offset = -cfa_store.offset;
2672             else
2673               {
2674                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2675                 offset = -cfa_temp.offset;
2676               }
2677           }
2678           break;
2679
2680           /* Rule 14 */
2681         case POST_INC:
2682           gcc_assert (cfa_temp.reg
2683                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2684           offset = -cfa_temp.offset;
2685           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2686           break;
2687
2688         default:
2689           gcc_unreachable ();
2690         }
2691
2692         /* Rule 17 */
2693         /* If the source operand of this MEM operation is not a
2694            register, basically the source is return address.  Here
2695            we only care how much stack grew and we don't save it.  */
2696       if (!REG_P (src))
2697         break;
2698
2699       if (REGNO (src) != STACK_POINTER_REGNUM
2700           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2701           && (unsigned) REGNO (src) == cfa.reg)
2702         {
2703           /* We're storing the current CFA reg into the stack.  */
2704
2705           if (cfa.offset == 0)
2706             {
2707               /* Rule 19 */
2708               /* If stack is aligned, putting CFA reg into stack means
2709                  we can no longer use reg + offset to represent CFA.
2710                  Here we use DW_CFA_def_cfa_expression instead.  The
2711                  result of this expression equals to the original CFA
2712                  value.  */
2713               if (fde
2714                   && fde->stack_realign
2715                   && cfa.indirect == 0
2716                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2717                 {
2718                   dw_cfa_location cfa_exp;
2719
2720                   gcc_assert (fde->drap_reg == cfa.reg);
2721
2722                   cfa_exp.indirect = 1;
2723                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2724                   cfa_exp.base_offset = offset;
2725                   cfa_exp.offset = 0;
2726
2727                   fde->drap_reg_saved = 1;
2728
2729                   def_cfa_1 (label, &cfa_exp);
2730                   break;
2731                 }
2732
2733               /* If the source register is exactly the CFA, assume
2734                  we're saving SP like any other register; this happens
2735                  on the ARM.  */
2736               def_cfa_1 (label, &cfa);
2737               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2738               break;
2739             }
2740           else
2741             {
2742               /* Otherwise, we'll need to look in the stack to
2743                  calculate the CFA.  */
2744               rtx x = XEXP (dest, 0);
2745
2746               if (!REG_P (x))
2747                 x = XEXP (x, 0);
2748               gcc_assert (REG_P (x));
2749
2750               cfa.reg = REGNO (x);
2751               cfa.base_offset = offset;
2752               cfa.indirect = 1;
2753               def_cfa_1 (label, &cfa);
2754               break;
2755             }
2756         }
2757
2758       def_cfa_1 (label, &cfa);
2759       {
2760         span = targetm.dwarf_register_span (src);
2761
2762         if (!span)
2763           queue_reg_save (label, src, NULL_RTX, offset);
2764         else
2765           {
2766             /* We have a PARALLEL describing where the contents of SRC
2767                live.  Queue register saves for each piece of the
2768                PARALLEL.  */
2769             int par_index;
2770             int limit;
2771             HOST_WIDE_INT span_offset = offset;
2772
2773             gcc_assert (GET_CODE (span) == PARALLEL);
2774
2775             limit = XVECLEN (span, 0);
2776             for (par_index = 0; par_index < limit; par_index++)
2777               {
2778                 rtx elem = XVECEXP (span, 0, par_index);
2779
2780                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2781                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2782               }
2783           }
2784       }
2785       break;
2786
2787     default:
2788       gcc_unreachable ();
2789     }
2790 }
2791
2792 /* Record call frame debugging information for INSN, which either
2793    sets SP or FP (adjusting how we calculate the frame address) or saves a
2794    register to the stack.  If INSN is NULL_RTX, initialize our state.
2795
2796    If AFTER_P is false, we're being called before the insn is emitted,
2797    otherwise after.  Call instructions get invoked twice.  */
2798
2799 void
2800 dwarf2out_frame_debug (rtx insn, bool after_p)
2801 {
2802   const char *label;
2803   rtx note, n;
2804   bool handled_one = false;
2805
2806   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2807     dwarf2out_flush_queued_reg_saves ();
2808
2809   if (!RTX_FRAME_RELATED_P (insn))
2810     {
2811       /* ??? This should be done unconditionally since stack adjustments
2812          matter if the stack pointer is not the CFA register anymore but
2813          is still used to save registers.  */
2814       if (!ACCUMULATE_OUTGOING_ARGS)
2815         dwarf2out_notice_stack_adjust (insn, after_p);
2816       return;
2817     }
2818
2819   label = dwarf2out_cfi_label (false);
2820   any_cfis_emitted = false;
2821
2822   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2823     switch (REG_NOTE_KIND (note))
2824       {
2825       case REG_FRAME_RELATED_EXPR:
2826         insn = XEXP (note, 0);
2827         goto found;
2828
2829       case REG_CFA_DEF_CFA:
2830         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2831         handled_one = true;
2832         break;
2833
2834       case REG_CFA_ADJUST_CFA:
2835         n = XEXP (note, 0);
2836         if (n == NULL)
2837           {
2838             n = PATTERN (insn);
2839             if (GET_CODE (n) == PARALLEL)
2840               n = XVECEXP (n, 0, 0);
2841           }
2842         dwarf2out_frame_debug_adjust_cfa (n, label);
2843         handled_one = true;
2844         break;
2845
2846       case REG_CFA_OFFSET:
2847         n = XEXP (note, 0);
2848         if (n == NULL)
2849           n = single_set (insn);
2850         dwarf2out_frame_debug_cfa_offset (n, label);
2851         handled_one = true;
2852         break;
2853
2854       case REG_CFA_REGISTER:
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_cfa_register (n, label);
2863         handled_one = true;
2864         break;
2865
2866       case REG_CFA_EXPRESSION:
2867         n = XEXP (note, 0);
2868         if (n == NULL)
2869           n = single_set (insn);
2870         dwarf2out_frame_debug_cfa_expression (n, label);
2871         handled_one = true;
2872         break;
2873
2874       case REG_CFA_RESTORE:
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             n = XEXP (n, 0);
2882           }
2883         dwarf2out_frame_debug_cfa_restore (n, label);
2884         handled_one = true;
2885         break;
2886
2887       case REG_CFA_SET_VDRAP:
2888         n = XEXP (note, 0);
2889         if (REG_P (n))
2890           {
2891             dw_fde_ref fde = current_fde ();
2892             if (fde)
2893               {
2894                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2895                 if (REG_P (n))
2896                   fde->vdrap_reg = REGNO (n);
2897               }
2898           }
2899         handled_one = true;
2900         break;
2901
2902       default:
2903         break;
2904       }
2905   if (handled_one)
2906     {
2907       if (any_cfis_emitted)
2908         dwarf2out_flush_queued_reg_saves ();
2909       return;
2910     }
2911
2912   insn = PATTERN (insn);
2913  found:
2914   dwarf2out_frame_debug_expr (insn, label);
2915
2916   /* Check again.  A parallel can save and update the same register.
2917      We could probably check just once, here, but this is safer than
2918      removing the check above.  */
2919   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2920     dwarf2out_flush_queued_reg_saves ();
2921 }
2922
2923 /* Called once at the start of final to initialize some data for the
2924    current function.  */
2925 void
2926 dwarf2out_frame_debug_init (void)
2927 {
2928   size_t i;
2929
2930   /* Flush any queued register saves.  */
2931   dwarf2out_flush_queued_reg_saves ();
2932
2933   /* Set up state for generating call frame debug info.  */
2934   lookup_cfa (&cfa);
2935   gcc_assert (cfa.reg
2936               == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2937
2938   cfa.reg = STACK_POINTER_REGNUM;
2939   cfa_store = cfa;
2940   cfa_temp.reg = -1;
2941   cfa_temp.offset = 0;
2942
2943   for (i = 0; i < num_regs_saved_in_regs; i++)
2944     {
2945       regs_saved_in_regs[i].orig_reg = NULL_RTX;
2946       regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2947     }
2948   num_regs_saved_in_regs = 0;
2949
2950   if (barrier_args_size)
2951     {
2952       XDELETEVEC (barrier_args_size);
2953       barrier_args_size = NULL;
2954     }
2955 }
2956
2957 /* Determine if we need to save and restore CFI information around this
2958    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2959    we do need to save/restore, then emit the save now, and insert a
2960    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2961
2962 void
2963 dwarf2out_cfi_begin_epilogue (rtx insn)
2964 {
2965   bool saw_frp = false;
2966   rtx i;
2967
2968   /* Scan forward to the return insn, noticing if there are possible
2969      frame related insns.  */
2970   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2971     {
2972       if (!INSN_P (i))
2973         continue;
2974
2975       /* Look for both regular and sibcalls to end the block.  */
2976       if (returnjump_p (i))
2977         break;
2978       if (CALL_P (i) && SIBLING_CALL_P (i))
2979         break;
2980
2981       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2982         {
2983           int idx;
2984           rtx seq = PATTERN (i);
2985
2986           if (returnjump_p (XVECEXP (seq, 0, 0)))
2987             break;
2988           if (CALL_P (XVECEXP (seq, 0, 0))
2989               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2990             break;
2991
2992           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2993             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2994               saw_frp = true;
2995         }
2996
2997       if (RTX_FRAME_RELATED_P (i))
2998         saw_frp = true;
2999     }
3000
3001   /* If the port doesn't emit epilogue unwind info, we don't need a
3002      save/restore pair.  */
3003   if (!saw_frp)
3004     return;
3005
3006   /* Otherwise, search forward to see if the return insn was the last
3007      basic block of the function.  If so, we don't need save/restore.  */
3008   gcc_assert (i != NULL);
3009   i = next_real_insn (i);
3010   if (i == NULL)
3011     return;
3012
3013   /* Insert the restore before that next real insn in the stream, and before
3014      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3015      properly nested.  This should be after any label or alignment.  This
3016      will be pushed into the CFI stream by the function below.  */
3017   while (1)
3018     {
3019       rtx p = PREV_INSN (i);
3020       if (!NOTE_P (p))
3021         break;
3022       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3023         break;
3024       i = p;
3025     }
3026   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3027
3028   emit_cfa_remember = true;
3029
3030   /* And emulate the state save.  */
3031   gcc_assert (!cfa_remember.in_use);
3032   cfa_remember = cfa;
3033   cfa_remember.in_use = 1;
3034 }
3035
3036 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3037    required.  */
3038
3039 void
3040 dwarf2out_frame_debug_restore_state (void)
3041 {
3042   dw_cfi_ref cfi = new_cfi ();
3043   const char *label = dwarf2out_cfi_label (false);
3044
3045   cfi->dw_cfi_opc = DW_CFA_restore_state;
3046   add_fde_cfi (label, cfi);
3047
3048   gcc_assert (cfa_remember.in_use);
3049   cfa = cfa_remember;
3050   cfa_remember.in_use = 0;
3051 }
3052
3053 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3054 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3055  (enum dwarf_call_frame_info cfi);
3056
3057 static enum dw_cfi_oprnd_type
3058 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3059 {
3060   switch (cfi)
3061     {
3062     case DW_CFA_nop:
3063     case DW_CFA_GNU_window_save:
3064     case DW_CFA_remember_state:
3065     case DW_CFA_restore_state:
3066       return dw_cfi_oprnd_unused;
3067
3068     case DW_CFA_set_loc:
3069     case DW_CFA_advance_loc1:
3070     case DW_CFA_advance_loc2:
3071     case DW_CFA_advance_loc4:
3072     case DW_CFA_MIPS_advance_loc8:
3073       return dw_cfi_oprnd_addr;
3074
3075     case DW_CFA_offset:
3076     case DW_CFA_offset_extended:
3077     case DW_CFA_def_cfa:
3078     case DW_CFA_offset_extended_sf:
3079     case DW_CFA_def_cfa_sf:
3080     case DW_CFA_restore:
3081     case DW_CFA_restore_extended:
3082     case DW_CFA_undefined:
3083     case DW_CFA_same_value:
3084     case DW_CFA_def_cfa_register:
3085     case DW_CFA_register:
3086     case DW_CFA_expression:
3087       return dw_cfi_oprnd_reg_num;
3088
3089     case DW_CFA_def_cfa_offset:
3090     case DW_CFA_GNU_args_size:
3091     case DW_CFA_def_cfa_offset_sf:
3092       return dw_cfi_oprnd_offset;
3093
3094     case DW_CFA_def_cfa_expression:
3095       return dw_cfi_oprnd_loc;
3096
3097     default:
3098       gcc_unreachable ();
3099     }
3100 }
3101
3102 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3103 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3104  (enum dwarf_call_frame_info cfi);
3105
3106 static enum dw_cfi_oprnd_type
3107 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3108 {
3109   switch (cfi)
3110     {
3111     case DW_CFA_def_cfa:
3112     case DW_CFA_def_cfa_sf:
3113     case DW_CFA_offset:
3114     case DW_CFA_offset_extended_sf:
3115     case DW_CFA_offset_extended:
3116       return dw_cfi_oprnd_offset;
3117
3118     case DW_CFA_register:
3119       return dw_cfi_oprnd_reg_num;
3120
3121     case DW_CFA_expression:
3122       return dw_cfi_oprnd_loc;
3123
3124     default:
3125       return dw_cfi_oprnd_unused;
3126     }
3127 }
3128
3129 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3130    switch to the data section instead, and write out a synthetic start label
3131    for collect2 the first time around.  */
3132
3133 static void
3134 switch_to_eh_frame_section (bool back)
3135 {
3136   tree label;
3137
3138 #ifdef EH_FRAME_SECTION_NAME
3139   if (eh_frame_section == 0)
3140     {
3141       int flags;
3142
3143       if (EH_TABLES_CAN_BE_READ_ONLY)
3144         {
3145           int fde_encoding;
3146           int per_encoding;
3147           int lsda_encoding;
3148
3149           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3150                                                        /*global=*/0);
3151           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3152                                                        /*global=*/1);
3153           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3154                                                         /*global=*/0);
3155           flags = ((! flag_pic
3156                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3157                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3158                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3159                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3160                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3161                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3162                    ? 0 : SECTION_WRITE);
3163         }
3164       else
3165         flags = SECTION_WRITE;
3166       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3167     }
3168 #endif /* EH_FRAME_SECTION_NAME */
3169
3170   if (eh_frame_section)
3171     switch_to_section (eh_frame_section);
3172   else
3173     {
3174       /* We have no special eh_frame section.  Put the information in
3175          the data section and emit special labels to guide collect2.  */
3176       switch_to_section (data_section);
3177
3178       if (!back)
3179         {
3180           label = get_file_function_name ("F");
3181           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3182           targetm.asm_out.globalize_label (asm_out_file,
3183                                            IDENTIFIER_POINTER (label));
3184           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3185         }
3186     }
3187 }
3188
3189 /* Switch [BACK] to the eh or debug frame table section, depending on
3190    FOR_EH.  */
3191
3192 static void
3193 switch_to_frame_table_section (int for_eh, bool back)
3194 {
3195   if (for_eh)
3196     switch_to_eh_frame_section (back);
3197   else
3198     {
3199       if (!debug_frame_section)
3200         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3201                                            SECTION_DEBUG, NULL);
3202       switch_to_section (debug_frame_section);
3203     }
3204 }
3205
3206 /* Output a Call Frame Information opcode and its operand(s).  */
3207
3208 static void
3209 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3210 {
3211   unsigned long r;
3212   HOST_WIDE_INT off;
3213
3214   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3215     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3216                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3217                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3218                          ((unsigned HOST_WIDE_INT)
3219                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3220   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3221     {
3222       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3223       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3224                            "DW_CFA_offset, column %#lx", r);
3225       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3226       dw2_asm_output_data_uleb128 (off, NULL);
3227     }
3228   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3229     {
3230       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3231       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3232                            "DW_CFA_restore, column %#lx", r);
3233     }
3234   else
3235     {
3236       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3237                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3238
3239       switch (cfi->dw_cfi_opc)
3240         {
3241         case DW_CFA_set_loc:
3242           if (for_eh)
3243             dw2_asm_output_encoded_addr_rtx (
3244                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3245                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3246                 false, NULL);
3247           else
3248             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3249                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3250           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3251           break;
3252
3253         case DW_CFA_advance_loc1:
3254           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3255                                 fde->dw_fde_current_label, NULL);
3256           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3257           break;
3258
3259         case DW_CFA_advance_loc2:
3260           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3261                                 fde->dw_fde_current_label, NULL);
3262           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3263           break;
3264
3265         case DW_CFA_advance_loc4:
3266           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3267                                 fde->dw_fde_current_label, NULL);
3268           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3269           break;
3270
3271         case DW_CFA_MIPS_advance_loc8:
3272           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3273                                 fde->dw_fde_current_label, NULL);
3274           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3275           break;
3276
3277         case DW_CFA_offset_extended:
3278           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3279           dw2_asm_output_data_uleb128 (r, NULL);
3280           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3281           dw2_asm_output_data_uleb128 (off, NULL);
3282           break;
3283
3284         case DW_CFA_def_cfa:
3285           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3286           dw2_asm_output_data_uleb128 (r, NULL);
3287           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3288           break;
3289
3290         case DW_CFA_offset_extended_sf:
3291           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3292           dw2_asm_output_data_uleb128 (r, NULL);
3293           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3294           dw2_asm_output_data_sleb128 (off, NULL);
3295           break;
3296
3297         case DW_CFA_def_cfa_sf:
3298           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3299           dw2_asm_output_data_uleb128 (r, NULL);
3300           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3301           dw2_asm_output_data_sleb128 (off, NULL);
3302           break;
3303
3304         case DW_CFA_restore_extended:
3305         case DW_CFA_undefined:
3306         case DW_CFA_same_value:
3307         case DW_CFA_def_cfa_register:
3308           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3309           dw2_asm_output_data_uleb128 (r, NULL);
3310           break;
3311
3312         case DW_CFA_register:
3313           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3314           dw2_asm_output_data_uleb128 (r, NULL);
3315           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3316           dw2_asm_output_data_uleb128 (r, NULL);
3317           break;
3318
3319         case DW_CFA_def_cfa_offset:
3320         case DW_CFA_GNU_args_size:
3321           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3322           break;
3323
3324         case DW_CFA_def_cfa_offset_sf:
3325           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3326           dw2_asm_output_data_sleb128 (off, NULL);
3327           break;
3328
3329         case DW_CFA_GNU_window_save:
3330           break;
3331
3332         case DW_CFA_def_cfa_expression:
3333         case DW_CFA_expression:
3334           output_cfa_loc (cfi, for_eh);
3335           break;
3336
3337         case DW_CFA_GNU_negative_offset_extended:
3338           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3339           gcc_unreachable ();
3340
3341         default:
3342           break;
3343         }
3344     }
3345 }
3346
3347 /* Similar, but do it via assembler directives instead.  */
3348
3349 static void
3350 output_cfi_directive (dw_cfi_ref cfi)
3351 {
3352   unsigned long r, r2;
3353
3354   switch (cfi->dw_cfi_opc)
3355     {
3356     case DW_CFA_advance_loc:
3357     case DW_CFA_advance_loc1:
3358     case DW_CFA_advance_loc2:
3359     case DW_CFA_advance_loc4:
3360     case DW_CFA_MIPS_advance_loc8:
3361     case DW_CFA_set_loc:
3362       /* Should only be created by add_fde_cfi in a code path not
3363          followed when emitting via directives.  The assembler is
3364          going to take care of this for us.  */
3365       gcc_unreachable ();
3366
3367     case DW_CFA_offset:
3368     case DW_CFA_offset_extended:
3369     case DW_CFA_offset_extended_sf:
3370       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3371       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3372                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3373       break;
3374
3375     case DW_CFA_restore:
3376     case DW_CFA_restore_extended:
3377       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3378       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3379       break;
3380
3381     case DW_CFA_undefined:
3382       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3383       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3384       break;
3385
3386     case DW_CFA_same_value:
3387       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3388       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3389       break;
3390
3391     case DW_CFA_def_cfa:
3392     case DW_CFA_def_cfa_sf:
3393       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3394       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3395                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3396       break;
3397
3398     case DW_CFA_def_cfa_register:
3399       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3400       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3401       break;
3402
3403     case DW_CFA_register:
3404       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3405       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3406       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3407       break;
3408
3409     case DW_CFA_def_cfa_offset:
3410     case DW_CFA_def_cfa_offset_sf:
3411       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3412                HOST_WIDE_INT_PRINT_DEC"\n",
3413                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3414       break;
3415
3416     case DW_CFA_remember_state:
3417       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3418       break;
3419     case DW_CFA_restore_state:
3420       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3421       break;
3422
3423     case DW_CFA_GNU_args_size:
3424       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3425       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3426       if (flag_debug_asm)
3427         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3428                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3429       fputc ('\n', asm_out_file);
3430       break;
3431
3432     case DW_CFA_GNU_window_save:
3433       fprintf (asm_out_file, "\t.cfi_window_save\n");
3434       break;
3435
3436     case DW_CFA_def_cfa_expression:
3437     case DW_CFA_expression:
3438       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3439       output_cfa_loc_raw (cfi);
3440       fputc ('\n', asm_out_file);
3441       break;
3442
3443     default:
3444       gcc_unreachable ();
3445     }
3446 }
3447
3448 /* Output CFIs from VEC, up to index UPTO, to bring current FDE to the
3449    same state as after executing CFIs in CFI chain.  DO_CFI_ASM is
3450    true if .cfi_* directives shall be emitted, false otherwise.  If it
3451    is false, FDE and FOR_EH are the other arguments to pass to
3452    output_cfi.  */
3453
3454 static void
3455 output_cfis (cfi_vec vec, int upto, bool do_cfi_asm,
3456              dw_fde_ref fde, bool for_eh)
3457 {
3458   int ix;
3459   struct dw_cfi_struct cfi_buf;
3460   dw_cfi_ref cfi2;
3461   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3462   VEC(dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3463   unsigned int len, idx;
3464
3465   for (ix = 0; ix < upto + 1; ix++)
3466     {
3467       dw_cfi_ref cfi = ix < upto ? VEC_index (dw_cfi_ref, vec, ix) : NULL;
3468       switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3469         {
3470         case DW_CFA_advance_loc:
3471         case DW_CFA_advance_loc1:
3472         case DW_CFA_advance_loc2:
3473         case DW_CFA_advance_loc4:
3474         case DW_CFA_MIPS_advance_loc8:
3475         case DW_CFA_set_loc:
3476           /* All advances should be ignored.  */
3477           break;
3478         case DW_CFA_remember_state:
3479           {
3480             dw_cfi_ref args_size = cfi_args_size;
3481
3482             /* Skip everything between .cfi_remember_state and
3483                .cfi_restore_state.  */
3484             ix++;
3485             if (ix == upto)
3486               goto flush_all;
3487
3488             for (; ix < upto; ix++)
3489               {
3490                 cfi2 = VEC_index (dw_cfi_ref, vec, ix);
3491                 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3492                   break;
3493                 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3494                   args_size = cfi2;
3495                 else
3496                   gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3497               }
3498
3499             cfi_args_size = args_size;
3500             break;
3501           }
3502         case DW_CFA_GNU_args_size:
3503           cfi_args_size = cfi;
3504           break;
3505         case DW_CFA_GNU_window_save:
3506           goto flush_all;
3507         case DW_CFA_offset:
3508         case DW_CFA_offset_extended:
3509         case DW_CFA_offset_extended_sf:
3510         case DW_CFA_restore:
3511         case DW_CFA_restore_extended:
3512         case DW_CFA_undefined:
3513         case DW_CFA_same_value:
3514         case DW_CFA_register:
3515         case DW_CFA_val_offset:
3516         case DW_CFA_val_offset_sf:
3517         case DW_CFA_expression:
3518         case DW_CFA_val_expression:
3519         case DW_CFA_GNU_negative_offset_extended:
3520           if (VEC_length (dw_cfi_ref, regs)
3521               <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3522             VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3523                                    cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3524           VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
3525                        cfi);
3526           break;
3527         case DW_CFA_def_cfa:
3528         case DW_CFA_def_cfa_sf:
3529         case DW_CFA_def_cfa_expression:
3530           cfi_cfa = cfi;
3531           cfi_cfa_offset = cfi;
3532           break;
3533         case DW_CFA_def_cfa_register:
3534           cfi_cfa = cfi;
3535           break;
3536         case DW_CFA_def_cfa_offset:
3537         case DW_CFA_def_cfa_offset_sf:
3538           cfi_cfa_offset = cfi;
3539           break;
3540         case DW_CFA_nop:
3541           gcc_assert (cfi == NULL);
3542         flush_all:
3543           len = VEC_length (dw_cfi_ref, regs);
3544           for (idx = 0; idx < len; idx++)
3545             {
3546               cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3547               if (cfi2 != NULL
3548                   && cfi2->dw_cfi_opc != DW_CFA_restore
3549                   && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3550                 {
3551                   if (do_cfi_asm)
3552                     output_cfi_directive (cfi2);
3553                   else
3554                     output_cfi (cfi2, fde, for_eh);
3555                 }
3556             }
3557           if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3558             {
3559               gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3560               cfi_buf = *cfi_cfa;
3561               switch (cfi_cfa_offset->dw_cfi_opc)
3562                 {
3563                 case DW_CFA_def_cfa_offset:
3564                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3565                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3566                   break;
3567                 case DW_CFA_def_cfa_offset_sf:
3568                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3569                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3570                   break;
3571                 case DW_CFA_def_cfa:
3572                 case DW_CFA_def_cfa_sf:
3573                   cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3574                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3575                   break;
3576                 default:
3577                   gcc_unreachable ();
3578                 }
3579               cfi_cfa = &cfi_buf;
3580             }
3581           else if (cfi_cfa_offset)
3582             cfi_cfa = cfi_cfa_offset;
3583           if (cfi_cfa)
3584             {
3585               if (do_cfi_asm)
3586                 output_cfi_directive (cfi_cfa);
3587               else
3588                 output_cfi (cfi_cfa, fde, for_eh);
3589             }
3590           cfi_cfa = NULL;
3591           cfi_cfa_offset = NULL;
3592           if (cfi_args_size
3593               && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3594             {
3595               if (do_cfi_asm)
3596                 output_cfi_directive (cfi_args_size);
3597               else
3598                 output_cfi (cfi_args_size, fde, for_eh);
3599             }
3600           cfi_args_size = NULL;
3601           if (cfi == NULL)
3602             {
3603               VEC_free (dw_cfi_ref, heap, regs);
3604               return;
3605             }
3606           else if (do_cfi_asm)
3607             output_cfi_directive (cfi);
3608           else
3609             output_cfi (cfi, fde, for_eh);
3610           break;
3611         default:
3612           gcc_unreachable ();
3613         }
3614     }
3615 }
3616
3617 /* Like output_cfis, but emit all CFIs in the vector.  */
3618 static void
3619 output_all_cfis (cfi_vec vec, bool do_cfi_asm,
3620                  dw_fde_ref fde, bool for_eh)
3621 {
3622   output_cfis (vec, VEC_length (dw_cfi_ref, vec), do_cfi_asm, fde, for_eh);
3623 }
3624
3625 /* Output one FDE.  */
3626
3627 static void
3628 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3629             char *section_start_label, int fde_encoding, char *augmentation,
3630             bool any_lsda_needed, int lsda_encoding)
3631 {
3632   int ix;
3633   const char *begin, *end;
3634   static unsigned int j;
3635   char l1[20], l2[20];
3636   dw_cfi_ref cfi;
3637
3638   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3639                                      /* empty */ 0);
3640   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3641                                   for_eh + j);
3642   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3643   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3644   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3645     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3646                          " indicating 64-bit DWARF extension");
3647   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3648                         "FDE Length");
3649   ASM_OUTPUT_LABEL (asm_out_file, l1);
3650
3651   if (for_eh)
3652     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3653   else
3654     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3655                            debug_frame_section, "FDE CIE offset");
3656
3657   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
3658   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
3659
3660   if (for_eh)
3661     {
3662       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3663       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3664       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3665                                        "FDE initial location");
3666       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3667                             end, begin, "FDE address range");
3668     }
3669   else
3670     {
3671       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3672       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3673     }
3674
3675   if (augmentation[0])
3676     {
3677       if (any_lsda_needed)
3678         {
3679           int size = size_of_encoded_value (lsda_encoding);
3680
3681           if (lsda_encoding == DW_EH_PE_aligned)
3682             {
3683               int offset = (  4         /* Length */
3684                             + 4         /* CIE offset */
3685                             + 2 * size_of_encoded_value (fde_encoding)
3686                             + 1         /* Augmentation size */ );
3687               int pad = -offset & (PTR_SIZE - 1);
3688
3689               size += pad;
3690               gcc_assert (size_of_uleb128 (size) == 1);
3691             }
3692
3693           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3694
3695           if (fde->uses_eh_lsda)
3696             {
3697               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3698                                            fde->funcdef_number);
3699               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3700                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3701                                                false,
3702                                                "Language Specific Data Area");
3703             }
3704           else
3705             {
3706               if (lsda_encoding == DW_EH_PE_aligned)
3707                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3708               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3709                                    "Language Specific Data Area (none)");
3710             }
3711         }
3712       else
3713         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3714     }
3715
3716   /* Loop through the Call Frame Instructions associated with
3717      this FDE.  */
3718   fde->dw_fde_current_label = begin;
3719   if (fde->dw_fde_second_begin == NULL)
3720     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3721       output_cfi (cfi, fde, for_eh);
3722   else if (!second)
3723     {
3724       if (fde->dw_fde_switch_cfi_index > 0)
3725         FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3726           {
3727             if (ix == fde->dw_fde_switch_cfi_index)
3728               break;
3729             output_cfi (cfi, fde, for_eh);
3730           }
3731     }
3732   else
3733     {
3734       int i, from = 0;
3735       int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
3736
3737       if (fde->dw_fde_switch_cfi_index > 0)
3738         {
3739           from = fde->dw_fde_switch_cfi_index;
3740           output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
3741         }
3742       for (i = from; i < until; i++)
3743         output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
3744                     fde, for_eh);
3745     }
3746
3747   /* If we are to emit a ref/link from function bodies to their frame tables,
3748      do it now.  This is typically performed to make sure that tables
3749      associated with functions are dragged with them and not discarded in
3750      garbage collecting links. We need to do this on a per function basis to
3751      cope with -ffunction-sections.  */
3752
3753 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3754   /* Switch to the function section, emit the ref to the tables, and
3755      switch *back* into the table section.  */
3756   switch_to_section (function_section (fde->decl));
3757   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3758   switch_to_frame_table_section (for_eh, true);
3759 #endif
3760
3761   /* Pad the FDE out to an address sized boundary.  */
3762   ASM_OUTPUT_ALIGN (asm_out_file,
3763                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3764   ASM_OUTPUT_LABEL (asm_out_file, l2);
3765
3766   j += 2;
3767 }
3768
3769 /* Return true if frame description entry FDE is needed for EH.  */
3770
3771 static bool
3772 fde_needed_for_eh_p (dw_fde_ref fde)
3773 {
3774   if (flag_asynchronous_unwind_tables)
3775     return true;
3776
3777   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3778     return true;
3779
3780   if (fde->uses_eh_lsda)
3781     return true;
3782
3783   /* If exceptions are enabled, we have collected nothrow info.  */
3784   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3785     return false;
3786
3787   return true;
3788 }
3789
3790 /* Output the call frame information used to record information
3791    that relates to calculating the frame pointer, and records the
3792    location of saved registers.  */
3793
3794 static void
3795 output_call_frame_info (int for_eh)
3796 {
3797   unsigned int i;
3798   dw_fde_ref fde;
3799   dw_cfi_ref cfi;
3800   char l1[20], l2[20], section_start_label[20];
3801   bool any_lsda_needed = false;
3802   char augmentation[6];
3803   int augmentation_size;
3804   int fde_encoding = DW_EH_PE_absptr;
3805   int per_encoding = DW_EH_PE_absptr;
3806   int lsda_encoding = DW_EH_PE_absptr;
3807   int return_reg;
3808   rtx personality = NULL;
3809   int dw_cie_version;
3810
3811   /* Don't emit a CIE if there won't be any FDEs.  */
3812   if (fde_table_in_use == 0)
3813     return;
3814
3815   /* Nothing to do if the assembler's doing it all.  */
3816   if (dwarf2out_do_cfi_asm ())
3817     return;
3818
3819   /* If we don't have any functions we'll want to unwind out of, don't emit
3820      any EH unwind information.  If we make FDEs linkonce, we may have to
3821      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3822      want to avoid having an FDE kept around when the function it refers to
3823      is discarded.  Example where this matters: a primary function template
3824      in C++ requires EH information, an explicit specialization doesn't.  */
3825   if (for_eh)
3826     {
3827       bool any_eh_needed = false;
3828
3829       for (i = 0; i < fde_table_in_use; i++)
3830         if (fde_table[i].uses_eh_lsda)
3831           any_eh_needed = any_lsda_needed = true;
3832         else if (fde_needed_for_eh_p (&fde_table[i]))
3833           any_eh_needed = true;
3834         else if (TARGET_USES_WEAK_UNWIND_INFO)
3835           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3836                                              1, 1);
3837
3838       if (!any_eh_needed)
3839         return;
3840     }
3841
3842   /* We're going to be generating comments, so turn on app.  */
3843   if (flag_debug_asm)
3844     app_enable ();
3845
3846   /* Switch to the proper frame section, first time.  */
3847   switch_to_frame_table_section (for_eh, false);
3848
3849   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3850   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3851
3852   /* Output the CIE.  */
3853   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3854   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3855   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3856     dw2_asm_output_data (4, 0xffffffff,
3857       "Initial length escape value indicating 64-bit DWARF extension");
3858   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3859                         "Length of Common Information Entry");
3860   ASM_OUTPUT_LABEL (asm_out_file, l1);
3861
3862   /* Now that the CIE pointer is PC-relative for EH,
3863      use 0 to identify the CIE.  */
3864   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3865                        (for_eh ? 0 : DWARF_CIE_ID),
3866                        "CIE Identifier Tag");
3867
3868   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3869      use CIE version 1, unless that would produce incorrect results
3870      due to overflowing the return register column.  */
3871   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3872   dw_cie_version = 1;
3873   if (return_reg >= 256 || dwarf_version > 2)
3874     dw_cie_version = 3;
3875   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3876
3877   augmentation[0] = 0;
3878   augmentation_size = 0;
3879
3880   personality = current_unit_personality;
3881   if (for_eh)
3882     {
3883       char *p;
3884
3885       /* Augmentation:
3886          z      Indicates that a uleb128 is present to size the
3887                 augmentation section.
3888          L      Indicates the encoding (and thus presence) of
3889                 an LSDA pointer in the FDE augmentation.
3890          R      Indicates a non-default pointer encoding for
3891                 FDE code pointers.
3892          P      Indicates the presence of an encoding + language
3893                 personality routine in the CIE augmentation.  */
3894
3895       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3896       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3897       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3898
3899       p = augmentation + 1;
3900       if (personality)
3901         {
3902           *p++ = 'P';
3903           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3904           assemble_external_libcall (personality);
3905         }
3906       if (any_lsda_needed)
3907         {
3908           *p++ = 'L';
3909           augmentation_size += 1;
3910         }
3911       if (fde_encoding != DW_EH_PE_absptr)
3912         {
3913           *p++ = 'R';
3914           augmentation_size += 1;
3915         }
3916       if (p > augmentation + 1)
3917         {
3918           augmentation[0] = 'z';
3919           *p = '\0';
3920         }
3921
3922       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3923       if (personality && per_encoding == DW_EH_PE_aligned)
3924         {
3925           int offset = (  4             /* Length */
3926                         + 4             /* CIE Id */
3927                         + 1             /* CIE version */
3928                         + strlen (augmentation) + 1     /* Augmentation */
3929                         + size_of_uleb128 (1)           /* Code alignment */
3930                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3931                         + 1             /* RA column */
3932                         + 1             /* Augmentation size */
3933                         + 1             /* Personality encoding */ );
3934           int pad = -offset & (PTR_SIZE - 1);
3935
3936           augmentation_size += pad;
3937
3938           /* Augmentations should be small, so there's scarce need to
3939              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3940           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3941         }
3942     }
3943
3944   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3945   if (dw_cie_version >= 4)
3946     {
3947       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3948       dw2_asm_output_data (1, 0, "CIE Segment Size");
3949     }
3950   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3951   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3952                                "CIE Data Alignment Factor");
3953
3954   if (dw_cie_version == 1)
3955     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3956   else
3957     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3958
3959   if (augmentation[0])
3960     {
3961       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3962       if (personality)
3963         {
3964           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3965                                eh_data_format_name (per_encoding));
3966           dw2_asm_output_encoded_addr_rtx (per_encoding,
3967                                            personality,
3968                                            true, NULL);
3969         }
3970
3971       if (any_lsda_needed)
3972         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3973                              eh_data_format_name (lsda_encoding));
3974
3975       if (fde_encoding != DW_EH_PE_absptr)
3976         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3977                              eh_data_format_name (fde_encoding));
3978     }
3979
3980   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
3981     output_cfi (cfi, NULL, for_eh);
3982
3983   /* Pad the CIE out to an address sized boundary.  */
3984   ASM_OUTPUT_ALIGN (asm_out_file,
3985                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3986   ASM_OUTPUT_LABEL (asm_out_file, l2);
3987
3988   /* Loop through all of the FDE's.  */
3989   for (i = 0; i < fde_table_in_use; i++)
3990     {
3991       unsigned int k;
3992       fde = &fde_table[i];
3993
3994       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3995       if (for_eh && !fde_needed_for_eh_p (fde))
3996         continue;
3997
3998       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
3999         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
4000                     augmentation, any_lsda_needed, lsda_encoding);
4001     }
4002
4003   if (for_eh && targetm.terminate_dw2_eh_frame_info)
4004     dw2_asm_output_data (4, 0, "End of Table");
4005 #ifdef MIPS_DEBUGGING_INFO
4006   /* Work around Irix 6 assembler bug whereby labels at the end of a section
4007      get a value of 0.  Putting .align 0 after the label fixes it.  */
4008   ASM_OUTPUT_ALIGN (asm_out_file, 0);
4009 #endif
4010
4011   /* Turn off app to make assembly quicker.  */
4012   if (flag_debug_asm)
4013     app_disable ();
4014 }
4015
4016 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4017
4018 static void
4019 dwarf2out_do_cfi_startproc (bool second)
4020 {
4021   int enc;
4022   rtx ref;
4023   rtx personality = get_personality_function (current_function_decl);
4024
4025   fprintf (asm_out_file, "\t.cfi_startproc\n");
4026
4027   if (personality)
4028     {
4029       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4030       ref = personality;
4031
4032       /* ??? The GAS support isn't entirely consistent.  We have to
4033          handle indirect support ourselves, but PC-relative is done
4034          in the assembler.  Further, the assembler can't handle any
4035          of the weirder relocation types.  */
4036       if (enc & DW_EH_PE_indirect)
4037         ref = dw2_force_const_mem (ref, true);
4038
4039       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4040       output_addr_const (asm_out_file, ref);
4041       fputc ('\n', asm_out_file);
4042     }
4043
4044   if (crtl->uses_eh_lsda)
4045     {
4046       char lab[20];
4047
4048       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4049       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4050                                    current_function_funcdef_no);
4051       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4052       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4053
4054       if (enc & DW_EH_PE_indirect)
4055         ref = dw2_force_const_mem (ref, true);
4056
4057       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4058       output_addr_const (asm_out_file, ref);
4059       fputc ('\n', asm_out_file);
4060     }
4061 }
4062
4063 /* Output a marker (i.e. a label) for the beginning of a function, before
4064    the prologue.  */
4065
4066 void
4067 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4068                           const char *file ATTRIBUTE_UNUSED)
4069 {
4070   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4071   char * dup_label;
4072   dw_fde_ref fde;
4073   section *fnsec;
4074   bool do_frame;
4075
4076   current_function_func_begin_label = NULL;
4077
4078   do_frame = dwarf2out_do_frame ();
4079
4080   /* ??? current_function_func_begin_label is also used by except.c for
4081      call-site information.  We must emit this label if it might be used.  */
4082   if (!do_frame
4083       && (!flag_exceptions
4084           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4085     return;
4086
4087   fnsec = function_section (current_function_decl);
4088   switch_to_section (fnsec);
4089   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4090                                current_function_funcdef_no);
4091   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4092                           current_function_funcdef_no);
4093   dup_label = xstrdup (label);
4094   current_function_func_begin_label = dup_label;
4095
4096   /* We can elide the fde allocation if we're not emitting debug info.  */
4097   if (!do_frame)
4098     return;
4099
4100   /* Expand the fde table if necessary.  */
4101   if (fde_table_in_use == fde_table_allocated)
4102     {
4103       fde_table_allocated += FDE_TABLE_INCREMENT;
4104       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4105       memset (fde_table + fde_table_in_use, 0,
4106               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4107     }
4108
4109   /* Record the FDE associated with this function.  */
4110   current_funcdef_fde = fde_table_in_use;
4111
4112   /* Add the new FDE at the end of the fde_table.  */
4113   fde = &fde_table[fde_table_in_use++];
4114   fde->decl = current_function_decl;
4115   fde->dw_fde_begin = dup_label;
4116   fde->dw_fde_end = NULL;
4117   fde->dw_fde_current_label = dup_label;
4118   fde->dw_fde_second_begin = NULL;
4119   fde->dw_fde_second_end = NULL;
4120   fde->dw_fde_vms_end_prologue = NULL;
4121   fde->dw_fde_vms_begin_epilogue = NULL;
4122   fde->dw_fde_cfi = VEC_alloc (dw_cfi_ref, gc, 20);
4123   fde->dw_fde_switch_cfi_index = 0;
4124   fde->funcdef_number = current_function_funcdef_no;
4125   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4126   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4127   fde->nothrow = crtl->nothrow;
4128   fde->drap_reg = INVALID_REGNUM;
4129   fde->vdrap_reg = INVALID_REGNUM;
4130   fde->in_std_section = (fnsec == text_section
4131                          || (cold_text_section && fnsec == cold_text_section));
4132   fde->second_in_std_section = 0;
4133
4134   args_size = old_args_size = 0;
4135
4136   /* We only want to output line number information for the genuine dwarf2
4137      prologue case, not the eh frame case.  */
4138 #ifdef DWARF2_DEBUGGING_INFO
4139   if (file)
4140     dwarf2out_source_line (line, file, 0, true);
4141 #endif
4142
4143   if (dwarf2out_do_cfi_asm ())
4144     dwarf2out_do_cfi_startproc (false);
4145   else
4146     {
4147       rtx personality = get_personality_function (current_function_decl);
4148       if (!current_unit_personality)
4149         current_unit_personality = personality;
4150
4151       /* We cannot keep a current personality per function as without CFI
4152          asm, at the point where we emit the CFI data, there is no current
4153          function anymore.  */
4154       if (personality && current_unit_personality != personality)
4155         sorry ("multiple EH personalities are supported only with assemblers "
4156                "supporting .cfi_personality directive");
4157     }
4158 }
4159
4160 /* Output a marker (i.e. a label) for the end of the generated code
4161    for a function prologue.  This gets called *after* the prologue code has
4162    been generated.  */
4163
4164 void
4165 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4166                         const char *file ATTRIBUTE_UNUSED)
4167 {
4168   dw_fde_ref fde;
4169   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4170
4171   /* Output a label to mark the endpoint of the code generated for this
4172      function.  */
4173   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4174                                current_function_funcdef_no);
4175   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4176                           current_function_funcdef_no);
4177   fde = &fde_table[fde_table_in_use - 1];
4178   fde->dw_fde_vms_end_prologue = xstrdup (label);
4179 }
4180
4181 /* Output a marker (i.e. a label) for the beginning of the generated code
4182    for a function epilogue.  This gets called *before* the prologue code has
4183    been generated.  */
4184
4185 void
4186 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4187                           const char *file ATTRIBUTE_UNUSED)
4188 {
4189   dw_fde_ref fde;
4190   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4191
4192   fde = &fde_table[fde_table_in_use - 1];
4193   if (fde->dw_fde_vms_begin_epilogue)
4194     return;
4195
4196   /* Output a label to mark the endpoint of the code generated for this
4197      function.  */
4198   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4199                                current_function_funcdef_no);
4200   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4201                           current_function_funcdef_no);
4202   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4203 }
4204
4205 /* Output a marker (i.e. a label) for the absolute end of the generated code
4206    for a function definition.  This gets called *after* the epilogue code has
4207    been generated.  */
4208
4209 void
4210 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4211                         const char *file ATTRIBUTE_UNUSED)
4212 {
4213   dw_fde_ref fde;
4214   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4215
4216   last_var_location_insn = NULL_RTX;
4217
4218   if (dwarf2out_do_cfi_asm ())
4219     fprintf (asm_out_file, "\t.cfi_endproc\n");
4220
4221   /* Output a label to mark the endpoint of the code generated for this
4222      function.  */
4223   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4224                                current_function_funcdef_no);
4225   ASM_OUTPUT_LABEL (asm_out_file, label);
4226   fde = current_fde ();
4227   gcc_assert (fde != NULL);
4228   if (fde->dw_fde_second_begin == NULL)
4229     fde->dw_fde_end = xstrdup (label);
4230 }
4231
4232 void
4233 dwarf2out_frame_init (void)
4234 {
4235   /* Allocate the initial hunk of the fde_table.  */
4236   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4237   fde_table_allocated = FDE_TABLE_INCREMENT;
4238   fde_table_in_use = 0;
4239
4240   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4241      sake of lookup_cfa.  */
4242
4243   /* On entry, the Canonical Frame Address is at SP.  */
4244   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4245
4246   if (targetm.debug_unwind_info () == UI_DWARF2
4247       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4248     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4249 }
4250
4251 void
4252 dwarf2out_frame_finish (void)
4253 {
4254   /* Output call frame information.  */
4255   if (targetm.debug_unwind_info () == UI_DWARF2)
4256     output_call_frame_info (0);
4257
4258   /* Output another copy for the unwinder.  */
4259   if ((flag_unwind_tables || flag_exceptions)
4260       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4261     output_call_frame_info (1);
4262 }
4263
4264 /* Note that the current function section is being used for code.  */
4265
4266 static void
4267 dwarf2out_note_section_used (void)
4268 {
4269   section *sec = current_function_section ();
4270   if (sec == text_section)
4271     text_section_used = true;
4272   else if (sec == cold_text_section)
4273     cold_text_section_used = true;
4274 }
4275
4276 static void var_location_switch_text_section (void);
4277 static void set_cur_line_info_table (section *);
4278
4279 void
4280 dwarf2out_switch_text_section (void)
4281 {
4282   section *sect;
4283   dw_fde_ref fde = current_fde ();
4284
4285   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
4286
4287   if (!in_cold_section_p)
4288     {
4289       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
4290       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
4291       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
4292     }
4293   else
4294     {
4295       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
4296       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
4297       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
4298     }
4299   have_multiple_function_sections = true;
4300
4301   /* Reset the current label on switching text sections, so that we
4302      don't attempt to advance_loc4 between labels in different sections.  */
4303   fde->dw_fde_current_label = NULL;
4304
4305   /* There is no need to mark used sections when not debugging.  */
4306   if (cold_text_section != NULL)
4307     dwarf2out_note_section_used ();
4308
4309   if (dwarf2out_do_cfi_asm ())
4310     fprintf (asm_out_file, "\t.cfi_endproc\n");
4311
4312   /* Now do the real section switch.  */
4313   sect = current_function_section ();
4314   switch_to_section (sect);
4315
4316   fde->second_in_std_section
4317     = (sect == text_section
4318        || (cold_text_section && sect == cold_text_section));
4319
4320   if (dwarf2out_do_cfi_asm ())
4321     {
4322       dwarf2out_do_cfi_startproc (true);
4323       /* As this is a different FDE, insert all current CFI instructions
4324          again.  */
4325       output_all_cfis (fde->dw_fde_cfi, true, fde, true);
4326     }
4327   fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
4328   var_location_switch_text_section ();
4329
4330   set_cur_line_info_table (sect);
4331 }
4332 \f
4333 /* And now, the subset of the debugging information support code necessary
4334    for emitting location expressions.  */
4335
4336 /* Data about a single source file.  */
4337 struct GTY(()) dwarf_file_data {
4338   const char * filename;
4339   int emitted_number;
4340 };
4341
4342 typedef struct dw_val_struct *dw_val_ref;
4343 typedef struct die_struct *dw_die_ref;
4344 typedef const struct die_struct *const_dw_die_ref;
4345 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4346 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4347
4348 typedef struct GTY(()) deferred_locations_struct
4349 {
4350   tree variable;
4351   dw_die_ref die;
4352 } deferred_locations;
4353
4354 DEF_VEC_O(deferred_locations);
4355 DEF_VEC_ALLOC_O(deferred_locations,gc);
4356
4357 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4358
4359 DEF_VEC_P(dw_die_ref);
4360 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4361
4362 /* Each DIE may have a series of attribute/value pairs.  Values
4363    can take on several forms.  The forms that are used in this
4364    implementation are listed below.  */
4365
4366 enum dw_val_class
4367 {
4368   dw_val_class_addr,
4369   dw_val_class_offset,
4370   dw_val_class_loc,
4371   dw_val_class_loc_list,
4372   dw_val_class_range_list,
4373   dw_val_class_const,
4374   dw_val_class_unsigned_const,
4375   dw_val_class_const_double,
4376   dw_val_class_vec,
4377   dw_val_class_flag,
4378   dw_val_class_die_ref,
4379   dw_val_class_fde_ref,
4380   dw_val_class_lbl_id,
4381   dw_val_class_lineptr,
4382   dw_val_class_str,
4383   dw_val_class_macptr,
4384   dw_val_class_file,
4385   dw_val_class_data8,
4386   dw_val_class_decl_ref,
4387   dw_val_class_vms_delta
4388 };
4389
4390 /* Describe a floating point constant value, or a vector constant value.  */
4391
4392 typedef struct GTY(()) dw_vec_struct {
4393   unsigned char * GTY((length ("%h.length"))) array;
4394   unsigned length;
4395   unsigned elt_size;
4396 }
4397 dw_vec_const;
4398
4399 /* The dw_val_node describes an attribute's value, as it is
4400    represented internally.  */
4401
4402 typedef struct GTY(()) dw_val_struct {
4403   enum dw_val_class val_class;
4404   union dw_val_struct_union
4405     {
4406       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4407       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4408       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4409       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4410       HOST_WIDE_INT GTY ((default)) val_int;
4411       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4412       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4413       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4414       struct dw_val_die_union
4415         {
4416           dw_die_ref die;
4417           int external;
4418         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4419       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4420       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4421       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4422       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4423       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4424       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4425       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4426       struct dw_val_vms_delta_union
4427         {
4428           char * lbl1;
4429           char * lbl2;
4430         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4431     }
4432   GTY ((desc ("%1.val_class"))) v;
4433 }
4434 dw_val_node;
4435
4436 /* Locations in memory are described using a sequence of stack machine
4437    operations.  */
4438
4439 typedef struct GTY(()) dw_loc_descr_struct {
4440   dw_loc_descr_ref dw_loc_next;
4441   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4442   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4443      from DW_OP_addr with a dtp-relative symbol relocation.  */
4444   unsigned int dtprel : 1;
4445   int dw_loc_addr;
4446   dw_val_node dw_loc_oprnd1;
4447   dw_val_node dw_loc_oprnd2;
4448 }
4449 dw_loc_descr_node;
4450
4451 /* Location lists are ranges + location descriptions for that range,
4452    so you can track variables that are in different places over
4453    their entire life.  */
4454 typedef struct GTY(()) dw_loc_list_struct {
4455   dw_loc_list_ref dw_loc_next;
4456   const char *begin; /* Label for begin address of range */
4457   const char *end;  /* Label for end address of range */
4458   char *ll_symbol; /* Label for beginning of location list.
4459                       Only on head of list */
4460   const char *section; /* Section this loclist is relative to */
4461   dw_loc_descr_ref expr;
4462   hashval_t hash;
4463   /* True if all addresses in this and subsequent lists are known to be
4464      resolved.  */
4465   bool resolved_addr;
4466   /* True if this list has been replaced by dw_loc_next.  */
4467   bool replaced;
4468   bool emitted;
4469 } dw_loc_list_node;
4470
4471 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4472
4473 /* Convert a DWARF stack opcode into its string name.  */
4474
4475 static const char *
4476 dwarf_stack_op_name (unsigned int op)
4477 {
4478   switch (op)
4479     {
4480     case DW_OP_addr:
4481       return "DW_OP_addr";
4482     case DW_OP_deref:
4483       return "DW_OP_deref";
4484     case DW_OP_const1u:
4485       return "DW_OP_const1u";
4486     case DW_OP_const1s:
4487       return "DW_OP_const1s";
4488     case DW_OP_const2u:
4489       return "DW_OP_const2u";
4490     case DW_OP_const2s:
4491       return "DW_OP_const2s";
4492     case DW_OP_const4u:
4493       return "DW_OP_const4u";
4494     case DW_OP_const4s:
4495       return "DW_OP_const4s";
4496     case DW_OP_const8u:
4497       return "DW_OP_const8u";
4498     case DW_OP_const8s:
4499       return "DW_OP_const8s";
4500     case DW_OP_constu:
4501       return "DW_OP_constu";
4502     case DW_OP_consts:
4503       return "DW_OP_consts";
4504     case DW_OP_dup:
4505       return "DW_OP_dup";
4506     case DW_OP_drop:
4507       return "DW_OP_drop";
4508     case DW_OP_over:
4509       return "DW_OP_over";
4510     case DW_OP_pick:
4511       return "DW_OP_pick";
4512     case DW_OP_swap:
4513       return "DW_OP_swap";
4514     case DW_OP_rot:
4515       return "DW_OP_rot";
4516     case DW_OP_xderef:
4517       return "DW_OP_xderef";
4518     case DW_OP_abs:
4519       return "DW_OP_abs";
4520     case DW_OP_and:
4521       return "DW_OP_and";
4522     case DW_OP_div:
4523       return "DW_OP_div";
4524     case DW_OP_minus:
4525       return "DW_OP_minus";
4526     case DW_OP_mod:
4527       return "DW_OP_mod";
4528     case DW_OP_mul:
4529       return "DW_OP_mul";
4530     case DW_OP_neg:
4531       return "DW_OP_neg";
4532     case DW_OP_not:
4533       return "DW_OP_not";
4534     case DW_OP_or:
4535       return "DW_OP_or";
4536     case DW_OP_plus:
4537       return "DW_OP_plus";
4538     case DW_OP_plus_uconst:
4539       return "DW_OP_plus_uconst";
4540     case DW_OP_shl:
4541       return "DW_OP_shl";
4542     case DW_OP_shr:
4543       return "DW_OP_shr";
4544     case DW_OP_shra:
4545       return "DW_OP_shra";
4546     case DW_OP_xor:
4547       return "DW_OP_xor";
4548     case DW_OP_bra:
4549       return "DW_OP_bra";
4550     case DW_OP_eq:
4551       return "DW_OP_eq";
4552     case DW_OP_ge:
4553       return "DW_OP_ge";
4554     case DW_OP_gt:
4555       return "DW_OP_gt";
4556     case DW_OP_le:
4557       return "DW_OP_le";
4558     case DW_OP_lt:
4559       return "DW_OP_lt";
4560     case DW_OP_ne:
4561       return "DW_OP_ne";
4562     case DW_OP_skip:
4563       return "DW_OP_skip";
4564     case DW_OP_lit0:
4565       return "DW_OP_lit0";
4566     case DW_OP_lit1:
4567       return "DW_OP_lit1";
4568     case DW_OP_lit2:
4569       return "DW_OP_lit2";
4570     case DW_OP_lit3:
4571       return "DW_OP_lit3";
4572     case DW_OP_lit4:
4573       return "DW_OP_lit4";
4574     case DW_OP_lit5:
4575       return "DW_OP_lit5";
4576     case DW_OP_lit6:
4577       return "DW_OP_lit6";
4578     case DW_OP_lit7:
4579       return "DW_OP_lit7";
4580     case DW_OP_lit8:
4581       return "DW_OP_lit8";
4582     case DW_OP_lit9:
4583       return "DW_OP_lit9";
4584     case DW_OP_lit10:
4585       return "DW_OP_lit10";
4586     case DW_OP_lit11:
4587       return "DW_OP_lit11";
4588     case DW_OP_lit12:
4589       return "DW_OP_lit12";
4590     case DW_OP_lit13:
4591       return "DW_OP_lit13";
4592     case DW_OP_lit14:
4593       return "DW_OP_lit14";
4594     case DW_OP_lit15:
4595       return "DW_OP_lit15";
4596     case DW_OP_lit16:
4597       return "DW_OP_lit16";
4598     case DW_OP_lit17:
4599       return "DW_OP_lit17";
4600     case DW_OP_lit18:
4601       return "DW_OP_lit18";
4602     case DW_OP_lit19:
4603       return "DW_OP_lit19";
4604     case DW_OP_lit20:
4605       return "DW_OP_lit20";
4606     case DW_OP_lit21:
4607       return "DW_OP_lit21";
4608     case DW_OP_lit22:
4609       return "DW_OP_lit22";
4610     case DW_OP_lit23:
4611       return "DW_OP_lit23";
4612     case DW_OP_lit24:
4613       return "DW_OP_lit24";
4614     case DW_OP_lit25:
4615       return "DW_OP_lit25";
4616     case DW_OP_lit26:
4617       return "DW_OP_lit26";
4618     case DW_OP_lit27:
4619       return "DW_OP_lit27";
4620     case DW_OP_lit28:
4621       return "DW_OP_lit28";
4622     case DW_OP_lit29:
4623       return "DW_OP_lit29";
4624     case DW_OP_lit30:
4625       return "DW_OP_lit30";
4626     case DW_OP_lit31:
4627       return "DW_OP_lit31";
4628     case DW_OP_reg0:
4629       return "DW_OP_reg0";
4630     case DW_OP_reg1:
4631       return "DW_OP_reg1";
4632     case DW_OP_reg2:
4633       return "DW_OP_reg2";
4634     case DW_OP_reg3:
4635       return "DW_OP_reg3";
4636     case DW_OP_reg4:
4637       return "DW_OP_reg4";
4638     case DW_OP_reg5:
4639       return "DW_OP_reg5";
4640     case DW_OP_reg6:
4641       return "DW_OP_reg6";
4642     case DW_OP_reg7:
4643       return "DW_OP_reg7";
4644     case DW_OP_reg8:
4645       return "DW_OP_reg8";
4646     case DW_OP_reg9:
4647       return "DW_OP_reg9";
4648     case DW_OP_reg10:
4649       return "DW_OP_reg10";
4650     case DW_OP_reg11:
4651       return "DW_OP_reg11";
4652     case DW_OP_reg12:
4653       return "DW_OP_reg12";
4654     case DW_OP_reg13:
4655       return "DW_OP_reg13";
4656     case DW_OP_reg14:
4657       return "DW_OP_reg14";
4658     case DW_OP_reg15:
4659       return "DW_OP_reg15";
4660     case DW_OP_reg16:
4661       return "DW_OP_reg16";
4662     case DW_OP_reg17:
4663       return "DW_OP_reg17";
4664     case DW_OP_reg18:
4665       return "DW_OP_reg18";
4666     case DW_OP_reg19:
4667       return "DW_OP_reg19";
4668     case DW_OP_reg20:
4669       return "DW_OP_reg20";
4670     case DW_OP_reg21:
4671       return "DW_OP_reg21";
4672     case DW_OP_reg22:
4673       return "DW_OP_reg22";
4674     case DW_OP_reg23:
4675       return "DW_OP_reg23";
4676     case DW_OP_reg24:
4677       return "DW_OP_reg24";
4678     case DW_OP_reg25:
4679       return "DW_OP_reg25";
4680     case DW_OP_reg26:
4681       return "DW_OP_reg26";
4682     case DW_OP_reg27:
4683       return "DW_OP_reg27";
4684     case DW_OP_reg28:
4685       return "DW_OP_reg28";
4686     case DW_OP_reg29:
4687       return "DW_OP_reg29";
4688     case DW_OP_reg30:
4689       return "DW_OP_reg30";
4690     case DW_OP_reg31:
4691       return "DW_OP_reg31";
4692     case DW_OP_breg0:
4693       return "DW_OP_breg0";
4694     case DW_OP_breg1:
4695       return "DW_OP_breg1";
4696     case DW_OP_breg2:
4697       return "DW_OP_breg2";
4698     case DW_OP_breg3:
4699       return "DW_OP_breg3";
4700     case DW_OP_breg4:
4701       return "DW_OP_breg4";
4702     case DW_OP_breg5:
4703       return "DW_OP_breg5";
4704     case DW_OP_breg6:
4705       return "DW_OP_breg6";
4706     case DW_OP_breg7:
4707       return "DW_OP_breg7";
4708     case DW_OP_breg8:
4709       return "DW_OP_breg8";
4710     case DW_OP_breg9:
4711       return "DW_OP_breg9";
4712     case DW_OP_breg10:
4713       return "DW_OP_breg10";
4714     case DW_OP_breg11:
4715       return "DW_OP_breg11";
4716     case DW_OP_breg12:
4717       return "DW_OP_breg12";
4718     case DW_OP_breg13:
4719       return "DW_OP_breg13";
4720     case DW_OP_breg14:
4721       return "DW_OP_breg14";
4722     case DW_OP_breg15:
4723       return "DW_OP_breg15";
4724     case DW_OP_breg16:
4725       return "DW_OP_breg16";
4726     case DW_OP_breg17:
4727       return "DW_OP_breg17";
4728     case DW_OP_breg18:
4729       return "DW_OP_breg18";
4730     case DW_OP_breg19:
4731       return "DW_OP_breg19";
4732     case DW_OP_breg20:
4733       return "DW_OP_breg20";
4734     case DW_OP_breg21:
4735       return "DW_OP_breg21";
4736     case DW_OP_breg22:
4737       return "DW_OP_breg22";
4738     case DW_OP_breg23:
4739       return "DW_OP_breg23";
4740     case DW_OP_breg24:
4741       return "DW_OP_breg24";
4742     case DW_OP_breg25:
4743       return "DW_OP_breg25";
4744     case DW_OP_breg26:
4745       return "DW_OP_breg26";
4746     case DW_OP_breg27:
4747       return "DW_OP_breg27";
4748     case DW_OP_breg28:
4749       return "DW_OP_breg28";
4750     case DW_OP_breg29:
4751       return "DW_OP_breg29";
4752     case DW_OP_breg30:
4753       return "DW_OP_breg30";
4754     case DW_OP_breg31:
4755       return "DW_OP_breg31";
4756     case DW_OP_regx:
4757       return "DW_OP_regx";
4758     case DW_OP_fbreg:
4759       return "DW_OP_fbreg";
4760     case DW_OP_bregx:
4761       return "DW_OP_bregx";
4762     case DW_OP_piece:
4763       return "DW_OP_piece";
4764     case DW_OP_deref_size:
4765       return "DW_OP_deref_size";
4766     case DW_OP_xderef_size:
4767       return "DW_OP_xderef_size";
4768     case DW_OP_nop:
4769       return "DW_OP_nop";
4770
4771     case DW_OP_push_object_address:
4772       return "DW_OP_push_object_address";
4773     case DW_OP_call2:
4774       return "DW_OP_call2";
4775     case DW_OP_call4:
4776       return "DW_OP_call4";
4777     case DW_OP_call_ref:
4778       return "DW_OP_call_ref";
4779     case DW_OP_implicit_value:
4780       return "DW_OP_implicit_value";
4781     case DW_OP_stack_value:
4782       return "DW_OP_stack_value";
4783     case DW_OP_form_tls_address:
4784       return "DW_OP_form_tls_address";
4785     case DW_OP_call_frame_cfa:
4786       return "DW_OP_call_frame_cfa";
4787     case DW_OP_bit_piece:
4788       return "DW_OP_bit_piece";
4789
4790     case DW_OP_GNU_push_tls_address:
4791       return "DW_OP_GNU_push_tls_address";
4792     case DW_OP_GNU_uninit:
4793       return "DW_OP_GNU_uninit";
4794     case DW_OP_GNU_encoded_addr:
4795       return "DW_OP_GNU_encoded_addr";
4796     case DW_OP_GNU_implicit_pointer:
4797       return "DW_OP_GNU_implicit_pointer";
4798     case DW_OP_GNU_entry_value:
4799       return "DW_OP_GNU_entry_value";
4800     case DW_OP_GNU_const_type:
4801       return "DW_OP_GNU_const_type";
4802     case DW_OP_GNU_regval_type:
4803       return "DW_OP_GNU_regval_type";
4804     case DW_OP_GNU_deref_type:
4805       return "DW_OP_GNU_deref_type";
4806     case DW_OP_GNU_convert:
4807       return "DW_OP_GNU_convert";
4808     case DW_OP_GNU_reinterpret:
4809       return "DW_OP_GNU_reinterpret";
4810
4811     default:
4812       return "OP_<unknown>";
4813     }
4814 }
4815
4816 /* Return a pointer to a newly allocated location description.  Location
4817    descriptions are simple expression terms that can be strung
4818    together to form more complicated location (address) descriptions.  */
4819
4820 static inline dw_loc_descr_ref
4821 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4822                unsigned HOST_WIDE_INT oprnd2)
4823 {
4824   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4825
4826   descr->dw_loc_opc = op;
4827   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4828   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4829   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4830   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4831
4832   return descr;
4833 }
4834
4835 /* Return a pointer to a newly allocated location description for
4836    REG and OFFSET.  */
4837
4838 static inline dw_loc_descr_ref
4839 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4840 {
4841   if (reg <= 31)
4842     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4843                           offset, 0);
4844   else
4845     return new_loc_descr (DW_OP_bregx, reg, offset);
4846 }
4847
4848 /* Add a location description term to a location description expression.  */
4849
4850 static inline void
4851 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4852 {
4853   dw_loc_descr_ref *d;
4854
4855   /* Find the end of the chain.  */
4856   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4857     ;
4858
4859   *d = descr;
4860 }
4861
4862 /* Add a constant OFFSET to a location expression.  */
4863
4864 static void
4865 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4866 {
4867   dw_loc_descr_ref loc;
4868   HOST_WIDE_INT *p;
4869
4870   gcc_assert (*list_head != NULL);
4871
4872   if (!offset)
4873     return;
4874
4875   /* Find the end of the chain.  */
4876   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4877     ;
4878
4879   p = NULL;
4880   if (loc->dw_loc_opc == DW_OP_fbreg
4881       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4882     p = &loc->dw_loc_oprnd1.v.val_int;
4883   else if (loc->dw_loc_opc == DW_OP_bregx)
4884     p = &loc->dw_loc_oprnd2.v.val_int;
4885
4886   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4887      offset.  Don't optimize if an signed integer overflow would happen.  */
4888   if (p != NULL
4889       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4890           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4891     *p += offset;
4892
4893   else if (offset > 0)
4894     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4895
4896   else
4897     {
4898       loc->dw_loc_next = int_loc_descriptor (-offset);
4899       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4900     }
4901 }
4902
4903 /* Add a constant OFFSET to a location list.  */
4904
4905 static void
4906 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4907 {
4908   dw_loc_list_ref d;
4909   for (d = list_head; d != NULL; d = d->dw_loc_next)
4910     loc_descr_plus_const (&d->expr, offset);
4911 }
4912
4913 #define DWARF_REF_SIZE  \
4914   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4915
4916 static unsigned long size_of_locs (dw_loc_descr_ref);
4917 static unsigned long int get_base_type_offset (dw_die_ref);
4918
4919 /* Return the size of a location descriptor.  */
4920
4921 static unsigned long
4922 size_of_loc_descr (dw_loc_descr_ref loc)
4923 {
4924   unsigned long size = 1;
4925
4926   switch (loc->dw_loc_opc)
4927     {
4928     case DW_OP_addr:
4929       size += DWARF2_ADDR_SIZE;
4930       break;
4931     case DW_OP_const1u:
4932     case DW_OP_const1s:
4933       size += 1;
4934       break;
4935     case DW_OP_const2u:
4936     case DW_OP_const2s:
4937       size += 2;
4938       break;
4939     case DW_OP_const4u:
4940     case DW_OP_const4s:
4941       size += 4;
4942       break;
4943     case DW_OP_const8u:
4944     case DW_OP_const8s:
4945       size += 8;
4946       break;
4947     case DW_OP_constu:
4948       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4949       break;
4950     case DW_OP_consts:
4951       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4952       break;
4953     case DW_OP_pick:
4954       size += 1;
4955       break;
4956     case DW_OP_plus_uconst:
4957       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4958       break;
4959     case DW_OP_skip:
4960     case DW_OP_bra:
4961       size += 2;
4962       break;
4963     case DW_OP_breg0:
4964     case DW_OP_breg1:
4965     case DW_OP_breg2:
4966     case DW_OP_breg3:
4967     case DW_OP_breg4:
4968     case DW_OP_breg5:
4969     case DW_OP_breg6:
4970     case DW_OP_breg7:
4971     case DW_OP_breg8:
4972     case DW_OP_breg9:
4973     case DW_OP_breg10:
4974     case DW_OP_breg11:
4975     case DW_OP_breg12:
4976     case DW_OP_breg13:
4977     case DW_OP_breg14:
4978     case DW_OP_breg15:
4979     case DW_OP_breg16:
4980     case DW_OP_breg17:
4981     case DW_OP_breg18:
4982     case DW_OP_breg19:
4983     case DW_OP_breg20:
4984     case DW_OP_breg21:
4985     case DW_OP_breg22:
4986     case DW_OP_breg23:
4987     case DW_OP_breg24:
4988     case DW_OP_breg25:
4989     case DW_OP_breg26:
4990     case DW_OP_breg27:
4991     case DW_OP_breg28:
4992     case DW_OP_breg29:
4993     case DW_OP_breg30:
4994     case DW_OP_breg31:
4995       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4996       break;
4997     case DW_OP_regx:
4998       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4999       break;
5000     case DW_OP_fbreg:
5001       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
5002       break;
5003     case DW_OP_bregx:
5004       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5005       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5006       break;
5007     case DW_OP_piece:
5008       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5009       break;
5010     case DW_OP_bit_piece:
5011       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5012       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
5013       break;
5014     case DW_OP_deref_size:
5015     case DW_OP_xderef_size:
5016       size += 1;
5017       break;
5018     case DW_OP_call2:
5019       size += 2;
5020       break;
5021     case DW_OP_call4:
5022       size += 4;
5023       break;
5024     case DW_OP_call_ref:
5025       size += DWARF_REF_SIZE;
5026       break;
5027     case DW_OP_implicit_value:
5028       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5029               + loc->dw_loc_oprnd1.v.val_unsigned;
5030       break;
5031     case DW_OP_GNU_implicit_pointer:
5032       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5033       break;
5034     case DW_OP_GNU_entry_value:
5035       {
5036         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5037         size += size_of_uleb128 (op_size) + op_size;
5038         break;
5039       }
5040     case DW_OP_GNU_const_type:
5041       {
5042         unsigned long o
5043           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
5044         size += size_of_uleb128 (o) + 1;
5045         switch (loc->dw_loc_oprnd2.val_class)
5046           {
5047           case dw_val_class_vec:
5048             size += loc->dw_loc_oprnd2.v.val_vec.length
5049                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
5050             break;
5051           case dw_val_class_const:
5052             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
5053             break;
5054           case dw_val_class_const_double:
5055             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
5056             break;
5057           default:
5058             gcc_unreachable ();
5059           }
5060         break;
5061       }
5062     case DW_OP_GNU_regval_type:
5063       {
5064         unsigned long o
5065           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
5066         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5067                 + size_of_uleb128 (o);
5068       }
5069       break;
5070     case DW_OP_GNU_deref_type:
5071       {
5072         unsigned long o
5073           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
5074         size += 1 + size_of_uleb128 (o);
5075       }
5076       break;
5077     case DW_OP_GNU_convert:
5078     case DW_OP_GNU_reinterpret:
5079       {
5080         unsigned long o
5081           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
5082         size += size_of_uleb128 (o);
5083       }
5084     default:
5085       break;
5086     }
5087
5088   return size;
5089 }
5090
5091 /* Return the size of a series of location descriptors.  */
5092
5093 static unsigned long
5094 size_of_locs (dw_loc_descr_ref loc)
5095 {
5096   dw_loc_descr_ref l;
5097   unsigned long size;
5098
5099   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5100      field, to avoid writing to a PCH file.  */
5101   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5102     {
5103       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5104         break;
5105       size += size_of_loc_descr (l);
5106     }
5107   if (! l)
5108     return size;
5109
5110   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5111     {
5112       l->dw_loc_addr = size;
5113       size += size_of_loc_descr (l);
5114     }
5115
5116   return size;
5117 }
5118
5119 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5120 static void get_ref_die_offset_label (char *, dw_die_ref);
5121 static void output_loc_sequence (dw_loc_descr_ref, int);
5122
5123 /* Output location description stack opcode's operands (if any).
5124    The for_eh_or_skip parameter controls whether register numbers are
5125    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5126    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5127    info).  This should be suppressed for the cases that have not been converted
5128    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5129
5130 static void
5131 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5132 {
5133   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5134   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5135
5136   switch (loc->dw_loc_opc)
5137     {
5138 #ifdef DWARF2_DEBUGGING_INFO
5139     case DW_OP_const2u:
5140     case DW_OP_const2s:
5141       dw2_asm_output_data (2, val1->v.val_int, NULL);
5142       break;
5143     case DW_OP_const4u:
5144       if (loc->dtprel)
5145         {
5146           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5147           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5148                                                val1->v.val_addr);
5149           fputc ('\n', asm_out_file);
5150           break;
5151         }
5152       /* FALLTHRU */
5153     case DW_OP_const4s:
5154       dw2_asm_output_data (4, val1->v.val_int, NULL);
5155       break;
5156     case DW_OP_const8u:
5157       if (loc->dtprel)
5158         {
5159           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5160           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5161                                                val1->v.val_addr);
5162           fputc ('\n', asm_out_file);
5163           break;
5164         }
5165       /* FALLTHRU */
5166     case DW_OP_const8s:
5167       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5168       dw2_asm_output_data (8, val1->v.val_int, NULL);
5169       break;
5170     case DW_OP_skip:
5171     case DW_OP_bra:
5172       {
5173         int offset;
5174
5175         gcc_assert (val1->val_class == dw_val_class_loc);
5176         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5177
5178         dw2_asm_output_data (2, offset, NULL);
5179       }
5180       break;
5181     case DW_OP_implicit_value:
5182       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5183       switch (val2->val_class)
5184         {
5185         case dw_val_class_const:
5186           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5187           break;
5188         case dw_val_class_vec:
5189           {
5190             unsigned int elt_size = val2->v.val_vec.elt_size;
5191             unsigned int len = val2->v.val_vec.length;
5192             unsigned int i;
5193             unsigned char *p;
5194
5195             if (elt_size > sizeof (HOST_WIDE_INT))
5196               {
5197                 elt_size /= 2;
5198                 len *= 2;
5199               }
5200             for (i = 0, p = val2->v.val_vec.array;
5201                  i < len;
5202                  i++, p += elt_size)
5203               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5204                                    "fp or vector constant word %u", i);
5205           }
5206           break;
5207         case dw_val_class_const_double:
5208           {
5209             unsigned HOST_WIDE_INT first, second;
5210
5211             if (WORDS_BIG_ENDIAN)
5212               {
5213                 first = val2->v.val_double.high;
5214                 second = val2->v.val_double.low;
5215               }
5216             else
5217               {
5218                 first = val2->v.val_double.low;
5219                 second = val2->v.val_double.high;
5220               }
5221             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5222                                  first, NULL);
5223             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5224                                  second, NULL);
5225           }
5226           break;
5227         case dw_val_class_addr:
5228           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5229           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5230           break;
5231         default:
5232           gcc_unreachable ();
5233         }
5234       break;
5235 #else
5236     case DW_OP_const2u:
5237     case DW_OP_const2s:
5238     case DW_OP_const4u:
5239     case DW_OP_const4s:
5240     case DW_OP_const8u:
5241     case DW_OP_const8s:
5242     case DW_OP_skip:
5243     case DW_OP_bra:
5244     case DW_OP_implicit_value:
5245       /* We currently don't make any attempt to make sure these are
5246          aligned properly like we do for the main unwind info, so
5247          don't support emitting things larger than a byte if we're
5248          only doing unwinding.  */
5249       gcc_unreachable ();
5250 #endif
5251     case DW_OP_const1u:
5252     case DW_OP_const1s:
5253       dw2_asm_output_data (1, val1->v.val_int, NULL);
5254       break;
5255     case DW_OP_constu:
5256       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5257       break;
5258     case DW_OP_consts:
5259       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5260       break;
5261     case DW_OP_pick:
5262       dw2_asm_output_data (1, val1->v.val_int, NULL);
5263       break;
5264     case DW_OP_plus_uconst:
5265       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5266       break;
5267     case DW_OP_breg0:
5268     case DW_OP_breg1:
5269     case DW_OP_breg2:
5270     case DW_OP_breg3:
5271     case DW_OP_breg4:
5272     case DW_OP_breg5:
5273     case DW_OP_breg6:
5274     case DW_OP_breg7:
5275     case DW_OP_breg8:
5276     case DW_OP_breg9:
5277     case DW_OP_breg10:
5278     case DW_OP_breg11:
5279     case DW_OP_breg12:
5280     case DW_OP_breg13:
5281     case DW_OP_breg14:
5282     case DW_OP_breg15:
5283     case DW_OP_breg16:
5284     case DW_OP_breg17:
5285     case DW_OP_breg18:
5286     case DW_OP_breg19:
5287     case DW_OP_breg20:
5288     case DW_OP_breg21:
5289     case DW_OP_breg22:
5290     case DW_OP_breg23:
5291     case DW_OP_breg24:
5292     case DW_OP_breg25:
5293     case DW_OP_breg26:
5294     case DW_OP_breg27:
5295     case DW_OP_breg28:
5296     case DW_OP_breg29:
5297     case DW_OP_breg30:
5298     case DW_OP_breg31:
5299       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5300       break;
5301     case DW_OP_regx:
5302       {
5303         unsigned r = val1->v.val_unsigned;
5304         if (for_eh_or_skip >= 0)
5305           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5306         gcc_assert (size_of_uleb128 (r) 
5307                     == size_of_uleb128 (val1->v.val_unsigned));
5308         dw2_asm_output_data_uleb128 (r, NULL);  
5309       }
5310       break;
5311     case DW_OP_fbreg:
5312       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5313       break;
5314     case DW_OP_bregx:
5315       {
5316         unsigned r = val1->v.val_unsigned;
5317         if (for_eh_or_skip >= 0)
5318           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5319         gcc_assert (size_of_uleb128 (r) 
5320                     == size_of_uleb128 (val1->v.val_unsigned));
5321         dw2_asm_output_data_uleb128 (r, NULL);  
5322         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5323       }
5324       break;
5325     case DW_OP_piece:
5326       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5327       break;
5328     case DW_OP_bit_piece:
5329       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5330       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5331       break;
5332     case DW_OP_deref_size:
5333     case DW_OP_xderef_size:
5334       dw2_asm_output_data (1, val1->v.val_int, NULL);
5335       break;
5336
5337     case DW_OP_addr:
5338       if (loc->dtprel)
5339         {
5340           if (targetm.asm_out.output_dwarf_dtprel)
5341             {
5342               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5343                                                    DWARF2_ADDR_SIZE,
5344                                                    val1->v.val_addr);
5345               fputc ('\n', asm_out_file);
5346             }
5347           else
5348             gcc_unreachable ();
5349         }
5350       else
5351         {
5352 #ifdef DWARF2_DEBUGGING_INFO
5353           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5354 #else
5355           gcc_unreachable ();
5356 #endif
5357         }
5358       break;
5359
5360     case DW_OP_GNU_implicit_pointer:
5361       {
5362         char label[MAX_ARTIFICIAL_LABEL_BYTES
5363                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5364         gcc_assert (val1->val_class == dw_val_class_die_ref);
5365         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5366         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5367         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5368       }
5369       break;
5370
5371     case DW_OP_GNU_entry_value:
5372       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5373       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5374       break;
5375
5376     case DW_OP_GNU_const_type:
5377       {
5378         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
5379         gcc_assert (o);
5380         dw2_asm_output_data_uleb128 (o, NULL);
5381         switch (val2->val_class)
5382           {
5383           case dw_val_class_const:
5384             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
5385             dw2_asm_output_data (1, l, NULL);
5386             dw2_asm_output_data (l, val2->v.val_int, NULL);
5387             break;
5388           case dw_val_class_vec:
5389             {
5390               unsigned int elt_size = val2->v.val_vec.elt_size;
5391               unsigned int len = val2->v.val_vec.length;
5392               unsigned int i;
5393               unsigned char *p;
5394
5395               l = len * elt_size;
5396               dw2_asm_output_data (1, l, NULL);
5397               if (elt_size > sizeof (HOST_WIDE_INT))
5398                 {
5399                   elt_size /= 2;
5400                   len *= 2;
5401                 }
5402               for (i = 0, p = val2->v.val_vec.array;
5403                    i < len;
5404                    i++, p += elt_size)
5405                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5406                                      "fp or vector constant word %u", i);
5407             }
5408             break;
5409           case dw_val_class_const_double:
5410             {
5411               unsigned HOST_WIDE_INT first, second;
5412               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
5413
5414               dw2_asm_output_data (1, 2 * l, NULL);
5415               if (WORDS_BIG_ENDIAN)
5416                 {
5417                   first = val2->v.val_double.high;
5418                   second = val2->v.val_double.low;
5419                 }
5420               else
5421                 {
5422                   first = val2->v.val_double.low;
5423                   second = val2->v.val_double.high;
5424                 }
5425               dw2_asm_output_data (l, first, NULL);
5426               dw2_asm_output_data (l, second, NULL);
5427             }
5428             break;
5429           default:
5430             gcc_unreachable ();
5431           }
5432       }
5433       break;
5434     case DW_OP_GNU_regval_type:
5435       {
5436         unsigned r = val1->v.val_unsigned;
5437         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
5438         gcc_assert (o);
5439         if (for_eh_or_skip >= 0)
5440           {
5441             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5442             gcc_assert (size_of_uleb128 (r)
5443                         == size_of_uleb128 (val1->v.val_unsigned));
5444           }
5445         dw2_asm_output_data_uleb128 (r, NULL);
5446         dw2_asm_output_data_uleb128 (o, NULL);
5447       }
5448       break;
5449     case DW_OP_GNU_deref_type:
5450       {
5451         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
5452         gcc_assert (o);
5453         dw2_asm_output_data (1, val1->v.val_int, NULL);
5454         dw2_asm_output_data_uleb128 (o, NULL);
5455       }
5456       break;
5457     case DW_OP_GNU_convert:
5458     case DW_OP_GNU_reinterpret:
5459       {
5460         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
5461         gcc_assert (o);
5462         dw2_asm_output_data_uleb128 (o, NULL);
5463       }
5464       break;
5465
5466     default:
5467       /* Other codes have no operands.  */
5468       break;
5469     }
5470 }
5471
5472 /* Output a sequence of location operations.  
5473    The for_eh_or_skip parameter controls whether register numbers are
5474    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5475    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5476    info).  This should be suppressed for the cases that have not been converted
5477    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5478
5479 static void
5480 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5481 {
5482   for (; loc != NULL; loc = loc->dw_loc_next)
5483     {
5484       enum dwarf_location_atom opc = loc->dw_loc_opc;
5485       /* Output the opcode.  */
5486       if (for_eh_or_skip >= 0 
5487           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5488         {
5489           unsigned r = (opc - DW_OP_breg0);
5490           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5491           gcc_assert (r <= 31);
5492           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5493         }
5494       else if (for_eh_or_skip >= 0 
5495                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5496         {
5497           unsigned r = (opc - DW_OP_reg0);
5498           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5499           gcc_assert (r <= 31);
5500           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5501         }
5502
5503       dw2_asm_output_data (1, opc,
5504                              "%s", dwarf_stack_op_name (opc));
5505
5506       /* Output the operand(s) (if any).  */
5507       output_loc_operands (loc, for_eh_or_skip);
5508     }
5509 }
5510
5511 /* Output location description stack opcode's operands (if any).
5512    The output is single bytes on a line, suitable for .cfi_escape.  */
5513
5514 static void
5515 output_loc_operands_raw (dw_loc_descr_ref loc)
5516 {
5517   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5518   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5519
5520   switch (loc->dw_loc_opc)
5521     {
5522     case DW_OP_addr:
5523     case DW_OP_implicit_value:
5524       /* We cannot output addresses in .cfi_escape, only bytes.  */
5525       gcc_unreachable ();
5526
5527     case DW_OP_const1u:
5528     case DW_OP_const1s:
5529     case DW_OP_pick:
5530     case DW_OP_deref_size:
5531     case DW_OP_xderef_size:
5532       fputc (',', asm_out_file);
5533       dw2_asm_output_data_raw (1, val1->v.val_int);
5534       break;
5535
5536     case DW_OP_const2u:
5537     case DW_OP_const2s:
5538       fputc (',', asm_out_file);
5539       dw2_asm_output_data_raw (2, val1->v.val_int);
5540       break;
5541
5542     case DW_OP_const4u:
5543     case DW_OP_const4s:
5544       fputc (',', asm_out_file);
5545       dw2_asm_output_data_raw (4, val1->v.val_int);
5546       break;
5547
5548     case DW_OP_const8u:
5549     case DW_OP_const8s:
5550       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5551       fputc (',', asm_out_file);
5552       dw2_asm_output_data_raw (8, val1->v.val_int);
5553       break;
5554
5555     case DW_OP_skip:
5556     case DW_OP_bra:
5557       {
5558         int offset;
5559
5560         gcc_assert (val1->val_class == dw_val_class_loc);
5561         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5562
5563         fputc (',', asm_out_file);
5564         dw2_asm_output_data_raw (2, offset);
5565       }
5566       break;
5567
5568     case DW_OP_regx:
5569       {
5570         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5571         gcc_assert (size_of_uleb128 (r) 
5572                     == size_of_uleb128 (val1->v.val_unsigned));
5573         fputc (',', asm_out_file);
5574         dw2_asm_output_data_uleb128_raw (r);
5575       }
5576       break;
5577       
5578     case DW_OP_constu:
5579     case DW_OP_plus_uconst:
5580     case DW_OP_piece:
5581       fputc (',', asm_out_file);
5582       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5583       break;
5584
5585     case DW_OP_bit_piece:
5586       fputc (',', asm_out_file);
5587       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5588       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5589       break;
5590
5591     case DW_OP_consts:
5592     case DW_OP_breg0:
5593     case DW_OP_breg1:
5594     case DW_OP_breg2:
5595     case DW_OP_breg3:
5596     case DW_OP_breg4:
5597     case DW_OP_breg5:
5598     case DW_OP_breg6:
5599     case DW_OP_breg7:
5600     case DW_OP_breg8:
5601     case DW_OP_breg9:
5602     case DW_OP_breg10:
5603     case DW_OP_breg11:
5604     case DW_OP_breg12:
5605     case DW_OP_breg13:
5606     case DW_OP_breg14:
5607     case DW_OP_breg15:
5608     case DW_OP_breg16:
5609     case DW_OP_breg17:
5610     case DW_OP_breg18:
5611     case DW_OP_breg19:
5612     case DW_OP_breg20:
5613     case DW_OP_breg21:
5614     case DW_OP_breg22:
5615     case DW_OP_breg23:
5616     case DW_OP_breg24:
5617     case DW_OP_breg25:
5618     case DW_OP_breg26:
5619     case DW_OP_breg27:
5620     case DW_OP_breg28:
5621     case DW_OP_breg29:
5622     case DW_OP_breg30:
5623     case DW_OP_breg31:
5624     case DW_OP_fbreg:
5625       fputc (',', asm_out_file);
5626       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5627       break;
5628
5629     case DW_OP_bregx:
5630       {
5631         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5632         gcc_assert (size_of_uleb128 (r) 
5633                     == size_of_uleb128 (val1->v.val_unsigned));
5634         fputc (',', asm_out_file);
5635         dw2_asm_output_data_uleb128_raw (r);
5636         fputc (',', asm_out_file);
5637         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5638       }
5639       break;
5640
5641     case DW_OP_GNU_implicit_pointer:
5642     case DW_OP_GNU_entry_value:
5643     case DW_OP_GNU_const_type:
5644     case DW_OP_GNU_regval_type:
5645     case DW_OP_GNU_deref_type:
5646     case DW_OP_GNU_convert:
5647     case DW_OP_GNU_reinterpret:
5648       gcc_unreachable ();
5649       break;
5650
5651     default:
5652       /* Other codes have no operands.  */
5653       break;
5654     }
5655 }
5656
5657 static void
5658 output_loc_sequence_raw (dw_loc_descr_ref loc)
5659 {
5660   while (1)
5661     {
5662       enum dwarf_location_atom opc = loc->dw_loc_opc;
5663       /* Output the opcode.  */
5664       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5665         {
5666           unsigned r = (opc - DW_OP_breg0);
5667           r = DWARF2_FRAME_REG_OUT (r, 1);
5668           gcc_assert (r <= 31);
5669           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5670         }
5671       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5672         {
5673           unsigned r = (opc - DW_OP_reg0);
5674           r = DWARF2_FRAME_REG_OUT (r, 1);
5675           gcc_assert (r <= 31);
5676           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5677         }
5678       /* Output the opcode.  */
5679       fprintf (asm_out_file, "%#x", opc);
5680       output_loc_operands_raw (loc);
5681
5682       if (!loc->dw_loc_next)
5683         break;
5684       loc = loc->dw_loc_next;
5685
5686       fputc (',', asm_out_file);
5687     }
5688 }
5689
5690 /* This routine will generate the correct assembly data for a location
5691    description based on a cfi entry with a complex address.  */
5692
5693 static void
5694 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5695 {
5696   dw_loc_descr_ref loc;
5697   unsigned long size;
5698
5699   if (cfi->dw_cfi_opc == DW_CFA_expression)
5700     {
5701       unsigned r = 
5702         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5703       dw2_asm_output_data (1, r, NULL);
5704       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5705     }
5706   else
5707     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5708
5709   /* Output the size of the block.  */
5710   size = size_of_locs (loc);
5711   dw2_asm_output_data_uleb128 (size, NULL);
5712
5713   /* Now output the operations themselves.  */
5714   output_loc_sequence (loc, for_eh);
5715 }
5716
5717 /* Similar, but used for .cfi_escape.  */
5718
5719 static void
5720 output_cfa_loc_raw (dw_cfi_ref cfi)
5721 {
5722   dw_loc_descr_ref loc;
5723   unsigned long size;
5724
5725   if (cfi->dw_cfi_opc == DW_CFA_expression)
5726     {
5727       unsigned r = 
5728         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5729       fprintf (asm_out_file, "%#x,", r);
5730       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5731     }
5732   else
5733     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5734
5735   /* Output the size of the block.  */
5736   size = size_of_locs (loc);
5737   dw2_asm_output_data_uleb128_raw (size);
5738   fputc (',', asm_out_file);
5739
5740   /* Now output the operations themselves.  */
5741   output_loc_sequence_raw (loc);
5742 }
5743
5744 /* This function builds a dwarf location descriptor sequence from a
5745    dw_cfa_location, adding the given OFFSET to the result of the
5746    expression.  */
5747
5748 static struct dw_loc_descr_struct *
5749 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5750 {
5751   struct dw_loc_descr_struct *head, *tmp;
5752
5753   offset += cfa->offset;
5754
5755   if (cfa->indirect)
5756     {
5757       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5758       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5759       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5760       add_loc_descr (&head, tmp);
5761       if (offset != 0)
5762         {
5763           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5764           add_loc_descr (&head, tmp);
5765         }
5766     }
5767   else
5768     head = new_reg_loc_descr (cfa->reg, offset);
5769
5770   return head;
5771 }
5772
5773 /* This function builds a dwarf location descriptor sequence for
5774    the address at OFFSET from the CFA when stack is aligned to
5775    ALIGNMENT byte.  */
5776
5777 static struct dw_loc_descr_struct *
5778 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5779 {
5780   struct dw_loc_descr_struct *head;
5781   unsigned int dwarf_fp
5782     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5783
5784  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5785   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5786     {
5787       head = new_reg_loc_descr (dwarf_fp, 0);
5788       add_loc_descr (&head, int_loc_descriptor (alignment));
5789       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5790       loc_descr_plus_const (&head, offset);
5791     }
5792   else
5793     head = new_reg_loc_descr (dwarf_fp, offset);
5794   return head;
5795 }
5796
5797 /* This function fills in aa dw_cfa_location structure from a dwarf location
5798    descriptor sequence.  */
5799
5800 static void
5801 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5802 {
5803   struct dw_loc_descr_struct *ptr;
5804   cfa->offset = 0;
5805   cfa->base_offset = 0;
5806   cfa->indirect = 0;
5807   cfa->reg = -1;
5808
5809   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5810     {
5811       enum dwarf_location_atom op = ptr->dw_loc_opc;
5812
5813       switch (op)
5814         {
5815         case DW_OP_reg0:
5816         case DW_OP_reg1:
5817         case DW_OP_reg2:
5818         case DW_OP_reg3:
5819         case DW_OP_reg4:
5820         case DW_OP_reg5:
5821         case DW_OP_reg6:
5822         case DW_OP_reg7:
5823         case DW_OP_reg8:
5824         case DW_OP_reg9:
5825         case DW_OP_reg10:
5826         case DW_OP_reg11:
5827         case DW_OP_reg12:
5828         case DW_OP_reg13:
5829         case DW_OP_reg14:
5830         case DW_OP_reg15:
5831         case DW_OP_reg16:
5832         case DW_OP_reg17:
5833         case DW_OP_reg18:
5834         case DW_OP_reg19:
5835         case DW_OP_reg20:
5836         case DW_OP_reg21:
5837         case DW_OP_reg22:
5838         case DW_OP_reg23:
5839         case DW_OP_reg24:
5840         case DW_OP_reg25:
5841         case DW_OP_reg26:
5842         case DW_OP_reg27:
5843         case DW_OP_reg28:
5844         case DW_OP_reg29:
5845         case DW_OP_reg30:
5846         case DW_OP_reg31:
5847           cfa->reg = op - DW_OP_reg0;
5848           break;
5849         case DW_OP_regx:
5850           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5851           break;
5852         case DW_OP_breg0:
5853         case DW_OP_breg1:
5854         case DW_OP_breg2:
5855         case DW_OP_breg3:
5856         case DW_OP_breg4:
5857         case DW_OP_breg5:
5858         case DW_OP_breg6:
5859         case DW_OP_breg7:
5860         case DW_OP_breg8:
5861         case DW_OP_breg9:
5862         case DW_OP_breg10:
5863         case DW_OP_breg11:
5864         case DW_OP_breg12:
5865         case DW_OP_breg13:
5866         case DW_OP_breg14:
5867         case DW_OP_breg15:
5868         case DW_OP_breg16:
5869         case DW_OP_breg17:
5870         case DW_OP_breg18:
5871         case DW_OP_breg19:
5872         case DW_OP_breg20:
5873         case DW_OP_breg21:
5874         case DW_OP_breg22:
5875         case DW_OP_breg23:
5876         case DW_OP_breg24:
5877         case DW_OP_breg25:
5878         case DW_OP_breg26:
5879         case DW_OP_breg27:
5880         case DW_OP_breg28:
5881         case DW_OP_breg29:
5882         case DW_OP_breg30:
5883         case DW_OP_breg31:
5884           cfa->reg = op - DW_OP_breg0;
5885           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5886           break;
5887         case DW_OP_bregx:
5888           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5889           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5890           break;
5891         case DW_OP_deref:
5892           cfa->indirect = 1;
5893           break;
5894         case DW_OP_plus_uconst:
5895           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5896           break;
5897         default:
5898           internal_error ("DW_LOC_OP %s not implemented",
5899                           dwarf_stack_op_name (ptr->dw_loc_opc));
5900         }
5901     }
5902 }
5903 \f
5904 /* And now, the support for symbolic debugging information.  */
5905
5906 /* .debug_str support.  */
5907 static int output_indirect_string (void **, void *);
5908
5909 static void dwarf2out_init (const char *);
5910 static void dwarf2out_finish (const char *);
5911 static void dwarf2out_assembly_start (void);
5912 static void dwarf2out_define (unsigned int, const char *);
5913 static void dwarf2out_undef (unsigned int, const char *);
5914 static void dwarf2out_start_source_file (unsigned, const char *);
5915 static void dwarf2out_end_source_file (unsigned);
5916 static void dwarf2out_function_decl (tree);
5917 static void dwarf2out_begin_block (unsigned, unsigned);
5918 static void dwarf2out_end_block (unsigned, unsigned);
5919 static bool dwarf2out_ignore_block (const_tree);
5920 static void dwarf2out_global_decl (tree);
5921 static void dwarf2out_type_decl (tree, int);
5922 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5923 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5924                                                  dw_die_ref);
5925 static void dwarf2out_abstract_function (tree);
5926 static void dwarf2out_var_location (rtx);
5927 static void dwarf2out_begin_function (tree);
5928 static void dwarf2out_set_name (tree, tree);
5929
5930 /* The debug hooks structure.  */
5931
5932 const struct gcc_debug_hooks dwarf2_debug_hooks =
5933 {
5934   dwarf2out_init,
5935   dwarf2out_finish,
5936   dwarf2out_assembly_start,
5937   dwarf2out_define,
5938   dwarf2out_undef,
5939   dwarf2out_start_source_file,
5940   dwarf2out_end_source_file,
5941   dwarf2out_begin_block,
5942   dwarf2out_end_block,
5943   dwarf2out_ignore_block,
5944   dwarf2out_source_line,
5945   dwarf2out_begin_prologue,
5946 #if VMS_DEBUGGING_INFO
5947   dwarf2out_vms_end_prologue,
5948   dwarf2out_vms_begin_epilogue,
5949 #else
5950   debug_nothing_int_charstar,
5951   debug_nothing_int_charstar,
5952 #endif
5953   dwarf2out_end_epilogue,
5954   dwarf2out_begin_function,
5955   debug_nothing_int,            /* end_function */
5956   dwarf2out_function_decl,      /* function_decl */
5957   dwarf2out_global_decl,
5958   dwarf2out_type_decl,          /* type_decl */
5959   dwarf2out_imported_module_or_decl,
5960   debug_nothing_tree,           /* deferred_inline_function */
5961   /* The DWARF 2 backend tries to reduce debugging bloat by not
5962      emitting the abstract description of inline functions until
5963      something tries to reference them.  */
5964   dwarf2out_abstract_function,  /* outlining_inline_function */
5965   debug_nothing_rtx,            /* label */
5966   debug_nothing_int,            /* handle_pch */
5967   dwarf2out_var_location,
5968   dwarf2out_switch_text_section,
5969   dwarf2out_set_name,
5970   1,                            /* start_end_main_source_file */
5971   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5972 };
5973 \f
5974 /* NOTE: In the comments in this file, many references are made to
5975    "Debugging Information Entries".  This term is abbreviated as `DIE'
5976    throughout the remainder of this file.  */
5977
5978 /* An internal representation of the DWARF output is built, and then
5979    walked to generate the DWARF debugging info.  The walk of the internal
5980    representation is done after the entire program has been compiled.
5981    The types below are used to describe the internal representation.  */
5982
5983 /* Whether to put type DIEs into their own section .debug_types instead
5984    of making them part of the .debug_info section.  Only supported for
5985    Dwarf V4 or higher and the user didn't disable them through
5986    -fno-debug-types-section.  It is more efficient to put them in a
5987    separate comdat sections since the linker will then be able to
5988    remove duplicates.  But not all tools support .debug_types sections
5989    yet.  */
5990
5991 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
5992
5993 /* Various DIE's use offsets relative to the beginning of the
5994    .debug_info section to refer to each other.  */
5995
5996 typedef long int dw_offset;
5997
5998 /* Define typedefs here to avoid circular dependencies.  */
5999
6000 typedef struct dw_attr_struct *dw_attr_ref;
6001 typedef struct dw_line_info_struct *dw_line_info_ref;
6002 typedef struct pubname_struct *pubname_ref;
6003 typedef struct dw_ranges_struct *dw_ranges_ref;
6004 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
6005 typedef struct comdat_type_struct *comdat_type_node_ref;
6006
6007 /* The entries in the line_info table more-or-less mirror the opcodes
6008    that are used in the real dwarf line table.  Arrays of these entries
6009    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
6010    supported.  */
6011
6012 enum dw_line_info_opcode {
6013   /* Emit DW_LNE_set_address; the operand is the label index.  */
6014   LI_set_address,
6015
6016   /* Emit a row to the matrix with the given line.  This may be done
6017      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
6018      special opcodes.  */
6019   LI_set_line,
6020
6021   /* Emit a DW_LNS_set_file.  */
6022   LI_set_file,
6023
6024   /* Emit a DW_LNS_set_column.  */
6025   LI_set_column,
6026
6027   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
6028   LI_negate_stmt,
6029
6030   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
6031   LI_set_prologue_end,
6032   LI_set_epilogue_begin,
6033
6034   /* Emit a DW_LNE_set_discriminator.  */
6035   LI_set_discriminator
6036 };
6037
6038 typedef struct GTY(()) dw_line_info_struct {
6039   enum dw_line_info_opcode opcode;
6040   unsigned int val;
6041 } dw_line_info_entry;
6042
6043 DEF_VEC_O(dw_line_info_entry);
6044 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
6045
6046 typedef struct GTY(()) dw_line_info_table_struct {
6047   /* The label that marks the end of this section.  */
6048   const char *end_label;
6049
6050   /* The values for the last row of the matrix, as collected in the table.
6051      These are used to minimize the changes to the next row.  */
6052   unsigned int file_num;
6053   unsigned int line_num;
6054   unsigned int column_num;
6055   int discrim_num;
6056   bool is_stmt;
6057   bool in_use;
6058
6059   VEC(dw_line_info_entry, gc) *entries;
6060 } dw_line_info_table;
6061
6062 typedef dw_line_info_table *dw_line_info_table_p;
6063
6064 DEF_VEC_P(dw_line_info_table_p);
6065 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
6066
6067 /* Each DIE attribute has a field specifying the attribute kind,
6068    a link to the next attribute in the chain, and an attribute value.
6069    Attributes are typically linked below the DIE they modify.  */
6070
6071 typedef struct GTY(()) dw_attr_struct {
6072   enum dwarf_attribute dw_attr;
6073   dw_val_node dw_attr_val;
6074 }
6075 dw_attr_node;
6076
6077 DEF_VEC_O(dw_attr_node);
6078 DEF_VEC_ALLOC_O(dw_attr_node,gc);
6079
6080 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
6081    The children of each node form a circular list linked by
6082    die_sib.  die_child points to the node *before* the "first" child node.  */
6083
6084 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
6085   union die_symbol_or_type_node
6086     {
6087       char * GTY ((tag ("0"))) die_symbol;
6088       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
6089     }
6090   GTY ((desc ("use_debug_types"))) die_id;
6091   VEC(dw_attr_node,gc) * die_attr;
6092   dw_die_ref die_parent;
6093   dw_die_ref die_child;
6094   dw_die_ref die_sib;
6095   dw_die_ref die_definition; /* ref from a specification to its definition */
6096   dw_offset die_offset;
6097   unsigned long die_abbrev;
6098   int die_mark;
6099   /* Die is used and must not be pruned as unused.  */
6100   int die_perennial_p;
6101   unsigned int decl_id;
6102   enum dwarf_tag die_tag;
6103 }
6104 die_node;
6105
6106 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
6107 #define FOR_EACH_CHILD(die, c, expr) do {       \
6108   c = die->die_child;                           \
6109   if (c) do {                                   \
6110     c = c->die_sib;                             \
6111     expr;                                       \
6112   } while (c != die->die_child);                \
6113 } while (0)
6114
6115 /* The pubname structure */
6116
6117 typedef struct GTY(()) pubname_struct {
6118   dw_die_ref die;
6119   const char *name;
6120 }
6121 pubname_entry;
6122
6123 DEF_VEC_O(pubname_entry);
6124 DEF_VEC_ALLOC_O(pubname_entry, gc);
6125
6126 struct GTY(()) dw_ranges_struct {
6127   /* If this is positive, it's a block number, otherwise it's a
6128      bitwise-negated index into dw_ranges_by_label.  */
6129   int num;
6130 };
6131
6132 /* A structure to hold a macinfo entry.  */
6133
6134 typedef struct GTY(()) macinfo_struct {
6135   unsigned HOST_WIDE_INT code;
6136   unsigned HOST_WIDE_INT lineno;
6137   const char *info;
6138 }
6139 macinfo_entry;
6140
6141 DEF_VEC_O(macinfo_entry);
6142 DEF_VEC_ALLOC_O(macinfo_entry, gc);
6143
6144 struct GTY(()) dw_ranges_by_label_struct {
6145   const char *begin;
6146   const char *end;
6147 };
6148
6149 /* The comdat type node structure.  */
6150 typedef struct GTY(()) comdat_type_struct
6151 {
6152   dw_die_ref root_die;
6153   dw_die_ref type_die;
6154   char signature[DWARF_TYPE_SIGNATURE_SIZE];
6155   struct comdat_type_struct *next;
6156 }
6157 comdat_type_node;
6158
6159 /* The limbo die list structure.  */
6160 typedef struct GTY(()) limbo_die_struct {
6161   dw_die_ref die;
6162   tree created_for;
6163   struct limbo_die_struct *next;
6164 }
6165 limbo_die_node;
6166
6167 typedef struct skeleton_chain_struct
6168 {
6169   dw_die_ref old_die;
6170   dw_die_ref new_die;
6171   struct skeleton_chain_struct *parent;
6172 }
6173 skeleton_chain_node;
6174
6175 /* How to start an assembler comment.  */
6176 #ifndef ASM_COMMENT_START
6177 #define ASM_COMMENT_START ";#"
6178 #endif
6179
6180 /* Define a macro which returns nonzero for a TYPE_DECL which was
6181    implicitly generated for a tagged type.
6182
6183    Note that unlike the gcc front end (which generates a NULL named
6184    TYPE_DECL node for each complete tagged type, each array type, and
6185    each function type node created) the g++ front end generates a
6186    _named_ TYPE_DECL node for each tagged type node created.
6187    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
6188    generate a DW_TAG_typedef DIE for them.  */
6189
6190 #define TYPE_DECL_IS_STUB(decl)                         \
6191   (DECL_NAME (decl) == NULL_TREE                        \
6192    || (DECL_ARTIFICIAL (decl)                           \
6193        && is_tagged_type (TREE_TYPE (decl))             \
6194        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
6195            /* This is necessary for stub decls that     \
6196               appear in nested inline functions.  */    \
6197            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6198                && (decl_ultimate_origin (decl)          \
6199                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6200
6201 /* Information concerning the compilation unit's programming
6202    language, and compiler version.  */
6203
6204 /* Fixed size portion of the DWARF compilation unit header.  */
6205 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6206   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6207
6208 /* Fixed size portion of the DWARF comdat type unit header.  */
6209 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6210   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6211    + DWARF_OFFSET_SIZE)
6212
6213 /* Fixed size portion of public names info.  */
6214 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6215
6216 /* Fixed size portion of the address range info.  */
6217 #define DWARF_ARANGES_HEADER_SIZE                                       \
6218   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6219                 DWARF2_ADDR_SIZE * 2)                                   \
6220    - DWARF_INITIAL_LENGTH_SIZE)
6221
6222 /* Size of padding portion in the address range info.  It must be
6223    aligned to twice the pointer size.  */
6224 #define DWARF_ARANGES_PAD_SIZE \
6225   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6226                 DWARF2_ADDR_SIZE * 2)                              \
6227    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6228
6229 /* Use assembler line directives if available.  */
6230 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6231 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6232 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6233 #else
6234 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6235 #endif
6236 #endif
6237
6238 /* Minimum line offset in a special line info. opcode.
6239    This value was chosen to give a reasonable range of values.  */
6240 #define DWARF_LINE_BASE  -10
6241
6242 /* First special line opcode - leave room for the standard opcodes.  */
6243 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
6244
6245 /* Range of line offsets in a special line info. opcode.  */
6246 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6247
6248 /* Flag that indicates the initial value of the is_stmt_start flag.
6249    In the present implementation, we do not mark any lines as
6250    the beginning of a source statement, because that information
6251    is not made available by the GCC front-end.  */
6252 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6253
6254 /* Maximum number of operations per instruction bundle.  */
6255 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6256 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6257 #endif
6258
6259 /* This location is used by calc_die_sizes() to keep track
6260    the offset of each DIE within the .debug_info section.  */
6261 static unsigned long next_die_offset;
6262
6263 /* Record the root of the DIE's built for the current compilation unit.  */
6264 static GTY(()) dw_die_ref single_comp_unit_die;
6265
6266 /* A list of type DIEs that have been separated into comdat sections.  */
6267 static GTY(()) comdat_type_node *comdat_type_list;
6268
6269 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6270 static GTY(()) limbo_die_node *limbo_die_list;
6271
6272 /* A list of DIEs for which we may have to generate
6273    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6274 static GTY(()) limbo_die_node *deferred_asm_name;
6275
6276 /* Filenames referenced by this compilation unit.  */
6277 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6278
6279 /* A hash table of references to DIE's that describe declarations.
6280    The key is a DECL_UID() which is a unique number identifying each decl.  */
6281 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6282
6283 /* A hash table of references to DIE's that describe COMMON blocks.
6284    The key is DECL_UID() ^ die_parent.  */
6285 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6286
6287 typedef struct GTY(()) die_arg_entry_struct {
6288     dw_die_ref die;
6289     tree arg;
6290 } die_arg_entry;
6291
6292 DEF_VEC_O(die_arg_entry);
6293 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6294
6295 /* Node of the variable location list.  */
6296 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6297   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6298      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6299      in mode of the EXPR_LIST node and first EXPR_LIST operand
6300      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6301      location or NULL for padding.  For larger bitsizes,
6302      mode is 0 and first operand is a CONCAT with bitsize
6303      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6304      NULL as second operand.  */
6305   rtx GTY (()) loc;
6306   const char * GTY (()) label;
6307   struct var_loc_node * GTY (()) next;
6308 };
6309
6310 /* Variable location list.  */
6311 struct GTY (()) var_loc_list_def {
6312   struct var_loc_node * GTY (()) first;
6313
6314   /* Pointer to the last but one or last element of the
6315      chained list.  If the list is empty, both first and
6316      last are NULL, if the list contains just one node
6317      or the last node certainly is not redundant, it points
6318      to the last node, otherwise points to the last but one.
6319      Do not mark it for GC because it is marked through the chain.  */
6320   struct var_loc_node * GTY ((skip ("%h"))) last;
6321
6322   /* Pointer to the last element before section switch,
6323      if NULL, either sections weren't switched or first
6324      is after section switch.  */
6325   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
6326
6327   /* DECL_UID of the variable decl.  */
6328   unsigned int decl_id;
6329 };
6330 typedef struct var_loc_list_def var_loc_list;
6331
6332 /* Call argument location list.  */
6333 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6334   rtx GTY (()) call_arg_loc_note;
6335   const char * GTY (()) label;
6336   tree GTY (()) block;
6337   bool tail_call_p;
6338   rtx GTY (()) symbol_ref;
6339   struct call_arg_loc_node * GTY (()) next;
6340 };
6341
6342
6343 /* Table of decl location linked lists.  */
6344 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6345
6346 /* Head and tail of call_arg_loc chain.  */
6347 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6348 static struct call_arg_loc_node *call_arg_loc_last;
6349
6350 /* Number of call sites in the current function.  */
6351 static int call_site_count = -1;
6352 /* Number of tail call sites in the current function.  */
6353 static int tail_call_site_count = -1;
6354
6355 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6356    DIEs.  */
6357 static VEC (dw_die_ref, heap) *block_map;
6358
6359 /* A cached location list.  */
6360 struct GTY (()) cached_dw_loc_list_def {
6361   /* The DECL_UID of the decl that this entry describes.  */
6362   unsigned int decl_id;
6363
6364   /* The cached location list.  */
6365   dw_loc_list_ref loc_list;
6366 };
6367 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
6368
6369 /* Table of cached location lists.  */
6370 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
6371
6372 /* A pointer to the base of a list of references to DIE's that
6373    are uniquely identified by their tag, presence/absence of
6374    children DIE's, and list of attribute/value pairs.  */
6375 static GTY((length ("abbrev_die_table_allocated")))
6376   dw_die_ref *abbrev_die_table;
6377
6378 /* Number of elements currently allocated for abbrev_die_table.  */
6379 static GTY(()) unsigned abbrev_die_table_allocated;
6380
6381 /* Number of elements in type_die_table currently in use.  */
6382 static GTY(()) unsigned abbrev_die_table_in_use;
6383
6384 /* Size (in elements) of increments by which we may expand the
6385    abbrev_die_table.  */
6386 #define ABBREV_DIE_TABLE_INCREMENT 256
6387
6388 /* A global counter for generating labels for line number data.  */
6389 static unsigned int line_info_label_num;
6390
6391 /* The current table to which we should emit line number information
6392    for the current function.  This will be set up at the beginning of
6393    assembly for the function.  */
6394 static dw_line_info_table *cur_line_info_table;
6395
6396 /* The two default tables of line number info.  */
6397 static GTY(()) dw_line_info_table *text_section_line_info;
6398 static GTY(()) dw_line_info_table *cold_text_section_line_info;
6399
6400 /* The set of all non-default tables of line number info.  */
6401 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
6402
6403 /* A flag to tell pubnames/types export if there is an info section to
6404    refer to.  */
6405 static bool info_section_emitted;
6406
6407 /* A pointer to the base of a table that contains a list of publicly
6408    accessible names.  */
6409 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6410
6411 /* A pointer to the base of a table that contains a list of publicly
6412    accessible types.  */
6413 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6414
6415 /* A pointer to the base of a table that contains a list of macro
6416    defines/undefines (and file start/end markers).  */
6417 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6418
6419 /* Array of dies for which we should generate .debug_ranges info.  */
6420 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6421
6422 /* Number of elements currently allocated for ranges_table.  */
6423 static GTY(()) unsigned ranges_table_allocated;
6424
6425 /* Number of elements in ranges_table currently in use.  */
6426 static GTY(()) unsigned ranges_table_in_use;
6427
6428 /* Array of pairs of labels referenced in ranges_table.  */
6429 static GTY ((length ("ranges_by_label_allocated")))
6430      dw_ranges_by_label_ref ranges_by_label;
6431
6432 /* Number of elements currently allocated for ranges_by_label.  */
6433 static GTY(()) unsigned ranges_by_label_allocated;
6434
6435 /* Number of elements in ranges_by_label currently in use.  */
6436 static GTY(()) unsigned ranges_by_label_in_use;
6437
6438 /* Size (in elements) of increments by which we may expand the
6439    ranges_table.  */
6440 #define RANGES_TABLE_INCREMENT 64
6441
6442 /* Whether we have location lists that need outputting */
6443 static GTY(()) bool have_location_lists;
6444
6445 /* Unique label counter.  */
6446 static GTY(()) unsigned int loclabel_num;
6447
6448 /* Unique label counter for point-of-call tables.  */
6449 static GTY(()) unsigned int poc_label_num;
6450
6451 /* Record whether the function being analyzed contains inlined functions.  */
6452 static int current_function_has_inlines;
6453
6454 /* The last file entry emitted by maybe_emit_file().  */
6455 static GTY(()) struct dwarf_file_data * last_emitted_file;
6456
6457 /* Number of internal labels generated by gen_internal_sym().  */
6458 static GTY(()) int label_num;
6459
6460 /* Cached result of previous call to lookup_filename.  */
6461 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6462
6463 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6464
6465 /* Instances of generic types for which we need to generate debug
6466    info that describe their generic parameters and arguments. That
6467    generation needs to happen once all types are properly laid out so
6468    we do it at the end of compilation.  */
6469 static GTY(()) VEC(tree,gc) *generic_type_instances;
6470
6471 /* Offset from the "steady-state frame pointer" to the frame base,
6472    within the current function.  */
6473 static HOST_WIDE_INT frame_pointer_fb_offset;
6474 static bool frame_pointer_fb_offset_valid;
6475
6476 static VEC (dw_die_ref, heap) *base_types;
6477
6478 /* Forward declarations for functions defined in this file.  */
6479
6480 static int is_pseudo_reg (const_rtx);
6481 static tree type_main_variant (tree);
6482 static int is_tagged_type (const_tree);
6483 static const char *dwarf_tag_name (unsigned);
6484 static const char *dwarf_attr_name (unsigned);
6485 static const char *dwarf_form_name (unsigned);
6486 static tree decl_ultimate_origin (const_tree);
6487 static tree decl_class_context (tree);
6488 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6489 static inline enum dw_val_class AT_class (dw_attr_ref);
6490 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6491 static inline unsigned AT_flag (dw_attr_ref);
6492 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6493 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6494 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6495 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6496 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6497                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6498 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6499                                unsigned int, unsigned char *);
6500 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6501 static hashval_t debug_str_do_hash (const void *);
6502 static int debug_str_eq (const void *, const void *);
6503 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6504 static inline const char *AT_string (dw_attr_ref);
6505 static enum dwarf_form AT_string_form (dw_attr_ref);
6506 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6507 static void add_AT_specification (dw_die_ref, dw_die_ref);
6508 static inline dw_die_ref AT_ref (dw_attr_ref);
6509 static inline int AT_ref_external (dw_attr_ref);
6510 static inline void set_AT_ref_external (dw_attr_ref, int);
6511 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6512 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6513 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6514 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6515                              dw_loc_list_ref);
6516 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6517 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6518 static inline rtx AT_addr (dw_attr_ref);
6519 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6520 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6521 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6522 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6523                            unsigned HOST_WIDE_INT);
6524 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6525                                unsigned long);
6526 static inline const char *AT_lbl (dw_attr_ref);
6527 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6528 static const char *get_AT_low_pc (dw_die_ref);
6529 static const char *get_AT_hi_pc (dw_die_ref);
6530 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6531 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6532 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6533 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6534 static bool is_cxx (void);
6535 static bool is_fortran (void);
6536 static bool is_ada (void);
6537 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6538 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6539 static void add_child_die (dw_die_ref, dw_die_ref);
6540 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6541 static dw_die_ref lookup_type_die (tree);
6542 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6543 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6544 static void equate_type_number_to_die (tree, dw_die_ref);
6545 static hashval_t decl_die_table_hash (const void *);
6546 static int decl_die_table_eq (const void *, const void *);
6547 static dw_die_ref lookup_decl_die (tree);
6548 static hashval_t common_block_die_table_hash (const void *);
6549 static int common_block_die_table_eq (const void *, const void *);
6550 static hashval_t decl_loc_table_hash (const void *);
6551 static int decl_loc_table_eq (const void *, const void *);
6552 static var_loc_list *lookup_decl_loc (const_tree);
6553 static void equate_decl_number_to_die (tree, dw_die_ref);
6554 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6555 static void print_spaces (FILE *);
6556 static void print_die (dw_die_ref, FILE *);
6557 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6558 static dw_die_ref pop_compile_unit (dw_die_ref);
6559 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6560 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6561 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6562 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6563 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6564 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6565 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6566                                    struct md5_ctx *, int *);
6567 struct checksum_attributes;
6568 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6569 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6570 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6571 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6572 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6573 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6574 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6575 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6576 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6577 static void compute_section_prefix (dw_die_ref);
6578 static int is_type_die (dw_die_ref);
6579 static int is_comdat_die (dw_die_ref);
6580 static int is_symbol_die (dw_die_ref);
6581 static void assign_symbol_names (dw_die_ref);
6582 static void break_out_includes (dw_die_ref);
6583 static int is_declaration_die (dw_die_ref);
6584 static int should_move_die_to_comdat (dw_die_ref);
6585 static dw_die_ref clone_as_declaration (dw_die_ref);
6586 static dw_die_ref clone_die (dw_die_ref);
6587 static dw_die_ref clone_tree (dw_die_ref);
6588 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6589 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6590 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6591 static dw_die_ref generate_skeleton (dw_die_ref);
6592 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6593                                                          dw_die_ref);
6594 static void break_out_comdat_types (dw_die_ref);
6595 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6596 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6597 static void copy_decls_for_unworthy_types (dw_die_ref);
6598
6599 static hashval_t htab_cu_hash (const void *);
6600 static int htab_cu_eq (const void *, const void *);
6601 static void htab_cu_del (void *);
6602 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6603 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6604 static void add_sibling_attributes (dw_die_ref);
6605 static void build_abbrev_table (dw_die_ref);
6606 static void output_location_lists (dw_die_ref);
6607 static int constant_size (unsigned HOST_WIDE_INT);
6608 static unsigned long size_of_die (dw_die_ref);
6609 static void calc_die_sizes (dw_die_ref);
6610 static void calc_base_type_die_sizes (void);
6611 static void mark_dies (dw_die_ref);
6612 static void unmark_dies (dw_die_ref);
6613 static void unmark_all_dies (dw_die_ref);
6614 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6615 static unsigned long size_of_aranges (void);
6616 static enum dwarf_form value_format (dw_attr_ref);
6617 static void output_value_format (dw_attr_ref);
6618 static void output_abbrev_section (void);
6619 static void output_die_symbol (dw_die_ref);
6620 static void output_die (dw_die_ref);
6621 static void output_compilation_unit_header (void);
6622 static void output_comp_unit (dw_die_ref, int);
6623 static void output_comdat_type_unit (comdat_type_node *);
6624 static const char *dwarf2_name (tree, int);
6625 static void add_pubname (tree, dw_die_ref);
6626 static void add_pubname_string (const char *, dw_die_ref);
6627 static void add_pubtype (tree, dw_die_ref);
6628 static void output_pubnames (VEC (pubname_entry,gc) *);
6629 static void output_aranges (unsigned long);
6630 static unsigned int add_ranges_num (int);
6631 static unsigned int add_ranges (const_tree);
6632 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6633                                   bool *);
6634 static void output_ranges (void);
6635 static dw_line_info_table *new_line_info_table (void);
6636 static void output_line_info (void);
6637 static void output_file_names (void);
6638 static dw_die_ref base_type_die (tree);
6639 static int is_base_type (tree);
6640 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6641 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6642 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6643 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6644 static int type_is_enum (const_tree);
6645 static unsigned int dbx_reg_number (const_rtx);
6646 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6647 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6648 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6649                                                 enum var_init_status);
6650 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6651                                                      enum var_init_status);
6652 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6653                                          enum var_init_status);
6654 static int is_based_loc (const_rtx);
6655 static int resolve_one_addr (rtx *, void *);
6656 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6657                                                enum var_init_status);
6658 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6659                                         enum var_init_status);
6660 static dw_loc_list_ref loc_list_from_tree (tree, int);
6661 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6662 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6663 static tree field_type (const_tree);
6664 static unsigned int simple_type_align_in_bits (const_tree);
6665 static unsigned int simple_decl_align_in_bits (const_tree);
6666 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6667 static HOST_WIDE_INT field_byte_offset (const_tree);
6668 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6669                                          dw_loc_list_ref);
6670 static void add_data_member_location_attribute (dw_die_ref, tree);
6671 static bool add_const_value_attribute (dw_die_ref, rtx);
6672 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6673 static void insert_double (double_int, unsigned char *);
6674 static void insert_float (const_rtx, unsigned char *);
6675 static rtx rtl_for_decl_location (tree);
6676 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
6677                                                    enum dwarf_attribute);
6678 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6679 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6680 static void add_name_attribute (dw_die_ref, const char *);
6681 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
6682 static void add_comp_dir_attribute (dw_die_ref);
6683 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6684 static void add_subscript_info (dw_die_ref, tree, bool);
6685 static void add_byte_size_attribute (dw_die_ref, tree);
6686 static void add_bit_offset_attribute (dw_die_ref, tree);
6687 static void add_bit_size_attribute (dw_die_ref, tree);
6688 static void add_prototyped_attribute (dw_die_ref, tree);
6689 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6690 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6691 static void add_src_coords_attributes (dw_die_ref, tree);
6692 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6693 static void push_decl_scope (tree);
6694 static void pop_decl_scope (void);
6695 static dw_die_ref scope_die_for (tree, dw_die_ref);
6696 static inline int local_scope_p (dw_die_ref);
6697 static inline int class_scope_p (dw_die_ref);
6698 static inline int class_or_namespace_scope_p (dw_die_ref);
6699 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6700 static void add_calling_convention_attribute (dw_die_ref, tree);
6701 static const char *type_tag (const_tree);
6702 static tree member_declared_type (const_tree);
6703 #if 0
6704 static const char *decl_start_label (tree);
6705 #endif
6706 static void gen_array_type_die (tree, dw_die_ref);
6707 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6708 #if 0
6709 static void gen_entry_point_die (tree, dw_die_ref);
6710 #endif
6711 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6712 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6713 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6714 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6715 static void gen_formal_types_die (tree, dw_die_ref);
6716 static void gen_subprogram_die (tree, dw_die_ref);
6717 static void gen_variable_die (tree, tree, dw_die_ref);
6718 static void gen_const_die (tree, dw_die_ref);
6719 static void gen_label_die (tree, dw_die_ref);
6720 static void gen_lexical_block_die (tree, dw_die_ref, int);
6721 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6722 static void gen_field_die (tree, dw_die_ref);
6723 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6724 static dw_die_ref gen_compile_unit_die (const char *);
6725 static void gen_inheritance_die (tree, tree, dw_die_ref);
6726 static void gen_member_die (tree, dw_die_ref);
6727 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6728                                                 enum debug_info_usage);
6729 static void gen_subroutine_type_die (tree, dw_die_ref);
6730 static void gen_typedef_die (tree, dw_die_ref);
6731 static void gen_type_die (tree, dw_die_ref);
6732 static void gen_block_die (tree, dw_die_ref, int);
6733 static void decls_for_scope (tree, dw_die_ref, int);
6734 static inline int is_redundant_typedef (const_tree);
6735 static bool is_naming_typedef_decl (const_tree);
6736 static inline dw_die_ref get_context_die (tree);
6737 static void gen_namespace_die (tree, dw_die_ref);
6738 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6739 static dw_die_ref force_decl_die (tree);
6740 static dw_die_ref force_type_die (tree);
6741 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6742 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6743 static struct dwarf_file_data * lookup_filename (const char *);
6744 static void retry_incomplete_types (void);
6745 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6746 static void gen_generic_params_dies (tree);
6747 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6748 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6749 static void splice_child_die (dw_die_ref, dw_die_ref);
6750 static int file_info_cmp (const void *, const void *);
6751 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6752                                      const char *, const char *);
6753 static void output_loc_list (dw_loc_list_ref);
6754 static char *gen_internal_sym (const char *);
6755
6756 static void prune_unmark_dies (dw_die_ref);
6757 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6758 static void prune_unused_types_mark (dw_die_ref, int);
6759 static void prune_unused_types_walk (dw_die_ref);
6760 static void prune_unused_types_walk_attribs (dw_die_ref);
6761 static void prune_unused_types_prune (dw_die_ref);
6762 static void prune_unused_types (void);
6763 static int maybe_emit_file (struct dwarf_file_data *fd);
6764 static inline const char *AT_vms_delta1 (dw_attr_ref);
6765 static inline const char *AT_vms_delta2 (dw_attr_ref);
6766 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6767                                      const char *, const char *);
6768 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6769 static void gen_remaining_tmpl_value_param_die_attribute (void);
6770 static bool generic_type_p (tree);
6771 static void schedule_generic_params_dies_gen (tree t);
6772 static void gen_scheduled_generic_parms_dies (void);
6773
6774 /* Section names used to hold DWARF debugging information.  */
6775 #ifndef DEBUG_INFO_SECTION
6776 #define DEBUG_INFO_SECTION      ".debug_info"
6777 #endif
6778 #ifndef DEBUG_ABBREV_SECTION
6779 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6780 #endif
6781 #ifndef DEBUG_ARANGES_SECTION
6782 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6783 #endif
6784 #ifndef DEBUG_MACINFO_SECTION
6785 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6786 #endif
6787 #ifndef DEBUG_LINE_SECTION
6788 #define DEBUG_LINE_SECTION      ".debug_line"
6789 #endif
6790 #ifndef DEBUG_LOC_SECTION
6791 #define DEBUG_LOC_SECTION       ".debug_loc"
6792 #endif
6793 #ifndef DEBUG_PUBNAMES_SECTION
6794 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6795 #endif
6796 #ifndef DEBUG_PUBTYPES_SECTION
6797 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6798 #endif
6799 #ifndef DEBUG_STR_SECTION
6800 #define DEBUG_STR_SECTION       ".debug_str"
6801 #endif
6802 #ifndef DEBUG_RANGES_SECTION
6803 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6804 #endif
6805
6806 /* Standard ELF section names for compiled code and data.  */
6807 #ifndef TEXT_SECTION_NAME
6808 #define TEXT_SECTION_NAME       ".text"
6809 #endif
6810
6811 /* Section flags for .debug_str section.  */
6812 #define DEBUG_STR_SECTION_FLAGS \
6813   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6814    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6815    : SECTION_DEBUG)
6816
6817 /* Labels we insert at beginning sections we can reference instead of
6818    the section names themselves.  */
6819
6820 #ifndef TEXT_SECTION_LABEL
6821 #define TEXT_SECTION_LABEL              "Ltext"
6822 #endif
6823 #ifndef COLD_TEXT_SECTION_LABEL
6824 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6825 #endif
6826 #ifndef DEBUG_LINE_SECTION_LABEL
6827 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6828 #endif
6829 #ifndef DEBUG_INFO_SECTION_LABEL
6830 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6831 #endif
6832 #ifndef DEBUG_ABBREV_SECTION_LABEL
6833 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6834 #endif
6835 #ifndef DEBUG_LOC_SECTION_LABEL
6836 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6837 #endif
6838 #ifndef DEBUG_RANGES_SECTION_LABEL
6839 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6840 #endif
6841 #ifndef DEBUG_MACINFO_SECTION_LABEL
6842 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6843 #endif
6844
6845
6846 /* Definitions of defaults for formats and names of various special
6847    (artificial) labels which may be generated within this file (when the -g
6848    options is used and DWARF2_DEBUGGING_INFO is in effect.
6849    If necessary, these may be overridden from within the tm.h file, but
6850    typically, overriding these defaults is unnecessary.  */
6851
6852 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6853 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6854 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6855 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6856 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6857 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6858 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6859 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6860 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6861 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6862
6863 #ifndef TEXT_END_LABEL
6864 #define TEXT_END_LABEL          "Letext"
6865 #endif
6866 #ifndef COLD_END_LABEL
6867 #define COLD_END_LABEL          "Letext_cold"
6868 #endif
6869 #ifndef BLOCK_BEGIN_LABEL
6870 #define BLOCK_BEGIN_LABEL       "LBB"
6871 #endif
6872 #ifndef BLOCK_END_LABEL
6873 #define BLOCK_END_LABEL         "LBE"
6874 #endif
6875 #ifndef LINE_CODE_LABEL
6876 #define LINE_CODE_LABEL         "LM"
6877 #endif
6878
6879 \f
6880 /* Return the root of the DIE's built for the current compilation unit.  */
6881 static dw_die_ref
6882 comp_unit_die (void)
6883 {
6884   if (!single_comp_unit_die)
6885     single_comp_unit_die = gen_compile_unit_die (NULL);
6886   return single_comp_unit_die;
6887 }
6888
6889 /* We allow a language front-end to designate a function that is to be
6890    called to "demangle" any name before it is put into a DIE.  */
6891
6892 static const char *(*demangle_name_func) (const char *);
6893
6894 void
6895 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6896 {
6897   demangle_name_func = func;
6898 }
6899
6900 /* Test if rtl node points to a pseudo register.  */
6901
6902 static inline int
6903 is_pseudo_reg (const_rtx rtl)
6904 {
6905   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6906           || (GET_CODE (rtl) == SUBREG
6907               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6908 }
6909
6910 /* Return a reference to a type, with its const and volatile qualifiers
6911    removed.  */
6912
6913 static inline tree
6914 type_main_variant (tree type)
6915 {
6916   type = TYPE_MAIN_VARIANT (type);
6917
6918   /* ??? There really should be only one main variant among any group of
6919      variants of a given type (and all of the MAIN_VARIANT values for all
6920      members of the group should point to that one type) but sometimes the C
6921      front-end messes this up for array types, so we work around that bug
6922      here.  */
6923   if (TREE_CODE (type) == ARRAY_TYPE)
6924     while (type != TYPE_MAIN_VARIANT (type))
6925       type = TYPE_MAIN_VARIANT (type);
6926
6927   return type;
6928 }
6929
6930 /* Return nonzero if the given type node represents a tagged type.  */
6931
6932 static inline int
6933 is_tagged_type (const_tree type)
6934 {
6935   enum tree_code code = TREE_CODE (type);
6936
6937   return (code == RECORD_TYPE || code == UNION_TYPE
6938           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6939 }
6940
6941 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6942
6943 static void
6944 get_ref_die_offset_label (char *label, dw_die_ref ref)
6945 {
6946   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6947 }
6948
6949 /* Return die_offset of a DIE reference to a base type.  */
6950
6951 static unsigned long int
6952 get_base_type_offset (dw_die_ref ref)
6953 {
6954   if (ref->die_offset)
6955     return ref->die_offset;
6956   if (comp_unit_die ()->die_abbrev)
6957     {
6958       calc_base_type_die_sizes ();
6959       gcc_assert (ref->die_offset);
6960     }
6961   return ref->die_offset;
6962 }
6963
6964 /* Convert a DIE tag into its string name.  */
6965
6966 static const char *
6967 dwarf_tag_name (unsigned int tag)
6968 {
6969   switch (tag)
6970     {
6971     case DW_TAG_padding:
6972       return "DW_TAG_padding";
6973     case DW_TAG_array_type:
6974       return "DW_TAG_array_type";
6975     case DW_TAG_class_type:
6976       return "DW_TAG_class_type";
6977     case DW_TAG_entry_point:
6978       return "DW_TAG_entry_point";
6979     case DW_TAG_enumeration_type:
6980       return "DW_TAG_enumeration_type";
6981     case DW_TAG_formal_parameter:
6982       return "DW_TAG_formal_parameter";
6983     case DW_TAG_imported_declaration:
6984       return "DW_TAG_imported_declaration";
6985     case DW_TAG_label:
6986       return "DW_TAG_label";
6987     case DW_TAG_lexical_block:
6988       return "DW_TAG_lexical_block";
6989     case DW_TAG_member:
6990       return "DW_TAG_member";
6991     case DW_TAG_pointer_type:
6992       return "DW_TAG_pointer_type";
6993     case DW_TAG_reference_type:
6994       return "DW_TAG_reference_type";
6995     case DW_TAG_compile_unit:
6996       return "DW_TAG_compile_unit";
6997     case DW_TAG_string_type:
6998       return "DW_TAG_string_type";
6999     case DW_TAG_structure_type:
7000       return "DW_TAG_structure_type";
7001     case DW_TAG_subroutine_type:
7002       return "DW_TAG_subroutine_type";
7003     case DW_TAG_typedef:
7004       return "DW_TAG_typedef";
7005     case DW_TAG_union_type:
7006       return "DW_TAG_union_type";
7007     case DW_TAG_unspecified_parameters:
7008       return "DW_TAG_unspecified_parameters";
7009     case DW_TAG_variant:
7010       return "DW_TAG_variant";
7011     case DW_TAG_common_block:
7012       return "DW_TAG_common_block";
7013     case DW_TAG_common_inclusion:
7014       return "DW_TAG_common_inclusion";
7015     case DW_TAG_inheritance:
7016       return "DW_TAG_inheritance";
7017     case DW_TAG_inlined_subroutine:
7018       return "DW_TAG_inlined_subroutine";
7019     case DW_TAG_module:
7020       return "DW_TAG_module";
7021     case DW_TAG_ptr_to_member_type:
7022       return "DW_TAG_ptr_to_member_type";
7023     case DW_TAG_set_type:
7024       return "DW_TAG_set_type";
7025     case DW_TAG_subrange_type:
7026       return "DW_TAG_subrange_type";
7027     case DW_TAG_with_stmt:
7028       return "DW_TAG_with_stmt";
7029     case DW_TAG_access_declaration:
7030       return "DW_TAG_access_declaration";
7031     case DW_TAG_base_type:
7032       return "DW_TAG_base_type";
7033     case DW_TAG_catch_block:
7034       return "DW_TAG_catch_block";
7035     case DW_TAG_const_type:
7036       return "DW_TAG_const_type";
7037     case DW_TAG_constant:
7038       return "DW_TAG_constant";
7039     case DW_TAG_enumerator:
7040       return "DW_TAG_enumerator";
7041     case DW_TAG_file_type:
7042       return "DW_TAG_file_type";
7043     case DW_TAG_friend:
7044       return "DW_TAG_friend";
7045     case DW_TAG_namelist:
7046       return "DW_TAG_namelist";
7047     case DW_TAG_namelist_item:
7048       return "DW_TAG_namelist_item";
7049     case DW_TAG_packed_type:
7050       return "DW_TAG_packed_type";
7051     case DW_TAG_subprogram:
7052       return "DW_TAG_subprogram";
7053     case DW_TAG_template_type_param:
7054       return "DW_TAG_template_type_param";
7055     case DW_TAG_template_value_param:
7056       return "DW_TAG_template_value_param";
7057     case DW_TAG_thrown_type:
7058       return "DW_TAG_thrown_type";
7059     case DW_TAG_try_block:
7060       return "DW_TAG_try_block";
7061     case DW_TAG_variant_part:
7062       return "DW_TAG_variant_part";
7063     case DW_TAG_variable:
7064       return "DW_TAG_variable";
7065     case DW_TAG_volatile_type:
7066       return "DW_TAG_volatile_type";
7067     case DW_TAG_dwarf_procedure:
7068       return "DW_TAG_dwarf_procedure";
7069     case DW_TAG_restrict_type:
7070       return "DW_TAG_restrict_type";
7071     case DW_TAG_interface_type:
7072       return "DW_TAG_interface_type";
7073     case DW_TAG_namespace:
7074       return "DW_TAG_namespace";
7075     case DW_TAG_imported_module:
7076       return "DW_TAG_imported_module";
7077     case DW_TAG_unspecified_type:
7078       return "DW_TAG_unspecified_type";
7079     case DW_TAG_partial_unit:
7080       return "DW_TAG_partial_unit";
7081     case DW_TAG_imported_unit:
7082       return "DW_TAG_imported_unit";
7083     case DW_TAG_condition:
7084       return "DW_TAG_condition";
7085     case DW_TAG_shared_type:
7086       return "DW_TAG_shared_type";
7087     case DW_TAG_type_unit:
7088       return "DW_TAG_type_unit";
7089     case DW_TAG_rvalue_reference_type:
7090       return "DW_TAG_rvalue_reference_type";
7091     case DW_TAG_template_alias:
7092       return "DW_TAG_template_alias";
7093     case DW_TAG_GNU_template_parameter_pack:
7094       return "DW_TAG_GNU_template_parameter_pack";
7095     case DW_TAG_GNU_formal_parameter_pack:
7096       return "DW_TAG_GNU_formal_parameter_pack";
7097     case DW_TAG_MIPS_loop:
7098       return "DW_TAG_MIPS_loop";
7099     case DW_TAG_format_label:
7100       return "DW_TAG_format_label";
7101     case DW_TAG_function_template:
7102       return "DW_TAG_function_template";
7103     case DW_TAG_class_template:
7104       return "DW_TAG_class_template";
7105     case DW_TAG_GNU_BINCL:
7106       return "DW_TAG_GNU_BINCL";
7107     case DW_TAG_GNU_EINCL:
7108       return "DW_TAG_GNU_EINCL";
7109     case DW_TAG_GNU_template_template_param:
7110       return "DW_TAG_GNU_template_template_param";
7111     case DW_TAG_GNU_call_site:
7112       return "DW_TAG_GNU_call_site";
7113     case DW_TAG_GNU_call_site_parameter:
7114       return "DW_TAG_GNU_call_site_parameter";
7115     default:
7116       return "DW_TAG_<unknown>";
7117     }
7118 }
7119
7120 /* Convert a DWARF attribute code into its string name.  */
7121
7122 static const char *
7123 dwarf_attr_name (unsigned int attr)
7124 {
7125   switch (attr)
7126     {
7127     case DW_AT_sibling:
7128       return "DW_AT_sibling";
7129     case DW_AT_location:
7130       return "DW_AT_location";
7131     case DW_AT_name:
7132       return "DW_AT_name";
7133     case DW_AT_ordering:
7134       return "DW_AT_ordering";
7135     case DW_AT_subscr_data:
7136       return "DW_AT_subscr_data";
7137     case DW_AT_byte_size:
7138       return "DW_AT_byte_size";
7139     case DW_AT_bit_offset:
7140       return "DW_AT_bit_offset";
7141     case DW_AT_bit_size:
7142       return "DW_AT_bit_size";
7143     case DW_AT_element_list:
7144       return "DW_AT_element_list";
7145     case DW_AT_stmt_list:
7146       return "DW_AT_stmt_list";
7147     case DW_AT_low_pc:
7148       return "DW_AT_low_pc";
7149     case DW_AT_high_pc:
7150       return "DW_AT_high_pc";
7151     case DW_AT_language:
7152       return "DW_AT_language";
7153     case DW_AT_member:
7154       return "DW_AT_member";
7155     case DW_AT_discr:
7156       return "DW_AT_discr";
7157     case DW_AT_discr_value:
7158       return "DW_AT_discr_value";
7159     case DW_AT_visibility:
7160       return "DW_AT_visibility";
7161     case DW_AT_import:
7162       return "DW_AT_import";
7163     case DW_AT_string_length:
7164       return "DW_AT_string_length";
7165     case DW_AT_common_reference:
7166       return "DW_AT_common_reference";
7167     case DW_AT_comp_dir:
7168       return "DW_AT_comp_dir";
7169     case DW_AT_const_value:
7170       return "DW_AT_const_value";
7171     case DW_AT_containing_type:
7172       return "DW_AT_containing_type";
7173     case DW_AT_default_value:
7174       return "DW_AT_default_value";
7175     case DW_AT_inline:
7176       return "DW_AT_inline";
7177     case DW_AT_is_optional:
7178       return "DW_AT_is_optional";
7179     case DW_AT_lower_bound:
7180       return "DW_AT_lower_bound";
7181     case DW_AT_producer:
7182       return "DW_AT_producer";
7183     case DW_AT_prototyped:
7184       return "DW_AT_prototyped";
7185     case DW_AT_return_addr:
7186       return "DW_AT_return_addr";
7187     case DW_AT_start_scope:
7188       return "DW_AT_start_scope";
7189     case DW_AT_bit_stride:
7190       return "DW_AT_bit_stride";
7191     case DW_AT_upper_bound:
7192       return "DW_AT_upper_bound";
7193     case DW_AT_abstract_origin:
7194       return "DW_AT_abstract_origin";
7195     case DW_AT_accessibility:
7196       return "DW_AT_accessibility";
7197     case DW_AT_address_class:
7198       return "DW_AT_address_class";
7199     case DW_AT_artificial:
7200       return "DW_AT_artificial";
7201     case DW_AT_base_types:
7202       return "DW_AT_base_types";
7203     case DW_AT_calling_convention:
7204       return "DW_AT_calling_convention";
7205     case DW_AT_count:
7206       return "DW_AT_count";
7207     case DW_AT_data_member_location:
7208       return "DW_AT_data_member_location";
7209     case DW_AT_decl_column:
7210       return "DW_AT_decl_column";
7211     case DW_AT_decl_file:
7212       return "DW_AT_decl_file";
7213     case DW_AT_decl_line:
7214       return "DW_AT_decl_line";
7215     case DW_AT_declaration:
7216       return "DW_AT_declaration";
7217     case DW_AT_discr_list:
7218       return "DW_AT_discr_list";
7219     case DW_AT_encoding:
7220       return "DW_AT_encoding";
7221     case DW_AT_external:
7222       return "DW_AT_external";
7223     case DW_AT_explicit:
7224       return "DW_AT_explicit";
7225     case DW_AT_frame_base:
7226       return "DW_AT_frame_base";
7227     case DW_AT_friend:
7228       return "DW_AT_friend";
7229     case DW_AT_identifier_case:
7230       return "DW_AT_identifier_case";
7231     case DW_AT_macro_info:
7232       return "DW_AT_macro_info";
7233     case DW_AT_namelist_items:
7234       return "DW_AT_namelist_items";
7235     case DW_AT_priority:
7236       return "DW_AT_priority";
7237     case DW_AT_segment:
7238       return "DW_AT_segment";
7239     case DW_AT_specification:
7240       return "DW_AT_specification";
7241     case DW_AT_static_link:
7242       return "DW_AT_static_link";
7243     case DW_AT_type:
7244       return "DW_AT_type";
7245     case DW_AT_use_location:
7246       return "DW_AT_use_location";
7247     case DW_AT_variable_parameter:
7248       return "DW_AT_variable_parameter";
7249     case DW_AT_virtuality:
7250       return "DW_AT_virtuality";
7251     case DW_AT_vtable_elem_location:
7252       return "DW_AT_vtable_elem_location";
7253
7254     case DW_AT_allocated:
7255       return "DW_AT_allocated";
7256     case DW_AT_associated:
7257       return "DW_AT_associated";
7258     case DW_AT_data_location:
7259       return "DW_AT_data_location";
7260     case DW_AT_byte_stride:
7261       return "DW_AT_byte_stride";
7262     case DW_AT_entry_pc:
7263       return "DW_AT_entry_pc";
7264     case DW_AT_use_UTF8:
7265       return "DW_AT_use_UTF8";
7266     case DW_AT_extension:
7267       return "DW_AT_extension";
7268     case DW_AT_ranges:
7269       return "DW_AT_ranges";
7270     case DW_AT_trampoline:
7271       return "DW_AT_trampoline";
7272     case DW_AT_call_column:
7273       return "DW_AT_call_column";
7274     case DW_AT_call_file:
7275       return "DW_AT_call_file";
7276     case DW_AT_call_line:
7277       return "DW_AT_call_line";
7278     case DW_AT_object_pointer:
7279       return "DW_AT_object_pointer";
7280
7281     case DW_AT_signature:
7282       return "DW_AT_signature";
7283     case DW_AT_main_subprogram:
7284       return "DW_AT_main_subprogram";
7285     case DW_AT_data_bit_offset:
7286       return "DW_AT_data_bit_offset";
7287     case DW_AT_const_expr:
7288       return "DW_AT_const_expr";
7289     case DW_AT_enum_class:
7290       return "DW_AT_enum_class";
7291     case DW_AT_linkage_name:
7292       return "DW_AT_linkage_name";
7293
7294     case DW_AT_MIPS_fde:
7295       return "DW_AT_MIPS_fde";
7296     case DW_AT_MIPS_loop_begin:
7297       return "DW_AT_MIPS_loop_begin";
7298     case DW_AT_MIPS_tail_loop_begin:
7299       return "DW_AT_MIPS_tail_loop_begin";
7300     case DW_AT_MIPS_epilog_begin:
7301       return "DW_AT_MIPS_epilog_begin";
7302 #if VMS_DEBUGGING_INFO
7303     case DW_AT_HP_prologue:
7304       return "DW_AT_HP_prologue";
7305 #else
7306     case DW_AT_MIPS_loop_unroll_factor:
7307       return "DW_AT_MIPS_loop_unroll_factor";
7308 #endif
7309     case DW_AT_MIPS_software_pipeline_depth:
7310       return "DW_AT_MIPS_software_pipeline_depth";
7311     case DW_AT_MIPS_linkage_name:
7312       return "DW_AT_MIPS_linkage_name";
7313 #if VMS_DEBUGGING_INFO
7314     case DW_AT_HP_epilogue:
7315       return "DW_AT_HP_epilogue";
7316 #else
7317     case DW_AT_MIPS_stride:
7318       return "DW_AT_MIPS_stride";
7319 #endif
7320     case DW_AT_MIPS_abstract_name:
7321       return "DW_AT_MIPS_abstract_name";
7322     case DW_AT_MIPS_clone_origin:
7323       return "DW_AT_MIPS_clone_origin";
7324     case DW_AT_MIPS_has_inlines:
7325       return "DW_AT_MIPS_has_inlines";
7326
7327     case DW_AT_sf_names:
7328       return "DW_AT_sf_names";
7329     case DW_AT_src_info:
7330       return "DW_AT_src_info";
7331     case DW_AT_mac_info:
7332       return "DW_AT_mac_info";
7333     case DW_AT_src_coords:
7334       return "DW_AT_src_coords";
7335     case DW_AT_body_begin:
7336       return "DW_AT_body_begin";
7337     case DW_AT_body_end:
7338       return "DW_AT_body_end";
7339
7340     case DW_AT_GNU_vector:
7341       return "DW_AT_GNU_vector";
7342     case DW_AT_GNU_guarded_by:
7343       return "DW_AT_GNU_guarded_by";
7344     case DW_AT_GNU_pt_guarded_by:
7345       return "DW_AT_GNU_pt_guarded_by";
7346     case DW_AT_GNU_guarded:
7347       return "DW_AT_GNU_guarded";
7348     case DW_AT_GNU_pt_guarded:
7349       return "DW_AT_GNU_pt_guarded";
7350     case DW_AT_GNU_locks_excluded:
7351       return "DW_AT_GNU_locks_excluded";
7352     case DW_AT_GNU_exclusive_locks_required:
7353       return "DW_AT_GNU_exclusive_locks_required";
7354     case DW_AT_GNU_shared_locks_required:
7355       return "DW_AT_GNU_shared_locks_required";
7356     case DW_AT_GNU_odr_signature:
7357       return "DW_AT_GNU_odr_signature";
7358     case DW_AT_GNU_template_name:
7359       return "DW_AT_GNU_template_name";
7360     case DW_AT_GNU_call_site_value:
7361       return "DW_AT_GNU_call_site_value";
7362     case DW_AT_GNU_call_site_data_value:
7363       return "DW_AT_GNU_call_site_data_value";
7364     case DW_AT_GNU_call_site_target:
7365       return "DW_AT_GNU_call_site_target";
7366     case DW_AT_GNU_call_site_target_clobbered:
7367       return "DW_AT_GNU_call_site_target_clobbered";
7368     case DW_AT_GNU_tail_call:
7369       return "DW_AT_GNU_tail_call";
7370     case DW_AT_GNU_all_tail_call_sites:
7371       return "DW_AT_GNU_all_tail_call_sites";
7372     case DW_AT_GNU_all_call_sites:
7373       return "DW_AT_GNU_all_call_sites";
7374     case DW_AT_GNU_all_source_call_sites:
7375       return "DW_AT_GNU_all_source_call_sites";
7376
7377     case DW_AT_GNAT_descriptive_type:
7378       return "DW_AT_GNAT_descriptive_type";
7379
7380     case DW_AT_VMS_rtnbeg_pd_address:
7381       return "DW_AT_VMS_rtnbeg_pd_address";
7382
7383     default:
7384       return "DW_AT_<unknown>";
7385     }
7386 }
7387
7388 /* Convert a DWARF value form code into its string name.  */
7389
7390 static const char *
7391 dwarf_form_name (unsigned int form)
7392 {
7393   switch (form)
7394     {
7395     case DW_FORM_addr:
7396       return "DW_FORM_addr";
7397     case DW_FORM_block2:
7398       return "DW_FORM_block2";
7399     case DW_FORM_block4:
7400       return "DW_FORM_block4";
7401     case DW_FORM_data2:
7402       return "DW_FORM_data2";
7403     case DW_FORM_data4:
7404       return "DW_FORM_data4";
7405     case DW_FORM_data8:
7406       return "DW_FORM_data8";
7407     case DW_FORM_string:
7408       return "DW_FORM_string";
7409     case DW_FORM_block:
7410       return "DW_FORM_block";
7411     case DW_FORM_block1:
7412       return "DW_FORM_block1";
7413     case DW_FORM_data1:
7414       return "DW_FORM_data1";
7415     case DW_FORM_flag:
7416       return "DW_FORM_flag";
7417     case DW_FORM_sdata:
7418       return "DW_FORM_sdata";
7419     case DW_FORM_strp:
7420       return "DW_FORM_strp";
7421     case DW_FORM_udata:
7422       return "DW_FORM_udata";
7423     case DW_FORM_ref_addr:
7424       return "DW_FORM_ref_addr";
7425     case DW_FORM_ref1:
7426       return "DW_FORM_ref1";
7427     case DW_FORM_ref2:
7428       return "DW_FORM_ref2";
7429     case DW_FORM_ref4:
7430       return "DW_FORM_ref4";
7431     case DW_FORM_ref8:
7432       return "DW_FORM_ref8";
7433     case DW_FORM_ref_udata:
7434       return "DW_FORM_ref_udata";
7435     case DW_FORM_indirect:
7436       return "DW_FORM_indirect";
7437     case DW_FORM_sec_offset:
7438       return "DW_FORM_sec_offset";
7439     case DW_FORM_exprloc:
7440       return "DW_FORM_exprloc";
7441     case DW_FORM_flag_present:
7442       return "DW_FORM_flag_present";
7443     case DW_FORM_ref_sig8:
7444       return "DW_FORM_ref_sig8";
7445     default:
7446       return "DW_FORM_<unknown>";
7447     }
7448 }
7449 \f
7450 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7451    instance of an inlined instance of a decl which is local to an inline
7452    function, so we have to trace all of the way back through the origin chain
7453    to find out what sort of node actually served as the original seed for the
7454    given block.  */
7455
7456 static tree
7457 decl_ultimate_origin (const_tree decl)
7458 {
7459   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7460     return NULL_TREE;
7461
7462   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7463      nodes in the function to point to themselves; ignore that if
7464      we're trying to output the abstract instance of this function.  */
7465   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7466     return NULL_TREE;
7467
7468   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7469      most distant ancestor, this should never happen.  */
7470   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7471
7472   return DECL_ABSTRACT_ORIGIN (decl);
7473 }
7474
7475 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7476    of a virtual function may refer to a base class, so we check the 'this'
7477    parameter.  */
7478
7479 static tree
7480 decl_class_context (tree decl)
7481 {
7482   tree context = NULL_TREE;
7483
7484   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7485     context = DECL_CONTEXT (decl);
7486   else
7487     context = TYPE_MAIN_VARIANT
7488       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7489
7490   if (context && !TYPE_P (context))
7491     context = NULL_TREE;
7492
7493   return context;
7494 }
7495 \f
7496 /* Add an attribute/value pair to a DIE.  */
7497
7498 static inline void
7499 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7500 {
7501   /* Maybe this should be an assert?  */
7502   if (die == NULL)
7503     return;
7504
7505   if (die->die_attr == NULL)
7506     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7507   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7508 }
7509
7510 static inline enum dw_val_class
7511 AT_class (dw_attr_ref a)
7512 {
7513   return a->dw_attr_val.val_class;
7514 }
7515
7516 /* Add a flag value attribute to a DIE.  */
7517
7518 static inline void
7519 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7520 {
7521   dw_attr_node attr;
7522
7523   attr.dw_attr = attr_kind;
7524   attr.dw_attr_val.val_class = dw_val_class_flag;
7525   attr.dw_attr_val.v.val_flag = flag;
7526   add_dwarf_attr (die, &attr);
7527 }
7528
7529 static inline unsigned
7530 AT_flag (dw_attr_ref a)
7531 {
7532   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7533   return a->dw_attr_val.v.val_flag;
7534 }
7535
7536 /* Add a signed integer attribute value to a DIE.  */
7537
7538 static inline void
7539 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7540 {
7541   dw_attr_node attr;
7542
7543   attr.dw_attr = attr_kind;
7544   attr.dw_attr_val.val_class = dw_val_class_const;
7545   attr.dw_attr_val.v.val_int = int_val;
7546   add_dwarf_attr (die, &attr);
7547 }
7548
7549 static inline HOST_WIDE_INT
7550 AT_int (dw_attr_ref a)
7551 {
7552   gcc_assert (a && AT_class (a) == dw_val_class_const);
7553   return a->dw_attr_val.v.val_int;
7554 }
7555
7556 /* Add an unsigned integer attribute value to a DIE.  */
7557
7558 static inline void
7559 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7560                  unsigned HOST_WIDE_INT unsigned_val)
7561 {
7562   dw_attr_node attr;
7563
7564   attr.dw_attr = attr_kind;
7565   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7566   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7567   add_dwarf_attr (die, &attr);
7568 }
7569
7570 static inline unsigned HOST_WIDE_INT
7571 AT_unsigned (dw_attr_ref a)
7572 {
7573   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7574   return a->dw_attr_val.v.val_unsigned;
7575 }
7576
7577 /* Add an unsigned double integer attribute value to a DIE.  */
7578
7579 static inline void
7580 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7581                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7582 {
7583   dw_attr_node attr;
7584
7585   attr.dw_attr = attr_kind;
7586   attr.dw_attr_val.val_class = dw_val_class_const_double;
7587   attr.dw_attr_val.v.val_double.high = high;
7588   attr.dw_attr_val.v.val_double.low = low;
7589   add_dwarf_attr (die, &attr);
7590 }
7591
7592 /* Add a floating point attribute value to a DIE and return it.  */
7593
7594 static inline void
7595 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7596             unsigned int length, unsigned int elt_size, unsigned char *array)
7597 {
7598   dw_attr_node attr;
7599
7600   attr.dw_attr = attr_kind;
7601   attr.dw_attr_val.val_class = dw_val_class_vec;
7602   attr.dw_attr_val.v.val_vec.length = length;
7603   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7604   attr.dw_attr_val.v.val_vec.array = array;
7605   add_dwarf_attr (die, &attr);
7606 }
7607
7608 /* Add an 8-byte data attribute value to a DIE.  */
7609
7610 static inline void
7611 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7612               unsigned char data8[8])
7613 {
7614   dw_attr_node attr;
7615
7616   attr.dw_attr = attr_kind;
7617   attr.dw_attr_val.val_class = dw_val_class_data8;
7618   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7619   add_dwarf_attr (die, &attr);
7620 }
7621
7622 /* Hash and equality functions for debug_str_hash.  */
7623
7624 static hashval_t
7625 debug_str_do_hash (const void *x)
7626 {
7627   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7628 }
7629
7630 static int
7631 debug_str_eq (const void *x1, const void *x2)
7632 {
7633   return strcmp ((((const struct indirect_string_node *)x1)->str),
7634                  (const char *)x2) == 0;
7635 }
7636
7637 /* Add STR to the indirect string hash table.  */
7638
7639 static struct indirect_string_node *
7640 find_AT_string (const char *str)
7641 {
7642   struct indirect_string_node *node;
7643   void **slot;
7644
7645   if (! debug_str_hash)
7646     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7647                                       debug_str_eq, NULL);
7648
7649   slot = htab_find_slot_with_hash (debug_str_hash, str,
7650                                    htab_hash_string (str), INSERT);
7651   if (*slot == NULL)
7652     {
7653       node = ggc_alloc_cleared_indirect_string_node ();
7654       node->str = ggc_strdup (str);
7655       *slot = node;
7656     }
7657   else
7658     node = (struct indirect_string_node *) *slot;
7659
7660   node->refcount++;
7661   return node;
7662 }
7663
7664 /* Add a string attribute value to a DIE.  */
7665
7666 static inline void
7667 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7668 {
7669   dw_attr_node attr;
7670   struct indirect_string_node *node;
7671
7672   node = find_AT_string (str);
7673
7674   attr.dw_attr = attr_kind;
7675   attr.dw_attr_val.val_class = dw_val_class_str;
7676   attr.dw_attr_val.v.val_str = node;
7677   add_dwarf_attr (die, &attr);
7678 }
7679
7680 static inline const char *
7681 AT_string (dw_attr_ref a)
7682 {
7683   gcc_assert (a && AT_class (a) == dw_val_class_str);
7684   return a->dw_attr_val.v.val_str->str;
7685 }
7686
7687 /* Find out whether a string should be output inline in DIE
7688    or out-of-line in .debug_str section.  */
7689
7690 static enum dwarf_form
7691 AT_string_form (dw_attr_ref a)
7692 {
7693   struct indirect_string_node *node;
7694   unsigned int len;
7695   char label[32];
7696
7697   gcc_assert (a && AT_class (a) == dw_val_class_str);
7698
7699   node = a->dw_attr_val.v.val_str;
7700   if (node->form)
7701     return node->form;
7702
7703   len = strlen (node->str) + 1;
7704
7705   /* If the string is shorter or equal to the size of the reference, it is
7706      always better to put it inline.  */
7707   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7708     return node->form = DW_FORM_string;
7709
7710   /* If we cannot expect the linker to merge strings in .debug_str
7711      section, only put it into .debug_str if it is worth even in this
7712      single module.  */
7713   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7714       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7715       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7716     return node->form = DW_FORM_string;
7717
7718   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7719   ++dw2_string_counter;
7720   node->label = xstrdup (label);
7721
7722   return node->form = DW_FORM_strp;
7723 }
7724
7725 /* Add a DIE reference attribute value to a DIE.  */
7726
7727 static inline void
7728 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7729 {
7730   dw_attr_node attr;
7731
7732 #ifdef ENABLE_CHECKING
7733   gcc_assert (targ_die != NULL);
7734 #else
7735   /* With LTO we can end up trying to reference something we didn't create
7736      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7737   if (targ_die == NULL)
7738     return;
7739 #endif
7740
7741   attr.dw_attr = attr_kind;
7742   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7743   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7744   attr.dw_attr_val.v.val_die_ref.external = 0;
7745   add_dwarf_attr (die, &attr);
7746 }
7747
7748 /* Add an AT_specification attribute to a DIE, and also make the back
7749    pointer from the specification to the definition.  */
7750
7751 static inline void
7752 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7753 {
7754   add_AT_die_ref (die, DW_AT_specification, targ_die);
7755   gcc_assert (!targ_die->die_definition);
7756   targ_die->die_definition = die;
7757 }
7758
7759 static inline dw_die_ref
7760 AT_ref (dw_attr_ref a)
7761 {
7762   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7763   return a->dw_attr_val.v.val_die_ref.die;
7764 }
7765
7766 static inline int
7767 AT_ref_external (dw_attr_ref a)
7768 {
7769   if (a && AT_class (a) == dw_val_class_die_ref)
7770     return a->dw_attr_val.v.val_die_ref.external;
7771
7772   return 0;
7773 }
7774
7775 static inline void
7776 set_AT_ref_external (dw_attr_ref a, int i)
7777 {
7778   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7779   a->dw_attr_val.v.val_die_ref.external = i;
7780 }
7781
7782 /* Add an FDE reference attribute value to a DIE.  */
7783
7784 static inline void
7785 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7786 {
7787   dw_attr_node attr;
7788
7789   attr.dw_attr = attr_kind;
7790   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7791   attr.dw_attr_val.v.val_fde_index = targ_fde;
7792   add_dwarf_attr (die, &attr);
7793 }
7794
7795 /* Add a location description attribute value to a DIE.  */
7796
7797 static inline void
7798 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7799 {
7800   dw_attr_node attr;
7801
7802   attr.dw_attr = attr_kind;
7803   attr.dw_attr_val.val_class = dw_val_class_loc;
7804   attr.dw_attr_val.v.val_loc = loc;
7805   add_dwarf_attr (die, &attr);
7806 }
7807
7808 static inline dw_loc_descr_ref
7809 AT_loc (dw_attr_ref a)
7810 {
7811   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7812   return a->dw_attr_val.v.val_loc;
7813 }
7814
7815 static inline void
7816 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7817 {
7818   dw_attr_node attr;
7819
7820   attr.dw_attr = attr_kind;
7821   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7822   attr.dw_attr_val.v.val_loc_list = loc_list;
7823   add_dwarf_attr (die, &attr);
7824   have_location_lists = true;
7825 }
7826
7827 static inline dw_loc_list_ref
7828 AT_loc_list (dw_attr_ref a)
7829 {
7830   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7831   return a->dw_attr_val.v.val_loc_list;
7832 }
7833
7834 static inline dw_loc_list_ref *
7835 AT_loc_list_ptr (dw_attr_ref a)
7836 {
7837   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7838   return &a->dw_attr_val.v.val_loc_list;
7839 }
7840
7841 /* Add an address constant attribute value to a DIE.  */
7842
7843 static inline void
7844 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7845 {
7846   dw_attr_node attr;
7847
7848   attr.dw_attr = attr_kind;
7849   attr.dw_attr_val.val_class = dw_val_class_addr;
7850   attr.dw_attr_val.v.val_addr = addr;
7851   add_dwarf_attr (die, &attr);
7852 }
7853
7854 /* Get the RTX from to an address DIE attribute.  */
7855
7856 static inline rtx
7857 AT_addr (dw_attr_ref a)
7858 {
7859   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7860   return a->dw_attr_val.v.val_addr;
7861 }
7862
7863 /* Add a file attribute value to a DIE.  */
7864
7865 static inline void
7866 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7867              struct dwarf_file_data *fd)
7868 {
7869   dw_attr_node attr;
7870
7871   attr.dw_attr = attr_kind;
7872   attr.dw_attr_val.val_class = dw_val_class_file;
7873   attr.dw_attr_val.v.val_file = fd;
7874   add_dwarf_attr (die, &attr);
7875 }
7876
7877 /* Get the dwarf_file_data from a file DIE attribute.  */
7878
7879 static inline struct dwarf_file_data *
7880 AT_file (dw_attr_ref a)
7881 {
7882   gcc_assert (a && AT_class (a) == dw_val_class_file);
7883   return a->dw_attr_val.v.val_file;
7884 }
7885
7886 /* Add a vms delta attribute value to a DIE.  */
7887
7888 static inline void
7889 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7890                   const char *lbl1, const char *lbl2)
7891 {
7892   dw_attr_node attr;
7893
7894   attr.dw_attr = attr_kind;
7895   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7896   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7897   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7898   add_dwarf_attr (die, &attr);
7899 }
7900
7901 /* Add a label identifier attribute value to a DIE.  */
7902
7903 static inline void
7904 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7905 {
7906   dw_attr_node attr;
7907
7908   attr.dw_attr = attr_kind;
7909   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7910   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7911   add_dwarf_attr (die, &attr);
7912 }
7913
7914 /* Add a section offset attribute value to a DIE, an offset into the
7915    debug_line section.  */
7916
7917 static inline void
7918 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7919                 const char *label)
7920 {
7921   dw_attr_node attr;
7922
7923   attr.dw_attr = attr_kind;
7924   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7925   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7926   add_dwarf_attr (die, &attr);
7927 }
7928
7929 /* Add a section offset attribute value to a DIE, an offset into the
7930    debug_macinfo section.  */
7931
7932 static inline void
7933 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7934                const char *label)
7935 {
7936   dw_attr_node attr;
7937
7938   attr.dw_attr = attr_kind;
7939   attr.dw_attr_val.val_class = dw_val_class_macptr;
7940   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7941   add_dwarf_attr (die, &attr);
7942 }
7943
7944 /* Add an offset attribute value to a DIE.  */
7945
7946 static inline void
7947 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7948                unsigned HOST_WIDE_INT offset)
7949 {
7950   dw_attr_node attr;
7951
7952   attr.dw_attr = attr_kind;
7953   attr.dw_attr_val.val_class = dw_val_class_offset;
7954   attr.dw_attr_val.v.val_offset = offset;
7955   add_dwarf_attr (die, &attr);
7956 }
7957
7958 /* Add an range_list attribute value to a DIE.  */
7959
7960 static void
7961 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7962                    long unsigned int offset)
7963 {
7964   dw_attr_node attr;
7965
7966   attr.dw_attr = attr_kind;
7967   attr.dw_attr_val.val_class = dw_val_class_range_list;
7968   attr.dw_attr_val.v.val_offset = offset;
7969   add_dwarf_attr (die, &attr);
7970 }
7971
7972 /* Return the start label of a delta attribute.  */
7973
7974 static inline const char *
7975 AT_vms_delta1 (dw_attr_ref a)
7976 {
7977   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7978   return a->dw_attr_val.v.val_vms_delta.lbl1;
7979 }
7980
7981 /* Return the end label of a delta attribute.  */
7982
7983 static inline const char *
7984 AT_vms_delta2 (dw_attr_ref a)
7985 {
7986   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7987   return a->dw_attr_val.v.val_vms_delta.lbl2;
7988 }
7989
7990 static inline const char *
7991 AT_lbl (dw_attr_ref a)
7992 {
7993   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7994                     || AT_class (a) == dw_val_class_lineptr
7995                     || AT_class (a) == dw_val_class_macptr));
7996   return a->dw_attr_val.v.val_lbl_id;
7997 }
7998
7999 /* Get the attribute of type attr_kind.  */
8000
8001 static dw_attr_ref
8002 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8003 {
8004   dw_attr_ref a;
8005   unsigned ix;
8006   dw_die_ref spec = NULL;
8007
8008   if (! die)
8009     return NULL;
8010
8011   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8012     if (a->dw_attr == attr_kind)
8013       return a;
8014     else if (a->dw_attr == DW_AT_specification
8015              || a->dw_attr == DW_AT_abstract_origin)
8016       spec = AT_ref (a);
8017
8018   if (spec)
8019     return get_AT (spec, attr_kind);
8020
8021   return NULL;
8022 }
8023
8024 /* Return the "low pc" attribute value, typically associated with a subprogram
8025    DIE.  Return null if the "low pc" attribute is either not present, or if it
8026    cannot be represented as an assembler label identifier.  */
8027
8028 static inline const char *
8029 get_AT_low_pc (dw_die_ref die)
8030 {
8031   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
8032
8033   return a ? AT_lbl (a) : NULL;
8034 }
8035
8036 /* Return the "high pc" attribute value, typically associated with a subprogram
8037    DIE.  Return null if the "high pc" attribute is either not present, or if it
8038    cannot be represented as an assembler label identifier.  */
8039
8040 static inline const char *
8041 get_AT_hi_pc (dw_die_ref die)
8042 {
8043   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
8044
8045   return a ? AT_lbl (a) : NULL;
8046 }
8047
8048 /* Return the value of the string attribute designated by ATTR_KIND, or
8049    NULL if it is not present.  */
8050
8051 static inline const char *
8052 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
8053 {
8054   dw_attr_ref a = get_AT (die, attr_kind);
8055
8056   return a ? AT_string (a) : NULL;
8057 }
8058
8059 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
8060    if it is not present.  */
8061
8062 static inline int
8063 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
8064 {
8065   dw_attr_ref a = get_AT (die, attr_kind);
8066
8067   return a ? AT_flag (a) : 0;
8068 }
8069
8070 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
8071    if it is not present.  */
8072
8073 static inline unsigned
8074 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
8075 {
8076   dw_attr_ref a = get_AT (die, attr_kind);
8077
8078   return a ? AT_unsigned (a) : 0;
8079 }
8080
8081 static inline dw_die_ref
8082 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
8083 {
8084   dw_attr_ref a = get_AT (die, attr_kind);
8085
8086   return a ? AT_ref (a) : NULL;
8087 }
8088
8089 static inline struct dwarf_file_data *
8090 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
8091 {
8092   dw_attr_ref a = get_AT (die, attr_kind);
8093
8094   return a ? AT_file (a) : NULL;
8095 }
8096
8097 /* Return TRUE if the language is C++.  */
8098
8099 static inline bool
8100 is_cxx (void)
8101 {
8102   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8103
8104   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
8105 }
8106
8107 /* Return TRUE if the language is Fortran.  */
8108
8109 static inline bool
8110 is_fortran (void)
8111 {
8112   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8113
8114   return (lang == DW_LANG_Fortran77
8115           || lang == DW_LANG_Fortran90
8116           || lang == DW_LANG_Fortran95);
8117 }
8118
8119 /* Return TRUE if the language is Ada.  */
8120
8121 static inline bool
8122 is_ada (void)
8123 {
8124   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8125
8126   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
8127 }
8128
8129 /* Remove the specified attribute if present.  */
8130
8131 static void
8132 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8133 {
8134   dw_attr_ref a;
8135   unsigned ix;
8136
8137   if (! die)
8138     return;
8139
8140   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8141     if (a->dw_attr == attr_kind)
8142       {
8143         if (AT_class (a) == dw_val_class_str)
8144           if (a->dw_attr_val.v.val_str->refcount)
8145             a->dw_attr_val.v.val_str->refcount--;
8146
8147         /* VEC_ordered_remove should help reduce the number of abbrevs
8148            that are needed.  */
8149         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
8150         return;
8151       }
8152 }
8153
8154 /* Remove CHILD from its parent.  PREV must have the property that
8155    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
8156
8157 static void
8158 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
8159 {
8160   gcc_assert (child->die_parent == prev->die_parent);
8161   gcc_assert (prev->die_sib == child);
8162   if (prev == child)
8163     {
8164       gcc_assert (child->die_parent->die_child == child);
8165       prev = NULL;
8166     }
8167   else
8168     prev->die_sib = child->die_sib;
8169   if (child->die_parent->die_child == child)
8170     child->die_parent->die_child = prev;
8171 }
8172
8173 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
8174    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
8175
8176 static void
8177 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
8178 {
8179   dw_die_ref parent = old_child->die_parent;
8180
8181   gcc_assert (parent == prev->die_parent);
8182   gcc_assert (prev->die_sib == old_child);
8183
8184   new_child->die_parent = parent;
8185   if (prev == old_child)
8186     {
8187       gcc_assert (parent->die_child == old_child);
8188       new_child->die_sib = new_child;
8189     }
8190   else
8191     {
8192       prev->die_sib = new_child;
8193       new_child->die_sib = old_child->die_sib;
8194     }
8195   if (old_child->die_parent->die_child == old_child)
8196     old_child->die_parent->die_child = new_child;
8197 }
8198
8199 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8200
8201 static void
8202 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8203 {
8204   dw_die_ref c;
8205   new_parent->die_child = old_parent->die_child;
8206   old_parent->die_child = NULL;
8207   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8208 }
8209
8210 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8211    matches TAG.  */
8212
8213 static void
8214 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8215 {
8216   dw_die_ref c;
8217
8218   c = die->die_child;
8219   if (c) do {
8220     dw_die_ref prev = c;
8221     c = c->die_sib;
8222     while (c->die_tag == tag)
8223       {
8224         remove_child_with_prev (c, prev);
8225         /* Might have removed every child.  */
8226         if (c == c->die_sib)
8227           return;
8228         c = c->die_sib;
8229       }
8230   } while (c != die->die_child);
8231 }
8232
8233 /* Add a CHILD_DIE as the last child of DIE.  */
8234
8235 static void
8236 add_child_die (dw_die_ref die, dw_die_ref child_die)
8237 {
8238   /* FIXME this should probably be an assert.  */
8239   if (! die || ! child_die)
8240     return;
8241   gcc_assert (die != child_die);
8242
8243   child_die->die_parent = die;
8244   if (die->die_child)
8245     {
8246       child_die->die_sib = die->die_child->die_sib;
8247       die->die_child->die_sib = child_die;
8248     }
8249   else
8250     child_die->die_sib = child_die;
8251   die->die_child = child_die;
8252 }
8253
8254 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8255    is the specification, to the end of PARENT's list of children.
8256    This is done by removing and re-adding it.  */
8257
8258 static void
8259 splice_child_die (dw_die_ref parent, dw_die_ref child)
8260 {
8261   dw_die_ref p;
8262
8263   /* We want the declaration DIE from inside the class, not the
8264      specification DIE at toplevel.  */
8265   if (child->die_parent != parent)
8266     {
8267       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8268
8269       if (tmp)
8270         child = tmp;
8271     }
8272
8273   gcc_assert (child->die_parent == parent
8274               || (child->die_parent
8275                   == get_AT_ref (parent, DW_AT_specification)));
8276
8277   for (p = child->die_parent->die_child; ; p = p->die_sib)
8278     if (p->die_sib == child)
8279       {
8280         remove_child_with_prev (child, p);
8281         break;
8282       }
8283
8284   add_child_die (parent, child);
8285 }
8286
8287 /* Return a pointer to a newly created DIE node.  */
8288
8289 static inline dw_die_ref
8290 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8291 {
8292   dw_die_ref die = ggc_alloc_cleared_die_node ();
8293
8294   die->die_tag = tag_value;
8295
8296   if (parent_die != NULL)
8297     add_child_die (parent_die, die);
8298   else
8299     {
8300       limbo_die_node *limbo_node;
8301
8302       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8303       limbo_node->die = die;
8304       limbo_node->created_for = t;
8305       limbo_node->next = limbo_die_list;
8306       limbo_die_list = limbo_node;
8307     }
8308
8309   return die;
8310 }
8311
8312 /* Return the DIE associated with the given type specifier.  */
8313
8314 static inline dw_die_ref
8315 lookup_type_die (tree type)
8316 {
8317   return TYPE_SYMTAB_DIE (type);
8318 }
8319
8320 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8321    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8322    anonymous type instead the one of the naming typedef.  */
8323
8324 static inline dw_die_ref
8325 strip_naming_typedef (tree type, dw_die_ref type_die)
8326 {
8327   if (type
8328       && TREE_CODE (type) == RECORD_TYPE
8329       && type_die
8330       && type_die->die_tag == DW_TAG_typedef
8331       && is_naming_typedef_decl (TYPE_NAME (type)))
8332     type_die = get_AT_ref (type_die, DW_AT_type);
8333   return type_die;
8334 }
8335
8336 /* Like lookup_type_die, but if type is an anonymous type named by a
8337    typedef[1], return the DIE of the anonymous type instead the one of
8338    the naming typedef.  This is because in gen_typedef_die, we did
8339    equate the anonymous struct named by the typedef with the DIE of
8340    the naming typedef. So by default, lookup_type_die on an anonymous
8341    struct yields the DIE of the naming typedef.
8342
8343    [1]: Read the comment of is_naming_typedef_decl to learn about what
8344    a naming typedef is.  */
8345
8346 static inline dw_die_ref
8347 lookup_type_die_strip_naming_typedef (tree type)
8348 {
8349   dw_die_ref die = lookup_type_die (type);
8350   return strip_naming_typedef (type, die);
8351 }
8352
8353 /* Equate a DIE to a given type specifier.  */
8354
8355 static inline void
8356 equate_type_number_to_die (tree type, dw_die_ref type_die)
8357 {
8358   TYPE_SYMTAB_DIE (type) = type_die;
8359 }
8360
8361 /* Returns a hash value for X (which really is a die_struct).  */
8362
8363 static hashval_t
8364 decl_die_table_hash (const void *x)
8365 {
8366   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8367 }
8368
8369 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8370
8371 static int
8372 decl_die_table_eq (const void *x, const void *y)
8373 {
8374   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8375 }
8376
8377 /* Return the DIE associated with a given declaration.  */
8378
8379 static inline dw_die_ref
8380 lookup_decl_die (tree decl)
8381 {
8382   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8383 }
8384
8385 /* Returns a hash value for X (which really is a var_loc_list).  */
8386
8387 static hashval_t
8388 decl_loc_table_hash (const void *x)
8389 {
8390   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8391 }
8392
8393 /* Return nonzero if decl_id of var_loc_list X is the same as
8394    UID of decl *Y.  */
8395
8396 static int
8397 decl_loc_table_eq (const void *x, const void *y)
8398 {
8399   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8400 }
8401
8402 /* Return the var_loc list associated with a given declaration.  */
8403
8404 static inline var_loc_list *
8405 lookup_decl_loc (const_tree decl)
8406 {
8407   if (!decl_loc_table)
8408     return NULL;
8409   return (var_loc_list *)
8410     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8411 }
8412
8413 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
8414
8415 static hashval_t
8416 cached_dw_loc_list_table_hash (const void *x)
8417 {
8418   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8419 }
8420
8421 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8422    UID of decl *Y.  */
8423
8424 static int
8425 cached_dw_loc_list_table_eq (const void *x, const void *y)
8426 {
8427   return (((const cached_dw_loc_list *) x)->decl_id
8428           == DECL_UID ((const_tree) y));
8429 }
8430
8431 /* Equate a DIE to a particular declaration.  */
8432
8433 static void
8434 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8435 {
8436   unsigned int decl_id = DECL_UID (decl);
8437   void **slot;
8438
8439   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8440   *slot = decl_die;
8441   decl_die->decl_id = decl_id;
8442 }
8443
8444 /* Return how many bits covers PIECE EXPR_LIST.  */
8445
8446 static int
8447 decl_piece_bitsize (rtx piece)
8448 {
8449   int ret = (int) GET_MODE (piece);
8450   if (ret)
8451     return ret;
8452   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8453               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8454   return INTVAL (XEXP (XEXP (piece, 0), 0));
8455 }
8456
8457 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8458
8459 static rtx *
8460 decl_piece_varloc_ptr (rtx piece)
8461 {
8462   if ((int) GET_MODE (piece))
8463     return &XEXP (piece, 0);
8464   else
8465     return &XEXP (XEXP (piece, 0), 1);
8466 }
8467
8468 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8469    Next is the chain of following piece nodes.  */
8470
8471 static rtx
8472 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8473 {
8474   if (bitsize <= (int) MAX_MACHINE_MODE)
8475     return alloc_EXPR_LIST (bitsize, loc_note, next);
8476   else
8477     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8478                                                GEN_INT (bitsize),
8479                                                loc_note), next);
8480 }
8481
8482 /* Return rtx that should be stored into loc field for
8483    LOC_NOTE and BITPOS/BITSIZE.  */
8484
8485 static rtx
8486 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8487                       HOST_WIDE_INT bitsize)
8488 {
8489   if (bitsize != -1)
8490     {
8491       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8492       if (bitpos != 0)
8493         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8494     }
8495   return loc_note;
8496 }
8497
8498 /* This function either modifies location piece list *DEST in
8499    place (if SRC and INNER is NULL), or copies location piece list
8500    *SRC to *DEST while modifying it.  Location BITPOS is modified
8501    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8502    not copied and if needed some padding around it is added.
8503    When modifying in place, DEST should point to EXPR_LIST where
8504    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8505    to the start of the whole list and INNER points to the EXPR_LIST
8506    where earlier pieces cover PIECE_BITPOS bits.  */
8507
8508 static void
8509 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8510                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8511                    HOST_WIDE_INT bitsize, rtx loc_note)
8512 {
8513   int diff;
8514   bool copy = inner != NULL;
8515
8516   if (copy)
8517     {
8518       /* First copy all nodes preceeding the current bitpos.  */
8519       while (src != inner)
8520         {
8521           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8522                                    decl_piece_bitsize (*src), NULL_RTX);
8523           dest = &XEXP (*dest, 1);
8524           src = &XEXP (*src, 1);
8525         }
8526     }
8527   /* Add padding if needed.  */
8528   if (bitpos != piece_bitpos)
8529     {
8530       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8531                                copy ? NULL_RTX : *dest);
8532       dest = &XEXP (*dest, 1);
8533     }
8534   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8535     {
8536       gcc_assert (!copy);
8537       /* A piece with correct bitpos and bitsize already exist,
8538          just update the location for it and return.  */
8539       *decl_piece_varloc_ptr (*dest) = loc_note;
8540       return;
8541     }
8542   /* Add the piece that changed.  */
8543   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8544   dest = &XEXP (*dest, 1);
8545   /* Skip over pieces that overlap it.  */
8546   diff = bitpos - piece_bitpos + bitsize;
8547   if (!copy)
8548     src = dest;
8549   while (diff > 0 && *src)
8550     {
8551       rtx piece = *src;
8552       diff -= decl_piece_bitsize (piece);
8553       if (copy)
8554         src = &XEXP (piece, 1);
8555       else
8556         {
8557           *src = XEXP (piece, 1);
8558           free_EXPR_LIST_node (piece);
8559         }
8560     }
8561   /* Add padding if needed.  */
8562   if (diff < 0 && *src)
8563     {
8564       if (!copy)
8565         dest = src;
8566       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8567       dest = &XEXP (*dest, 1);
8568     }
8569   if (!copy)
8570     return;
8571   /* Finally copy all nodes following it.  */
8572   while (*src)
8573     {
8574       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8575                                decl_piece_bitsize (*src), NULL_RTX);
8576       dest = &XEXP (*dest, 1);
8577       src = &XEXP (*src, 1);
8578     }
8579 }
8580
8581 /* Add a variable location node to the linked list for DECL.  */
8582
8583 static struct var_loc_node *
8584 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8585 {
8586   unsigned int decl_id;
8587   var_loc_list *temp;
8588   void **slot;
8589   struct var_loc_node *loc = NULL;
8590   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8591
8592   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8593     {
8594       tree realdecl = DECL_DEBUG_EXPR (decl);
8595       if (realdecl && handled_component_p (realdecl))
8596         {
8597           HOST_WIDE_INT maxsize;
8598           tree innerdecl;
8599           innerdecl
8600             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8601           if (!DECL_P (innerdecl)
8602               || DECL_IGNORED_P (innerdecl)
8603               || TREE_STATIC (innerdecl)
8604               || bitsize <= 0
8605               || bitpos + bitsize > 256
8606               || bitsize != maxsize)
8607             return NULL;
8608           decl = innerdecl;
8609         }
8610     }
8611
8612   decl_id = DECL_UID (decl);
8613   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8614   if (*slot == NULL)
8615     {
8616       temp = ggc_alloc_cleared_var_loc_list ();
8617       temp->decl_id = decl_id;
8618       *slot = temp;
8619     }
8620   else
8621     temp = (var_loc_list *) *slot;
8622
8623   if (temp->last)
8624     {
8625       struct var_loc_node *last = temp->last, *unused = NULL;
8626       rtx *piece_loc = NULL, last_loc_note;
8627       int piece_bitpos = 0;
8628       if (last->next)
8629         {
8630           last = last->next;
8631           gcc_assert (last->next == NULL);
8632         }
8633       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8634         {
8635           piece_loc = &last->loc;
8636           do
8637             {
8638               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8639               if (piece_bitpos + cur_bitsize > bitpos)
8640                 break;
8641               piece_bitpos += cur_bitsize;
8642               piece_loc = &XEXP (*piece_loc, 1);
8643             }
8644           while (*piece_loc);
8645         }
8646       /* TEMP->LAST here is either pointer to the last but one or
8647          last element in the chained list, LAST is pointer to the
8648          last element.  */
8649       if (label && strcmp (last->label, label) == 0)
8650         {
8651           /* For SRA optimized variables if there weren't any real
8652              insns since last note, just modify the last node.  */
8653           if (piece_loc != NULL)
8654             {
8655               adjust_piece_list (piece_loc, NULL, NULL,
8656                                  bitpos, piece_bitpos, bitsize, loc_note);
8657               return NULL;
8658             }
8659           /* If the last note doesn't cover any instructions, remove it.  */
8660           if (temp->last != last)
8661             {
8662               temp->last->next = NULL;
8663               unused = last;
8664               last = temp->last;
8665               gcc_assert (strcmp (last->label, label) != 0);
8666             }
8667           else
8668             {
8669               gcc_assert (temp->first == temp->last);
8670               memset (temp->last, '\0', sizeof (*temp->last));
8671               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8672               return temp->last;
8673             }
8674         }
8675       if (bitsize == -1 && NOTE_P (last->loc))
8676         last_loc_note = last->loc;
8677       else if (piece_loc != NULL
8678                && *piece_loc != NULL_RTX
8679                && piece_bitpos == bitpos
8680                && decl_piece_bitsize (*piece_loc) == bitsize)
8681         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8682       else
8683         last_loc_note = NULL_RTX;
8684       /* If the current location is the same as the end of the list,
8685          and either both or neither of the locations is uninitialized,
8686          we have nothing to do.  */
8687       if (last_loc_note == NULL_RTX
8688           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8689                             NOTE_VAR_LOCATION_LOC (loc_note)))
8690           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8691                != NOTE_VAR_LOCATION_STATUS (loc_note))
8692               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8693                    == VAR_INIT_STATUS_UNINITIALIZED)
8694                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8695                       == VAR_INIT_STATUS_UNINITIALIZED))))
8696         {
8697           /* Add LOC to the end of list and update LAST.  If the last
8698              element of the list has been removed above, reuse its
8699              memory for the new node, otherwise allocate a new one.  */
8700           if (unused)
8701             {
8702               loc = unused;
8703               memset (loc, '\0', sizeof (*loc));
8704             }
8705           else
8706             loc = ggc_alloc_cleared_var_loc_node ();
8707           if (bitsize == -1 || piece_loc == NULL)
8708             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8709           else
8710             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8711                                bitpos, piece_bitpos, bitsize, loc_note);
8712           last->next = loc;
8713           /* Ensure TEMP->LAST will point either to the new last but one
8714              element of the chain, or to the last element in it.  */
8715           if (last != temp->last)
8716             temp->last = last;
8717         }
8718       else if (unused)
8719         ggc_free (unused);
8720     }
8721   else
8722     {
8723       loc = ggc_alloc_cleared_var_loc_node ();
8724       temp->first = loc;
8725       temp->last = loc;
8726       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8727     }
8728   return loc;
8729 }
8730 \f
8731 /* Keep track of the number of spaces used to indent the
8732    output of the debugging routines that print the structure of
8733    the DIE internal representation.  */
8734 static int print_indent;
8735
8736 /* Indent the line the number of spaces given by print_indent.  */
8737
8738 static inline void
8739 print_spaces (FILE *outfile)
8740 {
8741   fprintf (outfile, "%*s", print_indent, "");
8742 }
8743
8744 /* Print a type signature in hex.  */
8745
8746 static inline void
8747 print_signature (FILE *outfile, char *sig)
8748 {
8749   int i;
8750
8751   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8752     fprintf (outfile, "%02x", sig[i] & 0xff);
8753 }
8754
8755 /* Print the information associated with a given DIE, and its children.
8756    This routine is a debugging aid only.  */
8757
8758 static void
8759 print_die (dw_die_ref die, FILE *outfile)
8760 {
8761   dw_attr_ref a;
8762   dw_die_ref c;
8763   unsigned ix;
8764
8765   print_spaces (outfile);
8766   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8767            die->die_offset, dwarf_tag_name (die->die_tag),
8768            (void*) die);
8769   print_spaces (outfile);
8770   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8771   fprintf (outfile, " offset: %ld", die->die_offset);
8772   fprintf (outfile, " mark: %d\n", die->die_mark);
8773
8774   if (use_debug_types && die->die_id.die_type_node)
8775     {
8776       print_spaces (outfile);
8777       fprintf (outfile, "  signature: ");
8778       print_signature (outfile, die->die_id.die_type_node->signature);
8779       fprintf (outfile, "\n");
8780     }
8781
8782   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8783     {
8784       print_spaces (outfile);
8785       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8786
8787       switch (AT_class (a))
8788         {
8789         case dw_val_class_addr:
8790           fprintf (outfile, "address");
8791           break;
8792         case dw_val_class_offset:
8793           fprintf (outfile, "offset");
8794           break;
8795         case dw_val_class_loc:
8796           fprintf (outfile, "location descriptor");
8797           break;
8798         case dw_val_class_loc_list:
8799           fprintf (outfile, "location list -> label:%s",
8800                    AT_loc_list (a)->ll_symbol);
8801           break;
8802         case dw_val_class_range_list:
8803           fprintf (outfile, "range list");
8804           break;
8805         case dw_val_class_const:
8806           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8807           break;
8808         case dw_val_class_unsigned_const:
8809           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8810           break;
8811         case dw_val_class_const_double:
8812           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8813                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8814                    a->dw_attr_val.v.val_double.high,
8815                    a->dw_attr_val.v.val_double.low);
8816           break;
8817         case dw_val_class_vec:
8818           fprintf (outfile, "floating-point or vector constant");
8819           break;
8820         case dw_val_class_flag:
8821           fprintf (outfile, "%u", AT_flag (a));
8822           break;
8823         case dw_val_class_die_ref:
8824           if (AT_ref (a) != NULL)
8825             {
8826               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
8827                 {
8828                   fprintf (outfile, "die -> signature: ");
8829                   print_signature (outfile,
8830                                    AT_ref (a)->die_id.die_type_node->signature);
8831                 }
8832               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
8833                 fprintf (outfile, "die -> label: %s",
8834                          AT_ref (a)->die_id.die_symbol);
8835               else
8836                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8837               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8838             }
8839           else
8840             fprintf (outfile, "die -> <null>");
8841           break;
8842         case dw_val_class_vms_delta:
8843           fprintf (outfile, "delta: @slotcount(%s-%s)",
8844                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8845           break;
8846         case dw_val_class_lbl_id:
8847         case dw_val_class_lineptr:
8848         case dw_val_class_macptr:
8849           fprintf (outfile, "label: %s", AT_lbl (a));
8850           break;
8851         case dw_val_class_str:
8852           if (AT_string (a) != NULL)
8853             fprintf (outfile, "\"%s\"", AT_string (a));
8854           else
8855             fprintf (outfile, "<null>");
8856           break;
8857         case dw_val_class_file:
8858           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8859                    AT_file (a)->emitted_number);
8860           break;
8861         case dw_val_class_data8:
8862           {
8863             int i;
8864
8865             for (i = 0; i < 8; i++)
8866               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8867             break;
8868           }
8869         default:
8870           break;
8871         }
8872
8873       fprintf (outfile, "\n");
8874     }
8875
8876   if (die->die_child != NULL)
8877     {
8878       print_indent += 4;
8879       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8880       print_indent -= 4;
8881     }
8882   if (print_indent == 0)
8883     fprintf (outfile, "\n");
8884 }
8885
8886 /* Print the information collected for a given DIE.  */
8887
8888 DEBUG_FUNCTION void
8889 debug_dwarf_die (dw_die_ref die)
8890 {
8891   print_die (die, stderr);
8892 }
8893
8894 /* Print all DWARF information collected for the compilation unit.
8895    This routine is a debugging aid only.  */
8896
8897 DEBUG_FUNCTION void
8898 debug_dwarf (void)
8899 {
8900   print_indent = 0;
8901   print_die (comp_unit_die (), stderr);
8902 }
8903 \f
8904 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8905    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8906    DIE that marks the start of the DIEs for this include file.  */
8907
8908 static dw_die_ref
8909 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8910 {
8911   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8912   dw_die_ref new_unit = gen_compile_unit_die (filename);
8913
8914   new_unit->die_sib = old_unit;
8915   return new_unit;
8916 }
8917
8918 /* Close an include-file CU and reopen the enclosing one.  */
8919
8920 static dw_die_ref
8921 pop_compile_unit (dw_die_ref old_unit)
8922 {
8923   dw_die_ref new_unit = old_unit->die_sib;
8924
8925   old_unit->die_sib = NULL;
8926   return new_unit;
8927 }
8928
8929 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8930 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8931
8932 /* Calculate the checksum of a location expression.  */
8933
8934 static inline void
8935 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8936 {
8937   int tem;
8938
8939   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8940   CHECKSUM (tem);
8941   CHECKSUM (loc->dw_loc_oprnd1);
8942   CHECKSUM (loc->dw_loc_oprnd2);
8943 }
8944
8945 /* Calculate the checksum of an attribute.  */
8946
8947 static void
8948 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8949 {
8950   dw_loc_descr_ref loc;
8951   rtx r;
8952
8953   CHECKSUM (at->dw_attr);
8954
8955   /* We don't care that this was compiled with a different compiler
8956      snapshot; if the output is the same, that's what matters.  */
8957   if (at->dw_attr == DW_AT_producer)
8958     return;
8959
8960   switch (AT_class (at))
8961     {
8962     case dw_val_class_const:
8963       CHECKSUM (at->dw_attr_val.v.val_int);
8964       break;
8965     case dw_val_class_unsigned_const:
8966       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8967       break;
8968     case dw_val_class_const_double:
8969       CHECKSUM (at->dw_attr_val.v.val_double);
8970       break;
8971     case dw_val_class_vec:
8972       CHECKSUM (at->dw_attr_val.v.val_vec);
8973       break;
8974     case dw_val_class_flag:
8975       CHECKSUM (at->dw_attr_val.v.val_flag);
8976       break;
8977     case dw_val_class_str:
8978       CHECKSUM_STRING (AT_string (at));
8979       break;
8980
8981     case dw_val_class_addr:
8982       r = AT_addr (at);
8983       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8984       CHECKSUM_STRING (XSTR (r, 0));
8985       break;
8986
8987     case dw_val_class_offset:
8988       CHECKSUM (at->dw_attr_val.v.val_offset);
8989       break;
8990
8991     case dw_val_class_loc:
8992       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8993         loc_checksum (loc, ctx);
8994       break;
8995
8996     case dw_val_class_die_ref:
8997       die_checksum (AT_ref (at), ctx, mark);
8998       break;
8999
9000     case dw_val_class_fde_ref:
9001     case dw_val_class_vms_delta:
9002     case dw_val_class_lbl_id:
9003     case dw_val_class_lineptr:
9004     case dw_val_class_macptr:
9005       break;
9006
9007     case dw_val_class_file:
9008       CHECKSUM_STRING (AT_file (at)->filename);
9009       break;
9010
9011     case dw_val_class_data8:
9012       CHECKSUM (at->dw_attr_val.v.val_data8);
9013       break;
9014
9015     default:
9016       break;
9017     }
9018 }
9019
9020 /* Calculate the checksum of a DIE.  */
9021
9022 static void
9023 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9024 {
9025   dw_die_ref c;
9026   dw_attr_ref a;
9027   unsigned ix;
9028
9029   /* To avoid infinite recursion.  */
9030   if (die->die_mark)
9031     {
9032       CHECKSUM (die->die_mark);
9033       return;
9034     }
9035   die->die_mark = ++(*mark);
9036
9037   CHECKSUM (die->die_tag);
9038
9039   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9040     attr_checksum (a, ctx, mark);
9041
9042   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
9043 }
9044
9045 #undef CHECKSUM
9046 #undef CHECKSUM_STRING
9047
9048 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
9049 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
9050 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
9051 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
9052 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
9053 #define CHECKSUM_ATTR(FOO) \
9054   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
9055
9056 /* Calculate the checksum of a number in signed LEB128 format.  */
9057
9058 static void
9059 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
9060 {
9061   unsigned char byte;
9062   bool more;
9063
9064   while (1)
9065     {
9066       byte = (value & 0x7f);
9067       value >>= 7;
9068       more = !((value == 0 && (byte & 0x40) == 0)
9069                 || (value == -1 && (byte & 0x40) != 0));
9070       if (more)
9071         byte |= 0x80;
9072       CHECKSUM (byte);
9073       if (!more)
9074         break;
9075     }
9076 }
9077
9078 /* Calculate the checksum of a number in unsigned LEB128 format.  */
9079
9080 static void
9081 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
9082 {
9083   while (1)
9084     {
9085       unsigned char byte = (value & 0x7f);
9086       value >>= 7;
9087       if (value != 0)
9088         /* More bytes to follow.  */
9089         byte |= 0x80;
9090       CHECKSUM (byte);
9091       if (value == 0)
9092         break;
9093     }
9094 }
9095
9096 /* Checksum the context of the DIE.  This adds the names of any
9097    surrounding namespaces or structures to the checksum.  */
9098
9099 static void
9100 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
9101 {
9102   const char *name;
9103   dw_die_ref spec;
9104   int tag = die->die_tag;
9105
9106   if (tag != DW_TAG_namespace
9107       && tag != DW_TAG_structure_type
9108       && tag != DW_TAG_class_type)
9109     return;
9110
9111   name = get_AT_string (die, DW_AT_name);
9112
9113   spec = get_AT_ref (die, DW_AT_specification);
9114   if (spec != NULL)
9115     die = spec;
9116
9117   if (die->die_parent != NULL)
9118     checksum_die_context (die->die_parent, ctx);
9119
9120   CHECKSUM_ULEB128 ('C');
9121   CHECKSUM_ULEB128 (tag);
9122   if (name != NULL)
9123     CHECKSUM_STRING (name);
9124 }
9125
9126 /* Calculate the checksum of a location expression.  */
9127
9128 static inline void
9129 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
9130 {
9131   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
9132      were emitted as a DW_FORM_sdata instead of a location expression.  */
9133   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
9134     {
9135       CHECKSUM_ULEB128 (DW_FORM_sdata);
9136       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
9137       return;
9138     }
9139
9140   /* Otherwise, just checksum the raw location expression.  */
9141   while (loc != NULL)
9142     {
9143       CHECKSUM_ULEB128 (loc->dw_loc_opc);
9144       CHECKSUM (loc->dw_loc_oprnd1);
9145       CHECKSUM (loc->dw_loc_oprnd2);
9146       loc = loc->dw_loc_next;
9147     }
9148 }
9149
9150 /* Calculate the checksum of an attribute.  */
9151
9152 static void
9153 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
9154                        struct md5_ctx *ctx, int *mark)
9155 {
9156   dw_loc_descr_ref loc;
9157   rtx r;
9158
9159   if (AT_class (at) == dw_val_class_die_ref)
9160     {
9161       dw_die_ref target_die = AT_ref (at);
9162
9163       /* For pointer and reference types, we checksum only the (qualified)
9164          name of the target type (if there is a name).  For friend entries,
9165          we checksum only the (qualified) name of the target type or function.
9166          This allows the checksum to remain the same whether the target type
9167          is complete or not.  */
9168       if ((at->dw_attr == DW_AT_type
9169            && (tag == DW_TAG_pointer_type
9170                || tag == DW_TAG_reference_type
9171                || tag == DW_TAG_rvalue_reference_type
9172                || tag == DW_TAG_ptr_to_member_type))
9173           || (at->dw_attr == DW_AT_friend
9174               && tag == DW_TAG_friend))
9175         {
9176           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9177
9178           if (name_attr != NULL)
9179             {
9180               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9181
9182               if (decl == NULL)
9183                 decl = target_die;
9184               CHECKSUM_ULEB128 ('N');
9185               CHECKSUM_ULEB128 (at->dw_attr);
9186               if (decl->die_parent != NULL)
9187                 checksum_die_context (decl->die_parent, ctx);
9188               CHECKSUM_ULEB128 ('E');
9189               CHECKSUM_STRING (AT_string (name_attr));
9190               return;
9191             }
9192         }
9193
9194       /* For all other references to another DIE, we check to see if the
9195          target DIE has already been visited.  If it has, we emit a
9196          backward reference; if not, we descend recursively.  */
9197       if (target_die->die_mark > 0)
9198         {
9199           CHECKSUM_ULEB128 ('R');
9200           CHECKSUM_ULEB128 (at->dw_attr);
9201           CHECKSUM_ULEB128 (target_die->die_mark);
9202         }
9203       else
9204         {
9205           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9206
9207           if (decl == NULL)
9208             decl = target_die;
9209           target_die->die_mark = ++(*mark);
9210           CHECKSUM_ULEB128 ('T');
9211           CHECKSUM_ULEB128 (at->dw_attr);
9212           if (decl->die_parent != NULL)
9213             checksum_die_context (decl->die_parent, ctx);
9214           die_checksum_ordered (target_die, ctx, mark);
9215         }
9216       return;
9217     }
9218
9219   CHECKSUM_ULEB128 ('A');
9220   CHECKSUM_ULEB128 (at->dw_attr);
9221
9222   switch (AT_class (at))
9223     {
9224     case dw_val_class_const:
9225       CHECKSUM_ULEB128 (DW_FORM_sdata);
9226       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9227       break;
9228
9229     case dw_val_class_unsigned_const:
9230       CHECKSUM_ULEB128 (DW_FORM_sdata);
9231       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9232       break;
9233
9234     case dw_val_class_const_double:
9235       CHECKSUM_ULEB128 (DW_FORM_block);
9236       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9237       CHECKSUM (at->dw_attr_val.v.val_double);
9238       break;
9239
9240     case dw_val_class_vec:
9241       CHECKSUM_ULEB128 (DW_FORM_block);
9242       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9243       CHECKSUM (at->dw_attr_val.v.val_vec);
9244       break;
9245
9246     case dw_val_class_flag:
9247       CHECKSUM_ULEB128 (DW_FORM_flag);
9248       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9249       break;
9250
9251     case dw_val_class_str:
9252       CHECKSUM_ULEB128 (DW_FORM_string);
9253       CHECKSUM_STRING (AT_string (at));
9254       break;
9255
9256     case dw_val_class_addr:
9257       r = AT_addr (at);
9258       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9259       CHECKSUM_ULEB128 (DW_FORM_string);
9260       CHECKSUM_STRING (XSTR (r, 0));
9261       break;
9262
9263     case dw_val_class_offset:
9264       CHECKSUM_ULEB128 (DW_FORM_sdata);
9265       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9266       break;
9267
9268     case dw_val_class_loc:
9269       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9270         loc_checksum_ordered (loc, ctx);
9271       break;
9272
9273     case dw_val_class_fde_ref:
9274     case dw_val_class_lbl_id:
9275     case dw_val_class_lineptr:
9276     case dw_val_class_macptr:
9277       break;
9278
9279     case dw_val_class_file:
9280       CHECKSUM_ULEB128 (DW_FORM_string);
9281       CHECKSUM_STRING (AT_file (at)->filename);
9282       break;
9283
9284     case dw_val_class_data8:
9285       CHECKSUM (at->dw_attr_val.v.val_data8);
9286       break;
9287
9288     default:
9289       break;
9290     }
9291 }
9292
9293 struct checksum_attributes
9294 {
9295   dw_attr_ref at_name;
9296   dw_attr_ref at_type;
9297   dw_attr_ref at_friend;
9298   dw_attr_ref at_accessibility;
9299   dw_attr_ref at_address_class;
9300   dw_attr_ref at_allocated;
9301   dw_attr_ref at_artificial;
9302   dw_attr_ref at_associated;
9303   dw_attr_ref at_binary_scale;
9304   dw_attr_ref at_bit_offset;
9305   dw_attr_ref at_bit_size;
9306   dw_attr_ref at_bit_stride;
9307   dw_attr_ref at_byte_size;
9308   dw_attr_ref at_byte_stride;
9309   dw_attr_ref at_const_value;
9310   dw_attr_ref at_containing_type;
9311   dw_attr_ref at_count;
9312   dw_attr_ref at_data_location;
9313   dw_attr_ref at_data_member_location;
9314   dw_attr_ref at_decimal_scale;
9315   dw_attr_ref at_decimal_sign;
9316   dw_attr_ref at_default_value;
9317   dw_attr_ref at_digit_count;
9318   dw_attr_ref at_discr;
9319   dw_attr_ref at_discr_list;
9320   dw_attr_ref at_discr_value;
9321   dw_attr_ref at_encoding;
9322   dw_attr_ref at_endianity;
9323   dw_attr_ref at_explicit;
9324   dw_attr_ref at_is_optional;
9325   dw_attr_ref at_location;
9326   dw_attr_ref at_lower_bound;
9327   dw_attr_ref at_mutable;
9328   dw_attr_ref at_ordering;
9329   dw_attr_ref at_picture_string;
9330   dw_attr_ref at_prototyped;
9331   dw_attr_ref at_small;
9332   dw_attr_ref at_segment;
9333   dw_attr_ref at_string_length;
9334   dw_attr_ref at_threads_scaled;
9335   dw_attr_ref at_upper_bound;
9336   dw_attr_ref at_use_location;
9337   dw_attr_ref at_use_UTF8;
9338   dw_attr_ref at_variable_parameter;
9339   dw_attr_ref at_virtuality;
9340   dw_attr_ref at_visibility;
9341   dw_attr_ref at_vtable_elem_location;
9342 };
9343
9344 /* Collect the attributes that we will want to use for the checksum.  */
9345
9346 static void
9347 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9348 {
9349   dw_attr_ref a;
9350   unsigned ix;
9351
9352   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9353     {
9354       switch (a->dw_attr)
9355         {
9356         case DW_AT_name:
9357           attrs->at_name = a;
9358           break;
9359         case DW_AT_type:
9360           attrs->at_type = a;
9361           break;
9362         case DW_AT_friend:
9363           attrs->at_friend = a;
9364           break;
9365         case DW_AT_accessibility:
9366           attrs->at_accessibility = a;
9367           break;
9368         case DW_AT_address_class:
9369           attrs->at_address_class = a;
9370           break;
9371         case DW_AT_allocated:
9372           attrs->at_allocated = a;
9373           break;
9374         case DW_AT_artificial:
9375           attrs->at_artificial = a;
9376           break;
9377         case DW_AT_associated:
9378           attrs->at_associated = a;
9379           break;
9380         case DW_AT_binary_scale:
9381           attrs->at_binary_scale = a;
9382           break;
9383         case DW_AT_bit_offset:
9384           attrs->at_bit_offset = a;
9385           break;
9386         case DW_AT_bit_size:
9387           attrs->at_bit_size = a;
9388           break;
9389         case DW_AT_bit_stride:
9390           attrs->at_bit_stride = a;
9391           break;
9392         case DW_AT_byte_size:
9393           attrs->at_byte_size = a;
9394           break;
9395         case DW_AT_byte_stride:
9396           attrs->at_byte_stride = a;
9397           break;
9398         case DW_AT_const_value:
9399           attrs->at_const_value = a;
9400           break;
9401         case DW_AT_containing_type:
9402           attrs->at_containing_type = a;
9403           break;
9404         case DW_AT_count:
9405           attrs->at_count = a;
9406           break;
9407         case DW_AT_data_location:
9408           attrs->at_data_location = a;
9409           break;
9410         case DW_AT_data_member_location:
9411           attrs->at_data_member_location = a;
9412           break;
9413         case DW_AT_decimal_scale:
9414           attrs->at_decimal_scale = a;
9415           break;
9416         case DW_AT_decimal_sign:
9417           attrs->at_decimal_sign = a;
9418           break;
9419         case DW_AT_default_value:
9420           attrs->at_default_value = a;
9421           break;
9422         case DW_AT_digit_count:
9423           attrs->at_digit_count = a;
9424           break;
9425         case DW_AT_discr:
9426           attrs->at_discr = a;
9427           break;
9428         case DW_AT_discr_list:
9429           attrs->at_discr_list = a;
9430           break;
9431         case DW_AT_discr_value:
9432           attrs->at_discr_value = a;
9433           break;
9434         case DW_AT_encoding:
9435           attrs->at_encoding = a;
9436           break;
9437         case DW_AT_endianity:
9438           attrs->at_endianity = a;
9439           break;
9440         case DW_AT_explicit:
9441           attrs->at_explicit = a;
9442           break;
9443         case DW_AT_is_optional:
9444           attrs->at_is_optional = a;
9445           break;
9446         case DW_AT_location:
9447           attrs->at_location = a;
9448           break;
9449         case DW_AT_lower_bound:
9450           attrs->at_lower_bound = a;
9451           break;
9452         case DW_AT_mutable:
9453           attrs->at_mutable = a;
9454           break;
9455         case DW_AT_ordering:
9456           attrs->at_ordering = a;
9457           break;
9458         case DW_AT_picture_string:
9459           attrs->at_picture_string = a;
9460           break;
9461         case DW_AT_prototyped:
9462           attrs->at_prototyped = a;
9463           break;
9464         case DW_AT_small:
9465           attrs->at_small = a;
9466           break;
9467         case DW_AT_segment:
9468           attrs->at_segment = a;
9469           break;
9470         case DW_AT_string_length:
9471           attrs->at_string_length = a;
9472           break;
9473         case DW_AT_threads_scaled:
9474           attrs->at_threads_scaled = a;
9475           break;
9476         case DW_AT_upper_bound:
9477           attrs->at_upper_bound = a;
9478           break;
9479         case DW_AT_use_location:
9480           attrs->at_use_location = a;
9481           break;
9482         case DW_AT_use_UTF8:
9483           attrs->at_use_UTF8 = a;
9484           break;
9485         case DW_AT_variable_parameter:
9486           attrs->at_variable_parameter = a;
9487           break;
9488         case DW_AT_virtuality:
9489           attrs->at_virtuality = a;
9490           break;
9491         case DW_AT_visibility:
9492           attrs->at_visibility = a;
9493           break;
9494         case DW_AT_vtable_elem_location:
9495           attrs->at_vtable_elem_location = a;
9496           break;
9497         default:
9498           break;
9499         }
9500     }
9501 }
9502
9503 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9504
9505 static void
9506 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9507 {
9508   dw_die_ref c;
9509   dw_die_ref decl;
9510   struct checksum_attributes attrs;
9511
9512   CHECKSUM_ULEB128 ('D');
9513   CHECKSUM_ULEB128 (die->die_tag);
9514
9515   memset (&attrs, 0, sizeof (attrs));
9516
9517   decl = get_AT_ref (die, DW_AT_specification);
9518   if (decl != NULL)
9519     collect_checksum_attributes (&attrs, decl);
9520   collect_checksum_attributes (&attrs, die);
9521
9522   CHECKSUM_ATTR (attrs.at_name);
9523   CHECKSUM_ATTR (attrs.at_accessibility);
9524   CHECKSUM_ATTR (attrs.at_address_class);
9525   CHECKSUM_ATTR (attrs.at_allocated);
9526   CHECKSUM_ATTR (attrs.at_artificial);
9527   CHECKSUM_ATTR (attrs.at_associated);
9528   CHECKSUM_ATTR (attrs.at_binary_scale);
9529   CHECKSUM_ATTR (attrs.at_bit_offset);
9530   CHECKSUM_ATTR (attrs.at_bit_size);
9531   CHECKSUM_ATTR (attrs.at_bit_stride);
9532   CHECKSUM_ATTR (attrs.at_byte_size);
9533   CHECKSUM_ATTR (attrs.at_byte_stride);
9534   CHECKSUM_ATTR (attrs.at_const_value);
9535   CHECKSUM_ATTR (attrs.at_containing_type);
9536   CHECKSUM_ATTR (attrs.at_count);
9537   CHECKSUM_ATTR (attrs.at_data_location);
9538   CHECKSUM_ATTR (attrs.at_data_member_location);
9539   CHECKSUM_ATTR (attrs.at_decimal_scale);
9540   CHECKSUM_ATTR (attrs.at_decimal_sign);
9541   CHECKSUM_ATTR (attrs.at_default_value);
9542   CHECKSUM_ATTR (attrs.at_digit_count);
9543   CHECKSUM_ATTR (attrs.at_discr);
9544   CHECKSUM_ATTR (attrs.at_discr_list);
9545   CHECKSUM_ATTR (attrs.at_discr_value);
9546   CHECKSUM_ATTR (attrs.at_encoding);
9547   CHECKSUM_ATTR (attrs.at_endianity);
9548   CHECKSUM_ATTR (attrs.at_explicit);
9549   CHECKSUM_ATTR (attrs.at_is_optional);
9550   CHECKSUM_ATTR (attrs.at_location);
9551   CHECKSUM_ATTR (attrs.at_lower_bound);
9552   CHECKSUM_ATTR (attrs.at_mutable);
9553   CHECKSUM_ATTR (attrs.at_ordering);
9554   CHECKSUM_ATTR (attrs.at_picture_string);
9555   CHECKSUM_ATTR (attrs.at_prototyped);
9556   CHECKSUM_ATTR (attrs.at_small);
9557   CHECKSUM_ATTR (attrs.at_segment);
9558   CHECKSUM_ATTR (attrs.at_string_length);
9559   CHECKSUM_ATTR (attrs.at_threads_scaled);
9560   CHECKSUM_ATTR (attrs.at_upper_bound);
9561   CHECKSUM_ATTR (attrs.at_use_location);
9562   CHECKSUM_ATTR (attrs.at_use_UTF8);
9563   CHECKSUM_ATTR (attrs.at_variable_parameter);
9564   CHECKSUM_ATTR (attrs.at_virtuality);
9565   CHECKSUM_ATTR (attrs.at_visibility);
9566   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9567   CHECKSUM_ATTR (attrs.at_type);
9568   CHECKSUM_ATTR (attrs.at_friend);
9569
9570   /* Checksum the child DIEs, except for nested types and member functions.  */
9571   c = die->die_child;
9572   if (c) do {
9573     dw_attr_ref name_attr;
9574
9575     c = c->die_sib;
9576     name_attr = get_AT (c, DW_AT_name);
9577     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9578         && name_attr != NULL)
9579       {
9580         CHECKSUM_ULEB128 ('S');
9581         CHECKSUM_ULEB128 (c->die_tag);
9582         CHECKSUM_STRING (AT_string (name_attr));
9583       }
9584     else
9585       {
9586         /* Mark this DIE so it gets processed when unmarking.  */
9587         if (c->die_mark == 0)
9588           c->die_mark = -1;
9589         die_checksum_ordered (c, ctx, mark);
9590       }
9591   } while (c != die->die_child);
9592
9593   CHECKSUM_ULEB128 (0);
9594 }
9595
9596 #undef CHECKSUM
9597 #undef CHECKSUM_STRING
9598 #undef CHECKSUM_ATTR
9599 #undef CHECKSUM_LEB128
9600 #undef CHECKSUM_ULEB128
9601
9602 /* Generate the type signature for DIE.  This is computed by generating an
9603    MD5 checksum over the DIE's tag, its relevant attributes, and its
9604    children.  Attributes that are references to other DIEs are processed
9605    by recursion, using the MARK field to prevent infinite recursion.
9606    If the DIE is nested inside a namespace or another type, we also
9607    need to include that context in the signature.  The lower 64 bits
9608    of the resulting MD5 checksum comprise the signature.  */
9609
9610 static void
9611 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9612 {
9613   int mark;
9614   const char *name;
9615   unsigned char checksum[16];
9616   struct md5_ctx ctx;
9617   dw_die_ref decl;
9618
9619   name = get_AT_string (die, DW_AT_name);
9620   decl = get_AT_ref (die, DW_AT_specification);
9621
9622   /* First, compute a signature for just the type name (and its surrounding
9623      context, if any.  This is stored in the type unit DIE for link-time
9624      ODR (one-definition rule) checking.  */
9625
9626   if (is_cxx() && name != NULL)
9627     {
9628       md5_init_ctx (&ctx);
9629
9630       /* Checksum the names of surrounding namespaces and structures.  */
9631       if (decl != NULL && decl->die_parent != NULL)
9632         checksum_die_context (decl->die_parent, &ctx);
9633
9634       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9635       md5_process_bytes (name, strlen (name) + 1, &ctx);
9636       md5_finish_ctx (&ctx, checksum);
9637
9638       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9639     }
9640
9641   /* Next, compute the complete type signature.  */
9642
9643   md5_init_ctx (&ctx);
9644   mark = 1;
9645   die->die_mark = mark;
9646
9647   /* Checksum the names of surrounding namespaces and structures.  */
9648   if (decl != NULL && decl->die_parent != NULL)
9649     checksum_die_context (decl->die_parent, &ctx);
9650
9651   /* Checksum the DIE and its children.  */
9652   die_checksum_ordered (die, &ctx, &mark);
9653   unmark_all_dies (die);
9654   md5_finish_ctx (&ctx, checksum);
9655
9656   /* Store the signature in the type node and link the type DIE and the
9657      type node together.  */
9658   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9659           DWARF_TYPE_SIGNATURE_SIZE);
9660   die->die_id.die_type_node = type_node;
9661   type_node->type_die = die;
9662
9663   /* If the DIE is a specification, link its declaration to the type node
9664      as well.  */
9665   if (decl != NULL)
9666     decl->die_id.die_type_node = type_node;
9667 }
9668
9669 /* Do the location expressions look same?  */
9670 static inline int
9671 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9672 {
9673   return loc1->dw_loc_opc == loc2->dw_loc_opc
9674          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9675          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9676 }
9677
9678 /* Do the values look the same?  */
9679 static int
9680 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9681 {
9682   dw_loc_descr_ref loc1, loc2;
9683   rtx r1, r2;
9684
9685   if (v1->val_class != v2->val_class)
9686     return 0;
9687
9688   switch (v1->val_class)
9689     {
9690     case dw_val_class_const:
9691       return v1->v.val_int == v2->v.val_int;
9692     case dw_val_class_unsigned_const:
9693       return v1->v.val_unsigned == v2->v.val_unsigned;
9694     case dw_val_class_const_double:
9695       return v1->v.val_double.high == v2->v.val_double.high
9696              && v1->v.val_double.low == v2->v.val_double.low;
9697     case dw_val_class_vec:
9698       if (v1->v.val_vec.length != v2->v.val_vec.length
9699           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9700         return 0;
9701       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9702                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9703         return 0;
9704       return 1;
9705     case dw_val_class_flag:
9706       return v1->v.val_flag == v2->v.val_flag;
9707     case dw_val_class_str:
9708       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9709
9710     case dw_val_class_addr:
9711       r1 = v1->v.val_addr;
9712       r2 = v2->v.val_addr;
9713       if (GET_CODE (r1) != GET_CODE (r2))
9714         return 0;
9715       return !rtx_equal_p (r1, r2);
9716
9717     case dw_val_class_offset:
9718       return v1->v.val_offset == v2->v.val_offset;
9719
9720     case dw_val_class_loc:
9721       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9722            loc1 && loc2;
9723            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9724         if (!same_loc_p (loc1, loc2, mark))
9725           return 0;
9726       return !loc1 && !loc2;
9727
9728     case dw_val_class_die_ref:
9729       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9730
9731     case dw_val_class_fde_ref:
9732     case dw_val_class_vms_delta:
9733     case dw_val_class_lbl_id:
9734     case dw_val_class_lineptr:
9735     case dw_val_class_macptr:
9736       return 1;
9737
9738     case dw_val_class_file:
9739       return v1->v.val_file == v2->v.val_file;
9740
9741     case dw_val_class_data8:
9742       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9743
9744     default:
9745       return 1;
9746     }
9747 }
9748
9749 /* Do the attributes look the same?  */
9750
9751 static int
9752 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9753 {
9754   if (at1->dw_attr != at2->dw_attr)
9755     return 0;
9756
9757   /* We don't care that this was compiled with a different compiler
9758      snapshot; if the output is the same, that's what matters. */
9759   if (at1->dw_attr == DW_AT_producer)
9760     return 1;
9761
9762   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9763 }
9764
9765 /* Do the dies look the same?  */
9766
9767 static int
9768 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9769 {
9770   dw_die_ref c1, c2;
9771   dw_attr_ref a1;
9772   unsigned ix;
9773
9774   /* To avoid infinite recursion.  */
9775   if (die1->die_mark)
9776     return die1->die_mark == die2->die_mark;
9777   die1->die_mark = die2->die_mark = ++(*mark);
9778
9779   if (die1->die_tag != die2->die_tag)
9780     return 0;
9781
9782   if (VEC_length (dw_attr_node, die1->die_attr)
9783       != VEC_length (dw_attr_node, die2->die_attr))
9784     return 0;
9785
9786   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9787     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9788       return 0;
9789
9790   c1 = die1->die_child;
9791   c2 = die2->die_child;
9792   if (! c1)
9793     {
9794       if (c2)
9795         return 0;
9796     }
9797   else
9798     for (;;)
9799       {
9800         if (!same_die_p (c1, c2, mark))
9801           return 0;
9802         c1 = c1->die_sib;
9803         c2 = c2->die_sib;
9804         if (c1 == die1->die_child)
9805           {
9806             if (c2 == die2->die_child)
9807               break;
9808             else
9809               return 0;
9810           }
9811     }
9812
9813   return 1;
9814 }
9815
9816 /* Do the dies look the same?  Wrapper around same_die_p.  */
9817
9818 static int
9819 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9820 {
9821   int mark = 0;
9822   int ret = same_die_p (die1, die2, &mark);
9823
9824   unmark_all_dies (die1);
9825   unmark_all_dies (die2);
9826
9827   return ret;
9828 }
9829
9830 /* The prefix to attach to symbols on DIEs in the current comdat debug
9831    info section.  */
9832 static char *comdat_symbol_id;
9833
9834 /* The index of the current symbol within the current comdat CU.  */
9835 static unsigned int comdat_symbol_number;
9836
9837 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9838    children, and set comdat_symbol_id accordingly.  */
9839
9840 static void
9841 compute_section_prefix (dw_die_ref unit_die)
9842 {
9843   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9844   const char *base = die_name ? lbasename (die_name) : "anonymous";
9845   char *name = XALLOCAVEC (char, strlen (base) + 64);
9846   char *p;
9847   int i, mark;
9848   unsigned char checksum[16];
9849   struct md5_ctx ctx;
9850
9851   /* Compute the checksum of the DIE, then append part of it as hex digits to
9852      the name filename of the unit.  */
9853
9854   md5_init_ctx (&ctx);
9855   mark = 0;
9856   die_checksum (unit_die, &ctx, &mark);
9857   unmark_all_dies (unit_die);
9858   md5_finish_ctx (&ctx, checksum);
9859
9860   sprintf (name, "%s.", base);
9861   clean_symbol_name (name);
9862
9863   p = name + strlen (name);
9864   for (i = 0; i < 4; i++)
9865     {
9866       sprintf (p, "%.2x", checksum[i]);
9867       p += 2;
9868     }
9869
9870   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9871   comdat_symbol_number = 0;
9872 }
9873
9874 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9875
9876 static int
9877 is_type_die (dw_die_ref die)
9878 {
9879   switch (die->die_tag)
9880     {
9881     case DW_TAG_array_type:
9882     case DW_TAG_class_type:
9883     case DW_TAG_interface_type:
9884     case DW_TAG_enumeration_type:
9885     case DW_TAG_pointer_type:
9886     case DW_TAG_reference_type:
9887     case DW_TAG_rvalue_reference_type:
9888     case DW_TAG_string_type:
9889     case DW_TAG_structure_type:
9890     case DW_TAG_subroutine_type:
9891     case DW_TAG_union_type:
9892     case DW_TAG_ptr_to_member_type:
9893     case DW_TAG_set_type:
9894     case DW_TAG_subrange_type:
9895     case DW_TAG_base_type:
9896     case DW_TAG_const_type:
9897     case DW_TAG_file_type:
9898     case DW_TAG_packed_type:
9899     case DW_TAG_volatile_type:
9900     case DW_TAG_typedef:
9901       return 1;
9902     default:
9903       return 0;
9904     }
9905 }
9906
9907 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9908    Basically, we want to choose the bits that are likely to be shared between
9909    compilations (types) and leave out the bits that are specific to individual
9910    compilations (functions).  */
9911
9912 static int
9913 is_comdat_die (dw_die_ref c)
9914 {
9915   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9916      we do for stabs.  The advantage is a greater likelihood of sharing between
9917      objects that don't include headers in the same order (and therefore would
9918      put the base types in a different comdat).  jason 8/28/00 */
9919
9920   if (c->die_tag == DW_TAG_base_type)
9921     return 0;
9922
9923   if (c->die_tag == DW_TAG_pointer_type
9924       || c->die_tag == DW_TAG_reference_type
9925       || c->die_tag == DW_TAG_rvalue_reference_type
9926       || c->die_tag == DW_TAG_const_type
9927       || c->die_tag == DW_TAG_volatile_type)
9928     {
9929       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9930
9931       return t ? is_comdat_die (t) : 0;
9932     }
9933
9934   return is_type_die (c);
9935 }
9936
9937 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9938    compilation unit.  */
9939
9940 static int
9941 is_symbol_die (dw_die_ref c)
9942 {
9943   return (is_type_die (c)
9944           || is_declaration_die (c)
9945           || c->die_tag == DW_TAG_namespace
9946           || c->die_tag == DW_TAG_module);
9947 }
9948
9949 /* Returns true iff C is a compile-unit DIE.  */
9950
9951 static inline bool
9952 is_cu_die (dw_die_ref c)
9953 {
9954   return c && c->die_tag == DW_TAG_compile_unit;
9955 }
9956
9957 static char *
9958 gen_internal_sym (const char *prefix)
9959 {
9960   char buf[256];
9961
9962   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9963   return xstrdup (buf);
9964 }
9965
9966 /* Assign symbols to all worthy DIEs under DIE.  */
9967
9968 static void
9969 assign_symbol_names (dw_die_ref die)
9970 {
9971   dw_die_ref c;
9972
9973   if (is_symbol_die (die))
9974     {
9975       if (comdat_symbol_id)
9976         {
9977           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9978
9979           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9980                    comdat_symbol_id, comdat_symbol_number++);
9981           die->die_id.die_symbol = xstrdup (p);
9982         }
9983       else
9984         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9985     }
9986
9987   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9988 }
9989
9990 struct cu_hash_table_entry
9991 {
9992   dw_die_ref cu;
9993   unsigned min_comdat_num, max_comdat_num;
9994   struct cu_hash_table_entry *next;
9995 };
9996
9997 /* Routines to manipulate hash table of CUs.  */
9998 static hashval_t
9999 htab_cu_hash (const void *of)
10000 {
10001   const struct cu_hash_table_entry *const entry =
10002     (const struct cu_hash_table_entry *) of;
10003
10004   return htab_hash_string (entry->cu->die_id.die_symbol);
10005 }
10006
10007 static int
10008 htab_cu_eq (const void *of1, const void *of2)
10009 {
10010   const struct cu_hash_table_entry *const entry1 =
10011     (const struct cu_hash_table_entry *) of1;
10012   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10013
10014   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
10015 }
10016
10017 static void
10018 htab_cu_del (void *what)
10019 {
10020   struct cu_hash_table_entry *next,
10021     *entry = (struct cu_hash_table_entry *) what;
10022
10023   while (entry)
10024     {
10025       next = entry->next;
10026       free (entry);
10027       entry = next;
10028     }
10029 }
10030
10031 /* Check whether we have already seen this CU and set up SYM_NUM
10032    accordingly.  */
10033 static int
10034 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
10035 {
10036   struct cu_hash_table_entry dummy;
10037   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
10038
10039   dummy.max_comdat_num = 0;
10040
10041   slot = (struct cu_hash_table_entry **)
10042     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10043         INSERT);
10044   entry = *slot;
10045
10046   for (; entry; last = entry, entry = entry->next)
10047     {
10048       if (same_die_p_wrap (cu, entry->cu))
10049         break;
10050     }
10051
10052   if (entry)
10053     {
10054       *sym_num = entry->min_comdat_num;
10055       return 1;
10056     }
10057
10058   entry = XCNEW (struct cu_hash_table_entry);
10059   entry->cu = cu;
10060   entry->min_comdat_num = *sym_num = last->max_comdat_num;
10061   entry->next = *slot;
10062   *slot = entry;
10063
10064   return 0;
10065 }
10066
10067 /* Record SYM_NUM to record of CU in HTABLE.  */
10068 static void
10069 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
10070 {
10071   struct cu_hash_table_entry **slot, *entry;
10072
10073   slot = (struct cu_hash_table_entry **)
10074     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10075         NO_INSERT);
10076   entry = *slot;
10077
10078   entry->max_comdat_num = sym_num;
10079 }
10080
10081 /* Traverse the DIE (which is always comp_unit_die), and set up
10082    additional compilation units for each of the include files we see
10083    bracketed by BINCL/EINCL.  */
10084
10085 static void
10086 break_out_includes (dw_die_ref die)
10087 {
10088   dw_die_ref c;
10089   dw_die_ref unit = NULL;
10090   limbo_die_node *node, **pnode;
10091   htab_t cu_hash_table;
10092
10093   c = die->die_child;
10094   if (c) do {
10095     dw_die_ref prev = c;
10096     c = c->die_sib;
10097     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
10098            || (unit && is_comdat_die (c)))
10099       {
10100         dw_die_ref next = c->die_sib;
10101
10102         /* This DIE is for a secondary CU; remove it from the main one.  */
10103         remove_child_with_prev (c, prev);
10104
10105         if (c->die_tag == DW_TAG_GNU_BINCL)
10106           unit = push_new_compile_unit (unit, c);
10107         else if (c->die_tag == DW_TAG_GNU_EINCL)
10108           unit = pop_compile_unit (unit);
10109         else
10110           add_child_die (unit, c);
10111         c = next;
10112         if (c == die->die_child)
10113           break;
10114       }
10115   } while (c != die->die_child);
10116
10117 #if 0
10118   /* We can only use this in debugging, since the frontend doesn't check
10119      to make sure that we leave every include file we enter.  */
10120   gcc_assert (!unit);
10121 #endif
10122
10123   assign_symbol_names (die);
10124   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
10125   for (node = limbo_die_list, pnode = &limbo_die_list;
10126        node;
10127        node = node->next)
10128     {
10129       int is_dupl;
10130
10131       compute_section_prefix (node->die);
10132       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
10133                         &comdat_symbol_number);
10134       assign_symbol_names (node->die);
10135       if (is_dupl)
10136         *pnode = node->next;
10137       else
10138         {
10139           pnode = &node->next;
10140           record_comdat_symbol_number (node->die, cu_hash_table,
10141                 comdat_symbol_number);
10142         }
10143     }
10144   htab_delete (cu_hash_table);
10145 }
10146
10147 /* Return non-zero if this DIE is a declaration.  */
10148
10149 static int
10150 is_declaration_die (dw_die_ref die)
10151 {
10152   dw_attr_ref a;
10153   unsigned ix;
10154
10155   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10156     if (a->dw_attr == DW_AT_declaration)
10157       return 1;
10158
10159   return 0;
10160 }
10161
10162 /* Return non-zero if this DIE is nested inside a subprogram.  */
10163
10164 static int
10165 is_nested_in_subprogram (dw_die_ref die)
10166 {
10167   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
10168
10169   if (decl == NULL)
10170     decl = die;
10171   return local_scope_p (decl);
10172 }
10173
10174 /* Return non-zero if this DIE contains a defining declaration of a
10175    subprogram.  */
10176
10177 static int
10178 contains_subprogram_definition (dw_die_ref die)
10179 {
10180   dw_die_ref c;
10181
10182   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
10183     return 1;
10184   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
10185   return 0;
10186 }
10187
10188 /* Return non-zero if this is a type DIE that should be moved to a
10189    COMDAT .debug_types section.  */
10190
10191 static int
10192 should_move_die_to_comdat (dw_die_ref die)
10193 {
10194   switch (die->die_tag)
10195     {
10196     case DW_TAG_class_type:
10197     case DW_TAG_structure_type:
10198     case DW_TAG_enumeration_type:
10199     case DW_TAG_union_type:
10200       /* Don't move declarations, inlined instances, or types nested in a
10201          subprogram.  */
10202       if (is_declaration_die (die)
10203           || get_AT (die, DW_AT_abstract_origin)
10204           || is_nested_in_subprogram (die))
10205         return 0;
10206       /* A type definition should never contain a subprogram definition.  */
10207       gcc_assert (!contains_subprogram_definition (die));
10208       return 1;
10209     case DW_TAG_array_type:
10210     case DW_TAG_interface_type:
10211     case DW_TAG_pointer_type:
10212     case DW_TAG_reference_type:
10213     case DW_TAG_rvalue_reference_type:
10214     case DW_TAG_string_type:
10215     case DW_TAG_subroutine_type:
10216     case DW_TAG_ptr_to_member_type:
10217     case DW_TAG_set_type:
10218     case DW_TAG_subrange_type:
10219     case DW_TAG_base_type:
10220     case DW_TAG_const_type:
10221     case DW_TAG_file_type:
10222     case DW_TAG_packed_type:
10223     case DW_TAG_volatile_type:
10224     case DW_TAG_typedef:
10225     default:
10226       return 0;
10227     }
10228 }
10229
10230 /* Make a clone of DIE.  */
10231
10232 static dw_die_ref
10233 clone_die (dw_die_ref die)
10234 {
10235   dw_die_ref clone;
10236   dw_attr_ref a;
10237   unsigned ix;
10238
10239   clone = ggc_alloc_cleared_die_node ();
10240   clone->die_tag = die->die_tag;
10241
10242   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10243     add_dwarf_attr (clone, a);
10244
10245   return clone;
10246 }
10247
10248 /* Make a clone of the tree rooted at DIE.  */
10249
10250 static dw_die_ref
10251 clone_tree (dw_die_ref die)
10252 {
10253   dw_die_ref c;
10254   dw_die_ref clone = clone_die (die);
10255
10256   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10257
10258   return clone;
10259 }
10260
10261 /* Make a clone of DIE as a declaration.  */
10262
10263 static dw_die_ref
10264 clone_as_declaration (dw_die_ref die)
10265 {
10266   dw_die_ref clone;
10267   dw_die_ref decl;
10268   dw_attr_ref a;
10269   unsigned ix;
10270
10271   /* If the DIE is already a declaration, just clone it.  */
10272   if (is_declaration_die (die))
10273     return clone_die (die);
10274
10275   /* If the DIE is a specification, just clone its declaration DIE.  */
10276   decl = get_AT_ref (die, DW_AT_specification);
10277   if (decl != NULL)
10278     return clone_die (decl);
10279
10280   clone = ggc_alloc_cleared_die_node ();
10281   clone->die_tag = die->die_tag;
10282
10283   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10284     {
10285       /* We don't want to copy over all attributes.
10286          For example we don't want DW_AT_byte_size because otherwise we will no
10287          longer have a declaration and GDB will treat it as a definition.  */
10288
10289       switch (a->dw_attr)
10290         {
10291         case DW_AT_artificial:
10292         case DW_AT_containing_type:
10293         case DW_AT_external:
10294         case DW_AT_name:
10295         case DW_AT_type:
10296         case DW_AT_virtuality:
10297         case DW_AT_linkage_name:
10298         case DW_AT_MIPS_linkage_name:
10299           add_dwarf_attr (clone, a);
10300           break;
10301         case DW_AT_byte_size:
10302         default:
10303           break;
10304         }
10305     }
10306
10307   if (die->die_id.die_type_node)
10308     add_AT_die_ref (clone, DW_AT_signature, die);
10309
10310   add_AT_flag (clone, DW_AT_declaration, 1);
10311   return clone;
10312 }
10313
10314 /* Copy the declaration context to the new compile unit DIE.  This includes
10315    any surrounding namespace or type declarations.  If the DIE has an
10316    AT_specification attribute, it also includes attributes and children
10317    attached to the specification.  */
10318
10319 static void
10320 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10321 {
10322   dw_die_ref decl;
10323   dw_die_ref new_decl;
10324
10325   decl = get_AT_ref (die, DW_AT_specification);
10326   if (decl == NULL)
10327     decl = die;
10328   else
10329     {
10330       unsigned ix;
10331       dw_die_ref c;
10332       dw_attr_ref a;
10333
10334       /* Copy the type node pointer from the new DIE to the original
10335          declaration DIE so we can forward references later.  */
10336       decl->die_id.die_type_node = die->die_id.die_type_node;
10337
10338       remove_AT (die, DW_AT_specification);
10339
10340       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10341         {
10342           if (a->dw_attr != DW_AT_name
10343               && a->dw_attr != DW_AT_declaration
10344               && a->dw_attr != DW_AT_external)
10345             add_dwarf_attr (die, a);
10346         }
10347
10348       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10349     }
10350
10351   if (decl->die_parent != NULL
10352       && decl->die_parent->die_tag != DW_TAG_compile_unit
10353       && decl->die_parent->die_tag != DW_TAG_type_unit)
10354     {
10355       new_decl = copy_ancestor_tree (unit, decl, NULL);
10356       if (new_decl != NULL)
10357         {
10358           remove_AT (new_decl, DW_AT_signature);
10359           add_AT_specification (die, new_decl);
10360         }
10361     }
10362 }
10363
10364 /* Generate the skeleton ancestor tree for the given NODE, then clone
10365    the DIE and add the clone into the tree.  */
10366
10367 static void
10368 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10369 {
10370   if (node->new_die != NULL)
10371     return;
10372
10373   node->new_die = clone_as_declaration (node->old_die);
10374
10375   if (node->parent != NULL)
10376     {
10377       generate_skeleton_ancestor_tree (node->parent);
10378       add_child_die (node->parent->new_die, node->new_die);
10379     }
10380 }
10381
10382 /* Generate a skeleton tree of DIEs containing any declarations that are
10383    found in the original tree.  We traverse the tree looking for declaration
10384    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10385
10386 static void
10387 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10388 {
10389   skeleton_chain_node node;
10390   dw_die_ref c;
10391   dw_die_ref first;
10392   dw_die_ref prev = NULL;
10393   dw_die_ref next = NULL;
10394
10395   node.parent = parent;
10396
10397   first = c = parent->old_die->die_child;
10398   if (c)
10399     next = c->die_sib;
10400   if (c) do {
10401     if (prev == NULL || prev->die_sib == c)
10402       prev = c;
10403     c = next;
10404     next = (c == first ? NULL : c->die_sib);
10405     node.old_die = c;
10406     node.new_die = NULL;
10407     if (is_declaration_die (c))
10408       {
10409         /* Clone the existing DIE, move the original to the skeleton
10410            tree (which is in the main CU), and put the clone, with
10411            all the original's children, where the original came from.  */
10412         dw_die_ref clone = clone_die (c);
10413         move_all_children (c, clone);
10414
10415         replace_child (c, clone, prev);
10416         generate_skeleton_ancestor_tree (parent);
10417         add_child_die (parent->new_die, c);
10418         node.new_die = c;
10419         c = clone;
10420       }
10421     generate_skeleton_bottom_up (&node);
10422   } while (next != NULL);
10423 }
10424
10425 /* Wrapper function for generate_skeleton_bottom_up.  */
10426
10427 static dw_die_ref
10428 generate_skeleton (dw_die_ref die)
10429 {
10430   skeleton_chain_node node;
10431
10432   node.old_die = die;
10433   node.new_die = NULL;
10434   node.parent = NULL;
10435
10436   /* If this type definition is nested inside another type,
10437      always leave at least a declaration in its place.  */
10438   if (die->die_parent != NULL && is_type_die (die->die_parent))
10439     node.new_die = clone_as_declaration (die);
10440
10441   generate_skeleton_bottom_up (&node);
10442   return node.new_die;
10443 }
10444
10445 /* Remove the DIE from its parent, possibly replacing it with a cloned
10446    declaration.  The original DIE will be moved to a new compile unit
10447    so that existing references to it follow it to the new location.  If
10448    any of the original DIE's descendants is a declaration, we need to
10449    replace the original DIE with a skeleton tree and move the
10450    declarations back into the skeleton tree.  */
10451
10452 static dw_die_ref
10453 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10454 {
10455   dw_die_ref skeleton;
10456
10457   skeleton = generate_skeleton (child);
10458   if (skeleton == NULL)
10459     remove_child_with_prev (child, prev);
10460   else
10461     {
10462       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10463       replace_child (child, skeleton, prev);
10464     }
10465
10466   return skeleton;
10467 }
10468
10469 /* Traverse the DIE and set up additional .debug_types sections for each
10470    type worthy of being placed in a COMDAT section.  */
10471
10472 static void
10473 break_out_comdat_types (dw_die_ref die)
10474 {
10475   dw_die_ref c;
10476   dw_die_ref first;
10477   dw_die_ref prev = NULL;
10478   dw_die_ref next = NULL;
10479   dw_die_ref unit = NULL;
10480
10481   first = c = die->die_child;
10482   if (c)
10483     next = c->die_sib;
10484   if (c) do {
10485     if (prev == NULL || prev->die_sib == c)
10486       prev = c;
10487     c = next;
10488     next = (c == first ? NULL : c->die_sib);
10489     if (should_move_die_to_comdat (c))
10490       {
10491         dw_die_ref replacement;
10492         comdat_type_node_ref type_node;
10493
10494         /* Create a new type unit DIE as the root for the new tree, and
10495            add it to the list of comdat types.  */
10496         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10497         add_AT_unsigned (unit, DW_AT_language,
10498                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10499         type_node = ggc_alloc_cleared_comdat_type_node ();
10500         type_node->root_die = unit;
10501         type_node->next = comdat_type_list;
10502         comdat_type_list = type_node;
10503
10504         /* Generate the type signature.  */
10505         generate_type_signature (c, type_node);
10506
10507         /* Copy the declaration context, attributes, and children of the
10508            declaration into the new compile unit DIE.  */
10509         copy_declaration_context (unit, c);
10510
10511         /* Remove this DIE from the main CU.  */
10512         replacement = remove_child_or_replace_with_skeleton (c, prev);
10513
10514         /* Break out nested types into their own type units.  */
10515         break_out_comdat_types (c);
10516
10517         /* Add the DIE to the new compunit.  */
10518         add_child_die (unit, c);
10519
10520         if (replacement != NULL)
10521           c = replacement;
10522       }
10523     else if (c->die_tag == DW_TAG_namespace
10524              || c->die_tag == DW_TAG_class_type
10525              || c->die_tag == DW_TAG_structure_type
10526              || c->die_tag == DW_TAG_union_type)
10527       {
10528         /* Look for nested types that can be broken out.  */
10529         break_out_comdat_types (c);
10530       }
10531   } while (next != NULL);
10532 }
10533
10534 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10535
10536 struct decl_table_entry
10537 {
10538   dw_die_ref orig;
10539   dw_die_ref copy;
10540 };
10541
10542 /* Routines to manipulate hash table of copied declarations.  */
10543
10544 static hashval_t
10545 htab_decl_hash (const void *of)
10546 {
10547   const struct decl_table_entry *const entry =
10548     (const struct decl_table_entry *) of;
10549
10550   return htab_hash_pointer (entry->orig);
10551 }
10552
10553 static int
10554 htab_decl_eq (const void *of1, const void *of2)
10555 {
10556   const struct decl_table_entry *const entry1 =
10557     (const struct decl_table_entry *) of1;
10558   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10559
10560   return entry1->orig == entry2;
10561 }
10562
10563 static void
10564 htab_decl_del (void *what)
10565 {
10566   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10567
10568   free (entry);
10569 }
10570
10571 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10572    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10573    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10574    to check if the ancestor has already been copied into UNIT.  */
10575
10576 static dw_die_ref
10577 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10578 {
10579   dw_die_ref parent = die->die_parent;
10580   dw_die_ref new_parent = unit;
10581   dw_die_ref copy;
10582   void **slot = NULL;
10583   struct decl_table_entry *entry = NULL;
10584
10585   if (decl_table)
10586     {
10587       /* Check if the entry has already been copied to UNIT.  */
10588       slot = htab_find_slot_with_hash (decl_table, die,
10589                                        htab_hash_pointer (die), INSERT);
10590       if (*slot != HTAB_EMPTY_ENTRY)
10591         {
10592           entry = (struct decl_table_entry *) *slot;
10593           return entry->copy;
10594         }
10595
10596       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10597       entry = XCNEW (struct decl_table_entry);
10598       entry->orig = die;
10599       entry->copy = NULL;
10600       *slot = entry;
10601     }
10602
10603   if (parent != NULL)
10604     {
10605       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10606       if (spec != NULL)
10607         parent = spec;
10608       if (parent->die_tag != DW_TAG_compile_unit
10609           && parent->die_tag != DW_TAG_type_unit)
10610         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10611     }
10612
10613   copy = clone_as_declaration (die);
10614   add_child_die (new_parent, copy);
10615
10616   if (decl_table != NULL)
10617     {
10618       /* Record the pointer to the copy.  */
10619       entry->copy = copy;
10620     }
10621
10622   return copy;
10623 }
10624
10625 /* Walk the DIE and its children, looking for references to incomplete
10626    or trivial types that are unmarked (i.e., that are not in the current
10627    type_unit).  */
10628
10629 static void
10630 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10631 {
10632   dw_die_ref c;
10633   dw_attr_ref a;
10634   unsigned ix;
10635
10636   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10637     {
10638       if (AT_class (a) == dw_val_class_die_ref)
10639         {
10640           dw_die_ref targ = AT_ref (a);
10641           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10642           void **slot;
10643           struct decl_table_entry *entry;
10644
10645           if (targ->die_mark != 0 || type_node != NULL)
10646             continue;
10647
10648           slot = htab_find_slot_with_hash (decl_table, targ,
10649                                            htab_hash_pointer (targ), INSERT);
10650
10651           if (*slot != HTAB_EMPTY_ENTRY)
10652             {
10653               /* TARG has already been copied, so we just need to
10654                  modify the reference to point to the copy.  */
10655               entry = (struct decl_table_entry *) *slot;
10656               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10657             }
10658           else
10659             {
10660               dw_die_ref parent = unit;
10661               dw_die_ref copy = clone_tree (targ);
10662
10663               /* Make sure the cloned tree is marked as part of the
10664                  type unit.  */
10665               mark_dies (copy);
10666
10667               /* Record in DECL_TABLE that TARG has been copied.
10668                  Need to do this now, before the recursive call,
10669                  because DECL_TABLE may be expanded and SLOT
10670                  would no longer be a valid pointer.  */
10671               entry = XCNEW (struct decl_table_entry);
10672               entry->orig = targ;
10673               entry->copy = copy;
10674               *slot = entry;
10675
10676               /* If TARG has surrounding context, copy its ancestor tree
10677                  into the new type unit.  */
10678               if (targ->die_parent != NULL
10679                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10680                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10681                 parent = copy_ancestor_tree (unit, targ->die_parent,
10682                                              decl_table);
10683
10684               add_child_die (parent, copy);
10685               a->dw_attr_val.v.val_die_ref.die = copy;
10686
10687               /* Make sure the newly-copied DIE is walked.  If it was
10688                  installed in a previously-added context, it won't
10689                  get visited otherwise.  */
10690               if (parent != unit)
10691                 {
10692                   /* Find the highest point of the newly-added tree,
10693                      mark each node along the way, and walk from there.  */
10694                   parent->die_mark = 1;
10695                   while (parent->die_parent
10696                          && parent->die_parent->die_mark == 0)
10697                     {
10698                       parent = parent->die_parent;
10699                       parent->die_mark = 1;
10700                     }
10701                   copy_decls_walk (unit, parent, decl_table);
10702                 }
10703             }
10704         }
10705     }
10706
10707   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10708 }
10709
10710 /* Copy declarations for "unworthy" types into the new comdat section.
10711    Incomplete types, modified types, and certain other types aren't broken
10712    out into comdat sections of their own, so they don't have a signature,
10713    and we need to copy the declaration into the same section so that we
10714    don't have an external reference.  */
10715
10716 static void
10717 copy_decls_for_unworthy_types (dw_die_ref unit)
10718 {
10719   htab_t decl_table;
10720
10721   mark_dies (unit);
10722   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10723   copy_decls_walk (unit, unit, decl_table);
10724   htab_delete (decl_table);
10725   unmark_dies (unit);
10726 }
10727
10728 /* Traverse the DIE and add a sibling attribute if it may have the
10729    effect of speeding up access to siblings.  To save some space,
10730    avoid generating sibling attributes for DIE's without children.  */
10731
10732 static void
10733 add_sibling_attributes (dw_die_ref die)
10734 {
10735   dw_die_ref c;
10736
10737   if (! die->die_child)
10738     return;
10739
10740   if (die->die_parent && die != die->die_parent->die_child)
10741     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10742
10743   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10744 }
10745
10746 /* Output all location lists for the DIE and its children.  */
10747
10748 static void
10749 output_location_lists (dw_die_ref die)
10750 {
10751   dw_die_ref c;
10752   dw_attr_ref a;
10753   unsigned ix;
10754
10755   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10756     if (AT_class (a) == dw_val_class_loc_list)
10757       output_loc_list (AT_loc_list (a));
10758
10759   FOR_EACH_CHILD (die, c, output_location_lists (c));
10760 }
10761
10762 /* The format of each DIE (and its attribute value pairs) is encoded in an
10763    abbreviation table.  This routine builds the abbreviation table and assigns
10764    a unique abbreviation id for each abbreviation entry.  The children of each
10765    die are visited recursively.  */
10766
10767 static void
10768 build_abbrev_table (dw_die_ref die)
10769 {
10770   unsigned long abbrev_id;
10771   unsigned int n_alloc;
10772   dw_die_ref c;
10773   dw_attr_ref a;
10774   unsigned ix;
10775
10776   /* Scan the DIE references, and mark as external any that refer to
10777      DIEs from other CUs (i.e. those which are not marked).  */
10778   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10779     if (AT_class (a) == dw_val_class_die_ref
10780         && AT_ref (a)->die_mark == 0)
10781       {
10782         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
10783         set_AT_ref_external (a, 1);
10784       }
10785
10786   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10787     {
10788       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10789       dw_attr_ref die_a, abbrev_a;
10790       unsigned ix;
10791       bool ok = true;
10792
10793       if (abbrev->die_tag != die->die_tag)
10794         continue;
10795       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10796         continue;
10797
10798       if (VEC_length (dw_attr_node, abbrev->die_attr)
10799           != VEC_length (dw_attr_node, die->die_attr))
10800         continue;
10801
10802       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10803         {
10804           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10805           if ((abbrev_a->dw_attr != die_a->dw_attr)
10806               || (value_format (abbrev_a) != value_format (die_a)))
10807             {
10808               ok = false;
10809               break;
10810             }
10811         }
10812       if (ok)
10813         break;
10814     }
10815
10816   if (abbrev_id >= abbrev_die_table_in_use)
10817     {
10818       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10819         {
10820           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10821           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10822                                             n_alloc);
10823
10824           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10825                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10826           abbrev_die_table_allocated = n_alloc;
10827         }
10828
10829       ++abbrev_die_table_in_use;
10830       abbrev_die_table[abbrev_id] = die;
10831     }
10832
10833   die->die_abbrev = abbrev_id;
10834   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10835 }
10836 \f
10837 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10838
10839 static int
10840 constant_size (unsigned HOST_WIDE_INT value)
10841 {
10842   int log;
10843
10844   if (value == 0)
10845     log = 0;
10846   else
10847     log = floor_log2 (value);
10848
10849   log = log / 8;
10850   log = 1 << (floor_log2 (log) + 1);
10851
10852   return log;
10853 }
10854
10855 /* Return the size of a DIE as it is represented in the
10856    .debug_info section.  */
10857
10858 static unsigned long
10859 size_of_die (dw_die_ref die)
10860 {
10861   unsigned long size = 0;
10862   dw_attr_ref a;
10863   unsigned ix;
10864
10865   size += size_of_uleb128 (die->die_abbrev);
10866   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10867     {
10868       switch (AT_class (a))
10869         {
10870         case dw_val_class_addr:
10871           size += DWARF2_ADDR_SIZE;
10872           break;
10873         case dw_val_class_offset:
10874           size += DWARF_OFFSET_SIZE;
10875           break;
10876         case dw_val_class_loc:
10877           {
10878             unsigned long lsize = size_of_locs (AT_loc (a));
10879
10880             /* Block length.  */
10881             if (dwarf_version >= 4)
10882               size += size_of_uleb128 (lsize);
10883             else
10884               size += constant_size (lsize);
10885             size += lsize;
10886           }
10887           break;
10888         case dw_val_class_loc_list:
10889           size += DWARF_OFFSET_SIZE;
10890           break;
10891         case dw_val_class_range_list:
10892           size += DWARF_OFFSET_SIZE;
10893           break;
10894         case dw_val_class_const:
10895           size += size_of_sleb128 (AT_int (a));
10896           break;
10897         case dw_val_class_unsigned_const:
10898           size += constant_size (AT_unsigned (a));
10899           break;
10900         case dw_val_class_const_double:
10901           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10902           if (HOST_BITS_PER_WIDE_INT >= 64)
10903             size++; /* block */
10904           break;
10905         case dw_val_class_vec:
10906           size += constant_size (a->dw_attr_val.v.val_vec.length
10907                                  * a->dw_attr_val.v.val_vec.elt_size)
10908                   + a->dw_attr_val.v.val_vec.length
10909                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10910           break;
10911         case dw_val_class_flag:
10912           if (dwarf_version >= 4)
10913             /* Currently all add_AT_flag calls pass in 1 as last argument,
10914                so DW_FORM_flag_present can be used.  If that ever changes,
10915                we'll need to use DW_FORM_flag and have some optimization
10916                in build_abbrev_table that will change those to
10917                DW_FORM_flag_present if it is set to 1 in all DIEs using
10918                the same abbrev entry.  */
10919             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10920           else
10921             size += 1;
10922           break;
10923         case dw_val_class_die_ref:
10924           if (AT_ref_external (a))
10925             {
10926               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10927                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10928                  is sized by target address length, whereas in DWARF3
10929                  it's always sized as an offset.  */
10930               if (use_debug_types)
10931                 size += DWARF_TYPE_SIGNATURE_SIZE;
10932               else if (dwarf_version == 2)
10933                 size += DWARF2_ADDR_SIZE;
10934               else
10935                 size += DWARF_OFFSET_SIZE;
10936             }
10937           else
10938             size += DWARF_OFFSET_SIZE;
10939           break;
10940         case dw_val_class_fde_ref:
10941           size += DWARF_OFFSET_SIZE;
10942           break;
10943         case dw_val_class_lbl_id:
10944           size += DWARF2_ADDR_SIZE;
10945           break;
10946         case dw_val_class_lineptr:
10947         case dw_val_class_macptr:
10948           size += DWARF_OFFSET_SIZE;
10949           break;
10950         case dw_val_class_str:
10951           if (AT_string_form (a) == DW_FORM_strp)
10952             size += DWARF_OFFSET_SIZE;
10953           else
10954             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10955           break;
10956         case dw_val_class_file:
10957           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10958           break;
10959         case dw_val_class_data8:
10960           size += 8;
10961           break;
10962         case dw_val_class_vms_delta:
10963           size += DWARF_OFFSET_SIZE;
10964           break;
10965         default:
10966           gcc_unreachable ();
10967         }
10968     }
10969
10970   return size;
10971 }
10972
10973 /* Size the debugging information associated with a given DIE.  Visits the
10974    DIE's children recursively.  Updates the global variable next_die_offset, on
10975    each time through.  Uses the current value of next_die_offset to update the
10976    die_offset field in each DIE.  */
10977
10978 static void
10979 calc_die_sizes (dw_die_ref die)
10980 {
10981   dw_die_ref c;
10982
10983   gcc_assert (die->die_offset == 0
10984               || (unsigned long int) die->die_offset == next_die_offset);
10985   die->die_offset = next_die_offset;
10986   next_die_offset += size_of_die (die);
10987
10988   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10989
10990   if (die->die_child != NULL)
10991     /* Count the null byte used to terminate sibling lists.  */
10992     next_die_offset += 1;
10993 }
10994
10995 /* Size just the base type children at the start of the CU.
10996    This is needed because build_abbrev needs to size locs
10997    and sizing of type based stack ops needs to know die_offset
10998    values for the base types.  */
10999
11000 static void
11001 calc_base_type_die_sizes (void)
11002 {
11003   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11004   unsigned int i;
11005   dw_die_ref base_type;
11006 #if ENABLE_ASSERT_CHECKING
11007   dw_die_ref prev = comp_unit_die ()->die_child;
11008 #endif
11009
11010   die_offset += size_of_die (comp_unit_die ());
11011   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
11012     {
11013 #if ENABLE_ASSERT_CHECKING
11014       gcc_assert (base_type->die_offset == 0
11015                   && prev->die_sib == base_type
11016                   && base_type->die_child == NULL
11017                   && base_type->die_abbrev);
11018       prev = base_type;
11019 #endif
11020       base_type->die_offset = die_offset;
11021       die_offset += size_of_die (base_type);
11022     }
11023 }
11024
11025 /* Set the marks for a die and its children.  We do this so
11026    that we know whether or not a reference needs to use FORM_ref_addr; only
11027    DIEs in the same CU will be marked.  We used to clear out the offset
11028    and use that as the flag, but ran into ordering problems.  */
11029
11030 static void
11031 mark_dies (dw_die_ref die)
11032 {
11033   dw_die_ref c;
11034
11035   gcc_assert (!die->die_mark);
11036
11037   die->die_mark = 1;
11038   FOR_EACH_CHILD (die, c, mark_dies (c));
11039 }
11040
11041 /* Clear the marks for a die and its children.  */
11042
11043 static void
11044 unmark_dies (dw_die_ref die)
11045 {
11046   dw_die_ref c;
11047
11048   if (! use_debug_types)
11049     gcc_assert (die->die_mark);
11050
11051   die->die_mark = 0;
11052   FOR_EACH_CHILD (die, c, unmark_dies (c));
11053 }
11054
11055 /* Clear the marks for a die, its children and referred dies.  */
11056
11057 static void
11058 unmark_all_dies (dw_die_ref die)
11059 {
11060   dw_die_ref c;
11061   dw_attr_ref a;
11062   unsigned ix;
11063
11064   if (!die->die_mark)
11065     return;
11066   die->die_mark = 0;
11067
11068   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
11069
11070   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11071     if (AT_class (a) == dw_val_class_die_ref)
11072       unmark_all_dies (AT_ref (a));
11073 }
11074
11075 /* Return the size of the .debug_pubnames or .debug_pubtypes table
11076    generated for the compilation unit.  */
11077
11078 static unsigned long
11079 size_of_pubnames (VEC (pubname_entry, gc) * names)
11080 {
11081   unsigned long size;
11082   unsigned i;
11083   pubname_ref p;
11084
11085   size = DWARF_PUBNAMES_HEADER_SIZE;
11086   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
11087     if (names != pubtype_table
11088         || p->die->die_offset != 0
11089         || !flag_eliminate_unused_debug_types)
11090       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
11091
11092   size += DWARF_OFFSET_SIZE;
11093   return size;
11094 }
11095
11096 /* Return the size of the information in the .debug_aranges section.  */
11097
11098 static unsigned long
11099 size_of_aranges (void)
11100 {
11101   unsigned long size;
11102
11103   size = DWARF_ARANGES_HEADER_SIZE;
11104
11105   /* Count the address/length pair for this compilation unit.  */
11106   if (text_section_used)
11107     size += 2 * DWARF2_ADDR_SIZE;
11108   if (cold_text_section_used)
11109     size += 2 * DWARF2_ADDR_SIZE;
11110   if (have_multiple_function_sections)
11111     {
11112       unsigned fde_idx = 0;
11113
11114       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11115         {
11116           dw_fde_ref fde = &fde_table[fde_idx];
11117
11118           if (!fde->in_std_section)
11119             size += 2 * DWARF2_ADDR_SIZE;
11120           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11121             size += 2 * DWARF2_ADDR_SIZE;
11122         }
11123     }
11124
11125   /* Count the two zero words used to terminated the address range table.  */
11126   size += 2 * DWARF2_ADDR_SIZE;
11127   return size;
11128 }
11129 \f
11130 /* Select the encoding of an attribute value.  */
11131
11132 static enum dwarf_form
11133 value_format (dw_attr_ref a)
11134 {
11135   switch (a->dw_attr_val.val_class)
11136     {
11137     case dw_val_class_addr:
11138       /* Only very few attributes allow DW_FORM_addr.  */
11139       switch (a->dw_attr)
11140         {
11141         case DW_AT_low_pc:
11142         case DW_AT_high_pc:
11143         case DW_AT_entry_pc:
11144         case DW_AT_trampoline:
11145           return DW_FORM_addr;
11146         default:
11147           break;
11148         }
11149       switch (DWARF2_ADDR_SIZE)
11150         {
11151         case 1:
11152           return DW_FORM_data1;
11153         case 2:
11154           return DW_FORM_data2;
11155         case 4:
11156           return DW_FORM_data4;
11157         case 8:
11158           return DW_FORM_data8;
11159         default:
11160           gcc_unreachable ();
11161         }
11162     case dw_val_class_range_list:
11163     case dw_val_class_loc_list:
11164       if (dwarf_version >= 4)
11165         return DW_FORM_sec_offset;
11166       /* FALLTHRU */
11167     case dw_val_class_vms_delta:
11168     case dw_val_class_offset:
11169       switch (DWARF_OFFSET_SIZE)
11170         {
11171         case 4:
11172           return DW_FORM_data4;
11173         case 8:
11174           return DW_FORM_data8;
11175         default:
11176           gcc_unreachable ();
11177         }
11178     case dw_val_class_loc:
11179       if (dwarf_version >= 4)
11180         return DW_FORM_exprloc;
11181       switch (constant_size (size_of_locs (AT_loc (a))))
11182         {
11183         case 1:
11184           return DW_FORM_block1;
11185         case 2:
11186           return DW_FORM_block2;
11187         default:
11188           gcc_unreachable ();
11189         }
11190     case dw_val_class_const:
11191       return DW_FORM_sdata;
11192     case dw_val_class_unsigned_const:
11193       switch (constant_size (AT_unsigned (a)))
11194         {
11195         case 1:
11196           return DW_FORM_data1;
11197         case 2:
11198           return DW_FORM_data2;
11199         case 4:
11200           return DW_FORM_data4;
11201         case 8:
11202           return DW_FORM_data8;
11203         default:
11204           gcc_unreachable ();
11205         }
11206     case dw_val_class_const_double:
11207       switch (HOST_BITS_PER_WIDE_INT)
11208         {
11209         case 8:
11210           return DW_FORM_data2;
11211         case 16:
11212           return DW_FORM_data4;
11213         case 32:
11214           return DW_FORM_data8;
11215         case 64:
11216         default:
11217           return DW_FORM_block1;
11218         }
11219     case dw_val_class_vec:
11220       switch (constant_size (a->dw_attr_val.v.val_vec.length
11221                              * a->dw_attr_val.v.val_vec.elt_size))
11222         {
11223         case 1:
11224           return DW_FORM_block1;
11225         case 2:
11226           return DW_FORM_block2;
11227         case 4:
11228           return DW_FORM_block4;
11229         default:
11230           gcc_unreachable ();
11231         }
11232     case dw_val_class_flag:
11233       if (dwarf_version >= 4)
11234         {
11235           /* Currently all add_AT_flag calls pass in 1 as last argument,
11236              so DW_FORM_flag_present can be used.  If that ever changes,
11237              we'll need to use DW_FORM_flag and have some optimization
11238              in build_abbrev_table that will change those to
11239              DW_FORM_flag_present if it is set to 1 in all DIEs using
11240              the same abbrev entry.  */
11241           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11242           return DW_FORM_flag_present;
11243         }
11244       return DW_FORM_flag;
11245     case dw_val_class_die_ref:
11246       if (AT_ref_external (a))
11247         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11248       else
11249         return DW_FORM_ref;
11250     case dw_val_class_fde_ref:
11251       return DW_FORM_data;
11252     case dw_val_class_lbl_id:
11253       return DW_FORM_addr;
11254     case dw_val_class_lineptr:
11255     case dw_val_class_macptr:
11256       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11257     case dw_val_class_str:
11258       return AT_string_form (a);
11259     case dw_val_class_file:
11260       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11261         {
11262         case 1:
11263           return DW_FORM_data1;
11264         case 2:
11265           return DW_FORM_data2;
11266         case 4:
11267           return DW_FORM_data4;
11268         default:
11269           gcc_unreachable ();
11270         }
11271
11272     case dw_val_class_data8:
11273       return DW_FORM_data8;
11274
11275     default:
11276       gcc_unreachable ();
11277     }
11278 }
11279
11280 /* Output the encoding of an attribute value.  */
11281
11282 static void
11283 output_value_format (dw_attr_ref a)
11284 {
11285   enum dwarf_form form = value_format (a);
11286
11287   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11288 }
11289
11290 /* Output the .debug_abbrev section which defines the DIE abbreviation
11291    table.  */
11292
11293 static void
11294 output_abbrev_section (void)
11295 {
11296   unsigned long abbrev_id;
11297
11298   if (abbrev_die_table_in_use == 1)
11299     return;
11300
11301   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11302     {
11303       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11304       unsigned ix;
11305       dw_attr_ref a_attr;
11306
11307       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11308       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11309                                    dwarf_tag_name (abbrev->die_tag));
11310
11311       if (abbrev->die_child != NULL)
11312         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11313       else
11314         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11315
11316       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11317            ix++)
11318         {
11319           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11320                                        dwarf_attr_name (a_attr->dw_attr));
11321           output_value_format (a_attr);
11322         }
11323
11324       dw2_asm_output_data (1, 0, NULL);
11325       dw2_asm_output_data (1, 0, NULL);
11326     }
11327
11328   /* Terminate the table.  */
11329   dw2_asm_output_data (1, 0, NULL);
11330 }
11331
11332 /* Output a symbol we can use to refer to this DIE from another CU.  */
11333
11334 static inline void
11335 output_die_symbol (dw_die_ref die)
11336 {
11337   char *sym = die->die_id.die_symbol;
11338
11339   if (sym == 0)
11340     return;
11341
11342   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11343     /* We make these global, not weak; if the target doesn't support
11344        .linkonce, it doesn't support combining the sections, so debugging
11345        will break.  */
11346     targetm.asm_out.globalize_label (asm_out_file, sym);
11347
11348   ASM_OUTPUT_LABEL (asm_out_file, sym);
11349 }
11350
11351 /* Return a new location list, given the begin and end range, and the
11352    expression.  */
11353
11354 static inline dw_loc_list_ref
11355 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11356               const char *section)
11357 {
11358   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11359
11360   retlist->begin = begin;
11361   retlist->end = end;
11362   retlist->expr = expr;
11363   retlist->section = section;
11364
11365   return retlist;
11366 }
11367
11368 /* Generate a new internal symbol for this location list node, if it
11369    hasn't got one yet.  */
11370
11371 static inline void
11372 gen_llsym (dw_loc_list_ref list)
11373 {
11374   gcc_assert (!list->ll_symbol);
11375   list->ll_symbol = gen_internal_sym ("LLST");
11376 }
11377
11378 /* Output the location list given to us.  */
11379
11380 static void
11381 output_loc_list (dw_loc_list_ref list_head)
11382 {
11383   dw_loc_list_ref curr = list_head;
11384
11385   if (list_head->emitted)
11386     return;
11387   list_head->emitted = true;
11388
11389   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11390
11391   /* Walk the location list, and output each range + expression.  */
11392   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11393     {
11394       unsigned long size;
11395       /* Don't output an entry that starts and ends at the same address.  */
11396       if (strcmp (curr->begin, curr->end) == 0)
11397         continue;
11398       if (!have_multiple_function_sections)
11399         {
11400           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11401                                 "Location list begin address (%s)",
11402                                 list_head->ll_symbol);
11403           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11404                                 "Location list end address (%s)",
11405                                 list_head->ll_symbol);
11406         }
11407       else
11408         {
11409           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11410                                "Location list begin address (%s)",
11411                                list_head->ll_symbol);
11412           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11413                                "Location list end address (%s)",
11414                                list_head->ll_symbol);
11415         }
11416       size = size_of_locs (curr->expr);
11417
11418       /* Output the block length for this list of location operations.  */
11419       gcc_assert (size <= 0xffff);
11420       dw2_asm_output_data (2, size, "%s", "Location expression size");
11421
11422       output_loc_sequence (curr->expr, -1);
11423     }
11424
11425   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11426                        "Location list terminator begin (%s)",
11427                        list_head->ll_symbol);
11428   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11429                        "Location list terminator end (%s)",
11430                        list_head->ll_symbol);
11431 }
11432
11433 /* Output a type signature.  */
11434
11435 static inline void
11436 output_signature (const char *sig, const char *name)
11437 {
11438   int i;
11439
11440   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11441     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11442 }
11443
11444 /* Output the DIE and its attributes.  Called recursively to generate
11445    the definitions of each child DIE.  */
11446
11447 static void
11448 output_die (dw_die_ref die)
11449 {
11450   dw_attr_ref a;
11451   dw_die_ref c;
11452   unsigned long size;
11453   unsigned ix;
11454
11455   /* If someone in another CU might refer to us, set up a symbol for
11456      them to point to.  */
11457   if (! use_debug_types && die->die_id.die_symbol)
11458     output_die_symbol (die);
11459
11460   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11461                                (unsigned long)die->die_offset,
11462                                dwarf_tag_name (die->die_tag));
11463
11464   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11465     {
11466       const char *name = dwarf_attr_name (a->dw_attr);
11467
11468       switch (AT_class (a))
11469         {
11470         case dw_val_class_addr:
11471           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11472           break;
11473
11474         case dw_val_class_offset:
11475           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11476                                "%s", name);
11477           break;
11478
11479         case dw_val_class_range_list:
11480           {
11481             char *p = strchr (ranges_section_label, '\0');
11482
11483             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11484                      a->dw_attr_val.v.val_offset);
11485             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11486                                    debug_ranges_section, "%s", name);
11487             *p = '\0';
11488           }
11489           break;
11490
11491         case dw_val_class_loc:
11492           size = size_of_locs (AT_loc (a));
11493
11494           /* Output the block length for this list of location operations.  */
11495           if (dwarf_version >= 4)
11496             dw2_asm_output_data_uleb128 (size, "%s", name);
11497           else
11498             dw2_asm_output_data (constant_size (size), size, "%s", name);
11499
11500           output_loc_sequence (AT_loc (a), -1);
11501           break;
11502
11503         case dw_val_class_const:
11504           /* ??? It would be slightly more efficient to use a scheme like is
11505              used for unsigned constants below, but gdb 4.x does not sign
11506              extend.  Gdb 5.x does sign extend.  */
11507           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11508           break;
11509
11510         case dw_val_class_unsigned_const:
11511           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11512                                AT_unsigned (a), "%s", name);
11513           break;
11514
11515         case dw_val_class_const_double:
11516           {
11517             unsigned HOST_WIDE_INT first, second;
11518
11519             if (HOST_BITS_PER_WIDE_INT >= 64)
11520               dw2_asm_output_data (1,
11521                                    2 * HOST_BITS_PER_WIDE_INT
11522                                    / HOST_BITS_PER_CHAR,
11523                                    NULL);
11524
11525             if (WORDS_BIG_ENDIAN)
11526               {
11527                 first = a->dw_attr_val.v.val_double.high;
11528                 second = a->dw_attr_val.v.val_double.low;
11529               }
11530             else
11531               {
11532                 first = a->dw_attr_val.v.val_double.low;
11533                 second = a->dw_attr_val.v.val_double.high;
11534               }
11535
11536             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11537                                  first, name);
11538             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11539                                  second, NULL);
11540           }
11541           break;
11542
11543         case dw_val_class_vec:
11544           {
11545             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11546             unsigned int len = a->dw_attr_val.v.val_vec.length;
11547             unsigned int i;
11548             unsigned char *p;
11549
11550             dw2_asm_output_data (constant_size (len * elt_size),
11551                                  len * elt_size, "%s", name);
11552             if (elt_size > sizeof (HOST_WIDE_INT))
11553               {
11554                 elt_size /= 2;
11555                 len *= 2;
11556               }
11557             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11558                  i < len;
11559                  i++, p += elt_size)
11560               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11561                                    "fp or vector constant word %u", i);
11562             break;
11563           }
11564
11565         case dw_val_class_flag:
11566           if (dwarf_version >= 4)
11567             {
11568               /* Currently all add_AT_flag calls pass in 1 as last argument,
11569                  so DW_FORM_flag_present can be used.  If that ever changes,
11570                  we'll need to use DW_FORM_flag and have some optimization
11571                  in build_abbrev_table that will change those to
11572                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11573                  the same abbrev entry.  */
11574               gcc_assert (AT_flag (a) == 1);
11575               if (flag_debug_asm)
11576                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11577                          ASM_COMMENT_START, name);
11578               break;
11579             }
11580           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11581           break;
11582
11583         case dw_val_class_loc_list:
11584           {
11585             char *sym = AT_loc_list (a)->ll_symbol;
11586
11587             gcc_assert (sym);
11588             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11589                                    "%s", name);
11590           }
11591           break;
11592
11593         case dw_val_class_die_ref:
11594           if (AT_ref_external (a))
11595             {
11596               if (use_debug_types)
11597                 {
11598                   comdat_type_node_ref type_node =
11599                     AT_ref (a)->die_id.die_type_node;
11600
11601                   gcc_assert (type_node);
11602                   output_signature (type_node->signature, name);
11603                 }
11604               else
11605                 {
11606                   char *sym = AT_ref (a)->die_id.die_symbol;
11607                   int size;
11608
11609                   gcc_assert (sym);
11610                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11611                      length, whereas in DWARF3 it's always sized as an
11612                      offset.  */
11613                   if (dwarf_version == 2)
11614                     size = DWARF2_ADDR_SIZE;
11615                   else
11616                     size = DWARF_OFFSET_SIZE;
11617                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11618                                          name);
11619                 }
11620             }
11621           else
11622             {
11623               gcc_assert (AT_ref (a)->die_offset);
11624               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11625                                    "%s", name);
11626             }
11627           break;
11628
11629         case dw_val_class_fde_ref:
11630           {
11631             char l1[20];
11632
11633             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11634                                          a->dw_attr_val.v.val_fde_index * 2);
11635             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11636                                    "%s", name);
11637           }
11638           break;
11639
11640         case dw_val_class_vms_delta:
11641           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11642                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11643                                     "%s", name);
11644           break;
11645
11646         case dw_val_class_lbl_id:
11647           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11648           break;
11649
11650         case dw_val_class_lineptr:
11651           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11652                                  debug_line_section, "%s", name);
11653           break;
11654
11655         case dw_val_class_macptr:
11656           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11657                                  debug_macinfo_section, "%s", name);
11658           break;
11659
11660         case dw_val_class_str:
11661           if (AT_string_form (a) == DW_FORM_strp)
11662             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11663                                    a->dw_attr_val.v.val_str->label,
11664                                    debug_str_section,
11665                                    "%s: \"%s\"", name, AT_string (a));
11666           else
11667             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11668           break;
11669
11670         case dw_val_class_file:
11671           {
11672             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11673
11674             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11675                                  a->dw_attr_val.v.val_file->filename);
11676             break;
11677           }
11678
11679         case dw_val_class_data8:
11680           {
11681             int i;
11682
11683             for (i = 0; i < 8; i++)
11684               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11685                                    i == 0 ? "%s" : NULL, name);
11686             break;
11687           }
11688
11689         default:
11690           gcc_unreachable ();
11691         }
11692     }
11693
11694   FOR_EACH_CHILD (die, c, output_die (c));
11695
11696   /* Add null byte to terminate sibling list.  */
11697   if (die->die_child != NULL)
11698     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11699                          (unsigned long) die->die_offset);
11700 }
11701
11702 /* Output the compilation unit that appears at the beginning of the
11703    .debug_info section, and precedes the DIE descriptions.  */
11704
11705 static void
11706 output_compilation_unit_header (void)
11707 {
11708   int ver = dwarf_version;
11709
11710   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11711     dw2_asm_output_data (4, 0xffffffff,
11712       "Initial length escape value indicating 64-bit DWARF extension");
11713   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11714                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11715                        "Length of Compilation Unit Info");
11716   dw2_asm_output_data (2, ver, "DWARF version number");
11717   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11718                          debug_abbrev_section,
11719                          "Offset Into Abbrev. Section");
11720   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11721 }
11722
11723 /* Output the compilation unit DIE and its children.  */
11724
11725 static void
11726 output_comp_unit (dw_die_ref die, int output_if_empty)
11727 {
11728   const char *secname;
11729   char *oldsym, *tmp;
11730
11731   /* Unless we are outputting main CU, we may throw away empty ones.  */
11732   if (!output_if_empty && die->die_child == NULL)
11733     return;
11734
11735   /* Even if there are no children of this DIE, we must output the information
11736      about the compilation unit.  Otherwise, on an empty translation unit, we
11737      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11738      will then complain when examining the file.  First mark all the DIEs in
11739      this CU so we know which get local refs.  */
11740   mark_dies (die);
11741
11742   build_abbrev_table (die);
11743
11744   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11745   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11746   calc_die_sizes (die);
11747
11748   oldsym = die->die_id.die_symbol;
11749   if (oldsym)
11750     {
11751       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11752
11753       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11754       secname = tmp;
11755       die->die_id.die_symbol = NULL;
11756       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11757     }
11758   else
11759     {
11760       switch_to_section (debug_info_section);
11761       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11762       info_section_emitted = true;
11763     }
11764
11765   /* Output debugging information.  */
11766   output_compilation_unit_header ();
11767   output_die (die);
11768
11769   /* Leave the marks on the main CU, so we can check them in
11770      output_pubnames.  */
11771   if (oldsym)
11772     {
11773       unmark_dies (die);
11774       die->die_id.die_symbol = oldsym;
11775     }
11776 }
11777
11778 /* Output a comdat type unit DIE and its children.  */
11779
11780 static void
11781 output_comdat_type_unit (comdat_type_node *node)
11782 {
11783   const char *secname;
11784   char *tmp;
11785   int i;
11786 #if defined (OBJECT_FORMAT_ELF)
11787   tree comdat_key;
11788 #endif
11789
11790   /* First mark all the DIEs in this CU so we know which get local refs.  */
11791   mark_dies (node->root_die);
11792
11793   build_abbrev_table (node->root_die);
11794
11795   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11796   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11797   calc_die_sizes (node->root_die);
11798
11799 #if defined (OBJECT_FORMAT_ELF)
11800   secname = ".debug_types";
11801   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11802   sprintf (tmp, "wt.");
11803   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11804     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11805   comdat_key = get_identifier (tmp);
11806   targetm.asm_out.named_section (secname,
11807                                  SECTION_DEBUG | SECTION_LINKONCE,
11808                                  comdat_key);
11809 #else
11810   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11811   sprintf (tmp, ".gnu.linkonce.wt.");
11812   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11813     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11814   secname = tmp;
11815   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11816 #endif
11817
11818   /* Output debugging information.  */
11819   output_compilation_unit_header ();
11820   output_signature (node->signature, "Type Signature");
11821   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11822                        "Offset to Type DIE");
11823   output_die (node->root_die);
11824
11825   unmark_dies (node->root_die);
11826 }
11827
11828 /* Return the DWARF2/3 pubname associated with a decl.  */
11829
11830 static const char *
11831 dwarf2_name (tree decl, int scope)
11832 {
11833   if (DECL_NAMELESS (decl))
11834     return NULL;
11835   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11836 }
11837
11838 /* Add a new entry to .debug_pubnames if appropriate.  */
11839
11840 static void
11841 add_pubname_string (const char *str, dw_die_ref die)
11842 {
11843   if (targetm.want_debug_pub_sections)
11844     {
11845       pubname_entry e;
11846
11847       e.die = die;
11848       e.name = xstrdup (str);
11849       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11850     }
11851 }
11852
11853 static void
11854 add_pubname (tree decl, dw_die_ref die)
11855 {
11856   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11857     {
11858       const char *name = dwarf2_name (decl, 1);
11859       if (name)
11860         add_pubname_string (name, die);
11861     }
11862 }
11863
11864 /* Add a new entry to .debug_pubtypes if appropriate.  */
11865
11866 static void
11867 add_pubtype (tree decl, dw_die_ref die)
11868 {
11869   pubname_entry e;
11870
11871   if (!targetm.want_debug_pub_sections)
11872     return;
11873
11874   e.name = NULL;
11875   if ((TREE_PUBLIC (decl)
11876        || is_cu_die (die->die_parent))
11877       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11878     {
11879       e.die = die;
11880       if (TYPE_P (decl))
11881         {
11882           if (TYPE_NAME (decl))
11883             {
11884               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11885                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11886               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11887                        && DECL_NAME (TYPE_NAME (decl)))
11888                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11889               else
11890                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11891             }
11892         }
11893       else
11894         {
11895           e.name = dwarf2_name (decl, 1);
11896           if (e.name)
11897             e.name = xstrdup (e.name);
11898         }
11899
11900       /* If we don't have a name for the type, there's no point in adding
11901          it to the table.  */
11902       if (e.name && e.name[0] != '\0')
11903         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11904     }
11905 }
11906
11907 /* Output the public names table used to speed up access to externally
11908    visible names; or the public types table used to find type definitions.  */
11909
11910 static void
11911 output_pubnames (VEC (pubname_entry, gc) * names)
11912 {
11913   unsigned i;
11914   unsigned long pubnames_length = size_of_pubnames (names);
11915   pubname_ref pub;
11916
11917   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11918     dw2_asm_output_data (4, 0xffffffff,
11919       "Initial length escape value indicating 64-bit DWARF extension");
11920   if (names == pubname_table)
11921     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11922                          "Length of Public Names Info");
11923   else
11924     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11925                          "Length of Public Type Names Info");
11926   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11927   dw2_asm_output_data (2, 2, "DWARF Version");
11928   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11929                          debug_info_section,
11930                          "Offset of Compilation Unit Info");
11931   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11932                        "Compilation Unit Length");
11933
11934   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11935     {
11936       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11937       if (names == pubname_table)
11938         gcc_assert (pub->die->die_mark);
11939
11940       if (names != pubtype_table
11941           || pub->die->die_offset != 0
11942           || !flag_eliminate_unused_debug_types)
11943         {
11944           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11945                                "DIE offset");
11946
11947           dw2_asm_output_nstring (pub->name, -1, "external name");
11948         }
11949     }
11950
11951   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11952 }
11953
11954 /* Output the information that goes into the .debug_aranges table.
11955    Namely, define the beginning and ending address range of the
11956    text section generated for this compilation unit.  */
11957
11958 static void
11959 output_aranges (unsigned long aranges_length)
11960 {
11961   unsigned i;
11962
11963   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11964     dw2_asm_output_data (4, 0xffffffff,
11965       "Initial length escape value indicating 64-bit DWARF extension");
11966   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11967                        "Length of Address Ranges Info");
11968   /* Version number for aranges is still 2, even in DWARF3.  */
11969   dw2_asm_output_data (2, 2, "DWARF Version");
11970   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11971                          debug_info_section,
11972                          "Offset of Compilation Unit Info");
11973   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11974   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11975
11976   /* We need to align to twice the pointer size here.  */
11977   if (DWARF_ARANGES_PAD_SIZE)
11978     {
11979       /* Pad using a 2 byte words so that padding is correct for any
11980          pointer size.  */
11981       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11982                            2 * DWARF2_ADDR_SIZE);
11983       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11984         dw2_asm_output_data (2, 0, NULL);
11985     }
11986
11987   /* It is necessary not to output these entries if the sections were
11988      not used; if the sections were not used, the length will be 0 and
11989      the address may end up as 0 if the section is discarded by ld
11990      --gc-sections, leaving an invalid (0, 0) entry that can be
11991      confused with the terminator.  */
11992   if (text_section_used)
11993     {
11994       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11995       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11996                             text_section_label, "Length");
11997     }
11998   if (cold_text_section_used)
11999     {
12000       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
12001                            "Address");
12002       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
12003                             cold_text_section_label, "Length");
12004     }
12005
12006   if (have_multiple_function_sections)
12007     {
12008       unsigned fde_idx = 0;
12009
12010       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
12011         {
12012           dw_fde_ref fde = &fde_table[fde_idx];
12013
12014           if (!fde->in_std_section)
12015             {
12016               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
12017                                    "Address");
12018               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
12019                                     fde->dw_fde_begin, "Length");
12020             }
12021           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
12022             {
12023               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
12024                                    "Address");
12025               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
12026                                     fde->dw_fde_second_begin, "Length");
12027             }
12028         }
12029     }
12030
12031   /* Output the terminator words.  */
12032   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12033   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12034 }
12035
12036 /* Add a new entry to .debug_ranges.  Return the offset at which it
12037    was placed.  */
12038
12039 static unsigned int
12040 add_ranges_num (int num)
12041 {
12042   unsigned int in_use = ranges_table_in_use;
12043
12044   if (in_use == ranges_table_allocated)
12045     {
12046       ranges_table_allocated += RANGES_TABLE_INCREMENT;
12047       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
12048                                     ranges_table_allocated);
12049       memset (ranges_table + ranges_table_in_use, 0,
12050               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
12051     }
12052
12053   ranges_table[in_use].num = num;
12054   ranges_table_in_use = in_use + 1;
12055
12056   return in_use * 2 * DWARF2_ADDR_SIZE;
12057 }
12058
12059 /* Add a new entry to .debug_ranges corresponding to a block, or a
12060    range terminator if BLOCK is NULL.  */
12061
12062 static unsigned int
12063 add_ranges (const_tree block)
12064 {
12065   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
12066 }
12067
12068 /* Add a new entry to .debug_ranges corresponding to a pair of
12069    labels.  */
12070
12071 static void
12072 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
12073                       bool *added)
12074 {
12075   unsigned int in_use = ranges_by_label_in_use;
12076   unsigned int offset;
12077
12078   if (in_use == ranges_by_label_allocated)
12079     {
12080       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
12081       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
12082                                        ranges_by_label,
12083                                        ranges_by_label_allocated);
12084       memset (ranges_by_label + ranges_by_label_in_use, 0,
12085               RANGES_TABLE_INCREMENT
12086               * sizeof (struct dw_ranges_by_label_struct));
12087     }
12088
12089   ranges_by_label[in_use].begin = begin;
12090   ranges_by_label[in_use].end = end;
12091   ranges_by_label_in_use = in_use + 1;
12092
12093   offset = add_ranges_num (-(int)in_use - 1);
12094   if (!*added)
12095     {
12096       add_AT_range_list (die, DW_AT_ranges, offset);
12097       *added = true;
12098     }
12099 }
12100
12101 static void
12102 output_ranges (void)
12103 {
12104   unsigned i;
12105   static const char *const start_fmt = "Offset %#x";
12106   const char *fmt = start_fmt;
12107
12108   for (i = 0; i < ranges_table_in_use; i++)
12109     {
12110       int block_num = ranges_table[i].num;
12111
12112       if (block_num > 0)
12113         {
12114           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12115           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12116
12117           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12118           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12119
12120           /* If all code is in the text section, then the compilation
12121              unit base address defaults to DW_AT_low_pc, which is the
12122              base of the text section.  */
12123           if (!have_multiple_function_sections)
12124             {
12125               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12126                                     text_section_label,
12127                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12128               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12129                                     text_section_label, NULL);
12130             }
12131
12132           /* Otherwise, the compilation unit base address is zero,
12133              which allows us to use absolute addresses, and not worry
12134              about whether the target supports cross-section
12135              arithmetic.  */
12136           else
12137             {
12138               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12139                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12140               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12141             }
12142
12143           fmt = NULL;
12144         }
12145
12146       /* Negative block_num stands for an index into ranges_by_label.  */
12147       else if (block_num < 0)
12148         {
12149           int lab_idx = - block_num - 1;
12150
12151           if (!have_multiple_function_sections)
12152             {
12153               gcc_unreachable ();
12154 #if 0
12155               /* If we ever use add_ranges_by_labels () for a single
12156                  function section, all we have to do is to take out
12157                  the #if 0 above.  */
12158               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12159                                     ranges_by_label[lab_idx].begin,
12160                                     text_section_label,
12161                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12162               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12163                                     ranges_by_label[lab_idx].end,
12164                                     text_section_label, NULL);
12165 #endif
12166             }
12167           else
12168             {
12169               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12170                                    ranges_by_label[lab_idx].begin,
12171                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12172               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12173                                    ranges_by_label[lab_idx].end,
12174                                    NULL);
12175             }
12176         }
12177       else
12178         {
12179           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12180           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12181           fmt = start_fmt;
12182         }
12183     }
12184 }
12185
12186 /* Data structure containing information about input files.  */
12187 struct file_info
12188 {
12189   const char *path;     /* Complete file name.  */
12190   const char *fname;    /* File name part.  */
12191   int length;           /* Length of entire string.  */
12192   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
12193   int dir_idx;          /* Index in directory table.  */
12194 };
12195
12196 /* Data structure containing information about directories with source
12197    files.  */
12198 struct dir_info
12199 {
12200   const char *path;     /* Path including directory name.  */
12201   int length;           /* Path length.  */
12202   int prefix;           /* Index of directory entry which is a prefix.  */
12203   int count;            /* Number of files in this directory.  */
12204   int dir_idx;          /* Index of directory used as base.  */
12205 };
12206
12207 /* Callback function for file_info comparison.  We sort by looking at
12208    the directories in the path.  */
12209
12210 static int
12211 file_info_cmp (const void *p1, const void *p2)
12212 {
12213   const struct file_info *const s1 = (const struct file_info *) p1;
12214   const struct file_info *const s2 = (const struct file_info *) p2;
12215   const unsigned char *cp1;
12216   const unsigned char *cp2;
12217
12218   /* Take care of file names without directories.  We need to make sure that
12219      we return consistent values to qsort since some will get confused if
12220      we return the same value when identical operands are passed in opposite
12221      orders.  So if neither has a directory, return 0 and otherwise return
12222      1 or -1 depending on which one has the directory.  */
12223   if ((s1->path == s1->fname || s2->path == s2->fname))
12224     return (s2->path == s2->fname) - (s1->path == s1->fname);
12225
12226   cp1 = (const unsigned char *) s1->path;
12227   cp2 = (const unsigned char *) s2->path;
12228
12229   while (1)
12230     {
12231       ++cp1;
12232       ++cp2;
12233       /* Reached the end of the first path?  If so, handle like above.  */
12234       if ((cp1 == (const unsigned char *) s1->fname)
12235           || (cp2 == (const unsigned char *) s2->fname))
12236         return ((cp2 == (const unsigned char *) s2->fname)
12237                 - (cp1 == (const unsigned char *) s1->fname));
12238
12239       /* Character of current path component the same?  */
12240       else if (*cp1 != *cp2)
12241         return *cp1 - *cp2;
12242     }
12243 }
12244
12245 struct file_name_acquire_data
12246 {
12247   struct file_info *files;
12248   int used_files;
12249   int max_files;
12250 };
12251
12252 /* Traversal function for the hash table.  */
12253
12254 static int
12255 file_name_acquire (void ** slot, void *data)
12256 {
12257   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12258   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12259   struct file_info *fi;
12260   const char *f;
12261
12262   gcc_assert (fnad->max_files >= d->emitted_number);
12263
12264   if (! d->emitted_number)
12265     return 1;
12266
12267   gcc_assert (fnad->max_files != fnad->used_files);
12268
12269   fi = fnad->files + fnad->used_files++;
12270
12271   /* Skip all leading "./".  */
12272   f = d->filename;
12273   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12274     f += 2;
12275
12276   /* Create a new array entry.  */
12277   fi->path = f;
12278   fi->length = strlen (f);
12279   fi->file_idx = d;
12280
12281   /* Search for the file name part.  */
12282   f = strrchr (f, DIR_SEPARATOR);
12283 #if defined (DIR_SEPARATOR_2)
12284   {
12285     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12286
12287     if (g != NULL)
12288       {
12289         if (f == NULL || f < g)
12290           f = g;
12291       }
12292   }
12293 #endif
12294
12295   fi->fname = f == NULL ? fi->path : f + 1;
12296   return 1;
12297 }
12298
12299 /* Output the directory table and the file name table.  We try to minimize
12300    the total amount of memory needed.  A heuristic is used to avoid large
12301    slowdowns with many input files.  */
12302
12303 static void
12304 output_file_names (void)
12305 {
12306   struct file_name_acquire_data fnad;
12307   int numfiles;
12308   struct file_info *files;
12309   struct dir_info *dirs;
12310   int *saved;
12311   int *savehere;
12312   int *backmap;
12313   int ndirs;
12314   int idx_offset;
12315   int i;
12316
12317   if (!last_emitted_file)
12318     {
12319       dw2_asm_output_data (1, 0, "End directory table");
12320       dw2_asm_output_data (1, 0, "End file name table");
12321       return;
12322     }
12323
12324   numfiles = last_emitted_file->emitted_number;
12325
12326   /* Allocate the various arrays we need.  */
12327   files = XALLOCAVEC (struct file_info, numfiles);
12328   dirs = XALLOCAVEC (struct dir_info, numfiles);
12329
12330   fnad.files = files;
12331   fnad.used_files = 0;
12332   fnad.max_files = numfiles;
12333   htab_traverse (file_table, file_name_acquire, &fnad);
12334   gcc_assert (fnad.used_files == fnad.max_files);
12335
12336   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12337
12338   /* Find all the different directories used.  */
12339   dirs[0].path = files[0].path;
12340   dirs[0].length = files[0].fname - files[0].path;
12341   dirs[0].prefix = -1;
12342   dirs[0].count = 1;
12343   dirs[0].dir_idx = 0;
12344   files[0].dir_idx = 0;
12345   ndirs = 1;
12346
12347   for (i = 1; i < numfiles; i++)
12348     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12349         && memcmp (dirs[ndirs - 1].path, files[i].path,
12350                    dirs[ndirs - 1].length) == 0)
12351       {
12352         /* Same directory as last entry.  */
12353         files[i].dir_idx = ndirs - 1;
12354         ++dirs[ndirs - 1].count;
12355       }
12356     else
12357       {
12358         int j;
12359
12360         /* This is a new directory.  */
12361         dirs[ndirs].path = files[i].path;
12362         dirs[ndirs].length = files[i].fname - files[i].path;
12363         dirs[ndirs].count = 1;
12364         dirs[ndirs].dir_idx = ndirs;
12365         files[i].dir_idx = ndirs;
12366
12367         /* Search for a prefix.  */
12368         dirs[ndirs].prefix = -1;
12369         for (j = 0; j < ndirs; j++)
12370           if (dirs[j].length < dirs[ndirs].length
12371               && dirs[j].length > 1
12372               && (dirs[ndirs].prefix == -1
12373                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12374               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12375             dirs[ndirs].prefix = j;
12376
12377         ++ndirs;
12378       }
12379
12380   /* Now to the actual work.  We have to find a subset of the directories which
12381      allow expressing the file name using references to the directory table
12382      with the least amount of characters.  We do not do an exhaustive search
12383      where we would have to check out every combination of every single
12384      possible prefix.  Instead we use a heuristic which provides nearly optimal
12385      results in most cases and never is much off.  */
12386   saved = XALLOCAVEC (int, ndirs);
12387   savehere = XALLOCAVEC (int, ndirs);
12388
12389   memset (saved, '\0', ndirs * sizeof (saved[0]));
12390   for (i = 0; i < ndirs; i++)
12391     {
12392       int j;
12393       int total;
12394
12395       /* We can always save some space for the current directory.  But this
12396          does not mean it will be enough to justify adding the directory.  */
12397       savehere[i] = dirs[i].length;
12398       total = (savehere[i] - saved[i]) * dirs[i].count;
12399
12400       for (j = i + 1; j < ndirs; j++)
12401         {
12402           savehere[j] = 0;
12403           if (saved[j] < dirs[i].length)
12404             {
12405               /* Determine whether the dirs[i] path is a prefix of the
12406                  dirs[j] path.  */
12407               int k;
12408
12409               k = dirs[j].prefix;
12410               while (k != -1 && k != (int) i)
12411                 k = dirs[k].prefix;
12412
12413               if (k == (int) i)
12414                 {
12415                   /* Yes it is.  We can possibly save some memory by
12416                      writing the filenames in dirs[j] relative to
12417                      dirs[i].  */
12418                   savehere[j] = dirs[i].length;
12419                   total += (savehere[j] - saved[j]) * dirs[j].count;
12420                 }
12421             }
12422         }
12423
12424       /* Check whether we can save enough to justify adding the dirs[i]
12425          directory.  */
12426       if (total > dirs[i].length + 1)
12427         {
12428           /* It's worthwhile adding.  */
12429           for (j = i; j < ndirs; j++)
12430             if (savehere[j] > 0)
12431               {
12432                 /* Remember how much we saved for this directory so far.  */
12433                 saved[j] = savehere[j];
12434
12435                 /* Remember the prefix directory.  */
12436                 dirs[j].dir_idx = i;
12437               }
12438         }
12439     }
12440
12441   /* Emit the directory name table.  */
12442   idx_offset = dirs[0].length > 0 ? 1 : 0;
12443   for (i = 1 - idx_offset; i < ndirs; i++)
12444     dw2_asm_output_nstring (dirs[i].path,
12445                             dirs[i].length
12446                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12447                             "Directory Entry: %#x", i + idx_offset);
12448
12449   dw2_asm_output_data (1, 0, "End directory table");
12450
12451   /* We have to emit them in the order of emitted_number since that's
12452      used in the debug info generation.  To do this efficiently we
12453      generate a back-mapping of the indices first.  */
12454   backmap = XALLOCAVEC (int, numfiles);
12455   for (i = 0; i < numfiles; i++)
12456     backmap[files[i].file_idx->emitted_number - 1] = i;
12457
12458   /* Now write all the file names.  */
12459   for (i = 0; i < numfiles; i++)
12460     {
12461       int file_idx = backmap[i];
12462       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12463
12464 #ifdef VMS_DEBUGGING_INFO
12465 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12466
12467       /* Setting these fields can lead to debugger miscomparisons,
12468          but VMS Debug requires them to be set correctly.  */
12469
12470       int ver;
12471       long long cdt;
12472       long siz;
12473       int maxfilelen = strlen (files[file_idx].path)
12474                                + dirs[dir_idx].length
12475                                + MAX_VMS_VERSION_LEN + 1;
12476       char *filebuf = XALLOCAVEC (char, maxfilelen);
12477
12478       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12479       snprintf (filebuf, maxfilelen, "%s;%d",
12480                 files[file_idx].path + dirs[dir_idx].length, ver);
12481
12482       dw2_asm_output_nstring
12483         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12484
12485       /* Include directory index.  */
12486       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12487
12488       /* Modification time.  */
12489       dw2_asm_output_data_uleb128
12490         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12491           ? cdt : 0,
12492          NULL);
12493
12494       /* File length in bytes.  */
12495       dw2_asm_output_data_uleb128
12496         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12497           ? siz : 0,
12498          NULL);
12499 #else
12500       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12501                               "File Entry: %#x", (unsigned) i + 1);
12502
12503       /* Include directory index.  */
12504       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12505
12506       /* Modification time.  */
12507       dw2_asm_output_data_uleb128 (0, NULL);
12508
12509       /* File length in bytes.  */
12510       dw2_asm_output_data_uleb128 (0, NULL);
12511 #endif /* VMS_DEBUGGING_INFO */
12512     }
12513
12514   dw2_asm_output_data (1, 0, "End file name table");
12515 }
12516
12517
12518 /* Output one line number table into the .debug_line section.  */
12519
12520 static void
12521 output_one_line_info_table (dw_line_info_table *table)
12522 {
12523   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12524   unsigned int current_line = 1;
12525   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12526   dw_line_info_entry *ent;
12527   size_t i;
12528
12529   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
12530     {
12531       switch (ent->opcode)
12532         {
12533         case LI_set_address:
12534           /* ??? Unfortunately, we have little choice here currently, and
12535              must always use the most general form.  GCC does not know the
12536              address delta itself, so we can't use DW_LNS_advance_pc.  Many
12537              ports do have length attributes which will give an upper bound
12538              on the address range.  We could perhaps use length attributes
12539              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
12540           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12541
12542           /* This can handle any delta.  This takes
12543              4+DWARF2_ADDR_SIZE bytes.  */
12544           dw2_asm_output_data (1, 0, "set address %s", line_label);
12545           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12546           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12547           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12548           break;
12549
12550         case LI_set_line:
12551           if (ent->val == current_line)
12552             {
12553               /* We still need to start a new row, so output a copy insn.  */
12554               dw2_asm_output_data (1, DW_LNS_copy,
12555                                    "copy line %u", current_line);
12556             }
12557           else
12558             {
12559               int line_offset = ent->val - current_line;
12560               int line_delta = line_offset - DWARF_LINE_BASE;
12561
12562               current_line = ent->val;
12563               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12564                 {
12565                   /* This can handle deltas from -10 to 234, using the current
12566                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12567                      This takes 1 byte.  */
12568                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12569                                        "line %u", current_line);
12570                 }
12571               else
12572                 {
12573                   /* This can handle any delta.  This takes at least 4 bytes,
12574                      depending on the value being encoded.  */
12575                   dw2_asm_output_data (1, DW_LNS_advance_line,
12576                                        "advance to line %u", current_line);
12577                   dw2_asm_output_data_sleb128 (line_offset, NULL);
12578                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
12579                 }
12580             }
12581           break;
12582
12583         case LI_set_file:
12584           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12585           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12586           break;
12587
12588         case LI_set_column:
12589           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12590           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12591           break;
12592
12593         case LI_negate_stmt:
12594           current_is_stmt = !current_is_stmt;
12595           dw2_asm_output_data (1, DW_LNS_negate_stmt,
12596                                "is_stmt %d", current_is_stmt);
12597           break;
12598
12599         case LI_set_prologue_end:
12600           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12601                                "set prologue end");
12602           break;
12603           
12604         case LI_set_epilogue_begin:
12605           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12606                                "set epilogue begin");
12607           break;
12608
12609         case LI_set_discriminator:
12610           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12611           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12612           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12613           dw2_asm_output_data_uleb128 (ent->val, NULL);
12614           break;
12615         }
12616     }
12617
12618   /* Emit debug info for the address of the end of the table.  */
12619   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12620   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12621   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12622   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12623
12624   dw2_asm_output_data (1, 0, "end sequence");
12625   dw2_asm_output_data_uleb128 (1, NULL);
12626   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12627 }
12628
12629 /* Output the source line number correspondence information.  This
12630    information goes into the .debug_line section.  */
12631
12632 static void
12633 output_line_info (void)
12634 {
12635   char l1[20], l2[20], p1[20], p2[20];
12636   int ver = dwarf_version;
12637   bool saw_one = false;
12638   int opc;
12639
12640   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12641   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12642   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12643   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12644
12645   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12646     dw2_asm_output_data (4, 0xffffffff,
12647       "Initial length escape value indicating 64-bit DWARF extension");
12648   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12649                         "Length of Source Line Info");
12650   ASM_OUTPUT_LABEL (asm_out_file, l1);
12651
12652   dw2_asm_output_data (2, ver, "DWARF Version");
12653   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12654   ASM_OUTPUT_LABEL (asm_out_file, p1);
12655
12656   /* Define the architecture-dependent minimum instruction length (in bytes).
12657      In this implementation of DWARF, this field is used for information
12658      purposes only.  Since GCC generates assembly language, we have no
12659      a priori knowledge of how many instruction bytes are generated for each
12660      source line, and therefore can use only the DW_LNE_set_address and
12661      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
12662      this as '1', which is "correct enough" for all architectures,
12663      and don't let the target override.  */
12664   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12665
12666   if (ver >= 4)
12667     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12668                          "Maximum Operations Per Instruction");
12669   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12670                        "Default is_stmt_start flag");
12671   dw2_asm_output_data (1, DWARF_LINE_BASE,
12672                        "Line Base Value (Special Opcodes)");
12673   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12674                        "Line Range Value (Special Opcodes)");
12675   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12676                        "Special Opcode Base");
12677
12678   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12679     {
12680       int n_op_args;
12681       switch (opc)
12682         {
12683         case DW_LNS_advance_pc:
12684         case DW_LNS_advance_line:
12685         case DW_LNS_set_file:
12686         case DW_LNS_set_column:
12687         case DW_LNS_fixed_advance_pc:
12688         case DW_LNS_set_isa:
12689           n_op_args = 1;
12690           break;
12691         default:
12692           n_op_args = 0;
12693           break;
12694         }
12695
12696       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12697                            opc, n_op_args);
12698     }
12699
12700   /* Write out the information about the files we use.  */
12701   output_file_names ();
12702   ASM_OUTPUT_LABEL (asm_out_file, p2);
12703
12704   if (separate_line_info)
12705     {
12706       dw_line_info_table *table;
12707       size_t i;
12708
12709       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
12710         if (table->in_use)
12711           {
12712             output_one_line_info_table (table);
12713             saw_one = true;
12714           }
12715     }
12716   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12717     {
12718       output_one_line_info_table (cold_text_section_line_info);
12719       saw_one = true;
12720     }
12721
12722   /* ??? Some Darwin linkers crash on a .debug_line section with no
12723      sequences.  Further, merely a DW_LNE_end_sequence entry is not
12724      sufficient -- the address column must also be initialized.
12725      Make sure to output at least one set_address/end_sequence pair,
12726      choosing .text since that section is always present.  */
12727   if (text_section_line_info->in_use || !saw_one)
12728     output_one_line_info_table (text_section_line_info);
12729
12730   /* Output the marker for the end of the line number info.  */
12731   ASM_OUTPUT_LABEL (asm_out_file, l2);
12732 }
12733 \f
12734 /* Given a pointer to a tree node for some base type, return a pointer to
12735    a DIE that describes the given type.
12736
12737    This routine must only be called for GCC type nodes that correspond to
12738    Dwarf base (fundamental) types.  */
12739
12740 static dw_die_ref
12741 base_type_die (tree type)
12742 {
12743   dw_die_ref base_type_result;
12744   enum dwarf_type encoding;
12745
12746   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12747     return 0;
12748
12749   /* If this is a subtype that should not be emitted as a subrange type,
12750      use the base type.  See subrange_type_for_debug_p.  */
12751   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12752     type = TREE_TYPE (type);
12753
12754   switch (TREE_CODE (type))
12755     {
12756     case INTEGER_TYPE:
12757       if ((dwarf_version >= 4 || !dwarf_strict)
12758           && TYPE_NAME (type)
12759           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12760           && DECL_IS_BUILTIN (TYPE_NAME (type))
12761           && DECL_NAME (TYPE_NAME (type)))
12762         {
12763           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12764           if (strcmp (name, "char16_t") == 0
12765               || strcmp (name, "char32_t") == 0)
12766             {
12767               encoding = DW_ATE_UTF;
12768               break;
12769             }
12770         }
12771       if (TYPE_STRING_FLAG (type))
12772         {
12773           if (TYPE_UNSIGNED (type))
12774             encoding = DW_ATE_unsigned_char;
12775           else
12776             encoding = DW_ATE_signed_char;
12777         }
12778       else if (TYPE_UNSIGNED (type))
12779         encoding = DW_ATE_unsigned;
12780       else
12781         encoding = DW_ATE_signed;
12782       break;
12783
12784     case REAL_TYPE:
12785       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12786         {
12787           if (dwarf_version >= 3 || !dwarf_strict)
12788             encoding = DW_ATE_decimal_float;
12789           else
12790             encoding = DW_ATE_lo_user;
12791         }
12792       else
12793         encoding = DW_ATE_float;
12794       break;
12795
12796     case FIXED_POINT_TYPE:
12797       if (!(dwarf_version >= 3 || !dwarf_strict))
12798         encoding = DW_ATE_lo_user;
12799       else if (TYPE_UNSIGNED (type))
12800         encoding = DW_ATE_unsigned_fixed;
12801       else
12802         encoding = DW_ATE_signed_fixed;
12803       break;
12804
12805       /* Dwarf2 doesn't know anything about complex ints, so use
12806          a user defined type for it.  */
12807     case COMPLEX_TYPE:
12808       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12809         encoding = DW_ATE_complex_float;
12810       else
12811         encoding = DW_ATE_lo_user;
12812       break;
12813
12814     case BOOLEAN_TYPE:
12815       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12816       encoding = DW_ATE_boolean;
12817       break;
12818
12819     default:
12820       /* No other TREE_CODEs are Dwarf fundamental types.  */
12821       gcc_unreachable ();
12822     }
12823
12824   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12825
12826   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12827                    int_size_in_bytes (type));
12828   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12829
12830   return base_type_result;
12831 }
12832
12833 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12834    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12835
12836 static inline int
12837 is_base_type (tree type)
12838 {
12839   switch (TREE_CODE (type))
12840     {
12841     case ERROR_MARK:
12842     case VOID_TYPE:
12843     case INTEGER_TYPE:
12844     case REAL_TYPE:
12845     case FIXED_POINT_TYPE:
12846     case COMPLEX_TYPE:
12847     case BOOLEAN_TYPE:
12848       return 1;
12849
12850     case ARRAY_TYPE:
12851     case RECORD_TYPE:
12852     case UNION_TYPE:
12853     case QUAL_UNION_TYPE:
12854     case ENUMERAL_TYPE:
12855     case FUNCTION_TYPE:
12856     case METHOD_TYPE:
12857     case POINTER_TYPE:
12858     case REFERENCE_TYPE:
12859     case NULLPTR_TYPE:
12860     case OFFSET_TYPE:
12861     case LANG_TYPE:
12862     case VECTOR_TYPE:
12863       return 0;
12864
12865     default:
12866       gcc_unreachable ();
12867     }
12868
12869   return 0;
12870 }
12871
12872 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12873    node, return the size in bits for the type if it is a constant, or else
12874    return the alignment for the type if the type's size is not constant, or
12875    else return BITS_PER_WORD if the type actually turns out to be an
12876    ERROR_MARK node.  */
12877
12878 static inline unsigned HOST_WIDE_INT
12879 simple_type_size_in_bits (const_tree type)
12880 {
12881   if (TREE_CODE (type) == ERROR_MARK)
12882     return BITS_PER_WORD;
12883   else if (TYPE_SIZE (type) == NULL_TREE)
12884     return 0;
12885   else if (host_integerp (TYPE_SIZE (type), 1))
12886     return tree_low_cst (TYPE_SIZE (type), 1);
12887   else
12888     return TYPE_ALIGN (type);
12889 }
12890
12891 /* Similarly, but return a double_int instead of UHWI.  */
12892
12893 static inline double_int
12894 double_int_type_size_in_bits (const_tree type)
12895 {
12896   if (TREE_CODE (type) == ERROR_MARK)
12897     return uhwi_to_double_int (BITS_PER_WORD);
12898   else if (TYPE_SIZE (type) == NULL_TREE)
12899     return double_int_zero;
12900   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12901     return tree_to_double_int (TYPE_SIZE (type));
12902   else
12903     return uhwi_to_double_int (TYPE_ALIGN (type));
12904 }
12905
12906 /*  Given a pointer to a tree node for a subrange type, return a pointer
12907     to a DIE that describes the given type.  */
12908
12909 static dw_die_ref
12910 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12911 {
12912   dw_die_ref subrange_die;
12913   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12914
12915   if (context_die == NULL)
12916     context_die = comp_unit_die ();
12917
12918   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12919
12920   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12921     {
12922       /* The size of the subrange type and its base type do not match,
12923          so we need to generate a size attribute for the subrange type.  */
12924       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12925     }
12926
12927   if (low)
12928     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12929   if (high)
12930     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12931
12932   return subrange_die;
12933 }
12934
12935 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12936    entry that chains various modifiers in front of the given type.  */
12937
12938 static dw_die_ref
12939 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12940                    dw_die_ref context_die)
12941 {
12942   enum tree_code code = TREE_CODE (type);
12943   dw_die_ref mod_type_die;
12944   dw_die_ref sub_die = NULL;
12945   tree item_type = NULL;
12946   tree qualified_type;
12947   tree name, low, high;
12948
12949   if (code == ERROR_MARK)
12950     return NULL;
12951
12952   /* See if we already have the appropriately qualified variant of
12953      this type.  */
12954   qualified_type
12955     = get_qualified_type (type,
12956                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12957                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12958
12959   if (qualified_type == sizetype
12960       && TYPE_NAME (qualified_type)
12961       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12962     {
12963       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12964
12965       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12966                            && TYPE_PRECISION (t)
12967                            == TYPE_PRECISION (qualified_type)
12968                            && TYPE_UNSIGNED (t)
12969                            == TYPE_UNSIGNED (qualified_type));
12970       qualified_type = t;
12971     }
12972
12973   /* If we do, then we can just use its DIE, if it exists.  */
12974   if (qualified_type)
12975     {
12976       mod_type_die = lookup_type_die (qualified_type);
12977       if (mod_type_die)
12978         return mod_type_die;
12979     }
12980
12981   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12982
12983   /* Handle C typedef types.  */
12984   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12985       && !DECL_ARTIFICIAL (name))
12986     {
12987       tree dtype = TREE_TYPE (name);
12988
12989       if (qualified_type == dtype)
12990         {
12991           /* For a named type, use the typedef.  */
12992           gen_type_die (qualified_type, context_die);
12993           return lookup_type_die (qualified_type);
12994         }
12995       else if (is_const_type < TYPE_READONLY (dtype)
12996                || is_volatile_type < TYPE_VOLATILE (dtype)
12997                || (is_const_type <= TYPE_READONLY (dtype)
12998                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12999                    && DECL_ORIGINAL_TYPE (name) != type))
13000         /* cv-unqualified version of named type.  Just use the unnamed
13001            type to which it refers.  */
13002         return modified_type_die (DECL_ORIGINAL_TYPE (name),
13003                                   is_const_type, is_volatile_type,
13004                                   context_die);
13005       /* Else cv-qualified version of named type; fall through.  */
13006     }
13007
13008   if (is_const_type
13009       /* If both is_const_type and is_volatile_type, prefer the path
13010          which leads to a qualified type.  */
13011       && (!is_volatile_type
13012           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
13013           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
13014     {
13015       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
13016       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
13017     }
13018   else if (is_volatile_type)
13019     {
13020       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
13021       sub_die = modified_type_die (type, is_const_type, 0, context_die);
13022     }
13023   else if (code == POINTER_TYPE)
13024     {
13025       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
13026       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13027                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13028       item_type = TREE_TYPE (type);
13029       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13030         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13031                          TYPE_ADDR_SPACE (item_type));
13032     }
13033   else if (code == REFERENCE_TYPE)
13034     {
13035       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13036         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
13037                                 type);
13038       else
13039         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
13040       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13041                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13042       item_type = TREE_TYPE (type);
13043       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13044         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13045                          TYPE_ADDR_SPACE (item_type));
13046     }
13047   else if (code == INTEGER_TYPE
13048            && TREE_TYPE (type) != NULL_TREE
13049            && subrange_type_for_debug_p (type, &low, &high))
13050     {
13051       mod_type_die = subrange_type_die (type, low, high, context_die);
13052       item_type = TREE_TYPE (type);
13053     }
13054   else if (is_base_type (type))
13055     mod_type_die = base_type_die (type);
13056   else
13057     {
13058       gen_type_die (type, context_die);
13059
13060       /* We have to get the type_main_variant here (and pass that to the
13061          `lookup_type_die' routine) because the ..._TYPE node we have
13062          might simply be a *copy* of some original type node (where the
13063          copy was created to help us keep track of typedef names) and
13064          that copy might have a different TYPE_UID from the original
13065          ..._TYPE node.  */
13066       if (TREE_CODE (type) != VECTOR_TYPE)
13067         return lookup_type_die (type_main_variant (type));
13068       else
13069         /* Vectors have the debugging information in the type,
13070            not the main variant.  */
13071         return lookup_type_die (type);
13072     }
13073
13074   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13075      don't output a DW_TAG_typedef, since there isn't one in the
13076      user's program; just attach a DW_AT_name to the type.
13077      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13078      if the base type already has the same name.  */
13079   if (name
13080       && ((TREE_CODE (name) != TYPE_DECL
13081            && (qualified_type == TYPE_MAIN_VARIANT (type)
13082                || (!is_const_type && !is_volatile_type)))
13083           || (TREE_CODE (name) == TYPE_DECL
13084               && TREE_TYPE (name) == qualified_type
13085               && DECL_NAME (name))))
13086     {
13087       if (TREE_CODE (name) == TYPE_DECL)
13088         /* Could just call add_name_and_src_coords_attributes here,
13089            but since this is a builtin type it doesn't have any
13090            useful source coordinates anyway.  */
13091         name = DECL_NAME (name);
13092       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13093       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13094     }
13095   /* This probably indicates a bug.  */
13096   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13097     add_name_attribute (mod_type_die, "__unknown__");
13098
13099   if (qualified_type)
13100     equate_type_number_to_die (qualified_type, mod_type_die);
13101
13102   if (item_type)
13103     /* We must do this after the equate_type_number_to_die call, in case
13104        this is a recursive type.  This ensures that the modified_type_die
13105        recursion will terminate even if the type is recursive.  Recursive
13106        types are possible in Ada.  */
13107     sub_die = modified_type_die (item_type,
13108                                  TYPE_READONLY (item_type),
13109                                  TYPE_VOLATILE (item_type),
13110                                  context_die);
13111
13112   if (sub_die != NULL)
13113     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13114
13115   return mod_type_die;
13116 }
13117
13118 /* Generate DIEs for the generic parameters of T.
13119    T must be either a generic type or a generic function.
13120    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13121
13122 static void
13123 gen_generic_params_dies (tree t)
13124 {
13125   tree parms, args;
13126   int parms_num, i;
13127   dw_die_ref die = NULL;
13128
13129   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13130     return;
13131
13132   if (TYPE_P (t))
13133     die = lookup_type_die (t);
13134   else if (DECL_P (t))
13135     die = lookup_decl_die (t);
13136
13137   gcc_assert (die);
13138
13139   parms = lang_hooks.get_innermost_generic_parms (t);
13140   if (!parms)
13141     /* T has no generic parameter. It means T is neither a generic type
13142        or function. End of story.  */
13143     return;
13144
13145   parms_num = TREE_VEC_LENGTH (parms);
13146   args = lang_hooks.get_innermost_generic_args (t);
13147   for (i = 0; i < parms_num; i++)
13148     {
13149       tree parm, arg, arg_pack_elems;
13150
13151       parm = TREE_VEC_ELT (parms, i);
13152       arg = TREE_VEC_ELT (args, i);
13153       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13154       gcc_assert (parm && TREE_VALUE (parm) && arg);
13155
13156       if (parm && TREE_VALUE (parm) && arg)
13157         {
13158           /* If PARM represents a template parameter pack,
13159              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13160              by DW_TAG_template_*_parameter DIEs for the argument
13161              pack elements of ARG. Note that ARG would then be
13162              an argument pack.  */
13163           if (arg_pack_elems)
13164             template_parameter_pack_die (TREE_VALUE (parm),
13165                                          arg_pack_elems,
13166                                          die);
13167           else
13168             generic_parameter_die (TREE_VALUE (parm), arg,
13169                                    true /* Emit DW_AT_name */, die);
13170         }
13171     }
13172 }
13173
13174 /* Create and return a DIE for PARM which should be
13175    the representation of a generic type parameter.
13176    For instance, in the C++ front end, PARM would be a template parameter.
13177    ARG is the argument to PARM.
13178    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13179    name of the PARM.
13180    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13181    as a child node.  */
13182
13183 static dw_die_ref
13184 generic_parameter_die (tree parm, tree arg,
13185                        bool emit_name_p,
13186                        dw_die_ref parent_die)
13187 {
13188   dw_die_ref tmpl_die = NULL;
13189   const char *name = NULL;
13190
13191   if (!parm || !DECL_NAME (parm) || !arg)
13192     return NULL;
13193
13194   /* We support non-type generic parameters and arguments,
13195      type generic parameters and arguments, as well as
13196      generic generic parameters (a.k.a. template template parameters in C++)
13197      and arguments.  */
13198   if (TREE_CODE (parm) == PARM_DECL)
13199     /* PARM is a nontype generic parameter  */
13200     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13201   else if (TREE_CODE (parm) == TYPE_DECL)
13202     /* PARM is a type generic parameter.  */
13203     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13204   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13205     /* PARM is a generic generic parameter.
13206        Its DIE is a GNU extension. It shall have a
13207        DW_AT_name attribute to represent the name of the template template
13208        parameter, and a DW_AT_GNU_template_name attribute to represent the
13209        name of the template template argument.  */
13210     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13211                         parent_die, parm);
13212   else
13213     gcc_unreachable ();
13214
13215   if (tmpl_die)
13216     {
13217       tree tmpl_type;
13218
13219       /* If PARM is a generic parameter pack, it means we are
13220          emitting debug info for a template argument pack element.
13221          In other terms, ARG is a template argument pack element.
13222          In that case, we don't emit any DW_AT_name attribute for
13223          the die.  */
13224       if (emit_name_p)
13225         {
13226           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13227           gcc_assert (name);
13228           add_AT_string (tmpl_die, DW_AT_name, name);
13229         }
13230
13231       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13232         {
13233           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13234              TMPL_DIE should have a child DW_AT_type attribute that is set
13235              to the type of the argument to PARM, which is ARG.
13236              If PARM is a type generic parameter, TMPL_DIE should have a
13237              child DW_AT_type that is set to ARG.  */
13238           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13239           add_type_attribute (tmpl_die, tmpl_type, 0,
13240                               TREE_THIS_VOLATILE (tmpl_type),
13241                               parent_die);
13242         }
13243       else
13244         {
13245           /* So TMPL_DIE is a DIE representing a
13246              a generic generic template parameter, a.k.a template template
13247              parameter in C++ and arg is a template.  */
13248
13249           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13250              to the name of the argument.  */
13251           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13252           if (name)
13253             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13254         }
13255
13256       if (TREE_CODE (parm) == PARM_DECL)
13257         /* So PARM is a non-type generic parameter.
13258            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13259            attribute of TMPL_DIE which value represents the value
13260            of ARG.
13261            We must be careful here:
13262            The value of ARG might reference some function decls.
13263            We might currently be emitting debug info for a generic
13264            type and types are emitted before function decls, we don't
13265            know if the function decls referenced by ARG will actually be
13266            emitted after cgraph computations.
13267            So must defer the generation of the DW_AT_const_value to
13268            after cgraph is ready.  */
13269         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13270     }
13271
13272   return tmpl_die;
13273 }
13274
13275 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13276    PARM_PACK must be a template parameter pack. The returned DIE
13277    will be child DIE of PARENT_DIE.  */
13278
13279 static dw_die_ref
13280 template_parameter_pack_die (tree parm_pack,
13281                              tree parm_pack_args,
13282                              dw_die_ref parent_die)
13283 {
13284   dw_die_ref die;
13285   int j;
13286
13287   gcc_assert (parent_die && parm_pack);
13288
13289   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13290   add_name_and_src_coords_attributes (die, parm_pack);
13291   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13292     generic_parameter_die (parm_pack,
13293                            TREE_VEC_ELT (parm_pack_args, j),
13294                            false /* Don't emit DW_AT_name */,
13295                            die);
13296   return die;
13297 }
13298
13299 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13300    an enumerated type.  */
13301
13302 static inline int
13303 type_is_enum (const_tree type)
13304 {
13305   return TREE_CODE (type) == ENUMERAL_TYPE;
13306 }
13307
13308 /* Return the DBX register number described by a given RTL node.  */
13309
13310 static unsigned int
13311 dbx_reg_number (const_rtx rtl)
13312 {
13313   unsigned regno = REGNO (rtl);
13314
13315   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13316
13317 #ifdef LEAF_REG_REMAP
13318   if (current_function_uses_only_leaf_regs)
13319     {
13320       int leaf_reg = LEAF_REG_REMAP (regno);
13321       if (leaf_reg != -1)
13322         regno = (unsigned) leaf_reg;
13323     }
13324 #endif
13325
13326   return DBX_REGISTER_NUMBER (regno);
13327 }
13328
13329 /* Optionally add a DW_OP_piece term to a location description expression.
13330    DW_OP_piece is only added if the location description expression already
13331    doesn't end with DW_OP_piece.  */
13332
13333 static void
13334 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13335 {
13336   dw_loc_descr_ref loc;
13337
13338   if (*list_head != NULL)
13339     {
13340       /* Find the end of the chain.  */
13341       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13342         ;
13343
13344       if (loc->dw_loc_opc != DW_OP_piece)
13345         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13346     }
13347 }
13348
13349 /* Return a location descriptor that designates a machine register or
13350    zero if there is none.  */
13351
13352 static dw_loc_descr_ref
13353 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13354 {
13355   rtx regs;
13356
13357   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13358     return 0;
13359
13360   /* We only use "frame base" when we're sure we're talking about the
13361      post-prologue local stack frame.  We do this by *not* running
13362      register elimination until this point, and recognizing the special
13363      argument pointer and soft frame pointer rtx's.
13364      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13365   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13366       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13367     {
13368       dw_loc_descr_ref result = NULL;
13369
13370       if (dwarf_version >= 4 || !dwarf_strict)
13371         {
13372           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13373                                        initialized);
13374           if (result)
13375             add_loc_descr (&result,
13376                            new_loc_descr (DW_OP_stack_value, 0, 0));
13377         }
13378       return result;
13379     }
13380
13381   regs = targetm.dwarf_register_span (rtl);
13382
13383   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13384     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13385   else
13386     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13387 }
13388
13389 /* Return a location descriptor that designates a machine register for
13390    a given hard register number.  */
13391
13392 static dw_loc_descr_ref
13393 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13394 {
13395   dw_loc_descr_ref reg_loc_descr;
13396
13397   if (regno <= 31)
13398     reg_loc_descr
13399       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13400   else
13401     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13402
13403   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13404     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13405
13406   return reg_loc_descr;
13407 }
13408
13409 /* Given an RTL of a register, return a location descriptor that
13410    designates a value that spans more than one register.  */
13411
13412 static dw_loc_descr_ref
13413 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13414                              enum var_init_status initialized)
13415 {
13416   int nregs, size, i;
13417   unsigned reg;
13418   dw_loc_descr_ref loc_result = NULL;
13419
13420   reg = REGNO (rtl);
13421 #ifdef LEAF_REG_REMAP
13422   if (current_function_uses_only_leaf_regs)
13423     {
13424       int leaf_reg = LEAF_REG_REMAP (reg);
13425       if (leaf_reg != -1)
13426         reg = (unsigned) leaf_reg;
13427     }
13428 #endif
13429   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13430   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13431
13432   /* Simple, contiguous registers.  */
13433   if (regs == NULL_RTX)
13434     {
13435       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13436
13437       loc_result = NULL;
13438       while (nregs--)
13439         {
13440           dw_loc_descr_ref t;
13441
13442           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13443                                       VAR_INIT_STATUS_INITIALIZED);
13444           add_loc_descr (&loc_result, t);
13445           add_loc_descr_op_piece (&loc_result, size);
13446           ++reg;
13447         }
13448       return loc_result;
13449     }
13450
13451   /* Now onto stupid register sets in non contiguous locations.  */
13452
13453   gcc_assert (GET_CODE (regs) == PARALLEL);
13454
13455   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13456   loc_result = NULL;
13457
13458   for (i = 0; i < XVECLEN (regs, 0); ++i)
13459     {
13460       dw_loc_descr_ref t;
13461
13462       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13463                                   VAR_INIT_STATUS_INITIALIZED);
13464       add_loc_descr (&loc_result, t);
13465       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13466       add_loc_descr_op_piece (&loc_result, size);
13467     }
13468
13469   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13470     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13471   return loc_result;
13472 }
13473
13474 /* Return a location descriptor that designates a constant.  */
13475
13476 static dw_loc_descr_ref
13477 int_loc_descriptor (HOST_WIDE_INT i)
13478 {
13479   enum dwarf_location_atom op;
13480
13481   /* Pick the smallest representation of a constant, rather than just
13482      defaulting to the LEB encoding.  */
13483   if (i >= 0)
13484     {
13485       if (i <= 31)
13486         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13487       else if (i <= 0xff)
13488         op = DW_OP_const1u;
13489       else if (i <= 0xffff)
13490         op = DW_OP_const2u;
13491       else if (HOST_BITS_PER_WIDE_INT == 32
13492                || i <= 0xffffffff)
13493         op = DW_OP_const4u;
13494       else
13495         op = DW_OP_constu;
13496     }
13497   else
13498     {
13499       if (i >= -0x80)
13500         op = DW_OP_const1s;
13501       else if (i >= -0x8000)
13502         op = DW_OP_const2s;
13503       else if (HOST_BITS_PER_WIDE_INT == 32
13504                || i >= -0x80000000)
13505         op = DW_OP_const4s;
13506       else
13507         op = DW_OP_consts;
13508     }
13509
13510   return new_loc_descr (op, i, 0);
13511 }
13512
13513 /* Return loc description representing "address" of integer value.
13514    This can appear only as toplevel expression.  */
13515
13516 static dw_loc_descr_ref
13517 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13518 {
13519   int litsize;
13520   dw_loc_descr_ref loc_result = NULL;
13521
13522   if (!(dwarf_version >= 4 || !dwarf_strict))
13523     return NULL;
13524
13525   if (i >= 0)
13526     {
13527       if (i <= 31)
13528         litsize = 1;
13529       else if (i <= 0xff)
13530         litsize = 2;
13531       else if (i <= 0xffff)
13532         litsize = 3;
13533       else if (HOST_BITS_PER_WIDE_INT == 32
13534                || i <= 0xffffffff)
13535         litsize = 5;
13536       else
13537         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13538     }
13539   else
13540     {
13541       if (i >= -0x80)
13542         litsize = 2;
13543       else if (i >= -0x8000)
13544         litsize = 3;
13545       else if (HOST_BITS_PER_WIDE_INT == 32
13546                || i >= -0x80000000)
13547         litsize = 5;
13548       else
13549         litsize = 1 + size_of_sleb128 (i);
13550     }
13551   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13552      is more compact.  For DW_OP_stack_value we need:
13553      litsize + 1 (DW_OP_stack_value)
13554      and for DW_OP_implicit_value:
13555      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13556   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13557     {
13558       loc_result = int_loc_descriptor (i);
13559       add_loc_descr (&loc_result,
13560                      new_loc_descr (DW_OP_stack_value, 0, 0));
13561       return loc_result;
13562     }
13563
13564   loc_result = new_loc_descr (DW_OP_implicit_value,
13565                               size, 0);
13566   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13567   loc_result->dw_loc_oprnd2.v.val_int = i;
13568   return loc_result;
13569 }
13570
13571 /* Return a location descriptor that designates a base+offset location.  */
13572
13573 static dw_loc_descr_ref
13574 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13575                  enum var_init_status initialized)
13576 {
13577   unsigned int regno;
13578   dw_loc_descr_ref result;
13579   dw_fde_ref fde = current_fde ();
13580
13581   /* We only use "frame base" when we're sure we're talking about the
13582      post-prologue local stack frame.  We do this by *not* running
13583      register elimination until this point, and recognizing the special
13584      argument pointer and soft frame pointer rtx's.  */
13585   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13586     {
13587       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13588
13589       if (elim != reg)
13590         {
13591           if (GET_CODE (elim) == PLUS)
13592             {
13593               offset += INTVAL (XEXP (elim, 1));
13594               elim = XEXP (elim, 0);
13595             }
13596           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13597                        && (elim == hard_frame_pointer_rtx
13598                            || elim == stack_pointer_rtx))
13599                       || elim == (frame_pointer_needed
13600                                   ? hard_frame_pointer_rtx
13601                                   : stack_pointer_rtx));
13602
13603           /* If drap register is used to align stack, use frame
13604              pointer + offset to access stack variables.  If stack
13605              is aligned without drap, use stack pointer + offset to
13606              access stack variables.  */
13607           if (crtl->stack_realign_tried
13608               && reg == frame_pointer_rtx)
13609             {
13610               int base_reg
13611                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13612                                       ? HARD_FRAME_POINTER_REGNUM
13613                                       : REGNO (elim));
13614               return new_reg_loc_descr (base_reg, offset);
13615             }
13616
13617           gcc_assert (frame_pointer_fb_offset_valid);
13618           offset += frame_pointer_fb_offset;
13619           return new_loc_descr (DW_OP_fbreg, offset, 0);
13620         }
13621     }
13622   else if (!optimize
13623            && fde
13624            && (fde->drap_reg == REGNO (reg)
13625                || fde->vdrap_reg == REGNO (reg)))
13626     {
13627       /* Use cfa+offset to represent the location of arguments passed
13628          on the stack when drap is used to align stack.
13629          Only do this when not optimizing, for optimized code var-tracking
13630          is supposed to track where the arguments live and the register
13631          used as vdrap or drap in some spot might be used for something
13632          else in other part of the routine.  */
13633       return new_loc_descr (DW_OP_fbreg, offset, 0);
13634     }
13635
13636   regno = dbx_reg_number (reg);
13637   if (regno <= 31)
13638     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13639                             offset, 0);
13640   else
13641     result = new_loc_descr (DW_OP_bregx, regno, offset);
13642
13643   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13644     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13645
13646   return result;
13647 }
13648
13649 /* Return true if this RTL expression describes a base+offset calculation.  */
13650
13651 static inline int
13652 is_based_loc (const_rtx rtl)
13653 {
13654   return (GET_CODE (rtl) == PLUS
13655           && ((REG_P (XEXP (rtl, 0))
13656                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13657                && CONST_INT_P (XEXP (rtl, 1)))));
13658 }
13659
13660 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13661    failed.  */
13662
13663 static dw_loc_descr_ref
13664 tls_mem_loc_descriptor (rtx mem)
13665 {
13666   tree base;
13667   dw_loc_descr_ref loc_result;
13668
13669   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13670     return NULL;
13671
13672   base = get_base_address (MEM_EXPR (mem));
13673   if (base == NULL
13674       || TREE_CODE (base) != VAR_DECL
13675       || !DECL_THREAD_LOCAL_P (base))
13676     return NULL;
13677
13678   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13679   if (loc_result == NULL)
13680     return NULL;
13681
13682   if (INTVAL (MEM_OFFSET (mem)))
13683     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13684
13685   return loc_result;
13686 }
13687
13688 /* Output debug info about reason why we failed to expand expression as dwarf
13689    expression.  */
13690
13691 static void
13692 expansion_failed (tree expr, rtx rtl, char const *reason)
13693 {
13694   if (dump_file && (dump_flags & TDF_DETAILS))
13695     {
13696       fprintf (dump_file, "Failed to expand as dwarf: ");
13697       if (expr)
13698         print_generic_expr (dump_file, expr, dump_flags);
13699       if (rtl)
13700         {
13701           fprintf (dump_file, "\n");
13702           print_rtl (dump_file, rtl);
13703         }
13704       fprintf (dump_file, "\nReason: %s\n", reason);
13705     }
13706 }
13707
13708 /* Helper function for const_ok_for_output, called either directly
13709    or via for_each_rtx.  */
13710
13711 static int
13712 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13713 {
13714   rtx rtl = *rtlp;
13715
13716   if (GET_CODE (rtl) == UNSPEC)
13717     {
13718       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13719          we can't express it in the debug info.  */
13720 #ifdef ENABLE_CHECKING
13721       /* Don't complain about TLS UNSPECs, those are just too hard to
13722          delegitimize.  */
13723       if (XVECLEN (rtl, 0) != 1
13724           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13725           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13726           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13727           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13728         inform (current_function_decl
13729                 ? DECL_SOURCE_LOCATION (current_function_decl)
13730                 : UNKNOWN_LOCATION,
13731 #if NUM_UNSPEC_VALUES > 0
13732                 "non-delegitimized UNSPEC %s (%d) found in variable location",
13733                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13734                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13735                 XINT (rtl, 1));
13736 #else
13737                 "non-delegitimized UNSPEC %d found in variable location",
13738                 XINT (rtl, 1));
13739 #endif
13740 #endif
13741       expansion_failed (NULL_TREE, rtl,
13742                         "UNSPEC hasn't been delegitimized.\n");
13743       return 1;
13744     }
13745
13746   if (GET_CODE (rtl) != SYMBOL_REF)
13747     return 0;
13748
13749   if (CONSTANT_POOL_ADDRESS_P (rtl))
13750     {
13751       bool marked;
13752       get_pool_constant_mark (rtl, &marked);
13753       /* If all references to this pool constant were optimized away,
13754          it was not output and thus we can't represent it.  */
13755       if (!marked)
13756         {
13757           expansion_failed (NULL_TREE, rtl,
13758                             "Constant was removed from constant pool.\n");
13759           return 1;
13760         }
13761     }
13762
13763   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13764     return 1;
13765
13766   /* Avoid references to external symbols in debug info, on several targets
13767      the linker might even refuse to link when linking a shared library,
13768      and in many other cases the relocations for .debug_info/.debug_loc are
13769      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13770      to be defined within the same shared library or executable are fine.  */
13771   if (SYMBOL_REF_EXTERNAL_P (rtl))
13772     {
13773       tree decl = SYMBOL_REF_DECL (rtl);
13774
13775       if (decl == NULL || !targetm.binds_local_p (decl))
13776         {
13777           expansion_failed (NULL_TREE, rtl,
13778                             "Symbol not defined in current TU.\n");
13779           return 1;
13780         }
13781     }
13782
13783   return 0;
13784 }
13785
13786 /* Return true if constant RTL can be emitted in DW_OP_addr or
13787    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13788    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13789
13790 static bool
13791 const_ok_for_output (rtx rtl)
13792 {
13793   if (GET_CODE (rtl) == SYMBOL_REF)
13794     return const_ok_for_output_1 (&rtl, NULL) == 0;
13795
13796   if (GET_CODE (rtl) == CONST)
13797     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13798
13799   return true;
13800 }
13801
13802 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13803    if possible, NULL otherwise.  */
13804
13805 static dw_die_ref
13806 base_type_for_mode (enum machine_mode mode, bool unsignedp)
13807 {
13808   dw_die_ref type_die;
13809   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13810
13811   if (type == NULL)
13812     return NULL;
13813   switch (TREE_CODE (type))
13814     {
13815     case INTEGER_TYPE:
13816     case REAL_TYPE:
13817       break;
13818     default:
13819       return NULL;
13820     }
13821   type_die = lookup_type_die (type);
13822   if (!type_die)
13823     type_die = modified_type_die (type, false, false, comp_unit_die ());
13824   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13825     return NULL;
13826   return type_die;
13827 }
13828
13829 /* For OP descriptor assumed to be in unsigned MODE, convert it to a signed
13830    type matching MODE, or, if MODE is narrower than DWARF2_ADDR_SIZE, signed
13831    type matching DWARF2_ADDR_SIZE.  Return NULL if the conversion is not
13832    possible.  */
13833
13834 static dw_loc_descr_ref
13835 convert_descriptor_to_signed (enum machine_mode mode, dw_loc_descr_ref op)
13836 {
13837   enum machine_mode outer_mode = mode;
13838   dw_die_ref type_die;
13839   dw_loc_descr_ref cvt;
13840
13841   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
13842     {
13843       outer_mode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
13844                                   MODE_INT, 0);
13845       if (outer_mode == BLKmode
13846           || GET_MODE_SIZE (outer_mode) != DWARF2_ADDR_SIZE)
13847         return NULL;
13848     }
13849   type_die = base_type_for_mode (outer_mode, 0);
13850   if (type_die == NULL)
13851     return NULL;
13852   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13853   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13854   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13855   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13856   add_loc_descr (&op, cvt);
13857   return op;
13858 }
13859
13860 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
13861
13862 static dw_loc_descr_ref
13863 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13864                         dw_loc_descr_ref op1)
13865 {
13866   dw_loc_descr_ref ret = op0;
13867   add_loc_descr (&ret, op1);
13868   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13869   if (STORE_FLAG_VALUE != 1)
13870     {
13871       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13872       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13873     }
13874   return ret;
13875 }
13876
13877 /* Return location descriptor for signed comparison OP RTL.  */
13878
13879 static dw_loc_descr_ref
13880 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13881                          enum machine_mode mem_mode)
13882 {
13883   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13884   dw_loc_descr_ref op0, op1;
13885   int shift;
13886
13887   if (op_mode == VOIDmode)
13888     op_mode = GET_MODE (XEXP (rtl, 1));
13889   if (op_mode == VOIDmode)
13890     return NULL;
13891
13892   if (dwarf_strict
13893       && (GET_MODE_CLASS (op_mode) != MODE_INT
13894           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13895     return NULL;
13896
13897   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13898                             VAR_INIT_STATUS_INITIALIZED);
13899   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13900                             VAR_INIT_STATUS_INITIALIZED);
13901
13902   if (op0 == NULL || op1 == NULL)
13903     return NULL;
13904
13905   if (GET_MODE_CLASS (op_mode) != MODE_INT
13906       || GET_MODE_SIZE (op_mode) >= DWARF2_ADDR_SIZE)
13907     return compare_loc_descriptor (op, op0, op1);
13908
13909   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13910   /* For eq/ne, if the operands are known to be zero-extended,
13911      there is no need to do the fancy shifting up.  */
13912   if (op == DW_OP_eq || op == DW_OP_ne)
13913     {
13914       dw_loc_descr_ref last0, last1;
13915       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13916         ;
13917       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13918         ;
13919       /* deref_size zero extends, and for constants we can check
13920          whether they are zero extended or not.  */
13921       if (((last0->dw_loc_opc == DW_OP_deref_size
13922             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13923            || (CONST_INT_P (XEXP (rtl, 0))
13924                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13925                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
13926           && ((last1->dw_loc_opc == DW_OP_deref_size
13927                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
13928               || (CONST_INT_P (XEXP (rtl, 1))
13929                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
13930                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
13931         return compare_loc_descriptor (op, op0, op1);
13932     }
13933   add_loc_descr (&op0, int_loc_descriptor (shift));
13934   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13935   if (CONST_INT_P (XEXP (rtl, 1)))
13936     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13937   else
13938     {
13939       add_loc_descr (&op1, int_loc_descriptor (shift));
13940       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13941     }
13942   return compare_loc_descriptor (op, op0, op1);
13943 }
13944
13945 /* Return location descriptor for unsigned comparison OP RTL.  */
13946
13947 static dw_loc_descr_ref
13948 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13949                          enum machine_mode mem_mode)
13950 {
13951   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13952   dw_loc_descr_ref op0, op1;
13953
13954   if (op_mode == VOIDmode)
13955     op_mode = GET_MODE (XEXP (rtl, 1));
13956   if (op_mode == VOIDmode)
13957     return NULL;
13958   if (GET_MODE_CLASS (op_mode) != MODE_INT)
13959     return NULL;
13960
13961   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13962     return NULL;
13963
13964   if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13965     return NULL;
13966
13967   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13968                             VAR_INIT_STATUS_INITIALIZED);
13969   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13970                             VAR_INIT_STATUS_INITIALIZED);
13971
13972   if (op0 == NULL || op1 == NULL)
13973     return NULL;
13974
13975   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13976     {
13977       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13978       dw_loc_descr_ref last0, last1;
13979       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13980         ;
13981       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13982         ;
13983       if (CONST_INT_P (XEXP (rtl, 0)))
13984         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13985       /* deref_size zero extends, so no need to mask it again.  */
13986       else if (last0->dw_loc_opc != DW_OP_deref_size
13987                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13988         {
13989           add_loc_descr (&op0, int_loc_descriptor (mask));
13990           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13991         }
13992       if (CONST_INT_P (XEXP (rtl, 1)))
13993         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13994       /* deref_size zero extends, so no need to mask it again.  */
13995       else if (last1->dw_loc_opc != DW_OP_deref_size
13996                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
13997         {
13998           add_loc_descr (&op1, int_loc_descriptor (mask));
13999           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14000         }
14001     }
14002   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14003     {
14004       HOST_WIDE_INT bias = 1;
14005       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14006       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14007       if (CONST_INT_P (XEXP (rtl, 1)))
14008         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14009                                   + INTVAL (XEXP (rtl, 1)));
14010       else
14011         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14012                                             bias, 0));
14013     }
14014   else
14015     {
14016       dw_die_ref type_die = base_type_for_mode (op_mode, 1);
14017       dw_loc_descr_ref cvt;
14018
14019       if (type_die == NULL)
14020         return NULL;
14021       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14022       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14023       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14024       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14025       add_loc_descr (&op0, cvt);
14026       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14027       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14028       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14029       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14030       add_loc_descr (&op1, cvt);
14031     }
14032   return compare_loc_descriptor (op, op0, op1);
14033 }
14034
14035 /* Return location descriptor for {U,S}{MIN,MAX}.  */
14036
14037 static dw_loc_descr_ref
14038 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
14039                        enum machine_mode mem_mode)
14040 {
14041   enum dwarf_location_atom op;
14042   dw_loc_descr_ref op0, op1, ret;
14043   dw_loc_descr_ref bra_node, drop_node;
14044
14045   if (dwarf_strict
14046       && (GET_MODE_CLASS (mode) != MODE_INT
14047           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14048     return NULL;
14049
14050   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14051                             VAR_INIT_STATUS_INITIALIZED);
14052   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14053                             VAR_INIT_STATUS_INITIALIZED);
14054
14055   if (op0 == NULL || op1 == NULL)
14056     return NULL;
14057
14058   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14059   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14060   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14061   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14062     {
14063       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14064         {
14065           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14066           add_loc_descr (&op0, int_loc_descriptor (mask));
14067           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14068           add_loc_descr (&op1, int_loc_descriptor (mask));
14069           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14070         }
14071       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14072         {
14073           HOST_WIDE_INT bias = 1;
14074           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14075           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14076           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14077         }
14078       else
14079         {
14080           dw_die_ref type_die = base_type_for_mode (mode, 1);
14081           dw_loc_descr_ref cvt;
14082           if (type_die == NULL)
14083             return NULL;
14084           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14085           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14086           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14087           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14088           add_loc_descr (&op0, cvt);
14089           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14090           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14091           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14092           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14093           add_loc_descr (&op1, cvt);
14094         }
14095     }
14096   else if (GET_MODE_CLASS (mode) == MODE_INT
14097            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14098     {
14099       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14100       add_loc_descr (&op0, int_loc_descriptor (shift));
14101       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14102       add_loc_descr (&op1, int_loc_descriptor (shift));
14103       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14104     }
14105
14106   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14107     op = DW_OP_lt;
14108   else
14109     op = DW_OP_gt;
14110   ret = op0;
14111   add_loc_descr (&ret, op1);
14112   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14113   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14114   add_loc_descr (&ret, bra_node);
14115   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14116   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14117   add_loc_descr (&ret, drop_node);
14118   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14119   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14120   return ret;
14121 }
14122
14123 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14124    const0 is DW_OP_lit0 or corresponding typed constant,
14125    const1 is DW_OP_lit1 or corresponding typed constant
14126    and constMSB is constant with just the MSB bit set
14127    for the mode):
14128        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14129    L1: const0 DW_OP_swap
14130    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14131        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14132    L3: DW_OP_drop
14133    L4: DW_OP_nop
14134
14135    CTZ is similar:
14136        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14137    L1: const0 DW_OP_swap
14138    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14139        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14140    L3: DW_OP_drop
14141    L4: DW_OP_nop
14142
14143    FFS is similar:
14144        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14145    L1: const1 DW_OP_swap
14146    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14147        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14148    L3: DW_OP_drop
14149    L4: DW_OP_nop  */
14150
14151 static dw_loc_descr_ref
14152 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
14153                     enum machine_mode mem_mode)
14154 {
14155   dw_loc_descr_ref op0, ret, tmp;
14156   HOST_WIDE_INT valv;
14157   dw_loc_descr_ref l1jump, l1label;
14158   dw_loc_descr_ref l2jump, l2label;
14159   dw_loc_descr_ref l3jump, l3label;
14160   dw_loc_descr_ref l4jump, l4label;
14161   rtx msb;
14162
14163   if (GET_MODE_CLASS (mode) != MODE_INT
14164       || GET_MODE (XEXP (rtl, 0)) != mode
14165       || (GET_CODE (rtl) == CLZ
14166           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
14167     return NULL;
14168
14169   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14170                             VAR_INIT_STATUS_INITIALIZED);
14171   if (op0 == NULL)
14172     return NULL;
14173   ret = op0;
14174   if (GET_CODE (rtl) == CLZ)
14175     {
14176       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14177         valv = GET_MODE_BITSIZE (mode);
14178     }
14179   else if (GET_CODE (rtl) == FFS)
14180     valv = 0;
14181   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14182     valv = GET_MODE_BITSIZE (mode);
14183   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14184   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14185   add_loc_descr (&ret, l1jump);
14186   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14187   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14188                             VAR_INIT_STATUS_INITIALIZED);
14189   if (tmp == NULL)
14190     return NULL;
14191   add_loc_descr (&ret, tmp);
14192   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14193   add_loc_descr (&ret, l4jump);
14194   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14195                                 ? const1_rtx : const0_rtx,
14196                                 mode, mem_mode,
14197                                 VAR_INIT_STATUS_INITIALIZED);
14198   if (l1label == NULL)
14199     return NULL;
14200   add_loc_descr (&ret, l1label);
14201   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14202   l2label = new_loc_descr (DW_OP_dup, 0, 0);
14203   add_loc_descr (&ret, l2label);
14204   if (GET_CODE (rtl) != CLZ)
14205     msb = const1_rtx;
14206   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14207     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
14208                    << (GET_MODE_BITSIZE (mode) - 1));
14209   else
14210     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
14211                                   << (GET_MODE_BITSIZE (mode)
14212                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
14213   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14214     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14215                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14216                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14217   else
14218     tmp = mem_loc_descriptor (msb, mode, mem_mode,
14219                               VAR_INIT_STATUS_INITIALIZED);
14220   if (tmp == NULL)
14221     return NULL;
14222   add_loc_descr (&ret, tmp);
14223   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14224   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14225   add_loc_descr (&ret, l3jump);
14226   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14227                             VAR_INIT_STATUS_INITIALIZED);
14228   if (tmp == NULL)
14229     return NULL;
14230   add_loc_descr (&ret, tmp);
14231   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14232                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
14233   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14234   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14235   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14236   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14237   add_loc_descr (&ret, l2jump);
14238   l3label = new_loc_descr (DW_OP_drop, 0, 0);
14239   add_loc_descr (&ret, l3label);
14240   l4label = new_loc_descr (DW_OP_nop, 0, 0);
14241   add_loc_descr (&ret, l4label);
14242   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14243   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14244   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14245   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14246   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14247   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14248   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14249   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14250   return ret;
14251 }
14252
14253 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14254    const1 is DW_OP_lit1 or corresponding typed constant):
14255        const0 DW_OP_swap
14256    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14257        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14258    L2: DW_OP_drop
14259
14260    PARITY is similar:
14261    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14262        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14263    L2: DW_OP_drop  */
14264
14265 static dw_loc_descr_ref
14266 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
14267                          enum machine_mode mem_mode)
14268 {
14269   dw_loc_descr_ref op0, ret, tmp;
14270   dw_loc_descr_ref l1jump, l1label;
14271   dw_loc_descr_ref l2jump, l2label;
14272
14273   if (GET_MODE_CLASS (mode) != MODE_INT
14274       || GET_MODE (XEXP (rtl, 0)) != mode)
14275     return NULL;
14276
14277   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14278                             VAR_INIT_STATUS_INITIALIZED);
14279   if (op0 == NULL)
14280     return NULL;
14281   ret = op0;
14282   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14283                             VAR_INIT_STATUS_INITIALIZED);
14284   if (tmp == NULL)
14285     return NULL;
14286   add_loc_descr (&ret, tmp);
14287   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14288   l1label = new_loc_descr (DW_OP_dup, 0, 0);
14289   add_loc_descr (&ret, l1label);
14290   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14291   add_loc_descr (&ret, l2jump);
14292   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14293   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14294   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14295                             VAR_INIT_STATUS_INITIALIZED);
14296   if (tmp == NULL)
14297     return NULL;
14298   add_loc_descr (&ret, tmp);
14299   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14300   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14301                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
14302   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14303   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14304                             VAR_INIT_STATUS_INITIALIZED);
14305   add_loc_descr (&ret, tmp);
14306   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14307   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14308   add_loc_descr (&ret, l1jump);
14309   l2label = new_loc_descr (DW_OP_drop, 0, 0);
14310   add_loc_descr (&ret, l2label);
14311   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14312   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14313   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14314   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14315   return ret;
14316 }
14317
14318 /* BSWAP (constS is initial shift count, either 56 or 24):
14319        constS const0
14320    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14321        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14322        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14323        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14324    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
14325
14326 static dw_loc_descr_ref
14327 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
14328                       enum machine_mode mem_mode)
14329 {
14330   dw_loc_descr_ref op0, ret, tmp;
14331   dw_loc_descr_ref l1jump, l1label;
14332   dw_loc_descr_ref l2jump, l2label;
14333
14334   if (GET_MODE_CLASS (mode) != MODE_INT
14335       || BITS_PER_UNIT != 8
14336       || (GET_MODE_BITSIZE (mode) != 32
14337           &&  GET_MODE_BITSIZE (mode) != 64))
14338     return NULL;
14339
14340   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14341                             VAR_INIT_STATUS_INITIALIZED);
14342   if (op0 == NULL)
14343     return NULL;
14344
14345   ret = op0;
14346   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14347                             mode, mem_mode,
14348                             VAR_INIT_STATUS_INITIALIZED);
14349   if (tmp == NULL)
14350     return NULL;
14351   add_loc_descr (&ret, tmp);
14352   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14353                             VAR_INIT_STATUS_INITIALIZED);
14354   if (tmp == NULL)
14355     return NULL;
14356   add_loc_descr (&ret, tmp);
14357   l1label = new_loc_descr (DW_OP_pick, 2, 0);
14358   add_loc_descr (&ret, l1label);
14359   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14360                             mode, mem_mode,
14361                             VAR_INIT_STATUS_INITIALIZED);
14362   add_loc_descr (&ret, tmp);
14363   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14364   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14365   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14366   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14367                             VAR_INIT_STATUS_INITIALIZED);
14368   if (tmp == NULL)
14369     return NULL;
14370   add_loc_descr (&ret, tmp);
14371   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14372   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14373   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14374   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14375   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14376   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14377   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14378                             VAR_INIT_STATUS_INITIALIZED);
14379   add_loc_descr (&ret, tmp);
14380   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14381   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14382   add_loc_descr (&ret, l2jump);
14383   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14384                             VAR_INIT_STATUS_INITIALIZED);
14385   add_loc_descr (&ret, tmp);
14386   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14387   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14388   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14389   add_loc_descr (&ret, l1jump);
14390   l2label = new_loc_descr (DW_OP_drop, 0, 0);
14391   add_loc_descr (&ret, l2label);
14392   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14393   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14394   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14395   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14396   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14397   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14398   return ret;
14399 }
14400
14401 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14402    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14403    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14404    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14405
14406    ROTATERT is similar:
14407    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14408    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14409    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
14410
14411 static dw_loc_descr_ref
14412 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
14413                        enum machine_mode mem_mode)
14414 {
14415   rtx rtlop1 = XEXP (rtl, 1);
14416   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14417   int i;
14418
14419   if (GET_MODE_CLASS (mode) != MODE_INT)
14420     return NULL;
14421
14422   if (GET_MODE (rtlop1) != VOIDmode
14423       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14424     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14425   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14426                             VAR_INIT_STATUS_INITIALIZED);
14427   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14428                             VAR_INIT_STATUS_INITIALIZED);
14429   if (op0 == NULL || op1 == NULL)
14430     return NULL;
14431   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14432     for (i = 0; i < 2; i++)
14433       {
14434         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14435           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14436                                         mode, mem_mode,
14437                                         VAR_INIT_STATUS_INITIALIZED);
14438         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14439           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14440                                    ? DW_OP_const4u
14441                                    : HOST_BITS_PER_WIDE_INT == 64
14442                                    ? DW_OP_const8u : DW_OP_constu,
14443                                    GET_MODE_MASK (mode), 0);
14444         else
14445           mask[i] = NULL;
14446         if (mask[i] == NULL)
14447           return NULL;
14448         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14449       }
14450   ret = op0;
14451   add_loc_descr (&ret, op1);
14452   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14453   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14454   if (GET_CODE (rtl) == ROTATERT)
14455     {
14456       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14457       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14458                                           GET_MODE_BITSIZE (mode), 0));
14459     }
14460   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14461   if (mask[0] != NULL)
14462     add_loc_descr (&ret, mask[0]);
14463   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14464   if (mask[1] != NULL)
14465     {
14466       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14467       add_loc_descr (&ret, mask[1]);
14468       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14469     }
14470   if (GET_CODE (rtl) == ROTATE)
14471     {
14472       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14473       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14474                                           GET_MODE_BITSIZE (mode), 0));
14475     }
14476   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14477   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14478   return ret;
14479 }
14480
14481 /* The following routine converts the RTL for a variable or parameter
14482    (resident in memory) into an equivalent Dwarf representation of a
14483    mechanism for getting the address of that same variable onto the top of a
14484    hypothetical "address evaluation" stack.
14485
14486    When creating memory location descriptors, we are effectively transforming
14487    the RTL for a memory-resident object into its Dwarf postfix expression
14488    equivalent.  This routine recursively descends an RTL tree, turning
14489    it into Dwarf postfix code as it goes.
14490
14491    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14492
14493    MEM_MODE is the mode of the memory reference, needed to handle some
14494    autoincrement addressing modes.
14495
14496    Return 0 if we can't represent the location.  */
14497
14498 static dw_loc_descr_ref
14499 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
14500                     enum machine_mode mem_mode,
14501                     enum var_init_status initialized)
14502 {
14503   dw_loc_descr_ref mem_loc_result = NULL;
14504   enum dwarf_location_atom op;
14505   dw_loc_descr_ref op0, op1;
14506
14507   if (mode == VOIDmode)
14508     mode = GET_MODE (rtl);
14509
14510   /* Note that for a dynamically sized array, the location we will generate a
14511      description of here will be the lowest numbered location which is
14512      actually within the array.  That's *not* necessarily the same as the
14513      zeroth element of the array.  */
14514
14515   rtl = targetm.delegitimize_address (rtl);
14516
14517   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14518     return NULL;
14519
14520   switch (GET_CODE (rtl))
14521     {
14522     case POST_INC:
14523     case POST_DEC:
14524     case POST_MODIFY:
14525       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14526
14527     case SUBREG:
14528       /* The case of a subreg may arise when we have a local (register)
14529          variable or a formal (register) parameter which doesn't quite fill
14530          up an entire register.  For now, just assume that it is
14531          legitimate to make the Dwarf info refer to the whole register which
14532          contains the given subreg.  */
14533       if (!subreg_lowpart_p (rtl))
14534         break;
14535       if (GET_MODE_CLASS (mode) == MODE_INT
14536           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
14537           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14538 #ifdef POINTERS_EXTEND_UNSIGNED
14539               || (mode == Pmode && mem_mode != VOIDmode)
14540 #endif
14541              )
14542           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
14543         {
14544           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
14545                                                GET_MODE (SUBREG_REG (rtl)),
14546                                                mem_mode, initialized);
14547           break;
14548         }
14549       if (dwarf_strict)
14550         break;
14551       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
14552         break;
14553       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
14554           && (GET_MODE_CLASS (mode) != MODE_INT
14555               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
14556         break;
14557       else
14558         {
14559           dw_die_ref type_die;
14560           dw_loc_descr_ref cvt;
14561
14562           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
14563                                                GET_MODE (SUBREG_REG (rtl)),
14564                                                mem_mode, initialized);
14565           if (mem_loc_result == NULL)
14566             break;
14567           type_die = base_type_for_mode (mode, 0);
14568           if (type_die == NULL)
14569             {
14570               mem_loc_result = NULL;
14571               break;
14572             }
14573           if (GET_MODE_SIZE (mode)
14574               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
14575             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14576           else
14577             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
14578           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14579           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14580           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14581           add_loc_descr (&mem_loc_result, cvt);
14582         }
14583       break;
14584
14585     case REG:
14586       if (GET_MODE_CLASS (mode) != MODE_INT
14587           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14588 #ifdef POINTERS_EXTEND_UNSIGNED
14589               && (mode != Pmode || mem_mode == VOIDmode)
14590 #endif
14591               ))
14592         {
14593           dw_die_ref type_die;
14594
14595           if (dwarf_strict)
14596             break;
14597           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14598             break;
14599           type_die = base_type_for_mode (mode, 0);
14600           if (type_die == NULL)
14601             break;
14602           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
14603                                           dbx_reg_number (rtl), 0);
14604           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14605           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14606           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14607           break;
14608         }
14609       /* Whenever a register number forms a part of the description of the
14610          method for calculating the (dynamic) address of a memory resident
14611          object, DWARF rules require the register number be referred to as
14612          a "base register".  This distinction is not based in any way upon
14613          what category of register the hardware believes the given register
14614          belongs to.  This is strictly DWARF terminology we're dealing with
14615          here. Note that in cases where the location of a memory-resident
14616          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14617          OP_CONST (0)) the actual DWARF location descriptor that we generate
14618          may just be OP_BASEREG (basereg).  This may look deceptively like
14619          the object in question was allocated to a register (rather than in
14620          memory) so DWARF consumers need to be aware of the subtle
14621          distinction between OP_REG and OP_BASEREG.  */
14622       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14623         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14624       else if (stack_realign_drap
14625                && crtl->drap_reg
14626                && crtl->args.internal_arg_pointer == rtl
14627                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14628         {
14629           /* If RTL is internal_arg_pointer, which has been optimized
14630              out, use DRAP instead.  */
14631           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14632                                             VAR_INIT_STATUS_INITIALIZED);
14633         }
14634       break;
14635
14636     case SIGN_EXTEND:
14637     case ZERO_EXTEND:
14638       gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
14639       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14640                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14641       if (op0 == 0)
14642         break;
14643       else if (GET_CODE (rtl) == ZERO_EXTEND
14644                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14645                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14646                   < HOST_BITS_PER_WIDE_INT
14647                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14648                   to expand zero extend as two shifts instead of
14649                   masking.  */
14650                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14651         {
14652           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
14653           mem_loc_result = op0;
14654           add_loc_descr (&mem_loc_result,
14655                          int_loc_descriptor (GET_MODE_MASK (imode)));
14656           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14657         }
14658       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14659         {
14660           int shift = DWARF2_ADDR_SIZE
14661                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14662           shift *= BITS_PER_UNIT;
14663           if (GET_CODE (rtl) == SIGN_EXTEND)
14664             op = DW_OP_shra;
14665           else
14666             op = DW_OP_shr;
14667           mem_loc_result = op0;
14668           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14669           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14670           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14671           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14672         }
14673       else if (!dwarf_strict)
14674         {
14675           dw_die_ref type_die1, type_die2;
14676           dw_loc_descr_ref cvt;
14677
14678           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14679                                           GET_CODE (rtl) == ZERO_EXTEND);
14680           if (type_die1 == NULL)
14681             break;
14682           type_die2 = base_type_for_mode (mode, 0);
14683           if (type_die2 == NULL)
14684             break;
14685           mem_loc_result = op0;
14686           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14687           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14688           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14689           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14690           add_loc_descr (&mem_loc_result, cvt);
14691           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14692           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14693           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14694           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14695           add_loc_descr (&mem_loc_result, cvt);
14696         }
14697       break;
14698
14699     case MEM:
14700       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14701                                            get_address_mode (rtl), mode,
14702                                            VAR_INIT_STATUS_INITIALIZED);
14703       if (mem_loc_result == NULL)
14704         mem_loc_result = tls_mem_loc_descriptor (rtl);
14705       if (mem_loc_result != 0)
14706         {
14707           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14708               || GET_MODE_CLASS (mode) != MODE_INT)
14709             {
14710               dw_die_ref type_die;
14711               dw_loc_descr_ref deref;
14712
14713               if (dwarf_strict)
14714                 return NULL;
14715               type_die = base_type_for_mode (mode, 0);
14716               if (type_die == NULL)
14717                 return NULL;
14718               deref = new_loc_descr (DW_OP_GNU_deref_type,
14719                                      GET_MODE_SIZE (mode), 0);
14720               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14721               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14722               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14723               add_loc_descr (&mem_loc_result, deref);
14724             }
14725           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14726             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14727           else
14728             add_loc_descr (&mem_loc_result,
14729                            new_loc_descr (DW_OP_deref_size,
14730                                           GET_MODE_SIZE (mode), 0));
14731         }
14732       else
14733         {
14734           rtx new_rtl = avoid_constant_pool_reference (rtl);
14735           if (new_rtl != rtl)
14736             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
14737         }
14738       break;
14739
14740     case LO_SUM:
14741       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14742
14743     case LABEL_REF:
14744       /* Some ports can transform a symbol ref into a label ref, because
14745          the symbol ref is too far away and has to be dumped into a constant
14746          pool.  */
14747     case CONST:
14748     case SYMBOL_REF:
14749       if (GET_MODE_CLASS (mode) != MODE_INT
14750           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14751 #ifdef POINTERS_EXTEND_UNSIGNED
14752               && (mode != Pmode || mem_mode == VOIDmode)
14753 #endif
14754               ))
14755         break;
14756       if (GET_CODE (rtl) == SYMBOL_REF
14757           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14758         {
14759           dw_loc_descr_ref temp;
14760
14761           /* If this is not defined, we have no way to emit the data.  */
14762           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14763             break;
14764
14765           /* We used to emit DW_OP_addr here, but that's wrong, since
14766              DW_OP_addr should be relocated by the debug info consumer,
14767              while DW_OP_GNU_push_tls_address operand should not.  */
14768           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
14769                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
14770           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
14771           temp->dw_loc_oprnd1.v.val_addr = rtl;
14772           temp->dtprel = true;
14773
14774           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
14775           add_loc_descr (&mem_loc_result, temp);
14776
14777           break;
14778         }
14779
14780       if (!const_ok_for_output (rtl))
14781         break;
14782
14783     symref:
14784       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14785       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14786       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14787       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14788       break;
14789
14790     case CONCAT:
14791     case CONCATN:
14792     case VAR_LOCATION:
14793     case DEBUG_IMPLICIT_PTR:
14794       expansion_failed (NULL_TREE, rtl,
14795                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14796       return 0;
14797
14798     case ENTRY_VALUE:
14799       if (dwarf_strict)
14800         return NULL;
14801       if (REG_P (ENTRY_VALUE_EXP (rtl)))
14802         {
14803           if (GET_MODE_CLASS (mode) != MODE_INT
14804               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14805             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14806                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14807           else
14808             op0
14809               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
14810                                         VAR_INIT_STATUS_INITIALIZED);
14811         }
14812       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14813                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14814         {
14815           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14816                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14817           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14818             return NULL;
14819         }
14820       else
14821         gcc_unreachable ();
14822       if (op0 == NULL)
14823         return NULL;
14824       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
14825       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14826       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14827       return mem_loc_result;
14828
14829     case PRE_MODIFY:
14830       /* Extract the PLUS expression nested inside and fall into
14831          PLUS code below.  */
14832       rtl = XEXP (rtl, 1);
14833       goto plus;
14834
14835     case PRE_INC:
14836     case PRE_DEC:
14837       /* Turn these into a PLUS expression and fall into the PLUS code
14838          below.  */
14839       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14840                           GEN_INT (GET_CODE (rtl) == PRE_INC
14841                                    ? GET_MODE_UNIT_SIZE (mem_mode)
14842                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
14843
14844       /* ... fall through ...  */
14845
14846     case PLUS:
14847     plus:
14848       if (is_based_loc (rtl)
14849           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14850           && GET_MODE_CLASS (mode) == MODE_INT)
14851         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14852                                           INTVAL (XEXP (rtl, 1)),
14853                                           VAR_INIT_STATUS_INITIALIZED);
14854       else
14855         {
14856           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14857                                                VAR_INIT_STATUS_INITIALIZED);
14858           if (mem_loc_result == 0)
14859             break;
14860
14861           if (CONST_INT_P (XEXP (rtl, 1))
14862               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14863             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14864           else
14865             {
14866               dw_loc_descr_ref mem_loc_result2
14867                 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14868                                       VAR_INIT_STATUS_INITIALIZED);
14869               if (mem_loc_result2 == 0)
14870                 break;
14871               add_loc_descr (&mem_loc_result, mem_loc_result2);
14872               add_loc_descr (&mem_loc_result,
14873                              new_loc_descr (DW_OP_plus, 0, 0));
14874             }
14875         }
14876       break;
14877
14878     /* If a pseudo-reg is optimized away, it is possible for it to
14879        be replaced with a MEM containing a multiply or shift.  */
14880     case MINUS:
14881       op = DW_OP_minus;
14882       goto do_binop;
14883
14884     case MULT:
14885       op = DW_OP_mul;
14886       goto do_binop;
14887
14888     case DIV:
14889       op = DW_OP_div;
14890       goto do_binop;
14891
14892     case UMOD:
14893       op = DW_OP_mod;
14894       goto do_binop;
14895
14896     case ASHIFT:
14897       op = DW_OP_shl;
14898       goto do_shift;
14899
14900     case ASHIFTRT:
14901       op = DW_OP_shra;
14902       goto do_shift;
14903
14904     case LSHIFTRT:
14905       op = DW_OP_shr;
14906       goto do_shift;
14907
14908     do_shift:
14909       if (GET_MODE_CLASS (mode) != MODE_INT)
14910         break;
14911       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14912                                 VAR_INIT_STATUS_INITIALIZED);
14913       {
14914         rtx rtlop1 = XEXP (rtl, 1);
14915         if (GET_MODE (rtlop1) != VOIDmode
14916             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
14917                < GET_MODE_BITSIZE (mode))
14918           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14919         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14920                                   VAR_INIT_STATUS_INITIALIZED);
14921       }
14922
14923       if (op0 == 0 || op1 == 0)
14924         break;
14925
14926       mem_loc_result = op0;
14927       add_loc_descr (&mem_loc_result, op1);
14928       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14929       break;
14930
14931     case AND:
14932       op = DW_OP_and;
14933       goto do_binop;
14934
14935     case IOR:
14936       op = DW_OP_or;
14937       goto do_binop;
14938
14939     case XOR:
14940       op = DW_OP_xor;
14941       goto do_binop;
14942
14943     do_binop:
14944       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14945                                 VAR_INIT_STATUS_INITIALIZED);
14946       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14947                                 VAR_INIT_STATUS_INITIALIZED);
14948
14949       if (op0 == 0 || op1 == 0)
14950         break;
14951
14952       mem_loc_result = op0;
14953       add_loc_descr (&mem_loc_result, op1);
14954       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14955       break;
14956
14957     case MOD:
14958       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
14959         {
14960           /* If MODE is wider than DWARF2_ADDR_SIZE, mem_loc_descriptor
14961              should return signed typed values and therefore DW_OP_mod
14962              won't be unsigned as it defaults for untyped stack values,
14963              but signed.  */
14964           op = DW_OP_mod;
14965           goto do_binop;
14966         }
14967
14968       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14969                                 VAR_INIT_STATUS_INITIALIZED);
14970       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14971                                 VAR_INIT_STATUS_INITIALIZED);
14972
14973       if (op0 == 0 || op1 == 0)
14974         break;
14975
14976       mem_loc_result = op0;
14977       add_loc_descr (&mem_loc_result, op1);
14978       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14979       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14980       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14981       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14982       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14983       break;
14984
14985     case UDIV:
14986       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
14987         {
14988           dw_die_ref type_die;
14989           dw_loc_descr_ref cvt;
14990
14991           type_die = base_type_for_mode (mode, 1);
14992           if (type_die == NULL)
14993             break;
14994           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14995                                     VAR_INIT_STATUS_INITIALIZED);
14996           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14997                                     VAR_INIT_STATUS_INITIALIZED);
14998           if (op0 == 0 || op1 == 0)
14999             break;
15000           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
15001           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15002           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15003           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15004           add_loc_descr (&op0, cvt);
15005           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
15006           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15007           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15008           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15009           add_loc_descr (&op1, cvt);
15010           mem_loc_result = op0;
15011           add_loc_descr (&mem_loc_result, op1);
15012           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15013           mem_loc_result = convert_descriptor_to_signed (mode, mem_loc_result);
15014         }
15015       break;
15016
15017     case NOT:
15018       op = DW_OP_not;
15019       goto do_unop;
15020
15021     case ABS:
15022       op = DW_OP_abs;
15023       goto do_unop;
15024
15025     case NEG:
15026       op = DW_OP_neg;
15027       goto do_unop;
15028
15029     do_unop:
15030       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15031                                 VAR_INIT_STATUS_INITIALIZED);
15032
15033       if (op0 == 0)
15034         break;
15035
15036       mem_loc_result = op0;
15037       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15038       break;
15039
15040     case CONST_INT:
15041       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15042 #ifdef POINTERS_EXTEND_UNSIGNED
15043           || (mode == Pmode
15044               && mem_mode != VOIDmode
15045               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15046 #endif
15047           )
15048         {
15049           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15050           break;
15051         }
15052       if (!dwarf_strict
15053           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15054               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
15055         {
15056           dw_die_ref type_die = base_type_for_mode (mode, 0);
15057           if (type_die == NULL)
15058             return NULL;
15059           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
15060                                           INTVAL (rtl));
15061           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15062           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15063           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15064           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15065             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15066           else
15067             {
15068               mem_loc_result->dw_loc_oprnd2.val_class
15069                 = dw_val_class_const_double;
15070               mem_loc_result->dw_loc_oprnd2.v.val_double
15071                 = shwi_to_double_int (INTVAL (rtl));
15072             }
15073         }
15074       break;
15075
15076     case CONST_DOUBLE:
15077       if (!dwarf_strict)
15078         {
15079           dw_die_ref type_die;
15080
15081           /* Note that a CONST_DOUBLE rtx could represent either an integer
15082              or a floating-point constant.  A CONST_DOUBLE is used whenever
15083              the constant requires more than one word in order to be
15084              adequately represented.  We output CONST_DOUBLEs as blocks.  */
15085           if (mode == VOIDmode
15086               || (GET_MODE (rtl) == VOIDmode
15087                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
15088             break;
15089           type_die = base_type_for_mode (mode, 0);
15090           if (type_die == NULL)
15091             return NULL;
15092           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
15093           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15094           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15095           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15096           if (SCALAR_FLOAT_MODE_P (mode))
15097             {
15098               unsigned int length = GET_MODE_SIZE (mode);
15099               unsigned char *array
15100                   = (unsigned char*) ggc_alloc_atomic (length);
15101
15102               insert_float (rtl, array);
15103               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15104               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15105               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15106               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15107             }
15108           else
15109             {
15110               mem_loc_result->dw_loc_oprnd2.val_class
15111                 = dw_val_class_const_double;
15112               mem_loc_result->dw_loc_oprnd2.v.val_double
15113                 = rtx_to_double_int (rtl);
15114             }
15115         }
15116       break;
15117
15118     case EQ:
15119       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15120       break;
15121
15122     case GE:
15123       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15124       break;
15125
15126     case GT:
15127       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15128       break;
15129
15130     case LE:
15131       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15132       break;
15133
15134     case LT:
15135       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15136       break;
15137
15138     case NE:
15139       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15140       break;
15141
15142     case GEU:
15143       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15144       break;
15145
15146     case GTU:
15147       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15148       break;
15149
15150     case LEU:
15151       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15152       break;
15153
15154     case LTU:
15155       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15156       break;
15157
15158     case UMIN:
15159     case UMAX:
15160       if (GET_MODE_CLASS (mode) != MODE_INT)
15161         break;
15162       /* FALLTHRU */
15163     case SMIN:
15164     case SMAX:
15165       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15166       break;
15167
15168     case ZERO_EXTRACT:
15169     case SIGN_EXTRACT:
15170       if (CONST_INT_P (XEXP (rtl, 1))
15171           && CONST_INT_P (XEXP (rtl, 2))
15172           && ((unsigned) INTVAL (XEXP (rtl, 1))
15173               + (unsigned) INTVAL (XEXP (rtl, 2))
15174               <= GET_MODE_BITSIZE (mode))
15175           && GET_MODE_CLASS (mode) == MODE_INT
15176           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15177           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15178         {
15179           int shift, size;
15180           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15181                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
15182           if (op0 == 0)
15183             break;
15184           if (GET_CODE (rtl) == SIGN_EXTRACT)
15185             op = DW_OP_shra;
15186           else
15187             op = DW_OP_shr;
15188           mem_loc_result = op0;
15189           size = INTVAL (XEXP (rtl, 1));
15190           shift = INTVAL (XEXP (rtl, 2));
15191           if (BITS_BIG_ENDIAN)
15192             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15193                     - shift - size;
15194           if (shift + size != (int) DWARF2_ADDR_SIZE)
15195             {
15196               add_loc_descr (&mem_loc_result,
15197                              int_loc_descriptor (DWARF2_ADDR_SIZE
15198                                                  - shift - size));
15199               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15200             }
15201           if (size != (int) DWARF2_ADDR_SIZE)
15202             {
15203               add_loc_descr (&mem_loc_result,
15204                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15205               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15206             }
15207         }
15208       break;
15209
15210     case IF_THEN_ELSE:
15211       {
15212         dw_loc_descr_ref op2, bra_node, drop_node;
15213         op0 = mem_loc_descriptor (XEXP (rtl, 0),
15214                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
15215                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
15216                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
15217         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15218                                   VAR_INIT_STATUS_INITIALIZED);
15219         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15220                                   VAR_INIT_STATUS_INITIALIZED);
15221         if (op0 == NULL || op1 == NULL || op2 == NULL)
15222           break;
15223
15224         mem_loc_result = op1;
15225         add_loc_descr (&mem_loc_result, op2);
15226         add_loc_descr (&mem_loc_result, op0);
15227         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15228         add_loc_descr (&mem_loc_result, bra_node);
15229         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15230         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15231         add_loc_descr (&mem_loc_result, drop_node);
15232         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15233         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15234       }
15235       break;
15236
15237     case FLOAT_EXTEND:
15238     case FLOAT_TRUNCATE:
15239     case FLOAT:
15240     case UNSIGNED_FLOAT:
15241     case FIX:
15242     case UNSIGNED_FIX:
15243       if (!dwarf_strict)
15244         {
15245           dw_die_ref type_die;
15246           dw_loc_descr_ref cvt;
15247
15248           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15249                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
15250           if (op0 == NULL)
15251             break;
15252           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
15253               && (GET_CODE (rtl) == UNSIGNED_FLOAT
15254                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15255                      <= DWARF2_ADDR_SIZE))
15256             {
15257               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15258                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
15259               if (type_die == NULL)
15260                 break;
15261               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
15262               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15263               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15264               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15265               add_loc_descr (&op0, cvt);
15266             }
15267           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15268           if (type_die == NULL)
15269             break;
15270           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
15271           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15272           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15273           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15274           add_loc_descr (&op0, cvt);
15275           if (GET_MODE_CLASS (mode) == MODE_INT
15276               && (GET_CODE (rtl) == UNSIGNED_FIX
15277                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15278             {
15279               op0 = convert_descriptor_to_signed (mode, op0);
15280               if (op0 == NULL)
15281                 break;
15282             }
15283           mem_loc_result = op0;
15284         }
15285       break;
15286
15287     case CLZ:
15288     case CTZ:
15289     case FFS:
15290       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15291       break;
15292
15293     case POPCOUNT:
15294     case PARITY:
15295       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15296       break;
15297
15298     case BSWAP:
15299       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15300       break;
15301
15302     case ROTATE:
15303     case ROTATERT:
15304       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15305       break;
15306
15307     case COMPARE:
15308     case TRUNCATE:
15309       /* In theory, we could implement the above.  */
15310       /* DWARF cannot represent the unsigned compare operations
15311          natively.  */
15312     case SS_MULT:
15313     case US_MULT:
15314     case SS_DIV:
15315     case US_DIV:
15316     case SS_PLUS:
15317     case US_PLUS:
15318     case SS_MINUS:
15319     case US_MINUS:
15320     case SS_NEG:
15321     case US_NEG:
15322     case SS_ABS:
15323     case SS_ASHIFT:
15324     case US_ASHIFT:
15325     case SS_TRUNCATE:
15326     case US_TRUNCATE:
15327     case UNORDERED:
15328     case ORDERED:
15329     case UNEQ:
15330     case UNGE:
15331     case UNGT:
15332     case UNLE:
15333     case UNLT:
15334     case LTGT:
15335     case FRACT_CONVERT:
15336     case UNSIGNED_FRACT_CONVERT:
15337     case SAT_FRACT:
15338     case UNSIGNED_SAT_FRACT:
15339     case SQRT:
15340     case ASM_OPERANDS:
15341     case VEC_MERGE:
15342     case VEC_SELECT:
15343     case VEC_CONCAT:
15344     case VEC_DUPLICATE:
15345     case UNSPEC:
15346     case HIGH:
15347     case FMA:
15348     case STRICT_LOW_PART:
15349     case CONST_VECTOR:
15350     case CONST_FIXED:
15351       /* If delegitimize_address couldn't do anything with the UNSPEC, we
15352          can't express it in the debug info.  This can happen e.g. with some
15353          TLS UNSPECs.  */
15354       break;
15355
15356     case CONST_STRING:
15357       resolve_one_addr (&rtl, NULL);
15358       goto symref;
15359
15360     default:
15361 #ifdef ENABLE_CHECKING
15362       print_rtl (stderr, rtl);
15363       gcc_unreachable ();
15364 #else
15365       break;
15366 #endif
15367     }
15368
15369   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15370     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15371
15372   return mem_loc_result;
15373 }
15374
15375 /* Return a descriptor that describes the concatenation of two locations.
15376    This is typically a complex variable.  */
15377
15378 static dw_loc_descr_ref
15379 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15380 {
15381   dw_loc_descr_ref cc_loc_result = NULL;
15382   dw_loc_descr_ref x0_ref
15383     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15384   dw_loc_descr_ref x1_ref
15385     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15386
15387   if (x0_ref == 0 || x1_ref == 0)
15388     return 0;
15389
15390   cc_loc_result = x0_ref;
15391   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15392
15393   add_loc_descr (&cc_loc_result, x1_ref);
15394   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15395
15396   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15397     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15398
15399   return cc_loc_result;
15400 }
15401
15402 /* Return a descriptor that describes the concatenation of N
15403    locations.  */
15404
15405 static dw_loc_descr_ref
15406 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15407 {
15408   unsigned int i;
15409   dw_loc_descr_ref cc_loc_result = NULL;
15410   unsigned int n = XVECLEN (concatn, 0);
15411
15412   for (i = 0; i < n; ++i)
15413     {
15414       dw_loc_descr_ref ref;
15415       rtx x = XVECEXP (concatn, 0, i);
15416
15417       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15418       if (ref == NULL)
15419         return NULL;
15420
15421       add_loc_descr (&cc_loc_result, ref);
15422       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15423     }
15424
15425   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15426     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15427
15428   return cc_loc_result;
15429 }
15430
15431 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
15432    for DEBUG_IMPLICIT_PTR RTL.  */
15433
15434 static dw_loc_descr_ref
15435 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15436 {
15437   dw_loc_descr_ref ret;
15438   dw_die_ref ref;
15439
15440   if (dwarf_strict)
15441     return NULL;
15442   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15443               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15444               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15445   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15446   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
15447   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15448   if (ref)
15449     {
15450       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15451       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15452       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15453     }
15454   else
15455     {
15456       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15457       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15458     }
15459   return ret;
15460 }
15461
15462 /* Output a proper Dwarf location descriptor for a variable or parameter
15463    which is either allocated in a register or in a memory location.  For a
15464    register, we just generate an OP_REG and the register number.  For a
15465    memory location we provide a Dwarf postfix expression describing how to
15466    generate the (dynamic) address of the object onto the address stack.
15467
15468    MODE is mode of the decl if this loc_descriptor is going to be used in
15469    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15470    allowed, VOIDmode otherwise.
15471
15472    If we don't know how to describe it, return 0.  */
15473
15474 static dw_loc_descr_ref
15475 loc_descriptor (rtx rtl, enum machine_mode mode,
15476                 enum var_init_status initialized)
15477 {
15478   dw_loc_descr_ref loc_result = NULL;
15479
15480   switch (GET_CODE (rtl))
15481     {
15482     case SUBREG:
15483       /* The case of a subreg may arise when we have a local (register)
15484          variable or a formal (register) parameter which doesn't quite fill
15485          up an entire register.  For now, just assume that it is
15486          legitimate to make the Dwarf info refer to the whole register which
15487          contains the given subreg.  */
15488       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15489         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
15490       else
15491         goto do_default;
15492       break;
15493
15494     case REG:
15495       loc_result = reg_loc_descriptor (rtl, initialized);
15496       break;
15497
15498     case MEM:
15499       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15500                                        GET_MODE (rtl), initialized);
15501       if (loc_result == NULL)
15502         loc_result = tls_mem_loc_descriptor (rtl);
15503       if (loc_result == NULL)
15504         {
15505           rtx new_rtl = avoid_constant_pool_reference (rtl);
15506           if (new_rtl != rtl)
15507             loc_result = loc_descriptor (new_rtl, mode, initialized);
15508         }
15509       break;
15510
15511     case CONCAT:
15512       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15513                                           initialized);
15514       break;
15515
15516     case CONCATN:
15517       loc_result = concatn_loc_descriptor (rtl, initialized);
15518       break;
15519
15520     case VAR_LOCATION:
15521       /* Single part.  */
15522       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15523         {
15524           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15525           if (GET_CODE (loc) == EXPR_LIST)
15526             loc = XEXP (loc, 0);
15527           loc_result = loc_descriptor (loc, mode, initialized);
15528           break;
15529         }
15530
15531       rtl = XEXP (rtl, 1);
15532       /* FALLTHRU */
15533
15534     case PARALLEL:
15535       {
15536         rtvec par_elems = XVEC (rtl, 0);
15537         int num_elem = GET_NUM_ELEM (par_elems);
15538         enum machine_mode mode;
15539         int i;
15540
15541         /* Create the first one, so we have something to add to.  */
15542         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15543                                      VOIDmode, initialized);
15544         if (loc_result == NULL)
15545           return NULL;
15546         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15547         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15548         for (i = 1; i < num_elem; i++)
15549           {
15550             dw_loc_descr_ref temp;
15551
15552             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15553                                    VOIDmode, initialized);
15554             if (temp == NULL)
15555               return NULL;
15556             add_loc_descr (&loc_result, temp);
15557             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15558             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15559           }
15560       }
15561       break;
15562
15563     case CONST_INT:
15564       if (mode != VOIDmode && mode != BLKmode)
15565         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15566                                                     INTVAL (rtl));
15567       break;
15568
15569     case CONST_DOUBLE:
15570       if (mode == VOIDmode)
15571         mode = GET_MODE (rtl);
15572
15573       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15574         {
15575           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15576
15577           /* Note that a CONST_DOUBLE rtx could represent either an integer
15578              or a floating-point constant.  A CONST_DOUBLE is used whenever
15579              the constant requires more than one word in order to be
15580              adequately represented.  We output CONST_DOUBLEs as blocks.  */
15581           loc_result = new_loc_descr (DW_OP_implicit_value,
15582                                       GET_MODE_SIZE (mode), 0);
15583           if (SCALAR_FLOAT_MODE_P (mode))
15584             {
15585               unsigned int length = GET_MODE_SIZE (mode);
15586               unsigned char *array
15587                   = (unsigned char*) ggc_alloc_atomic (length);
15588
15589               insert_float (rtl, array);
15590               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15591               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15592               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15593               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15594             }
15595           else
15596             {
15597               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15598               loc_result->dw_loc_oprnd2.v.val_double
15599                 = rtx_to_double_int (rtl);
15600             }
15601         }
15602       break;
15603
15604     case CONST_VECTOR:
15605       if (mode == VOIDmode)
15606         mode = GET_MODE (rtl);
15607
15608       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15609         {
15610           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15611           unsigned int length = CONST_VECTOR_NUNITS (rtl);
15612           unsigned char *array = (unsigned char *)
15613             ggc_alloc_atomic (length * elt_size);
15614           unsigned int i;
15615           unsigned char *p;
15616
15617           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15618           switch (GET_MODE_CLASS (mode))
15619             {
15620             case MODE_VECTOR_INT:
15621               for (i = 0, p = array; i < length; i++, p += elt_size)
15622                 {
15623                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15624                   double_int val = rtx_to_double_int (elt);
15625
15626                   if (elt_size <= sizeof (HOST_WIDE_INT))
15627                     insert_int (double_int_to_shwi (val), elt_size, p);
15628                   else
15629                     {
15630                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15631                       insert_double (val, p);
15632                     }
15633                 }
15634               break;
15635
15636             case MODE_VECTOR_FLOAT:
15637               for (i = 0, p = array; i < length; i++, p += elt_size)
15638                 {
15639                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15640                   insert_float (elt, p);
15641                 }
15642               break;
15643
15644             default:
15645               gcc_unreachable ();
15646             }
15647
15648           loc_result = new_loc_descr (DW_OP_implicit_value,
15649                                       length * elt_size, 0);
15650           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15651           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15652           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15653           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15654         }
15655       break;
15656
15657     case CONST:
15658       if (mode == VOIDmode
15659           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
15660           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
15661           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15662         {
15663           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15664           break;
15665         }
15666       /* FALLTHROUGH */
15667     case SYMBOL_REF:
15668       if (!const_ok_for_output (rtl))
15669         break;
15670     case LABEL_REF:
15671       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15672           && (dwarf_version >= 4 || !dwarf_strict))
15673         {
15674           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
15675           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
15676           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
15677           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15678           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15679         }
15680       break;
15681
15682     case DEBUG_IMPLICIT_PTR:
15683       loc_result = implicit_ptr_descriptor (rtl, 0);
15684       break;
15685
15686     case PLUS:
15687       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15688           && CONST_INT_P (XEXP (rtl, 1)))
15689         {
15690           loc_result
15691             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15692           break;
15693         }
15694       /* FALLTHRU */
15695     do_default:
15696     default:
15697       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
15698            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15699            && dwarf_version >= 4)
15700           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15701         {
15702           /* Value expression.  */
15703           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15704           if (loc_result)
15705             add_loc_descr (&loc_result,
15706                            new_loc_descr (DW_OP_stack_value, 0, 0));
15707         }
15708       break;
15709     }
15710
15711   return loc_result;
15712 }
15713
15714 /* We need to figure out what section we should use as the base for the
15715    address ranges where a given location is valid.
15716    1. If this particular DECL has a section associated with it, use that.
15717    2. If this function has a section associated with it, use that.
15718    3. Otherwise, use the text section.
15719    XXX: If you split a variable across multiple sections, we won't notice.  */
15720
15721 static const char *
15722 secname_for_decl (const_tree decl)
15723 {
15724   const char *secname;
15725
15726   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
15727     {
15728       tree sectree = DECL_SECTION_NAME (decl);
15729       secname = TREE_STRING_POINTER (sectree);
15730     }
15731   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15732     {
15733       tree sectree = DECL_SECTION_NAME (current_function_decl);
15734       secname = TREE_STRING_POINTER (sectree);
15735     }
15736   else if (cfun && in_cold_section_p)
15737     secname = crtl->subsections.cold_section_label;
15738   else
15739     secname = text_section_label;
15740
15741   return secname;
15742 }
15743
15744 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
15745
15746 static bool
15747 decl_by_reference_p (tree decl)
15748 {
15749   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15750            || TREE_CODE (decl) == VAR_DECL)
15751           && DECL_BY_REFERENCE (decl));
15752 }
15753
15754 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15755    for VARLOC.  */
15756
15757 static dw_loc_descr_ref
15758 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15759                enum var_init_status initialized)
15760 {
15761   int have_address = 0;
15762   dw_loc_descr_ref descr;
15763   enum machine_mode mode;
15764
15765   if (want_address != 2)
15766     {
15767       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15768       /* Single part.  */
15769       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15770         {
15771           varloc = PAT_VAR_LOCATION_LOC (varloc);
15772           if (GET_CODE (varloc) == EXPR_LIST)
15773             varloc = XEXP (varloc, 0);
15774           mode = GET_MODE (varloc);
15775           if (MEM_P (varloc))
15776             {
15777               rtx addr = XEXP (varloc, 0);
15778               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15779                                           mode, initialized);
15780               if (descr)
15781                 have_address = 1;
15782               else
15783                 {
15784                   rtx x = avoid_constant_pool_reference (varloc);
15785                   if (x != varloc)
15786                     descr = mem_loc_descriptor (x, mode, VOIDmode,
15787                                                 initialized);
15788                 }
15789             }
15790           else
15791             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15792         }
15793       else
15794         return 0;
15795     }
15796   else
15797     {
15798       if (GET_CODE (varloc) == VAR_LOCATION)
15799         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15800       else
15801         mode = DECL_MODE (loc);
15802       descr = loc_descriptor (varloc, mode, initialized);
15803       have_address = 1;
15804     }
15805
15806   if (!descr)
15807     return 0;
15808
15809   if (want_address == 2 && !have_address
15810       && (dwarf_version >= 4 || !dwarf_strict))
15811     {
15812       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15813         {
15814           expansion_failed (loc, NULL_RTX,
15815                             "DWARF address size mismatch");
15816           return 0;
15817         }
15818       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15819       have_address = 1;
15820     }
15821   /* Show if we can't fill the request for an address.  */
15822   if (want_address && !have_address)
15823     {
15824       expansion_failed (loc, NULL_RTX,
15825                         "Want address and only have value");
15826       return 0;
15827     }
15828
15829   /* If we've got an address and don't want one, dereference.  */
15830   if (!want_address && have_address)
15831     {
15832       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15833       enum dwarf_location_atom op;
15834
15835       if (size > DWARF2_ADDR_SIZE || size == -1)
15836         {
15837           expansion_failed (loc, NULL_RTX,
15838                             "DWARF address size mismatch");
15839           return 0;
15840         }
15841       else if (size == DWARF2_ADDR_SIZE)
15842         op = DW_OP_deref;
15843       else
15844         op = DW_OP_deref_size;
15845
15846       add_loc_descr (&descr, new_loc_descr (op, size, 0));
15847     }
15848
15849   return descr;
15850 }
15851
15852 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15853    if it is not possible.  */
15854
15855 static dw_loc_descr_ref
15856 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15857 {
15858   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15859     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15860   else if (dwarf_version >= 3 || !dwarf_strict)
15861     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15862   else
15863     return NULL;
15864 }
15865
15866 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15867    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
15868
15869 static dw_loc_descr_ref
15870 dw_sra_loc_expr (tree decl, rtx loc)
15871 {
15872   rtx p;
15873   unsigned int padsize = 0;
15874   dw_loc_descr_ref descr, *descr_tail;
15875   unsigned HOST_WIDE_INT decl_size;
15876   rtx varloc;
15877   enum var_init_status initialized;
15878
15879   if (DECL_SIZE (decl) == NULL
15880       || !host_integerp (DECL_SIZE (decl), 1))
15881     return NULL;
15882
15883   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
15884   descr = NULL;
15885   descr_tail = &descr;
15886
15887   for (p = loc; p; p = XEXP (p, 1))
15888     {
15889       unsigned int bitsize = decl_piece_bitsize (p);
15890       rtx loc_note = *decl_piece_varloc_ptr (p);
15891       dw_loc_descr_ref cur_descr;
15892       dw_loc_descr_ref *tail, last = NULL;
15893       unsigned int opsize = 0;
15894
15895       if (loc_note == NULL_RTX
15896           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15897         {
15898           padsize += bitsize;
15899           continue;
15900         }
15901       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15902       varloc = NOTE_VAR_LOCATION (loc_note);
15903       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15904       if (cur_descr == NULL)
15905         {
15906           padsize += bitsize;
15907           continue;
15908         }
15909
15910       /* Check that cur_descr either doesn't use
15911          DW_OP_*piece operations, or their sum is equal
15912          to bitsize.  Otherwise we can't embed it.  */
15913       for (tail = &cur_descr; *tail != NULL;
15914            tail = &(*tail)->dw_loc_next)
15915         if ((*tail)->dw_loc_opc == DW_OP_piece)
15916           {
15917             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
15918                       * BITS_PER_UNIT;
15919             last = *tail;
15920           }
15921         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
15922           {
15923             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
15924             last = *tail;
15925           }
15926
15927       if (last != NULL && opsize != bitsize)
15928         {
15929           padsize += bitsize;
15930           continue;
15931         }
15932
15933       /* If there is a hole, add DW_OP_*piece after empty DWARF
15934          expression, which means that those bits are optimized out.  */
15935       if (padsize)
15936         {
15937           if (padsize > decl_size)
15938             return NULL;
15939           decl_size -= padsize;
15940           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15941           if (*descr_tail == NULL)
15942             return NULL;
15943           descr_tail = &(*descr_tail)->dw_loc_next;
15944           padsize = 0;
15945         }
15946       *descr_tail = cur_descr;
15947       descr_tail = tail;
15948       if (bitsize > decl_size)
15949         return NULL;
15950       decl_size -= bitsize;
15951       if (last == NULL)
15952         {
15953           HOST_WIDE_INT offset = 0;
15954           if (GET_CODE (varloc) == VAR_LOCATION
15955               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15956             {
15957               varloc = PAT_VAR_LOCATION_LOC (varloc);
15958               if (GET_CODE (varloc) == EXPR_LIST)
15959                 varloc = XEXP (varloc, 0);
15960             }
15961           do 
15962             {
15963               if (GET_CODE (varloc) == CONST
15964                   || GET_CODE (varloc) == SIGN_EXTEND
15965                   || GET_CODE (varloc) == ZERO_EXTEND)
15966                 varloc = XEXP (varloc, 0);
15967               else if (GET_CODE (varloc) == SUBREG)
15968                 varloc = SUBREG_REG (varloc);
15969               else
15970                 break;
15971             }
15972           while (1);
15973           /* DW_OP_bit_size offset should be zero for register
15974              or implicit location descriptions and empty location
15975              descriptions, but for memory addresses needs big endian
15976              adjustment.  */
15977           if (MEM_P (varloc))
15978             {
15979               unsigned HOST_WIDE_INT memsize
15980                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15981               if (memsize != bitsize)
15982                 {
15983                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15984                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15985                     return NULL;
15986                   if (memsize < bitsize)
15987                     return NULL;
15988                   if (BITS_BIG_ENDIAN)
15989                     offset = memsize - bitsize;
15990                 }
15991             }
15992
15993           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15994           if (*descr_tail == NULL)
15995             return NULL;
15996           descr_tail = &(*descr_tail)->dw_loc_next;
15997         }
15998     }
15999
16000   /* If there were any non-empty expressions, add padding till the end of
16001      the decl.  */
16002   if (descr != NULL && decl_size != 0)
16003     {
16004       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16005       if (*descr_tail == NULL)
16006         return NULL;
16007     }
16008   return descr;
16009 }
16010
16011 /* Return the dwarf representation of the location list LOC_LIST of
16012    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
16013    function.  */
16014
16015 static dw_loc_list_ref
16016 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16017 {
16018   const char *endname, *secname;
16019   rtx varloc;
16020   enum var_init_status initialized;
16021   struct var_loc_node *node;
16022   dw_loc_descr_ref descr;
16023   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16024   dw_loc_list_ref list = NULL;
16025   dw_loc_list_ref *listp = &list;
16026
16027   /* Now that we know what section we are using for a base,
16028      actually construct the list of locations.
16029      The first location information is what is passed to the
16030      function that creates the location list, and the remaining
16031      locations just get added on to that list.
16032      Note that we only know the start address for a location
16033      (IE location changes), so to build the range, we use
16034      the range [current location start, next location start].
16035      This means we have to special case the last node, and generate
16036      a range of [last location start, end of function label].  */
16037
16038   secname = secname_for_decl (decl);
16039
16040   for (node = loc_list->first; node; node = node->next)
16041     if (GET_CODE (node->loc) == EXPR_LIST
16042         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16043       {
16044         if (GET_CODE (node->loc) == EXPR_LIST)
16045           {
16046             /* This requires DW_OP_{,bit_}piece, which is not usable
16047                inside DWARF expressions.  */
16048             if (want_address != 2)
16049               continue;
16050             descr = dw_sra_loc_expr (decl, node->loc);
16051             if (descr == NULL)
16052               continue;
16053           }
16054         else
16055           {
16056             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16057             varloc = NOTE_VAR_LOCATION (node->loc);
16058             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16059           }
16060         if (descr)
16061           {
16062             bool range_across_switch = false;
16063             /* If section switch happens in between node->label
16064                and node->next->label (or end of function) and
16065                we can't emit it as a single entry list,
16066                emit two ranges, first one ending at the end
16067                of first partition and second one starting at the
16068                beginning of second partition.  */
16069             if (node == loc_list->last_before_switch
16070                 && (node != loc_list->first || loc_list->first->next)
16071                 && current_function_decl)
16072               {
16073                 endname = current_fde ()->dw_fde_end;
16074                 range_across_switch = true;
16075               }
16076             /* The variable has a location between NODE->LABEL and
16077                NODE->NEXT->LABEL.  */
16078             else if (node->next)
16079               endname = node->next->label;
16080             /* If the variable has a location at the last label
16081                it keeps its location until the end of function.  */
16082             else if (!current_function_decl)
16083               endname = text_end_label;
16084             else
16085               {
16086                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16087                                              current_function_funcdef_no);
16088                 endname = ggc_strdup (label_id);
16089               }
16090
16091             *listp = new_loc_list (descr, node->label, endname, secname);
16092             listp = &(*listp)->dw_loc_next;
16093
16094             if (range_across_switch)
16095               {
16096                 if (GET_CODE (node->loc) == EXPR_LIST)
16097                   descr = dw_sra_loc_expr (decl, node->loc);
16098                 else
16099                   {
16100                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16101                     varloc = NOTE_VAR_LOCATION (node->loc);
16102                     descr = dw_loc_list_1 (decl, varloc, want_address,
16103                                            initialized);
16104                   }
16105                 gcc_assert (descr);
16106                 /* The variable has a location between NODE->LABEL and
16107                    NODE->NEXT->LABEL.  */
16108                 if (node->next)
16109                   endname = node->next->label;
16110                 else
16111                   endname = current_fde ()->dw_fde_second_end;
16112                 *listp = new_loc_list (descr,
16113                                        current_fde ()->dw_fde_second_begin,
16114                                        endname, secname);
16115                 listp = &(*listp)->dw_loc_next;
16116               }
16117           }
16118       }
16119
16120   /* Try to avoid the overhead of a location list emitting a location
16121      expression instead, but only if we didn't have more than one
16122      location entry in the first place.  If some entries were not
16123      representable, we don't want to pretend a single entry that was
16124      applies to the entire scope in which the variable is
16125      available.  */
16126   if (list && loc_list->first->next)
16127     gen_llsym (list);
16128
16129   return list;
16130 }
16131
16132 /* Return if the loc_list has only single element and thus can be represented
16133    as location description.   */
16134
16135 static bool
16136 single_element_loc_list_p (dw_loc_list_ref list)
16137 {
16138   gcc_assert (!list->dw_loc_next || list->ll_symbol);
16139   return !list->ll_symbol;
16140 }
16141
16142 /* To each location in list LIST add loc descr REF.  */
16143
16144 static void
16145 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16146 {
16147   dw_loc_descr_ref copy;
16148   add_loc_descr (&list->expr, ref);
16149   list = list->dw_loc_next;
16150   while (list)
16151     {
16152       copy = ggc_alloc_dw_loc_descr_node ();
16153       memcpy (copy, ref, sizeof (dw_loc_descr_node));
16154       add_loc_descr (&list->expr, copy);
16155       while (copy->dw_loc_next)
16156         {
16157           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
16158           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
16159           copy->dw_loc_next = new_copy;
16160           copy = new_copy;
16161         }
16162       list = list->dw_loc_next;
16163     }
16164 }
16165
16166 /* Given two lists RET and LIST
16167    produce location list that is result of adding expression in LIST
16168    to expression in RET on each possition in program.
16169    Might be destructive on both RET and LIST.
16170
16171    TODO: We handle only simple cases of RET or LIST having at most one
16172    element. General case would inolve sorting the lists in program order
16173    and merging them that will need some additional work.
16174    Adding that will improve quality of debug info especially for SRA-ed
16175    structures.  */
16176
16177 static void
16178 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16179 {
16180   if (!list)
16181     return;
16182   if (!*ret)
16183     {
16184       *ret = list;
16185       return;
16186     }
16187   if (!list->dw_loc_next)
16188     {
16189       add_loc_descr_to_each (*ret, list->expr);
16190       return;
16191     }
16192   if (!(*ret)->dw_loc_next)
16193     {
16194       add_loc_descr_to_each (list, (*ret)->expr);
16195       *ret = list;
16196       return;
16197     }
16198   expansion_failed (NULL_TREE, NULL_RTX,
16199                     "Don't know how to merge two non-trivial"
16200                     " location lists.\n");
16201   *ret = NULL;
16202   return;
16203 }
16204
16205 /* LOC is constant expression.  Try a luck, look it up in constant
16206    pool and return its loc_descr of its address.  */
16207
16208 static dw_loc_descr_ref
16209 cst_pool_loc_descr (tree loc)
16210 {
16211   /* Get an RTL for this, if something has been emitted.  */
16212   rtx rtl = lookup_constant_def (loc);
16213
16214   if (!rtl || !MEM_P (rtl))
16215     {
16216       gcc_assert (!rtl);
16217       return 0;
16218     }
16219   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16220
16221   /* TODO: We might get more coverage if we was actually delaying expansion
16222      of all expressions till end of compilation when constant pools are fully
16223      populated.  */
16224   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16225     {
16226       expansion_failed (loc, NULL_RTX,
16227                         "CST value in contant pool but not marked.");
16228       return 0;
16229     }
16230   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16231                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16232 }
16233
16234 /* Return dw_loc_list representing address of addr_expr LOC
16235    by looking for innder INDIRECT_REF expression and turing it
16236    into simple arithmetics.  */
16237
16238 static dw_loc_list_ref
16239 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
16240 {
16241   tree obj, offset;
16242   HOST_WIDE_INT bitsize, bitpos, bytepos;
16243   enum machine_mode mode;
16244   int volatilep;
16245   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
16246   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16247
16248   obj = get_inner_reference (TREE_OPERAND (loc, 0),
16249                              &bitsize, &bitpos, &offset, &mode,
16250                              &unsignedp, &volatilep, false);
16251   STRIP_NOPS (obj);
16252   if (bitpos % BITS_PER_UNIT)
16253     {
16254       expansion_failed (loc, NULL_RTX, "bitfield access");
16255       return 0;
16256     }
16257   if (!INDIRECT_REF_P (obj))
16258     {
16259       expansion_failed (obj,
16260                         NULL_RTX, "no indirect ref in inner refrence");
16261       return 0;
16262     }
16263   if (!offset && !bitpos)
16264     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
16265   else if (toplev
16266            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16267            && (dwarf_version >= 4 || !dwarf_strict))
16268     {
16269       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
16270       if (!list_ret)
16271         return 0;
16272       if (offset)
16273         {
16274           /* Variable offset.  */
16275           list_ret1 = loc_list_from_tree (offset, 0);
16276           if (list_ret1 == 0)
16277             return 0;
16278           add_loc_list (&list_ret, list_ret1);
16279           if (!list_ret)
16280             return 0;
16281           add_loc_descr_to_each (list_ret,
16282                                  new_loc_descr (DW_OP_plus, 0, 0));
16283         }
16284       bytepos = bitpos / BITS_PER_UNIT;
16285       if (bytepos > 0)
16286         add_loc_descr_to_each (list_ret,
16287                                new_loc_descr (DW_OP_plus_uconst,
16288                                               bytepos, 0));
16289       else if (bytepos < 0)
16290         loc_list_plus_const (list_ret, bytepos);
16291       add_loc_descr_to_each (list_ret,
16292                              new_loc_descr (DW_OP_stack_value, 0, 0));
16293     }
16294   return list_ret;
16295 }
16296
16297
16298 /* Generate Dwarf location list representing LOC.
16299    If WANT_ADDRESS is false, expression computing LOC will be computed
16300    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
16301    if WANT_ADDRESS is 2, expression computing address useable in location
16302      will be returned (i.e. DW_OP_reg can be used
16303      to refer to register values).  */
16304
16305 static dw_loc_list_ref
16306 loc_list_from_tree (tree loc, int want_address)
16307 {
16308   dw_loc_descr_ref ret = NULL, ret1 = NULL;
16309   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16310   int have_address = 0;
16311   enum dwarf_location_atom op;
16312
16313   /* ??? Most of the time we do not take proper care for sign/zero
16314      extending the values properly.  Hopefully this won't be a real
16315      problem...  */
16316
16317   switch (TREE_CODE (loc))
16318     {
16319     case ERROR_MARK:
16320       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
16321       return 0;
16322
16323     case PLACEHOLDER_EXPR:
16324       /* This case involves extracting fields from an object to determine the
16325          position of other fields.  We don't try to encode this here.  The
16326          only user of this is Ada, which encodes the needed information using
16327          the names of types.  */
16328       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
16329       return 0;
16330
16331     case CALL_EXPR:
16332       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
16333       /* There are no opcodes for these operations.  */
16334       return 0;
16335
16336     case PREINCREMENT_EXPR:
16337     case PREDECREMENT_EXPR:
16338     case POSTINCREMENT_EXPR:
16339     case POSTDECREMENT_EXPR:
16340       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
16341       /* There are no opcodes for these operations.  */
16342       return 0;
16343
16344     case ADDR_EXPR:
16345       /* If we already want an address, see if there is INDIRECT_REF inside
16346          e.g. for &this->field.  */
16347       if (want_address)
16348         {
16349           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
16350                        (loc, want_address == 2);
16351           if (list_ret)
16352             have_address = 1;
16353           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
16354                    && (ret = cst_pool_loc_descr (loc)))
16355             have_address = 1;
16356         }
16357         /* Otherwise, process the argument and look for the address.  */
16358       if (!list_ret && !ret)
16359         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
16360       else
16361         {
16362           if (want_address)
16363             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
16364           return NULL;
16365         }
16366       break;
16367
16368     case VAR_DECL:
16369       if (DECL_THREAD_LOCAL_P (loc))
16370         {
16371           rtx rtl;
16372           enum dwarf_location_atom first_op;
16373           enum dwarf_location_atom second_op;
16374           bool dtprel = false;
16375
16376           if (targetm.have_tls)
16377             {
16378               /* If this is not defined, we have no way to emit the
16379                  data.  */
16380               if (!targetm.asm_out.output_dwarf_dtprel)
16381                 return 0;
16382
16383                /* The way DW_OP_GNU_push_tls_address is specified, we
16384                   can only look up addresses of objects in the current
16385                   module.  We used DW_OP_addr as first op, but that's
16386                   wrong, because DW_OP_addr is relocated by the debug
16387                   info consumer, while DW_OP_GNU_push_tls_address
16388                   operand shouldn't be.  */
16389               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
16390                 return 0;
16391               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
16392               dtprel = true;
16393               second_op = DW_OP_GNU_push_tls_address;
16394             }
16395           else
16396             {
16397               if (!targetm.emutls.debug_form_tls_address
16398                   || !(dwarf_version >= 3 || !dwarf_strict))
16399                 return 0;
16400               /* We stuffed the control variable into the DECL_VALUE_EXPR
16401                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
16402                  no longer appear in gimple code.  We used the control
16403                  variable in specific so that we could pick it up here.  */
16404               loc = DECL_VALUE_EXPR (loc);
16405               first_op = DW_OP_addr;
16406               second_op = DW_OP_form_tls_address;
16407             }
16408
16409           rtl = rtl_for_decl_location (loc);
16410           if (rtl == NULL_RTX)
16411             return 0;
16412
16413           if (!MEM_P (rtl))
16414             return 0;
16415           rtl = XEXP (rtl, 0);
16416           if (! CONSTANT_P (rtl))
16417             return 0;
16418
16419           ret = new_loc_descr (first_op, 0, 0);
16420           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
16421           ret->dw_loc_oprnd1.v.val_addr = rtl;
16422           ret->dtprel = dtprel;
16423
16424           ret1 = new_loc_descr (second_op, 0, 0);
16425           add_loc_descr (&ret, ret1);
16426
16427           have_address = 1;
16428           break;
16429         }
16430       /* FALLTHRU */
16431
16432     case PARM_DECL:
16433     case RESULT_DECL:
16434       if (DECL_HAS_VALUE_EXPR_P (loc))
16435         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
16436                                    want_address);
16437       /* FALLTHRU */
16438
16439     case FUNCTION_DECL:
16440       {
16441         rtx rtl;
16442         var_loc_list *loc_list = lookup_decl_loc (loc);
16443
16444         if (loc_list && loc_list->first)
16445           {
16446             list_ret = dw_loc_list (loc_list, loc, want_address);
16447             have_address = want_address != 0;
16448             break;
16449           }
16450         rtl = rtl_for_decl_location (loc);
16451         if (rtl == NULL_RTX)
16452           {
16453             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
16454             return 0;
16455           }
16456         else if (CONST_INT_P (rtl))
16457           {
16458             HOST_WIDE_INT val = INTVAL (rtl);
16459             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16460               val &= GET_MODE_MASK (DECL_MODE (loc));
16461             ret = int_loc_descriptor (val);
16462           }
16463         else if (GET_CODE (rtl) == CONST_STRING)
16464           {
16465             expansion_failed (loc, NULL_RTX, "CONST_STRING");
16466             return 0;
16467           }
16468         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16469           {
16470             ret = new_loc_descr (DW_OP_addr, 0, 0);
16471             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
16472             ret->dw_loc_oprnd1.v.val_addr = rtl;
16473           }
16474         else
16475           {
16476             enum machine_mode mode, mem_mode;
16477
16478             /* Certain constructs can only be represented at top-level.  */
16479             if (want_address == 2)
16480               {
16481                 ret = loc_descriptor (rtl, VOIDmode,
16482                                       VAR_INIT_STATUS_INITIALIZED);
16483                 have_address = 1;
16484               }
16485             else
16486               {
16487                 mode = GET_MODE (rtl);
16488                 mem_mode = VOIDmode;
16489                 if (MEM_P (rtl))
16490                   {
16491                     mem_mode = mode;
16492                     mode = get_address_mode (rtl);
16493                     rtl = XEXP (rtl, 0);
16494                     have_address = 1;
16495                   }
16496                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16497                                           VAR_INIT_STATUS_INITIALIZED);
16498               }
16499             if (!ret)
16500               expansion_failed (loc, rtl,
16501                                 "failed to produce loc descriptor for rtl");
16502           }
16503       }
16504       break;
16505
16506     case MEM_REF:
16507       /* ??? FIXME.  */
16508       if (!integer_zerop (TREE_OPERAND (loc, 1)))
16509         return 0;
16510       /* Fallthru.  */
16511     case INDIRECT_REF:
16512       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16513       have_address = 1;
16514       break;
16515
16516     case COMPOUND_EXPR:
16517       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
16518
16519     CASE_CONVERT:
16520     case VIEW_CONVERT_EXPR:
16521     case SAVE_EXPR:
16522     case MODIFY_EXPR:
16523       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
16524
16525     case COMPONENT_REF:
16526     case BIT_FIELD_REF:
16527     case ARRAY_REF:
16528     case ARRAY_RANGE_REF:
16529     case REALPART_EXPR:
16530     case IMAGPART_EXPR:
16531       {
16532         tree obj, offset;
16533         HOST_WIDE_INT bitsize, bitpos, bytepos;
16534         enum machine_mode mode;
16535         int volatilep;
16536         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
16537
16538         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16539                                    &unsignedp, &volatilep, false);
16540
16541         gcc_assert (obj != loc);
16542
16543         list_ret = loc_list_from_tree (obj,
16544                                        want_address == 2
16545                                        && !bitpos && !offset ? 2 : 1);
16546         /* TODO: We can extract value of the small expression via shifting even
16547            for nonzero bitpos.  */
16548         if (list_ret == 0)
16549           return 0;
16550         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16551           {
16552             expansion_failed (loc, NULL_RTX,
16553                               "bitfield access");
16554             return 0;
16555           }
16556
16557         if (offset != NULL_TREE)
16558           {
16559             /* Variable offset.  */
16560             list_ret1 = loc_list_from_tree (offset, 0);
16561             if (list_ret1 == 0)
16562               return 0;
16563             add_loc_list (&list_ret, list_ret1);
16564             if (!list_ret)
16565               return 0;
16566             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16567           }
16568
16569         bytepos = bitpos / BITS_PER_UNIT;
16570         if (bytepos > 0)
16571           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16572         else if (bytepos < 0)
16573           loc_list_plus_const (list_ret, bytepos);
16574
16575         have_address = 1;
16576         break;
16577       }
16578
16579     case INTEGER_CST:
16580       if ((want_address || !host_integerp (loc, 0))
16581           && (ret = cst_pool_loc_descr (loc)))
16582         have_address = 1;
16583       else if (want_address == 2
16584                && host_integerp (loc, 0)
16585                && (ret = address_of_int_loc_descriptor
16586                            (int_size_in_bytes (TREE_TYPE (loc)),
16587                             tree_low_cst (loc, 0))))
16588         have_address = 1;
16589       else if (host_integerp (loc, 0))
16590         ret = int_loc_descriptor (tree_low_cst (loc, 0));
16591       else
16592         {
16593           expansion_failed (loc, NULL_RTX,
16594                             "Integer operand is not host integer");
16595           return 0;
16596         }
16597       break;
16598
16599     case CONSTRUCTOR:
16600     case REAL_CST:
16601     case STRING_CST:
16602     case COMPLEX_CST:
16603       if ((ret = cst_pool_loc_descr (loc)))
16604         have_address = 1;
16605       else
16606       /* We can construct small constants here using int_loc_descriptor.  */
16607         expansion_failed (loc, NULL_RTX,
16608                           "constructor or constant not in constant pool");
16609       break;
16610
16611     case TRUTH_AND_EXPR:
16612     case TRUTH_ANDIF_EXPR:
16613     case BIT_AND_EXPR:
16614       op = DW_OP_and;
16615       goto do_binop;
16616
16617     case TRUTH_XOR_EXPR:
16618     case BIT_XOR_EXPR:
16619       op = DW_OP_xor;
16620       goto do_binop;
16621
16622     case TRUTH_OR_EXPR:
16623     case TRUTH_ORIF_EXPR:
16624     case BIT_IOR_EXPR:
16625       op = DW_OP_or;
16626       goto do_binop;
16627
16628     case FLOOR_DIV_EXPR:
16629     case CEIL_DIV_EXPR:
16630     case ROUND_DIV_EXPR:
16631     case TRUNC_DIV_EXPR:
16632       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16633         return 0;
16634       op = DW_OP_div;
16635       goto do_binop;
16636
16637     case MINUS_EXPR:
16638       op = DW_OP_minus;
16639       goto do_binop;
16640
16641     case FLOOR_MOD_EXPR:
16642     case CEIL_MOD_EXPR:
16643     case ROUND_MOD_EXPR:
16644     case TRUNC_MOD_EXPR:
16645       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16646         {
16647           op = DW_OP_mod;
16648           goto do_binop;
16649         }
16650       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16651       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16652       if (list_ret == 0 || list_ret1 == 0)
16653         return 0;
16654
16655       add_loc_list (&list_ret, list_ret1);
16656       if (list_ret == 0)
16657         return 0;
16658       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16659       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16660       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16661       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16662       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16663       break;
16664
16665     case MULT_EXPR:
16666       op = DW_OP_mul;
16667       goto do_binop;
16668
16669     case LSHIFT_EXPR:
16670       op = DW_OP_shl;
16671       goto do_binop;
16672
16673     case RSHIFT_EXPR:
16674       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16675       goto do_binop;
16676
16677     case POINTER_PLUS_EXPR:
16678     case PLUS_EXPR:
16679       if (host_integerp (TREE_OPERAND (loc, 1), 0))
16680         {
16681           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16682           if (list_ret == 0)
16683             return 0;
16684
16685           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
16686           break;
16687         }
16688
16689       op = DW_OP_plus;
16690       goto do_binop;
16691
16692     case LE_EXPR:
16693       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16694         return 0;
16695
16696       op = DW_OP_le;
16697       goto do_binop;
16698
16699     case GE_EXPR:
16700       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16701         return 0;
16702
16703       op = DW_OP_ge;
16704       goto do_binop;
16705
16706     case LT_EXPR:
16707       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16708         return 0;
16709
16710       op = DW_OP_lt;
16711       goto do_binop;
16712
16713     case GT_EXPR:
16714       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16715         return 0;
16716
16717       op = DW_OP_gt;
16718       goto do_binop;
16719
16720     case EQ_EXPR:
16721       op = DW_OP_eq;
16722       goto do_binop;
16723
16724     case NE_EXPR:
16725       op = DW_OP_ne;
16726       goto do_binop;
16727
16728     do_binop:
16729       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16730       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16731       if (list_ret == 0 || list_ret1 == 0)
16732         return 0;
16733
16734       add_loc_list (&list_ret, list_ret1);
16735       if (list_ret == 0)
16736         return 0;
16737       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16738       break;
16739
16740     case TRUTH_NOT_EXPR:
16741     case BIT_NOT_EXPR:
16742       op = DW_OP_not;
16743       goto do_unop;
16744
16745     case ABS_EXPR:
16746       op = DW_OP_abs;
16747       goto do_unop;
16748
16749     case NEGATE_EXPR:
16750       op = DW_OP_neg;
16751       goto do_unop;
16752
16753     do_unop:
16754       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16755       if (list_ret == 0)
16756         return 0;
16757
16758       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16759       break;
16760
16761     case MIN_EXPR:
16762     case MAX_EXPR:
16763       {
16764         const enum tree_code code =
16765           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16766
16767         loc = build3 (COND_EXPR, TREE_TYPE (loc),
16768                       build2 (code, integer_type_node,
16769                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16770                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16771       }
16772
16773       /* ... fall through ...  */
16774
16775     case COND_EXPR:
16776       {
16777         dw_loc_descr_ref lhs
16778           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
16779         dw_loc_list_ref rhs
16780           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
16781         dw_loc_descr_ref bra_node, jump_node, tmp;
16782
16783         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16784         if (list_ret == 0 || lhs == 0 || rhs == 0)
16785           return 0;
16786
16787         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16788         add_loc_descr_to_each (list_ret, bra_node);
16789
16790         add_loc_list (&list_ret, rhs);
16791         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16792         add_loc_descr_to_each (list_ret, jump_node);
16793
16794         add_loc_descr_to_each (list_ret, lhs);
16795         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16796         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16797
16798         /* ??? Need a node to point the skip at.  Use a nop.  */
16799         tmp = new_loc_descr (DW_OP_nop, 0, 0);
16800         add_loc_descr_to_each (list_ret, tmp);
16801         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16802         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16803       }
16804       break;
16805
16806     case FIX_TRUNC_EXPR:
16807       return 0;
16808
16809     default:
16810       /* Leave front-end specific codes as simply unknown.  This comes
16811          up, for instance, with the C STMT_EXPR.  */
16812       if ((unsigned int) TREE_CODE (loc)
16813           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16814         {
16815           expansion_failed (loc, NULL_RTX,
16816                             "language specific tree node");
16817           return 0;
16818         }
16819
16820 #ifdef ENABLE_CHECKING
16821       /* Otherwise this is a generic code; we should just lists all of
16822          these explicitly.  We forgot one.  */
16823       gcc_unreachable ();
16824 #else
16825       /* In a release build, we want to degrade gracefully: better to
16826          generate incomplete debugging information than to crash.  */
16827       return NULL;
16828 #endif
16829     }
16830
16831   if (!ret && !list_ret)
16832     return 0;
16833
16834   if (want_address == 2 && !have_address
16835       && (dwarf_version >= 4 || !dwarf_strict))
16836     {
16837       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16838         {
16839           expansion_failed (loc, NULL_RTX,
16840                             "DWARF address size mismatch");
16841           return 0;
16842         }
16843       if (ret)
16844         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16845       else
16846         add_loc_descr_to_each (list_ret,
16847                                new_loc_descr (DW_OP_stack_value, 0, 0));
16848       have_address = 1;
16849     }
16850   /* Show if we can't fill the request for an address.  */
16851   if (want_address && !have_address)
16852     {
16853       expansion_failed (loc, NULL_RTX,
16854                         "Want address and only have value");
16855       return 0;
16856     }
16857
16858   gcc_assert (!ret || !list_ret);
16859
16860   /* If we've got an address and don't want one, dereference.  */
16861   if (!want_address && have_address)
16862     {
16863       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16864
16865       if (size > DWARF2_ADDR_SIZE || size == -1)
16866         {
16867           expansion_failed (loc, NULL_RTX,
16868                             "DWARF address size mismatch");
16869           return 0;
16870         }
16871       else if (size == DWARF2_ADDR_SIZE)
16872         op = DW_OP_deref;
16873       else
16874         op = DW_OP_deref_size;
16875
16876       if (ret)
16877         add_loc_descr (&ret, new_loc_descr (op, size, 0));
16878       else
16879         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16880     }
16881   if (ret)
16882     list_ret = new_loc_list (ret, NULL, NULL, NULL);
16883
16884   return list_ret;
16885 }
16886
16887 /* Same as above but return only single location expression.  */
16888 static dw_loc_descr_ref
16889 loc_descriptor_from_tree (tree loc, int want_address)
16890 {
16891   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
16892   if (!ret)
16893     return NULL;
16894   if (ret->dw_loc_next)
16895     {
16896       expansion_failed (loc, NULL_RTX,
16897                         "Location list where only loc descriptor needed");
16898       return NULL;
16899     }
16900   return ret->expr;
16901 }
16902
16903 /* Given a value, round it up to the lowest multiple of `boundary'
16904    which is not less than the value itself.  */
16905
16906 static inline HOST_WIDE_INT
16907 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16908 {
16909   return (((value + boundary - 1) / boundary) * boundary);
16910 }
16911
16912 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16913    pointer to the declared type for the relevant field variable, or return
16914    `integer_type_node' if the given node turns out to be an
16915    ERROR_MARK node.  */
16916
16917 static inline tree
16918 field_type (const_tree decl)
16919 {
16920   tree type;
16921
16922   if (TREE_CODE (decl) == ERROR_MARK)
16923     return integer_type_node;
16924
16925   type = DECL_BIT_FIELD_TYPE (decl);
16926   if (type == NULL_TREE)
16927     type = TREE_TYPE (decl);
16928
16929   return type;
16930 }
16931
16932 /* Given a pointer to a tree node, return the alignment in bits for
16933    it, or else return BITS_PER_WORD if the node actually turns out to
16934    be an ERROR_MARK node.  */
16935
16936 static inline unsigned
16937 simple_type_align_in_bits (const_tree type)
16938 {
16939   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16940 }
16941
16942 static inline unsigned
16943 simple_decl_align_in_bits (const_tree decl)
16944 {
16945   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16946 }
16947
16948 /* Return the result of rounding T up to ALIGN.  */
16949
16950 static inline double_int
16951 round_up_to_align (double_int t, unsigned int align)
16952 {
16953   double_int alignd = uhwi_to_double_int (align);
16954   t = double_int_add (t, alignd);
16955   t = double_int_add (t, double_int_minus_one);
16956   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
16957   t = double_int_mul (t, alignd);
16958   return t;
16959 }
16960
16961 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
16962    lowest addressed byte of the "containing object" for the given FIELD_DECL,
16963    or return 0 if we are unable to determine what that offset is, either
16964    because the argument turns out to be a pointer to an ERROR_MARK node, or
16965    because the offset is actually variable.  (We can't handle the latter case
16966    just yet).  */
16967
16968 static HOST_WIDE_INT
16969 field_byte_offset (const_tree decl)
16970 {
16971   double_int object_offset_in_bits;
16972   double_int object_offset_in_bytes;
16973   double_int bitpos_int;
16974
16975   if (TREE_CODE (decl) == ERROR_MARK)
16976     return 0;
16977
16978   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16979
16980   /* We cannot yet cope with fields whose positions are variable, so
16981      for now, when we see such things, we simply return 0.  Someday, we may
16982      be able to handle such cases, but it will be damn difficult.  */
16983   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16984     return 0;
16985
16986   bitpos_int = tree_to_double_int (bit_position (decl));
16987
16988 #ifdef PCC_BITFIELD_TYPE_MATTERS
16989   if (PCC_BITFIELD_TYPE_MATTERS)
16990     {
16991       tree type;
16992       tree field_size_tree;
16993       double_int deepest_bitpos;
16994       double_int field_size_in_bits;
16995       unsigned int type_align_in_bits;
16996       unsigned int decl_align_in_bits;
16997       double_int type_size_in_bits;
16998
16999       type = field_type (decl);
17000       type_size_in_bits = double_int_type_size_in_bits (type);
17001       type_align_in_bits = simple_type_align_in_bits (type);
17002
17003       field_size_tree = DECL_SIZE (decl);
17004
17005       /* The size could be unspecified if there was an error, or for
17006          a flexible array member.  */
17007       if (!field_size_tree)
17008         field_size_tree = bitsize_zero_node;
17009
17010       /* If the size of the field is not constant, use the type size.  */
17011       if (TREE_CODE (field_size_tree) == INTEGER_CST)
17012         field_size_in_bits = tree_to_double_int (field_size_tree);
17013       else
17014         field_size_in_bits = type_size_in_bits;
17015
17016       decl_align_in_bits = simple_decl_align_in_bits (decl);
17017
17018       /* The GCC front-end doesn't make any attempt to keep track of the
17019          starting bit offset (relative to the start of the containing
17020          structure type) of the hypothetical "containing object" for a
17021          bit-field.  Thus, when computing the byte offset value for the
17022          start of the "containing object" of a bit-field, we must deduce
17023          this information on our own. This can be rather tricky to do in
17024          some cases.  For example, handling the following structure type
17025          definition when compiling for an i386/i486 target (which only
17026          aligns long long's to 32-bit boundaries) can be very tricky:
17027
17028          struct S { int field1; long long field2:31; };
17029
17030          Fortunately, there is a simple rule-of-thumb which can be used
17031          in such cases.  When compiling for an i386/i486, GCC will
17032          allocate 8 bytes for the structure shown above.  It decides to
17033          do this based upon one simple rule for bit-field allocation.
17034          GCC allocates each "containing object" for each bit-field at
17035          the first (i.e. lowest addressed) legitimate alignment boundary
17036          (based upon the required minimum alignment for the declared
17037          type of the field) which it can possibly use, subject to the
17038          condition that there is still enough available space remaining
17039          in the containing object (when allocated at the selected point)
17040          to fully accommodate all of the bits of the bit-field itself.
17041
17042          This simple rule makes it obvious why GCC allocates 8 bytes for
17043          each object of the structure type shown above.  When looking
17044          for a place to allocate the "containing object" for `field2',
17045          the compiler simply tries to allocate a 64-bit "containing
17046          object" at each successive 32-bit boundary (starting at zero)
17047          until it finds a place to allocate that 64- bit field such that
17048          at least 31 contiguous (and previously unallocated) bits remain
17049          within that selected 64 bit field.  (As it turns out, for the
17050          example above, the compiler finds it is OK to allocate the
17051          "containing object" 64-bit field at bit-offset zero within the
17052          structure type.)
17053
17054          Here we attempt to work backwards from the limited set of facts
17055          we're given, and we try to deduce from those facts, where GCC
17056          must have believed that the containing object started (within
17057          the structure type). The value we deduce is then used (by the
17058          callers of this routine) to generate DW_AT_location and
17059          DW_AT_bit_offset attributes for fields (both bit-fields and, in
17060          the case of DW_AT_location, regular fields as well).  */
17061
17062       /* Figure out the bit-distance from the start of the structure to
17063          the "deepest" bit of the bit-field.  */
17064       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
17065
17066       /* This is the tricky part.  Use some fancy footwork to deduce
17067          where the lowest addressed bit of the containing object must
17068          be.  */
17069       object_offset_in_bits
17070         = double_int_sub (deepest_bitpos, type_size_in_bits);
17071
17072       /* Round up to type_align by default.  This works best for
17073          bitfields.  */
17074       object_offset_in_bits
17075         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
17076
17077       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
17078         {
17079           object_offset_in_bits
17080             = double_int_sub (deepest_bitpos, type_size_in_bits);
17081
17082           /* Round up to decl_align instead.  */
17083           object_offset_in_bits
17084             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
17085         }
17086     }
17087   else
17088 #endif /* PCC_BITFIELD_TYPE_MATTERS */
17089     object_offset_in_bits = bitpos_int;
17090
17091   object_offset_in_bytes
17092     = double_int_div (object_offset_in_bits,
17093                       uhwi_to_double_int (BITS_PER_UNIT), true,
17094                       TRUNC_DIV_EXPR);
17095   return double_int_to_shwi (object_offset_in_bytes);
17096 }
17097 \f
17098 /* The following routines define various Dwarf attributes and any data
17099    associated with them.  */
17100
17101 /* Add a location description attribute value to a DIE.
17102
17103    This emits location attributes suitable for whole variables and
17104    whole parameters.  Note that the location attributes for struct fields are
17105    generated by the routine `data_member_location_attribute' below.  */
17106
17107 static inline void
17108 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
17109                              dw_loc_list_ref descr)
17110 {
17111   if (descr == 0)
17112     return;
17113   if (single_element_loc_list_p (descr))
17114     add_AT_loc (die, attr_kind, descr->expr);
17115   else
17116     add_AT_loc_list (die, attr_kind, descr);
17117 }
17118
17119 /* Add DW_AT_accessibility attribute to DIE if needed.  */
17120
17121 static void
17122 add_accessibility_attribute (dw_die_ref die, tree decl)
17123 {
17124   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
17125      children, otherwise the default is DW_ACCESS_public.  In DWARF2
17126      the default has always been DW_ACCESS_public.  */
17127   if (TREE_PROTECTED (decl))
17128     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
17129   else if (TREE_PRIVATE (decl))
17130     {
17131       if (dwarf_version == 2
17132           || die->die_parent == NULL
17133           || die->die_parent->die_tag != DW_TAG_class_type)
17134         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
17135     }
17136   else if (dwarf_version > 2
17137            && die->die_parent
17138            && die->die_parent->die_tag == DW_TAG_class_type)
17139     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
17140 }
17141
17142 /* Attach the specialized form of location attribute used for data members of
17143    struct and union types.  In the special case of a FIELD_DECL node which
17144    represents a bit-field, the "offset" part of this special location
17145    descriptor must indicate the distance in bytes from the lowest-addressed
17146    byte of the containing struct or union type to the lowest-addressed byte of
17147    the "containing object" for the bit-field.  (See the `field_byte_offset'
17148    function above).
17149
17150    For any given bit-field, the "containing object" is a hypothetical object
17151    (of some integral or enum type) within which the given bit-field lives.  The
17152    type of this hypothetical "containing object" is always the same as the
17153    declared type of the individual bit-field itself (for GCC anyway... the
17154    DWARF spec doesn't actually mandate this).  Note that it is the size (in
17155    bytes) of the hypothetical "containing object" which will be given in the
17156    DW_AT_byte_size attribute for this bit-field.  (See the
17157    `byte_size_attribute' function below.)  It is also used when calculating the
17158    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
17159    function below.)  */
17160
17161 static void
17162 add_data_member_location_attribute (dw_die_ref die, tree decl)
17163 {
17164   HOST_WIDE_INT offset;
17165   dw_loc_descr_ref loc_descr = 0;
17166
17167   if (TREE_CODE (decl) == TREE_BINFO)
17168     {
17169       /* We're working on the TAG_inheritance for a base class.  */
17170       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
17171         {
17172           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
17173              aren't at a fixed offset from all (sub)objects of the same
17174              type.  We need to extract the appropriate offset from our
17175              vtable.  The following dwarf expression means
17176
17177                BaseAddr = ObAddr + *((*ObAddr) - Offset)
17178
17179              This is specific to the V3 ABI, of course.  */
17180
17181           dw_loc_descr_ref tmp;
17182
17183           /* Make a copy of the object address.  */
17184           tmp = new_loc_descr (DW_OP_dup, 0, 0);
17185           add_loc_descr (&loc_descr, tmp);
17186
17187           /* Extract the vtable address.  */
17188           tmp = new_loc_descr (DW_OP_deref, 0, 0);
17189           add_loc_descr (&loc_descr, tmp);
17190
17191           /* Calculate the address of the offset.  */
17192           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
17193           gcc_assert (offset < 0);
17194
17195           tmp = int_loc_descriptor (-offset);
17196           add_loc_descr (&loc_descr, tmp);
17197           tmp = new_loc_descr (DW_OP_minus, 0, 0);
17198           add_loc_descr (&loc_descr, tmp);
17199
17200           /* Extract the offset.  */
17201           tmp = new_loc_descr (DW_OP_deref, 0, 0);
17202           add_loc_descr (&loc_descr, tmp);
17203
17204           /* Add it to the object address.  */
17205           tmp = new_loc_descr (DW_OP_plus, 0, 0);
17206           add_loc_descr (&loc_descr, tmp);
17207         }
17208       else
17209         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
17210     }
17211   else
17212     offset = field_byte_offset (decl);
17213
17214   if (! loc_descr)
17215     {
17216       if (dwarf_version > 2)
17217         {
17218           /* Don't need to output a location expression, just the constant. */
17219           if (offset < 0)
17220             add_AT_int (die, DW_AT_data_member_location, offset);
17221           else
17222             add_AT_unsigned (die, DW_AT_data_member_location, offset);
17223           return;
17224         }
17225       else
17226         {
17227           enum dwarf_location_atom op;
17228
17229           /* The DWARF2 standard says that we should assume that the structure
17230              address is already on the stack, so we can specify a structure
17231              field address by using DW_OP_plus_uconst.  */
17232
17233 #ifdef MIPS_DEBUGGING_INFO
17234           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
17235              operator correctly.  It works only if we leave the offset on the
17236              stack.  */
17237           op = DW_OP_constu;
17238 #else
17239           op = DW_OP_plus_uconst;
17240 #endif
17241
17242           loc_descr = new_loc_descr (op, offset, 0);
17243         }
17244     }
17245
17246   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
17247 }
17248
17249 /* Writes integer values to dw_vec_const array.  */
17250
17251 static void
17252 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
17253 {
17254   while (size != 0)
17255     {
17256       *dest++ = val & 0xff;
17257       val >>= 8;
17258       --size;
17259     }
17260 }
17261
17262 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
17263
17264 static HOST_WIDE_INT
17265 extract_int (const unsigned char *src, unsigned int size)
17266 {
17267   HOST_WIDE_INT val = 0;
17268
17269   src += size;
17270   while (size != 0)
17271     {
17272       val <<= 8;
17273       val |= *--src & 0xff;
17274       --size;
17275     }
17276   return val;
17277 }
17278
17279 /* Writes double_int values to dw_vec_const array.  */
17280
17281 static void
17282 insert_double (double_int val, unsigned char *dest)
17283 {
17284   unsigned char *p0 = dest;
17285   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
17286
17287   if (WORDS_BIG_ENDIAN)
17288     {
17289       p0 = p1;
17290       p1 = dest;
17291     }
17292
17293   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
17294   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
17295 }
17296
17297 /* Writes floating point values to dw_vec_const array.  */
17298
17299 static void
17300 insert_float (const_rtx rtl, unsigned char *array)
17301 {
17302   REAL_VALUE_TYPE rv;
17303   long val[4];
17304   int i;
17305
17306   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
17307   real_to_target (val, &rv, GET_MODE (rtl));
17308
17309   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
17310   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17311     {
17312       insert_int (val[i], 4, array);
17313       array += 4;
17314     }
17315 }
17316
17317 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17318    does not have a "location" either in memory or in a register.  These
17319    things can arise in GNU C when a constant is passed as an actual parameter
17320    to an inlined function.  They can also arise in C++ where declared
17321    constants do not necessarily get memory "homes".  */
17322
17323 static bool
17324 add_const_value_attribute (dw_die_ref die, rtx rtl)
17325 {
17326   switch (GET_CODE (rtl))
17327     {
17328     case CONST_INT:
17329       {
17330         HOST_WIDE_INT val = INTVAL (rtl);
17331
17332         if (val < 0)
17333           add_AT_int (die, DW_AT_const_value, val);
17334         else
17335           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17336       }
17337       return true;
17338
17339     case CONST_DOUBLE:
17340       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17341          floating-point constant.  A CONST_DOUBLE is used whenever the
17342          constant requires more than one word in order to be adequately
17343          represented.  */
17344       {
17345         enum machine_mode mode = GET_MODE (rtl);
17346
17347         if (SCALAR_FLOAT_MODE_P (mode))
17348           {
17349             unsigned int length = GET_MODE_SIZE (mode);
17350             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
17351
17352             insert_float (rtl, array);
17353             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17354           }
17355         else
17356           add_AT_double (die, DW_AT_const_value,
17357                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17358       }
17359       return true;
17360
17361     case CONST_VECTOR:
17362       {
17363         enum machine_mode mode = GET_MODE (rtl);
17364         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17365         unsigned int length = CONST_VECTOR_NUNITS (rtl);
17366         unsigned char *array = (unsigned char *) ggc_alloc_atomic
17367           (length * elt_size);
17368         unsigned int i;
17369         unsigned char *p;
17370
17371         switch (GET_MODE_CLASS (mode))
17372           {
17373           case MODE_VECTOR_INT:
17374             for (i = 0, p = array; i < length; i++, p += elt_size)
17375               {
17376                 rtx elt = CONST_VECTOR_ELT (rtl, i);
17377                 double_int val = rtx_to_double_int (elt);
17378
17379                 if (elt_size <= sizeof (HOST_WIDE_INT))
17380                   insert_int (double_int_to_shwi (val), elt_size, p);
17381                 else
17382                   {
17383                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
17384                     insert_double (val, p);
17385                   }
17386               }
17387             break;
17388
17389           case MODE_VECTOR_FLOAT:
17390             for (i = 0, p = array; i < length; i++, p += elt_size)
17391               {
17392                 rtx elt = CONST_VECTOR_ELT (rtl, i);
17393                 insert_float (elt, p);
17394               }
17395             break;
17396
17397           default:
17398             gcc_unreachable ();
17399           }
17400
17401         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17402       }
17403       return true;
17404
17405     case CONST_STRING:
17406       if (dwarf_version >= 4 || !dwarf_strict)
17407         {
17408           dw_loc_descr_ref loc_result;
17409           resolve_one_addr (&rtl, NULL);
17410         rtl_addr:
17411           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
17412           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
17413           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
17414           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17415           add_AT_loc (die, DW_AT_location, loc_result);
17416           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
17417           return true;
17418         }
17419       return false;
17420
17421     case CONST:
17422       if (CONSTANT_P (XEXP (rtl, 0)))
17423         return add_const_value_attribute (die, XEXP (rtl, 0));
17424       /* FALLTHROUGH */
17425     case SYMBOL_REF:
17426       if (!const_ok_for_output (rtl))
17427         return false;
17428     case LABEL_REF:
17429       if (dwarf_version >= 4 || !dwarf_strict)
17430         goto rtl_addr;
17431       return false;
17432
17433     case PLUS:
17434       /* In cases where an inlined instance of an inline function is passed
17435          the address of an `auto' variable (which is local to the caller) we
17436          can get a situation where the DECL_RTL of the artificial local
17437          variable (for the inlining) which acts as a stand-in for the
17438          corresponding formal parameter (of the inline function) will look
17439          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
17440          exactly a compile-time constant expression, but it isn't the address
17441          of the (artificial) local variable either.  Rather, it represents the
17442          *value* which the artificial local variable always has during its
17443          lifetime.  We currently have no way to represent such quasi-constant
17444          values in Dwarf, so for now we just punt and generate nothing.  */
17445       return false;
17446
17447     case HIGH:
17448     case CONST_FIXED:
17449       return false;
17450
17451     case MEM:
17452       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17453           && MEM_READONLY_P (rtl)
17454           && GET_MODE (rtl) == BLKmode)
17455         {
17456           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17457           return true;
17458         }
17459       return false;
17460
17461     default:
17462       /* No other kinds of rtx should be possible here.  */
17463       gcc_unreachable ();
17464     }
17465   return false;
17466 }
17467
17468 /* Determine whether the evaluation of EXPR references any variables
17469    or functions which aren't otherwise used (and therefore may not be
17470    output).  */
17471 static tree
17472 reference_to_unused (tree * tp, int * walk_subtrees,
17473                      void * data ATTRIBUTE_UNUSED)
17474 {
17475   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17476     *walk_subtrees = 0;
17477
17478   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17479       && ! TREE_ASM_WRITTEN (*tp))
17480     return *tp;
17481   /* ???  The C++ FE emits debug information for using decls, so
17482      putting gcc_unreachable here falls over.  See PR31899.  For now
17483      be conservative.  */
17484   else if (!cgraph_global_info_ready
17485            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17486     return *tp;
17487   else if (TREE_CODE (*tp) == VAR_DECL)
17488     {
17489       struct varpool_node *node = varpool_get_node (*tp);
17490       if (!node || !node->needed)
17491         return *tp;
17492     }
17493   else if (TREE_CODE (*tp) == FUNCTION_DECL
17494            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17495     {
17496       /* The call graph machinery must have finished analyzing,
17497          optimizing and gimplifying the CU by now.
17498          So if *TP has no call graph node associated
17499          to it, it means *TP will not be emitted.  */
17500       if (!cgraph_get_node (*tp))
17501         return *tp;
17502     }
17503   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17504     return *tp;
17505
17506   return NULL_TREE;
17507 }
17508
17509 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17510    for use in a later add_const_value_attribute call.  */
17511
17512 static rtx
17513 rtl_for_decl_init (tree init, tree type)
17514 {
17515   rtx rtl = NULL_RTX;
17516
17517   STRIP_NOPS (init);
17518
17519   /* If a variable is initialized with a string constant without embedded
17520      zeros, build CONST_STRING.  */
17521   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17522     {
17523       tree enttype = TREE_TYPE (type);
17524       tree domain = TYPE_DOMAIN (type);
17525       enum machine_mode mode = TYPE_MODE (enttype);
17526
17527       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17528           && domain
17529           && integer_zerop (TYPE_MIN_VALUE (domain))
17530           && compare_tree_int (TYPE_MAX_VALUE (domain),
17531                                TREE_STRING_LENGTH (init) - 1) == 0
17532           && ((size_t) TREE_STRING_LENGTH (init)
17533               == strlen (TREE_STRING_POINTER (init)) + 1))
17534         {
17535           rtl = gen_rtx_CONST_STRING (VOIDmode,
17536                                       ggc_strdup (TREE_STRING_POINTER (init)));
17537           rtl = gen_rtx_MEM (BLKmode, rtl);
17538           MEM_READONLY_P (rtl) = 1;
17539         }
17540     }
17541   /* Other aggregates, and complex values, could be represented using
17542      CONCAT: FIXME!  */
17543   else if (AGGREGATE_TYPE_P (type)
17544            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17545                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17546            || TREE_CODE (type) == COMPLEX_TYPE)
17547     ;
17548   /* Vectors only work if their mode is supported by the target.
17549      FIXME: generic vectors ought to work too.  */
17550   else if (TREE_CODE (type) == VECTOR_TYPE
17551            && !VECTOR_MODE_P (TYPE_MODE (type)))
17552     ;
17553   /* If the initializer is something that we know will expand into an
17554      immediate RTL constant, expand it now.  We must be careful not to
17555      reference variables which won't be output.  */
17556   else if (initializer_constant_valid_p (init, type)
17557            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17558     {
17559       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17560          possible.  */
17561       if (TREE_CODE (type) == VECTOR_TYPE)
17562         switch (TREE_CODE (init))
17563           {
17564           case VECTOR_CST:
17565             break;
17566           case CONSTRUCTOR:
17567             if (TREE_CONSTANT (init))
17568               {
17569                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
17570                 bool constant_p = true;
17571                 tree value;
17572                 unsigned HOST_WIDE_INT ix;
17573
17574                 /* Even when ctor is constant, it might contain non-*_CST
17575                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17576                    belong into VECTOR_CST nodes.  */
17577                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17578                   if (!CONSTANT_CLASS_P (value))
17579                     {
17580                       constant_p = false;
17581                       break;
17582                     }
17583
17584                 if (constant_p)
17585                   {
17586                     init = build_vector_from_ctor (type, elts);
17587                     break;
17588                   }
17589               }
17590             /* FALLTHRU */
17591
17592           default:
17593             return NULL;
17594           }
17595
17596       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17597
17598       /* If expand_expr returns a MEM, it wasn't immediate.  */
17599       gcc_assert (!rtl || !MEM_P (rtl));
17600     }
17601
17602   return rtl;
17603 }
17604
17605 /* Generate RTL for the variable DECL to represent its location.  */
17606
17607 static rtx
17608 rtl_for_decl_location (tree decl)
17609 {
17610   rtx rtl;
17611
17612   /* Here we have to decide where we are going to say the parameter "lives"
17613      (as far as the debugger is concerned).  We only have a couple of
17614      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17615
17616      DECL_RTL normally indicates where the parameter lives during most of the
17617      activation of the function.  If optimization is enabled however, this
17618      could be either NULL or else a pseudo-reg.  Both of those cases indicate
17619      that the parameter doesn't really live anywhere (as far as the code
17620      generation parts of GCC are concerned) during most of the function's
17621      activation.  That will happen (for example) if the parameter is never
17622      referenced within the function.
17623
17624      We could just generate a location descriptor here for all non-NULL
17625      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17626      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17627      where DECL_RTL is NULL or is a pseudo-reg.
17628
17629      Note however that we can only get away with using DECL_INCOMING_RTL as
17630      a backup substitute for DECL_RTL in certain limited cases.  In cases
17631      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17632      we can be sure that the parameter was passed using the same type as it is
17633      declared to have within the function, and that its DECL_INCOMING_RTL
17634      points us to a place where a value of that type is passed.
17635
17636      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17637      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17638      because in these cases DECL_INCOMING_RTL points us to a value of some
17639      type which is *different* from the type of the parameter itself.  Thus,
17640      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17641      such cases, the debugger would end up (for example) trying to fetch a
17642      `float' from a place which actually contains the first part of a
17643      `double'.  That would lead to really incorrect and confusing
17644      output at debug-time.
17645
17646      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17647      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
17648      are a couple of exceptions however.  On little-endian machines we can
17649      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17650      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17651      an integral type that is smaller than TREE_TYPE (decl). These cases arise
17652      when (on a little-endian machine) a non-prototyped function has a
17653      parameter declared to be of type `short' or `char'.  In such cases,
17654      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17655      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17656      passed `int' value.  If the debugger then uses that address to fetch
17657      a `short' or a `char' (on a little-endian machine) the result will be
17658      the correct data, so we allow for such exceptional cases below.
17659
17660      Note that our goal here is to describe the place where the given formal
17661      parameter lives during most of the function's activation (i.e. between the
17662      end of the prologue and the start of the epilogue).  We'll do that as best
17663      as we can. Note however that if the given formal parameter is modified
17664      sometime during the execution of the function, then a stack backtrace (at
17665      debug-time) will show the function as having been called with the *new*
17666      value rather than the value which was originally passed in.  This happens
17667      rarely enough that it is not a major problem, but it *is* a problem, and
17668      I'd like to fix it.
17669
17670      A future version of dwarf2out.c may generate two additional attributes for
17671      any given DW_TAG_formal_parameter DIE which will describe the "passed
17672      type" and the "passed location" for the given formal parameter in addition
17673      to the attributes we now generate to indicate the "declared type" and the
17674      "active location" for each parameter.  This additional set of attributes
17675      could be used by debuggers for stack backtraces. Separately, note that
17676      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17677      This happens (for example) for inlined-instances of inline function formal
17678      parameters which are never referenced.  This really shouldn't be
17679      happening.  All PARM_DECL nodes should get valid non-NULL
17680      DECL_INCOMING_RTL values.  FIXME.  */
17681
17682   /* Use DECL_RTL as the "location" unless we find something better.  */
17683   rtl = DECL_RTL_IF_SET (decl);
17684
17685   /* When generating abstract instances, ignore everything except
17686      constants, symbols living in memory, and symbols living in
17687      fixed registers.  */
17688   if (! reload_completed)
17689     {
17690       if (rtl
17691           && (CONSTANT_P (rtl)
17692               || (MEM_P (rtl)
17693                   && CONSTANT_P (XEXP (rtl, 0)))
17694               || (REG_P (rtl)
17695                   && TREE_CODE (decl) == VAR_DECL
17696                   && TREE_STATIC (decl))))
17697         {
17698           rtl = targetm.delegitimize_address (rtl);
17699           return rtl;
17700         }
17701       rtl = NULL_RTX;
17702     }
17703   else if (TREE_CODE (decl) == PARM_DECL)
17704     {
17705       if (rtl == NULL_RTX
17706           || is_pseudo_reg (rtl)
17707           || (MEM_P (rtl)
17708               && is_pseudo_reg (XEXP (rtl, 0))
17709               && DECL_INCOMING_RTL (decl)
17710               && MEM_P (DECL_INCOMING_RTL (decl))
17711               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17712         {
17713           tree declared_type = TREE_TYPE (decl);
17714           tree passed_type = DECL_ARG_TYPE (decl);
17715           enum machine_mode dmode = TYPE_MODE (declared_type);
17716           enum machine_mode pmode = TYPE_MODE (passed_type);
17717
17718           /* This decl represents a formal parameter which was optimized out.
17719              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17720              all cases where (rtl == NULL_RTX) just below.  */
17721           if (dmode == pmode)
17722             rtl = DECL_INCOMING_RTL (decl);
17723           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17724                    && SCALAR_INT_MODE_P (dmode)
17725                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17726                    && DECL_INCOMING_RTL (decl))
17727             {
17728               rtx inc = DECL_INCOMING_RTL (decl);
17729               if (REG_P (inc))
17730                 rtl = inc;
17731               else if (MEM_P (inc))
17732                 {
17733                   if (BYTES_BIG_ENDIAN)
17734                     rtl = adjust_address_nv (inc, dmode,
17735                                              GET_MODE_SIZE (pmode)
17736                                              - GET_MODE_SIZE (dmode));
17737                   else
17738                     rtl = inc;
17739                 }
17740             }
17741         }
17742
17743       /* If the parm was passed in registers, but lives on the stack, then
17744          make a big endian correction if the mode of the type of the
17745          parameter is not the same as the mode of the rtl.  */
17746       /* ??? This is the same series of checks that are made in dbxout.c before
17747          we reach the big endian correction code there.  It isn't clear if all
17748          of these checks are necessary here, but keeping them all is the safe
17749          thing to do.  */
17750       else if (MEM_P (rtl)
17751                && XEXP (rtl, 0) != const0_rtx
17752                && ! CONSTANT_P (XEXP (rtl, 0))
17753                /* Not passed in memory.  */
17754                && !MEM_P (DECL_INCOMING_RTL (decl))
17755                /* Not passed by invisible reference.  */
17756                && (!REG_P (XEXP (rtl, 0))
17757                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17758                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17759 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17760                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17761 #endif
17762                      )
17763                /* Big endian correction check.  */
17764                && BYTES_BIG_ENDIAN
17765                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17766                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17767                    < UNITS_PER_WORD))
17768         {
17769           int offset = (UNITS_PER_WORD
17770                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17771
17772           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17773                              plus_constant (XEXP (rtl, 0), offset));
17774         }
17775     }
17776   else if (TREE_CODE (decl) == VAR_DECL
17777            && rtl
17778            && MEM_P (rtl)
17779            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17780            && BYTES_BIG_ENDIAN)
17781     {
17782       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17783       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17784
17785       /* If a variable is declared "register" yet is smaller than
17786          a register, then if we store the variable to memory, it
17787          looks like we're storing a register-sized value, when in
17788          fact we are not.  We need to adjust the offset of the
17789          storage location to reflect the actual value's bytes,
17790          else gdb will not be able to display it.  */
17791       if (rsize > dsize)
17792         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17793                            plus_constant (XEXP (rtl, 0), rsize-dsize));
17794     }
17795
17796   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17797      and will have been substituted directly into all expressions that use it.
17798      C does not have such a concept, but C++ and other languages do.  */
17799   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17800     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17801
17802   if (rtl)
17803     rtl = targetm.delegitimize_address (rtl);
17804
17805   /* If we don't look past the constant pool, we risk emitting a
17806      reference to a constant pool entry that isn't referenced from
17807      code, and thus is not emitted.  */
17808   if (rtl)
17809     rtl = avoid_constant_pool_reference (rtl);
17810
17811   /* Try harder to get a rtl.  If this symbol ends up not being emitted
17812      in the current CU, resolve_addr will remove the expression referencing
17813      it.  */
17814   if (rtl == NULL_RTX
17815       && TREE_CODE (decl) == VAR_DECL
17816       && !DECL_EXTERNAL (decl)
17817       && TREE_STATIC (decl)
17818       && DECL_NAME (decl)
17819       && !DECL_HARD_REGISTER (decl)
17820       && DECL_MODE (decl) != VOIDmode)
17821     {
17822       rtl = make_decl_rtl_for_debug (decl);
17823       if (!MEM_P (rtl)
17824           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17825           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17826         rtl = NULL_RTX;
17827     }
17828
17829   return rtl;
17830 }
17831
17832 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17833    returned.  If so, the decl for the COMMON block is returned, and the
17834    value is the offset into the common block for the symbol.  */
17835
17836 static tree
17837 fortran_common (tree decl, HOST_WIDE_INT *value)
17838 {
17839   tree val_expr, cvar;
17840   enum machine_mode mode;
17841   HOST_WIDE_INT bitsize, bitpos;
17842   tree offset;
17843   int volatilep = 0, unsignedp = 0;
17844
17845   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17846      it does not have a value (the offset into the common area), or if it
17847      is thread local (as opposed to global) then it isn't common, and shouldn't
17848      be handled as such.  */
17849   if (TREE_CODE (decl) != VAR_DECL
17850       || !TREE_STATIC (decl)
17851       || !DECL_HAS_VALUE_EXPR_P (decl)
17852       || !is_fortran ())
17853     return NULL_TREE;
17854
17855   val_expr = DECL_VALUE_EXPR (decl);
17856   if (TREE_CODE (val_expr) != COMPONENT_REF)
17857     return NULL_TREE;
17858
17859   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
17860                               &mode, &unsignedp, &volatilep, true);
17861
17862   if (cvar == NULL_TREE
17863       || TREE_CODE (cvar) != VAR_DECL
17864       || DECL_ARTIFICIAL (cvar)
17865       || !TREE_PUBLIC (cvar))
17866     return NULL_TREE;
17867
17868   *value = 0;
17869   if (offset != NULL)
17870     {
17871       if (!host_integerp (offset, 0))
17872         return NULL_TREE;
17873       *value = tree_low_cst (offset, 0);
17874     }
17875   if (bitpos != 0)
17876     *value += bitpos / BITS_PER_UNIT;
17877
17878   return cvar;
17879 }
17880
17881 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17882    data attribute for a variable or a parameter.  We generate the
17883    DW_AT_const_value attribute only in those cases where the given variable
17884    or parameter does not have a true "location" either in memory or in a
17885    register.  This can happen (for example) when a constant is passed as an
17886    actual argument in a call to an inline function.  (It's possible that
17887    these things can crop up in other ways also.)  Note that one type of
17888    constant value which can be passed into an inlined function is a constant
17889    pointer.  This can happen for example if an actual argument in an inlined
17890    function call evaluates to a compile-time constant address.
17891
17892    CACHE_P is true if it is worth caching the location list for DECL,
17893    so that future calls can reuse it rather than regenerate it from scratch.
17894    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17895    since we will need to refer to them each time the function is inlined.  */
17896
17897 static bool
17898 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
17899                                        enum dwarf_attribute attr)
17900 {
17901   rtx rtl;
17902   dw_loc_list_ref list;
17903   var_loc_list *loc_list;
17904   cached_dw_loc_list *cache;
17905   void **slot;
17906
17907   if (TREE_CODE (decl) == ERROR_MARK)
17908     return false;
17909
17910   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17911               || TREE_CODE (decl) == RESULT_DECL);
17912
17913   /* Try to get some constant RTL for this decl, and use that as the value of
17914      the location.  */
17915
17916   rtl = rtl_for_decl_location (decl);
17917   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17918       && add_const_value_attribute (die, rtl))
17919     return true;
17920
17921   /* See if we have single element location list that is equivalent to
17922      a constant value.  That way we are better to use add_const_value_attribute
17923      rather than expanding constant value equivalent.  */
17924   loc_list = lookup_decl_loc (decl);
17925   if (loc_list
17926       && loc_list->first
17927       && loc_list->first->next == NULL
17928       && NOTE_P (loc_list->first->loc)
17929       && NOTE_VAR_LOCATION (loc_list->first->loc)
17930       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17931     {
17932       struct var_loc_node *node;
17933
17934       node = loc_list->first;
17935       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17936       if (GET_CODE (rtl) == EXPR_LIST)
17937         rtl = XEXP (rtl, 0);
17938       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17939           && add_const_value_attribute (die, rtl))
17940          return true;
17941     }
17942   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17943      list several times.  See if we've already cached the contents.  */
17944   list = NULL;
17945   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17946     cache_p = false;
17947   if (cache_p)
17948     {
17949       cache = (cached_dw_loc_list *)
17950         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
17951       if (cache)
17952         list = cache->loc_list;
17953     }
17954   if (list == NULL)
17955     {
17956       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
17957       /* It is usually worth caching this result if the decl is from
17958          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
17959       if (cache_p && list && list->dw_loc_next)
17960         {
17961           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
17962                                            DECL_UID (decl), INSERT);
17963           cache = ggc_alloc_cleared_cached_dw_loc_list ();
17964           cache->decl_id = DECL_UID (decl);
17965           cache->loc_list = list;
17966           *slot = cache;
17967         }
17968     }
17969   if (list)
17970     {
17971       add_AT_location_description (die, attr, list);
17972       return true;
17973     }
17974   /* None of that worked, so it must not really have a location;
17975      try adding a constant value attribute from the DECL_INITIAL.  */
17976   return tree_add_const_value_attribute_for_decl (die, decl);
17977 }
17978
17979 /* Add VARIABLE and DIE into deferred locations list.  */
17980
17981 static void
17982 defer_location (tree variable, dw_die_ref die)
17983 {
17984   deferred_locations entry;
17985   entry.variable = variable;
17986   entry.die = die;
17987   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
17988 }
17989
17990 /* Helper function for tree_add_const_value_attribute.  Natively encode
17991    initializer INIT into an array.  Return true if successful.  */
17992
17993 static bool
17994 native_encode_initializer (tree init, unsigned char *array, int size)
17995 {
17996   tree type;
17997
17998   if (init == NULL_TREE)
17999     return false;
18000
18001   STRIP_NOPS (init);
18002   switch (TREE_CODE (init))
18003     {
18004     case STRING_CST:
18005       type = TREE_TYPE (init);
18006       if (TREE_CODE (type) == ARRAY_TYPE)
18007         {
18008           tree enttype = TREE_TYPE (type);
18009           enum machine_mode mode = TYPE_MODE (enttype);
18010
18011           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
18012             return false;
18013           if (int_size_in_bytes (type) != size)
18014             return false;
18015           if (size > TREE_STRING_LENGTH (init))
18016             {
18017               memcpy (array, TREE_STRING_POINTER (init),
18018                       TREE_STRING_LENGTH (init));
18019               memset (array + TREE_STRING_LENGTH (init),
18020                       '\0', size - TREE_STRING_LENGTH (init));
18021             }
18022           else
18023             memcpy (array, TREE_STRING_POINTER (init), size);
18024           return true;
18025         }
18026       return false;
18027     case CONSTRUCTOR:
18028       type = TREE_TYPE (init);
18029       if (int_size_in_bytes (type) != size)
18030         return false;
18031       if (TREE_CODE (type) == ARRAY_TYPE)
18032         {
18033           HOST_WIDE_INT min_index;
18034           unsigned HOST_WIDE_INT cnt;
18035           int curpos = 0, fieldsize;
18036           constructor_elt *ce;
18037
18038           if (TYPE_DOMAIN (type) == NULL_TREE
18039               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
18040             return false;
18041
18042           fieldsize = int_size_in_bytes (TREE_TYPE (type));
18043           if (fieldsize <= 0)
18044             return false;
18045
18046           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
18047           memset (array, '\0', size);
18048           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
18049             {
18050               tree val = ce->value;
18051               tree index = ce->index;
18052               int pos = curpos;
18053               if (index && TREE_CODE (index) == RANGE_EXPR)
18054                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
18055                       * fieldsize;
18056               else if (index)
18057                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
18058
18059               if (val)
18060                 {
18061                   STRIP_NOPS (val);
18062                   if (!native_encode_initializer (val, array + pos, fieldsize))
18063                     return false;
18064                 }
18065               curpos = pos + fieldsize;
18066               if (index && TREE_CODE (index) == RANGE_EXPR)
18067                 {
18068                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
18069                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
18070                   while (count-- > 0)
18071                     {
18072                       if (val)
18073                         memcpy (array + curpos, array + pos, fieldsize);
18074                       curpos += fieldsize;
18075                     }
18076                 }
18077               gcc_assert (curpos <= size);
18078             }
18079           return true;
18080         }
18081       else if (TREE_CODE (type) == RECORD_TYPE
18082                || TREE_CODE (type) == UNION_TYPE)
18083         {
18084           tree field = NULL_TREE;
18085           unsigned HOST_WIDE_INT cnt;
18086           constructor_elt *ce;
18087
18088           if (int_size_in_bytes (type) != size)
18089             return false;
18090
18091           if (TREE_CODE (type) == RECORD_TYPE)
18092             field = TYPE_FIELDS (type);
18093
18094           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
18095             {
18096               tree val = ce->value;
18097               int pos, fieldsize;
18098
18099               if (ce->index != 0)
18100                 field = ce->index;
18101
18102               if (val)
18103                 STRIP_NOPS (val);
18104
18105               if (field == NULL_TREE || DECL_BIT_FIELD (field))
18106                 return false;
18107
18108               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
18109                   && TYPE_DOMAIN (TREE_TYPE (field))
18110                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
18111                 return false;
18112               else if (DECL_SIZE_UNIT (field) == NULL_TREE
18113                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
18114                 return false;
18115               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
18116               pos = int_byte_position (field);
18117               gcc_assert (pos + fieldsize <= size);
18118               if (val
18119                   && !native_encode_initializer (val, array + pos, fieldsize))
18120                 return false;
18121             }
18122           return true;
18123         }
18124       return false;
18125     case VIEW_CONVERT_EXPR:
18126     case NON_LVALUE_EXPR:
18127       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
18128     default:
18129       return native_encode_expr (init, array, size) == size;
18130     }
18131 }
18132
18133 /* Attach a DW_AT_const_value attribute to DIE. The value of the
18134    attribute is the const value T.  */
18135
18136 static bool
18137 tree_add_const_value_attribute (dw_die_ref die, tree t)
18138 {
18139   tree init;
18140   tree type = TREE_TYPE (t);
18141   rtx rtl;
18142
18143   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
18144     return false;
18145
18146   init = t;
18147   gcc_assert (!DECL_P (init));
18148
18149   rtl = rtl_for_decl_init (init, type);
18150   if (rtl)
18151     return add_const_value_attribute (die, rtl);
18152   /* If the host and target are sane, try harder.  */
18153   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
18154            && initializer_constant_valid_p (init, type))
18155     {
18156       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
18157       if (size > 0 && (int) size == size)
18158         {
18159           unsigned char *array = (unsigned char *)
18160             ggc_alloc_cleared_atomic (size);
18161
18162           if (native_encode_initializer (init, array, size))
18163             {
18164               add_AT_vec (die, DW_AT_const_value, size, 1, array);
18165               return true;
18166             }
18167         }
18168     }
18169   return false;
18170 }
18171
18172 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
18173    attribute is the const value of T, where T is an integral constant
18174    variable with static storage duration
18175    (so it can't be a PARM_DECL or a RESULT_DECL).  */
18176
18177 static bool
18178 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
18179 {
18180
18181   if (!decl
18182       || (TREE_CODE (decl) != VAR_DECL
18183           && TREE_CODE (decl) != CONST_DECL)
18184       || (TREE_CODE (decl) == VAR_DECL
18185           && !TREE_STATIC (decl)))
18186     return false;
18187
18188     if (TREE_READONLY (decl)
18189         && ! TREE_THIS_VOLATILE (decl)
18190         && DECL_INITIAL (decl))
18191       /* OK */;
18192     else
18193       return false;
18194
18195   /* Don't add DW_AT_const_value if abstract origin already has one.  */
18196   if (get_AT (var_die, DW_AT_const_value))
18197     return false;
18198
18199   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
18200 }
18201
18202 /* Convert the CFI instructions for the current function into a
18203    location list.  This is used for DW_AT_frame_base when we targeting
18204    a dwarf2 consumer that does not support the dwarf3
18205    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
18206    expressions.  */
18207
18208 static dw_loc_list_ref
18209 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
18210 {
18211   int ix;
18212   dw_fde_ref fde;
18213   dw_loc_list_ref list, *list_tail;
18214   dw_cfi_ref cfi;
18215   dw_cfa_location last_cfa, next_cfa;
18216   const char *start_label, *last_label, *section;
18217   dw_cfa_location remember;
18218
18219   fde = current_fde ();
18220   gcc_assert (fde != NULL);
18221
18222   section = secname_for_decl (current_function_decl);
18223   list_tail = &list;
18224   list = NULL;
18225
18226   memset (&next_cfa, 0, sizeof (next_cfa));
18227   next_cfa.reg = INVALID_REGNUM;
18228   remember = next_cfa;
18229
18230   start_label = fde->dw_fde_begin;
18231
18232   /* ??? Bald assumption that the CIE opcode list does not contain
18233      advance opcodes.  */
18234   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
18235     lookup_cfa_1 (cfi, &next_cfa, &remember);
18236
18237   last_cfa = next_cfa;
18238   last_label = start_label;
18239
18240   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
18241     {
18242       /* If the first partition contained no CFI adjustments, the
18243          CIE opcodes apply to the whole first partition.  */
18244       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18245                                  fde->dw_fde_begin, fde->dw_fde_end, section);
18246       list_tail =&(*list_tail)->dw_loc_next;
18247       start_label = last_label = fde->dw_fde_second_begin;
18248     }
18249
18250   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
18251     {
18252       switch (cfi->dw_cfi_opc)
18253         {
18254         case DW_CFA_set_loc:
18255         case DW_CFA_advance_loc1:
18256         case DW_CFA_advance_loc2:
18257         case DW_CFA_advance_loc4:
18258           if (!cfa_equal_p (&last_cfa, &next_cfa))
18259             {
18260               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18261                                          start_label, last_label, section);
18262
18263               list_tail = &(*list_tail)->dw_loc_next;
18264               last_cfa = next_cfa;
18265               start_label = last_label;
18266             }
18267           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18268           break;
18269
18270         case DW_CFA_advance_loc:
18271           /* The encoding is complex enough that we should never emit this.  */
18272           gcc_unreachable ();
18273
18274         default:
18275           lookup_cfa_1 (cfi, &next_cfa, &remember);
18276           break;
18277         }
18278       if (ix + 1 == fde->dw_fde_switch_cfi_index)
18279         {
18280           if (!cfa_equal_p (&last_cfa, &next_cfa))
18281             {
18282               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18283                                          start_label, last_label, section);
18284
18285               list_tail = &(*list_tail)->dw_loc_next;
18286               last_cfa = next_cfa;
18287               start_label = last_label;
18288             }
18289           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18290                                      start_label, fde->dw_fde_end, section);
18291           list_tail = &(*list_tail)->dw_loc_next;
18292           start_label = last_label = fde->dw_fde_second_begin;
18293         }
18294     }
18295
18296   if (!cfa_equal_p (&last_cfa, &next_cfa))
18297     {
18298       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18299                                  start_label, last_label, section);
18300       list_tail = &(*list_tail)->dw_loc_next;
18301       start_label = last_label;
18302     }
18303
18304   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18305                              start_label,
18306                              fde->dw_fde_second_begin
18307                              ? fde->dw_fde_second_end : fde->dw_fde_end,
18308                              section);
18309
18310   if (list && list->dw_loc_next)
18311     gen_llsym (list);
18312
18313   return list;
18314 }
18315
18316 /* Compute a displacement from the "steady-state frame pointer" to the
18317    frame base (often the same as the CFA), and store it in
18318    frame_pointer_fb_offset.  OFFSET is added to the displacement
18319    before the latter is negated.  */
18320
18321 static void
18322 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18323 {
18324   rtx reg, elim;
18325
18326 #ifdef FRAME_POINTER_CFA_OFFSET
18327   reg = frame_pointer_rtx;
18328   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18329 #else
18330   reg = arg_pointer_rtx;
18331   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18332 #endif
18333
18334   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
18335   if (GET_CODE (elim) == PLUS)
18336     {
18337       offset += INTVAL (XEXP (elim, 1));
18338       elim = XEXP (elim, 0);
18339     }
18340
18341   frame_pointer_fb_offset = -offset;
18342
18343   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18344      in which to eliminate.  This is because it's stack pointer isn't 
18345      directly accessible as a register within the ISA.  To work around
18346      this, assume that while we cannot provide a proper value for
18347      frame_pointer_fb_offset, we won't need one either.  */
18348   frame_pointer_fb_offset_valid
18349     = ((SUPPORTS_STACK_ALIGNMENT
18350         && (elim == hard_frame_pointer_rtx
18351             || elim == stack_pointer_rtx))
18352        || elim == (frame_pointer_needed
18353                    ? hard_frame_pointer_rtx
18354                    : stack_pointer_rtx));
18355 }
18356
18357 /* Generate a DW_AT_name attribute given some string value to be included as
18358    the value of the attribute.  */
18359
18360 static void
18361 add_name_attribute (dw_die_ref die, const char *name_string)
18362 {
18363   if (name_string != NULL && *name_string != 0)
18364     {
18365       if (demangle_name_func)
18366         name_string = (*demangle_name_func) (name_string);
18367
18368       add_AT_string (die, DW_AT_name, name_string);
18369     }
18370 }
18371
18372 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18373    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18374    of TYPE accordingly.
18375
18376    ??? This is a temporary measure until after we're able to generate
18377    regular DWARF for the complex Ada type system.  */
18378
18379 static void 
18380 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18381                                      dw_die_ref context_die)
18382 {
18383   tree dtype;
18384   dw_die_ref dtype_die;
18385
18386   if (!lang_hooks.types.descriptive_type)
18387     return;
18388
18389   dtype = lang_hooks.types.descriptive_type (type);
18390   if (!dtype)
18391     return;
18392
18393   dtype_die = lookup_type_die (dtype);
18394   if (!dtype_die)
18395     {
18396       gen_type_die (dtype, context_die);
18397       dtype_die = lookup_type_die (dtype);
18398       gcc_assert (dtype_die);
18399     }
18400
18401   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18402 }
18403
18404 /* Generate a DW_AT_comp_dir attribute for DIE.  */
18405
18406 static void
18407 add_comp_dir_attribute (dw_die_ref die)
18408 {
18409   const char *wd = get_src_pwd ();
18410   char *wd1;
18411
18412   if (wd == NULL)
18413     return;
18414
18415   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18416     {
18417       int wdlen;
18418
18419       wdlen = strlen (wd);
18420       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
18421       strcpy (wd1, wd);
18422       wd1 [wdlen] = DIR_SEPARATOR;
18423       wd1 [wdlen + 1] = 0;
18424       wd = wd1;
18425     }
18426
18427     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
18428 }
18429
18430 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18431    default.  */
18432
18433 static int
18434 lower_bound_default (void)
18435 {
18436   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18437     {
18438     case DW_LANG_C:
18439     case DW_LANG_C89:
18440     case DW_LANG_C99:
18441     case DW_LANG_C_plus_plus:
18442     case DW_LANG_ObjC:
18443     case DW_LANG_ObjC_plus_plus:
18444     case DW_LANG_Java:
18445       return 0;
18446     case DW_LANG_Fortran77:
18447     case DW_LANG_Fortran90:
18448     case DW_LANG_Fortran95:
18449       return 1;
18450     case DW_LANG_UPC:
18451     case DW_LANG_D:
18452     case DW_LANG_Python:
18453       return dwarf_version >= 4 ? 0 : -1;
18454     case DW_LANG_Ada95:
18455     case DW_LANG_Ada83:
18456     case DW_LANG_Cobol74:
18457     case DW_LANG_Cobol85:
18458     case DW_LANG_Pascal83:
18459     case DW_LANG_Modula2:
18460     case DW_LANG_PLI:
18461       return dwarf_version >= 4 ? 1 : -1;
18462     default:
18463       return -1;
18464     }
18465 }
18466
18467 /* Given a tree node describing an array bound (either lower or upper) output
18468    a representation for that bound.  */
18469
18470 static void
18471 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
18472 {
18473   switch (TREE_CODE (bound))
18474     {
18475     case ERROR_MARK:
18476       return;
18477
18478     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
18479     case INTEGER_CST:
18480       {
18481         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
18482         int dflt;
18483
18484         /* Use the default if possible.  */
18485         if (bound_attr == DW_AT_lower_bound
18486             && host_integerp (bound, 0)
18487             && (dflt = lower_bound_default ()) != -1
18488             && tree_low_cst (bound, 0) == dflt)
18489           ;
18490
18491         /* Otherwise represent the bound as an unsigned value with the
18492            precision of its type.  The precision and signedness of the
18493            type will be necessary to re-interpret it unambiguously.  */
18494         else if (prec < HOST_BITS_PER_WIDE_INT)
18495           {
18496             unsigned HOST_WIDE_INT mask
18497               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
18498             add_AT_unsigned (subrange_die, bound_attr,
18499                              TREE_INT_CST_LOW (bound) & mask);
18500           }
18501         else if (prec == HOST_BITS_PER_WIDE_INT
18502                  || TREE_INT_CST_HIGH (bound) == 0)
18503           add_AT_unsigned (subrange_die, bound_attr,
18504                            TREE_INT_CST_LOW (bound));
18505         else
18506           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
18507                          TREE_INT_CST_LOW (bound));
18508       }
18509       break;
18510
18511     CASE_CONVERT:
18512     case VIEW_CONVERT_EXPR:
18513       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
18514       break;
18515
18516     case SAVE_EXPR:
18517       break;
18518
18519     case VAR_DECL:
18520     case PARM_DECL:
18521     case RESULT_DECL:
18522       {
18523         dw_die_ref decl_die = lookup_decl_die (bound);
18524
18525         /* ??? Can this happen, or should the variable have been bound
18526            first?  Probably it can, since I imagine that we try to create
18527            the types of parameters in the order in which they exist in
18528            the list, and won't have created a forward reference to a
18529            later parameter.  */
18530         if (decl_die != NULL)
18531           {
18532             add_AT_die_ref (subrange_die, bound_attr, decl_die);
18533             break;
18534           }
18535       }
18536       /* FALLTHRU */
18537
18538     default:
18539       {
18540         /* Otherwise try to create a stack operation procedure to
18541            evaluate the value of the array bound.  */
18542
18543         dw_die_ref ctx, decl_die;
18544         dw_loc_list_ref list;
18545
18546         list = loc_list_from_tree (bound, 2);
18547         if (list == NULL || single_element_loc_list_p (list))
18548           {
18549             /* If DW_AT_*bound is not a reference nor constant, it is
18550                a DWARF expression rather than location description.
18551                For that loc_list_from_tree (bound, 0) is needed.
18552                If that fails to give a single element list,
18553                fall back to outputting this as a reference anyway.  */
18554             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
18555             if (list2 && single_element_loc_list_p (list2))
18556               {
18557                 add_AT_loc (subrange_die, bound_attr, list2->expr);
18558                 break;
18559               }
18560           }
18561         if (list == NULL)
18562           break;
18563
18564         if (current_function_decl == 0)
18565           ctx = comp_unit_die ();
18566         else
18567           ctx = lookup_decl_die (current_function_decl);
18568
18569         decl_die = new_die (DW_TAG_variable, ctx, bound);
18570         add_AT_flag (decl_die, DW_AT_artificial, 1);
18571         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
18572         add_AT_location_description (decl_die, DW_AT_location, list);
18573         add_AT_die_ref (subrange_die, bound_attr, decl_die);
18574         break;
18575       }
18576     }
18577 }
18578
18579 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18580    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18581    Note that the block of subscript information for an array type also
18582    includes information about the element type of the given array type.  */
18583
18584 static void
18585 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18586 {
18587   unsigned dimension_number;
18588   tree lower, upper;
18589   dw_die_ref subrange_die;
18590
18591   for (dimension_number = 0;
18592        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18593        type = TREE_TYPE (type), dimension_number++)
18594     {
18595       tree domain = TYPE_DOMAIN (type);
18596
18597       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18598         break;
18599
18600       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18601          and (in GNU C only) variable bounds.  Handle all three forms
18602          here.  */
18603       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18604       if (domain)
18605         {
18606           /* We have an array type with specified bounds.  */
18607           lower = TYPE_MIN_VALUE (domain);
18608           upper = TYPE_MAX_VALUE (domain);
18609
18610           /* Define the index type.  */
18611           if (TREE_TYPE (domain))
18612             {
18613               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
18614                  TREE_TYPE field.  We can't emit debug info for this
18615                  because it is an unnamed integral type.  */
18616               if (TREE_CODE (domain) == INTEGER_TYPE
18617                   && TYPE_NAME (domain) == NULL_TREE
18618                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18619                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18620                 ;
18621               else
18622                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
18623                                     type_die);
18624             }
18625
18626           /* ??? If upper is NULL, the array has unspecified length,
18627              but it does have a lower bound.  This happens with Fortran
18628                dimension arr(N:*)
18629              Since the debugger is definitely going to need to know N
18630              to produce useful results, go ahead and output the lower
18631              bound solo, and hope the debugger can cope.  */
18632
18633           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
18634           if (upper)
18635             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
18636         }
18637
18638       /* Otherwise we have an array type with an unspecified length.  The
18639          DWARF-2 spec does not say how to handle this; let's just leave out the
18640          bounds.  */
18641     }
18642 }
18643
18644 static void
18645 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18646 {
18647   unsigned size;
18648
18649   switch (TREE_CODE (tree_node))
18650     {
18651     case ERROR_MARK:
18652       size = 0;
18653       break;
18654     case ENUMERAL_TYPE:
18655     case RECORD_TYPE:
18656     case UNION_TYPE:
18657     case QUAL_UNION_TYPE:
18658       size = int_size_in_bytes (tree_node);
18659       break;
18660     case FIELD_DECL:
18661       /* For a data member of a struct or union, the DW_AT_byte_size is
18662          generally given as the number of bytes normally allocated for an
18663          object of the *declared* type of the member itself.  This is true
18664          even for bit-fields.  */
18665       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
18666       break;
18667     default:
18668       gcc_unreachable ();
18669     }
18670
18671   /* Note that `size' might be -1 when we get to this point.  If it is, that
18672      indicates that the byte size of the entity in question is variable.  We
18673      have no good way of expressing this fact in Dwarf at the present time,
18674      so just let the -1 pass on through.  */
18675   add_AT_unsigned (die, DW_AT_byte_size, size);
18676 }
18677
18678 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18679    which specifies the distance in bits from the highest order bit of the
18680    "containing object" for the bit-field to the highest order bit of the
18681    bit-field itself.
18682
18683    For any given bit-field, the "containing object" is a hypothetical object
18684    (of some integral or enum type) within which the given bit-field lives.  The
18685    type of this hypothetical "containing object" is always the same as the
18686    declared type of the individual bit-field itself.  The determination of the
18687    exact location of the "containing object" for a bit-field is rather
18688    complicated.  It's handled by the `field_byte_offset' function (above).
18689
18690    Note that it is the size (in bytes) of the hypothetical "containing object"
18691    which will be given in the DW_AT_byte_size attribute for this bit-field.
18692    (See `byte_size_attribute' above).  */
18693
18694 static inline void
18695 add_bit_offset_attribute (dw_die_ref die, tree decl)
18696 {
18697   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
18698   tree type = DECL_BIT_FIELD_TYPE (decl);
18699   HOST_WIDE_INT bitpos_int;
18700   HOST_WIDE_INT highest_order_object_bit_offset;
18701   HOST_WIDE_INT highest_order_field_bit_offset;
18702   HOST_WIDE_INT bit_offset;
18703
18704   /* Must be a field and a bit field.  */
18705   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
18706
18707   /* We can't yet handle bit-fields whose offsets are variable, so if we
18708      encounter such things, just return without generating any attribute
18709      whatsoever.  Likewise for variable or too large size.  */
18710   if (! host_integerp (bit_position (decl), 0)
18711       || ! host_integerp (DECL_SIZE (decl), 1))
18712     return;
18713
18714   bitpos_int = int_bit_position (decl);
18715
18716   /* Note that the bit offset is always the distance (in bits) from the
18717      highest-order bit of the "containing object" to the highest-order bit of
18718      the bit-field itself.  Since the "high-order end" of any object or field
18719      is different on big-endian and little-endian machines, the computation
18720      below must take account of these differences.  */
18721   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18722   highest_order_field_bit_offset = bitpos_int;
18723
18724   if (! BYTES_BIG_ENDIAN)
18725     {
18726       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
18727       highest_order_object_bit_offset += simple_type_size_in_bits (type);
18728     }
18729
18730   bit_offset
18731     = (! BYTES_BIG_ENDIAN
18732        ? highest_order_object_bit_offset - highest_order_field_bit_offset
18733        : highest_order_field_bit_offset - highest_order_object_bit_offset);
18734
18735   if (bit_offset < 0)
18736     add_AT_int (die, DW_AT_bit_offset, bit_offset);
18737   else
18738     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18739 }
18740
18741 /* For a FIELD_DECL node which represents a bit field, output an attribute
18742    which specifies the length in bits of the given field.  */
18743
18744 static inline void
18745 add_bit_size_attribute (dw_die_ref die, tree decl)
18746 {
18747   /* Must be a field and a bit field.  */
18748   gcc_assert (TREE_CODE (decl) == FIELD_DECL
18749               && DECL_BIT_FIELD_TYPE (decl));
18750
18751   if (host_integerp (DECL_SIZE (decl), 1))
18752     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
18753 }
18754
18755 /* If the compiled language is ANSI C, then add a 'prototyped'
18756    attribute, if arg types are given for the parameters of a function.  */
18757
18758 static inline void
18759 add_prototyped_attribute (dw_die_ref die, tree func_type)
18760 {
18761   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
18762       && prototype_p (func_type))
18763     add_AT_flag (die, DW_AT_prototyped, 1);
18764 }
18765
18766 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
18767    by looking in either the type declaration or object declaration
18768    equate table.  */
18769
18770 static inline dw_die_ref
18771 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18772 {
18773   dw_die_ref origin_die = NULL;
18774
18775   if (TREE_CODE (origin) != FUNCTION_DECL)
18776     {
18777       /* We may have gotten separated from the block for the inlined
18778          function, if we're in an exception handler or some such; make
18779          sure that the abstract function has been written out.
18780
18781          Doing this for nested functions is wrong, however; functions are
18782          distinct units, and our context might not even be inline.  */
18783       tree fn = origin;
18784
18785       if (TYPE_P (fn))
18786         fn = TYPE_STUB_DECL (fn);
18787
18788       fn = decl_function_context (fn);
18789       if (fn)
18790         dwarf2out_abstract_function (fn);
18791     }
18792
18793   if (DECL_P (origin))
18794     origin_die = lookup_decl_die (origin);
18795   else if (TYPE_P (origin))
18796     origin_die = lookup_type_die (origin);
18797
18798   /* XXX: Functions that are never lowered don't always have correct block
18799      trees (in the case of java, they simply have no block tree, in some other
18800      languages).  For these functions, there is nothing we can really do to
18801      output correct debug info for inlined functions in all cases.  Rather
18802      than die, we'll just produce deficient debug info now, in that we will
18803      have variables without a proper abstract origin.  In the future, when all
18804      functions are lowered, we should re-add a gcc_assert (origin_die)
18805      here.  */
18806
18807   if (origin_die)
18808     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18809   return origin_die;
18810 }
18811
18812 /* We do not currently support the pure_virtual attribute.  */
18813
18814 static inline void
18815 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18816 {
18817   if (DECL_VINDEX (func_decl))
18818     {
18819       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18820
18821       if (host_integerp (DECL_VINDEX (func_decl), 0))
18822         add_AT_loc (die, DW_AT_vtable_elem_location,
18823                     new_loc_descr (DW_OP_constu,
18824                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
18825                                    0));
18826
18827       /* GNU extension: Record what type this method came from originally.  */
18828       if (debug_info_level > DINFO_LEVEL_TERSE
18829           && DECL_CONTEXT (func_decl))
18830         add_AT_die_ref (die, DW_AT_containing_type,
18831                         lookup_type_die (DECL_CONTEXT (func_decl)));
18832     }
18833 }
18834 \f
18835 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18836    given decl.  This used to be a vendor extension until after DWARF 4
18837    standardized it.  */
18838
18839 static void
18840 add_linkage_attr (dw_die_ref die, tree decl)
18841 {
18842   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18843
18844   /* Mimic what assemble_name_raw does with a leading '*'.  */
18845   if (name[0] == '*')
18846     name = &name[1];
18847
18848   if (dwarf_version >= 4)
18849     add_AT_string (die, DW_AT_linkage_name, name);
18850   else
18851     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18852 }
18853
18854 /* Add source coordinate attributes for the given decl.  */
18855
18856 static void
18857 add_src_coords_attributes (dw_die_ref die, tree decl)
18858 {
18859   expanded_location s;
18860
18861   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
18862     return;
18863   s = expand_location (DECL_SOURCE_LOCATION (decl));
18864   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18865   add_AT_unsigned (die, DW_AT_decl_line, s.line);
18866 }
18867
18868 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
18869
18870 static void
18871 add_linkage_name (dw_die_ref die, tree decl)
18872 {
18873   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18874        && TREE_PUBLIC (decl)
18875        && !DECL_ABSTRACT (decl)
18876        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18877        && die->die_tag != DW_TAG_member)
18878     {
18879       /* Defer until we have an assembler name set.  */
18880       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18881         {
18882           limbo_die_node *asm_name;
18883
18884           asm_name = ggc_alloc_cleared_limbo_die_node ();
18885           asm_name->die = die;
18886           asm_name->created_for = decl;
18887           asm_name->next = deferred_asm_name;
18888           deferred_asm_name = asm_name;
18889         }
18890       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18891         add_linkage_attr (die, decl);
18892     }
18893 }
18894
18895 /* Add a DW_AT_name attribute and source coordinate attribute for the
18896    given decl, but only if it actually has a name.  */
18897
18898 static void
18899 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18900 {
18901   tree decl_name;
18902
18903   decl_name = DECL_NAME (decl);
18904   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18905     {
18906       const char *name = dwarf2_name (decl, 0);
18907       if (name)
18908         add_name_attribute (die, name);
18909       if (! DECL_ARTIFICIAL (decl))
18910         add_src_coords_attributes (die, decl);
18911
18912       add_linkage_name (die, decl);
18913     }
18914
18915 #ifdef VMS_DEBUGGING_INFO
18916   /* Get the function's name, as described by its RTL.  This may be different
18917      from the DECL_NAME name used in the source file.  */
18918   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18919     {
18920       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18921                    XEXP (DECL_RTL (decl), 0));
18922       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
18923     }
18924 #endif /* VMS_DEBUGGING_INFO */
18925 }
18926
18927 #ifdef VMS_DEBUGGING_INFO
18928 /* Output the debug main pointer die for VMS */
18929
18930 void
18931 dwarf2out_vms_debug_main_pointer (void)
18932 {
18933   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18934   dw_die_ref die;
18935
18936   /* Allocate the VMS debug main subprogram die.  */
18937   die = ggc_alloc_cleared_die_node ();
18938   die->die_tag = DW_TAG_subprogram;
18939   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18940   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18941                                current_function_funcdef_no);
18942   add_AT_lbl_id (die, DW_AT_entry_pc, label);
18943
18944   /* Make it the first child of comp_unit_die ().  */
18945   die->die_parent = comp_unit_die ();
18946   if (comp_unit_die ()->die_child)
18947     {
18948       die->die_sib = comp_unit_die ()->die_child->die_sib;
18949       comp_unit_die ()->die_child->die_sib = die;
18950     }
18951   else
18952     {
18953       die->die_sib = die;
18954       comp_unit_die ()->die_child = die;
18955     }
18956 }
18957 #endif /* VMS_DEBUGGING_INFO */
18958
18959 /* Push a new declaration scope.  */
18960
18961 static void
18962 push_decl_scope (tree scope)
18963 {
18964   VEC_safe_push (tree, gc, decl_scope_table, scope);
18965 }
18966
18967 /* Pop a declaration scope.  */
18968
18969 static inline void
18970 pop_decl_scope (void)
18971 {
18972   VEC_pop (tree, decl_scope_table);
18973 }
18974
18975 /* Return the DIE for the scope that immediately contains this type.
18976    Non-named types get global scope.  Named types nested in other
18977    types get their containing scope if it's open, or global scope
18978    otherwise.  All other types (i.e. function-local named types) get
18979    the current active scope.  */
18980
18981 static dw_die_ref
18982 scope_die_for (tree t, dw_die_ref context_die)
18983 {
18984   dw_die_ref scope_die = NULL;
18985   tree containing_scope;
18986   int i;
18987
18988   /* Non-types always go in the current scope.  */
18989   gcc_assert (TYPE_P (t));
18990
18991   containing_scope = TYPE_CONTEXT (t);
18992
18993   /* Use the containing namespace if it was passed in (for a declaration).  */
18994   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18995     {
18996       if (context_die == lookup_decl_die (containing_scope))
18997         /* OK */;
18998       else
18999         containing_scope = NULL_TREE;
19000     }
19001
19002   /* Ignore function type "scopes" from the C frontend.  They mean that
19003      a tagged type is local to a parmlist of a function declarator, but
19004      that isn't useful to DWARF.  */
19005   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19006     containing_scope = NULL_TREE;
19007
19008   if (SCOPE_FILE_SCOPE_P (containing_scope))
19009     scope_die = comp_unit_die ();
19010   else if (TYPE_P (containing_scope))
19011     {
19012       /* For types, we can just look up the appropriate DIE.  But
19013          first we check to see if we're in the middle of emitting it
19014          so we know where the new DIE should go.  */
19015       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
19016         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
19017           break;
19018
19019       if (i < 0)
19020         {
19021           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
19022                       || TREE_ASM_WRITTEN (containing_scope));
19023           /*We are not in the middle of emitting the type
19024             CONTAINING_SCOPE. Let's see if it's emitted already.  */
19025           scope_die = lookup_type_die (containing_scope);
19026
19027           /* If none of the current dies are suitable, we get file scope.  */
19028           if (scope_die == NULL)
19029             scope_die = comp_unit_die ();
19030         }
19031       else
19032         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19033     }
19034   else
19035     scope_die = context_die;
19036
19037   return scope_die;
19038 }
19039
19040 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
19041
19042 static inline int
19043 local_scope_p (dw_die_ref context_die)
19044 {
19045   for (; context_die; context_die = context_die->die_parent)
19046     if (context_die->die_tag == DW_TAG_inlined_subroutine
19047         || context_die->die_tag == DW_TAG_subprogram)
19048       return 1;
19049
19050   return 0;
19051 }
19052
19053 /* Returns nonzero if CONTEXT_DIE is a class.  */
19054
19055 static inline int
19056 class_scope_p (dw_die_ref context_die)
19057 {
19058   return (context_die
19059           && (context_die->die_tag == DW_TAG_structure_type
19060               || context_die->die_tag == DW_TAG_class_type
19061               || context_die->die_tag == DW_TAG_interface_type
19062               || context_die->die_tag == DW_TAG_union_type));
19063 }
19064
19065 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19066    whether or not to treat a DIE in this context as a declaration.  */
19067
19068 static inline int
19069 class_or_namespace_scope_p (dw_die_ref context_die)
19070 {
19071   return (class_scope_p (context_die)
19072           || (context_die && context_die->die_tag == DW_TAG_namespace));
19073 }
19074
19075 /* Many forms of DIEs require a "type description" attribute.  This
19076    routine locates the proper "type descriptor" die for the type given
19077    by 'type', and adds a DW_AT_type attribute below the given die.  */
19078
19079 static void
19080 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
19081                     int decl_volatile, dw_die_ref context_die)
19082 {
19083   enum tree_code code  = TREE_CODE (type);
19084   dw_die_ref type_die  = NULL;
19085
19086   /* ??? If this type is an unnamed subrange type of an integral, floating-point
19087      or fixed-point type, use the inner type.  This is because we have no
19088      support for unnamed types in base_type_die.  This can happen if this is
19089      an Ada subrange type.  Correct solution is emit a subrange type die.  */
19090   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19091       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19092     type = TREE_TYPE (type), code = TREE_CODE (type);
19093
19094   if (code == ERROR_MARK
19095       /* Handle a special case.  For functions whose return type is void, we
19096          generate *no* type attribute.  (Note that no object may have type
19097          `void', so this only applies to function return types).  */
19098       || code == VOID_TYPE)
19099     return;
19100
19101   type_die = modified_type_die (type,
19102                                 decl_const || TYPE_READONLY (type),
19103                                 decl_volatile || TYPE_VOLATILE (type),
19104                                 context_die);
19105
19106   if (type_die != NULL)
19107     add_AT_die_ref (object_die, DW_AT_type, type_die);
19108 }
19109
19110 /* Given an object die, add the calling convention attribute for the
19111    function call type.  */
19112 static void
19113 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19114 {
19115   enum dwarf_calling_convention value = DW_CC_normal;
19116
19117   value = ((enum dwarf_calling_convention)
19118            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19119
19120   if (is_fortran ()
19121       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19122     {
19123       /* DWARF 2 doesn't provide a way to identify a program's source-level
19124         entry point.  DW_AT_calling_convention attributes are only meant
19125         to describe functions' calling conventions.  However, lacking a
19126         better way to signal the Fortran main program, we used this for 
19127         a long time, following existing custom.  Now, DWARF 4 has 
19128         DW_AT_main_subprogram, which we add below, but some tools still
19129         rely on the old way, which we thus keep.  */
19130       value = DW_CC_program;
19131
19132       if (dwarf_version >= 4 || !dwarf_strict)
19133         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19134     }
19135
19136   /* Only add the attribute if the backend requests it, and
19137      is not DW_CC_normal.  */
19138   if (value && (value != DW_CC_normal))
19139     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19140 }
19141
19142 /* Given a tree pointer to a struct, class, union, or enum type node, return
19143    a pointer to the (string) tag name for the given type, or zero if the type
19144    was declared without a tag.  */
19145
19146 static const char *
19147 type_tag (const_tree type)
19148 {
19149   const char *name = 0;
19150
19151   if (TYPE_NAME (type) != 0)
19152     {
19153       tree t = 0;
19154
19155       /* Find the IDENTIFIER_NODE for the type name.  */
19156       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19157           && !TYPE_NAMELESS (type))
19158         t = TYPE_NAME (type);
19159
19160       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19161          a TYPE_DECL node, regardless of whether or not a `typedef' was
19162          involved.  */
19163       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19164                && ! DECL_IGNORED_P (TYPE_NAME (type)))
19165         {
19166           /* We want to be extra verbose.  Don't call dwarf_name if
19167              DECL_NAME isn't set.  The default hook for decl_printable_name
19168              doesn't like that, and in this context it's correct to return
19169              0, instead of "<anonymous>" or the like.  */
19170           if (DECL_NAME (TYPE_NAME (type))
19171               && !DECL_NAMELESS (TYPE_NAME (type)))
19172             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19173         }
19174
19175       /* Now get the name as a string, or invent one.  */
19176       if (!name && t != 0)
19177         name = IDENTIFIER_POINTER (t);
19178     }
19179
19180   return (name == 0 || *name == '\0') ? 0 : name;
19181 }
19182
19183 /* Return the type associated with a data member, make a special check
19184    for bit field types.  */
19185
19186 static inline tree
19187 member_declared_type (const_tree member)
19188 {
19189   return (DECL_BIT_FIELD_TYPE (member)
19190           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19191 }
19192
19193 /* Get the decl's label, as described by its RTL. This may be different
19194    from the DECL_NAME name used in the source file.  */
19195
19196 #if 0
19197 static const char *
19198 decl_start_label (tree decl)
19199 {
19200   rtx x;
19201   const char *fnname;
19202
19203   x = DECL_RTL (decl);
19204   gcc_assert (MEM_P (x));
19205
19206   x = XEXP (x, 0);
19207   gcc_assert (GET_CODE (x) == SYMBOL_REF);
19208
19209   fnname = XSTR (x, 0);
19210   return fnname;
19211 }
19212 #endif
19213 \f
19214 /* These routines generate the internal representation of the DIE's for
19215    the compilation unit.  Debugging information is collected by walking
19216    the declaration trees passed in from dwarf2out_decl().  */
19217
19218 static void
19219 gen_array_type_die (tree type, dw_die_ref context_die)
19220 {
19221   dw_die_ref scope_die = scope_die_for (type, context_die);
19222   dw_die_ref array_die;
19223
19224   /* GNU compilers represent multidimensional array types as sequences of one
19225      dimensional array types whose element types are themselves array types.
19226      We sometimes squish that down to a single array_type DIE with multiple
19227      subscripts in the Dwarf debugging info.  The draft Dwarf specification
19228      say that we are allowed to do this kind of compression in C, because
19229      there is no difference between an array of arrays and a multidimensional
19230      array.  We don't do this for Ada to remain as close as possible to the
19231      actual representation, which is especially important against the language
19232      flexibilty wrt arrays of variable size.  */
19233
19234   bool collapse_nested_arrays = !is_ada ();
19235   tree element_type;
19236
19237   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19238      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
19239   if (TYPE_STRING_FLAG (type)
19240       && TREE_CODE (type) == ARRAY_TYPE
19241       && is_fortran ()
19242       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19243     {
19244       HOST_WIDE_INT size;
19245
19246       array_die = new_die (DW_TAG_string_type, scope_die, type);
19247       add_name_attribute (array_die, type_tag (type));
19248       equate_type_number_to_die (type, array_die);
19249       size = int_size_in_bytes (type);
19250       if (size >= 0)
19251         add_AT_unsigned (array_die, DW_AT_byte_size, size);
19252       else if (TYPE_DOMAIN (type) != NULL_TREE
19253                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
19254                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
19255         {
19256           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19257           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
19258
19259           size = int_size_in_bytes (TREE_TYPE (szdecl));
19260           if (loc && size > 0)
19261             {
19262               add_AT_location_description (array_die, DW_AT_string_length, loc);
19263               if (size != DWARF2_ADDR_SIZE)
19264                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19265             }
19266         }
19267       return;
19268     }
19269
19270   /* ??? The SGI dwarf reader fails for array of array of enum types
19271      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
19272      array type comes before the outer array type.  We thus call gen_type_die
19273      before we new_die and must prevent nested array types collapsing for this
19274      target.  */
19275
19276 #ifdef MIPS_DEBUGGING_INFO
19277   gen_type_die (TREE_TYPE (type), context_die);
19278   collapse_nested_arrays = false;
19279 #endif
19280
19281   array_die = new_die (DW_TAG_array_type, scope_die, type);
19282   add_name_attribute (array_die, type_tag (type));
19283   add_gnat_descriptive_type_attribute (array_die, type, context_die);
19284   equate_type_number_to_die (type, array_die);
19285
19286   if (TREE_CODE (type) == VECTOR_TYPE)
19287     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19288
19289   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
19290   if (is_fortran ()
19291       && TREE_CODE (type) == ARRAY_TYPE
19292       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19293       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19294     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19295
19296 #if 0
19297   /* We default the array ordering.  SDB will probably do
19298      the right things even if DW_AT_ordering is not present.  It's not even
19299      an issue until we start to get into multidimensional arrays anyway.  If
19300      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19301      then we'll have to put the DW_AT_ordering attribute back in.  (But if
19302      and when we find out that we need to put these in, we will only do so
19303      for multidimensional arrays.  */
19304   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19305 #endif
19306
19307 #ifdef MIPS_DEBUGGING_INFO
19308   /* The SGI compilers handle arrays of unknown bound by setting
19309      AT_declaration and not emitting any subrange DIEs.  */
19310   if (TREE_CODE (type) == ARRAY_TYPE
19311       && ! TYPE_DOMAIN (type))
19312     add_AT_flag (array_die, DW_AT_declaration, 1);
19313   else
19314 #endif
19315   if (TREE_CODE (type) == VECTOR_TYPE)
19316     {
19317       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
19318       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19319       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
19320       add_bound_info (subrange_die, DW_AT_upper_bound,
19321                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
19322     }
19323   else
19324     add_subscript_info (array_die, type, collapse_nested_arrays);
19325
19326   /* Add representation of the type of the elements of this array type and
19327      emit the corresponding DIE if we haven't done it already.  */
19328   element_type = TREE_TYPE (type);
19329   if (collapse_nested_arrays)
19330     while (TREE_CODE (element_type) == ARRAY_TYPE)
19331       {
19332         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19333           break;
19334         element_type = TREE_TYPE (element_type);
19335       }
19336
19337 #ifndef MIPS_DEBUGGING_INFO
19338   gen_type_die (element_type, context_die);
19339 #endif
19340
19341   add_type_attribute (array_die, element_type, 0, 0, context_die);
19342
19343   if (get_AT (array_die, DW_AT_name))
19344     add_pubtype (type, array_die);
19345 }
19346
19347 static dw_loc_descr_ref
19348 descr_info_loc (tree val, tree base_decl)
19349 {
19350   HOST_WIDE_INT size;
19351   dw_loc_descr_ref loc, loc2;
19352   enum dwarf_location_atom op;
19353
19354   if (val == base_decl)
19355     return new_loc_descr (DW_OP_push_object_address, 0, 0);
19356
19357   switch (TREE_CODE (val))
19358     {
19359     CASE_CONVERT:
19360       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19361     case VAR_DECL:
19362       return loc_descriptor_from_tree (val, 0);
19363     case INTEGER_CST:
19364       if (host_integerp (val, 0))
19365         return int_loc_descriptor (tree_low_cst (val, 0));
19366       break;
19367     case INDIRECT_REF:
19368       size = int_size_in_bytes (TREE_TYPE (val));
19369       if (size < 0)
19370         break;
19371       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19372       if (!loc)
19373         break;
19374       if (size == DWARF2_ADDR_SIZE)
19375         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
19376       else
19377         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
19378       return loc;
19379     case POINTER_PLUS_EXPR:
19380     case PLUS_EXPR:
19381       if (host_integerp (TREE_OPERAND (val, 1), 1)
19382           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
19383              < 16384)
19384         {
19385           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19386           if (!loc)
19387             break;
19388           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
19389         }
19390       else
19391         {
19392           op = DW_OP_plus;
19393         do_binop:
19394           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19395           if (!loc)
19396             break;
19397           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
19398           if (!loc2)
19399             break;
19400           add_loc_descr (&loc, loc2);
19401           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
19402         }
19403       return loc;
19404     case MINUS_EXPR:
19405       op = DW_OP_minus;
19406       goto do_binop;
19407     case MULT_EXPR:
19408       op = DW_OP_mul;
19409       goto do_binop;
19410     case EQ_EXPR:
19411       op = DW_OP_eq;
19412       goto do_binop;
19413     case NE_EXPR:
19414       op = DW_OP_ne;
19415       goto do_binop;
19416     default:
19417       break;
19418     }
19419   return NULL;
19420 }
19421
19422 static void
19423 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
19424                       tree val, tree base_decl)
19425 {
19426   dw_loc_descr_ref loc;
19427
19428   if (host_integerp (val, 0))
19429     {
19430       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
19431       return;
19432     }
19433
19434   loc = descr_info_loc (val, base_decl);
19435   if (!loc)
19436     return;
19437
19438   add_AT_loc (die, attr, loc);
19439 }
19440
19441 /* This routine generates DIE for array with hidden descriptor, details
19442    are filled into *info by a langhook.  */
19443
19444 static void
19445 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19446                           dw_die_ref context_die)
19447 {
19448   dw_die_ref scope_die = scope_die_for (type, context_die);
19449   dw_die_ref array_die;
19450   int dim;
19451
19452   array_die = new_die (DW_TAG_array_type, scope_die, type);
19453   add_name_attribute (array_die, type_tag (type));
19454   equate_type_number_to_die (type, array_die);
19455
19456   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
19457   if (is_fortran ()
19458       && info->ndimensions >= 2)
19459     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19460
19461   if (info->data_location)
19462     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
19463                           info->base_decl);
19464   if (info->associated)
19465     add_descr_info_field (array_die, DW_AT_associated, info->associated,
19466                           info->base_decl);
19467   if (info->allocated)
19468     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
19469                           info->base_decl);
19470
19471   for (dim = 0; dim < info->ndimensions; dim++)
19472     {
19473       dw_die_ref subrange_die
19474         = new_die (DW_TAG_subrange_type, array_die, NULL);
19475
19476       if (info->dimen[dim].lower_bound)
19477         {
19478           /* If it is the default value, omit it.  */
19479           int dflt;
19480
19481           if (host_integerp (info->dimen[dim].lower_bound, 0)
19482               && (dflt = lower_bound_default ()) != -1
19483               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
19484             ;
19485           else
19486             add_descr_info_field (subrange_die, DW_AT_lower_bound,
19487                                   info->dimen[dim].lower_bound,
19488                                   info->base_decl);
19489         }
19490       if (info->dimen[dim].upper_bound)
19491         add_descr_info_field (subrange_die, DW_AT_upper_bound,
19492                               info->dimen[dim].upper_bound,
19493                               info->base_decl);
19494       if (info->dimen[dim].stride)
19495         add_descr_info_field (subrange_die, DW_AT_byte_stride,
19496                               info->dimen[dim].stride,
19497                               info->base_decl);
19498     }
19499
19500   gen_type_die (info->element_type, context_die);
19501   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
19502
19503   if (get_AT (array_die, DW_AT_name))
19504     add_pubtype (type, array_die);
19505 }
19506
19507 #if 0
19508 static void
19509 gen_entry_point_die (tree decl, dw_die_ref context_die)
19510 {
19511   tree origin = decl_ultimate_origin (decl);
19512   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19513
19514   if (origin != NULL)
19515     add_abstract_origin_attribute (decl_die, origin);
19516   else
19517     {
19518       add_name_and_src_coords_attributes (decl_die, decl);
19519       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19520                           0, 0, context_die);
19521     }
19522
19523   if (DECL_ABSTRACT (decl))
19524     equate_decl_number_to_die (decl, decl_die);
19525   else
19526     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19527 }
19528 #endif
19529
19530 /* Walk through the list of incomplete types again, trying once more to
19531    emit full debugging info for them.  */
19532
19533 static void
19534 retry_incomplete_types (void)
19535 {
19536   int i;
19537
19538   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
19539     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
19540                                   DINFO_USAGE_DIR_USE))
19541       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
19542 }
19543
19544 /* Determine what tag to use for a record type.  */
19545
19546 static enum dwarf_tag
19547 record_type_tag (tree type)
19548 {
19549   if (! lang_hooks.types.classify_record)
19550     return DW_TAG_structure_type;
19551
19552   switch (lang_hooks.types.classify_record (type))
19553     {
19554     case RECORD_IS_STRUCT:
19555       return DW_TAG_structure_type;
19556
19557     case RECORD_IS_CLASS:
19558       return DW_TAG_class_type;
19559
19560     case RECORD_IS_INTERFACE:
19561       if (dwarf_version >= 3 || !dwarf_strict)
19562         return DW_TAG_interface_type;
19563       return DW_TAG_structure_type;
19564
19565     default:
19566       gcc_unreachable ();
19567     }
19568 }
19569
19570 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
19571    include all of the information about the enumeration values also. Each
19572    enumerated type name/value is listed as a child of the enumerated type
19573    DIE.  */
19574
19575 static dw_die_ref
19576 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19577 {
19578   dw_die_ref type_die = lookup_type_die (type);
19579
19580   if (type_die == NULL)
19581     {
19582       type_die = new_die (DW_TAG_enumeration_type,
19583                           scope_die_for (type, context_die), type);
19584       equate_type_number_to_die (type, type_die);
19585       add_name_attribute (type_die, type_tag (type));
19586       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19587       if (dwarf_version >= 4 || !dwarf_strict)
19588         {
19589           if (ENUM_IS_SCOPED (type))
19590             add_AT_flag (type_die, DW_AT_enum_class, 1);
19591           if (ENUM_IS_OPAQUE (type))
19592             add_AT_flag (type_die, DW_AT_declaration, 1);
19593         }
19594     }
19595   else if (! TYPE_SIZE (type))
19596     return type_die;
19597   else
19598     remove_AT (type_die, DW_AT_declaration);
19599
19600   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
19601      given enum type is incomplete, do not generate the DW_AT_byte_size
19602      attribute or the DW_AT_element_list attribute.  */
19603   if (TYPE_SIZE (type))
19604     {
19605       tree link;
19606
19607       TREE_ASM_WRITTEN (type) = 1;
19608       add_byte_size_attribute (type_die, type);
19609       if (TYPE_STUB_DECL (type) != NULL_TREE)
19610         {
19611           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19612           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19613         }
19614
19615       /* If the first reference to this type was as the return type of an
19616          inline function, then it may not have a parent.  Fix this now.  */
19617       if (type_die->die_parent == NULL)
19618         add_child_die (scope_die_for (type, context_die), type_die);
19619
19620       for (link = TYPE_VALUES (type);
19621            link != NULL; link = TREE_CHAIN (link))
19622         {
19623           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19624           tree value = TREE_VALUE (link);
19625
19626           add_name_attribute (enum_die,
19627                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19628
19629           if (TREE_CODE (value) == CONST_DECL)
19630             value = DECL_INITIAL (value);
19631
19632           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
19633             /* DWARF2 does not provide a way of indicating whether or
19634                not enumeration constants are signed or unsigned.  GDB
19635                always assumes the values are signed, so we output all
19636                values as if they were signed.  That means that
19637                enumeration constants with very large unsigned values
19638                will appear to have negative values in the debugger.  */
19639             add_AT_int (enum_die, DW_AT_const_value,
19640                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
19641         }
19642     }
19643   else
19644     add_AT_flag (type_die, DW_AT_declaration, 1);
19645
19646   if (get_AT (type_die, DW_AT_name))
19647     add_pubtype (type, type_die);
19648
19649   return type_die;
19650 }
19651
19652 /* Generate a DIE to represent either a real live formal parameter decl or to
19653    represent just the type of some formal parameter position in some function
19654    type.
19655
19656    Note that this routine is a bit unusual because its argument may be a
19657    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19658    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19659    node.  If it's the former then this function is being called to output a
19660    DIE to represent a formal parameter object (or some inlining thereof).  If
19661    it's the latter, then this function is only being called to output a
19662    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19663    argument type of some subprogram type.
19664    If EMIT_NAME_P is true, name and source coordinate attributes
19665    are emitted.  */
19666
19667 static dw_die_ref
19668 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19669                           dw_die_ref context_die)
19670 {
19671   tree node_or_origin = node ? node : origin;
19672   tree ultimate_origin;
19673   dw_die_ref parm_die
19674     = new_die (DW_TAG_formal_parameter, context_die, node);
19675
19676   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19677     {
19678     case tcc_declaration:
19679       ultimate_origin = decl_ultimate_origin (node_or_origin);
19680       if (node || ultimate_origin)
19681         origin = ultimate_origin;
19682       if (origin != NULL)
19683         add_abstract_origin_attribute (parm_die, origin);
19684       else if (emit_name_p)
19685         add_name_and_src_coords_attributes (parm_die, node);
19686       if (origin == NULL
19687           || (! DECL_ABSTRACT (node_or_origin)
19688               && variably_modified_type_p (TREE_TYPE (node_or_origin),
19689                                            decl_function_context
19690                                                             (node_or_origin))))
19691         {
19692           tree type = TREE_TYPE (node_or_origin);
19693           if (decl_by_reference_p (node_or_origin))
19694             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
19695                                 context_die);
19696           else
19697             add_type_attribute (parm_die, type,
19698                                 TREE_READONLY (node_or_origin),
19699                                 TREE_THIS_VOLATILE (node_or_origin),
19700                                 context_die);
19701         }
19702       if (origin == NULL && DECL_ARTIFICIAL (node))
19703         add_AT_flag (parm_die, DW_AT_artificial, 1);
19704
19705       if (node && node != origin)
19706         equate_decl_number_to_die (node, parm_die);
19707       if (! DECL_ABSTRACT (node_or_origin))
19708         add_location_or_const_value_attribute (parm_die, node_or_origin,
19709                                                node == NULL, DW_AT_location);
19710
19711       break;
19712
19713     case tcc_type:
19714       /* We were called with some kind of a ..._TYPE node.  */
19715       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
19716       break;
19717
19718     default:
19719       gcc_unreachable ();
19720     }
19721
19722   return parm_die;
19723 }
19724
19725 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19726    children DW_TAG_formal_parameter DIEs representing the arguments of the
19727    parameter pack.
19728
19729    PARM_PACK must be a function parameter pack.
19730    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19731    must point to the subsequent arguments of the function PACK_ARG belongs to.
19732    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19733    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19734    following the last one for which a DIE was generated.  */
19735
19736 static dw_die_ref
19737 gen_formal_parameter_pack_die  (tree parm_pack,
19738                                 tree pack_arg,
19739                                 dw_die_ref subr_die,
19740                                 tree *next_arg)
19741 {
19742   tree arg;
19743   dw_die_ref parm_pack_die;
19744
19745   gcc_assert (parm_pack
19746               && lang_hooks.function_parameter_pack_p (parm_pack)
19747               && subr_die);
19748
19749   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19750   add_src_coords_attributes (parm_pack_die, parm_pack);
19751
19752   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19753     {
19754       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19755                                                                  parm_pack))
19756         break;
19757       gen_formal_parameter_die (arg, NULL,
19758                                 false /* Don't emit name attribute.  */,
19759                                 parm_pack_die);
19760     }
19761   if (next_arg)
19762     *next_arg = arg;
19763   return parm_pack_die;
19764 }
19765
19766 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19767    at the end of an (ANSI prototyped) formal parameters list.  */
19768
19769 static void
19770 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19771 {
19772   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19773 }
19774
19775 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19776    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19777    parameters as specified in some function type specification (except for
19778    those which appear as part of a function *definition*).  */
19779
19780 static void
19781 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19782 {
19783   tree link;
19784   tree formal_type = NULL;
19785   tree first_parm_type;
19786   tree arg;
19787
19788   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19789     {
19790       arg = DECL_ARGUMENTS (function_or_method_type);
19791       function_or_method_type = TREE_TYPE (function_or_method_type);
19792     }
19793   else
19794     arg = NULL_TREE;
19795
19796   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19797
19798   /* Make our first pass over the list of formal parameter types and output a
19799      DW_TAG_formal_parameter DIE for each one.  */
19800   for (link = first_parm_type; link; )
19801     {
19802       dw_die_ref parm_die;
19803
19804       formal_type = TREE_VALUE (link);
19805       if (formal_type == void_type_node)
19806         break;
19807
19808       /* Output a (nameless) DIE to represent the formal parameter itself.  */
19809       parm_die = gen_formal_parameter_die (formal_type, NULL,
19810                                            true /* Emit name attribute.  */,
19811                                            context_die);
19812       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19813           && link == first_parm_type)
19814         {
19815           add_AT_flag (parm_die, DW_AT_artificial, 1);
19816           if (dwarf_version >= 3 || !dwarf_strict)
19817             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19818         }
19819       else if (arg && DECL_ARTIFICIAL (arg))
19820         add_AT_flag (parm_die, DW_AT_artificial, 1);
19821
19822       link = TREE_CHAIN (link);
19823       if (arg)
19824         arg = DECL_CHAIN (arg);
19825     }
19826
19827   /* If this function type has an ellipsis, add a
19828      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
19829   if (formal_type != void_type_node)
19830     gen_unspecified_parameters_die (function_or_method_type, context_die);
19831
19832   /* Make our second (and final) pass over the list of formal parameter types
19833      and output DIEs to represent those types (as necessary).  */
19834   for (link = TYPE_ARG_TYPES (function_or_method_type);
19835        link && TREE_VALUE (link);
19836        link = TREE_CHAIN (link))
19837     gen_type_die (TREE_VALUE (link), context_die);
19838 }
19839
19840 /* We want to generate the DIE for TYPE so that we can generate the
19841    die for MEMBER, which has been defined; we will need to refer back
19842    to the member declaration nested within TYPE.  If we're trying to
19843    generate minimal debug info for TYPE, processing TYPE won't do the
19844    trick; we need to attach the member declaration by hand.  */
19845
19846 static void
19847 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19848 {
19849   gen_type_die (type, context_die);
19850
19851   /* If we're trying to avoid duplicate debug info, we may not have
19852      emitted the member decl for this function.  Emit it now.  */
19853   if (TYPE_STUB_DECL (type)
19854       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19855       && ! lookup_decl_die (member))
19856     {
19857       dw_die_ref type_die;
19858       gcc_assert (!decl_ultimate_origin (member));
19859
19860       push_decl_scope (type);
19861       type_die = lookup_type_die_strip_naming_typedef (type);
19862       if (TREE_CODE (member) == FUNCTION_DECL)
19863         gen_subprogram_die (member, type_die);
19864       else if (TREE_CODE (member) == FIELD_DECL)
19865         {
19866           /* Ignore the nameless fields that are used to skip bits but handle
19867              C++ anonymous unions and structs.  */
19868           if (DECL_NAME (member) != NULL_TREE
19869               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19870               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19871             {
19872               gen_type_die (member_declared_type (member), type_die);
19873               gen_field_die (member, type_die);
19874             }
19875         }
19876       else
19877         gen_variable_die (member, NULL_TREE, type_die);
19878
19879       pop_decl_scope ();
19880     }
19881 }
19882
19883 /* Generate the DWARF2 info for the "abstract" instance of a function which we
19884    may later generate inlined and/or out-of-line instances of.  */
19885
19886 static void
19887 dwarf2out_abstract_function (tree decl)
19888 {
19889   dw_die_ref old_die;
19890   tree save_fn;
19891   tree context;
19892   int was_abstract;
19893   htab_t old_decl_loc_table;
19894   htab_t old_cached_dw_loc_list_table;
19895   int old_call_site_count, old_tail_call_site_count;
19896   struct call_arg_loc_node *old_call_arg_locations;
19897
19898   /* Make sure we have the actual abstract inline, not a clone.  */
19899   decl = DECL_ORIGIN (decl);
19900
19901   old_die = lookup_decl_die (decl);
19902   if (old_die && get_AT (old_die, DW_AT_inline))
19903     /* We've already generated the abstract instance.  */
19904     return;
19905
19906   /* We can be called while recursively when seeing block defining inlined subroutine
19907      DIE.  Be sure to not clobber the outer location table nor use it or we would
19908      get locations in abstract instantces.  */
19909   old_decl_loc_table = decl_loc_table;
19910   decl_loc_table = NULL;
19911   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
19912   cached_dw_loc_list_table = NULL;
19913   old_call_arg_locations = call_arg_locations;
19914   call_arg_locations = NULL;
19915   old_call_site_count = call_site_count;
19916   call_site_count = -1;
19917   old_tail_call_site_count = tail_call_site_count;
19918   tail_call_site_count = -1;
19919
19920   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
19921      we don't get confused by DECL_ABSTRACT.  */
19922   if (debug_info_level > DINFO_LEVEL_TERSE)
19923     {
19924       context = decl_class_context (decl);
19925       if (context)
19926         gen_type_die_for_member
19927           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
19928     }
19929
19930   /* Pretend we've just finished compiling this function.  */
19931   save_fn = current_function_decl;
19932   current_function_decl = decl;
19933   push_cfun (DECL_STRUCT_FUNCTION (decl));
19934
19935   was_abstract = DECL_ABSTRACT (decl);
19936   set_decl_abstract_flags (decl, 1);
19937   dwarf2out_decl (decl);
19938   if (! was_abstract)
19939     set_decl_abstract_flags (decl, 0);
19940
19941   current_function_decl = save_fn;
19942   decl_loc_table = old_decl_loc_table;
19943   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
19944   call_arg_locations = old_call_arg_locations;
19945   call_site_count = old_call_site_count;
19946   tail_call_site_count = old_tail_call_site_count;
19947   pop_cfun ();
19948 }
19949
19950 /* Helper function of premark_used_types() which gets called through
19951    htab_traverse.
19952
19953    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19954    marked as unused by prune_unused_types.  */
19955
19956 static int
19957 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
19958 {
19959   tree type;
19960   dw_die_ref die;
19961
19962   type = (tree) *slot;
19963   die = lookup_type_die (type);
19964   if (die != NULL)
19965     die->die_perennial_p = 1;
19966   return 1;
19967 }
19968
19969 /* Helper function of premark_types_used_by_global_vars which gets called
19970    through htab_traverse.
19971
19972    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19973    marked as unused by prune_unused_types. The DIE of the type is marked
19974    only if the global variable using the type will actually be emitted.  */
19975
19976 static int
19977 premark_types_used_by_global_vars_helper (void **slot,
19978                                           void *data ATTRIBUTE_UNUSED)
19979 {
19980   struct types_used_by_vars_entry *entry;
19981   dw_die_ref die;
19982
19983   entry = (struct types_used_by_vars_entry *) *slot;
19984   gcc_assert (entry->type != NULL
19985               && entry->var_decl != NULL);
19986   die = lookup_type_die (entry->type);
19987   if (die)
19988     {
19989       /* Ask cgraph if the global variable really is to be emitted.
19990          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
19991       struct varpool_node *node = varpool_get_node (entry->var_decl);
19992       if (node && node->needed)
19993         {
19994           die->die_perennial_p = 1;
19995           /* Keep the parent DIEs as well.  */
19996           while ((die = die->die_parent) && die->die_perennial_p == 0)
19997             die->die_perennial_p = 1;
19998         }
19999     }
20000   return 1;
20001 }
20002
20003 /* Mark all members of used_types_hash as perennial.  */
20004
20005 static void
20006 premark_used_types (void)
20007 {
20008   if (cfun && cfun->used_types_hash)
20009     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
20010 }
20011
20012 /* Mark all members of types_used_by_vars_entry as perennial.  */
20013
20014 static void
20015 premark_types_used_by_global_vars (void)
20016 {
20017   if (types_used_by_vars_hash)
20018     htab_traverse (types_used_by_vars_hash,
20019                    premark_types_used_by_global_vars_helper, NULL);
20020 }
20021
20022 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20023    for CA_LOC call arg loc node.  */
20024
20025 static dw_die_ref
20026 gen_call_site_die (tree decl, dw_die_ref subr_die,
20027                    struct call_arg_loc_node *ca_loc)
20028 {
20029   dw_die_ref stmt_die = NULL, die;
20030   tree block = ca_loc->block;
20031
20032   while (block
20033          && block != DECL_INITIAL (decl)
20034          && TREE_CODE (block) == BLOCK)
20035     {
20036       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
20037         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
20038       if (stmt_die)
20039         break;
20040       block = BLOCK_SUPERCONTEXT (block);
20041     }
20042   if (stmt_die == NULL)
20043     stmt_die = subr_die;
20044   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20045   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20046   if (ca_loc->tail_call_p)
20047     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20048   if (ca_loc->symbol_ref)
20049     {
20050       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20051       if (tdie)
20052         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20053       else
20054         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
20055     }
20056   return die;
20057 }
20058
20059 /* Generate a DIE to represent a declared function (either file-scope or
20060    block-local).  */
20061
20062 static void
20063 gen_subprogram_die (tree decl, dw_die_ref context_die)
20064 {
20065   tree origin = decl_ultimate_origin (decl);
20066   dw_die_ref subr_die;
20067   tree outer_scope;
20068   dw_die_ref old_die = lookup_decl_die (decl);
20069   int declaration = (current_function_decl != decl
20070                      || class_or_namespace_scope_p (context_die));
20071
20072   premark_used_types ();
20073
20074   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
20075      started to generate the abstract instance of an inline, decided to output
20076      its containing class, and proceeded to emit the declaration of the inline
20077      from the member list for the class.  If so, DECLARATION takes priority;
20078      we'll get back to the abstract instance when done with the class.  */
20079
20080   /* The class-scope declaration DIE must be the primary DIE.  */
20081   if (origin && declaration && class_or_namespace_scope_p (context_die))
20082     {
20083       origin = NULL;
20084       gcc_assert (!old_die);
20085     }
20086
20087   /* Now that the C++ front end lazily declares artificial member fns, we
20088      might need to retrofit the declaration into its class.  */
20089   if (!declaration && !origin && !old_die
20090       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20091       && !class_or_namespace_scope_p (context_die)
20092       && debug_info_level > DINFO_LEVEL_TERSE)
20093     old_die = force_decl_die (decl);
20094
20095   if (origin != NULL)
20096     {
20097       gcc_assert (!declaration || local_scope_p (context_die));
20098
20099       /* Fixup die_parent for the abstract instance of a nested
20100          inline function.  */
20101       if (old_die && old_die->die_parent == NULL)
20102         add_child_die (context_die, old_die);
20103
20104       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20105       add_abstract_origin_attribute (subr_die, origin);
20106       /*  This is where the actual code for a cloned function is.
20107           Let's emit linkage name attribute for it.  This helps
20108           debuggers to e.g, set breakpoints into
20109           constructors/destructors when the user asks "break
20110           K::K".  */
20111       add_linkage_name (subr_die, decl);
20112     }
20113   else if (old_die)
20114     {
20115       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20116       struct dwarf_file_data * file_index = lookup_filename (s.file);
20117
20118       if (!get_AT_flag (old_die, DW_AT_declaration)
20119           /* We can have a normal definition following an inline one in the
20120              case of redefinition of GNU C extern inlines.
20121              It seems reasonable to use AT_specification in this case.  */
20122           && !get_AT (old_die, DW_AT_inline))
20123         {
20124           /* Detect and ignore this case, where we are trying to output
20125              something we have already output.  */
20126           return;
20127         }
20128
20129       /* If the definition comes from the same place as the declaration,
20130          maybe use the old DIE.  We always want the DIE for this function
20131          that has the *_pc attributes to be under comp_unit_die so the
20132          debugger can find it.  We also need to do this for abstract
20133          instances of inlines, since the spec requires the out-of-line copy
20134          to have the same parent.  For local class methods, this doesn't
20135          apply; we just use the old DIE.  */
20136       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
20137           && (DECL_ARTIFICIAL (decl)
20138               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20139                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
20140                       == (unsigned) s.line))))
20141         {
20142           subr_die = old_die;
20143
20144           /* Clear out the declaration attribute and the formal parameters.
20145              Do not remove all children, because it is possible that this
20146              declaration die was forced using force_decl_die(). In such
20147              cases die that forced declaration die (e.g. TAG_imported_module)
20148              is one of the children that we do not want to remove.  */
20149           remove_AT (subr_die, DW_AT_declaration);
20150           remove_AT (subr_die, DW_AT_object_pointer);
20151           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20152         }
20153       else
20154         {
20155           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20156           add_AT_specification (subr_die, old_die);
20157           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20158             add_AT_file (subr_die, DW_AT_decl_file, file_index);
20159           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20160             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20161         }
20162     }
20163   else
20164     {
20165       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20166
20167       if (TREE_PUBLIC (decl))
20168         add_AT_flag (subr_die, DW_AT_external, 1);
20169
20170       add_name_and_src_coords_attributes (subr_die, decl);
20171       if (debug_info_level > DINFO_LEVEL_TERSE)
20172         {
20173           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20174           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20175                               0, 0, context_die);
20176         }
20177
20178       add_pure_or_virtual_attribute (subr_die, decl);
20179       if (DECL_ARTIFICIAL (decl))
20180         add_AT_flag (subr_die, DW_AT_artificial, 1);
20181
20182       add_accessibility_attribute (subr_die, decl);
20183     }
20184
20185   if (declaration)
20186     {
20187       if (!old_die || !get_AT (old_die, DW_AT_inline))
20188         {
20189           add_AT_flag (subr_die, DW_AT_declaration, 1);
20190
20191           /* If this is an explicit function declaration then generate
20192              a DW_AT_explicit attribute.  */
20193           if (lang_hooks.decls.function_decl_explicit_p (decl)
20194               && (dwarf_version >= 3 || !dwarf_strict))
20195             add_AT_flag (subr_die, DW_AT_explicit, 1);
20196
20197           /* The first time we see a member function, it is in the context of
20198              the class to which it belongs.  We make sure of this by emitting
20199              the class first.  The next time is the definition, which is
20200              handled above.  The two may come from the same source text.
20201
20202              Note that force_decl_die() forces function declaration die. It is
20203              later reused to represent definition.  */
20204           equate_decl_number_to_die (decl, subr_die);
20205         }
20206     }
20207   else if (DECL_ABSTRACT (decl))
20208     {
20209       if (DECL_DECLARED_INLINE_P (decl))
20210         {
20211           if (cgraph_function_possibly_inlined_p (decl))
20212             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20213           else
20214             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20215         }
20216       else
20217         {
20218           if (cgraph_function_possibly_inlined_p (decl))
20219             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20220           else
20221             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20222         }
20223
20224       if (DECL_DECLARED_INLINE_P (decl)
20225           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20226         add_AT_flag (subr_die, DW_AT_artificial, 1);
20227
20228       equate_decl_number_to_die (decl, subr_die);
20229     }
20230   else if (!DECL_EXTERNAL (decl))
20231     {
20232       HOST_WIDE_INT cfa_fb_offset;
20233
20234       if (!old_die || !get_AT (old_die, DW_AT_inline))
20235         equate_decl_number_to_die (decl, subr_die);
20236
20237       if (!flag_reorder_blocks_and_partition)
20238         {
20239           dw_fde_ref fde = &fde_table[current_funcdef_fde];
20240           if (fde->dw_fde_begin)
20241             {
20242               /* We have already generated the labels.  */
20243               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
20244               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
20245             }
20246           else
20247             {
20248               /* Create start/end labels and add the range.  */
20249               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
20250               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
20251                                            current_function_funcdef_no);
20252               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
20253               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
20254                                            current_function_funcdef_no);
20255               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
20256             }
20257
20258 #if VMS_DEBUGGING_INFO
20259       /* HP OpenVMS Industry Standard 64: DWARF Extensions
20260          Section 2.3 Prologue and Epilogue Attributes:
20261          When a breakpoint is set on entry to a function, it is generally
20262          desirable for execution to be suspended, not on the very first
20263          instruction of the function, but rather at a point after the
20264          function's frame has been set up, after any language defined local
20265          declaration processing has been completed, and before execution of
20266          the first statement of the function begins. Debuggers generally
20267          cannot properly determine where this point is.  Similarly for a
20268          breakpoint set on exit from a function. The prologue and epilogue
20269          attributes allow a compiler to communicate the location(s) to use.  */
20270
20271       {
20272         if (fde->dw_fde_vms_end_prologue)
20273           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20274             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20275
20276         if (fde->dw_fde_vms_begin_epilogue)
20277           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20278             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20279       }
20280 #endif
20281
20282           add_pubname (decl, subr_die);
20283         }
20284       else
20285         {  /* Generate pubnames entries for the split function code
20286               ranges.  */
20287           dw_fde_ref fde = &fde_table[current_funcdef_fde];
20288
20289           if (fde->dw_fde_second_begin)
20290             {
20291               if (dwarf_version >= 3 || !dwarf_strict)
20292                 {
20293                   /* We should use ranges for non-contiguous code section 
20294                      addresses.  Use the actual code range for the initial
20295                      section, since the HOT/COLD labels might precede an 
20296                      alignment offset.  */
20297                   bool range_list_added = false;
20298                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20299                                         fde->dw_fde_end, &range_list_added);
20300                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20301                                         fde->dw_fde_second_end,
20302                                         &range_list_added);
20303                   add_pubname (decl, subr_die);
20304                   if (range_list_added)
20305                     add_ranges (NULL);
20306                 }
20307               else
20308                 {
20309                   /* There is no real support in DW2 for this .. so we make
20310                      a work-around.  First, emit the pub name for the segment
20311                      containing the function label.  Then make and emit a
20312                      simplified subprogram DIE for the second segment with the
20313                      name pre-fixed by __hot/cold_sect_of_.  We use the same
20314                      linkage name for the second die so that gdb will find both
20315                      sections when given "b foo".  */
20316                   const char *name = NULL;
20317                   tree decl_name = DECL_NAME (decl);
20318                   dw_die_ref seg_die;
20319
20320                   /* Do the 'primary' section.   */
20321                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
20322                                  fde->dw_fde_begin);
20323                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
20324                                  fde->dw_fde_end);
20325                   /* Add it.   */
20326                   add_pubname (decl, subr_die);
20327
20328                   /* Build a minimal DIE for the secondary section.  */
20329                   seg_die = new_die (DW_TAG_subprogram,
20330                                      subr_die->die_parent, decl);
20331
20332                   if (TREE_PUBLIC (decl))
20333                     add_AT_flag (seg_die, DW_AT_external, 1);
20334
20335                   if (decl_name != NULL 
20336                       && IDENTIFIER_POINTER (decl_name) != NULL)
20337                     {
20338                       name = dwarf2_name (decl, 1);
20339                       if (! DECL_ARTIFICIAL (decl))
20340                         add_src_coords_attributes (seg_die, decl);
20341
20342                       add_linkage_name (seg_die, decl);
20343                     }
20344                   gcc_assert (name != NULL);
20345                   add_pure_or_virtual_attribute (seg_die, decl);
20346                   if (DECL_ARTIFICIAL (decl))
20347                     add_AT_flag (seg_die, DW_AT_artificial, 1);
20348
20349                   name = concat ("__second_sect_of_", name, NULL); 
20350                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
20351                                  fde->dw_fde_second_begin);
20352                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
20353                                  fde->dw_fde_second_end);
20354                   add_name_attribute (seg_die, name);
20355                   add_pubname_string (name, seg_die);
20356                 }
20357             }
20358           else
20359             {
20360               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
20361               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
20362               add_pubname (decl, subr_die);
20363             }
20364         }
20365
20366 #ifdef MIPS_DEBUGGING_INFO
20367       /* Add a reference to the FDE for this routine.  */
20368       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
20369 #endif
20370
20371       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20372
20373       /* We define the "frame base" as the function's CFA.  This is more
20374          convenient for several reasons: (1) It's stable across the prologue
20375          and epilogue, which makes it better than just a frame pointer,
20376          (2) With dwarf3, there exists a one-byte encoding that allows us
20377          to reference the .debug_frame data by proxy, but failing that,
20378          (3) We can at least reuse the code inspection and interpretation
20379          code that determines the CFA position at various points in the
20380          function.  */
20381       if (dwarf_version >= 3)
20382         {
20383           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20384           add_AT_loc (subr_die, DW_AT_frame_base, op);
20385         }
20386       else
20387         {
20388           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20389           if (list->dw_loc_next)
20390             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20391           else
20392             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20393         }
20394
20395       /* Compute a displacement from the "steady-state frame pointer" to
20396          the CFA.  The former is what all stack slots and argument slots
20397          will reference in the rtl; the later is what we've told the
20398          debugger about.  We'll need to adjust all frame_base references
20399          by this displacement.  */
20400       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20401
20402       if (cfun->static_chain_decl)
20403         add_AT_location_description (subr_die, DW_AT_static_link,
20404                  loc_list_from_tree (cfun->static_chain_decl, 2));
20405     }
20406
20407   /* Generate child dies for template paramaters.  */
20408   if (debug_info_level > DINFO_LEVEL_TERSE)
20409     gen_generic_params_dies (decl);
20410
20411   /* Now output descriptions of the arguments for this function. This gets
20412      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20413      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20414      `...' at the end of the formal parameter list.  In order to find out if
20415      there was a trailing ellipsis or not, we must instead look at the type
20416      associated with the FUNCTION_DECL.  This will be a node of type
20417      FUNCTION_TYPE. If the chain of type nodes hanging off of this
20418      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20419      an ellipsis at the end.  */
20420
20421   /* In the case where we are describing a mere function declaration, all we
20422      need to do here (and all we *can* do here) is to describe the *types* of
20423      its formal parameters.  */
20424   if (debug_info_level <= DINFO_LEVEL_TERSE)
20425     ;
20426   else if (declaration)
20427     gen_formal_types_die (decl, subr_die);
20428   else
20429     {
20430       /* Generate DIEs to represent all known formal parameters.  */
20431       tree parm = DECL_ARGUMENTS (decl);
20432       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
20433       tree generic_decl_parm = generic_decl
20434                                 ? DECL_ARGUMENTS (generic_decl)
20435                                 : NULL;
20436
20437       /* Now we want to walk the list of parameters of the function and
20438          emit their relevant DIEs.
20439
20440          We consider the case of DECL being an instance of a generic function
20441          as well as it being a normal function.
20442
20443          If DECL is an instance of a generic function we walk the
20444          parameters of the generic function declaration _and_ the parameters of
20445          DECL itself. This is useful because we want to emit specific DIEs for
20446          function parameter packs and those are declared as part of the
20447          generic function declaration. In that particular case,
20448          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20449          That DIE has children DIEs representing the set of arguments
20450          of the pack. Note that the set of pack arguments can be empty.
20451          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20452          children DIE.
20453
20454          Otherwise, we just consider the parameters of DECL.  */
20455       while (generic_decl_parm || parm)
20456         {
20457           if (generic_decl_parm
20458               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20459             gen_formal_parameter_pack_die (generic_decl_parm,
20460                                            parm, subr_die,
20461                                            &parm);
20462           else if (parm)
20463             {
20464               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
20465
20466               if (parm == DECL_ARGUMENTS (decl)
20467                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20468                   && parm_die
20469                   && (dwarf_version >= 3 || !dwarf_strict))
20470                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20471
20472               parm = DECL_CHAIN (parm);
20473             }
20474
20475           if (generic_decl_parm)
20476             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20477         }
20478
20479       /* Decide whether we need an unspecified_parameters DIE at the end.
20480          There are 2 more cases to do this for: 1) the ansi ... declaration -
20481          this is detectable when the end of the arg list is not a
20482          void_type_node 2) an unprototyped function declaration (not a
20483          definition).  This just means that we have no info about the
20484          parameters at all.  */
20485       if (prototype_p (TREE_TYPE (decl)))
20486         {
20487           /* This is the prototyped case, check for....  */
20488           if (stdarg_p (TREE_TYPE (decl)))
20489             gen_unspecified_parameters_die (decl, subr_die);
20490         }
20491       else if (DECL_INITIAL (decl) == NULL_TREE)
20492         gen_unspecified_parameters_die (decl, subr_die);
20493     }
20494
20495   /* Output Dwarf info for all of the stuff within the body of the function
20496      (if it has one - it may be just a declaration).  */
20497   outer_scope = DECL_INITIAL (decl);
20498
20499   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20500      a function.  This BLOCK actually represents the outermost binding contour
20501      for the function, i.e. the contour in which the function's formal
20502      parameters and labels get declared. Curiously, it appears that the front
20503      end doesn't actually put the PARM_DECL nodes for the current function onto
20504      the BLOCK_VARS list for this outer scope, but are strung off of the
20505      DECL_ARGUMENTS list for the function instead.
20506
20507      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20508      the LABEL_DECL nodes for the function however, and we output DWARF info
20509      for those in decls_for_scope.  Just within the `outer_scope' there will be
20510      a BLOCK node representing the function's outermost pair of curly braces,
20511      and any blocks used for the base and member initializers of a C++
20512      constructor function.  */
20513   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
20514     {
20515       int call_site_note_count = 0;
20516       int tail_call_site_note_count = 0;
20517
20518       /* Emit a DW_TAG_variable DIE for a named return value.  */
20519       if (DECL_NAME (DECL_RESULT (decl)))
20520         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
20521
20522       current_function_has_inlines = 0;
20523       decls_for_scope (outer_scope, subr_die, 0);
20524
20525       if (call_arg_locations && !dwarf_strict)
20526         {
20527           struct call_arg_loc_node *ca_loc;
20528           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20529             {
20530               dw_die_ref die = NULL;
20531               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20532               rtx arg, next_arg;
20533
20534               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
20535                    arg; arg = next_arg)
20536                 {
20537                   dw_loc_descr_ref reg, val;
20538                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
20539                   dw_die_ref cdie;
20540
20541                   next_arg = XEXP (arg, 1);
20542                   if (REG_P (XEXP (XEXP (arg, 0), 0))
20543                       && next_arg
20544                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
20545                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
20546                       && REGNO (XEXP (XEXP (arg, 0), 0))
20547                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
20548                     next_arg = XEXP (next_arg, 1);
20549                   if (mode == VOIDmode)
20550                     {
20551                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
20552                       if (mode == VOIDmode)
20553                         mode = GET_MODE (XEXP (arg, 0));
20554                     }
20555                   if (mode == VOIDmode || mode == BLKmode)
20556                     continue;
20557                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
20558                     {
20559                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20560                       tloc = XEXP (XEXP (arg, 0), 1);
20561                       continue;
20562                     }
20563                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
20564                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
20565                     {
20566                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20567                       tlocc = XEXP (XEXP (arg, 0), 1);
20568                       continue;
20569                     }
20570                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
20571                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
20572                                               VAR_INIT_STATUS_INITIALIZED);
20573                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
20574                     {
20575                       rtx mem = XEXP (XEXP (arg, 0), 0);
20576                       reg = mem_loc_descriptor (XEXP (mem, 0),
20577                                                 get_address_mode (mem),
20578                                                 GET_MODE (mem),
20579                                                 VAR_INIT_STATUS_INITIALIZED);
20580                     }
20581                   else
20582                     continue;
20583                   if (reg == NULL)
20584                     continue;
20585                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
20586                                             VOIDmode,
20587                                             VAR_INIT_STATUS_INITIALIZED);
20588                   if (val == NULL)
20589                     continue;
20590                   if (die == NULL)
20591                     die = gen_call_site_die (decl, subr_die, ca_loc);
20592                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
20593                                   NULL_TREE);           
20594                   add_AT_loc (cdie, DW_AT_location, reg);
20595                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
20596                   if (next_arg != XEXP (arg, 1))
20597                     {
20598                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
20599                       if (mode == VOIDmode)
20600                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
20601                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
20602                                                             0), 1),
20603                                                 mode, VOIDmode,
20604                                                 VAR_INIT_STATUS_INITIALIZED);
20605                       if (val != NULL)
20606                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
20607                     }
20608                 }
20609               if (die == NULL
20610                   && (ca_loc->symbol_ref || tloc))
20611                 die = gen_call_site_die (decl, subr_die, ca_loc);
20612               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
20613                 {
20614                   dw_loc_descr_ref tval = NULL;
20615
20616                   if (tloc != NULL_RTX)
20617                     tval = mem_loc_descriptor (tloc,
20618                                                GET_MODE (tloc) == VOIDmode
20619                                                ? Pmode : GET_MODE (tloc),
20620                                                VOIDmode,
20621                                                VAR_INIT_STATUS_INITIALIZED);
20622                   if (tval)
20623                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
20624                   else if (tlocc != NULL_RTX)
20625                     {
20626                       tval = mem_loc_descriptor (tlocc,
20627                                                  GET_MODE (tlocc) == VOIDmode
20628                                                  ? Pmode : GET_MODE (tlocc),
20629                                                  VOIDmode,
20630                                                  VAR_INIT_STATUS_INITIALIZED);
20631                       if (tval)
20632                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
20633                                     tval);
20634                     }
20635                 }
20636               if (die != NULL)
20637                 {
20638                   call_site_note_count++;
20639                   if (ca_loc->tail_call_p)
20640                     tail_call_site_note_count++;
20641                 }
20642             }
20643         }
20644       call_arg_locations = NULL;
20645       call_arg_loc_last = NULL;
20646       if (tail_call_site_count >= 0
20647           && tail_call_site_count == tail_call_site_note_count
20648           && !dwarf_strict)
20649         {
20650           if (call_site_count >= 0
20651               && call_site_count == call_site_note_count)
20652             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
20653           else
20654             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
20655         }
20656       call_site_count = -1;
20657       tail_call_site_count = -1;
20658     }
20659   /* Add the calling convention attribute if requested.  */
20660   add_calling_convention_attribute (subr_die, decl);
20661
20662 }
20663
20664 /* Returns a hash value for X (which really is a die_struct).  */
20665
20666 static hashval_t
20667 common_block_die_table_hash (const void *x)
20668 {
20669   const_dw_die_ref d = (const_dw_die_ref) x;
20670   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
20671 }
20672
20673 /* Return nonzero if decl_id and die_parent of die_struct X is the same
20674    as decl_id and die_parent of die_struct Y.  */
20675
20676 static int
20677 common_block_die_table_eq (const void *x, const void *y)
20678 {
20679   const_dw_die_ref d = (const_dw_die_ref) x;
20680   const_dw_die_ref e = (const_dw_die_ref) y;
20681   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
20682 }
20683
20684 /* Generate a DIE to represent a declared data object.
20685    Either DECL or ORIGIN must be non-null.  */
20686
20687 static void
20688 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
20689 {
20690   HOST_WIDE_INT off;
20691   tree com_decl;
20692   tree decl_or_origin = decl ? decl : origin;
20693   tree ultimate_origin;
20694   dw_die_ref var_die;
20695   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
20696   dw_die_ref origin_die;
20697   bool declaration = (DECL_EXTERNAL (decl_or_origin)
20698                       || class_or_namespace_scope_p (context_die));
20699   bool specialization_p = false;
20700
20701   ultimate_origin = decl_ultimate_origin (decl_or_origin);
20702   if (decl || ultimate_origin)
20703     origin = ultimate_origin;
20704   com_decl = fortran_common (decl_or_origin, &off);
20705
20706   /* Symbol in common gets emitted as a child of the common block, in the form
20707      of a data member.  */
20708   if (com_decl)
20709     {
20710       dw_die_ref com_die;
20711       dw_loc_list_ref loc;
20712       die_node com_die_arg;
20713
20714       var_die = lookup_decl_die (decl_or_origin);
20715       if (var_die)
20716         {
20717           if (get_AT (var_die, DW_AT_location) == NULL)
20718             {
20719               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
20720               if (loc)
20721                 {
20722                   if (off)
20723                     {
20724                       /* Optimize the common case.  */
20725                       if (single_element_loc_list_p (loc)
20726                           && loc->expr->dw_loc_opc == DW_OP_addr
20727                           && loc->expr->dw_loc_next == NULL
20728                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
20729                              == SYMBOL_REF)
20730                         loc->expr->dw_loc_oprnd1.v.val_addr
20731                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20732                         else
20733                           loc_list_plus_const (loc, off);
20734                     }
20735                   add_AT_location_description (var_die, DW_AT_location, loc);
20736                   remove_AT (var_die, DW_AT_declaration);
20737                 }
20738             }
20739           return;
20740         }
20741
20742       if (common_block_die_table == NULL)
20743         common_block_die_table
20744           = htab_create_ggc (10, common_block_die_table_hash,
20745                              common_block_die_table_eq, NULL);
20746
20747       com_die_arg.decl_id = DECL_UID (com_decl);
20748       com_die_arg.die_parent = context_die;
20749       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
20750       loc = loc_list_from_tree (com_decl, 2);
20751       if (com_die == NULL)
20752         {
20753           const char *cnam
20754             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
20755           void **slot;
20756
20757           com_die = new_die (DW_TAG_common_block, context_die, decl);
20758           add_name_and_src_coords_attributes (com_die, com_decl);
20759           if (loc)
20760             {
20761               add_AT_location_description (com_die, DW_AT_location, loc);
20762               /* Avoid sharing the same loc descriptor between
20763                  DW_TAG_common_block and DW_TAG_variable.  */
20764               loc = loc_list_from_tree (com_decl, 2);
20765             }
20766           else if (DECL_EXTERNAL (decl))
20767             add_AT_flag (com_die, DW_AT_declaration, 1);
20768           add_pubname_string (cnam, com_die); /* ??? needed? */
20769           com_die->decl_id = DECL_UID (com_decl);
20770           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
20771           *slot = (void *) com_die;
20772         }
20773       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
20774         {
20775           add_AT_location_description (com_die, DW_AT_location, loc);
20776           loc = loc_list_from_tree (com_decl, 2);
20777           remove_AT (com_die, DW_AT_declaration);
20778         }
20779       var_die = new_die (DW_TAG_variable, com_die, decl);
20780       add_name_and_src_coords_attributes (var_die, decl);
20781       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
20782                           TREE_THIS_VOLATILE (decl), context_die);
20783       add_AT_flag (var_die, DW_AT_external, 1);
20784       if (loc)
20785         {
20786           if (off)
20787             {
20788               /* Optimize the common case.  */
20789               if (single_element_loc_list_p (loc)
20790                   && loc->expr->dw_loc_opc == DW_OP_addr
20791                   && loc->expr->dw_loc_next == NULL
20792                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
20793                 loc->expr->dw_loc_oprnd1.v.val_addr
20794                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20795               else
20796                 loc_list_plus_const (loc, off);
20797             }
20798           add_AT_location_description (var_die, DW_AT_location, loc);
20799         }
20800       else if (DECL_EXTERNAL (decl))
20801         add_AT_flag (var_die, DW_AT_declaration, 1);
20802       equate_decl_number_to_die (decl, var_die);
20803       return;
20804     }
20805
20806   /* If the compiler emitted a definition for the DECL declaration
20807      and if we already emitted a DIE for it, don't emit a second
20808      DIE for it again. Allow re-declarations of DECLs that are
20809      inside functions, though.  */
20810   if (old_die && declaration && !local_scope_p (context_die))
20811     return;
20812
20813   /* For static data members, the declaration in the class is supposed
20814      to have DW_TAG_member tag; the specification should still be
20815      DW_TAG_variable referencing the DW_TAG_member DIE.  */
20816   if (declaration && class_scope_p (context_die))
20817     var_die = new_die (DW_TAG_member, context_die, decl);
20818   else
20819     var_die = new_die (DW_TAG_variable, context_die, decl);
20820
20821   origin_die = NULL;
20822   if (origin != NULL)
20823     origin_die = add_abstract_origin_attribute (var_die, origin);
20824
20825   /* Loop unrolling can create multiple blocks that refer to the same
20826      static variable, so we must test for the DW_AT_declaration flag.
20827
20828      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
20829      copy decls and set the DECL_ABSTRACT flag on them instead of
20830      sharing them.
20831
20832      ??? Duplicated blocks have been rewritten to use .debug_ranges.
20833
20834      ??? The declare_in_namespace support causes us to get two DIEs for one
20835      variable, both of which are declarations.  We want to avoid considering
20836      one to be a specification, so we must test that this DIE is not a
20837      declaration.  */
20838   else if (old_die && TREE_STATIC (decl) && ! declaration
20839            && get_AT_flag (old_die, DW_AT_declaration) == 1)
20840     {
20841       /* This is a definition of a C++ class level static.  */
20842       add_AT_specification (var_die, old_die);
20843       specialization_p = true;
20844       if (DECL_NAME (decl))
20845         {
20846           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20847           struct dwarf_file_data * file_index = lookup_filename (s.file);
20848
20849           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20850             add_AT_file (var_die, DW_AT_decl_file, file_index);
20851
20852           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20853             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
20854
20855           if (old_die->die_tag == DW_TAG_member)
20856             add_linkage_name (var_die, decl);
20857         }
20858     }
20859   else
20860     add_name_and_src_coords_attributes (var_die, decl);
20861
20862   if ((origin == NULL && !specialization_p)
20863       || (origin != NULL
20864           && !DECL_ABSTRACT (decl_or_origin)
20865           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
20866                                        decl_function_context
20867                                                         (decl_or_origin))))
20868     {
20869       tree type = TREE_TYPE (decl_or_origin);
20870
20871       if (decl_by_reference_p (decl_or_origin))
20872         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
20873       else
20874         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
20875                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
20876     }
20877
20878   if (origin == NULL && !specialization_p)
20879     {
20880       if (TREE_PUBLIC (decl))
20881         add_AT_flag (var_die, DW_AT_external, 1);
20882
20883       if (DECL_ARTIFICIAL (decl))
20884         add_AT_flag (var_die, DW_AT_artificial, 1);
20885
20886       add_accessibility_attribute (var_die, decl);
20887     }
20888
20889   if (declaration)
20890     add_AT_flag (var_die, DW_AT_declaration, 1);
20891
20892   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
20893     equate_decl_number_to_die (decl, var_die);
20894
20895   if (! declaration
20896       && (! DECL_ABSTRACT (decl_or_origin)
20897           /* Local static vars are shared between all clones/inlines,
20898              so emit DW_AT_location on the abstract DIE if DECL_RTL is
20899              already set.  */
20900           || (TREE_CODE (decl_or_origin) == VAR_DECL
20901               && TREE_STATIC (decl_or_origin)
20902               && DECL_RTL_SET_P (decl_or_origin)))
20903       /* When abstract origin already has DW_AT_location attribute, no need
20904          to add it again.  */
20905       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
20906     {
20907       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
20908           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
20909         defer_location (decl_or_origin, var_die);
20910       else
20911         add_location_or_const_value_attribute (var_die, decl_or_origin,
20912                                                decl == NULL, DW_AT_location);
20913       add_pubname (decl_or_origin, var_die);
20914     }
20915   else
20916     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
20917 }
20918
20919 /* Generate a DIE to represent a named constant.  */
20920
20921 static void
20922 gen_const_die (tree decl, dw_die_ref context_die)
20923 {
20924   dw_die_ref const_die;
20925   tree type = TREE_TYPE (decl);
20926
20927   const_die = new_die (DW_TAG_constant, context_die, decl);
20928   add_name_and_src_coords_attributes (const_die, decl);
20929   add_type_attribute (const_die, type, 1, 0, context_die);
20930   if (TREE_PUBLIC (decl))
20931     add_AT_flag (const_die, DW_AT_external, 1);
20932   if (DECL_ARTIFICIAL (decl))
20933     add_AT_flag (const_die, DW_AT_artificial, 1);
20934   tree_add_const_value_attribute_for_decl (const_die, decl);
20935 }
20936
20937 /* Generate a DIE to represent a label identifier.  */
20938
20939 static void
20940 gen_label_die (tree decl, dw_die_ref context_die)
20941 {
20942   tree origin = decl_ultimate_origin (decl);
20943   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
20944   rtx insn;
20945   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20946
20947   if (origin != NULL)
20948     add_abstract_origin_attribute (lbl_die, origin);
20949   else
20950     add_name_and_src_coords_attributes (lbl_die, decl);
20951
20952   if (DECL_ABSTRACT (decl))
20953     equate_decl_number_to_die (decl, lbl_die);
20954   else
20955     {
20956       insn = DECL_RTL_IF_SET (decl);
20957
20958       /* Deleted labels are programmer specified labels which have been
20959          eliminated because of various optimizations.  We still emit them
20960          here so that it is possible to put breakpoints on them.  */
20961       if (insn
20962           && (LABEL_P (insn)
20963               || ((NOTE_P (insn)
20964                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
20965         {
20966           /* When optimization is enabled (via -O) some parts of the compiler
20967              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
20968              represent source-level labels which were explicitly declared by
20969              the user.  This really shouldn't be happening though, so catch
20970              it if it ever does happen.  */
20971           gcc_assert (!INSN_DELETED_P (insn));
20972
20973           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
20974           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
20975         }
20976     }
20977 }
20978
20979 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
20980    attributes to the DIE for a block STMT, to describe where the inlined
20981    function was called from.  This is similar to add_src_coords_attributes.  */
20982
20983 static inline void
20984 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
20985 {
20986   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
20987
20988   if (dwarf_version >= 3 || !dwarf_strict)
20989     {
20990       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
20991       add_AT_unsigned (die, DW_AT_call_line, s.line);
20992     }
20993 }
20994
20995
20996 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
20997    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
20998
20999 static inline void
21000 add_high_low_attributes (tree stmt, dw_die_ref die)
21001 {
21002   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21003
21004   if (BLOCK_FRAGMENT_CHAIN (stmt)
21005       && (dwarf_version >= 3 || !dwarf_strict))
21006     {
21007       tree chain;
21008
21009       if (inlined_function_outer_scope_p (stmt))
21010         {
21011           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21012                                        BLOCK_NUMBER (stmt));
21013           add_AT_lbl_id (die, DW_AT_entry_pc, label);
21014         }
21015
21016       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
21017
21018       chain = BLOCK_FRAGMENT_CHAIN (stmt);
21019       do
21020         {
21021           add_ranges (chain);
21022           chain = BLOCK_FRAGMENT_CHAIN (chain);
21023         }
21024       while (chain);
21025       add_ranges (NULL);
21026     }
21027   else
21028     {
21029       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21030                                    BLOCK_NUMBER (stmt));
21031       add_AT_lbl_id (die, DW_AT_low_pc, label);
21032       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
21033                                    BLOCK_NUMBER (stmt));
21034       add_AT_lbl_id (die, DW_AT_high_pc, label);
21035     }
21036 }
21037
21038 /* Generate a DIE for a lexical block.  */
21039
21040 static void
21041 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
21042 {
21043   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21044
21045   if (call_arg_locations)
21046     {
21047       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
21048         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
21049                                BLOCK_NUMBER (stmt) + 1);
21050       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
21051     }
21052
21053   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21054     add_high_low_attributes (stmt, stmt_die);
21055
21056   decls_for_scope (stmt, stmt_die, depth);
21057 }
21058
21059 /* Generate a DIE for an inlined subprogram.  */
21060
21061 static void
21062 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
21063 {
21064   tree decl;
21065
21066   /* The instance of function that is effectively being inlined shall not
21067      be abstract.  */
21068   gcc_assert (! BLOCK_ABSTRACT (stmt));
21069
21070   decl = block_ultimate_origin (stmt);
21071
21072   /* Emit info for the abstract instance first, if we haven't yet.  We
21073      must emit this even if the block is abstract, otherwise when we
21074      emit the block below (or elsewhere), we may end up trying to emit
21075      a die whose origin die hasn't been emitted, and crashing.  */
21076   dwarf2out_abstract_function (decl);
21077
21078   if (! BLOCK_ABSTRACT (stmt))
21079     {
21080       dw_die_ref subr_die
21081         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21082
21083       if (call_arg_locations)
21084         {
21085           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
21086             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
21087                                    BLOCK_NUMBER (stmt) + 1);
21088           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
21089         }
21090       add_abstract_origin_attribute (subr_die, decl);
21091       if (TREE_ASM_WRITTEN (stmt))
21092         add_high_low_attributes (stmt, subr_die);
21093       add_call_src_coords_attributes (stmt, subr_die);
21094
21095       decls_for_scope (stmt, subr_die, depth);
21096       current_function_has_inlines = 1;
21097     }
21098 }
21099
21100 /* Generate a DIE for a field in a record, or structure.  */
21101
21102 static void
21103 gen_field_die (tree decl, dw_die_ref context_die)
21104 {
21105   dw_die_ref decl_die;
21106
21107   if (TREE_TYPE (decl) == error_mark_node)
21108     return;
21109
21110   decl_die = new_die (DW_TAG_member, context_die, decl);
21111   add_name_and_src_coords_attributes (decl_die, decl);
21112   add_type_attribute (decl_die, member_declared_type (decl),
21113                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
21114                       context_die);
21115
21116   if (DECL_BIT_FIELD_TYPE (decl))
21117     {
21118       add_byte_size_attribute (decl_die, decl);
21119       add_bit_size_attribute (decl_die, decl);
21120       add_bit_offset_attribute (decl_die, decl);
21121     }
21122
21123   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21124     add_data_member_location_attribute (decl_die, decl);
21125
21126   if (DECL_ARTIFICIAL (decl))
21127     add_AT_flag (decl_die, DW_AT_artificial, 1);
21128
21129   add_accessibility_attribute (decl_die, decl);
21130
21131   /* Equate decl number to die, so that we can look up this decl later on.  */
21132   equate_decl_number_to_die (decl, decl_die);
21133 }
21134
21135 #if 0
21136 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21137    Use modified_type_die instead.
21138    We keep this code here just in case these types of DIEs may be needed to
21139    represent certain things in other languages (e.g. Pascal) someday.  */
21140
21141 static void
21142 gen_pointer_type_die (tree type, dw_die_ref context_die)
21143 {
21144   dw_die_ref ptr_die
21145     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21146
21147   equate_type_number_to_die (type, ptr_die);
21148   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
21149   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21150 }
21151
21152 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21153    Use modified_type_die instead.
21154    We keep this code here just in case these types of DIEs may be needed to
21155    represent certain things in other languages (e.g. Pascal) someday.  */
21156
21157 static void
21158 gen_reference_type_die (tree type, dw_die_ref context_die)
21159 {
21160   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21161
21162   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21163     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21164   else
21165     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21166
21167   equate_type_number_to_die (type, ref_die);
21168   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
21169   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21170 }
21171 #endif
21172
21173 /* Generate a DIE for a pointer to a member type.  */
21174
21175 static void
21176 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21177 {
21178   dw_die_ref ptr_die
21179     = new_die (DW_TAG_ptr_to_member_type,
21180                scope_die_for (type, context_die), type);
21181
21182   equate_type_number_to_die (type, ptr_die);
21183   add_AT_die_ref (ptr_die, DW_AT_containing_type,
21184                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21185   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
21186 }
21187
21188 /* Generate the DIE for the compilation unit.  */
21189
21190 static dw_die_ref
21191 gen_compile_unit_die (const char *filename)
21192 {
21193   dw_die_ref die;
21194   char producer[250];
21195   const char *language_string = lang_hooks.name;
21196   int language;
21197
21198   die = new_die (DW_TAG_compile_unit, NULL, NULL);
21199
21200   if (filename)
21201     {
21202       add_name_attribute (die, filename);
21203       /* Don't add cwd for <built-in>.  */
21204       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
21205         add_comp_dir_attribute (die);
21206     }
21207
21208   sprintf (producer, "%s %s", language_string, version_string);
21209
21210 #ifdef MIPS_DEBUGGING_INFO
21211   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
21212      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
21213      not appear in the producer string, the debugger reaches the conclusion
21214      that the object file is stripped and has no debugging information.
21215      To get the MIPS/SGI debugger to believe that there is debugging
21216      information in the object file, we add a -g to the producer string.  */
21217   if (debug_info_level > DINFO_LEVEL_TERSE)
21218     strcat (producer, " -g");
21219 #endif
21220
21221   add_AT_string (die, DW_AT_producer, producer);
21222
21223   /* If our producer is LTO try to figure out a common language to use
21224      from the global list of translation units.  */
21225   if (strcmp (language_string, "GNU GIMPLE") == 0)
21226     {
21227       unsigned i;
21228       tree t;
21229       const char *common_lang = NULL;
21230
21231       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
21232         {
21233           if (!TRANSLATION_UNIT_LANGUAGE (t))
21234             continue;
21235           if (!common_lang)
21236             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
21237           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
21238             ;
21239           else if (strncmp (common_lang, "GNU C", 5) == 0
21240                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
21241             /* Mixing C and C++ is ok, use C++ in that case.  */
21242             common_lang = "GNU C++";
21243           else
21244             {
21245               /* Fall back to C.  */
21246               common_lang = NULL;
21247               break;
21248             }
21249         }
21250
21251       if (common_lang)
21252         language_string = common_lang;
21253     }
21254
21255   language = DW_LANG_C89;
21256   if (strcmp (language_string, "GNU C++") == 0)
21257     language = DW_LANG_C_plus_plus;
21258   else if (strcmp (language_string, "GNU F77") == 0)
21259     language = DW_LANG_Fortran77;
21260   else if (strcmp (language_string, "GNU Pascal") == 0)
21261     language = DW_LANG_Pascal83;
21262   else if (dwarf_version >= 3 || !dwarf_strict)
21263     {
21264       if (strcmp (language_string, "GNU Ada") == 0)
21265         language = DW_LANG_Ada95;
21266       else if (strcmp (language_string, "GNU Fortran") == 0)
21267         language = DW_LANG_Fortran95;
21268       else if (strcmp (language_string, "GNU Java") == 0)
21269         language = DW_LANG_Java;
21270       else if (strcmp (language_string, "GNU Objective-C") == 0)
21271         language = DW_LANG_ObjC;
21272       else if (strcmp (language_string, "GNU Objective-C++") == 0)
21273         language = DW_LANG_ObjC_plus_plus;
21274     }
21275
21276   add_AT_unsigned (die, DW_AT_language, language);
21277
21278   switch (language)
21279     {
21280     case DW_LANG_Fortran77:
21281     case DW_LANG_Fortran90:
21282     case DW_LANG_Fortran95:
21283       /* Fortran has case insensitive identifiers and the front-end
21284          lowercases everything.  */
21285       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
21286       break;
21287     default:
21288       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
21289       break;
21290     }
21291   return die;
21292 }
21293
21294 /* Generate the DIE for a base class.  */
21295
21296 static void
21297 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
21298 {
21299   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
21300
21301   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
21302   add_data_member_location_attribute (die, binfo);
21303
21304   if (BINFO_VIRTUAL_P (binfo))
21305     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21306
21307   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
21308      children, otherwise the default is DW_ACCESS_public.  In DWARF2
21309      the default has always been DW_ACCESS_private.  */
21310   if (access == access_public_node)
21311     {
21312       if (dwarf_version == 2
21313           || context_die->die_tag == DW_TAG_class_type)
21314       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
21315     }
21316   else if (access == access_protected_node)
21317     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
21318   else if (dwarf_version > 2
21319            && context_die->die_tag != DW_TAG_class_type)
21320     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
21321 }
21322
21323 /* Generate a DIE for a class member.  */
21324
21325 static void
21326 gen_member_die (tree type, dw_die_ref context_die)
21327 {
21328   tree member;
21329   tree binfo = TYPE_BINFO (type);
21330   dw_die_ref child;
21331
21332   /* If this is not an incomplete type, output descriptions of each of its
21333      members. Note that as we output the DIEs necessary to represent the
21334      members of this record or union type, we will also be trying to output
21335      DIEs to represent the *types* of those members. However the `type'
21336      function (above) will specifically avoid generating type DIEs for member
21337      types *within* the list of member DIEs for this (containing) type except
21338      for those types (of members) which are explicitly marked as also being
21339      members of this (containing) type themselves.  The g++ front- end can
21340      force any given type to be treated as a member of some other (containing)
21341      type by setting the TYPE_CONTEXT of the given (member) type to point to
21342      the TREE node representing the appropriate (containing) type.  */
21343
21344   /* First output info about the base classes.  */
21345   if (binfo)
21346     {
21347       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
21348       int i;
21349       tree base;
21350
21351       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
21352         gen_inheritance_die (base,
21353                              (accesses ? VEC_index (tree, accesses, i)
21354                               : access_public_node), context_die);
21355     }
21356
21357   /* Now output info about the data members and type members.  */
21358   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
21359     {
21360       /* If we thought we were generating minimal debug info for TYPE
21361          and then changed our minds, some of the member declarations
21362          may have already been defined.  Don't define them again, but
21363          do put them in the right order.  */
21364
21365       child = lookup_decl_die (member);
21366       if (child)
21367         splice_child_die (context_die, child);
21368       else
21369         gen_decl_die (member, NULL, context_die);
21370     }
21371
21372   /* Now output info about the function members (if any).  */
21373   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
21374     {
21375       /* Don't include clones in the member list.  */
21376       if (DECL_ABSTRACT_ORIGIN (member))
21377         continue;
21378
21379       child = lookup_decl_die (member);
21380       if (child)
21381         splice_child_die (context_die, child);
21382       else
21383         gen_decl_die (member, NULL, context_die);
21384     }
21385 }
21386
21387 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
21388    is set, we pretend that the type was never defined, so we only get the
21389    member DIEs needed by later specification DIEs.  */
21390
21391 static void
21392 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
21393                                 enum debug_info_usage usage)
21394 {
21395   dw_die_ref type_die = lookup_type_die (type);
21396   dw_die_ref scope_die = 0;
21397   int nested = 0;
21398   int complete = (TYPE_SIZE (type)
21399                   && (! TYPE_STUB_DECL (type)
21400                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
21401   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
21402   complete = complete && should_emit_struct_debug (type, usage);
21403
21404   if (type_die && ! complete)
21405     return;
21406
21407   if (TYPE_CONTEXT (type) != NULL_TREE
21408       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
21409           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
21410     nested = 1;
21411
21412   scope_die = scope_die_for (type, context_die);
21413
21414   if (! type_die || (nested && is_cu_die (scope_die)))
21415     /* First occurrence of type or toplevel definition of nested class.  */
21416     {
21417       dw_die_ref old_die = type_die;
21418
21419       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
21420                           ? record_type_tag (type) : DW_TAG_union_type,
21421                           scope_die, type);
21422       equate_type_number_to_die (type, type_die);
21423       if (old_die)
21424         add_AT_specification (type_die, old_die);
21425       else
21426         {
21427           add_name_attribute (type_die, type_tag (type));
21428           add_gnat_descriptive_type_attribute (type_die, type, context_die);
21429         }
21430     }
21431   else
21432     remove_AT (type_die, DW_AT_declaration);
21433
21434   /* Generate child dies for template paramaters.  */
21435   if (debug_info_level > DINFO_LEVEL_TERSE
21436       && COMPLETE_TYPE_P (type))
21437     schedule_generic_params_dies_gen (type);
21438
21439   /* If this type has been completed, then give it a byte_size attribute and
21440      then give a list of members.  */
21441   if (complete && !ns_decl)
21442     {
21443       /* Prevent infinite recursion in cases where the type of some member of
21444          this type is expressed in terms of this type itself.  */
21445       TREE_ASM_WRITTEN (type) = 1;
21446       add_byte_size_attribute (type_die, type);
21447       if (TYPE_STUB_DECL (type) != NULL_TREE)
21448         {
21449           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21450           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21451         }
21452
21453       /* If the first reference to this type was as the return type of an
21454          inline function, then it may not have a parent.  Fix this now.  */
21455       if (type_die->die_parent == NULL)
21456         add_child_die (scope_die, type_die);
21457
21458       push_decl_scope (type);
21459       gen_member_die (type, type_die);
21460       pop_decl_scope ();
21461
21462       /* GNU extension: Record what type our vtable lives in.  */
21463       if (TYPE_VFIELD (type))
21464         {
21465           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
21466
21467           gen_type_die (vtype, context_die);
21468           add_AT_die_ref (type_die, DW_AT_containing_type,
21469                           lookup_type_die (vtype));
21470         }
21471     }
21472   else
21473     {
21474       add_AT_flag (type_die, DW_AT_declaration, 1);
21475
21476       /* We don't need to do this for function-local types.  */
21477       if (TYPE_STUB_DECL (type)
21478           && ! decl_function_context (TYPE_STUB_DECL (type)))
21479         VEC_safe_push (tree, gc, incomplete_types, type);
21480     }
21481
21482   if (get_AT (type_die, DW_AT_name))
21483     add_pubtype (type, type_die);
21484 }
21485
21486 /* Generate a DIE for a subroutine _type_.  */
21487
21488 static void
21489 gen_subroutine_type_die (tree type, dw_die_ref context_die)
21490 {
21491   tree return_type = TREE_TYPE (type);
21492   dw_die_ref subr_die
21493     = new_die (DW_TAG_subroutine_type,
21494                scope_die_for (type, context_die), type);
21495
21496   equate_type_number_to_die (type, subr_die);
21497   add_prototyped_attribute (subr_die, type);
21498   add_type_attribute (subr_die, return_type, 0, 0, context_die);
21499   gen_formal_types_die (type, subr_die);
21500
21501   if (get_AT (subr_die, DW_AT_name))
21502     add_pubtype (type, subr_die);
21503 }
21504
21505 /* Generate a DIE for a type definition.  */
21506
21507 static void
21508 gen_typedef_die (tree decl, dw_die_ref context_die)
21509 {
21510   dw_die_ref type_die;
21511   tree origin;
21512
21513   if (TREE_ASM_WRITTEN (decl))
21514     return;
21515
21516   TREE_ASM_WRITTEN (decl) = 1;
21517   type_die = new_die (DW_TAG_typedef, context_die, decl);
21518   origin = decl_ultimate_origin (decl);
21519   if (origin != NULL)
21520     add_abstract_origin_attribute (type_die, origin);
21521   else
21522     {
21523       tree type;
21524
21525       add_name_and_src_coords_attributes (type_die, decl);
21526       if (DECL_ORIGINAL_TYPE (decl))
21527         {
21528           type = DECL_ORIGINAL_TYPE (decl);
21529
21530           gcc_assert (type != TREE_TYPE (decl));
21531           equate_type_number_to_die (TREE_TYPE (decl), type_die);
21532         }
21533       else
21534         {
21535           type = TREE_TYPE (decl);
21536
21537           if (is_naming_typedef_decl (TYPE_NAME (type)))
21538             {
21539               /* Here, we are in the case of decl being a typedef naming
21540                  an anonymous type, e.g:
21541                      typedef struct {...} foo;
21542                  In that case TREE_TYPE (decl) is not a typedef variant
21543                  type and TYPE_NAME of the anonymous type is set to the
21544                  TYPE_DECL of the typedef. This construct is emitted by
21545                  the C++ FE.
21546
21547                  TYPE is the anonymous struct named by the typedef
21548                  DECL. As we need the DW_AT_type attribute of the
21549                  DW_TAG_typedef to point to the DIE of TYPE, let's
21550                  generate that DIE right away. add_type_attribute
21551                  called below will then pick (via lookup_type_die) that
21552                  anonymous struct DIE.  */
21553               if (!TREE_ASM_WRITTEN (type))
21554                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
21555
21556               /* This is a GNU Extension.  We are adding a
21557                  DW_AT_linkage_name attribute to the DIE of the
21558                  anonymous struct TYPE.  The value of that attribute
21559                  is the name of the typedef decl naming the anonymous
21560                  struct.  This greatly eases the work of consumers of
21561                  this debug info.  */
21562               add_linkage_attr (lookup_type_die (type), decl);
21563             }
21564         }
21565
21566       add_type_attribute (type_die, type, TREE_READONLY (decl),
21567                           TREE_THIS_VOLATILE (decl), context_die);
21568
21569       if (is_naming_typedef_decl (decl))
21570         /* We want that all subsequent calls to lookup_type_die with
21571            TYPE in argument yield the DW_TAG_typedef we have just
21572            created.  */
21573         equate_type_number_to_die (type, type_die);
21574
21575       add_accessibility_attribute (type_die, decl);
21576     }
21577
21578   if (DECL_ABSTRACT (decl))
21579     equate_decl_number_to_die (decl, type_die);
21580
21581   if (get_AT (type_die, DW_AT_name))
21582     add_pubtype (decl, type_die);
21583 }
21584
21585 /* Generate a DIE for a struct, class, enum or union type.  */
21586
21587 static void
21588 gen_tagged_type_die (tree type,
21589                      dw_die_ref context_die,
21590                      enum debug_info_usage usage)
21591 {
21592   int need_pop;
21593
21594   if (type == NULL_TREE
21595       || !is_tagged_type (type))
21596     return;
21597
21598   /* If this is a nested type whose containing class hasn't been written
21599      out yet, writing it out will cover this one, too.  This does not apply
21600      to instantiations of member class templates; they need to be added to
21601      the containing class as they are generated.  FIXME: This hurts the
21602      idea of combining type decls from multiple TUs, since we can't predict
21603      what set of template instantiations we'll get.  */
21604   if (TYPE_CONTEXT (type)
21605       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
21606       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
21607     {
21608       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
21609
21610       if (TREE_ASM_WRITTEN (type))
21611         return;
21612
21613       /* If that failed, attach ourselves to the stub.  */
21614       push_decl_scope (TYPE_CONTEXT (type));
21615       context_die = lookup_type_die (TYPE_CONTEXT (type));
21616       need_pop = 1;
21617     }
21618   else if (TYPE_CONTEXT (type) != NULL_TREE
21619            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
21620     {
21621       /* If this type is local to a function that hasn't been written
21622          out yet, use a NULL context for now; it will be fixed up in
21623          decls_for_scope.  */
21624       context_die = lookup_decl_die (TYPE_CONTEXT (type));
21625       /* A declaration DIE doesn't count; nested types need to go in the
21626          specification.  */
21627       if (context_die && is_declaration_die (context_die))
21628         context_die = NULL;
21629       need_pop = 0;
21630     }
21631   else
21632     {
21633       context_die = declare_in_namespace (type, context_die);
21634       need_pop = 0;
21635     }
21636
21637   if (TREE_CODE (type) == ENUMERAL_TYPE)
21638     {
21639       /* This might have been written out by the call to
21640          declare_in_namespace.  */
21641       if (!TREE_ASM_WRITTEN (type))
21642         gen_enumeration_type_die (type, context_die);
21643     }
21644   else
21645     gen_struct_or_union_type_die (type, context_die, usage);
21646
21647   if (need_pop)
21648     pop_decl_scope ();
21649
21650   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
21651      it up if it is ever completed.  gen_*_type_die will set it for us
21652      when appropriate.  */
21653 }
21654
21655 /* Generate a type description DIE.  */
21656
21657 static void
21658 gen_type_die_with_usage (tree type, dw_die_ref context_die,
21659                          enum debug_info_usage usage)
21660 {
21661   struct array_descr_info info;
21662
21663   if (type == NULL_TREE || type == error_mark_node)
21664     return;
21665
21666   if (TYPE_NAME (type) != NULL_TREE
21667       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21668       && is_redundant_typedef (TYPE_NAME (type))
21669       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
21670     /* The DECL of this type is a typedef we don't want to emit debug
21671        info for but we want debug info for its underlying typedef.
21672        This can happen for e.g, the injected-class-name of a C++
21673        type.  */
21674     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
21675
21676   /* If TYPE is a typedef type variant, let's generate debug info
21677      for the parent typedef which TYPE is a type of.  */
21678   if (typedef_variant_p (type))
21679     {
21680       if (TREE_ASM_WRITTEN (type))
21681         return;
21682
21683       /* Prevent broken recursion; we can't hand off to the same type.  */
21684       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
21685
21686       /* Use the DIE of the containing namespace as the parent DIE of
21687          the type description DIE we want to generate.  */
21688       if (DECL_CONTEXT (TYPE_NAME (type))
21689           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21690         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21691
21692       TREE_ASM_WRITTEN (type) = 1;
21693
21694       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21695       return;
21696     }
21697
21698   /* If type is an anonymous tagged type named by a typedef, let's
21699      generate debug info for the typedef.  */
21700   if (is_naming_typedef_decl (TYPE_NAME (type)))
21701     {
21702       /* Use the DIE of the containing namespace as the parent DIE of
21703          the type description DIE we want to generate.  */
21704       if (DECL_CONTEXT (TYPE_NAME (type))
21705           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21706         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21707       
21708       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21709       return;
21710     }
21711
21712   /* If this is an array type with hidden descriptor, handle it first.  */
21713   if (!TREE_ASM_WRITTEN (type)
21714       && lang_hooks.types.get_array_descr_info
21715       && lang_hooks.types.get_array_descr_info (type, &info)
21716       && (dwarf_version >= 3 || !dwarf_strict))
21717     {
21718       gen_descr_array_type_die (type, &info, context_die);
21719       TREE_ASM_WRITTEN (type) = 1;
21720       return;
21721     }
21722
21723   /* We are going to output a DIE to represent the unqualified version
21724      of this type (i.e. without any const or volatile qualifiers) so
21725      get the main variant (i.e. the unqualified version) of this type
21726      now.  (Vectors are special because the debugging info is in the
21727      cloned type itself).  */
21728   if (TREE_CODE (type) != VECTOR_TYPE)
21729     type = type_main_variant (type);
21730
21731   if (TREE_ASM_WRITTEN (type))
21732     return;
21733
21734   switch (TREE_CODE (type))
21735     {
21736     case ERROR_MARK:
21737       break;
21738
21739     case POINTER_TYPE:
21740     case REFERENCE_TYPE:
21741       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
21742          ensures that the gen_type_die recursion will terminate even if the
21743          type is recursive.  Recursive types are possible in Ada.  */
21744       /* ??? We could perhaps do this for all types before the switch
21745          statement.  */
21746       TREE_ASM_WRITTEN (type) = 1;
21747
21748       /* For these types, all that is required is that we output a DIE (or a
21749          set of DIEs) to represent the "basis" type.  */
21750       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21751                                 DINFO_USAGE_IND_USE);
21752       break;
21753
21754     case OFFSET_TYPE:
21755       /* This code is used for C++ pointer-to-data-member types.
21756          Output a description of the relevant class type.  */
21757       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
21758                                         DINFO_USAGE_IND_USE);
21759
21760       /* Output a description of the type of the object pointed to.  */
21761       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21762                                         DINFO_USAGE_IND_USE);
21763
21764       /* Now output a DIE to represent this pointer-to-data-member type
21765          itself.  */
21766       gen_ptr_to_mbr_type_die (type, context_die);
21767       break;
21768
21769     case FUNCTION_TYPE:
21770       /* Force out return type (in case it wasn't forced out already).  */
21771       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21772                                         DINFO_USAGE_DIR_USE);
21773       gen_subroutine_type_die (type, context_die);
21774       break;
21775
21776     case METHOD_TYPE:
21777       /* Force out return type (in case it wasn't forced out already).  */
21778       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21779                                         DINFO_USAGE_DIR_USE);
21780       gen_subroutine_type_die (type, context_die);
21781       break;
21782
21783     case ARRAY_TYPE:
21784       gen_array_type_die (type, context_die);
21785       break;
21786
21787     case VECTOR_TYPE:
21788       gen_array_type_die (type, context_die);
21789       break;
21790
21791     case ENUMERAL_TYPE:
21792     case RECORD_TYPE:
21793     case UNION_TYPE:
21794     case QUAL_UNION_TYPE:
21795       gen_tagged_type_die (type, context_die, usage);
21796       return;
21797
21798     case VOID_TYPE:
21799     case INTEGER_TYPE:
21800     case REAL_TYPE:
21801     case FIXED_POINT_TYPE:
21802     case COMPLEX_TYPE:
21803     case BOOLEAN_TYPE:
21804       /* No DIEs needed for fundamental types.  */
21805       break;
21806
21807     case NULLPTR_TYPE:
21808     case LANG_TYPE:
21809       /* Just use DW_TAG_unspecified_type.  */
21810       {
21811         dw_die_ref type_die = lookup_type_die (type);
21812         if (type_die == NULL)
21813           {
21814             tree name = TYPE_NAME (type);
21815             if (TREE_CODE (name) == TYPE_DECL)
21816               name = DECL_NAME (name);
21817             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
21818             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
21819             equate_type_number_to_die (type, type_die);
21820           }
21821       }
21822       break;
21823
21824     default:
21825       gcc_unreachable ();
21826     }
21827
21828   TREE_ASM_WRITTEN (type) = 1;
21829 }
21830
21831 static void
21832 gen_type_die (tree type, dw_die_ref context_die)
21833 {
21834   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
21835 }
21836
21837 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
21838    things which are local to the given block.  */
21839
21840 static void
21841 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
21842 {
21843   int must_output_die = 0;
21844   bool inlined_func;
21845
21846   /* Ignore blocks that are NULL.  */
21847   if (stmt == NULL_TREE)
21848     return;
21849
21850   inlined_func = inlined_function_outer_scope_p (stmt);
21851
21852   /* If the block is one fragment of a non-contiguous block, do not
21853      process the variables, since they will have been done by the
21854      origin block.  Do process subblocks.  */
21855   if (BLOCK_FRAGMENT_ORIGIN (stmt))
21856     {
21857       tree sub;
21858
21859       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
21860         gen_block_die (sub, context_die, depth + 1);
21861
21862       return;
21863     }
21864
21865   /* Determine if we need to output any Dwarf DIEs at all to represent this
21866      block.  */
21867   if (inlined_func)
21868     /* The outer scopes for inlinings *must* always be represented.  We
21869        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
21870     must_output_die = 1;
21871   else
21872     {
21873       /* Determine if this block directly contains any "significant"
21874          local declarations which we will need to output DIEs for.  */
21875       if (debug_info_level > DINFO_LEVEL_TERSE)
21876         /* We are not in terse mode so *any* local declaration counts
21877            as being a "significant" one.  */
21878         must_output_die = ((BLOCK_VARS (stmt) != NULL
21879                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
21880                            && (TREE_USED (stmt)
21881                                || TREE_ASM_WRITTEN (stmt)
21882                                || BLOCK_ABSTRACT (stmt)));
21883       else if ((TREE_USED (stmt)
21884                 || TREE_ASM_WRITTEN (stmt)
21885                 || BLOCK_ABSTRACT (stmt))
21886                && !dwarf2out_ignore_block (stmt))
21887         must_output_die = 1;
21888     }
21889
21890   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
21891      DIE for any block which contains no significant local declarations at
21892      all.  Rather, in such cases we just call `decls_for_scope' so that any
21893      needed Dwarf info for any sub-blocks will get properly generated. Note
21894      that in terse mode, our definition of what constitutes a "significant"
21895      local declaration gets restricted to include only inlined function
21896      instances and local (nested) function definitions.  */
21897   if (must_output_die)
21898     {
21899       if (inlined_func)
21900         {
21901           /* If STMT block is abstract, that means we have been called
21902              indirectly from dwarf2out_abstract_function.
21903              That function rightfully marks the descendent blocks (of
21904              the abstract function it is dealing with) as being abstract,
21905              precisely to prevent us from emitting any
21906              DW_TAG_inlined_subroutine DIE as a descendent
21907              of an abstract function instance. So in that case, we should
21908              not call gen_inlined_subroutine_die.
21909
21910              Later though, when cgraph asks dwarf2out to emit info
21911              for the concrete instance of the function decl into which
21912              the concrete instance of STMT got inlined, the later will lead
21913              to the generation of a DW_TAG_inlined_subroutine DIE.  */
21914           if (! BLOCK_ABSTRACT (stmt))
21915             gen_inlined_subroutine_die (stmt, context_die, depth);
21916         }
21917       else
21918         gen_lexical_block_die (stmt, context_die, depth);
21919     }
21920   else
21921     decls_for_scope (stmt, context_die, depth);
21922 }
21923
21924 /* Process variable DECL (or variable with origin ORIGIN) within
21925    block STMT and add it to CONTEXT_DIE.  */
21926 static void
21927 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21928 {
21929   dw_die_ref die;
21930   tree decl_or_origin = decl ? decl : origin;
21931
21932   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21933     die = lookup_decl_die (decl_or_origin);
21934   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21935            && TYPE_DECL_IS_STUB (decl_or_origin))
21936     die = lookup_type_die (TREE_TYPE (decl_or_origin));
21937   else
21938     die = NULL;
21939
21940   if (die != NULL && die->die_parent == NULL)
21941     add_child_die (context_die, die);
21942   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21943     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21944                                          stmt, context_die);
21945   else
21946     gen_decl_die (decl, origin, context_die);
21947 }
21948
21949 /* Generate all of the decls declared within a given scope and (recursively)
21950    all of its sub-blocks.  */
21951
21952 static void
21953 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
21954 {
21955   tree decl;
21956   unsigned int i;
21957   tree subblocks;
21958
21959   /* Ignore NULL blocks.  */
21960   if (stmt == NULL_TREE)
21961     return;
21962
21963   /* Output the DIEs to represent all of the data objects and typedefs
21964      declared directly within this block but not within any nested
21965      sub-blocks.  Also, nested function and tag DIEs have been
21966      generated with a parent of NULL; fix that up now.  */
21967   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21968     process_scope_var (stmt, decl, NULL_TREE, context_die);
21969   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21970     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21971                        context_die);
21972
21973   /* If we're at -g1, we're not interested in subblocks.  */
21974   if (debug_info_level <= DINFO_LEVEL_TERSE)
21975     return;
21976
21977   /* Output the DIEs to represent all sub-blocks (and the items declared
21978      therein) of this block.  */
21979   for (subblocks = BLOCK_SUBBLOCKS (stmt);
21980        subblocks != NULL;
21981        subblocks = BLOCK_CHAIN (subblocks))
21982     gen_block_die (subblocks, context_die, depth + 1);
21983 }
21984
21985 /* Is this a typedef we can avoid emitting?  */
21986
21987 static inline int
21988 is_redundant_typedef (const_tree decl)
21989 {
21990   if (TYPE_DECL_IS_STUB (decl))
21991     return 1;
21992
21993   if (DECL_ARTIFICIAL (decl)
21994       && DECL_CONTEXT (decl)
21995       && is_tagged_type (DECL_CONTEXT (decl))
21996       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21997       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21998     /* Also ignore the artificial member typedef for the class name.  */
21999     return 1;
22000
22001   return 0;
22002 }
22003
22004 /* Return TRUE if TYPE is a typedef that names a type for linkage
22005    purposes. This kind of typedefs is produced by the C++ FE for
22006    constructs like:
22007
22008    typedef struct {...} foo;
22009
22010    In that case, there is no typedef variant type produced for foo.
22011    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
22012    struct type.  */
22013
22014 static bool
22015 is_naming_typedef_decl (const_tree decl)
22016 {
22017   if (decl == NULL_TREE
22018       || TREE_CODE (decl) != TYPE_DECL
22019       || !is_tagged_type (TREE_TYPE (decl))
22020       || DECL_IS_BUILTIN (decl)
22021       || is_redundant_typedef (decl)
22022       /* It looks like Ada produces TYPE_DECLs that are very similar
22023          to C++ naming typedefs but that have different
22024          semantics. Let's be specific to c++ for now.  */
22025       || !is_cxx ())
22026     return FALSE;
22027
22028   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
22029           && TYPE_NAME (TREE_TYPE (decl)) == decl
22030           && (TYPE_STUB_DECL (TREE_TYPE (decl))
22031               != TYPE_NAME (TREE_TYPE (decl))));
22032 }
22033
22034 /* Returns the DIE for a context.  */
22035
22036 static inline dw_die_ref
22037 get_context_die (tree context)
22038 {
22039   if (context)
22040     {
22041       /* Find die that represents this context.  */
22042       if (TYPE_P (context))
22043         {
22044           context = TYPE_MAIN_VARIANT (context);
22045           return strip_naming_typedef (context, force_type_die (context));
22046         }
22047       else
22048         return force_decl_die (context);
22049     }
22050   return comp_unit_die ();
22051 }
22052
22053 /* Returns the DIE for decl.  A DIE will always be returned.  */
22054
22055 static dw_die_ref
22056 force_decl_die (tree decl)
22057 {
22058   dw_die_ref decl_die;
22059   unsigned saved_external_flag;
22060   tree save_fn = NULL_TREE;
22061   decl_die = lookup_decl_die (decl);
22062   if (!decl_die)
22063     {
22064       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
22065
22066       decl_die = lookup_decl_die (decl);
22067       if (decl_die)
22068         return decl_die;
22069
22070       switch (TREE_CODE (decl))
22071         {
22072         case FUNCTION_DECL:
22073           /* Clear current_function_decl, so that gen_subprogram_die thinks
22074              that this is a declaration. At this point, we just want to force
22075              declaration die.  */
22076           save_fn = current_function_decl;
22077           current_function_decl = NULL_TREE;
22078           gen_subprogram_die (decl, context_die);
22079           current_function_decl = save_fn;
22080           break;
22081
22082         case VAR_DECL:
22083           /* Set external flag to force declaration die. Restore it after
22084            gen_decl_die() call.  */
22085           saved_external_flag = DECL_EXTERNAL (decl);
22086           DECL_EXTERNAL (decl) = 1;
22087           gen_decl_die (decl, NULL, context_die);
22088           DECL_EXTERNAL (decl) = saved_external_flag;
22089           break;
22090
22091         case NAMESPACE_DECL:
22092           if (dwarf_version >= 3 || !dwarf_strict)
22093             dwarf2out_decl (decl);
22094           else
22095             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
22096             decl_die = comp_unit_die ();
22097           break;
22098
22099         case TRANSLATION_UNIT_DECL:
22100           decl_die = comp_unit_die ();
22101           break;
22102
22103         default:
22104           gcc_unreachable ();
22105         }
22106
22107       /* We should be able to find the DIE now.  */
22108       if (!decl_die)
22109         decl_die = lookup_decl_die (decl);
22110       gcc_assert (decl_die);
22111     }
22112
22113   return decl_die;
22114 }
22115
22116 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
22117    always returned.  */
22118
22119 static dw_die_ref
22120 force_type_die (tree type)
22121 {
22122   dw_die_ref type_die;
22123
22124   type_die = lookup_type_die (type);
22125   if (!type_die)
22126     {
22127       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
22128
22129       type_die = modified_type_die (type, TYPE_READONLY (type),
22130                                     TYPE_VOLATILE (type), context_die);
22131       gcc_assert (type_die);
22132     }
22133   return type_die;
22134 }
22135
22136 /* Force out any required namespaces to be able to output DECL,
22137    and return the new context_die for it, if it's changed.  */
22138
22139 static dw_die_ref
22140 setup_namespace_context (tree thing, dw_die_ref context_die)
22141 {
22142   tree context = (DECL_P (thing)
22143                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
22144   if (context && TREE_CODE (context) == NAMESPACE_DECL)
22145     /* Force out the namespace.  */
22146     context_die = force_decl_die (context);
22147
22148   return context_die;
22149 }
22150
22151 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
22152    type) within its namespace, if appropriate.
22153
22154    For compatibility with older debuggers, namespace DIEs only contain
22155    declarations; all definitions are emitted at CU scope.  */
22156
22157 static dw_die_ref
22158 declare_in_namespace (tree thing, dw_die_ref context_die)
22159 {
22160   dw_die_ref ns_context;
22161
22162   if (debug_info_level <= DINFO_LEVEL_TERSE)
22163     return context_die;
22164
22165   /* If this decl is from an inlined function, then don't try to emit it in its
22166      namespace, as we will get confused.  It would have already been emitted
22167      when the abstract instance of the inline function was emitted anyways.  */
22168   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
22169     return context_die;
22170
22171   ns_context = setup_namespace_context (thing, context_die);
22172
22173   if (ns_context != context_die)
22174     {
22175       if (is_fortran ())
22176         return ns_context;
22177       if (DECL_P (thing))
22178         gen_decl_die (thing, NULL, ns_context);
22179       else
22180         gen_type_die (thing, ns_context);
22181     }
22182   return context_die;
22183 }
22184
22185 /* Generate a DIE for a namespace or namespace alias.  */
22186
22187 static void
22188 gen_namespace_die (tree decl, dw_die_ref context_die)
22189 {
22190   dw_die_ref namespace_die;
22191
22192   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
22193      they are an alias of.  */
22194   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
22195     {
22196       /* Output a real namespace or module.  */
22197       context_die = setup_namespace_context (decl, comp_unit_die ());
22198       namespace_die = new_die (is_fortran ()
22199                                ? DW_TAG_module : DW_TAG_namespace,
22200                                context_die, decl);
22201       /* For Fortran modules defined in different CU don't add src coords.  */
22202       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
22203         {
22204           const char *name = dwarf2_name (decl, 0);
22205           if (name)
22206             add_name_attribute (namespace_die, name);
22207         }
22208       else
22209         add_name_and_src_coords_attributes (namespace_die, decl);
22210       if (DECL_EXTERNAL (decl))
22211         add_AT_flag (namespace_die, DW_AT_declaration, 1);
22212       equate_decl_number_to_die (decl, namespace_die);
22213     }
22214   else
22215     {
22216       /* Output a namespace alias.  */
22217
22218       /* Force out the namespace we are an alias of, if necessary.  */
22219       dw_die_ref origin_die
22220         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
22221
22222       if (DECL_FILE_SCOPE_P (decl)
22223           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
22224         context_die = setup_namespace_context (decl, comp_unit_die ());
22225       /* Now create the namespace alias DIE.  */
22226       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
22227       add_name_and_src_coords_attributes (namespace_die, decl);
22228       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
22229       equate_decl_number_to_die (decl, namespace_die);
22230     }
22231 }
22232
22233 /* Generate Dwarf debug information for a decl described by DECL.
22234    The return value is currently only meaningful for PARM_DECLs,
22235    for all other decls it returns NULL.  */
22236
22237 static dw_die_ref
22238 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
22239 {
22240   tree decl_or_origin = decl ? decl : origin;
22241   tree class_origin = NULL, ultimate_origin;
22242
22243   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
22244     return NULL;
22245
22246   switch (TREE_CODE (decl_or_origin))
22247     {
22248     case ERROR_MARK:
22249       break;
22250
22251     case CONST_DECL:
22252       if (!is_fortran () && !is_ada ())
22253         {
22254           /* The individual enumerators of an enum type get output when we output
22255              the Dwarf representation of the relevant enum type itself.  */
22256           break;
22257         }
22258
22259       /* Emit its type.  */
22260       gen_type_die (TREE_TYPE (decl), context_die);
22261
22262       /* And its containing namespace.  */
22263       context_die = declare_in_namespace (decl, context_die);
22264
22265       gen_const_die (decl, context_die);
22266       break;
22267
22268     case FUNCTION_DECL:
22269       /* Don't output any DIEs to represent mere function declarations,
22270          unless they are class members or explicit block externs.  */
22271       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
22272           && DECL_FILE_SCOPE_P (decl_or_origin)
22273           && (current_function_decl == NULL_TREE
22274               || DECL_ARTIFICIAL (decl_or_origin)))
22275         break;
22276
22277 #if 0
22278       /* FIXME */
22279       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
22280          on local redeclarations of global functions.  That seems broken.  */
22281       if (current_function_decl != decl)
22282         /* This is only a declaration.  */;
22283 #endif
22284
22285       /* If we're emitting a clone, emit info for the abstract instance.  */
22286       if (origin || DECL_ORIGIN (decl) != decl)
22287         dwarf2out_abstract_function (origin
22288                                      ? DECL_ORIGIN (origin)
22289                                      : DECL_ABSTRACT_ORIGIN (decl));
22290
22291       /* If we're emitting an out-of-line copy of an inline function,
22292          emit info for the abstract instance and set up to refer to it.  */
22293       else if (cgraph_function_possibly_inlined_p (decl)
22294                && ! DECL_ABSTRACT (decl)
22295                && ! class_or_namespace_scope_p (context_die)
22296                /* dwarf2out_abstract_function won't emit a die if this is just
22297                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
22298                   that case, because that works only if we have a die.  */
22299                && DECL_INITIAL (decl) != NULL_TREE)
22300         {
22301           dwarf2out_abstract_function (decl);
22302           set_decl_origin_self (decl);
22303         }
22304
22305       /* Otherwise we're emitting the primary DIE for this decl.  */
22306       else if (debug_info_level > DINFO_LEVEL_TERSE)
22307         {
22308           /* Before we describe the FUNCTION_DECL itself, make sure that we
22309              have its containing type.  */
22310           if (!origin)
22311             origin = decl_class_context (decl);
22312           if (origin != NULL_TREE)
22313             gen_type_die (origin, context_die);
22314
22315           /* And its return type.  */
22316           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
22317
22318           /* And its virtual context.  */
22319           if (DECL_VINDEX (decl) != NULL_TREE)
22320             gen_type_die (DECL_CONTEXT (decl), context_die);
22321
22322           /* Make sure we have a member DIE for decl.  */
22323           if (origin != NULL_TREE)
22324             gen_type_die_for_member (origin, decl, context_die);
22325
22326           /* And its containing namespace.  */
22327           context_die = declare_in_namespace (decl, context_die);
22328         }
22329
22330       /* Now output a DIE to represent the function itself.  */
22331       if (decl)
22332         gen_subprogram_die (decl, context_die);
22333       break;
22334
22335     case TYPE_DECL:
22336       /* If we are in terse mode, don't generate any DIEs to represent any
22337          actual typedefs.  */
22338       if (debug_info_level <= DINFO_LEVEL_TERSE)
22339         break;
22340
22341       /* In the special case of a TYPE_DECL node representing the declaration
22342          of some type tag, if the given TYPE_DECL is marked as having been
22343          instantiated from some other (original) TYPE_DECL node (e.g. one which
22344          was generated within the original definition of an inline function) we
22345          used to generate a special (abbreviated) DW_TAG_structure_type,
22346          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
22347          should be actually referencing those DIEs, as variable DIEs with that
22348          type would be emitted already in the abstract origin, so it was always
22349          removed during unused type prunning.  Don't add anything in this
22350          case.  */
22351       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
22352         break;
22353
22354       if (is_redundant_typedef (decl))
22355         gen_type_die (TREE_TYPE (decl), context_die);
22356       else
22357         /* Output a DIE to represent the typedef itself.  */
22358         gen_typedef_die (decl, context_die);
22359       break;
22360
22361     case LABEL_DECL:
22362       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22363         gen_label_die (decl, context_die);
22364       break;
22365
22366     case VAR_DECL:
22367     case RESULT_DECL:
22368       /* If we are in terse mode, don't generate any DIEs to represent any
22369          variable declarations or definitions.  */
22370       if (debug_info_level <= DINFO_LEVEL_TERSE)
22371         break;
22372
22373       /* Output any DIEs that are needed to specify the type of this data
22374          object.  */
22375       if (decl_by_reference_p (decl_or_origin))
22376         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
22377       else
22378         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
22379
22380       /* And its containing type.  */
22381       class_origin = decl_class_context (decl_or_origin);
22382       if (class_origin != NULL_TREE)
22383         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
22384
22385       /* And its containing namespace.  */
22386       context_die = declare_in_namespace (decl_or_origin, context_die);
22387
22388       /* Now output the DIE to represent the data object itself.  This gets
22389          complicated because of the possibility that the VAR_DECL really
22390          represents an inlined instance of a formal parameter for an inline
22391          function.  */
22392       ultimate_origin = decl_ultimate_origin (decl_or_origin);
22393       if (ultimate_origin != NULL_TREE
22394           && TREE_CODE (ultimate_origin) == PARM_DECL)
22395         gen_formal_parameter_die (decl, origin,
22396                                   true /* Emit name attribute.  */,
22397                                   context_die);
22398       else
22399         gen_variable_die (decl, origin, context_die);
22400       break;
22401
22402     case FIELD_DECL:
22403       /* Ignore the nameless fields that are used to skip bits but handle C++
22404          anonymous unions and structs.  */
22405       if (DECL_NAME (decl) != NULL_TREE
22406           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
22407           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
22408         {
22409           gen_type_die (member_declared_type (decl), context_die);
22410           gen_field_die (decl, context_die);
22411         }
22412       break;
22413
22414     case PARM_DECL:
22415       if (DECL_BY_REFERENCE (decl_or_origin))
22416         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
22417       else
22418         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
22419       return gen_formal_parameter_die (decl, origin,
22420                                        true /* Emit name attribute.  */,
22421                                        context_die);
22422
22423     case NAMESPACE_DECL:
22424     case IMPORTED_DECL:
22425       if (dwarf_version >= 3 || !dwarf_strict)
22426         gen_namespace_die (decl, context_die);
22427       break;
22428
22429     default:
22430       /* Probably some frontend-internal decl.  Assume we don't care.  */
22431       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
22432       break;
22433     }
22434
22435   return NULL;
22436 }
22437 \f
22438 /* Output debug information for global decl DECL.  Called from toplev.c after
22439    compilation proper has finished.  */
22440
22441 static void
22442 dwarf2out_global_decl (tree decl)
22443 {
22444   /* Output DWARF2 information for file-scope tentative data object
22445      declarations, file-scope (extern) function declarations (which
22446      had no corresponding body) and file-scope tagged type declarations
22447      and definitions which have not yet been forced out.  */
22448   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
22449     dwarf2out_decl (decl);
22450 }
22451
22452 /* Output debug information for type decl DECL.  Called from toplev.c
22453    and from language front ends (to record built-in types).  */
22454 static void
22455 dwarf2out_type_decl (tree decl, int local)
22456 {
22457   if (!local)
22458     dwarf2out_decl (decl);
22459 }
22460
22461 /* Output debug information for imported module or decl DECL.
22462    NAME is non-NULL name in the lexical block if the decl has been renamed.
22463    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
22464    that DECL belongs to.
22465    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
22466 static void
22467 dwarf2out_imported_module_or_decl_1 (tree decl,
22468                                      tree name,
22469                                      tree lexical_block,
22470                                      dw_die_ref lexical_block_die)
22471 {
22472   expanded_location xloc;
22473   dw_die_ref imported_die = NULL;
22474   dw_die_ref at_import_die;
22475
22476   if (TREE_CODE (decl) == IMPORTED_DECL)
22477     {
22478       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
22479       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
22480       gcc_assert (decl);
22481     }
22482   else
22483     xloc = expand_location (input_location);
22484
22485   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
22486     {
22487       at_import_die = force_type_die (TREE_TYPE (decl));
22488       /* For namespace N { typedef void T; } using N::T; base_type_die
22489          returns NULL, but DW_TAG_imported_declaration requires
22490          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
22491       if (!at_import_die)
22492         {
22493           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
22494           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
22495           at_import_die = lookup_type_die (TREE_TYPE (decl));
22496           gcc_assert (at_import_die);
22497         }
22498     }
22499   else
22500     {
22501       at_import_die = lookup_decl_die (decl);
22502       if (!at_import_die)
22503         {
22504           /* If we're trying to avoid duplicate debug info, we may not have
22505              emitted the member decl for this field.  Emit it now.  */
22506           if (TREE_CODE (decl) == FIELD_DECL)
22507             {
22508               tree type = DECL_CONTEXT (decl);
22509
22510               if (TYPE_CONTEXT (type)
22511                   && TYPE_P (TYPE_CONTEXT (type))
22512                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
22513                                                 DINFO_USAGE_DIR_USE))
22514                 return;
22515               gen_type_die_for_member (type, decl,
22516                                        get_context_die (TYPE_CONTEXT (type)));
22517             }
22518           at_import_die = force_decl_die (decl);
22519         }
22520     }
22521
22522   if (TREE_CODE (decl) == NAMESPACE_DECL)
22523     {
22524       if (dwarf_version >= 3 || !dwarf_strict)
22525         imported_die = new_die (DW_TAG_imported_module,
22526                                 lexical_block_die,
22527                                 lexical_block);
22528       else
22529         return;
22530     }
22531   else
22532     imported_die = new_die (DW_TAG_imported_declaration,
22533                             lexical_block_die,
22534                             lexical_block);
22535
22536   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
22537   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
22538   if (name)
22539     add_AT_string (imported_die, DW_AT_name,
22540                    IDENTIFIER_POINTER (name));
22541   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
22542 }
22543
22544 /* Output debug information for imported module or decl DECL.
22545    NAME is non-NULL name in context if the decl has been renamed.
22546    CHILD is true if decl is one of the renamed decls as part of
22547    importing whole module.  */
22548
22549 static void
22550 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
22551                                    bool child)
22552 {
22553   /* dw_die_ref at_import_die;  */
22554   dw_die_ref scope_die;
22555
22556   if (debug_info_level <= DINFO_LEVEL_TERSE)
22557     return;
22558
22559   gcc_assert (decl);
22560
22561   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
22562      We need decl DIE for reference and scope die. First, get DIE for the decl
22563      itself.  */
22564
22565   /* Get the scope die for decl context. Use comp_unit_die for global module
22566      or decl. If die is not found for non globals, force new die.  */
22567   if (context
22568       && TYPE_P (context)
22569       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
22570     return;
22571
22572   if (!(dwarf_version >= 3 || !dwarf_strict))
22573     return;
22574
22575   scope_die = get_context_die (context);
22576
22577   if (child)
22578     {
22579       gcc_assert (scope_die->die_child);
22580       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
22581       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
22582       scope_die = scope_die->die_child;
22583     }
22584
22585   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
22586   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
22587
22588 }
22589
22590 /* Write the debugging output for DECL.  */
22591
22592 void
22593 dwarf2out_decl (tree decl)
22594 {
22595   dw_die_ref context_die = comp_unit_die ();
22596
22597   switch (TREE_CODE (decl))
22598     {
22599     case ERROR_MARK:
22600       return;
22601
22602     case FUNCTION_DECL:
22603       /* What we would really like to do here is to filter out all mere
22604          file-scope declarations of file-scope functions which are never
22605          referenced later within this translation unit (and keep all of ones
22606          that *are* referenced later on) but we aren't clairvoyant, so we have
22607          no idea which functions will be referenced in the future (i.e. later
22608          on within the current translation unit). So here we just ignore all
22609          file-scope function declarations which are not also definitions.  If
22610          and when the debugger needs to know something about these functions,
22611          it will have to hunt around and find the DWARF information associated
22612          with the definition of the function.
22613
22614          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
22615          nodes represent definitions and which ones represent mere
22616          declarations.  We have to check DECL_INITIAL instead. That's because
22617          the C front-end supports some weird semantics for "extern inline"
22618          function definitions.  These can get inlined within the current
22619          translation unit (and thus, we need to generate Dwarf info for their
22620          abstract instances so that the Dwarf info for the concrete inlined
22621          instances can have something to refer to) but the compiler never
22622          generates any out-of-lines instances of such things (despite the fact
22623          that they *are* definitions).
22624
22625          The important point is that the C front-end marks these "extern
22626          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
22627          them anyway. Note that the C++ front-end also plays some similar games
22628          for inline function definitions appearing within include files which
22629          also contain `#pragma interface' pragmas.  */
22630       if (DECL_INITIAL (decl) == NULL_TREE)
22631         return;
22632
22633       /* If we're a nested function, initially use a parent of NULL; if we're
22634          a plain function, this will be fixed up in decls_for_scope.  If
22635          we're a method, it will be ignored, since we already have a DIE.  */
22636       if (decl_function_context (decl)
22637           /* But if we're in terse mode, we don't care about scope.  */
22638           && debug_info_level > DINFO_LEVEL_TERSE)
22639         context_die = NULL;
22640       break;
22641
22642     case VAR_DECL:
22643       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
22644          declaration and if the declaration was never even referenced from
22645          within this entire compilation unit.  We suppress these DIEs in
22646          order to save space in the .debug section (by eliminating entries
22647          which are probably useless).  Note that we must not suppress
22648          block-local extern declarations (whether used or not) because that
22649          would screw-up the debugger's name lookup mechanism and cause it to
22650          miss things which really ought to be in scope at a given point.  */
22651       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
22652         return;
22653
22654       /* For local statics lookup proper context die.  */
22655       if (TREE_STATIC (decl) && decl_function_context (decl))
22656         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22657
22658       /* If we are in terse mode, don't generate any DIEs to represent any
22659          variable declarations or definitions.  */
22660       if (debug_info_level <= DINFO_LEVEL_TERSE)
22661         return;
22662       break;
22663
22664     case CONST_DECL:
22665       if (debug_info_level <= DINFO_LEVEL_TERSE)
22666         return;
22667       if (!is_fortran () && !is_ada ())
22668         return;
22669       if (TREE_STATIC (decl) && decl_function_context (decl))
22670         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22671       break;
22672
22673     case NAMESPACE_DECL:
22674     case IMPORTED_DECL:
22675       if (debug_info_level <= DINFO_LEVEL_TERSE)
22676         return;
22677       if (lookup_decl_die (decl) != NULL)
22678         return;
22679       break;
22680
22681     case TYPE_DECL:
22682       /* Don't emit stubs for types unless they are needed by other DIEs.  */
22683       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
22684         return;
22685
22686       /* Don't bother trying to generate any DIEs to represent any of the
22687          normal built-in types for the language we are compiling.  */
22688       if (DECL_IS_BUILTIN (decl))
22689         return;
22690
22691       /* If we are in terse mode, don't generate any DIEs for types.  */
22692       if (debug_info_level <= DINFO_LEVEL_TERSE)
22693         return;
22694
22695       /* If we're a function-scope tag, initially use a parent of NULL;
22696          this will be fixed up in decls_for_scope.  */
22697       if (decl_function_context (decl))
22698         context_die = NULL;
22699
22700       break;
22701
22702     default:
22703       return;
22704     }
22705
22706   gen_decl_die (decl, NULL, context_die);
22707 }
22708
22709 /* Write the debugging output for DECL.  */
22710
22711 static void
22712 dwarf2out_function_decl (tree decl)
22713 {
22714   dwarf2out_decl (decl);
22715   call_arg_locations = NULL;
22716   call_arg_loc_last = NULL;
22717   call_site_count = -1;
22718   tail_call_site_count = -1;
22719   VEC_free (dw_die_ref, heap, block_map);
22720   htab_empty (decl_loc_table);
22721   htab_empty (cached_dw_loc_list_table);
22722 }
22723
22724 /* Output a marker (i.e. a label) for the beginning of the generated code for
22725    a lexical block.  */
22726
22727 static void
22728 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
22729                        unsigned int blocknum)
22730 {
22731   switch_to_section (current_function_section ());
22732   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
22733 }
22734
22735 /* Output a marker (i.e. a label) for the end of the generated code for a
22736    lexical block.  */
22737
22738 static void
22739 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22740 {
22741   switch_to_section (current_function_section ());
22742   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22743 }
22744
22745 /* Returns nonzero if it is appropriate not to emit any debugging
22746    information for BLOCK, because it doesn't contain any instructions.
22747
22748    Don't allow this for blocks with nested functions or local classes
22749    as we would end up with orphans, and in the presence of scheduling
22750    we may end up calling them anyway.  */
22751
22752 static bool
22753 dwarf2out_ignore_block (const_tree block)
22754 {
22755   tree decl;
22756   unsigned int i;
22757
22758   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22759     if (TREE_CODE (decl) == FUNCTION_DECL
22760         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22761       return 0;
22762   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22763     {
22764       decl = BLOCK_NONLOCALIZED_VAR (block, i);
22765       if (TREE_CODE (decl) == FUNCTION_DECL
22766           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22767       return 0;
22768     }
22769
22770   return 1;
22771 }
22772
22773 /* Hash table routines for file_hash.  */
22774
22775 static int
22776 file_table_eq (const void *p1_p, const void *p2_p)
22777 {
22778   const struct dwarf_file_data *const p1 =
22779     (const struct dwarf_file_data *) p1_p;
22780   const char *const p2 = (const char *) p2_p;
22781   return filename_cmp (p1->filename, p2) == 0;
22782 }
22783
22784 static hashval_t
22785 file_table_hash (const void *p_p)
22786 {
22787   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
22788   return htab_hash_string (p->filename);
22789 }
22790
22791 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22792    dwarf2out.c) and return its "index".  The index of each (known) filename is
22793    just a unique number which is associated with only that one filename.  We
22794    need such numbers for the sake of generating labels (in the .debug_sfnames
22795    section) and references to those files numbers (in the .debug_srcinfo
22796    and.debug_macinfo sections).  If the filename given as an argument is not
22797    found in our current list, add it to the list and assign it the next
22798    available unique index number.  In order to speed up searches, we remember
22799    the index of the filename was looked up last.  This handles the majority of
22800    all searches.  */
22801
22802 static struct dwarf_file_data *
22803 lookup_filename (const char *file_name)
22804 {
22805   void ** slot;
22806   struct dwarf_file_data * created;
22807
22808   /* Check to see if the file name that was searched on the previous
22809      call matches this file name.  If so, return the index.  */
22810   if (file_table_last_lookup
22811       && (file_name == file_table_last_lookup->filename
22812           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
22813     return file_table_last_lookup;
22814
22815   /* Didn't match the previous lookup, search the table.  */
22816   slot = htab_find_slot_with_hash (file_table, file_name,
22817                                    htab_hash_string (file_name), INSERT);
22818   if (*slot)
22819     return (struct dwarf_file_data *) *slot;
22820
22821   created = ggc_alloc_dwarf_file_data ();
22822   created->filename = file_name;
22823   created->emitted_number = 0;
22824   *slot = created;
22825   return created;
22826 }
22827
22828 /* If the assembler will construct the file table, then translate the compiler
22829    internal file table number into the assembler file table number, and emit
22830    a .file directive if we haven't already emitted one yet.  The file table
22831    numbers are different because we prune debug info for unused variables and
22832    types, which may include filenames.  */
22833
22834 static int
22835 maybe_emit_file (struct dwarf_file_data * fd)
22836 {
22837   if (! fd->emitted_number)
22838     {
22839       if (last_emitted_file)
22840         fd->emitted_number = last_emitted_file->emitted_number + 1;
22841       else
22842         fd->emitted_number = 1;
22843       last_emitted_file = fd;
22844
22845       if (DWARF2_ASM_LINE_DEBUG_INFO)
22846         {
22847           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22848           output_quoted_string (asm_out_file,
22849                                 remap_debug_filename (fd->filename));
22850           fputc ('\n', asm_out_file);
22851         }
22852     }
22853
22854   return fd->emitted_number;
22855 }
22856
22857 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22858    That generation should happen after function debug info has been
22859    generated. The value of the attribute is the constant value of ARG.  */
22860
22861 static void
22862 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22863 {
22864   die_arg_entry entry;
22865
22866   if (!die || !arg)
22867     return;
22868
22869   if (!tmpl_value_parm_die_table)
22870     tmpl_value_parm_die_table
22871       = VEC_alloc (die_arg_entry, gc, 32);
22872
22873   entry.die = die;
22874   entry.arg = arg;
22875   VEC_safe_push (die_arg_entry, gc,
22876                  tmpl_value_parm_die_table,
22877                  &entry);
22878 }
22879
22880 /* Return TRUE if T is an instance of generic type, FALSE
22881    otherwise.  */
22882
22883 static bool
22884 generic_type_p (tree t)
22885 {
22886   if (t == NULL_TREE || !TYPE_P (t))
22887     return false;
22888   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22889 }
22890
22891 /* Schedule the generation of the generic parameter dies for the
22892   instance of generic type T. The proper generation itself is later
22893   done by gen_scheduled_generic_parms_dies. */
22894
22895 static void
22896 schedule_generic_params_dies_gen (tree t)
22897 {
22898   if (!generic_type_p (t))
22899     return;
22900
22901   if (generic_type_instances == NULL)
22902     generic_type_instances = VEC_alloc (tree, gc, 256);
22903
22904   VEC_safe_push (tree, gc, generic_type_instances, t);
22905 }
22906
22907 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22908    by append_entry_to_tmpl_value_parm_die_table. This function must
22909    be called after function DIEs have been generated.  */
22910
22911 static void
22912 gen_remaining_tmpl_value_param_die_attribute (void)
22913 {
22914   if (tmpl_value_parm_die_table)
22915     {
22916       unsigned i;
22917       die_arg_entry *e;
22918
22919       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
22920         tree_add_const_value_attribute (e->die, e->arg);
22921     }
22922 }
22923
22924 /* Generate generic parameters DIEs for instances of generic types
22925    that have been previously scheduled by
22926    schedule_generic_params_dies_gen. This function must be called
22927    after all the types of the CU have been laid out.  */
22928
22929 static void
22930 gen_scheduled_generic_parms_dies (void)
22931 {
22932   unsigned i;
22933   tree t;
22934
22935   if (generic_type_instances == NULL)
22936     return;
22937   
22938   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
22939     gen_generic_params_dies (t);
22940 }
22941
22942
22943 /* Replace DW_AT_name for the decl with name.  */
22944
22945 static void
22946 dwarf2out_set_name (tree decl, tree name)
22947 {
22948   dw_die_ref die;
22949   dw_attr_ref attr;
22950   const char *dname;
22951
22952   die = TYPE_SYMTAB_DIE (decl);
22953   if (!die)
22954     return;
22955
22956   dname = dwarf2_name (name, 0);
22957   if (!dname)
22958     return;
22959
22960   attr = get_AT (die, DW_AT_name);
22961   if (attr)
22962     {
22963       struct indirect_string_node *node;
22964
22965       node = find_AT_string (dname);
22966       /* replace the string.  */
22967       attr->dw_attr_val.v.val_str = node;
22968     }
22969
22970   else
22971     add_name_attribute (die, dname);
22972 }
22973
22974 /* Called by the final INSN scan whenever we see a var location.  We
22975    use it to drop labels in the right places, and throw the location in
22976    our lookup table.  */
22977
22978 static void
22979 dwarf2out_var_location (rtx loc_note)
22980 {
22981   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22982   struct var_loc_node *newloc;
22983   rtx next_real;
22984   static const char *last_label;
22985   static const char *last_postcall_label;
22986   static bool last_in_cold_section_p;
22987   tree decl;
22988   bool var_loc_p;
22989
22990   if (!NOTE_P (loc_note))
22991     {
22992       if (CALL_P (loc_note))
22993         {
22994           call_site_count++;
22995           if (SIBLING_CALL_P (loc_note))
22996             tail_call_site_count++;
22997         }
22998       return;
22999     }
23000
23001   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
23002   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
23003     return;
23004
23005   next_real = next_real_insn (loc_note);
23006
23007   /* If there are no instructions which would be affected by this note,
23008      don't do anything.  */
23009   if (var_loc_p
23010       && next_real == NULL_RTX
23011       && !NOTE_DURING_CALL_P (loc_note))
23012     return;
23013
23014   if (next_real == NULL_RTX)
23015     next_real = get_last_insn ();
23016
23017   /* If there were any real insns between note we processed last time
23018      and this note (or if it is the first note), clear
23019      last_{,postcall_}label so that they are not reused this time.  */
23020   if (last_var_location_insn == NULL_RTX
23021       || last_var_location_insn != next_real
23022       || last_in_cold_section_p != in_cold_section_p)
23023     {
23024       last_label = NULL;
23025       last_postcall_label = NULL;
23026     }
23027
23028   if (var_loc_p)
23029     {
23030       decl = NOTE_VAR_LOCATION_DECL (loc_note);
23031       newloc = add_var_loc_to_decl (decl, loc_note,
23032                                     NOTE_DURING_CALL_P (loc_note)
23033                                     ? last_postcall_label : last_label);
23034       if (newloc == NULL)
23035         return;
23036     }
23037   else
23038     {
23039       decl = NULL_TREE;
23040       newloc = NULL;
23041     }
23042
23043   /* If there were no real insns between note we processed last time
23044      and this note, use the label we emitted last time.  Otherwise
23045      create a new label and emit it.  */
23046   if (last_label == NULL)
23047     {
23048       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
23049       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
23050       loclabel_num++;
23051       last_label = ggc_strdup (loclabel);
23052     }
23053
23054   if (!var_loc_p)
23055     {
23056       struct call_arg_loc_node *ca_loc
23057         = ggc_alloc_cleared_call_arg_loc_node ();
23058       rtx prev = prev_real_insn (loc_note), x;
23059       ca_loc->call_arg_loc_note = loc_note;
23060       ca_loc->next = NULL;
23061       ca_loc->label = last_label;
23062       gcc_assert (prev
23063                   && (CALL_P (prev)
23064                       || (NONJUMP_INSN_P (prev)
23065                           && GET_CODE (PATTERN (prev)) == SEQUENCE
23066                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
23067       if (!CALL_P (prev))
23068         prev = XVECEXP (PATTERN (prev), 0, 0);
23069       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
23070       x = PATTERN (prev);
23071       if (GET_CODE (x) == PARALLEL)
23072         x = XVECEXP (x, 0, 0);
23073       if (GET_CODE (x) == SET)
23074         x = SET_SRC (x);
23075       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
23076         {
23077           x = XEXP (XEXP (x, 0), 0);
23078           if (GET_CODE (x) == SYMBOL_REF
23079               && SYMBOL_REF_DECL (x)
23080               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
23081             ca_loc->symbol_ref = x;
23082         }
23083       ca_loc->block = insn_scope (prev);
23084       if (call_arg_locations)
23085         call_arg_loc_last->next = ca_loc;
23086       else
23087         call_arg_locations = ca_loc;
23088       call_arg_loc_last = ca_loc;
23089     }
23090   else if (!NOTE_DURING_CALL_P (loc_note))
23091     newloc->label = last_label;
23092   else
23093     {
23094       if (!last_postcall_label)
23095         {
23096           sprintf (loclabel, "%s-1", last_label);
23097           last_postcall_label = ggc_strdup (loclabel);
23098         }
23099       newloc->label = last_postcall_label;
23100     }
23101
23102   last_var_location_insn = next_real;
23103   last_in_cold_section_p = in_cold_section_p;
23104 }
23105
23106 /* Note in one location list that text section has changed.  */
23107
23108 static int
23109 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
23110 {
23111   var_loc_list *list = (var_loc_list *) *slot;
23112   if (list->first)
23113     list->last_before_switch
23114       = list->last->next ? list->last->next : list->last;
23115   return 1;
23116 }
23117
23118 /* Note in all location lists that text section has changed.  */
23119
23120 static void
23121 var_location_switch_text_section (void)
23122 {
23123   if (decl_loc_table == NULL)
23124     return;
23125
23126   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
23127 }
23128
23129 /* Create a new line number table.  */
23130
23131 static dw_line_info_table *
23132 new_line_info_table (void)
23133 {
23134   dw_line_info_table *table;
23135
23136   table = ggc_alloc_cleared_dw_line_info_table_struct ();
23137   table->file_num = 1;
23138   table->line_num = 1;
23139   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
23140
23141   return table;
23142 }
23143
23144 /* Lookup the "current" table into which we emit line info, so
23145    that we don't have to do it for every source line.  */
23146
23147 static void
23148 set_cur_line_info_table (section *sec)
23149 {
23150   dw_line_info_table *table;
23151
23152   if (sec == text_section)
23153     table = text_section_line_info;
23154   else if (sec == cold_text_section)
23155     {
23156       table = cold_text_section_line_info;
23157       if (!table)
23158         {
23159           cold_text_section_line_info = table = new_line_info_table ();
23160           table->end_label = cold_end_label;
23161         }
23162     }
23163   else
23164     {
23165       const char *end_label;
23166
23167       if (flag_reorder_blocks_and_partition)
23168         {
23169           if (in_cold_section_p)
23170             end_label = crtl->subsections.cold_section_end_label;
23171           else
23172             end_label = crtl->subsections.hot_section_end_label;
23173         }
23174       else
23175         {
23176           char label[MAX_ARTIFICIAL_LABEL_BYTES];
23177           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
23178                                        current_function_funcdef_no);
23179           end_label = ggc_strdup (label);
23180         }
23181
23182       table = new_line_info_table ();
23183       table->end_label = end_label;
23184
23185       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
23186     }
23187
23188   cur_line_info_table = table;
23189 }
23190
23191
23192 /* We need to reset the locations at the beginning of each
23193    function. We can't do this in the end_function hook, because the
23194    declarations that use the locations won't have been output when
23195    that hook is called.  Also compute have_multiple_function_sections here.  */
23196
23197 static void
23198 dwarf2out_begin_function (tree fun)
23199 {
23200   section *sec = function_section (fun);
23201
23202   if (sec != text_section)
23203     have_multiple_function_sections = true;
23204
23205   if (flag_reorder_blocks_and_partition && !cold_text_section)
23206     {
23207       gcc_assert (current_function_decl == fun);
23208       cold_text_section = unlikely_text_section ();
23209       switch_to_section (cold_text_section);
23210       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
23211       switch_to_section (sec);
23212     }
23213
23214   dwarf2out_note_section_used ();
23215   call_site_count = 0;
23216   tail_call_site_count = 0;
23217
23218   set_cur_line_info_table (sec);
23219 }
23220
23221 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
23222
23223 static void
23224 push_dw_line_info_entry (dw_line_info_table *table,
23225                          enum dw_line_info_opcode opcode, unsigned int val)
23226 {
23227   dw_line_info_entry e;
23228   e.opcode = opcode;
23229   e.val = val;
23230   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
23231 }
23232
23233 /* Output a label to mark the beginning of a source code line entry
23234    and record information relating to this source line, in
23235    'line_info_table' for later output of the .debug_line section.  */
23236 /* ??? The discriminator parameter ought to be unsigned.  */
23237
23238 static void
23239 dwarf2out_source_line (unsigned int line, const char *filename,
23240                        int discriminator, bool is_stmt)
23241 {
23242   unsigned int file_num;
23243   dw_line_info_table *table;
23244
23245   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
23246     return;
23247
23248   /* The discriminator column was added in dwarf4.  Simplify the below
23249      by simply removing it if we're not supposed to output it.  */
23250   if (dwarf_version < 4 && dwarf_strict)
23251     discriminator = 0;
23252
23253   table = cur_line_info_table;
23254   file_num = maybe_emit_file (lookup_filename (filename));
23255
23256   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
23257      the debugger has used the second (possibly duplicate) line number
23258      at the beginning of the function to mark the end of the prologue.
23259      We could eliminate any other duplicates within the function.  For
23260      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
23261      that second line number entry.  */
23262   /* Recall that this end-of-prologue indication is *not* the same thing
23263      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
23264      to which the hook corresponds, follows the last insn that was 
23265      emitted by gen_prologue.  What we need is to preceed the first insn
23266      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
23267      insn that corresponds to something the user wrote.  These may be
23268      very different locations once scheduling is enabled.  */
23269
23270   if (0 && file_num == table->file_num
23271       && line == table->line_num
23272       && discriminator == table->discrim_num
23273       && is_stmt == table->is_stmt)
23274     return;
23275
23276   switch_to_section (current_function_section ());
23277
23278   /* If requested, emit something human-readable.  */
23279   if (flag_debug_asm)
23280     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
23281
23282   if (DWARF2_ASM_LINE_DEBUG_INFO)
23283     {
23284       /* Emit the .loc directive understood by GNU as.  */
23285       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
23286       if (is_stmt != table->is_stmt)
23287         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
23288       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
23289         fprintf (asm_out_file, " discriminator %d", discriminator);
23290       fputc ('\n', asm_out_file);
23291     }
23292   else
23293     {
23294       unsigned int label_num = ++line_info_label_num;
23295
23296       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
23297
23298       push_dw_line_info_entry (table, LI_set_address, label_num);
23299       if (file_num != table->file_num)
23300         push_dw_line_info_entry (table, LI_set_file, file_num);
23301       if (discriminator != table->discrim_num)
23302         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
23303       if (is_stmt != table->is_stmt)
23304         push_dw_line_info_entry (table, LI_negate_stmt, 0);
23305       push_dw_line_info_entry (table, LI_set_line, line);
23306     }
23307
23308   table->file_num = file_num;
23309   table->line_num = line;
23310   table->discrim_num = discriminator;
23311   table->is_stmt = is_stmt;
23312   table->in_use = true;
23313 }
23314
23315 /* Record the beginning of a new source file.  */
23316
23317 static void
23318 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
23319 {
23320   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
23321     {
23322       /* Record the beginning of the file for break_out_includes.  */
23323       dw_die_ref bincl_die;
23324
23325       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
23326       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
23327     }
23328
23329   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23330     {
23331       macinfo_entry e;
23332       e.code = DW_MACINFO_start_file;
23333       e.lineno = lineno;
23334       e.info = xstrdup (filename);
23335       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23336     }
23337 }
23338
23339 /* Record the end of a source file.  */
23340
23341 static void
23342 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
23343 {
23344   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
23345     /* Record the end of the file for break_out_includes.  */
23346     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
23347
23348   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23349     {
23350       macinfo_entry e;
23351       e.code = DW_MACINFO_end_file;
23352       e.lineno = lineno;
23353       e.info = NULL;
23354       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23355     }
23356 }
23357
23358 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
23359    the tail part of the directive line, i.e. the part which is past the
23360    initial whitespace, #, whitespace, directive-name, whitespace part.  */
23361
23362 static void
23363 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
23364                   const char *buffer ATTRIBUTE_UNUSED)
23365 {
23366   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23367     {
23368       macinfo_entry e;
23369       e.code = DW_MACINFO_define;
23370       e.lineno = lineno;
23371       e.info = xstrdup (buffer);;
23372       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23373     }
23374 }
23375
23376 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
23377    the tail part of the directive line, i.e. the part which is past the
23378    initial whitespace, #, whitespace, directive-name, whitespace part.  */
23379
23380 static void
23381 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
23382                  const char *buffer ATTRIBUTE_UNUSED)
23383 {
23384   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23385     {
23386       macinfo_entry e;
23387       e.code = DW_MACINFO_undef;
23388       e.lineno = lineno;
23389       e.info = xstrdup (buffer);;
23390       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23391     }
23392 }
23393
23394 static void
23395 output_macinfo (void)
23396 {
23397   unsigned i;
23398   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
23399   macinfo_entry *ref;
23400
23401   if (! length)
23402     return;
23403
23404   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
23405     {
23406       switch (ref->code)
23407         {
23408           case DW_MACINFO_start_file:
23409             {
23410               int file_num = maybe_emit_file (lookup_filename (ref->info));
23411               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
23412               dw2_asm_output_data_uleb128 
23413                         (ref->lineno, "Included from line number %lu", 
23414                                                 (unsigned long)ref->lineno);
23415               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
23416             }
23417             break;
23418           case DW_MACINFO_end_file:
23419             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
23420             break;
23421           case DW_MACINFO_define:
23422             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
23423             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
23424                                                 (unsigned long)ref->lineno);
23425             dw2_asm_output_nstring (ref->info, -1, "The macro");
23426             break;
23427           case DW_MACINFO_undef:
23428             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
23429             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
23430                                                 (unsigned long)ref->lineno);
23431             dw2_asm_output_nstring (ref->info, -1, "The macro");
23432             break;
23433           default:
23434            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
23435              ASM_COMMENT_START, (unsigned long)ref->code);
23436           break;
23437         }
23438     }
23439 }
23440
23441 /* Set up for Dwarf output at the start of compilation.  */
23442
23443 static void
23444 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23445 {
23446   /* Allocate the file_table.  */
23447   file_table = htab_create_ggc (50, file_table_hash,
23448                                 file_table_eq, NULL);
23449
23450   /* Allocate the decl_die_table.  */
23451   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
23452                                     decl_die_table_eq, NULL);
23453
23454   /* Allocate the decl_loc_table.  */
23455   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
23456                                     decl_loc_table_eq, NULL);
23457
23458   /* Allocate the cached_dw_loc_list_table.  */
23459   cached_dw_loc_list_table
23460     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
23461                        cached_dw_loc_list_table_eq, NULL);
23462
23463   /* Allocate the initial hunk of the decl_scope_table.  */
23464   decl_scope_table = VEC_alloc (tree, gc, 256);
23465
23466   /* Allocate the initial hunk of the abbrev_die_table.  */
23467   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
23468     (ABBREV_DIE_TABLE_INCREMENT);
23469   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23470   /* Zero-th entry is allocated, but unused.  */
23471   abbrev_die_table_in_use = 1;
23472
23473   /* Allocate the pubtypes and pubnames vectors.  */
23474   pubname_table = VEC_alloc (pubname_entry, gc, 32);
23475   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
23476
23477   incomplete_types = VEC_alloc (tree, gc, 64);
23478
23479   used_rtx_array = VEC_alloc (rtx, gc, 32);
23480
23481   debug_info_section = get_section (DEBUG_INFO_SECTION,
23482                                     SECTION_DEBUG, NULL);
23483   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23484                                       SECTION_DEBUG, NULL);
23485   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23486                                        SECTION_DEBUG, NULL);
23487   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
23488                                        SECTION_DEBUG, NULL);
23489   debug_line_section = get_section (DEBUG_LINE_SECTION,
23490                                     SECTION_DEBUG, NULL);
23491   debug_loc_section = get_section (DEBUG_LOC_SECTION,
23492                                    SECTION_DEBUG, NULL);
23493   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23494                                         SECTION_DEBUG, NULL);
23495   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23496                                         SECTION_DEBUG, NULL);
23497   debug_str_section = get_section (DEBUG_STR_SECTION,
23498                                    DEBUG_STR_SECTION_FLAGS, NULL);
23499   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23500                                       SECTION_DEBUG, NULL);
23501   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23502                                      SECTION_DEBUG, NULL);
23503
23504   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23505   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23506                                DEBUG_ABBREV_SECTION_LABEL, 0);
23507   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23508   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23509                                COLD_TEXT_SECTION_LABEL, 0);
23510   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23511
23512   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23513                                DEBUG_INFO_SECTION_LABEL, 0);
23514   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23515                                DEBUG_LINE_SECTION_LABEL, 0);
23516   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23517                                DEBUG_RANGES_SECTION_LABEL, 0);
23518   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23519                                DEBUG_MACINFO_SECTION_LABEL, 0);
23520
23521   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23522     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
23523
23524   switch_to_section (text_section);
23525   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23526
23527   /* Make sure the line number table for .text always exists.  */
23528   text_section_line_info = new_line_info_table ();
23529   text_section_line_info->end_label = text_end_label;
23530 }
23531
23532 /* Called before cgraph_optimize starts outputtting functions, variables
23533    and toplevel asms into assembly.  */
23534
23535 static void
23536 dwarf2out_assembly_start (void)
23537 {
23538   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23539       && dwarf2out_do_cfi_asm ()
23540       && (!(flag_unwind_tables || flag_exceptions)
23541           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
23542     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23543 }
23544
23545 /* A helper function for dwarf2out_finish called through
23546    htab_traverse.  Emit one queued .debug_str string.  */
23547
23548 static int
23549 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
23550 {
23551   struct indirect_string_node *node = (struct indirect_string_node *) *h;
23552
23553   if (node->form == DW_FORM_strp)
23554     {
23555       switch_to_section (debug_str_section);
23556       ASM_OUTPUT_LABEL (asm_out_file, node->label);
23557       assemble_string (node->str, strlen (node->str) + 1);
23558     }
23559
23560   return 1;
23561 }
23562
23563 #if ENABLE_ASSERT_CHECKING
23564 /* Verify that all marks are clear.  */
23565
23566 static void
23567 verify_marks_clear (dw_die_ref die)
23568 {
23569   dw_die_ref c;
23570
23571   gcc_assert (! die->die_mark);
23572   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23573 }
23574 #endif /* ENABLE_ASSERT_CHECKING */
23575
23576 /* Clear the marks for a die and its children.
23577    Be cool if the mark isn't set.  */
23578
23579 static void
23580 prune_unmark_dies (dw_die_ref die)
23581 {
23582   dw_die_ref c;
23583
23584   if (die->die_mark)
23585     die->die_mark = 0;
23586   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23587 }
23588
23589 /* Given DIE that we're marking as used, find any other dies
23590    it references as attributes and mark them as used.  */
23591
23592 static void
23593 prune_unused_types_walk_attribs (dw_die_ref die)
23594 {
23595   dw_attr_ref a;
23596   unsigned ix;
23597
23598   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23599     {
23600       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23601         {
23602           /* A reference to another DIE.
23603              Make sure that it will get emitted.
23604              If it was broken out into a comdat group, don't follow it.  */
23605           if (! use_debug_types
23606               || a->dw_attr == DW_AT_specification
23607               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
23608             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23609         }
23610       /* Set the string's refcount to 0 so that prune_unused_types_mark
23611          accounts properly for it.  */
23612       if (AT_class (a) == dw_val_class_str)
23613         a->dw_attr_val.v.val_str->refcount = 0;
23614     }
23615 }
23616
23617 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23618
23619 static void
23620 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23621 {
23622   dw_die_ref c;
23623
23624   if (die == NULL || die->die_child == NULL)
23625     return;
23626   c = die->die_child;
23627   do
23628     {
23629       switch (c->die_tag)
23630         {
23631         case DW_TAG_template_type_param:
23632         case DW_TAG_template_value_param:
23633         case DW_TAG_GNU_template_template_param:
23634         case DW_TAG_GNU_template_parameter_pack:
23635           prune_unused_types_mark (c, 1);
23636           break;
23637         default:
23638           break;
23639         }
23640       c = c->die_sib;
23641     } while (c && c != die->die_child);
23642 }
23643
23644 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23645    to DIE's children.  */
23646
23647 static void
23648 prune_unused_types_mark (dw_die_ref die, int dokids)
23649 {
23650   dw_die_ref c;
23651
23652   if (die->die_mark == 0)
23653     {
23654       /* We haven't done this node yet.  Mark it as used.  */
23655       die->die_mark = 1;
23656       /* If this is the DIE of a generic type instantiation,
23657          mark the children DIEs that describe its generic parms and
23658          args.  */
23659       prune_unused_types_mark_generic_parms_dies (die);
23660
23661       /* We also have to mark its parents as used.
23662          (But we don't want to mark our parents' kids due to this.)  */
23663       if (die->die_parent)
23664         prune_unused_types_mark (die->die_parent, 0);
23665
23666       /* Mark any referenced nodes.  */
23667       prune_unused_types_walk_attribs (die);
23668
23669       /* If this node is a specification,
23670          also mark the definition, if it exists.  */
23671       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23672         prune_unused_types_mark (die->die_definition, 1);
23673     }
23674
23675   if (dokids && die->die_mark != 2)
23676     {
23677       /* We need to walk the children, but haven't done so yet.
23678          Remember that we've walked the kids.  */
23679       die->die_mark = 2;
23680
23681       /* If this is an array type, we need to make sure our
23682          kids get marked, even if they're types.  If we're
23683          breaking out types into comdat sections, do this
23684          for all type definitions.  */
23685       if (die->die_tag == DW_TAG_array_type
23686           || (use_debug_types
23687               && is_type_die (die) && ! is_declaration_die (die)))
23688         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23689       else
23690         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23691     }
23692 }
23693
23694 /* For local classes, look if any static member functions were emitted
23695    and if so, mark them.  */
23696
23697 static void
23698 prune_unused_types_walk_local_classes (dw_die_ref die)
23699 {
23700   dw_die_ref c;
23701
23702   if (die->die_mark == 2)
23703     return;
23704
23705   switch (die->die_tag)
23706     {
23707     case DW_TAG_structure_type:
23708     case DW_TAG_union_type:
23709     case DW_TAG_class_type:
23710       break;
23711
23712     case DW_TAG_subprogram:
23713       if (!get_AT_flag (die, DW_AT_declaration)
23714           || die->die_definition != NULL)
23715         prune_unused_types_mark (die, 1);
23716       return;
23717
23718     default:
23719       return;
23720     }
23721
23722   /* Mark children.  */
23723   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23724 }
23725
23726 /* Walk the tree DIE and mark types that we actually use.  */
23727
23728 static void
23729 prune_unused_types_walk (dw_die_ref die)
23730 {
23731   dw_die_ref c;
23732
23733   /* Don't do anything if this node is already marked and
23734      children have been marked as well.  */
23735   if (die->die_mark == 2)
23736     return;
23737
23738   switch (die->die_tag)
23739     {
23740     case DW_TAG_structure_type:
23741     case DW_TAG_union_type:
23742     case DW_TAG_class_type:
23743       if (die->die_perennial_p)
23744         break;
23745
23746       for (c = die->die_parent; c; c = c->die_parent)
23747         if (c->die_tag == DW_TAG_subprogram)
23748           break;
23749
23750       /* Finding used static member functions inside of classes
23751          is needed just for local classes, because for other classes
23752          static member function DIEs with DW_AT_specification
23753          are emitted outside of the DW_TAG_*_type.  If we ever change
23754          it, we'd need to call this even for non-local classes.  */
23755       if (c)
23756         prune_unused_types_walk_local_classes (die);
23757
23758       /* It's a type node --- don't mark it.  */
23759       return;
23760
23761     case DW_TAG_const_type:
23762     case DW_TAG_packed_type:
23763     case DW_TAG_pointer_type:
23764     case DW_TAG_reference_type:
23765     case DW_TAG_rvalue_reference_type:
23766     case DW_TAG_volatile_type:
23767     case DW_TAG_typedef:
23768     case DW_TAG_array_type:
23769     case DW_TAG_interface_type:
23770     case DW_TAG_friend:
23771     case DW_TAG_variant_part:
23772     case DW_TAG_enumeration_type:
23773     case DW_TAG_subroutine_type:
23774     case DW_TAG_string_type:
23775     case DW_TAG_set_type:
23776     case DW_TAG_subrange_type:
23777     case DW_TAG_ptr_to_member_type:
23778     case DW_TAG_file_type:
23779       if (die->die_perennial_p)
23780         break;
23781
23782       /* It's a type node --- don't mark it.  */
23783       return;
23784
23785     default:
23786       /* Mark everything else.  */
23787       break;
23788   }
23789
23790   if (die->die_mark == 0)
23791     {
23792       die->die_mark = 1;
23793
23794       /* Now, mark any dies referenced from here.  */
23795       prune_unused_types_walk_attribs (die);
23796     }
23797
23798   die->die_mark = 2;
23799
23800   /* Mark children.  */
23801   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23802 }
23803
23804 /* Increment the string counts on strings referred to from DIE's
23805    attributes.  */
23806
23807 static void
23808 prune_unused_types_update_strings (dw_die_ref die)
23809 {
23810   dw_attr_ref a;
23811   unsigned ix;
23812
23813   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23814     if (AT_class (a) == dw_val_class_str)
23815       {
23816         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23817         s->refcount++;
23818         /* Avoid unnecessarily putting strings that are used less than
23819            twice in the hash table.  */
23820         if (s->refcount
23821             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23822           {
23823             void ** slot;
23824             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
23825                                              htab_hash_string (s->str),
23826                                              INSERT);
23827             gcc_assert (*slot == NULL);
23828             *slot = s;
23829           }
23830       }
23831 }
23832
23833 /* Remove from the tree DIE any dies that aren't marked.  */
23834
23835 static void
23836 prune_unused_types_prune (dw_die_ref die)
23837 {
23838   dw_die_ref c;
23839
23840   gcc_assert (die->die_mark);
23841   prune_unused_types_update_strings (die);
23842
23843   if (! die->die_child)
23844     return;
23845
23846   c = die->die_child;
23847   do {
23848     dw_die_ref prev = c;
23849     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23850       if (c == die->die_child)
23851         {
23852           /* No marked children between 'prev' and the end of the list.  */
23853           if (prev == c)
23854             /* No marked children at all.  */
23855             die->die_child = NULL;
23856           else
23857             {
23858               prev->die_sib = c->die_sib;
23859               die->die_child = prev;
23860             }
23861           return;
23862         }
23863
23864     if (c != prev->die_sib)
23865       prev->die_sib = c;
23866     prune_unused_types_prune (c);
23867   } while (c != die->die_child);
23868 }
23869
23870 /* Remove dies representing declarations that we never use.  */
23871
23872 static void
23873 prune_unused_types (void)
23874 {
23875   unsigned int i;
23876   limbo_die_node *node;
23877   comdat_type_node *ctnode;
23878   pubname_ref pub;
23879   dw_die_ref base_type;
23880
23881 #if ENABLE_ASSERT_CHECKING
23882   /* All the marks should already be clear.  */
23883   verify_marks_clear (comp_unit_die ());
23884   for (node = limbo_die_list; node; node = node->next)
23885     verify_marks_clear (node->die);
23886   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23887     verify_marks_clear (ctnode->root_die);
23888 #endif /* ENABLE_ASSERT_CHECKING */
23889
23890   /* Mark types that are used in global variables.  */
23891   premark_types_used_by_global_vars ();
23892
23893   /* Set the mark on nodes that are actually used.  */
23894   prune_unused_types_walk (comp_unit_die ());
23895   for (node = limbo_die_list; node; node = node->next)
23896     prune_unused_types_walk (node->die);
23897   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23898     {
23899       prune_unused_types_walk (ctnode->root_die);
23900       prune_unused_types_mark (ctnode->type_die, 1);
23901     }
23902
23903   /* Also set the mark on nodes referenced from the
23904      pubname_table.  */
23905   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
23906     prune_unused_types_mark (pub->die, 1);
23907   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
23908     prune_unused_types_mark (base_type, 1);
23909
23910   if (debug_str_hash)
23911     htab_empty (debug_str_hash);
23912   prune_unused_types_prune (comp_unit_die ());
23913   for (node = limbo_die_list; node; node = node->next)
23914     prune_unused_types_prune (node->die);
23915   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23916     prune_unused_types_prune (ctnode->root_die);
23917
23918   /* Leave the marks clear.  */
23919   prune_unmark_dies (comp_unit_die ());
23920   for (node = limbo_die_list; node; node = node->next)
23921     prune_unmark_dies (node->die);
23922   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23923     prune_unmark_dies (ctnode->root_die);
23924 }
23925
23926 /* Set the parameter to true if there are any relative pathnames in
23927    the file table.  */
23928 static int
23929 file_table_relative_p (void ** slot, void *param)
23930 {
23931   bool *p = (bool *) param;
23932   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
23933   if (!IS_ABSOLUTE_PATH (d->filename))
23934     {
23935       *p = true;
23936       return 0;
23937     }
23938   return 1;
23939 }
23940
23941 /* Routines to manipulate hash table of comdat type units.  */
23942
23943 static hashval_t
23944 htab_ct_hash (const void *of)
23945 {
23946   hashval_t h;
23947   const comdat_type_node *const type_node = (const comdat_type_node *) of;
23948
23949   memcpy (&h, type_node->signature, sizeof (h));
23950   return h;
23951 }
23952
23953 static int
23954 htab_ct_eq (const void *of1, const void *of2)
23955 {
23956   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
23957   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
23958
23959   return (! memcmp (type_node_1->signature, type_node_2->signature,
23960                     DWARF_TYPE_SIGNATURE_SIZE));
23961 }
23962
23963 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23964    to the location it would have been added, should we know its
23965    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23966    probably improve compactness of debug info, removing equivalent
23967    abbrevs, and hide any differences caused by deferring the
23968    computation of the assembler name, triggered by e.g. PCH.  */
23969
23970 static inline void
23971 move_linkage_attr (dw_die_ref die)
23972 {
23973   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
23974   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
23975
23976   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23977               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23978
23979   while (--ix > 0)
23980     {
23981       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
23982
23983       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23984         break;
23985     }
23986
23987   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
23988     {
23989       VEC_pop (dw_attr_node, die->die_attr);
23990       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
23991     }
23992 }
23993
23994 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23995    referenced from typed stack ops and count how often they are used.  */
23996
23997 static void
23998 mark_base_types (dw_loc_descr_ref loc)
23999 {
24000   dw_die_ref base_type = NULL;
24001
24002   for (; loc; loc = loc->dw_loc_next)
24003     {
24004       switch (loc->dw_loc_opc)
24005         {
24006         case DW_OP_GNU_regval_type:
24007         case DW_OP_GNU_deref_type:
24008           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
24009           break;
24010         case DW_OP_GNU_const_type:
24011         case DW_OP_GNU_convert:
24012         case DW_OP_GNU_reinterpret:
24013           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
24014           break;
24015         case DW_OP_GNU_entry_value:
24016           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
24017           continue;
24018         default:
24019           continue;
24020         }
24021       gcc_assert (base_type->die_parent == comp_unit_die ());
24022       if (base_type->die_mark)
24023         base_type->die_mark++;
24024       else
24025         {
24026           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
24027           base_type->die_mark = 1;
24028         }
24029     }
24030 }
24031
24032 /* Comparison function for sorting marked base types.  */
24033
24034 static int
24035 base_type_cmp (const void *x, const void *y)
24036 {
24037   dw_die_ref dx = *(const dw_die_ref *) x;
24038   dw_die_ref dy = *(const dw_die_ref *) y;
24039   unsigned int byte_size1, byte_size2;
24040   unsigned int encoding1, encoding2;
24041   if (dx->die_mark > dy->die_mark)
24042     return -1;
24043   if (dx->die_mark < dy->die_mark)
24044     return 1;
24045   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24046   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24047   if (byte_size1 < byte_size2)
24048     return 1;
24049   if (byte_size1 > byte_size2)
24050     return -1;
24051   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24052   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24053   if (encoding1 < encoding2)
24054     return 1;
24055   if (encoding1 > encoding2)
24056     return -1;
24057   return 0;
24058 }
24059
24060 /* Move base types marked by mark_base_types as early as possible
24061    in the CU, sorted by decreasing usage count both to make the
24062    uleb128 references as small as possible and to make sure they
24063    will have die_offset already computed by calc_die_sizes when
24064    sizes of typed stack loc ops is computed.  */
24065
24066 static void
24067 move_marked_base_types (void)
24068 {
24069   unsigned int i;
24070   dw_die_ref base_type, die, c;
24071
24072   if (VEC_empty (dw_die_ref, base_types))
24073     return;
24074
24075   /* Sort by decreasing usage count, they will be added again in that
24076      order later on.  */
24077   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
24078   die = comp_unit_die ();
24079   c = die->die_child;
24080   do
24081     {
24082       dw_die_ref prev = c;
24083       c = c->die_sib;
24084       while (c->die_mark)
24085         {
24086           remove_child_with_prev (c, prev);
24087           /* As base types got marked, there must be at least
24088              one node other than DW_TAG_base_type.  */
24089           gcc_assert (c != c->die_sib);
24090           c = c->die_sib;
24091         }
24092     }
24093   while (c != die->die_child);
24094   gcc_assert (die->die_child);
24095   c = die->die_child;
24096   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
24097     {
24098       base_type->die_mark = 0;
24099       base_type->die_sib = c->die_sib;
24100       c->die_sib = base_type;
24101       c = base_type;
24102     }
24103 }
24104
24105 /* Helper function for resolve_addr, attempt to resolve
24106    one CONST_STRING, return non-zero if not successful.  Similarly verify that
24107    SYMBOL_REFs refer to variables emitted in the current CU.  */
24108
24109 static int
24110 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
24111 {
24112   rtx rtl = *addr;
24113
24114   if (GET_CODE (rtl) == CONST_STRING)
24115     {
24116       size_t len = strlen (XSTR (rtl, 0)) + 1;
24117       tree t = build_string (len, XSTR (rtl, 0));
24118       tree tlen = size_int (len - 1);
24119       TREE_TYPE (t)
24120         = build_array_type (char_type_node, build_index_type (tlen));
24121       rtl = lookup_constant_def (t);
24122       if (!rtl || !MEM_P (rtl))
24123         return 1;
24124       rtl = XEXP (rtl, 0);
24125       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
24126       *addr = rtl;
24127       return 0;
24128     }
24129
24130   if (GET_CODE (rtl) == SYMBOL_REF
24131       && SYMBOL_REF_DECL (rtl))
24132     {
24133       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24134         {
24135           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24136             return 1;
24137         }
24138       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24139         return 1;
24140     }
24141
24142   if (GET_CODE (rtl) == CONST
24143       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
24144     return 1;
24145
24146   return 0;
24147 }
24148
24149 /* Helper function for resolve_addr, handle one location
24150    expression, return false if at least one CONST_STRING or SYMBOL_REF in
24151    the location list couldn't be resolved.  */
24152
24153 static bool
24154 resolve_addr_in_expr (dw_loc_descr_ref loc)
24155 {
24156   dw_loc_descr_ref keep = NULL;
24157   for (; loc; loc = loc->dw_loc_next)
24158     switch (loc->dw_loc_opc)
24159       {
24160       case DW_OP_addr:
24161         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
24162           return false;
24163         break;
24164       case DW_OP_const4u:
24165       case DW_OP_const8u:
24166         if (loc->dtprel
24167             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
24168           return false;
24169         break;
24170       case DW_OP_implicit_value:
24171         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24172             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
24173           return false;
24174         break;
24175       case DW_OP_GNU_implicit_pointer:
24176         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24177           {
24178             dw_die_ref ref
24179               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24180             if (ref == NULL)
24181               return false;
24182             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24183             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24184             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24185           }
24186         break;
24187       case DW_OP_GNU_const_type:
24188       case DW_OP_GNU_regval_type:
24189       case DW_OP_GNU_deref_type:
24190       case DW_OP_GNU_convert:
24191       case DW_OP_GNU_reinterpret:
24192         while (loc->dw_loc_next
24193                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24194           {
24195             dw_die_ref base1, base2;
24196             unsigned enc1, enc2, size1, size2;
24197             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24198                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24199               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24200             else
24201               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24202             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24203             gcc_assert (base1->die_tag == DW_TAG_base_type
24204                         && base2->die_tag == DW_TAG_base_type);
24205             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24206             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24207             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24208             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24209             if (size1 == size2
24210                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24211                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24212                      && loc != keep)
24213                     || enc1 == enc2))
24214               {
24215                 /* Optimize away next DW_OP_GNU_convert after
24216                    adjusting LOC's base type die reference.  */
24217                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24218                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24219                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24220                 else
24221                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24222                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24223                 continue;
24224               }
24225             /* Don't change integer DW_OP_GNU_convert after e.g. floating
24226                point typed stack entry.  */
24227             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24228               keep = loc;
24229             break;
24230           }
24231         break;
24232       default:
24233         break;
24234       }
24235   return true;
24236 }
24237
24238 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24239    an address in .rodata section if the string literal is emitted there,
24240    or remove the containing location list or replace DW_AT_const_value
24241    with DW_AT_location and empty location expression, if it isn't found
24242    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
24243    to something that has been emitted in the current CU.  */
24244
24245 static void
24246 resolve_addr (dw_die_ref die)
24247 {
24248   dw_die_ref c;
24249   dw_attr_ref a;
24250   dw_loc_list_ref *curr, *start, loc;
24251   unsigned ix;
24252
24253   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
24254     switch (AT_class (a))
24255       {
24256       case dw_val_class_loc_list:
24257         start = curr = AT_loc_list_ptr (a);
24258         loc = *curr;
24259         gcc_assert (loc);
24260         /* The same list can be referenced more than once.  See if we have
24261            already recorded the result from a previous pass.  */
24262         if (loc->replaced)
24263           *curr = loc->dw_loc_next;
24264         else if (!loc->resolved_addr)
24265           {
24266             /* As things stand, we do not expect or allow one die to
24267                reference a suffix of another die's location list chain.
24268                References must be identical or completely separate.
24269                There is therefore no need to cache the result of this
24270                pass on any list other than the first; doing so
24271                would lead to unnecessary writes.  */
24272             while (*curr)
24273               {
24274                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24275                 if (!resolve_addr_in_expr ((*curr)->expr))
24276                   {
24277                     dw_loc_list_ref next = (*curr)->dw_loc_next;
24278                     if (next && (*curr)->ll_symbol)
24279                       {
24280                         gcc_assert (!next->ll_symbol);
24281                         next->ll_symbol = (*curr)->ll_symbol;
24282                       }
24283                     *curr = next;
24284                   }
24285                 else
24286                   {
24287                     mark_base_types ((*curr)->expr);
24288                     curr = &(*curr)->dw_loc_next;
24289                   }
24290               }
24291             if (loc == *start)
24292               loc->resolved_addr = 1;
24293             else
24294               {
24295                 loc->replaced = 1;
24296                 loc->dw_loc_next = *start;
24297               }
24298           }
24299         if (!*start)
24300           {
24301             remove_AT (die, a->dw_attr);
24302             ix--;
24303           }
24304         break;
24305       case dw_val_class_loc:
24306         if (!resolve_addr_in_expr (AT_loc (a)))
24307           {
24308             remove_AT (die, a->dw_attr);
24309             ix--;
24310           }
24311         else
24312           mark_base_types (AT_loc (a));
24313         break;
24314       case dw_val_class_addr:
24315         if (a->dw_attr == DW_AT_const_value
24316             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
24317           {
24318             remove_AT (die, a->dw_attr);
24319             ix--;
24320           }
24321         if (die->die_tag == DW_TAG_GNU_call_site
24322             && a->dw_attr == DW_AT_abstract_origin)
24323           {
24324             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24325             dw_die_ref tdie = lookup_decl_die (tdecl);
24326             if (tdie == NULL
24327                 && DECL_EXTERNAL (tdecl)
24328                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
24329               {
24330                 force_decl_die (tdecl);
24331                 tdie = lookup_decl_die (tdecl);
24332               }
24333             if (tdie)
24334               {
24335                 a->dw_attr_val.val_class = dw_val_class_die_ref;
24336                 a->dw_attr_val.v.val_die_ref.die = tdie;
24337                 a->dw_attr_val.v.val_die_ref.external = 0;
24338               }
24339             else
24340               {
24341                 remove_AT (die, a->dw_attr);
24342                 ix--;
24343               }
24344           }
24345         break;
24346       default:
24347         break;
24348       }
24349
24350   FOR_EACH_CHILD (die, c, resolve_addr (c));
24351 }
24352 \f
24353 /* Helper routines for optimize_location_lists.
24354    This pass tries to share identical local lists in .debug_loc
24355    section.  */
24356
24357 /* Iteratively hash operands of LOC opcode.  */
24358
24359 static inline hashval_t
24360 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
24361 {
24362   dw_val_ref val1 = &loc->dw_loc_oprnd1;
24363   dw_val_ref val2 = &loc->dw_loc_oprnd2;
24364
24365   switch (loc->dw_loc_opc)
24366     {
24367     case DW_OP_const4u:
24368     case DW_OP_const8u:
24369       if (loc->dtprel)
24370         goto hash_addr;
24371       /* FALLTHRU */
24372     case DW_OP_const1u:
24373     case DW_OP_const1s:
24374     case DW_OP_const2u:
24375     case DW_OP_const2s:
24376     case DW_OP_const4s:
24377     case DW_OP_const8s:
24378     case DW_OP_constu:
24379     case DW_OP_consts:
24380     case DW_OP_pick:
24381     case DW_OP_plus_uconst:
24382     case DW_OP_breg0:
24383     case DW_OP_breg1:
24384     case DW_OP_breg2:
24385     case DW_OP_breg3:
24386     case DW_OP_breg4:
24387     case DW_OP_breg5:
24388     case DW_OP_breg6:
24389     case DW_OP_breg7:
24390     case DW_OP_breg8:
24391     case DW_OP_breg9:
24392     case DW_OP_breg10:
24393     case DW_OP_breg11:
24394     case DW_OP_breg12:
24395     case DW_OP_breg13:
24396     case DW_OP_breg14:
24397     case DW_OP_breg15:
24398     case DW_OP_breg16:
24399     case DW_OP_breg17:
24400     case DW_OP_breg18:
24401     case DW_OP_breg19:
24402     case DW_OP_breg20:
24403     case DW_OP_breg21:
24404     case DW_OP_breg22:
24405     case DW_OP_breg23:
24406     case DW_OP_breg24:
24407     case DW_OP_breg25:
24408     case DW_OP_breg26:
24409     case DW_OP_breg27:
24410     case DW_OP_breg28:
24411     case DW_OP_breg29:
24412     case DW_OP_breg30:
24413     case DW_OP_breg31:
24414     case DW_OP_regx:
24415     case DW_OP_fbreg:
24416     case DW_OP_piece:
24417     case DW_OP_deref_size:
24418     case DW_OP_xderef_size:
24419       hash = iterative_hash_object (val1->v.val_int, hash);
24420       break;
24421     case DW_OP_skip:
24422     case DW_OP_bra:
24423       {
24424         int offset;
24425
24426         gcc_assert (val1->val_class == dw_val_class_loc);
24427         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24428         hash = iterative_hash_object (offset, hash);
24429       }
24430       break;
24431     case DW_OP_implicit_value:
24432       hash = iterative_hash_object (val1->v.val_unsigned, hash);
24433       switch (val2->val_class)
24434         {
24435         case dw_val_class_const:
24436           hash = iterative_hash_object (val2->v.val_int, hash);
24437           break;
24438         case dw_val_class_vec:
24439           {
24440             unsigned int elt_size = val2->v.val_vec.elt_size;
24441             unsigned int len = val2->v.val_vec.length;
24442
24443             hash = iterative_hash_object (elt_size, hash);
24444             hash = iterative_hash_object (len, hash);
24445             hash = iterative_hash (val2->v.val_vec.array,
24446                                    len * elt_size, hash);
24447           }
24448           break;
24449         case dw_val_class_const_double:
24450           hash = iterative_hash_object (val2->v.val_double.low, hash);
24451           hash = iterative_hash_object (val2->v.val_double.high, hash);
24452           break;
24453         case dw_val_class_addr:
24454           hash = iterative_hash_rtx (val2->v.val_addr, hash);
24455           break;
24456         default:
24457           gcc_unreachable ();
24458         }
24459       break;
24460     case DW_OP_bregx:
24461     case DW_OP_bit_piece:
24462       hash = iterative_hash_object (val1->v.val_int, hash);
24463       hash = iterative_hash_object (val2->v.val_int, hash);
24464       break;
24465     case DW_OP_addr:
24466     hash_addr:
24467       if (loc->dtprel)
24468         {
24469           unsigned char dtprel = 0xd1;
24470           hash = iterative_hash_object (dtprel, hash);
24471         }
24472       hash = iterative_hash_rtx (val1->v.val_addr, hash);
24473       break;
24474     case DW_OP_GNU_implicit_pointer:
24475       hash = iterative_hash_object (val2->v.val_int, hash);
24476       break;
24477     case DW_OP_GNU_entry_value:
24478       hash = hash_loc_operands (val1->v.val_loc, hash);
24479       break;
24480     case DW_OP_GNU_regval_type:
24481     case DW_OP_GNU_deref_type:
24482       {
24483         unsigned int byte_size
24484           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24485         unsigned int encoding
24486           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24487         hash = iterative_hash_object (val1->v.val_int, hash);
24488         hash = iterative_hash_object (byte_size, hash);
24489         hash = iterative_hash_object (encoding, hash);
24490       }
24491       break;
24492     case DW_OP_GNU_convert:
24493     case DW_OP_GNU_reinterpret:
24494     case DW_OP_GNU_const_type:
24495       {
24496         unsigned int byte_size
24497           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24498         unsigned int encoding
24499           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24500         hash = iterative_hash_object (byte_size, hash);
24501         hash = iterative_hash_object (encoding, hash);
24502         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24503           break;
24504         hash = iterative_hash_object (val2->val_class, hash);
24505         switch (val2->val_class)
24506           {
24507           case dw_val_class_const:
24508             hash = iterative_hash_object (val2->v.val_int, hash);
24509             break;
24510           case dw_val_class_vec:
24511             {
24512               unsigned int elt_size = val2->v.val_vec.elt_size;
24513               unsigned int len = val2->v.val_vec.length;
24514
24515               hash = iterative_hash_object (elt_size, hash);
24516               hash = iterative_hash_object (len, hash);
24517               hash = iterative_hash (val2->v.val_vec.array,
24518                                      len * elt_size, hash);
24519             }
24520             break;
24521           case dw_val_class_const_double:
24522             hash = iterative_hash_object (val2->v.val_double.low, hash);
24523             hash = iterative_hash_object (val2->v.val_double.high, hash);
24524             break;
24525           default:
24526             gcc_unreachable ();
24527           }
24528       }
24529       break;
24530
24531     default:
24532       /* Other codes have no operands.  */
24533       break;
24534     }
24535   return hash;
24536 }
24537
24538 /* Iteratively hash the whole DWARF location expression LOC.  */
24539
24540 static inline hashval_t
24541 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
24542 {
24543   dw_loc_descr_ref l;
24544   bool sizes_computed = false;
24545   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24546   size_of_locs (loc);
24547
24548   for (l = loc; l != NULL; l = l->dw_loc_next)
24549     {
24550       enum dwarf_location_atom opc = l->dw_loc_opc;
24551       hash = iterative_hash_object (opc, hash);
24552       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24553         {
24554           size_of_locs (loc);
24555           sizes_computed = true;
24556         }
24557       hash = hash_loc_operands (l, hash);
24558     }
24559   return hash;
24560 }
24561
24562 /* Compute hash of the whole location list LIST_HEAD.  */
24563
24564 static inline void
24565 hash_loc_list (dw_loc_list_ref list_head)
24566 {
24567   dw_loc_list_ref curr = list_head;
24568   hashval_t hash = 0;
24569
24570   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24571     {
24572       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
24573       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
24574       if (curr->section)
24575         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
24576                                hash);
24577       hash = hash_locs (curr->expr, hash);
24578     }
24579   list_head->hash = hash;
24580 }
24581
24582 /* Return true if X and Y opcodes have the same operands.  */
24583
24584 static inline bool
24585 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24586 {
24587   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24588   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24589   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24590   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24591
24592   switch (x->dw_loc_opc)
24593     {
24594     case DW_OP_const4u:
24595     case DW_OP_const8u:
24596       if (x->dtprel)
24597         goto hash_addr;
24598       /* FALLTHRU */
24599     case DW_OP_const1u:
24600     case DW_OP_const1s:
24601     case DW_OP_const2u:
24602     case DW_OP_const2s:
24603     case DW_OP_const4s:
24604     case DW_OP_const8s:
24605     case DW_OP_constu:
24606     case DW_OP_consts:
24607     case DW_OP_pick:
24608     case DW_OP_plus_uconst:
24609     case DW_OP_breg0:
24610     case DW_OP_breg1:
24611     case DW_OP_breg2:
24612     case DW_OP_breg3:
24613     case DW_OP_breg4:
24614     case DW_OP_breg5:
24615     case DW_OP_breg6:
24616     case DW_OP_breg7:
24617     case DW_OP_breg8:
24618     case DW_OP_breg9:
24619     case DW_OP_breg10:
24620     case DW_OP_breg11:
24621     case DW_OP_breg12:
24622     case DW_OP_breg13:
24623     case DW_OP_breg14:
24624     case DW_OP_breg15:
24625     case DW_OP_breg16:
24626     case DW_OP_breg17:
24627     case DW_OP_breg18:
24628     case DW_OP_breg19:
24629     case DW_OP_breg20:
24630     case DW_OP_breg21:
24631     case DW_OP_breg22:
24632     case DW_OP_breg23:
24633     case DW_OP_breg24:
24634     case DW_OP_breg25:
24635     case DW_OP_breg26:
24636     case DW_OP_breg27:
24637     case DW_OP_breg28:
24638     case DW_OP_breg29:
24639     case DW_OP_breg30:
24640     case DW_OP_breg31:
24641     case DW_OP_regx:
24642     case DW_OP_fbreg:
24643     case DW_OP_piece:
24644     case DW_OP_deref_size:
24645     case DW_OP_xderef_size:
24646       return valx1->v.val_int == valy1->v.val_int;
24647     case DW_OP_skip:
24648     case DW_OP_bra:
24649       gcc_assert (valx1->val_class == dw_val_class_loc
24650                   && valy1->val_class == dw_val_class_loc
24651                   && x->dw_loc_addr == y->dw_loc_addr);
24652       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24653     case DW_OP_implicit_value:
24654       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24655           || valx2->val_class != valy2->val_class)
24656         return false;
24657       switch (valx2->val_class)
24658         {
24659         case dw_val_class_const:
24660           return valx2->v.val_int == valy2->v.val_int;
24661         case dw_val_class_vec:
24662           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24663                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24664                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24665                             valx2->v.val_vec.elt_size
24666                             * valx2->v.val_vec.length) == 0;
24667         case dw_val_class_const_double:
24668           return valx2->v.val_double.low == valy2->v.val_double.low
24669                  && valx2->v.val_double.high == valy2->v.val_double.high;
24670         case dw_val_class_addr:
24671           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24672         default:
24673           gcc_unreachable ();
24674         }
24675     case DW_OP_bregx:
24676     case DW_OP_bit_piece:
24677       return valx1->v.val_int == valy1->v.val_int
24678              && valx2->v.val_int == valy2->v.val_int;
24679     case DW_OP_addr:
24680     hash_addr:
24681       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24682     case DW_OP_GNU_implicit_pointer:
24683       return valx1->val_class == dw_val_class_die_ref
24684              && valx1->val_class == valy1->val_class
24685              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24686              && valx2->v.val_int == valy2->v.val_int;
24687     case DW_OP_GNU_entry_value:
24688       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24689     case DW_OP_GNU_const_type:
24690       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24691           || valx2->val_class != valy2->val_class)
24692         return false;
24693       switch (valx2->val_class)
24694         {
24695         case dw_val_class_const:
24696           return valx2->v.val_int == valy2->v.val_int;
24697         case dw_val_class_vec:
24698           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24699                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24700                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24701                             valx2->v.val_vec.elt_size
24702                             * valx2->v.val_vec.length) == 0;
24703         case dw_val_class_const_double:
24704           return valx2->v.val_double.low == valy2->v.val_double.low
24705                  && valx2->v.val_double.high == valy2->v.val_double.high;
24706         default:
24707           gcc_unreachable ();
24708         }
24709     case DW_OP_GNU_regval_type:
24710     case DW_OP_GNU_deref_type:
24711       return valx1->v.val_int == valy1->v.val_int
24712              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24713     case DW_OP_GNU_convert:
24714     case DW_OP_GNU_reinterpret:
24715       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24716     default:
24717       /* Other codes have no operands.  */
24718       return true;
24719     }
24720 }
24721
24722 /* Return true if DWARF location expressions X and Y are the same.  */
24723
24724 static inline bool
24725 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24726 {
24727   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24728     if (x->dw_loc_opc != y->dw_loc_opc
24729         || x->dtprel != y->dtprel
24730         || !compare_loc_operands (x, y))
24731       break;
24732   return x == NULL && y == NULL;
24733 }
24734
24735 /* Return precomputed hash of location list X.  */
24736
24737 static hashval_t
24738 loc_list_hash (const void *x)
24739 {
24740   return ((const struct dw_loc_list_struct *) x)->hash;
24741 }
24742
24743 /* Return 1 if location lists X and Y are the same.  */
24744
24745 static int
24746 loc_list_eq (const void *x, const void *y)
24747 {
24748   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
24749   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
24750   if (a == b)
24751     return 1;
24752   if (a->hash != b->hash)
24753     return 0;
24754   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24755     if (strcmp (a->begin, b->begin) != 0
24756         || strcmp (a->end, b->end) != 0
24757         || (a->section == NULL) != (b->section == NULL)
24758         || (a->section && strcmp (a->section, b->section) != 0)
24759         || !compare_locs (a->expr, b->expr))
24760       break;
24761   return a == NULL && b == NULL;
24762 }
24763
24764 /* Recursively optimize location lists referenced from DIE
24765    children and share them whenever possible.  */
24766
24767 static void
24768 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
24769 {
24770   dw_die_ref c;
24771   dw_attr_ref a;
24772   unsigned ix;
24773   void **slot;
24774
24775   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
24776     if (AT_class (a) == dw_val_class_loc_list)
24777       {
24778         dw_loc_list_ref list = AT_loc_list (a);
24779         /* TODO: perform some optimizations here, before hashing
24780            it and storing into the hash table.  */
24781         hash_loc_list (list);
24782         slot = htab_find_slot_with_hash (htab, list, list->hash,
24783                                          INSERT);
24784         if (*slot == NULL)
24785           *slot = (void *) list;
24786         else
24787           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
24788       }
24789
24790   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24791 }
24792
24793 /* Optimize location lists referenced from DIE
24794    children and share them whenever possible.  */
24795
24796 static void
24797 optimize_location_lists (dw_die_ref die)
24798 {
24799   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
24800   optimize_location_lists_1 (die, htab);
24801   htab_delete (htab);
24802 }
24803 \f
24804 /* Output stuff that dwarf requires at the end of every file,
24805    and generate the DWARF-2 debugging info.  */
24806
24807 static void
24808 dwarf2out_finish (const char *filename)
24809 {
24810   limbo_die_node *node, *next_node;
24811   comdat_type_node *ctnode;
24812   htab_t comdat_type_table;
24813   unsigned int i;
24814
24815   gen_scheduled_generic_parms_dies ();
24816   gen_remaining_tmpl_value_param_die_attribute ();
24817
24818   /* Add the name for the main input file now.  We delayed this from
24819      dwarf2out_init to avoid complications with PCH.  */
24820   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24821   if (!IS_ABSOLUTE_PATH (filename))
24822     add_comp_dir_attribute (comp_unit_die ());
24823   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24824     {
24825       bool p = false;
24826       htab_traverse (file_table, file_table_relative_p, &p);
24827       if (p)
24828         add_comp_dir_attribute (comp_unit_die ());
24829     }
24830
24831   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
24832     {
24833       add_location_or_const_value_attribute (
24834         VEC_index (deferred_locations, deferred_locations_list, i)->die,
24835         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
24836         false,
24837         DW_AT_location);
24838     }
24839
24840   /* Traverse the limbo die list, and add parent/child links.  The only
24841      dies without parents that should be here are concrete instances of
24842      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
24843      For concrete instances, we can get the parent die from the abstract
24844      instance.  */
24845   for (node = limbo_die_list; node; node = next_node)
24846     {
24847       dw_die_ref die = node->die;
24848       next_node = node->next;
24849
24850       if (die->die_parent == NULL)
24851         {
24852           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24853
24854           if (origin)
24855             add_child_die (origin->die_parent, die);
24856           else if (is_cu_die (die))
24857             ;
24858           else if (seen_error ())
24859             /* It's OK to be confused by errors in the input.  */
24860             add_child_die (comp_unit_die (), die);
24861           else
24862             {
24863               /* In certain situations, the lexical block containing a
24864                  nested function can be optimized away, which results
24865                  in the nested function die being orphaned.  Likewise
24866                  with the return type of that nested function.  Force
24867                  this to be a child of the containing function.
24868
24869                  It may happen that even the containing function got fully
24870                  inlined and optimized out.  In that case we are lost and
24871                  assign the empty child.  This should not be big issue as
24872                  the function is likely unreachable too.  */
24873               tree context = NULL_TREE;
24874
24875               gcc_assert (node->created_for);
24876
24877               if (DECL_P (node->created_for))
24878                 context = DECL_CONTEXT (node->created_for);
24879               else if (TYPE_P (node->created_for))
24880                 context = TYPE_CONTEXT (node->created_for);
24881
24882               gcc_assert (context
24883                           && (TREE_CODE (context) == FUNCTION_DECL
24884                               || TREE_CODE (context) == NAMESPACE_DECL));
24885
24886               origin = lookup_decl_die (context);
24887               if (origin)
24888                 add_child_die (origin, die);
24889               else
24890                 add_child_die (comp_unit_die (), die);
24891             }
24892         }
24893     }
24894
24895   limbo_die_list = NULL;
24896
24897 #if ENABLE_ASSERT_CHECKING
24898   {
24899     dw_die_ref die = comp_unit_die (), c;
24900     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24901   }
24902 #endif
24903   resolve_addr (comp_unit_die ());
24904   move_marked_base_types ();
24905
24906   for (node = deferred_asm_name; node; node = node->next)
24907     {
24908       tree decl = node->created_for;
24909       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24910         {
24911           add_linkage_attr (node->die, decl);
24912           move_linkage_attr (node->die);
24913         }
24914     }
24915
24916   deferred_asm_name = NULL;
24917
24918   /* Walk through the list of incomplete types again, trying once more to
24919      emit full debugging info for them.  */
24920   retry_incomplete_types ();
24921
24922   if (flag_eliminate_unused_debug_types)
24923     prune_unused_types ();
24924
24925   /* Generate separate CUs for each of the include files we've seen.
24926      They will go into limbo_die_list.  */
24927   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
24928     break_out_includes (comp_unit_die ());
24929
24930   /* Generate separate COMDAT sections for type DIEs. */
24931   if (use_debug_types)
24932     {
24933       break_out_comdat_types (comp_unit_die ());
24934
24935       /* Each new type_unit DIE was added to the limbo die list when created.
24936          Since these have all been added to comdat_type_list, clear the
24937          limbo die list.  */
24938       limbo_die_list = NULL;
24939
24940       /* For each new comdat type unit, copy declarations for incomplete
24941          types to make the new unit self-contained (i.e., no direct
24942          references to the main compile unit).  */
24943       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24944         copy_decls_for_unworthy_types (ctnode->root_die);
24945       copy_decls_for_unworthy_types (comp_unit_die ());
24946
24947       /* In the process of copying declarations from one unit to another,
24948          we may have left some declarations behind that are no longer
24949          referenced.  Prune them.  */
24950       prune_unused_types ();
24951     }
24952
24953   /* Traverse the DIE's and add add sibling attributes to those DIE's
24954      that have children.  */
24955   add_sibling_attributes (comp_unit_die ());
24956   for (node = limbo_die_list; node; node = node->next)
24957     add_sibling_attributes (node->die);
24958   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24959     add_sibling_attributes (ctnode->root_die);
24960
24961   /* Output a terminator label for the .text section.  */
24962   switch_to_section (text_section);
24963   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24964   if (cold_text_section)
24965     {
24966       switch_to_section (cold_text_section);
24967       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24968     }
24969
24970   /* We can only use the low/high_pc attributes if all of the code was
24971      in .text.  */
24972   if (!have_multiple_function_sections 
24973       || (dwarf_version < 3 && dwarf_strict))
24974     {
24975       /* Don't add if the CU has no associated code.  */
24976       if (text_section_used)
24977         {
24978           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
24979           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
24980         }
24981     }
24982   else
24983     {
24984       unsigned fde_idx = 0;
24985       bool range_list_added = false;
24986
24987       if (text_section_used)
24988         add_ranges_by_labels (comp_unit_die (), text_section_label,
24989                               text_end_label, &range_list_added);
24990       if (cold_text_section_used)
24991         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
24992                               cold_end_label, &range_list_added);
24993
24994       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
24995         {
24996           dw_fde_ref fde = &fde_table[fde_idx];
24997
24998           if (!fde->in_std_section)
24999             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
25000                                   fde->dw_fde_end, &range_list_added);
25001           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25002             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
25003                                   fde->dw_fde_second_end, &range_list_added);
25004         }
25005
25006       if (range_list_added)
25007         {
25008           /* We need to give .debug_loc and .debug_ranges an appropriate
25009              "base address".  Use zero so that these addresses become
25010              absolute.  Historically, we've emitted the unexpected
25011              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25012              Emit both to give time for other tools to adapt.  */
25013           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
25014           if (! dwarf_strict && dwarf_version < 4)
25015             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
25016
25017           add_ranges (NULL);
25018         }
25019     }
25020
25021   if (debug_info_level >= DINFO_LEVEL_NORMAL)
25022     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
25023                     debug_line_section_label);
25024
25025   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25026     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
25027
25028   if (have_location_lists)
25029     optimize_location_lists (comp_unit_die ());
25030
25031   /* Output all of the compilation units.  We put the main one last so that
25032      the offsets are available to output_pubnames.  */
25033   for (node = limbo_die_list; node; node = node->next)
25034     output_comp_unit (node->die, 0);
25035
25036   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
25037   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25038     {
25039       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
25040
25041       /* Don't output duplicate types.  */
25042       if (*slot != HTAB_EMPTY_ENTRY)
25043         continue;
25044
25045       /* Add a pointer to the line table for the main compilation unit
25046          so that the debugger can make sense of DW_AT_decl_file
25047          attributes.  */
25048       if (debug_info_level >= DINFO_LEVEL_NORMAL)
25049         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25050                         debug_line_section_label);
25051
25052       output_comdat_type_unit (ctnode);
25053       *slot = ctnode;
25054     }
25055   htab_delete (comdat_type_table);
25056
25057   /* Output the main compilation unit if non-empty or if .debug_macinfo
25058      will be emitted.  */
25059   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
25060
25061   /* Output the abbreviation table.  */
25062   switch_to_section (debug_abbrev_section);
25063   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25064   output_abbrev_section ();
25065
25066   /* Output location list section if necessary.  */
25067   if (have_location_lists)
25068     {
25069       /* Output the location lists info.  */
25070       switch_to_section (debug_loc_section);
25071       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
25072                                    DEBUG_LOC_SECTION_LABEL, 0);
25073       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25074       output_location_lists (comp_unit_die ());
25075     }
25076
25077   /* Output public names table if necessary.  */
25078   if (!VEC_empty (pubname_entry, pubname_table))
25079     {
25080       gcc_assert (info_section_emitted);
25081       switch_to_section (debug_pubnames_section);
25082       output_pubnames (pubname_table);
25083     }
25084
25085   /* Output public types table if necessary.  */
25086   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
25087      It shouldn't hurt to emit it always, since pure DWARF2 consumers
25088      simply won't look for the section.  */
25089   if (!VEC_empty (pubname_entry, pubtype_table))
25090     {
25091       bool empty = false;
25092       
25093       if (flag_eliminate_unused_debug_types)
25094         {
25095           /* The pubtypes table might be emptied by pruning unused items.  */
25096           unsigned i;
25097           pubname_ref p;
25098           empty = true;
25099           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
25100             if (p->die->die_offset != 0)
25101               {
25102                 empty = false;
25103                 break;
25104               }
25105         }
25106       if (!empty)
25107         {
25108           gcc_assert (info_section_emitted);
25109           switch_to_section (debug_pubtypes_section);
25110           output_pubnames (pubtype_table);
25111         }
25112     }
25113
25114   /* Output the address range information if a CU (.debug_info section)
25115      was emitted.  We output an empty table even if we had no functions
25116      to put in it.  This because the consumer has no way to tell the
25117      difference between an empty table that we omitted and failure to
25118      generate a table that would have contained data.  */
25119   if (info_section_emitted)
25120     {
25121       unsigned long aranges_length = size_of_aranges ();
25122
25123       switch_to_section (debug_aranges_section);
25124       output_aranges (aranges_length);
25125     }
25126
25127   /* Output ranges section if necessary.  */
25128   if (ranges_table_in_use)
25129     {
25130       switch_to_section (debug_ranges_section);
25131       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25132       output_ranges ();
25133     }
25134
25135   /* Output the source line correspondence table.  We must do this
25136      even if there is no line information.  Otherwise, on an empty
25137      translation unit, we will generate a present, but empty,
25138      .debug_info section.  IRIX 6.5 `nm' will then complain when
25139      examining the file.  This is done late so that any filenames
25140      used by the debug_info section are marked as 'used'.  */
25141   switch_to_section (debug_line_section);
25142   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25143   if (! DWARF2_ASM_LINE_DEBUG_INFO)
25144     output_line_info ();
25145
25146   /* Have to end the macro section.  */
25147   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25148     {
25149       switch_to_section (debug_macinfo_section);
25150       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25151       if (!VEC_empty (macinfo_entry, macinfo_table))
25152         output_macinfo ();
25153       dw2_asm_output_data (1, 0, "End compilation unit");
25154     }
25155
25156   /* If we emitted any DW_FORM_strp form attribute, output the string
25157      table too.  */
25158   if (debug_str_hash)
25159     htab_traverse (debug_str_hash, output_indirect_string, NULL);
25160 }
25161
25162 #include "gt-dwarf2out.h"