OSDN Git Service

6d58a00dd634d98453e2f899368a4feb1afbcb1a
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 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 "common/common-target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.h"
95 #include "tree-flow.h"
96 #include "cfglayout.h"
97
98 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
99 static rtx last_var_location_insn;
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 /* ??? Poison these here until it can be done generically.  They've been
118    totally replaced in this file; make sure it stays that way.  */
119 #undef DWARF2_UNWIND_INFO
120 #undef DWARF2_FRAME_INFO
121 #if (GCC_VERSION >= 3000)
122  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
123 #endif
124
125 #ifndef INCOMING_RETURN_ADDR_RTX
126 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
127 #endif
128
129 /* Map register numbers held in the call frame info that gcc has
130    collected using DWARF_FRAME_REGNUM to those that should be output in
131    .debug_frame and .eh_frame.  */
132 #ifndef DWARF2_FRAME_REG_OUT
133 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
134 #endif
135
136 /* Save the result of dwarf2out_do_frame across PCH.  */
137 static GTY(()) bool saved_do_cfi_asm = 0;
138
139 /* Decide whether we want to emit frame unwind information for the current
140    translation unit.  */
141
142 int
143 dwarf2out_do_frame (void)
144 {
145   /* We want to emit correct CFA location expressions or lists, so we
146      have to return true if we're going to output debug info, even if
147      we're not going to output frame or unwind info.  */
148   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
149     return true;
150
151   if (saved_do_cfi_asm)
152     return true;
153
154   if (targetm.debug_unwind_info () == UI_DWARF2)
155     return true;
156
157   if ((flag_unwind_tables || flag_exceptions)
158       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
159     return true;
160
161   return false;
162 }
163
164 /* Decide whether to emit frame unwind via assembler directives.  */
165
166 int
167 dwarf2out_do_cfi_asm (void)
168 {
169   int enc;
170
171 #ifdef MIPS_DEBUGGING_INFO
172   return false;
173 #endif
174   if (saved_do_cfi_asm)
175     return true;
176   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
177     return false;
178   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
179     return false;
180
181   /* Make sure the personality encoding is one the assembler can support.
182      In particular, aligned addresses can't be handled.  */
183   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
184   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
185     return false;
186   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
187   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
188     return false;
189
190   /* If we can't get the assembler to emit only .debug_frame, and we don't need
191      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
192   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
193       && !flag_unwind_tables && !flag_exceptions
194       && targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
195     return false;
196
197   saved_do_cfi_asm = true;
198   return true;
199 }
200
201 /* The size of the target's pointer type.  */
202 #ifndef PTR_SIZE
203 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
204 #endif
205
206 /* Array of RTXes referenced by the debugging information, which therefore
207    must be kept around forever.  */
208 static GTY(()) VEC(rtx,gc) *used_rtx_array;
209
210 /* A pointer to the base of a list of incomplete types which might be
211    completed at some later time.  incomplete_types_list needs to be a
212    VEC(tree,gc) because we want to tell the garbage collector about
213    it.  */
214 static GTY(()) VEC(tree,gc) *incomplete_types;
215
216 /* A pointer to the base of a table of references to declaration
217    scopes.  This table is a display which tracks the nesting
218    of declaration scopes at the current scope and containing
219    scopes.  This table is used to find the proper place to
220    define type declaration DIE's.  */
221 static GTY(()) VEC(tree,gc) *decl_scope_table;
222
223 /* Pointers to various DWARF2 sections.  */
224 static GTY(()) section *debug_info_section;
225 static GTY(()) section *debug_abbrev_section;
226 static GTY(()) section *debug_aranges_section;
227 static GTY(()) section *debug_macinfo_section;
228 static GTY(()) section *debug_line_section;
229 static GTY(()) section *debug_loc_section;
230 static GTY(()) section *debug_pubnames_section;
231 static GTY(()) section *debug_pubtypes_section;
232 static GTY(()) section *debug_str_section;
233 static GTY(()) section *debug_ranges_section;
234 static GTY(()) section *debug_frame_section;
235
236 /* Personality decl of current unit.  Used only when assembler does not support
237    personality CFI.  */
238 static GTY(()) rtx current_unit_personality;
239
240 /* How to start an assembler comment.  */
241 #ifndef ASM_COMMENT_START
242 #define ASM_COMMENT_START ";#"
243 #endif
244
245 typedef struct dw_cfi_struct *dw_cfi_ref;
246 typedef struct dw_fde_struct *dw_fde_ref;
247 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
248
249 /* Call frames are described using a sequence of Call Frame
250    Information instructions.  The register number, offset
251    and address fields are provided as possible operands;
252    their use is selected by the opcode field.  */
253
254 enum dw_cfi_oprnd_type {
255   dw_cfi_oprnd_unused,
256   dw_cfi_oprnd_reg_num,
257   dw_cfi_oprnd_offset,
258   dw_cfi_oprnd_addr,
259   dw_cfi_oprnd_loc
260 };
261
262 typedef union GTY(()) dw_cfi_oprnd_struct {
263   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
264   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
265   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
266   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
267 }
268 dw_cfi_oprnd;
269
270 typedef struct GTY(()) dw_cfi_struct {
271   enum dwarf_call_frame_info dw_cfi_opc;
272   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
273     dw_cfi_oprnd1;
274   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
275     dw_cfi_oprnd2;
276 }
277 dw_cfi_node;
278
279 DEF_VEC_P (dw_cfi_ref);
280 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
281 DEF_VEC_ALLOC_P (dw_cfi_ref, gc);
282
283 typedef VEC(dw_cfi_ref, gc) *cfi_vec;
284
285 /* This is how we define the location of the CFA. We use to handle it
286    as REG + OFFSET all the time,  but now it can be more complex.
287    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
288    Instead of passing around REG and OFFSET, we pass a copy
289    of this structure.  */
290 typedef struct cfa_loc {
291   HOST_WIDE_INT offset;
292   HOST_WIDE_INT base_offset;
293   unsigned int reg;
294   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
295   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
296 } dw_cfa_location;
297
298 /* All call frame descriptions (FDE's) in the GCC generated DWARF
299    refer to a single Common Information Entry (CIE), defined at
300    the beginning of the .debug_frame section.  This use of a single
301    CIE obviates the need to keep track of multiple CIE's
302    in the DWARF generation routines below.  */
303
304 typedef struct GTY(()) dw_fde_struct {
305   tree decl;
306   const char *dw_fde_begin;
307   const char *dw_fde_current_label;
308   const char *dw_fde_end;
309   const char *dw_fde_vms_end_prologue;
310   const char *dw_fde_vms_begin_epilogue;
311   const char *dw_fde_second_begin;
312   const char *dw_fde_second_end;
313   cfi_vec dw_fde_cfi;
314   int dw_fde_switch_cfi_index; /* Last CFI before switching sections.  */
315   HOST_WIDE_INT stack_realignment;
316   unsigned funcdef_number;
317   /* Dynamic realign argument pointer register.  */
318   unsigned int drap_reg;
319   /* Virtual dynamic realign argument pointer register.  */
320   unsigned int vdrap_reg;
321   /* These 3 flags are copied from rtl_data in function.h.  */
322   unsigned all_throwers_are_sibcalls : 1;
323   unsigned uses_eh_lsda : 1;
324   unsigned nothrow : 1;
325   /* Whether we did stack realign in this call frame.  */
326   unsigned stack_realign : 1;
327   /* Whether dynamic realign argument pointer register has been saved.  */
328   unsigned drap_reg_saved: 1;
329   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
330   unsigned in_std_section : 1;
331   /* True iff dw_fde_second_begin label is in text_section or
332      cold_text_section.  */
333   unsigned second_in_std_section : 1;
334 }
335 dw_fde_node;
336
337 /* Maximum size (in bytes) of an artificially generated label.  */
338 #define MAX_ARTIFICIAL_LABEL_BYTES      30
339
340 /* The size of addresses as they appear in the Dwarf 2 data.
341    Some architectures use word addresses to refer to code locations,
342    but Dwarf 2 info always uses byte addresses.  On such machines,
343    Dwarf 2 addresses need to be larger than the architecture's
344    pointers.  */
345 #ifndef DWARF2_ADDR_SIZE
346 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
347 #endif
348
349 /* The size in bytes of a DWARF field indicating an offset or length
350    relative to a debug info section, specified to be 4 bytes in the
351    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
352    as PTR_SIZE.  */
353
354 #ifndef DWARF_OFFSET_SIZE
355 #define DWARF_OFFSET_SIZE 4
356 #endif
357
358 /* The size in bytes of a DWARF 4 type signature.  */
359
360 #ifndef DWARF_TYPE_SIGNATURE_SIZE
361 #define DWARF_TYPE_SIGNATURE_SIZE 8
362 #endif
363
364 /* According to the (draft) DWARF 3 specification, the initial length
365    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
366    bytes are 0xffffffff, followed by the length stored in the next 8
367    bytes.
368
369    However, the SGI/MIPS ABI uses an initial length which is equal to
370    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
371
372 #ifndef DWARF_INITIAL_LENGTH_SIZE
373 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
374 #endif
375
376 /* Round SIZE up to the nearest BOUNDARY.  */
377 #define DWARF_ROUND(SIZE,BOUNDARY) \
378   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
379
380 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
381 #ifndef DWARF_CIE_DATA_ALIGNMENT
382 #ifdef STACK_GROWS_DOWNWARD
383 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
384 #else
385 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
386 #endif
387 #endif
388
389 /* CIE identifier.  */
390 #if HOST_BITS_PER_WIDE_INT >= 64
391 #define DWARF_CIE_ID \
392   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
393 #else
394 #define DWARF_CIE_ID DW_CIE_ID
395 #endif
396
397 /* A pointer to the base of a table that contains frame description
398    information for each routine.  */
399 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
400
401 /* Number of elements currently allocated for fde_table.  */
402 static GTY(()) unsigned fde_table_allocated;
403
404 /* Number of elements in fde_table currently in use.  */
405 static GTY(()) unsigned fde_table_in_use;
406
407 /* Size (in elements) of increments by which we may expand the
408    fde_table.  */
409 #define FDE_TABLE_INCREMENT 256
410
411 /* Get the current fde_table entry we should use.  */
412
413 static inline dw_fde_ref
414 current_fde (void)
415 {
416   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
417 }
418
419 /* A vector of call frame insns for the CIE.  */
420 static GTY(()) cfi_vec cie_cfi_vec;
421
422 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
423    attribute that accelerates the lookup of the FDE associated
424    with the subprogram.  This variable holds the table index of the FDE
425    associated with the current function (body) definition.  */
426 static unsigned current_funcdef_fde;
427
428 struct GTY(()) indirect_string_node {
429   const char *str;
430   unsigned int refcount;
431   enum dwarf_form form;
432   char *label;
433 };
434
435 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
436
437 static GTY(()) int dw2_string_counter;
438 static GTY(()) unsigned long dwarf2out_cfi_label_num;
439
440 /* True if the compilation unit places functions in more than one section.  */
441 static GTY(()) bool have_multiple_function_sections = false;
442
443 /* Whether the default text and cold text sections have been used at all.  */
444
445 static GTY(()) bool text_section_used = false;
446 static GTY(()) bool cold_text_section_used = false;
447
448 /* The default cold text section.  */
449 static GTY(()) section *cold_text_section;
450
451 /* Forward declarations for functions defined in this file.  */
452
453 static char *stripattributes (const char *);
454 static const char *dwarf_cfi_name (unsigned);
455 static dw_cfi_ref new_cfi (void);
456 static void add_cfi (cfi_vec *, dw_cfi_ref);
457 static void add_fde_cfi (const char *, dw_cfi_ref);
458 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
459 static void lookup_cfa (dw_cfa_location *);
460 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
461 static void initial_return_save (rtx);
462 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
463                                           HOST_WIDE_INT);
464 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
465 static void output_cfi_directive (dw_cfi_ref);
466 static void output_call_frame_info (int);
467 static void dwarf2out_note_section_used (void);
468 static bool clobbers_queued_reg_save (const_rtx);
469 static void dwarf2out_frame_debug_expr (rtx, const char *);
470
471 /* Support for complex CFA locations.  */
472 static void output_cfa_loc (dw_cfi_ref, int);
473 static void output_cfa_loc_raw (dw_cfi_ref);
474 static void get_cfa_from_loc_descr (dw_cfa_location *,
475                                     struct dw_loc_descr_struct *);
476 static struct dw_loc_descr_struct *build_cfa_loc
477   (dw_cfa_location *, HOST_WIDE_INT);
478 static struct dw_loc_descr_struct *build_cfa_aligned_loc
479   (HOST_WIDE_INT, HOST_WIDE_INT);
480 static void def_cfa_1 (const char *, dw_cfa_location *);
481 static struct dw_loc_descr_struct *mem_loc_descriptor
482   (rtx, enum machine_mode mode, enum machine_mode mem_mode,
483    enum var_init_status);
484
485 /* How to start an assembler comment.  */
486 #ifndef ASM_COMMENT_START
487 #define ASM_COMMENT_START ";#"
488 #endif
489
490 /* Data and reference forms for relocatable data.  */
491 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
492 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
493
494 #ifndef DEBUG_FRAME_SECTION
495 #define DEBUG_FRAME_SECTION     ".debug_frame"
496 #endif
497
498 #ifndef FUNC_BEGIN_LABEL
499 #define FUNC_BEGIN_LABEL        "LFB"
500 #endif
501
502 #ifndef FUNC_END_LABEL
503 #define FUNC_END_LABEL          "LFE"
504 #endif
505
506 #ifndef PROLOGUE_END_LABEL
507 #define PROLOGUE_END_LABEL      "LPE"
508 #endif
509
510 #ifndef EPILOGUE_BEGIN_LABEL
511 #define EPILOGUE_BEGIN_LABEL    "LEB"
512 #endif
513
514 #ifndef FRAME_BEGIN_LABEL
515 #define FRAME_BEGIN_LABEL       "Lframe"
516 #endif
517 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
518 #define CIE_END_LABEL           "LECIE"
519 #define FDE_LABEL               "LSFDE"
520 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
521 #define FDE_END_LABEL           "LEFDE"
522 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
523 #define LINE_NUMBER_END_LABEL   "LELT"
524 #define LN_PROLOG_AS_LABEL      "LASLTP"
525 #define LN_PROLOG_END_LABEL     "LELTP"
526 #define DIE_LABEL_PREFIX        "DW"
527
528 /* The DWARF 2 CFA column which tracks the return address.  Normally this
529    is the column for PC, or the first column after all of the hard
530    registers.  */
531 #ifndef DWARF_FRAME_RETURN_COLUMN
532 #ifdef PC_REGNUM
533 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
534 #else
535 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
536 #endif
537 #endif
538
539 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
540    default, we just provide columns for all registers.  */
541 #ifndef DWARF_FRAME_REGNUM
542 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
543 #endif
544 \f
545 /* Match the base name of a file to the base name of a compilation unit. */
546
547 static int
548 matches_main_base (const char *path)
549 {
550   /* Cache the last query. */
551   static const char *last_path = NULL;
552   static int last_match = 0;
553   if (path != last_path)
554     {
555       const char *base;
556       int length = base_of_path (path, &base);
557       last_path = path;
558       last_match = (length == main_input_baselength
559                     && memcmp (base, main_input_basename, length) == 0);
560     }
561   return last_match;
562 }
563
564 #ifdef DEBUG_DEBUG_STRUCT
565
566 static int
567 dump_struct_debug (tree type, enum debug_info_usage usage,
568                    enum debug_struct_file criterion, int generic,
569                    int matches, int result)
570 {
571   /* Find the type name. */
572   tree type_decl = TYPE_STUB_DECL (type);
573   tree t = type_decl;
574   const char *name = 0;
575   if (TREE_CODE (t) == TYPE_DECL)
576     t = DECL_NAME (t);
577   if (t)
578     name = IDENTIFIER_POINTER (t);
579
580   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
581            criterion,
582            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
583            matches ? "bas" : "hdr",
584            generic ? "gen" : "ord",
585            usage == DINFO_USAGE_DFN ? ";" :
586              usage == DINFO_USAGE_DIR_USE ? "." : "*",
587            result,
588            (void*) type_decl, name);
589   return result;
590 }
591 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
592   dump_struct_debug (type, usage, criterion, generic, matches, result)
593
594 #else
595
596 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
597   (result)
598
599 #endif
600
601 static bool
602 should_emit_struct_debug (tree type, enum debug_info_usage usage)
603 {
604   enum debug_struct_file criterion;
605   tree type_decl;
606   bool generic = lang_hooks.types.generic_p (type);
607
608   if (generic)
609     criterion = debug_struct_generic[usage];
610   else
611     criterion = debug_struct_ordinary[usage];
612
613   if (criterion == DINFO_STRUCT_FILE_NONE)
614     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
615   if (criterion == DINFO_STRUCT_FILE_ANY)
616     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
617
618   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
619
620   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
621     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
622
623   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
624     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
625   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
626 }
627 \f
628 /* Hook used by __throw.  */
629
630 rtx
631 expand_builtin_dwarf_sp_column (void)
632 {
633   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
634   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
635 }
636
637 /* Return a pointer to a copy of the section string name S with all
638    attributes stripped off, and an asterisk prepended (for assemble_name).  */
639
640 static inline char *
641 stripattributes (const char *s)
642 {
643   char *stripped = XNEWVEC (char, strlen (s) + 2);
644   char *p = stripped;
645
646   *p++ = '*';
647
648   while (*s && *s != ',')
649     *p++ = *s++;
650
651   *p = '\0';
652   return stripped;
653 }
654
655 /* MEM is a memory reference for the register size table, each element of
656    which has mode MODE.  Initialize column C as a return address column.  */
657
658 static void
659 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
660 {
661   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
662   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
663   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
664 }
665
666 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
667
668 static inline HOST_WIDE_INT
669 div_data_align (HOST_WIDE_INT off)
670 {
671   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
672   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
673   return r;
674 }
675
676 /* Return true if we need a signed version of a given opcode
677    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
678
679 static inline bool
680 need_data_align_sf_opcode (HOST_WIDE_INT off)
681 {
682   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
683 }
684
685 /* Generate code to initialize the register size table.  */
686
687 void
688 expand_builtin_init_dwarf_reg_sizes (tree address)
689 {
690   unsigned int i;
691   enum machine_mode mode = TYPE_MODE (char_type_node);
692   rtx addr = expand_normal (address);
693   rtx mem = gen_rtx_MEM (BLKmode, addr);
694   bool wrote_return_column = false;
695
696   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
697     {
698       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
699
700       if (rnum < DWARF_FRAME_REGISTERS)
701         {
702           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
703           enum machine_mode save_mode = reg_raw_mode[i];
704           HOST_WIDE_INT size;
705
706           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
707             save_mode = choose_hard_reg_mode (i, 1, true);
708           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
709             {
710               if (save_mode == VOIDmode)
711                 continue;
712               wrote_return_column = true;
713             }
714           size = GET_MODE_SIZE (save_mode);
715           if (offset < 0)
716             continue;
717
718           emit_move_insn (adjust_address (mem, mode, offset),
719                           gen_int_mode (size, mode));
720         }
721     }
722
723   if (!wrote_return_column)
724     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
725
726 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
727   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
728 #endif
729
730   targetm.init_dwarf_reg_sizes_extra (address);
731 }
732
733 /* Convert a DWARF call frame info. operation to its string name */
734
735 static const char *
736 dwarf_cfi_name (unsigned int cfi_opc)
737 {
738   switch (cfi_opc)
739     {
740     case DW_CFA_advance_loc:
741       return "DW_CFA_advance_loc";
742     case DW_CFA_offset:
743       return "DW_CFA_offset";
744     case DW_CFA_restore:
745       return "DW_CFA_restore";
746     case DW_CFA_nop:
747       return "DW_CFA_nop";
748     case DW_CFA_set_loc:
749       return "DW_CFA_set_loc";
750     case DW_CFA_advance_loc1:
751       return "DW_CFA_advance_loc1";
752     case DW_CFA_advance_loc2:
753       return "DW_CFA_advance_loc2";
754     case DW_CFA_advance_loc4:
755       return "DW_CFA_advance_loc4";
756     case DW_CFA_offset_extended:
757       return "DW_CFA_offset_extended";
758     case DW_CFA_restore_extended:
759       return "DW_CFA_restore_extended";
760     case DW_CFA_undefined:
761       return "DW_CFA_undefined";
762     case DW_CFA_same_value:
763       return "DW_CFA_same_value";
764     case DW_CFA_register:
765       return "DW_CFA_register";
766     case DW_CFA_remember_state:
767       return "DW_CFA_remember_state";
768     case DW_CFA_restore_state:
769       return "DW_CFA_restore_state";
770     case DW_CFA_def_cfa:
771       return "DW_CFA_def_cfa";
772     case DW_CFA_def_cfa_register:
773       return "DW_CFA_def_cfa_register";
774     case DW_CFA_def_cfa_offset:
775       return "DW_CFA_def_cfa_offset";
776
777     /* DWARF 3 */
778     case DW_CFA_def_cfa_expression:
779       return "DW_CFA_def_cfa_expression";
780     case DW_CFA_expression:
781       return "DW_CFA_expression";
782     case DW_CFA_offset_extended_sf:
783       return "DW_CFA_offset_extended_sf";
784     case DW_CFA_def_cfa_sf:
785       return "DW_CFA_def_cfa_sf";
786     case DW_CFA_def_cfa_offset_sf:
787       return "DW_CFA_def_cfa_offset_sf";
788
789     /* SGI/MIPS specific */
790     case DW_CFA_MIPS_advance_loc8:
791       return "DW_CFA_MIPS_advance_loc8";
792
793     /* GNU extensions */
794     case DW_CFA_GNU_window_save:
795       return "DW_CFA_GNU_window_save";
796     case DW_CFA_GNU_args_size:
797       return "DW_CFA_GNU_args_size";
798     case DW_CFA_GNU_negative_offset_extended:
799       return "DW_CFA_GNU_negative_offset_extended";
800
801     default:
802       return "DW_CFA_<unknown>";
803     }
804 }
805
806 /* Return a pointer to a newly allocated Call Frame Instruction.  */
807
808 static inline dw_cfi_ref
809 new_cfi (void)
810 {
811   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
812
813   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
814   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
815
816   return cfi;
817 }
818
819 /* Add a Call Frame Instruction to list of instructions.  */
820
821 static inline void
822 add_cfi (cfi_vec *vec, dw_cfi_ref cfi)
823 {
824   dw_fde_ref fde = current_fde ();
825
826   /* When DRAP is used, CFA is defined with an expression.  Redefine
827      CFA may lead to a different CFA value.   */
828   /* ??? Of course, this heuristic fails when we're annotating epilogues,
829      because of course we'll always want to redefine the CFA back to the
830      stack pointer on the way out.  Where should we move this check?  */
831   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
832     switch (cfi->dw_cfi_opc)
833       {
834         case DW_CFA_def_cfa_register:
835         case DW_CFA_def_cfa_offset:
836         case DW_CFA_def_cfa_offset_sf:
837         case DW_CFA_def_cfa:
838         case DW_CFA_def_cfa_sf:
839           gcc_unreachable ();
840
841         default:
842           break;
843       }
844
845   VEC_safe_push (dw_cfi_ref, gc, *vec, cfi);
846 }
847
848 /* Generate a new label for the CFI info to refer to.  FORCE is true
849    if a label needs to be output even when using .cfi_* directives.  */
850
851 char *
852 dwarf2out_cfi_label (bool force)
853 {
854   static char label[20];
855
856   if (!force && dwarf2out_do_cfi_asm ())
857     {
858       /* In this case, we will be emitting the asm directive instead of
859          the label, so just return a placeholder to keep the rest of the
860          interfaces happy.  */
861       strcpy (label, "<do not output>");
862     }
863   else
864     {
865       int num = dwarf2out_cfi_label_num++;
866       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
867       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
868     }
869
870   return label;
871 }
872
873 /* True if remember_state should be emitted before following CFI directive.  */
874 static bool emit_cfa_remember;
875
876 /* True if any CFI directives were emitted at the current insn.  */
877 static bool any_cfis_emitted;
878
879 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
880    or to the CIE if LABEL is NULL.  */
881
882 static void
883 add_fde_cfi (const char *label, dw_cfi_ref cfi)
884 {
885   cfi_vec *vec;
886
887   if (cie_cfi_vec == NULL)
888     cie_cfi_vec = VEC_alloc (dw_cfi_ref, gc, 20);
889
890   vec = &cie_cfi_vec;
891
892   if (emit_cfa_remember)
893     {
894       dw_cfi_ref cfi_remember;
895
896       /* Emit the state save.  */
897       emit_cfa_remember = false;
898       cfi_remember = new_cfi ();
899       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
900       add_fde_cfi (label, cfi_remember);
901     }
902
903   if (dwarf2out_do_cfi_asm ())
904     {
905       if (label)
906         {
907           dw_fde_ref fde = current_fde ();
908
909           gcc_assert (fde != NULL);
910
911           /* We still have to add the cfi to the list so that lookup_cfa
912              works later on.  When -g2 and above we even need to force
913              emitting of CFI labels and add to list a DW_CFA_set_loc for
914              convert_cfa_to_fb_loc_list purposes.  If we're generating
915              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
916              convert_cfa_to_fb_loc_list.  */
917           if (dwarf_version == 2
918               && debug_info_level > DINFO_LEVEL_TERSE
919               && (write_symbols == DWARF2_DEBUG
920                   || write_symbols == VMS_AND_DWARF2_DEBUG))
921             {
922               switch (cfi->dw_cfi_opc)
923                 {
924                 case DW_CFA_def_cfa_offset:
925                 case DW_CFA_def_cfa_offset_sf:
926                 case DW_CFA_def_cfa_register:
927                 case DW_CFA_def_cfa:
928                 case DW_CFA_def_cfa_sf:
929                 case DW_CFA_def_cfa_expression:
930                 case DW_CFA_restore_state:
931                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
932                     label = dwarf2out_cfi_label (true);
933
934                   if (fde->dw_fde_current_label == NULL
935                       || strcmp (label, fde->dw_fde_current_label) != 0)
936                     {
937                       dw_cfi_ref xcfi;
938
939                       label = xstrdup (label);
940
941                       /* Set the location counter to the new label.  */
942                       xcfi = new_cfi ();
943                       /* It doesn't metter whether DW_CFA_set_loc
944                          or DW_CFA_advance_loc4 is added here, those aren't
945                          emitted into assembly, only looked up by
946                          convert_cfa_to_fb_loc_list.  */
947                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
948                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
949                       add_cfi (&fde->dw_fde_cfi, xcfi);
950                       fde->dw_fde_current_label = label;
951                     }
952                   break;
953                 default:
954                   break;
955                 }
956             }
957
958           output_cfi_directive (cfi);
959
960           vec = &fde->dw_fde_cfi;
961           any_cfis_emitted = true;
962         }
963       /* ??? If this is a CFI for the CIE, we don't emit.  This
964          assumes that the standard CIE contents that the assembler
965          uses matches the standard CIE contents that the compiler
966          uses.  This is probably a bad assumption.  I'm not quite
967          sure how to address this for now.  */
968     }
969   else if (label)
970     {
971       dw_fde_ref fde = current_fde ();
972
973       gcc_assert (fde != NULL);
974
975       if (*label == 0)
976         label = dwarf2out_cfi_label (false);
977
978       if (fde->dw_fde_current_label == NULL
979           || strcmp (label, fde->dw_fde_current_label) != 0)
980         {
981           dw_cfi_ref xcfi;
982
983           label = xstrdup (label);
984
985           /* Set the location counter to the new label.  */
986           xcfi = new_cfi ();
987           /* If we have a current label, advance from there, otherwise
988              set the location directly using set_loc.  */
989           xcfi->dw_cfi_opc = fde->dw_fde_current_label
990                              ? DW_CFA_advance_loc4
991                              : DW_CFA_set_loc;
992           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
993           add_cfi (&fde->dw_fde_cfi, xcfi);
994
995           fde->dw_fde_current_label = label;
996         }
997
998       vec = &fde->dw_fde_cfi;
999       any_cfis_emitted = true;
1000     }
1001
1002   add_cfi (vec, cfi);
1003 }
1004
1005 /* Subroutine of lookup_cfa.  */
1006
1007 static void
1008 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1009 {
1010   switch (cfi->dw_cfi_opc)
1011     {
1012     case DW_CFA_def_cfa_offset:
1013     case DW_CFA_def_cfa_offset_sf:
1014       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1015       break;
1016     case DW_CFA_def_cfa_register:
1017       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1018       break;
1019     case DW_CFA_def_cfa:
1020     case DW_CFA_def_cfa_sf:
1021       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1022       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1023       break;
1024     case DW_CFA_def_cfa_expression:
1025       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1026       break;
1027
1028     case DW_CFA_remember_state:
1029       gcc_assert (!remember->in_use);
1030       *remember = *loc;
1031       remember->in_use = 1;
1032       break;
1033     case DW_CFA_restore_state:
1034       gcc_assert (remember->in_use);
1035       *loc = *remember;
1036       remember->in_use = 0;
1037       break;
1038
1039     default:
1040       break;
1041     }
1042 }
1043
1044 /* Find the previous value for the CFA.  */
1045
1046 static void
1047 lookup_cfa (dw_cfa_location *loc)
1048 {
1049   int ix;
1050   dw_cfi_ref cfi;
1051   dw_fde_ref fde;
1052   dw_cfa_location remember;
1053
1054   memset (loc, 0, sizeof (*loc));
1055   loc->reg = INVALID_REGNUM;
1056   remember = *loc;
1057
1058   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
1059     lookup_cfa_1 (cfi, loc, &remember);
1060
1061   fde = current_fde ();
1062   if (fde)
1063     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
1064       lookup_cfa_1 (cfi, loc, &remember);
1065 }
1066
1067 /* The current rule for calculating the DWARF2 canonical frame address.  */
1068 static dw_cfa_location cfa;
1069
1070 /* The register used for saving registers to the stack, and its offset
1071    from the CFA.  */
1072 static dw_cfa_location cfa_store;
1073
1074 /* The current save location around an epilogue.  */
1075 static dw_cfa_location cfa_remember;
1076
1077 /* The running total of the size of arguments pushed onto the stack.  */
1078 static HOST_WIDE_INT args_size;
1079
1080 /* The last args_size we actually output.  */
1081 static HOST_WIDE_INT old_args_size;
1082
1083 /* Entry point to update the canonical frame address (CFA).
1084    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1085    calculated from REG+OFFSET.  */
1086
1087 void
1088 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1089 {
1090   dw_cfa_location loc;
1091   loc.indirect = 0;
1092   loc.base_offset = 0;
1093   loc.reg = reg;
1094   loc.offset = offset;
1095   def_cfa_1 (label, &loc);
1096 }
1097
1098 /* Determine if two dw_cfa_location structures define the same data.  */
1099
1100 static bool
1101 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1102 {
1103   return (loc1->reg == loc2->reg
1104           && loc1->offset == loc2->offset
1105           && loc1->indirect == loc2->indirect
1106           && (loc1->indirect == 0
1107               || loc1->base_offset == loc2->base_offset));
1108 }
1109
1110 /* This routine does the actual work.  The CFA is now calculated from
1111    the dw_cfa_location structure.  */
1112
1113 static void
1114 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1115 {
1116   dw_cfi_ref cfi;
1117   dw_cfa_location old_cfa, loc;
1118
1119   cfa = *loc_p;
1120   loc = *loc_p;
1121
1122   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1123     cfa_store.offset = loc.offset;
1124
1125   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1126   lookup_cfa (&old_cfa);
1127
1128   /* If nothing changed, no need to issue any call frame instructions.  */
1129   if (cfa_equal_p (&loc, &old_cfa))
1130     return;
1131
1132   cfi = new_cfi ();
1133
1134   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1135     {
1136       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1137          the CFA register did not change but the offset did.  The data
1138          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1139          in the assembler via the .cfi_def_cfa_offset directive.  */
1140       if (loc.offset < 0)
1141         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1142       else
1143         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1144       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1145     }
1146
1147 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1148   else if (loc.offset == old_cfa.offset
1149            && old_cfa.reg != INVALID_REGNUM
1150            && !loc.indirect
1151            && !old_cfa.indirect)
1152     {
1153       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1154          indicating the CFA register has changed to <register> but the
1155          offset has not changed.  */
1156       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1157       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1158     }
1159 #endif
1160
1161   else if (loc.indirect == 0)
1162     {
1163       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1164          indicating the CFA register has changed to <register> with
1165          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1166          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1167          directive.  */
1168       if (loc.offset < 0)
1169         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1170       else
1171         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1172       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1173       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1174     }
1175   else
1176     {
1177       /* Construct a DW_CFA_def_cfa_expression instruction to
1178          calculate the CFA using a full location expression since no
1179          register-offset pair is available.  */
1180       struct dw_loc_descr_struct *loc_list;
1181
1182       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1183       loc_list = build_cfa_loc (&loc, 0);
1184       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1185     }
1186
1187   add_fde_cfi (label, cfi);
1188 }
1189
1190 /* Add the CFI for saving a register.  REG is the CFA column number.
1191    LABEL is passed to add_fde_cfi.
1192    If SREG is -1, the register is saved at OFFSET from the CFA;
1193    otherwise it is saved in SREG.  */
1194
1195 static void
1196 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1197 {
1198   dw_cfi_ref cfi = new_cfi ();
1199   dw_fde_ref fde = current_fde ();
1200
1201   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1202
1203   /* When stack is aligned, store REG using DW_CFA_expression with
1204      FP.  */
1205   if (fde
1206       && fde->stack_realign
1207       && sreg == INVALID_REGNUM)
1208     {
1209       cfi->dw_cfi_opc = DW_CFA_expression;
1210       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1211       cfi->dw_cfi_oprnd2.dw_cfi_loc
1212         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1213     }
1214   else if (sreg == INVALID_REGNUM)
1215     {
1216       if (need_data_align_sf_opcode (offset))
1217         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1218       else if (reg & ~0x3f)
1219         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1220       else
1221         cfi->dw_cfi_opc = DW_CFA_offset;
1222       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1223     }
1224   else if (sreg == reg)
1225     cfi->dw_cfi_opc = DW_CFA_same_value;
1226   else
1227     {
1228       cfi->dw_cfi_opc = DW_CFA_register;
1229       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1230     }
1231
1232   add_fde_cfi (label, cfi);
1233 }
1234
1235 /* Entry point for saving a register to the stack.  REG is the GCC register
1236    number.  LABEL and OFFSET are passed to reg_save.  */
1237
1238 void
1239 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1240 {
1241   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1242 }
1243
1244 /* Entry point for saving the return address in the stack.
1245    LABEL and OFFSET are passed to reg_save.  */
1246
1247 void
1248 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1249 {
1250   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1251 }
1252
1253 /* Entry point for saving the return address in a register.
1254    LABEL and SREG are passed to reg_save.  */
1255
1256 void
1257 dwarf2out_return_reg (const char *label, unsigned int sreg)
1258 {
1259   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1260 }
1261
1262 /* Record the initial position of the return address.  RTL is
1263    INCOMING_RETURN_ADDR_RTX.  */
1264
1265 static void
1266 initial_return_save (rtx rtl)
1267 {
1268   unsigned int reg = INVALID_REGNUM;
1269   HOST_WIDE_INT offset = 0;
1270
1271   switch (GET_CODE (rtl))
1272     {
1273     case REG:
1274       /* RA is in a register.  */
1275       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1276       break;
1277
1278     case MEM:
1279       /* RA is on the stack.  */
1280       rtl = XEXP (rtl, 0);
1281       switch (GET_CODE (rtl))
1282         {
1283         case REG:
1284           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1285           offset = 0;
1286           break;
1287
1288         case PLUS:
1289           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1290           offset = INTVAL (XEXP (rtl, 1));
1291           break;
1292
1293         case MINUS:
1294           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1295           offset = -INTVAL (XEXP (rtl, 1));
1296           break;
1297
1298         default:
1299           gcc_unreachable ();
1300         }
1301
1302       break;
1303
1304     case PLUS:
1305       /* The return address is at some offset from any value we can
1306          actually load.  For instance, on the SPARC it is in %i7+8. Just
1307          ignore the offset for now; it doesn't matter for unwinding frames.  */
1308       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1309       initial_return_save (XEXP (rtl, 0));
1310       return;
1311
1312     default:
1313       gcc_unreachable ();
1314     }
1315
1316   if (reg != DWARF_FRAME_RETURN_COLUMN)
1317     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1318 }
1319
1320 /* Given a SET, calculate the amount of stack adjustment it
1321    contains.  */
1322
1323 static HOST_WIDE_INT
1324 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1325                      HOST_WIDE_INT cur_offset)
1326 {
1327   const_rtx src = SET_SRC (pattern);
1328   const_rtx dest = SET_DEST (pattern);
1329   HOST_WIDE_INT offset = 0;
1330   enum rtx_code code;
1331
1332   if (dest == stack_pointer_rtx)
1333     {
1334       code = GET_CODE (src);
1335
1336       /* Assume (set (reg sp) (reg whatever)) sets args_size
1337          level to 0.  */
1338       if (code == REG && src != stack_pointer_rtx)
1339         {
1340           offset = -cur_args_size;
1341 #ifndef STACK_GROWS_DOWNWARD
1342           offset = -offset;
1343 #endif
1344           return offset - cur_offset;
1345         }
1346
1347       if (! (code == PLUS || code == MINUS)
1348           || XEXP (src, 0) != stack_pointer_rtx
1349           || !CONST_INT_P (XEXP (src, 1)))
1350         return 0;
1351
1352       /* (set (reg sp) (plus (reg sp) (const_int))) */
1353       offset = INTVAL (XEXP (src, 1));
1354       if (code == PLUS)
1355         offset = -offset;
1356       return offset;
1357     }
1358
1359   if (MEM_P (src) && !MEM_P (dest))
1360     dest = src;
1361   if (MEM_P (dest))
1362     {
1363       /* (set (mem (pre_dec (reg sp))) (foo)) */
1364       src = XEXP (dest, 0);
1365       code = GET_CODE (src);
1366
1367       switch (code)
1368         {
1369         case PRE_MODIFY:
1370         case POST_MODIFY:
1371           if (XEXP (src, 0) == stack_pointer_rtx)
1372             {
1373               rtx val = XEXP (XEXP (src, 1), 1);
1374               /* We handle only adjustments by constant amount.  */
1375               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1376                           && CONST_INT_P (val));
1377               offset = -INTVAL (val);
1378               break;
1379             }
1380           return 0;
1381
1382         case PRE_DEC:
1383         case POST_DEC:
1384           if (XEXP (src, 0) == stack_pointer_rtx)
1385             {
1386               offset = GET_MODE_SIZE (GET_MODE (dest));
1387               break;
1388             }
1389           return 0;
1390
1391         case PRE_INC:
1392         case POST_INC:
1393           if (XEXP (src, 0) == stack_pointer_rtx)
1394             {
1395               offset = -GET_MODE_SIZE (GET_MODE (dest));
1396               break;
1397             }
1398           return 0;
1399
1400         default:
1401           return 0;
1402         }
1403     }
1404   else
1405     return 0;
1406
1407   return offset;
1408 }
1409
1410 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1411    indexed by INSN_UID.  */
1412
1413 static HOST_WIDE_INT *barrier_args_size;
1414
1415 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1416
1417 static HOST_WIDE_INT
1418 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1419                              VEC (rtx, heap) **next)
1420 {
1421   HOST_WIDE_INT offset = 0;
1422   int i;
1423
1424   if (! RTX_FRAME_RELATED_P (insn))
1425     {
1426       if (prologue_epilogue_contains (insn))
1427         /* Nothing */;
1428       else if (GET_CODE (PATTERN (insn)) == SET)
1429         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1430       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1431                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1432         {
1433           /* There may be stack adjustments inside compound insns.  Search
1434              for them.  */
1435           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1436             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1437               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1438                                              cur_args_size, offset);
1439         }
1440     }
1441   else
1442     {
1443       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1444
1445       if (expr)
1446         {
1447           expr = XEXP (expr, 0);
1448           if (GET_CODE (expr) == PARALLEL
1449               || GET_CODE (expr) == SEQUENCE)
1450             for (i = 1; i < XVECLEN (expr, 0); i++)
1451               {
1452                 rtx elem = XVECEXP (expr, 0, i);
1453
1454                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1455                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1456               }
1457         }
1458     }
1459
1460 #ifndef STACK_GROWS_DOWNWARD
1461   offset = -offset;
1462 #endif
1463
1464   cur_args_size += offset;
1465   if (cur_args_size < 0)
1466     cur_args_size = 0;
1467
1468   if (JUMP_P (insn))
1469     {
1470       rtx dest = JUMP_LABEL (insn);
1471
1472       if (dest)
1473         {
1474           if (barrier_args_size [INSN_UID (dest)] < 0)
1475             {
1476               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1477               VEC_safe_push (rtx, heap, *next, dest);
1478             }
1479         }
1480     }
1481
1482   return cur_args_size;
1483 }
1484
1485 /* Walk the whole function and compute args_size on BARRIERs.  */
1486
1487 static void
1488 compute_barrier_args_size (void)
1489 {
1490   int max_uid = get_max_uid (), i;
1491   rtx insn;
1492   VEC (rtx, heap) *worklist, *next, *tmp;
1493
1494   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1495   for (i = 0; i < max_uid; i++)
1496     barrier_args_size[i] = -1;
1497
1498   worklist = VEC_alloc (rtx, heap, 20);
1499   next = VEC_alloc (rtx, heap, 20);
1500   insn = get_insns ();
1501   barrier_args_size[INSN_UID (insn)] = 0;
1502   VEC_quick_push (rtx, worklist, insn);
1503   for (;;)
1504     {
1505       while (!VEC_empty (rtx, worklist))
1506         {
1507           rtx prev, body, first_insn;
1508           HOST_WIDE_INT cur_args_size;
1509
1510           first_insn = insn = VEC_pop (rtx, worklist);
1511           cur_args_size = barrier_args_size[INSN_UID (insn)];
1512           prev = prev_nonnote_insn (insn);
1513           if (prev && BARRIER_P (prev))
1514             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1515
1516           for (; insn; insn = NEXT_INSN (insn))
1517             {
1518               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1519                 continue;
1520               if (BARRIER_P (insn))
1521                 break;
1522
1523               if (LABEL_P (insn))
1524                 {
1525                   if (insn == first_insn)
1526                     continue;
1527                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1528                     {
1529                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1530                       continue;
1531                     }
1532                   else
1533                     {
1534                       /* The insns starting with this label have been
1535                          already scanned or are in the worklist.  */
1536                       break;
1537                     }
1538                 }
1539
1540               body = PATTERN (insn);
1541               if (GET_CODE (body) == SEQUENCE)
1542                 {
1543                   HOST_WIDE_INT dest_args_size = cur_args_size;
1544                   for (i = 1; i < XVECLEN (body, 0); i++)
1545                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1546                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1547                       dest_args_size
1548                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1549                                                        dest_args_size, &next);
1550                     else
1551                       cur_args_size
1552                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1553                                                        cur_args_size, &next);
1554
1555                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1556                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1557                                                  dest_args_size, &next);
1558                   else
1559                     cur_args_size
1560                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1561                                                      cur_args_size, &next);
1562                 }
1563               else
1564                 cur_args_size
1565                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1566             }
1567         }
1568
1569       if (VEC_empty (rtx, next))
1570         break;
1571
1572       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1573       tmp = next;
1574       next = worklist;
1575       worklist = tmp;
1576       VEC_truncate (rtx, next, 0);
1577     }
1578
1579   VEC_free (rtx, heap, worklist);
1580   VEC_free (rtx, heap, next);
1581 }
1582
1583 /* Add a CFI to update the running total of the size of arguments
1584    pushed onto the stack.  */
1585
1586 static void
1587 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1588 {
1589   dw_cfi_ref cfi;
1590
1591   if (size == old_args_size)
1592     return;
1593
1594   old_args_size = size;
1595
1596   cfi = new_cfi ();
1597   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1598   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1599   add_fde_cfi (label, cfi);
1600 }
1601
1602 /* Record a stack adjustment of OFFSET bytes.  */
1603
1604 static void
1605 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1606 {
1607   if (cfa.reg == STACK_POINTER_REGNUM)
1608     cfa.offset += offset;
1609
1610   if (cfa_store.reg == STACK_POINTER_REGNUM)
1611     cfa_store.offset += offset;
1612
1613   if (ACCUMULATE_OUTGOING_ARGS)
1614     return;
1615
1616 #ifndef STACK_GROWS_DOWNWARD
1617   offset = -offset;
1618 #endif
1619
1620   args_size += offset;
1621   if (args_size < 0)
1622     args_size = 0;
1623
1624   def_cfa_1 (label, &cfa);
1625   if (flag_asynchronous_unwind_tables)
1626     dwarf2out_args_size (label, args_size);
1627 }
1628
1629 /* Check INSN to see if it looks like a push or a stack adjustment, and
1630    make a note of it if it does.  EH uses this information to find out
1631    how much extra space it needs to pop off the stack.  */
1632
1633 static void
1634 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1635 {
1636   HOST_WIDE_INT offset;
1637   const char *label;
1638   int i;
1639
1640   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1641      with this function.  Proper support would require all frame-related
1642      insns to be marked, and to be able to handle saving state around
1643      epilogues textually in the middle of the function.  */
1644   if (prologue_epilogue_contains (insn))
1645     return;
1646
1647   /* If INSN is an instruction from target of an annulled branch, the
1648      effects are for the target only and so current argument size
1649      shouldn't change at all.  */
1650   if (final_sequence
1651       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1652       && INSN_FROM_TARGET_P (insn))
1653     return;
1654
1655   /* If only calls can throw, and we have a frame pointer,
1656      save up adjustments until we see the CALL_INSN.  */
1657   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1658     {
1659       if (CALL_P (insn) && !after_p)
1660         {
1661           /* Extract the size of the args from the CALL rtx itself.  */
1662           insn = PATTERN (insn);
1663           if (GET_CODE (insn) == PARALLEL)
1664             insn = XVECEXP (insn, 0, 0);
1665           if (GET_CODE (insn) == SET)
1666             insn = SET_SRC (insn);
1667           gcc_assert (GET_CODE (insn) == CALL);
1668           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1669         }
1670       return;
1671     }
1672
1673   if (CALL_P (insn) && !after_p)
1674     {
1675       if (!flag_asynchronous_unwind_tables)
1676         dwarf2out_args_size ("", args_size);
1677       return;
1678     }
1679   else if (BARRIER_P (insn))
1680     {
1681       /* Don't call compute_barrier_args_size () if the only
1682          BARRIER is at the end of function.  */
1683       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1684         compute_barrier_args_size ();
1685       if (barrier_args_size == NULL)
1686         offset = 0;
1687       else
1688         {
1689           offset = barrier_args_size[INSN_UID (insn)];
1690           if (offset < 0)
1691             offset = 0;
1692         }
1693
1694       offset -= args_size;
1695 #ifndef STACK_GROWS_DOWNWARD
1696       offset = -offset;
1697 #endif
1698     }
1699   else if (GET_CODE (PATTERN (insn)) == SET)
1700     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1701   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1702            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1703     {
1704       /* There may be stack adjustments inside compound insns.  Search
1705          for them.  */
1706       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1707         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1708           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1709                                          args_size, offset);
1710     }
1711   else
1712     return;
1713
1714   if (offset == 0)
1715     return;
1716
1717   label = dwarf2out_cfi_label (false);
1718   dwarf2out_stack_adjust (offset, label);
1719 }
1720
1721 /* We delay emitting a register save until either (a) we reach the end
1722    of the prologue or (b) the register is clobbered.  This clusters
1723    register saves so that there are fewer pc advances.  */
1724
1725 struct GTY(()) queued_reg_save {
1726   struct queued_reg_save *next;
1727   rtx reg;
1728   HOST_WIDE_INT cfa_offset;
1729   rtx saved_reg;
1730 };
1731
1732 static GTY(()) struct queued_reg_save *queued_reg_saves;
1733
1734 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1735 struct GTY(()) reg_saved_in_data {
1736   rtx orig_reg;
1737   rtx saved_in_reg;
1738 };
1739
1740 /* A list of registers saved in other registers.
1741    The list intentionally has a small maximum capacity of 4; if your
1742    port needs more than that, you might consider implementing a
1743    more efficient data structure.  */
1744 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1745 static GTY(()) size_t num_regs_saved_in_regs;
1746
1747 static const char *last_reg_save_label;
1748
1749 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1750    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1751
1752 static void
1753 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1754 {
1755   struct queued_reg_save *q;
1756
1757   /* Duplicates waste space, but it's also necessary to remove them
1758      for correctness, since the queue gets output in reverse
1759      order.  */
1760   for (q = queued_reg_saves; q != NULL; q = q->next)
1761     if (REGNO (q->reg) == REGNO (reg))
1762       break;
1763
1764   if (q == NULL)
1765     {
1766       q = ggc_alloc_queued_reg_save ();
1767       q->next = queued_reg_saves;
1768       queued_reg_saves = q;
1769     }
1770
1771   q->reg = reg;
1772   q->cfa_offset = offset;
1773   q->saved_reg = sreg;
1774
1775   last_reg_save_label = label;
1776 }
1777
1778 /* Output all the entries in QUEUED_REG_SAVES.  */
1779
1780 void
1781 dwarf2out_flush_queued_reg_saves (void)
1782 {
1783   struct queued_reg_save *q;
1784
1785   for (q = queued_reg_saves; q; q = q->next)
1786     {
1787       size_t i;
1788       unsigned int reg, sreg;
1789
1790       for (i = 0; i < num_regs_saved_in_regs; i++)
1791         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1792           break;
1793       if (q->saved_reg && i == num_regs_saved_in_regs)
1794         {
1795           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1796           num_regs_saved_in_regs++;
1797         }
1798       if (i != num_regs_saved_in_regs)
1799         {
1800           regs_saved_in_regs[i].orig_reg = q->reg;
1801           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1802         }
1803
1804       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1805       if (q->saved_reg)
1806         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1807       else
1808         sreg = INVALID_REGNUM;
1809       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1810     }
1811
1812   queued_reg_saves = NULL;
1813   last_reg_save_label = NULL;
1814 }
1815
1816 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1817    location for?  Or, does it clobber a register which we've previously
1818    said that some other register is saved in, and for which we now
1819    have a new location for?  */
1820
1821 static bool
1822 clobbers_queued_reg_save (const_rtx insn)
1823 {
1824   struct queued_reg_save *q;
1825
1826   for (q = queued_reg_saves; q; q = q->next)
1827     {
1828       size_t i;
1829       if (modified_in_p (q->reg, insn))
1830         return true;
1831       for (i = 0; i < num_regs_saved_in_regs; i++)
1832         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1833             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1834           return true;
1835     }
1836
1837   return false;
1838 }
1839
1840 /* Entry point for saving the first register into the second.  */
1841
1842 void
1843 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1844 {
1845   size_t i;
1846   unsigned int regno, sregno;
1847
1848   for (i = 0; i < num_regs_saved_in_regs; i++)
1849     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1850       break;
1851   if (i == num_regs_saved_in_regs)
1852     {
1853       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1854       num_regs_saved_in_regs++;
1855     }
1856   regs_saved_in_regs[i].orig_reg = reg;
1857   regs_saved_in_regs[i].saved_in_reg = sreg;
1858
1859   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1860   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1861   reg_save (label, regno, sregno, 0);
1862 }
1863
1864 /* What register, if any, is currently saved in REG?  */
1865
1866 static rtx
1867 reg_saved_in (rtx reg)
1868 {
1869   unsigned int regn = REGNO (reg);
1870   size_t i;
1871   struct queued_reg_save *q;
1872
1873   for (q = queued_reg_saves; q; q = q->next)
1874     if (q->saved_reg && regn == REGNO (q->saved_reg))
1875       return q->reg;
1876
1877   for (i = 0; i < num_regs_saved_in_regs; i++)
1878     if (regs_saved_in_regs[i].saved_in_reg
1879         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1880       return regs_saved_in_regs[i].orig_reg;
1881
1882   return NULL_RTX;
1883 }
1884
1885
1886 /* A temporary register holding an integral value used in adjusting SP
1887    or setting up the store_reg.  The "offset" field holds the integer
1888    value, not an offset.  */
1889 static dw_cfa_location cfa_temp;
1890
1891 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1892
1893 static void
1894 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1895 {
1896   memset (&cfa, 0, sizeof (cfa));
1897
1898   switch (GET_CODE (pat))
1899     {
1900     case PLUS:
1901       cfa.reg = REGNO (XEXP (pat, 0));
1902       cfa.offset = INTVAL (XEXP (pat, 1));
1903       break;
1904
1905     case REG:
1906       cfa.reg = REGNO (pat);
1907       break;
1908
1909     case MEM:
1910       cfa.indirect = 1;
1911       pat = XEXP (pat, 0);
1912       if (GET_CODE (pat) == PLUS)
1913         {
1914           cfa.base_offset = INTVAL (XEXP (pat, 1));
1915           pat = XEXP (pat, 0);
1916         }
1917       cfa.reg = REGNO (pat);
1918       break;
1919
1920     default:
1921       /* Recurse and define an expression.  */
1922       gcc_unreachable ();
1923     }
1924
1925   def_cfa_1 (label, &cfa);
1926 }
1927
1928 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1929
1930 static void
1931 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1932 {
1933   rtx src, dest;
1934
1935   gcc_assert (GET_CODE (pat) == SET);
1936   dest = XEXP (pat, 0);
1937   src = XEXP (pat, 1);
1938
1939   switch (GET_CODE (src))
1940     {
1941     case PLUS:
1942       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1943       cfa.offset -= INTVAL (XEXP (src, 1));
1944       break;
1945
1946     case REG:
1947         break;
1948
1949     default:
1950         gcc_unreachable ();
1951     }
1952
1953   cfa.reg = REGNO (dest);
1954   gcc_assert (cfa.indirect == 0);
1955
1956   def_cfa_1 (label, &cfa);
1957 }
1958
1959 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1960
1961 static void
1962 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1963 {
1964   HOST_WIDE_INT offset;
1965   rtx src, addr, span;
1966
1967   src = XEXP (set, 1);
1968   addr = XEXP (set, 0);
1969   gcc_assert (MEM_P (addr));
1970   addr = XEXP (addr, 0);
1971
1972   /* As documented, only consider extremely simple addresses.  */
1973   switch (GET_CODE (addr))
1974     {
1975     case REG:
1976       gcc_assert (REGNO (addr) == cfa.reg);
1977       offset = -cfa.offset;
1978       break;
1979     case PLUS:
1980       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1981       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1982       break;
1983     default:
1984       gcc_unreachable ();
1985     }
1986
1987   span = targetm.dwarf_register_span (src);
1988
1989   /* ??? We'd like to use queue_reg_save, but we need to come up with
1990      a different flushing heuristic for epilogues.  */
1991   if (!span)
1992     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1993   else
1994     {
1995       /* We have a PARALLEL describing where the contents of SRC live.
1996          Queue register saves for each piece of the PARALLEL.  */
1997       int par_index;
1998       int limit;
1999       HOST_WIDE_INT span_offset = offset;
2000
2001       gcc_assert (GET_CODE (span) == PARALLEL);
2002
2003       limit = XVECLEN (span, 0);
2004       for (par_index = 0; par_index < limit; par_index++)
2005         {
2006           rtx elem = XVECEXP (span, 0, par_index);
2007
2008           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2009                     INVALID_REGNUM, span_offset);
2010           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2011         }
2012     }
2013 }
2014
2015 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2016
2017 static void
2018 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2019 {
2020   rtx src, dest;
2021   unsigned sregno, dregno;
2022
2023   src = XEXP (set, 1);
2024   dest = XEXP (set, 0);
2025
2026   if (src == pc_rtx)
2027     sregno = DWARF_FRAME_RETURN_COLUMN;
2028   else
2029     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2030
2031   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2032
2033   /* ??? We'd like to use queue_reg_save, but we need to come up with
2034      a different flushing heuristic for epilogues.  */
2035   reg_save (label, sregno, dregno, 0);
2036 }
2037
2038 /* Helper function to get mode of MEM's address.  */
2039
2040 static inline enum machine_mode
2041 get_address_mode (rtx mem)
2042 {
2043   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
2044   if (mode != VOIDmode)
2045     return mode;
2046   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
2047 }
2048
2049 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2050
2051 static void
2052 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2053 {
2054   rtx src, dest, span;
2055   dw_cfi_ref cfi = new_cfi ();
2056
2057   dest = SET_DEST (set);
2058   src = SET_SRC (set);
2059
2060   gcc_assert (REG_P (src));
2061   gcc_assert (MEM_P (dest));
2062
2063   span = targetm.dwarf_register_span (src);
2064   gcc_assert (!span);
2065
2066   cfi->dw_cfi_opc = DW_CFA_expression;
2067   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2068   cfi->dw_cfi_oprnd2.dw_cfi_loc
2069     = mem_loc_descriptor (XEXP (dest, 0), get_address_mode (dest),
2070                           GET_MODE (dest), VAR_INIT_STATUS_INITIALIZED);
2071
2072   /* ??? We'd like to use queue_reg_save, were the interface different,
2073      and, as above, we could manage flushing for epilogues.  */
2074   add_fde_cfi (label, cfi);
2075 }
2076
2077 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2078
2079 static void
2080 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2081 {
2082   dw_cfi_ref cfi = new_cfi ();
2083   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2084
2085   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2086   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2087
2088   add_fde_cfi (label, cfi);
2089 }
2090
2091 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_WINDOW_SAVE.
2092    ??? Perhaps we should note in the CIE where windows are saved (instead of
2093    assuming 0(cfa)) and what registers are in the window.  */
2094
2095 static void
2096 dwarf2out_frame_debug_cfa_window_save (const char *label)
2097 {
2098   dw_cfi_ref cfi = new_cfi ();
2099
2100   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
2101   add_fde_cfi (label, cfi);
2102 }
2103
2104 /* Record call frame debugging information for an expression EXPR,
2105    which either sets SP or FP (adjusting how we calculate the frame
2106    address) or saves a register to the stack or another register.
2107    LABEL indicates the address of EXPR.
2108
2109    This function encodes a state machine mapping rtxes to actions on
2110    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2111    users need not read the source code.
2112
2113   The High-Level Picture
2114
2115   Changes in the register we use to calculate the CFA: Currently we
2116   assume that if you copy the CFA register into another register, we
2117   should take the other one as the new CFA register; this seems to
2118   work pretty well.  If it's wrong for some target, it's simple
2119   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2120
2121   Changes in the register we use for saving registers to the stack:
2122   This is usually SP, but not always.  Again, we deduce that if you
2123   copy SP into another register (and SP is not the CFA register),
2124   then the new register is the one we will be using for register
2125   saves.  This also seems to work.
2126
2127   Register saves: There's not much guesswork about this one; if
2128   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2129   register save, and the register used to calculate the destination
2130   had better be the one we think we're using for this purpose.
2131   It's also assumed that a copy from a call-saved register to another
2132   register is saving that register if RTX_FRAME_RELATED_P is set on
2133   that instruction.  If the copy is from a call-saved register to
2134   the *same* register, that means that the register is now the same
2135   value as in the caller.
2136
2137   Except: If the register being saved is the CFA register, and the
2138   offset is nonzero, we are saving the CFA, so we assume we have to
2139   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2140   the intent is to save the value of SP from the previous frame.
2141
2142   In addition, if a register has previously been saved to a different
2143   register,
2144
2145   Invariants / Summaries of Rules
2146
2147   cfa          current rule for calculating the CFA.  It usually
2148                consists of a register and an offset.
2149   cfa_store    register used by prologue code to save things to the stack
2150                cfa_store.offset is the offset from the value of
2151                cfa_store.reg to the actual CFA
2152   cfa_temp     register holding an integral value.  cfa_temp.offset
2153                stores the value, which will be used to adjust the
2154                stack pointer.  cfa_temp is also used like cfa_store,
2155                to track stores to the stack via fp or a temp reg.
2156
2157   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2158                with cfa.reg as the first operand changes the cfa.reg and its
2159                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2160                cfa_temp.offset.
2161
2162   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2163                expression yielding a constant.  This sets cfa_temp.reg
2164                and cfa_temp.offset.
2165
2166   Rule 5:      Create a new register cfa_store used to save items to the
2167                stack.
2168
2169   Rules 10-14: Save a register to the stack.  Define offset as the
2170                difference of the original location and cfa_store's
2171                location (or cfa_temp's location if cfa_temp is used).
2172
2173   Rules 16-20: If AND operation happens on sp in prologue, we assume
2174                stack is realigned.  We will use a group of DW_OP_XXX
2175                expressions to represent the location of the stored
2176                register instead of CFA+offset.
2177
2178   The Rules
2179
2180   "{a,b}" indicates a choice of a xor b.
2181   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2182
2183   Rule 1:
2184   (set <reg1> <reg2>:cfa.reg)
2185   effects: cfa.reg = <reg1>
2186            cfa.offset unchanged
2187            cfa_temp.reg = <reg1>
2188            cfa_temp.offset = cfa.offset
2189
2190   Rule 2:
2191   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2192                               {<const_int>,<reg>:cfa_temp.reg}))
2193   effects: cfa.reg = sp if fp used
2194            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2195            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2196              if cfa_store.reg==sp
2197
2198   Rule 3:
2199   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2200   effects: cfa.reg = fp
2201            cfa_offset += +/- <const_int>
2202
2203   Rule 4:
2204   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2205   constraints: <reg1> != fp
2206                <reg1> != sp
2207   effects: cfa.reg = <reg1>
2208            cfa_temp.reg = <reg1>
2209            cfa_temp.offset = cfa.offset
2210
2211   Rule 5:
2212   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2213   constraints: <reg1> != fp
2214                <reg1> != sp
2215   effects: cfa_store.reg = <reg1>
2216            cfa_store.offset = cfa.offset - cfa_temp.offset
2217
2218   Rule 6:
2219   (set <reg> <const_int>)
2220   effects: cfa_temp.reg = <reg>
2221            cfa_temp.offset = <const_int>
2222
2223   Rule 7:
2224   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2225   effects: cfa_temp.reg = <reg1>
2226            cfa_temp.offset |= <const_int>
2227
2228   Rule 8:
2229   (set <reg> (high <exp>))
2230   effects: none
2231
2232   Rule 9:
2233   (set <reg> (lo_sum <exp> <const_int>))
2234   effects: cfa_temp.reg = <reg>
2235            cfa_temp.offset = <const_int>
2236
2237   Rule 10:
2238   (set (mem ({pre,post}_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2239   effects: cfa_store.offset -= <const_int>
2240            cfa.offset = cfa_store.offset if cfa.reg == sp
2241            cfa.reg = sp
2242            cfa.base_offset = -cfa_store.offset
2243
2244   Rule 11:
2245   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2246   effects: cfa_store.offset += -/+ mode_size(mem)
2247            cfa.offset = cfa_store.offset if cfa.reg == sp
2248            cfa.reg = sp
2249            cfa.base_offset = -cfa_store.offset
2250
2251   Rule 12:
2252   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2253
2254        <reg2>)
2255   effects: cfa.reg = <reg1>
2256            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2257
2258   Rule 13:
2259   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2260   effects: cfa.reg = <reg1>
2261            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2262
2263   Rule 14:
2264   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2265   effects: cfa.reg = <reg1>
2266            cfa.base_offset = -cfa_temp.offset
2267            cfa_temp.offset -= mode_size(mem)
2268
2269   Rule 15:
2270   (set <reg> {unspec, unspec_volatile})
2271   effects: target-dependent
2272
2273   Rule 16:
2274   (set sp (and: sp <const_int>))
2275   constraints: cfa_store.reg == sp
2276   effects: current_fde.stack_realign = 1
2277            cfa_store.offset = 0
2278            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2279
2280   Rule 17:
2281   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2282   effects: cfa_store.offset += -/+ mode_size(mem)
2283
2284   Rule 18:
2285   (set (mem ({pre_inc, pre_dec} sp)) fp)
2286   constraints: fde->stack_realign == 1
2287   effects: cfa_store.offset = 0
2288            cfa.reg != HARD_FRAME_POINTER_REGNUM
2289
2290   Rule 19:
2291   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2292   constraints: fde->stack_realign == 1
2293                && cfa.offset == 0
2294                && cfa.indirect == 0
2295                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2296   effects: Use DW_CFA_def_cfa_expression to define cfa
2297            cfa.reg == fde->drap_reg  */
2298
2299 static void
2300 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2301 {
2302   rtx src, dest, span;
2303   HOST_WIDE_INT offset;
2304   dw_fde_ref fde;
2305
2306   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2307      the PARALLEL independently. The first element is always processed if
2308      it is a SET. This is for backward compatibility.   Other elements
2309      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2310      flag is set in them.  */
2311   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2312     {
2313       int par_index;
2314       int limit = XVECLEN (expr, 0);
2315       rtx elem;
2316
2317       /* PARALLELs have strict read-modify-write semantics, so we
2318          ought to evaluate every rvalue before changing any lvalue.
2319          It's cumbersome to do that in general, but there's an
2320          easy approximation that is enough for all current users:
2321          handle register saves before register assignments.  */
2322       if (GET_CODE (expr) == PARALLEL)
2323         for (par_index = 0; par_index < limit; par_index++)
2324           {
2325             elem = XVECEXP (expr, 0, par_index);
2326             if (GET_CODE (elem) == SET
2327                 && MEM_P (SET_DEST (elem))
2328                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2329               dwarf2out_frame_debug_expr (elem, label);
2330           }
2331
2332       for (par_index = 0; par_index < limit; par_index++)
2333         {
2334           elem = XVECEXP (expr, 0, par_index);
2335           if (GET_CODE (elem) == SET
2336               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2337               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2338             dwarf2out_frame_debug_expr (elem, label);
2339           else if (GET_CODE (elem) == SET
2340                    && par_index != 0
2341                    && !RTX_FRAME_RELATED_P (elem))
2342             {
2343               /* Stack adjustment combining might combine some post-prologue
2344                  stack adjustment into a prologue stack adjustment.  */
2345               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2346
2347               if (offset != 0)
2348                 dwarf2out_stack_adjust (offset, label);
2349             }
2350         }
2351       return;
2352     }
2353
2354   gcc_assert (GET_CODE (expr) == SET);
2355
2356   src = SET_SRC (expr);
2357   dest = SET_DEST (expr);
2358
2359   if (REG_P (src))
2360     {
2361       rtx rsi = reg_saved_in (src);
2362       if (rsi)
2363         src = rsi;
2364     }
2365
2366   fde = current_fde ();
2367
2368   switch (GET_CODE (dest))
2369     {
2370     case REG:
2371       switch (GET_CODE (src))
2372         {
2373           /* Setting FP from SP.  */
2374         case REG:
2375           if (cfa.reg == (unsigned) REGNO (src))
2376             {
2377               /* Rule 1 */
2378               /* Update the CFA rule wrt SP or FP.  Make sure src is
2379                  relative to the current CFA register.
2380
2381                  We used to require that dest be either SP or FP, but the
2382                  ARM copies SP to a temporary register, and from there to
2383                  FP.  So we just rely on the backends to only set
2384                  RTX_FRAME_RELATED_P on appropriate insns.  */
2385               cfa.reg = REGNO (dest);
2386               cfa_temp.reg = cfa.reg;
2387               cfa_temp.offset = cfa.offset;
2388             }
2389           else
2390             {
2391               /* Saving a register in a register.  */
2392               gcc_assert (!fixed_regs [REGNO (dest)]
2393                           /* For the SPARC and its register window.  */
2394                           || (DWARF_FRAME_REGNUM (REGNO (src))
2395                               == DWARF_FRAME_RETURN_COLUMN));
2396
2397               /* After stack is aligned, we can only save SP in FP
2398                  if drap register is used.  In this case, we have
2399                  to restore stack pointer with the CFA value and we
2400                  don't generate this DWARF information.  */
2401               if (fde
2402                   && fde->stack_realign
2403                   && REGNO (src) == STACK_POINTER_REGNUM)
2404                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2405                             && fde->drap_reg != INVALID_REGNUM
2406                             && cfa.reg != REGNO (src));
2407               else
2408                 queue_reg_save (label, src, dest, 0);
2409             }
2410           break;
2411
2412         case PLUS:
2413         case MINUS:
2414         case LO_SUM:
2415           if (dest == stack_pointer_rtx)
2416             {
2417               /* Rule 2 */
2418               /* Adjusting SP.  */
2419               switch (GET_CODE (XEXP (src, 1)))
2420                 {
2421                 case CONST_INT:
2422                   offset = INTVAL (XEXP (src, 1));
2423                   break;
2424                 case REG:
2425                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2426                               == cfa_temp.reg);
2427                   offset = cfa_temp.offset;
2428                   break;
2429                 default:
2430                   gcc_unreachable ();
2431                 }
2432
2433               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2434                 {
2435                   /* Restoring SP from FP in the epilogue.  */
2436                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2437                   cfa.reg = STACK_POINTER_REGNUM;
2438                 }
2439               else if (GET_CODE (src) == LO_SUM)
2440                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2441                 ;
2442               else
2443                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2444
2445               if (GET_CODE (src) != MINUS)
2446                 offset = -offset;
2447               if (cfa.reg == STACK_POINTER_REGNUM)
2448                 cfa.offset += offset;
2449               if (cfa_store.reg == STACK_POINTER_REGNUM)
2450                 cfa_store.offset += offset;
2451             }
2452           else if (dest == hard_frame_pointer_rtx)
2453             {
2454               /* Rule 3 */
2455               /* Either setting the FP from an offset of the SP,
2456                  or adjusting the FP */
2457               gcc_assert (frame_pointer_needed);
2458
2459               gcc_assert (REG_P (XEXP (src, 0))
2460                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2461                           && CONST_INT_P (XEXP (src, 1)));
2462               offset = INTVAL (XEXP (src, 1));
2463               if (GET_CODE (src) != MINUS)
2464                 offset = -offset;
2465               cfa.offset += offset;
2466               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2467             }
2468           else
2469             {
2470               gcc_assert (GET_CODE (src) != MINUS);
2471
2472               /* Rule 4 */
2473               if (REG_P (XEXP (src, 0))
2474                   && REGNO (XEXP (src, 0)) == cfa.reg
2475                   && CONST_INT_P (XEXP (src, 1)))
2476                 {
2477                   /* Setting a temporary CFA register that will be copied
2478                      into the FP later on.  */
2479                   offset = - INTVAL (XEXP (src, 1));
2480                   cfa.offset += offset;
2481                   cfa.reg = REGNO (dest);
2482                   /* Or used to save regs to the stack.  */
2483                   cfa_temp.reg = cfa.reg;
2484                   cfa_temp.offset = cfa.offset;
2485                 }
2486
2487               /* Rule 5 */
2488               else if (REG_P (XEXP (src, 0))
2489                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2490                        && XEXP (src, 1) == stack_pointer_rtx)
2491                 {
2492                   /* Setting a scratch register that we will use instead
2493                      of SP for saving registers to the stack.  */
2494                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2495                   cfa_store.reg = REGNO (dest);
2496                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2497                 }
2498
2499               /* Rule 9 */
2500               else if (GET_CODE (src) == LO_SUM
2501                        && CONST_INT_P (XEXP (src, 1)))
2502                 {
2503                   cfa_temp.reg = REGNO (dest);
2504                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2505                 }
2506               else
2507                 gcc_unreachable ();
2508             }
2509           break;
2510
2511           /* Rule 6 */
2512         case CONST_INT:
2513           cfa_temp.reg = REGNO (dest);
2514           cfa_temp.offset = INTVAL (src);
2515           break;
2516
2517           /* Rule 7 */
2518         case IOR:
2519           gcc_assert (REG_P (XEXP (src, 0))
2520                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2521                       && CONST_INT_P (XEXP (src, 1)));
2522
2523           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2524             cfa_temp.reg = REGNO (dest);
2525           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2526           break;
2527
2528           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2529              which will fill in all of the bits.  */
2530           /* Rule 8 */
2531         case HIGH:
2532           break;
2533
2534           /* Rule 15 */
2535         case UNSPEC:
2536         case UNSPEC_VOLATILE:
2537           gcc_assert (targetm.dwarf_handle_frame_unspec);
2538           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2539           return;
2540
2541           /* Rule 16 */
2542         case AND:
2543           /* If this AND operation happens on stack pointer in prologue,
2544              we assume the stack is realigned and we extract the
2545              alignment.  */
2546           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2547             {
2548               /* We interpret reg_save differently with stack_realign set.
2549                  Thus we must flush whatever we have queued first.  */
2550               dwarf2out_flush_queued_reg_saves ();
2551
2552               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2553               fde->stack_realign = 1;
2554               fde->stack_realignment = INTVAL (XEXP (src, 1));
2555               cfa_store.offset = 0;
2556
2557               if (cfa.reg != STACK_POINTER_REGNUM
2558                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2559                 fde->drap_reg = cfa.reg;
2560             }
2561           return;
2562
2563         default:
2564           gcc_unreachable ();
2565         }
2566
2567       def_cfa_1 (label, &cfa);
2568       break;
2569
2570     case MEM:
2571
2572       /* Saving a register to the stack.  Make sure dest is relative to the
2573          CFA register.  */
2574       switch (GET_CODE (XEXP (dest, 0)))
2575         {
2576           /* Rule 10 */
2577           /* With a push.  */
2578         case PRE_MODIFY:
2579         case POST_MODIFY:
2580           /* We can't handle variable size modifications.  */
2581           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2582                       == CONST_INT);
2583           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2584
2585           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2586                       && cfa_store.reg == STACK_POINTER_REGNUM);
2587
2588           cfa_store.offset += offset;
2589           if (cfa.reg == STACK_POINTER_REGNUM)
2590             cfa.offset = cfa_store.offset;
2591
2592           if (GET_CODE (XEXP (dest, 0)) == POST_MODIFY)
2593             offset -= cfa_store.offset;
2594           else
2595             offset = -cfa_store.offset;
2596           break;
2597
2598           /* Rule 11 */
2599         case PRE_INC:
2600         case PRE_DEC:
2601         case POST_DEC:
2602           offset = GET_MODE_SIZE (GET_MODE (dest));
2603           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2604             offset = -offset;
2605
2606           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2607                        == STACK_POINTER_REGNUM)
2608                       && cfa_store.reg == STACK_POINTER_REGNUM);
2609
2610           cfa_store.offset += offset;
2611
2612           /* Rule 18: If stack is aligned, we will use FP as a
2613              reference to represent the address of the stored
2614              regiser.  */
2615           if (fde
2616               && fde->stack_realign
2617               && src == hard_frame_pointer_rtx)
2618             {
2619               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2620               cfa_store.offset = 0;
2621             }
2622
2623           if (cfa.reg == STACK_POINTER_REGNUM)
2624             cfa.offset = cfa_store.offset;
2625
2626           if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2627             offset += -cfa_store.offset;
2628           else
2629             offset = -cfa_store.offset;
2630           break;
2631
2632           /* Rule 12 */
2633           /* With an offset.  */
2634         case PLUS:
2635         case MINUS:
2636         case LO_SUM:
2637           {
2638             int regno;
2639
2640             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2641                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2642             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2643             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2644               offset = -offset;
2645
2646             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2647
2648             if (cfa.reg == (unsigned) regno)
2649               offset -= cfa.offset;
2650             else if (cfa_store.reg == (unsigned) regno)
2651               offset -= cfa_store.offset;
2652             else
2653               {
2654                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2655                 offset -= cfa_temp.offset;
2656               }
2657           }
2658           break;
2659
2660           /* Rule 13 */
2661           /* Without an offset.  */
2662         case REG:
2663           {
2664             int regno = REGNO (XEXP (dest, 0));
2665
2666             if (cfa.reg == (unsigned) regno)
2667               offset = -cfa.offset;
2668             else if (cfa_store.reg == (unsigned) regno)
2669               offset = -cfa_store.offset;
2670             else
2671               {
2672                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2673                 offset = -cfa_temp.offset;
2674               }
2675           }
2676           break;
2677
2678           /* Rule 14 */
2679         case POST_INC:
2680           gcc_assert (cfa_temp.reg
2681                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2682           offset = -cfa_temp.offset;
2683           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2684           break;
2685
2686         default:
2687           gcc_unreachable ();
2688         }
2689
2690         /* Rule 17 */
2691         /* If the source operand of this MEM operation is not a
2692            register, basically the source is return address.  Here
2693            we only care how much stack grew and we don't save it.  */
2694       if (!REG_P (src))
2695         break;
2696
2697       if (REGNO (src) != STACK_POINTER_REGNUM
2698           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2699           && (unsigned) REGNO (src) == cfa.reg)
2700         {
2701           /* We're storing the current CFA reg into the stack.  */
2702
2703           if (cfa.offset == 0)
2704             {
2705               /* Rule 19 */
2706               /* If stack is aligned, putting CFA reg into stack means
2707                  we can no longer use reg + offset to represent CFA.
2708                  Here we use DW_CFA_def_cfa_expression instead.  The
2709                  result of this expression equals to the original CFA
2710                  value.  */
2711               if (fde
2712                   && fde->stack_realign
2713                   && cfa.indirect == 0
2714                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2715                 {
2716                   dw_cfa_location cfa_exp;
2717
2718                   gcc_assert (fde->drap_reg == cfa.reg);
2719
2720                   cfa_exp.indirect = 1;
2721                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2722                   cfa_exp.base_offset = offset;
2723                   cfa_exp.offset = 0;
2724
2725                   fde->drap_reg_saved = 1;
2726
2727                   def_cfa_1 (label, &cfa_exp);
2728                   break;
2729                 }
2730
2731               /* If the source register is exactly the CFA, assume
2732                  we're saving SP like any other register; this happens
2733                  on the ARM.  */
2734               def_cfa_1 (label, &cfa);
2735               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2736               break;
2737             }
2738           else
2739             {
2740               /* Otherwise, we'll need to look in the stack to
2741                  calculate the CFA.  */
2742               rtx x = XEXP (dest, 0);
2743
2744               if (!REG_P (x))
2745                 x = XEXP (x, 0);
2746               gcc_assert (REG_P (x));
2747
2748               cfa.reg = REGNO (x);
2749               cfa.base_offset = offset;
2750               cfa.indirect = 1;
2751               def_cfa_1 (label, &cfa);
2752               break;
2753             }
2754         }
2755
2756       def_cfa_1 (label, &cfa);
2757       {
2758         span = targetm.dwarf_register_span (src);
2759
2760         if (!span)
2761           queue_reg_save (label, src, NULL_RTX, offset);
2762         else
2763           {
2764             /* We have a PARALLEL describing where the contents of SRC
2765                live.  Queue register saves for each piece of the
2766                PARALLEL.  */
2767             int par_index;
2768             int limit;
2769             HOST_WIDE_INT span_offset = offset;
2770
2771             gcc_assert (GET_CODE (span) == PARALLEL);
2772
2773             limit = XVECLEN (span, 0);
2774             for (par_index = 0; par_index < limit; par_index++)
2775               {
2776                 rtx elem = XVECEXP (span, 0, par_index);
2777
2778                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2779                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2780               }
2781           }
2782       }
2783       break;
2784
2785     default:
2786       gcc_unreachable ();
2787     }
2788 }
2789
2790 /* Record call frame debugging information for INSN, which either
2791    sets SP or FP (adjusting how we calculate the frame address) or saves a
2792    register to the stack.  If INSN is NULL_RTX, initialize our state.
2793
2794    If AFTER_P is false, we're being called before the insn is emitted,
2795    otherwise after.  Call instructions get invoked twice.  */
2796
2797 void
2798 dwarf2out_frame_debug (rtx insn, bool after_p)
2799 {
2800   const char *label;
2801   rtx note, n;
2802   bool handled_one = false;
2803
2804   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2805     dwarf2out_flush_queued_reg_saves ();
2806
2807   if (!RTX_FRAME_RELATED_P (insn))
2808     {
2809       /* ??? This should be done unconditionally since stack adjustments
2810          matter if the stack pointer is not the CFA register anymore but
2811          is still used to save registers.  */
2812       if (!ACCUMULATE_OUTGOING_ARGS)
2813         dwarf2out_notice_stack_adjust (insn, after_p);
2814       return;
2815     }
2816
2817   label = dwarf2out_cfi_label (false);
2818   any_cfis_emitted = false;
2819
2820   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2821     switch (REG_NOTE_KIND (note))
2822       {
2823       case REG_FRAME_RELATED_EXPR:
2824         insn = XEXP (note, 0);
2825         goto found;
2826
2827       case REG_CFA_DEF_CFA:
2828         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2829         handled_one = true;
2830         break;
2831
2832       case REG_CFA_ADJUST_CFA:
2833         n = XEXP (note, 0);
2834         if (n == NULL)
2835           {
2836             n = PATTERN (insn);
2837             if (GET_CODE (n) == PARALLEL)
2838               n = XVECEXP (n, 0, 0);
2839           }
2840         dwarf2out_frame_debug_adjust_cfa (n, label);
2841         handled_one = true;
2842         break;
2843
2844       case REG_CFA_OFFSET:
2845         n = XEXP (note, 0);
2846         if (n == NULL)
2847           n = single_set (insn);
2848         dwarf2out_frame_debug_cfa_offset (n, label);
2849         handled_one = true;
2850         break;
2851
2852       case REG_CFA_REGISTER:
2853         n = XEXP (note, 0);
2854         if (n == NULL)
2855           {
2856             n = PATTERN (insn);
2857             if (GET_CODE (n) == PARALLEL)
2858               n = XVECEXP (n, 0, 0);
2859           }
2860         dwarf2out_frame_debug_cfa_register (n, label);
2861         handled_one = true;
2862         break;
2863
2864       case REG_CFA_EXPRESSION:
2865         n = XEXP (note, 0);
2866         if (n == NULL)
2867           n = single_set (insn);
2868         dwarf2out_frame_debug_cfa_expression (n, label);
2869         handled_one = true;
2870         break;
2871
2872       case REG_CFA_RESTORE:
2873         n = XEXP (note, 0);
2874         if (n == NULL)
2875           {
2876             n = PATTERN (insn);
2877             if (GET_CODE (n) == PARALLEL)
2878               n = XVECEXP (n, 0, 0);
2879             n = XEXP (n, 0);
2880           }
2881         dwarf2out_frame_debug_cfa_restore (n, label);
2882         handled_one = true;
2883         break;
2884
2885       case REG_CFA_SET_VDRAP:
2886         n = XEXP (note, 0);
2887         if (REG_P (n))
2888           {
2889             dw_fde_ref fde = current_fde ();
2890             if (fde)
2891               {
2892                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2893                 if (REG_P (n))
2894                   fde->vdrap_reg = REGNO (n);
2895               }
2896           }
2897         handled_one = true;
2898         break;
2899
2900       case REG_CFA_WINDOW_SAVE:
2901         dwarf2out_frame_debug_cfa_window_save (label);
2902         handled_one = true;
2903         break;
2904
2905       default:
2906         break;
2907       }
2908   if (handled_one)
2909     {
2910       if (any_cfis_emitted)
2911         dwarf2out_flush_queued_reg_saves ();
2912       return;
2913     }
2914
2915   insn = PATTERN (insn);
2916  found:
2917   dwarf2out_frame_debug_expr (insn, label);
2918
2919   /* Check again.  A parallel can save and update the same register.
2920      We could probably check just once, here, but this is safer than
2921      removing the check above.  */
2922   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2923     dwarf2out_flush_queued_reg_saves ();
2924 }
2925
2926 /* Called once at the start of final to initialize some data for the
2927    current function.  */
2928 void
2929 dwarf2out_frame_debug_init (void)
2930 {
2931   size_t i;
2932
2933   /* Flush any queued register saves.  */
2934   dwarf2out_flush_queued_reg_saves ();
2935
2936   /* Set up state for generating call frame debug info.  */
2937   lookup_cfa (&cfa);
2938   gcc_assert (cfa.reg
2939               == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2940
2941   cfa.reg = STACK_POINTER_REGNUM;
2942   cfa_store = cfa;
2943   cfa_temp.reg = -1;
2944   cfa_temp.offset = 0;
2945
2946   for (i = 0; i < num_regs_saved_in_regs; i++)
2947     {
2948       regs_saved_in_regs[i].orig_reg = NULL_RTX;
2949       regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2950     }
2951   num_regs_saved_in_regs = 0;
2952
2953   if (barrier_args_size)
2954     {
2955       XDELETEVEC (barrier_args_size);
2956       barrier_args_size = NULL;
2957     }
2958 }
2959
2960 /* Determine if we need to save and restore CFI information around this
2961    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2962    we do need to save/restore, then emit the save now, and insert a
2963    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2964
2965 void
2966 dwarf2out_cfi_begin_epilogue (rtx insn)
2967 {
2968   bool saw_frp = false;
2969   rtx i;
2970
2971   /* Scan forward to the return insn, noticing if there are possible
2972      frame related insns.  */
2973   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2974     {
2975       if (!INSN_P (i))
2976         continue;
2977
2978       /* Look for both regular and sibcalls to end the block.  */
2979       if (returnjump_p (i))
2980         break;
2981       if (CALL_P (i) && SIBLING_CALL_P (i))
2982         break;
2983
2984       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2985         {
2986           int idx;
2987           rtx seq = PATTERN (i);
2988
2989           if (returnjump_p (XVECEXP (seq, 0, 0)))
2990             break;
2991           if (CALL_P (XVECEXP (seq, 0, 0))
2992               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2993             break;
2994
2995           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2996             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2997               saw_frp = true;
2998         }
2999
3000       if (RTX_FRAME_RELATED_P (i))
3001         saw_frp = true;
3002     }
3003
3004   /* If the port doesn't emit epilogue unwind info, we don't need a
3005      save/restore pair.  */
3006   if (!saw_frp)
3007     return;
3008
3009   /* Otherwise, search forward to see if the return insn was the last
3010      basic block of the function.  If so, we don't need save/restore.  */
3011   gcc_assert (i != NULL);
3012   i = next_real_insn (i);
3013   if (i == NULL)
3014     return;
3015
3016   /* Insert the restore before that next real insn in the stream, and before
3017      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3018      properly nested.  This should be after any label or alignment.  This
3019      will be pushed into the CFI stream by the function below.  */
3020   while (1)
3021     {
3022       rtx p = PREV_INSN (i);
3023       if (!NOTE_P (p))
3024         break;
3025       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3026         break;
3027       i = p;
3028     }
3029   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3030
3031   emit_cfa_remember = true;
3032
3033   /* And emulate the state save.  */
3034   gcc_assert (!cfa_remember.in_use);
3035   cfa_remember = cfa;
3036   cfa_remember.in_use = 1;
3037 }
3038
3039 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3040    required.  */
3041
3042 void
3043 dwarf2out_frame_debug_restore_state (void)
3044 {
3045   dw_cfi_ref cfi = new_cfi ();
3046   const char *label = dwarf2out_cfi_label (false);
3047
3048   cfi->dw_cfi_opc = DW_CFA_restore_state;
3049   add_fde_cfi (label, cfi);
3050
3051   gcc_assert (cfa_remember.in_use);
3052   cfa = cfa_remember;
3053   cfa_remember.in_use = 0;
3054 }
3055
3056 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3057 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3058  (enum dwarf_call_frame_info cfi);
3059
3060 static enum dw_cfi_oprnd_type
3061 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3062 {
3063   switch (cfi)
3064     {
3065     case DW_CFA_nop:
3066     case DW_CFA_GNU_window_save:
3067     case DW_CFA_remember_state:
3068     case DW_CFA_restore_state:
3069       return dw_cfi_oprnd_unused;
3070
3071     case DW_CFA_set_loc:
3072     case DW_CFA_advance_loc1:
3073     case DW_CFA_advance_loc2:
3074     case DW_CFA_advance_loc4:
3075     case DW_CFA_MIPS_advance_loc8:
3076       return dw_cfi_oprnd_addr;
3077
3078     case DW_CFA_offset:
3079     case DW_CFA_offset_extended:
3080     case DW_CFA_def_cfa:
3081     case DW_CFA_offset_extended_sf:
3082     case DW_CFA_def_cfa_sf:
3083     case DW_CFA_restore:
3084     case DW_CFA_restore_extended:
3085     case DW_CFA_undefined:
3086     case DW_CFA_same_value:
3087     case DW_CFA_def_cfa_register:
3088     case DW_CFA_register:
3089     case DW_CFA_expression:
3090       return dw_cfi_oprnd_reg_num;
3091
3092     case DW_CFA_def_cfa_offset:
3093     case DW_CFA_GNU_args_size:
3094     case DW_CFA_def_cfa_offset_sf:
3095       return dw_cfi_oprnd_offset;
3096
3097     case DW_CFA_def_cfa_expression:
3098       return dw_cfi_oprnd_loc;
3099
3100     default:
3101       gcc_unreachable ();
3102     }
3103 }
3104
3105 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3106 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3107  (enum dwarf_call_frame_info cfi);
3108
3109 static enum dw_cfi_oprnd_type
3110 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3111 {
3112   switch (cfi)
3113     {
3114     case DW_CFA_def_cfa:
3115     case DW_CFA_def_cfa_sf:
3116     case DW_CFA_offset:
3117     case DW_CFA_offset_extended_sf:
3118     case DW_CFA_offset_extended:
3119       return dw_cfi_oprnd_offset;
3120
3121     case DW_CFA_register:
3122       return dw_cfi_oprnd_reg_num;
3123
3124     case DW_CFA_expression:
3125       return dw_cfi_oprnd_loc;
3126
3127     default:
3128       return dw_cfi_oprnd_unused;
3129     }
3130 }
3131
3132 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3133    switch to the data section instead, and write out a synthetic start label
3134    for collect2 the first time around.  */
3135
3136 static void
3137 switch_to_eh_frame_section (bool back)
3138 {
3139   tree label;
3140
3141 #ifdef EH_FRAME_SECTION_NAME
3142   if (eh_frame_section == 0)
3143     {
3144       int flags;
3145
3146       if (EH_TABLES_CAN_BE_READ_ONLY)
3147         {
3148           int fde_encoding;
3149           int per_encoding;
3150           int lsda_encoding;
3151
3152           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3153                                                        /*global=*/0);
3154           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3155                                                        /*global=*/1);
3156           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3157                                                         /*global=*/0);
3158           flags = ((! flag_pic
3159                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3160                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3161                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3162                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3163                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3164                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3165                    ? 0 : SECTION_WRITE);
3166         }
3167       else
3168         flags = SECTION_WRITE;
3169       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3170     }
3171 #endif /* EH_FRAME_SECTION_NAME */
3172
3173   if (eh_frame_section)
3174     switch_to_section (eh_frame_section);
3175   else
3176     {
3177       /* We have no special eh_frame section.  Put the information in
3178          the data section and emit special labels to guide collect2.  */
3179       switch_to_section (data_section);
3180
3181       if (!back)
3182         {
3183           label = get_file_function_name ("F");
3184           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3185           targetm.asm_out.globalize_label (asm_out_file,
3186                                            IDENTIFIER_POINTER (label));
3187           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3188         }
3189     }
3190 }
3191
3192 /* Switch [BACK] to the eh or debug frame table section, depending on
3193    FOR_EH.  */
3194
3195 static void
3196 switch_to_frame_table_section (int for_eh, bool back)
3197 {
3198   if (for_eh)
3199     switch_to_eh_frame_section (back);
3200   else
3201     {
3202       if (!debug_frame_section)
3203         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3204                                            SECTION_DEBUG, NULL);
3205       switch_to_section (debug_frame_section);
3206     }
3207 }
3208
3209 /* Output a Call Frame Information opcode and its operand(s).  */
3210
3211 static void
3212 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3213 {
3214   unsigned long r;
3215   HOST_WIDE_INT off;
3216
3217   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3218     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3219                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3220                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3221                          ((unsigned HOST_WIDE_INT)
3222                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3223   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3224     {
3225       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3226       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3227                            "DW_CFA_offset, column %#lx", r);
3228       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3229       dw2_asm_output_data_uleb128 (off, NULL);
3230     }
3231   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3232     {
3233       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3234       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3235                            "DW_CFA_restore, column %#lx", r);
3236     }
3237   else
3238     {
3239       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3240                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3241
3242       switch (cfi->dw_cfi_opc)
3243         {
3244         case DW_CFA_set_loc:
3245           if (for_eh)
3246             dw2_asm_output_encoded_addr_rtx (
3247                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3248                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3249                 false, NULL);
3250           else
3251             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3252                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3253           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3254           break;
3255
3256         case DW_CFA_advance_loc1:
3257           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3258                                 fde->dw_fde_current_label, NULL);
3259           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3260           break;
3261
3262         case DW_CFA_advance_loc2:
3263           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3264                                 fde->dw_fde_current_label, NULL);
3265           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3266           break;
3267
3268         case DW_CFA_advance_loc4:
3269           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3270                                 fde->dw_fde_current_label, NULL);
3271           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3272           break;
3273
3274         case DW_CFA_MIPS_advance_loc8:
3275           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3276                                 fde->dw_fde_current_label, NULL);
3277           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3278           break;
3279
3280         case DW_CFA_offset_extended:
3281           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3282           dw2_asm_output_data_uleb128 (r, NULL);
3283           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3284           dw2_asm_output_data_uleb128 (off, NULL);
3285           break;
3286
3287         case DW_CFA_def_cfa:
3288           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3289           dw2_asm_output_data_uleb128 (r, NULL);
3290           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3291           break;
3292
3293         case DW_CFA_offset_extended_sf:
3294           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3295           dw2_asm_output_data_uleb128 (r, NULL);
3296           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3297           dw2_asm_output_data_sleb128 (off, NULL);
3298           break;
3299
3300         case DW_CFA_def_cfa_sf:
3301           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3302           dw2_asm_output_data_uleb128 (r, NULL);
3303           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3304           dw2_asm_output_data_sleb128 (off, NULL);
3305           break;
3306
3307         case DW_CFA_restore_extended:
3308         case DW_CFA_undefined:
3309         case DW_CFA_same_value:
3310         case DW_CFA_def_cfa_register:
3311           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3312           dw2_asm_output_data_uleb128 (r, NULL);
3313           break;
3314
3315         case DW_CFA_register:
3316           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3317           dw2_asm_output_data_uleb128 (r, NULL);
3318           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3319           dw2_asm_output_data_uleb128 (r, NULL);
3320           break;
3321
3322         case DW_CFA_def_cfa_offset:
3323         case DW_CFA_GNU_args_size:
3324           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3325           break;
3326
3327         case DW_CFA_def_cfa_offset_sf:
3328           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3329           dw2_asm_output_data_sleb128 (off, NULL);
3330           break;
3331
3332         case DW_CFA_GNU_window_save:
3333           break;
3334
3335         case DW_CFA_def_cfa_expression:
3336         case DW_CFA_expression:
3337           output_cfa_loc (cfi, for_eh);
3338           break;
3339
3340         case DW_CFA_GNU_negative_offset_extended:
3341           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3342           gcc_unreachable ();
3343
3344         default:
3345           break;
3346         }
3347     }
3348 }
3349
3350 /* Similar, but do it via assembler directives instead.  */
3351
3352 static void
3353 output_cfi_directive (dw_cfi_ref cfi)
3354 {
3355   unsigned long r, r2;
3356
3357   switch (cfi->dw_cfi_opc)
3358     {
3359     case DW_CFA_advance_loc:
3360     case DW_CFA_advance_loc1:
3361     case DW_CFA_advance_loc2:
3362     case DW_CFA_advance_loc4:
3363     case DW_CFA_MIPS_advance_loc8:
3364     case DW_CFA_set_loc:
3365       /* Should only be created by add_fde_cfi in a code path not
3366          followed when emitting via directives.  The assembler is
3367          going to take care of this for us.  */
3368       gcc_unreachable ();
3369
3370     case DW_CFA_offset:
3371     case DW_CFA_offset_extended:
3372     case DW_CFA_offset_extended_sf:
3373       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3374       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3375                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3376       break;
3377
3378     case DW_CFA_restore:
3379     case DW_CFA_restore_extended:
3380       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3381       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3382       break;
3383
3384     case DW_CFA_undefined:
3385       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3386       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3387       break;
3388
3389     case DW_CFA_same_value:
3390       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3391       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3392       break;
3393
3394     case DW_CFA_def_cfa:
3395     case DW_CFA_def_cfa_sf:
3396       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3397       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3398                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3399       break;
3400
3401     case DW_CFA_def_cfa_register:
3402       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3403       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3404       break;
3405
3406     case DW_CFA_register:
3407       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3408       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3409       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3410       break;
3411
3412     case DW_CFA_def_cfa_offset:
3413     case DW_CFA_def_cfa_offset_sf:
3414       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3415                HOST_WIDE_INT_PRINT_DEC"\n",
3416                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3417       break;
3418
3419     case DW_CFA_remember_state:
3420       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3421       break;
3422     case DW_CFA_restore_state:
3423       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3424       break;
3425
3426     case DW_CFA_GNU_args_size:
3427       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3428       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3429       if (flag_debug_asm)
3430         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3431                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3432       fputc ('\n', asm_out_file);
3433       break;
3434
3435     case DW_CFA_GNU_window_save:
3436       fprintf (asm_out_file, "\t.cfi_window_save\n");
3437       break;
3438
3439     case DW_CFA_def_cfa_expression:
3440     case DW_CFA_expression:
3441       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3442       output_cfa_loc_raw (cfi);
3443       fputc ('\n', asm_out_file);
3444       break;
3445
3446     default:
3447       gcc_unreachable ();
3448     }
3449 }
3450
3451 /* Output CFIs from VEC, up to index UPTO, to bring current FDE to the
3452    same state as after executing CFIs in CFI chain.  DO_CFI_ASM is
3453    true if .cfi_* directives shall be emitted, false otherwise.  If it
3454    is false, FDE and FOR_EH are the other arguments to pass to
3455    output_cfi.  */
3456
3457 static void
3458 output_cfis (cfi_vec vec, int upto, bool do_cfi_asm,
3459              dw_fde_ref fde, bool for_eh)
3460 {
3461   int ix;
3462   struct dw_cfi_struct cfi_buf;
3463   dw_cfi_ref cfi2;
3464   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3465   VEC(dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3466   unsigned int len, idx;
3467
3468   for (ix = 0; ix < upto + 1; ix++)
3469     {
3470       dw_cfi_ref cfi = ix < upto ? VEC_index (dw_cfi_ref, vec, ix) : NULL;
3471       switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3472         {
3473         case DW_CFA_advance_loc:
3474         case DW_CFA_advance_loc1:
3475         case DW_CFA_advance_loc2:
3476         case DW_CFA_advance_loc4:
3477         case DW_CFA_MIPS_advance_loc8:
3478         case DW_CFA_set_loc:
3479           /* All advances should be ignored.  */
3480           break;
3481         case DW_CFA_remember_state:
3482           {
3483             dw_cfi_ref args_size = cfi_args_size;
3484
3485             /* Skip everything between .cfi_remember_state and
3486                .cfi_restore_state.  */
3487             ix++;
3488             if (ix == upto)
3489               goto flush_all;
3490
3491             for (; ix < upto; ix++)
3492               {
3493                 cfi2 = VEC_index (dw_cfi_ref, vec, ix);
3494                 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3495                   break;
3496                 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3497                   args_size = cfi2;
3498                 else
3499                   gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3500               }
3501
3502             cfi_args_size = args_size;
3503             break;
3504           }
3505         case DW_CFA_GNU_args_size:
3506           cfi_args_size = cfi;
3507           break;
3508         case DW_CFA_GNU_window_save:
3509           goto flush_all;
3510         case DW_CFA_offset:
3511         case DW_CFA_offset_extended:
3512         case DW_CFA_offset_extended_sf:
3513         case DW_CFA_restore:
3514         case DW_CFA_restore_extended:
3515         case DW_CFA_undefined:
3516         case DW_CFA_same_value:
3517         case DW_CFA_register:
3518         case DW_CFA_val_offset:
3519         case DW_CFA_val_offset_sf:
3520         case DW_CFA_expression:
3521         case DW_CFA_val_expression:
3522         case DW_CFA_GNU_negative_offset_extended:
3523           if (VEC_length (dw_cfi_ref, regs)
3524               <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3525             VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3526                                    cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3527           VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
3528                        cfi);
3529           break;
3530         case DW_CFA_def_cfa:
3531         case DW_CFA_def_cfa_sf:
3532         case DW_CFA_def_cfa_expression:
3533           cfi_cfa = cfi;
3534           cfi_cfa_offset = cfi;
3535           break;
3536         case DW_CFA_def_cfa_register:
3537           cfi_cfa = cfi;
3538           break;
3539         case DW_CFA_def_cfa_offset:
3540         case DW_CFA_def_cfa_offset_sf:
3541           cfi_cfa_offset = cfi;
3542           break;
3543         case DW_CFA_nop:
3544           gcc_assert (cfi == NULL);
3545         flush_all:
3546           len = VEC_length (dw_cfi_ref, regs);
3547           for (idx = 0; idx < len; idx++)
3548             {
3549               cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3550               if (cfi2 != NULL
3551                   && cfi2->dw_cfi_opc != DW_CFA_restore
3552                   && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3553                 {
3554                   if (do_cfi_asm)
3555                     output_cfi_directive (cfi2);
3556                   else
3557                     output_cfi (cfi2, fde, for_eh);
3558                 }
3559             }
3560           if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3561             {
3562               gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3563               cfi_buf = *cfi_cfa;
3564               switch (cfi_cfa_offset->dw_cfi_opc)
3565                 {
3566                 case DW_CFA_def_cfa_offset:
3567                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3568                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3569                   break;
3570                 case DW_CFA_def_cfa_offset_sf:
3571                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3572                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3573                   break;
3574                 case DW_CFA_def_cfa:
3575                 case DW_CFA_def_cfa_sf:
3576                   cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3577                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3578                   break;
3579                 default:
3580                   gcc_unreachable ();
3581                 }
3582               cfi_cfa = &cfi_buf;
3583             }
3584           else if (cfi_cfa_offset)
3585             cfi_cfa = cfi_cfa_offset;
3586           if (cfi_cfa)
3587             {
3588               if (do_cfi_asm)
3589                 output_cfi_directive (cfi_cfa);
3590               else
3591                 output_cfi (cfi_cfa, fde, for_eh);
3592             }
3593           cfi_cfa = NULL;
3594           cfi_cfa_offset = NULL;
3595           if (cfi_args_size
3596               && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3597             {
3598               if (do_cfi_asm)
3599                 output_cfi_directive (cfi_args_size);
3600               else
3601                 output_cfi (cfi_args_size, fde, for_eh);
3602             }
3603           cfi_args_size = NULL;
3604           if (cfi == NULL)
3605             {
3606               VEC_free (dw_cfi_ref, heap, regs);
3607               return;
3608             }
3609           else if (do_cfi_asm)
3610             output_cfi_directive (cfi);
3611           else
3612             output_cfi (cfi, fde, for_eh);
3613           break;
3614         default:
3615           gcc_unreachable ();
3616         }
3617     }
3618 }
3619
3620 /* Like output_cfis, but emit all CFIs in the vector.  */
3621 static void
3622 output_all_cfis (cfi_vec vec, bool do_cfi_asm,
3623                  dw_fde_ref fde, bool for_eh)
3624 {
3625   output_cfis (vec, VEC_length (dw_cfi_ref, vec), do_cfi_asm, fde, for_eh);
3626 }
3627
3628 /* Output one FDE.  */
3629
3630 static void
3631 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3632             char *section_start_label, int fde_encoding, char *augmentation,
3633             bool any_lsda_needed, int lsda_encoding)
3634 {
3635   int ix;
3636   const char *begin, *end;
3637   static unsigned int j;
3638   char l1[20], l2[20];
3639   dw_cfi_ref cfi;
3640
3641   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3642                                      /* empty */ 0);
3643   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3644                                   for_eh + j);
3645   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3646   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3647   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3648     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3649                          " indicating 64-bit DWARF extension");
3650   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3651                         "FDE Length");
3652   ASM_OUTPUT_LABEL (asm_out_file, l1);
3653
3654   if (for_eh)
3655     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3656   else
3657     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3658                            debug_frame_section, "FDE CIE offset");
3659
3660   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
3661   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
3662
3663   if (for_eh)
3664     {
3665       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3666       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3667       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3668                                        "FDE initial location");
3669       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3670                             end, begin, "FDE address range");
3671     }
3672   else
3673     {
3674       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3675       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3676     }
3677
3678   if (augmentation[0])
3679     {
3680       if (any_lsda_needed)
3681         {
3682           int size = size_of_encoded_value (lsda_encoding);
3683
3684           if (lsda_encoding == DW_EH_PE_aligned)
3685             {
3686               int offset = (  4         /* Length */
3687                             + 4         /* CIE offset */
3688                             + 2 * size_of_encoded_value (fde_encoding)
3689                             + 1         /* Augmentation size */ );
3690               int pad = -offset & (PTR_SIZE - 1);
3691
3692               size += pad;
3693               gcc_assert (size_of_uleb128 (size) == 1);
3694             }
3695
3696           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3697
3698           if (fde->uses_eh_lsda)
3699             {
3700               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3701                                            fde->funcdef_number);
3702               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3703                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3704                                                false,
3705                                                "Language Specific Data Area");
3706             }
3707           else
3708             {
3709               if (lsda_encoding == DW_EH_PE_aligned)
3710                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3711               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3712                                    "Language Specific Data Area (none)");
3713             }
3714         }
3715       else
3716         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3717     }
3718
3719   /* Loop through the Call Frame Instructions associated with
3720      this FDE.  */
3721   fde->dw_fde_current_label = begin;
3722   if (fde->dw_fde_second_begin == NULL)
3723     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3724       output_cfi (cfi, fde, for_eh);
3725   else if (!second)
3726     {
3727       if (fde->dw_fde_switch_cfi_index > 0)
3728         FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3729           {
3730             if (ix == fde->dw_fde_switch_cfi_index)
3731               break;
3732             output_cfi (cfi, fde, for_eh);
3733           }
3734     }
3735   else
3736     {
3737       int i, from = 0;
3738       int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
3739
3740       if (fde->dw_fde_switch_cfi_index > 0)
3741         {
3742           from = fde->dw_fde_switch_cfi_index;
3743           output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
3744         }
3745       for (i = from; i < until; i++)
3746         output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
3747                     fde, for_eh);
3748     }
3749
3750   /* If we are to emit a ref/link from function bodies to their frame tables,
3751      do it now.  This is typically performed to make sure that tables
3752      associated with functions are dragged with them and not discarded in
3753      garbage collecting links. We need to do this on a per function basis to
3754      cope with -ffunction-sections.  */
3755
3756 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3757   /* Switch to the function section, emit the ref to the tables, and
3758      switch *back* into the table section.  */
3759   switch_to_section (function_section (fde->decl));
3760   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3761   switch_to_frame_table_section (for_eh, true);
3762 #endif
3763
3764   /* Pad the FDE out to an address sized boundary.  */
3765   ASM_OUTPUT_ALIGN (asm_out_file,
3766                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3767   ASM_OUTPUT_LABEL (asm_out_file, l2);
3768
3769   j += 2;
3770 }
3771
3772 /* Return true if frame description entry FDE is needed for EH.  */
3773
3774 static bool
3775 fde_needed_for_eh_p (dw_fde_ref fde)
3776 {
3777   if (flag_asynchronous_unwind_tables)
3778     return true;
3779
3780   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3781     return true;
3782
3783   if (fde->uses_eh_lsda)
3784     return true;
3785
3786   /* If exceptions are enabled, we have collected nothrow info.  */
3787   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3788     return false;
3789
3790   return true;
3791 }
3792
3793 /* Output the call frame information used to record information
3794    that relates to calculating the frame pointer, and records the
3795    location of saved registers.  */
3796
3797 static void
3798 output_call_frame_info (int for_eh)
3799 {
3800   unsigned int i;
3801   dw_fde_ref fde;
3802   dw_cfi_ref cfi;
3803   char l1[20], l2[20], section_start_label[20];
3804   bool any_lsda_needed = false;
3805   char augmentation[6];
3806   int augmentation_size;
3807   int fde_encoding = DW_EH_PE_absptr;
3808   int per_encoding = DW_EH_PE_absptr;
3809   int lsda_encoding = DW_EH_PE_absptr;
3810   int return_reg;
3811   rtx personality = NULL;
3812   int dw_cie_version;
3813
3814   /* Don't emit a CIE if there won't be any FDEs.  */
3815   if (fde_table_in_use == 0)
3816     return;
3817
3818   /* Nothing to do if the assembler's doing it all.  */
3819   if (dwarf2out_do_cfi_asm ())
3820     return;
3821
3822   /* If we don't have any functions we'll want to unwind out of, don't emit
3823      any EH unwind information.  If we make FDEs linkonce, we may have to
3824      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3825      want to avoid having an FDE kept around when the function it refers to
3826      is discarded.  Example where this matters: a primary function template
3827      in C++ requires EH information, an explicit specialization doesn't.  */
3828   if (for_eh)
3829     {
3830       bool any_eh_needed = false;
3831
3832       for (i = 0; i < fde_table_in_use; i++)
3833         if (fde_table[i].uses_eh_lsda)
3834           any_eh_needed = any_lsda_needed = true;
3835         else if (fde_needed_for_eh_p (&fde_table[i]))
3836           any_eh_needed = true;
3837         else if (TARGET_USES_WEAK_UNWIND_INFO)
3838           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3839                                              1, 1);
3840
3841       if (!any_eh_needed)
3842         return;
3843     }
3844
3845   /* We're going to be generating comments, so turn on app.  */
3846   if (flag_debug_asm)
3847     app_enable ();
3848
3849   /* Switch to the proper frame section, first time.  */
3850   switch_to_frame_table_section (for_eh, false);
3851
3852   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3853   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3854
3855   /* Output the CIE.  */
3856   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3857   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3858   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3859     dw2_asm_output_data (4, 0xffffffff,
3860       "Initial length escape value indicating 64-bit DWARF extension");
3861   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3862                         "Length of Common Information Entry");
3863   ASM_OUTPUT_LABEL (asm_out_file, l1);
3864
3865   /* Now that the CIE pointer is PC-relative for EH,
3866      use 0 to identify the CIE.  */
3867   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3868                        (for_eh ? 0 : DWARF_CIE_ID),
3869                        "CIE Identifier Tag");
3870
3871   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3872      use CIE version 1, unless that would produce incorrect results
3873      due to overflowing the return register column.  */
3874   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3875   dw_cie_version = 1;
3876   if (return_reg >= 256 || dwarf_version > 2)
3877     dw_cie_version = 3;
3878   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3879
3880   augmentation[0] = 0;
3881   augmentation_size = 0;
3882
3883   personality = current_unit_personality;
3884   if (for_eh)
3885     {
3886       char *p;
3887
3888       /* Augmentation:
3889          z      Indicates that a uleb128 is present to size the
3890                 augmentation section.
3891          L      Indicates the encoding (and thus presence) of
3892                 an LSDA pointer in the FDE augmentation.
3893          R      Indicates a non-default pointer encoding for
3894                 FDE code pointers.
3895          P      Indicates the presence of an encoding + language
3896                 personality routine in the CIE augmentation.  */
3897
3898       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3899       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3900       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3901
3902       p = augmentation + 1;
3903       if (personality)
3904         {
3905           *p++ = 'P';
3906           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3907           assemble_external_libcall (personality);
3908         }
3909       if (any_lsda_needed)
3910         {
3911           *p++ = 'L';
3912           augmentation_size += 1;
3913         }
3914       if (fde_encoding != DW_EH_PE_absptr)
3915         {
3916           *p++ = 'R';
3917           augmentation_size += 1;
3918         }
3919       if (p > augmentation + 1)
3920         {
3921           augmentation[0] = 'z';
3922           *p = '\0';
3923         }
3924
3925       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3926       if (personality && per_encoding == DW_EH_PE_aligned)
3927         {
3928           int offset = (  4             /* Length */
3929                         + 4             /* CIE Id */
3930                         + 1             /* CIE version */
3931                         + strlen (augmentation) + 1     /* Augmentation */
3932                         + size_of_uleb128 (1)           /* Code alignment */
3933                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3934                         + 1             /* RA column */
3935                         + 1             /* Augmentation size */
3936                         + 1             /* Personality encoding */ );
3937           int pad = -offset & (PTR_SIZE - 1);
3938
3939           augmentation_size += pad;
3940
3941           /* Augmentations should be small, so there's scarce need to
3942              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3943           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3944         }
3945     }
3946
3947   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3948   if (dw_cie_version >= 4)
3949     {
3950       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3951       dw2_asm_output_data (1, 0, "CIE Segment Size");
3952     }
3953   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3954   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3955                                "CIE Data Alignment Factor");
3956
3957   if (dw_cie_version == 1)
3958     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3959   else
3960     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3961
3962   if (augmentation[0])
3963     {
3964       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3965       if (personality)
3966         {
3967           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3968                                eh_data_format_name (per_encoding));
3969           dw2_asm_output_encoded_addr_rtx (per_encoding,
3970                                            personality,
3971                                            true, NULL);
3972         }
3973
3974       if (any_lsda_needed)
3975         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3976                              eh_data_format_name (lsda_encoding));
3977
3978       if (fde_encoding != DW_EH_PE_absptr)
3979         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3980                              eh_data_format_name (fde_encoding));
3981     }
3982
3983   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
3984     output_cfi (cfi, NULL, for_eh);
3985
3986   /* Pad the CIE out to an address sized boundary.  */
3987   ASM_OUTPUT_ALIGN (asm_out_file,
3988                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3989   ASM_OUTPUT_LABEL (asm_out_file, l2);
3990
3991   /* Loop through all of the FDE's.  */
3992   for (i = 0; i < fde_table_in_use; i++)
3993     {
3994       unsigned int k;
3995       fde = &fde_table[i];
3996
3997       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3998       if (for_eh && !fde_needed_for_eh_p (fde))
3999         continue;
4000
4001       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
4002         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
4003                     augmentation, any_lsda_needed, lsda_encoding);
4004     }
4005
4006   if (for_eh && targetm.terminate_dw2_eh_frame_info)
4007     dw2_asm_output_data (4, 0, "End of Table");
4008 #ifdef MIPS_DEBUGGING_INFO
4009   /* Work around Irix 6 assembler bug whereby labels at the end of a section
4010      get a value of 0.  Putting .align 0 after the label fixes it.  */
4011   ASM_OUTPUT_ALIGN (asm_out_file, 0);
4012 #endif
4013
4014   /* Turn off app to make assembly quicker.  */
4015   if (flag_debug_asm)
4016     app_disable ();
4017 }
4018
4019 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4020
4021 static void
4022 dwarf2out_do_cfi_startproc (bool second)
4023 {
4024   int enc;
4025   rtx ref;
4026   rtx personality = get_personality_function (current_function_decl);
4027
4028   fprintf (asm_out_file, "\t.cfi_startproc\n");
4029
4030   if (personality)
4031     {
4032       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4033       ref = personality;
4034
4035       /* ??? The GAS support isn't entirely consistent.  We have to
4036          handle indirect support ourselves, but PC-relative is done
4037          in the assembler.  Further, the assembler can't handle any
4038          of the weirder relocation types.  */
4039       if (enc & DW_EH_PE_indirect)
4040         ref = dw2_force_const_mem (ref, true);
4041
4042       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4043       output_addr_const (asm_out_file, ref);
4044       fputc ('\n', asm_out_file);
4045     }
4046
4047   if (crtl->uses_eh_lsda)
4048     {
4049       char lab[20];
4050
4051       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4052       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4053                                    current_function_funcdef_no);
4054       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4055       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4056
4057       if (enc & DW_EH_PE_indirect)
4058         ref = dw2_force_const_mem (ref, true);
4059
4060       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4061       output_addr_const (asm_out_file, ref);
4062       fputc ('\n', asm_out_file);
4063     }
4064 }
4065
4066 /* Output a marker (i.e. a label) for the beginning of a function, before
4067    the prologue.  */
4068
4069 void
4070 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4071                           const char *file ATTRIBUTE_UNUSED)
4072 {
4073   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4074   char * dup_label;
4075   dw_fde_ref fde;
4076   section *fnsec;
4077   bool do_frame;
4078
4079   current_function_func_begin_label = NULL;
4080
4081   do_frame = dwarf2out_do_frame ();
4082
4083   /* ??? current_function_func_begin_label is also used by except.c for
4084      call-site information.  We must emit this label if it might be used.  */
4085   if (!do_frame
4086       && (!flag_exceptions
4087           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
4088     return;
4089
4090   fnsec = function_section (current_function_decl);
4091   switch_to_section (fnsec);
4092   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4093                                current_function_funcdef_no);
4094   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4095                           current_function_funcdef_no);
4096   dup_label = xstrdup (label);
4097   current_function_func_begin_label = dup_label;
4098
4099   /* We can elide the fde allocation if we're not emitting debug info.  */
4100   if (!do_frame)
4101     return;
4102
4103   /* Expand the fde table if necessary.  */
4104   if (fde_table_in_use == fde_table_allocated)
4105     {
4106       fde_table_allocated += FDE_TABLE_INCREMENT;
4107       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4108       memset (fde_table + fde_table_in_use, 0,
4109               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4110     }
4111
4112   /* Record the FDE associated with this function.  */
4113   current_funcdef_fde = fde_table_in_use;
4114
4115   /* Add the new FDE at the end of the fde_table.  */
4116   fde = &fde_table[fde_table_in_use++];
4117   fde->decl = current_function_decl;
4118   fde->dw_fde_begin = dup_label;
4119   fde->dw_fde_end = NULL;
4120   fde->dw_fde_current_label = dup_label;
4121   fde->dw_fde_second_begin = NULL;
4122   fde->dw_fde_second_end = NULL;
4123   fde->dw_fde_vms_end_prologue = NULL;
4124   fde->dw_fde_vms_begin_epilogue = NULL;
4125   fde->dw_fde_cfi = VEC_alloc (dw_cfi_ref, gc, 20);
4126   fde->dw_fde_switch_cfi_index = 0;
4127   fde->funcdef_number = current_function_funcdef_no;
4128   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4129   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4130   fde->nothrow = crtl->nothrow;
4131   fde->drap_reg = INVALID_REGNUM;
4132   fde->vdrap_reg = INVALID_REGNUM;
4133   fde->in_std_section = (fnsec == text_section
4134                          || (cold_text_section && fnsec == cold_text_section));
4135   fde->second_in_std_section = 0;
4136
4137   args_size = old_args_size = 0;
4138
4139   /* We only want to output line number information for the genuine dwarf2
4140      prologue case, not the eh frame case.  */
4141 #ifdef DWARF2_DEBUGGING_INFO
4142   if (file)
4143     dwarf2out_source_line (line, file, 0, true);
4144 #endif
4145
4146   if (dwarf2out_do_cfi_asm ())
4147     dwarf2out_do_cfi_startproc (false);
4148   else
4149     {
4150       rtx personality = get_personality_function (current_function_decl);
4151       if (!current_unit_personality)
4152         current_unit_personality = personality;
4153
4154       /* We cannot keep a current personality per function as without CFI
4155          asm, at the point where we emit the CFI data, there is no current
4156          function anymore.  */
4157       if (personality && current_unit_personality != personality)
4158         sorry ("multiple EH personalities are supported only with assemblers "
4159                "supporting .cfi_personality directive");
4160     }
4161 }
4162
4163 /* Output a marker (i.e. a label) for the end of the generated code
4164    for a function prologue.  This gets called *after* the prologue code has
4165    been generated.  */
4166
4167 void
4168 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4169                         const char *file ATTRIBUTE_UNUSED)
4170 {
4171   dw_fde_ref fde;
4172   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4173
4174   /* Output a label to mark the endpoint of the code generated for this
4175      function.  */
4176   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4177                                current_function_funcdef_no);
4178   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4179                           current_function_funcdef_no);
4180   fde = &fde_table[fde_table_in_use - 1];
4181   fde->dw_fde_vms_end_prologue = xstrdup (label);
4182 }
4183
4184 /* Output a marker (i.e. a label) for the beginning of the generated code
4185    for a function epilogue.  This gets called *before* the prologue code has
4186    been generated.  */
4187
4188 void
4189 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4190                           const char *file ATTRIBUTE_UNUSED)
4191 {
4192   dw_fde_ref fde;
4193   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4194
4195   fde = &fde_table[fde_table_in_use - 1];
4196   if (fde->dw_fde_vms_begin_epilogue)
4197     return;
4198
4199   /* Output a label to mark the endpoint of the code generated for this
4200      function.  */
4201   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4202                                current_function_funcdef_no);
4203   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4204                           current_function_funcdef_no);
4205   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4206 }
4207
4208 /* Output a marker (i.e. a label) for the absolute end of the generated code
4209    for a function definition.  This gets called *after* the epilogue code has
4210    been generated.  */
4211
4212 void
4213 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4214                         const char *file ATTRIBUTE_UNUSED)
4215 {
4216   dw_fde_ref fde;
4217   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4218
4219   last_var_location_insn = NULL_RTX;
4220
4221   if (dwarf2out_do_cfi_asm ())
4222     fprintf (asm_out_file, "\t.cfi_endproc\n");
4223
4224   /* Output a label to mark the endpoint of the code generated for this
4225      function.  */
4226   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4227                                current_function_funcdef_no);
4228   ASM_OUTPUT_LABEL (asm_out_file, label);
4229   fde = current_fde ();
4230   gcc_assert (fde != NULL);
4231   if (fde->dw_fde_second_begin == NULL)
4232     fde->dw_fde_end = xstrdup (label);
4233 }
4234
4235 void
4236 dwarf2out_frame_init (void)
4237 {
4238   /* Allocate the initial hunk of the fde_table.  */
4239   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4240   fde_table_allocated = FDE_TABLE_INCREMENT;
4241   fde_table_in_use = 0;
4242
4243   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4244      sake of lookup_cfa.  */
4245
4246   /* On entry, the Canonical Frame Address is at SP.  */
4247   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4248
4249   if (targetm.debug_unwind_info () == UI_DWARF2
4250       || targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
4251     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4252 }
4253
4254 void
4255 dwarf2out_frame_finish (void)
4256 {
4257   /* Output call frame information.  */
4258   if (targetm.debug_unwind_info () == UI_DWARF2)
4259     output_call_frame_info (0);
4260
4261   /* Output another copy for the unwinder.  */
4262   if ((flag_unwind_tables || flag_exceptions)
4263       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
4264     output_call_frame_info (1);
4265 }
4266
4267 /* Note that the current function section is being used for code.  */
4268
4269 static void
4270 dwarf2out_note_section_used (void)
4271 {
4272   section *sec = current_function_section ();
4273   if (sec == text_section)
4274     text_section_used = true;
4275   else if (sec == cold_text_section)
4276     cold_text_section_used = true;
4277 }
4278
4279 static void var_location_switch_text_section (void);
4280 static void set_cur_line_info_table (section *);
4281
4282 void
4283 dwarf2out_switch_text_section (void)
4284 {
4285   section *sect;
4286   dw_fde_ref fde = current_fde ();
4287
4288   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
4289
4290   if (!in_cold_section_p)
4291     {
4292       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
4293       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
4294       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
4295     }
4296   else
4297     {
4298       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
4299       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
4300       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
4301     }
4302   have_multiple_function_sections = true;
4303
4304   /* Reset the current label on switching text sections, so that we
4305      don't attempt to advance_loc4 between labels in different sections.  */
4306   fde->dw_fde_current_label = NULL;
4307
4308   /* There is no need to mark used sections when not debugging.  */
4309   if (cold_text_section != NULL)
4310     dwarf2out_note_section_used ();
4311
4312   if (dwarf2out_do_cfi_asm ())
4313     fprintf (asm_out_file, "\t.cfi_endproc\n");
4314
4315   /* Now do the real section switch.  */
4316   sect = current_function_section ();
4317   switch_to_section (sect);
4318
4319   fde->second_in_std_section
4320     = (sect == text_section
4321        || (cold_text_section && sect == cold_text_section));
4322
4323   if (dwarf2out_do_cfi_asm ())
4324     {
4325       dwarf2out_do_cfi_startproc (true);
4326       /* As this is a different FDE, insert all current CFI instructions
4327          again.  */
4328       output_all_cfis (fde->dw_fde_cfi, true, fde, true);
4329     }
4330   fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
4331   var_location_switch_text_section ();
4332
4333   set_cur_line_info_table (sect);
4334 }
4335 \f
4336 /* And now, the subset of the debugging information support code necessary
4337    for emitting location expressions.  */
4338
4339 /* Data about a single source file.  */
4340 struct GTY(()) dwarf_file_data {
4341   const char * filename;
4342   int emitted_number;
4343 };
4344
4345 typedef struct dw_val_struct *dw_val_ref;
4346 typedef struct die_struct *dw_die_ref;
4347 typedef const struct die_struct *const_dw_die_ref;
4348 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4349 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4350
4351 typedef struct GTY(()) deferred_locations_struct
4352 {
4353   tree variable;
4354   dw_die_ref die;
4355 } deferred_locations;
4356
4357 DEF_VEC_O(deferred_locations);
4358 DEF_VEC_ALLOC_O(deferred_locations,gc);
4359
4360 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4361
4362 DEF_VEC_P(dw_die_ref);
4363 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4364
4365 /* Each DIE may have a series of attribute/value pairs.  Values
4366    can take on several forms.  The forms that are used in this
4367    implementation are listed below.  */
4368
4369 enum dw_val_class
4370 {
4371   dw_val_class_addr,
4372   dw_val_class_offset,
4373   dw_val_class_loc,
4374   dw_val_class_loc_list,
4375   dw_val_class_range_list,
4376   dw_val_class_const,
4377   dw_val_class_unsigned_const,
4378   dw_val_class_const_double,
4379   dw_val_class_vec,
4380   dw_val_class_flag,
4381   dw_val_class_die_ref,
4382   dw_val_class_fde_ref,
4383   dw_val_class_lbl_id,
4384   dw_val_class_lineptr,
4385   dw_val_class_str,
4386   dw_val_class_macptr,
4387   dw_val_class_file,
4388   dw_val_class_data8,
4389   dw_val_class_decl_ref,
4390   dw_val_class_vms_delta
4391 };
4392
4393 /* Describe a floating point constant value, or a vector constant value.  */
4394
4395 typedef struct GTY(()) dw_vec_struct {
4396   unsigned char * GTY((length ("%h.length"))) array;
4397   unsigned length;
4398   unsigned elt_size;
4399 }
4400 dw_vec_const;
4401
4402 /* The dw_val_node describes an attribute's value, as it is
4403    represented internally.  */
4404
4405 typedef struct GTY(()) dw_val_struct {
4406   enum dw_val_class val_class;
4407   union dw_val_struct_union
4408     {
4409       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4410       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4411       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4412       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4413       HOST_WIDE_INT GTY ((default)) val_int;
4414       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4415       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4416       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4417       struct dw_val_die_union
4418         {
4419           dw_die_ref die;
4420           int external;
4421         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4422       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4423       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4424       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4425       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4426       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4427       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4428       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4429       struct dw_val_vms_delta_union
4430         {
4431           char * lbl1;
4432           char * lbl2;
4433         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4434     }
4435   GTY ((desc ("%1.val_class"))) v;
4436 }
4437 dw_val_node;
4438
4439 /* Locations in memory are described using a sequence of stack machine
4440    operations.  */
4441
4442 typedef struct GTY(()) dw_loc_descr_struct {
4443   dw_loc_descr_ref dw_loc_next;
4444   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4445   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4446      from DW_OP_addr with a dtp-relative symbol relocation.  */
4447   unsigned int dtprel : 1;
4448   int dw_loc_addr;
4449   dw_val_node dw_loc_oprnd1;
4450   dw_val_node dw_loc_oprnd2;
4451 }
4452 dw_loc_descr_node;
4453
4454 /* Location lists are ranges + location descriptions for that range,
4455    so you can track variables that are in different places over
4456    their entire life.  */
4457 typedef struct GTY(()) dw_loc_list_struct {
4458   dw_loc_list_ref dw_loc_next;
4459   const char *begin; /* Label for begin address of range */
4460   const char *end;  /* Label for end address of range */
4461   char *ll_symbol; /* Label for beginning of location list.
4462                       Only on head of list */
4463   const char *section; /* Section this loclist is relative to */
4464   dw_loc_descr_ref expr;
4465   hashval_t hash;
4466   /* True if all addresses in this and subsequent lists are known to be
4467      resolved.  */
4468   bool resolved_addr;
4469   /* True if this list has been replaced by dw_loc_next.  */
4470   bool replaced;
4471   bool emitted;
4472   /* True if the range should be emitted even if begin and end
4473      are the same.  */
4474   bool force;
4475 } dw_loc_list_node;
4476
4477 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4478
4479 /* Convert a DWARF stack opcode into its string name.  */
4480
4481 static const char *
4482 dwarf_stack_op_name (unsigned int op)
4483 {
4484   switch (op)
4485     {
4486     case DW_OP_addr:
4487       return "DW_OP_addr";
4488     case DW_OP_deref:
4489       return "DW_OP_deref";
4490     case DW_OP_const1u:
4491       return "DW_OP_const1u";
4492     case DW_OP_const1s:
4493       return "DW_OP_const1s";
4494     case DW_OP_const2u:
4495       return "DW_OP_const2u";
4496     case DW_OP_const2s:
4497       return "DW_OP_const2s";
4498     case DW_OP_const4u:
4499       return "DW_OP_const4u";
4500     case DW_OP_const4s:
4501       return "DW_OP_const4s";
4502     case DW_OP_const8u:
4503       return "DW_OP_const8u";
4504     case DW_OP_const8s:
4505       return "DW_OP_const8s";
4506     case DW_OP_constu:
4507       return "DW_OP_constu";
4508     case DW_OP_consts:
4509       return "DW_OP_consts";
4510     case DW_OP_dup:
4511       return "DW_OP_dup";
4512     case DW_OP_drop:
4513       return "DW_OP_drop";
4514     case DW_OP_over:
4515       return "DW_OP_over";
4516     case DW_OP_pick:
4517       return "DW_OP_pick";
4518     case DW_OP_swap:
4519       return "DW_OP_swap";
4520     case DW_OP_rot:
4521       return "DW_OP_rot";
4522     case DW_OP_xderef:
4523       return "DW_OP_xderef";
4524     case DW_OP_abs:
4525       return "DW_OP_abs";
4526     case DW_OP_and:
4527       return "DW_OP_and";
4528     case DW_OP_div:
4529       return "DW_OP_div";
4530     case DW_OP_minus:
4531       return "DW_OP_minus";
4532     case DW_OP_mod:
4533       return "DW_OP_mod";
4534     case DW_OP_mul:
4535       return "DW_OP_mul";
4536     case DW_OP_neg:
4537       return "DW_OP_neg";
4538     case DW_OP_not:
4539       return "DW_OP_not";
4540     case DW_OP_or:
4541       return "DW_OP_or";
4542     case DW_OP_plus:
4543       return "DW_OP_plus";
4544     case DW_OP_plus_uconst:
4545       return "DW_OP_plus_uconst";
4546     case DW_OP_shl:
4547       return "DW_OP_shl";
4548     case DW_OP_shr:
4549       return "DW_OP_shr";
4550     case DW_OP_shra:
4551       return "DW_OP_shra";
4552     case DW_OP_xor:
4553       return "DW_OP_xor";
4554     case DW_OP_bra:
4555       return "DW_OP_bra";
4556     case DW_OP_eq:
4557       return "DW_OP_eq";
4558     case DW_OP_ge:
4559       return "DW_OP_ge";
4560     case DW_OP_gt:
4561       return "DW_OP_gt";
4562     case DW_OP_le:
4563       return "DW_OP_le";
4564     case DW_OP_lt:
4565       return "DW_OP_lt";
4566     case DW_OP_ne:
4567       return "DW_OP_ne";
4568     case DW_OP_skip:
4569       return "DW_OP_skip";
4570     case DW_OP_lit0:
4571       return "DW_OP_lit0";
4572     case DW_OP_lit1:
4573       return "DW_OP_lit1";
4574     case DW_OP_lit2:
4575       return "DW_OP_lit2";
4576     case DW_OP_lit3:
4577       return "DW_OP_lit3";
4578     case DW_OP_lit4:
4579       return "DW_OP_lit4";
4580     case DW_OP_lit5:
4581       return "DW_OP_lit5";
4582     case DW_OP_lit6:
4583       return "DW_OP_lit6";
4584     case DW_OP_lit7:
4585       return "DW_OP_lit7";
4586     case DW_OP_lit8:
4587       return "DW_OP_lit8";
4588     case DW_OP_lit9:
4589       return "DW_OP_lit9";
4590     case DW_OP_lit10:
4591       return "DW_OP_lit10";
4592     case DW_OP_lit11:
4593       return "DW_OP_lit11";
4594     case DW_OP_lit12:
4595       return "DW_OP_lit12";
4596     case DW_OP_lit13:
4597       return "DW_OP_lit13";
4598     case DW_OP_lit14:
4599       return "DW_OP_lit14";
4600     case DW_OP_lit15:
4601       return "DW_OP_lit15";
4602     case DW_OP_lit16:
4603       return "DW_OP_lit16";
4604     case DW_OP_lit17:
4605       return "DW_OP_lit17";
4606     case DW_OP_lit18:
4607       return "DW_OP_lit18";
4608     case DW_OP_lit19:
4609       return "DW_OP_lit19";
4610     case DW_OP_lit20:
4611       return "DW_OP_lit20";
4612     case DW_OP_lit21:
4613       return "DW_OP_lit21";
4614     case DW_OP_lit22:
4615       return "DW_OP_lit22";
4616     case DW_OP_lit23:
4617       return "DW_OP_lit23";
4618     case DW_OP_lit24:
4619       return "DW_OP_lit24";
4620     case DW_OP_lit25:
4621       return "DW_OP_lit25";
4622     case DW_OP_lit26:
4623       return "DW_OP_lit26";
4624     case DW_OP_lit27:
4625       return "DW_OP_lit27";
4626     case DW_OP_lit28:
4627       return "DW_OP_lit28";
4628     case DW_OP_lit29:
4629       return "DW_OP_lit29";
4630     case DW_OP_lit30:
4631       return "DW_OP_lit30";
4632     case DW_OP_lit31:
4633       return "DW_OP_lit31";
4634     case DW_OP_reg0:
4635       return "DW_OP_reg0";
4636     case DW_OP_reg1:
4637       return "DW_OP_reg1";
4638     case DW_OP_reg2:
4639       return "DW_OP_reg2";
4640     case DW_OP_reg3:
4641       return "DW_OP_reg3";
4642     case DW_OP_reg4:
4643       return "DW_OP_reg4";
4644     case DW_OP_reg5:
4645       return "DW_OP_reg5";
4646     case DW_OP_reg6:
4647       return "DW_OP_reg6";
4648     case DW_OP_reg7:
4649       return "DW_OP_reg7";
4650     case DW_OP_reg8:
4651       return "DW_OP_reg8";
4652     case DW_OP_reg9:
4653       return "DW_OP_reg9";
4654     case DW_OP_reg10:
4655       return "DW_OP_reg10";
4656     case DW_OP_reg11:
4657       return "DW_OP_reg11";
4658     case DW_OP_reg12:
4659       return "DW_OP_reg12";
4660     case DW_OP_reg13:
4661       return "DW_OP_reg13";
4662     case DW_OP_reg14:
4663       return "DW_OP_reg14";
4664     case DW_OP_reg15:
4665       return "DW_OP_reg15";
4666     case DW_OP_reg16:
4667       return "DW_OP_reg16";
4668     case DW_OP_reg17:
4669       return "DW_OP_reg17";
4670     case DW_OP_reg18:
4671       return "DW_OP_reg18";
4672     case DW_OP_reg19:
4673       return "DW_OP_reg19";
4674     case DW_OP_reg20:
4675       return "DW_OP_reg20";
4676     case DW_OP_reg21:
4677       return "DW_OP_reg21";
4678     case DW_OP_reg22:
4679       return "DW_OP_reg22";
4680     case DW_OP_reg23:
4681       return "DW_OP_reg23";
4682     case DW_OP_reg24:
4683       return "DW_OP_reg24";
4684     case DW_OP_reg25:
4685       return "DW_OP_reg25";
4686     case DW_OP_reg26:
4687       return "DW_OP_reg26";
4688     case DW_OP_reg27:
4689       return "DW_OP_reg27";
4690     case DW_OP_reg28:
4691       return "DW_OP_reg28";
4692     case DW_OP_reg29:
4693       return "DW_OP_reg29";
4694     case DW_OP_reg30:
4695       return "DW_OP_reg30";
4696     case DW_OP_reg31:
4697       return "DW_OP_reg31";
4698     case DW_OP_breg0:
4699       return "DW_OP_breg0";
4700     case DW_OP_breg1:
4701       return "DW_OP_breg1";
4702     case DW_OP_breg2:
4703       return "DW_OP_breg2";
4704     case DW_OP_breg3:
4705       return "DW_OP_breg3";
4706     case DW_OP_breg4:
4707       return "DW_OP_breg4";
4708     case DW_OP_breg5:
4709       return "DW_OP_breg5";
4710     case DW_OP_breg6:
4711       return "DW_OP_breg6";
4712     case DW_OP_breg7:
4713       return "DW_OP_breg7";
4714     case DW_OP_breg8:
4715       return "DW_OP_breg8";
4716     case DW_OP_breg9:
4717       return "DW_OP_breg9";
4718     case DW_OP_breg10:
4719       return "DW_OP_breg10";
4720     case DW_OP_breg11:
4721       return "DW_OP_breg11";
4722     case DW_OP_breg12:
4723       return "DW_OP_breg12";
4724     case DW_OP_breg13:
4725       return "DW_OP_breg13";
4726     case DW_OP_breg14:
4727       return "DW_OP_breg14";
4728     case DW_OP_breg15:
4729       return "DW_OP_breg15";
4730     case DW_OP_breg16:
4731       return "DW_OP_breg16";
4732     case DW_OP_breg17:
4733       return "DW_OP_breg17";
4734     case DW_OP_breg18:
4735       return "DW_OP_breg18";
4736     case DW_OP_breg19:
4737       return "DW_OP_breg19";
4738     case DW_OP_breg20:
4739       return "DW_OP_breg20";
4740     case DW_OP_breg21:
4741       return "DW_OP_breg21";
4742     case DW_OP_breg22:
4743       return "DW_OP_breg22";
4744     case DW_OP_breg23:
4745       return "DW_OP_breg23";
4746     case DW_OP_breg24:
4747       return "DW_OP_breg24";
4748     case DW_OP_breg25:
4749       return "DW_OP_breg25";
4750     case DW_OP_breg26:
4751       return "DW_OP_breg26";
4752     case DW_OP_breg27:
4753       return "DW_OP_breg27";
4754     case DW_OP_breg28:
4755       return "DW_OP_breg28";
4756     case DW_OP_breg29:
4757       return "DW_OP_breg29";
4758     case DW_OP_breg30:
4759       return "DW_OP_breg30";
4760     case DW_OP_breg31:
4761       return "DW_OP_breg31";
4762     case DW_OP_regx:
4763       return "DW_OP_regx";
4764     case DW_OP_fbreg:
4765       return "DW_OP_fbreg";
4766     case DW_OP_bregx:
4767       return "DW_OP_bregx";
4768     case DW_OP_piece:
4769       return "DW_OP_piece";
4770     case DW_OP_deref_size:
4771       return "DW_OP_deref_size";
4772     case DW_OP_xderef_size:
4773       return "DW_OP_xderef_size";
4774     case DW_OP_nop:
4775       return "DW_OP_nop";
4776
4777     case DW_OP_push_object_address:
4778       return "DW_OP_push_object_address";
4779     case DW_OP_call2:
4780       return "DW_OP_call2";
4781     case DW_OP_call4:
4782       return "DW_OP_call4";
4783     case DW_OP_call_ref:
4784       return "DW_OP_call_ref";
4785     case DW_OP_implicit_value:
4786       return "DW_OP_implicit_value";
4787     case DW_OP_stack_value:
4788       return "DW_OP_stack_value";
4789     case DW_OP_form_tls_address:
4790       return "DW_OP_form_tls_address";
4791     case DW_OP_call_frame_cfa:
4792       return "DW_OP_call_frame_cfa";
4793     case DW_OP_bit_piece:
4794       return "DW_OP_bit_piece";
4795
4796     case DW_OP_GNU_push_tls_address:
4797       return "DW_OP_GNU_push_tls_address";
4798     case DW_OP_GNU_uninit:
4799       return "DW_OP_GNU_uninit";
4800     case DW_OP_GNU_encoded_addr:
4801       return "DW_OP_GNU_encoded_addr";
4802     case DW_OP_GNU_implicit_pointer:
4803       return "DW_OP_GNU_implicit_pointer";
4804     case DW_OP_GNU_entry_value:
4805       return "DW_OP_GNU_entry_value";
4806     case DW_OP_GNU_const_type:
4807       return "DW_OP_GNU_const_type";
4808     case DW_OP_GNU_regval_type:
4809       return "DW_OP_GNU_regval_type";
4810     case DW_OP_GNU_deref_type:
4811       return "DW_OP_GNU_deref_type";
4812     case DW_OP_GNU_convert:
4813       return "DW_OP_GNU_convert";
4814     case DW_OP_GNU_reinterpret:
4815       return "DW_OP_GNU_reinterpret";
4816     case DW_OP_GNU_parameter_ref:
4817       return "DW_OP_GNU_parameter_ref";
4818
4819     default:
4820       return "OP_<unknown>";
4821     }
4822 }
4823
4824 /* Return a pointer to a newly allocated location description.  Location
4825    descriptions are simple expression terms that can be strung
4826    together to form more complicated location (address) descriptions.  */
4827
4828 static inline dw_loc_descr_ref
4829 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4830                unsigned HOST_WIDE_INT oprnd2)
4831 {
4832   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4833
4834   descr->dw_loc_opc = op;
4835   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4836   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4837   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4838   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4839
4840   return descr;
4841 }
4842
4843 /* Return a pointer to a newly allocated location description for
4844    REG and OFFSET.  */
4845
4846 static inline dw_loc_descr_ref
4847 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4848 {
4849   if (reg <= 31)
4850     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4851                           offset, 0);
4852   else
4853     return new_loc_descr (DW_OP_bregx, reg, offset);
4854 }
4855
4856 /* Add a location description term to a location description expression.  */
4857
4858 static inline void
4859 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4860 {
4861   dw_loc_descr_ref *d;
4862
4863   /* Find the end of the chain.  */
4864   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4865     ;
4866
4867   *d = descr;
4868 }
4869
4870 /* Add a constant OFFSET to a location expression.  */
4871
4872 static void
4873 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4874 {
4875   dw_loc_descr_ref loc;
4876   HOST_WIDE_INT *p;
4877
4878   gcc_assert (*list_head != NULL);
4879
4880   if (!offset)
4881     return;
4882
4883   /* Find the end of the chain.  */
4884   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4885     ;
4886
4887   p = NULL;
4888   if (loc->dw_loc_opc == DW_OP_fbreg
4889       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4890     p = &loc->dw_loc_oprnd1.v.val_int;
4891   else if (loc->dw_loc_opc == DW_OP_bregx)
4892     p = &loc->dw_loc_oprnd2.v.val_int;
4893
4894   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4895      offset.  Don't optimize if an signed integer overflow would happen.  */
4896   if (p != NULL
4897       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4898           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4899     *p += offset;
4900
4901   else if (offset > 0)
4902     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4903
4904   else
4905     {
4906       loc->dw_loc_next = int_loc_descriptor (-offset);
4907       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4908     }
4909 }
4910
4911 /* Add a constant OFFSET to a location list.  */
4912
4913 static void
4914 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4915 {
4916   dw_loc_list_ref d;
4917   for (d = list_head; d != NULL; d = d->dw_loc_next)
4918     loc_descr_plus_const (&d->expr, offset);
4919 }
4920
4921 #define DWARF_REF_SIZE  \
4922   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4923
4924 static unsigned long size_of_locs (dw_loc_descr_ref);
4925 static unsigned long int get_base_type_offset (dw_die_ref);
4926
4927 /* Return the size of a location descriptor.  */
4928
4929 static unsigned long
4930 size_of_loc_descr (dw_loc_descr_ref loc)
4931 {
4932   unsigned long size = 1;
4933
4934   switch (loc->dw_loc_opc)
4935     {
4936     case DW_OP_addr:
4937       size += DWARF2_ADDR_SIZE;
4938       break;
4939     case DW_OP_const1u:
4940     case DW_OP_const1s:
4941       size += 1;
4942       break;
4943     case DW_OP_const2u:
4944     case DW_OP_const2s:
4945       size += 2;
4946       break;
4947     case DW_OP_const4u:
4948     case DW_OP_const4s:
4949       size += 4;
4950       break;
4951     case DW_OP_const8u:
4952     case DW_OP_const8s:
4953       size += 8;
4954       break;
4955     case DW_OP_constu:
4956       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4957       break;
4958     case DW_OP_consts:
4959       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4960       break;
4961     case DW_OP_pick:
4962       size += 1;
4963       break;
4964     case DW_OP_plus_uconst:
4965       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4966       break;
4967     case DW_OP_skip:
4968     case DW_OP_bra:
4969       size += 2;
4970       break;
4971     case DW_OP_breg0:
4972     case DW_OP_breg1:
4973     case DW_OP_breg2:
4974     case DW_OP_breg3:
4975     case DW_OP_breg4:
4976     case DW_OP_breg5:
4977     case DW_OP_breg6:
4978     case DW_OP_breg7:
4979     case DW_OP_breg8:
4980     case DW_OP_breg9:
4981     case DW_OP_breg10:
4982     case DW_OP_breg11:
4983     case DW_OP_breg12:
4984     case DW_OP_breg13:
4985     case DW_OP_breg14:
4986     case DW_OP_breg15:
4987     case DW_OP_breg16:
4988     case DW_OP_breg17:
4989     case DW_OP_breg18:
4990     case DW_OP_breg19:
4991     case DW_OP_breg20:
4992     case DW_OP_breg21:
4993     case DW_OP_breg22:
4994     case DW_OP_breg23:
4995     case DW_OP_breg24:
4996     case DW_OP_breg25:
4997     case DW_OP_breg26:
4998     case DW_OP_breg27:
4999     case DW_OP_breg28:
5000     case DW_OP_breg29:
5001     case DW_OP_breg30:
5002     case DW_OP_breg31:
5003       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
5004       break;
5005     case DW_OP_regx:
5006       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5007       break;
5008     case DW_OP_fbreg:
5009       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
5010       break;
5011     case DW_OP_bregx:
5012       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5013       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5014       break;
5015     case DW_OP_piece:
5016       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5017       break;
5018     case DW_OP_bit_piece:
5019       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5020       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
5021       break;
5022     case DW_OP_deref_size:
5023     case DW_OP_xderef_size:
5024       size += 1;
5025       break;
5026     case DW_OP_call2:
5027       size += 2;
5028       break;
5029     case DW_OP_call4:
5030       size += 4;
5031       break;
5032     case DW_OP_call_ref:
5033       size += DWARF_REF_SIZE;
5034       break;
5035     case DW_OP_implicit_value:
5036       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5037               + loc->dw_loc_oprnd1.v.val_unsigned;
5038       break;
5039     case DW_OP_GNU_implicit_pointer:
5040       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5041       break;
5042     case DW_OP_GNU_entry_value:
5043       {
5044         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5045         size += size_of_uleb128 (op_size) + op_size;
5046         break;
5047       }
5048     case DW_OP_GNU_const_type:
5049       {
5050         unsigned long o
5051           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
5052         size += size_of_uleb128 (o) + 1;
5053         switch (loc->dw_loc_oprnd2.val_class)
5054           {
5055           case dw_val_class_vec:
5056             size += loc->dw_loc_oprnd2.v.val_vec.length
5057                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
5058             break;
5059           case dw_val_class_const:
5060             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
5061             break;
5062           case dw_val_class_const_double:
5063             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
5064             break;
5065           default:
5066             gcc_unreachable ();
5067           }
5068         break;
5069       }
5070     case DW_OP_GNU_regval_type:
5071       {
5072         unsigned long o
5073           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
5074         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5075                 + size_of_uleb128 (o);
5076       }
5077       break;
5078     case DW_OP_GNU_deref_type:
5079       {
5080         unsigned long o
5081           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
5082         size += 1 + size_of_uleb128 (o);
5083       }
5084       break;
5085     case DW_OP_GNU_convert:
5086     case DW_OP_GNU_reinterpret:
5087       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
5088         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5089       else
5090         {
5091           unsigned long o
5092             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
5093           size += size_of_uleb128 (o);
5094         }
5095       break;
5096     case DW_OP_GNU_parameter_ref:
5097       size += 4;
5098       break;
5099     default:
5100       break;
5101     }
5102
5103   return size;
5104 }
5105
5106 /* Return the size of a series of location descriptors.  */
5107
5108 static unsigned long
5109 size_of_locs (dw_loc_descr_ref loc)
5110 {
5111   dw_loc_descr_ref l;
5112   unsigned long size;
5113
5114   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5115      field, to avoid writing to a PCH file.  */
5116   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5117     {
5118       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5119         break;
5120       size += size_of_loc_descr (l);
5121     }
5122   if (! l)
5123     return size;
5124
5125   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5126     {
5127       l->dw_loc_addr = size;
5128       size += size_of_loc_descr (l);
5129     }
5130
5131   return size;
5132 }
5133
5134 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5135 static void get_ref_die_offset_label (char *, dw_die_ref);
5136 static unsigned long int get_ref_die_offset (dw_die_ref);
5137 static void output_loc_sequence (dw_loc_descr_ref, int);
5138
5139 /* Output location description stack opcode's operands (if any).
5140    The for_eh_or_skip parameter controls whether register numbers are
5141    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5142    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5143    info).  This should be suppressed for the cases that have not been converted
5144    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5145
5146 static void
5147 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5148 {
5149   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5150   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5151
5152   switch (loc->dw_loc_opc)
5153     {
5154 #ifdef DWARF2_DEBUGGING_INFO
5155     case DW_OP_const2u:
5156     case DW_OP_const2s:
5157       dw2_asm_output_data (2, val1->v.val_int, NULL);
5158       break;
5159     case DW_OP_const4u:
5160       if (loc->dtprel)
5161         {
5162           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5163           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5164                                                val1->v.val_addr);
5165           fputc ('\n', asm_out_file);
5166           break;
5167         }
5168       /* FALLTHRU */
5169     case DW_OP_const4s:
5170       dw2_asm_output_data (4, val1->v.val_int, NULL);
5171       break;
5172     case DW_OP_const8u:
5173       if (loc->dtprel)
5174         {
5175           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5176           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5177                                                val1->v.val_addr);
5178           fputc ('\n', asm_out_file);
5179           break;
5180         }
5181       /* FALLTHRU */
5182     case DW_OP_const8s:
5183       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5184       dw2_asm_output_data (8, val1->v.val_int, NULL);
5185       break;
5186     case DW_OP_skip:
5187     case DW_OP_bra:
5188       {
5189         int offset;
5190
5191         gcc_assert (val1->val_class == dw_val_class_loc);
5192         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5193
5194         dw2_asm_output_data (2, offset, NULL);
5195       }
5196       break;
5197     case DW_OP_implicit_value:
5198       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5199       switch (val2->val_class)
5200         {
5201         case dw_val_class_const:
5202           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5203           break;
5204         case dw_val_class_vec:
5205           {
5206             unsigned int elt_size = val2->v.val_vec.elt_size;
5207             unsigned int len = val2->v.val_vec.length;
5208             unsigned int i;
5209             unsigned char *p;
5210
5211             if (elt_size > sizeof (HOST_WIDE_INT))
5212               {
5213                 elt_size /= 2;
5214                 len *= 2;
5215               }
5216             for (i = 0, p = val2->v.val_vec.array;
5217                  i < len;
5218                  i++, p += elt_size)
5219               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5220                                    "fp or vector constant word %u", i);
5221           }
5222           break;
5223         case dw_val_class_const_double:
5224           {
5225             unsigned HOST_WIDE_INT first, second;
5226
5227             if (WORDS_BIG_ENDIAN)
5228               {
5229                 first = val2->v.val_double.high;
5230                 second = val2->v.val_double.low;
5231               }
5232             else
5233               {
5234                 first = val2->v.val_double.low;
5235                 second = val2->v.val_double.high;
5236               }
5237             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5238                                  first, NULL);
5239             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5240                                  second, NULL);
5241           }
5242           break;
5243         case dw_val_class_addr:
5244           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5245           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5246           break;
5247         default:
5248           gcc_unreachable ();
5249         }
5250       break;
5251 #else
5252     case DW_OP_const2u:
5253     case DW_OP_const2s:
5254     case DW_OP_const4u:
5255     case DW_OP_const4s:
5256     case DW_OP_const8u:
5257     case DW_OP_const8s:
5258     case DW_OP_skip:
5259     case DW_OP_bra:
5260     case DW_OP_implicit_value:
5261       /* We currently don't make any attempt to make sure these are
5262          aligned properly like we do for the main unwind info, so
5263          don't support emitting things larger than a byte if we're
5264          only doing unwinding.  */
5265       gcc_unreachable ();
5266 #endif
5267     case DW_OP_const1u:
5268     case DW_OP_const1s:
5269       dw2_asm_output_data (1, val1->v.val_int, NULL);
5270       break;
5271     case DW_OP_constu:
5272       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5273       break;
5274     case DW_OP_consts:
5275       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5276       break;
5277     case DW_OP_pick:
5278       dw2_asm_output_data (1, val1->v.val_int, NULL);
5279       break;
5280     case DW_OP_plus_uconst:
5281       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5282       break;
5283     case DW_OP_breg0:
5284     case DW_OP_breg1:
5285     case DW_OP_breg2:
5286     case DW_OP_breg3:
5287     case DW_OP_breg4:
5288     case DW_OP_breg5:
5289     case DW_OP_breg6:
5290     case DW_OP_breg7:
5291     case DW_OP_breg8:
5292     case DW_OP_breg9:
5293     case DW_OP_breg10:
5294     case DW_OP_breg11:
5295     case DW_OP_breg12:
5296     case DW_OP_breg13:
5297     case DW_OP_breg14:
5298     case DW_OP_breg15:
5299     case DW_OP_breg16:
5300     case DW_OP_breg17:
5301     case DW_OP_breg18:
5302     case DW_OP_breg19:
5303     case DW_OP_breg20:
5304     case DW_OP_breg21:
5305     case DW_OP_breg22:
5306     case DW_OP_breg23:
5307     case DW_OP_breg24:
5308     case DW_OP_breg25:
5309     case DW_OP_breg26:
5310     case DW_OP_breg27:
5311     case DW_OP_breg28:
5312     case DW_OP_breg29:
5313     case DW_OP_breg30:
5314     case DW_OP_breg31:
5315       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5316       break;
5317     case DW_OP_regx:
5318       {
5319         unsigned r = val1->v.val_unsigned;
5320         if (for_eh_or_skip >= 0)
5321           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5322         gcc_assert (size_of_uleb128 (r) 
5323                     == size_of_uleb128 (val1->v.val_unsigned));
5324         dw2_asm_output_data_uleb128 (r, NULL);  
5325       }
5326       break;
5327     case DW_OP_fbreg:
5328       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5329       break;
5330     case DW_OP_bregx:
5331       {
5332         unsigned r = val1->v.val_unsigned;
5333         if (for_eh_or_skip >= 0)
5334           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5335         gcc_assert (size_of_uleb128 (r) 
5336                     == size_of_uleb128 (val1->v.val_unsigned));
5337         dw2_asm_output_data_uleb128 (r, NULL);  
5338         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5339       }
5340       break;
5341     case DW_OP_piece:
5342       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5343       break;
5344     case DW_OP_bit_piece:
5345       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5346       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5347       break;
5348     case DW_OP_deref_size:
5349     case DW_OP_xderef_size:
5350       dw2_asm_output_data (1, val1->v.val_int, NULL);
5351       break;
5352
5353     case DW_OP_addr:
5354       if (loc->dtprel)
5355         {
5356           if (targetm.asm_out.output_dwarf_dtprel)
5357             {
5358               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5359                                                    DWARF2_ADDR_SIZE,
5360                                                    val1->v.val_addr);
5361               fputc ('\n', asm_out_file);
5362             }
5363           else
5364             gcc_unreachable ();
5365         }
5366       else
5367         {
5368 #ifdef DWARF2_DEBUGGING_INFO
5369           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5370 #else
5371           gcc_unreachable ();
5372 #endif
5373         }
5374       break;
5375
5376     case DW_OP_GNU_implicit_pointer:
5377       {
5378         char label[MAX_ARTIFICIAL_LABEL_BYTES
5379                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5380         gcc_assert (val1->val_class == dw_val_class_die_ref);
5381         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5382         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5383         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5384       }
5385       break;
5386
5387     case DW_OP_GNU_entry_value:
5388       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5389       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5390       break;
5391
5392     case DW_OP_GNU_const_type:
5393       {
5394         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
5395         gcc_assert (o);
5396         dw2_asm_output_data_uleb128 (o, NULL);
5397         switch (val2->val_class)
5398           {
5399           case dw_val_class_const:
5400             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
5401             dw2_asm_output_data (1, l, NULL);
5402             dw2_asm_output_data (l, val2->v.val_int, NULL);
5403             break;
5404           case dw_val_class_vec:
5405             {
5406               unsigned int elt_size = val2->v.val_vec.elt_size;
5407               unsigned int len = val2->v.val_vec.length;
5408               unsigned int i;
5409               unsigned char *p;
5410
5411               l = len * elt_size;
5412               dw2_asm_output_data (1, l, NULL);
5413               if (elt_size > sizeof (HOST_WIDE_INT))
5414                 {
5415                   elt_size /= 2;
5416                   len *= 2;
5417                 }
5418               for (i = 0, p = val2->v.val_vec.array;
5419                    i < len;
5420                    i++, p += elt_size)
5421                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5422                                      "fp or vector constant word %u", i);
5423             }
5424             break;
5425           case dw_val_class_const_double:
5426             {
5427               unsigned HOST_WIDE_INT first, second;
5428               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
5429
5430               dw2_asm_output_data (1, 2 * l, NULL);
5431               if (WORDS_BIG_ENDIAN)
5432                 {
5433                   first = val2->v.val_double.high;
5434                   second = val2->v.val_double.low;
5435                 }
5436               else
5437                 {
5438                   first = val2->v.val_double.low;
5439                   second = val2->v.val_double.high;
5440                 }
5441               dw2_asm_output_data (l, first, NULL);
5442               dw2_asm_output_data (l, second, NULL);
5443             }
5444             break;
5445           default:
5446             gcc_unreachable ();
5447           }
5448       }
5449       break;
5450     case DW_OP_GNU_regval_type:
5451       {
5452         unsigned r = val1->v.val_unsigned;
5453         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
5454         gcc_assert (o);
5455         if (for_eh_or_skip >= 0)
5456           {
5457             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5458             gcc_assert (size_of_uleb128 (r)
5459                         == size_of_uleb128 (val1->v.val_unsigned));
5460           }
5461         dw2_asm_output_data_uleb128 (r, NULL);
5462         dw2_asm_output_data_uleb128 (o, NULL);
5463       }
5464       break;
5465     case DW_OP_GNU_deref_type:
5466       {
5467         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
5468         gcc_assert (o);
5469         dw2_asm_output_data (1, val1->v.val_int, NULL);
5470         dw2_asm_output_data_uleb128 (o, NULL);
5471       }
5472       break;
5473     case DW_OP_GNU_convert:
5474     case DW_OP_GNU_reinterpret:
5475       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
5476         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5477       else
5478         {
5479           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
5480           gcc_assert (o);
5481           dw2_asm_output_data_uleb128 (o, NULL);
5482         }
5483       break;
5484
5485     case DW_OP_GNU_parameter_ref:
5486       {
5487         unsigned long o;
5488         gcc_assert (val1->val_class == dw_val_class_die_ref);
5489         o = get_ref_die_offset (val1->v.val_die_ref.die);
5490         dw2_asm_output_data (4, o, NULL);
5491       }
5492       break;
5493
5494     default:
5495       /* Other codes have no operands.  */
5496       break;
5497     }
5498 }
5499
5500 /* Output a sequence of location operations.  
5501    The for_eh_or_skip parameter controls whether register numbers are
5502    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5503    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5504    info).  This should be suppressed for the cases that have not been converted
5505    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5506
5507 static void
5508 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5509 {
5510   for (; loc != NULL; loc = loc->dw_loc_next)
5511     {
5512       enum dwarf_location_atom opc = loc->dw_loc_opc;
5513       /* Output the opcode.  */
5514       if (for_eh_or_skip >= 0 
5515           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5516         {
5517           unsigned r = (opc - DW_OP_breg0);
5518           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5519           gcc_assert (r <= 31);
5520           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5521         }
5522       else if (for_eh_or_skip >= 0 
5523                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5524         {
5525           unsigned r = (opc - DW_OP_reg0);
5526           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5527           gcc_assert (r <= 31);
5528           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5529         }
5530
5531       dw2_asm_output_data (1, opc,
5532                              "%s", dwarf_stack_op_name (opc));
5533
5534       /* Output the operand(s) (if any).  */
5535       output_loc_operands (loc, for_eh_or_skip);
5536     }
5537 }
5538
5539 /* Output location description stack opcode's operands (if any).
5540    The output is single bytes on a line, suitable for .cfi_escape.  */
5541
5542 static void
5543 output_loc_operands_raw (dw_loc_descr_ref loc)
5544 {
5545   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5546   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5547
5548   switch (loc->dw_loc_opc)
5549     {
5550     case DW_OP_addr:
5551     case DW_OP_implicit_value:
5552       /* We cannot output addresses in .cfi_escape, only bytes.  */
5553       gcc_unreachable ();
5554
5555     case DW_OP_const1u:
5556     case DW_OP_const1s:
5557     case DW_OP_pick:
5558     case DW_OP_deref_size:
5559     case DW_OP_xderef_size:
5560       fputc (',', asm_out_file);
5561       dw2_asm_output_data_raw (1, val1->v.val_int);
5562       break;
5563
5564     case DW_OP_const2u:
5565     case DW_OP_const2s:
5566       fputc (',', asm_out_file);
5567       dw2_asm_output_data_raw (2, val1->v.val_int);
5568       break;
5569
5570     case DW_OP_const4u:
5571     case DW_OP_const4s:
5572       fputc (',', asm_out_file);
5573       dw2_asm_output_data_raw (4, val1->v.val_int);
5574       break;
5575
5576     case DW_OP_const8u:
5577     case DW_OP_const8s:
5578       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5579       fputc (',', asm_out_file);
5580       dw2_asm_output_data_raw (8, val1->v.val_int);
5581       break;
5582
5583     case DW_OP_skip:
5584     case DW_OP_bra:
5585       {
5586         int offset;
5587
5588         gcc_assert (val1->val_class == dw_val_class_loc);
5589         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5590
5591         fputc (',', asm_out_file);
5592         dw2_asm_output_data_raw (2, offset);
5593       }
5594       break;
5595
5596     case DW_OP_regx:
5597       {
5598         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5599         gcc_assert (size_of_uleb128 (r) 
5600                     == size_of_uleb128 (val1->v.val_unsigned));
5601         fputc (',', asm_out_file);
5602         dw2_asm_output_data_uleb128_raw (r);
5603       }
5604       break;
5605       
5606     case DW_OP_constu:
5607     case DW_OP_plus_uconst:
5608     case DW_OP_piece:
5609       fputc (',', asm_out_file);
5610       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5611       break;
5612
5613     case DW_OP_bit_piece:
5614       fputc (',', asm_out_file);
5615       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5616       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5617       break;
5618
5619     case DW_OP_consts:
5620     case DW_OP_breg0:
5621     case DW_OP_breg1:
5622     case DW_OP_breg2:
5623     case DW_OP_breg3:
5624     case DW_OP_breg4:
5625     case DW_OP_breg5:
5626     case DW_OP_breg6:
5627     case DW_OP_breg7:
5628     case DW_OP_breg8:
5629     case DW_OP_breg9:
5630     case DW_OP_breg10:
5631     case DW_OP_breg11:
5632     case DW_OP_breg12:
5633     case DW_OP_breg13:
5634     case DW_OP_breg14:
5635     case DW_OP_breg15:
5636     case DW_OP_breg16:
5637     case DW_OP_breg17:
5638     case DW_OP_breg18:
5639     case DW_OP_breg19:
5640     case DW_OP_breg20:
5641     case DW_OP_breg21:
5642     case DW_OP_breg22:
5643     case DW_OP_breg23:
5644     case DW_OP_breg24:
5645     case DW_OP_breg25:
5646     case DW_OP_breg26:
5647     case DW_OP_breg27:
5648     case DW_OP_breg28:
5649     case DW_OP_breg29:
5650     case DW_OP_breg30:
5651     case DW_OP_breg31:
5652     case DW_OP_fbreg:
5653       fputc (',', asm_out_file);
5654       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5655       break;
5656
5657     case DW_OP_bregx:
5658       {
5659         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5660         gcc_assert (size_of_uleb128 (r) 
5661                     == size_of_uleb128 (val1->v.val_unsigned));
5662         fputc (',', asm_out_file);
5663         dw2_asm_output_data_uleb128_raw (r);
5664         fputc (',', asm_out_file);
5665         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5666       }
5667       break;
5668
5669     case DW_OP_GNU_implicit_pointer:
5670     case DW_OP_GNU_entry_value:
5671     case DW_OP_GNU_const_type:
5672     case DW_OP_GNU_regval_type:
5673     case DW_OP_GNU_deref_type:
5674     case DW_OP_GNU_convert:
5675     case DW_OP_GNU_reinterpret:
5676     case DW_OP_GNU_parameter_ref:
5677       gcc_unreachable ();
5678       break;
5679
5680     default:
5681       /* Other codes have no operands.  */
5682       break;
5683     }
5684 }
5685
5686 static void
5687 output_loc_sequence_raw (dw_loc_descr_ref loc)
5688 {
5689   while (1)
5690     {
5691       enum dwarf_location_atom opc = loc->dw_loc_opc;
5692       /* Output the opcode.  */
5693       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5694         {
5695           unsigned r = (opc - DW_OP_breg0);
5696           r = DWARF2_FRAME_REG_OUT (r, 1);
5697           gcc_assert (r <= 31);
5698           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5699         }
5700       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5701         {
5702           unsigned r = (opc - DW_OP_reg0);
5703           r = DWARF2_FRAME_REG_OUT (r, 1);
5704           gcc_assert (r <= 31);
5705           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5706         }
5707       /* Output the opcode.  */
5708       fprintf (asm_out_file, "%#x", opc);
5709       output_loc_operands_raw (loc);
5710
5711       if (!loc->dw_loc_next)
5712         break;
5713       loc = loc->dw_loc_next;
5714
5715       fputc (',', asm_out_file);
5716     }
5717 }
5718
5719 /* This routine will generate the correct assembly data for a location
5720    description based on a cfi entry with a complex address.  */
5721
5722 static void
5723 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5724 {
5725   dw_loc_descr_ref loc;
5726   unsigned long size;
5727
5728   if (cfi->dw_cfi_opc == DW_CFA_expression)
5729     {
5730       unsigned r = 
5731         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5732       dw2_asm_output_data (1, r, NULL);
5733       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5734     }
5735   else
5736     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5737
5738   /* Output the size of the block.  */
5739   size = size_of_locs (loc);
5740   dw2_asm_output_data_uleb128 (size, NULL);
5741
5742   /* Now output the operations themselves.  */
5743   output_loc_sequence (loc, for_eh);
5744 }
5745
5746 /* Similar, but used for .cfi_escape.  */
5747
5748 static void
5749 output_cfa_loc_raw (dw_cfi_ref cfi)
5750 {
5751   dw_loc_descr_ref loc;
5752   unsigned long size;
5753
5754   if (cfi->dw_cfi_opc == DW_CFA_expression)
5755     {
5756       unsigned r = 
5757         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5758       fprintf (asm_out_file, "%#x,", r);
5759       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5760     }
5761   else
5762     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5763
5764   /* Output the size of the block.  */
5765   size = size_of_locs (loc);
5766   dw2_asm_output_data_uleb128_raw (size);
5767   fputc (',', asm_out_file);
5768
5769   /* Now output the operations themselves.  */
5770   output_loc_sequence_raw (loc);
5771 }
5772
5773 /* This function builds a dwarf location descriptor sequence from a
5774    dw_cfa_location, adding the given OFFSET to the result of the
5775    expression.  */
5776
5777 static struct dw_loc_descr_struct *
5778 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5779 {
5780   struct dw_loc_descr_struct *head, *tmp;
5781
5782   offset += cfa->offset;
5783
5784   if (cfa->indirect)
5785     {
5786       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5787       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5788       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5789       add_loc_descr (&head, tmp);
5790       if (offset != 0)
5791         {
5792           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5793           add_loc_descr (&head, tmp);
5794         }
5795     }
5796   else
5797     head = new_reg_loc_descr (cfa->reg, offset);
5798
5799   return head;
5800 }
5801
5802 /* This function builds a dwarf location descriptor sequence for
5803    the address at OFFSET from the CFA when stack is aligned to
5804    ALIGNMENT byte.  */
5805
5806 static struct dw_loc_descr_struct *
5807 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5808 {
5809   struct dw_loc_descr_struct *head;
5810   unsigned int dwarf_fp
5811     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5812
5813  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5814   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5815     {
5816       head = new_reg_loc_descr (dwarf_fp, 0);
5817       add_loc_descr (&head, int_loc_descriptor (alignment));
5818       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5819       loc_descr_plus_const (&head, offset);
5820     }
5821   else
5822     head = new_reg_loc_descr (dwarf_fp, offset);
5823   return head;
5824 }
5825
5826 /* This function fills in aa dw_cfa_location structure from a dwarf location
5827    descriptor sequence.  */
5828
5829 static void
5830 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5831 {
5832   struct dw_loc_descr_struct *ptr;
5833   cfa->offset = 0;
5834   cfa->base_offset = 0;
5835   cfa->indirect = 0;
5836   cfa->reg = -1;
5837
5838   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5839     {
5840       enum dwarf_location_atom op = ptr->dw_loc_opc;
5841
5842       switch (op)
5843         {
5844         case DW_OP_reg0:
5845         case DW_OP_reg1:
5846         case DW_OP_reg2:
5847         case DW_OP_reg3:
5848         case DW_OP_reg4:
5849         case DW_OP_reg5:
5850         case DW_OP_reg6:
5851         case DW_OP_reg7:
5852         case DW_OP_reg8:
5853         case DW_OP_reg9:
5854         case DW_OP_reg10:
5855         case DW_OP_reg11:
5856         case DW_OP_reg12:
5857         case DW_OP_reg13:
5858         case DW_OP_reg14:
5859         case DW_OP_reg15:
5860         case DW_OP_reg16:
5861         case DW_OP_reg17:
5862         case DW_OP_reg18:
5863         case DW_OP_reg19:
5864         case DW_OP_reg20:
5865         case DW_OP_reg21:
5866         case DW_OP_reg22:
5867         case DW_OP_reg23:
5868         case DW_OP_reg24:
5869         case DW_OP_reg25:
5870         case DW_OP_reg26:
5871         case DW_OP_reg27:
5872         case DW_OP_reg28:
5873         case DW_OP_reg29:
5874         case DW_OP_reg30:
5875         case DW_OP_reg31:
5876           cfa->reg = op - DW_OP_reg0;
5877           break;
5878         case DW_OP_regx:
5879           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5880           break;
5881         case DW_OP_breg0:
5882         case DW_OP_breg1:
5883         case DW_OP_breg2:
5884         case DW_OP_breg3:
5885         case DW_OP_breg4:
5886         case DW_OP_breg5:
5887         case DW_OP_breg6:
5888         case DW_OP_breg7:
5889         case DW_OP_breg8:
5890         case DW_OP_breg9:
5891         case DW_OP_breg10:
5892         case DW_OP_breg11:
5893         case DW_OP_breg12:
5894         case DW_OP_breg13:
5895         case DW_OP_breg14:
5896         case DW_OP_breg15:
5897         case DW_OP_breg16:
5898         case DW_OP_breg17:
5899         case DW_OP_breg18:
5900         case DW_OP_breg19:
5901         case DW_OP_breg20:
5902         case DW_OP_breg21:
5903         case DW_OP_breg22:
5904         case DW_OP_breg23:
5905         case DW_OP_breg24:
5906         case DW_OP_breg25:
5907         case DW_OP_breg26:
5908         case DW_OP_breg27:
5909         case DW_OP_breg28:
5910         case DW_OP_breg29:
5911         case DW_OP_breg30:
5912         case DW_OP_breg31:
5913           cfa->reg = op - DW_OP_breg0;
5914           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5915           break;
5916         case DW_OP_bregx:
5917           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5918           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5919           break;
5920         case DW_OP_deref:
5921           cfa->indirect = 1;
5922           break;
5923         case DW_OP_plus_uconst:
5924           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5925           break;
5926         default:
5927           internal_error ("DW_LOC_OP %s not implemented",
5928                           dwarf_stack_op_name (ptr->dw_loc_opc));
5929         }
5930     }
5931 }
5932 \f
5933 /* And now, the support for symbolic debugging information.  */
5934
5935 /* .debug_str support.  */
5936 static int output_indirect_string (void **, void *);
5937
5938 static void dwarf2out_init (const char *);
5939 static void dwarf2out_finish (const char *);
5940 static void dwarf2out_assembly_start (void);
5941 static void dwarf2out_define (unsigned int, const char *);
5942 static void dwarf2out_undef (unsigned int, const char *);
5943 static void dwarf2out_start_source_file (unsigned, const char *);
5944 static void dwarf2out_end_source_file (unsigned);
5945 static void dwarf2out_function_decl (tree);
5946 static void dwarf2out_begin_block (unsigned, unsigned);
5947 static void dwarf2out_end_block (unsigned, unsigned);
5948 static bool dwarf2out_ignore_block (const_tree);
5949 static void dwarf2out_global_decl (tree);
5950 static void dwarf2out_type_decl (tree, int);
5951 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5952 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5953                                                  dw_die_ref);
5954 static void dwarf2out_abstract_function (tree);
5955 static void dwarf2out_var_location (rtx);
5956 static void dwarf2out_begin_function (tree);
5957 static void dwarf2out_set_name (tree, tree);
5958
5959 /* The debug hooks structure.  */
5960
5961 const struct gcc_debug_hooks dwarf2_debug_hooks =
5962 {
5963   dwarf2out_init,
5964   dwarf2out_finish,
5965   dwarf2out_assembly_start,
5966   dwarf2out_define,
5967   dwarf2out_undef,
5968   dwarf2out_start_source_file,
5969   dwarf2out_end_source_file,
5970   dwarf2out_begin_block,
5971   dwarf2out_end_block,
5972   dwarf2out_ignore_block,
5973   dwarf2out_source_line,
5974   dwarf2out_begin_prologue,
5975 #if VMS_DEBUGGING_INFO
5976   dwarf2out_vms_end_prologue,
5977   dwarf2out_vms_begin_epilogue,
5978 #else
5979   debug_nothing_int_charstar,
5980   debug_nothing_int_charstar,
5981 #endif
5982   dwarf2out_end_epilogue,
5983   dwarf2out_begin_function,
5984   debug_nothing_int,            /* end_function */
5985   dwarf2out_function_decl,      /* function_decl */
5986   dwarf2out_global_decl,
5987   dwarf2out_type_decl,          /* type_decl */
5988   dwarf2out_imported_module_or_decl,
5989   debug_nothing_tree,           /* deferred_inline_function */
5990   /* The DWARF 2 backend tries to reduce debugging bloat by not
5991      emitting the abstract description of inline functions until
5992      something tries to reference them.  */
5993   dwarf2out_abstract_function,  /* outlining_inline_function */
5994   debug_nothing_rtx,            /* label */
5995   debug_nothing_int,            /* handle_pch */
5996   dwarf2out_var_location,
5997   dwarf2out_switch_text_section,
5998   dwarf2out_set_name,
5999   1,                            /* start_end_main_source_file */
6000   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
6001 };
6002 \f
6003 /* NOTE: In the comments in this file, many references are made to
6004    "Debugging Information Entries".  This term is abbreviated as `DIE'
6005    throughout the remainder of this file.  */
6006
6007 /* An internal representation of the DWARF output is built, and then
6008    walked to generate the DWARF debugging info.  The walk of the internal
6009    representation is done after the entire program has been compiled.
6010    The types below are used to describe the internal representation.  */
6011
6012 /* Whether to put type DIEs into their own section .debug_types instead
6013    of making them part of the .debug_info section.  Only supported for
6014    Dwarf V4 or higher and the user didn't disable them through
6015    -fno-debug-types-section.  It is more efficient to put them in a
6016    separate comdat sections since the linker will then be able to
6017    remove duplicates.  But not all tools support .debug_types sections
6018    yet.  */
6019
6020 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
6021
6022 /* Various DIE's use offsets relative to the beginning of the
6023    .debug_info section to refer to each other.  */
6024
6025 typedef long int dw_offset;
6026
6027 /* Define typedefs here to avoid circular dependencies.  */
6028
6029 typedef struct dw_attr_struct *dw_attr_ref;
6030 typedef struct dw_line_info_struct *dw_line_info_ref;
6031 typedef struct pubname_struct *pubname_ref;
6032 typedef struct dw_ranges_struct *dw_ranges_ref;
6033 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
6034 typedef struct comdat_type_struct *comdat_type_node_ref;
6035
6036 /* The entries in the line_info table more-or-less mirror the opcodes
6037    that are used in the real dwarf line table.  Arrays of these entries
6038    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
6039    supported.  */
6040
6041 enum dw_line_info_opcode {
6042   /* Emit DW_LNE_set_address; the operand is the label index.  */
6043   LI_set_address,
6044
6045   /* Emit a row to the matrix with the given line.  This may be done
6046      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
6047      special opcodes.  */
6048   LI_set_line,
6049
6050   /* Emit a DW_LNS_set_file.  */
6051   LI_set_file,
6052
6053   /* Emit a DW_LNS_set_column.  */
6054   LI_set_column,
6055
6056   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
6057   LI_negate_stmt,
6058
6059   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
6060   LI_set_prologue_end,
6061   LI_set_epilogue_begin,
6062
6063   /* Emit a DW_LNE_set_discriminator.  */
6064   LI_set_discriminator
6065 };
6066
6067 typedef struct GTY(()) dw_line_info_struct {
6068   enum dw_line_info_opcode opcode;
6069   unsigned int val;
6070 } dw_line_info_entry;
6071
6072 DEF_VEC_O(dw_line_info_entry);
6073 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
6074
6075 typedef struct GTY(()) dw_line_info_table_struct {
6076   /* The label that marks the end of this section.  */
6077   const char *end_label;
6078
6079   /* The values for the last row of the matrix, as collected in the table.
6080      These are used to minimize the changes to the next row.  */
6081   unsigned int file_num;
6082   unsigned int line_num;
6083   unsigned int column_num;
6084   int discrim_num;
6085   bool is_stmt;
6086   bool in_use;
6087
6088   VEC(dw_line_info_entry, gc) *entries;
6089 } dw_line_info_table;
6090
6091 typedef dw_line_info_table *dw_line_info_table_p;
6092
6093 DEF_VEC_P(dw_line_info_table_p);
6094 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
6095
6096 /* Each DIE attribute has a field specifying the attribute kind,
6097    a link to the next attribute in the chain, and an attribute value.
6098    Attributes are typically linked below the DIE they modify.  */
6099
6100 typedef struct GTY(()) dw_attr_struct {
6101   enum dwarf_attribute dw_attr;
6102   dw_val_node dw_attr_val;
6103 }
6104 dw_attr_node;
6105
6106 DEF_VEC_O(dw_attr_node);
6107 DEF_VEC_ALLOC_O(dw_attr_node,gc);
6108
6109 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
6110    The children of each node form a circular list linked by
6111    die_sib.  die_child points to the node *before* the "first" child node.  */
6112
6113 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
6114   union die_symbol_or_type_node
6115     {
6116       char * GTY ((tag ("0"))) die_symbol;
6117       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
6118     }
6119   GTY ((desc ("use_debug_types"))) die_id;
6120   VEC(dw_attr_node,gc) * die_attr;
6121   dw_die_ref die_parent;
6122   dw_die_ref die_child;
6123   dw_die_ref die_sib;
6124   dw_die_ref die_definition; /* ref from a specification to its definition */
6125   dw_offset die_offset;
6126   unsigned long die_abbrev;
6127   int die_mark;
6128   /* Die is used and must not be pruned as unused.  */
6129   int die_perennial_p;
6130   unsigned int decl_id;
6131   enum dwarf_tag die_tag;
6132 }
6133 die_node;
6134
6135 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
6136 #define FOR_EACH_CHILD(die, c, expr) do {       \
6137   c = die->die_child;                           \
6138   if (c) do {                                   \
6139     c = c->die_sib;                             \
6140     expr;                                       \
6141   } while (c != die->die_child);                \
6142 } while (0)
6143
6144 /* The pubname structure */
6145
6146 typedef struct GTY(()) pubname_struct {
6147   dw_die_ref die;
6148   const char *name;
6149 }
6150 pubname_entry;
6151
6152 DEF_VEC_O(pubname_entry);
6153 DEF_VEC_ALLOC_O(pubname_entry, gc);
6154
6155 struct GTY(()) dw_ranges_struct {
6156   /* If this is positive, it's a block number, otherwise it's a
6157      bitwise-negated index into dw_ranges_by_label.  */
6158   int num;
6159 };
6160
6161 /* A structure to hold a macinfo entry.  */
6162
6163 typedef struct GTY(()) macinfo_struct {
6164   unsigned HOST_WIDE_INT code;
6165   unsigned HOST_WIDE_INT lineno;
6166   const char *info;
6167 }
6168 macinfo_entry;
6169
6170 DEF_VEC_O(macinfo_entry);
6171 DEF_VEC_ALLOC_O(macinfo_entry, gc);
6172
6173 struct GTY(()) dw_ranges_by_label_struct {
6174   const char *begin;
6175   const char *end;
6176 };
6177
6178 /* The comdat type node structure.  */
6179 typedef struct GTY(()) comdat_type_struct
6180 {
6181   dw_die_ref root_die;
6182   dw_die_ref type_die;
6183   char signature[DWARF_TYPE_SIGNATURE_SIZE];
6184   struct comdat_type_struct *next;
6185 }
6186 comdat_type_node;
6187
6188 /* The limbo die list structure.  */
6189 typedef struct GTY(()) limbo_die_struct {
6190   dw_die_ref die;
6191   tree created_for;
6192   struct limbo_die_struct *next;
6193 }
6194 limbo_die_node;
6195
6196 typedef struct skeleton_chain_struct
6197 {
6198   dw_die_ref old_die;
6199   dw_die_ref new_die;
6200   struct skeleton_chain_struct *parent;
6201 }
6202 skeleton_chain_node;
6203
6204 /* How to start an assembler comment.  */
6205 #ifndef ASM_COMMENT_START
6206 #define ASM_COMMENT_START ";#"
6207 #endif
6208
6209 /* Define a macro which returns nonzero for a TYPE_DECL which was
6210    implicitly generated for a tagged type.
6211
6212    Note that unlike the gcc front end (which generates a NULL named
6213    TYPE_DECL node for each complete tagged type, each array type, and
6214    each function type node created) the g++ front end generates a
6215    _named_ TYPE_DECL node for each tagged type node created.
6216    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
6217    generate a DW_TAG_typedef DIE for them.  */
6218
6219 #define TYPE_DECL_IS_STUB(decl)                         \
6220   (DECL_NAME (decl) == NULL_TREE                        \
6221    || (DECL_ARTIFICIAL (decl)                           \
6222        && is_tagged_type (TREE_TYPE (decl))             \
6223        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
6224            /* This is necessary for stub decls that     \
6225               appear in nested inline functions.  */    \
6226            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6227                && (decl_ultimate_origin (decl)          \
6228                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6229
6230 /* Information concerning the compilation unit's programming
6231    language, and compiler version.  */
6232
6233 /* Fixed size portion of the DWARF compilation unit header.  */
6234 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6235   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6236
6237 /* Fixed size portion of the DWARF comdat type unit header.  */
6238 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6239   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6240    + DWARF_OFFSET_SIZE)
6241
6242 /* Fixed size portion of public names info.  */
6243 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6244
6245 /* Fixed size portion of the address range info.  */
6246 #define DWARF_ARANGES_HEADER_SIZE                                       \
6247   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6248                 DWARF2_ADDR_SIZE * 2)                                   \
6249    - DWARF_INITIAL_LENGTH_SIZE)
6250
6251 /* Size of padding portion in the address range info.  It must be
6252    aligned to twice the pointer size.  */
6253 #define DWARF_ARANGES_PAD_SIZE \
6254   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6255                 DWARF2_ADDR_SIZE * 2)                              \
6256    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6257
6258 /* Use assembler line directives if available.  */
6259 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6260 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6261 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6262 #else
6263 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6264 #endif
6265 #endif
6266
6267 /* Minimum line offset in a special line info. opcode.
6268    This value was chosen to give a reasonable range of values.  */
6269 #define DWARF_LINE_BASE  -10
6270
6271 /* First special line opcode - leave room for the standard opcodes.  */
6272 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
6273
6274 /* Range of line offsets in a special line info. opcode.  */
6275 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6276
6277 /* Flag that indicates the initial value of the is_stmt_start flag.
6278    In the present implementation, we do not mark any lines as
6279    the beginning of a source statement, because that information
6280    is not made available by the GCC front-end.  */
6281 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6282
6283 /* Maximum number of operations per instruction bundle.  */
6284 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6285 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6286 #endif
6287
6288 /* This location is used by calc_die_sizes() to keep track
6289    the offset of each DIE within the .debug_info section.  */
6290 static unsigned long next_die_offset;
6291
6292 /* Record the root of the DIE's built for the current compilation unit.  */
6293 static GTY(()) dw_die_ref single_comp_unit_die;
6294
6295 /* A list of type DIEs that have been separated into comdat sections.  */
6296 static GTY(()) comdat_type_node *comdat_type_list;
6297
6298 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6299 static GTY(()) limbo_die_node *limbo_die_list;
6300
6301 /* A list of DIEs for which we may have to generate
6302    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6303 static GTY(()) limbo_die_node *deferred_asm_name;
6304
6305 /* Filenames referenced by this compilation unit.  */
6306 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6307
6308 /* A hash table of references to DIE's that describe declarations.
6309    The key is a DECL_UID() which is a unique number identifying each decl.  */
6310 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6311
6312 /* A hash table of references to DIE's that describe COMMON blocks.
6313    The key is DECL_UID() ^ die_parent.  */
6314 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6315
6316 typedef struct GTY(()) die_arg_entry_struct {
6317     dw_die_ref die;
6318     tree arg;
6319 } die_arg_entry;
6320
6321 DEF_VEC_O(die_arg_entry);
6322 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6323
6324 /* Node of the variable location list.  */
6325 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6326   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6327      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6328      in mode of the EXPR_LIST node and first EXPR_LIST operand
6329      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6330      location or NULL for padding.  For larger bitsizes,
6331      mode is 0 and first operand is a CONCAT with bitsize
6332      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6333      NULL as second operand.  */
6334   rtx GTY (()) loc;
6335   const char * GTY (()) label;
6336   struct var_loc_node * GTY (()) next;
6337 };
6338
6339 /* Variable location list.  */
6340 struct GTY (()) var_loc_list_def {
6341   struct var_loc_node * GTY (()) first;
6342
6343   /* Pointer to the last but one or last element of the
6344      chained list.  If the list is empty, both first and
6345      last are NULL, if the list contains just one node
6346      or the last node certainly is not redundant, it points
6347      to the last node, otherwise points to the last but one.
6348      Do not mark it for GC because it is marked through the chain.  */
6349   struct var_loc_node * GTY ((skip ("%h"))) last;
6350
6351   /* Pointer to the last element before section switch,
6352      if NULL, either sections weren't switched or first
6353      is after section switch.  */
6354   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
6355
6356   /* DECL_UID of the variable decl.  */
6357   unsigned int decl_id;
6358 };
6359 typedef struct var_loc_list_def var_loc_list;
6360
6361 /* Call argument location list.  */
6362 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6363   rtx GTY (()) call_arg_loc_note;
6364   const char * GTY (()) label;
6365   tree GTY (()) block;
6366   bool tail_call_p;
6367   rtx GTY (()) symbol_ref;
6368   struct call_arg_loc_node * GTY (()) next;
6369 };
6370
6371
6372 /* Table of decl location linked lists.  */
6373 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6374
6375 /* Head and tail of call_arg_loc chain.  */
6376 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6377 static struct call_arg_loc_node *call_arg_loc_last;
6378
6379 /* Number of call sites in the current function.  */
6380 static int call_site_count = -1;
6381 /* Number of tail call sites in the current function.  */
6382 static int tail_call_site_count = -1;
6383
6384 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6385    DIEs.  */
6386 static VEC (dw_die_ref, heap) *block_map;
6387
6388 /* A cached location list.  */
6389 struct GTY (()) cached_dw_loc_list_def {
6390   /* The DECL_UID of the decl that this entry describes.  */
6391   unsigned int decl_id;
6392
6393   /* The cached location list.  */
6394   dw_loc_list_ref loc_list;
6395 };
6396 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
6397
6398 /* Table of cached location lists.  */
6399 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
6400
6401 /* A pointer to the base of a list of references to DIE's that
6402    are uniquely identified by their tag, presence/absence of
6403    children DIE's, and list of attribute/value pairs.  */
6404 static GTY((length ("abbrev_die_table_allocated")))
6405   dw_die_ref *abbrev_die_table;
6406
6407 /* Number of elements currently allocated for abbrev_die_table.  */
6408 static GTY(()) unsigned abbrev_die_table_allocated;
6409
6410 /* Number of elements in type_die_table currently in use.  */
6411 static GTY(()) unsigned abbrev_die_table_in_use;
6412
6413 /* Size (in elements) of increments by which we may expand the
6414    abbrev_die_table.  */
6415 #define ABBREV_DIE_TABLE_INCREMENT 256
6416
6417 /* A global counter for generating labels for line number data.  */
6418 static unsigned int line_info_label_num;
6419
6420 /* The current table to which we should emit line number information
6421    for the current function.  This will be set up at the beginning of
6422    assembly for the function.  */
6423 static dw_line_info_table *cur_line_info_table;
6424
6425 /* The two default tables of line number info.  */
6426 static GTY(()) dw_line_info_table *text_section_line_info;
6427 static GTY(()) dw_line_info_table *cold_text_section_line_info;
6428
6429 /* The set of all non-default tables of line number info.  */
6430 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
6431
6432 /* A flag to tell pubnames/types export if there is an info section to
6433    refer to.  */
6434 static bool info_section_emitted;
6435
6436 /* A pointer to the base of a table that contains a list of publicly
6437    accessible names.  */
6438 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6439
6440 /* A pointer to the base of a table that contains a list of publicly
6441    accessible types.  */
6442 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6443
6444 /* A pointer to the base of a table that contains a list of macro
6445    defines/undefines (and file start/end markers).  */
6446 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6447
6448 /* Array of dies for which we should generate .debug_ranges info.  */
6449 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6450
6451 /* Number of elements currently allocated for ranges_table.  */
6452 static GTY(()) unsigned ranges_table_allocated;
6453
6454 /* Number of elements in ranges_table currently in use.  */
6455 static GTY(()) unsigned ranges_table_in_use;
6456
6457 /* Array of pairs of labels referenced in ranges_table.  */
6458 static GTY ((length ("ranges_by_label_allocated")))
6459      dw_ranges_by_label_ref ranges_by_label;
6460
6461 /* Number of elements currently allocated for ranges_by_label.  */
6462 static GTY(()) unsigned ranges_by_label_allocated;
6463
6464 /* Number of elements in ranges_by_label currently in use.  */
6465 static GTY(()) unsigned ranges_by_label_in_use;
6466
6467 /* Size (in elements) of increments by which we may expand the
6468    ranges_table.  */
6469 #define RANGES_TABLE_INCREMENT 64
6470
6471 /* Whether we have location lists that need outputting */
6472 static GTY(()) bool have_location_lists;
6473
6474 /* Unique label counter.  */
6475 static GTY(()) unsigned int loclabel_num;
6476
6477 /* Unique label counter for point-of-call tables.  */
6478 static GTY(()) unsigned int poc_label_num;
6479
6480 /* Record whether the function being analyzed contains inlined functions.  */
6481 static int current_function_has_inlines;
6482
6483 /* The last file entry emitted by maybe_emit_file().  */
6484 static GTY(()) struct dwarf_file_data * last_emitted_file;
6485
6486 /* Number of internal labels generated by gen_internal_sym().  */
6487 static GTY(()) int label_num;
6488
6489 /* Cached result of previous call to lookup_filename.  */
6490 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6491
6492 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6493
6494 /* Instances of generic types for which we need to generate debug
6495    info that describe their generic parameters and arguments. That
6496    generation needs to happen once all types are properly laid out so
6497    we do it at the end of compilation.  */
6498 static GTY(()) VEC(tree,gc) *generic_type_instances;
6499
6500 /* Offset from the "steady-state frame pointer" to the frame base,
6501    within the current function.  */
6502 static HOST_WIDE_INT frame_pointer_fb_offset;
6503 static bool frame_pointer_fb_offset_valid;
6504
6505 static VEC (dw_die_ref, heap) *base_types;
6506
6507 /* Forward declarations for functions defined in this file.  */
6508
6509 static int is_pseudo_reg (const_rtx);
6510 static tree type_main_variant (tree);
6511 static int is_tagged_type (const_tree);
6512 static const char *dwarf_tag_name (unsigned);
6513 static const char *dwarf_attr_name (unsigned);
6514 static const char *dwarf_form_name (unsigned);
6515 static tree decl_ultimate_origin (const_tree);
6516 static tree decl_class_context (tree);
6517 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6518 static inline enum dw_val_class AT_class (dw_attr_ref);
6519 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6520 static inline unsigned AT_flag (dw_attr_ref);
6521 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6522 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6523 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6524 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6525 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6526                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6527 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6528                                unsigned int, unsigned char *);
6529 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6530 static hashval_t debug_str_do_hash (const void *);
6531 static int debug_str_eq (const void *, const void *);
6532 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6533 static inline const char *AT_string (dw_attr_ref);
6534 static enum dwarf_form AT_string_form (dw_attr_ref);
6535 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6536 static void add_AT_specification (dw_die_ref, dw_die_ref);
6537 static inline dw_die_ref AT_ref (dw_attr_ref);
6538 static inline int AT_ref_external (dw_attr_ref);
6539 static inline void set_AT_ref_external (dw_attr_ref, int);
6540 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6541 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6542 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6543 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6544                              dw_loc_list_ref);
6545 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6546 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6547 static inline rtx AT_addr (dw_attr_ref);
6548 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6549 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6550 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6551 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6552                            unsigned HOST_WIDE_INT);
6553 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6554                                unsigned long);
6555 static inline const char *AT_lbl (dw_attr_ref);
6556 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6557 static const char *get_AT_low_pc (dw_die_ref);
6558 static const char *get_AT_hi_pc (dw_die_ref);
6559 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6560 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6561 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6562 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6563 static bool is_cxx (void);
6564 static bool is_fortran (void);
6565 static bool is_ada (void);
6566 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6567 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6568 static void add_child_die (dw_die_ref, dw_die_ref);
6569 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6570 static dw_die_ref lookup_type_die (tree);
6571 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6572 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6573 static void equate_type_number_to_die (tree, dw_die_ref);
6574 static hashval_t decl_die_table_hash (const void *);
6575 static int decl_die_table_eq (const void *, const void *);
6576 static dw_die_ref lookup_decl_die (tree);
6577 static hashval_t common_block_die_table_hash (const void *);
6578 static int common_block_die_table_eq (const void *, const void *);
6579 static hashval_t decl_loc_table_hash (const void *);
6580 static int decl_loc_table_eq (const void *, const void *);
6581 static var_loc_list *lookup_decl_loc (const_tree);
6582 static void equate_decl_number_to_die (tree, dw_die_ref);
6583 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6584 static void print_spaces (FILE *);
6585 static void print_die (dw_die_ref, FILE *);
6586 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6587 static dw_die_ref pop_compile_unit (dw_die_ref);
6588 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6589 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6590 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6591 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6592 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6593 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6594 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6595                                    struct md5_ctx *, int *);
6596 struct checksum_attributes;
6597 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6598 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6599 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6600 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6601 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6602 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6603 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6604 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6605 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6606 static void compute_section_prefix (dw_die_ref);
6607 static int is_type_die (dw_die_ref);
6608 static int is_comdat_die (dw_die_ref);
6609 static int is_symbol_die (dw_die_ref);
6610 static void assign_symbol_names (dw_die_ref);
6611 static void break_out_includes (dw_die_ref);
6612 static int is_declaration_die (dw_die_ref);
6613 static int should_move_die_to_comdat (dw_die_ref);
6614 static dw_die_ref clone_as_declaration (dw_die_ref);
6615 static dw_die_ref clone_die (dw_die_ref);
6616 static dw_die_ref clone_tree (dw_die_ref);
6617 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6618 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6619 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6620 static dw_die_ref generate_skeleton (dw_die_ref);
6621 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6622                                                          dw_die_ref);
6623 static void break_out_comdat_types (dw_die_ref);
6624 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6625 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6626 static void copy_decls_for_unworthy_types (dw_die_ref);
6627
6628 static hashval_t htab_cu_hash (const void *);
6629 static int htab_cu_eq (const void *, const void *);
6630 static void htab_cu_del (void *);
6631 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6632 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6633 static void add_sibling_attributes (dw_die_ref);
6634 static void build_abbrev_table (dw_die_ref);
6635 static void output_location_lists (dw_die_ref);
6636 static int constant_size (unsigned HOST_WIDE_INT);
6637 static unsigned long size_of_die (dw_die_ref);
6638 static void calc_die_sizes (dw_die_ref);
6639 static void calc_base_type_die_sizes (void);
6640 static void mark_dies (dw_die_ref);
6641 static void unmark_dies (dw_die_ref);
6642 static void unmark_all_dies (dw_die_ref);
6643 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6644 static unsigned long size_of_aranges (void);
6645 static enum dwarf_form value_format (dw_attr_ref);
6646 static void output_value_format (dw_attr_ref);
6647 static void output_abbrev_section (void);
6648 static void output_die_symbol (dw_die_ref);
6649 static void output_die (dw_die_ref);
6650 static void output_compilation_unit_header (void);
6651 static void output_comp_unit (dw_die_ref, int);
6652 static void output_comdat_type_unit (comdat_type_node *);
6653 static const char *dwarf2_name (tree, int);
6654 static void add_pubname (tree, dw_die_ref);
6655 static void add_pubname_string (const char *, dw_die_ref);
6656 static void add_pubtype (tree, dw_die_ref);
6657 static void output_pubnames (VEC (pubname_entry,gc) *);
6658 static void output_aranges (unsigned long);
6659 static unsigned int add_ranges_num (int);
6660 static unsigned int add_ranges (const_tree);
6661 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6662                                   bool *);
6663 static void output_ranges (void);
6664 static dw_line_info_table *new_line_info_table (void);
6665 static void output_line_info (void);
6666 static void output_file_names (void);
6667 static dw_die_ref base_type_die (tree);
6668 static int is_base_type (tree);
6669 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6670 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6671 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6672 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6673 static int type_is_enum (const_tree);
6674 static unsigned int dbx_reg_number (const_rtx);
6675 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6676 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6677 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6678                                                 enum var_init_status);
6679 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6680                                                      enum var_init_status);
6681 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6682                                          enum var_init_status);
6683 static int is_based_loc (const_rtx);
6684 static int resolve_one_addr (rtx *, void *);
6685 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6686                                                enum var_init_status);
6687 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6688                                         enum var_init_status);
6689 static dw_loc_list_ref loc_list_from_tree (tree, int);
6690 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6691 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6692 static tree field_type (const_tree);
6693 static unsigned int simple_type_align_in_bits (const_tree);
6694 static unsigned int simple_decl_align_in_bits (const_tree);
6695 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6696 static HOST_WIDE_INT field_byte_offset (const_tree);
6697 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6698                                          dw_loc_list_ref);
6699 static void add_data_member_location_attribute (dw_die_ref, tree);
6700 static bool add_const_value_attribute (dw_die_ref, rtx);
6701 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6702 static void insert_double (double_int, unsigned char *);
6703 static void insert_float (const_rtx, unsigned char *);
6704 static rtx rtl_for_decl_location (tree);
6705 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
6706                                                    enum dwarf_attribute);
6707 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6708 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6709 static void add_name_attribute (dw_die_ref, const char *);
6710 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
6711 static void add_comp_dir_attribute (dw_die_ref);
6712 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6713 static void add_subscript_info (dw_die_ref, tree, bool);
6714 static void add_byte_size_attribute (dw_die_ref, tree);
6715 static void add_bit_offset_attribute (dw_die_ref, tree);
6716 static void add_bit_size_attribute (dw_die_ref, tree);
6717 static void add_prototyped_attribute (dw_die_ref, tree);
6718 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6719 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6720 static void add_src_coords_attributes (dw_die_ref, tree);
6721 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6722 static void push_decl_scope (tree);
6723 static void pop_decl_scope (void);
6724 static dw_die_ref scope_die_for (tree, dw_die_ref);
6725 static inline int local_scope_p (dw_die_ref);
6726 static inline int class_scope_p (dw_die_ref);
6727 static inline int class_or_namespace_scope_p (dw_die_ref);
6728 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6729 static void add_calling_convention_attribute (dw_die_ref, tree);
6730 static const char *type_tag (const_tree);
6731 static tree member_declared_type (const_tree);
6732 #if 0
6733 static const char *decl_start_label (tree);
6734 #endif
6735 static void gen_array_type_die (tree, dw_die_ref);
6736 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6737 #if 0
6738 static void gen_entry_point_die (tree, dw_die_ref);
6739 #endif
6740 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6741 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6742 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6743 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6744 static void gen_formal_types_die (tree, dw_die_ref);
6745 static void gen_subprogram_die (tree, dw_die_ref);
6746 static void gen_variable_die (tree, tree, dw_die_ref);
6747 static void gen_const_die (tree, dw_die_ref);
6748 static void gen_label_die (tree, dw_die_ref);
6749 static void gen_lexical_block_die (tree, dw_die_ref, int);
6750 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6751 static void gen_field_die (tree, dw_die_ref);
6752 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6753 static dw_die_ref gen_compile_unit_die (const char *);
6754 static void gen_inheritance_die (tree, tree, dw_die_ref);
6755 static void gen_member_die (tree, dw_die_ref);
6756 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6757                                                 enum debug_info_usage);
6758 static void gen_subroutine_type_die (tree, dw_die_ref);
6759 static void gen_typedef_die (tree, dw_die_ref);
6760 static void gen_type_die (tree, dw_die_ref);
6761 static void gen_block_die (tree, dw_die_ref, int);
6762 static void decls_for_scope (tree, dw_die_ref, int);
6763 static inline int is_redundant_typedef (const_tree);
6764 static bool is_naming_typedef_decl (const_tree);
6765 static inline dw_die_ref get_context_die (tree);
6766 static void gen_namespace_die (tree, dw_die_ref);
6767 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6768 static dw_die_ref force_decl_die (tree);
6769 static dw_die_ref force_type_die (tree);
6770 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6771 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6772 static struct dwarf_file_data * lookup_filename (const char *);
6773 static void retry_incomplete_types (void);
6774 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6775 static void gen_generic_params_dies (tree);
6776 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6777 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6778 static void splice_child_die (dw_die_ref, dw_die_ref);
6779 static int file_info_cmp (const void *, const void *);
6780 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6781                                      const char *, const char *);
6782 static void output_loc_list (dw_loc_list_ref);
6783 static char *gen_internal_sym (const char *);
6784
6785 static void prune_unmark_dies (dw_die_ref);
6786 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6787 static void prune_unused_types_mark (dw_die_ref, int);
6788 static void prune_unused_types_walk (dw_die_ref);
6789 static void prune_unused_types_walk_attribs (dw_die_ref);
6790 static void prune_unused_types_prune (dw_die_ref);
6791 static void prune_unused_types (void);
6792 static int maybe_emit_file (struct dwarf_file_data *fd);
6793 static inline const char *AT_vms_delta1 (dw_attr_ref);
6794 static inline const char *AT_vms_delta2 (dw_attr_ref);
6795 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6796                                      const char *, const char *);
6797 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6798 static void gen_remaining_tmpl_value_param_die_attribute (void);
6799 static bool generic_type_p (tree);
6800 static void schedule_generic_params_dies_gen (tree t);
6801 static void gen_scheduled_generic_parms_dies (void);
6802
6803 /* Section names used to hold DWARF debugging information.  */
6804 #ifndef DEBUG_INFO_SECTION
6805 #define DEBUG_INFO_SECTION      ".debug_info"
6806 #endif
6807 #ifndef DEBUG_ABBREV_SECTION
6808 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6809 #endif
6810 #ifndef DEBUG_ARANGES_SECTION
6811 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6812 #endif
6813 #ifndef DEBUG_MACINFO_SECTION
6814 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6815 #endif
6816 #ifndef DEBUG_LINE_SECTION
6817 #define DEBUG_LINE_SECTION      ".debug_line"
6818 #endif
6819 #ifndef DEBUG_LOC_SECTION
6820 #define DEBUG_LOC_SECTION       ".debug_loc"
6821 #endif
6822 #ifndef DEBUG_PUBNAMES_SECTION
6823 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6824 #endif
6825 #ifndef DEBUG_PUBTYPES_SECTION
6826 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6827 #endif
6828 #ifndef DEBUG_STR_SECTION
6829 #define DEBUG_STR_SECTION       ".debug_str"
6830 #endif
6831 #ifndef DEBUG_RANGES_SECTION
6832 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6833 #endif
6834
6835 /* Standard ELF section names for compiled code and data.  */
6836 #ifndef TEXT_SECTION_NAME
6837 #define TEXT_SECTION_NAME       ".text"
6838 #endif
6839
6840 /* Section flags for .debug_str section.  */
6841 #define DEBUG_STR_SECTION_FLAGS \
6842   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6843    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6844    : SECTION_DEBUG)
6845
6846 /* Labels we insert at beginning sections we can reference instead of
6847    the section names themselves.  */
6848
6849 #ifndef TEXT_SECTION_LABEL
6850 #define TEXT_SECTION_LABEL              "Ltext"
6851 #endif
6852 #ifndef COLD_TEXT_SECTION_LABEL
6853 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6854 #endif
6855 #ifndef DEBUG_LINE_SECTION_LABEL
6856 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6857 #endif
6858 #ifndef DEBUG_INFO_SECTION_LABEL
6859 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6860 #endif
6861 #ifndef DEBUG_ABBREV_SECTION_LABEL
6862 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6863 #endif
6864 #ifndef DEBUG_LOC_SECTION_LABEL
6865 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6866 #endif
6867 #ifndef DEBUG_RANGES_SECTION_LABEL
6868 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6869 #endif
6870 #ifndef DEBUG_MACINFO_SECTION_LABEL
6871 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6872 #endif
6873
6874
6875 /* Definitions of defaults for formats and names of various special
6876    (artificial) labels which may be generated within this file (when the -g
6877    options is used and DWARF2_DEBUGGING_INFO is in effect.
6878    If necessary, these may be overridden from within the tm.h file, but
6879    typically, overriding these defaults is unnecessary.  */
6880
6881 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6882 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6883 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6884 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6885 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6886 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6887 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6888 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6889 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6890 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6891
6892 #ifndef TEXT_END_LABEL
6893 #define TEXT_END_LABEL          "Letext"
6894 #endif
6895 #ifndef COLD_END_LABEL
6896 #define COLD_END_LABEL          "Letext_cold"
6897 #endif
6898 #ifndef BLOCK_BEGIN_LABEL
6899 #define BLOCK_BEGIN_LABEL       "LBB"
6900 #endif
6901 #ifndef BLOCK_END_LABEL
6902 #define BLOCK_END_LABEL         "LBE"
6903 #endif
6904 #ifndef LINE_CODE_LABEL
6905 #define LINE_CODE_LABEL         "LM"
6906 #endif
6907
6908 \f
6909 /* Return the root of the DIE's built for the current compilation unit.  */
6910 static dw_die_ref
6911 comp_unit_die (void)
6912 {
6913   if (!single_comp_unit_die)
6914     single_comp_unit_die = gen_compile_unit_die (NULL);
6915   return single_comp_unit_die;
6916 }
6917
6918 /* We allow a language front-end to designate a function that is to be
6919    called to "demangle" any name before it is put into a DIE.  */
6920
6921 static const char *(*demangle_name_func) (const char *);
6922
6923 void
6924 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6925 {
6926   demangle_name_func = func;
6927 }
6928
6929 /* Test if rtl node points to a pseudo register.  */
6930
6931 static inline int
6932 is_pseudo_reg (const_rtx rtl)
6933 {
6934   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6935           || (GET_CODE (rtl) == SUBREG
6936               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6937 }
6938
6939 /* Return a reference to a type, with its const and volatile qualifiers
6940    removed.  */
6941
6942 static inline tree
6943 type_main_variant (tree type)
6944 {
6945   type = TYPE_MAIN_VARIANT (type);
6946
6947   /* ??? There really should be only one main variant among any group of
6948      variants of a given type (and all of the MAIN_VARIANT values for all
6949      members of the group should point to that one type) but sometimes the C
6950      front-end messes this up for array types, so we work around that bug
6951      here.  */
6952   if (TREE_CODE (type) == ARRAY_TYPE)
6953     while (type != TYPE_MAIN_VARIANT (type))
6954       type = TYPE_MAIN_VARIANT (type);
6955
6956   return type;
6957 }
6958
6959 /* Return nonzero if the given type node represents a tagged type.  */
6960
6961 static inline int
6962 is_tagged_type (const_tree type)
6963 {
6964   enum tree_code code = TREE_CODE (type);
6965
6966   return (code == RECORD_TYPE || code == UNION_TYPE
6967           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6968 }
6969
6970 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6971
6972 static void
6973 get_ref_die_offset_label (char *label, dw_die_ref ref)
6974 {
6975   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6976 }
6977
6978 /* Return die_offset of a DIE reference to a base type.  */
6979
6980 static unsigned long int
6981 get_base_type_offset (dw_die_ref ref)
6982 {
6983   if (ref->die_offset)
6984     return ref->die_offset;
6985   if (comp_unit_die ()->die_abbrev)
6986     {
6987       calc_base_type_die_sizes ();
6988       gcc_assert (ref->die_offset);
6989     }
6990   return ref->die_offset;
6991 }
6992
6993 /* Return die_offset of a DIE reference other than base type.  */
6994
6995 static unsigned long int
6996 get_ref_die_offset (dw_die_ref ref)
6997 {
6998   gcc_assert (ref->die_offset);
6999   return ref->die_offset;
7000 }
7001
7002 /* Convert a DIE tag into its string name.  */
7003
7004 static const char *
7005 dwarf_tag_name (unsigned int tag)
7006 {
7007   switch (tag)
7008     {
7009     case DW_TAG_padding:
7010       return "DW_TAG_padding";
7011     case DW_TAG_array_type:
7012       return "DW_TAG_array_type";
7013     case DW_TAG_class_type:
7014       return "DW_TAG_class_type";
7015     case DW_TAG_entry_point:
7016       return "DW_TAG_entry_point";
7017     case DW_TAG_enumeration_type:
7018       return "DW_TAG_enumeration_type";
7019     case DW_TAG_formal_parameter:
7020       return "DW_TAG_formal_parameter";
7021     case DW_TAG_imported_declaration:
7022       return "DW_TAG_imported_declaration";
7023     case DW_TAG_label:
7024       return "DW_TAG_label";
7025     case DW_TAG_lexical_block:
7026       return "DW_TAG_lexical_block";
7027     case DW_TAG_member:
7028       return "DW_TAG_member";
7029     case DW_TAG_pointer_type:
7030       return "DW_TAG_pointer_type";
7031     case DW_TAG_reference_type:
7032       return "DW_TAG_reference_type";
7033     case DW_TAG_compile_unit:
7034       return "DW_TAG_compile_unit";
7035     case DW_TAG_string_type:
7036       return "DW_TAG_string_type";
7037     case DW_TAG_structure_type:
7038       return "DW_TAG_structure_type";
7039     case DW_TAG_subroutine_type:
7040       return "DW_TAG_subroutine_type";
7041     case DW_TAG_typedef:
7042       return "DW_TAG_typedef";
7043     case DW_TAG_union_type:
7044       return "DW_TAG_union_type";
7045     case DW_TAG_unspecified_parameters:
7046       return "DW_TAG_unspecified_parameters";
7047     case DW_TAG_variant:
7048       return "DW_TAG_variant";
7049     case DW_TAG_common_block:
7050       return "DW_TAG_common_block";
7051     case DW_TAG_common_inclusion:
7052       return "DW_TAG_common_inclusion";
7053     case DW_TAG_inheritance:
7054       return "DW_TAG_inheritance";
7055     case DW_TAG_inlined_subroutine:
7056       return "DW_TAG_inlined_subroutine";
7057     case DW_TAG_module:
7058       return "DW_TAG_module";
7059     case DW_TAG_ptr_to_member_type:
7060       return "DW_TAG_ptr_to_member_type";
7061     case DW_TAG_set_type:
7062       return "DW_TAG_set_type";
7063     case DW_TAG_subrange_type:
7064       return "DW_TAG_subrange_type";
7065     case DW_TAG_with_stmt:
7066       return "DW_TAG_with_stmt";
7067     case DW_TAG_access_declaration:
7068       return "DW_TAG_access_declaration";
7069     case DW_TAG_base_type:
7070       return "DW_TAG_base_type";
7071     case DW_TAG_catch_block:
7072       return "DW_TAG_catch_block";
7073     case DW_TAG_const_type:
7074       return "DW_TAG_const_type";
7075     case DW_TAG_constant:
7076       return "DW_TAG_constant";
7077     case DW_TAG_enumerator:
7078       return "DW_TAG_enumerator";
7079     case DW_TAG_file_type:
7080       return "DW_TAG_file_type";
7081     case DW_TAG_friend:
7082       return "DW_TAG_friend";
7083     case DW_TAG_namelist:
7084       return "DW_TAG_namelist";
7085     case DW_TAG_namelist_item:
7086       return "DW_TAG_namelist_item";
7087     case DW_TAG_packed_type:
7088       return "DW_TAG_packed_type";
7089     case DW_TAG_subprogram:
7090       return "DW_TAG_subprogram";
7091     case DW_TAG_template_type_param:
7092       return "DW_TAG_template_type_param";
7093     case DW_TAG_template_value_param:
7094       return "DW_TAG_template_value_param";
7095     case DW_TAG_thrown_type:
7096       return "DW_TAG_thrown_type";
7097     case DW_TAG_try_block:
7098       return "DW_TAG_try_block";
7099     case DW_TAG_variant_part:
7100       return "DW_TAG_variant_part";
7101     case DW_TAG_variable:
7102       return "DW_TAG_variable";
7103     case DW_TAG_volatile_type:
7104       return "DW_TAG_volatile_type";
7105     case DW_TAG_dwarf_procedure:
7106       return "DW_TAG_dwarf_procedure";
7107     case DW_TAG_restrict_type:
7108       return "DW_TAG_restrict_type";
7109     case DW_TAG_interface_type:
7110       return "DW_TAG_interface_type";
7111     case DW_TAG_namespace:
7112       return "DW_TAG_namespace";
7113     case DW_TAG_imported_module:
7114       return "DW_TAG_imported_module";
7115     case DW_TAG_unspecified_type:
7116       return "DW_TAG_unspecified_type";
7117     case DW_TAG_partial_unit:
7118       return "DW_TAG_partial_unit";
7119     case DW_TAG_imported_unit:
7120       return "DW_TAG_imported_unit";
7121     case DW_TAG_condition:
7122       return "DW_TAG_condition";
7123     case DW_TAG_shared_type:
7124       return "DW_TAG_shared_type";
7125     case DW_TAG_type_unit:
7126       return "DW_TAG_type_unit";
7127     case DW_TAG_rvalue_reference_type:
7128       return "DW_TAG_rvalue_reference_type";
7129     case DW_TAG_template_alias:
7130       return "DW_TAG_template_alias";
7131     case DW_TAG_GNU_template_parameter_pack:
7132       return "DW_TAG_GNU_template_parameter_pack";
7133     case DW_TAG_GNU_formal_parameter_pack:
7134       return "DW_TAG_GNU_formal_parameter_pack";
7135     case DW_TAG_MIPS_loop:
7136       return "DW_TAG_MIPS_loop";
7137     case DW_TAG_format_label:
7138       return "DW_TAG_format_label";
7139     case DW_TAG_function_template:
7140       return "DW_TAG_function_template";
7141     case DW_TAG_class_template:
7142       return "DW_TAG_class_template";
7143     case DW_TAG_GNU_BINCL:
7144       return "DW_TAG_GNU_BINCL";
7145     case DW_TAG_GNU_EINCL:
7146       return "DW_TAG_GNU_EINCL";
7147     case DW_TAG_GNU_template_template_param:
7148       return "DW_TAG_GNU_template_template_param";
7149     case DW_TAG_GNU_call_site:
7150       return "DW_TAG_GNU_call_site";
7151     case DW_TAG_GNU_call_site_parameter:
7152       return "DW_TAG_GNU_call_site_parameter";
7153     default:
7154       return "DW_TAG_<unknown>";
7155     }
7156 }
7157
7158 /* Convert a DWARF attribute code into its string name.  */
7159
7160 static const char *
7161 dwarf_attr_name (unsigned int attr)
7162 {
7163   switch (attr)
7164     {
7165     case DW_AT_sibling:
7166       return "DW_AT_sibling";
7167     case DW_AT_location:
7168       return "DW_AT_location";
7169     case DW_AT_name:
7170       return "DW_AT_name";
7171     case DW_AT_ordering:
7172       return "DW_AT_ordering";
7173     case DW_AT_subscr_data:
7174       return "DW_AT_subscr_data";
7175     case DW_AT_byte_size:
7176       return "DW_AT_byte_size";
7177     case DW_AT_bit_offset:
7178       return "DW_AT_bit_offset";
7179     case DW_AT_bit_size:
7180       return "DW_AT_bit_size";
7181     case DW_AT_element_list:
7182       return "DW_AT_element_list";
7183     case DW_AT_stmt_list:
7184       return "DW_AT_stmt_list";
7185     case DW_AT_low_pc:
7186       return "DW_AT_low_pc";
7187     case DW_AT_high_pc:
7188       return "DW_AT_high_pc";
7189     case DW_AT_language:
7190       return "DW_AT_language";
7191     case DW_AT_member:
7192       return "DW_AT_member";
7193     case DW_AT_discr:
7194       return "DW_AT_discr";
7195     case DW_AT_discr_value:
7196       return "DW_AT_discr_value";
7197     case DW_AT_visibility:
7198       return "DW_AT_visibility";
7199     case DW_AT_import:
7200       return "DW_AT_import";
7201     case DW_AT_string_length:
7202       return "DW_AT_string_length";
7203     case DW_AT_common_reference:
7204       return "DW_AT_common_reference";
7205     case DW_AT_comp_dir:
7206       return "DW_AT_comp_dir";
7207     case DW_AT_const_value:
7208       return "DW_AT_const_value";
7209     case DW_AT_containing_type:
7210       return "DW_AT_containing_type";
7211     case DW_AT_default_value:
7212       return "DW_AT_default_value";
7213     case DW_AT_inline:
7214       return "DW_AT_inline";
7215     case DW_AT_is_optional:
7216       return "DW_AT_is_optional";
7217     case DW_AT_lower_bound:
7218       return "DW_AT_lower_bound";
7219     case DW_AT_producer:
7220       return "DW_AT_producer";
7221     case DW_AT_prototyped:
7222       return "DW_AT_prototyped";
7223     case DW_AT_return_addr:
7224       return "DW_AT_return_addr";
7225     case DW_AT_start_scope:
7226       return "DW_AT_start_scope";
7227     case DW_AT_bit_stride:
7228       return "DW_AT_bit_stride";
7229     case DW_AT_upper_bound:
7230       return "DW_AT_upper_bound";
7231     case DW_AT_abstract_origin:
7232       return "DW_AT_abstract_origin";
7233     case DW_AT_accessibility:
7234       return "DW_AT_accessibility";
7235     case DW_AT_address_class:
7236       return "DW_AT_address_class";
7237     case DW_AT_artificial:
7238       return "DW_AT_artificial";
7239     case DW_AT_base_types:
7240       return "DW_AT_base_types";
7241     case DW_AT_calling_convention:
7242       return "DW_AT_calling_convention";
7243     case DW_AT_count:
7244       return "DW_AT_count";
7245     case DW_AT_data_member_location:
7246       return "DW_AT_data_member_location";
7247     case DW_AT_decl_column:
7248       return "DW_AT_decl_column";
7249     case DW_AT_decl_file:
7250       return "DW_AT_decl_file";
7251     case DW_AT_decl_line:
7252       return "DW_AT_decl_line";
7253     case DW_AT_declaration:
7254       return "DW_AT_declaration";
7255     case DW_AT_discr_list:
7256       return "DW_AT_discr_list";
7257     case DW_AT_encoding:
7258       return "DW_AT_encoding";
7259     case DW_AT_external:
7260       return "DW_AT_external";
7261     case DW_AT_explicit:
7262       return "DW_AT_explicit";
7263     case DW_AT_frame_base:
7264       return "DW_AT_frame_base";
7265     case DW_AT_friend:
7266       return "DW_AT_friend";
7267     case DW_AT_identifier_case:
7268       return "DW_AT_identifier_case";
7269     case DW_AT_macro_info:
7270       return "DW_AT_macro_info";
7271     case DW_AT_namelist_items:
7272       return "DW_AT_namelist_items";
7273     case DW_AT_priority:
7274       return "DW_AT_priority";
7275     case DW_AT_segment:
7276       return "DW_AT_segment";
7277     case DW_AT_specification:
7278       return "DW_AT_specification";
7279     case DW_AT_static_link:
7280       return "DW_AT_static_link";
7281     case DW_AT_type:
7282       return "DW_AT_type";
7283     case DW_AT_use_location:
7284       return "DW_AT_use_location";
7285     case DW_AT_variable_parameter:
7286       return "DW_AT_variable_parameter";
7287     case DW_AT_virtuality:
7288       return "DW_AT_virtuality";
7289     case DW_AT_vtable_elem_location:
7290       return "DW_AT_vtable_elem_location";
7291
7292     case DW_AT_allocated:
7293       return "DW_AT_allocated";
7294     case DW_AT_associated:
7295       return "DW_AT_associated";
7296     case DW_AT_data_location:
7297       return "DW_AT_data_location";
7298     case DW_AT_byte_stride:
7299       return "DW_AT_byte_stride";
7300     case DW_AT_entry_pc:
7301       return "DW_AT_entry_pc";
7302     case DW_AT_use_UTF8:
7303       return "DW_AT_use_UTF8";
7304     case DW_AT_extension:
7305       return "DW_AT_extension";
7306     case DW_AT_ranges:
7307       return "DW_AT_ranges";
7308     case DW_AT_trampoline:
7309       return "DW_AT_trampoline";
7310     case DW_AT_call_column:
7311       return "DW_AT_call_column";
7312     case DW_AT_call_file:
7313       return "DW_AT_call_file";
7314     case DW_AT_call_line:
7315       return "DW_AT_call_line";
7316     case DW_AT_object_pointer:
7317       return "DW_AT_object_pointer";
7318
7319     case DW_AT_signature:
7320       return "DW_AT_signature";
7321     case DW_AT_main_subprogram:
7322       return "DW_AT_main_subprogram";
7323     case DW_AT_data_bit_offset:
7324       return "DW_AT_data_bit_offset";
7325     case DW_AT_const_expr:
7326       return "DW_AT_const_expr";
7327     case DW_AT_enum_class:
7328       return "DW_AT_enum_class";
7329     case DW_AT_linkage_name:
7330       return "DW_AT_linkage_name";
7331
7332     case DW_AT_MIPS_fde:
7333       return "DW_AT_MIPS_fde";
7334     case DW_AT_MIPS_loop_begin:
7335       return "DW_AT_MIPS_loop_begin";
7336     case DW_AT_MIPS_tail_loop_begin:
7337       return "DW_AT_MIPS_tail_loop_begin";
7338     case DW_AT_MIPS_epilog_begin:
7339       return "DW_AT_MIPS_epilog_begin";
7340 #if VMS_DEBUGGING_INFO
7341     case DW_AT_HP_prologue:
7342       return "DW_AT_HP_prologue";
7343 #else
7344     case DW_AT_MIPS_loop_unroll_factor:
7345       return "DW_AT_MIPS_loop_unroll_factor";
7346 #endif
7347     case DW_AT_MIPS_software_pipeline_depth:
7348       return "DW_AT_MIPS_software_pipeline_depth";
7349     case DW_AT_MIPS_linkage_name:
7350       return "DW_AT_MIPS_linkage_name";
7351 #if VMS_DEBUGGING_INFO
7352     case DW_AT_HP_epilogue:
7353       return "DW_AT_HP_epilogue";
7354 #else
7355     case DW_AT_MIPS_stride:
7356       return "DW_AT_MIPS_stride";
7357 #endif
7358     case DW_AT_MIPS_abstract_name:
7359       return "DW_AT_MIPS_abstract_name";
7360     case DW_AT_MIPS_clone_origin:
7361       return "DW_AT_MIPS_clone_origin";
7362     case DW_AT_MIPS_has_inlines:
7363       return "DW_AT_MIPS_has_inlines";
7364
7365     case DW_AT_sf_names:
7366       return "DW_AT_sf_names";
7367     case DW_AT_src_info:
7368       return "DW_AT_src_info";
7369     case DW_AT_mac_info:
7370       return "DW_AT_mac_info";
7371     case DW_AT_src_coords:
7372       return "DW_AT_src_coords";
7373     case DW_AT_body_begin:
7374       return "DW_AT_body_begin";
7375     case DW_AT_body_end:
7376       return "DW_AT_body_end";
7377
7378     case DW_AT_GNU_vector:
7379       return "DW_AT_GNU_vector";
7380     case DW_AT_GNU_guarded_by:
7381       return "DW_AT_GNU_guarded_by";
7382     case DW_AT_GNU_pt_guarded_by:
7383       return "DW_AT_GNU_pt_guarded_by";
7384     case DW_AT_GNU_guarded:
7385       return "DW_AT_GNU_guarded";
7386     case DW_AT_GNU_pt_guarded:
7387       return "DW_AT_GNU_pt_guarded";
7388     case DW_AT_GNU_locks_excluded:
7389       return "DW_AT_GNU_locks_excluded";
7390     case DW_AT_GNU_exclusive_locks_required:
7391       return "DW_AT_GNU_exclusive_locks_required";
7392     case DW_AT_GNU_shared_locks_required:
7393       return "DW_AT_GNU_shared_locks_required";
7394     case DW_AT_GNU_odr_signature:
7395       return "DW_AT_GNU_odr_signature";
7396     case DW_AT_GNU_template_name:
7397       return "DW_AT_GNU_template_name";
7398     case DW_AT_GNU_call_site_value:
7399       return "DW_AT_GNU_call_site_value";
7400     case DW_AT_GNU_call_site_data_value:
7401       return "DW_AT_GNU_call_site_data_value";
7402     case DW_AT_GNU_call_site_target:
7403       return "DW_AT_GNU_call_site_target";
7404     case DW_AT_GNU_call_site_target_clobbered:
7405       return "DW_AT_GNU_call_site_target_clobbered";
7406     case DW_AT_GNU_tail_call:
7407       return "DW_AT_GNU_tail_call";
7408     case DW_AT_GNU_all_tail_call_sites:
7409       return "DW_AT_GNU_all_tail_call_sites";
7410     case DW_AT_GNU_all_call_sites:
7411       return "DW_AT_GNU_all_call_sites";
7412     case DW_AT_GNU_all_source_call_sites:
7413       return "DW_AT_GNU_all_source_call_sites";
7414
7415     case DW_AT_GNAT_descriptive_type:
7416       return "DW_AT_GNAT_descriptive_type";
7417
7418     case DW_AT_VMS_rtnbeg_pd_address:
7419       return "DW_AT_VMS_rtnbeg_pd_address";
7420
7421     default:
7422       return "DW_AT_<unknown>";
7423     }
7424 }
7425
7426 /* Convert a DWARF value form code into its string name.  */
7427
7428 static const char *
7429 dwarf_form_name (unsigned int form)
7430 {
7431   switch (form)
7432     {
7433     case DW_FORM_addr:
7434       return "DW_FORM_addr";
7435     case DW_FORM_block2:
7436       return "DW_FORM_block2";
7437     case DW_FORM_block4:
7438       return "DW_FORM_block4";
7439     case DW_FORM_data2:
7440       return "DW_FORM_data2";
7441     case DW_FORM_data4:
7442       return "DW_FORM_data4";
7443     case DW_FORM_data8:
7444       return "DW_FORM_data8";
7445     case DW_FORM_string:
7446       return "DW_FORM_string";
7447     case DW_FORM_block:
7448       return "DW_FORM_block";
7449     case DW_FORM_block1:
7450       return "DW_FORM_block1";
7451     case DW_FORM_data1:
7452       return "DW_FORM_data1";
7453     case DW_FORM_flag:
7454       return "DW_FORM_flag";
7455     case DW_FORM_sdata:
7456       return "DW_FORM_sdata";
7457     case DW_FORM_strp:
7458       return "DW_FORM_strp";
7459     case DW_FORM_udata:
7460       return "DW_FORM_udata";
7461     case DW_FORM_ref_addr:
7462       return "DW_FORM_ref_addr";
7463     case DW_FORM_ref1:
7464       return "DW_FORM_ref1";
7465     case DW_FORM_ref2:
7466       return "DW_FORM_ref2";
7467     case DW_FORM_ref4:
7468       return "DW_FORM_ref4";
7469     case DW_FORM_ref8:
7470       return "DW_FORM_ref8";
7471     case DW_FORM_ref_udata:
7472       return "DW_FORM_ref_udata";
7473     case DW_FORM_indirect:
7474       return "DW_FORM_indirect";
7475     case DW_FORM_sec_offset:
7476       return "DW_FORM_sec_offset";
7477     case DW_FORM_exprloc:
7478       return "DW_FORM_exprloc";
7479     case DW_FORM_flag_present:
7480       return "DW_FORM_flag_present";
7481     case DW_FORM_ref_sig8:
7482       return "DW_FORM_ref_sig8";
7483     default:
7484       return "DW_FORM_<unknown>";
7485     }
7486 }
7487 \f
7488 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7489    instance of an inlined instance of a decl which is local to an inline
7490    function, so we have to trace all of the way back through the origin chain
7491    to find out what sort of node actually served as the original seed for the
7492    given block.  */
7493
7494 static tree
7495 decl_ultimate_origin (const_tree decl)
7496 {
7497   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7498     return NULL_TREE;
7499
7500   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7501      nodes in the function to point to themselves; ignore that if
7502      we're trying to output the abstract instance of this function.  */
7503   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7504     return NULL_TREE;
7505
7506   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7507      most distant ancestor, this should never happen.  */
7508   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7509
7510   return DECL_ABSTRACT_ORIGIN (decl);
7511 }
7512
7513 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7514    of a virtual function may refer to a base class, so we check the 'this'
7515    parameter.  */
7516
7517 static tree
7518 decl_class_context (tree decl)
7519 {
7520   tree context = NULL_TREE;
7521
7522   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7523     context = DECL_CONTEXT (decl);
7524   else
7525     context = TYPE_MAIN_VARIANT
7526       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7527
7528   if (context && !TYPE_P (context))
7529     context = NULL_TREE;
7530
7531   return context;
7532 }
7533 \f
7534 /* Add an attribute/value pair to a DIE.  */
7535
7536 static inline void
7537 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7538 {
7539   /* Maybe this should be an assert?  */
7540   if (die == NULL)
7541     return;
7542
7543   if (die->die_attr == NULL)
7544     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7545   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7546 }
7547
7548 static inline enum dw_val_class
7549 AT_class (dw_attr_ref a)
7550 {
7551   return a->dw_attr_val.val_class;
7552 }
7553
7554 /* Add a flag value attribute to a DIE.  */
7555
7556 static inline void
7557 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7558 {
7559   dw_attr_node attr;
7560
7561   attr.dw_attr = attr_kind;
7562   attr.dw_attr_val.val_class = dw_val_class_flag;
7563   attr.dw_attr_val.v.val_flag = flag;
7564   add_dwarf_attr (die, &attr);
7565 }
7566
7567 static inline unsigned
7568 AT_flag (dw_attr_ref a)
7569 {
7570   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7571   return a->dw_attr_val.v.val_flag;
7572 }
7573
7574 /* Add a signed integer attribute value to a DIE.  */
7575
7576 static inline void
7577 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7578 {
7579   dw_attr_node attr;
7580
7581   attr.dw_attr = attr_kind;
7582   attr.dw_attr_val.val_class = dw_val_class_const;
7583   attr.dw_attr_val.v.val_int = int_val;
7584   add_dwarf_attr (die, &attr);
7585 }
7586
7587 static inline HOST_WIDE_INT
7588 AT_int (dw_attr_ref a)
7589 {
7590   gcc_assert (a && AT_class (a) == dw_val_class_const);
7591   return a->dw_attr_val.v.val_int;
7592 }
7593
7594 /* Add an unsigned integer attribute value to a DIE.  */
7595
7596 static inline void
7597 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7598                  unsigned HOST_WIDE_INT unsigned_val)
7599 {
7600   dw_attr_node attr;
7601
7602   attr.dw_attr = attr_kind;
7603   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7604   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7605   add_dwarf_attr (die, &attr);
7606 }
7607
7608 static inline unsigned HOST_WIDE_INT
7609 AT_unsigned (dw_attr_ref a)
7610 {
7611   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7612   return a->dw_attr_val.v.val_unsigned;
7613 }
7614
7615 /* Add an unsigned double integer attribute value to a DIE.  */
7616
7617 static inline void
7618 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7619                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7620 {
7621   dw_attr_node attr;
7622
7623   attr.dw_attr = attr_kind;
7624   attr.dw_attr_val.val_class = dw_val_class_const_double;
7625   attr.dw_attr_val.v.val_double.high = high;
7626   attr.dw_attr_val.v.val_double.low = low;
7627   add_dwarf_attr (die, &attr);
7628 }
7629
7630 /* Add a floating point attribute value to a DIE and return it.  */
7631
7632 static inline void
7633 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7634             unsigned int length, unsigned int elt_size, unsigned char *array)
7635 {
7636   dw_attr_node attr;
7637
7638   attr.dw_attr = attr_kind;
7639   attr.dw_attr_val.val_class = dw_val_class_vec;
7640   attr.dw_attr_val.v.val_vec.length = length;
7641   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7642   attr.dw_attr_val.v.val_vec.array = array;
7643   add_dwarf_attr (die, &attr);
7644 }
7645
7646 /* Add an 8-byte data attribute value to a DIE.  */
7647
7648 static inline void
7649 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7650               unsigned char data8[8])
7651 {
7652   dw_attr_node attr;
7653
7654   attr.dw_attr = attr_kind;
7655   attr.dw_attr_val.val_class = dw_val_class_data8;
7656   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7657   add_dwarf_attr (die, &attr);
7658 }
7659
7660 /* Hash and equality functions for debug_str_hash.  */
7661
7662 static hashval_t
7663 debug_str_do_hash (const void *x)
7664 {
7665   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7666 }
7667
7668 static int
7669 debug_str_eq (const void *x1, const void *x2)
7670 {
7671   return strcmp ((((const struct indirect_string_node *)x1)->str),
7672                  (const char *)x2) == 0;
7673 }
7674
7675 /* Add STR to the indirect string hash table.  */
7676
7677 static struct indirect_string_node *
7678 find_AT_string (const char *str)
7679 {
7680   struct indirect_string_node *node;
7681   void **slot;
7682
7683   if (! debug_str_hash)
7684     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7685                                       debug_str_eq, NULL);
7686
7687   slot = htab_find_slot_with_hash (debug_str_hash, str,
7688                                    htab_hash_string (str), INSERT);
7689   if (*slot == NULL)
7690     {
7691       node = ggc_alloc_cleared_indirect_string_node ();
7692       node->str = ggc_strdup (str);
7693       *slot = node;
7694     }
7695   else
7696     node = (struct indirect_string_node *) *slot;
7697
7698   node->refcount++;
7699   return node;
7700 }
7701
7702 /* Add a string attribute value to a DIE.  */
7703
7704 static inline void
7705 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7706 {
7707   dw_attr_node attr;
7708   struct indirect_string_node *node;
7709
7710   node = find_AT_string (str);
7711
7712   attr.dw_attr = attr_kind;
7713   attr.dw_attr_val.val_class = dw_val_class_str;
7714   attr.dw_attr_val.v.val_str = node;
7715   add_dwarf_attr (die, &attr);
7716 }
7717
7718 static inline const char *
7719 AT_string (dw_attr_ref a)
7720 {
7721   gcc_assert (a && AT_class (a) == dw_val_class_str);
7722   return a->dw_attr_val.v.val_str->str;
7723 }
7724
7725 /* Find out whether a string should be output inline in DIE
7726    or out-of-line in .debug_str section.  */
7727
7728 static enum dwarf_form
7729 AT_string_form (dw_attr_ref a)
7730 {
7731   struct indirect_string_node *node;
7732   unsigned int len;
7733   char label[32];
7734
7735   gcc_assert (a && AT_class (a) == dw_val_class_str);
7736
7737   node = a->dw_attr_val.v.val_str;
7738   if (node->form)
7739     return node->form;
7740
7741   len = strlen (node->str) + 1;
7742
7743   /* If the string is shorter or equal to the size of the reference, it is
7744      always better to put it inline.  */
7745   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7746     return node->form = DW_FORM_string;
7747
7748   /* If we cannot expect the linker to merge strings in .debug_str
7749      section, only put it into .debug_str if it is worth even in this
7750      single module.  */
7751   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7752       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7753       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7754     return node->form = DW_FORM_string;
7755
7756   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7757   ++dw2_string_counter;
7758   node->label = xstrdup (label);
7759
7760   return node->form = DW_FORM_strp;
7761 }
7762
7763 /* Add a DIE reference attribute value to a DIE.  */
7764
7765 static inline void
7766 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7767 {
7768   dw_attr_node attr;
7769
7770 #ifdef ENABLE_CHECKING
7771   gcc_assert (targ_die != NULL);
7772 #else
7773   /* With LTO we can end up trying to reference something we didn't create
7774      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7775   if (targ_die == NULL)
7776     return;
7777 #endif
7778
7779   attr.dw_attr = attr_kind;
7780   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7781   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7782   attr.dw_attr_val.v.val_die_ref.external = 0;
7783   add_dwarf_attr (die, &attr);
7784 }
7785
7786 /* Add an AT_specification attribute to a DIE, and also make the back
7787    pointer from the specification to the definition.  */
7788
7789 static inline void
7790 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7791 {
7792   add_AT_die_ref (die, DW_AT_specification, targ_die);
7793   gcc_assert (!targ_die->die_definition);
7794   targ_die->die_definition = die;
7795 }
7796
7797 static inline dw_die_ref
7798 AT_ref (dw_attr_ref a)
7799 {
7800   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7801   return a->dw_attr_val.v.val_die_ref.die;
7802 }
7803
7804 static inline int
7805 AT_ref_external (dw_attr_ref a)
7806 {
7807   if (a && AT_class (a) == dw_val_class_die_ref)
7808     return a->dw_attr_val.v.val_die_ref.external;
7809
7810   return 0;
7811 }
7812
7813 static inline void
7814 set_AT_ref_external (dw_attr_ref a, int i)
7815 {
7816   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7817   a->dw_attr_val.v.val_die_ref.external = i;
7818 }
7819
7820 /* Add an FDE reference attribute value to a DIE.  */
7821
7822 static inline void
7823 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7824 {
7825   dw_attr_node attr;
7826
7827   attr.dw_attr = attr_kind;
7828   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7829   attr.dw_attr_val.v.val_fde_index = targ_fde;
7830   add_dwarf_attr (die, &attr);
7831 }
7832
7833 /* Add a location description attribute value to a DIE.  */
7834
7835 static inline void
7836 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7837 {
7838   dw_attr_node attr;
7839
7840   attr.dw_attr = attr_kind;
7841   attr.dw_attr_val.val_class = dw_val_class_loc;
7842   attr.dw_attr_val.v.val_loc = loc;
7843   add_dwarf_attr (die, &attr);
7844 }
7845
7846 static inline dw_loc_descr_ref
7847 AT_loc (dw_attr_ref a)
7848 {
7849   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7850   return a->dw_attr_val.v.val_loc;
7851 }
7852
7853 static inline void
7854 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7855 {
7856   dw_attr_node attr;
7857
7858   attr.dw_attr = attr_kind;
7859   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7860   attr.dw_attr_val.v.val_loc_list = loc_list;
7861   add_dwarf_attr (die, &attr);
7862   have_location_lists = true;
7863 }
7864
7865 static inline dw_loc_list_ref
7866 AT_loc_list (dw_attr_ref a)
7867 {
7868   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7869   return a->dw_attr_val.v.val_loc_list;
7870 }
7871
7872 static inline dw_loc_list_ref *
7873 AT_loc_list_ptr (dw_attr_ref a)
7874 {
7875   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7876   return &a->dw_attr_val.v.val_loc_list;
7877 }
7878
7879 /* Add an address constant attribute value to a DIE.  */
7880
7881 static inline void
7882 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7883 {
7884   dw_attr_node attr;
7885
7886   attr.dw_attr = attr_kind;
7887   attr.dw_attr_val.val_class = dw_val_class_addr;
7888   attr.dw_attr_val.v.val_addr = addr;
7889   add_dwarf_attr (die, &attr);
7890 }
7891
7892 /* Get the RTX from to an address DIE attribute.  */
7893
7894 static inline rtx
7895 AT_addr (dw_attr_ref a)
7896 {
7897   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7898   return a->dw_attr_val.v.val_addr;
7899 }
7900
7901 /* Add a file attribute value to a DIE.  */
7902
7903 static inline void
7904 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7905              struct dwarf_file_data *fd)
7906 {
7907   dw_attr_node attr;
7908
7909   attr.dw_attr = attr_kind;
7910   attr.dw_attr_val.val_class = dw_val_class_file;
7911   attr.dw_attr_val.v.val_file = fd;
7912   add_dwarf_attr (die, &attr);
7913 }
7914
7915 /* Get the dwarf_file_data from a file DIE attribute.  */
7916
7917 static inline struct dwarf_file_data *
7918 AT_file (dw_attr_ref a)
7919 {
7920   gcc_assert (a && AT_class (a) == dw_val_class_file);
7921   return a->dw_attr_val.v.val_file;
7922 }
7923
7924 /* Add a vms delta attribute value to a DIE.  */
7925
7926 static inline void
7927 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7928                   const char *lbl1, const char *lbl2)
7929 {
7930   dw_attr_node attr;
7931
7932   attr.dw_attr = attr_kind;
7933   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7934   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7935   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7936   add_dwarf_attr (die, &attr);
7937 }
7938
7939 /* Add a label identifier attribute value to a DIE.  */
7940
7941 static inline void
7942 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7943 {
7944   dw_attr_node attr;
7945
7946   attr.dw_attr = attr_kind;
7947   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7948   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7949   add_dwarf_attr (die, &attr);
7950 }
7951
7952 /* Add a section offset attribute value to a DIE, an offset into the
7953    debug_line section.  */
7954
7955 static inline void
7956 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7957                 const char *label)
7958 {
7959   dw_attr_node attr;
7960
7961   attr.dw_attr = attr_kind;
7962   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7963   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7964   add_dwarf_attr (die, &attr);
7965 }
7966
7967 /* Add a section offset attribute value to a DIE, an offset into the
7968    debug_macinfo section.  */
7969
7970 static inline void
7971 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7972                const char *label)
7973 {
7974   dw_attr_node attr;
7975
7976   attr.dw_attr = attr_kind;
7977   attr.dw_attr_val.val_class = dw_val_class_macptr;
7978   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7979   add_dwarf_attr (die, &attr);
7980 }
7981
7982 /* Add an offset attribute value to a DIE.  */
7983
7984 static inline void
7985 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7986                unsigned HOST_WIDE_INT offset)
7987 {
7988   dw_attr_node attr;
7989
7990   attr.dw_attr = attr_kind;
7991   attr.dw_attr_val.val_class = dw_val_class_offset;
7992   attr.dw_attr_val.v.val_offset = offset;
7993   add_dwarf_attr (die, &attr);
7994 }
7995
7996 /* Add an range_list attribute value to a DIE.  */
7997
7998 static void
7999 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
8000                    long unsigned int offset)
8001 {
8002   dw_attr_node attr;
8003
8004   attr.dw_attr = attr_kind;
8005   attr.dw_attr_val.val_class = dw_val_class_range_list;
8006   attr.dw_attr_val.v.val_offset = offset;
8007   add_dwarf_attr (die, &attr);
8008 }
8009
8010 /* Return the start label of a delta attribute.  */
8011
8012 static inline const char *
8013 AT_vms_delta1 (dw_attr_ref a)
8014 {
8015   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
8016   return a->dw_attr_val.v.val_vms_delta.lbl1;
8017 }
8018
8019 /* Return the end label of a delta attribute.  */
8020
8021 static inline const char *
8022 AT_vms_delta2 (dw_attr_ref a)
8023 {
8024   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
8025   return a->dw_attr_val.v.val_vms_delta.lbl2;
8026 }
8027
8028 static inline const char *
8029 AT_lbl (dw_attr_ref a)
8030 {
8031   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
8032                     || AT_class (a) == dw_val_class_lineptr
8033                     || AT_class (a) == dw_val_class_macptr));
8034   return a->dw_attr_val.v.val_lbl_id;
8035 }
8036
8037 /* Get the attribute of type attr_kind.  */
8038
8039 static dw_attr_ref
8040 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8041 {
8042   dw_attr_ref a;
8043   unsigned ix;
8044   dw_die_ref spec = NULL;
8045
8046   if (! die)
8047     return NULL;
8048
8049   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8050     if (a->dw_attr == attr_kind)
8051       return a;
8052     else if (a->dw_attr == DW_AT_specification
8053              || a->dw_attr == DW_AT_abstract_origin)
8054       spec = AT_ref (a);
8055
8056   if (spec)
8057     return get_AT (spec, attr_kind);
8058
8059   return NULL;
8060 }
8061
8062 /* Return the "low pc" attribute value, typically associated with a subprogram
8063    DIE.  Return null if the "low pc" attribute is either not present, or if it
8064    cannot be represented as an assembler label identifier.  */
8065
8066 static inline const char *
8067 get_AT_low_pc (dw_die_ref die)
8068 {
8069   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
8070
8071   return a ? AT_lbl (a) : NULL;
8072 }
8073
8074 /* Return the "high pc" attribute value, typically associated with a subprogram
8075    DIE.  Return null if the "high pc" attribute is either not present, or if it
8076    cannot be represented as an assembler label identifier.  */
8077
8078 static inline const char *
8079 get_AT_hi_pc (dw_die_ref die)
8080 {
8081   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
8082
8083   return a ? AT_lbl (a) : NULL;
8084 }
8085
8086 /* Return the value of the string attribute designated by ATTR_KIND, or
8087    NULL if it is not present.  */
8088
8089 static inline const char *
8090 get_AT_string (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_string (a) : NULL;
8095 }
8096
8097 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
8098    if it is not present.  */
8099
8100 static inline int
8101 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
8102 {
8103   dw_attr_ref a = get_AT (die, attr_kind);
8104
8105   return a ? AT_flag (a) : 0;
8106 }
8107
8108 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
8109    if it is not present.  */
8110
8111 static inline unsigned
8112 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
8113 {
8114   dw_attr_ref a = get_AT (die, attr_kind);
8115
8116   return a ? AT_unsigned (a) : 0;
8117 }
8118
8119 static inline dw_die_ref
8120 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
8121 {
8122   dw_attr_ref a = get_AT (die, attr_kind);
8123
8124   return a ? AT_ref (a) : NULL;
8125 }
8126
8127 static inline struct dwarf_file_data *
8128 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
8129 {
8130   dw_attr_ref a = get_AT (die, attr_kind);
8131
8132   return a ? AT_file (a) : NULL;
8133 }
8134
8135 /* Return TRUE if the language is C++.  */
8136
8137 static inline bool
8138 is_cxx (void)
8139 {
8140   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8141
8142   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
8143 }
8144
8145 /* Return TRUE if the language is Fortran.  */
8146
8147 static inline bool
8148 is_fortran (void)
8149 {
8150   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8151
8152   return (lang == DW_LANG_Fortran77
8153           || lang == DW_LANG_Fortran90
8154           || lang == DW_LANG_Fortran95);
8155 }
8156
8157 /* Return TRUE if the language is Ada.  */
8158
8159 static inline bool
8160 is_ada (void)
8161 {
8162   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8163
8164   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
8165 }
8166
8167 /* Remove the specified attribute if present.  */
8168
8169 static void
8170 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8171 {
8172   dw_attr_ref a;
8173   unsigned ix;
8174
8175   if (! die)
8176     return;
8177
8178   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8179     if (a->dw_attr == attr_kind)
8180       {
8181         if (AT_class (a) == dw_val_class_str)
8182           if (a->dw_attr_val.v.val_str->refcount)
8183             a->dw_attr_val.v.val_str->refcount--;
8184
8185         /* VEC_ordered_remove should help reduce the number of abbrevs
8186            that are needed.  */
8187         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
8188         return;
8189       }
8190 }
8191
8192 /* Remove CHILD from its parent.  PREV must have the property that
8193    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
8194
8195 static void
8196 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
8197 {
8198   gcc_assert (child->die_parent == prev->die_parent);
8199   gcc_assert (prev->die_sib == child);
8200   if (prev == child)
8201     {
8202       gcc_assert (child->die_parent->die_child == child);
8203       prev = NULL;
8204     }
8205   else
8206     prev->die_sib = child->die_sib;
8207   if (child->die_parent->die_child == child)
8208     child->die_parent->die_child = prev;
8209 }
8210
8211 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
8212    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
8213
8214 static void
8215 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
8216 {
8217   dw_die_ref parent = old_child->die_parent;
8218
8219   gcc_assert (parent == prev->die_parent);
8220   gcc_assert (prev->die_sib == old_child);
8221
8222   new_child->die_parent = parent;
8223   if (prev == old_child)
8224     {
8225       gcc_assert (parent->die_child == old_child);
8226       new_child->die_sib = new_child;
8227     }
8228   else
8229     {
8230       prev->die_sib = new_child;
8231       new_child->die_sib = old_child->die_sib;
8232     }
8233   if (old_child->die_parent->die_child == old_child)
8234     old_child->die_parent->die_child = new_child;
8235 }
8236
8237 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8238
8239 static void
8240 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8241 {
8242   dw_die_ref c;
8243   new_parent->die_child = old_parent->die_child;
8244   old_parent->die_child = NULL;
8245   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8246 }
8247
8248 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8249    matches TAG.  */
8250
8251 static void
8252 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8253 {
8254   dw_die_ref c;
8255
8256   c = die->die_child;
8257   if (c) do {
8258     dw_die_ref prev = c;
8259     c = c->die_sib;
8260     while (c->die_tag == tag)
8261       {
8262         remove_child_with_prev (c, prev);
8263         /* Might have removed every child.  */
8264         if (c == c->die_sib)
8265           return;
8266         c = c->die_sib;
8267       }
8268   } while (c != die->die_child);
8269 }
8270
8271 /* Add a CHILD_DIE as the last child of DIE.  */
8272
8273 static void
8274 add_child_die (dw_die_ref die, dw_die_ref child_die)
8275 {
8276   /* FIXME this should probably be an assert.  */
8277   if (! die || ! child_die)
8278     return;
8279   gcc_assert (die != child_die);
8280
8281   child_die->die_parent = die;
8282   if (die->die_child)
8283     {
8284       child_die->die_sib = die->die_child->die_sib;
8285       die->die_child->die_sib = child_die;
8286     }
8287   else
8288     child_die->die_sib = child_die;
8289   die->die_child = child_die;
8290 }
8291
8292 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8293    is the specification, to the end of PARENT's list of children.
8294    This is done by removing and re-adding it.  */
8295
8296 static void
8297 splice_child_die (dw_die_ref parent, dw_die_ref child)
8298 {
8299   dw_die_ref p;
8300
8301   /* We want the declaration DIE from inside the class, not the
8302      specification DIE at toplevel.  */
8303   if (child->die_parent != parent)
8304     {
8305       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8306
8307       if (tmp)
8308         child = tmp;
8309     }
8310
8311   gcc_assert (child->die_parent == parent
8312               || (child->die_parent
8313                   == get_AT_ref (parent, DW_AT_specification)));
8314
8315   for (p = child->die_parent->die_child; ; p = p->die_sib)
8316     if (p->die_sib == child)
8317       {
8318         remove_child_with_prev (child, p);
8319         break;
8320       }
8321
8322   add_child_die (parent, child);
8323 }
8324
8325 /* Return a pointer to a newly created DIE node.  */
8326
8327 static inline dw_die_ref
8328 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8329 {
8330   dw_die_ref die = ggc_alloc_cleared_die_node ();
8331
8332   die->die_tag = tag_value;
8333
8334   if (parent_die != NULL)
8335     add_child_die (parent_die, die);
8336   else
8337     {
8338       limbo_die_node *limbo_node;
8339
8340       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8341       limbo_node->die = die;
8342       limbo_node->created_for = t;
8343       limbo_node->next = limbo_die_list;
8344       limbo_die_list = limbo_node;
8345     }
8346
8347   return die;
8348 }
8349
8350 /* Return the DIE associated with the given type specifier.  */
8351
8352 static inline dw_die_ref
8353 lookup_type_die (tree type)
8354 {
8355   return TYPE_SYMTAB_DIE (type);
8356 }
8357
8358 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8359    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8360    anonymous type instead the one of the naming typedef.  */
8361
8362 static inline dw_die_ref
8363 strip_naming_typedef (tree type, dw_die_ref type_die)
8364 {
8365   if (type
8366       && TREE_CODE (type) == RECORD_TYPE
8367       && type_die
8368       && type_die->die_tag == DW_TAG_typedef
8369       && is_naming_typedef_decl (TYPE_NAME (type)))
8370     type_die = get_AT_ref (type_die, DW_AT_type);
8371   return type_die;
8372 }
8373
8374 /* Like lookup_type_die, but if type is an anonymous type named by a
8375    typedef[1], return the DIE of the anonymous type instead the one of
8376    the naming typedef.  This is because in gen_typedef_die, we did
8377    equate the anonymous struct named by the typedef with the DIE of
8378    the naming typedef. So by default, lookup_type_die on an anonymous
8379    struct yields the DIE of the naming typedef.
8380
8381    [1]: Read the comment of is_naming_typedef_decl to learn about what
8382    a naming typedef is.  */
8383
8384 static inline dw_die_ref
8385 lookup_type_die_strip_naming_typedef (tree type)
8386 {
8387   dw_die_ref die = lookup_type_die (type);
8388   return strip_naming_typedef (type, die);
8389 }
8390
8391 /* Equate a DIE to a given type specifier.  */
8392
8393 static inline void
8394 equate_type_number_to_die (tree type, dw_die_ref type_die)
8395 {
8396   TYPE_SYMTAB_DIE (type) = type_die;
8397 }
8398
8399 /* Returns a hash value for X (which really is a die_struct).  */
8400
8401 static hashval_t
8402 decl_die_table_hash (const void *x)
8403 {
8404   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8405 }
8406
8407 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8408
8409 static int
8410 decl_die_table_eq (const void *x, const void *y)
8411 {
8412   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8413 }
8414
8415 /* Return the DIE associated with a given declaration.  */
8416
8417 static inline dw_die_ref
8418 lookup_decl_die (tree decl)
8419 {
8420   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8421 }
8422
8423 /* Returns a hash value for X (which really is a var_loc_list).  */
8424
8425 static hashval_t
8426 decl_loc_table_hash (const void *x)
8427 {
8428   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8429 }
8430
8431 /* Return nonzero if decl_id of var_loc_list X is the same as
8432    UID of decl *Y.  */
8433
8434 static int
8435 decl_loc_table_eq (const void *x, const void *y)
8436 {
8437   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8438 }
8439
8440 /* Return the var_loc list associated with a given declaration.  */
8441
8442 static inline var_loc_list *
8443 lookup_decl_loc (const_tree decl)
8444 {
8445   if (!decl_loc_table)
8446     return NULL;
8447   return (var_loc_list *)
8448     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8449 }
8450
8451 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
8452
8453 static hashval_t
8454 cached_dw_loc_list_table_hash (const void *x)
8455 {
8456   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8457 }
8458
8459 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8460    UID of decl *Y.  */
8461
8462 static int
8463 cached_dw_loc_list_table_eq (const void *x, const void *y)
8464 {
8465   return (((const cached_dw_loc_list *) x)->decl_id
8466           == DECL_UID ((const_tree) y));
8467 }
8468
8469 /* Equate a DIE to a particular declaration.  */
8470
8471 static void
8472 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8473 {
8474   unsigned int decl_id = DECL_UID (decl);
8475   void **slot;
8476
8477   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8478   *slot = decl_die;
8479   decl_die->decl_id = decl_id;
8480 }
8481
8482 /* Return how many bits covers PIECE EXPR_LIST.  */
8483
8484 static int
8485 decl_piece_bitsize (rtx piece)
8486 {
8487   int ret = (int) GET_MODE (piece);
8488   if (ret)
8489     return ret;
8490   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8491               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8492   return INTVAL (XEXP (XEXP (piece, 0), 0));
8493 }
8494
8495 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8496
8497 static rtx *
8498 decl_piece_varloc_ptr (rtx piece)
8499 {
8500   if ((int) GET_MODE (piece))
8501     return &XEXP (piece, 0);
8502   else
8503     return &XEXP (XEXP (piece, 0), 1);
8504 }
8505
8506 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8507    Next is the chain of following piece nodes.  */
8508
8509 static rtx
8510 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8511 {
8512   if (bitsize <= (int) MAX_MACHINE_MODE)
8513     return alloc_EXPR_LIST (bitsize, loc_note, next);
8514   else
8515     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8516                                                GEN_INT (bitsize),
8517                                                loc_note), next);
8518 }
8519
8520 /* Return rtx that should be stored into loc field for
8521    LOC_NOTE and BITPOS/BITSIZE.  */
8522
8523 static rtx
8524 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8525                       HOST_WIDE_INT bitsize)
8526 {
8527   if (bitsize != -1)
8528     {
8529       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8530       if (bitpos != 0)
8531         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8532     }
8533   return loc_note;
8534 }
8535
8536 /* This function either modifies location piece list *DEST in
8537    place (if SRC and INNER is NULL), or copies location piece list
8538    *SRC to *DEST while modifying it.  Location BITPOS is modified
8539    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8540    not copied and if needed some padding around it is added.
8541    When modifying in place, DEST should point to EXPR_LIST where
8542    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8543    to the start of the whole list and INNER points to the EXPR_LIST
8544    where earlier pieces cover PIECE_BITPOS bits.  */
8545
8546 static void
8547 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8548                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8549                    HOST_WIDE_INT bitsize, rtx loc_note)
8550 {
8551   int diff;
8552   bool copy = inner != NULL;
8553
8554   if (copy)
8555     {
8556       /* First copy all nodes preceeding the current bitpos.  */
8557       while (src != inner)
8558         {
8559           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8560                                    decl_piece_bitsize (*src), NULL_RTX);
8561           dest = &XEXP (*dest, 1);
8562           src = &XEXP (*src, 1);
8563         }
8564     }
8565   /* Add padding if needed.  */
8566   if (bitpos != piece_bitpos)
8567     {
8568       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8569                                copy ? NULL_RTX : *dest);
8570       dest = &XEXP (*dest, 1);
8571     }
8572   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8573     {
8574       gcc_assert (!copy);
8575       /* A piece with correct bitpos and bitsize already exist,
8576          just update the location for it and return.  */
8577       *decl_piece_varloc_ptr (*dest) = loc_note;
8578       return;
8579     }
8580   /* Add the piece that changed.  */
8581   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8582   dest = &XEXP (*dest, 1);
8583   /* Skip over pieces that overlap it.  */
8584   diff = bitpos - piece_bitpos + bitsize;
8585   if (!copy)
8586     src = dest;
8587   while (diff > 0 && *src)
8588     {
8589       rtx piece = *src;
8590       diff -= decl_piece_bitsize (piece);
8591       if (copy)
8592         src = &XEXP (piece, 1);
8593       else
8594         {
8595           *src = XEXP (piece, 1);
8596           free_EXPR_LIST_node (piece);
8597         }
8598     }
8599   /* Add padding if needed.  */
8600   if (diff < 0 && *src)
8601     {
8602       if (!copy)
8603         dest = src;
8604       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8605       dest = &XEXP (*dest, 1);
8606     }
8607   if (!copy)
8608     return;
8609   /* Finally copy all nodes following it.  */
8610   while (*src)
8611     {
8612       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8613                                decl_piece_bitsize (*src), NULL_RTX);
8614       dest = &XEXP (*dest, 1);
8615       src = &XEXP (*src, 1);
8616     }
8617 }
8618
8619 /* Add a variable location node to the linked list for DECL.  */
8620
8621 static struct var_loc_node *
8622 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8623 {
8624   unsigned int decl_id;
8625   var_loc_list *temp;
8626   void **slot;
8627   struct var_loc_node *loc = NULL;
8628   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8629
8630   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8631     {
8632       tree realdecl = DECL_DEBUG_EXPR (decl);
8633       if (realdecl && handled_component_p (realdecl))
8634         {
8635           HOST_WIDE_INT maxsize;
8636           tree innerdecl;
8637           innerdecl
8638             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8639           if (!DECL_P (innerdecl)
8640               || DECL_IGNORED_P (innerdecl)
8641               || TREE_STATIC (innerdecl)
8642               || bitsize <= 0
8643               || bitpos + bitsize > 256
8644               || bitsize != maxsize)
8645             return NULL;
8646           decl = innerdecl;
8647         }
8648     }
8649
8650   decl_id = DECL_UID (decl);
8651   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8652   if (*slot == NULL)
8653     {
8654       temp = ggc_alloc_cleared_var_loc_list ();
8655       temp->decl_id = decl_id;
8656       *slot = temp;
8657     }
8658   else
8659     temp = (var_loc_list *) *slot;
8660
8661   /* For PARM_DECLs try to keep around the original incoming value,
8662      even if that means we'll emit a zero-range .debug_loc entry.  */
8663   if (temp->last
8664       && temp->first == temp->last
8665       && TREE_CODE (decl) == PARM_DECL
8666       && GET_CODE (temp->first->loc) == NOTE
8667       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
8668       && DECL_INCOMING_RTL (decl)
8669       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
8670       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
8671          == GET_CODE (DECL_INCOMING_RTL (decl))
8672       && prev_real_insn (temp->first->loc) == NULL_RTX
8673       && (bitsize != -1
8674           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
8675                            NOTE_VAR_LOCATION_LOC (loc_note))
8676           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
8677               != NOTE_VAR_LOCATION_STATUS (loc_note))))
8678     {
8679       loc = ggc_alloc_cleared_var_loc_node ();
8680       temp->first->next = loc;
8681       temp->last = loc;
8682       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8683     }
8684   else if (temp->last)
8685     {
8686       struct var_loc_node *last = temp->last, *unused = NULL;
8687       rtx *piece_loc = NULL, last_loc_note;
8688       int piece_bitpos = 0;
8689       if (last->next)
8690         {
8691           last = last->next;
8692           gcc_assert (last->next == NULL);
8693         }
8694       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8695         {
8696           piece_loc = &last->loc;
8697           do
8698             {
8699               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8700               if (piece_bitpos + cur_bitsize > bitpos)
8701                 break;
8702               piece_bitpos += cur_bitsize;
8703               piece_loc = &XEXP (*piece_loc, 1);
8704             }
8705           while (*piece_loc);
8706         }
8707       /* TEMP->LAST here is either pointer to the last but one or
8708          last element in the chained list, LAST is pointer to the
8709          last element.  */
8710       if (label && strcmp (last->label, label) == 0)
8711         {
8712           /* For SRA optimized variables if there weren't any real
8713              insns since last note, just modify the last node.  */
8714           if (piece_loc != NULL)
8715             {
8716               adjust_piece_list (piece_loc, NULL, NULL,
8717                                  bitpos, piece_bitpos, bitsize, loc_note);
8718               return NULL;
8719             }
8720           /* If the last note doesn't cover any instructions, remove it.  */
8721           if (temp->last != last)
8722             {
8723               temp->last->next = NULL;
8724               unused = last;
8725               last = temp->last;
8726               gcc_assert (strcmp (last->label, label) != 0);
8727             }
8728           else
8729             {
8730               gcc_assert (temp->first == temp->last
8731                           || (temp->first->next == temp->last
8732                               && TREE_CODE (decl) == PARM_DECL));
8733               memset (temp->last, '\0', sizeof (*temp->last));
8734               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8735               return temp->last;
8736             }
8737         }
8738       if (bitsize == -1 && NOTE_P (last->loc))
8739         last_loc_note = last->loc;
8740       else if (piece_loc != NULL
8741                && *piece_loc != NULL_RTX
8742                && piece_bitpos == bitpos
8743                && decl_piece_bitsize (*piece_loc) == bitsize)
8744         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8745       else
8746         last_loc_note = NULL_RTX;
8747       /* If the current location is the same as the end of the list,
8748          and either both or neither of the locations is uninitialized,
8749          we have nothing to do.  */
8750       if (last_loc_note == NULL_RTX
8751           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8752                             NOTE_VAR_LOCATION_LOC (loc_note)))
8753           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8754                != NOTE_VAR_LOCATION_STATUS (loc_note))
8755               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8756                    == VAR_INIT_STATUS_UNINITIALIZED)
8757                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8758                       == VAR_INIT_STATUS_UNINITIALIZED))))
8759         {
8760           /* Add LOC to the end of list and update LAST.  If the last
8761              element of the list has been removed above, reuse its
8762              memory for the new node, otherwise allocate a new one.  */
8763           if (unused)
8764             {
8765               loc = unused;
8766               memset (loc, '\0', sizeof (*loc));
8767             }
8768           else
8769             loc = ggc_alloc_cleared_var_loc_node ();
8770           if (bitsize == -1 || piece_loc == NULL)
8771             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8772           else
8773             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8774                                bitpos, piece_bitpos, bitsize, loc_note);
8775           last->next = loc;
8776           /* Ensure TEMP->LAST will point either to the new last but one
8777              element of the chain, or to the last element in it.  */
8778           if (last != temp->last)
8779             temp->last = last;
8780         }
8781       else if (unused)
8782         ggc_free (unused);
8783     }
8784   else
8785     {
8786       loc = ggc_alloc_cleared_var_loc_node ();
8787       temp->first = loc;
8788       temp->last = loc;
8789       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8790     }
8791   return loc;
8792 }
8793 \f
8794 /* Keep track of the number of spaces used to indent the
8795    output of the debugging routines that print the structure of
8796    the DIE internal representation.  */
8797 static int print_indent;
8798
8799 /* Indent the line the number of spaces given by print_indent.  */
8800
8801 static inline void
8802 print_spaces (FILE *outfile)
8803 {
8804   fprintf (outfile, "%*s", print_indent, "");
8805 }
8806
8807 /* Print a type signature in hex.  */
8808
8809 static inline void
8810 print_signature (FILE *outfile, char *sig)
8811 {
8812   int i;
8813
8814   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8815     fprintf (outfile, "%02x", sig[i] & 0xff);
8816 }
8817
8818 /* Print the information associated with a given DIE, and its children.
8819    This routine is a debugging aid only.  */
8820
8821 static void
8822 print_die (dw_die_ref die, FILE *outfile)
8823 {
8824   dw_attr_ref a;
8825   dw_die_ref c;
8826   unsigned ix;
8827
8828   print_spaces (outfile);
8829   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8830            die->die_offset, dwarf_tag_name (die->die_tag),
8831            (void*) die);
8832   print_spaces (outfile);
8833   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8834   fprintf (outfile, " offset: %ld", die->die_offset);
8835   fprintf (outfile, " mark: %d\n", die->die_mark);
8836
8837   if (use_debug_types && die->die_id.die_type_node)
8838     {
8839       print_spaces (outfile);
8840       fprintf (outfile, "  signature: ");
8841       print_signature (outfile, die->die_id.die_type_node->signature);
8842       fprintf (outfile, "\n");
8843     }
8844
8845   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8846     {
8847       print_spaces (outfile);
8848       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8849
8850       switch (AT_class (a))
8851         {
8852         case dw_val_class_addr:
8853           fprintf (outfile, "address");
8854           break;
8855         case dw_val_class_offset:
8856           fprintf (outfile, "offset");
8857           break;
8858         case dw_val_class_loc:
8859           fprintf (outfile, "location descriptor");
8860           break;
8861         case dw_val_class_loc_list:
8862           fprintf (outfile, "location list -> label:%s",
8863                    AT_loc_list (a)->ll_symbol);
8864           break;
8865         case dw_val_class_range_list:
8866           fprintf (outfile, "range list");
8867           break;
8868         case dw_val_class_const:
8869           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8870           break;
8871         case dw_val_class_unsigned_const:
8872           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8873           break;
8874         case dw_val_class_const_double:
8875           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8876                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8877                    a->dw_attr_val.v.val_double.high,
8878                    a->dw_attr_val.v.val_double.low);
8879           break;
8880         case dw_val_class_vec:
8881           fprintf (outfile, "floating-point or vector constant");
8882           break;
8883         case dw_val_class_flag:
8884           fprintf (outfile, "%u", AT_flag (a));
8885           break;
8886         case dw_val_class_die_ref:
8887           if (AT_ref (a) != NULL)
8888             {
8889               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
8890                 {
8891                   fprintf (outfile, "die -> signature: ");
8892                   print_signature (outfile,
8893                                    AT_ref (a)->die_id.die_type_node->signature);
8894                 }
8895               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
8896                 fprintf (outfile, "die -> label: %s",
8897                          AT_ref (a)->die_id.die_symbol);
8898               else
8899                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8900               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8901             }
8902           else
8903             fprintf (outfile, "die -> <null>");
8904           break;
8905         case dw_val_class_vms_delta:
8906           fprintf (outfile, "delta: @slotcount(%s-%s)",
8907                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8908           break;
8909         case dw_val_class_lbl_id:
8910         case dw_val_class_lineptr:
8911         case dw_val_class_macptr:
8912           fprintf (outfile, "label: %s", AT_lbl (a));
8913           break;
8914         case dw_val_class_str:
8915           if (AT_string (a) != NULL)
8916             fprintf (outfile, "\"%s\"", AT_string (a));
8917           else
8918             fprintf (outfile, "<null>");
8919           break;
8920         case dw_val_class_file:
8921           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8922                    AT_file (a)->emitted_number);
8923           break;
8924         case dw_val_class_data8:
8925           {
8926             int i;
8927
8928             for (i = 0; i < 8; i++)
8929               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8930             break;
8931           }
8932         default:
8933           break;
8934         }
8935
8936       fprintf (outfile, "\n");
8937     }
8938
8939   if (die->die_child != NULL)
8940     {
8941       print_indent += 4;
8942       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8943       print_indent -= 4;
8944     }
8945   if (print_indent == 0)
8946     fprintf (outfile, "\n");
8947 }
8948
8949 /* Print the information collected for a given DIE.  */
8950
8951 DEBUG_FUNCTION void
8952 debug_dwarf_die (dw_die_ref die)
8953 {
8954   print_die (die, stderr);
8955 }
8956
8957 /* Print all DWARF information collected for the compilation unit.
8958    This routine is a debugging aid only.  */
8959
8960 DEBUG_FUNCTION void
8961 debug_dwarf (void)
8962 {
8963   print_indent = 0;
8964   print_die (comp_unit_die (), stderr);
8965 }
8966 \f
8967 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8968    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8969    DIE that marks the start of the DIEs for this include file.  */
8970
8971 static dw_die_ref
8972 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8973 {
8974   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8975   dw_die_ref new_unit = gen_compile_unit_die (filename);
8976
8977   new_unit->die_sib = old_unit;
8978   return new_unit;
8979 }
8980
8981 /* Close an include-file CU and reopen the enclosing one.  */
8982
8983 static dw_die_ref
8984 pop_compile_unit (dw_die_ref old_unit)
8985 {
8986   dw_die_ref new_unit = old_unit->die_sib;
8987
8988   old_unit->die_sib = NULL;
8989   return new_unit;
8990 }
8991
8992 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8993 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8994
8995 /* Calculate the checksum of a location expression.  */
8996
8997 static inline void
8998 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8999 {
9000   int tem;
9001
9002   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
9003   CHECKSUM (tem);
9004   CHECKSUM (loc->dw_loc_oprnd1);
9005   CHECKSUM (loc->dw_loc_oprnd2);
9006 }
9007
9008 /* Calculate the checksum of an attribute.  */
9009
9010 static void
9011 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
9012 {
9013   dw_loc_descr_ref loc;
9014   rtx r;
9015
9016   CHECKSUM (at->dw_attr);
9017
9018   /* We don't care that this was compiled with a different compiler
9019      snapshot; if the output is the same, that's what matters.  */
9020   if (at->dw_attr == DW_AT_producer)
9021     return;
9022
9023   switch (AT_class (at))
9024     {
9025     case dw_val_class_const:
9026       CHECKSUM (at->dw_attr_val.v.val_int);
9027       break;
9028     case dw_val_class_unsigned_const:
9029       CHECKSUM (at->dw_attr_val.v.val_unsigned);
9030       break;
9031     case dw_val_class_const_double:
9032       CHECKSUM (at->dw_attr_val.v.val_double);
9033       break;
9034     case dw_val_class_vec:
9035       CHECKSUM (at->dw_attr_val.v.val_vec);
9036       break;
9037     case dw_val_class_flag:
9038       CHECKSUM (at->dw_attr_val.v.val_flag);
9039       break;
9040     case dw_val_class_str:
9041       CHECKSUM_STRING (AT_string (at));
9042       break;
9043
9044     case dw_val_class_addr:
9045       r = AT_addr (at);
9046       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9047       CHECKSUM_STRING (XSTR (r, 0));
9048       break;
9049
9050     case dw_val_class_offset:
9051       CHECKSUM (at->dw_attr_val.v.val_offset);
9052       break;
9053
9054     case dw_val_class_loc:
9055       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9056         loc_checksum (loc, ctx);
9057       break;
9058
9059     case dw_val_class_die_ref:
9060       die_checksum (AT_ref (at), ctx, mark);
9061       break;
9062
9063     case dw_val_class_fde_ref:
9064     case dw_val_class_vms_delta:
9065     case dw_val_class_lbl_id:
9066     case dw_val_class_lineptr:
9067     case dw_val_class_macptr:
9068       break;
9069
9070     case dw_val_class_file:
9071       CHECKSUM_STRING (AT_file (at)->filename);
9072       break;
9073
9074     case dw_val_class_data8:
9075       CHECKSUM (at->dw_attr_val.v.val_data8);
9076       break;
9077
9078     default:
9079       break;
9080     }
9081 }
9082
9083 /* Calculate the checksum of a DIE.  */
9084
9085 static void
9086 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9087 {
9088   dw_die_ref c;
9089   dw_attr_ref a;
9090   unsigned ix;
9091
9092   /* To avoid infinite recursion.  */
9093   if (die->die_mark)
9094     {
9095       CHECKSUM (die->die_mark);
9096       return;
9097     }
9098   die->die_mark = ++(*mark);
9099
9100   CHECKSUM (die->die_tag);
9101
9102   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9103     attr_checksum (a, ctx, mark);
9104
9105   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
9106 }
9107
9108 #undef CHECKSUM
9109 #undef CHECKSUM_STRING
9110
9111 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
9112 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
9113 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
9114 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
9115 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
9116 #define CHECKSUM_ATTR(FOO) \
9117   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
9118
9119 /* Calculate the checksum of a number in signed LEB128 format.  */
9120
9121 static void
9122 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
9123 {
9124   unsigned char byte;
9125   bool more;
9126
9127   while (1)
9128     {
9129       byte = (value & 0x7f);
9130       value >>= 7;
9131       more = !((value == 0 && (byte & 0x40) == 0)
9132                 || (value == -1 && (byte & 0x40) != 0));
9133       if (more)
9134         byte |= 0x80;
9135       CHECKSUM (byte);
9136       if (!more)
9137         break;
9138     }
9139 }
9140
9141 /* Calculate the checksum of a number in unsigned LEB128 format.  */
9142
9143 static void
9144 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
9145 {
9146   while (1)
9147     {
9148       unsigned char byte = (value & 0x7f);
9149       value >>= 7;
9150       if (value != 0)
9151         /* More bytes to follow.  */
9152         byte |= 0x80;
9153       CHECKSUM (byte);
9154       if (value == 0)
9155         break;
9156     }
9157 }
9158
9159 /* Checksum the context of the DIE.  This adds the names of any
9160    surrounding namespaces or structures to the checksum.  */
9161
9162 static void
9163 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
9164 {
9165   const char *name;
9166   dw_die_ref spec;
9167   int tag = die->die_tag;
9168
9169   if (tag != DW_TAG_namespace
9170       && tag != DW_TAG_structure_type
9171       && tag != DW_TAG_class_type)
9172     return;
9173
9174   name = get_AT_string (die, DW_AT_name);
9175
9176   spec = get_AT_ref (die, DW_AT_specification);
9177   if (spec != NULL)
9178     die = spec;
9179
9180   if (die->die_parent != NULL)
9181     checksum_die_context (die->die_parent, ctx);
9182
9183   CHECKSUM_ULEB128 ('C');
9184   CHECKSUM_ULEB128 (tag);
9185   if (name != NULL)
9186     CHECKSUM_STRING (name);
9187 }
9188
9189 /* Calculate the checksum of a location expression.  */
9190
9191 static inline void
9192 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
9193 {
9194   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
9195      were emitted as a DW_FORM_sdata instead of a location expression.  */
9196   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
9197     {
9198       CHECKSUM_ULEB128 (DW_FORM_sdata);
9199       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
9200       return;
9201     }
9202
9203   /* Otherwise, just checksum the raw location expression.  */
9204   while (loc != NULL)
9205     {
9206       CHECKSUM_ULEB128 (loc->dw_loc_opc);
9207       CHECKSUM (loc->dw_loc_oprnd1);
9208       CHECKSUM (loc->dw_loc_oprnd2);
9209       loc = loc->dw_loc_next;
9210     }
9211 }
9212
9213 /* Calculate the checksum of an attribute.  */
9214
9215 static void
9216 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
9217                        struct md5_ctx *ctx, int *mark)
9218 {
9219   dw_loc_descr_ref loc;
9220   rtx r;
9221
9222   if (AT_class (at) == dw_val_class_die_ref)
9223     {
9224       dw_die_ref target_die = AT_ref (at);
9225
9226       /* For pointer and reference types, we checksum only the (qualified)
9227          name of the target type (if there is a name).  For friend entries,
9228          we checksum only the (qualified) name of the target type or function.
9229          This allows the checksum to remain the same whether the target type
9230          is complete or not.  */
9231       if ((at->dw_attr == DW_AT_type
9232            && (tag == DW_TAG_pointer_type
9233                || tag == DW_TAG_reference_type
9234                || tag == DW_TAG_rvalue_reference_type
9235                || tag == DW_TAG_ptr_to_member_type))
9236           || (at->dw_attr == DW_AT_friend
9237               && tag == DW_TAG_friend))
9238         {
9239           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9240
9241           if (name_attr != NULL)
9242             {
9243               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9244
9245               if (decl == NULL)
9246                 decl = target_die;
9247               CHECKSUM_ULEB128 ('N');
9248               CHECKSUM_ULEB128 (at->dw_attr);
9249               if (decl->die_parent != NULL)
9250                 checksum_die_context (decl->die_parent, ctx);
9251               CHECKSUM_ULEB128 ('E');
9252               CHECKSUM_STRING (AT_string (name_attr));
9253               return;
9254             }
9255         }
9256
9257       /* For all other references to another DIE, we check to see if the
9258          target DIE has already been visited.  If it has, we emit a
9259          backward reference; if not, we descend recursively.  */
9260       if (target_die->die_mark > 0)
9261         {
9262           CHECKSUM_ULEB128 ('R');
9263           CHECKSUM_ULEB128 (at->dw_attr);
9264           CHECKSUM_ULEB128 (target_die->die_mark);
9265         }
9266       else
9267         {
9268           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9269
9270           if (decl == NULL)
9271             decl = target_die;
9272           target_die->die_mark = ++(*mark);
9273           CHECKSUM_ULEB128 ('T');
9274           CHECKSUM_ULEB128 (at->dw_attr);
9275           if (decl->die_parent != NULL)
9276             checksum_die_context (decl->die_parent, ctx);
9277           die_checksum_ordered (target_die, ctx, mark);
9278         }
9279       return;
9280     }
9281
9282   CHECKSUM_ULEB128 ('A');
9283   CHECKSUM_ULEB128 (at->dw_attr);
9284
9285   switch (AT_class (at))
9286     {
9287     case dw_val_class_const:
9288       CHECKSUM_ULEB128 (DW_FORM_sdata);
9289       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9290       break;
9291
9292     case dw_val_class_unsigned_const:
9293       CHECKSUM_ULEB128 (DW_FORM_sdata);
9294       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9295       break;
9296
9297     case dw_val_class_const_double:
9298       CHECKSUM_ULEB128 (DW_FORM_block);
9299       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9300       CHECKSUM (at->dw_attr_val.v.val_double);
9301       break;
9302
9303     case dw_val_class_vec:
9304       CHECKSUM_ULEB128 (DW_FORM_block);
9305       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9306       CHECKSUM (at->dw_attr_val.v.val_vec);
9307       break;
9308
9309     case dw_val_class_flag:
9310       CHECKSUM_ULEB128 (DW_FORM_flag);
9311       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9312       break;
9313
9314     case dw_val_class_str:
9315       CHECKSUM_ULEB128 (DW_FORM_string);
9316       CHECKSUM_STRING (AT_string (at));
9317       break;
9318
9319     case dw_val_class_addr:
9320       r = AT_addr (at);
9321       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9322       CHECKSUM_ULEB128 (DW_FORM_string);
9323       CHECKSUM_STRING (XSTR (r, 0));
9324       break;
9325
9326     case dw_val_class_offset:
9327       CHECKSUM_ULEB128 (DW_FORM_sdata);
9328       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9329       break;
9330
9331     case dw_val_class_loc:
9332       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9333         loc_checksum_ordered (loc, ctx);
9334       break;
9335
9336     case dw_val_class_fde_ref:
9337     case dw_val_class_lbl_id:
9338     case dw_val_class_lineptr:
9339     case dw_val_class_macptr:
9340       break;
9341
9342     case dw_val_class_file:
9343       CHECKSUM_ULEB128 (DW_FORM_string);
9344       CHECKSUM_STRING (AT_file (at)->filename);
9345       break;
9346
9347     case dw_val_class_data8:
9348       CHECKSUM (at->dw_attr_val.v.val_data8);
9349       break;
9350
9351     default:
9352       break;
9353     }
9354 }
9355
9356 struct checksum_attributes
9357 {
9358   dw_attr_ref at_name;
9359   dw_attr_ref at_type;
9360   dw_attr_ref at_friend;
9361   dw_attr_ref at_accessibility;
9362   dw_attr_ref at_address_class;
9363   dw_attr_ref at_allocated;
9364   dw_attr_ref at_artificial;
9365   dw_attr_ref at_associated;
9366   dw_attr_ref at_binary_scale;
9367   dw_attr_ref at_bit_offset;
9368   dw_attr_ref at_bit_size;
9369   dw_attr_ref at_bit_stride;
9370   dw_attr_ref at_byte_size;
9371   dw_attr_ref at_byte_stride;
9372   dw_attr_ref at_const_value;
9373   dw_attr_ref at_containing_type;
9374   dw_attr_ref at_count;
9375   dw_attr_ref at_data_location;
9376   dw_attr_ref at_data_member_location;
9377   dw_attr_ref at_decimal_scale;
9378   dw_attr_ref at_decimal_sign;
9379   dw_attr_ref at_default_value;
9380   dw_attr_ref at_digit_count;
9381   dw_attr_ref at_discr;
9382   dw_attr_ref at_discr_list;
9383   dw_attr_ref at_discr_value;
9384   dw_attr_ref at_encoding;
9385   dw_attr_ref at_endianity;
9386   dw_attr_ref at_explicit;
9387   dw_attr_ref at_is_optional;
9388   dw_attr_ref at_location;
9389   dw_attr_ref at_lower_bound;
9390   dw_attr_ref at_mutable;
9391   dw_attr_ref at_ordering;
9392   dw_attr_ref at_picture_string;
9393   dw_attr_ref at_prototyped;
9394   dw_attr_ref at_small;
9395   dw_attr_ref at_segment;
9396   dw_attr_ref at_string_length;
9397   dw_attr_ref at_threads_scaled;
9398   dw_attr_ref at_upper_bound;
9399   dw_attr_ref at_use_location;
9400   dw_attr_ref at_use_UTF8;
9401   dw_attr_ref at_variable_parameter;
9402   dw_attr_ref at_virtuality;
9403   dw_attr_ref at_visibility;
9404   dw_attr_ref at_vtable_elem_location;
9405 };
9406
9407 /* Collect the attributes that we will want to use for the checksum.  */
9408
9409 static void
9410 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9411 {
9412   dw_attr_ref a;
9413   unsigned ix;
9414
9415   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9416     {
9417       switch (a->dw_attr)
9418         {
9419         case DW_AT_name:
9420           attrs->at_name = a;
9421           break;
9422         case DW_AT_type:
9423           attrs->at_type = a;
9424           break;
9425         case DW_AT_friend:
9426           attrs->at_friend = a;
9427           break;
9428         case DW_AT_accessibility:
9429           attrs->at_accessibility = a;
9430           break;
9431         case DW_AT_address_class:
9432           attrs->at_address_class = a;
9433           break;
9434         case DW_AT_allocated:
9435           attrs->at_allocated = a;
9436           break;
9437         case DW_AT_artificial:
9438           attrs->at_artificial = a;
9439           break;
9440         case DW_AT_associated:
9441           attrs->at_associated = a;
9442           break;
9443         case DW_AT_binary_scale:
9444           attrs->at_binary_scale = a;
9445           break;
9446         case DW_AT_bit_offset:
9447           attrs->at_bit_offset = a;
9448           break;
9449         case DW_AT_bit_size:
9450           attrs->at_bit_size = a;
9451           break;
9452         case DW_AT_bit_stride:
9453           attrs->at_bit_stride = a;
9454           break;
9455         case DW_AT_byte_size:
9456           attrs->at_byte_size = a;
9457           break;
9458         case DW_AT_byte_stride:
9459           attrs->at_byte_stride = a;
9460           break;
9461         case DW_AT_const_value:
9462           attrs->at_const_value = a;
9463           break;
9464         case DW_AT_containing_type:
9465           attrs->at_containing_type = a;
9466           break;
9467         case DW_AT_count:
9468           attrs->at_count = a;
9469           break;
9470         case DW_AT_data_location:
9471           attrs->at_data_location = a;
9472           break;
9473         case DW_AT_data_member_location:
9474           attrs->at_data_member_location = a;
9475           break;
9476         case DW_AT_decimal_scale:
9477           attrs->at_decimal_scale = a;
9478           break;
9479         case DW_AT_decimal_sign:
9480           attrs->at_decimal_sign = a;
9481           break;
9482         case DW_AT_default_value:
9483           attrs->at_default_value = a;
9484           break;
9485         case DW_AT_digit_count:
9486           attrs->at_digit_count = a;
9487           break;
9488         case DW_AT_discr:
9489           attrs->at_discr = a;
9490           break;
9491         case DW_AT_discr_list:
9492           attrs->at_discr_list = a;
9493           break;
9494         case DW_AT_discr_value:
9495           attrs->at_discr_value = a;
9496           break;
9497         case DW_AT_encoding:
9498           attrs->at_encoding = a;
9499           break;
9500         case DW_AT_endianity:
9501           attrs->at_endianity = a;
9502           break;
9503         case DW_AT_explicit:
9504           attrs->at_explicit = a;
9505           break;
9506         case DW_AT_is_optional:
9507           attrs->at_is_optional = a;
9508           break;
9509         case DW_AT_location:
9510           attrs->at_location = a;
9511           break;
9512         case DW_AT_lower_bound:
9513           attrs->at_lower_bound = a;
9514           break;
9515         case DW_AT_mutable:
9516           attrs->at_mutable = a;
9517           break;
9518         case DW_AT_ordering:
9519           attrs->at_ordering = a;
9520           break;
9521         case DW_AT_picture_string:
9522           attrs->at_picture_string = a;
9523           break;
9524         case DW_AT_prototyped:
9525           attrs->at_prototyped = a;
9526           break;
9527         case DW_AT_small:
9528           attrs->at_small = a;
9529           break;
9530         case DW_AT_segment:
9531           attrs->at_segment = a;
9532           break;
9533         case DW_AT_string_length:
9534           attrs->at_string_length = a;
9535           break;
9536         case DW_AT_threads_scaled:
9537           attrs->at_threads_scaled = a;
9538           break;
9539         case DW_AT_upper_bound:
9540           attrs->at_upper_bound = a;
9541           break;
9542         case DW_AT_use_location:
9543           attrs->at_use_location = a;
9544           break;
9545         case DW_AT_use_UTF8:
9546           attrs->at_use_UTF8 = a;
9547           break;
9548         case DW_AT_variable_parameter:
9549           attrs->at_variable_parameter = a;
9550           break;
9551         case DW_AT_virtuality:
9552           attrs->at_virtuality = a;
9553           break;
9554         case DW_AT_visibility:
9555           attrs->at_visibility = a;
9556           break;
9557         case DW_AT_vtable_elem_location:
9558           attrs->at_vtable_elem_location = a;
9559           break;
9560         default:
9561           break;
9562         }
9563     }
9564 }
9565
9566 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9567
9568 static void
9569 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9570 {
9571   dw_die_ref c;
9572   dw_die_ref decl;
9573   struct checksum_attributes attrs;
9574
9575   CHECKSUM_ULEB128 ('D');
9576   CHECKSUM_ULEB128 (die->die_tag);
9577
9578   memset (&attrs, 0, sizeof (attrs));
9579
9580   decl = get_AT_ref (die, DW_AT_specification);
9581   if (decl != NULL)
9582     collect_checksum_attributes (&attrs, decl);
9583   collect_checksum_attributes (&attrs, die);
9584
9585   CHECKSUM_ATTR (attrs.at_name);
9586   CHECKSUM_ATTR (attrs.at_accessibility);
9587   CHECKSUM_ATTR (attrs.at_address_class);
9588   CHECKSUM_ATTR (attrs.at_allocated);
9589   CHECKSUM_ATTR (attrs.at_artificial);
9590   CHECKSUM_ATTR (attrs.at_associated);
9591   CHECKSUM_ATTR (attrs.at_binary_scale);
9592   CHECKSUM_ATTR (attrs.at_bit_offset);
9593   CHECKSUM_ATTR (attrs.at_bit_size);
9594   CHECKSUM_ATTR (attrs.at_bit_stride);
9595   CHECKSUM_ATTR (attrs.at_byte_size);
9596   CHECKSUM_ATTR (attrs.at_byte_stride);
9597   CHECKSUM_ATTR (attrs.at_const_value);
9598   CHECKSUM_ATTR (attrs.at_containing_type);
9599   CHECKSUM_ATTR (attrs.at_count);
9600   CHECKSUM_ATTR (attrs.at_data_location);
9601   CHECKSUM_ATTR (attrs.at_data_member_location);
9602   CHECKSUM_ATTR (attrs.at_decimal_scale);
9603   CHECKSUM_ATTR (attrs.at_decimal_sign);
9604   CHECKSUM_ATTR (attrs.at_default_value);
9605   CHECKSUM_ATTR (attrs.at_digit_count);
9606   CHECKSUM_ATTR (attrs.at_discr);
9607   CHECKSUM_ATTR (attrs.at_discr_list);
9608   CHECKSUM_ATTR (attrs.at_discr_value);
9609   CHECKSUM_ATTR (attrs.at_encoding);
9610   CHECKSUM_ATTR (attrs.at_endianity);
9611   CHECKSUM_ATTR (attrs.at_explicit);
9612   CHECKSUM_ATTR (attrs.at_is_optional);
9613   CHECKSUM_ATTR (attrs.at_location);
9614   CHECKSUM_ATTR (attrs.at_lower_bound);
9615   CHECKSUM_ATTR (attrs.at_mutable);
9616   CHECKSUM_ATTR (attrs.at_ordering);
9617   CHECKSUM_ATTR (attrs.at_picture_string);
9618   CHECKSUM_ATTR (attrs.at_prototyped);
9619   CHECKSUM_ATTR (attrs.at_small);
9620   CHECKSUM_ATTR (attrs.at_segment);
9621   CHECKSUM_ATTR (attrs.at_string_length);
9622   CHECKSUM_ATTR (attrs.at_threads_scaled);
9623   CHECKSUM_ATTR (attrs.at_upper_bound);
9624   CHECKSUM_ATTR (attrs.at_use_location);
9625   CHECKSUM_ATTR (attrs.at_use_UTF8);
9626   CHECKSUM_ATTR (attrs.at_variable_parameter);
9627   CHECKSUM_ATTR (attrs.at_virtuality);
9628   CHECKSUM_ATTR (attrs.at_visibility);
9629   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9630   CHECKSUM_ATTR (attrs.at_type);
9631   CHECKSUM_ATTR (attrs.at_friend);
9632
9633   /* Checksum the child DIEs, except for nested types and member functions.  */
9634   c = die->die_child;
9635   if (c) do {
9636     dw_attr_ref name_attr;
9637
9638     c = c->die_sib;
9639     name_attr = get_AT (c, DW_AT_name);
9640     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9641         && name_attr != NULL)
9642       {
9643         CHECKSUM_ULEB128 ('S');
9644         CHECKSUM_ULEB128 (c->die_tag);
9645         CHECKSUM_STRING (AT_string (name_attr));
9646       }
9647     else
9648       {
9649         /* Mark this DIE so it gets processed when unmarking.  */
9650         if (c->die_mark == 0)
9651           c->die_mark = -1;
9652         die_checksum_ordered (c, ctx, mark);
9653       }
9654   } while (c != die->die_child);
9655
9656   CHECKSUM_ULEB128 (0);
9657 }
9658
9659 #undef CHECKSUM
9660 #undef CHECKSUM_STRING
9661 #undef CHECKSUM_ATTR
9662 #undef CHECKSUM_LEB128
9663 #undef CHECKSUM_ULEB128
9664
9665 /* Generate the type signature for DIE.  This is computed by generating an
9666    MD5 checksum over the DIE's tag, its relevant attributes, and its
9667    children.  Attributes that are references to other DIEs are processed
9668    by recursion, using the MARK field to prevent infinite recursion.
9669    If the DIE is nested inside a namespace or another type, we also
9670    need to include that context in the signature.  The lower 64 bits
9671    of the resulting MD5 checksum comprise the signature.  */
9672
9673 static void
9674 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9675 {
9676   int mark;
9677   const char *name;
9678   unsigned char checksum[16];
9679   struct md5_ctx ctx;
9680   dw_die_ref decl;
9681
9682   name = get_AT_string (die, DW_AT_name);
9683   decl = get_AT_ref (die, DW_AT_specification);
9684
9685   /* First, compute a signature for just the type name (and its surrounding
9686      context, if any.  This is stored in the type unit DIE for link-time
9687      ODR (one-definition rule) checking.  */
9688
9689   if (is_cxx() && name != NULL)
9690     {
9691       md5_init_ctx (&ctx);
9692
9693       /* Checksum the names of surrounding namespaces and structures.  */
9694       if (decl != NULL && decl->die_parent != NULL)
9695         checksum_die_context (decl->die_parent, &ctx);
9696
9697       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9698       md5_process_bytes (name, strlen (name) + 1, &ctx);
9699       md5_finish_ctx (&ctx, checksum);
9700
9701       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9702     }
9703
9704   /* Next, compute the complete type signature.  */
9705
9706   md5_init_ctx (&ctx);
9707   mark = 1;
9708   die->die_mark = mark;
9709
9710   /* Checksum the names of surrounding namespaces and structures.  */
9711   if (decl != NULL && decl->die_parent != NULL)
9712     checksum_die_context (decl->die_parent, &ctx);
9713
9714   /* Checksum the DIE and its children.  */
9715   die_checksum_ordered (die, &ctx, &mark);
9716   unmark_all_dies (die);
9717   md5_finish_ctx (&ctx, checksum);
9718
9719   /* Store the signature in the type node and link the type DIE and the
9720      type node together.  */
9721   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9722           DWARF_TYPE_SIGNATURE_SIZE);
9723   die->die_id.die_type_node = type_node;
9724   type_node->type_die = die;
9725
9726   /* If the DIE is a specification, link its declaration to the type node
9727      as well.  */
9728   if (decl != NULL)
9729     decl->die_id.die_type_node = type_node;
9730 }
9731
9732 /* Do the location expressions look same?  */
9733 static inline int
9734 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9735 {
9736   return loc1->dw_loc_opc == loc2->dw_loc_opc
9737          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9738          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9739 }
9740
9741 /* Do the values look the same?  */
9742 static int
9743 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9744 {
9745   dw_loc_descr_ref loc1, loc2;
9746   rtx r1, r2;
9747
9748   if (v1->val_class != v2->val_class)
9749     return 0;
9750
9751   switch (v1->val_class)
9752     {
9753     case dw_val_class_const:
9754       return v1->v.val_int == v2->v.val_int;
9755     case dw_val_class_unsigned_const:
9756       return v1->v.val_unsigned == v2->v.val_unsigned;
9757     case dw_val_class_const_double:
9758       return v1->v.val_double.high == v2->v.val_double.high
9759              && v1->v.val_double.low == v2->v.val_double.low;
9760     case dw_val_class_vec:
9761       if (v1->v.val_vec.length != v2->v.val_vec.length
9762           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9763         return 0;
9764       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9765                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9766         return 0;
9767       return 1;
9768     case dw_val_class_flag:
9769       return v1->v.val_flag == v2->v.val_flag;
9770     case dw_val_class_str:
9771       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9772
9773     case dw_val_class_addr:
9774       r1 = v1->v.val_addr;
9775       r2 = v2->v.val_addr;
9776       if (GET_CODE (r1) != GET_CODE (r2))
9777         return 0;
9778       return !rtx_equal_p (r1, r2);
9779
9780     case dw_val_class_offset:
9781       return v1->v.val_offset == v2->v.val_offset;
9782
9783     case dw_val_class_loc:
9784       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9785            loc1 && loc2;
9786            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9787         if (!same_loc_p (loc1, loc2, mark))
9788           return 0;
9789       return !loc1 && !loc2;
9790
9791     case dw_val_class_die_ref:
9792       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9793
9794     case dw_val_class_fde_ref:
9795     case dw_val_class_vms_delta:
9796     case dw_val_class_lbl_id:
9797     case dw_val_class_lineptr:
9798     case dw_val_class_macptr:
9799       return 1;
9800
9801     case dw_val_class_file:
9802       return v1->v.val_file == v2->v.val_file;
9803
9804     case dw_val_class_data8:
9805       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9806
9807     default:
9808       return 1;
9809     }
9810 }
9811
9812 /* Do the attributes look the same?  */
9813
9814 static int
9815 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9816 {
9817   if (at1->dw_attr != at2->dw_attr)
9818     return 0;
9819
9820   /* We don't care that this was compiled with a different compiler
9821      snapshot; if the output is the same, that's what matters. */
9822   if (at1->dw_attr == DW_AT_producer)
9823     return 1;
9824
9825   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9826 }
9827
9828 /* Do the dies look the same?  */
9829
9830 static int
9831 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9832 {
9833   dw_die_ref c1, c2;
9834   dw_attr_ref a1;
9835   unsigned ix;
9836
9837   /* To avoid infinite recursion.  */
9838   if (die1->die_mark)
9839     return die1->die_mark == die2->die_mark;
9840   die1->die_mark = die2->die_mark = ++(*mark);
9841
9842   if (die1->die_tag != die2->die_tag)
9843     return 0;
9844
9845   if (VEC_length (dw_attr_node, die1->die_attr)
9846       != VEC_length (dw_attr_node, die2->die_attr))
9847     return 0;
9848
9849   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9850     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9851       return 0;
9852
9853   c1 = die1->die_child;
9854   c2 = die2->die_child;
9855   if (! c1)
9856     {
9857       if (c2)
9858         return 0;
9859     }
9860   else
9861     for (;;)
9862       {
9863         if (!same_die_p (c1, c2, mark))
9864           return 0;
9865         c1 = c1->die_sib;
9866         c2 = c2->die_sib;
9867         if (c1 == die1->die_child)
9868           {
9869             if (c2 == die2->die_child)
9870               break;
9871             else
9872               return 0;
9873           }
9874     }
9875
9876   return 1;
9877 }
9878
9879 /* Do the dies look the same?  Wrapper around same_die_p.  */
9880
9881 static int
9882 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9883 {
9884   int mark = 0;
9885   int ret = same_die_p (die1, die2, &mark);
9886
9887   unmark_all_dies (die1);
9888   unmark_all_dies (die2);
9889
9890   return ret;
9891 }
9892
9893 /* The prefix to attach to symbols on DIEs in the current comdat debug
9894    info section.  */
9895 static char *comdat_symbol_id;
9896
9897 /* The index of the current symbol within the current comdat CU.  */
9898 static unsigned int comdat_symbol_number;
9899
9900 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9901    children, and set comdat_symbol_id accordingly.  */
9902
9903 static void
9904 compute_section_prefix (dw_die_ref unit_die)
9905 {
9906   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9907   const char *base = die_name ? lbasename (die_name) : "anonymous";
9908   char *name = XALLOCAVEC (char, strlen (base) + 64);
9909   char *p;
9910   int i, mark;
9911   unsigned char checksum[16];
9912   struct md5_ctx ctx;
9913
9914   /* Compute the checksum of the DIE, then append part of it as hex digits to
9915      the name filename of the unit.  */
9916
9917   md5_init_ctx (&ctx);
9918   mark = 0;
9919   die_checksum (unit_die, &ctx, &mark);
9920   unmark_all_dies (unit_die);
9921   md5_finish_ctx (&ctx, checksum);
9922
9923   sprintf (name, "%s.", base);
9924   clean_symbol_name (name);
9925
9926   p = name + strlen (name);
9927   for (i = 0; i < 4; i++)
9928     {
9929       sprintf (p, "%.2x", checksum[i]);
9930       p += 2;
9931     }
9932
9933   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9934   comdat_symbol_number = 0;
9935 }
9936
9937 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9938
9939 static int
9940 is_type_die (dw_die_ref die)
9941 {
9942   switch (die->die_tag)
9943     {
9944     case DW_TAG_array_type:
9945     case DW_TAG_class_type:
9946     case DW_TAG_interface_type:
9947     case DW_TAG_enumeration_type:
9948     case DW_TAG_pointer_type:
9949     case DW_TAG_reference_type:
9950     case DW_TAG_rvalue_reference_type:
9951     case DW_TAG_string_type:
9952     case DW_TAG_structure_type:
9953     case DW_TAG_subroutine_type:
9954     case DW_TAG_union_type:
9955     case DW_TAG_ptr_to_member_type:
9956     case DW_TAG_set_type:
9957     case DW_TAG_subrange_type:
9958     case DW_TAG_base_type:
9959     case DW_TAG_const_type:
9960     case DW_TAG_file_type:
9961     case DW_TAG_packed_type:
9962     case DW_TAG_volatile_type:
9963     case DW_TAG_typedef:
9964       return 1;
9965     default:
9966       return 0;
9967     }
9968 }
9969
9970 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9971    Basically, we want to choose the bits that are likely to be shared between
9972    compilations (types) and leave out the bits that are specific to individual
9973    compilations (functions).  */
9974
9975 static int
9976 is_comdat_die (dw_die_ref c)
9977 {
9978   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9979      we do for stabs.  The advantage is a greater likelihood of sharing between
9980      objects that don't include headers in the same order (and therefore would
9981      put the base types in a different comdat).  jason 8/28/00 */
9982
9983   if (c->die_tag == DW_TAG_base_type)
9984     return 0;
9985
9986   if (c->die_tag == DW_TAG_pointer_type
9987       || c->die_tag == DW_TAG_reference_type
9988       || c->die_tag == DW_TAG_rvalue_reference_type
9989       || c->die_tag == DW_TAG_const_type
9990       || c->die_tag == DW_TAG_volatile_type)
9991     {
9992       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9993
9994       return t ? is_comdat_die (t) : 0;
9995     }
9996
9997   return is_type_die (c);
9998 }
9999
10000 /* Returns 1 iff C is the sort of DIE that might be referred to from another
10001    compilation unit.  */
10002
10003 static int
10004 is_symbol_die (dw_die_ref c)
10005 {
10006   return (is_type_die (c)
10007           || is_declaration_die (c)
10008           || c->die_tag == DW_TAG_namespace
10009           || c->die_tag == DW_TAG_module);
10010 }
10011
10012 /* Returns true iff C is a compile-unit DIE.  */
10013
10014 static inline bool
10015 is_cu_die (dw_die_ref c)
10016 {
10017   return c && c->die_tag == DW_TAG_compile_unit;
10018 }
10019
10020 static char *
10021 gen_internal_sym (const char *prefix)
10022 {
10023   char buf[256];
10024
10025   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
10026   return xstrdup (buf);
10027 }
10028
10029 /* Assign symbols to all worthy DIEs under DIE.  */
10030
10031 static void
10032 assign_symbol_names (dw_die_ref die)
10033 {
10034   dw_die_ref c;
10035
10036   if (is_symbol_die (die))
10037     {
10038       if (comdat_symbol_id)
10039         {
10040           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
10041
10042           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
10043                    comdat_symbol_id, comdat_symbol_number++);
10044           die->die_id.die_symbol = xstrdup (p);
10045         }
10046       else
10047         die->die_id.die_symbol = gen_internal_sym ("LDIE");
10048     }
10049
10050   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
10051 }
10052
10053 struct cu_hash_table_entry
10054 {
10055   dw_die_ref cu;
10056   unsigned min_comdat_num, max_comdat_num;
10057   struct cu_hash_table_entry *next;
10058 };
10059
10060 /* Routines to manipulate hash table of CUs.  */
10061 static hashval_t
10062 htab_cu_hash (const void *of)
10063 {
10064   const struct cu_hash_table_entry *const entry =
10065     (const struct cu_hash_table_entry *) of;
10066
10067   return htab_hash_string (entry->cu->die_id.die_symbol);
10068 }
10069
10070 static int
10071 htab_cu_eq (const void *of1, const void *of2)
10072 {
10073   const struct cu_hash_table_entry *const entry1 =
10074     (const struct cu_hash_table_entry *) of1;
10075   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10076
10077   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
10078 }
10079
10080 static void
10081 htab_cu_del (void *what)
10082 {
10083   struct cu_hash_table_entry *next,
10084     *entry = (struct cu_hash_table_entry *) what;
10085
10086   while (entry)
10087     {
10088       next = entry->next;
10089       free (entry);
10090       entry = next;
10091     }
10092 }
10093
10094 /* Check whether we have already seen this CU and set up SYM_NUM
10095    accordingly.  */
10096 static int
10097 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
10098 {
10099   struct cu_hash_table_entry dummy;
10100   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
10101
10102   dummy.max_comdat_num = 0;
10103
10104   slot = (struct cu_hash_table_entry **)
10105     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10106         INSERT);
10107   entry = *slot;
10108
10109   for (; entry; last = entry, entry = entry->next)
10110     {
10111       if (same_die_p_wrap (cu, entry->cu))
10112         break;
10113     }
10114
10115   if (entry)
10116     {
10117       *sym_num = entry->min_comdat_num;
10118       return 1;
10119     }
10120
10121   entry = XCNEW (struct cu_hash_table_entry);
10122   entry->cu = cu;
10123   entry->min_comdat_num = *sym_num = last->max_comdat_num;
10124   entry->next = *slot;
10125   *slot = entry;
10126
10127   return 0;
10128 }
10129
10130 /* Record SYM_NUM to record of CU in HTABLE.  */
10131 static void
10132 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
10133 {
10134   struct cu_hash_table_entry **slot, *entry;
10135
10136   slot = (struct cu_hash_table_entry **)
10137     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10138         NO_INSERT);
10139   entry = *slot;
10140
10141   entry->max_comdat_num = sym_num;
10142 }
10143
10144 /* Traverse the DIE (which is always comp_unit_die), and set up
10145    additional compilation units for each of the include files we see
10146    bracketed by BINCL/EINCL.  */
10147
10148 static void
10149 break_out_includes (dw_die_ref die)
10150 {
10151   dw_die_ref c;
10152   dw_die_ref unit = NULL;
10153   limbo_die_node *node, **pnode;
10154   htab_t cu_hash_table;
10155
10156   c = die->die_child;
10157   if (c) do {
10158     dw_die_ref prev = c;
10159     c = c->die_sib;
10160     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
10161            || (unit && is_comdat_die (c)))
10162       {
10163         dw_die_ref next = c->die_sib;
10164
10165         /* This DIE is for a secondary CU; remove it from the main one.  */
10166         remove_child_with_prev (c, prev);
10167
10168         if (c->die_tag == DW_TAG_GNU_BINCL)
10169           unit = push_new_compile_unit (unit, c);
10170         else if (c->die_tag == DW_TAG_GNU_EINCL)
10171           unit = pop_compile_unit (unit);
10172         else
10173           add_child_die (unit, c);
10174         c = next;
10175         if (c == die->die_child)
10176           break;
10177       }
10178   } while (c != die->die_child);
10179
10180 #if 0
10181   /* We can only use this in debugging, since the frontend doesn't check
10182      to make sure that we leave every include file we enter.  */
10183   gcc_assert (!unit);
10184 #endif
10185
10186   assign_symbol_names (die);
10187   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
10188   for (node = limbo_die_list, pnode = &limbo_die_list;
10189        node;
10190        node = node->next)
10191     {
10192       int is_dupl;
10193
10194       compute_section_prefix (node->die);
10195       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
10196                         &comdat_symbol_number);
10197       assign_symbol_names (node->die);
10198       if (is_dupl)
10199         *pnode = node->next;
10200       else
10201         {
10202           pnode = &node->next;
10203           record_comdat_symbol_number (node->die, cu_hash_table,
10204                 comdat_symbol_number);
10205         }
10206     }
10207   htab_delete (cu_hash_table);
10208 }
10209
10210 /* Return non-zero if this DIE is a declaration.  */
10211
10212 static int
10213 is_declaration_die (dw_die_ref die)
10214 {
10215   dw_attr_ref a;
10216   unsigned ix;
10217
10218   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10219     if (a->dw_attr == DW_AT_declaration)
10220       return 1;
10221
10222   return 0;
10223 }
10224
10225 /* Return non-zero if this DIE is nested inside a subprogram.  */
10226
10227 static int
10228 is_nested_in_subprogram (dw_die_ref die)
10229 {
10230   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
10231
10232   if (decl == NULL)
10233     decl = die;
10234   return local_scope_p (decl);
10235 }
10236
10237 /* Return non-zero if this DIE contains a defining declaration of a
10238    subprogram.  */
10239
10240 static int
10241 contains_subprogram_definition (dw_die_ref die)
10242 {
10243   dw_die_ref c;
10244
10245   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
10246     return 1;
10247   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
10248   return 0;
10249 }
10250
10251 /* Return non-zero if this is a type DIE that should be moved to a
10252    COMDAT .debug_types section.  */
10253
10254 static int
10255 should_move_die_to_comdat (dw_die_ref die)
10256 {
10257   switch (die->die_tag)
10258     {
10259     case DW_TAG_class_type:
10260     case DW_TAG_structure_type:
10261     case DW_TAG_enumeration_type:
10262     case DW_TAG_union_type:
10263       /* Don't move declarations, inlined instances, or types nested in a
10264          subprogram.  */
10265       if (is_declaration_die (die)
10266           || get_AT (die, DW_AT_abstract_origin)
10267           || is_nested_in_subprogram (die))
10268         return 0;
10269       /* A type definition should never contain a subprogram definition.  */
10270       gcc_assert (!contains_subprogram_definition (die));
10271       return 1;
10272     case DW_TAG_array_type:
10273     case DW_TAG_interface_type:
10274     case DW_TAG_pointer_type:
10275     case DW_TAG_reference_type:
10276     case DW_TAG_rvalue_reference_type:
10277     case DW_TAG_string_type:
10278     case DW_TAG_subroutine_type:
10279     case DW_TAG_ptr_to_member_type:
10280     case DW_TAG_set_type:
10281     case DW_TAG_subrange_type:
10282     case DW_TAG_base_type:
10283     case DW_TAG_const_type:
10284     case DW_TAG_file_type:
10285     case DW_TAG_packed_type:
10286     case DW_TAG_volatile_type:
10287     case DW_TAG_typedef:
10288     default:
10289       return 0;
10290     }
10291 }
10292
10293 /* Make a clone of DIE.  */
10294
10295 static dw_die_ref
10296 clone_die (dw_die_ref die)
10297 {
10298   dw_die_ref clone;
10299   dw_attr_ref a;
10300   unsigned ix;
10301
10302   clone = ggc_alloc_cleared_die_node ();
10303   clone->die_tag = die->die_tag;
10304
10305   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10306     add_dwarf_attr (clone, a);
10307
10308   return clone;
10309 }
10310
10311 /* Make a clone of the tree rooted at DIE.  */
10312
10313 static dw_die_ref
10314 clone_tree (dw_die_ref die)
10315 {
10316   dw_die_ref c;
10317   dw_die_ref clone = clone_die (die);
10318
10319   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10320
10321   return clone;
10322 }
10323
10324 /* Make a clone of DIE as a declaration.  */
10325
10326 static dw_die_ref
10327 clone_as_declaration (dw_die_ref die)
10328 {
10329   dw_die_ref clone;
10330   dw_die_ref decl;
10331   dw_attr_ref a;
10332   unsigned ix;
10333
10334   /* If the DIE is already a declaration, just clone it.  */
10335   if (is_declaration_die (die))
10336     return clone_die (die);
10337
10338   /* If the DIE is a specification, just clone its declaration DIE.  */
10339   decl = get_AT_ref (die, DW_AT_specification);
10340   if (decl != NULL)
10341     return clone_die (decl);
10342
10343   clone = ggc_alloc_cleared_die_node ();
10344   clone->die_tag = die->die_tag;
10345
10346   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10347     {
10348       /* We don't want to copy over all attributes.
10349          For example we don't want DW_AT_byte_size because otherwise we will no
10350          longer have a declaration and GDB will treat it as a definition.  */
10351
10352       switch (a->dw_attr)
10353         {
10354         case DW_AT_artificial:
10355         case DW_AT_containing_type:
10356         case DW_AT_external:
10357         case DW_AT_name:
10358         case DW_AT_type:
10359         case DW_AT_virtuality:
10360         case DW_AT_linkage_name:
10361         case DW_AT_MIPS_linkage_name:
10362           add_dwarf_attr (clone, a);
10363           break;
10364         case DW_AT_byte_size:
10365         default:
10366           break;
10367         }
10368     }
10369
10370   if (die->die_id.die_type_node)
10371     add_AT_die_ref (clone, DW_AT_signature, die);
10372
10373   add_AT_flag (clone, DW_AT_declaration, 1);
10374   return clone;
10375 }
10376
10377 /* Copy the declaration context to the new compile unit DIE.  This includes
10378    any surrounding namespace or type declarations.  If the DIE has an
10379    AT_specification attribute, it also includes attributes and children
10380    attached to the specification.  */
10381
10382 static void
10383 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10384 {
10385   dw_die_ref decl;
10386   dw_die_ref new_decl;
10387
10388   decl = get_AT_ref (die, DW_AT_specification);
10389   if (decl == NULL)
10390     decl = die;
10391   else
10392     {
10393       unsigned ix;
10394       dw_die_ref c;
10395       dw_attr_ref a;
10396
10397       /* Copy the type node pointer from the new DIE to the original
10398          declaration DIE so we can forward references later.  */
10399       decl->die_id.die_type_node = die->die_id.die_type_node;
10400
10401       remove_AT (die, DW_AT_specification);
10402
10403       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10404         {
10405           if (a->dw_attr != DW_AT_name
10406               && a->dw_attr != DW_AT_declaration
10407               && a->dw_attr != DW_AT_external)
10408             add_dwarf_attr (die, a);
10409         }
10410
10411       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10412     }
10413
10414   if (decl->die_parent != NULL
10415       && decl->die_parent->die_tag != DW_TAG_compile_unit
10416       && decl->die_parent->die_tag != DW_TAG_type_unit)
10417     {
10418       new_decl = copy_ancestor_tree (unit, decl, NULL);
10419       if (new_decl != NULL)
10420         {
10421           remove_AT (new_decl, DW_AT_signature);
10422           add_AT_specification (die, new_decl);
10423         }
10424     }
10425 }
10426
10427 /* Generate the skeleton ancestor tree for the given NODE, then clone
10428    the DIE and add the clone into the tree.  */
10429
10430 static void
10431 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10432 {
10433   if (node->new_die != NULL)
10434     return;
10435
10436   node->new_die = clone_as_declaration (node->old_die);
10437
10438   if (node->parent != NULL)
10439     {
10440       generate_skeleton_ancestor_tree (node->parent);
10441       add_child_die (node->parent->new_die, node->new_die);
10442     }
10443 }
10444
10445 /* Generate a skeleton tree of DIEs containing any declarations that are
10446    found in the original tree.  We traverse the tree looking for declaration
10447    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10448
10449 static void
10450 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10451 {
10452   skeleton_chain_node node;
10453   dw_die_ref c;
10454   dw_die_ref first;
10455   dw_die_ref prev = NULL;
10456   dw_die_ref next = NULL;
10457
10458   node.parent = parent;
10459
10460   first = c = parent->old_die->die_child;
10461   if (c)
10462     next = c->die_sib;
10463   if (c) do {
10464     if (prev == NULL || prev->die_sib == c)
10465       prev = c;
10466     c = next;
10467     next = (c == first ? NULL : c->die_sib);
10468     node.old_die = c;
10469     node.new_die = NULL;
10470     if (is_declaration_die (c))
10471       {
10472         /* Clone the existing DIE, move the original to the skeleton
10473            tree (which is in the main CU), and put the clone, with
10474            all the original's children, where the original came from.  */
10475         dw_die_ref clone = clone_die (c);
10476         move_all_children (c, clone);
10477
10478         replace_child (c, clone, prev);
10479         generate_skeleton_ancestor_tree (parent);
10480         add_child_die (parent->new_die, c);
10481         node.new_die = c;
10482         c = clone;
10483       }
10484     generate_skeleton_bottom_up (&node);
10485   } while (next != NULL);
10486 }
10487
10488 /* Wrapper function for generate_skeleton_bottom_up.  */
10489
10490 static dw_die_ref
10491 generate_skeleton (dw_die_ref die)
10492 {
10493   skeleton_chain_node node;
10494
10495   node.old_die = die;
10496   node.new_die = NULL;
10497   node.parent = NULL;
10498
10499   /* If this type definition is nested inside another type,
10500      always leave at least a declaration in its place.  */
10501   if (die->die_parent != NULL && is_type_die (die->die_parent))
10502     node.new_die = clone_as_declaration (die);
10503
10504   generate_skeleton_bottom_up (&node);
10505   return node.new_die;
10506 }
10507
10508 /* Remove the DIE from its parent, possibly replacing it with a cloned
10509    declaration.  The original DIE will be moved to a new compile unit
10510    so that existing references to it follow it to the new location.  If
10511    any of the original DIE's descendants is a declaration, we need to
10512    replace the original DIE with a skeleton tree and move the
10513    declarations back into the skeleton tree.  */
10514
10515 static dw_die_ref
10516 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10517 {
10518   dw_die_ref skeleton;
10519
10520   skeleton = generate_skeleton (child);
10521   if (skeleton == NULL)
10522     remove_child_with_prev (child, prev);
10523   else
10524     {
10525       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10526       replace_child (child, skeleton, prev);
10527     }
10528
10529   return skeleton;
10530 }
10531
10532 /* Traverse the DIE and set up additional .debug_types sections for each
10533    type worthy of being placed in a COMDAT section.  */
10534
10535 static void
10536 break_out_comdat_types (dw_die_ref die)
10537 {
10538   dw_die_ref c;
10539   dw_die_ref first;
10540   dw_die_ref prev = NULL;
10541   dw_die_ref next = NULL;
10542   dw_die_ref unit = NULL;
10543
10544   first = c = die->die_child;
10545   if (c)
10546     next = c->die_sib;
10547   if (c) do {
10548     if (prev == NULL || prev->die_sib == c)
10549       prev = c;
10550     c = next;
10551     next = (c == first ? NULL : c->die_sib);
10552     if (should_move_die_to_comdat (c))
10553       {
10554         dw_die_ref replacement;
10555         comdat_type_node_ref type_node;
10556
10557         /* Create a new type unit DIE as the root for the new tree, and
10558            add it to the list of comdat types.  */
10559         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10560         add_AT_unsigned (unit, DW_AT_language,
10561                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10562         type_node = ggc_alloc_cleared_comdat_type_node ();
10563         type_node->root_die = unit;
10564         type_node->next = comdat_type_list;
10565         comdat_type_list = type_node;
10566
10567         /* Generate the type signature.  */
10568         generate_type_signature (c, type_node);
10569
10570         /* Copy the declaration context, attributes, and children of the
10571            declaration into the new compile unit DIE.  */
10572         copy_declaration_context (unit, c);
10573
10574         /* Remove this DIE from the main CU.  */
10575         replacement = remove_child_or_replace_with_skeleton (c, prev);
10576
10577         /* Break out nested types into their own type units.  */
10578         break_out_comdat_types (c);
10579
10580         /* Add the DIE to the new compunit.  */
10581         add_child_die (unit, c);
10582
10583         if (replacement != NULL)
10584           c = replacement;
10585       }
10586     else if (c->die_tag == DW_TAG_namespace
10587              || c->die_tag == DW_TAG_class_type
10588              || c->die_tag == DW_TAG_structure_type
10589              || c->die_tag == DW_TAG_union_type)
10590       {
10591         /* Look for nested types that can be broken out.  */
10592         break_out_comdat_types (c);
10593       }
10594   } while (next != NULL);
10595 }
10596
10597 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10598
10599 struct decl_table_entry
10600 {
10601   dw_die_ref orig;
10602   dw_die_ref copy;
10603 };
10604
10605 /* Routines to manipulate hash table of copied declarations.  */
10606
10607 static hashval_t
10608 htab_decl_hash (const void *of)
10609 {
10610   const struct decl_table_entry *const entry =
10611     (const struct decl_table_entry *) of;
10612
10613   return htab_hash_pointer (entry->orig);
10614 }
10615
10616 static int
10617 htab_decl_eq (const void *of1, const void *of2)
10618 {
10619   const struct decl_table_entry *const entry1 =
10620     (const struct decl_table_entry *) of1;
10621   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10622
10623   return entry1->orig == entry2;
10624 }
10625
10626 static void
10627 htab_decl_del (void *what)
10628 {
10629   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10630
10631   free (entry);
10632 }
10633
10634 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10635    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10636    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10637    to check if the ancestor has already been copied into UNIT.  */
10638
10639 static dw_die_ref
10640 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10641 {
10642   dw_die_ref parent = die->die_parent;
10643   dw_die_ref new_parent = unit;
10644   dw_die_ref copy;
10645   void **slot = NULL;
10646   struct decl_table_entry *entry = NULL;
10647
10648   if (decl_table)
10649     {
10650       /* Check if the entry has already been copied to UNIT.  */
10651       slot = htab_find_slot_with_hash (decl_table, die,
10652                                        htab_hash_pointer (die), INSERT);
10653       if (*slot != HTAB_EMPTY_ENTRY)
10654         {
10655           entry = (struct decl_table_entry *) *slot;
10656           return entry->copy;
10657         }
10658
10659       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10660       entry = XCNEW (struct decl_table_entry);
10661       entry->orig = die;
10662       entry->copy = NULL;
10663       *slot = entry;
10664     }
10665
10666   if (parent != NULL)
10667     {
10668       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10669       if (spec != NULL)
10670         parent = spec;
10671       if (parent->die_tag != DW_TAG_compile_unit
10672           && parent->die_tag != DW_TAG_type_unit)
10673         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10674     }
10675
10676   copy = clone_as_declaration (die);
10677   add_child_die (new_parent, copy);
10678
10679   if (decl_table != NULL)
10680     {
10681       /* Record the pointer to the copy.  */
10682       entry->copy = copy;
10683     }
10684
10685   return copy;
10686 }
10687
10688 /* Walk the DIE and its children, looking for references to incomplete
10689    or trivial types that are unmarked (i.e., that are not in the current
10690    type_unit).  */
10691
10692 static void
10693 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10694 {
10695   dw_die_ref c;
10696   dw_attr_ref a;
10697   unsigned ix;
10698
10699   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10700     {
10701       if (AT_class (a) == dw_val_class_die_ref)
10702         {
10703           dw_die_ref targ = AT_ref (a);
10704           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10705           void **slot;
10706           struct decl_table_entry *entry;
10707
10708           if (targ->die_mark != 0 || type_node != NULL)
10709             continue;
10710
10711           slot = htab_find_slot_with_hash (decl_table, targ,
10712                                            htab_hash_pointer (targ), INSERT);
10713
10714           if (*slot != HTAB_EMPTY_ENTRY)
10715             {
10716               /* TARG has already been copied, so we just need to
10717                  modify the reference to point to the copy.  */
10718               entry = (struct decl_table_entry *) *slot;
10719               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10720             }
10721           else
10722             {
10723               dw_die_ref parent = unit;
10724               dw_die_ref copy = clone_tree (targ);
10725
10726               /* Make sure the cloned tree is marked as part of the
10727                  type unit.  */
10728               mark_dies (copy);
10729
10730               /* Record in DECL_TABLE that TARG has been copied.
10731                  Need to do this now, before the recursive call,
10732                  because DECL_TABLE may be expanded and SLOT
10733                  would no longer be a valid pointer.  */
10734               entry = XCNEW (struct decl_table_entry);
10735               entry->orig = targ;
10736               entry->copy = copy;
10737               *slot = entry;
10738
10739               /* If TARG has surrounding context, copy its ancestor tree
10740                  into the new type unit.  */
10741               if (targ->die_parent != NULL
10742                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10743                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10744                 parent = copy_ancestor_tree (unit, targ->die_parent,
10745                                              decl_table);
10746
10747               add_child_die (parent, copy);
10748               a->dw_attr_val.v.val_die_ref.die = copy;
10749
10750               /* Make sure the newly-copied DIE is walked.  If it was
10751                  installed in a previously-added context, it won't
10752                  get visited otherwise.  */
10753               if (parent != unit)
10754                 {
10755                   /* Find the highest point of the newly-added tree,
10756                      mark each node along the way, and walk from there.  */
10757                   parent->die_mark = 1;
10758                   while (parent->die_parent
10759                          && parent->die_parent->die_mark == 0)
10760                     {
10761                       parent = parent->die_parent;
10762                       parent->die_mark = 1;
10763                     }
10764                   copy_decls_walk (unit, parent, decl_table);
10765                 }
10766             }
10767         }
10768     }
10769
10770   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10771 }
10772
10773 /* Copy declarations for "unworthy" types into the new comdat section.
10774    Incomplete types, modified types, and certain other types aren't broken
10775    out into comdat sections of their own, so they don't have a signature,
10776    and we need to copy the declaration into the same section so that we
10777    don't have an external reference.  */
10778
10779 static void
10780 copy_decls_for_unworthy_types (dw_die_ref unit)
10781 {
10782   htab_t decl_table;
10783
10784   mark_dies (unit);
10785   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10786   copy_decls_walk (unit, unit, decl_table);
10787   htab_delete (decl_table);
10788   unmark_dies (unit);
10789 }
10790
10791 /* Traverse the DIE and add a sibling attribute if it may have the
10792    effect of speeding up access to siblings.  To save some space,
10793    avoid generating sibling attributes for DIE's without children.  */
10794
10795 static void
10796 add_sibling_attributes (dw_die_ref die)
10797 {
10798   dw_die_ref c;
10799
10800   if (! die->die_child)
10801     return;
10802
10803   if (die->die_parent && die != die->die_parent->die_child)
10804     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10805
10806   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10807 }
10808
10809 /* Output all location lists for the DIE and its children.  */
10810
10811 static void
10812 output_location_lists (dw_die_ref die)
10813 {
10814   dw_die_ref c;
10815   dw_attr_ref a;
10816   unsigned ix;
10817
10818   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10819     if (AT_class (a) == dw_val_class_loc_list)
10820       output_loc_list (AT_loc_list (a));
10821
10822   FOR_EACH_CHILD (die, c, output_location_lists (c));
10823 }
10824
10825 /* The format of each DIE (and its attribute value pairs) is encoded in an
10826    abbreviation table.  This routine builds the abbreviation table and assigns
10827    a unique abbreviation id for each abbreviation entry.  The children of each
10828    die are visited recursively.  */
10829
10830 static void
10831 build_abbrev_table (dw_die_ref die)
10832 {
10833   unsigned long abbrev_id;
10834   unsigned int n_alloc;
10835   dw_die_ref c;
10836   dw_attr_ref a;
10837   unsigned ix;
10838
10839   /* Scan the DIE references, and mark as external any that refer to
10840      DIEs from other CUs (i.e. those which are not marked).  */
10841   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10842     if (AT_class (a) == dw_val_class_die_ref
10843         && AT_ref (a)->die_mark == 0)
10844       {
10845         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
10846         set_AT_ref_external (a, 1);
10847       }
10848
10849   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10850     {
10851       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10852       dw_attr_ref die_a, abbrev_a;
10853       unsigned ix;
10854       bool ok = true;
10855
10856       if (abbrev->die_tag != die->die_tag)
10857         continue;
10858       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10859         continue;
10860
10861       if (VEC_length (dw_attr_node, abbrev->die_attr)
10862           != VEC_length (dw_attr_node, die->die_attr))
10863         continue;
10864
10865       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10866         {
10867           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10868           if ((abbrev_a->dw_attr != die_a->dw_attr)
10869               || (value_format (abbrev_a) != value_format (die_a)))
10870             {
10871               ok = false;
10872               break;
10873             }
10874         }
10875       if (ok)
10876         break;
10877     }
10878
10879   if (abbrev_id >= abbrev_die_table_in_use)
10880     {
10881       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10882         {
10883           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10884           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10885                                             n_alloc);
10886
10887           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10888                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10889           abbrev_die_table_allocated = n_alloc;
10890         }
10891
10892       ++abbrev_die_table_in_use;
10893       abbrev_die_table[abbrev_id] = die;
10894     }
10895
10896   die->die_abbrev = abbrev_id;
10897   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10898 }
10899 \f
10900 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10901
10902 static int
10903 constant_size (unsigned HOST_WIDE_INT value)
10904 {
10905   int log;
10906
10907   if (value == 0)
10908     log = 0;
10909   else
10910     log = floor_log2 (value);
10911
10912   log = log / 8;
10913   log = 1 << (floor_log2 (log) + 1);
10914
10915   return log;
10916 }
10917
10918 /* Return the size of a DIE as it is represented in the
10919    .debug_info section.  */
10920
10921 static unsigned long
10922 size_of_die (dw_die_ref die)
10923 {
10924   unsigned long size = 0;
10925   dw_attr_ref a;
10926   unsigned ix;
10927
10928   size += size_of_uleb128 (die->die_abbrev);
10929   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10930     {
10931       switch (AT_class (a))
10932         {
10933         case dw_val_class_addr:
10934           size += DWARF2_ADDR_SIZE;
10935           break;
10936         case dw_val_class_offset:
10937           size += DWARF_OFFSET_SIZE;
10938           break;
10939         case dw_val_class_loc:
10940           {
10941             unsigned long lsize = size_of_locs (AT_loc (a));
10942
10943             /* Block length.  */
10944             if (dwarf_version >= 4)
10945               size += size_of_uleb128 (lsize);
10946             else
10947               size += constant_size (lsize);
10948             size += lsize;
10949           }
10950           break;
10951         case dw_val_class_loc_list:
10952           size += DWARF_OFFSET_SIZE;
10953           break;
10954         case dw_val_class_range_list:
10955           size += DWARF_OFFSET_SIZE;
10956           break;
10957         case dw_val_class_const:
10958           size += size_of_sleb128 (AT_int (a));
10959           break;
10960         case dw_val_class_unsigned_const:
10961           size += constant_size (AT_unsigned (a));
10962           break;
10963         case dw_val_class_const_double:
10964           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10965           if (HOST_BITS_PER_WIDE_INT >= 64)
10966             size++; /* block */
10967           break;
10968         case dw_val_class_vec:
10969           size += constant_size (a->dw_attr_val.v.val_vec.length
10970                                  * a->dw_attr_val.v.val_vec.elt_size)
10971                   + a->dw_attr_val.v.val_vec.length
10972                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10973           break;
10974         case dw_val_class_flag:
10975           if (dwarf_version >= 4)
10976             /* Currently all add_AT_flag calls pass in 1 as last argument,
10977                so DW_FORM_flag_present can be used.  If that ever changes,
10978                we'll need to use DW_FORM_flag and have some optimization
10979                in build_abbrev_table that will change those to
10980                DW_FORM_flag_present if it is set to 1 in all DIEs using
10981                the same abbrev entry.  */
10982             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10983           else
10984             size += 1;
10985           break;
10986         case dw_val_class_die_ref:
10987           if (AT_ref_external (a))
10988             {
10989               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10990                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10991                  is sized by target address length, whereas in DWARF3
10992                  it's always sized as an offset.  */
10993               if (use_debug_types)
10994                 size += DWARF_TYPE_SIGNATURE_SIZE;
10995               else if (dwarf_version == 2)
10996                 size += DWARF2_ADDR_SIZE;
10997               else
10998                 size += DWARF_OFFSET_SIZE;
10999             }
11000           else
11001             size += DWARF_OFFSET_SIZE;
11002           break;
11003         case dw_val_class_fde_ref:
11004           size += DWARF_OFFSET_SIZE;
11005           break;
11006         case dw_val_class_lbl_id:
11007           size += DWARF2_ADDR_SIZE;
11008           break;
11009         case dw_val_class_lineptr:
11010         case dw_val_class_macptr:
11011           size += DWARF_OFFSET_SIZE;
11012           break;
11013         case dw_val_class_str:
11014           if (AT_string_form (a) == DW_FORM_strp)
11015             size += DWARF_OFFSET_SIZE;
11016           else
11017             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
11018           break;
11019         case dw_val_class_file:
11020           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
11021           break;
11022         case dw_val_class_data8:
11023           size += 8;
11024           break;
11025         case dw_val_class_vms_delta:
11026           size += DWARF_OFFSET_SIZE;
11027           break;
11028         default:
11029           gcc_unreachable ();
11030         }
11031     }
11032
11033   return size;
11034 }
11035
11036 /* Size the debugging information associated with a given DIE.  Visits the
11037    DIE's children recursively.  Updates the global variable next_die_offset, on
11038    each time through.  Uses the current value of next_die_offset to update the
11039    die_offset field in each DIE.  */
11040
11041 static void
11042 calc_die_sizes (dw_die_ref die)
11043 {
11044   dw_die_ref c;
11045
11046   gcc_assert (die->die_offset == 0
11047               || (unsigned long int) die->die_offset == next_die_offset);
11048   die->die_offset = next_die_offset;
11049   next_die_offset += size_of_die (die);
11050
11051   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
11052
11053   if (die->die_child != NULL)
11054     /* Count the null byte used to terminate sibling lists.  */
11055     next_die_offset += 1;
11056 }
11057
11058 /* Size just the base type children at the start of the CU.
11059    This is needed because build_abbrev needs to size locs
11060    and sizing of type based stack ops needs to know die_offset
11061    values for the base types.  */
11062
11063 static void
11064 calc_base_type_die_sizes (void)
11065 {
11066   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11067   unsigned int i;
11068   dw_die_ref base_type;
11069 #if ENABLE_ASSERT_CHECKING
11070   dw_die_ref prev = comp_unit_die ()->die_child;
11071 #endif
11072
11073   die_offset += size_of_die (comp_unit_die ());
11074   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
11075     {
11076 #if ENABLE_ASSERT_CHECKING
11077       gcc_assert (base_type->die_offset == 0
11078                   && prev->die_sib == base_type
11079                   && base_type->die_child == NULL
11080                   && base_type->die_abbrev);
11081       prev = base_type;
11082 #endif
11083       base_type->die_offset = die_offset;
11084       die_offset += size_of_die (base_type);
11085     }
11086 }
11087
11088 /* Set the marks for a die and its children.  We do this so
11089    that we know whether or not a reference needs to use FORM_ref_addr; only
11090    DIEs in the same CU will be marked.  We used to clear out the offset
11091    and use that as the flag, but ran into ordering problems.  */
11092
11093 static void
11094 mark_dies (dw_die_ref die)
11095 {
11096   dw_die_ref c;
11097
11098   gcc_assert (!die->die_mark);
11099
11100   die->die_mark = 1;
11101   FOR_EACH_CHILD (die, c, mark_dies (c));
11102 }
11103
11104 /* Clear the marks for a die and its children.  */
11105
11106 static void
11107 unmark_dies (dw_die_ref die)
11108 {
11109   dw_die_ref c;
11110
11111   if (! use_debug_types)
11112     gcc_assert (die->die_mark);
11113
11114   die->die_mark = 0;
11115   FOR_EACH_CHILD (die, c, unmark_dies (c));
11116 }
11117
11118 /* Clear the marks for a die, its children and referred dies.  */
11119
11120 static void
11121 unmark_all_dies (dw_die_ref die)
11122 {
11123   dw_die_ref c;
11124   dw_attr_ref a;
11125   unsigned ix;
11126
11127   if (!die->die_mark)
11128     return;
11129   die->die_mark = 0;
11130
11131   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
11132
11133   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11134     if (AT_class (a) == dw_val_class_die_ref)
11135       unmark_all_dies (AT_ref (a));
11136 }
11137
11138 /* Return the size of the .debug_pubnames or .debug_pubtypes table
11139    generated for the compilation unit.  */
11140
11141 static unsigned long
11142 size_of_pubnames (VEC (pubname_entry, gc) * names)
11143 {
11144   unsigned long size;
11145   unsigned i;
11146   pubname_ref p;
11147
11148   size = DWARF_PUBNAMES_HEADER_SIZE;
11149   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
11150     if (names != pubtype_table
11151         || p->die->die_offset != 0
11152         || !flag_eliminate_unused_debug_types)
11153       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
11154
11155   size += DWARF_OFFSET_SIZE;
11156   return size;
11157 }
11158
11159 /* Return the size of the information in the .debug_aranges section.  */
11160
11161 static unsigned long
11162 size_of_aranges (void)
11163 {
11164   unsigned long size;
11165
11166   size = DWARF_ARANGES_HEADER_SIZE;
11167
11168   /* Count the address/length pair for this compilation unit.  */
11169   if (text_section_used)
11170     size += 2 * DWARF2_ADDR_SIZE;
11171   if (cold_text_section_used)
11172     size += 2 * DWARF2_ADDR_SIZE;
11173   if (have_multiple_function_sections)
11174     {
11175       unsigned fde_idx = 0;
11176
11177       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11178         {
11179           dw_fde_ref fde = &fde_table[fde_idx];
11180
11181           if (!fde->in_std_section)
11182             size += 2 * DWARF2_ADDR_SIZE;
11183           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11184             size += 2 * DWARF2_ADDR_SIZE;
11185         }
11186     }
11187
11188   /* Count the two zero words used to terminated the address range table.  */
11189   size += 2 * DWARF2_ADDR_SIZE;
11190   return size;
11191 }
11192 \f
11193 /* Select the encoding of an attribute value.  */
11194
11195 static enum dwarf_form
11196 value_format (dw_attr_ref a)
11197 {
11198   switch (a->dw_attr_val.val_class)
11199     {
11200     case dw_val_class_addr:
11201       /* Only very few attributes allow DW_FORM_addr.  */
11202       switch (a->dw_attr)
11203         {
11204         case DW_AT_low_pc:
11205         case DW_AT_high_pc:
11206         case DW_AT_entry_pc:
11207         case DW_AT_trampoline:
11208           return DW_FORM_addr;
11209         default:
11210           break;
11211         }
11212       switch (DWARF2_ADDR_SIZE)
11213         {
11214         case 1:
11215           return DW_FORM_data1;
11216         case 2:
11217           return DW_FORM_data2;
11218         case 4:
11219           return DW_FORM_data4;
11220         case 8:
11221           return DW_FORM_data8;
11222         default:
11223           gcc_unreachable ();
11224         }
11225     case dw_val_class_range_list:
11226     case dw_val_class_loc_list:
11227       if (dwarf_version >= 4)
11228         return DW_FORM_sec_offset;
11229       /* FALLTHRU */
11230     case dw_val_class_vms_delta:
11231     case dw_val_class_offset:
11232       switch (DWARF_OFFSET_SIZE)
11233         {
11234         case 4:
11235           return DW_FORM_data4;
11236         case 8:
11237           return DW_FORM_data8;
11238         default:
11239           gcc_unreachable ();
11240         }
11241     case dw_val_class_loc:
11242       if (dwarf_version >= 4)
11243         return DW_FORM_exprloc;
11244       switch (constant_size (size_of_locs (AT_loc (a))))
11245         {
11246         case 1:
11247           return DW_FORM_block1;
11248         case 2:
11249           return DW_FORM_block2;
11250         default:
11251           gcc_unreachable ();
11252         }
11253     case dw_val_class_const:
11254       return DW_FORM_sdata;
11255     case dw_val_class_unsigned_const:
11256       switch (constant_size (AT_unsigned (a)))
11257         {
11258         case 1:
11259           return DW_FORM_data1;
11260         case 2:
11261           return DW_FORM_data2;
11262         case 4:
11263           return DW_FORM_data4;
11264         case 8:
11265           return DW_FORM_data8;
11266         default:
11267           gcc_unreachable ();
11268         }
11269     case dw_val_class_const_double:
11270       switch (HOST_BITS_PER_WIDE_INT)
11271         {
11272         case 8:
11273           return DW_FORM_data2;
11274         case 16:
11275           return DW_FORM_data4;
11276         case 32:
11277           return DW_FORM_data8;
11278         case 64:
11279         default:
11280           return DW_FORM_block1;
11281         }
11282     case dw_val_class_vec:
11283       switch (constant_size (a->dw_attr_val.v.val_vec.length
11284                              * a->dw_attr_val.v.val_vec.elt_size))
11285         {
11286         case 1:
11287           return DW_FORM_block1;
11288         case 2:
11289           return DW_FORM_block2;
11290         case 4:
11291           return DW_FORM_block4;
11292         default:
11293           gcc_unreachable ();
11294         }
11295     case dw_val_class_flag:
11296       if (dwarf_version >= 4)
11297         {
11298           /* Currently all add_AT_flag calls pass in 1 as last argument,
11299              so DW_FORM_flag_present can be used.  If that ever changes,
11300              we'll need to use DW_FORM_flag and have some optimization
11301              in build_abbrev_table that will change those to
11302              DW_FORM_flag_present if it is set to 1 in all DIEs using
11303              the same abbrev entry.  */
11304           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11305           return DW_FORM_flag_present;
11306         }
11307       return DW_FORM_flag;
11308     case dw_val_class_die_ref:
11309       if (AT_ref_external (a))
11310         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11311       else
11312         return DW_FORM_ref;
11313     case dw_val_class_fde_ref:
11314       return DW_FORM_data;
11315     case dw_val_class_lbl_id:
11316       return DW_FORM_addr;
11317     case dw_val_class_lineptr:
11318     case dw_val_class_macptr:
11319       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11320     case dw_val_class_str:
11321       return AT_string_form (a);
11322     case dw_val_class_file:
11323       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11324         {
11325         case 1:
11326           return DW_FORM_data1;
11327         case 2:
11328           return DW_FORM_data2;
11329         case 4:
11330           return DW_FORM_data4;
11331         default:
11332           gcc_unreachable ();
11333         }
11334
11335     case dw_val_class_data8:
11336       return DW_FORM_data8;
11337
11338     default:
11339       gcc_unreachable ();
11340     }
11341 }
11342
11343 /* Output the encoding of an attribute value.  */
11344
11345 static void
11346 output_value_format (dw_attr_ref a)
11347 {
11348   enum dwarf_form form = value_format (a);
11349
11350   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11351 }
11352
11353 /* Output the .debug_abbrev section which defines the DIE abbreviation
11354    table.  */
11355
11356 static void
11357 output_abbrev_section (void)
11358 {
11359   unsigned long abbrev_id;
11360
11361   if (abbrev_die_table_in_use == 1)
11362     return;
11363
11364   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11365     {
11366       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11367       unsigned ix;
11368       dw_attr_ref a_attr;
11369
11370       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11371       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11372                                    dwarf_tag_name (abbrev->die_tag));
11373
11374       if (abbrev->die_child != NULL)
11375         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11376       else
11377         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11378
11379       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11380            ix++)
11381         {
11382           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11383                                        dwarf_attr_name (a_attr->dw_attr));
11384           output_value_format (a_attr);
11385         }
11386
11387       dw2_asm_output_data (1, 0, NULL);
11388       dw2_asm_output_data (1, 0, NULL);
11389     }
11390
11391   /* Terminate the table.  */
11392   dw2_asm_output_data (1, 0, NULL);
11393 }
11394
11395 /* Output a symbol we can use to refer to this DIE from another CU.  */
11396
11397 static inline void
11398 output_die_symbol (dw_die_ref die)
11399 {
11400   char *sym = die->die_id.die_symbol;
11401
11402   if (sym == 0)
11403     return;
11404
11405   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11406     /* We make these global, not weak; if the target doesn't support
11407        .linkonce, it doesn't support combining the sections, so debugging
11408        will break.  */
11409     targetm.asm_out.globalize_label (asm_out_file, sym);
11410
11411   ASM_OUTPUT_LABEL (asm_out_file, sym);
11412 }
11413
11414 /* Return a new location list, given the begin and end range, and the
11415    expression.  */
11416
11417 static inline dw_loc_list_ref
11418 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11419               const char *section)
11420 {
11421   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11422
11423   retlist->begin = begin;
11424   retlist->end = end;
11425   retlist->expr = expr;
11426   retlist->section = section;
11427
11428   return retlist;
11429 }
11430
11431 /* Generate a new internal symbol for this location list node, if it
11432    hasn't got one yet.  */
11433
11434 static inline void
11435 gen_llsym (dw_loc_list_ref list)
11436 {
11437   gcc_assert (!list->ll_symbol);
11438   list->ll_symbol = gen_internal_sym ("LLST");
11439 }
11440
11441 /* Output the location list given to us.  */
11442
11443 static void
11444 output_loc_list (dw_loc_list_ref list_head)
11445 {
11446   dw_loc_list_ref curr = list_head;
11447
11448   if (list_head->emitted)
11449     return;
11450   list_head->emitted = true;
11451
11452   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11453
11454   /* Walk the location list, and output each range + expression.  */
11455   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11456     {
11457       unsigned long size;
11458       /* Don't output an entry that starts and ends at the same address.  */
11459       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
11460         continue;
11461       if (!have_multiple_function_sections)
11462         {
11463           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11464                                 "Location list begin address (%s)",
11465                                 list_head->ll_symbol);
11466           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11467                                 "Location list end address (%s)",
11468                                 list_head->ll_symbol);
11469         }
11470       else
11471         {
11472           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11473                                "Location list begin address (%s)",
11474                                list_head->ll_symbol);
11475           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11476                                "Location list end address (%s)",
11477                                list_head->ll_symbol);
11478         }
11479       size = size_of_locs (curr->expr);
11480
11481       /* Output the block length for this list of location operations.  */
11482       gcc_assert (size <= 0xffff);
11483       dw2_asm_output_data (2, size, "%s", "Location expression size");
11484
11485       output_loc_sequence (curr->expr, -1);
11486     }
11487
11488   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11489                        "Location list terminator begin (%s)",
11490                        list_head->ll_symbol);
11491   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11492                        "Location list terminator end (%s)",
11493                        list_head->ll_symbol);
11494 }
11495
11496 /* Output a type signature.  */
11497
11498 static inline void
11499 output_signature (const char *sig, const char *name)
11500 {
11501   int i;
11502
11503   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11504     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11505 }
11506
11507 /* Output the DIE and its attributes.  Called recursively to generate
11508    the definitions of each child DIE.  */
11509
11510 static void
11511 output_die (dw_die_ref die)
11512 {
11513   dw_attr_ref a;
11514   dw_die_ref c;
11515   unsigned long size;
11516   unsigned ix;
11517
11518   /* If someone in another CU might refer to us, set up a symbol for
11519      them to point to.  */
11520   if (! use_debug_types && die->die_id.die_symbol)
11521     output_die_symbol (die);
11522
11523   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11524                                (unsigned long)die->die_offset,
11525                                dwarf_tag_name (die->die_tag));
11526
11527   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11528     {
11529       const char *name = dwarf_attr_name (a->dw_attr);
11530
11531       switch (AT_class (a))
11532         {
11533         case dw_val_class_addr:
11534           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11535           break;
11536
11537         case dw_val_class_offset:
11538           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11539                                "%s", name);
11540           break;
11541
11542         case dw_val_class_range_list:
11543           {
11544             char *p = strchr (ranges_section_label, '\0');
11545
11546             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11547                      a->dw_attr_val.v.val_offset);
11548             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11549                                    debug_ranges_section, "%s", name);
11550             *p = '\0';
11551           }
11552           break;
11553
11554         case dw_val_class_loc:
11555           size = size_of_locs (AT_loc (a));
11556
11557           /* Output the block length for this list of location operations.  */
11558           if (dwarf_version >= 4)
11559             dw2_asm_output_data_uleb128 (size, "%s", name);
11560           else
11561             dw2_asm_output_data (constant_size (size), size, "%s", name);
11562
11563           output_loc_sequence (AT_loc (a), -1);
11564           break;
11565
11566         case dw_val_class_const:
11567           /* ??? It would be slightly more efficient to use a scheme like is
11568              used for unsigned constants below, but gdb 4.x does not sign
11569              extend.  Gdb 5.x does sign extend.  */
11570           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11571           break;
11572
11573         case dw_val_class_unsigned_const:
11574           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11575                                AT_unsigned (a), "%s", name);
11576           break;
11577
11578         case dw_val_class_const_double:
11579           {
11580             unsigned HOST_WIDE_INT first, second;
11581
11582             if (HOST_BITS_PER_WIDE_INT >= 64)
11583               dw2_asm_output_data (1,
11584                                    2 * HOST_BITS_PER_WIDE_INT
11585                                    / HOST_BITS_PER_CHAR,
11586                                    NULL);
11587
11588             if (WORDS_BIG_ENDIAN)
11589               {
11590                 first = a->dw_attr_val.v.val_double.high;
11591                 second = a->dw_attr_val.v.val_double.low;
11592               }
11593             else
11594               {
11595                 first = a->dw_attr_val.v.val_double.low;
11596                 second = a->dw_attr_val.v.val_double.high;
11597               }
11598
11599             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11600                                  first, name);
11601             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11602                                  second, NULL);
11603           }
11604           break;
11605
11606         case dw_val_class_vec:
11607           {
11608             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11609             unsigned int len = a->dw_attr_val.v.val_vec.length;
11610             unsigned int i;
11611             unsigned char *p;
11612
11613             dw2_asm_output_data (constant_size (len * elt_size),
11614                                  len * elt_size, "%s", name);
11615             if (elt_size > sizeof (HOST_WIDE_INT))
11616               {
11617                 elt_size /= 2;
11618                 len *= 2;
11619               }
11620             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11621                  i < len;
11622                  i++, p += elt_size)
11623               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11624                                    "fp or vector constant word %u", i);
11625             break;
11626           }
11627
11628         case dw_val_class_flag:
11629           if (dwarf_version >= 4)
11630             {
11631               /* Currently all add_AT_flag calls pass in 1 as last argument,
11632                  so DW_FORM_flag_present can be used.  If that ever changes,
11633                  we'll need to use DW_FORM_flag and have some optimization
11634                  in build_abbrev_table that will change those to
11635                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11636                  the same abbrev entry.  */
11637               gcc_assert (AT_flag (a) == 1);
11638               if (flag_debug_asm)
11639                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11640                          ASM_COMMENT_START, name);
11641               break;
11642             }
11643           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11644           break;
11645
11646         case dw_val_class_loc_list:
11647           {
11648             char *sym = AT_loc_list (a)->ll_symbol;
11649
11650             gcc_assert (sym);
11651             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11652                                    "%s", name);
11653           }
11654           break;
11655
11656         case dw_val_class_die_ref:
11657           if (AT_ref_external (a))
11658             {
11659               if (use_debug_types)
11660                 {
11661                   comdat_type_node_ref type_node =
11662                     AT_ref (a)->die_id.die_type_node;
11663
11664                   gcc_assert (type_node);
11665                   output_signature (type_node->signature, name);
11666                 }
11667               else
11668                 {
11669                   char *sym = AT_ref (a)->die_id.die_symbol;
11670                   int size;
11671
11672                   gcc_assert (sym);
11673                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11674                      length, whereas in DWARF3 it's always sized as an
11675                      offset.  */
11676                   if (dwarf_version == 2)
11677                     size = DWARF2_ADDR_SIZE;
11678                   else
11679                     size = DWARF_OFFSET_SIZE;
11680                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11681                                          name);
11682                 }
11683             }
11684           else
11685             {
11686               gcc_assert (AT_ref (a)->die_offset);
11687               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11688                                    "%s", name);
11689             }
11690           break;
11691
11692         case dw_val_class_fde_ref:
11693           {
11694             char l1[20];
11695
11696             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11697                                          a->dw_attr_val.v.val_fde_index * 2);
11698             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11699                                    "%s", name);
11700           }
11701           break;
11702
11703         case dw_val_class_vms_delta:
11704           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11705                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11706                                     "%s", name);
11707           break;
11708
11709         case dw_val_class_lbl_id:
11710           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11711           break;
11712
11713         case dw_val_class_lineptr:
11714           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11715                                  debug_line_section, "%s", name);
11716           break;
11717
11718         case dw_val_class_macptr:
11719           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11720                                  debug_macinfo_section, "%s", name);
11721           break;
11722
11723         case dw_val_class_str:
11724           if (AT_string_form (a) == DW_FORM_strp)
11725             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11726                                    a->dw_attr_val.v.val_str->label,
11727                                    debug_str_section,
11728                                    "%s: \"%s\"", name, AT_string (a));
11729           else
11730             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11731           break;
11732
11733         case dw_val_class_file:
11734           {
11735             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11736
11737             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11738                                  a->dw_attr_val.v.val_file->filename);
11739             break;
11740           }
11741
11742         case dw_val_class_data8:
11743           {
11744             int i;
11745
11746             for (i = 0; i < 8; i++)
11747               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11748                                    i == 0 ? "%s" : NULL, name);
11749             break;
11750           }
11751
11752         default:
11753           gcc_unreachable ();
11754         }
11755     }
11756
11757   FOR_EACH_CHILD (die, c, output_die (c));
11758
11759   /* Add null byte to terminate sibling list.  */
11760   if (die->die_child != NULL)
11761     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11762                          (unsigned long) die->die_offset);
11763 }
11764
11765 /* Output the compilation unit that appears at the beginning of the
11766    .debug_info section, and precedes the DIE descriptions.  */
11767
11768 static void
11769 output_compilation_unit_header (void)
11770 {
11771   int ver = dwarf_version;
11772
11773   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11774     dw2_asm_output_data (4, 0xffffffff,
11775       "Initial length escape value indicating 64-bit DWARF extension");
11776   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11777                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11778                        "Length of Compilation Unit Info");
11779   dw2_asm_output_data (2, ver, "DWARF version number");
11780   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11781                          debug_abbrev_section,
11782                          "Offset Into Abbrev. Section");
11783   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11784 }
11785
11786 /* Output the compilation unit DIE and its children.  */
11787
11788 static void
11789 output_comp_unit (dw_die_ref die, int output_if_empty)
11790 {
11791   const char *secname;
11792   char *oldsym, *tmp;
11793
11794   /* Unless we are outputting main CU, we may throw away empty ones.  */
11795   if (!output_if_empty && die->die_child == NULL)
11796     return;
11797
11798   /* Even if there are no children of this DIE, we must output the information
11799      about the compilation unit.  Otherwise, on an empty translation unit, we
11800      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11801      will then complain when examining the file.  First mark all the DIEs in
11802      this CU so we know which get local refs.  */
11803   mark_dies (die);
11804
11805   build_abbrev_table (die);
11806
11807   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11808   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11809   calc_die_sizes (die);
11810
11811   oldsym = die->die_id.die_symbol;
11812   if (oldsym)
11813     {
11814       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11815
11816       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11817       secname = tmp;
11818       die->die_id.die_symbol = NULL;
11819       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11820     }
11821   else
11822     {
11823       switch_to_section (debug_info_section);
11824       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11825       info_section_emitted = true;
11826     }
11827
11828   /* Output debugging information.  */
11829   output_compilation_unit_header ();
11830   output_die (die);
11831
11832   /* Leave the marks on the main CU, so we can check them in
11833      output_pubnames.  */
11834   if (oldsym)
11835     {
11836       unmark_dies (die);
11837       die->die_id.die_symbol = oldsym;
11838     }
11839 }
11840
11841 /* Output a comdat type unit DIE and its children.  */
11842
11843 static void
11844 output_comdat_type_unit (comdat_type_node *node)
11845 {
11846   const char *secname;
11847   char *tmp;
11848   int i;
11849 #if defined (OBJECT_FORMAT_ELF)
11850   tree comdat_key;
11851 #endif
11852
11853   /* First mark all the DIEs in this CU so we know which get local refs.  */
11854   mark_dies (node->root_die);
11855
11856   build_abbrev_table (node->root_die);
11857
11858   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11859   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11860   calc_die_sizes (node->root_die);
11861
11862 #if defined (OBJECT_FORMAT_ELF)
11863   secname = ".debug_types";
11864   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11865   sprintf (tmp, "wt.");
11866   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11867     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11868   comdat_key = get_identifier (tmp);
11869   targetm.asm_out.named_section (secname,
11870                                  SECTION_DEBUG | SECTION_LINKONCE,
11871                                  comdat_key);
11872 #else
11873   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11874   sprintf (tmp, ".gnu.linkonce.wt.");
11875   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11876     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11877   secname = tmp;
11878   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11879 #endif
11880
11881   /* Output debugging information.  */
11882   output_compilation_unit_header ();
11883   output_signature (node->signature, "Type Signature");
11884   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11885                        "Offset to Type DIE");
11886   output_die (node->root_die);
11887
11888   unmark_dies (node->root_die);
11889 }
11890
11891 /* Return the DWARF2/3 pubname associated with a decl.  */
11892
11893 static const char *
11894 dwarf2_name (tree decl, int scope)
11895 {
11896   if (DECL_NAMELESS (decl))
11897     return NULL;
11898   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11899 }
11900
11901 /* Add a new entry to .debug_pubnames if appropriate.  */
11902
11903 static void
11904 add_pubname_string (const char *str, dw_die_ref die)
11905 {
11906   if (targetm.want_debug_pub_sections)
11907     {
11908       pubname_entry e;
11909
11910       e.die = die;
11911       e.name = xstrdup (str);
11912       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11913     }
11914 }
11915
11916 static void
11917 add_pubname (tree decl, dw_die_ref die)
11918 {
11919   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11920     {
11921       const char *name = dwarf2_name (decl, 1);
11922       if (name)
11923         add_pubname_string (name, die);
11924     }
11925 }
11926
11927 /* Add a new entry to .debug_pubtypes if appropriate.  */
11928
11929 static void
11930 add_pubtype (tree decl, dw_die_ref die)
11931 {
11932   pubname_entry e;
11933
11934   if (!targetm.want_debug_pub_sections)
11935     return;
11936
11937   e.name = NULL;
11938   if ((TREE_PUBLIC (decl)
11939        || is_cu_die (die->die_parent))
11940       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11941     {
11942       e.die = die;
11943       if (TYPE_P (decl))
11944         {
11945           if (TYPE_NAME (decl))
11946             {
11947               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11948                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11949               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11950                        && DECL_NAME (TYPE_NAME (decl)))
11951                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11952               else
11953                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11954             }
11955         }
11956       else
11957         {
11958           e.name = dwarf2_name (decl, 1);
11959           if (e.name)
11960             e.name = xstrdup (e.name);
11961         }
11962
11963       /* If we don't have a name for the type, there's no point in adding
11964          it to the table.  */
11965       if (e.name && e.name[0] != '\0')
11966         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11967     }
11968 }
11969
11970 /* Output the public names table used to speed up access to externally
11971    visible names; or the public types table used to find type definitions.  */
11972
11973 static void
11974 output_pubnames (VEC (pubname_entry, gc) * names)
11975 {
11976   unsigned i;
11977   unsigned long pubnames_length = size_of_pubnames (names);
11978   pubname_ref pub;
11979
11980   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11981     dw2_asm_output_data (4, 0xffffffff,
11982       "Initial length escape value indicating 64-bit DWARF extension");
11983   if (names == pubname_table)
11984     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11985                          "Length of Public Names Info");
11986   else
11987     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11988                          "Length of Public Type Names Info");
11989   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11990   dw2_asm_output_data (2, 2, "DWARF Version");
11991   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11992                          debug_info_section,
11993                          "Offset of Compilation Unit Info");
11994   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11995                        "Compilation Unit Length");
11996
11997   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11998     {
11999       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
12000       if (names == pubname_table)
12001         gcc_assert (pub->die->die_mark);
12002
12003       if (names != pubtype_table
12004           || pub->die->die_offset != 0
12005           || !flag_eliminate_unused_debug_types)
12006         {
12007           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
12008                                "DIE offset");
12009
12010           dw2_asm_output_nstring (pub->name, -1, "external name");
12011         }
12012     }
12013
12014   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
12015 }
12016
12017 /* Output the information that goes into the .debug_aranges table.
12018    Namely, define the beginning and ending address range of the
12019    text section generated for this compilation unit.  */
12020
12021 static void
12022 output_aranges (unsigned long aranges_length)
12023 {
12024   unsigned i;
12025
12026   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12027     dw2_asm_output_data (4, 0xffffffff,
12028       "Initial length escape value indicating 64-bit DWARF extension");
12029   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
12030                        "Length of Address Ranges Info");
12031   /* Version number for aranges is still 2, even in DWARF3.  */
12032   dw2_asm_output_data (2, 2, "DWARF Version");
12033   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12034                          debug_info_section,
12035                          "Offset of Compilation Unit Info");
12036   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
12037   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
12038
12039   /* We need to align to twice the pointer size here.  */
12040   if (DWARF_ARANGES_PAD_SIZE)
12041     {
12042       /* Pad using a 2 byte words so that padding is correct for any
12043          pointer size.  */
12044       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
12045                            2 * DWARF2_ADDR_SIZE);
12046       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
12047         dw2_asm_output_data (2, 0, NULL);
12048     }
12049
12050   /* It is necessary not to output these entries if the sections were
12051      not used; if the sections were not used, the length will be 0 and
12052      the address may end up as 0 if the section is discarded by ld
12053      --gc-sections, leaving an invalid (0, 0) entry that can be
12054      confused with the terminator.  */
12055   if (text_section_used)
12056     {
12057       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
12058       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
12059                             text_section_label, "Length");
12060     }
12061   if (cold_text_section_used)
12062     {
12063       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
12064                            "Address");
12065       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
12066                             cold_text_section_label, "Length");
12067     }
12068
12069   if (have_multiple_function_sections)
12070     {
12071       unsigned fde_idx = 0;
12072
12073       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
12074         {
12075           dw_fde_ref fde = &fde_table[fde_idx];
12076
12077           if (!fde->in_std_section)
12078             {
12079               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
12080                                    "Address");
12081               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
12082                                     fde->dw_fde_begin, "Length");
12083             }
12084           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
12085             {
12086               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
12087                                    "Address");
12088               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
12089                                     fde->dw_fde_second_begin, "Length");
12090             }
12091         }
12092     }
12093
12094   /* Output the terminator words.  */
12095   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12096   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12097 }
12098
12099 /* Add a new entry to .debug_ranges.  Return the offset at which it
12100    was placed.  */
12101
12102 static unsigned int
12103 add_ranges_num (int num)
12104 {
12105   unsigned int in_use = ranges_table_in_use;
12106
12107   if (in_use == ranges_table_allocated)
12108     {
12109       ranges_table_allocated += RANGES_TABLE_INCREMENT;
12110       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
12111                                     ranges_table_allocated);
12112       memset (ranges_table + ranges_table_in_use, 0,
12113               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
12114     }
12115
12116   ranges_table[in_use].num = num;
12117   ranges_table_in_use = in_use + 1;
12118
12119   return in_use * 2 * DWARF2_ADDR_SIZE;
12120 }
12121
12122 /* Add a new entry to .debug_ranges corresponding to a block, or a
12123    range terminator if BLOCK is NULL.  */
12124
12125 static unsigned int
12126 add_ranges (const_tree block)
12127 {
12128   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
12129 }
12130
12131 /* Add a new entry to .debug_ranges corresponding to a pair of
12132    labels.  */
12133
12134 static void
12135 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
12136                       bool *added)
12137 {
12138   unsigned int in_use = ranges_by_label_in_use;
12139   unsigned int offset;
12140
12141   if (in_use == ranges_by_label_allocated)
12142     {
12143       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
12144       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
12145                                        ranges_by_label,
12146                                        ranges_by_label_allocated);
12147       memset (ranges_by_label + ranges_by_label_in_use, 0,
12148               RANGES_TABLE_INCREMENT
12149               * sizeof (struct dw_ranges_by_label_struct));
12150     }
12151
12152   ranges_by_label[in_use].begin = begin;
12153   ranges_by_label[in_use].end = end;
12154   ranges_by_label_in_use = in_use + 1;
12155
12156   offset = add_ranges_num (-(int)in_use - 1);
12157   if (!*added)
12158     {
12159       add_AT_range_list (die, DW_AT_ranges, offset);
12160       *added = true;
12161     }
12162 }
12163
12164 static void
12165 output_ranges (void)
12166 {
12167   unsigned i;
12168   static const char *const start_fmt = "Offset %#x";
12169   const char *fmt = start_fmt;
12170
12171   for (i = 0; i < ranges_table_in_use; i++)
12172     {
12173       int block_num = ranges_table[i].num;
12174
12175       if (block_num > 0)
12176         {
12177           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12178           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12179
12180           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12181           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12182
12183           /* If all code is in the text section, then the compilation
12184              unit base address defaults to DW_AT_low_pc, which is the
12185              base of the text section.  */
12186           if (!have_multiple_function_sections)
12187             {
12188               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12189                                     text_section_label,
12190                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12191               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12192                                     text_section_label, NULL);
12193             }
12194
12195           /* Otherwise, the compilation unit base address is zero,
12196              which allows us to use absolute addresses, and not worry
12197              about whether the target supports cross-section
12198              arithmetic.  */
12199           else
12200             {
12201               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12202                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12203               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12204             }
12205
12206           fmt = NULL;
12207         }
12208
12209       /* Negative block_num stands for an index into ranges_by_label.  */
12210       else if (block_num < 0)
12211         {
12212           int lab_idx = - block_num - 1;
12213
12214           if (!have_multiple_function_sections)
12215             {
12216               gcc_unreachable ();
12217 #if 0
12218               /* If we ever use add_ranges_by_labels () for a single
12219                  function section, all we have to do is to take out
12220                  the #if 0 above.  */
12221               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12222                                     ranges_by_label[lab_idx].begin,
12223                                     text_section_label,
12224                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12225               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12226                                     ranges_by_label[lab_idx].end,
12227                                     text_section_label, NULL);
12228 #endif
12229             }
12230           else
12231             {
12232               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12233                                    ranges_by_label[lab_idx].begin,
12234                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12235               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12236                                    ranges_by_label[lab_idx].end,
12237                                    NULL);
12238             }
12239         }
12240       else
12241         {
12242           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12243           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12244           fmt = start_fmt;
12245         }
12246     }
12247 }
12248
12249 /* Data structure containing information about input files.  */
12250 struct file_info
12251 {
12252   const char *path;     /* Complete file name.  */
12253   const char *fname;    /* File name part.  */
12254   int length;           /* Length of entire string.  */
12255   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
12256   int dir_idx;          /* Index in directory table.  */
12257 };
12258
12259 /* Data structure containing information about directories with source
12260    files.  */
12261 struct dir_info
12262 {
12263   const char *path;     /* Path including directory name.  */
12264   int length;           /* Path length.  */
12265   int prefix;           /* Index of directory entry which is a prefix.  */
12266   int count;            /* Number of files in this directory.  */
12267   int dir_idx;          /* Index of directory used as base.  */
12268 };
12269
12270 /* Callback function for file_info comparison.  We sort by looking at
12271    the directories in the path.  */
12272
12273 static int
12274 file_info_cmp (const void *p1, const void *p2)
12275 {
12276   const struct file_info *const s1 = (const struct file_info *) p1;
12277   const struct file_info *const s2 = (const struct file_info *) p2;
12278   const unsigned char *cp1;
12279   const unsigned char *cp2;
12280
12281   /* Take care of file names without directories.  We need to make sure that
12282      we return consistent values to qsort since some will get confused if
12283      we return the same value when identical operands are passed in opposite
12284      orders.  So if neither has a directory, return 0 and otherwise return
12285      1 or -1 depending on which one has the directory.  */
12286   if ((s1->path == s1->fname || s2->path == s2->fname))
12287     return (s2->path == s2->fname) - (s1->path == s1->fname);
12288
12289   cp1 = (const unsigned char *) s1->path;
12290   cp2 = (const unsigned char *) s2->path;
12291
12292   while (1)
12293     {
12294       ++cp1;
12295       ++cp2;
12296       /* Reached the end of the first path?  If so, handle like above.  */
12297       if ((cp1 == (const unsigned char *) s1->fname)
12298           || (cp2 == (const unsigned char *) s2->fname))
12299         return ((cp2 == (const unsigned char *) s2->fname)
12300                 - (cp1 == (const unsigned char *) s1->fname));
12301
12302       /* Character of current path component the same?  */
12303       else if (*cp1 != *cp2)
12304         return *cp1 - *cp2;
12305     }
12306 }
12307
12308 struct file_name_acquire_data
12309 {
12310   struct file_info *files;
12311   int used_files;
12312   int max_files;
12313 };
12314
12315 /* Traversal function for the hash table.  */
12316
12317 static int
12318 file_name_acquire (void ** slot, void *data)
12319 {
12320   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12321   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12322   struct file_info *fi;
12323   const char *f;
12324
12325   gcc_assert (fnad->max_files >= d->emitted_number);
12326
12327   if (! d->emitted_number)
12328     return 1;
12329
12330   gcc_assert (fnad->max_files != fnad->used_files);
12331
12332   fi = fnad->files + fnad->used_files++;
12333
12334   /* Skip all leading "./".  */
12335   f = d->filename;
12336   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12337     f += 2;
12338
12339   /* Create a new array entry.  */
12340   fi->path = f;
12341   fi->length = strlen (f);
12342   fi->file_idx = d;
12343
12344   /* Search for the file name part.  */
12345   f = strrchr (f, DIR_SEPARATOR);
12346 #if defined (DIR_SEPARATOR_2)
12347   {
12348     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12349
12350     if (g != NULL)
12351       {
12352         if (f == NULL || f < g)
12353           f = g;
12354       }
12355   }
12356 #endif
12357
12358   fi->fname = f == NULL ? fi->path : f + 1;
12359   return 1;
12360 }
12361
12362 /* Output the directory table and the file name table.  We try to minimize
12363    the total amount of memory needed.  A heuristic is used to avoid large
12364    slowdowns with many input files.  */
12365
12366 static void
12367 output_file_names (void)
12368 {
12369   struct file_name_acquire_data fnad;
12370   int numfiles;
12371   struct file_info *files;
12372   struct dir_info *dirs;
12373   int *saved;
12374   int *savehere;
12375   int *backmap;
12376   int ndirs;
12377   int idx_offset;
12378   int i;
12379
12380   if (!last_emitted_file)
12381     {
12382       dw2_asm_output_data (1, 0, "End directory table");
12383       dw2_asm_output_data (1, 0, "End file name table");
12384       return;
12385     }
12386
12387   numfiles = last_emitted_file->emitted_number;
12388
12389   /* Allocate the various arrays we need.  */
12390   files = XALLOCAVEC (struct file_info, numfiles);
12391   dirs = XALLOCAVEC (struct dir_info, numfiles);
12392
12393   fnad.files = files;
12394   fnad.used_files = 0;
12395   fnad.max_files = numfiles;
12396   htab_traverse (file_table, file_name_acquire, &fnad);
12397   gcc_assert (fnad.used_files == fnad.max_files);
12398
12399   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12400
12401   /* Find all the different directories used.  */
12402   dirs[0].path = files[0].path;
12403   dirs[0].length = files[0].fname - files[0].path;
12404   dirs[0].prefix = -1;
12405   dirs[0].count = 1;
12406   dirs[0].dir_idx = 0;
12407   files[0].dir_idx = 0;
12408   ndirs = 1;
12409
12410   for (i = 1; i < numfiles; i++)
12411     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12412         && memcmp (dirs[ndirs - 1].path, files[i].path,
12413                    dirs[ndirs - 1].length) == 0)
12414       {
12415         /* Same directory as last entry.  */
12416         files[i].dir_idx = ndirs - 1;
12417         ++dirs[ndirs - 1].count;
12418       }
12419     else
12420       {
12421         int j;
12422
12423         /* This is a new directory.  */
12424         dirs[ndirs].path = files[i].path;
12425         dirs[ndirs].length = files[i].fname - files[i].path;
12426         dirs[ndirs].count = 1;
12427         dirs[ndirs].dir_idx = ndirs;
12428         files[i].dir_idx = ndirs;
12429
12430         /* Search for a prefix.  */
12431         dirs[ndirs].prefix = -1;
12432         for (j = 0; j < ndirs; j++)
12433           if (dirs[j].length < dirs[ndirs].length
12434               && dirs[j].length > 1
12435               && (dirs[ndirs].prefix == -1
12436                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12437               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12438             dirs[ndirs].prefix = j;
12439
12440         ++ndirs;
12441       }
12442
12443   /* Now to the actual work.  We have to find a subset of the directories which
12444      allow expressing the file name using references to the directory table
12445      with the least amount of characters.  We do not do an exhaustive search
12446      where we would have to check out every combination of every single
12447      possible prefix.  Instead we use a heuristic which provides nearly optimal
12448      results in most cases and never is much off.  */
12449   saved = XALLOCAVEC (int, ndirs);
12450   savehere = XALLOCAVEC (int, ndirs);
12451
12452   memset (saved, '\0', ndirs * sizeof (saved[0]));
12453   for (i = 0; i < ndirs; i++)
12454     {
12455       int j;
12456       int total;
12457
12458       /* We can always save some space for the current directory.  But this
12459          does not mean it will be enough to justify adding the directory.  */
12460       savehere[i] = dirs[i].length;
12461       total = (savehere[i] - saved[i]) * dirs[i].count;
12462
12463       for (j = i + 1; j < ndirs; j++)
12464         {
12465           savehere[j] = 0;
12466           if (saved[j] < dirs[i].length)
12467             {
12468               /* Determine whether the dirs[i] path is a prefix of the
12469                  dirs[j] path.  */
12470               int k;
12471
12472               k = dirs[j].prefix;
12473               while (k != -1 && k != (int) i)
12474                 k = dirs[k].prefix;
12475
12476               if (k == (int) i)
12477                 {
12478                   /* Yes it is.  We can possibly save some memory by
12479                      writing the filenames in dirs[j] relative to
12480                      dirs[i].  */
12481                   savehere[j] = dirs[i].length;
12482                   total += (savehere[j] - saved[j]) * dirs[j].count;
12483                 }
12484             }
12485         }
12486
12487       /* Check whether we can save enough to justify adding the dirs[i]
12488          directory.  */
12489       if (total > dirs[i].length + 1)
12490         {
12491           /* It's worthwhile adding.  */
12492           for (j = i; j < ndirs; j++)
12493             if (savehere[j] > 0)
12494               {
12495                 /* Remember how much we saved for this directory so far.  */
12496                 saved[j] = savehere[j];
12497
12498                 /* Remember the prefix directory.  */
12499                 dirs[j].dir_idx = i;
12500               }
12501         }
12502     }
12503
12504   /* Emit the directory name table.  */
12505   idx_offset = dirs[0].length > 0 ? 1 : 0;
12506   for (i = 1 - idx_offset; i < ndirs; i++)
12507     dw2_asm_output_nstring (dirs[i].path,
12508                             dirs[i].length
12509                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12510                             "Directory Entry: %#x", i + idx_offset);
12511
12512   dw2_asm_output_data (1, 0, "End directory table");
12513
12514   /* We have to emit them in the order of emitted_number since that's
12515      used in the debug info generation.  To do this efficiently we
12516      generate a back-mapping of the indices first.  */
12517   backmap = XALLOCAVEC (int, numfiles);
12518   for (i = 0; i < numfiles; i++)
12519     backmap[files[i].file_idx->emitted_number - 1] = i;
12520
12521   /* Now write all the file names.  */
12522   for (i = 0; i < numfiles; i++)
12523     {
12524       int file_idx = backmap[i];
12525       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12526
12527 #ifdef VMS_DEBUGGING_INFO
12528 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12529
12530       /* Setting these fields can lead to debugger miscomparisons,
12531          but VMS Debug requires them to be set correctly.  */
12532
12533       int ver;
12534       long long cdt;
12535       long siz;
12536       int maxfilelen = strlen (files[file_idx].path)
12537                                + dirs[dir_idx].length
12538                                + MAX_VMS_VERSION_LEN + 1;
12539       char *filebuf = XALLOCAVEC (char, maxfilelen);
12540
12541       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12542       snprintf (filebuf, maxfilelen, "%s;%d",
12543                 files[file_idx].path + dirs[dir_idx].length, ver);
12544
12545       dw2_asm_output_nstring
12546         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12547
12548       /* Include directory index.  */
12549       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12550
12551       /* Modification time.  */
12552       dw2_asm_output_data_uleb128
12553         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12554           ? cdt : 0,
12555          NULL);
12556
12557       /* File length in bytes.  */
12558       dw2_asm_output_data_uleb128
12559         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12560           ? siz : 0,
12561          NULL);
12562 #else
12563       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12564                               "File Entry: %#x", (unsigned) i + 1);
12565
12566       /* Include directory index.  */
12567       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12568
12569       /* Modification time.  */
12570       dw2_asm_output_data_uleb128 (0, NULL);
12571
12572       /* File length in bytes.  */
12573       dw2_asm_output_data_uleb128 (0, NULL);
12574 #endif /* VMS_DEBUGGING_INFO */
12575     }
12576
12577   dw2_asm_output_data (1, 0, "End file name table");
12578 }
12579
12580
12581 /* Output one line number table into the .debug_line section.  */
12582
12583 static void
12584 output_one_line_info_table (dw_line_info_table *table)
12585 {
12586   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12587   unsigned int current_line = 1;
12588   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12589   dw_line_info_entry *ent;
12590   size_t i;
12591
12592   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
12593     {
12594       switch (ent->opcode)
12595         {
12596         case LI_set_address:
12597           /* ??? Unfortunately, we have little choice here currently, and
12598              must always use the most general form.  GCC does not know the
12599              address delta itself, so we can't use DW_LNS_advance_pc.  Many
12600              ports do have length attributes which will give an upper bound
12601              on the address range.  We could perhaps use length attributes
12602              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
12603           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12604
12605           /* This can handle any delta.  This takes
12606              4+DWARF2_ADDR_SIZE bytes.  */
12607           dw2_asm_output_data (1, 0, "set address %s", line_label);
12608           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12609           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12610           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12611           break;
12612
12613         case LI_set_line:
12614           if (ent->val == current_line)
12615             {
12616               /* We still need to start a new row, so output a copy insn.  */
12617               dw2_asm_output_data (1, DW_LNS_copy,
12618                                    "copy line %u", current_line);
12619             }
12620           else
12621             {
12622               int line_offset = ent->val - current_line;
12623               int line_delta = line_offset - DWARF_LINE_BASE;
12624
12625               current_line = ent->val;
12626               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12627                 {
12628                   /* This can handle deltas from -10 to 234, using the current
12629                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12630                      This takes 1 byte.  */
12631                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12632                                        "line %u", current_line);
12633                 }
12634               else
12635                 {
12636                   /* This can handle any delta.  This takes at least 4 bytes,
12637                      depending on the value being encoded.  */
12638                   dw2_asm_output_data (1, DW_LNS_advance_line,
12639                                        "advance to line %u", current_line);
12640                   dw2_asm_output_data_sleb128 (line_offset, NULL);
12641                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
12642                 }
12643             }
12644           break;
12645
12646         case LI_set_file:
12647           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12648           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12649           break;
12650
12651         case LI_set_column:
12652           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12653           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12654           break;
12655
12656         case LI_negate_stmt:
12657           current_is_stmt = !current_is_stmt;
12658           dw2_asm_output_data (1, DW_LNS_negate_stmt,
12659                                "is_stmt %d", current_is_stmt);
12660           break;
12661
12662         case LI_set_prologue_end:
12663           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12664                                "set prologue end");
12665           break;
12666           
12667         case LI_set_epilogue_begin:
12668           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12669                                "set epilogue begin");
12670           break;
12671
12672         case LI_set_discriminator:
12673           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12674           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12675           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12676           dw2_asm_output_data_uleb128 (ent->val, NULL);
12677           break;
12678         }
12679     }
12680
12681   /* Emit debug info for the address of the end of the table.  */
12682   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12683   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12684   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12685   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12686
12687   dw2_asm_output_data (1, 0, "end sequence");
12688   dw2_asm_output_data_uleb128 (1, NULL);
12689   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12690 }
12691
12692 /* Output the source line number correspondence information.  This
12693    information goes into the .debug_line section.  */
12694
12695 static void
12696 output_line_info (void)
12697 {
12698   char l1[20], l2[20], p1[20], p2[20];
12699   int ver = dwarf_version;
12700   bool saw_one = false;
12701   int opc;
12702
12703   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12704   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12705   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12706   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12707
12708   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12709     dw2_asm_output_data (4, 0xffffffff,
12710       "Initial length escape value indicating 64-bit DWARF extension");
12711   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12712                         "Length of Source Line Info");
12713   ASM_OUTPUT_LABEL (asm_out_file, l1);
12714
12715   dw2_asm_output_data (2, ver, "DWARF Version");
12716   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12717   ASM_OUTPUT_LABEL (asm_out_file, p1);
12718
12719   /* Define the architecture-dependent minimum instruction length (in bytes).
12720      In this implementation of DWARF, this field is used for information
12721      purposes only.  Since GCC generates assembly language, we have no
12722      a priori knowledge of how many instruction bytes are generated for each
12723      source line, and therefore can use only the DW_LNE_set_address and
12724      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
12725      this as '1', which is "correct enough" for all architectures,
12726      and don't let the target override.  */
12727   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12728
12729   if (ver >= 4)
12730     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12731                          "Maximum Operations Per Instruction");
12732   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12733                        "Default is_stmt_start flag");
12734   dw2_asm_output_data (1, DWARF_LINE_BASE,
12735                        "Line Base Value (Special Opcodes)");
12736   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12737                        "Line Range Value (Special Opcodes)");
12738   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12739                        "Special Opcode Base");
12740
12741   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12742     {
12743       int n_op_args;
12744       switch (opc)
12745         {
12746         case DW_LNS_advance_pc:
12747         case DW_LNS_advance_line:
12748         case DW_LNS_set_file:
12749         case DW_LNS_set_column:
12750         case DW_LNS_fixed_advance_pc:
12751         case DW_LNS_set_isa:
12752           n_op_args = 1;
12753           break;
12754         default:
12755           n_op_args = 0;
12756           break;
12757         }
12758
12759       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12760                            opc, n_op_args);
12761     }
12762
12763   /* Write out the information about the files we use.  */
12764   output_file_names ();
12765   ASM_OUTPUT_LABEL (asm_out_file, p2);
12766
12767   if (separate_line_info)
12768     {
12769       dw_line_info_table *table;
12770       size_t i;
12771
12772       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
12773         if (table->in_use)
12774           {
12775             output_one_line_info_table (table);
12776             saw_one = true;
12777           }
12778     }
12779   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12780     {
12781       output_one_line_info_table (cold_text_section_line_info);
12782       saw_one = true;
12783     }
12784
12785   /* ??? Some Darwin linkers crash on a .debug_line section with no
12786      sequences.  Further, merely a DW_LNE_end_sequence entry is not
12787      sufficient -- the address column must also be initialized.
12788      Make sure to output at least one set_address/end_sequence pair,
12789      choosing .text since that section is always present.  */
12790   if (text_section_line_info->in_use || !saw_one)
12791     output_one_line_info_table (text_section_line_info);
12792
12793   /* Output the marker for the end of the line number info.  */
12794   ASM_OUTPUT_LABEL (asm_out_file, l2);
12795 }
12796 \f
12797 /* Given a pointer to a tree node for some base type, return a pointer to
12798    a DIE that describes the given type.
12799
12800    This routine must only be called for GCC type nodes that correspond to
12801    Dwarf base (fundamental) types.  */
12802
12803 static dw_die_ref
12804 base_type_die (tree type)
12805 {
12806   dw_die_ref base_type_result;
12807   enum dwarf_type encoding;
12808
12809   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12810     return 0;
12811
12812   /* If this is a subtype that should not be emitted as a subrange type,
12813      use the base type.  See subrange_type_for_debug_p.  */
12814   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12815     type = TREE_TYPE (type);
12816
12817   switch (TREE_CODE (type))
12818     {
12819     case INTEGER_TYPE:
12820       if ((dwarf_version >= 4 || !dwarf_strict)
12821           && TYPE_NAME (type)
12822           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12823           && DECL_IS_BUILTIN (TYPE_NAME (type))
12824           && DECL_NAME (TYPE_NAME (type)))
12825         {
12826           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12827           if (strcmp (name, "char16_t") == 0
12828               || strcmp (name, "char32_t") == 0)
12829             {
12830               encoding = DW_ATE_UTF;
12831               break;
12832             }
12833         }
12834       if (TYPE_STRING_FLAG (type))
12835         {
12836           if (TYPE_UNSIGNED (type))
12837             encoding = DW_ATE_unsigned_char;
12838           else
12839             encoding = DW_ATE_signed_char;
12840         }
12841       else if (TYPE_UNSIGNED (type))
12842         encoding = DW_ATE_unsigned;
12843       else
12844         encoding = DW_ATE_signed;
12845       break;
12846
12847     case REAL_TYPE:
12848       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12849         {
12850           if (dwarf_version >= 3 || !dwarf_strict)
12851             encoding = DW_ATE_decimal_float;
12852           else
12853             encoding = DW_ATE_lo_user;
12854         }
12855       else
12856         encoding = DW_ATE_float;
12857       break;
12858
12859     case FIXED_POINT_TYPE:
12860       if (!(dwarf_version >= 3 || !dwarf_strict))
12861         encoding = DW_ATE_lo_user;
12862       else if (TYPE_UNSIGNED (type))
12863         encoding = DW_ATE_unsigned_fixed;
12864       else
12865         encoding = DW_ATE_signed_fixed;
12866       break;
12867
12868       /* Dwarf2 doesn't know anything about complex ints, so use
12869          a user defined type for it.  */
12870     case COMPLEX_TYPE:
12871       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12872         encoding = DW_ATE_complex_float;
12873       else
12874         encoding = DW_ATE_lo_user;
12875       break;
12876
12877     case BOOLEAN_TYPE:
12878       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12879       encoding = DW_ATE_boolean;
12880       break;
12881
12882     default:
12883       /* No other TREE_CODEs are Dwarf fundamental types.  */
12884       gcc_unreachable ();
12885     }
12886
12887   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12888
12889   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12890                    int_size_in_bytes (type));
12891   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12892
12893   return base_type_result;
12894 }
12895
12896 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12897    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12898
12899 static inline int
12900 is_base_type (tree type)
12901 {
12902   switch (TREE_CODE (type))
12903     {
12904     case ERROR_MARK:
12905     case VOID_TYPE:
12906     case INTEGER_TYPE:
12907     case REAL_TYPE:
12908     case FIXED_POINT_TYPE:
12909     case COMPLEX_TYPE:
12910     case BOOLEAN_TYPE:
12911       return 1;
12912
12913     case ARRAY_TYPE:
12914     case RECORD_TYPE:
12915     case UNION_TYPE:
12916     case QUAL_UNION_TYPE:
12917     case ENUMERAL_TYPE:
12918     case FUNCTION_TYPE:
12919     case METHOD_TYPE:
12920     case POINTER_TYPE:
12921     case REFERENCE_TYPE:
12922     case NULLPTR_TYPE:
12923     case OFFSET_TYPE:
12924     case LANG_TYPE:
12925     case VECTOR_TYPE:
12926       return 0;
12927
12928     default:
12929       gcc_unreachable ();
12930     }
12931
12932   return 0;
12933 }
12934
12935 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12936    node, return the size in bits for the type if it is a constant, or else
12937    return the alignment for the type if the type's size is not constant, or
12938    else return BITS_PER_WORD if the type actually turns out to be an
12939    ERROR_MARK node.  */
12940
12941 static inline unsigned HOST_WIDE_INT
12942 simple_type_size_in_bits (const_tree type)
12943 {
12944   if (TREE_CODE (type) == ERROR_MARK)
12945     return BITS_PER_WORD;
12946   else if (TYPE_SIZE (type) == NULL_TREE)
12947     return 0;
12948   else if (host_integerp (TYPE_SIZE (type), 1))
12949     return tree_low_cst (TYPE_SIZE (type), 1);
12950   else
12951     return TYPE_ALIGN (type);
12952 }
12953
12954 /* Similarly, but return a double_int instead of UHWI.  */
12955
12956 static inline double_int
12957 double_int_type_size_in_bits (const_tree type)
12958 {
12959   if (TREE_CODE (type) == ERROR_MARK)
12960     return uhwi_to_double_int (BITS_PER_WORD);
12961   else if (TYPE_SIZE (type) == NULL_TREE)
12962     return double_int_zero;
12963   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12964     return tree_to_double_int (TYPE_SIZE (type));
12965   else
12966     return uhwi_to_double_int (TYPE_ALIGN (type));
12967 }
12968
12969 /*  Given a pointer to a tree node for a subrange type, return a pointer
12970     to a DIE that describes the given type.  */
12971
12972 static dw_die_ref
12973 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12974 {
12975   dw_die_ref subrange_die;
12976   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12977
12978   if (context_die == NULL)
12979     context_die = comp_unit_die ();
12980
12981   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12982
12983   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12984     {
12985       /* The size of the subrange type and its base type do not match,
12986          so we need to generate a size attribute for the subrange type.  */
12987       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12988     }
12989
12990   if (low)
12991     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12992   if (high)
12993     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12994
12995   return subrange_die;
12996 }
12997
12998 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12999    entry that chains various modifiers in front of the given type.  */
13000
13001 static dw_die_ref
13002 modified_type_die (tree type, int is_const_type, int is_volatile_type,
13003                    dw_die_ref context_die)
13004 {
13005   enum tree_code code = TREE_CODE (type);
13006   dw_die_ref mod_type_die;
13007   dw_die_ref sub_die = NULL;
13008   tree item_type = NULL;
13009   tree qualified_type;
13010   tree name, low, high;
13011
13012   if (code == ERROR_MARK)
13013     return NULL;
13014
13015   /* See if we already have the appropriately qualified variant of
13016      this type.  */
13017   qualified_type
13018     = get_qualified_type (type,
13019                           ((is_const_type ? TYPE_QUAL_CONST : 0)
13020                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
13021
13022   if (qualified_type == sizetype
13023       && TYPE_NAME (qualified_type)
13024       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13025     {
13026       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13027
13028       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13029                            && TYPE_PRECISION (t)
13030                            == TYPE_PRECISION (qualified_type)
13031                            && TYPE_UNSIGNED (t)
13032                            == TYPE_UNSIGNED (qualified_type));
13033       qualified_type = t;
13034     }
13035
13036   /* If we do, then we can just use its DIE, if it exists.  */
13037   if (qualified_type)
13038     {
13039       mod_type_die = lookup_type_die (qualified_type);
13040       if (mod_type_die)
13041         return mod_type_die;
13042     }
13043
13044   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13045
13046   /* Handle C typedef types.  */
13047   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
13048       && !DECL_ARTIFICIAL (name))
13049     {
13050       tree dtype = TREE_TYPE (name);
13051
13052       if (qualified_type == dtype)
13053         {
13054           /* For a named type, use the typedef.  */
13055           gen_type_die (qualified_type, context_die);
13056           return lookup_type_die (qualified_type);
13057         }
13058       else if (is_const_type < TYPE_READONLY (dtype)
13059                || is_volatile_type < TYPE_VOLATILE (dtype)
13060                || (is_const_type <= TYPE_READONLY (dtype)
13061                    && is_volatile_type <= TYPE_VOLATILE (dtype)
13062                    && DECL_ORIGINAL_TYPE (name) != type))
13063         /* cv-unqualified version of named type.  Just use the unnamed
13064            type to which it refers.  */
13065         return modified_type_die (DECL_ORIGINAL_TYPE (name),
13066                                   is_const_type, is_volatile_type,
13067                                   context_die);
13068       /* Else cv-qualified version of named type; fall through.  */
13069     }
13070
13071   if (is_const_type
13072       /* If both is_const_type and is_volatile_type, prefer the path
13073          which leads to a qualified type.  */
13074       && (!is_volatile_type
13075           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
13076           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
13077     {
13078       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
13079       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
13080     }
13081   else if (is_volatile_type)
13082     {
13083       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
13084       sub_die = modified_type_die (type, is_const_type, 0, context_die);
13085     }
13086   else if (code == POINTER_TYPE)
13087     {
13088       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
13089       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13090                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13091       item_type = TREE_TYPE (type);
13092       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13093         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13094                          TYPE_ADDR_SPACE (item_type));
13095     }
13096   else if (code == REFERENCE_TYPE)
13097     {
13098       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13099         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
13100                                 type);
13101       else
13102         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
13103       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13104                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13105       item_type = TREE_TYPE (type);
13106       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13107         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13108                          TYPE_ADDR_SPACE (item_type));
13109     }
13110   else if (code == INTEGER_TYPE
13111            && TREE_TYPE (type) != NULL_TREE
13112            && subrange_type_for_debug_p (type, &low, &high))
13113     {
13114       mod_type_die = subrange_type_die (type, low, high, context_die);
13115       item_type = TREE_TYPE (type);
13116     }
13117   else if (is_base_type (type))
13118     mod_type_die = base_type_die (type);
13119   else
13120     {
13121       gen_type_die (type, context_die);
13122
13123       /* We have to get the type_main_variant here (and pass that to the
13124          `lookup_type_die' routine) because the ..._TYPE node we have
13125          might simply be a *copy* of some original type node (where the
13126          copy was created to help us keep track of typedef names) and
13127          that copy might have a different TYPE_UID from the original
13128          ..._TYPE node.  */
13129       if (TREE_CODE (type) != VECTOR_TYPE)
13130         return lookup_type_die (type_main_variant (type));
13131       else
13132         /* Vectors have the debugging information in the type,
13133            not the main variant.  */
13134         return lookup_type_die (type);
13135     }
13136
13137   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13138      don't output a DW_TAG_typedef, since there isn't one in the
13139      user's program; just attach a DW_AT_name to the type.
13140      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13141      if the base type already has the same name.  */
13142   if (name
13143       && ((TREE_CODE (name) != TYPE_DECL
13144            && (qualified_type == TYPE_MAIN_VARIANT (type)
13145                || (!is_const_type && !is_volatile_type)))
13146           || (TREE_CODE (name) == TYPE_DECL
13147               && TREE_TYPE (name) == qualified_type
13148               && DECL_NAME (name))))
13149     {
13150       if (TREE_CODE (name) == TYPE_DECL)
13151         /* Could just call add_name_and_src_coords_attributes here,
13152            but since this is a builtin type it doesn't have any
13153            useful source coordinates anyway.  */
13154         name = DECL_NAME (name);
13155       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13156       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13157     }
13158   /* This probably indicates a bug.  */
13159   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13160     add_name_attribute (mod_type_die, "__unknown__");
13161
13162   if (qualified_type)
13163     equate_type_number_to_die (qualified_type, mod_type_die);
13164
13165   if (item_type)
13166     /* We must do this after the equate_type_number_to_die call, in case
13167        this is a recursive type.  This ensures that the modified_type_die
13168        recursion will terminate even if the type is recursive.  Recursive
13169        types are possible in Ada.  */
13170     sub_die = modified_type_die (item_type,
13171                                  TYPE_READONLY (item_type),
13172                                  TYPE_VOLATILE (item_type),
13173                                  context_die);
13174
13175   if (sub_die != NULL)
13176     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13177
13178   return mod_type_die;
13179 }
13180
13181 /* Generate DIEs for the generic parameters of T.
13182    T must be either a generic type or a generic function.
13183    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13184
13185 static void
13186 gen_generic_params_dies (tree t)
13187 {
13188   tree parms, args;
13189   int parms_num, i;
13190   dw_die_ref die = NULL;
13191
13192   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13193     return;
13194
13195   if (TYPE_P (t))
13196     die = lookup_type_die (t);
13197   else if (DECL_P (t))
13198     die = lookup_decl_die (t);
13199
13200   gcc_assert (die);
13201
13202   parms = lang_hooks.get_innermost_generic_parms (t);
13203   if (!parms)
13204     /* T has no generic parameter. It means T is neither a generic type
13205        or function. End of story.  */
13206     return;
13207
13208   parms_num = TREE_VEC_LENGTH (parms);
13209   args = lang_hooks.get_innermost_generic_args (t);
13210   for (i = 0; i < parms_num; i++)
13211     {
13212       tree parm, arg, arg_pack_elems;
13213
13214       parm = TREE_VEC_ELT (parms, i);
13215       arg = TREE_VEC_ELT (args, i);
13216       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13217       gcc_assert (parm && TREE_VALUE (parm) && arg);
13218
13219       if (parm && TREE_VALUE (parm) && arg)
13220         {
13221           /* If PARM represents a template parameter pack,
13222              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13223              by DW_TAG_template_*_parameter DIEs for the argument
13224              pack elements of ARG. Note that ARG would then be
13225              an argument pack.  */
13226           if (arg_pack_elems)
13227             template_parameter_pack_die (TREE_VALUE (parm),
13228                                          arg_pack_elems,
13229                                          die);
13230           else
13231             generic_parameter_die (TREE_VALUE (parm), arg,
13232                                    true /* Emit DW_AT_name */, die);
13233         }
13234     }
13235 }
13236
13237 /* Create and return a DIE for PARM which should be
13238    the representation of a generic type parameter.
13239    For instance, in the C++ front end, PARM would be a template parameter.
13240    ARG is the argument to PARM.
13241    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13242    name of the PARM.
13243    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13244    as a child node.  */
13245
13246 static dw_die_ref
13247 generic_parameter_die (tree parm, tree arg,
13248                        bool emit_name_p,
13249                        dw_die_ref parent_die)
13250 {
13251   dw_die_ref tmpl_die = NULL;
13252   const char *name = NULL;
13253
13254   if (!parm || !DECL_NAME (parm) || !arg)
13255     return NULL;
13256
13257   /* We support non-type generic parameters and arguments,
13258      type generic parameters and arguments, as well as
13259      generic generic parameters (a.k.a. template template parameters in C++)
13260      and arguments.  */
13261   if (TREE_CODE (parm) == PARM_DECL)
13262     /* PARM is a nontype generic parameter  */
13263     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13264   else if (TREE_CODE (parm) == TYPE_DECL)
13265     /* PARM is a type generic parameter.  */
13266     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13267   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13268     /* PARM is a generic generic parameter.
13269        Its DIE is a GNU extension. It shall have a
13270        DW_AT_name attribute to represent the name of the template template
13271        parameter, and a DW_AT_GNU_template_name attribute to represent the
13272        name of the template template argument.  */
13273     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13274                         parent_die, parm);
13275   else
13276     gcc_unreachable ();
13277
13278   if (tmpl_die)
13279     {
13280       tree tmpl_type;
13281
13282       /* If PARM is a generic parameter pack, it means we are
13283          emitting debug info for a template argument pack element.
13284          In other terms, ARG is a template argument pack element.
13285          In that case, we don't emit any DW_AT_name attribute for
13286          the die.  */
13287       if (emit_name_p)
13288         {
13289           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13290           gcc_assert (name);
13291           add_AT_string (tmpl_die, DW_AT_name, name);
13292         }
13293
13294       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13295         {
13296           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13297              TMPL_DIE should have a child DW_AT_type attribute that is set
13298              to the type of the argument to PARM, which is ARG.
13299              If PARM is a type generic parameter, TMPL_DIE should have a
13300              child DW_AT_type that is set to ARG.  */
13301           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13302           add_type_attribute (tmpl_die, tmpl_type, 0,
13303                               TREE_THIS_VOLATILE (tmpl_type),
13304                               parent_die);
13305         }
13306       else
13307         {
13308           /* So TMPL_DIE is a DIE representing a
13309              a generic generic template parameter, a.k.a template template
13310              parameter in C++ and arg is a template.  */
13311
13312           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13313              to the name of the argument.  */
13314           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13315           if (name)
13316             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13317         }
13318
13319       if (TREE_CODE (parm) == PARM_DECL)
13320         /* So PARM is a non-type generic parameter.
13321            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13322            attribute of TMPL_DIE which value represents the value
13323            of ARG.
13324            We must be careful here:
13325            The value of ARG might reference some function decls.
13326            We might currently be emitting debug info for a generic
13327            type and types are emitted before function decls, we don't
13328            know if the function decls referenced by ARG will actually be
13329            emitted after cgraph computations.
13330            So must defer the generation of the DW_AT_const_value to
13331            after cgraph is ready.  */
13332         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13333     }
13334
13335   return tmpl_die;
13336 }
13337
13338 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13339    PARM_PACK must be a template parameter pack. The returned DIE
13340    will be child DIE of PARENT_DIE.  */
13341
13342 static dw_die_ref
13343 template_parameter_pack_die (tree parm_pack,
13344                              tree parm_pack_args,
13345                              dw_die_ref parent_die)
13346 {
13347   dw_die_ref die;
13348   int j;
13349
13350   gcc_assert (parent_die && parm_pack);
13351
13352   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13353   add_name_and_src_coords_attributes (die, parm_pack);
13354   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13355     generic_parameter_die (parm_pack,
13356                            TREE_VEC_ELT (parm_pack_args, j),
13357                            false /* Don't emit DW_AT_name */,
13358                            die);
13359   return die;
13360 }
13361
13362 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13363    an enumerated type.  */
13364
13365 static inline int
13366 type_is_enum (const_tree type)
13367 {
13368   return TREE_CODE (type) == ENUMERAL_TYPE;
13369 }
13370
13371 /* Return the DBX register number described by a given RTL node.  */
13372
13373 static unsigned int
13374 dbx_reg_number (const_rtx rtl)
13375 {
13376   unsigned regno = REGNO (rtl);
13377
13378   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13379
13380 #ifdef LEAF_REG_REMAP
13381   if (current_function_uses_only_leaf_regs)
13382     {
13383       int leaf_reg = LEAF_REG_REMAP (regno);
13384       if (leaf_reg != -1)
13385         regno = (unsigned) leaf_reg;
13386     }
13387 #endif
13388
13389   return DBX_REGISTER_NUMBER (regno);
13390 }
13391
13392 /* Optionally add a DW_OP_piece term to a location description expression.
13393    DW_OP_piece is only added if the location description expression already
13394    doesn't end with DW_OP_piece.  */
13395
13396 static void
13397 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13398 {
13399   dw_loc_descr_ref loc;
13400
13401   if (*list_head != NULL)
13402     {
13403       /* Find the end of the chain.  */
13404       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13405         ;
13406
13407       if (loc->dw_loc_opc != DW_OP_piece)
13408         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13409     }
13410 }
13411
13412 /* Return a location descriptor that designates a machine register or
13413    zero if there is none.  */
13414
13415 static dw_loc_descr_ref
13416 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13417 {
13418   rtx regs;
13419
13420   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13421     return 0;
13422
13423   /* We only use "frame base" when we're sure we're talking about the
13424      post-prologue local stack frame.  We do this by *not* running
13425      register elimination until this point, and recognizing the special
13426      argument pointer and soft frame pointer rtx's.
13427      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13428   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13429       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13430     {
13431       dw_loc_descr_ref result = NULL;
13432
13433       if (dwarf_version >= 4 || !dwarf_strict)
13434         {
13435           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13436                                        initialized);
13437           if (result)
13438             add_loc_descr (&result,
13439                            new_loc_descr (DW_OP_stack_value, 0, 0));
13440         }
13441       return result;
13442     }
13443
13444   regs = targetm.dwarf_register_span (rtl);
13445
13446   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13447     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13448   else
13449     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13450 }
13451
13452 /* Return a location descriptor that designates a machine register for
13453    a given hard register number.  */
13454
13455 static dw_loc_descr_ref
13456 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13457 {
13458   dw_loc_descr_ref reg_loc_descr;
13459
13460   if (regno <= 31)
13461     reg_loc_descr
13462       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13463   else
13464     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13465
13466   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13467     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13468
13469   return reg_loc_descr;
13470 }
13471
13472 /* Given an RTL of a register, return a location descriptor that
13473    designates a value that spans more than one register.  */
13474
13475 static dw_loc_descr_ref
13476 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13477                              enum var_init_status initialized)
13478 {
13479   int nregs, size, i;
13480   unsigned reg;
13481   dw_loc_descr_ref loc_result = NULL;
13482
13483   reg = REGNO (rtl);
13484 #ifdef LEAF_REG_REMAP
13485   if (current_function_uses_only_leaf_regs)
13486     {
13487       int leaf_reg = LEAF_REG_REMAP (reg);
13488       if (leaf_reg != -1)
13489         reg = (unsigned) leaf_reg;
13490     }
13491 #endif
13492   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13493   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13494
13495   /* Simple, contiguous registers.  */
13496   if (regs == NULL_RTX)
13497     {
13498       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13499
13500       loc_result = NULL;
13501       while (nregs--)
13502         {
13503           dw_loc_descr_ref t;
13504
13505           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13506                                       VAR_INIT_STATUS_INITIALIZED);
13507           add_loc_descr (&loc_result, t);
13508           add_loc_descr_op_piece (&loc_result, size);
13509           ++reg;
13510         }
13511       return loc_result;
13512     }
13513
13514   /* Now onto stupid register sets in non contiguous locations.  */
13515
13516   gcc_assert (GET_CODE (regs) == PARALLEL);
13517
13518   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13519   loc_result = NULL;
13520
13521   for (i = 0; i < XVECLEN (regs, 0); ++i)
13522     {
13523       dw_loc_descr_ref t;
13524
13525       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13526                                   VAR_INIT_STATUS_INITIALIZED);
13527       add_loc_descr (&loc_result, t);
13528       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13529       add_loc_descr_op_piece (&loc_result, size);
13530     }
13531
13532   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13533     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13534   return loc_result;
13535 }
13536
13537 /* Return a location descriptor that designates a constant.  */
13538
13539 static dw_loc_descr_ref
13540 int_loc_descriptor (HOST_WIDE_INT i)
13541 {
13542   enum dwarf_location_atom op;
13543
13544   /* Pick the smallest representation of a constant, rather than just
13545      defaulting to the LEB encoding.  */
13546   if (i >= 0)
13547     {
13548       if (i <= 31)
13549         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13550       else if (i <= 0xff)
13551         op = DW_OP_const1u;
13552       else if (i <= 0xffff)
13553         op = DW_OP_const2u;
13554       else if (HOST_BITS_PER_WIDE_INT == 32
13555                || i <= 0xffffffff)
13556         op = DW_OP_const4u;
13557       else
13558         op = DW_OP_constu;
13559     }
13560   else
13561     {
13562       if (i >= -0x80)
13563         op = DW_OP_const1s;
13564       else if (i >= -0x8000)
13565         op = DW_OP_const2s;
13566       else if (HOST_BITS_PER_WIDE_INT == 32
13567                || i >= -0x80000000)
13568         op = DW_OP_const4s;
13569       else
13570         op = DW_OP_consts;
13571     }
13572
13573   return new_loc_descr (op, i, 0);
13574 }
13575
13576 /* Return loc description representing "address" of integer value.
13577    This can appear only as toplevel expression.  */
13578
13579 static dw_loc_descr_ref
13580 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13581 {
13582   int litsize;
13583   dw_loc_descr_ref loc_result = NULL;
13584
13585   if (!(dwarf_version >= 4 || !dwarf_strict))
13586     return NULL;
13587
13588   if (i >= 0)
13589     {
13590       if (i <= 31)
13591         litsize = 1;
13592       else if (i <= 0xff)
13593         litsize = 2;
13594       else if (i <= 0xffff)
13595         litsize = 3;
13596       else if (HOST_BITS_PER_WIDE_INT == 32
13597                || i <= 0xffffffff)
13598         litsize = 5;
13599       else
13600         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13601     }
13602   else
13603     {
13604       if (i >= -0x80)
13605         litsize = 2;
13606       else if (i >= -0x8000)
13607         litsize = 3;
13608       else if (HOST_BITS_PER_WIDE_INT == 32
13609                || i >= -0x80000000)
13610         litsize = 5;
13611       else
13612         litsize = 1 + size_of_sleb128 (i);
13613     }
13614   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13615      is more compact.  For DW_OP_stack_value we need:
13616      litsize + 1 (DW_OP_stack_value)
13617      and for DW_OP_implicit_value:
13618      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13619   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13620     {
13621       loc_result = int_loc_descriptor (i);
13622       add_loc_descr (&loc_result,
13623                      new_loc_descr (DW_OP_stack_value, 0, 0));
13624       return loc_result;
13625     }
13626
13627   loc_result = new_loc_descr (DW_OP_implicit_value,
13628                               size, 0);
13629   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13630   loc_result->dw_loc_oprnd2.v.val_int = i;
13631   return loc_result;
13632 }
13633
13634 /* Return a location descriptor that designates a base+offset location.  */
13635
13636 static dw_loc_descr_ref
13637 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13638                  enum var_init_status initialized)
13639 {
13640   unsigned int regno;
13641   dw_loc_descr_ref result;
13642   dw_fde_ref fde = current_fde ();
13643
13644   /* We only use "frame base" when we're sure we're talking about the
13645      post-prologue local stack frame.  We do this by *not* running
13646      register elimination until this point, and recognizing the special
13647      argument pointer and soft frame pointer rtx's.  */
13648   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13649     {
13650       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13651
13652       if (elim != reg)
13653         {
13654           if (GET_CODE (elim) == PLUS)
13655             {
13656               offset += INTVAL (XEXP (elim, 1));
13657               elim = XEXP (elim, 0);
13658             }
13659           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13660                        && (elim == hard_frame_pointer_rtx
13661                            || elim == stack_pointer_rtx))
13662                       || elim == (frame_pointer_needed
13663                                   ? hard_frame_pointer_rtx
13664                                   : stack_pointer_rtx));
13665
13666           /* If drap register is used to align stack, use frame
13667              pointer + offset to access stack variables.  If stack
13668              is aligned without drap, use stack pointer + offset to
13669              access stack variables.  */
13670           if (crtl->stack_realign_tried
13671               && reg == frame_pointer_rtx)
13672             {
13673               int base_reg
13674                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13675                                       ? HARD_FRAME_POINTER_REGNUM
13676                                       : REGNO (elim));
13677               return new_reg_loc_descr (base_reg, offset);
13678             }
13679
13680           gcc_assert (frame_pointer_fb_offset_valid);
13681           offset += frame_pointer_fb_offset;
13682           return new_loc_descr (DW_OP_fbreg, offset, 0);
13683         }
13684     }
13685   else if (!optimize
13686            && fde
13687            && (fde->drap_reg == REGNO (reg)
13688                || fde->vdrap_reg == REGNO (reg)))
13689     {
13690       /* Use cfa+offset to represent the location of arguments passed
13691          on the stack when drap is used to align stack.
13692          Only do this when not optimizing, for optimized code var-tracking
13693          is supposed to track where the arguments live and the register
13694          used as vdrap or drap in some spot might be used for something
13695          else in other part of the routine.  */
13696       return new_loc_descr (DW_OP_fbreg, offset, 0);
13697     }
13698
13699   regno = dbx_reg_number (reg);
13700   if (regno <= 31)
13701     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13702                             offset, 0);
13703   else
13704     result = new_loc_descr (DW_OP_bregx, regno, offset);
13705
13706   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13707     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13708
13709   return result;
13710 }
13711
13712 /* Return true if this RTL expression describes a base+offset calculation.  */
13713
13714 static inline int
13715 is_based_loc (const_rtx rtl)
13716 {
13717   return (GET_CODE (rtl) == PLUS
13718           && ((REG_P (XEXP (rtl, 0))
13719                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13720                && CONST_INT_P (XEXP (rtl, 1)))));
13721 }
13722
13723 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13724    failed.  */
13725
13726 static dw_loc_descr_ref
13727 tls_mem_loc_descriptor (rtx mem)
13728 {
13729   tree base;
13730   dw_loc_descr_ref loc_result;
13731
13732   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13733     return NULL;
13734
13735   base = get_base_address (MEM_EXPR (mem));
13736   if (base == NULL
13737       || TREE_CODE (base) != VAR_DECL
13738       || !DECL_THREAD_LOCAL_P (base))
13739     return NULL;
13740
13741   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13742   if (loc_result == NULL)
13743     return NULL;
13744
13745   if (INTVAL (MEM_OFFSET (mem)))
13746     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13747
13748   return loc_result;
13749 }
13750
13751 /* Output debug info about reason why we failed to expand expression as dwarf
13752    expression.  */
13753
13754 static void
13755 expansion_failed (tree expr, rtx rtl, char const *reason)
13756 {
13757   if (dump_file && (dump_flags & TDF_DETAILS))
13758     {
13759       fprintf (dump_file, "Failed to expand as dwarf: ");
13760       if (expr)
13761         print_generic_expr (dump_file, expr, dump_flags);
13762       if (rtl)
13763         {
13764           fprintf (dump_file, "\n");
13765           print_rtl (dump_file, rtl);
13766         }
13767       fprintf (dump_file, "\nReason: %s\n", reason);
13768     }
13769 }
13770
13771 /* Helper function for const_ok_for_output, called either directly
13772    or via for_each_rtx.  */
13773
13774 static int
13775 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13776 {
13777   rtx rtl = *rtlp;
13778
13779   if (GET_CODE (rtl) == UNSPEC)
13780     {
13781       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13782          we can't express it in the debug info.  */
13783 #ifdef ENABLE_CHECKING
13784       /* Don't complain about TLS UNSPECs, those are just too hard to
13785          delegitimize.  */
13786       if (XVECLEN (rtl, 0) != 1
13787           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13788           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13789           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13790           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13791         inform (current_function_decl
13792                 ? DECL_SOURCE_LOCATION (current_function_decl)
13793                 : UNKNOWN_LOCATION,
13794 #if NUM_UNSPEC_VALUES > 0
13795                 "non-delegitimized UNSPEC %s (%d) found in variable location",
13796                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13797                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13798                 XINT (rtl, 1));
13799 #else
13800                 "non-delegitimized UNSPEC %d found in variable location",
13801                 XINT (rtl, 1));
13802 #endif
13803 #endif
13804       expansion_failed (NULL_TREE, rtl,
13805                         "UNSPEC hasn't been delegitimized.\n");
13806       return 1;
13807     }
13808
13809   if (GET_CODE (rtl) != SYMBOL_REF)
13810     return 0;
13811
13812   if (CONSTANT_POOL_ADDRESS_P (rtl))
13813     {
13814       bool marked;
13815       get_pool_constant_mark (rtl, &marked);
13816       /* If all references to this pool constant were optimized away,
13817          it was not output and thus we can't represent it.  */
13818       if (!marked)
13819         {
13820           expansion_failed (NULL_TREE, rtl,
13821                             "Constant was removed from constant pool.\n");
13822           return 1;
13823         }
13824     }
13825
13826   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13827     return 1;
13828
13829   /* Avoid references to external symbols in debug info, on several targets
13830      the linker might even refuse to link when linking a shared library,
13831      and in many other cases the relocations for .debug_info/.debug_loc are
13832      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13833      to be defined within the same shared library or executable are fine.  */
13834   if (SYMBOL_REF_EXTERNAL_P (rtl))
13835     {
13836       tree decl = SYMBOL_REF_DECL (rtl);
13837
13838       if (decl == NULL || !targetm.binds_local_p (decl))
13839         {
13840           expansion_failed (NULL_TREE, rtl,
13841                             "Symbol not defined in current TU.\n");
13842           return 1;
13843         }
13844     }
13845
13846   return 0;
13847 }
13848
13849 /* Return true if constant RTL can be emitted in DW_OP_addr or
13850    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13851    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13852
13853 static bool
13854 const_ok_for_output (rtx rtl)
13855 {
13856   if (GET_CODE (rtl) == SYMBOL_REF)
13857     return const_ok_for_output_1 (&rtl, NULL) == 0;
13858
13859   if (GET_CODE (rtl) == CONST)
13860     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13861
13862   return true;
13863 }
13864
13865 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13866    if possible, NULL otherwise.  */
13867
13868 static dw_die_ref
13869 base_type_for_mode (enum machine_mode mode, bool unsignedp)
13870 {
13871   dw_die_ref type_die;
13872   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13873
13874   if (type == NULL)
13875     return NULL;
13876   switch (TREE_CODE (type))
13877     {
13878     case INTEGER_TYPE:
13879     case REAL_TYPE:
13880       break;
13881     default:
13882       return NULL;
13883     }
13884   type_die = lookup_type_die (type);
13885   if (!type_die)
13886     type_die = modified_type_die (type, false, false, comp_unit_die ());
13887   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13888     return NULL;
13889   return type_die;
13890 }
13891
13892 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13893    type matching MODE, or, if MODE is narrower than or as wide as
13894    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
13895    possible.  */
13896
13897 static dw_loc_descr_ref
13898 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
13899 {
13900   enum machine_mode outer_mode = mode;
13901   dw_die_ref type_die;
13902   dw_loc_descr_ref cvt;
13903
13904   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13905     {
13906       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
13907       return op;
13908     }
13909   type_die = base_type_for_mode (outer_mode, 1);
13910   if (type_die == NULL)
13911     return NULL;
13912   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13913   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13914   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13915   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13916   add_loc_descr (&op, cvt);
13917   return op;
13918 }
13919
13920 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
13921
13922 static dw_loc_descr_ref
13923 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13924                         dw_loc_descr_ref op1)
13925 {
13926   dw_loc_descr_ref ret = op0;
13927   add_loc_descr (&ret, op1);
13928   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13929   if (STORE_FLAG_VALUE != 1)
13930     {
13931       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13932       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13933     }
13934   return ret;
13935 }
13936
13937 /* Return location descriptor for signed comparison OP RTL.  */
13938
13939 static dw_loc_descr_ref
13940 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13941                          enum machine_mode mem_mode)
13942 {
13943   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13944   dw_loc_descr_ref op0, op1;
13945   int shift;
13946
13947   if (op_mode == VOIDmode)
13948     op_mode = GET_MODE (XEXP (rtl, 1));
13949   if (op_mode == VOIDmode)
13950     return NULL;
13951
13952   if (dwarf_strict
13953       && (GET_MODE_CLASS (op_mode) != MODE_INT
13954           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13955     return NULL;
13956
13957   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13958                             VAR_INIT_STATUS_INITIALIZED);
13959   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13960                             VAR_INIT_STATUS_INITIALIZED);
13961
13962   if (op0 == NULL || op1 == NULL)
13963     return NULL;
13964
13965   if (GET_MODE_CLASS (op_mode) != MODE_INT
13966       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
13967     return compare_loc_descriptor (op, op0, op1);
13968
13969   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
13970     {
13971       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
13972       dw_loc_descr_ref cvt;
13973
13974       if (type_die == NULL)
13975         return NULL;
13976       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13977       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13978       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13979       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13980       add_loc_descr (&op0, cvt);
13981       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13982       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13983       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13984       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13985       add_loc_descr (&op1, cvt);
13986       return compare_loc_descriptor (op, op0, op1);
13987     }
13988
13989   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
13990   /* For eq/ne, if the operands are known to be zero-extended,
13991      there is no need to do the fancy shifting up.  */
13992   if (op == DW_OP_eq || op == DW_OP_ne)
13993     {
13994       dw_loc_descr_ref last0, last1;
13995       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
13996         ;
13997       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
13998         ;
13999       /* deref_size zero extends, and for constants we can check
14000          whether they are zero extended or not.  */
14001       if (((last0->dw_loc_opc == DW_OP_deref_size
14002             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14003            || (CONST_INT_P (XEXP (rtl, 0))
14004                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14005                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14006           && ((last1->dw_loc_opc == DW_OP_deref_size
14007                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14008               || (CONST_INT_P (XEXP (rtl, 1))
14009                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14010                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14011         return compare_loc_descriptor (op, op0, op1);
14012     }
14013   add_loc_descr (&op0, int_loc_descriptor (shift));
14014   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14015   if (CONST_INT_P (XEXP (rtl, 1)))
14016     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14017   else
14018     {
14019       add_loc_descr (&op1, int_loc_descriptor (shift));
14020       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14021     }
14022   return compare_loc_descriptor (op, op0, op1);
14023 }
14024
14025 /* Return location descriptor for unsigned comparison OP RTL.  */
14026
14027 static dw_loc_descr_ref
14028 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14029                          enum machine_mode mem_mode)
14030 {
14031   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14032   dw_loc_descr_ref op0, op1;
14033
14034   if (op_mode == VOIDmode)
14035     op_mode = GET_MODE (XEXP (rtl, 1));
14036   if (op_mode == VOIDmode)
14037     return NULL;
14038   if (GET_MODE_CLASS (op_mode) != MODE_INT)
14039     return NULL;
14040
14041   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14042     return NULL;
14043
14044   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14045                             VAR_INIT_STATUS_INITIALIZED);
14046   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14047                             VAR_INIT_STATUS_INITIALIZED);
14048
14049   if (op0 == NULL || op1 == NULL)
14050     return NULL;
14051
14052   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14053     {
14054       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14055       dw_loc_descr_ref last0, last1;
14056       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14057         ;
14058       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14059         ;
14060       if (CONST_INT_P (XEXP (rtl, 0)))
14061         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14062       /* deref_size zero extends, so no need to mask it again.  */
14063       else if (last0->dw_loc_opc != DW_OP_deref_size
14064                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14065         {
14066           add_loc_descr (&op0, int_loc_descriptor (mask));
14067           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14068         }
14069       if (CONST_INT_P (XEXP (rtl, 1)))
14070         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14071       /* deref_size zero extends, so no need to mask it again.  */
14072       else if (last1->dw_loc_opc != DW_OP_deref_size
14073                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14074         {
14075           add_loc_descr (&op1, int_loc_descriptor (mask));
14076           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14077         }
14078     }
14079   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14080     {
14081       HOST_WIDE_INT bias = 1;
14082       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14083       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14084       if (CONST_INT_P (XEXP (rtl, 1)))
14085         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14086                                   + INTVAL (XEXP (rtl, 1)));
14087       else
14088         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14089                                             bias, 0));
14090     }
14091   return compare_loc_descriptor (op, op0, op1);
14092 }
14093
14094 /* Return location descriptor for {U,S}{MIN,MAX}.  */
14095
14096 static dw_loc_descr_ref
14097 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
14098                        enum machine_mode mem_mode)
14099 {
14100   enum dwarf_location_atom op;
14101   dw_loc_descr_ref op0, op1, ret;
14102   dw_loc_descr_ref bra_node, drop_node;
14103
14104   if (dwarf_strict
14105       && (GET_MODE_CLASS (mode) != MODE_INT
14106           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14107     return NULL;
14108
14109   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14110                             VAR_INIT_STATUS_INITIALIZED);
14111   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14112                             VAR_INIT_STATUS_INITIALIZED);
14113
14114   if (op0 == NULL || op1 == NULL)
14115     return NULL;
14116
14117   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14118   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14119   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14120   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14121     {
14122       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14123         {
14124           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14125           add_loc_descr (&op0, int_loc_descriptor (mask));
14126           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14127           add_loc_descr (&op1, int_loc_descriptor (mask));
14128           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14129         }
14130       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14131         {
14132           HOST_WIDE_INT bias = 1;
14133           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14134           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14135           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14136         }
14137     }
14138   else if (GET_MODE_CLASS (mode) == MODE_INT
14139            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14140     {
14141       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14142       add_loc_descr (&op0, int_loc_descriptor (shift));
14143       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14144       add_loc_descr (&op1, int_loc_descriptor (shift));
14145       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14146     }
14147   else if (GET_MODE_CLASS (mode) == MODE_INT
14148            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14149     {
14150       dw_die_ref type_die = base_type_for_mode (mode, 0);
14151       dw_loc_descr_ref cvt;
14152       if (type_die == NULL)
14153         return NULL;
14154       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14155       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14156       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14157       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14158       add_loc_descr (&op0, cvt);
14159       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14160       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14161       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14162       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14163       add_loc_descr (&op1, cvt);
14164     }
14165
14166   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14167     op = DW_OP_lt;
14168   else
14169     op = DW_OP_gt;
14170   ret = op0;
14171   add_loc_descr (&ret, op1);
14172   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14173   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14174   add_loc_descr (&ret, bra_node);
14175   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14176   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14177   add_loc_descr (&ret, drop_node);
14178   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14179   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14180   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14181       && GET_MODE_CLASS (mode) == MODE_INT
14182       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14183     ret = convert_descriptor_to_mode (mode, ret);
14184   return ret;
14185 }
14186
14187 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
14188    but after converting arguments to type_die, afterwards
14189    convert back to unsigned.  */
14190
14191 static dw_loc_descr_ref
14192 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14193              enum machine_mode mode, enum machine_mode mem_mode)
14194 {
14195   dw_loc_descr_ref cvt, op0, op1;
14196
14197   if (type_die == NULL)
14198     return NULL;
14199   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14200                             VAR_INIT_STATUS_INITIALIZED);
14201   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14202                             VAR_INIT_STATUS_INITIALIZED);
14203   if (op0 == NULL || op1 == NULL)
14204     return NULL;
14205   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14206   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14207   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14208   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14209   add_loc_descr (&op0, cvt);
14210   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14211   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14212   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14213   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14214   add_loc_descr (&op1, cvt);
14215   add_loc_descr (&op0, op1);
14216   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14217   return convert_descriptor_to_mode (mode, op0);
14218 }
14219
14220 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14221    const0 is DW_OP_lit0 or corresponding typed constant,
14222    const1 is DW_OP_lit1 or corresponding typed constant
14223    and constMSB is constant with just the MSB bit set
14224    for the mode):
14225        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14226    L1: const0 DW_OP_swap
14227    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14228        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14229    L3: DW_OP_drop
14230    L4: DW_OP_nop
14231
14232    CTZ is similar:
14233        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14234    L1: const0 DW_OP_swap
14235    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14236        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14237    L3: DW_OP_drop
14238    L4: DW_OP_nop
14239
14240    FFS is similar:
14241        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14242    L1: const1 DW_OP_swap
14243    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14244        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14245    L3: DW_OP_drop
14246    L4: DW_OP_nop  */
14247
14248 static dw_loc_descr_ref
14249 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
14250                     enum machine_mode mem_mode)
14251 {
14252   dw_loc_descr_ref op0, ret, tmp;
14253   HOST_WIDE_INT valv;
14254   dw_loc_descr_ref l1jump, l1label;
14255   dw_loc_descr_ref l2jump, l2label;
14256   dw_loc_descr_ref l3jump, l3label;
14257   dw_loc_descr_ref l4jump, l4label;
14258   rtx msb;
14259
14260   if (GET_MODE_CLASS (mode) != MODE_INT
14261       || GET_MODE (XEXP (rtl, 0)) != mode
14262       || (GET_CODE (rtl) == CLZ
14263           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
14264     return NULL;
14265
14266   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14267                             VAR_INIT_STATUS_INITIALIZED);
14268   if (op0 == NULL)
14269     return NULL;
14270   ret = op0;
14271   if (GET_CODE (rtl) == CLZ)
14272     {
14273       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14274         valv = GET_MODE_BITSIZE (mode);
14275     }
14276   else if (GET_CODE (rtl) == FFS)
14277     valv = 0;
14278   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14279     valv = GET_MODE_BITSIZE (mode);
14280   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14281   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14282   add_loc_descr (&ret, l1jump);
14283   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14284   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14285                             VAR_INIT_STATUS_INITIALIZED);
14286   if (tmp == NULL)
14287     return NULL;
14288   add_loc_descr (&ret, tmp);
14289   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14290   add_loc_descr (&ret, l4jump);
14291   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14292                                 ? const1_rtx : const0_rtx,
14293                                 mode, mem_mode,
14294                                 VAR_INIT_STATUS_INITIALIZED);
14295   if (l1label == NULL)
14296     return NULL;
14297   add_loc_descr (&ret, l1label);
14298   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14299   l2label = new_loc_descr (DW_OP_dup, 0, 0);
14300   add_loc_descr (&ret, l2label);
14301   if (GET_CODE (rtl) != CLZ)
14302     msb = const1_rtx;
14303   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14304     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
14305                    << (GET_MODE_BITSIZE (mode) - 1));
14306   else
14307     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
14308                                   << (GET_MODE_BITSIZE (mode)
14309                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
14310   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14311     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14312                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14313                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14314   else
14315     tmp = mem_loc_descriptor (msb, mode, mem_mode,
14316                               VAR_INIT_STATUS_INITIALIZED);
14317   if (tmp == NULL)
14318     return NULL;
14319   add_loc_descr (&ret, tmp);
14320   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14321   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14322   add_loc_descr (&ret, l3jump);
14323   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14324                             VAR_INIT_STATUS_INITIALIZED);
14325   if (tmp == NULL)
14326     return NULL;
14327   add_loc_descr (&ret, tmp);
14328   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14329                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
14330   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14331   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14332   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14333   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14334   add_loc_descr (&ret, l2jump);
14335   l3label = new_loc_descr (DW_OP_drop, 0, 0);
14336   add_loc_descr (&ret, l3label);
14337   l4label = new_loc_descr (DW_OP_nop, 0, 0);
14338   add_loc_descr (&ret, l4label);
14339   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14340   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14341   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14342   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14343   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14344   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14345   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14346   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14347   return ret;
14348 }
14349
14350 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14351    const1 is DW_OP_lit1 or corresponding typed constant):
14352        const0 DW_OP_swap
14353    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14354        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14355    L2: DW_OP_drop
14356
14357    PARITY is similar:
14358    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14359        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14360    L2: DW_OP_drop  */
14361
14362 static dw_loc_descr_ref
14363 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
14364                          enum machine_mode mem_mode)
14365 {
14366   dw_loc_descr_ref op0, ret, tmp;
14367   dw_loc_descr_ref l1jump, l1label;
14368   dw_loc_descr_ref l2jump, l2label;
14369
14370   if (GET_MODE_CLASS (mode) != MODE_INT
14371       || GET_MODE (XEXP (rtl, 0)) != mode)
14372     return NULL;
14373
14374   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14375                             VAR_INIT_STATUS_INITIALIZED);
14376   if (op0 == NULL)
14377     return NULL;
14378   ret = op0;
14379   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14380                             VAR_INIT_STATUS_INITIALIZED);
14381   if (tmp == NULL)
14382     return NULL;
14383   add_loc_descr (&ret, tmp);
14384   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14385   l1label = new_loc_descr (DW_OP_dup, 0, 0);
14386   add_loc_descr (&ret, l1label);
14387   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14388   add_loc_descr (&ret, l2jump);
14389   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14390   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14391   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14392                             VAR_INIT_STATUS_INITIALIZED);
14393   if (tmp == NULL)
14394     return NULL;
14395   add_loc_descr (&ret, tmp);
14396   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14397   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14398                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
14399   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14400   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14401                             VAR_INIT_STATUS_INITIALIZED);
14402   add_loc_descr (&ret, tmp);
14403   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14404   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14405   add_loc_descr (&ret, l1jump);
14406   l2label = new_loc_descr (DW_OP_drop, 0, 0);
14407   add_loc_descr (&ret, l2label);
14408   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14409   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14410   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14411   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14412   return ret;
14413 }
14414
14415 /* BSWAP (constS is initial shift count, either 56 or 24):
14416        constS const0
14417    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14418        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14419        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14420        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14421    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
14422
14423 static dw_loc_descr_ref
14424 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
14425                       enum machine_mode mem_mode)
14426 {
14427   dw_loc_descr_ref op0, ret, tmp;
14428   dw_loc_descr_ref l1jump, l1label;
14429   dw_loc_descr_ref l2jump, l2label;
14430
14431   if (GET_MODE_CLASS (mode) != MODE_INT
14432       || BITS_PER_UNIT != 8
14433       || (GET_MODE_BITSIZE (mode) != 32
14434           &&  GET_MODE_BITSIZE (mode) != 64))
14435     return NULL;
14436
14437   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14438                             VAR_INIT_STATUS_INITIALIZED);
14439   if (op0 == NULL)
14440     return NULL;
14441
14442   ret = op0;
14443   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14444                             mode, mem_mode,
14445                             VAR_INIT_STATUS_INITIALIZED);
14446   if (tmp == NULL)
14447     return NULL;
14448   add_loc_descr (&ret, tmp);
14449   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14450                             VAR_INIT_STATUS_INITIALIZED);
14451   if (tmp == NULL)
14452     return NULL;
14453   add_loc_descr (&ret, tmp);
14454   l1label = new_loc_descr (DW_OP_pick, 2, 0);
14455   add_loc_descr (&ret, l1label);
14456   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14457                             mode, mem_mode,
14458                             VAR_INIT_STATUS_INITIALIZED);
14459   add_loc_descr (&ret, tmp);
14460   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14461   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14462   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14463   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14464                             VAR_INIT_STATUS_INITIALIZED);
14465   if (tmp == NULL)
14466     return NULL;
14467   add_loc_descr (&ret, tmp);
14468   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14469   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14470   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14471   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14472   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14473   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14474   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14475                             VAR_INIT_STATUS_INITIALIZED);
14476   add_loc_descr (&ret, tmp);
14477   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14478   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14479   add_loc_descr (&ret, l2jump);
14480   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14481                             VAR_INIT_STATUS_INITIALIZED);
14482   add_loc_descr (&ret, tmp);
14483   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14484   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14485   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14486   add_loc_descr (&ret, l1jump);
14487   l2label = new_loc_descr (DW_OP_drop, 0, 0);
14488   add_loc_descr (&ret, l2label);
14489   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14490   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14491   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14492   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14493   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14494   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14495   return ret;
14496 }
14497
14498 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14499    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14500    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14501    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14502
14503    ROTATERT is similar:
14504    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14505    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14506    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
14507
14508 static dw_loc_descr_ref
14509 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
14510                        enum machine_mode mem_mode)
14511 {
14512   rtx rtlop1 = XEXP (rtl, 1);
14513   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14514   int i;
14515
14516   if (GET_MODE_CLASS (mode) != MODE_INT)
14517     return NULL;
14518
14519   if (GET_MODE (rtlop1) != VOIDmode
14520       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14521     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14522   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14523                             VAR_INIT_STATUS_INITIALIZED);
14524   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14525                             VAR_INIT_STATUS_INITIALIZED);
14526   if (op0 == NULL || op1 == NULL)
14527     return NULL;
14528   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14529     for (i = 0; i < 2; i++)
14530       {
14531         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14532           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14533                                         mode, mem_mode,
14534                                         VAR_INIT_STATUS_INITIALIZED);
14535         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14536           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14537                                    ? DW_OP_const4u
14538                                    : HOST_BITS_PER_WIDE_INT == 64
14539                                    ? DW_OP_const8u : DW_OP_constu,
14540                                    GET_MODE_MASK (mode), 0);
14541         else
14542           mask[i] = NULL;
14543         if (mask[i] == NULL)
14544           return NULL;
14545         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14546       }
14547   ret = op0;
14548   add_loc_descr (&ret, op1);
14549   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14550   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14551   if (GET_CODE (rtl) == ROTATERT)
14552     {
14553       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14554       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14555                                           GET_MODE_BITSIZE (mode), 0));
14556     }
14557   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14558   if (mask[0] != NULL)
14559     add_loc_descr (&ret, mask[0]);
14560   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14561   if (mask[1] != NULL)
14562     {
14563       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14564       add_loc_descr (&ret, mask[1]);
14565       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14566     }
14567   if (GET_CODE (rtl) == ROTATE)
14568     {
14569       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14570       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14571                                           GET_MODE_BITSIZE (mode), 0));
14572     }
14573   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14574   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14575   return ret;
14576 }
14577
14578 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
14579    for DEBUG_PARAMETER_REF RTL.  */
14580
14581 static dw_loc_descr_ref
14582 parameter_ref_descriptor (rtx rtl)
14583 {
14584   dw_loc_descr_ref ret;
14585   dw_die_ref ref;
14586
14587   if (dwarf_strict)
14588     return NULL;
14589   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14590   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14591   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14592   if (ref)
14593     {
14594       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14595       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14596       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14597     }
14598   else
14599     {
14600       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14601       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14602     }
14603   return ret;
14604 }
14605
14606 /* The following routine converts the RTL for a variable or parameter
14607    (resident in memory) into an equivalent Dwarf representation of a
14608    mechanism for getting the address of that same variable onto the top of a
14609    hypothetical "address evaluation" stack.
14610
14611    When creating memory location descriptors, we are effectively transforming
14612    the RTL for a memory-resident object into its Dwarf postfix expression
14613    equivalent.  This routine recursively descends an RTL tree, turning
14614    it into Dwarf postfix code as it goes.
14615
14616    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14617
14618    MEM_MODE is the mode of the memory reference, needed to handle some
14619    autoincrement addressing modes.
14620
14621    Return 0 if we can't represent the location.  */
14622
14623 static dw_loc_descr_ref
14624 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
14625                     enum machine_mode mem_mode,
14626                     enum var_init_status initialized)
14627 {
14628   dw_loc_descr_ref mem_loc_result = NULL;
14629   enum dwarf_location_atom op;
14630   dw_loc_descr_ref op0, op1;
14631
14632   if (mode == VOIDmode)
14633     mode = GET_MODE (rtl);
14634
14635   /* Note that for a dynamically sized array, the location we will generate a
14636      description of here will be the lowest numbered location which is
14637      actually within the array.  That's *not* necessarily the same as the
14638      zeroth element of the array.  */
14639
14640   rtl = targetm.delegitimize_address (rtl);
14641
14642   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14643     return NULL;
14644
14645   switch (GET_CODE (rtl))
14646     {
14647     case POST_INC:
14648     case POST_DEC:
14649     case POST_MODIFY:
14650       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14651
14652     case SUBREG:
14653       /* The case of a subreg may arise when we have a local (register)
14654          variable or a formal (register) parameter which doesn't quite fill
14655          up an entire register.  For now, just assume that it is
14656          legitimate to make the Dwarf info refer to the whole register which
14657          contains the given subreg.  */
14658       if (!subreg_lowpart_p (rtl))
14659         break;
14660       if (GET_MODE_CLASS (mode) == MODE_INT
14661           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
14662           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14663 #ifdef POINTERS_EXTEND_UNSIGNED
14664               || (mode == Pmode && mem_mode != VOIDmode)
14665 #endif
14666              )
14667           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
14668         {
14669           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
14670                                                GET_MODE (SUBREG_REG (rtl)),
14671                                                mem_mode, initialized);
14672           break;
14673         }
14674       if (dwarf_strict)
14675         break;
14676       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
14677         break;
14678       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
14679           && (GET_MODE_CLASS (mode) != MODE_INT
14680               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
14681         break;
14682       else
14683         {
14684           dw_die_ref type_die;
14685           dw_loc_descr_ref cvt;
14686
14687           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
14688                                                GET_MODE (SUBREG_REG (rtl)),
14689                                                mem_mode, initialized);
14690           if (mem_loc_result == NULL)
14691             break;
14692           type_die = base_type_for_mode (mode,
14693                                          GET_MODE_CLASS (mode) == MODE_INT);
14694           if (type_die == NULL)
14695             {
14696               mem_loc_result = NULL;
14697               break;
14698             }
14699           if (GET_MODE_SIZE (mode)
14700               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
14701             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14702           else
14703             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
14704           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14705           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14706           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14707           add_loc_descr (&mem_loc_result, cvt);
14708         }
14709       break;
14710
14711     case REG:
14712       if (GET_MODE_CLASS (mode) != MODE_INT
14713           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14714 #ifdef POINTERS_EXTEND_UNSIGNED
14715               && (mode != Pmode || mem_mode == VOIDmode)
14716 #endif
14717               ))
14718         {
14719           dw_die_ref type_die;
14720
14721           if (dwarf_strict)
14722             break;
14723           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14724             break;
14725           type_die = base_type_for_mode (mode,
14726                                          GET_MODE_CLASS (mode) == MODE_INT);
14727           if (type_die == NULL)
14728             break;
14729           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
14730                                           dbx_reg_number (rtl), 0);
14731           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14732           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14733           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14734           break;
14735         }
14736       /* Whenever a register number forms a part of the description of the
14737          method for calculating the (dynamic) address of a memory resident
14738          object, DWARF rules require the register number be referred to as
14739          a "base register".  This distinction is not based in any way upon
14740          what category of register the hardware believes the given register
14741          belongs to.  This is strictly DWARF terminology we're dealing with
14742          here. Note that in cases where the location of a memory-resident
14743          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14744          OP_CONST (0)) the actual DWARF location descriptor that we generate
14745          may just be OP_BASEREG (basereg).  This may look deceptively like
14746          the object in question was allocated to a register (rather than in
14747          memory) so DWARF consumers need to be aware of the subtle
14748          distinction between OP_REG and OP_BASEREG.  */
14749       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14750         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14751       else if (stack_realign_drap
14752                && crtl->drap_reg
14753                && crtl->args.internal_arg_pointer == rtl
14754                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14755         {
14756           /* If RTL is internal_arg_pointer, which has been optimized
14757              out, use DRAP instead.  */
14758           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14759                                             VAR_INIT_STATUS_INITIALIZED);
14760         }
14761       break;
14762
14763     case SIGN_EXTEND:
14764     case ZERO_EXTEND:
14765       gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
14766       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14767                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14768       if (op0 == 0)
14769         break;
14770       else if (GET_CODE (rtl) == ZERO_EXTEND
14771                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14772                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14773                   < HOST_BITS_PER_WIDE_INT
14774                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14775                   to expand zero extend as two shifts instead of
14776                   masking.  */
14777                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14778         {
14779           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
14780           mem_loc_result = op0;
14781           add_loc_descr (&mem_loc_result,
14782                          int_loc_descriptor (GET_MODE_MASK (imode)));
14783           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14784         }
14785       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14786         {
14787           int shift = DWARF2_ADDR_SIZE
14788                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14789           shift *= BITS_PER_UNIT;
14790           if (GET_CODE (rtl) == SIGN_EXTEND)
14791             op = DW_OP_shra;
14792           else
14793             op = DW_OP_shr;
14794           mem_loc_result = op0;
14795           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14796           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14797           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14798           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14799         }
14800       else if (!dwarf_strict)
14801         {
14802           dw_die_ref type_die1, type_die2;
14803           dw_loc_descr_ref cvt;
14804
14805           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14806                                           GET_CODE (rtl) == ZERO_EXTEND);
14807           if (type_die1 == NULL)
14808             break;
14809           type_die2 = base_type_for_mode (mode, 1);
14810           if (type_die2 == NULL)
14811             break;
14812           mem_loc_result = op0;
14813           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14814           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14815           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14816           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14817           add_loc_descr (&mem_loc_result, cvt);
14818           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14819           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14820           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14821           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14822           add_loc_descr (&mem_loc_result, cvt);
14823         }
14824       break;
14825
14826     case MEM:
14827       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14828                                            get_address_mode (rtl), mode,
14829                                            VAR_INIT_STATUS_INITIALIZED);
14830       if (mem_loc_result == NULL)
14831         mem_loc_result = tls_mem_loc_descriptor (rtl);
14832       if (mem_loc_result != 0)
14833         {
14834           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14835               || GET_MODE_CLASS (mode) != MODE_INT)
14836             {
14837               dw_die_ref type_die;
14838               dw_loc_descr_ref deref;
14839
14840               if (dwarf_strict)
14841                 return NULL;
14842               type_die
14843                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
14844               if (type_die == NULL)
14845                 return NULL;
14846               deref = new_loc_descr (DW_OP_GNU_deref_type,
14847                                      GET_MODE_SIZE (mode), 0);
14848               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14849               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14850               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14851               add_loc_descr (&mem_loc_result, deref);
14852             }
14853           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14854             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14855           else
14856             add_loc_descr (&mem_loc_result,
14857                            new_loc_descr (DW_OP_deref_size,
14858                                           GET_MODE_SIZE (mode), 0));
14859         }
14860       else
14861         {
14862           rtx new_rtl = avoid_constant_pool_reference (rtl);
14863           if (new_rtl != rtl)
14864             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
14865         }
14866       break;
14867
14868     case LO_SUM:
14869       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14870
14871     case LABEL_REF:
14872       /* Some ports can transform a symbol ref into a label ref, because
14873          the symbol ref is too far away and has to be dumped into a constant
14874          pool.  */
14875     case CONST:
14876     case SYMBOL_REF:
14877       if (GET_MODE_CLASS (mode) != MODE_INT
14878           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14879 #ifdef POINTERS_EXTEND_UNSIGNED
14880               && (mode != Pmode || mem_mode == VOIDmode)
14881 #endif
14882               ))
14883         break;
14884       if (GET_CODE (rtl) == SYMBOL_REF
14885           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14886         {
14887           dw_loc_descr_ref temp;
14888
14889           /* If this is not defined, we have no way to emit the data.  */
14890           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14891             break;
14892
14893           /* We used to emit DW_OP_addr here, but that's wrong, since
14894              DW_OP_addr should be relocated by the debug info consumer,
14895              while DW_OP_GNU_push_tls_address operand should not.  */
14896           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
14897                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
14898           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
14899           temp->dw_loc_oprnd1.v.val_addr = rtl;
14900           temp->dtprel = true;
14901
14902           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
14903           add_loc_descr (&mem_loc_result, temp);
14904
14905           break;
14906         }
14907
14908       if (!const_ok_for_output (rtl))
14909         break;
14910
14911     symref:
14912       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14913       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14914       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14915       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14916       break;
14917
14918     case CONCAT:
14919     case CONCATN:
14920     case VAR_LOCATION:
14921     case DEBUG_IMPLICIT_PTR:
14922       expansion_failed (NULL_TREE, rtl,
14923                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14924       return 0;
14925
14926     case ENTRY_VALUE:
14927       if (dwarf_strict)
14928         return NULL;
14929       if (REG_P (ENTRY_VALUE_EXP (rtl)))
14930         {
14931           if (GET_MODE_CLASS (mode) != MODE_INT
14932               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14933             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14934                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14935           else
14936             op0
14937               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
14938                                         VAR_INIT_STATUS_INITIALIZED);
14939         }
14940       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14941                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14942         {
14943           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14944                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14945           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14946             return NULL;
14947         }
14948       else
14949         gcc_unreachable ();
14950       if (op0 == NULL)
14951         return NULL;
14952       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
14953       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14954       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14955       break;
14956
14957     case DEBUG_PARAMETER_REF:
14958       mem_loc_result = parameter_ref_descriptor (rtl);
14959       break;
14960
14961     case PRE_MODIFY:
14962       /* Extract the PLUS expression nested inside and fall into
14963          PLUS code below.  */
14964       rtl = XEXP (rtl, 1);
14965       goto plus;
14966
14967     case PRE_INC:
14968     case PRE_DEC:
14969       /* Turn these into a PLUS expression and fall into the PLUS code
14970          below.  */
14971       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14972                           GEN_INT (GET_CODE (rtl) == PRE_INC
14973                                    ? GET_MODE_UNIT_SIZE (mem_mode)
14974                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
14975
14976       /* ... fall through ...  */
14977
14978     case PLUS:
14979     plus:
14980       if (is_based_loc (rtl)
14981           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14982           && GET_MODE_CLASS (mode) == MODE_INT)
14983         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14984                                           INTVAL (XEXP (rtl, 1)),
14985                                           VAR_INIT_STATUS_INITIALIZED);
14986       else
14987         {
14988           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14989                                                VAR_INIT_STATUS_INITIALIZED);
14990           if (mem_loc_result == 0)
14991             break;
14992
14993           if (CONST_INT_P (XEXP (rtl, 1))
14994               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14995             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14996           else
14997             {
14998               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14999                                         VAR_INIT_STATUS_INITIALIZED);
15000               if (op1 == 0)
15001                 break;
15002               add_loc_descr (&mem_loc_result, op1);
15003               add_loc_descr (&mem_loc_result,
15004                              new_loc_descr (DW_OP_plus, 0, 0));
15005             }
15006         }
15007       break;
15008
15009     /* If a pseudo-reg is optimized away, it is possible for it to
15010        be replaced with a MEM containing a multiply or shift.  */
15011     case MINUS:
15012       op = DW_OP_minus;
15013       goto do_binop;
15014
15015     case MULT:
15016       op = DW_OP_mul;
15017       goto do_binop;
15018
15019     case DIV:
15020       if (!dwarf_strict
15021           && GET_MODE_CLASS (mode) == MODE_INT
15022           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15023         {
15024           mem_loc_result = typed_binop (DW_OP_div, rtl,
15025                                         base_type_for_mode (mode, 0),
15026                                         mode, mem_mode);
15027           break;
15028         }
15029       op = DW_OP_div;
15030       goto do_binop;
15031
15032     case UMOD:
15033       op = DW_OP_mod;
15034       goto do_binop;
15035
15036     case ASHIFT:
15037       op = DW_OP_shl;
15038       goto do_shift;
15039
15040     case ASHIFTRT:
15041       op = DW_OP_shra;
15042       goto do_shift;
15043
15044     case LSHIFTRT:
15045       op = DW_OP_shr;
15046       goto do_shift;
15047
15048     do_shift:
15049       if (GET_MODE_CLASS (mode) != MODE_INT)
15050         break;
15051       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15052                                 VAR_INIT_STATUS_INITIALIZED);
15053       {
15054         rtx rtlop1 = XEXP (rtl, 1);
15055         if (GET_MODE (rtlop1) != VOIDmode
15056             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15057                < GET_MODE_BITSIZE (mode))
15058           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15059         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15060                                   VAR_INIT_STATUS_INITIALIZED);
15061       }
15062
15063       if (op0 == 0 || op1 == 0)
15064         break;
15065
15066       mem_loc_result = op0;
15067       add_loc_descr (&mem_loc_result, op1);
15068       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15069       break;
15070
15071     case AND:
15072       op = DW_OP_and;
15073       goto do_binop;
15074
15075     case IOR:
15076       op = DW_OP_or;
15077       goto do_binop;
15078
15079     case XOR:
15080       op = DW_OP_xor;
15081       goto do_binop;
15082
15083     do_binop:
15084       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15085                                 VAR_INIT_STATUS_INITIALIZED);
15086       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15087                                 VAR_INIT_STATUS_INITIALIZED);
15088
15089       if (op0 == 0 || op1 == 0)
15090         break;
15091
15092       mem_loc_result = op0;
15093       add_loc_descr (&mem_loc_result, op1);
15094       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15095       break;
15096
15097     case MOD:
15098       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
15099         {
15100           mem_loc_result = typed_binop (DW_OP_mod, rtl,
15101                                         base_type_for_mode (mode, 0),
15102                                         mode, mem_mode);
15103           break;
15104         }
15105
15106       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15107                                 VAR_INIT_STATUS_INITIALIZED);
15108       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15109                                 VAR_INIT_STATUS_INITIALIZED);
15110
15111       if (op0 == 0 || op1 == 0)
15112         break;
15113
15114       mem_loc_result = op0;
15115       add_loc_descr (&mem_loc_result, op1);
15116       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15117       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15118       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15119       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15120       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15121       break;
15122
15123     case UDIV:
15124       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
15125         {
15126           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
15127             {
15128               op = DW_OP_div;
15129               goto do_binop;
15130             }
15131           mem_loc_result = typed_binop (DW_OP_div, rtl,
15132                                         base_type_for_mode (mode, 1),
15133                                         mode, mem_mode);
15134         }
15135       break;
15136
15137     case NOT:
15138       op = DW_OP_not;
15139       goto do_unop;
15140
15141     case ABS:
15142       op = DW_OP_abs;
15143       goto do_unop;
15144
15145     case NEG:
15146       op = DW_OP_neg;
15147       goto do_unop;
15148
15149     do_unop:
15150       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15151                                 VAR_INIT_STATUS_INITIALIZED);
15152
15153       if (op0 == 0)
15154         break;
15155
15156       mem_loc_result = op0;
15157       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15158       break;
15159
15160     case CONST_INT:
15161       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15162 #ifdef POINTERS_EXTEND_UNSIGNED
15163           || (mode == Pmode
15164               && mem_mode != VOIDmode
15165               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15166 #endif
15167           )
15168         {
15169           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15170           break;
15171         }
15172       if (!dwarf_strict
15173           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15174               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
15175         {
15176           dw_die_ref type_die = base_type_for_mode (mode, 1);
15177           if (type_die == NULL)
15178             return NULL;
15179           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
15180                                           INTVAL (rtl));
15181           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15182           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15183           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15184           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15185             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15186           else
15187             {
15188               mem_loc_result->dw_loc_oprnd2.val_class
15189                 = dw_val_class_const_double;
15190               mem_loc_result->dw_loc_oprnd2.v.val_double
15191                 = shwi_to_double_int (INTVAL (rtl));
15192             }
15193         }
15194       break;
15195
15196     case CONST_DOUBLE:
15197       if (!dwarf_strict)
15198         {
15199           dw_die_ref type_die;
15200
15201           /* Note that a CONST_DOUBLE rtx could represent either an integer
15202              or a floating-point constant.  A CONST_DOUBLE is used whenever
15203              the constant requires more than one word in order to be
15204              adequately represented.  We output CONST_DOUBLEs as blocks.  */
15205           if (mode == VOIDmode
15206               || (GET_MODE (rtl) == VOIDmode
15207                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
15208             break;
15209           type_die = base_type_for_mode (mode,
15210                                          GET_MODE_CLASS (mode) == MODE_INT);
15211           if (type_die == NULL)
15212             return NULL;
15213           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
15214           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15215           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15216           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15217           if (SCALAR_FLOAT_MODE_P (mode))
15218             {
15219               unsigned int length = GET_MODE_SIZE (mode);
15220               unsigned char *array
15221                   = (unsigned char*) ggc_alloc_atomic (length);
15222
15223               insert_float (rtl, array);
15224               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15225               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15226               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15227               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15228             }
15229           else
15230             {
15231               mem_loc_result->dw_loc_oprnd2.val_class
15232                 = dw_val_class_const_double;
15233               mem_loc_result->dw_loc_oprnd2.v.val_double
15234                 = rtx_to_double_int (rtl);
15235             }
15236         }
15237       break;
15238
15239     case EQ:
15240       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15241       break;
15242
15243     case GE:
15244       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15245       break;
15246
15247     case GT:
15248       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15249       break;
15250
15251     case LE:
15252       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15253       break;
15254
15255     case LT:
15256       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15257       break;
15258
15259     case NE:
15260       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15261       break;
15262
15263     case GEU:
15264       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15265       break;
15266
15267     case GTU:
15268       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15269       break;
15270
15271     case LEU:
15272       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15273       break;
15274
15275     case LTU:
15276       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15277       break;
15278
15279     case UMIN:
15280     case UMAX:
15281       if (GET_MODE_CLASS (mode) != MODE_INT)
15282         break;
15283       /* FALLTHRU */
15284     case SMIN:
15285     case SMAX:
15286       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15287       break;
15288
15289     case ZERO_EXTRACT:
15290     case SIGN_EXTRACT:
15291       if (CONST_INT_P (XEXP (rtl, 1))
15292           && CONST_INT_P (XEXP (rtl, 2))
15293           && ((unsigned) INTVAL (XEXP (rtl, 1))
15294               + (unsigned) INTVAL (XEXP (rtl, 2))
15295               <= GET_MODE_BITSIZE (mode))
15296           && GET_MODE_CLASS (mode) == MODE_INT
15297           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15298           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15299         {
15300           int shift, size;
15301           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15302                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
15303           if (op0 == 0)
15304             break;
15305           if (GET_CODE (rtl) == SIGN_EXTRACT)
15306             op = DW_OP_shra;
15307           else
15308             op = DW_OP_shr;
15309           mem_loc_result = op0;
15310           size = INTVAL (XEXP (rtl, 1));
15311           shift = INTVAL (XEXP (rtl, 2));
15312           if (BITS_BIG_ENDIAN)
15313             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15314                     - shift - size;
15315           if (shift + size != (int) DWARF2_ADDR_SIZE)
15316             {
15317               add_loc_descr (&mem_loc_result,
15318                              int_loc_descriptor (DWARF2_ADDR_SIZE
15319                                                  - shift - size));
15320               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15321             }
15322           if (size != (int) DWARF2_ADDR_SIZE)
15323             {
15324               add_loc_descr (&mem_loc_result,
15325                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15326               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15327             }
15328         }
15329       break;
15330
15331     case IF_THEN_ELSE:
15332       {
15333         dw_loc_descr_ref op2, bra_node, drop_node;
15334         op0 = mem_loc_descriptor (XEXP (rtl, 0),
15335                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
15336                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
15337                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
15338         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15339                                   VAR_INIT_STATUS_INITIALIZED);
15340         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15341                                   VAR_INIT_STATUS_INITIALIZED);
15342         if (op0 == NULL || op1 == NULL || op2 == NULL)
15343           break;
15344
15345         mem_loc_result = op1;
15346         add_loc_descr (&mem_loc_result, op2);
15347         add_loc_descr (&mem_loc_result, op0);
15348         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15349         add_loc_descr (&mem_loc_result, bra_node);
15350         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15351         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15352         add_loc_descr (&mem_loc_result, drop_node);
15353         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15354         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15355       }
15356       break;
15357
15358     case FLOAT_EXTEND:
15359     case FLOAT_TRUNCATE:
15360     case FLOAT:
15361     case UNSIGNED_FLOAT:
15362     case FIX:
15363     case UNSIGNED_FIX:
15364       if (!dwarf_strict)
15365         {
15366           dw_die_ref type_die;
15367           dw_loc_descr_ref cvt;
15368
15369           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15370                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
15371           if (op0 == NULL)
15372             break;
15373           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
15374               && (GET_CODE (rtl) == FLOAT
15375                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15376                      <= DWARF2_ADDR_SIZE))
15377             {
15378               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15379                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
15380               if (type_die == NULL)
15381                 break;
15382               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
15383               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15384               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15385               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15386               add_loc_descr (&op0, cvt);
15387             }
15388           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15389           if (type_die == NULL)
15390             break;
15391           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
15392           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15393           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15394           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15395           add_loc_descr (&op0, cvt);
15396           if (GET_MODE_CLASS (mode) == MODE_INT
15397               && (GET_CODE (rtl) == FIX
15398                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15399             {
15400               op0 = convert_descriptor_to_mode (mode, op0);
15401               if (op0 == NULL)
15402                 break;
15403             }
15404           mem_loc_result = op0;
15405         }
15406       break;
15407
15408     case CLZ:
15409     case CTZ:
15410     case FFS:
15411       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15412       break;
15413
15414     case POPCOUNT:
15415     case PARITY:
15416       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15417       break;
15418
15419     case BSWAP:
15420       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15421       break;
15422
15423     case ROTATE:
15424     case ROTATERT:
15425       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15426       break;
15427
15428     case COMPARE:
15429     case TRUNCATE:
15430       /* In theory, we could implement the above.  */
15431       /* DWARF cannot represent the unsigned compare operations
15432          natively.  */
15433     case SS_MULT:
15434     case US_MULT:
15435     case SS_DIV:
15436     case US_DIV:
15437     case SS_PLUS:
15438     case US_PLUS:
15439     case SS_MINUS:
15440     case US_MINUS:
15441     case SS_NEG:
15442     case US_NEG:
15443     case SS_ABS:
15444     case SS_ASHIFT:
15445     case US_ASHIFT:
15446     case SS_TRUNCATE:
15447     case US_TRUNCATE:
15448     case UNORDERED:
15449     case ORDERED:
15450     case UNEQ:
15451     case UNGE:
15452     case UNGT:
15453     case UNLE:
15454     case UNLT:
15455     case LTGT:
15456     case FRACT_CONVERT:
15457     case UNSIGNED_FRACT_CONVERT:
15458     case SAT_FRACT:
15459     case UNSIGNED_SAT_FRACT:
15460     case SQRT:
15461     case ASM_OPERANDS:
15462     case VEC_MERGE:
15463     case VEC_SELECT:
15464     case VEC_CONCAT:
15465     case VEC_DUPLICATE:
15466     case UNSPEC:
15467     case HIGH:
15468     case FMA:
15469     case STRICT_LOW_PART:
15470     case CONST_VECTOR:
15471     case CONST_FIXED:
15472     case CLRSB:
15473       /* If delegitimize_address couldn't do anything with the UNSPEC, we
15474          can't express it in the debug info.  This can happen e.g. with some
15475          TLS UNSPECs.  */
15476       break;
15477
15478     case CONST_STRING:
15479       resolve_one_addr (&rtl, NULL);
15480       goto symref;
15481
15482     default:
15483 #ifdef ENABLE_CHECKING
15484       print_rtl (stderr, rtl);
15485       gcc_unreachable ();
15486 #else
15487       break;
15488 #endif
15489     }
15490
15491   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15492     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15493
15494   return mem_loc_result;
15495 }
15496
15497 /* Return a descriptor that describes the concatenation of two locations.
15498    This is typically a complex variable.  */
15499
15500 static dw_loc_descr_ref
15501 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15502 {
15503   dw_loc_descr_ref cc_loc_result = NULL;
15504   dw_loc_descr_ref x0_ref
15505     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15506   dw_loc_descr_ref x1_ref
15507     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15508
15509   if (x0_ref == 0 || x1_ref == 0)
15510     return 0;
15511
15512   cc_loc_result = x0_ref;
15513   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15514
15515   add_loc_descr (&cc_loc_result, x1_ref);
15516   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15517
15518   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15519     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15520
15521   return cc_loc_result;
15522 }
15523
15524 /* Return a descriptor that describes the concatenation of N
15525    locations.  */
15526
15527 static dw_loc_descr_ref
15528 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15529 {
15530   unsigned int i;
15531   dw_loc_descr_ref cc_loc_result = NULL;
15532   unsigned int n = XVECLEN (concatn, 0);
15533
15534   for (i = 0; i < n; ++i)
15535     {
15536       dw_loc_descr_ref ref;
15537       rtx x = XVECEXP (concatn, 0, i);
15538
15539       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15540       if (ref == NULL)
15541         return NULL;
15542
15543       add_loc_descr (&cc_loc_result, ref);
15544       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15545     }
15546
15547   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15548     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15549
15550   return cc_loc_result;
15551 }
15552
15553 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
15554    for DEBUG_IMPLICIT_PTR RTL.  */
15555
15556 static dw_loc_descr_ref
15557 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15558 {
15559   dw_loc_descr_ref ret;
15560   dw_die_ref ref;
15561
15562   if (dwarf_strict)
15563     return NULL;
15564   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15565               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15566               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15567   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15568   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
15569   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15570   if (ref)
15571     {
15572       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15573       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15574       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15575     }
15576   else
15577     {
15578       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15579       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15580     }
15581   return ret;
15582 }
15583
15584 /* Output a proper Dwarf location descriptor for a variable or parameter
15585    which is either allocated in a register or in a memory location.  For a
15586    register, we just generate an OP_REG and the register number.  For a
15587    memory location we provide a Dwarf postfix expression describing how to
15588    generate the (dynamic) address of the object onto the address stack.
15589
15590    MODE is mode of the decl if this loc_descriptor is going to be used in
15591    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15592    allowed, VOIDmode otherwise.
15593
15594    If we don't know how to describe it, return 0.  */
15595
15596 static dw_loc_descr_ref
15597 loc_descriptor (rtx rtl, enum machine_mode mode,
15598                 enum var_init_status initialized)
15599 {
15600   dw_loc_descr_ref loc_result = NULL;
15601
15602   switch (GET_CODE (rtl))
15603     {
15604     case SUBREG:
15605       /* The case of a subreg may arise when we have a local (register)
15606          variable or a formal (register) parameter which doesn't quite fill
15607          up an entire register.  For now, just assume that it is
15608          legitimate to make the Dwarf info refer to the whole register which
15609          contains the given subreg.  */
15610       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15611         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
15612       else
15613         goto do_default;
15614       break;
15615
15616     case REG:
15617       loc_result = reg_loc_descriptor (rtl, initialized);
15618       break;
15619
15620     case MEM:
15621       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15622                                        GET_MODE (rtl), initialized);
15623       if (loc_result == NULL)
15624         loc_result = tls_mem_loc_descriptor (rtl);
15625       if (loc_result == NULL)
15626         {
15627           rtx new_rtl = avoid_constant_pool_reference (rtl);
15628           if (new_rtl != rtl)
15629             loc_result = loc_descriptor (new_rtl, mode, initialized);
15630         }
15631       break;
15632
15633     case CONCAT:
15634       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15635                                           initialized);
15636       break;
15637
15638     case CONCATN:
15639       loc_result = concatn_loc_descriptor (rtl, initialized);
15640       break;
15641
15642     case VAR_LOCATION:
15643       /* Single part.  */
15644       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15645         {
15646           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15647           if (GET_CODE (loc) == EXPR_LIST)
15648             loc = XEXP (loc, 0);
15649           loc_result = loc_descriptor (loc, mode, initialized);
15650           break;
15651         }
15652
15653       rtl = XEXP (rtl, 1);
15654       /* FALLTHRU */
15655
15656     case PARALLEL:
15657       {
15658         rtvec par_elems = XVEC (rtl, 0);
15659         int num_elem = GET_NUM_ELEM (par_elems);
15660         enum machine_mode mode;
15661         int i;
15662
15663         /* Create the first one, so we have something to add to.  */
15664         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15665                                      VOIDmode, initialized);
15666         if (loc_result == NULL)
15667           return NULL;
15668         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15669         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15670         for (i = 1; i < num_elem; i++)
15671           {
15672             dw_loc_descr_ref temp;
15673
15674             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15675                                    VOIDmode, initialized);
15676             if (temp == NULL)
15677               return NULL;
15678             add_loc_descr (&loc_result, temp);
15679             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15680             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15681           }
15682       }
15683       break;
15684
15685     case CONST_INT:
15686       if (mode != VOIDmode && mode != BLKmode)
15687         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15688                                                     INTVAL (rtl));
15689       break;
15690
15691     case CONST_DOUBLE:
15692       if (mode == VOIDmode)
15693         mode = GET_MODE (rtl);
15694
15695       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15696         {
15697           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15698
15699           /* Note that a CONST_DOUBLE rtx could represent either an integer
15700              or a floating-point constant.  A CONST_DOUBLE is used whenever
15701              the constant requires more than one word in order to be
15702              adequately represented.  We output CONST_DOUBLEs as blocks.  */
15703           loc_result = new_loc_descr (DW_OP_implicit_value,
15704                                       GET_MODE_SIZE (mode), 0);
15705           if (SCALAR_FLOAT_MODE_P (mode))
15706             {
15707               unsigned int length = GET_MODE_SIZE (mode);
15708               unsigned char *array
15709                   = (unsigned char*) ggc_alloc_atomic (length);
15710
15711               insert_float (rtl, array);
15712               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15713               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15714               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15715               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15716             }
15717           else
15718             {
15719               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15720               loc_result->dw_loc_oprnd2.v.val_double
15721                 = rtx_to_double_int (rtl);
15722             }
15723         }
15724       break;
15725
15726     case CONST_VECTOR:
15727       if (mode == VOIDmode)
15728         mode = GET_MODE (rtl);
15729
15730       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15731         {
15732           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15733           unsigned int length = CONST_VECTOR_NUNITS (rtl);
15734           unsigned char *array = (unsigned char *)
15735             ggc_alloc_atomic (length * elt_size);
15736           unsigned int i;
15737           unsigned char *p;
15738
15739           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15740           switch (GET_MODE_CLASS (mode))
15741             {
15742             case MODE_VECTOR_INT:
15743               for (i = 0, p = array; i < length; i++, p += elt_size)
15744                 {
15745                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15746                   double_int val = rtx_to_double_int (elt);
15747
15748                   if (elt_size <= sizeof (HOST_WIDE_INT))
15749                     insert_int (double_int_to_shwi (val), elt_size, p);
15750                   else
15751                     {
15752                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15753                       insert_double (val, p);
15754                     }
15755                 }
15756               break;
15757
15758             case MODE_VECTOR_FLOAT:
15759               for (i = 0, p = array; i < length; i++, p += elt_size)
15760                 {
15761                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15762                   insert_float (elt, p);
15763                 }
15764               break;
15765
15766             default:
15767               gcc_unreachable ();
15768             }
15769
15770           loc_result = new_loc_descr (DW_OP_implicit_value,
15771                                       length * elt_size, 0);
15772           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15773           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15774           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15775           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15776         }
15777       break;
15778
15779     case CONST:
15780       if (mode == VOIDmode
15781           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
15782           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
15783           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15784         {
15785           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15786           break;
15787         }
15788       /* FALLTHROUGH */
15789     case SYMBOL_REF:
15790       if (!const_ok_for_output (rtl))
15791         break;
15792     case LABEL_REF:
15793       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15794           && (dwarf_version >= 4 || !dwarf_strict))
15795         {
15796           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
15797           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
15798           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
15799           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15800           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15801         }
15802       break;
15803
15804     case DEBUG_IMPLICIT_PTR:
15805       loc_result = implicit_ptr_descriptor (rtl, 0);
15806       break;
15807
15808     case PLUS:
15809       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15810           && CONST_INT_P (XEXP (rtl, 1)))
15811         {
15812           loc_result
15813             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15814           break;
15815         }
15816       /* FALLTHRU */
15817     do_default:
15818     default:
15819       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
15820            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15821            && dwarf_version >= 4)
15822           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15823         {
15824           /* Value expression.  */
15825           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15826           if (loc_result)
15827             add_loc_descr (&loc_result,
15828                            new_loc_descr (DW_OP_stack_value, 0, 0));
15829         }
15830       break;
15831     }
15832
15833   return loc_result;
15834 }
15835
15836 /* We need to figure out what section we should use as the base for the
15837    address ranges where a given location is valid.
15838    1. If this particular DECL has a section associated with it, use that.
15839    2. If this function has a section associated with it, use that.
15840    3. Otherwise, use the text section.
15841    XXX: If you split a variable across multiple sections, we won't notice.  */
15842
15843 static const char *
15844 secname_for_decl (const_tree decl)
15845 {
15846   const char *secname;
15847
15848   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
15849     {
15850       tree sectree = DECL_SECTION_NAME (decl);
15851       secname = TREE_STRING_POINTER (sectree);
15852     }
15853   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15854     {
15855       tree sectree = DECL_SECTION_NAME (current_function_decl);
15856       secname = TREE_STRING_POINTER (sectree);
15857     }
15858   else if (cfun && in_cold_section_p)
15859     secname = crtl->subsections.cold_section_label;
15860   else
15861     secname = text_section_label;
15862
15863   return secname;
15864 }
15865
15866 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
15867
15868 static bool
15869 decl_by_reference_p (tree decl)
15870 {
15871   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15872            || TREE_CODE (decl) == VAR_DECL)
15873           && DECL_BY_REFERENCE (decl));
15874 }
15875
15876 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15877    for VARLOC.  */
15878
15879 static dw_loc_descr_ref
15880 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15881                enum var_init_status initialized)
15882 {
15883   int have_address = 0;
15884   dw_loc_descr_ref descr;
15885   enum machine_mode mode;
15886
15887   if (want_address != 2)
15888     {
15889       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15890       /* Single part.  */
15891       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15892         {
15893           varloc = PAT_VAR_LOCATION_LOC (varloc);
15894           if (GET_CODE (varloc) == EXPR_LIST)
15895             varloc = XEXP (varloc, 0);
15896           mode = GET_MODE (varloc);
15897           if (MEM_P (varloc))
15898             {
15899               rtx addr = XEXP (varloc, 0);
15900               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15901                                           mode, initialized);
15902               if (descr)
15903                 have_address = 1;
15904               else
15905                 {
15906                   rtx x = avoid_constant_pool_reference (varloc);
15907                   if (x != varloc)
15908                     descr = mem_loc_descriptor (x, mode, VOIDmode,
15909                                                 initialized);
15910                 }
15911             }
15912           else
15913             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15914         }
15915       else
15916         return 0;
15917     }
15918   else
15919     {
15920       if (GET_CODE (varloc) == VAR_LOCATION)
15921         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15922       else
15923         mode = DECL_MODE (loc);
15924       descr = loc_descriptor (varloc, mode, initialized);
15925       have_address = 1;
15926     }
15927
15928   if (!descr)
15929     return 0;
15930
15931   if (want_address == 2 && !have_address
15932       && (dwarf_version >= 4 || !dwarf_strict))
15933     {
15934       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15935         {
15936           expansion_failed (loc, NULL_RTX,
15937                             "DWARF address size mismatch");
15938           return 0;
15939         }
15940       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15941       have_address = 1;
15942     }
15943   /* Show if we can't fill the request for an address.  */
15944   if (want_address && !have_address)
15945     {
15946       expansion_failed (loc, NULL_RTX,
15947                         "Want address and only have value");
15948       return 0;
15949     }
15950
15951   /* If we've got an address and don't want one, dereference.  */
15952   if (!want_address && have_address)
15953     {
15954       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15955       enum dwarf_location_atom op;
15956
15957       if (size > DWARF2_ADDR_SIZE || size == -1)
15958         {
15959           expansion_failed (loc, NULL_RTX,
15960                             "DWARF address size mismatch");
15961           return 0;
15962         }
15963       else if (size == DWARF2_ADDR_SIZE)
15964         op = DW_OP_deref;
15965       else
15966         op = DW_OP_deref_size;
15967
15968       add_loc_descr (&descr, new_loc_descr (op, size, 0));
15969     }
15970
15971   return descr;
15972 }
15973
15974 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15975    if it is not possible.  */
15976
15977 static dw_loc_descr_ref
15978 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15979 {
15980   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15981     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15982   else if (dwarf_version >= 3 || !dwarf_strict)
15983     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15984   else
15985     return NULL;
15986 }
15987
15988 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15989    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
15990
15991 static dw_loc_descr_ref
15992 dw_sra_loc_expr (tree decl, rtx loc)
15993 {
15994   rtx p;
15995   unsigned int padsize = 0;
15996   dw_loc_descr_ref descr, *descr_tail;
15997   unsigned HOST_WIDE_INT decl_size;
15998   rtx varloc;
15999   enum var_init_status initialized;
16000
16001   if (DECL_SIZE (decl) == NULL
16002       || !host_integerp (DECL_SIZE (decl), 1))
16003     return NULL;
16004
16005   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
16006   descr = NULL;
16007   descr_tail = &descr;
16008
16009   for (p = loc; p; p = XEXP (p, 1))
16010     {
16011       unsigned int bitsize = decl_piece_bitsize (p);
16012       rtx loc_note = *decl_piece_varloc_ptr (p);
16013       dw_loc_descr_ref cur_descr;
16014       dw_loc_descr_ref *tail, last = NULL;
16015       unsigned int opsize = 0;
16016
16017       if (loc_note == NULL_RTX
16018           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16019         {
16020           padsize += bitsize;
16021           continue;
16022         }
16023       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16024       varloc = NOTE_VAR_LOCATION (loc_note);
16025       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16026       if (cur_descr == NULL)
16027         {
16028           padsize += bitsize;
16029           continue;
16030         }
16031
16032       /* Check that cur_descr either doesn't use
16033          DW_OP_*piece operations, or their sum is equal
16034          to bitsize.  Otherwise we can't embed it.  */
16035       for (tail = &cur_descr; *tail != NULL;
16036            tail = &(*tail)->dw_loc_next)
16037         if ((*tail)->dw_loc_opc == DW_OP_piece)
16038           {
16039             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16040                       * BITS_PER_UNIT;
16041             last = *tail;
16042           }
16043         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16044           {
16045             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16046             last = *tail;
16047           }
16048
16049       if (last != NULL && opsize != bitsize)
16050         {
16051           padsize += bitsize;
16052           continue;
16053         }
16054
16055       /* If there is a hole, add DW_OP_*piece after empty DWARF
16056          expression, which means that those bits are optimized out.  */
16057       if (padsize)
16058         {
16059           if (padsize > decl_size)
16060             return NULL;
16061           decl_size -= padsize;
16062           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16063           if (*descr_tail == NULL)
16064             return NULL;
16065           descr_tail = &(*descr_tail)->dw_loc_next;
16066           padsize = 0;
16067         }
16068       *descr_tail = cur_descr;
16069       descr_tail = tail;
16070       if (bitsize > decl_size)
16071         return NULL;
16072       decl_size -= bitsize;
16073       if (last == NULL)
16074         {
16075           HOST_WIDE_INT offset = 0;
16076           if (GET_CODE (varloc) == VAR_LOCATION
16077               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16078             {
16079               varloc = PAT_VAR_LOCATION_LOC (varloc);
16080               if (GET_CODE (varloc) == EXPR_LIST)
16081                 varloc = XEXP (varloc, 0);
16082             }
16083           do 
16084             {
16085               if (GET_CODE (varloc) == CONST
16086                   || GET_CODE (varloc) == SIGN_EXTEND
16087                   || GET_CODE (varloc) == ZERO_EXTEND)
16088                 varloc = XEXP (varloc, 0);
16089               else if (GET_CODE (varloc) == SUBREG)
16090                 varloc = SUBREG_REG (varloc);
16091               else
16092                 break;
16093             }
16094           while (1);
16095           /* DW_OP_bit_size offset should be zero for register
16096              or implicit location descriptions and empty location
16097              descriptions, but for memory addresses needs big endian
16098              adjustment.  */
16099           if (MEM_P (varloc))
16100             {
16101               unsigned HOST_WIDE_INT memsize
16102                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
16103               if (memsize != bitsize)
16104                 {
16105                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16106                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16107                     return NULL;
16108                   if (memsize < bitsize)
16109                     return NULL;
16110                   if (BITS_BIG_ENDIAN)
16111                     offset = memsize - bitsize;
16112                 }
16113             }
16114
16115           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16116           if (*descr_tail == NULL)
16117             return NULL;
16118           descr_tail = &(*descr_tail)->dw_loc_next;
16119         }
16120     }
16121
16122   /* If there were any non-empty expressions, add padding till the end of
16123      the decl.  */
16124   if (descr != NULL && decl_size != 0)
16125     {
16126       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16127       if (*descr_tail == NULL)
16128         return NULL;
16129     }
16130   return descr;
16131 }
16132
16133 /* Return the dwarf representation of the location list LOC_LIST of
16134    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
16135    function.  */
16136
16137 static dw_loc_list_ref
16138 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16139 {
16140   const char *endname, *secname;
16141   rtx varloc;
16142   enum var_init_status initialized;
16143   struct var_loc_node *node;
16144   dw_loc_descr_ref descr;
16145   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16146   dw_loc_list_ref list = NULL;
16147   dw_loc_list_ref *listp = &list;
16148
16149   /* Now that we know what section we are using for a base,
16150      actually construct the list of locations.
16151      The first location information is what is passed to the
16152      function that creates the location list, and the remaining
16153      locations just get added on to that list.
16154      Note that we only know the start address for a location
16155      (IE location changes), so to build the range, we use
16156      the range [current location start, next location start].
16157      This means we have to special case the last node, and generate
16158      a range of [last location start, end of function label].  */
16159
16160   secname = secname_for_decl (decl);
16161
16162   for (node = loc_list->first; node; node = node->next)
16163     if (GET_CODE (node->loc) == EXPR_LIST
16164         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16165       {
16166         if (GET_CODE (node->loc) == EXPR_LIST)
16167           {
16168             /* This requires DW_OP_{,bit_}piece, which is not usable
16169                inside DWARF expressions.  */
16170             if (want_address != 2)
16171               continue;
16172             descr = dw_sra_loc_expr (decl, node->loc);
16173             if (descr == NULL)
16174               continue;
16175           }
16176         else
16177           {
16178             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16179             varloc = NOTE_VAR_LOCATION (node->loc);
16180             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16181           }
16182         if (descr)
16183           {
16184             bool range_across_switch = false;
16185             /* If section switch happens in between node->label
16186                and node->next->label (or end of function) and
16187                we can't emit it as a single entry list,
16188                emit two ranges, first one ending at the end
16189                of first partition and second one starting at the
16190                beginning of second partition.  */
16191             if (node == loc_list->last_before_switch
16192                 && (node != loc_list->first || loc_list->first->next)
16193                 && current_function_decl)
16194               {
16195                 endname = current_fde ()->dw_fde_end;
16196                 range_across_switch = true;
16197               }
16198             /* The variable has a location between NODE->LABEL and
16199                NODE->NEXT->LABEL.  */
16200             else if (node->next)
16201               endname = node->next->label;
16202             /* If the variable has a location at the last label
16203                it keeps its location until the end of function.  */
16204             else if (!current_function_decl)
16205               endname = text_end_label;
16206             else
16207               {
16208                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16209                                              current_function_funcdef_no);
16210                 endname = ggc_strdup (label_id);
16211               }
16212
16213             *listp = new_loc_list (descr, node->label, endname, secname);
16214             if (TREE_CODE (decl) == PARM_DECL
16215                 && node == loc_list->first
16216                 && GET_CODE (node->loc) == NOTE
16217                 && strcmp (node->label, endname) == 0)
16218               (*listp)->force = true;
16219             listp = &(*listp)->dw_loc_next;
16220
16221             if (range_across_switch)
16222               {
16223                 if (GET_CODE (node->loc) == EXPR_LIST)
16224                   descr = dw_sra_loc_expr (decl, node->loc);
16225                 else
16226                   {
16227                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16228                     varloc = NOTE_VAR_LOCATION (node->loc);
16229                     descr = dw_loc_list_1 (decl, varloc, want_address,
16230                                            initialized);
16231                   }
16232                 gcc_assert (descr);
16233                 /* The variable has a location between NODE->LABEL and
16234                    NODE->NEXT->LABEL.  */
16235                 if (node->next)
16236                   endname = node->next->label;
16237                 else
16238                   endname = current_fde ()->dw_fde_second_end;
16239                 *listp = new_loc_list (descr,
16240                                        current_fde ()->dw_fde_second_begin,
16241                                        endname, secname);
16242                 listp = &(*listp)->dw_loc_next;
16243               }
16244           }
16245       }
16246
16247   /* Try to avoid the overhead of a location list emitting a location
16248      expression instead, but only if we didn't have more than one
16249      location entry in the first place.  If some entries were not
16250      representable, we don't want to pretend a single entry that was
16251      applies to the entire scope in which the variable is
16252      available.  */
16253   if (list && loc_list->first->next)
16254     gen_llsym (list);
16255
16256   return list;
16257 }
16258
16259 /* Return if the loc_list has only single element and thus can be represented
16260    as location description.   */
16261
16262 static bool
16263 single_element_loc_list_p (dw_loc_list_ref list)
16264 {
16265   gcc_assert (!list->dw_loc_next || list->ll_symbol);
16266   return !list->ll_symbol;
16267 }
16268
16269 /* To each location in list LIST add loc descr REF.  */
16270
16271 static void
16272 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16273 {
16274   dw_loc_descr_ref copy;
16275   add_loc_descr (&list->expr, ref);
16276   list = list->dw_loc_next;
16277   while (list)
16278     {
16279       copy = ggc_alloc_dw_loc_descr_node ();
16280       memcpy (copy, ref, sizeof (dw_loc_descr_node));
16281       add_loc_descr (&list->expr, copy);
16282       while (copy->dw_loc_next)
16283         {
16284           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
16285           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
16286           copy->dw_loc_next = new_copy;
16287           copy = new_copy;
16288         }
16289       list = list->dw_loc_next;
16290     }
16291 }
16292
16293 /* Given two lists RET and LIST
16294    produce location list that is result of adding expression in LIST
16295    to expression in RET on each possition in program.
16296    Might be destructive on both RET and LIST.
16297
16298    TODO: We handle only simple cases of RET or LIST having at most one
16299    element. General case would inolve sorting the lists in program order
16300    and merging them that will need some additional work.
16301    Adding that will improve quality of debug info especially for SRA-ed
16302    structures.  */
16303
16304 static void
16305 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16306 {
16307   if (!list)
16308     return;
16309   if (!*ret)
16310     {
16311       *ret = list;
16312       return;
16313     }
16314   if (!list->dw_loc_next)
16315     {
16316       add_loc_descr_to_each (*ret, list->expr);
16317       return;
16318     }
16319   if (!(*ret)->dw_loc_next)
16320     {
16321       add_loc_descr_to_each (list, (*ret)->expr);
16322       *ret = list;
16323       return;
16324     }
16325   expansion_failed (NULL_TREE, NULL_RTX,
16326                     "Don't know how to merge two non-trivial"
16327                     " location lists.\n");
16328   *ret = NULL;
16329   return;
16330 }
16331
16332 /* LOC is constant expression.  Try a luck, look it up in constant
16333    pool and return its loc_descr of its address.  */
16334
16335 static dw_loc_descr_ref
16336 cst_pool_loc_descr (tree loc)
16337 {
16338   /* Get an RTL for this, if something has been emitted.  */
16339   rtx rtl = lookup_constant_def (loc);
16340
16341   if (!rtl || !MEM_P (rtl))
16342     {
16343       gcc_assert (!rtl);
16344       return 0;
16345     }
16346   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16347
16348   /* TODO: We might get more coverage if we was actually delaying expansion
16349      of all expressions till end of compilation when constant pools are fully
16350      populated.  */
16351   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16352     {
16353       expansion_failed (loc, NULL_RTX,
16354                         "CST value in contant pool but not marked.");
16355       return 0;
16356     }
16357   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16358                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16359 }
16360
16361 /* Return dw_loc_list representing address of addr_expr LOC
16362    by looking for innder INDIRECT_REF expression and turing it
16363    into simple arithmetics.  */
16364
16365 static dw_loc_list_ref
16366 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
16367 {
16368   tree obj, offset;
16369   HOST_WIDE_INT bitsize, bitpos, bytepos;
16370   enum machine_mode mode;
16371   int volatilep;
16372   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
16373   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16374
16375   obj = get_inner_reference (TREE_OPERAND (loc, 0),
16376                              &bitsize, &bitpos, &offset, &mode,
16377                              &unsignedp, &volatilep, false);
16378   STRIP_NOPS (obj);
16379   if (bitpos % BITS_PER_UNIT)
16380     {
16381       expansion_failed (loc, NULL_RTX, "bitfield access");
16382       return 0;
16383     }
16384   if (!INDIRECT_REF_P (obj))
16385     {
16386       expansion_failed (obj,
16387                         NULL_RTX, "no indirect ref in inner refrence");
16388       return 0;
16389     }
16390   if (!offset && !bitpos)
16391     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
16392   else if (toplev
16393            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16394            && (dwarf_version >= 4 || !dwarf_strict))
16395     {
16396       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
16397       if (!list_ret)
16398         return 0;
16399       if (offset)
16400         {
16401           /* Variable offset.  */
16402           list_ret1 = loc_list_from_tree (offset, 0);
16403           if (list_ret1 == 0)
16404             return 0;
16405           add_loc_list (&list_ret, list_ret1);
16406           if (!list_ret)
16407             return 0;
16408           add_loc_descr_to_each (list_ret,
16409                                  new_loc_descr (DW_OP_plus, 0, 0));
16410         }
16411       bytepos = bitpos / BITS_PER_UNIT;
16412       if (bytepos > 0)
16413         add_loc_descr_to_each (list_ret,
16414                                new_loc_descr (DW_OP_plus_uconst,
16415                                               bytepos, 0));
16416       else if (bytepos < 0)
16417         loc_list_plus_const (list_ret, bytepos);
16418       add_loc_descr_to_each (list_ret,
16419                              new_loc_descr (DW_OP_stack_value, 0, 0));
16420     }
16421   return list_ret;
16422 }
16423
16424
16425 /* Generate Dwarf location list representing LOC.
16426    If WANT_ADDRESS is false, expression computing LOC will be computed
16427    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
16428    if WANT_ADDRESS is 2, expression computing address useable in location
16429      will be returned (i.e. DW_OP_reg can be used
16430      to refer to register values).  */
16431
16432 static dw_loc_list_ref
16433 loc_list_from_tree (tree loc, int want_address)
16434 {
16435   dw_loc_descr_ref ret = NULL, ret1 = NULL;
16436   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16437   int have_address = 0;
16438   enum dwarf_location_atom op;
16439
16440   /* ??? Most of the time we do not take proper care for sign/zero
16441      extending the values properly.  Hopefully this won't be a real
16442      problem...  */
16443
16444   switch (TREE_CODE (loc))
16445     {
16446     case ERROR_MARK:
16447       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
16448       return 0;
16449
16450     case PLACEHOLDER_EXPR:
16451       /* This case involves extracting fields from an object to determine the
16452          position of other fields.  We don't try to encode this here.  The
16453          only user of this is Ada, which encodes the needed information using
16454          the names of types.  */
16455       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
16456       return 0;
16457
16458     case CALL_EXPR:
16459       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
16460       /* There are no opcodes for these operations.  */
16461       return 0;
16462
16463     case PREINCREMENT_EXPR:
16464     case PREDECREMENT_EXPR:
16465     case POSTINCREMENT_EXPR:
16466     case POSTDECREMENT_EXPR:
16467       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
16468       /* There are no opcodes for these operations.  */
16469       return 0;
16470
16471     case ADDR_EXPR:
16472       /* If we already want an address, see if there is INDIRECT_REF inside
16473          e.g. for &this->field.  */
16474       if (want_address)
16475         {
16476           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
16477                        (loc, want_address == 2);
16478           if (list_ret)
16479             have_address = 1;
16480           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
16481                    && (ret = cst_pool_loc_descr (loc)))
16482             have_address = 1;
16483         }
16484         /* Otherwise, process the argument and look for the address.  */
16485       if (!list_ret && !ret)
16486         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
16487       else
16488         {
16489           if (want_address)
16490             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
16491           return NULL;
16492         }
16493       break;
16494
16495     case VAR_DECL:
16496       if (DECL_THREAD_LOCAL_P (loc))
16497         {
16498           rtx rtl;
16499           enum dwarf_location_atom first_op;
16500           enum dwarf_location_atom second_op;
16501           bool dtprel = false;
16502
16503           if (targetm.have_tls)
16504             {
16505               /* If this is not defined, we have no way to emit the
16506                  data.  */
16507               if (!targetm.asm_out.output_dwarf_dtprel)
16508                 return 0;
16509
16510                /* The way DW_OP_GNU_push_tls_address is specified, we
16511                   can only look up addresses of objects in the current
16512                   module.  We used DW_OP_addr as first op, but that's
16513                   wrong, because DW_OP_addr is relocated by the debug
16514                   info consumer, while DW_OP_GNU_push_tls_address
16515                   operand shouldn't be.  */
16516               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
16517                 return 0;
16518               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
16519               dtprel = true;
16520               second_op = DW_OP_GNU_push_tls_address;
16521             }
16522           else
16523             {
16524               if (!targetm.emutls.debug_form_tls_address
16525                   || !(dwarf_version >= 3 || !dwarf_strict))
16526                 return 0;
16527               /* We stuffed the control variable into the DECL_VALUE_EXPR
16528                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
16529                  no longer appear in gimple code.  We used the control
16530                  variable in specific so that we could pick it up here.  */
16531               loc = DECL_VALUE_EXPR (loc);
16532               first_op = DW_OP_addr;
16533               second_op = DW_OP_form_tls_address;
16534             }
16535
16536           rtl = rtl_for_decl_location (loc);
16537           if (rtl == NULL_RTX)
16538             return 0;
16539
16540           if (!MEM_P (rtl))
16541             return 0;
16542           rtl = XEXP (rtl, 0);
16543           if (! CONSTANT_P (rtl))
16544             return 0;
16545
16546           ret = new_loc_descr (first_op, 0, 0);
16547           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
16548           ret->dw_loc_oprnd1.v.val_addr = rtl;
16549           ret->dtprel = dtprel;
16550
16551           ret1 = new_loc_descr (second_op, 0, 0);
16552           add_loc_descr (&ret, ret1);
16553
16554           have_address = 1;
16555           break;
16556         }
16557       /* FALLTHRU */
16558
16559     case PARM_DECL:
16560     case RESULT_DECL:
16561       if (DECL_HAS_VALUE_EXPR_P (loc))
16562         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
16563                                    want_address);
16564       /* FALLTHRU */
16565
16566     case FUNCTION_DECL:
16567       {
16568         rtx rtl;
16569         var_loc_list *loc_list = lookup_decl_loc (loc);
16570
16571         if (loc_list && loc_list->first)
16572           {
16573             list_ret = dw_loc_list (loc_list, loc, want_address);
16574             have_address = want_address != 0;
16575             break;
16576           }
16577         rtl = rtl_for_decl_location (loc);
16578         if (rtl == NULL_RTX)
16579           {
16580             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
16581             return 0;
16582           }
16583         else if (CONST_INT_P (rtl))
16584           {
16585             HOST_WIDE_INT val = INTVAL (rtl);
16586             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16587               val &= GET_MODE_MASK (DECL_MODE (loc));
16588             ret = int_loc_descriptor (val);
16589           }
16590         else if (GET_CODE (rtl) == CONST_STRING)
16591           {
16592             expansion_failed (loc, NULL_RTX, "CONST_STRING");
16593             return 0;
16594           }
16595         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16596           {
16597             ret = new_loc_descr (DW_OP_addr, 0, 0);
16598             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
16599             ret->dw_loc_oprnd1.v.val_addr = rtl;
16600           }
16601         else
16602           {
16603             enum machine_mode mode, mem_mode;
16604
16605             /* Certain constructs can only be represented at top-level.  */
16606             if (want_address == 2)
16607               {
16608                 ret = loc_descriptor (rtl, VOIDmode,
16609                                       VAR_INIT_STATUS_INITIALIZED);
16610                 have_address = 1;
16611               }
16612             else
16613               {
16614                 mode = GET_MODE (rtl);
16615                 mem_mode = VOIDmode;
16616                 if (MEM_P (rtl))
16617                   {
16618                     mem_mode = mode;
16619                     mode = get_address_mode (rtl);
16620                     rtl = XEXP (rtl, 0);
16621                     have_address = 1;
16622                   }
16623                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16624                                           VAR_INIT_STATUS_INITIALIZED);
16625               }
16626             if (!ret)
16627               expansion_failed (loc, rtl,
16628                                 "failed to produce loc descriptor for rtl");
16629           }
16630       }
16631       break;
16632
16633     case MEM_REF:
16634       /* ??? FIXME.  */
16635       if (!integer_zerop (TREE_OPERAND (loc, 1)))
16636         return 0;
16637       /* Fallthru.  */
16638     case INDIRECT_REF:
16639       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16640       have_address = 1;
16641       break;
16642
16643     case COMPOUND_EXPR:
16644       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
16645
16646     CASE_CONVERT:
16647     case VIEW_CONVERT_EXPR:
16648     case SAVE_EXPR:
16649     case MODIFY_EXPR:
16650       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
16651
16652     case COMPONENT_REF:
16653     case BIT_FIELD_REF:
16654     case ARRAY_REF:
16655     case ARRAY_RANGE_REF:
16656     case REALPART_EXPR:
16657     case IMAGPART_EXPR:
16658       {
16659         tree obj, offset;
16660         HOST_WIDE_INT bitsize, bitpos, bytepos;
16661         enum machine_mode mode;
16662         int volatilep;
16663         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
16664
16665         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16666                                    &unsignedp, &volatilep, false);
16667
16668         gcc_assert (obj != loc);
16669
16670         list_ret = loc_list_from_tree (obj,
16671                                        want_address == 2
16672                                        && !bitpos && !offset ? 2 : 1);
16673         /* TODO: We can extract value of the small expression via shifting even
16674            for nonzero bitpos.  */
16675         if (list_ret == 0)
16676           return 0;
16677         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16678           {
16679             expansion_failed (loc, NULL_RTX,
16680                               "bitfield access");
16681             return 0;
16682           }
16683
16684         if (offset != NULL_TREE)
16685           {
16686             /* Variable offset.  */
16687             list_ret1 = loc_list_from_tree (offset, 0);
16688             if (list_ret1 == 0)
16689               return 0;
16690             add_loc_list (&list_ret, list_ret1);
16691             if (!list_ret)
16692               return 0;
16693             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16694           }
16695
16696         bytepos = bitpos / BITS_PER_UNIT;
16697         if (bytepos > 0)
16698           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16699         else if (bytepos < 0)
16700           loc_list_plus_const (list_ret, bytepos);
16701
16702         have_address = 1;
16703         break;
16704       }
16705
16706     case INTEGER_CST:
16707       if ((want_address || !host_integerp (loc, 0))
16708           && (ret = cst_pool_loc_descr (loc)))
16709         have_address = 1;
16710       else if (want_address == 2
16711                && host_integerp (loc, 0)
16712                && (ret = address_of_int_loc_descriptor
16713                            (int_size_in_bytes (TREE_TYPE (loc)),
16714                             tree_low_cst (loc, 0))))
16715         have_address = 1;
16716       else if (host_integerp (loc, 0))
16717         ret = int_loc_descriptor (tree_low_cst (loc, 0));
16718       else
16719         {
16720           expansion_failed (loc, NULL_RTX,
16721                             "Integer operand is not host integer");
16722           return 0;
16723         }
16724       break;
16725
16726     case CONSTRUCTOR:
16727     case REAL_CST:
16728     case STRING_CST:
16729     case COMPLEX_CST:
16730       if ((ret = cst_pool_loc_descr (loc)))
16731         have_address = 1;
16732       else
16733       /* We can construct small constants here using int_loc_descriptor.  */
16734         expansion_failed (loc, NULL_RTX,
16735                           "constructor or constant not in constant pool");
16736       break;
16737
16738     case TRUTH_AND_EXPR:
16739     case TRUTH_ANDIF_EXPR:
16740     case BIT_AND_EXPR:
16741       op = DW_OP_and;
16742       goto do_binop;
16743
16744     case TRUTH_XOR_EXPR:
16745     case BIT_XOR_EXPR:
16746       op = DW_OP_xor;
16747       goto do_binop;
16748
16749     case TRUTH_OR_EXPR:
16750     case TRUTH_ORIF_EXPR:
16751     case BIT_IOR_EXPR:
16752       op = DW_OP_or;
16753       goto do_binop;
16754
16755     case FLOOR_DIV_EXPR:
16756     case CEIL_DIV_EXPR:
16757     case ROUND_DIV_EXPR:
16758     case TRUNC_DIV_EXPR:
16759       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16760         return 0;
16761       op = DW_OP_div;
16762       goto do_binop;
16763
16764     case MINUS_EXPR:
16765       op = DW_OP_minus;
16766       goto do_binop;
16767
16768     case FLOOR_MOD_EXPR:
16769     case CEIL_MOD_EXPR:
16770     case ROUND_MOD_EXPR:
16771     case TRUNC_MOD_EXPR:
16772       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16773         {
16774           op = DW_OP_mod;
16775           goto do_binop;
16776         }
16777       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16778       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16779       if (list_ret == 0 || list_ret1 == 0)
16780         return 0;
16781
16782       add_loc_list (&list_ret, list_ret1);
16783       if (list_ret == 0)
16784         return 0;
16785       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16786       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16787       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16788       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16789       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16790       break;
16791
16792     case MULT_EXPR:
16793       op = DW_OP_mul;
16794       goto do_binop;
16795
16796     case LSHIFT_EXPR:
16797       op = DW_OP_shl;
16798       goto do_binop;
16799
16800     case RSHIFT_EXPR:
16801       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16802       goto do_binop;
16803
16804     case POINTER_PLUS_EXPR:
16805     case PLUS_EXPR:
16806       if (host_integerp (TREE_OPERAND (loc, 1), 0))
16807         {
16808           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16809           if (list_ret == 0)
16810             return 0;
16811
16812           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
16813           break;
16814         }
16815
16816       op = DW_OP_plus;
16817       goto do_binop;
16818
16819     case LE_EXPR:
16820       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16821         return 0;
16822
16823       op = DW_OP_le;
16824       goto do_binop;
16825
16826     case GE_EXPR:
16827       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16828         return 0;
16829
16830       op = DW_OP_ge;
16831       goto do_binop;
16832
16833     case LT_EXPR:
16834       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16835         return 0;
16836
16837       op = DW_OP_lt;
16838       goto do_binop;
16839
16840     case GT_EXPR:
16841       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16842         return 0;
16843
16844       op = DW_OP_gt;
16845       goto do_binop;
16846
16847     case EQ_EXPR:
16848       op = DW_OP_eq;
16849       goto do_binop;
16850
16851     case NE_EXPR:
16852       op = DW_OP_ne;
16853       goto do_binop;
16854
16855     do_binop:
16856       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16857       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16858       if (list_ret == 0 || list_ret1 == 0)
16859         return 0;
16860
16861       add_loc_list (&list_ret, list_ret1);
16862       if (list_ret == 0)
16863         return 0;
16864       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16865       break;
16866
16867     case TRUTH_NOT_EXPR:
16868     case BIT_NOT_EXPR:
16869       op = DW_OP_not;
16870       goto do_unop;
16871
16872     case ABS_EXPR:
16873       op = DW_OP_abs;
16874       goto do_unop;
16875
16876     case NEGATE_EXPR:
16877       op = DW_OP_neg;
16878       goto do_unop;
16879
16880     do_unop:
16881       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16882       if (list_ret == 0)
16883         return 0;
16884
16885       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16886       break;
16887
16888     case MIN_EXPR:
16889     case MAX_EXPR:
16890       {
16891         const enum tree_code code =
16892           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16893
16894         loc = build3 (COND_EXPR, TREE_TYPE (loc),
16895                       build2 (code, integer_type_node,
16896                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16897                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16898       }
16899
16900       /* ... fall through ...  */
16901
16902     case COND_EXPR:
16903       {
16904         dw_loc_descr_ref lhs
16905           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
16906         dw_loc_list_ref rhs
16907           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
16908         dw_loc_descr_ref bra_node, jump_node, tmp;
16909
16910         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16911         if (list_ret == 0 || lhs == 0 || rhs == 0)
16912           return 0;
16913
16914         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16915         add_loc_descr_to_each (list_ret, bra_node);
16916
16917         add_loc_list (&list_ret, rhs);
16918         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16919         add_loc_descr_to_each (list_ret, jump_node);
16920
16921         add_loc_descr_to_each (list_ret, lhs);
16922         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16923         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16924
16925         /* ??? Need a node to point the skip at.  Use a nop.  */
16926         tmp = new_loc_descr (DW_OP_nop, 0, 0);
16927         add_loc_descr_to_each (list_ret, tmp);
16928         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16929         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16930       }
16931       break;
16932
16933     case FIX_TRUNC_EXPR:
16934       return 0;
16935
16936     default:
16937       /* Leave front-end specific codes as simply unknown.  This comes
16938          up, for instance, with the C STMT_EXPR.  */
16939       if ((unsigned int) TREE_CODE (loc)
16940           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16941         {
16942           expansion_failed (loc, NULL_RTX,
16943                             "language specific tree node");
16944           return 0;
16945         }
16946
16947 #ifdef ENABLE_CHECKING
16948       /* Otherwise this is a generic code; we should just lists all of
16949          these explicitly.  We forgot one.  */
16950       gcc_unreachable ();
16951 #else
16952       /* In a release build, we want to degrade gracefully: better to
16953          generate incomplete debugging information than to crash.  */
16954       return NULL;
16955 #endif
16956     }
16957
16958   if (!ret && !list_ret)
16959     return 0;
16960
16961   if (want_address == 2 && !have_address
16962       && (dwarf_version >= 4 || !dwarf_strict))
16963     {
16964       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16965         {
16966           expansion_failed (loc, NULL_RTX,
16967                             "DWARF address size mismatch");
16968           return 0;
16969         }
16970       if (ret)
16971         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16972       else
16973         add_loc_descr_to_each (list_ret,
16974                                new_loc_descr (DW_OP_stack_value, 0, 0));
16975       have_address = 1;
16976     }
16977   /* Show if we can't fill the request for an address.  */
16978   if (want_address && !have_address)
16979     {
16980       expansion_failed (loc, NULL_RTX,
16981                         "Want address and only have value");
16982       return 0;
16983     }
16984
16985   gcc_assert (!ret || !list_ret);
16986
16987   /* If we've got an address and don't want one, dereference.  */
16988   if (!want_address && have_address)
16989     {
16990       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16991
16992       if (size > DWARF2_ADDR_SIZE || size == -1)
16993         {
16994           expansion_failed (loc, NULL_RTX,
16995                             "DWARF address size mismatch");
16996           return 0;
16997         }
16998       else if (size == DWARF2_ADDR_SIZE)
16999         op = DW_OP_deref;
17000       else
17001         op = DW_OP_deref_size;
17002
17003       if (ret)
17004         add_loc_descr (&ret, new_loc_descr (op, size, 0));
17005       else
17006         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17007     }
17008   if (ret)
17009     list_ret = new_loc_list (ret, NULL, NULL, NULL);
17010
17011   return list_ret;
17012 }
17013
17014 /* Same as above but return only single location expression.  */
17015 static dw_loc_descr_ref
17016 loc_descriptor_from_tree (tree loc, int want_address)
17017 {
17018   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
17019   if (!ret)
17020     return NULL;
17021   if (ret->dw_loc_next)
17022     {
17023       expansion_failed (loc, NULL_RTX,
17024                         "Location list where only loc descriptor needed");
17025       return NULL;
17026     }
17027   return ret->expr;
17028 }
17029
17030 /* Given a value, round it up to the lowest multiple of `boundary'
17031    which is not less than the value itself.  */
17032
17033 static inline HOST_WIDE_INT
17034 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17035 {
17036   return (((value + boundary - 1) / boundary) * boundary);
17037 }
17038
17039 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17040    pointer to the declared type for the relevant field variable, or return
17041    `integer_type_node' if the given node turns out to be an
17042    ERROR_MARK node.  */
17043
17044 static inline tree
17045 field_type (const_tree decl)
17046 {
17047   tree type;
17048
17049   if (TREE_CODE (decl) == ERROR_MARK)
17050     return integer_type_node;
17051
17052   type = DECL_BIT_FIELD_TYPE (decl);
17053   if (type == NULL_TREE)
17054     type = TREE_TYPE (decl);
17055
17056   return type;
17057 }
17058
17059 /* Given a pointer to a tree node, return the alignment in bits for
17060    it, or else return BITS_PER_WORD if the node actually turns out to
17061    be an ERROR_MARK node.  */
17062
17063 static inline unsigned
17064 simple_type_align_in_bits (const_tree type)
17065 {
17066   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
17067 }
17068
17069 static inline unsigned
17070 simple_decl_align_in_bits (const_tree decl)
17071 {
17072   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
17073 }
17074
17075 /* Return the result of rounding T up to ALIGN.  */
17076
17077 static inline double_int
17078 round_up_to_align (double_int t, unsigned int align)
17079 {
17080   double_int alignd = uhwi_to_double_int (align);
17081   t = double_int_add (t, alignd);
17082   t = double_int_add (t, double_int_minus_one);
17083   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
17084   t = double_int_mul (t, alignd);
17085   return t;
17086 }
17087
17088 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
17089    lowest addressed byte of the "containing object" for the given FIELD_DECL,
17090    or return 0 if we are unable to determine what that offset is, either
17091    because the argument turns out to be a pointer to an ERROR_MARK node, or
17092    because the offset is actually variable.  (We can't handle the latter case
17093    just yet).  */
17094
17095 static HOST_WIDE_INT
17096 field_byte_offset (const_tree decl)
17097 {
17098   double_int object_offset_in_bits;
17099   double_int object_offset_in_bytes;
17100   double_int bitpos_int;
17101
17102   if (TREE_CODE (decl) == ERROR_MARK)
17103     return 0;
17104
17105   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
17106
17107   /* We cannot yet cope with fields whose positions are variable, so
17108      for now, when we see such things, we simply return 0.  Someday, we may
17109      be able to handle such cases, but it will be damn difficult.  */
17110   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
17111     return 0;
17112
17113   bitpos_int = tree_to_double_int (bit_position (decl));
17114
17115 #ifdef PCC_BITFIELD_TYPE_MATTERS
17116   if (PCC_BITFIELD_TYPE_MATTERS)
17117     {
17118       tree type;
17119       tree field_size_tree;
17120       double_int deepest_bitpos;
17121       double_int field_size_in_bits;
17122       unsigned int type_align_in_bits;
17123       unsigned int decl_align_in_bits;
17124       double_int type_size_in_bits;
17125
17126       type = field_type (decl);
17127       type_size_in_bits = double_int_type_size_in_bits (type);
17128       type_align_in_bits = simple_type_align_in_bits (type);
17129
17130       field_size_tree = DECL_SIZE (decl);
17131
17132       /* The size could be unspecified if there was an error, or for
17133          a flexible array member.  */
17134       if (!field_size_tree)
17135         field_size_tree = bitsize_zero_node;
17136
17137       /* If the size of the field is not constant, use the type size.  */
17138       if (TREE_CODE (field_size_tree) == INTEGER_CST)
17139         field_size_in_bits = tree_to_double_int (field_size_tree);
17140       else
17141         field_size_in_bits = type_size_in_bits;
17142
17143       decl_align_in_bits = simple_decl_align_in_bits (decl);
17144
17145       /* The GCC front-end doesn't make any attempt to keep track of the
17146          starting bit offset (relative to the start of the containing
17147          structure type) of the hypothetical "containing object" for a
17148          bit-field.  Thus, when computing the byte offset value for the
17149          start of the "containing object" of a bit-field, we must deduce
17150          this information on our own. This can be rather tricky to do in
17151          some cases.  For example, handling the following structure type
17152          definition when compiling for an i386/i486 target (which only
17153          aligns long long's to 32-bit boundaries) can be very tricky:
17154
17155          struct S { int field1; long long field2:31; };
17156
17157          Fortunately, there is a simple rule-of-thumb which can be used
17158          in such cases.  When compiling for an i386/i486, GCC will
17159          allocate 8 bytes for the structure shown above.  It decides to
17160          do this based upon one simple rule for bit-field allocation.
17161          GCC allocates each "containing object" for each bit-field at
17162          the first (i.e. lowest addressed) legitimate alignment boundary
17163          (based upon the required minimum alignment for the declared
17164          type of the field) which it can possibly use, subject to the
17165          condition that there is still enough available space remaining
17166          in the containing object (when allocated at the selected point)
17167          to fully accommodate all of the bits of the bit-field itself.
17168
17169          This simple rule makes it obvious why GCC allocates 8 bytes for
17170          each object of the structure type shown above.  When looking
17171          for a place to allocate the "containing object" for `field2',
17172          the compiler simply tries to allocate a 64-bit "containing
17173          object" at each successive 32-bit boundary (starting at zero)
17174          until it finds a place to allocate that 64- bit field such that
17175          at least 31 contiguous (and previously unallocated) bits remain
17176          within that selected 64 bit field.  (As it turns out, for the
17177          example above, the compiler finds it is OK to allocate the
17178          "containing object" 64-bit field at bit-offset zero within the
17179          structure type.)
17180
17181          Here we attempt to work backwards from the limited set of facts
17182          we're given, and we try to deduce from those facts, where GCC
17183          must have believed that the containing object started (within
17184          the structure type). The value we deduce is then used (by the
17185          callers of this routine) to generate DW_AT_location and
17186          DW_AT_bit_offset attributes for fields (both bit-fields and, in
17187          the case of DW_AT_location, regular fields as well).  */
17188
17189       /* Figure out the bit-distance from the start of the structure to
17190          the "deepest" bit of the bit-field.  */
17191       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
17192
17193       /* This is the tricky part.  Use some fancy footwork to deduce
17194          where the lowest addressed bit of the containing object must
17195          be.  */
17196       object_offset_in_bits
17197         = double_int_sub (deepest_bitpos, type_size_in_bits);
17198
17199       /* Round up to type_align by default.  This works best for
17200          bitfields.  */
17201       object_offset_in_bits
17202         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
17203
17204       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
17205         {
17206           object_offset_in_bits
17207             = double_int_sub (deepest_bitpos, type_size_in_bits);
17208
17209           /* Round up to decl_align instead.  */
17210           object_offset_in_bits
17211             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
17212         }
17213     }
17214   else
17215 #endif /* PCC_BITFIELD_TYPE_MATTERS */
17216     object_offset_in_bits = bitpos_int;
17217
17218   object_offset_in_bytes
17219     = double_int_div (object_offset_in_bits,
17220                       uhwi_to_double_int (BITS_PER_UNIT), true,
17221                       TRUNC_DIV_EXPR);
17222   return double_int_to_shwi (object_offset_in_bytes);
17223 }
17224 \f
17225 /* The following routines define various Dwarf attributes and any data
17226    associated with them.  */
17227
17228 /* Add a location description attribute value to a DIE.
17229
17230    This emits location attributes suitable for whole variables and
17231    whole parameters.  Note that the location attributes for struct fields are
17232    generated by the routine `data_member_location_attribute' below.  */
17233
17234 static inline void
17235 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
17236                              dw_loc_list_ref descr)
17237 {
17238   if (descr == 0)
17239     return;
17240   if (single_element_loc_list_p (descr))
17241     add_AT_loc (die, attr_kind, descr->expr);
17242   else
17243     add_AT_loc_list (die, attr_kind, descr);
17244 }
17245
17246 /* Add DW_AT_accessibility attribute to DIE if needed.  */
17247
17248 static void
17249 add_accessibility_attribute (dw_die_ref die, tree decl)
17250 {
17251   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
17252      children, otherwise the default is DW_ACCESS_public.  In DWARF2
17253      the default has always been DW_ACCESS_public.  */
17254   if (TREE_PROTECTED (decl))
17255     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
17256   else if (TREE_PRIVATE (decl))
17257     {
17258       if (dwarf_version == 2
17259           || die->die_parent == NULL
17260           || die->die_parent->die_tag != DW_TAG_class_type)
17261         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
17262     }
17263   else if (dwarf_version > 2
17264            && die->die_parent
17265            && die->die_parent->die_tag == DW_TAG_class_type)
17266     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
17267 }
17268
17269 /* Attach the specialized form of location attribute used for data members of
17270    struct and union types.  In the special case of a FIELD_DECL node which
17271    represents a bit-field, the "offset" part of this special location
17272    descriptor must indicate the distance in bytes from the lowest-addressed
17273    byte of the containing struct or union type to the lowest-addressed byte of
17274    the "containing object" for the bit-field.  (See the `field_byte_offset'
17275    function above).
17276
17277    For any given bit-field, the "containing object" is a hypothetical object
17278    (of some integral or enum type) within which the given bit-field lives.  The
17279    type of this hypothetical "containing object" is always the same as the
17280    declared type of the individual bit-field itself (for GCC anyway... the
17281    DWARF spec doesn't actually mandate this).  Note that it is the size (in
17282    bytes) of the hypothetical "containing object" which will be given in the
17283    DW_AT_byte_size attribute for this bit-field.  (See the
17284    `byte_size_attribute' function below.)  It is also used when calculating the
17285    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
17286    function below.)  */
17287
17288 static void
17289 add_data_member_location_attribute (dw_die_ref die, tree decl)
17290 {
17291   HOST_WIDE_INT offset;
17292   dw_loc_descr_ref loc_descr = 0;
17293
17294   if (TREE_CODE (decl) == TREE_BINFO)
17295     {
17296       /* We're working on the TAG_inheritance for a base class.  */
17297       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
17298         {
17299           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
17300              aren't at a fixed offset from all (sub)objects of the same
17301              type.  We need to extract the appropriate offset from our
17302              vtable.  The following dwarf expression means
17303
17304                BaseAddr = ObAddr + *((*ObAddr) - Offset)
17305
17306              This is specific to the V3 ABI, of course.  */
17307
17308           dw_loc_descr_ref tmp;
17309
17310           /* Make a copy of the object address.  */
17311           tmp = new_loc_descr (DW_OP_dup, 0, 0);
17312           add_loc_descr (&loc_descr, tmp);
17313
17314           /* Extract the vtable address.  */
17315           tmp = new_loc_descr (DW_OP_deref, 0, 0);
17316           add_loc_descr (&loc_descr, tmp);
17317
17318           /* Calculate the address of the offset.  */
17319           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
17320           gcc_assert (offset < 0);
17321
17322           tmp = int_loc_descriptor (-offset);
17323           add_loc_descr (&loc_descr, tmp);
17324           tmp = new_loc_descr (DW_OP_minus, 0, 0);
17325           add_loc_descr (&loc_descr, tmp);
17326
17327           /* Extract the offset.  */
17328           tmp = new_loc_descr (DW_OP_deref, 0, 0);
17329           add_loc_descr (&loc_descr, tmp);
17330
17331           /* Add it to the object address.  */
17332           tmp = new_loc_descr (DW_OP_plus, 0, 0);
17333           add_loc_descr (&loc_descr, tmp);
17334         }
17335       else
17336         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
17337     }
17338   else
17339     offset = field_byte_offset (decl);
17340
17341   if (! loc_descr)
17342     {
17343       if (dwarf_version > 2)
17344         {
17345           /* Don't need to output a location expression, just the constant. */
17346           if (offset < 0)
17347             add_AT_int (die, DW_AT_data_member_location, offset);
17348           else
17349             add_AT_unsigned (die, DW_AT_data_member_location, offset);
17350           return;
17351         }
17352       else
17353         {
17354           enum dwarf_location_atom op;
17355
17356           /* The DWARF2 standard says that we should assume that the structure
17357              address is already on the stack, so we can specify a structure
17358              field address by using DW_OP_plus_uconst.  */
17359
17360 #ifdef MIPS_DEBUGGING_INFO
17361           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
17362              operator correctly.  It works only if we leave the offset on the
17363              stack.  */
17364           op = DW_OP_constu;
17365 #else
17366           op = DW_OP_plus_uconst;
17367 #endif
17368
17369           loc_descr = new_loc_descr (op, offset, 0);
17370         }
17371     }
17372
17373   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
17374 }
17375
17376 /* Writes integer values to dw_vec_const array.  */
17377
17378 static void
17379 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
17380 {
17381   while (size != 0)
17382     {
17383       *dest++ = val & 0xff;
17384       val >>= 8;
17385       --size;
17386     }
17387 }
17388
17389 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
17390
17391 static HOST_WIDE_INT
17392 extract_int (const unsigned char *src, unsigned int size)
17393 {
17394   HOST_WIDE_INT val = 0;
17395
17396   src += size;
17397   while (size != 0)
17398     {
17399       val <<= 8;
17400       val |= *--src & 0xff;
17401       --size;
17402     }
17403   return val;
17404 }
17405
17406 /* Writes double_int values to dw_vec_const array.  */
17407
17408 static void
17409 insert_double (double_int val, unsigned char *dest)
17410 {
17411   unsigned char *p0 = dest;
17412   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
17413
17414   if (WORDS_BIG_ENDIAN)
17415     {
17416       p0 = p1;
17417       p1 = dest;
17418     }
17419
17420   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
17421   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
17422 }
17423
17424 /* Writes floating point values to dw_vec_const array.  */
17425
17426 static void
17427 insert_float (const_rtx rtl, unsigned char *array)
17428 {
17429   REAL_VALUE_TYPE rv;
17430   long val[4];
17431   int i;
17432
17433   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
17434   real_to_target (val, &rv, GET_MODE (rtl));
17435
17436   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
17437   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17438     {
17439       insert_int (val[i], 4, array);
17440       array += 4;
17441     }
17442 }
17443
17444 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17445    does not have a "location" either in memory or in a register.  These
17446    things can arise in GNU C when a constant is passed as an actual parameter
17447    to an inlined function.  They can also arise in C++ where declared
17448    constants do not necessarily get memory "homes".  */
17449
17450 static bool
17451 add_const_value_attribute (dw_die_ref die, rtx rtl)
17452 {
17453   switch (GET_CODE (rtl))
17454     {
17455     case CONST_INT:
17456       {
17457         HOST_WIDE_INT val = INTVAL (rtl);
17458
17459         if (val < 0)
17460           add_AT_int (die, DW_AT_const_value, val);
17461         else
17462           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17463       }
17464       return true;
17465
17466     case CONST_DOUBLE:
17467       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17468          floating-point constant.  A CONST_DOUBLE is used whenever the
17469          constant requires more than one word in order to be adequately
17470          represented.  */
17471       {
17472         enum machine_mode mode = GET_MODE (rtl);
17473
17474         if (SCALAR_FLOAT_MODE_P (mode))
17475           {
17476             unsigned int length = GET_MODE_SIZE (mode);
17477             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
17478
17479             insert_float (rtl, array);
17480             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17481           }
17482         else
17483           add_AT_double (die, DW_AT_const_value,
17484                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17485       }
17486       return true;
17487
17488     case CONST_VECTOR:
17489       {
17490         enum machine_mode mode = GET_MODE (rtl);
17491         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17492         unsigned int length = CONST_VECTOR_NUNITS (rtl);
17493         unsigned char *array = (unsigned char *) ggc_alloc_atomic
17494           (length * elt_size);
17495         unsigned int i;
17496         unsigned char *p;
17497
17498         switch (GET_MODE_CLASS (mode))
17499           {
17500           case MODE_VECTOR_INT:
17501             for (i = 0, p = array; i < length; i++, p += elt_size)
17502               {
17503                 rtx elt = CONST_VECTOR_ELT (rtl, i);
17504                 double_int val = rtx_to_double_int (elt);
17505
17506                 if (elt_size <= sizeof (HOST_WIDE_INT))
17507                   insert_int (double_int_to_shwi (val), elt_size, p);
17508                 else
17509                   {
17510                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
17511                     insert_double (val, p);
17512                   }
17513               }
17514             break;
17515
17516           case MODE_VECTOR_FLOAT:
17517             for (i = 0, p = array; i < length; i++, p += elt_size)
17518               {
17519                 rtx elt = CONST_VECTOR_ELT (rtl, i);
17520                 insert_float (elt, p);
17521               }
17522             break;
17523
17524           default:
17525             gcc_unreachable ();
17526           }
17527
17528         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17529       }
17530       return true;
17531
17532     case CONST_STRING:
17533       if (dwarf_version >= 4 || !dwarf_strict)
17534         {
17535           dw_loc_descr_ref loc_result;
17536           resolve_one_addr (&rtl, NULL);
17537         rtl_addr:
17538           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
17539           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
17540           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
17541           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17542           add_AT_loc (die, DW_AT_location, loc_result);
17543           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
17544           return true;
17545         }
17546       return false;
17547
17548     case CONST:
17549       if (CONSTANT_P (XEXP (rtl, 0)))
17550         return add_const_value_attribute (die, XEXP (rtl, 0));
17551       /* FALLTHROUGH */
17552     case SYMBOL_REF:
17553       if (!const_ok_for_output (rtl))
17554         return false;
17555     case LABEL_REF:
17556       if (dwarf_version >= 4 || !dwarf_strict)
17557         goto rtl_addr;
17558       return false;
17559
17560     case PLUS:
17561       /* In cases where an inlined instance of an inline function is passed
17562          the address of an `auto' variable (which is local to the caller) we
17563          can get a situation where the DECL_RTL of the artificial local
17564          variable (for the inlining) which acts as a stand-in for the
17565          corresponding formal parameter (of the inline function) will look
17566          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
17567          exactly a compile-time constant expression, but it isn't the address
17568          of the (artificial) local variable either.  Rather, it represents the
17569          *value* which the artificial local variable always has during its
17570          lifetime.  We currently have no way to represent such quasi-constant
17571          values in Dwarf, so for now we just punt and generate nothing.  */
17572       return false;
17573
17574     case HIGH:
17575     case CONST_FIXED:
17576       return false;
17577
17578     case MEM:
17579       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17580           && MEM_READONLY_P (rtl)
17581           && GET_MODE (rtl) == BLKmode)
17582         {
17583           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17584           return true;
17585         }
17586       return false;
17587
17588     default:
17589       /* No other kinds of rtx should be possible here.  */
17590       gcc_unreachable ();
17591     }
17592   return false;
17593 }
17594
17595 /* Determine whether the evaluation of EXPR references any variables
17596    or functions which aren't otherwise used (and therefore may not be
17597    output).  */
17598 static tree
17599 reference_to_unused (tree * tp, int * walk_subtrees,
17600                      void * data ATTRIBUTE_UNUSED)
17601 {
17602   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17603     *walk_subtrees = 0;
17604
17605   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17606       && ! TREE_ASM_WRITTEN (*tp))
17607     return *tp;
17608   /* ???  The C++ FE emits debug information for using decls, so
17609      putting gcc_unreachable here falls over.  See PR31899.  For now
17610      be conservative.  */
17611   else if (!cgraph_global_info_ready
17612            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17613     return *tp;
17614   else if (TREE_CODE (*tp) == VAR_DECL)
17615     {
17616       struct varpool_node *node = varpool_get_node (*tp);
17617       if (!node || !node->needed)
17618         return *tp;
17619     }
17620   else if (TREE_CODE (*tp) == FUNCTION_DECL
17621            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17622     {
17623       /* The call graph machinery must have finished analyzing,
17624          optimizing and gimplifying the CU by now.
17625          So if *TP has no call graph node associated
17626          to it, it means *TP will not be emitted.  */
17627       if (!cgraph_get_node (*tp))
17628         return *tp;
17629     }
17630   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17631     return *tp;
17632
17633   return NULL_TREE;
17634 }
17635
17636 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17637    for use in a later add_const_value_attribute call.  */
17638
17639 static rtx
17640 rtl_for_decl_init (tree init, tree type)
17641 {
17642   rtx rtl = NULL_RTX;
17643
17644   STRIP_NOPS (init);
17645
17646   /* If a variable is initialized with a string constant without embedded
17647      zeros, build CONST_STRING.  */
17648   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17649     {
17650       tree enttype = TREE_TYPE (type);
17651       tree domain = TYPE_DOMAIN (type);
17652       enum machine_mode mode = TYPE_MODE (enttype);
17653
17654       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17655           && domain
17656           && integer_zerop (TYPE_MIN_VALUE (domain))
17657           && compare_tree_int (TYPE_MAX_VALUE (domain),
17658                                TREE_STRING_LENGTH (init) - 1) == 0
17659           && ((size_t) TREE_STRING_LENGTH (init)
17660               == strlen (TREE_STRING_POINTER (init)) + 1))
17661         {
17662           rtl = gen_rtx_CONST_STRING (VOIDmode,
17663                                       ggc_strdup (TREE_STRING_POINTER (init)));
17664           rtl = gen_rtx_MEM (BLKmode, rtl);
17665           MEM_READONLY_P (rtl) = 1;
17666         }
17667     }
17668   /* Other aggregates, and complex values, could be represented using
17669      CONCAT: FIXME!  */
17670   else if (AGGREGATE_TYPE_P (type)
17671            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17672                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17673            || TREE_CODE (type) == COMPLEX_TYPE)
17674     ;
17675   /* Vectors only work if their mode is supported by the target.
17676      FIXME: generic vectors ought to work too.  */
17677   else if (TREE_CODE (type) == VECTOR_TYPE
17678            && !VECTOR_MODE_P (TYPE_MODE (type)))
17679     ;
17680   /* If the initializer is something that we know will expand into an
17681      immediate RTL constant, expand it now.  We must be careful not to
17682      reference variables which won't be output.  */
17683   else if (initializer_constant_valid_p (init, type)
17684            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17685     {
17686       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17687          possible.  */
17688       if (TREE_CODE (type) == VECTOR_TYPE)
17689         switch (TREE_CODE (init))
17690           {
17691           case VECTOR_CST:
17692             break;
17693           case CONSTRUCTOR:
17694             if (TREE_CONSTANT (init))
17695               {
17696                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
17697                 bool constant_p = true;
17698                 tree value;
17699                 unsigned HOST_WIDE_INT ix;
17700
17701                 /* Even when ctor is constant, it might contain non-*_CST
17702                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17703                    belong into VECTOR_CST nodes.  */
17704                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17705                   if (!CONSTANT_CLASS_P (value))
17706                     {
17707                       constant_p = false;
17708                       break;
17709                     }
17710
17711                 if (constant_p)
17712                   {
17713                     init = build_vector_from_ctor (type, elts);
17714                     break;
17715                   }
17716               }
17717             /* FALLTHRU */
17718
17719           default:
17720             return NULL;
17721           }
17722
17723       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17724
17725       /* If expand_expr returns a MEM, it wasn't immediate.  */
17726       gcc_assert (!rtl || !MEM_P (rtl));
17727     }
17728
17729   return rtl;
17730 }
17731
17732 /* Generate RTL for the variable DECL to represent its location.  */
17733
17734 static rtx
17735 rtl_for_decl_location (tree decl)
17736 {
17737   rtx rtl;
17738
17739   /* Here we have to decide where we are going to say the parameter "lives"
17740      (as far as the debugger is concerned).  We only have a couple of
17741      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17742
17743      DECL_RTL normally indicates where the parameter lives during most of the
17744      activation of the function.  If optimization is enabled however, this
17745      could be either NULL or else a pseudo-reg.  Both of those cases indicate
17746      that the parameter doesn't really live anywhere (as far as the code
17747      generation parts of GCC are concerned) during most of the function's
17748      activation.  That will happen (for example) if the parameter is never
17749      referenced within the function.
17750
17751      We could just generate a location descriptor here for all non-NULL
17752      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17753      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17754      where DECL_RTL is NULL or is a pseudo-reg.
17755
17756      Note however that we can only get away with using DECL_INCOMING_RTL as
17757      a backup substitute for DECL_RTL in certain limited cases.  In cases
17758      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17759      we can be sure that the parameter was passed using the same type as it is
17760      declared to have within the function, and that its DECL_INCOMING_RTL
17761      points us to a place where a value of that type is passed.
17762
17763      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17764      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17765      because in these cases DECL_INCOMING_RTL points us to a value of some
17766      type which is *different* from the type of the parameter itself.  Thus,
17767      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17768      such cases, the debugger would end up (for example) trying to fetch a
17769      `float' from a place which actually contains the first part of a
17770      `double'.  That would lead to really incorrect and confusing
17771      output at debug-time.
17772
17773      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17774      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
17775      are a couple of exceptions however.  On little-endian machines we can
17776      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17777      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17778      an integral type that is smaller than TREE_TYPE (decl). These cases arise
17779      when (on a little-endian machine) a non-prototyped function has a
17780      parameter declared to be of type `short' or `char'.  In such cases,
17781      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17782      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17783      passed `int' value.  If the debugger then uses that address to fetch
17784      a `short' or a `char' (on a little-endian machine) the result will be
17785      the correct data, so we allow for such exceptional cases below.
17786
17787      Note that our goal here is to describe the place where the given formal
17788      parameter lives during most of the function's activation (i.e. between the
17789      end of the prologue and the start of the epilogue).  We'll do that as best
17790      as we can. Note however that if the given formal parameter is modified
17791      sometime during the execution of the function, then a stack backtrace (at
17792      debug-time) will show the function as having been called with the *new*
17793      value rather than the value which was originally passed in.  This happens
17794      rarely enough that it is not a major problem, but it *is* a problem, and
17795      I'd like to fix it.
17796
17797      A future version of dwarf2out.c may generate two additional attributes for
17798      any given DW_TAG_formal_parameter DIE which will describe the "passed
17799      type" and the "passed location" for the given formal parameter in addition
17800      to the attributes we now generate to indicate the "declared type" and the
17801      "active location" for each parameter.  This additional set of attributes
17802      could be used by debuggers for stack backtraces. Separately, note that
17803      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17804      This happens (for example) for inlined-instances of inline function formal
17805      parameters which are never referenced.  This really shouldn't be
17806      happening.  All PARM_DECL nodes should get valid non-NULL
17807      DECL_INCOMING_RTL values.  FIXME.  */
17808
17809   /* Use DECL_RTL as the "location" unless we find something better.  */
17810   rtl = DECL_RTL_IF_SET (decl);
17811
17812   /* When generating abstract instances, ignore everything except
17813      constants, symbols living in memory, and symbols living in
17814      fixed registers.  */
17815   if (! reload_completed)
17816     {
17817       if (rtl
17818           && (CONSTANT_P (rtl)
17819               || (MEM_P (rtl)
17820                   && CONSTANT_P (XEXP (rtl, 0)))
17821               || (REG_P (rtl)
17822                   && TREE_CODE (decl) == VAR_DECL
17823                   && TREE_STATIC (decl))))
17824         {
17825           rtl = targetm.delegitimize_address (rtl);
17826           return rtl;
17827         }
17828       rtl = NULL_RTX;
17829     }
17830   else if (TREE_CODE (decl) == PARM_DECL)
17831     {
17832       if (rtl == NULL_RTX
17833           || is_pseudo_reg (rtl)
17834           || (MEM_P (rtl)
17835               && is_pseudo_reg (XEXP (rtl, 0))
17836               && DECL_INCOMING_RTL (decl)
17837               && MEM_P (DECL_INCOMING_RTL (decl))
17838               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17839         {
17840           tree declared_type = TREE_TYPE (decl);
17841           tree passed_type = DECL_ARG_TYPE (decl);
17842           enum machine_mode dmode = TYPE_MODE (declared_type);
17843           enum machine_mode pmode = TYPE_MODE (passed_type);
17844
17845           /* This decl represents a formal parameter which was optimized out.
17846              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17847              all cases where (rtl == NULL_RTX) just below.  */
17848           if (dmode == pmode)
17849             rtl = DECL_INCOMING_RTL (decl);
17850           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17851                    && SCALAR_INT_MODE_P (dmode)
17852                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17853                    && DECL_INCOMING_RTL (decl))
17854             {
17855               rtx inc = DECL_INCOMING_RTL (decl);
17856               if (REG_P (inc))
17857                 rtl = inc;
17858               else if (MEM_P (inc))
17859                 {
17860                   if (BYTES_BIG_ENDIAN)
17861                     rtl = adjust_address_nv (inc, dmode,
17862                                              GET_MODE_SIZE (pmode)
17863                                              - GET_MODE_SIZE (dmode));
17864                   else
17865                     rtl = inc;
17866                 }
17867             }
17868         }
17869
17870       /* If the parm was passed in registers, but lives on the stack, then
17871          make a big endian correction if the mode of the type of the
17872          parameter is not the same as the mode of the rtl.  */
17873       /* ??? This is the same series of checks that are made in dbxout.c before
17874          we reach the big endian correction code there.  It isn't clear if all
17875          of these checks are necessary here, but keeping them all is the safe
17876          thing to do.  */
17877       else if (MEM_P (rtl)
17878                && XEXP (rtl, 0) != const0_rtx
17879                && ! CONSTANT_P (XEXP (rtl, 0))
17880                /* Not passed in memory.  */
17881                && !MEM_P (DECL_INCOMING_RTL (decl))
17882                /* Not passed by invisible reference.  */
17883                && (!REG_P (XEXP (rtl, 0))
17884                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17885                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17886 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17887                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17888 #endif
17889                      )
17890                /* Big endian correction check.  */
17891                && BYTES_BIG_ENDIAN
17892                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17893                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17894                    < UNITS_PER_WORD))
17895         {
17896           int offset = (UNITS_PER_WORD
17897                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17898
17899           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17900                              plus_constant (XEXP (rtl, 0), offset));
17901         }
17902     }
17903   else if (TREE_CODE (decl) == VAR_DECL
17904            && rtl
17905            && MEM_P (rtl)
17906            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17907            && BYTES_BIG_ENDIAN)
17908     {
17909       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17910       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17911
17912       /* If a variable is declared "register" yet is smaller than
17913          a register, then if we store the variable to memory, it
17914          looks like we're storing a register-sized value, when in
17915          fact we are not.  We need to adjust the offset of the
17916          storage location to reflect the actual value's bytes,
17917          else gdb will not be able to display it.  */
17918       if (rsize > dsize)
17919         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17920                            plus_constant (XEXP (rtl, 0), rsize-dsize));
17921     }
17922
17923   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17924      and will have been substituted directly into all expressions that use it.
17925      C does not have such a concept, but C++ and other languages do.  */
17926   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17927     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17928
17929   if (rtl)
17930     rtl = targetm.delegitimize_address (rtl);
17931
17932   /* If we don't look past the constant pool, we risk emitting a
17933      reference to a constant pool entry that isn't referenced from
17934      code, and thus is not emitted.  */
17935   if (rtl)
17936     rtl = avoid_constant_pool_reference (rtl);
17937
17938   /* Try harder to get a rtl.  If this symbol ends up not being emitted
17939      in the current CU, resolve_addr will remove the expression referencing
17940      it.  */
17941   if (rtl == NULL_RTX
17942       && TREE_CODE (decl) == VAR_DECL
17943       && !DECL_EXTERNAL (decl)
17944       && TREE_STATIC (decl)
17945       && DECL_NAME (decl)
17946       && !DECL_HARD_REGISTER (decl)
17947       && DECL_MODE (decl) != VOIDmode)
17948     {
17949       rtl = make_decl_rtl_for_debug (decl);
17950       if (!MEM_P (rtl)
17951           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17952           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17953         rtl = NULL_RTX;
17954     }
17955
17956   return rtl;
17957 }
17958
17959 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17960    returned.  If so, the decl for the COMMON block is returned, and the
17961    value is the offset into the common block for the symbol.  */
17962
17963 static tree
17964 fortran_common (tree decl, HOST_WIDE_INT *value)
17965 {
17966   tree val_expr, cvar;
17967   enum machine_mode mode;
17968   HOST_WIDE_INT bitsize, bitpos;
17969   tree offset;
17970   int volatilep = 0, unsignedp = 0;
17971
17972   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17973      it does not have a value (the offset into the common area), or if it
17974      is thread local (as opposed to global) then it isn't common, and shouldn't
17975      be handled as such.  */
17976   if (TREE_CODE (decl) != VAR_DECL
17977       || !TREE_STATIC (decl)
17978       || !DECL_HAS_VALUE_EXPR_P (decl)
17979       || !is_fortran ())
17980     return NULL_TREE;
17981
17982   val_expr = DECL_VALUE_EXPR (decl);
17983   if (TREE_CODE (val_expr) != COMPONENT_REF)
17984     return NULL_TREE;
17985
17986   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
17987                               &mode, &unsignedp, &volatilep, true);
17988
17989   if (cvar == NULL_TREE
17990       || TREE_CODE (cvar) != VAR_DECL
17991       || DECL_ARTIFICIAL (cvar)
17992       || !TREE_PUBLIC (cvar))
17993     return NULL_TREE;
17994
17995   *value = 0;
17996   if (offset != NULL)
17997     {
17998       if (!host_integerp (offset, 0))
17999         return NULL_TREE;
18000       *value = tree_low_cst (offset, 0);
18001     }
18002   if (bitpos != 0)
18003     *value += bitpos / BITS_PER_UNIT;
18004
18005   return cvar;
18006 }
18007
18008 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
18009    data attribute for a variable or a parameter.  We generate the
18010    DW_AT_const_value attribute only in those cases where the given variable
18011    or parameter does not have a true "location" either in memory or in a
18012    register.  This can happen (for example) when a constant is passed as an
18013    actual argument in a call to an inline function.  (It's possible that
18014    these things can crop up in other ways also.)  Note that one type of
18015    constant value which can be passed into an inlined function is a constant
18016    pointer.  This can happen for example if an actual argument in an inlined
18017    function call evaluates to a compile-time constant address.
18018
18019    CACHE_P is true if it is worth caching the location list for DECL,
18020    so that future calls can reuse it rather than regenerate it from scratch.
18021    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
18022    since we will need to refer to them each time the function is inlined.  */
18023
18024 static bool
18025 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
18026                                        enum dwarf_attribute attr)
18027 {
18028   rtx rtl;
18029   dw_loc_list_ref list;
18030   var_loc_list *loc_list;
18031   cached_dw_loc_list *cache;
18032   void **slot;
18033
18034   if (TREE_CODE (decl) == ERROR_MARK)
18035     return false;
18036
18037   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
18038               || TREE_CODE (decl) == RESULT_DECL);
18039
18040   /* Try to get some constant RTL for this decl, and use that as the value of
18041      the location.  */
18042
18043   rtl = rtl_for_decl_location (decl);
18044   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18045       && add_const_value_attribute (die, rtl))
18046     return true;
18047
18048   /* See if we have single element location list that is equivalent to
18049      a constant value.  That way we are better to use add_const_value_attribute
18050      rather than expanding constant value equivalent.  */
18051   loc_list = lookup_decl_loc (decl);
18052   if (loc_list
18053       && loc_list->first
18054       && loc_list->first->next == NULL
18055       && NOTE_P (loc_list->first->loc)
18056       && NOTE_VAR_LOCATION (loc_list->first->loc)
18057       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
18058     {
18059       struct var_loc_node *node;
18060
18061       node = loc_list->first;
18062       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
18063       if (GET_CODE (rtl) == EXPR_LIST)
18064         rtl = XEXP (rtl, 0);
18065       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18066           && add_const_value_attribute (die, rtl))
18067          return true;
18068     }
18069   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
18070      list several times.  See if we've already cached the contents.  */
18071   list = NULL;
18072   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
18073     cache_p = false;
18074   if (cache_p)
18075     {
18076       cache = (cached_dw_loc_list *)
18077         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
18078       if (cache)
18079         list = cache->loc_list;
18080     }
18081   if (list == NULL)
18082     {
18083       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
18084       /* It is usually worth caching this result if the decl is from
18085          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
18086       if (cache_p && list && list->dw_loc_next)
18087         {
18088           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
18089                                            DECL_UID (decl), INSERT);
18090           cache = ggc_alloc_cleared_cached_dw_loc_list ();
18091           cache->decl_id = DECL_UID (decl);
18092           cache->loc_list = list;
18093           *slot = cache;
18094         }
18095     }
18096   if (list)
18097     {
18098       add_AT_location_description (die, attr, list);
18099       return true;
18100     }
18101   /* None of that worked, so it must not really have a location;
18102      try adding a constant value attribute from the DECL_INITIAL.  */
18103   return tree_add_const_value_attribute_for_decl (die, decl);
18104 }
18105
18106 /* Add VARIABLE and DIE into deferred locations list.  */
18107
18108 static void
18109 defer_location (tree variable, dw_die_ref die)
18110 {
18111   deferred_locations entry;
18112   entry.variable = variable;
18113   entry.die = die;
18114   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
18115 }
18116
18117 /* Helper function for tree_add_const_value_attribute.  Natively encode
18118    initializer INIT into an array.  Return true if successful.  */
18119
18120 static bool
18121 native_encode_initializer (tree init, unsigned char *array, int size)
18122 {
18123   tree type;
18124
18125   if (init == NULL_TREE)
18126     return false;
18127
18128   STRIP_NOPS (init);
18129   switch (TREE_CODE (init))
18130     {
18131     case STRING_CST:
18132       type = TREE_TYPE (init);
18133       if (TREE_CODE (type) == ARRAY_TYPE)
18134         {
18135           tree enttype = TREE_TYPE (type);
18136           enum machine_mode mode = TYPE_MODE (enttype);
18137
18138           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
18139             return false;
18140           if (int_size_in_bytes (type) != size)
18141             return false;
18142           if (size > TREE_STRING_LENGTH (init))
18143             {
18144               memcpy (array, TREE_STRING_POINTER (init),
18145                       TREE_STRING_LENGTH (init));
18146               memset (array + TREE_STRING_LENGTH (init),
18147                       '\0', size - TREE_STRING_LENGTH (init));
18148             }
18149           else
18150             memcpy (array, TREE_STRING_POINTER (init), size);
18151           return true;
18152         }
18153       return false;
18154     case CONSTRUCTOR:
18155       type = TREE_TYPE (init);
18156       if (int_size_in_bytes (type) != size)
18157         return false;
18158       if (TREE_CODE (type) == ARRAY_TYPE)
18159         {
18160           HOST_WIDE_INT min_index;
18161           unsigned HOST_WIDE_INT cnt;
18162           int curpos = 0, fieldsize;
18163           constructor_elt *ce;
18164
18165           if (TYPE_DOMAIN (type) == NULL_TREE
18166               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
18167             return false;
18168
18169           fieldsize = int_size_in_bytes (TREE_TYPE (type));
18170           if (fieldsize <= 0)
18171             return false;
18172
18173           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
18174           memset (array, '\0', size);
18175           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
18176             {
18177               tree val = ce->value;
18178               tree index = ce->index;
18179               int pos = curpos;
18180               if (index && TREE_CODE (index) == RANGE_EXPR)
18181                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
18182                       * fieldsize;
18183               else if (index)
18184                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
18185
18186               if (val)
18187                 {
18188                   STRIP_NOPS (val);
18189                   if (!native_encode_initializer (val, array + pos, fieldsize))
18190                     return false;
18191                 }
18192               curpos = pos + fieldsize;
18193               if (index && TREE_CODE (index) == RANGE_EXPR)
18194                 {
18195                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
18196                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
18197                   while (count-- > 0)
18198                     {
18199                       if (val)
18200                         memcpy (array + curpos, array + pos, fieldsize);
18201                       curpos += fieldsize;
18202                     }
18203                 }
18204               gcc_assert (curpos <= size);
18205             }
18206           return true;
18207         }
18208       else if (TREE_CODE (type) == RECORD_TYPE
18209                || TREE_CODE (type) == UNION_TYPE)
18210         {
18211           tree field = NULL_TREE;
18212           unsigned HOST_WIDE_INT cnt;
18213           constructor_elt *ce;
18214
18215           if (int_size_in_bytes (type) != size)
18216             return false;
18217
18218           if (TREE_CODE (type) == RECORD_TYPE)
18219             field = TYPE_FIELDS (type);
18220
18221           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
18222             {
18223               tree val = ce->value;
18224               int pos, fieldsize;
18225
18226               if (ce->index != 0)
18227                 field = ce->index;
18228
18229               if (val)
18230                 STRIP_NOPS (val);
18231
18232               if (field == NULL_TREE || DECL_BIT_FIELD (field))
18233                 return false;
18234
18235               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
18236                   && TYPE_DOMAIN (TREE_TYPE (field))
18237                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
18238                 return false;
18239               else if (DECL_SIZE_UNIT (field) == NULL_TREE
18240                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
18241                 return false;
18242               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
18243               pos = int_byte_position (field);
18244               gcc_assert (pos + fieldsize <= size);
18245               if (val
18246                   && !native_encode_initializer (val, array + pos, fieldsize))
18247                 return false;
18248             }
18249           return true;
18250         }
18251       return false;
18252     case VIEW_CONVERT_EXPR:
18253     case NON_LVALUE_EXPR:
18254       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
18255     default:
18256       return native_encode_expr (init, array, size) == size;
18257     }
18258 }
18259
18260 /* Attach a DW_AT_const_value attribute to DIE. The value of the
18261    attribute is the const value T.  */
18262
18263 static bool
18264 tree_add_const_value_attribute (dw_die_ref die, tree t)
18265 {
18266   tree init;
18267   tree type = TREE_TYPE (t);
18268   rtx rtl;
18269
18270   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
18271     return false;
18272
18273   init = t;
18274   gcc_assert (!DECL_P (init));
18275
18276   rtl = rtl_for_decl_init (init, type);
18277   if (rtl)
18278     return add_const_value_attribute (die, rtl);
18279   /* If the host and target are sane, try harder.  */
18280   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
18281            && initializer_constant_valid_p (init, type))
18282     {
18283       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
18284       if (size > 0 && (int) size == size)
18285         {
18286           unsigned char *array = (unsigned char *)
18287             ggc_alloc_cleared_atomic (size);
18288
18289           if (native_encode_initializer (init, array, size))
18290             {
18291               add_AT_vec (die, DW_AT_const_value, size, 1, array);
18292               return true;
18293             }
18294         }
18295     }
18296   return false;
18297 }
18298
18299 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
18300    attribute is the const value of T, where T is an integral constant
18301    variable with static storage duration
18302    (so it can't be a PARM_DECL or a RESULT_DECL).  */
18303
18304 static bool
18305 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
18306 {
18307
18308   if (!decl
18309       || (TREE_CODE (decl) != VAR_DECL
18310           && TREE_CODE (decl) != CONST_DECL)
18311       || (TREE_CODE (decl) == VAR_DECL
18312           && !TREE_STATIC (decl)))
18313     return false;
18314
18315     if (TREE_READONLY (decl)
18316         && ! TREE_THIS_VOLATILE (decl)
18317         && DECL_INITIAL (decl))
18318       /* OK */;
18319     else
18320       return false;
18321
18322   /* Don't add DW_AT_const_value if abstract origin already has one.  */
18323   if (get_AT (var_die, DW_AT_const_value))
18324     return false;
18325
18326   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
18327 }
18328
18329 /* Convert the CFI instructions for the current function into a
18330    location list.  This is used for DW_AT_frame_base when we targeting
18331    a dwarf2 consumer that does not support the dwarf3
18332    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
18333    expressions.  */
18334
18335 static dw_loc_list_ref
18336 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
18337 {
18338   int ix;
18339   dw_fde_ref fde;
18340   dw_loc_list_ref list, *list_tail;
18341   dw_cfi_ref cfi;
18342   dw_cfa_location last_cfa, next_cfa;
18343   const char *start_label, *last_label, *section;
18344   dw_cfa_location remember;
18345
18346   fde = current_fde ();
18347   gcc_assert (fde != NULL);
18348
18349   section = secname_for_decl (current_function_decl);
18350   list_tail = &list;
18351   list = NULL;
18352
18353   memset (&next_cfa, 0, sizeof (next_cfa));
18354   next_cfa.reg = INVALID_REGNUM;
18355   remember = next_cfa;
18356
18357   start_label = fde->dw_fde_begin;
18358
18359   /* ??? Bald assumption that the CIE opcode list does not contain
18360      advance opcodes.  */
18361   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
18362     lookup_cfa_1 (cfi, &next_cfa, &remember);
18363
18364   last_cfa = next_cfa;
18365   last_label = start_label;
18366
18367   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
18368     {
18369       /* If the first partition contained no CFI adjustments, the
18370          CIE opcodes apply to the whole first partition.  */
18371       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18372                                  fde->dw_fde_begin, fde->dw_fde_end, section);
18373       list_tail =&(*list_tail)->dw_loc_next;
18374       start_label = last_label = fde->dw_fde_second_begin;
18375     }
18376
18377   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
18378     {
18379       switch (cfi->dw_cfi_opc)
18380         {
18381         case DW_CFA_set_loc:
18382         case DW_CFA_advance_loc1:
18383         case DW_CFA_advance_loc2:
18384         case DW_CFA_advance_loc4:
18385           if (!cfa_equal_p (&last_cfa, &next_cfa))
18386             {
18387               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18388                                          start_label, last_label, section);
18389
18390               list_tail = &(*list_tail)->dw_loc_next;
18391               last_cfa = next_cfa;
18392               start_label = last_label;
18393             }
18394           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18395           break;
18396
18397         case DW_CFA_advance_loc:
18398           /* The encoding is complex enough that we should never emit this.  */
18399           gcc_unreachable ();
18400
18401         default:
18402           lookup_cfa_1 (cfi, &next_cfa, &remember);
18403           break;
18404         }
18405       if (ix + 1 == fde->dw_fde_switch_cfi_index)
18406         {
18407           if (!cfa_equal_p (&last_cfa, &next_cfa))
18408             {
18409               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18410                                          start_label, last_label, section);
18411
18412               list_tail = &(*list_tail)->dw_loc_next;
18413               last_cfa = next_cfa;
18414               start_label = last_label;
18415             }
18416           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18417                                      start_label, fde->dw_fde_end, section);
18418           list_tail = &(*list_tail)->dw_loc_next;
18419           start_label = last_label = fde->dw_fde_second_begin;
18420         }
18421     }
18422
18423   if (!cfa_equal_p (&last_cfa, &next_cfa))
18424     {
18425       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18426                                  start_label, last_label, section);
18427       list_tail = &(*list_tail)->dw_loc_next;
18428       start_label = last_label;
18429     }
18430
18431   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18432                              start_label,
18433                              fde->dw_fde_second_begin
18434                              ? fde->dw_fde_second_end : fde->dw_fde_end,
18435                              section);
18436
18437   if (list && list->dw_loc_next)
18438     gen_llsym (list);
18439
18440   return list;
18441 }
18442
18443 /* Compute a displacement from the "steady-state frame pointer" to the
18444    frame base (often the same as the CFA), and store it in
18445    frame_pointer_fb_offset.  OFFSET is added to the displacement
18446    before the latter is negated.  */
18447
18448 static void
18449 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18450 {
18451   rtx reg, elim;
18452
18453 #ifdef FRAME_POINTER_CFA_OFFSET
18454   reg = frame_pointer_rtx;
18455   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18456 #else
18457   reg = arg_pointer_rtx;
18458   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18459 #endif
18460
18461   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
18462   if (GET_CODE (elim) == PLUS)
18463     {
18464       offset += INTVAL (XEXP (elim, 1));
18465       elim = XEXP (elim, 0);
18466     }
18467
18468   frame_pointer_fb_offset = -offset;
18469
18470   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18471      in which to eliminate.  This is because it's stack pointer isn't 
18472      directly accessible as a register within the ISA.  To work around
18473      this, assume that while we cannot provide a proper value for
18474      frame_pointer_fb_offset, we won't need one either.  */
18475   frame_pointer_fb_offset_valid
18476     = ((SUPPORTS_STACK_ALIGNMENT
18477         && (elim == hard_frame_pointer_rtx
18478             || elim == stack_pointer_rtx))
18479        || elim == (frame_pointer_needed
18480                    ? hard_frame_pointer_rtx
18481                    : stack_pointer_rtx));
18482 }
18483
18484 /* Generate a DW_AT_name attribute given some string value to be included as
18485    the value of the attribute.  */
18486
18487 static void
18488 add_name_attribute (dw_die_ref die, const char *name_string)
18489 {
18490   if (name_string != NULL && *name_string != 0)
18491     {
18492       if (demangle_name_func)
18493         name_string = (*demangle_name_func) (name_string);
18494
18495       add_AT_string (die, DW_AT_name, name_string);
18496     }
18497 }
18498
18499 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18500    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18501    of TYPE accordingly.
18502
18503    ??? This is a temporary measure until after we're able to generate
18504    regular DWARF for the complex Ada type system.  */
18505
18506 static void 
18507 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18508                                      dw_die_ref context_die)
18509 {
18510   tree dtype;
18511   dw_die_ref dtype_die;
18512
18513   if (!lang_hooks.types.descriptive_type)
18514     return;
18515
18516   dtype = lang_hooks.types.descriptive_type (type);
18517   if (!dtype)
18518     return;
18519
18520   dtype_die = lookup_type_die (dtype);
18521   if (!dtype_die)
18522     {
18523       gen_type_die (dtype, context_die);
18524       dtype_die = lookup_type_die (dtype);
18525       gcc_assert (dtype_die);
18526     }
18527
18528   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18529 }
18530
18531 /* Generate a DW_AT_comp_dir attribute for DIE.  */
18532
18533 static void
18534 add_comp_dir_attribute (dw_die_ref die)
18535 {
18536   const char *wd = get_src_pwd ();
18537   char *wd1;
18538
18539   if (wd == NULL)
18540     return;
18541
18542   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18543     {
18544       int wdlen;
18545
18546       wdlen = strlen (wd);
18547       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
18548       strcpy (wd1, wd);
18549       wd1 [wdlen] = DIR_SEPARATOR;
18550       wd1 [wdlen + 1] = 0;
18551       wd = wd1;
18552     }
18553
18554     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
18555 }
18556
18557 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18558    default.  */
18559
18560 static int
18561 lower_bound_default (void)
18562 {
18563   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18564     {
18565     case DW_LANG_C:
18566     case DW_LANG_C89:
18567     case DW_LANG_C99:
18568     case DW_LANG_C_plus_plus:
18569     case DW_LANG_ObjC:
18570     case DW_LANG_ObjC_plus_plus:
18571     case DW_LANG_Java:
18572       return 0;
18573     case DW_LANG_Fortran77:
18574     case DW_LANG_Fortran90:
18575     case DW_LANG_Fortran95:
18576       return 1;
18577     case DW_LANG_UPC:
18578     case DW_LANG_D:
18579     case DW_LANG_Python:
18580       return dwarf_version >= 4 ? 0 : -1;
18581     case DW_LANG_Ada95:
18582     case DW_LANG_Ada83:
18583     case DW_LANG_Cobol74:
18584     case DW_LANG_Cobol85:
18585     case DW_LANG_Pascal83:
18586     case DW_LANG_Modula2:
18587     case DW_LANG_PLI:
18588       return dwarf_version >= 4 ? 1 : -1;
18589     default:
18590       return -1;
18591     }
18592 }
18593
18594 /* Given a tree node describing an array bound (either lower or upper) output
18595    a representation for that bound.  */
18596
18597 static void
18598 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
18599 {
18600   switch (TREE_CODE (bound))
18601     {
18602     case ERROR_MARK:
18603       return;
18604
18605     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
18606     case INTEGER_CST:
18607       {
18608         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
18609         int dflt;
18610
18611         /* Use the default if possible.  */
18612         if (bound_attr == DW_AT_lower_bound
18613             && host_integerp (bound, 0)
18614             && (dflt = lower_bound_default ()) != -1
18615             && tree_low_cst (bound, 0) == dflt)
18616           ;
18617
18618         /* Otherwise represent the bound as an unsigned value with the
18619            precision of its type.  The precision and signedness of the
18620            type will be necessary to re-interpret it unambiguously.  */
18621         else if (prec < HOST_BITS_PER_WIDE_INT)
18622           {
18623             unsigned HOST_WIDE_INT mask
18624               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
18625             add_AT_unsigned (subrange_die, bound_attr,
18626                              TREE_INT_CST_LOW (bound) & mask);
18627           }
18628         else if (prec == HOST_BITS_PER_WIDE_INT
18629                  || TREE_INT_CST_HIGH (bound) == 0)
18630           add_AT_unsigned (subrange_die, bound_attr,
18631                            TREE_INT_CST_LOW (bound));
18632         else
18633           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
18634                          TREE_INT_CST_LOW (bound));
18635       }
18636       break;
18637
18638     CASE_CONVERT:
18639     case VIEW_CONVERT_EXPR:
18640       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
18641       break;
18642
18643     case SAVE_EXPR:
18644       break;
18645
18646     case VAR_DECL:
18647     case PARM_DECL:
18648     case RESULT_DECL:
18649       {
18650         dw_die_ref decl_die = lookup_decl_die (bound);
18651
18652         /* ??? Can this happen, or should the variable have been bound
18653            first?  Probably it can, since I imagine that we try to create
18654            the types of parameters in the order in which they exist in
18655            the list, and won't have created a forward reference to a
18656            later parameter.  */
18657         if (decl_die != NULL)
18658           {
18659             add_AT_die_ref (subrange_die, bound_attr, decl_die);
18660             break;
18661           }
18662       }
18663       /* FALLTHRU */
18664
18665     default:
18666       {
18667         /* Otherwise try to create a stack operation procedure to
18668            evaluate the value of the array bound.  */
18669
18670         dw_die_ref ctx, decl_die;
18671         dw_loc_list_ref list;
18672
18673         list = loc_list_from_tree (bound, 2);
18674         if (list == NULL || single_element_loc_list_p (list))
18675           {
18676             /* If DW_AT_*bound is not a reference nor constant, it is
18677                a DWARF expression rather than location description.
18678                For that loc_list_from_tree (bound, 0) is needed.
18679                If that fails to give a single element list,
18680                fall back to outputting this as a reference anyway.  */
18681             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
18682             if (list2 && single_element_loc_list_p (list2))
18683               {
18684                 add_AT_loc (subrange_die, bound_attr, list2->expr);
18685                 break;
18686               }
18687           }
18688         if (list == NULL)
18689           break;
18690
18691         if (current_function_decl == 0)
18692           ctx = comp_unit_die ();
18693         else
18694           ctx = lookup_decl_die (current_function_decl);
18695
18696         decl_die = new_die (DW_TAG_variable, ctx, bound);
18697         add_AT_flag (decl_die, DW_AT_artificial, 1);
18698         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
18699         add_AT_location_description (decl_die, DW_AT_location, list);
18700         add_AT_die_ref (subrange_die, bound_attr, decl_die);
18701         break;
18702       }
18703     }
18704 }
18705
18706 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18707    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18708    Note that the block of subscript information for an array type also
18709    includes information about the element type of the given array type.  */
18710
18711 static void
18712 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18713 {
18714   unsigned dimension_number;
18715   tree lower, upper;
18716   dw_die_ref subrange_die;
18717
18718   for (dimension_number = 0;
18719        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18720        type = TREE_TYPE (type), dimension_number++)
18721     {
18722       tree domain = TYPE_DOMAIN (type);
18723
18724       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18725         break;
18726
18727       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18728          and (in GNU C only) variable bounds.  Handle all three forms
18729          here.  */
18730       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18731       if (domain)
18732         {
18733           /* We have an array type with specified bounds.  */
18734           lower = TYPE_MIN_VALUE (domain);
18735           upper = TYPE_MAX_VALUE (domain);
18736
18737           /* Define the index type.  */
18738           if (TREE_TYPE (domain))
18739             {
18740               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
18741                  TREE_TYPE field.  We can't emit debug info for this
18742                  because it is an unnamed integral type.  */
18743               if (TREE_CODE (domain) == INTEGER_TYPE
18744                   && TYPE_NAME (domain) == NULL_TREE
18745                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18746                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18747                 ;
18748               else
18749                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
18750                                     type_die);
18751             }
18752
18753           /* ??? If upper is NULL, the array has unspecified length,
18754              but it does have a lower bound.  This happens with Fortran
18755                dimension arr(N:*)
18756              Since the debugger is definitely going to need to know N
18757              to produce useful results, go ahead and output the lower
18758              bound solo, and hope the debugger can cope.  */
18759
18760           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
18761           if (upper)
18762             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
18763         }
18764
18765       /* Otherwise we have an array type with an unspecified length.  The
18766          DWARF-2 spec does not say how to handle this; let's just leave out the
18767          bounds.  */
18768     }
18769 }
18770
18771 static void
18772 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18773 {
18774   unsigned size;
18775
18776   switch (TREE_CODE (tree_node))
18777     {
18778     case ERROR_MARK:
18779       size = 0;
18780       break;
18781     case ENUMERAL_TYPE:
18782     case RECORD_TYPE:
18783     case UNION_TYPE:
18784     case QUAL_UNION_TYPE:
18785       size = int_size_in_bytes (tree_node);
18786       break;
18787     case FIELD_DECL:
18788       /* For a data member of a struct or union, the DW_AT_byte_size is
18789          generally given as the number of bytes normally allocated for an
18790          object of the *declared* type of the member itself.  This is true
18791          even for bit-fields.  */
18792       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
18793       break;
18794     default:
18795       gcc_unreachable ();
18796     }
18797
18798   /* Note that `size' might be -1 when we get to this point.  If it is, that
18799      indicates that the byte size of the entity in question is variable.  We
18800      have no good way of expressing this fact in Dwarf at the present time,
18801      so just let the -1 pass on through.  */
18802   add_AT_unsigned (die, DW_AT_byte_size, size);
18803 }
18804
18805 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18806    which specifies the distance in bits from the highest order bit of the
18807    "containing object" for the bit-field to the highest order bit of the
18808    bit-field itself.
18809
18810    For any given bit-field, the "containing object" is a hypothetical object
18811    (of some integral or enum type) within which the given bit-field lives.  The
18812    type of this hypothetical "containing object" is always the same as the
18813    declared type of the individual bit-field itself.  The determination of the
18814    exact location of the "containing object" for a bit-field is rather
18815    complicated.  It's handled by the `field_byte_offset' function (above).
18816
18817    Note that it is the size (in bytes) of the hypothetical "containing object"
18818    which will be given in the DW_AT_byte_size attribute for this bit-field.
18819    (See `byte_size_attribute' above).  */
18820
18821 static inline void
18822 add_bit_offset_attribute (dw_die_ref die, tree decl)
18823 {
18824   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
18825   tree type = DECL_BIT_FIELD_TYPE (decl);
18826   HOST_WIDE_INT bitpos_int;
18827   HOST_WIDE_INT highest_order_object_bit_offset;
18828   HOST_WIDE_INT highest_order_field_bit_offset;
18829   HOST_WIDE_INT bit_offset;
18830
18831   /* Must be a field and a bit field.  */
18832   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
18833
18834   /* We can't yet handle bit-fields whose offsets are variable, so if we
18835      encounter such things, just return without generating any attribute
18836      whatsoever.  Likewise for variable or too large size.  */
18837   if (! host_integerp (bit_position (decl), 0)
18838       || ! host_integerp (DECL_SIZE (decl), 1))
18839     return;
18840
18841   bitpos_int = int_bit_position (decl);
18842
18843   /* Note that the bit offset is always the distance (in bits) from the
18844      highest-order bit of the "containing object" to the highest-order bit of
18845      the bit-field itself.  Since the "high-order end" of any object or field
18846      is different on big-endian and little-endian machines, the computation
18847      below must take account of these differences.  */
18848   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18849   highest_order_field_bit_offset = bitpos_int;
18850
18851   if (! BYTES_BIG_ENDIAN)
18852     {
18853       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
18854       highest_order_object_bit_offset += simple_type_size_in_bits (type);
18855     }
18856
18857   bit_offset
18858     = (! BYTES_BIG_ENDIAN
18859        ? highest_order_object_bit_offset - highest_order_field_bit_offset
18860        : highest_order_field_bit_offset - highest_order_object_bit_offset);
18861
18862   if (bit_offset < 0)
18863     add_AT_int (die, DW_AT_bit_offset, bit_offset);
18864   else
18865     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18866 }
18867
18868 /* For a FIELD_DECL node which represents a bit field, output an attribute
18869    which specifies the length in bits of the given field.  */
18870
18871 static inline void
18872 add_bit_size_attribute (dw_die_ref die, tree decl)
18873 {
18874   /* Must be a field and a bit field.  */
18875   gcc_assert (TREE_CODE (decl) == FIELD_DECL
18876               && DECL_BIT_FIELD_TYPE (decl));
18877
18878   if (host_integerp (DECL_SIZE (decl), 1))
18879     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
18880 }
18881
18882 /* If the compiled language is ANSI C, then add a 'prototyped'
18883    attribute, if arg types are given for the parameters of a function.  */
18884
18885 static inline void
18886 add_prototyped_attribute (dw_die_ref die, tree func_type)
18887 {
18888   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
18889       && prototype_p (func_type))
18890     add_AT_flag (die, DW_AT_prototyped, 1);
18891 }
18892
18893 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
18894    by looking in either the type declaration or object declaration
18895    equate table.  */
18896
18897 static inline dw_die_ref
18898 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18899 {
18900   dw_die_ref origin_die = NULL;
18901
18902   if (TREE_CODE (origin) != FUNCTION_DECL)
18903     {
18904       /* We may have gotten separated from the block for the inlined
18905          function, if we're in an exception handler or some such; make
18906          sure that the abstract function has been written out.
18907
18908          Doing this for nested functions is wrong, however; functions are
18909          distinct units, and our context might not even be inline.  */
18910       tree fn = origin;
18911
18912       if (TYPE_P (fn))
18913         fn = TYPE_STUB_DECL (fn);
18914
18915       fn = decl_function_context (fn);
18916       if (fn)
18917         dwarf2out_abstract_function (fn);
18918     }
18919
18920   if (DECL_P (origin))
18921     origin_die = lookup_decl_die (origin);
18922   else if (TYPE_P (origin))
18923     origin_die = lookup_type_die (origin);
18924
18925   /* XXX: Functions that are never lowered don't always have correct block
18926      trees (in the case of java, they simply have no block tree, in some other
18927      languages).  For these functions, there is nothing we can really do to
18928      output correct debug info for inlined functions in all cases.  Rather
18929      than die, we'll just produce deficient debug info now, in that we will
18930      have variables without a proper abstract origin.  In the future, when all
18931      functions are lowered, we should re-add a gcc_assert (origin_die)
18932      here.  */
18933
18934   if (origin_die)
18935     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18936   return origin_die;
18937 }
18938
18939 /* We do not currently support the pure_virtual attribute.  */
18940
18941 static inline void
18942 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18943 {
18944   if (DECL_VINDEX (func_decl))
18945     {
18946       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18947
18948       if (host_integerp (DECL_VINDEX (func_decl), 0))
18949         add_AT_loc (die, DW_AT_vtable_elem_location,
18950                     new_loc_descr (DW_OP_constu,
18951                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
18952                                    0));
18953
18954       /* GNU extension: Record what type this method came from originally.  */
18955       if (debug_info_level > DINFO_LEVEL_TERSE
18956           && DECL_CONTEXT (func_decl))
18957         add_AT_die_ref (die, DW_AT_containing_type,
18958                         lookup_type_die (DECL_CONTEXT (func_decl)));
18959     }
18960 }
18961 \f
18962 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18963    given decl.  This used to be a vendor extension until after DWARF 4
18964    standardized it.  */
18965
18966 static void
18967 add_linkage_attr (dw_die_ref die, tree decl)
18968 {
18969   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18970
18971   /* Mimic what assemble_name_raw does with a leading '*'.  */
18972   if (name[0] == '*')
18973     name = &name[1];
18974
18975   if (dwarf_version >= 4)
18976     add_AT_string (die, DW_AT_linkage_name, name);
18977   else
18978     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18979 }
18980
18981 /* Add source coordinate attributes for the given decl.  */
18982
18983 static void
18984 add_src_coords_attributes (dw_die_ref die, tree decl)
18985 {
18986   expanded_location s;
18987
18988   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
18989     return;
18990   s = expand_location (DECL_SOURCE_LOCATION (decl));
18991   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18992   add_AT_unsigned (die, DW_AT_decl_line, s.line);
18993 }
18994
18995 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
18996
18997 static void
18998 add_linkage_name (dw_die_ref die, tree decl)
18999 {
19000   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
19001        && TREE_PUBLIC (decl)
19002        && !DECL_ABSTRACT (decl)
19003        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
19004        && die->die_tag != DW_TAG_member)
19005     {
19006       /* Defer until we have an assembler name set.  */
19007       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
19008         {
19009           limbo_die_node *asm_name;
19010
19011           asm_name = ggc_alloc_cleared_limbo_die_node ();
19012           asm_name->die = die;
19013           asm_name->created_for = decl;
19014           asm_name->next = deferred_asm_name;
19015           deferred_asm_name = asm_name;
19016         }
19017       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
19018         add_linkage_attr (die, decl);
19019     }
19020 }
19021
19022 /* Add a DW_AT_name attribute and source coordinate attribute for the
19023    given decl, but only if it actually has a name.  */
19024
19025 static void
19026 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
19027 {
19028   tree decl_name;
19029
19030   decl_name = DECL_NAME (decl);
19031   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
19032     {
19033       const char *name = dwarf2_name (decl, 0);
19034       if (name)
19035         add_name_attribute (die, name);
19036       if (! DECL_ARTIFICIAL (decl))
19037         add_src_coords_attributes (die, decl);
19038
19039       add_linkage_name (die, decl);
19040     }
19041
19042 #ifdef VMS_DEBUGGING_INFO
19043   /* Get the function's name, as described by its RTL.  This may be different
19044      from the DECL_NAME name used in the source file.  */
19045   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
19046     {
19047       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
19048                    XEXP (DECL_RTL (decl), 0));
19049       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
19050     }
19051 #endif /* VMS_DEBUGGING_INFO */
19052 }
19053
19054 #ifdef VMS_DEBUGGING_INFO
19055 /* Output the debug main pointer die for VMS */
19056
19057 void
19058 dwarf2out_vms_debug_main_pointer (void)
19059 {
19060   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19061   dw_die_ref die;
19062
19063   /* Allocate the VMS debug main subprogram die.  */
19064   die = ggc_alloc_cleared_die_node ();
19065   die->die_tag = DW_TAG_subprogram;
19066   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
19067   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
19068                                current_function_funcdef_no);
19069   add_AT_lbl_id (die, DW_AT_entry_pc, label);
19070
19071   /* Make it the first child of comp_unit_die ().  */
19072   die->die_parent = comp_unit_die ();
19073   if (comp_unit_die ()->die_child)
19074     {
19075       die->die_sib = comp_unit_die ()->die_child->die_sib;
19076       comp_unit_die ()->die_child->die_sib = die;
19077     }
19078   else
19079     {
19080       die->die_sib = die;
19081       comp_unit_die ()->die_child = die;
19082     }
19083 }
19084 #endif /* VMS_DEBUGGING_INFO */
19085
19086 /* Push a new declaration scope.  */
19087
19088 static void
19089 push_decl_scope (tree scope)
19090 {
19091   VEC_safe_push (tree, gc, decl_scope_table, scope);
19092 }
19093
19094 /* Pop a declaration scope.  */
19095
19096 static inline void
19097 pop_decl_scope (void)
19098 {
19099   VEC_pop (tree, decl_scope_table);
19100 }
19101
19102 /* Return the DIE for the scope that immediately contains this type.
19103    Non-named types get global scope.  Named types nested in other
19104    types get their containing scope if it's open, or global scope
19105    otherwise.  All other types (i.e. function-local named types) get
19106    the current active scope.  */
19107
19108 static dw_die_ref
19109 scope_die_for (tree t, dw_die_ref context_die)
19110 {
19111   dw_die_ref scope_die = NULL;
19112   tree containing_scope;
19113   int i;
19114
19115   /* Non-types always go in the current scope.  */
19116   gcc_assert (TYPE_P (t));
19117
19118   containing_scope = TYPE_CONTEXT (t);
19119
19120   /* Use the containing namespace if it was passed in (for a declaration).  */
19121   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
19122     {
19123       if (context_die == lookup_decl_die (containing_scope))
19124         /* OK */;
19125       else
19126         containing_scope = NULL_TREE;
19127     }
19128
19129   /* Ignore function type "scopes" from the C frontend.  They mean that
19130      a tagged type is local to a parmlist of a function declarator, but
19131      that isn't useful to DWARF.  */
19132   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19133     containing_scope = NULL_TREE;
19134
19135   if (SCOPE_FILE_SCOPE_P (containing_scope))
19136     scope_die = comp_unit_die ();
19137   else if (TYPE_P (containing_scope))
19138     {
19139       /* For types, we can just look up the appropriate DIE.  But
19140          first we check to see if we're in the middle of emitting it
19141          so we know where the new DIE should go.  */
19142       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
19143         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
19144           break;
19145
19146       if (i < 0)
19147         {
19148           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
19149                       || TREE_ASM_WRITTEN (containing_scope));
19150           /*We are not in the middle of emitting the type
19151             CONTAINING_SCOPE. Let's see if it's emitted already.  */
19152           scope_die = lookup_type_die (containing_scope);
19153
19154           /* If none of the current dies are suitable, we get file scope.  */
19155           if (scope_die == NULL)
19156             scope_die = comp_unit_die ();
19157         }
19158       else
19159         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19160     }
19161   else
19162     scope_die = context_die;
19163
19164   return scope_die;
19165 }
19166
19167 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
19168
19169 static inline int
19170 local_scope_p (dw_die_ref context_die)
19171 {
19172   for (; context_die; context_die = context_die->die_parent)
19173     if (context_die->die_tag == DW_TAG_inlined_subroutine
19174         || context_die->die_tag == DW_TAG_subprogram)
19175       return 1;
19176
19177   return 0;
19178 }
19179
19180 /* Returns nonzero if CONTEXT_DIE is a class.  */
19181
19182 static inline int
19183 class_scope_p (dw_die_ref context_die)
19184 {
19185   return (context_die
19186           && (context_die->die_tag == DW_TAG_structure_type
19187               || context_die->die_tag == DW_TAG_class_type
19188               || context_die->die_tag == DW_TAG_interface_type
19189               || context_die->die_tag == DW_TAG_union_type));
19190 }
19191
19192 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19193    whether or not to treat a DIE in this context as a declaration.  */
19194
19195 static inline int
19196 class_or_namespace_scope_p (dw_die_ref context_die)
19197 {
19198   return (class_scope_p (context_die)
19199           || (context_die && context_die->die_tag == DW_TAG_namespace));
19200 }
19201
19202 /* Many forms of DIEs require a "type description" attribute.  This
19203    routine locates the proper "type descriptor" die for the type given
19204    by 'type', and adds a DW_AT_type attribute below the given die.  */
19205
19206 static void
19207 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
19208                     int decl_volatile, dw_die_ref context_die)
19209 {
19210   enum tree_code code  = TREE_CODE (type);
19211   dw_die_ref type_die  = NULL;
19212
19213   /* ??? If this type is an unnamed subrange type of an integral, floating-point
19214      or fixed-point type, use the inner type.  This is because we have no
19215      support for unnamed types in base_type_die.  This can happen if this is
19216      an Ada subrange type.  Correct solution is emit a subrange type die.  */
19217   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19218       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19219     type = TREE_TYPE (type), code = TREE_CODE (type);
19220
19221   if (code == ERROR_MARK
19222       /* Handle a special case.  For functions whose return type is void, we
19223          generate *no* type attribute.  (Note that no object may have type
19224          `void', so this only applies to function return types).  */
19225       || code == VOID_TYPE)
19226     return;
19227
19228   type_die = modified_type_die (type,
19229                                 decl_const || TYPE_READONLY (type),
19230                                 decl_volatile || TYPE_VOLATILE (type),
19231                                 context_die);
19232
19233   if (type_die != NULL)
19234     add_AT_die_ref (object_die, DW_AT_type, type_die);
19235 }
19236
19237 /* Given an object die, add the calling convention attribute for the
19238    function call type.  */
19239 static void
19240 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19241 {
19242   enum dwarf_calling_convention value = DW_CC_normal;
19243
19244   value = ((enum dwarf_calling_convention)
19245            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19246
19247   if (is_fortran ()
19248       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19249     {
19250       /* DWARF 2 doesn't provide a way to identify a program's source-level
19251         entry point.  DW_AT_calling_convention attributes are only meant
19252         to describe functions' calling conventions.  However, lacking a
19253         better way to signal the Fortran main program, we used this for 
19254         a long time, following existing custom.  Now, DWARF 4 has 
19255         DW_AT_main_subprogram, which we add below, but some tools still
19256         rely on the old way, which we thus keep.  */
19257       value = DW_CC_program;
19258
19259       if (dwarf_version >= 4 || !dwarf_strict)
19260         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19261     }
19262
19263   /* Only add the attribute if the backend requests it, and
19264      is not DW_CC_normal.  */
19265   if (value && (value != DW_CC_normal))
19266     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19267 }
19268
19269 /* Given a tree pointer to a struct, class, union, or enum type node, return
19270    a pointer to the (string) tag name for the given type, or zero if the type
19271    was declared without a tag.  */
19272
19273 static const char *
19274 type_tag (const_tree type)
19275 {
19276   const char *name = 0;
19277
19278   if (TYPE_NAME (type) != 0)
19279     {
19280       tree t = 0;
19281
19282       /* Find the IDENTIFIER_NODE for the type name.  */
19283       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19284           && !TYPE_NAMELESS (type))
19285         t = TYPE_NAME (type);
19286
19287       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19288          a TYPE_DECL node, regardless of whether or not a `typedef' was
19289          involved.  */
19290       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19291                && ! DECL_IGNORED_P (TYPE_NAME (type)))
19292         {
19293           /* We want to be extra verbose.  Don't call dwarf_name if
19294              DECL_NAME isn't set.  The default hook for decl_printable_name
19295              doesn't like that, and in this context it's correct to return
19296              0, instead of "<anonymous>" or the like.  */
19297           if (DECL_NAME (TYPE_NAME (type))
19298               && !DECL_NAMELESS (TYPE_NAME (type)))
19299             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19300         }
19301
19302       /* Now get the name as a string, or invent one.  */
19303       if (!name && t != 0)
19304         name = IDENTIFIER_POINTER (t);
19305     }
19306
19307   return (name == 0 || *name == '\0') ? 0 : name;
19308 }
19309
19310 /* Return the type associated with a data member, make a special check
19311    for bit field types.  */
19312
19313 static inline tree
19314 member_declared_type (const_tree member)
19315 {
19316   return (DECL_BIT_FIELD_TYPE (member)
19317           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19318 }
19319
19320 /* Get the decl's label, as described by its RTL. This may be different
19321    from the DECL_NAME name used in the source file.  */
19322
19323 #if 0
19324 static const char *
19325 decl_start_label (tree decl)
19326 {
19327   rtx x;
19328   const char *fnname;
19329
19330   x = DECL_RTL (decl);
19331   gcc_assert (MEM_P (x));
19332
19333   x = XEXP (x, 0);
19334   gcc_assert (GET_CODE (x) == SYMBOL_REF);
19335
19336   fnname = XSTR (x, 0);
19337   return fnname;
19338 }
19339 #endif
19340 \f
19341 /* These routines generate the internal representation of the DIE's for
19342    the compilation unit.  Debugging information is collected by walking
19343    the declaration trees passed in from dwarf2out_decl().  */
19344
19345 static void
19346 gen_array_type_die (tree type, dw_die_ref context_die)
19347 {
19348   dw_die_ref scope_die = scope_die_for (type, context_die);
19349   dw_die_ref array_die;
19350
19351   /* GNU compilers represent multidimensional array types as sequences of one
19352      dimensional array types whose element types are themselves array types.
19353      We sometimes squish that down to a single array_type DIE with multiple
19354      subscripts in the Dwarf debugging info.  The draft Dwarf specification
19355      say that we are allowed to do this kind of compression in C, because
19356      there is no difference between an array of arrays and a multidimensional
19357      array.  We don't do this for Ada to remain as close as possible to the
19358      actual representation, which is especially important against the language
19359      flexibilty wrt arrays of variable size.  */
19360
19361   bool collapse_nested_arrays = !is_ada ();
19362   tree element_type;
19363
19364   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19365      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
19366   if (TYPE_STRING_FLAG (type)
19367       && TREE_CODE (type) == ARRAY_TYPE
19368       && is_fortran ()
19369       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19370     {
19371       HOST_WIDE_INT size;
19372
19373       array_die = new_die (DW_TAG_string_type, scope_die, type);
19374       add_name_attribute (array_die, type_tag (type));
19375       equate_type_number_to_die (type, array_die);
19376       size = int_size_in_bytes (type);
19377       if (size >= 0)
19378         add_AT_unsigned (array_die, DW_AT_byte_size, size);
19379       else if (TYPE_DOMAIN (type) != NULL_TREE
19380                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
19381                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
19382         {
19383           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19384           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
19385
19386           size = int_size_in_bytes (TREE_TYPE (szdecl));
19387           if (loc && size > 0)
19388             {
19389               add_AT_location_description (array_die, DW_AT_string_length, loc);
19390               if (size != DWARF2_ADDR_SIZE)
19391                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19392             }
19393         }
19394       return;
19395     }
19396
19397   /* ??? The SGI dwarf reader fails for array of array of enum types
19398      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
19399      array type comes before the outer array type.  We thus call gen_type_die
19400      before we new_die and must prevent nested array types collapsing for this
19401      target.  */
19402
19403 #ifdef MIPS_DEBUGGING_INFO
19404   gen_type_die (TREE_TYPE (type), context_die);
19405   collapse_nested_arrays = false;
19406 #endif
19407
19408   array_die = new_die (DW_TAG_array_type, scope_die, type);
19409   add_name_attribute (array_die, type_tag (type));
19410   add_gnat_descriptive_type_attribute (array_die, type, context_die);
19411   equate_type_number_to_die (type, array_die);
19412
19413   if (TREE_CODE (type) == VECTOR_TYPE)
19414     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19415
19416   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
19417   if (is_fortran ()
19418       && TREE_CODE (type) == ARRAY_TYPE
19419       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19420       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19421     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19422
19423 #if 0
19424   /* We default the array ordering.  SDB will probably do
19425      the right things even if DW_AT_ordering is not present.  It's not even
19426      an issue until we start to get into multidimensional arrays anyway.  If
19427      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19428      then we'll have to put the DW_AT_ordering attribute back in.  (But if
19429      and when we find out that we need to put these in, we will only do so
19430      for multidimensional arrays.  */
19431   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19432 #endif
19433
19434 #ifdef MIPS_DEBUGGING_INFO
19435   /* The SGI compilers handle arrays of unknown bound by setting
19436      AT_declaration and not emitting any subrange DIEs.  */
19437   if (TREE_CODE (type) == ARRAY_TYPE
19438       && ! TYPE_DOMAIN (type))
19439     add_AT_flag (array_die, DW_AT_declaration, 1);
19440   else
19441 #endif
19442   if (TREE_CODE (type) == VECTOR_TYPE)
19443     {
19444       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
19445       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19446       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
19447       add_bound_info (subrange_die, DW_AT_upper_bound,
19448                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
19449     }
19450   else
19451     add_subscript_info (array_die, type, collapse_nested_arrays);
19452
19453   /* Add representation of the type of the elements of this array type and
19454      emit the corresponding DIE if we haven't done it already.  */
19455   element_type = TREE_TYPE (type);
19456   if (collapse_nested_arrays)
19457     while (TREE_CODE (element_type) == ARRAY_TYPE)
19458       {
19459         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19460           break;
19461         element_type = TREE_TYPE (element_type);
19462       }
19463
19464 #ifndef MIPS_DEBUGGING_INFO
19465   gen_type_die (element_type, context_die);
19466 #endif
19467
19468   add_type_attribute (array_die, element_type, 0, 0, context_die);
19469
19470   if (get_AT (array_die, DW_AT_name))
19471     add_pubtype (type, array_die);
19472 }
19473
19474 static dw_loc_descr_ref
19475 descr_info_loc (tree val, tree base_decl)
19476 {
19477   HOST_WIDE_INT size;
19478   dw_loc_descr_ref loc, loc2;
19479   enum dwarf_location_atom op;
19480
19481   if (val == base_decl)
19482     return new_loc_descr (DW_OP_push_object_address, 0, 0);
19483
19484   switch (TREE_CODE (val))
19485     {
19486     CASE_CONVERT:
19487       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19488     case VAR_DECL:
19489       return loc_descriptor_from_tree (val, 0);
19490     case INTEGER_CST:
19491       if (host_integerp (val, 0))
19492         return int_loc_descriptor (tree_low_cst (val, 0));
19493       break;
19494     case INDIRECT_REF:
19495       size = int_size_in_bytes (TREE_TYPE (val));
19496       if (size < 0)
19497         break;
19498       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19499       if (!loc)
19500         break;
19501       if (size == DWARF2_ADDR_SIZE)
19502         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
19503       else
19504         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
19505       return loc;
19506     case POINTER_PLUS_EXPR:
19507     case PLUS_EXPR:
19508       if (host_integerp (TREE_OPERAND (val, 1), 1)
19509           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
19510              < 16384)
19511         {
19512           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19513           if (!loc)
19514             break;
19515           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
19516         }
19517       else
19518         {
19519           op = DW_OP_plus;
19520         do_binop:
19521           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19522           if (!loc)
19523             break;
19524           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
19525           if (!loc2)
19526             break;
19527           add_loc_descr (&loc, loc2);
19528           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
19529         }
19530       return loc;
19531     case MINUS_EXPR:
19532       op = DW_OP_minus;
19533       goto do_binop;
19534     case MULT_EXPR:
19535       op = DW_OP_mul;
19536       goto do_binop;
19537     case EQ_EXPR:
19538       op = DW_OP_eq;
19539       goto do_binop;
19540     case NE_EXPR:
19541       op = DW_OP_ne;
19542       goto do_binop;
19543     default:
19544       break;
19545     }
19546   return NULL;
19547 }
19548
19549 static void
19550 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
19551                       tree val, tree base_decl)
19552 {
19553   dw_loc_descr_ref loc;
19554
19555   if (host_integerp (val, 0))
19556     {
19557       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
19558       return;
19559     }
19560
19561   loc = descr_info_loc (val, base_decl);
19562   if (!loc)
19563     return;
19564
19565   add_AT_loc (die, attr, loc);
19566 }
19567
19568 /* This routine generates DIE for array with hidden descriptor, details
19569    are filled into *info by a langhook.  */
19570
19571 static void
19572 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19573                           dw_die_ref context_die)
19574 {
19575   dw_die_ref scope_die = scope_die_for (type, context_die);
19576   dw_die_ref array_die;
19577   int dim;
19578
19579   array_die = new_die (DW_TAG_array_type, scope_die, type);
19580   add_name_attribute (array_die, type_tag (type));
19581   equate_type_number_to_die (type, array_die);
19582
19583   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
19584   if (is_fortran ()
19585       && info->ndimensions >= 2)
19586     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19587
19588   if (info->data_location)
19589     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
19590                           info->base_decl);
19591   if (info->associated)
19592     add_descr_info_field (array_die, DW_AT_associated, info->associated,
19593                           info->base_decl);
19594   if (info->allocated)
19595     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
19596                           info->base_decl);
19597
19598   for (dim = 0; dim < info->ndimensions; dim++)
19599     {
19600       dw_die_ref subrange_die
19601         = new_die (DW_TAG_subrange_type, array_die, NULL);
19602
19603       if (info->dimen[dim].lower_bound)
19604         {
19605           /* If it is the default value, omit it.  */
19606           int dflt;
19607
19608           if (host_integerp (info->dimen[dim].lower_bound, 0)
19609               && (dflt = lower_bound_default ()) != -1
19610               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
19611             ;
19612           else
19613             add_descr_info_field (subrange_die, DW_AT_lower_bound,
19614                                   info->dimen[dim].lower_bound,
19615                                   info->base_decl);
19616         }
19617       if (info->dimen[dim].upper_bound)
19618         add_descr_info_field (subrange_die, DW_AT_upper_bound,
19619                               info->dimen[dim].upper_bound,
19620                               info->base_decl);
19621       if (info->dimen[dim].stride)
19622         add_descr_info_field (subrange_die, DW_AT_byte_stride,
19623                               info->dimen[dim].stride,
19624                               info->base_decl);
19625     }
19626
19627   gen_type_die (info->element_type, context_die);
19628   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
19629
19630   if (get_AT (array_die, DW_AT_name))
19631     add_pubtype (type, array_die);
19632 }
19633
19634 #if 0
19635 static void
19636 gen_entry_point_die (tree decl, dw_die_ref context_die)
19637 {
19638   tree origin = decl_ultimate_origin (decl);
19639   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19640
19641   if (origin != NULL)
19642     add_abstract_origin_attribute (decl_die, origin);
19643   else
19644     {
19645       add_name_and_src_coords_attributes (decl_die, decl);
19646       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19647                           0, 0, context_die);
19648     }
19649
19650   if (DECL_ABSTRACT (decl))
19651     equate_decl_number_to_die (decl, decl_die);
19652   else
19653     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19654 }
19655 #endif
19656
19657 /* Walk through the list of incomplete types again, trying once more to
19658    emit full debugging info for them.  */
19659
19660 static void
19661 retry_incomplete_types (void)
19662 {
19663   int i;
19664
19665   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
19666     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
19667                                   DINFO_USAGE_DIR_USE))
19668       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
19669 }
19670
19671 /* Determine what tag to use for a record type.  */
19672
19673 static enum dwarf_tag
19674 record_type_tag (tree type)
19675 {
19676   if (! lang_hooks.types.classify_record)
19677     return DW_TAG_structure_type;
19678
19679   switch (lang_hooks.types.classify_record (type))
19680     {
19681     case RECORD_IS_STRUCT:
19682       return DW_TAG_structure_type;
19683
19684     case RECORD_IS_CLASS:
19685       return DW_TAG_class_type;
19686
19687     case RECORD_IS_INTERFACE:
19688       if (dwarf_version >= 3 || !dwarf_strict)
19689         return DW_TAG_interface_type;
19690       return DW_TAG_structure_type;
19691
19692     default:
19693       gcc_unreachable ();
19694     }
19695 }
19696
19697 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
19698    include all of the information about the enumeration values also. Each
19699    enumerated type name/value is listed as a child of the enumerated type
19700    DIE.  */
19701
19702 static dw_die_ref
19703 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19704 {
19705   dw_die_ref type_die = lookup_type_die (type);
19706
19707   if (type_die == NULL)
19708     {
19709       type_die = new_die (DW_TAG_enumeration_type,
19710                           scope_die_for (type, context_die), type);
19711       equate_type_number_to_die (type, type_die);
19712       add_name_attribute (type_die, type_tag (type));
19713       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19714       if (dwarf_version >= 4 || !dwarf_strict)
19715         {
19716           if (ENUM_IS_SCOPED (type))
19717             add_AT_flag (type_die, DW_AT_enum_class, 1);
19718           if (ENUM_IS_OPAQUE (type))
19719             add_AT_flag (type_die, DW_AT_declaration, 1);
19720         }
19721     }
19722   else if (! TYPE_SIZE (type))
19723     return type_die;
19724   else
19725     remove_AT (type_die, DW_AT_declaration);
19726
19727   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
19728      given enum type is incomplete, do not generate the DW_AT_byte_size
19729      attribute or the DW_AT_element_list attribute.  */
19730   if (TYPE_SIZE (type))
19731     {
19732       tree link;
19733
19734       TREE_ASM_WRITTEN (type) = 1;
19735       add_byte_size_attribute (type_die, type);
19736       if (TYPE_STUB_DECL (type) != NULL_TREE)
19737         {
19738           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19739           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19740         }
19741
19742       /* If the first reference to this type was as the return type of an
19743          inline function, then it may not have a parent.  Fix this now.  */
19744       if (type_die->die_parent == NULL)
19745         add_child_die (scope_die_for (type, context_die), type_die);
19746
19747       for (link = TYPE_VALUES (type);
19748            link != NULL; link = TREE_CHAIN (link))
19749         {
19750           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19751           tree value = TREE_VALUE (link);
19752
19753           add_name_attribute (enum_die,
19754                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19755
19756           if (TREE_CODE (value) == CONST_DECL)
19757             value = DECL_INITIAL (value);
19758
19759           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
19760             /* DWARF2 does not provide a way of indicating whether or
19761                not enumeration constants are signed or unsigned.  GDB
19762                always assumes the values are signed, so we output all
19763                values as if they were signed.  That means that
19764                enumeration constants with very large unsigned values
19765                will appear to have negative values in the debugger.  */
19766             add_AT_int (enum_die, DW_AT_const_value,
19767                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
19768         }
19769     }
19770   else
19771     add_AT_flag (type_die, DW_AT_declaration, 1);
19772
19773   if (get_AT (type_die, DW_AT_name))
19774     add_pubtype (type, type_die);
19775
19776   return type_die;
19777 }
19778
19779 /* Generate a DIE to represent either a real live formal parameter decl or to
19780    represent just the type of some formal parameter position in some function
19781    type.
19782
19783    Note that this routine is a bit unusual because its argument may be a
19784    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19785    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19786    node.  If it's the former then this function is being called to output a
19787    DIE to represent a formal parameter object (or some inlining thereof).  If
19788    it's the latter, then this function is only being called to output a
19789    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19790    argument type of some subprogram type.
19791    If EMIT_NAME_P is true, name and source coordinate attributes
19792    are emitted.  */
19793
19794 static dw_die_ref
19795 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19796                           dw_die_ref context_die)
19797 {
19798   tree node_or_origin = node ? node : origin;
19799   tree ultimate_origin;
19800   dw_die_ref parm_die
19801     = new_die (DW_TAG_formal_parameter, context_die, node);
19802
19803   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19804     {
19805     case tcc_declaration:
19806       ultimate_origin = decl_ultimate_origin (node_or_origin);
19807       if (node || ultimate_origin)
19808         origin = ultimate_origin;
19809       if (origin != NULL)
19810         add_abstract_origin_attribute (parm_die, origin);
19811       else if (emit_name_p)
19812         add_name_and_src_coords_attributes (parm_die, node);
19813       if (origin == NULL
19814           || (! DECL_ABSTRACT (node_or_origin)
19815               && variably_modified_type_p (TREE_TYPE (node_or_origin),
19816                                            decl_function_context
19817                                                             (node_or_origin))))
19818         {
19819           tree type = TREE_TYPE (node_or_origin);
19820           if (decl_by_reference_p (node_or_origin))
19821             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
19822                                 context_die);
19823           else
19824             add_type_attribute (parm_die, type,
19825                                 TREE_READONLY (node_or_origin),
19826                                 TREE_THIS_VOLATILE (node_or_origin),
19827                                 context_die);
19828         }
19829       if (origin == NULL && DECL_ARTIFICIAL (node))
19830         add_AT_flag (parm_die, DW_AT_artificial, 1);
19831
19832       if (node && node != origin)
19833         equate_decl_number_to_die (node, parm_die);
19834       if (! DECL_ABSTRACT (node_or_origin))
19835         add_location_or_const_value_attribute (parm_die, node_or_origin,
19836                                                node == NULL, DW_AT_location);
19837
19838       break;
19839
19840     case tcc_type:
19841       /* We were called with some kind of a ..._TYPE node.  */
19842       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
19843       break;
19844
19845     default:
19846       gcc_unreachable ();
19847     }
19848
19849   return parm_die;
19850 }
19851
19852 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19853    children DW_TAG_formal_parameter DIEs representing the arguments of the
19854    parameter pack.
19855
19856    PARM_PACK must be a function parameter pack.
19857    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19858    must point to the subsequent arguments of the function PACK_ARG belongs to.
19859    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19860    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19861    following the last one for which a DIE was generated.  */
19862
19863 static dw_die_ref
19864 gen_formal_parameter_pack_die  (tree parm_pack,
19865                                 tree pack_arg,
19866                                 dw_die_ref subr_die,
19867                                 tree *next_arg)
19868 {
19869   tree arg;
19870   dw_die_ref parm_pack_die;
19871
19872   gcc_assert (parm_pack
19873               && lang_hooks.function_parameter_pack_p (parm_pack)
19874               && subr_die);
19875
19876   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19877   add_src_coords_attributes (parm_pack_die, parm_pack);
19878
19879   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19880     {
19881       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19882                                                                  parm_pack))
19883         break;
19884       gen_formal_parameter_die (arg, NULL,
19885                                 false /* Don't emit name attribute.  */,
19886                                 parm_pack_die);
19887     }
19888   if (next_arg)
19889     *next_arg = arg;
19890   return parm_pack_die;
19891 }
19892
19893 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19894    at the end of an (ANSI prototyped) formal parameters list.  */
19895
19896 static void
19897 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19898 {
19899   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19900 }
19901
19902 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19903    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19904    parameters as specified in some function type specification (except for
19905    those which appear as part of a function *definition*).  */
19906
19907 static void
19908 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19909 {
19910   tree link;
19911   tree formal_type = NULL;
19912   tree first_parm_type;
19913   tree arg;
19914
19915   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19916     {
19917       arg = DECL_ARGUMENTS (function_or_method_type);
19918       function_or_method_type = TREE_TYPE (function_or_method_type);
19919     }
19920   else
19921     arg = NULL_TREE;
19922
19923   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19924
19925   /* Make our first pass over the list of formal parameter types and output a
19926      DW_TAG_formal_parameter DIE for each one.  */
19927   for (link = first_parm_type; link; )
19928     {
19929       dw_die_ref parm_die;
19930
19931       formal_type = TREE_VALUE (link);
19932       if (formal_type == void_type_node)
19933         break;
19934
19935       /* Output a (nameless) DIE to represent the formal parameter itself.  */
19936       parm_die = gen_formal_parameter_die (formal_type, NULL,
19937                                            true /* Emit name attribute.  */,
19938                                            context_die);
19939       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19940           && link == first_parm_type)
19941         {
19942           add_AT_flag (parm_die, DW_AT_artificial, 1);
19943           if (dwarf_version >= 3 || !dwarf_strict)
19944             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19945         }
19946       else if (arg && DECL_ARTIFICIAL (arg))
19947         add_AT_flag (parm_die, DW_AT_artificial, 1);
19948
19949       link = TREE_CHAIN (link);
19950       if (arg)
19951         arg = DECL_CHAIN (arg);
19952     }
19953
19954   /* If this function type has an ellipsis, add a
19955      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
19956   if (formal_type != void_type_node)
19957     gen_unspecified_parameters_die (function_or_method_type, context_die);
19958
19959   /* Make our second (and final) pass over the list of formal parameter types
19960      and output DIEs to represent those types (as necessary).  */
19961   for (link = TYPE_ARG_TYPES (function_or_method_type);
19962        link && TREE_VALUE (link);
19963        link = TREE_CHAIN (link))
19964     gen_type_die (TREE_VALUE (link), context_die);
19965 }
19966
19967 /* We want to generate the DIE for TYPE so that we can generate the
19968    die for MEMBER, which has been defined; we will need to refer back
19969    to the member declaration nested within TYPE.  If we're trying to
19970    generate minimal debug info for TYPE, processing TYPE won't do the
19971    trick; we need to attach the member declaration by hand.  */
19972
19973 static void
19974 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19975 {
19976   gen_type_die (type, context_die);
19977
19978   /* If we're trying to avoid duplicate debug info, we may not have
19979      emitted the member decl for this function.  Emit it now.  */
19980   if (TYPE_STUB_DECL (type)
19981       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19982       && ! lookup_decl_die (member))
19983     {
19984       dw_die_ref type_die;
19985       gcc_assert (!decl_ultimate_origin (member));
19986
19987       push_decl_scope (type);
19988       type_die = lookup_type_die_strip_naming_typedef (type);
19989       if (TREE_CODE (member) == FUNCTION_DECL)
19990         gen_subprogram_die (member, type_die);
19991       else if (TREE_CODE (member) == FIELD_DECL)
19992         {
19993           /* Ignore the nameless fields that are used to skip bits but handle
19994              C++ anonymous unions and structs.  */
19995           if (DECL_NAME (member) != NULL_TREE
19996               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19997               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19998             {
19999               gen_type_die (member_declared_type (member), type_die);
20000               gen_field_die (member, type_die);
20001             }
20002         }
20003       else
20004         gen_variable_die (member, NULL_TREE, type_die);
20005
20006       pop_decl_scope ();
20007     }
20008 }
20009
20010 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20011    may later generate inlined and/or out-of-line instances of.  */
20012
20013 static void
20014 dwarf2out_abstract_function (tree decl)
20015 {
20016   dw_die_ref old_die;
20017   tree save_fn;
20018   tree context;
20019   int was_abstract;
20020   htab_t old_decl_loc_table;
20021   htab_t old_cached_dw_loc_list_table;
20022   int old_call_site_count, old_tail_call_site_count;
20023   struct call_arg_loc_node *old_call_arg_locations;
20024
20025   /* Make sure we have the actual abstract inline, not a clone.  */
20026   decl = DECL_ORIGIN (decl);
20027
20028   old_die = lookup_decl_die (decl);
20029   if (old_die && get_AT (old_die, DW_AT_inline))
20030     /* We've already generated the abstract instance.  */
20031     return;
20032
20033   /* We can be called while recursively when seeing block defining inlined subroutine
20034      DIE.  Be sure to not clobber the outer location table nor use it or we would
20035      get locations in abstract instantces.  */
20036   old_decl_loc_table = decl_loc_table;
20037   decl_loc_table = NULL;
20038   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20039   cached_dw_loc_list_table = NULL;
20040   old_call_arg_locations = call_arg_locations;
20041   call_arg_locations = NULL;
20042   old_call_site_count = call_site_count;
20043   call_site_count = -1;
20044   old_tail_call_site_count = tail_call_site_count;
20045   tail_call_site_count = -1;
20046
20047   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20048      we don't get confused by DECL_ABSTRACT.  */
20049   if (debug_info_level > DINFO_LEVEL_TERSE)
20050     {
20051       context = decl_class_context (decl);
20052       if (context)
20053         gen_type_die_for_member
20054           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20055     }
20056
20057   /* Pretend we've just finished compiling this function.  */
20058   save_fn = current_function_decl;
20059   current_function_decl = decl;
20060   push_cfun (DECL_STRUCT_FUNCTION (decl));
20061
20062   was_abstract = DECL_ABSTRACT (decl);
20063   set_decl_abstract_flags (decl, 1);
20064   dwarf2out_decl (decl);
20065   if (! was_abstract)
20066     set_decl_abstract_flags (decl, 0);
20067
20068   current_function_decl = save_fn;
20069   decl_loc_table = old_decl_loc_table;
20070   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20071   call_arg_locations = old_call_arg_locations;
20072   call_site_count = old_call_site_count;
20073   tail_call_site_count = old_tail_call_site_count;
20074   pop_cfun ();
20075 }
20076
20077 /* Helper function of premark_used_types() which gets called through
20078    htab_traverse.
20079
20080    Marks the DIE of a given type in *SLOT as perennial, so it never gets
20081    marked as unused by prune_unused_types.  */
20082
20083 static int
20084 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
20085 {
20086   tree type;
20087   dw_die_ref die;
20088
20089   type = (tree) *slot;
20090   die = lookup_type_die (type);
20091   if (die != NULL)
20092     die->die_perennial_p = 1;
20093   return 1;
20094 }
20095
20096 /* Helper function of premark_types_used_by_global_vars which gets called
20097    through htab_traverse.
20098
20099    Marks the DIE of a given type in *SLOT as perennial, so it never gets
20100    marked as unused by prune_unused_types. The DIE of the type is marked
20101    only if the global variable using the type will actually be emitted.  */
20102
20103 static int
20104 premark_types_used_by_global_vars_helper (void **slot,
20105                                           void *data ATTRIBUTE_UNUSED)
20106 {
20107   struct types_used_by_vars_entry *entry;
20108   dw_die_ref die;
20109
20110   entry = (struct types_used_by_vars_entry *) *slot;
20111   gcc_assert (entry->type != NULL
20112               && entry->var_decl != NULL);
20113   die = lookup_type_die (entry->type);
20114   if (die)
20115     {
20116       /* Ask cgraph if the global variable really is to be emitted.
20117          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
20118       struct varpool_node *node = varpool_get_node (entry->var_decl);
20119       if (node && node->needed)
20120         {
20121           die->die_perennial_p = 1;
20122           /* Keep the parent DIEs as well.  */
20123           while ((die = die->die_parent) && die->die_perennial_p == 0)
20124             die->die_perennial_p = 1;
20125         }
20126     }
20127   return 1;
20128 }
20129
20130 /* Mark all members of used_types_hash as perennial.  */
20131
20132 static void
20133 premark_used_types (void)
20134 {
20135   if (cfun && cfun->used_types_hash)
20136     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
20137 }
20138
20139 /* Mark all members of types_used_by_vars_entry as perennial.  */
20140
20141 static void
20142 premark_types_used_by_global_vars (void)
20143 {
20144   if (types_used_by_vars_hash)
20145     htab_traverse (types_used_by_vars_hash,
20146                    premark_types_used_by_global_vars_helper, NULL);
20147 }
20148
20149 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20150    for CA_LOC call arg loc node.  */
20151
20152 static dw_die_ref
20153 gen_call_site_die (tree decl, dw_die_ref subr_die,
20154                    struct call_arg_loc_node *ca_loc)
20155 {
20156   dw_die_ref stmt_die = NULL, die;
20157   tree block = ca_loc->block;
20158
20159   while (block
20160          && block != DECL_INITIAL (decl)
20161          && TREE_CODE (block) == BLOCK)
20162     {
20163       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
20164         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
20165       if (stmt_die)
20166         break;
20167       block = BLOCK_SUPERCONTEXT (block);
20168     }
20169   if (stmt_die == NULL)
20170     stmt_die = subr_die;
20171   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20172   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20173   if (ca_loc->tail_call_p)
20174     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20175   if (ca_loc->symbol_ref)
20176     {
20177       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20178       if (tdie)
20179         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20180       else
20181         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
20182     }
20183   return die;
20184 }
20185
20186 /* Generate a DIE to represent a declared function (either file-scope or
20187    block-local).  */
20188
20189 static void
20190 gen_subprogram_die (tree decl, dw_die_ref context_die)
20191 {
20192   tree origin = decl_ultimate_origin (decl);
20193   dw_die_ref subr_die;
20194   tree outer_scope;
20195   dw_die_ref old_die = lookup_decl_die (decl);
20196   int declaration = (current_function_decl != decl
20197                      || class_or_namespace_scope_p (context_die));
20198
20199   premark_used_types ();
20200
20201   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
20202      started to generate the abstract instance of an inline, decided to output
20203      its containing class, and proceeded to emit the declaration of the inline
20204      from the member list for the class.  If so, DECLARATION takes priority;
20205      we'll get back to the abstract instance when done with the class.  */
20206
20207   /* The class-scope declaration DIE must be the primary DIE.  */
20208   if (origin && declaration && class_or_namespace_scope_p (context_die))
20209     {
20210       origin = NULL;
20211       gcc_assert (!old_die);
20212     }
20213
20214   /* Now that the C++ front end lazily declares artificial member fns, we
20215      might need to retrofit the declaration into its class.  */
20216   if (!declaration && !origin && !old_die
20217       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20218       && !class_or_namespace_scope_p (context_die)
20219       && debug_info_level > DINFO_LEVEL_TERSE)
20220     old_die = force_decl_die (decl);
20221
20222   if (origin != NULL)
20223     {
20224       gcc_assert (!declaration || local_scope_p (context_die));
20225
20226       /* Fixup die_parent for the abstract instance of a nested
20227          inline function.  */
20228       if (old_die && old_die->die_parent == NULL)
20229         add_child_die (context_die, old_die);
20230
20231       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20232       add_abstract_origin_attribute (subr_die, origin);
20233       /*  This is where the actual code for a cloned function is.
20234           Let's emit linkage name attribute for it.  This helps
20235           debuggers to e.g, set breakpoints into
20236           constructors/destructors when the user asks "break
20237           K::K".  */
20238       add_linkage_name (subr_die, decl);
20239     }
20240   else if (old_die)
20241     {
20242       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20243       struct dwarf_file_data * file_index = lookup_filename (s.file);
20244
20245       if (!get_AT_flag (old_die, DW_AT_declaration)
20246           /* We can have a normal definition following an inline one in the
20247              case of redefinition of GNU C extern inlines.
20248              It seems reasonable to use AT_specification in this case.  */
20249           && !get_AT (old_die, DW_AT_inline))
20250         {
20251           /* Detect and ignore this case, where we are trying to output
20252              something we have already output.  */
20253           return;
20254         }
20255
20256       /* If the definition comes from the same place as the declaration,
20257          maybe use the old DIE.  We always want the DIE for this function
20258          that has the *_pc attributes to be under comp_unit_die so the
20259          debugger can find it.  We also need to do this for abstract
20260          instances of inlines, since the spec requires the out-of-line copy
20261          to have the same parent.  For local class methods, this doesn't
20262          apply; we just use the old DIE.  */
20263       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
20264           && (DECL_ARTIFICIAL (decl)
20265               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20266                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
20267                       == (unsigned) s.line))))
20268         {
20269           subr_die = old_die;
20270
20271           /* Clear out the declaration attribute and the formal parameters.
20272              Do not remove all children, because it is possible that this
20273              declaration die was forced using force_decl_die(). In such
20274              cases die that forced declaration die (e.g. TAG_imported_module)
20275              is one of the children that we do not want to remove.  */
20276           remove_AT (subr_die, DW_AT_declaration);
20277           remove_AT (subr_die, DW_AT_object_pointer);
20278           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20279         }
20280       else
20281         {
20282           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20283           add_AT_specification (subr_die, old_die);
20284           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20285             add_AT_file (subr_die, DW_AT_decl_file, file_index);
20286           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20287             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20288         }
20289     }
20290   else
20291     {
20292       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20293
20294       if (TREE_PUBLIC (decl))
20295         add_AT_flag (subr_die, DW_AT_external, 1);
20296
20297       add_name_and_src_coords_attributes (subr_die, decl);
20298       if (debug_info_level > DINFO_LEVEL_TERSE)
20299         {
20300           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20301           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20302                               0, 0, context_die);
20303         }
20304
20305       add_pure_or_virtual_attribute (subr_die, decl);
20306       if (DECL_ARTIFICIAL (decl))
20307         add_AT_flag (subr_die, DW_AT_artificial, 1);
20308
20309       add_accessibility_attribute (subr_die, decl);
20310     }
20311
20312   if (declaration)
20313     {
20314       if (!old_die || !get_AT (old_die, DW_AT_inline))
20315         {
20316           add_AT_flag (subr_die, DW_AT_declaration, 1);
20317
20318           /* If this is an explicit function declaration then generate
20319              a DW_AT_explicit attribute.  */
20320           if (lang_hooks.decls.function_decl_explicit_p (decl)
20321               && (dwarf_version >= 3 || !dwarf_strict))
20322             add_AT_flag (subr_die, DW_AT_explicit, 1);
20323
20324           /* The first time we see a member function, it is in the context of
20325              the class to which it belongs.  We make sure of this by emitting
20326              the class first.  The next time is the definition, which is
20327              handled above.  The two may come from the same source text.
20328
20329              Note that force_decl_die() forces function declaration die. It is
20330              later reused to represent definition.  */
20331           equate_decl_number_to_die (decl, subr_die);
20332         }
20333     }
20334   else if (DECL_ABSTRACT (decl))
20335     {
20336       if (DECL_DECLARED_INLINE_P (decl))
20337         {
20338           if (cgraph_function_possibly_inlined_p (decl))
20339             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20340           else
20341             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20342         }
20343       else
20344         {
20345           if (cgraph_function_possibly_inlined_p (decl))
20346             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20347           else
20348             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20349         }
20350
20351       if (DECL_DECLARED_INLINE_P (decl)
20352           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20353         add_AT_flag (subr_die, DW_AT_artificial, 1);
20354
20355       equate_decl_number_to_die (decl, subr_die);
20356     }
20357   else if (!DECL_EXTERNAL (decl))
20358     {
20359       HOST_WIDE_INT cfa_fb_offset;
20360
20361       if (!old_die || !get_AT (old_die, DW_AT_inline))
20362         equate_decl_number_to_die (decl, subr_die);
20363
20364       if (!flag_reorder_blocks_and_partition)
20365         {
20366           dw_fde_ref fde = &fde_table[current_funcdef_fde];
20367           if (fde->dw_fde_begin)
20368             {
20369               /* We have already generated the labels.  */
20370               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
20371               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
20372             }
20373           else
20374             {
20375               /* Create start/end labels and add the range.  */
20376               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
20377               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
20378                                            current_function_funcdef_no);
20379               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
20380               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
20381                                            current_function_funcdef_no);
20382               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
20383             }
20384
20385 #if VMS_DEBUGGING_INFO
20386       /* HP OpenVMS Industry Standard 64: DWARF Extensions
20387          Section 2.3 Prologue and Epilogue Attributes:
20388          When a breakpoint is set on entry to a function, it is generally
20389          desirable for execution to be suspended, not on the very first
20390          instruction of the function, but rather at a point after the
20391          function's frame has been set up, after any language defined local
20392          declaration processing has been completed, and before execution of
20393          the first statement of the function begins. Debuggers generally
20394          cannot properly determine where this point is.  Similarly for a
20395          breakpoint set on exit from a function. The prologue and epilogue
20396          attributes allow a compiler to communicate the location(s) to use.  */
20397
20398       {
20399         if (fde->dw_fde_vms_end_prologue)
20400           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20401             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20402
20403         if (fde->dw_fde_vms_begin_epilogue)
20404           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20405             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20406       }
20407 #endif
20408
20409           add_pubname (decl, subr_die);
20410         }
20411       else
20412         {  /* Generate pubnames entries for the split function code
20413               ranges.  */
20414           dw_fde_ref fde = &fde_table[current_funcdef_fde];
20415
20416           if (fde->dw_fde_second_begin)
20417             {
20418               if (dwarf_version >= 3 || !dwarf_strict)
20419                 {
20420                   /* We should use ranges for non-contiguous code section 
20421                      addresses.  Use the actual code range for the initial
20422                      section, since the HOT/COLD labels might precede an 
20423                      alignment offset.  */
20424                   bool range_list_added = false;
20425                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20426                                         fde->dw_fde_end, &range_list_added);
20427                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20428                                         fde->dw_fde_second_end,
20429                                         &range_list_added);
20430                   add_pubname (decl, subr_die);
20431                   if (range_list_added)
20432                     add_ranges (NULL);
20433                 }
20434               else
20435                 {
20436                   /* There is no real support in DW2 for this .. so we make
20437                      a work-around.  First, emit the pub name for the segment
20438                      containing the function label.  Then make and emit a
20439                      simplified subprogram DIE for the second segment with the
20440                      name pre-fixed by __hot/cold_sect_of_.  We use the same
20441                      linkage name for the second die so that gdb will find both
20442                      sections when given "b foo".  */
20443                   const char *name = NULL;
20444                   tree decl_name = DECL_NAME (decl);
20445                   dw_die_ref seg_die;
20446
20447                   /* Do the 'primary' section.   */
20448                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
20449                                  fde->dw_fde_begin);
20450                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
20451                                  fde->dw_fde_end);
20452                   /* Add it.   */
20453                   add_pubname (decl, subr_die);
20454
20455                   /* Build a minimal DIE for the secondary section.  */
20456                   seg_die = new_die (DW_TAG_subprogram,
20457                                      subr_die->die_parent, decl);
20458
20459                   if (TREE_PUBLIC (decl))
20460                     add_AT_flag (seg_die, DW_AT_external, 1);
20461
20462                   if (decl_name != NULL 
20463                       && IDENTIFIER_POINTER (decl_name) != NULL)
20464                     {
20465                       name = dwarf2_name (decl, 1);
20466                       if (! DECL_ARTIFICIAL (decl))
20467                         add_src_coords_attributes (seg_die, decl);
20468
20469                       add_linkage_name (seg_die, decl);
20470                     }
20471                   gcc_assert (name != NULL);
20472                   add_pure_or_virtual_attribute (seg_die, decl);
20473                   if (DECL_ARTIFICIAL (decl))
20474                     add_AT_flag (seg_die, DW_AT_artificial, 1);
20475
20476                   name = concat ("__second_sect_of_", name, NULL); 
20477                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
20478                                  fde->dw_fde_second_begin);
20479                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
20480                                  fde->dw_fde_second_end);
20481                   add_name_attribute (seg_die, name);
20482                   add_pubname_string (name, seg_die);
20483                 }
20484             }
20485           else
20486             {
20487               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
20488               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
20489               add_pubname (decl, subr_die);
20490             }
20491         }
20492
20493 #ifdef MIPS_DEBUGGING_INFO
20494       /* Add a reference to the FDE for this routine.  */
20495       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
20496 #endif
20497
20498       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20499
20500       /* We define the "frame base" as the function's CFA.  This is more
20501          convenient for several reasons: (1) It's stable across the prologue
20502          and epilogue, which makes it better than just a frame pointer,
20503          (2) With dwarf3, there exists a one-byte encoding that allows us
20504          to reference the .debug_frame data by proxy, but failing that,
20505          (3) We can at least reuse the code inspection and interpretation
20506          code that determines the CFA position at various points in the
20507          function.  */
20508       if (dwarf_version >= 3)
20509         {
20510           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20511           add_AT_loc (subr_die, DW_AT_frame_base, op);
20512         }
20513       else
20514         {
20515           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20516           if (list->dw_loc_next)
20517             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20518           else
20519             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20520         }
20521
20522       /* Compute a displacement from the "steady-state frame pointer" to
20523          the CFA.  The former is what all stack slots and argument slots
20524          will reference in the rtl; the later is what we've told the
20525          debugger about.  We'll need to adjust all frame_base references
20526          by this displacement.  */
20527       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20528
20529       if (cfun->static_chain_decl)
20530         add_AT_location_description (subr_die, DW_AT_static_link,
20531                  loc_list_from_tree (cfun->static_chain_decl, 2));
20532     }
20533
20534   /* Generate child dies for template paramaters.  */
20535   if (debug_info_level > DINFO_LEVEL_TERSE)
20536     gen_generic_params_dies (decl);
20537
20538   /* Now output descriptions of the arguments for this function. This gets
20539      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20540      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20541      `...' at the end of the formal parameter list.  In order to find out if
20542      there was a trailing ellipsis or not, we must instead look at the type
20543      associated with the FUNCTION_DECL.  This will be a node of type
20544      FUNCTION_TYPE. If the chain of type nodes hanging off of this
20545      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20546      an ellipsis at the end.  */
20547
20548   /* In the case where we are describing a mere function declaration, all we
20549      need to do here (and all we *can* do here) is to describe the *types* of
20550      its formal parameters.  */
20551   if (debug_info_level <= DINFO_LEVEL_TERSE)
20552     ;
20553   else if (declaration)
20554     gen_formal_types_die (decl, subr_die);
20555   else
20556     {
20557       /* Generate DIEs to represent all known formal parameters.  */
20558       tree parm = DECL_ARGUMENTS (decl);
20559       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
20560       tree generic_decl_parm = generic_decl
20561                                 ? DECL_ARGUMENTS (generic_decl)
20562                                 : NULL;
20563
20564       /* Now we want to walk the list of parameters of the function and
20565          emit their relevant DIEs.
20566
20567          We consider the case of DECL being an instance of a generic function
20568          as well as it being a normal function.
20569
20570          If DECL is an instance of a generic function we walk the
20571          parameters of the generic function declaration _and_ the parameters of
20572          DECL itself. This is useful because we want to emit specific DIEs for
20573          function parameter packs and those are declared as part of the
20574          generic function declaration. In that particular case,
20575          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20576          That DIE has children DIEs representing the set of arguments
20577          of the pack. Note that the set of pack arguments can be empty.
20578          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20579          children DIE.
20580
20581          Otherwise, we just consider the parameters of DECL.  */
20582       while (generic_decl_parm || parm)
20583         {
20584           if (generic_decl_parm
20585               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20586             gen_formal_parameter_pack_die (generic_decl_parm,
20587                                            parm, subr_die,
20588                                            &parm);
20589           else if (parm)
20590             {
20591               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
20592
20593               if (parm == DECL_ARGUMENTS (decl)
20594                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20595                   && parm_die
20596                   && (dwarf_version >= 3 || !dwarf_strict))
20597                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20598
20599               parm = DECL_CHAIN (parm);
20600             }
20601
20602           if (generic_decl_parm)
20603             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20604         }
20605
20606       /* Decide whether we need an unspecified_parameters DIE at the end.
20607          There are 2 more cases to do this for: 1) the ansi ... declaration -
20608          this is detectable when the end of the arg list is not a
20609          void_type_node 2) an unprototyped function declaration (not a
20610          definition).  This just means that we have no info about the
20611          parameters at all.  */
20612       if (prototype_p (TREE_TYPE (decl)))
20613         {
20614           /* This is the prototyped case, check for....  */
20615           if (stdarg_p (TREE_TYPE (decl)))
20616             gen_unspecified_parameters_die (decl, subr_die);
20617         }
20618       else if (DECL_INITIAL (decl) == NULL_TREE)
20619         gen_unspecified_parameters_die (decl, subr_die);
20620     }
20621
20622   /* Output Dwarf info for all of the stuff within the body of the function
20623      (if it has one - it may be just a declaration).  */
20624   outer_scope = DECL_INITIAL (decl);
20625
20626   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20627      a function.  This BLOCK actually represents the outermost binding contour
20628      for the function, i.e. the contour in which the function's formal
20629      parameters and labels get declared. Curiously, it appears that the front
20630      end doesn't actually put the PARM_DECL nodes for the current function onto
20631      the BLOCK_VARS list for this outer scope, but are strung off of the
20632      DECL_ARGUMENTS list for the function instead.
20633
20634      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20635      the LABEL_DECL nodes for the function however, and we output DWARF info
20636      for those in decls_for_scope.  Just within the `outer_scope' there will be
20637      a BLOCK node representing the function's outermost pair of curly braces,
20638      and any blocks used for the base and member initializers of a C++
20639      constructor function.  */
20640   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
20641     {
20642       int call_site_note_count = 0;
20643       int tail_call_site_note_count = 0;
20644
20645       /* Emit a DW_TAG_variable DIE for a named return value.  */
20646       if (DECL_NAME (DECL_RESULT (decl)))
20647         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
20648
20649       current_function_has_inlines = 0;
20650       decls_for_scope (outer_scope, subr_die, 0);
20651
20652       if (call_arg_locations && !dwarf_strict)
20653         {
20654           struct call_arg_loc_node *ca_loc;
20655           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20656             {
20657               dw_die_ref die = NULL;
20658               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20659               rtx arg, next_arg;
20660
20661               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
20662                    arg; arg = next_arg)
20663                 {
20664                   dw_loc_descr_ref reg, val;
20665                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
20666                   dw_die_ref cdie, tdie = NULL;
20667
20668                   next_arg = XEXP (arg, 1);
20669                   if (REG_P (XEXP (XEXP (arg, 0), 0))
20670                       && next_arg
20671                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
20672                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
20673                       && REGNO (XEXP (XEXP (arg, 0), 0))
20674                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
20675                     next_arg = XEXP (next_arg, 1);
20676                   if (mode == VOIDmode)
20677                     {
20678                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
20679                       if (mode == VOIDmode)
20680                         mode = GET_MODE (XEXP (arg, 0));
20681                     }
20682                   if (mode == VOIDmode || mode == BLKmode)
20683                     continue;
20684                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
20685                     {
20686                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20687                       tloc = XEXP (XEXP (arg, 0), 1);
20688                       continue;
20689                     }
20690                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
20691                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
20692                     {
20693                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20694                       tlocc = XEXP (XEXP (arg, 0), 1);
20695                       continue;
20696                     }
20697                   reg = NULL;
20698                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
20699                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
20700                                               VAR_INIT_STATUS_INITIALIZED);
20701                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
20702                     {
20703                       rtx mem = XEXP (XEXP (arg, 0), 0);
20704                       reg = mem_loc_descriptor (XEXP (mem, 0),
20705                                                 get_address_mode (mem),
20706                                                 GET_MODE (mem),
20707                                                 VAR_INIT_STATUS_INITIALIZED);
20708                     }
20709                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
20710                            == DEBUG_PARAMETER_REF)
20711                     {
20712                       tree tdecl
20713                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
20714                       tdie = lookup_decl_die (tdecl);
20715                       if (tdie == NULL)
20716                         continue;
20717                     }
20718                   else
20719                     continue;
20720                   if (reg == NULL
20721                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
20722                          != DEBUG_PARAMETER_REF)
20723                     continue;
20724                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
20725                                             VOIDmode,
20726                                             VAR_INIT_STATUS_INITIALIZED);
20727                   if (val == NULL)
20728                     continue;
20729                   if (die == NULL)
20730                     die = gen_call_site_die (decl, subr_die, ca_loc);
20731                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
20732                                   NULL_TREE);
20733                   if (reg != NULL)
20734                     add_AT_loc (cdie, DW_AT_location, reg);
20735                   else if (tdie != NULL)
20736                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
20737                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
20738                   if (next_arg != XEXP (arg, 1))
20739                     {
20740                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
20741                       if (mode == VOIDmode)
20742                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
20743                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
20744                                                             0), 1),
20745                                                 mode, VOIDmode,
20746                                                 VAR_INIT_STATUS_INITIALIZED);
20747                       if (val != NULL)
20748                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
20749                     }
20750                 }
20751               if (die == NULL
20752                   && (ca_loc->symbol_ref || tloc))
20753                 die = gen_call_site_die (decl, subr_die, ca_loc);
20754               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
20755                 {
20756                   dw_loc_descr_ref tval = NULL;
20757
20758                   if (tloc != NULL_RTX)
20759                     tval = mem_loc_descriptor (tloc,
20760                                                GET_MODE (tloc) == VOIDmode
20761                                                ? Pmode : GET_MODE (tloc),
20762                                                VOIDmode,
20763                                                VAR_INIT_STATUS_INITIALIZED);
20764                   if (tval)
20765                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
20766                   else if (tlocc != NULL_RTX)
20767                     {
20768                       tval = mem_loc_descriptor (tlocc,
20769                                                  GET_MODE (tlocc) == VOIDmode
20770                                                  ? Pmode : GET_MODE (tlocc),
20771                                                  VOIDmode,
20772                                                  VAR_INIT_STATUS_INITIALIZED);
20773                       if (tval)
20774                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
20775                                     tval);
20776                     }
20777                 }
20778               if (die != NULL)
20779                 {
20780                   call_site_note_count++;
20781                   if (ca_loc->tail_call_p)
20782                     tail_call_site_note_count++;
20783                 }
20784             }
20785         }
20786       call_arg_locations = NULL;
20787       call_arg_loc_last = NULL;
20788       if (tail_call_site_count >= 0
20789           && tail_call_site_count == tail_call_site_note_count
20790           && !dwarf_strict)
20791         {
20792           if (call_site_count >= 0
20793               && call_site_count == call_site_note_count)
20794             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
20795           else
20796             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
20797         }
20798       call_site_count = -1;
20799       tail_call_site_count = -1;
20800     }
20801   /* Add the calling convention attribute if requested.  */
20802   add_calling_convention_attribute (subr_die, decl);
20803
20804 }
20805
20806 /* Returns a hash value for X (which really is a die_struct).  */
20807
20808 static hashval_t
20809 common_block_die_table_hash (const void *x)
20810 {
20811   const_dw_die_ref d = (const_dw_die_ref) x;
20812   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
20813 }
20814
20815 /* Return nonzero if decl_id and die_parent of die_struct X is the same
20816    as decl_id and die_parent of die_struct Y.  */
20817
20818 static int
20819 common_block_die_table_eq (const void *x, const void *y)
20820 {
20821   const_dw_die_ref d = (const_dw_die_ref) x;
20822   const_dw_die_ref e = (const_dw_die_ref) y;
20823   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
20824 }
20825
20826 /* Generate a DIE to represent a declared data object.
20827    Either DECL or ORIGIN must be non-null.  */
20828
20829 static void
20830 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
20831 {
20832   HOST_WIDE_INT off;
20833   tree com_decl;
20834   tree decl_or_origin = decl ? decl : origin;
20835   tree ultimate_origin;
20836   dw_die_ref var_die;
20837   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
20838   dw_die_ref origin_die;
20839   bool declaration = (DECL_EXTERNAL (decl_or_origin)
20840                       || class_or_namespace_scope_p (context_die));
20841   bool specialization_p = false;
20842
20843   ultimate_origin = decl_ultimate_origin (decl_or_origin);
20844   if (decl || ultimate_origin)
20845     origin = ultimate_origin;
20846   com_decl = fortran_common (decl_or_origin, &off);
20847
20848   /* Symbol in common gets emitted as a child of the common block, in the form
20849      of a data member.  */
20850   if (com_decl)
20851     {
20852       dw_die_ref com_die;
20853       dw_loc_list_ref loc;
20854       die_node com_die_arg;
20855
20856       var_die = lookup_decl_die (decl_or_origin);
20857       if (var_die)
20858         {
20859           if (get_AT (var_die, DW_AT_location) == NULL)
20860             {
20861               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
20862               if (loc)
20863                 {
20864                   if (off)
20865                     {
20866                       /* Optimize the common case.  */
20867                       if (single_element_loc_list_p (loc)
20868                           && loc->expr->dw_loc_opc == DW_OP_addr
20869                           && loc->expr->dw_loc_next == NULL
20870                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
20871                              == SYMBOL_REF)
20872                         loc->expr->dw_loc_oprnd1.v.val_addr
20873                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20874                         else
20875                           loc_list_plus_const (loc, off);
20876                     }
20877                   add_AT_location_description (var_die, DW_AT_location, loc);
20878                   remove_AT (var_die, DW_AT_declaration);
20879                 }
20880             }
20881           return;
20882         }
20883
20884       if (common_block_die_table == NULL)
20885         common_block_die_table
20886           = htab_create_ggc (10, common_block_die_table_hash,
20887                              common_block_die_table_eq, NULL);
20888
20889       com_die_arg.decl_id = DECL_UID (com_decl);
20890       com_die_arg.die_parent = context_die;
20891       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
20892       loc = loc_list_from_tree (com_decl, 2);
20893       if (com_die == NULL)
20894         {
20895           const char *cnam
20896             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
20897           void **slot;
20898
20899           com_die = new_die (DW_TAG_common_block, context_die, decl);
20900           add_name_and_src_coords_attributes (com_die, com_decl);
20901           if (loc)
20902             {
20903               add_AT_location_description (com_die, DW_AT_location, loc);
20904               /* Avoid sharing the same loc descriptor between
20905                  DW_TAG_common_block and DW_TAG_variable.  */
20906               loc = loc_list_from_tree (com_decl, 2);
20907             }
20908           else if (DECL_EXTERNAL (decl))
20909             add_AT_flag (com_die, DW_AT_declaration, 1);
20910           add_pubname_string (cnam, com_die); /* ??? needed? */
20911           com_die->decl_id = DECL_UID (com_decl);
20912           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
20913           *slot = (void *) com_die;
20914         }
20915       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
20916         {
20917           add_AT_location_description (com_die, DW_AT_location, loc);
20918           loc = loc_list_from_tree (com_decl, 2);
20919           remove_AT (com_die, DW_AT_declaration);
20920         }
20921       var_die = new_die (DW_TAG_variable, com_die, decl);
20922       add_name_and_src_coords_attributes (var_die, decl);
20923       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
20924                           TREE_THIS_VOLATILE (decl), context_die);
20925       add_AT_flag (var_die, DW_AT_external, 1);
20926       if (loc)
20927         {
20928           if (off)
20929             {
20930               /* Optimize the common case.  */
20931               if (single_element_loc_list_p (loc)
20932                   && loc->expr->dw_loc_opc == DW_OP_addr
20933                   && loc->expr->dw_loc_next == NULL
20934                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
20935                 loc->expr->dw_loc_oprnd1.v.val_addr
20936                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20937               else
20938                 loc_list_plus_const (loc, off);
20939             }
20940           add_AT_location_description (var_die, DW_AT_location, loc);
20941         }
20942       else if (DECL_EXTERNAL (decl))
20943         add_AT_flag (var_die, DW_AT_declaration, 1);
20944       equate_decl_number_to_die (decl, var_die);
20945       return;
20946     }
20947
20948   /* If the compiler emitted a definition for the DECL declaration
20949      and if we already emitted a DIE for it, don't emit a second
20950      DIE for it again. Allow re-declarations of DECLs that are
20951      inside functions, though.  */
20952   if (old_die && declaration && !local_scope_p (context_die))
20953     return;
20954
20955   /* For static data members, the declaration in the class is supposed
20956      to have DW_TAG_member tag; the specification should still be
20957      DW_TAG_variable referencing the DW_TAG_member DIE.  */
20958   if (declaration && class_scope_p (context_die))
20959     var_die = new_die (DW_TAG_member, context_die, decl);
20960   else
20961     var_die = new_die (DW_TAG_variable, context_die, decl);
20962
20963   origin_die = NULL;
20964   if (origin != NULL)
20965     origin_die = add_abstract_origin_attribute (var_die, origin);
20966
20967   /* Loop unrolling can create multiple blocks that refer to the same
20968      static variable, so we must test for the DW_AT_declaration flag.
20969
20970      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
20971      copy decls and set the DECL_ABSTRACT flag on them instead of
20972      sharing them.
20973
20974      ??? Duplicated blocks have been rewritten to use .debug_ranges.
20975
20976      ??? The declare_in_namespace support causes us to get two DIEs for one
20977      variable, both of which are declarations.  We want to avoid considering
20978      one to be a specification, so we must test that this DIE is not a
20979      declaration.  */
20980   else if (old_die && TREE_STATIC (decl) && ! declaration
20981            && get_AT_flag (old_die, DW_AT_declaration) == 1)
20982     {
20983       /* This is a definition of a C++ class level static.  */
20984       add_AT_specification (var_die, old_die);
20985       specialization_p = true;
20986       if (DECL_NAME (decl))
20987         {
20988           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20989           struct dwarf_file_data * file_index = lookup_filename (s.file);
20990
20991           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20992             add_AT_file (var_die, DW_AT_decl_file, file_index);
20993
20994           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20995             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
20996
20997           if (old_die->die_tag == DW_TAG_member)
20998             add_linkage_name (var_die, decl);
20999         }
21000     }
21001   else
21002     add_name_and_src_coords_attributes (var_die, decl);
21003
21004   if ((origin == NULL && !specialization_p)
21005       || (origin != NULL
21006           && !DECL_ABSTRACT (decl_or_origin)
21007           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21008                                        decl_function_context
21009                                                         (decl_or_origin))))
21010     {
21011       tree type = TREE_TYPE (decl_or_origin);
21012
21013       if (decl_by_reference_p (decl_or_origin))
21014         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
21015       else
21016         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
21017                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
21018     }
21019
21020   if (origin == NULL && !specialization_p)
21021     {
21022       if (TREE_PUBLIC (decl))
21023         add_AT_flag (var_die, DW_AT_external, 1);
21024
21025       if (DECL_ARTIFICIAL (decl))
21026         add_AT_flag (var_die, DW_AT_artificial, 1);
21027
21028       add_accessibility_attribute (var_die, decl);
21029     }
21030
21031   if (declaration)
21032     add_AT_flag (var_die, DW_AT_declaration, 1);
21033
21034   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
21035     equate_decl_number_to_die (decl, var_die);
21036
21037   if (! declaration
21038       && (! DECL_ABSTRACT (decl_or_origin)
21039           /* Local static vars are shared between all clones/inlines,
21040              so emit DW_AT_location on the abstract DIE if DECL_RTL is
21041              already set.  */
21042           || (TREE_CODE (decl_or_origin) == VAR_DECL
21043               && TREE_STATIC (decl_or_origin)
21044               && DECL_RTL_SET_P (decl_or_origin)))
21045       /* When abstract origin already has DW_AT_location attribute, no need
21046          to add it again.  */
21047       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21048     {
21049       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
21050           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
21051         defer_location (decl_or_origin, var_die);
21052       else
21053         add_location_or_const_value_attribute (var_die, decl_or_origin,
21054                                                decl == NULL, DW_AT_location);
21055       add_pubname (decl_or_origin, var_die);
21056     }
21057   else
21058     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21059 }
21060
21061 /* Generate a DIE to represent a named constant.  */
21062
21063 static void
21064 gen_const_die (tree decl, dw_die_ref context_die)
21065 {
21066   dw_die_ref const_die;
21067   tree type = TREE_TYPE (decl);
21068
21069   const_die = new_die (DW_TAG_constant, context_die, decl);
21070   add_name_and_src_coords_attributes (const_die, decl);
21071   add_type_attribute (const_die, type, 1, 0, context_die);
21072   if (TREE_PUBLIC (decl))
21073     add_AT_flag (const_die, DW_AT_external, 1);
21074   if (DECL_ARTIFICIAL (decl))
21075     add_AT_flag (const_die, DW_AT_artificial, 1);
21076   tree_add_const_value_attribute_for_decl (const_die, decl);
21077 }
21078
21079 /* Generate a DIE to represent a label identifier.  */
21080
21081 static void
21082 gen_label_die (tree decl, dw_die_ref context_die)
21083 {
21084   tree origin = decl_ultimate_origin (decl);
21085   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
21086   rtx insn;
21087   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21088
21089   if (origin != NULL)
21090     add_abstract_origin_attribute (lbl_die, origin);
21091   else
21092     add_name_and_src_coords_attributes (lbl_die, decl);
21093
21094   if (DECL_ABSTRACT (decl))
21095     equate_decl_number_to_die (decl, lbl_die);
21096   else
21097     {
21098       insn = DECL_RTL_IF_SET (decl);
21099
21100       /* Deleted labels are programmer specified labels which have been
21101          eliminated because of various optimizations.  We still emit them
21102          here so that it is possible to put breakpoints on them.  */
21103       if (insn
21104           && (LABEL_P (insn)
21105               || ((NOTE_P (insn)
21106                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21107         {
21108           /* When optimization is enabled (via -O) some parts of the compiler
21109              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21110              represent source-level labels which were explicitly declared by
21111              the user.  This really shouldn't be happening though, so catch
21112              it if it ever does happen.  */
21113           gcc_assert (!INSN_DELETED_P (insn));
21114
21115           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21116           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21117         }
21118     }
21119 }
21120
21121 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
21122    attributes to the DIE for a block STMT, to describe where the inlined
21123    function was called from.  This is similar to add_src_coords_attributes.  */
21124
21125 static inline void
21126 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21127 {
21128   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21129
21130   if (dwarf_version >= 3 || !dwarf_strict)
21131     {
21132       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21133       add_AT_unsigned (die, DW_AT_call_line, s.line);
21134     }
21135 }
21136
21137
21138 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21139    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
21140
21141 static inline void
21142 add_high_low_attributes (tree stmt, dw_die_ref die)
21143 {
21144   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21145
21146   if (BLOCK_FRAGMENT_CHAIN (stmt)
21147       && (dwarf_version >= 3 || !dwarf_strict))
21148     {
21149       tree chain;
21150
21151       if (inlined_function_outer_scope_p (stmt))
21152         {
21153           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21154                                        BLOCK_NUMBER (stmt));
21155           add_AT_lbl_id (die, DW_AT_entry_pc, label);
21156         }
21157
21158       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
21159
21160       chain = BLOCK_FRAGMENT_CHAIN (stmt);
21161       do
21162         {
21163           add_ranges (chain);
21164           chain = BLOCK_FRAGMENT_CHAIN (chain);
21165         }
21166       while (chain);
21167       add_ranges (NULL);
21168     }
21169   else
21170     {
21171       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21172                                    BLOCK_NUMBER (stmt));
21173       add_AT_lbl_id (die, DW_AT_low_pc, label);
21174       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
21175                                    BLOCK_NUMBER (stmt));
21176       add_AT_lbl_id (die, DW_AT_high_pc, label);
21177     }
21178 }
21179
21180 /* Generate a DIE for a lexical block.  */
21181
21182 static void
21183 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
21184 {
21185   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21186
21187   if (call_arg_locations)
21188     {
21189       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
21190         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
21191                                BLOCK_NUMBER (stmt) + 1);
21192       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
21193     }
21194
21195   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21196     add_high_low_attributes (stmt, stmt_die);
21197
21198   decls_for_scope (stmt, stmt_die, depth);
21199 }
21200
21201 /* Generate a DIE for an inlined subprogram.  */
21202
21203 static void
21204 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
21205 {
21206   tree decl;
21207
21208   /* The instance of function that is effectively being inlined shall not
21209      be abstract.  */
21210   gcc_assert (! BLOCK_ABSTRACT (stmt));
21211
21212   decl = block_ultimate_origin (stmt);
21213
21214   /* Emit info for the abstract instance first, if we haven't yet.  We
21215      must emit this even if the block is abstract, otherwise when we
21216      emit the block below (or elsewhere), we may end up trying to emit
21217      a die whose origin die hasn't been emitted, and crashing.  */
21218   dwarf2out_abstract_function (decl);
21219
21220   if (! BLOCK_ABSTRACT (stmt))
21221     {
21222       dw_die_ref subr_die
21223         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21224
21225       if (call_arg_locations)
21226         {
21227           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
21228             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
21229                                    BLOCK_NUMBER (stmt) + 1);
21230           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
21231         }
21232       add_abstract_origin_attribute (subr_die, decl);
21233       if (TREE_ASM_WRITTEN (stmt))
21234         add_high_low_attributes (stmt, subr_die);
21235       add_call_src_coords_attributes (stmt, subr_die);
21236
21237       decls_for_scope (stmt, subr_die, depth);
21238       current_function_has_inlines = 1;
21239     }
21240 }
21241
21242 /* Generate a DIE for a field in a record, or structure.  */
21243
21244 static void
21245 gen_field_die (tree decl, dw_die_ref context_die)
21246 {
21247   dw_die_ref decl_die;
21248
21249   if (TREE_TYPE (decl) == error_mark_node)
21250     return;
21251
21252   decl_die = new_die (DW_TAG_member, context_die, decl);
21253   add_name_and_src_coords_attributes (decl_die, decl);
21254   add_type_attribute (decl_die, member_declared_type (decl),
21255                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
21256                       context_die);
21257
21258   if (DECL_BIT_FIELD_TYPE (decl))
21259     {
21260       add_byte_size_attribute (decl_die, decl);
21261       add_bit_size_attribute (decl_die, decl);
21262       add_bit_offset_attribute (decl_die, decl);
21263     }
21264
21265   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21266     add_data_member_location_attribute (decl_die, decl);
21267
21268   if (DECL_ARTIFICIAL (decl))
21269     add_AT_flag (decl_die, DW_AT_artificial, 1);
21270
21271   add_accessibility_attribute (decl_die, decl);
21272
21273   /* Equate decl number to die, so that we can look up this decl later on.  */
21274   equate_decl_number_to_die (decl, decl_die);
21275 }
21276
21277 #if 0
21278 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21279    Use modified_type_die instead.
21280    We keep this code here just in case these types of DIEs may be needed to
21281    represent certain things in other languages (e.g. Pascal) someday.  */
21282
21283 static void
21284 gen_pointer_type_die (tree type, dw_die_ref context_die)
21285 {
21286   dw_die_ref ptr_die
21287     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21288
21289   equate_type_number_to_die (type, ptr_die);
21290   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
21291   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21292 }
21293
21294 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21295    Use modified_type_die instead.
21296    We keep this code here just in case these types of DIEs may be needed to
21297    represent certain things in other languages (e.g. Pascal) someday.  */
21298
21299 static void
21300 gen_reference_type_die (tree type, dw_die_ref context_die)
21301 {
21302   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21303
21304   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21305     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21306   else
21307     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21308
21309   equate_type_number_to_die (type, ref_die);
21310   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
21311   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21312 }
21313 #endif
21314
21315 /* Generate a DIE for a pointer to a member type.  */
21316
21317 static void
21318 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21319 {
21320   dw_die_ref ptr_die
21321     = new_die (DW_TAG_ptr_to_member_type,
21322                scope_die_for (type, context_die), type);
21323
21324   equate_type_number_to_die (type, ptr_die);
21325   add_AT_die_ref (ptr_die, DW_AT_containing_type,
21326                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21327   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
21328 }
21329
21330 /* Generate the DIE for the compilation unit.  */
21331
21332 static dw_die_ref
21333 gen_compile_unit_die (const char *filename)
21334 {
21335   dw_die_ref die;
21336   char producer[250];
21337   const char *language_string = lang_hooks.name;
21338   int language;
21339
21340   die = new_die (DW_TAG_compile_unit, NULL, NULL);
21341
21342   if (filename)
21343     {
21344       add_name_attribute (die, filename);
21345       /* Don't add cwd for <built-in>.  */
21346       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
21347         add_comp_dir_attribute (die);
21348     }
21349
21350   sprintf (producer, "%s %s", language_string, version_string);
21351
21352 #ifdef MIPS_DEBUGGING_INFO
21353   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
21354      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
21355      not appear in the producer string, the debugger reaches the conclusion
21356      that the object file is stripped and has no debugging information.
21357      To get the MIPS/SGI debugger to believe that there is debugging
21358      information in the object file, we add a -g to the producer string.  */
21359   if (debug_info_level > DINFO_LEVEL_TERSE)
21360     strcat (producer, " -g");
21361 #endif
21362
21363   add_AT_string (die, DW_AT_producer, producer);
21364
21365   /* If our producer is LTO try to figure out a common language to use
21366      from the global list of translation units.  */
21367   if (strcmp (language_string, "GNU GIMPLE") == 0)
21368     {
21369       unsigned i;
21370       tree t;
21371       const char *common_lang = NULL;
21372
21373       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
21374         {
21375           if (!TRANSLATION_UNIT_LANGUAGE (t))
21376             continue;
21377           if (!common_lang)
21378             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
21379           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
21380             ;
21381           else if (strncmp (common_lang, "GNU C", 5) == 0
21382                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
21383             /* Mixing C and C++ is ok, use C++ in that case.  */
21384             common_lang = "GNU C++";
21385           else
21386             {
21387               /* Fall back to C.  */
21388               common_lang = NULL;
21389               break;
21390             }
21391         }
21392
21393       if (common_lang)
21394         language_string = common_lang;
21395     }
21396
21397   language = DW_LANG_C89;
21398   if (strcmp (language_string, "GNU C++") == 0)
21399     language = DW_LANG_C_plus_plus;
21400   else if (strcmp (language_string, "GNU F77") == 0)
21401     language = DW_LANG_Fortran77;
21402   else if (strcmp (language_string, "GNU Pascal") == 0)
21403     language = DW_LANG_Pascal83;
21404   else if (dwarf_version >= 3 || !dwarf_strict)
21405     {
21406       if (strcmp (language_string, "GNU Ada") == 0)
21407         language = DW_LANG_Ada95;
21408       else if (strcmp (language_string, "GNU Fortran") == 0)
21409         language = DW_LANG_Fortran95;
21410       else if (strcmp (language_string, "GNU Java") == 0)
21411         language = DW_LANG_Java;
21412       else if (strcmp (language_string, "GNU Objective-C") == 0)
21413         language = DW_LANG_ObjC;
21414       else if (strcmp (language_string, "GNU Objective-C++") == 0)
21415         language = DW_LANG_ObjC_plus_plus;
21416     }
21417
21418   add_AT_unsigned (die, DW_AT_language, language);
21419
21420   switch (language)
21421     {
21422     case DW_LANG_Fortran77:
21423     case DW_LANG_Fortran90:
21424     case DW_LANG_Fortran95:
21425       /* Fortran has case insensitive identifiers and the front-end
21426          lowercases everything.  */
21427       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
21428       break;
21429     default:
21430       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
21431       break;
21432     }
21433   return die;
21434 }
21435
21436 /* Generate the DIE for a base class.  */
21437
21438 static void
21439 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
21440 {
21441   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
21442
21443   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
21444   add_data_member_location_attribute (die, binfo);
21445
21446   if (BINFO_VIRTUAL_P (binfo))
21447     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21448
21449   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
21450      children, otherwise the default is DW_ACCESS_public.  In DWARF2
21451      the default has always been DW_ACCESS_private.  */
21452   if (access == access_public_node)
21453     {
21454       if (dwarf_version == 2
21455           || context_die->die_tag == DW_TAG_class_type)
21456       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
21457     }
21458   else if (access == access_protected_node)
21459     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
21460   else if (dwarf_version > 2
21461            && context_die->die_tag != DW_TAG_class_type)
21462     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
21463 }
21464
21465 /* Generate a DIE for a class member.  */
21466
21467 static void
21468 gen_member_die (tree type, dw_die_ref context_die)
21469 {
21470   tree member;
21471   tree binfo = TYPE_BINFO (type);
21472   dw_die_ref child;
21473
21474   /* If this is not an incomplete type, output descriptions of each of its
21475      members. Note that as we output the DIEs necessary to represent the
21476      members of this record or union type, we will also be trying to output
21477      DIEs to represent the *types* of those members. However the `type'
21478      function (above) will specifically avoid generating type DIEs for member
21479      types *within* the list of member DIEs for this (containing) type except
21480      for those types (of members) which are explicitly marked as also being
21481      members of this (containing) type themselves.  The g++ front- end can
21482      force any given type to be treated as a member of some other (containing)
21483      type by setting the TYPE_CONTEXT of the given (member) type to point to
21484      the TREE node representing the appropriate (containing) type.  */
21485
21486   /* First output info about the base classes.  */
21487   if (binfo)
21488     {
21489       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
21490       int i;
21491       tree base;
21492
21493       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
21494         gen_inheritance_die (base,
21495                              (accesses ? VEC_index (tree, accesses, i)
21496                               : access_public_node), context_die);
21497     }
21498
21499   /* Now output info about the data members and type members.  */
21500   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
21501     {
21502       /* If we thought we were generating minimal debug info for TYPE
21503          and then changed our minds, some of the member declarations
21504          may have already been defined.  Don't define them again, but
21505          do put them in the right order.  */
21506
21507       child = lookup_decl_die (member);
21508       if (child)
21509         splice_child_die (context_die, child);
21510       else
21511         gen_decl_die (member, NULL, context_die);
21512     }
21513
21514   /* Now output info about the function members (if any).  */
21515   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
21516     {
21517       /* Don't include clones in the member list.  */
21518       if (DECL_ABSTRACT_ORIGIN (member))
21519         continue;
21520
21521       child = lookup_decl_die (member);
21522       if (child)
21523         splice_child_die (context_die, child);
21524       else
21525         gen_decl_die (member, NULL, context_die);
21526     }
21527 }
21528
21529 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
21530    is set, we pretend that the type was never defined, so we only get the
21531    member DIEs needed by later specification DIEs.  */
21532
21533 static void
21534 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
21535                                 enum debug_info_usage usage)
21536 {
21537   dw_die_ref type_die = lookup_type_die (type);
21538   dw_die_ref scope_die = 0;
21539   int nested = 0;
21540   int complete = (TYPE_SIZE (type)
21541                   && (! TYPE_STUB_DECL (type)
21542                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
21543   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
21544   complete = complete && should_emit_struct_debug (type, usage);
21545
21546   if (type_die && ! complete)
21547     return;
21548
21549   if (TYPE_CONTEXT (type) != NULL_TREE
21550       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
21551           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
21552     nested = 1;
21553
21554   scope_die = scope_die_for (type, context_die);
21555
21556   if (! type_die || (nested && is_cu_die (scope_die)))
21557     /* First occurrence of type or toplevel definition of nested class.  */
21558     {
21559       dw_die_ref old_die = type_die;
21560
21561       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
21562                           ? record_type_tag (type) : DW_TAG_union_type,
21563                           scope_die, type);
21564       equate_type_number_to_die (type, type_die);
21565       if (old_die)
21566         add_AT_specification (type_die, old_die);
21567       else
21568         {
21569           add_name_attribute (type_die, type_tag (type));
21570           add_gnat_descriptive_type_attribute (type_die, type, context_die);
21571         }
21572     }
21573   else
21574     remove_AT (type_die, DW_AT_declaration);
21575
21576   /* Generate child dies for template paramaters.  */
21577   if (debug_info_level > DINFO_LEVEL_TERSE
21578       && COMPLETE_TYPE_P (type))
21579     schedule_generic_params_dies_gen (type);
21580
21581   /* If this type has been completed, then give it a byte_size attribute and
21582      then give a list of members.  */
21583   if (complete && !ns_decl)
21584     {
21585       /* Prevent infinite recursion in cases where the type of some member of
21586          this type is expressed in terms of this type itself.  */
21587       TREE_ASM_WRITTEN (type) = 1;
21588       add_byte_size_attribute (type_die, type);
21589       if (TYPE_STUB_DECL (type) != NULL_TREE)
21590         {
21591           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21592           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21593         }
21594
21595       /* If the first reference to this type was as the return type of an
21596          inline function, then it may not have a parent.  Fix this now.  */
21597       if (type_die->die_parent == NULL)
21598         add_child_die (scope_die, type_die);
21599
21600       push_decl_scope (type);
21601       gen_member_die (type, type_die);
21602       pop_decl_scope ();
21603
21604       /* GNU extension: Record what type our vtable lives in.  */
21605       if (TYPE_VFIELD (type))
21606         {
21607           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
21608
21609           gen_type_die (vtype, context_die);
21610           add_AT_die_ref (type_die, DW_AT_containing_type,
21611                           lookup_type_die (vtype));
21612         }
21613     }
21614   else
21615     {
21616       add_AT_flag (type_die, DW_AT_declaration, 1);
21617
21618       /* We don't need to do this for function-local types.  */
21619       if (TYPE_STUB_DECL (type)
21620           && ! decl_function_context (TYPE_STUB_DECL (type)))
21621         VEC_safe_push (tree, gc, incomplete_types, type);
21622     }
21623
21624   if (get_AT (type_die, DW_AT_name))
21625     add_pubtype (type, type_die);
21626 }
21627
21628 /* Generate a DIE for a subroutine _type_.  */
21629
21630 static void
21631 gen_subroutine_type_die (tree type, dw_die_ref context_die)
21632 {
21633   tree return_type = TREE_TYPE (type);
21634   dw_die_ref subr_die
21635     = new_die (DW_TAG_subroutine_type,
21636                scope_die_for (type, context_die), type);
21637
21638   equate_type_number_to_die (type, subr_die);
21639   add_prototyped_attribute (subr_die, type);
21640   add_type_attribute (subr_die, return_type, 0, 0, context_die);
21641   gen_formal_types_die (type, subr_die);
21642
21643   if (get_AT (subr_die, DW_AT_name))
21644     add_pubtype (type, subr_die);
21645 }
21646
21647 /* Generate a DIE for a type definition.  */
21648
21649 static void
21650 gen_typedef_die (tree decl, dw_die_ref context_die)
21651 {
21652   dw_die_ref type_die;
21653   tree origin;
21654
21655   if (TREE_ASM_WRITTEN (decl))
21656     return;
21657
21658   TREE_ASM_WRITTEN (decl) = 1;
21659   type_die = new_die (DW_TAG_typedef, context_die, decl);
21660   origin = decl_ultimate_origin (decl);
21661   if (origin != NULL)
21662     add_abstract_origin_attribute (type_die, origin);
21663   else
21664     {
21665       tree type;
21666
21667       add_name_and_src_coords_attributes (type_die, decl);
21668       if (DECL_ORIGINAL_TYPE (decl))
21669         {
21670           type = DECL_ORIGINAL_TYPE (decl);
21671
21672           gcc_assert (type != TREE_TYPE (decl));
21673           equate_type_number_to_die (TREE_TYPE (decl), type_die);
21674         }
21675       else
21676         {
21677           type = TREE_TYPE (decl);
21678
21679           if (is_naming_typedef_decl (TYPE_NAME (type)))
21680             {
21681               /* Here, we are in the case of decl being a typedef naming
21682                  an anonymous type, e.g:
21683                      typedef struct {...} foo;
21684                  In that case TREE_TYPE (decl) is not a typedef variant
21685                  type and TYPE_NAME of the anonymous type is set to the
21686                  TYPE_DECL of the typedef. This construct is emitted by
21687                  the C++ FE.
21688
21689                  TYPE is the anonymous struct named by the typedef
21690                  DECL. As we need the DW_AT_type attribute of the
21691                  DW_TAG_typedef to point to the DIE of TYPE, let's
21692                  generate that DIE right away. add_type_attribute
21693                  called below will then pick (via lookup_type_die) that
21694                  anonymous struct DIE.  */
21695               if (!TREE_ASM_WRITTEN (type))
21696                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
21697
21698               /* This is a GNU Extension.  We are adding a
21699                  DW_AT_linkage_name attribute to the DIE of the
21700                  anonymous struct TYPE.  The value of that attribute
21701                  is the name of the typedef decl naming the anonymous
21702                  struct.  This greatly eases the work of consumers of
21703                  this debug info.  */
21704               add_linkage_attr (lookup_type_die (type), decl);
21705             }
21706         }
21707
21708       add_type_attribute (type_die, type, TREE_READONLY (decl),
21709                           TREE_THIS_VOLATILE (decl), context_die);
21710
21711       if (is_naming_typedef_decl (decl))
21712         /* We want that all subsequent calls to lookup_type_die with
21713            TYPE in argument yield the DW_TAG_typedef we have just
21714            created.  */
21715         equate_type_number_to_die (type, type_die);
21716
21717       add_accessibility_attribute (type_die, decl);
21718     }
21719
21720   if (DECL_ABSTRACT (decl))
21721     equate_decl_number_to_die (decl, type_die);
21722
21723   if (get_AT (type_die, DW_AT_name))
21724     add_pubtype (decl, type_die);
21725 }
21726
21727 /* Generate a DIE for a struct, class, enum or union type.  */
21728
21729 static void
21730 gen_tagged_type_die (tree type,
21731                      dw_die_ref context_die,
21732                      enum debug_info_usage usage)
21733 {
21734   int need_pop;
21735
21736   if (type == NULL_TREE
21737       || !is_tagged_type (type))
21738     return;
21739
21740   /* If this is a nested type whose containing class hasn't been written
21741      out yet, writing it out will cover this one, too.  This does not apply
21742      to instantiations of member class templates; they need to be added to
21743      the containing class as they are generated.  FIXME: This hurts the
21744      idea of combining type decls from multiple TUs, since we can't predict
21745      what set of template instantiations we'll get.  */
21746   if (TYPE_CONTEXT (type)
21747       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
21748       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
21749     {
21750       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
21751
21752       if (TREE_ASM_WRITTEN (type))
21753         return;
21754
21755       /* If that failed, attach ourselves to the stub.  */
21756       push_decl_scope (TYPE_CONTEXT (type));
21757       context_die = lookup_type_die (TYPE_CONTEXT (type));
21758       need_pop = 1;
21759     }
21760   else if (TYPE_CONTEXT (type) != NULL_TREE
21761            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
21762     {
21763       /* If this type is local to a function that hasn't been written
21764          out yet, use a NULL context for now; it will be fixed up in
21765          decls_for_scope.  */
21766       context_die = lookup_decl_die (TYPE_CONTEXT (type));
21767       /* A declaration DIE doesn't count; nested types need to go in the
21768          specification.  */
21769       if (context_die && is_declaration_die (context_die))
21770         context_die = NULL;
21771       need_pop = 0;
21772     }
21773   else
21774     {
21775       context_die = declare_in_namespace (type, context_die);
21776       need_pop = 0;
21777     }
21778
21779   if (TREE_CODE (type) == ENUMERAL_TYPE)
21780     {
21781       /* This might have been written out by the call to
21782          declare_in_namespace.  */
21783       if (!TREE_ASM_WRITTEN (type))
21784         gen_enumeration_type_die (type, context_die);
21785     }
21786   else
21787     gen_struct_or_union_type_die (type, context_die, usage);
21788
21789   if (need_pop)
21790     pop_decl_scope ();
21791
21792   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
21793      it up if it is ever completed.  gen_*_type_die will set it for us
21794      when appropriate.  */
21795 }
21796
21797 /* Generate a type description DIE.  */
21798
21799 static void
21800 gen_type_die_with_usage (tree type, dw_die_ref context_die,
21801                          enum debug_info_usage usage)
21802 {
21803   struct array_descr_info info;
21804
21805   if (type == NULL_TREE || type == error_mark_node)
21806     return;
21807
21808   if (TYPE_NAME (type) != NULL_TREE
21809       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21810       && is_redundant_typedef (TYPE_NAME (type))
21811       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
21812     /* The DECL of this type is a typedef we don't want to emit debug
21813        info for but we want debug info for its underlying typedef.
21814        This can happen for e.g, the injected-class-name of a C++
21815        type.  */
21816     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
21817
21818   /* If TYPE is a typedef type variant, let's generate debug info
21819      for the parent typedef which TYPE is a type of.  */
21820   if (typedef_variant_p (type))
21821     {
21822       if (TREE_ASM_WRITTEN (type))
21823         return;
21824
21825       /* Prevent broken recursion; we can't hand off to the same type.  */
21826       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
21827
21828       /* Use the DIE of the containing namespace as the parent DIE of
21829          the type description DIE we want to generate.  */
21830       if (DECL_CONTEXT (TYPE_NAME (type))
21831           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21832         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21833
21834       TREE_ASM_WRITTEN (type) = 1;
21835
21836       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21837       return;
21838     }
21839
21840   /* If type is an anonymous tagged type named by a typedef, let's
21841      generate debug info for the typedef.  */
21842   if (is_naming_typedef_decl (TYPE_NAME (type)))
21843     {
21844       /* Use the DIE of the containing namespace as the parent DIE of
21845          the type description DIE we want to generate.  */
21846       if (DECL_CONTEXT (TYPE_NAME (type))
21847           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21848         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21849       
21850       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21851       return;
21852     }
21853
21854   /* If this is an array type with hidden descriptor, handle it first.  */
21855   if (!TREE_ASM_WRITTEN (type)
21856       && lang_hooks.types.get_array_descr_info
21857       && lang_hooks.types.get_array_descr_info (type, &info)
21858       && (dwarf_version >= 3 || !dwarf_strict))
21859     {
21860       gen_descr_array_type_die (type, &info, context_die);
21861       TREE_ASM_WRITTEN (type) = 1;
21862       return;
21863     }
21864
21865   /* We are going to output a DIE to represent the unqualified version
21866      of this type (i.e. without any const or volatile qualifiers) so
21867      get the main variant (i.e. the unqualified version) of this type
21868      now.  (Vectors are special because the debugging info is in the
21869      cloned type itself).  */
21870   if (TREE_CODE (type) != VECTOR_TYPE)
21871     type = type_main_variant (type);
21872
21873   if (TREE_ASM_WRITTEN (type))
21874     return;
21875
21876   switch (TREE_CODE (type))
21877     {
21878     case ERROR_MARK:
21879       break;
21880
21881     case POINTER_TYPE:
21882     case REFERENCE_TYPE:
21883       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
21884          ensures that the gen_type_die recursion will terminate even if the
21885          type is recursive.  Recursive types are possible in Ada.  */
21886       /* ??? We could perhaps do this for all types before the switch
21887          statement.  */
21888       TREE_ASM_WRITTEN (type) = 1;
21889
21890       /* For these types, all that is required is that we output a DIE (or a
21891          set of DIEs) to represent the "basis" type.  */
21892       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21893                                 DINFO_USAGE_IND_USE);
21894       break;
21895
21896     case OFFSET_TYPE:
21897       /* This code is used for C++ pointer-to-data-member types.
21898          Output a description of the relevant class type.  */
21899       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
21900                                         DINFO_USAGE_IND_USE);
21901
21902       /* Output a description of the type of the object pointed to.  */
21903       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21904                                         DINFO_USAGE_IND_USE);
21905
21906       /* Now output a DIE to represent this pointer-to-data-member type
21907          itself.  */
21908       gen_ptr_to_mbr_type_die (type, context_die);
21909       break;
21910
21911     case FUNCTION_TYPE:
21912       /* Force out return type (in case it wasn't forced out already).  */
21913       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21914                                         DINFO_USAGE_DIR_USE);
21915       gen_subroutine_type_die (type, context_die);
21916       break;
21917
21918     case METHOD_TYPE:
21919       /* Force out return type (in case it wasn't forced out already).  */
21920       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21921                                         DINFO_USAGE_DIR_USE);
21922       gen_subroutine_type_die (type, context_die);
21923       break;
21924
21925     case ARRAY_TYPE:
21926       gen_array_type_die (type, context_die);
21927       break;
21928
21929     case VECTOR_TYPE:
21930       gen_array_type_die (type, context_die);
21931       break;
21932
21933     case ENUMERAL_TYPE:
21934     case RECORD_TYPE:
21935     case UNION_TYPE:
21936     case QUAL_UNION_TYPE:
21937       gen_tagged_type_die (type, context_die, usage);
21938       return;
21939
21940     case VOID_TYPE:
21941     case INTEGER_TYPE:
21942     case REAL_TYPE:
21943     case FIXED_POINT_TYPE:
21944     case COMPLEX_TYPE:
21945     case BOOLEAN_TYPE:
21946       /* No DIEs needed for fundamental types.  */
21947       break;
21948
21949     case NULLPTR_TYPE:
21950     case LANG_TYPE:
21951       /* Just use DW_TAG_unspecified_type.  */
21952       {
21953         dw_die_ref type_die = lookup_type_die (type);
21954         if (type_die == NULL)
21955           {
21956             tree name = TYPE_NAME (type);
21957             if (TREE_CODE (name) == TYPE_DECL)
21958               name = DECL_NAME (name);
21959             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
21960             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
21961             equate_type_number_to_die (type, type_die);
21962           }
21963       }
21964       break;
21965
21966     default:
21967       gcc_unreachable ();
21968     }
21969
21970   TREE_ASM_WRITTEN (type) = 1;
21971 }
21972
21973 static void
21974 gen_type_die (tree type, dw_die_ref context_die)
21975 {
21976   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
21977 }
21978
21979 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
21980    things which are local to the given block.  */
21981
21982 static void
21983 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
21984 {
21985   int must_output_die = 0;
21986   bool inlined_func;
21987
21988   /* Ignore blocks that are NULL.  */
21989   if (stmt == NULL_TREE)
21990     return;
21991
21992   inlined_func = inlined_function_outer_scope_p (stmt);
21993
21994   /* If the block is one fragment of a non-contiguous block, do not
21995      process the variables, since they will have been done by the
21996      origin block.  Do process subblocks.  */
21997   if (BLOCK_FRAGMENT_ORIGIN (stmt))
21998     {
21999       tree sub;
22000
22001       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
22002         gen_block_die (sub, context_die, depth + 1);
22003
22004       return;
22005     }
22006
22007   /* Determine if we need to output any Dwarf DIEs at all to represent this
22008      block.  */
22009   if (inlined_func)
22010     /* The outer scopes for inlinings *must* always be represented.  We
22011        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
22012     must_output_die = 1;
22013   else
22014     {
22015       /* Determine if this block directly contains any "significant"
22016          local declarations which we will need to output DIEs for.  */
22017       if (debug_info_level > DINFO_LEVEL_TERSE)
22018         /* We are not in terse mode so *any* local declaration counts
22019            as being a "significant" one.  */
22020         must_output_die = ((BLOCK_VARS (stmt) != NULL
22021                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
22022                            && (TREE_USED (stmt)
22023                                || TREE_ASM_WRITTEN (stmt)
22024                                || BLOCK_ABSTRACT (stmt)));
22025       else if ((TREE_USED (stmt)
22026                 || TREE_ASM_WRITTEN (stmt)
22027                 || BLOCK_ABSTRACT (stmt))
22028                && !dwarf2out_ignore_block (stmt))
22029         must_output_die = 1;
22030     }
22031
22032   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
22033      DIE for any block which contains no significant local declarations at
22034      all.  Rather, in such cases we just call `decls_for_scope' so that any
22035      needed Dwarf info for any sub-blocks will get properly generated. Note
22036      that in terse mode, our definition of what constitutes a "significant"
22037      local declaration gets restricted to include only inlined function
22038      instances and local (nested) function definitions.  */
22039   if (must_output_die)
22040     {
22041       if (inlined_func)
22042         {
22043           /* If STMT block is abstract, that means we have been called
22044              indirectly from dwarf2out_abstract_function.
22045              That function rightfully marks the descendent blocks (of
22046              the abstract function it is dealing with) as being abstract,
22047              precisely to prevent us from emitting any
22048              DW_TAG_inlined_subroutine DIE as a descendent
22049              of an abstract function instance. So in that case, we should
22050              not call gen_inlined_subroutine_die.
22051
22052              Later though, when cgraph asks dwarf2out to emit info
22053              for the concrete instance of the function decl into which
22054              the concrete instance of STMT got inlined, the later will lead
22055              to the generation of a DW_TAG_inlined_subroutine DIE.  */
22056           if (! BLOCK_ABSTRACT (stmt))
22057             gen_inlined_subroutine_die (stmt, context_die, depth);
22058         }
22059       else
22060         gen_lexical_block_die (stmt, context_die, depth);
22061     }
22062   else
22063     decls_for_scope (stmt, context_die, depth);
22064 }
22065
22066 /* Process variable DECL (or variable with origin ORIGIN) within
22067    block STMT and add it to CONTEXT_DIE.  */
22068 static void
22069 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
22070 {
22071   dw_die_ref die;
22072   tree decl_or_origin = decl ? decl : origin;
22073
22074   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
22075     die = lookup_decl_die (decl_or_origin);
22076   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
22077            && TYPE_DECL_IS_STUB (decl_or_origin))
22078     die = lookup_type_die (TREE_TYPE (decl_or_origin));
22079   else
22080     die = NULL;
22081
22082   if (die != NULL && die->die_parent == NULL)
22083     add_child_die (context_die, die);
22084   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
22085     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
22086                                          stmt, context_die);
22087   else
22088     gen_decl_die (decl, origin, context_die);
22089 }
22090
22091 /* Generate all of the decls declared within a given scope and (recursively)
22092    all of its sub-blocks.  */
22093
22094 static void
22095 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
22096 {
22097   tree decl;
22098   unsigned int i;
22099   tree subblocks;
22100
22101   /* Ignore NULL blocks.  */
22102   if (stmt == NULL_TREE)
22103     return;
22104
22105   /* Output the DIEs to represent all of the data objects and typedefs
22106      declared directly within this block but not within any nested
22107      sub-blocks.  Also, nested function and tag DIEs have been
22108      generated with a parent of NULL; fix that up now.  */
22109   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
22110     process_scope_var (stmt, decl, NULL_TREE, context_die);
22111   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
22112     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
22113                        context_die);
22114
22115   /* If we're at -g1, we're not interested in subblocks.  */
22116   if (debug_info_level <= DINFO_LEVEL_TERSE)
22117     return;
22118
22119   /* Output the DIEs to represent all sub-blocks (and the items declared
22120      therein) of this block.  */
22121   for (subblocks = BLOCK_SUBBLOCKS (stmt);
22122        subblocks != NULL;
22123        subblocks = BLOCK_CHAIN (subblocks))
22124     gen_block_die (subblocks, context_die, depth + 1);
22125 }
22126
22127 /* Is this a typedef we can avoid emitting?  */
22128
22129 static inline int
22130 is_redundant_typedef (const_tree decl)
22131 {
22132   if (TYPE_DECL_IS_STUB (decl))
22133     return 1;
22134
22135   if (DECL_ARTIFICIAL (decl)
22136       && DECL_CONTEXT (decl)
22137       && is_tagged_type (DECL_CONTEXT (decl))
22138       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
22139       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
22140     /* Also ignore the artificial member typedef for the class name.  */
22141     return 1;
22142
22143   return 0;
22144 }
22145
22146 /* Return TRUE if TYPE is a typedef that names a type for linkage
22147    purposes. This kind of typedefs is produced by the C++ FE for
22148    constructs like:
22149
22150    typedef struct {...} foo;
22151
22152    In that case, there is no typedef variant type produced for foo.
22153    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
22154    struct type.  */
22155
22156 static bool
22157 is_naming_typedef_decl (const_tree decl)
22158 {
22159   if (decl == NULL_TREE
22160       || TREE_CODE (decl) != TYPE_DECL
22161       || !is_tagged_type (TREE_TYPE (decl))
22162       || DECL_IS_BUILTIN (decl)
22163       || is_redundant_typedef (decl)
22164       /* It looks like Ada produces TYPE_DECLs that are very similar
22165          to C++ naming typedefs but that have different
22166          semantics. Let's be specific to c++ for now.  */
22167       || !is_cxx ())
22168     return FALSE;
22169
22170   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
22171           && TYPE_NAME (TREE_TYPE (decl)) == decl
22172           && (TYPE_STUB_DECL (TREE_TYPE (decl))
22173               != TYPE_NAME (TREE_TYPE (decl))));
22174 }
22175
22176 /* Returns the DIE for a context.  */
22177
22178 static inline dw_die_ref
22179 get_context_die (tree context)
22180 {
22181   if (context)
22182     {
22183       /* Find die that represents this context.  */
22184       if (TYPE_P (context))
22185         {
22186           context = TYPE_MAIN_VARIANT (context);
22187           return strip_naming_typedef (context, force_type_die (context));
22188         }
22189       else
22190         return force_decl_die (context);
22191     }
22192   return comp_unit_die ();
22193 }
22194
22195 /* Returns the DIE for decl.  A DIE will always be returned.  */
22196
22197 static dw_die_ref
22198 force_decl_die (tree decl)
22199 {
22200   dw_die_ref decl_die;
22201   unsigned saved_external_flag;
22202   tree save_fn = NULL_TREE;
22203   decl_die = lookup_decl_die (decl);
22204   if (!decl_die)
22205     {
22206       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
22207
22208       decl_die = lookup_decl_die (decl);
22209       if (decl_die)
22210         return decl_die;
22211
22212       switch (TREE_CODE (decl))
22213         {
22214         case FUNCTION_DECL:
22215           /* Clear current_function_decl, so that gen_subprogram_die thinks
22216              that this is a declaration. At this point, we just want to force
22217              declaration die.  */
22218           save_fn = current_function_decl;
22219           current_function_decl = NULL_TREE;
22220           gen_subprogram_die (decl, context_die);
22221           current_function_decl = save_fn;
22222           break;
22223
22224         case VAR_DECL:
22225           /* Set external flag to force declaration die. Restore it after
22226            gen_decl_die() call.  */
22227           saved_external_flag = DECL_EXTERNAL (decl);
22228           DECL_EXTERNAL (decl) = 1;
22229           gen_decl_die (decl, NULL, context_die);
22230           DECL_EXTERNAL (decl) = saved_external_flag;
22231           break;
22232
22233         case NAMESPACE_DECL:
22234           if (dwarf_version >= 3 || !dwarf_strict)
22235             dwarf2out_decl (decl);
22236           else
22237             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
22238             decl_die = comp_unit_die ();
22239           break;
22240
22241         case TRANSLATION_UNIT_DECL:
22242           decl_die = comp_unit_die ();
22243           break;
22244
22245         default:
22246           gcc_unreachable ();
22247         }
22248
22249       /* We should be able to find the DIE now.  */
22250       if (!decl_die)
22251         decl_die = lookup_decl_die (decl);
22252       gcc_assert (decl_die);
22253     }
22254
22255   return decl_die;
22256 }
22257
22258 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
22259    always returned.  */
22260
22261 static dw_die_ref
22262 force_type_die (tree type)
22263 {
22264   dw_die_ref type_die;
22265
22266   type_die = lookup_type_die (type);
22267   if (!type_die)
22268     {
22269       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
22270
22271       type_die = modified_type_die (type, TYPE_READONLY (type),
22272                                     TYPE_VOLATILE (type), context_die);
22273       gcc_assert (type_die);
22274     }
22275   return type_die;
22276 }
22277
22278 /* Force out any required namespaces to be able to output DECL,
22279    and return the new context_die for it, if it's changed.  */
22280
22281 static dw_die_ref
22282 setup_namespace_context (tree thing, dw_die_ref context_die)
22283 {
22284   tree context = (DECL_P (thing)
22285                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
22286   if (context && TREE_CODE (context) == NAMESPACE_DECL)
22287     /* Force out the namespace.  */
22288     context_die = force_decl_die (context);
22289
22290   return context_die;
22291 }
22292
22293 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
22294    type) within its namespace, if appropriate.
22295
22296    For compatibility with older debuggers, namespace DIEs only contain
22297    declarations; all definitions are emitted at CU scope.  */
22298
22299 static dw_die_ref
22300 declare_in_namespace (tree thing, dw_die_ref context_die)
22301 {
22302   dw_die_ref ns_context;
22303
22304   if (debug_info_level <= DINFO_LEVEL_TERSE)
22305     return context_die;
22306
22307   /* If this decl is from an inlined function, then don't try to emit it in its
22308      namespace, as we will get confused.  It would have already been emitted
22309      when the abstract instance of the inline function was emitted anyways.  */
22310   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
22311     return context_die;
22312
22313   ns_context = setup_namespace_context (thing, context_die);
22314
22315   if (ns_context != context_die)
22316     {
22317       if (is_fortran ())
22318         return ns_context;
22319       if (DECL_P (thing))
22320         gen_decl_die (thing, NULL, ns_context);
22321       else
22322         gen_type_die (thing, ns_context);
22323     }
22324   return context_die;
22325 }
22326
22327 /* Generate a DIE for a namespace or namespace alias.  */
22328
22329 static void
22330 gen_namespace_die (tree decl, dw_die_ref context_die)
22331 {
22332   dw_die_ref namespace_die;
22333
22334   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
22335      they are an alias of.  */
22336   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
22337     {
22338       /* Output a real namespace or module.  */
22339       context_die = setup_namespace_context (decl, comp_unit_die ());
22340       namespace_die = new_die (is_fortran ()
22341                                ? DW_TAG_module : DW_TAG_namespace,
22342                                context_die, decl);
22343       /* For Fortran modules defined in different CU don't add src coords.  */
22344       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
22345         {
22346           const char *name = dwarf2_name (decl, 0);
22347           if (name)
22348             add_name_attribute (namespace_die, name);
22349         }
22350       else
22351         add_name_and_src_coords_attributes (namespace_die, decl);
22352       if (DECL_EXTERNAL (decl))
22353         add_AT_flag (namespace_die, DW_AT_declaration, 1);
22354       equate_decl_number_to_die (decl, namespace_die);
22355     }
22356   else
22357     {
22358       /* Output a namespace alias.  */
22359
22360       /* Force out the namespace we are an alias of, if necessary.  */
22361       dw_die_ref origin_die
22362         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
22363
22364       if (DECL_FILE_SCOPE_P (decl)
22365           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
22366         context_die = setup_namespace_context (decl, comp_unit_die ());
22367       /* Now create the namespace alias DIE.  */
22368       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
22369       add_name_and_src_coords_attributes (namespace_die, decl);
22370       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
22371       equate_decl_number_to_die (decl, namespace_die);
22372     }
22373 }
22374
22375 /* Generate Dwarf debug information for a decl described by DECL.
22376    The return value is currently only meaningful for PARM_DECLs,
22377    for all other decls it returns NULL.  */
22378
22379 static dw_die_ref
22380 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
22381 {
22382   tree decl_or_origin = decl ? decl : origin;
22383   tree class_origin = NULL, ultimate_origin;
22384
22385   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
22386     return NULL;
22387
22388   switch (TREE_CODE (decl_or_origin))
22389     {
22390     case ERROR_MARK:
22391       break;
22392
22393     case CONST_DECL:
22394       if (!is_fortran () && !is_ada ())
22395         {
22396           /* The individual enumerators of an enum type get output when we output
22397              the Dwarf representation of the relevant enum type itself.  */
22398           break;
22399         }
22400
22401       /* Emit its type.  */
22402       gen_type_die (TREE_TYPE (decl), context_die);
22403
22404       /* And its containing namespace.  */
22405       context_die = declare_in_namespace (decl, context_die);
22406
22407       gen_const_die (decl, context_die);
22408       break;
22409
22410     case FUNCTION_DECL:
22411       /* Don't output any DIEs to represent mere function declarations,
22412          unless they are class members or explicit block externs.  */
22413       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
22414           && DECL_FILE_SCOPE_P (decl_or_origin)
22415           && (current_function_decl == NULL_TREE
22416               || DECL_ARTIFICIAL (decl_or_origin)))
22417         break;
22418
22419 #if 0
22420       /* FIXME */
22421       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
22422          on local redeclarations of global functions.  That seems broken.  */
22423       if (current_function_decl != decl)
22424         /* This is only a declaration.  */;
22425 #endif
22426
22427       /* If we're emitting a clone, emit info for the abstract instance.  */
22428       if (origin || DECL_ORIGIN (decl) != decl)
22429         dwarf2out_abstract_function (origin
22430                                      ? DECL_ORIGIN (origin)
22431                                      : DECL_ABSTRACT_ORIGIN (decl));
22432
22433       /* If we're emitting an out-of-line copy of an inline function,
22434          emit info for the abstract instance and set up to refer to it.  */
22435       else if (cgraph_function_possibly_inlined_p (decl)
22436                && ! DECL_ABSTRACT (decl)
22437                && ! class_or_namespace_scope_p (context_die)
22438                /* dwarf2out_abstract_function won't emit a die if this is just
22439                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
22440                   that case, because that works only if we have a die.  */
22441                && DECL_INITIAL (decl) != NULL_TREE)
22442         {
22443           dwarf2out_abstract_function (decl);
22444           set_decl_origin_self (decl);
22445         }
22446
22447       /* Otherwise we're emitting the primary DIE for this decl.  */
22448       else if (debug_info_level > DINFO_LEVEL_TERSE)
22449         {
22450           /* Before we describe the FUNCTION_DECL itself, make sure that we
22451              have its containing type.  */
22452           if (!origin)
22453             origin = decl_class_context (decl);
22454           if (origin != NULL_TREE)
22455             gen_type_die (origin, context_die);
22456
22457           /* And its return type.  */
22458           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
22459
22460           /* And its virtual context.  */
22461           if (DECL_VINDEX (decl) != NULL_TREE)
22462             gen_type_die (DECL_CONTEXT (decl), context_die);
22463
22464           /* Make sure we have a member DIE for decl.  */
22465           if (origin != NULL_TREE)
22466             gen_type_die_for_member (origin, decl, context_die);
22467
22468           /* And its containing namespace.  */
22469           context_die = declare_in_namespace (decl, context_die);
22470         }
22471
22472       /* Now output a DIE to represent the function itself.  */
22473       if (decl)
22474         gen_subprogram_die (decl, context_die);
22475       break;
22476
22477     case TYPE_DECL:
22478       /* If we are in terse mode, don't generate any DIEs to represent any
22479          actual typedefs.  */
22480       if (debug_info_level <= DINFO_LEVEL_TERSE)
22481         break;
22482
22483       /* In the special case of a TYPE_DECL node representing the declaration
22484          of some type tag, if the given TYPE_DECL is marked as having been
22485          instantiated from some other (original) TYPE_DECL node (e.g. one which
22486          was generated within the original definition of an inline function) we
22487          used to generate a special (abbreviated) DW_TAG_structure_type,
22488          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
22489          should be actually referencing those DIEs, as variable DIEs with that
22490          type would be emitted already in the abstract origin, so it was always
22491          removed during unused type prunning.  Don't add anything in this
22492          case.  */
22493       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
22494         break;
22495
22496       if (is_redundant_typedef (decl))
22497         gen_type_die (TREE_TYPE (decl), context_die);
22498       else
22499         /* Output a DIE to represent the typedef itself.  */
22500         gen_typedef_die (decl, context_die);
22501       break;
22502
22503     case LABEL_DECL:
22504       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22505         gen_label_die (decl, context_die);
22506       break;
22507
22508     case VAR_DECL:
22509     case RESULT_DECL:
22510       /* If we are in terse mode, don't generate any DIEs to represent any
22511          variable declarations or definitions.  */
22512       if (debug_info_level <= DINFO_LEVEL_TERSE)
22513         break;
22514
22515       /* Output any DIEs that are needed to specify the type of this data
22516          object.  */
22517       if (decl_by_reference_p (decl_or_origin))
22518         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
22519       else
22520         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
22521
22522       /* And its containing type.  */
22523       class_origin = decl_class_context (decl_or_origin);
22524       if (class_origin != NULL_TREE)
22525         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
22526
22527       /* And its containing namespace.  */
22528       context_die = declare_in_namespace (decl_or_origin, context_die);
22529
22530       /* Now output the DIE to represent the data object itself.  This gets
22531          complicated because of the possibility that the VAR_DECL really
22532          represents an inlined instance of a formal parameter for an inline
22533          function.  */
22534       ultimate_origin = decl_ultimate_origin (decl_or_origin);
22535       if (ultimate_origin != NULL_TREE
22536           && TREE_CODE (ultimate_origin) == PARM_DECL)
22537         gen_formal_parameter_die (decl, origin,
22538                                   true /* Emit name attribute.  */,
22539                                   context_die);
22540       else
22541         gen_variable_die (decl, origin, context_die);
22542       break;
22543
22544     case FIELD_DECL:
22545       /* Ignore the nameless fields that are used to skip bits but handle C++
22546          anonymous unions and structs.  */
22547       if (DECL_NAME (decl) != NULL_TREE
22548           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
22549           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
22550         {
22551           gen_type_die (member_declared_type (decl), context_die);
22552           gen_field_die (decl, context_die);
22553         }
22554       break;
22555
22556     case PARM_DECL:
22557       if (DECL_BY_REFERENCE (decl_or_origin))
22558         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
22559       else
22560         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
22561       return gen_formal_parameter_die (decl, origin,
22562                                        true /* Emit name attribute.  */,
22563                                        context_die);
22564
22565     case NAMESPACE_DECL:
22566     case IMPORTED_DECL:
22567       if (dwarf_version >= 3 || !dwarf_strict)
22568         gen_namespace_die (decl, context_die);
22569       break;
22570
22571     default:
22572       /* Probably some frontend-internal decl.  Assume we don't care.  */
22573       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
22574       break;
22575     }
22576
22577   return NULL;
22578 }
22579 \f
22580 /* Output debug information for global decl DECL.  Called from toplev.c after
22581    compilation proper has finished.  */
22582
22583 static void
22584 dwarf2out_global_decl (tree decl)
22585 {
22586   /* Output DWARF2 information for file-scope tentative data object
22587      declarations, file-scope (extern) function declarations (which
22588      had no corresponding body) and file-scope tagged type declarations
22589      and definitions which have not yet been forced out.  */
22590   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
22591     dwarf2out_decl (decl);
22592 }
22593
22594 /* Output debug information for type decl DECL.  Called from toplev.c
22595    and from language front ends (to record built-in types).  */
22596 static void
22597 dwarf2out_type_decl (tree decl, int local)
22598 {
22599   if (!local)
22600     dwarf2out_decl (decl);
22601 }
22602
22603 /* Output debug information for imported module or decl DECL.
22604    NAME is non-NULL name in the lexical block if the decl has been renamed.
22605    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
22606    that DECL belongs to.
22607    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
22608 static void
22609 dwarf2out_imported_module_or_decl_1 (tree decl,
22610                                      tree name,
22611                                      tree lexical_block,
22612                                      dw_die_ref lexical_block_die)
22613 {
22614   expanded_location xloc;
22615   dw_die_ref imported_die = NULL;
22616   dw_die_ref at_import_die;
22617
22618   if (TREE_CODE (decl) == IMPORTED_DECL)
22619     {
22620       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
22621       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
22622       gcc_assert (decl);
22623     }
22624   else
22625     xloc = expand_location (input_location);
22626
22627   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
22628     {
22629       at_import_die = force_type_die (TREE_TYPE (decl));
22630       /* For namespace N { typedef void T; } using N::T; base_type_die
22631          returns NULL, but DW_TAG_imported_declaration requires
22632          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
22633       if (!at_import_die)
22634         {
22635           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
22636           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
22637           at_import_die = lookup_type_die (TREE_TYPE (decl));
22638           gcc_assert (at_import_die);
22639         }
22640     }
22641   else
22642     {
22643       at_import_die = lookup_decl_die (decl);
22644       if (!at_import_die)
22645         {
22646           /* If we're trying to avoid duplicate debug info, we may not have
22647              emitted the member decl for this field.  Emit it now.  */
22648           if (TREE_CODE (decl) == FIELD_DECL)
22649             {
22650               tree type = DECL_CONTEXT (decl);
22651
22652               if (TYPE_CONTEXT (type)
22653                   && TYPE_P (TYPE_CONTEXT (type))
22654                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
22655                                                 DINFO_USAGE_DIR_USE))
22656                 return;
22657               gen_type_die_for_member (type, decl,
22658                                        get_context_die (TYPE_CONTEXT (type)));
22659             }
22660           at_import_die = force_decl_die (decl);
22661         }
22662     }
22663
22664   if (TREE_CODE (decl) == NAMESPACE_DECL)
22665     {
22666       if (dwarf_version >= 3 || !dwarf_strict)
22667         imported_die = new_die (DW_TAG_imported_module,
22668                                 lexical_block_die,
22669                                 lexical_block);
22670       else
22671         return;
22672     }
22673   else
22674     imported_die = new_die (DW_TAG_imported_declaration,
22675                             lexical_block_die,
22676                             lexical_block);
22677
22678   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
22679   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
22680   if (name)
22681     add_AT_string (imported_die, DW_AT_name,
22682                    IDENTIFIER_POINTER (name));
22683   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
22684 }
22685
22686 /* Output debug information for imported module or decl DECL.
22687    NAME is non-NULL name in context if the decl has been renamed.
22688    CHILD is true if decl is one of the renamed decls as part of
22689    importing whole module.  */
22690
22691 static void
22692 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
22693                                    bool child)
22694 {
22695   /* dw_die_ref at_import_die;  */
22696   dw_die_ref scope_die;
22697
22698   if (debug_info_level <= DINFO_LEVEL_TERSE)
22699     return;
22700
22701   gcc_assert (decl);
22702
22703   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
22704      We need decl DIE for reference and scope die. First, get DIE for the decl
22705      itself.  */
22706
22707   /* Get the scope die for decl context. Use comp_unit_die for global module
22708      or decl. If die is not found for non globals, force new die.  */
22709   if (context
22710       && TYPE_P (context)
22711       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
22712     return;
22713
22714   if (!(dwarf_version >= 3 || !dwarf_strict))
22715     return;
22716
22717   scope_die = get_context_die (context);
22718
22719   if (child)
22720     {
22721       gcc_assert (scope_die->die_child);
22722       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
22723       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
22724       scope_die = scope_die->die_child;
22725     }
22726
22727   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
22728   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
22729
22730 }
22731
22732 /* Write the debugging output for DECL.  */
22733
22734 void
22735 dwarf2out_decl (tree decl)
22736 {
22737   dw_die_ref context_die = comp_unit_die ();
22738
22739   switch (TREE_CODE (decl))
22740     {
22741     case ERROR_MARK:
22742       return;
22743
22744     case FUNCTION_DECL:
22745       /* What we would really like to do here is to filter out all mere
22746          file-scope declarations of file-scope functions which are never
22747          referenced later within this translation unit (and keep all of ones
22748          that *are* referenced later on) but we aren't clairvoyant, so we have
22749          no idea which functions will be referenced in the future (i.e. later
22750          on within the current translation unit). So here we just ignore all
22751          file-scope function declarations which are not also definitions.  If
22752          and when the debugger needs to know something about these functions,
22753          it will have to hunt around and find the DWARF information associated
22754          with the definition of the function.
22755
22756          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
22757          nodes represent definitions and which ones represent mere
22758          declarations.  We have to check DECL_INITIAL instead. That's because
22759          the C front-end supports some weird semantics for "extern inline"
22760          function definitions.  These can get inlined within the current
22761          translation unit (and thus, we need to generate Dwarf info for their
22762          abstract instances so that the Dwarf info for the concrete inlined
22763          instances can have something to refer to) but the compiler never
22764          generates any out-of-lines instances of such things (despite the fact
22765          that they *are* definitions).
22766
22767          The important point is that the C front-end marks these "extern
22768          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
22769          them anyway. Note that the C++ front-end also plays some similar games
22770          for inline function definitions appearing within include files which
22771          also contain `#pragma interface' pragmas.  */
22772       if (DECL_INITIAL (decl) == NULL_TREE)
22773         return;
22774
22775       /* If we're a nested function, initially use a parent of NULL; if we're
22776          a plain function, this will be fixed up in decls_for_scope.  If
22777          we're a method, it will be ignored, since we already have a DIE.  */
22778       if (decl_function_context (decl)
22779           /* But if we're in terse mode, we don't care about scope.  */
22780           && debug_info_level > DINFO_LEVEL_TERSE)
22781         context_die = NULL;
22782       break;
22783
22784     case VAR_DECL:
22785       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
22786          declaration and if the declaration was never even referenced from
22787          within this entire compilation unit.  We suppress these DIEs in
22788          order to save space in the .debug section (by eliminating entries
22789          which are probably useless).  Note that we must not suppress
22790          block-local extern declarations (whether used or not) because that
22791          would screw-up the debugger's name lookup mechanism and cause it to
22792          miss things which really ought to be in scope at a given point.  */
22793       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
22794         return;
22795
22796       /* For local statics lookup proper context die.  */
22797       if (TREE_STATIC (decl) && decl_function_context (decl))
22798         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22799
22800       /* If we are in terse mode, don't generate any DIEs to represent any
22801          variable declarations or definitions.  */
22802       if (debug_info_level <= DINFO_LEVEL_TERSE)
22803         return;
22804       break;
22805
22806     case CONST_DECL:
22807       if (debug_info_level <= DINFO_LEVEL_TERSE)
22808         return;
22809       if (!is_fortran () && !is_ada ())
22810         return;
22811       if (TREE_STATIC (decl) && decl_function_context (decl))
22812         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22813       break;
22814
22815     case NAMESPACE_DECL:
22816     case IMPORTED_DECL:
22817       if (debug_info_level <= DINFO_LEVEL_TERSE)
22818         return;
22819       if (lookup_decl_die (decl) != NULL)
22820         return;
22821       break;
22822
22823     case TYPE_DECL:
22824       /* Don't emit stubs for types unless they are needed by other DIEs.  */
22825       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
22826         return;
22827
22828       /* Don't bother trying to generate any DIEs to represent any of the
22829          normal built-in types for the language we are compiling.  */
22830       if (DECL_IS_BUILTIN (decl))
22831         return;
22832
22833       /* If we are in terse mode, don't generate any DIEs for types.  */
22834       if (debug_info_level <= DINFO_LEVEL_TERSE)
22835         return;
22836
22837       /* If we're a function-scope tag, initially use a parent of NULL;
22838          this will be fixed up in decls_for_scope.  */
22839       if (decl_function_context (decl))
22840         context_die = NULL;
22841
22842       break;
22843
22844     default:
22845       return;
22846     }
22847
22848   gen_decl_die (decl, NULL, context_die);
22849 }
22850
22851 /* Write the debugging output for DECL.  */
22852
22853 static void
22854 dwarf2out_function_decl (tree decl)
22855 {
22856   dwarf2out_decl (decl);
22857   call_arg_locations = NULL;
22858   call_arg_loc_last = NULL;
22859   call_site_count = -1;
22860   tail_call_site_count = -1;
22861   VEC_free (dw_die_ref, heap, block_map);
22862   htab_empty (decl_loc_table);
22863   htab_empty (cached_dw_loc_list_table);
22864 }
22865
22866 /* Output a marker (i.e. a label) for the beginning of the generated code for
22867    a lexical block.  */
22868
22869 static void
22870 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
22871                        unsigned int blocknum)
22872 {
22873   switch_to_section (current_function_section ());
22874   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
22875 }
22876
22877 /* Output a marker (i.e. a label) for the end of the generated code for a
22878    lexical block.  */
22879
22880 static void
22881 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22882 {
22883   switch_to_section (current_function_section ());
22884   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22885 }
22886
22887 /* Returns nonzero if it is appropriate not to emit any debugging
22888    information for BLOCK, because it doesn't contain any instructions.
22889
22890    Don't allow this for blocks with nested functions or local classes
22891    as we would end up with orphans, and in the presence of scheduling
22892    we may end up calling them anyway.  */
22893
22894 static bool
22895 dwarf2out_ignore_block (const_tree block)
22896 {
22897   tree decl;
22898   unsigned int i;
22899
22900   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22901     if (TREE_CODE (decl) == FUNCTION_DECL
22902         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22903       return 0;
22904   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22905     {
22906       decl = BLOCK_NONLOCALIZED_VAR (block, i);
22907       if (TREE_CODE (decl) == FUNCTION_DECL
22908           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22909       return 0;
22910     }
22911
22912   return 1;
22913 }
22914
22915 /* Hash table routines for file_hash.  */
22916
22917 static int
22918 file_table_eq (const void *p1_p, const void *p2_p)
22919 {
22920   const struct dwarf_file_data *const p1 =
22921     (const struct dwarf_file_data *) p1_p;
22922   const char *const p2 = (const char *) p2_p;
22923   return filename_cmp (p1->filename, p2) == 0;
22924 }
22925
22926 static hashval_t
22927 file_table_hash (const void *p_p)
22928 {
22929   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
22930   return htab_hash_string (p->filename);
22931 }
22932
22933 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22934    dwarf2out.c) and return its "index".  The index of each (known) filename is
22935    just a unique number which is associated with only that one filename.  We
22936    need such numbers for the sake of generating labels (in the .debug_sfnames
22937    section) and references to those files numbers (in the .debug_srcinfo
22938    and.debug_macinfo sections).  If the filename given as an argument is not
22939    found in our current list, add it to the list and assign it the next
22940    available unique index number.  In order to speed up searches, we remember
22941    the index of the filename was looked up last.  This handles the majority of
22942    all searches.  */
22943
22944 static struct dwarf_file_data *
22945 lookup_filename (const char *file_name)
22946 {
22947   void ** slot;
22948   struct dwarf_file_data * created;
22949
22950   /* Check to see if the file name that was searched on the previous
22951      call matches this file name.  If so, return the index.  */
22952   if (file_table_last_lookup
22953       && (file_name == file_table_last_lookup->filename
22954           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
22955     return file_table_last_lookup;
22956
22957   /* Didn't match the previous lookup, search the table.  */
22958   slot = htab_find_slot_with_hash (file_table, file_name,
22959                                    htab_hash_string (file_name), INSERT);
22960   if (*slot)
22961     return (struct dwarf_file_data *) *slot;
22962
22963   created = ggc_alloc_dwarf_file_data ();
22964   created->filename = file_name;
22965   created->emitted_number = 0;
22966   *slot = created;
22967   return created;
22968 }
22969
22970 /* If the assembler will construct the file table, then translate the compiler
22971    internal file table number into the assembler file table number, and emit
22972    a .file directive if we haven't already emitted one yet.  The file table
22973    numbers are different because we prune debug info for unused variables and
22974    types, which may include filenames.  */
22975
22976 static int
22977 maybe_emit_file (struct dwarf_file_data * fd)
22978 {
22979   if (! fd->emitted_number)
22980     {
22981       if (last_emitted_file)
22982         fd->emitted_number = last_emitted_file->emitted_number + 1;
22983       else
22984         fd->emitted_number = 1;
22985       last_emitted_file = fd;
22986
22987       if (DWARF2_ASM_LINE_DEBUG_INFO)
22988         {
22989           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22990           output_quoted_string (asm_out_file,
22991                                 remap_debug_filename (fd->filename));
22992           fputc ('\n', asm_out_file);
22993         }
22994     }
22995
22996   return fd->emitted_number;
22997 }
22998
22999 /* Schedule generation of a DW_AT_const_value attribute to DIE.
23000    That generation should happen after function debug info has been
23001    generated. The value of the attribute is the constant value of ARG.  */
23002
23003 static void
23004 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
23005 {
23006   die_arg_entry entry;
23007
23008   if (!die || !arg)
23009     return;
23010
23011   if (!tmpl_value_parm_die_table)
23012     tmpl_value_parm_die_table
23013       = VEC_alloc (die_arg_entry, gc, 32);
23014
23015   entry.die = die;
23016   entry.arg = arg;
23017   VEC_safe_push (die_arg_entry, gc,
23018                  tmpl_value_parm_die_table,
23019                  &entry);
23020 }
23021
23022 /* Return TRUE if T is an instance of generic type, FALSE
23023    otherwise.  */
23024
23025 static bool
23026 generic_type_p (tree t)
23027 {
23028   if (t == NULL_TREE || !TYPE_P (t))
23029     return false;
23030   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
23031 }
23032
23033 /* Schedule the generation of the generic parameter dies for the
23034   instance of generic type T. The proper generation itself is later
23035   done by gen_scheduled_generic_parms_dies. */
23036
23037 static void
23038 schedule_generic_params_dies_gen (tree t)
23039 {
23040   if (!generic_type_p (t))
23041     return;
23042
23043   if (generic_type_instances == NULL)
23044     generic_type_instances = VEC_alloc (tree, gc, 256);
23045
23046   VEC_safe_push (tree, gc, generic_type_instances, t);
23047 }
23048
23049 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
23050    by append_entry_to_tmpl_value_parm_die_table. This function must
23051    be called after function DIEs have been generated.  */
23052
23053 static void
23054 gen_remaining_tmpl_value_param_die_attribute (void)
23055 {
23056   if (tmpl_value_parm_die_table)
23057     {
23058       unsigned i;
23059       die_arg_entry *e;
23060
23061       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
23062         tree_add_const_value_attribute (e->die, e->arg);
23063     }
23064 }
23065
23066 /* Generate generic parameters DIEs for instances of generic types
23067    that have been previously scheduled by
23068    schedule_generic_params_dies_gen. This function must be called
23069    after all the types of the CU have been laid out.  */
23070
23071 static void
23072 gen_scheduled_generic_parms_dies (void)
23073 {
23074   unsigned i;
23075   tree t;
23076
23077   if (generic_type_instances == NULL)
23078     return;
23079   
23080   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
23081     gen_generic_params_dies (t);
23082 }
23083
23084
23085 /* Replace DW_AT_name for the decl with name.  */
23086
23087 static void
23088 dwarf2out_set_name (tree decl, tree name)
23089 {
23090   dw_die_ref die;
23091   dw_attr_ref attr;
23092   const char *dname;
23093
23094   die = TYPE_SYMTAB_DIE (decl);
23095   if (!die)
23096     return;
23097
23098   dname = dwarf2_name (name, 0);
23099   if (!dname)
23100     return;
23101
23102   attr = get_AT (die, DW_AT_name);
23103   if (attr)
23104     {
23105       struct indirect_string_node *node;
23106
23107       node = find_AT_string (dname);
23108       /* replace the string.  */
23109       attr->dw_attr_val.v.val_str = node;
23110     }
23111
23112   else
23113     add_name_attribute (die, dname);
23114 }
23115
23116 /* Called by the final INSN scan whenever we see a var location.  We
23117    use it to drop labels in the right places, and throw the location in
23118    our lookup table.  */
23119
23120 static void
23121 dwarf2out_var_location (rtx loc_note)
23122 {
23123   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
23124   struct var_loc_node *newloc;
23125   rtx next_real;
23126   static const char *last_label;
23127   static const char *last_postcall_label;
23128   static bool last_in_cold_section_p;
23129   tree decl;
23130   bool var_loc_p;
23131
23132   if (!NOTE_P (loc_note))
23133     {
23134       if (CALL_P (loc_note))
23135         {
23136           call_site_count++;
23137           if (SIBLING_CALL_P (loc_note))
23138             tail_call_site_count++;
23139         }
23140       return;
23141     }
23142
23143   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
23144   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
23145     return;
23146
23147   next_real = next_real_insn (loc_note);
23148
23149   /* If there are no instructions which would be affected by this note,
23150      don't do anything.  */
23151   if (var_loc_p
23152       && next_real == NULL_RTX
23153       && !NOTE_DURING_CALL_P (loc_note))
23154     return;
23155
23156   if (next_real == NULL_RTX)
23157     next_real = get_last_insn ();
23158
23159   /* If there were any real insns between note we processed last time
23160      and this note (or if it is the first note), clear
23161      last_{,postcall_}label so that they are not reused this time.  */
23162   if (last_var_location_insn == NULL_RTX
23163       || last_var_location_insn != next_real
23164       || last_in_cold_section_p != in_cold_section_p)
23165     {
23166       last_label = NULL;
23167       last_postcall_label = NULL;
23168     }
23169
23170   if (var_loc_p)
23171     {
23172       decl = NOTE_VAR_LOCATION_DECL (loc_note);
23173       newloc = add_var_loc_to_decl (decl, loc_note,
23174                                     NOTE_DURING_CALL_P (loc_note)
23175                                     ? last_postcall_label : last_label);
23176       if (newloc == NULL)
23177         return;
23178     }
23179   else
23180     {
23181       decl = NULL_TREE;
23182       newloc = NULL;
23183     }
23184
23185   /* If there were no real insns between note we processed last time
23186      and this note, use the label we emitted last time.  Otherwise
23187      create a new label and emit it.  */
23188   if (last_label == NULL)
23189     {
23190       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
23191       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
23192       loclabel_num++;
23193       last_label = ggc_strdup (loclabel);
23194     }
23195
23196   if (!var_loc_p)
23197     {
23198       struct call_arg_loc_node *ca_loc
23199         = ggc_alloc_cleared_call_arg_loc_node ();
23200       rtx prev = prev_real_insn (loc_note), x;
23201       ca_loc->call_arg_loc_note = loc_note;
23202       ca_loc->next = NULL;
23203       ca_loc->label = last_label;
23204       gcc_assert (prev
23205                   && (CALL_P (prev)
23206                       || (NONJUMP_INSN_P (prev)
23207                           && GET_CODE (PATTERN (prev)) == SEQUENCE
23208                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
23209       if (!CALL_P (prev))
23210         prev = XVECEXP (PATTERN (prev), 0, 0);
23211       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
23212       x = PATTERN (prev);
23213       if (GET_CODE (x) == PARALLEL)
23214         x = XVECEXP (x, 0, 0);
23215       if (GET_CODE (x) == SET)
23216         x = SET_SRC (x);
23217       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
23218         {
23219           x = XEXP (XEXP (x, 0), 0);
23220           if (GET_CODE (x) == SYMBOL_REF
23221               && SYMBOL_REF_DECL (x)
23222               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
23223             ca_loc->symbol_ref = x;
23224         }
23225       ca_loc->block = insn_scope (prev);
23226       if (call_arg_locations)
23227         call_arg_loc_last->next = ca_loc;
23228       else
23229         call_arg_locations = ca_loc;
23230       call_arg_loc_last = ca_loc;
23231     }
23232   else if (!NOTE_DURING_CALL_P (loc_note))
23233     newloc->label = last_label;
23234   else
23235     {
23236       if (!last_postcall_label)
23237         {
23238           sprintf (loclabel, "%s-1", last_label);
23239           last_postcall_label = ggc_strdup (loclabel);
23240         }
23241       newloc->label = last_postcall_label;
23242     }
23243
23244   last_var_location_insn = next_real;
23245   last_in_cold_section_p = in_cold_section_p;
23246 }
23247
23248 /* Note in one location list that text section has changed.  */
23249
23250 static int
23251 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
23252 {
23253   var_loc_list *list = (var_loc_list *) *slot;
23254   if (list->first)
23255     list->last_before_switch
23256       = list->last->next ? list->last->next : list->last;
23257   return 1;
23258 }
23259
23260 /* Note in all location lists that text section has changed.  */
23261
23262 static void
23263 var_location_switch_text_section (void)
23264 {
23265   if (decl_loc_table == NULL)
23266     return;
23267
23268   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
23269 }
23270
23271 /* Create a new line number table.  */
23272
23273 static dw_line_info_table *
23274 new_line_info_table (void)
23275 {
23276   dw_line_info_table *table;
23277
23278   table = ggc_alloc_cleared_dw_line_info_table_struct ();
23279   table->file_num = 1;
23280   table->line_num = 1;
23281   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
23282
23283   return table;
23284 }
23285
23286 /* Lookup the "current" table into which we emit line info, so
23287    that we don't have to do it for every source line.  */
23288
23289 static void
23290 set_cur_line_info_table (section *sec)
23291 {
23292   dw_line_info_table *table;
23293
23294   if (sec == text_section)
23295     table = text_section_line_info;
23296   else if (sec == cold_text_section)
23297     {
23298       table = cold_text_section_line_info;
23299       if (!table)
23300         {
23301           cold_text_section_line_info = table = new_line_info_table ();
23302           table->end_label = cold_end_label;
23303         }
23304     }
23305   else
23306     {
23307       const char *end_label;
23308
23309       if (flag_reorder_blocks_and_partition)
23310         {
23311           if (in_cold_section_p)
23312             end_label = crtl->subsections.cold_section_end_label;
23313           else
23314             end_label = crtl->subsections.hot_section_end_label;
23315         }
23316       else
23317         {
23318           char label[MAX_ARTIFICIAL_LABEL_BYTES];
23319           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
23320                                        current_function_funcdef_no);
23321           end_label = ggc_strdup (label);
23322         }
23323
23324       table = new_line_info_table ();
23325       table->end_label = end_label;
23326
23327       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
23328     }
23329
23330   cur_line_info_table = table;
23331 }
23332
23333
23334 /* We need to reset the locations at the beginning of each
23335    function. We can't do this in the end_function hook, because the
23336    declarations that use the locations won't have been output when
23337    that hook is called.  Also compute have_multiple_function_sections here.  */
23338
23339 static void
23340 dwarf2out_begin_function (tree fun)
23341 {
23342   section *sec = function_section (fun);
23343
23344   if (sec != text_section)
23345     have_multiple_function_sections = true;
23346
23347   if (flag_reorder_blocks_and_partition && !cold_text_section)
23348     {
23349       gcc_assert (current_function_decl == fun);
23350       cold_text_section = unlikely_text_section ();
23351       switch_to_section (cold_text_section);
23352       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
23353       switch_to_section (sec);
23354     }
23355
23356   dwarf2out_note_section_used ();
23357   call_site_count = 0;
23358   tail_call_site_count = 0;
23359
23360   set_cur_line_info_table (sec);
23361 }
23362
23363 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
23364
23365 static void
23366 push_dw_line_info_entry (dw_line_info_table *table,
23367                          enum dw_line_info_opcode opcode, unsigned int val)
23368 {
23369   dw_line_info_entry e;
23370   e.opcode = opcode;
23371   e.val = val;
23372   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
23373 }
23374
23375 /* Output a label to mark the beginning of a source code line entry
23376    and record information relating to this source line, in
23377    'line_info_table' for later output of the .debug_line section.  */
23378 /* ??? The discriminator parameter ought to be unsigned.  */
23379
23380 static void
23381 dwarf2out_source_line (unsigned int line, const char *filename,
23382                        int discriminator, bool is_stmt)
23383 {
23384   unsigned int file_num;
23385   dw_line_info_table *table;
23386
23387   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
23388     return;
23389
23390   /* The discriminator column was added in dwarf4.  Simplify the below
23391      by simply removing it if we're not supposed to output it.  */
23392   if (dwarf_version < 4 && dwarf_strict)
23393     discriminator = 0;
23394
23395   table = cur_line_info_table;
23396   file_num = maybe_emit_file (lookup_filename (filename));
23397
23398   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
23399      the debugger has used the second (possibly duplicate) line number
23400      at the beginning of the function to mark the end of the prologue.
23401      We could eliminate any other duplicates within the function.  For
23402      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
23403      that second line number entry.  */
23404   /* Recall that this end-of-prologue indication is *not* the same thing
23405      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
23406      to which the hook corresponds, follows the last insn that was 
23407      emitted by gen_prologue.  What we need is to preceed the first insn
23408      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
23409      insn that corresponds to something the user wrote.  These may be
23410      very different locations once scheduling is enabled.  */
23411
23412   if (0 && file_num == table->file_num
23413       && line == table->line_num
23414       && discriminator == table->discrim_num
23415       && is_stmt == table->is_stmt)
23416     return;
23417
23418   switch_to_section (current_function_section ());
23419
23420   /* If requested, emit something human-readable.  */
23421   if (flag_debug_asm)
23422     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
23423
23424   if (DWARF2_ASM_LINE_DEBUG_INFO)
23425     {
23426       /* Emit the .loc directive understood by GNU as.  */
23427       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
23428       if (is_stmt != table->is_stmt)
23429         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
23430       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
23431         fprintf (asm_out_file, " discriminator %d", discriminator);
23432       fputc ('\n', asm_out_file);
23433     }
23434   else
23435     {
23436       unsigned int label_num = ++line_info_label_num;
23437
23438       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
23439
23440       push_dw_line_info_entry (table, LI_set_address, label_num);
23441       if (file_num != table->file_num)
23442         push_dw_line_info_entry (table, LI_set_file, file_num);
23443       if (discriminator != table->discrim_num)
23444         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
23445       if (is_stmt != table->is_stmt)
23446         push_dw_line_info_entry (table, LI_negate_stmt, 0);
23447       push_dw_line_info_entry (table, LI_set_line, line);
23448     }
23449
23450   table->file_num = file_num;
23451   table->line_num = line;
23452   table->discrim_num = discriminator;
23453   table->is_stmt = is_stmt;
23454   table->in_use = true;
23455 }
23456
23457 /* Record the beginning of a new source file.  */
23458
23459 static void
23460 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
23461 {
23462   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
23463     {
23464       /* Record the beginning of the file for break_out_includes.  */
23465       dw_die_ref bincl_die;
23466
23467       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
23468       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
23469     }
23470
23471   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23472     {
23473       macinfo_entry e;
23474       e.code = DW_MACINFO_start_file;
23475       e.lineno = lineno;
23476       e.info = xstrdup (filename);
23477       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23478     }
23479 }
23480
23481 /* Record the end of a source file.  */
23482
23483 static void
23484 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
23485 {
23486   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
23487     /* Record the end of the file for break_out_includes.  */
23488     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
23489
23490   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23491     {
23492       macinfo_entry e;
23493       e.code = DW_MACINFO_end_file;
23494       e.lineno = lineno;
23495       e.info = NULL;
23496       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23497     }
23498 }
23499
23500 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
23501    the tail part of the directive line, i.e. the part which is past the
23502    initial whitespace, #, whitespace, directive-name, whitespace part.  */
23503
23504 static void
23505 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
23506                   const char *buffer ATTRIBUTE_UNUSED)
23507 {
23508   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23509     {
23510       macinfo_entry e;
23511       e.code = DW_MACINFO_define;
23512       e.lineno = lineno;
23513       e.info = xstrdup (buffer);;
23514       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23515     }
23516 }
23517
23518 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
23519    the tail part of the directive line, i.e. the part which is past the
23520    initial whitespace, #, whitespace, directive-name, whitespace part.  */
23521
23522 static void
23523 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
23524                  const char *buffer ATTRIBUTE_UNUSED)
23525 {
23526   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23527     {
23528       macinfo_entry e;
23529       e.code = DW_MACINFO_undef;
23530       e.lineno = lineno;
23531       e.info = xstrdup (buffer);;
23532       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23533     }
23534 }
23535
23536 static void
23537 output_macinfo (void)
23538 {
23539   unsigned i;
23540   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
23541   macinfo_entry *ref;
23542
23543   if (! length)
23544     return;
23545
23546   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
23547     {
23548       switch (ref->code)
23549         {
23550           case DW_MACINFO_start_file:
23551             {
23552               int file_num = maybe_emit_file (lookup_filename (ref->info));
23553               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
23554               dw2_asm_output_data_uleb128 
23555                         (ref->lineno, "Included from line number %lu", 
23556                                                 (unsigned long)ref->lineno);
23557               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
23558             }
23559             break;
23560           case DW_MACINFO_end_file:
23561             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
23562             break;
23563           case DW_MACINFO_define:
23564             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
23565             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
23566                                                 (unsigned long)ref->lineno);
23567             dw2_asm_output_nstring (ref->info, -1, "The macro");
23568             break;
23569           case DW_MACINFO_undef:
23570             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
23571             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
23572                                                 (unsigned long)ref->lineno);
23573             dw2_asm_output_nstring (ref->info, -1, "The macro");
23574             break;
23575           default:
23576            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
23577              ASM_COMMENT_START, (unsigned long)ref->code);
23578           break;
23579         }
23580     }
23581 }
23582
23583 /* Set up for Dwarf output at the start of compilation.  */
23584
23585 static void
23586 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23587 {
23588   /* Allocate the file_table.  */
23589   file_table = htab_create_ggc (50, file_table_hash,
23590                                 file_table_eq, NULL);
23591
23592   /* Allocate the decl_die_table.  */
23593   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
23594                                     decl_die_table_eq, NULL);
23595
23596   /* Allocate the decl_loc_table.  */
23597   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
23598                                     decl_loc_table_eq, NULL);
23599
23600   /* Allocate the cached_dw_loc_list_table.  */
23601   cached_dw_loc_list_table
23602     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
23603                        cached_dw_loc_list_table_eq, NULL);
23604
23605   /* Allocate the initial hunk of the decl_scope_table.  */
23606   decl_scope_table = VEC_alloc (tree, gc, 256);
23607
23608   /* Allocate the initial hunk of the abbrev_die_table.  */
23609   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
23610     (ABBREV_DIE_TABLE_INCREMENT);
23611   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23612   /* Zero-th entry is allocated, but unused.  */
23613   abbrev_die_table_in_use = 1;
23614
23615   /* Allocate the pubtypes and pubnames vectors.  */
23616   pubname_table = VEC_alloc (pubname_entry, gc, 32);
23617   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
23618
23619   incomplete_types = VEC_alloc (tree, gc, 64);
23620
23621   used_rtx_array = VEC_alloc (rtx, gc, 32);
23622
23623   debug_info_section = get_section (DEBUG_INFO_SECTION,
23624                                     SECTION_DEBUG, NULL);
23625   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23626                                       SECTION_DEBUG, NULL);
23627   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23628                                        SECTION_DEBUG, NULL);
23629   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
23630                                        SECTION_DEBUG, NULL);
23631   debug_line_section = get_section (DEBUG_LINE_SECTION,
23632                                     SECTION_DEBUG, NULL);
23633   debug_loc_section = get_section (DEBUG_LOC_SECTION,
23634                                    SECTION_DEBUG, NULL);
23635   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23636                                         SECTION_DEBUG, NULL);
23637   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23638                                         SECTION_DEBUG, NULL);
23639   debug_str_section = get_section (DEBUG_STR_SECTION,
23640                                    DEBUG_STR_SECTION_FLAGS, NULL);
23641   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23642                                       SECTION_DEBUG, NULL);
23643   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23644                                      SECTION_DEBUG, NULL);
23645
23646   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23647   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23648                                DEBUG_ABBREV_SECTION_LABEL, 0);
23649   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23650   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23651                                COLD_TEXT_SECTION_LABEL, 0);
23652   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23653
23654   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23655                                DEBUG_INFO_SECTION_LABEL, 0);
23656   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23657                                DEBUG_LINE_SECTION_LABEL, 0);
23658   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23659                                DEBUG_RANGES_SECTION_LABEL, 0);
23660   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23661                                DEBUG_MACINFO_SECTION_LABEL, 0);
23662
23663   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23664     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
23665
23666   switch_to_section (text_section);
23667   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23668
23669   /* Make sure the line number table for .text always exists.  */
23670   text_section_line_info = new_line_info_table ();
23671   text_section_line_info->end_label = text_end_label;
23672 }
23673
23674 /* Called before cgraph_optimize starts outputtting functions, variables
23675    and toplevel asms into assembly.  */
23676
23677 static void
23678 dwarf2out_assembly_start (void)
23679 {
23680   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23681       && dwarf2out_do_cfi_asm ()
23682       && (!(flag_unwind_tables || flag_exceptions)
23683           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23684     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23685 }
23686
23687 /* A helper function for dwarf2out_finish called through
23688    htab_traverse.  Emit one queued .debug_str string.  */
23689
23690 static int
23691 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
23692 {
23693   struct indirect_string_node *node = (struct indirect_string_node *) *h;
23694
23695   if (node->form == DW_FORM_strp)
23696     {
23697       switch_to_section (debug_str_section);
23698       ASM_OUTPUT_LABEL (asm_out_file, node->label);
23699       assemble_string (node->str, strlen (node->str) + 1);
23700     }
23701
23702   return 1;
23703 }
23704
23705 #if ENABLE_ASSERT_CHECKING
23706 /* Verify that all marks are clear.  */
23707
23708 static void
23709 verify_marks_clear (dw_die_ref die)
23710 {
23711   dw_die_ref c;
23712
23713   gcc_assert (! die->die_mark);
23714   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23715 }
23716 #endif /* ENABLE_ASSERT_CHECKING */
23717
23718 /* Clear the marks for a die and its children.
23719    Be cool if the mark isn't set.  */
23720
23721 static void
23722 prune_unmark_dies (dw_die_ref die)
23723 {
23724   dw_die_ref c;
23725
23726   if (die->die_mark)
23727     die->die_mark = 0;
23728   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23729 }
23730
23731 /* Given DIE that we're marking as used, find any other dies
23732    it references as attributes and mark them as used.  */
23733
23734 static void
23735 prune_unused_types_walk_attribs (dw_die_ref die)
23736 {
23737   dw_attr_ref a;
23738   unsigned ix;
23739
23740   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23741     {
23742       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23743         {
23744           /* A reference to another DIE.
23745              Make sure that it will get emitted.
23746              If it was broken out into a comdat group, don't follow it.  */
23747           if (! use_debug_types
23748               || a->dw_attr == DW_AT_specification
23749               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
23750             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23751         }
23752       /* Set the string's refcount to 0 so that prune_unused_types_mark
23753          accounts properly for it.  */
23754       if (AT_class (a) == dw_val_class_str)
23755         a->dw_attr_val.v.val_str->refcount = 0;
23756     }
23757 }
23758
23759 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23760
23761 static void
23762 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23763 {
23764   dw_die_ref c;
23765
23766   if (die == NULL || die->die_child == NULL)
23767     return;
23768   c = die->die_child;
23769   do
23770     {
23771       switch (c->die_tag)
23772         {
23773         case DW_TAG_template_type_param:
23774         case DW_TAG_template_value_param:
23775         case DW_TAG_GNU_template_template_param:
23776         case DW_TAG_GNU_template_parameter_pack:
23777           prune_unused_types_mark (c, 1);
23778           break;
23779         default:
23780           break;
23781         }
23782       c = c->die_sib;
23783     } while (c && c != die->die_child);
23784 }
23785
23786 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23787    to DIE's children.  */
23788
23789 static void
23790 prune_unused_types_mark (dw_die_ref die, int dokids)
23791 {
23792   dw_die_ref c;
23793
23794   if (die->die_mark == 0)
23795     {
23796       /* We haven't done this node yet.  Mark it as used.  */
23797       die->die_mark = 1;
23798       /* If this is the DIE of a generic type instantiation,
23799          mark the children DIEs that describe its generic parms and
23800          args.  */
23801       prune_unused_types_mark_generic_parms_dies (die);
23802
23803       /* We also have to mark its parents as used.
23804          (But we don't want to mark our parents' kids due to this.)  */
23805       if (die->die_parent)
23806         prune_unused_types_mark (die->die_parent, 0);
23807
23808       /* Mark any referenced nodes.  */
23809       prune_unused_types_walk_attribs (die);
23810
23811       /* If this node is a specification,
23812          also mark the definition, if it exists.  */
23813       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23814         prune_unused_types_mark (die->die_definition, 1);
23815     }
23816
23817   if (dokids && die->die_mark != 2)
23818     {
23819       /* We need to walk the children, but haven't done so yet.
23820          Remember that we've walked the kids.  */
23821       die->die_mark = 2;
23822
23823       /* If this is an array type, we need to make sure our
23824          kids get marked, even if they're types.  If we're
23825          breaking out types into comdat sections, do this
23826          for all type definitions.  */
23827       if (die->die_tag == DW_TAG_array_type
23828           || (use_debug_types
23829               && is_type_die (die) && ! is_declaration_die (die)))
23830         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23831       else
23832         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23833     }
23834 }
23835
23836 /* For local classes, look if any static member functions were emitted
23837    and if so, mark them.  */
23838
23839 static void
23840 prune_unused_types_walk_local_classes (dw_die_ref die)
23841 {
23842   dw_die_ref c;
23843
23844   if (die->die_mark == 2)
23845     return;
23846
23847   switch (die->die_tag)
23848     {
23849     case DW_TAG_structure_type:
23850     case DW_TAG_union_type:
23851     case DW_TAG_class_type:
23852       break;
23853
23854     case DW_TAG_subprogram:
23855       if (!get_AT_flag (die, DW_AT_declaration)
23856           || die->die_definition != NULL)
23857         prune_unused_types_mark (die, 1);
23858       return;
23859
23860     default:
23861       return;
23862     }
23863
23864   /* Mark children.  */
23865   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23866 }
23867
23868 /* Walk the tree DIE and mark types that we actually use.  */
23869
23870 static void
23871 prune_unused_types_walk (dw_die_ref die)
23872 {
23873   dw_die_ref c;
23874
23875   /* Don't do anything if this node is already marked and
23876      children have been marked as well.  */
23877   if (die->die_mark == 2)
23878     return;
23879
23880   switch (die->die_tag)
23881     {
23882     case DW_TAG_structure_type:
23883     case DW_TAG_union_type:
23884     case DW_TAG_class_type:
23885       if (die->die_perennial_p)
23886         break;
23887
23888       for (c = die->die_parent; c; c = c->die_parent)
23889         if (c->die_tag == DW_TAG_subprogram)
23890           break;
23891
23892       /* Finding used static member functions inside of classes
23893          is needed just for local classes, because for other classes
23894          static member function DIEs with DW_AT_specification
23895          are emitted outside of the DW_TAG_*_type.  If we ever change
23896          it, we'd need to call this even for non-local classes.  */
23897       if (c)
23898         prune_unused_types_walk_local_classes (die);
23899
23900       /* It's a type node --- don't mark it.  */
23901       return;
23902
23903     case DW_TAG_const_type:
23904     case DW_TAG_packed_type:
23905     case DW_TAG_pointer_type:
23906     case DW_TAG_reference_type:
23907     case DW_TAG_rvalue_reference_type:
23908     case DW_TAG_volatile_type:
23909     case DW_TAG_typedef:
23910     case DW_TAG_array_type:
23911     case DW_TAG_interface_type:
23912     case DW_TAG_friend:
23913     case DW_TAG_variant_part:
23914     case DW_TAG_enumeration_type:
23915     case DW_TAG_subroutine_type:
23916     case DW_TAG_string_type:
23917     case DW_TAG_set_type:
23918     case DW_TAG_subrange_type:
23919     case DW_TAG_ptr_to_member_type:
23920     case DW_TAG_file_type:
23921       if (die->die_perennial_p)
23922         break;
23923
23924       /* It's a type node --- don't mark it.  */
23925       return;
23926
23927     default:
23928       /* Mark everything else.  */
23929       break;
23930   }
23931
23932   if (die->die_mark == 0)
23933     {
23934       die->die_mark = 1;
23935
23936       /* Now, mark any dies referenced from here.  */
23937       prune_unused_types_walk_attribs (die);
23938     }
23939
23940   die->die_mark = 2;
23941
23942   /* Mark children.  */
23943   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23944 }
23945
23946 /* Increment the string counts on strings referred to from DIE's
23947    attributes.  */
23948
23949 static void
23950 prune_unused_types_update_strings (dw_die_ref die)
23951 {
23952   dw_attr_ref a;
23953   unsigned ix;
23954
23955   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23956     if (AT_class (a) == dw_val_class_str)
23957       {
23958         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23959         s->refcount++;
23960         /* Avoid unnecessarily putting strings that are used less than
23961            twice in the hash table.  */
23962         if (s->refcount
23963             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23964           {
23965             void ** slot;
23966             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
23967                                              htab_hash_string (s->str),
23968                                              INSERT);
23969             gcc_assert (*slot == NULL);
23970             *slot = s;
23971           }
23972       }
23973 }
23974
23975 /* Remove from the tree DIE any dies that aren't marked.  */
23976
23977 static void
23978 prune_unused_types_prune (dw_die_ref die)
23979 {
23980   dw_die_ref c;
23981
23982   gcc_assert (die->die_mark);
23983   prune_unused_types_update_strings (die);
23984
23985   if (! die->die_child)
23986     return;
23987
23988   c = die->die_child;
23989   do {
23990     dw_die_ref prev = c;
23991     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23992       if (c == die->die_child)
23993         {
23994           /* No marked children between 'prev' and the end of the list.  */
23995           if (prev == c)
23996             /* No marked children at all.  */
23997             die->die_child = NULL;
23998           else
23999             {
24000               prev->die_sib = c->die_sib;
24001               die->die_child = prev;
24002             }
24003           return;
24004         }
24005
24006     if (c != prev->die_sib)
24007       prev->die_sib = c;
24008     prune_unused_types_prune (c);
24009   } while (c != die->die_child);
24010 }
24011
24012 /* Remove dies representing declarations that we never use.  */
24013
24014 static void
24015 prune_unused_types (void)
24016 {
24017   unsigned int i;
24018   limbo_die_node *node;
24019   comdat_type_node *ctnode;
24020   pubname_ref pub;
24021   dw_die_ref base_type;
24022
24023 #if ENABLE_ASSERT_CHECKING
24024   /* All the marks should already be clear.  */
24025   verify_marks_clear (comp_unit_die ());
24026   for (node = limbo_die_list; node; node = node->next)
24027     verify_marks_clear (node->die);
24028   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
24029     verify_marks_clear (ctnode->root_die);
24030 #endif /* ENABLE_ASSERT_CHECKING */
24031
24032   /* Mark types that are used in global variables.  */
24033   premark_types_used_by_global_vars ();
24034
24035   /* Set the mark on nodes that are actually used.  */
24036   prune_unused_types_walk (comp_unit_die ());
24037   for (node = limbo_die_list; node; node = node->next)
24038     prune_unused_types_walk (node->die);
24039   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
24040     {
24041       prune_unused_types_walk (ctnode->root_die);
24042       prune_unused_types_mark (ctnode->type_die, 1);
24043     }
24044
24045   /* Also set the mark on nodes referenced from the
24046      pubname_table.  */
24047   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
24048     prune_unused_types_mark (pub->die, 1);
24049   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
24050     prune_unused_types_mark (base_type, 1);
24051
24052   if (debug_str_hash)
24053     htab_empty (debug_str_hash);
24054   prune_unused_types_prune (comp_unit_die ());
24055   for (node = limbo_die_list; node; node = node->next)
24056     prune_unused_types_prune (node->die);
24057   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
24058     prune_unused_types_prune (ctnode->root_die);
24059
24060   /* Leave the marks clear.  */
24061   prune_unmark_dies (comp_unit_die ());
24062   for (node = limbo_die_list; node; node = node->next)
24063     prune_unmark_dies (node->die);
24064   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
24065     prune_unmark_dies (ctnode->root_die);
24066 }
24067
24068 /* Set the parameter to true if there are any relative pathnames in
24069    the file table.  */
24070 static int
24071 file_table_relative_p (void ** slot, void *param)
24072 {
24073   bool *p = (bool *) param;
24074   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
24075   if (!IS_ABSOLUTE_PATH (d->filename))
24076     {
24077       *p = true;
24078       return 0;
24079     }
24080   return 1;
24081 }
24082
24083 /* Routines to manipulate hash table of comdat type units.  */
24084
24085 static hashval_t
24086 htab_ct_hash (const void *of)
24087 {
24088   hashval_t h;
24089   const comdat_type_node *const type_node = (const comdat_type_node *) of;
24090
24091   memcpy (&h, type_node->signature, sizeof (h));
24092   return h;
24093 }
24094
24095 static int
24096 htab_ct_eq (const void *of1, const void *of2)
24097 {
24098   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
24099   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
24100
24101   return (! memcmp (type_node_1->signature, type_node_2->signature,
24102                     DWARF_TYPE_SIGNATURE_SIZE));
24103 }
24104
24105 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
24106    to the location it would have been added, should we know its
24107    DECL_ASSEMBLER_NAME when we added other attributes.  This will
24108    probably improve compactness of debug info, removing equivalent
24109    abbrevs, and hide any differences caused by deferring the
24110    computation of the assembler name, triggered by e.g. PCH.  */
24111
24112 static inline void
24113 move_linkage_attr (dw_die_ref die)
24114 {
24115   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
24116   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
24117
24118   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
24119               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
24120
24121   while (--ix > 0)
24122     {
24123       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
24124
24125       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
24126         break;
24127     }
24128
24129   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
24130     {
24131       VEC_pop (dw_attr_node, die->die_attr);
24132       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
24133     }
24134 }
24135
24136 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
24137    referenced from typed stack ops and count how often they are used.  */
24138
24139 static void
24140 mark_base_types (dw_loc_descr_ref loc)
24141 {
24142   dw_die_ref base_type = NULL;
24143
24144   for (; loc; loc = loc->dw_loc_next)
24145     {
24146       switch (loc->dw_loc_opc)
24147         {
24148         case DW_OP_GNU_regval_type:
24149         case DW_OP_GNU_deref_type:
24150           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
24151           break;
24152         case DW_OP_GNU_convert:
24153         case DW_OP_GNU_reinterpret:
24154           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
24155             continue;
24156           /* FALLTHRU */
24157         case DW_OP_GNU_const_type:
24158           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
24159           break;
24160         case DW_OP_GNU_entry_value:
24161           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
24162           continue;
24163         default:
24164           continue;
24165         }
24166       gcc_assert (base_type->die_parent == comp_unit_die ());
24167       if (base_type->die_mark)
24168         base_type->die_mark++;
24169       else
24170         {
24171           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
24172           base_type->die_mark = 1;
24173         }
24174     }
24175 }
24176
24177 /* Comparison function for sorting marked base types.  */
24178
24179 static int
24180 base_type_cmp (const void *x, const void *y)
24181 {
24182   dw_die_ref dx = *(const dw_die_ref *) x;
24183   dw_die_ref dy = *(const dw_die_ref *) y;
24184   unsigned int byte_size1, byte_size2;
24185   unsigned int encoding1, encoding2;
24186   if (dx->die_mark > dy->die_mark)
24187     return -1;
24188   if (dx->die_mark < dy->die_mark)
24189     return 1;
24190   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24191   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24192   if (byte_size1 < byte_size2)
24193     return 1;
24194   if (byte_size1 > byte_size2)
24195     return -1;
24196   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24197   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24198   if (encoding1 < encoding2)
24199     return 1;
24200   if (encoding1 > encoding2)
24201     return -1;
24202   return 0;
24203 }
24204
24205 /* Move base types marked by mark_base_types as early as possible
24206    in the CU, sorted by decreasing usage count both to make the
24207    uleb128 references as small as possible and to make sure they
24208    will have die_offset already computed by calc_die_sizes when
24209    sizes of typed stack loc ops is computed.  */
24210
24211 static void
24212 move_marked_base_types (void)
24213 {
24214   unsigned int i;
24215   dw_die_ref base_type, die, c;
24216
24217   if (VEC_empty (dw_die_ref, base_types))
24218     return;
24219
24220   /* Sort by decreasing usage count, they will be added again in that
24221      order later on.  */
24222   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
24223   die = comp_unit_die ();
24224   c = die->die_child;
24225   do
24226     {
24227       dw_die_ref prev = c;
24228       c = c->die_sib;
24229       while (c->die_mark)
24230         {
24231           remove_child_with_prev (c, prev);
24232           /* As base types got marked, there must be at least
24233              one node other than DW_TAG_base_type.  */
24234           gcc_assert (c != c->die_sib);
24235           c = c->die_sib;
24236         }
24237     }
24238   while (c != die->die_child);
24239   gcc_assert (die->die_child);
24240   c = die->die_child;
24241   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
24242     {
24243       base_type->die_mark = 0;
24244       base_type->die_sib = c->die_sib;
24245       c->die_sib = base_type;
24246       c = base_type;
24247     }
24248 }
24249
24250 /* Helper function for resolve_addr, attempt to resolve
24251    one CONST_STRING, return non-zero if not successful.  Similarly verify that
24252    SYMBOL_REFs refer to variables emitted in the current CU.  */
24253
24254 static int
24255 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
24256 {
24257   rtx rtl = *addr;
24258
24259   if (GET_CODE (rtl) == CONST_STRING)
24260     {
24261       size_t len = strlen (XSTR (rtl, 0)) + 1;
24262       tree t = build_string (len, XSTR (rtl, 0));
24263       tree tlen = size_int (len - 1);
24264       TREE_TYPE (t)
24265         = build_array_type (char_type_node, build_index_type (tlen));
24266       rtl = lookup_constant_def (t);
24267       if (!rtl || !MEM_P (rtl))
24268         return 1;
24269       rtl = XEXP (rtl, 0);
24270       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
24271       *addr = rtl;
24272       return 0;
24273     }
24274
24275   if (GET_CODE (rtl) == SYMBOL_REF
24276       && SYMBOL_REF_DECL (rtl))
24277     {
24278       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24279         {
24280           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24281             return 1;
24282         }
24283       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24284         return 1;
24285     }
24286
24287   if (GET_CODE (rtl) == CONST
24288       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
24289     return 1;
24290
24291   return 0;
24292 }
24293
24294 /* Helper function for resolve_addr, handle one location
24295    expression, return false if at least one CONST_STRING or SYMBOL_REF in
24296    the location list couldn't be resolved.  */
24297
24298 static bool
24299 resolve_addr_in_expr (dw_loc_descr_ref loc)
24300 {
24301   dw_loc_descr_ref keep = NULL;
24302   for (; loc; loc = loc->dw_loc_next)
24303     switch (loc->dw_loc_opc)
24304       {
24305       case DW_OP_addr:
24306         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
24307           return false;
24308         break;
24309       case DW_OP_const4u:
24310       case DW_OP_const8u:
24311         if (loc->dtprel
24312             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
24313           return false;
24314         break;
24315       case DW_OP_implicit_value:
24316         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24317             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
24318           return false;
24319         break;
24320       case DW_OP_GNU_implicit_pointer:
24321       case DW_OP_GNU_parameter_ref:
24322         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24323           {
24324             dw_die_ref ref
24325               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24326             if (ref == NULL)
24327               return false;
24328             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24329             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24330             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24331           }
24332         break;
24333       case DW_OP_GNU_const_type:
24334       case DW_OP_GNU_regval_type:
24335       case DW_OP_GNU_deref_type:
24336       case DW_OP_GNU_convert:
24337       case DW_OP_GNU_reinterpret:
24338         while (loc->dw_loc_next
24339                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24340           {
24341             dw_die_ref base1, base2;
24342             unsigned enc1, enc2, size1, size2;
24343             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24344                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24345               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24346             else if (loc->dw_loc_oprnd1.val_class
24347                      == dw_val_class_unsigned_const)
24348               break;
24349             else
24350               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24351             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24352                 == dw_val_class_unsigned_const)
24353               break;
24354             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24355             gcc_assert (base1->die_tag == DW_TAG_base_type
24356                         && base2->die_tag == DW_TAG_base_type);
24357             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24358             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24359             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24360             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24361             if (size1 == size2
24362                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24363                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24364                      && loc != keep)
24365                     || enc1 == enc2))
24366               {
24367                 /* Optimize away next DW_OP_GNU_convert after
24368                    adjusting LOC's base type die reference.  */
24369                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24370                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24371                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24372                 else
24373                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24374                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24375                 continue;
24376               }
24377             /* Don't change integer DW_OP_GNU_convert after e.g. floating
24378                point typed stack entry.  */
24379             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24380               keep = loc->dw_loc_next;
24381             break;
24382           }
24383         break;
24384       default:
24385         break;
24386       }
24387   return true;
24388 }
24389
24390 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24391    an address in .rodata section if the string literal is emitted there,
24392    or remove the containing location list or replace DW_AT_const_value
24393    with DW_AT_location and empty location expression, if it isn't found
24394    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
24395    to something that has been emitted in the current CU.  */
24396
24397 static void
24398 resolve_addr (dw_die_ref die)
24399 {
24400   dw_die_ref c;
24401   dw_attr_ref a;
24402   dw_loc_list_ref *curr, *start, loc;
24403   unsigned ix;
24404
24405   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
24406     switch (AT_class (a))
24407       {
24408       case dw_val_class_loc_list:
24409         start = curr = AT_loc_list_ptr (a);
24410         loc = *curr;
24411         gcc_assert (loc);
24412         /* The same list can be referenced more than once.  See if we have
24413            already recorded the result from a previous pass.  */
24414         if (loc->replaced)
24415           *curr = loc->dw_loc_next;
24416         else if (!loc->resolved_addr)
24417           {
24418             /* As things stand, we do not expect or allow one die to
24419                reference a suffix of another die's location list chain.
24420                References must be identical or completely separate.
24421                There is therefore no need to cache the result of this
24422                pass on any list other than the first; doing so
24423                would lead to unnecessary writes.  */
24424             while (*curr)
24425               {
24426                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24427                 if (!resolve_addr_in_expr ((*curr)->expr))
24428                   {
24429                     dw_loc_list_ref next = (*curr)->dw_loc_next;
24430                     if (next && (*curr)->ll_symbol)
24431                       {
24432                         gcc_assert (!next->ll_symbol);
24433                         next->ll_symbol = (*curr)->ll_symbol;
24434                       }
24435                     *curr = next;
24436                   }
24437                 else
24438                   {
24439                     mark_base_types ((*curr)->expr);
24440                     curr = &(*curr)->dw_loc_next;
24441                   }
24442               }
24443             if (loc == *start)
24444               loc->resolved_addr = 1;
24445             else
24446               {
24447                 loc->replaced = 1;
24448                 loc->dw_loc_next = *start;
24449               }
24450           }
24451         if (!*start)
24452           {
24453             remove_AT (die, a->dw_attr);
24454             ix--;
24455           }
24456         break;
24457       case dw_val_class_loc:
24458         if (!resolve_addr_in_expr (AT_loc (a)))
24459           {
24460             remove_AT (die, a->dw_attr);
24461             ix--;
24462           }
24463         else
24464           mark_base_types (AT_loc (a));
24465         break;
24466       case dw_val_class_addr:
24467         if (a->dw_attr == DW_AT_const_value
24468             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
24469           {
24470             remove_AT (die, a->dw_attr);
24471             ix--;
24472           }
24473         if (die->die_tag == DW_TAG_GNU_call_site
24474             && a->dw_attr == DW_AT_abstract_origin)
24475           {
24476             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24477             dw_die_ref tdie = lookup_decl_die (tdecl);
24478             if (tdie == NULL
24479                 && DECL_EXTERNAL (tdecl)
24480                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
24481               {
24482                 force_decl_die (tdecl);
24483                 tdie = lookup_decl_die (tdecl);
24484               }
24485             if (tdie)
24486               {
24487                 a->dw_attr_val.val_class = dw_val_class_die_ref;
24488                 a->dw_attr_val.v.val_die_ref.die = tdie;
24489                 a->dw_attr_val.v.val_die_ref.external = 0;
24490               }
24491             else
24492               {
24493                 remove_AT (die, a->dw_attr);
24494                 ix--;
24495               }
24496           }
24497         break;
24498       default:
24499         break;
24500       }
24501
24502   FOR_EACH_CHILD (die, c, resolve_addr (c));
24503 }
24504 \f
24505 /* Helper routines for optimize_location_lists.
24506    This pass tries to share identical local lists in .debug_loc
24507    section.  */
24508
24509 /* Iteratively hash operands of LOC opcode.  */
24510
24511 static inline hashval_t
24512 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
24513 {
24514   dw_val_ref val1 = &loc->dw_loc_oprnd1;
24515   dw_val_ref val2 = &loc->dw_loc_oprnd2;
24516
24517   switch (loc->dw_loc_opc)
24518     {
24519     case DW_OP_const4u:
24520     case DW_OP_const8u:
24521       if (loc->dtprel)
24522         goto hash_addr;
24523       /* FALLTHRU */
24524     case DW_OP_const1u:
24525     case DW_OP_const1s:
24526     case DW_OP_const2u:
24527     case DW_OP_const2s:
24528     case DW_OP_const4s:
24529     case DW_OP_const8s:
24530     case DW_OP_constu:
24531     case DW_OP_consts:
24532     case DW_OP_pick:
24533     case DW_OP_plus_uconst:
24534     case DW_OP_breg0:
24535     case DW_OP_breg1:
24536     case DW_OP_breg2:
24537     case DW_OP_breg3:
24538     case DW_OP_breg4:
24539     case DW_OP_breg5:
24540     case DW_OP_breg6:
24541     case DW_OP_breg7:
24542     case DW_OP_breg8:
24543     case DW_OP_breg9:
24544     case DW_OP_breg10:
24545     case DW_OP_breg11:
24546     case DW_OP_breg12:
24547     case DW_OP_breg13:
24548     case DW_OP_breg14:
24549     case DW_OP_breg15:
24550     case DW_OP_breg16:
24551     case DW_OP_breg17:
24552     case DW_OP_breg18:
24553     case DW_OP_breg19:
24554     case DW_OP_breg20:
24555     case DW_OP_breg21:
24556     case DW_OP_breg22:
24557     case DW_OP_breg23:
24558     case DW_OP_breg24:
24559     case DW_OP_breg25:
24560     case DW_OP_breg26:
24561     case DW_OP_breg27:
24562     case DW_OP_breg28:
24563     case DW_OP_breg29:
24564     case DW_OP_breg30:
24565     case DW_OP_breg31:
24566     case DW_OP_regx:
24567     case DW_OP_fbreg:
24568     case DW_OP_piece:
24569     case DW_OP_deref_size:
24570     case DW_OP_xderef_size:
24571       hash = iterative_hash_object (val1->v.val_int, hash);
24572       break;
24573     case DW_OP_skip:
24574     case DW_OP_bra:
24575       {
24576         int offset;
24577
24578         gcc_assert (val1->val_class == dw_val_class_loc);
24579         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24580         hash = iterative_hash_object (offset, hash);
24581       }
24582       break;
24583     case DW_OP_implicit_value:
24584       hash = iterative_hash_object (val1->v.val_unsigned, hash);
24585       switch (val2->val_class)
24586         {
24587         case dw_val_class_const:
24588           hash = iterative_hash_object (val2->v.val_int, hash);
24589           break;
24590         case dw_val_class_vec:
24591           {
24592             unsigned int elt_size = val2->v.val_vec.elt_size;
24593             unsigned int len = val2->v.val_vec.length;
24594
24595             hash = iterative_hash_object (elt_size, hash);
24596             hash = iterative_hash_object (len, hash);
24597             hash = iterative_hash (val2->v.val_vec.array,
24598                                    len * elt_size, hash);
24599           }
24600           break;
24601         case dw_val_class_const_double:
24602           hash = iterative_hash_object (val2->v.val_double.low, hash);
24603           hash = iterative_hash_object (val2->v.val_double.high, hash);
24604           break;
24605         case dw_val_class_addr:
24606           hash = iterative_hash_rtx (val2->v.val_addr, hash);
24607           break;
24608         default:
24609           gcc_unreachable ();
24610         }
24611       break;
24612     case DW_OP_bregx:
24613     case DW_OP_bit_piece:
24614       hash = iterative_hash_object (val1->v.val_int, hash);
24615       hash = iterative_hash_object (val2->v.val_int, hash);
24616       break;
24617     case DW_OP_addr:
24618     hash_addr:
24619       if (loc->dtprel)
24620         {
24621           unsigned char dtprel = 0xd1;
24622           hash = iterative_hash_object (dtprel, hash);
24623         }
24624       hash = iterative_hash_rtx (val1->v.val_addr, hash);
24625       break;
24626     case DW_OP_GNU_implicit_pointer:
24627       hash = iterative_hash_object (val2->v.val_int, hash);
24628       break;
24629     case DW_OP_GNU_entry_value:
24630       hash = hash_loc_operands (val1->v.val_loc, hash);
24631       break;
24632     case DW_OP_GNU_regval_type:
24633     case DW_OP_GNU_deref_type:
24634       {
24635         unsigned int byte_size
24636           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24637         unsigned int encoding
24638           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24639         hash = iterative_hash_object (val1->v.val_int, hash);
24640         hash = iterative_hash_object (byte_size, hash);
24641         hash = iterative_hash_object (encoding, hash);
24642       }
24643       break;
24644     case DW_OP_GNU_convert:
24645     case DW_OP_GNU_reinterpret:
24646       if (val1->val_class == dw_val_class_unsigned_const)
24647         {
24648           hash = iterative_hash_object (val1->v.val_unsigned, hash);
24649           break;
24650         }
24651       /* FALLTHRU */
24652     case DW_OP_GNU_const_type:
24653       {
24654         unsigned int byte_size
24655           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24656         unsigned int encoding
24657           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24658         hash = iterative_hash_object (byte_size, hash);
24659         hash = iterative_hash_object (encoding, hash);
24660         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24661           break;
24662         hash = iterative_hash_object (val2->val_class, hash);
24663         switch (val2->val_class)
24664           {
24665           case dw_val_class_const:
24666             hash = iterative_hash_object (val2->v.val_int, hash);
24667             break;
24668           case dw_val_class_vec:
24669             {
24670               unsigned int elt_size = val2->v.val_vec.elt_size;
24671               unsigned int len = val2->v.val_vec.length;
24672
24673               hash = iterative_hash_object (elt_size, hash);
24674               hash = iterative_hash_object (len, hash);
24675               hash = iterative_hash (val2->v.val_vec.array,
24676                                      len * elt_size, hash);
24677             }
24678             break;
24679           case dw_val_class_const_double:
24680             hash = iterative_hash_object (val2->v.val_double.low, hash);
24681             hash = iterative_hash_object (val2->v.val_double.high, hash);
24682             break;
24683           default:
24684             gcc_unreachable ();
24685           }
24686       }
24687       break;
24688
24689     default:
24690       /* Other codes have no operands.  */
24691       break;
24692     }
24693   return hash;
24694 }
24695
24696 /* Iteratively hash the whole DWARF location expression LOC.  */
24697
24698 static inline hashval_t
24699 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
24700 {
24701   dw_loc_descr_ref l;
24702   bool sizes_computed = false;
24703   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24704   size_of_locs (loc);
24705
24706   for (l = loc; l != NULL; l = l->dw_loc_next)
24707     {
24708       enum dwarf_location_atom opc = l->dw_loc_opc;
24709       hash = iterative_hash_object (opc, hash);
24710       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24711         {
24712           size_of_locs (loc);
24713           sizes_computed = true;
24714         }
24715       hash = hash_loc_operands (l, hash);
24716     }
24717   return hash;
24718 }
24719
24720 /* Compute hash of the whole location list LIST_HEAD.  */
24721
24722 static inline void
24723 hash_loc_list (dw_loc_list_ref list_head)
24724 {
24725   dw_loc_list_ref curr = list_head;
24726   hashval_t hash = 0;
24727
24728   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24729     {
24730       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
24731       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
24732       if (curr->section)
24733         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
24734                                hash);
24735       hash = hash_locs (curr->expr, hash);
24736     }
24737   list_head->hash = hash;
24738 }
24739
24740 /* Return true if X and Y opcodes have the same operands.  */
24741
24742 static inline bool
24743 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24744 {
24745   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24746   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24747   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24748   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24749
24750   switch (x->dw_loc_opc)
24751     {
24752     case DW_OP_const4u:
24753     case DW_OP_const8u:
24754       if (x->dtprel)
24755         goto hash_addr;
24756       /* FALLTHRU */
24757     case DW_OP_const1u:
24758     case DW_OP_const1s:
24759     case DW_OP_const2u:
24760     case DW_OP_const2s:
24761     case DW_OP_const4s:
24762     case DW_OP_const8s:
24763     case DW_OP_constu:
24764     case DW_OP_consts:
24765     case DW_OP_pick:
24766     case DW_OP_plus_uconst:
24767     case DW_OP_breg0:
24768     case DW_OP_breg1:
24769     case DW_OP_breg2:
24770     case DW_OP_breg3:
24771     case DW_OP_breg4:
24772     case DW_OP_breg5:
24773     case DW_OP_breg6:
24774     case DW_OP_breg7:
24775     case DW_OP_breg8:
24776     case DW_OP_breg9:
24777     case DW_OP_breg10:
24778     case DW_OP_breg11:
24779     case DW_OP_breg12:
24780     case DW_OP_breg13:
24781     case DW_OP_breg14:
24782     case DW_OP_breg15:
24783     case DW_OP_breg16:
24784     case DW_OP_breg17:
24785     case DW_OP_breg18:
24786     case DW_OP_breg19:
24787     case DW_OP_breg20:
24788     case DW_OP_breg21:
24789     case DW_OP_breg22:
24790     case DW_OP_breg23:
24791     case DW_OP_breg24:
24792     case DW_OP_breg25:
24793     case DW_OP_breg26:
24794     case DW_OP_breg27:
24795     case DW_OP_breg28:
24796     case DW_OP_breg29:
24797     case DW_OP_breg30:
24798     case DW_OP_breg31:
24799     case DW_OP_regx:
24800     case DW_OP_fbreg:
24801     case DW_OP_piece:
24802     case DW_OP_deref_size:
24803     case DW_OP_xderef_size:
24804       return valx1->v.val_int == valy1->v.val_int;
24805     case DW_OP_skip:
24806     case DW_OP_bra:
24807       gcc_assert (valx1->val_class == dw_val_class_loc
24808                   && valy1->val_class == dw_val_class_loc
24809                   && x->dw_loc_addr == y->dw_loc_addr);
24810       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24811     case DW_OP_implicit_value:
24812       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24813           || valx2->val_class != valy2->val_class)
24814         return false;
24815       switch (valx2->val_class)
24816         {
24817         case dw_val_class_const:
24818           return valx2->v.val_int == valy2->v.val_int;
24819         case dw_val_class_vec:
24820           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24821                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24822                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24823                             valx2->v.val_vec.elt_size
24824                             * valx2->v.val_vec.length) == 0;
24825         case dw_val_class_const_double:
24826           return valx2->v.val_double.low == valy2->v.val_double.low
24827                  && valx2->v.val_double.high == valy2->v.val_double.high;
24828         case dw_val_class_addr:
24829           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24830         default:
24831           gcc_unreachable ();
24832         }
24833     case DW_OP_bregx:
24834     case DW_OP_bit_piece:
24835       return valx1->v.val_int == valy1->v.val_int
24836              && valx2->v.val_int == valy2->v.val_int;
24837     case DW_OP_addr:
24838     hash_addr:
24839       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24840     case DW_OP_GNU_implicit_pointer:
24841       return valx1->val_class == dw_val_class_die_ref
24842              && valx1->val_class == valy1->val_class
24843              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24844              && valx2->v.val_int == valy2->v.val_int;
24845     case DW_OP_GNU_entry_value:
24846       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24847     case DW_OP_GNU_const_type:
24848       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24849           || valx2->val_class != valy2->val_class)
24850         return false;
24851       switch (valx2->val_class)
24852         {
24853         case dw_val_class_const:
24854           return valx2->v.val_int == valy2->v.val_int;
24855         case dw_val_class_vec:
24856           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24857                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24858                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24859                             valx2->v.val_vec.elt_size
24860                             * valx2->v.val_vec.length) == 0;
24861         case dw_val_class_const_double:
24862           return valx2->v.val_double.low == valy2->v.val_double.low
24863                  && valx2->v.val_double.high == valy2->v.val_double.high;
24864         default:
24865           gcc_unreachable ();
24866         }
24867     case DW_OP_GNU_regval_type:
24868     case DW_OP_GNU_deref_type:
24869       return valx1->v.val_int == valy1->v.val_int
24870              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24871     case DW_OP_GNU_convert:
24872     case DW_OP_GNU_reinterpret:
24873       if (valx1->val_class != valy1->val_class)
24874         return false;
24875       if (valx1->val_class == dw_val_class_unsigned_const)
24876         return valx1->v.val_unsigned == valy1->v.val_unsigned;
24877       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24878     case DW_OP_GNU_parameter_ref:
24879       return valx1->val_class == dw_val_class_die_ref
24880              && valx1->val_class == valy1->val_class
24881              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24882     default:
24883       /* Other codes have no operands.  */
24884       return true;
24885     }
24886 }
24887
24888 /* Return true if DWARF location expressions X and Y are the same.  */
24889
24890 static inline bool
24891 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24892 {
24893   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24894     if (x->dw_loc_opc != y->dw_loc_opc
24895         || x->dtprel != y->dtprel
24896         || !compare_loc_operands (x, y))
24897       break;
24898   return x == NULL && y == NULL;
24899 }
24900
24901 /* Return precomputed hash of location list X.  */
24902
24903 static hashval_t
24904 loc_list_hash (const void *x)
24905 {
24906   return ((const struct dw_loc_list_struct *) x)->hash;
24907 }
24908
24909 /* Return 1 if location lists X and Y are the same.  */
24910
24911 static int
24912 loc_list_eq (const void *x, const void *y)
24913 {
24914   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
24915   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
24916   if (a == b)
24917     return 1;
24918   if (a->hash != b->hash)
24919     return 0;
24920   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24921     if (strcmp (a->begin, b->begin) != 0
24922         || strcmp (a->end, b->end) != 0
24923         || (a->section == NULL) != (b->section == NULL)
24924         || (a->section && strcmp (a->section, b->section) != 0)
24925         || !compare_locs (a->expr, b->expr))
24926       break;
24927   return a == NULL && b == NULL;
24928 }
24929
24930 /* Recursively optimize location lists referenced from DIE
24931    children and share them whenever possible.  */
24932
24933 static void
24934 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
24935 {
24936   dw_die_ref c;
24937   dw_attr_ref a;
24938   unsigned ix;
24939   void **slot;
24940
24941   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
24942     if (AT_class (a) == dw_val_class_loc_list)
24943       {
24944         dw_loc_list_ref list = AT_loc_list (a);
24945         /* TODO: perform some optimizations here, before hashing
24946            it and storing into the hash table.  */
24947         hash_loc_list (list);
24948         slot = htab_find_slot_with_hash (htab, list, list->hash,
24949                                          INSERT);
24950         if (*slot == NULL)
24951           *slot = (void *) list;
24952         else
24953           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
24954       }
24955
24956   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24957 }
24958
24959 /* Optimize location lists referenced from DIE
24960    children and share them whenever possible.  */
24961
24962 static void
24963 optimize_location_lists (dw_die_ref die)
24964 {
24965   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
24966   optimize_location_lists_1 (die, htab);
24967   htab_delete (htab);
24968 }
24969 \f
24970 /* Output stuff that dwarf requires at the end of every file,
24971    and generate the DWARF-2 debugging info.  */
24972
24973 static void
24974 dwarf2out_finish (const char *filename)
24975 {
24976   limbo_die_node *node, *next_node;
24977   comdat_type_node *ctnode;
24978   htab_t comdat_type_table;
24979   unsigned int i;
24980
24981   gen_scheduled_generic_parms_dies ();
24982   gen_remaining_tmpl_value_param_die_attribute ();
24983
24984   /* Add the name for the main input file now.  We delayed this from
24985      dwarf2out_init to avoid complications with PCH.  */
24986   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24987   if (!IS_ABSOLUTE_PATH (filename))
24988     add_comp_dir_attribute (comp_unit_die ());
24989   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24990     {
24991       bool p = false;
24992       htab_traverse (file_table, file_table_relative_p, &p);
24993       if (p)
24994         add_comp_dir_attribute (comp_unit_die ());
24995     }
24996
24997   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
24998     {
24999       add_location_or_const_value_attribute (
25000         VEC_index (deferred_locations, deferred_locations_list, i)->die,
25001         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
25002         false,
25003         DW_AT_location);
25004     }
25005
25006   /* Traverse the limbo die list, and add parent/child links.  The only
25007      dies without parents that should be here are concrete instances of
25008      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
25009      For concrete instances, we can get the parent die from the abstract
25010      instance.  */
25011   for (node = limbo_die_list; node; node = next_node)
25012     {
25013       dw_die_ref die = node->die;
25014       next_node = node->next;
25015
25016       if (die->die_parent == NULL)
25017         {
25018           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25019
25020           if (origin)
25021             add_child_die (origin->die_parent, die);
25022           else if (is_cu_die (die))
25023             ;
25024           else if (seen_error ())
25025             /* It's OK to be confused by errors in the input.  */
25026             add_child_die (comp_unit_die (), die);
25027           else
25028             {
25029               /* In certain situations, the lexical block containing a
25030                  nested function can be optimized away, which results
25031                  in the nested function die being orphaned.  Likewise
25032                  with the return type of that nested function.  Force
25033                  this to be a child of the containing function.
25034
25035                  It may happen that even the containing function got fully
25036                  inlined and optimized out.  In that case we are lost and
25037                  assign the empty child.  This should not be big issue as
25038                  the function is likely unreachable too.  */
25039               tree context = NULL_TREE;
25040
25041               gcc_assert (node->created_for);
25042
25043               if (DECL_P (node->created_for))
25044                 context = DECL_CONTEXT (node->created_for);
25045               else if (TYPE_P (node->created_for))
25046                 context = TYPE_CONTEXT (node->created_for);
25047
25048               gcc_assert (context
25049                           && (TREE_CODE (context) == FUNCTION_DECL
25050                               || TREE_CODE (context) == NAMESPACE_DECL));
25051
25052               origin = lookup_decl_die (context);
25053               if (origin)
25054                 add_child_die (origin, die);
25055               else
25056                 add_child_die (comp_unit_die (), die);
25057             }
25058         }
25059     }
25060
25061   limbo_die_list = NULL;
25062
25063 #if ENABLE_ASSERT_CHECKING
25064   {
25065     dw_die_ref die = comp_unit_die (), c;
25066     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25067   }
25068 #endif
25069   resolve_addr (comp_unit_die ());
25070   move_marked_base_types ();
25071
25072   for (node = deferred_asm_name; node; node = node->next)
25073     {
25074       tree decl = node->created_for;
25075       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
25076         {
25077           add_linkage_attr (node->die, decl);
25078           move_linkage_attr (node->die);
25079         }
25080     }
25081
25082   deferred_asm_name = NULL;
25083
25084   /* Walk through the list of incomplete types again, trying once more to
25085      emit full debugging info for them.  */
25086   retry_incomplete_types ();
25087
25088   if (flag_eliminate_unused_debug_types)
25089     prune_unused_types ();
25090
25091   /* Generate separate CUs for each of the include files we've seen.
25092      They will go into limbo_die_list.  */
25093   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
25094     break_out_includes (comp_unit_die ());
25095
25096   /* Generate separate COMDAT sections for type DIEs. */
25097   if (use_debug_types)
25098     {
25099       break_out_comdat_types (comp_unit_die ());
25100
25101       /* Each new type_unit DIE was added to the limbo die list when created.
25102          Since these have all been added to comdat_type_list, clear the
25103          limbo die list.  */
25104       limbo_die_list = NULL;
25105
25106       /* For each new comdat type unit, copy declarations for incomplete
25107          types to make the new unit self-contained (i.e., no direct
25108          references to the main compile unit).  */
25109       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25110         copy_decls_for_unworthy_types (ctnode->root_die);
25111       copy_decls_for_unworthy_types (comp_unit_die ());
25112
25113       /* In the process of copying declarations from one unit to another,
25114          we may have left some declarations behind that are no longer
25115          referenced.  Prune them.  */
25116       prune_unused_types ();
25117     }
25118
25119   /* Traverse the DIE's and add add sibling attributes to those DIE's
25120      that have children.  */
25121   add_sibling_attributes (comp_unit_die ());
25122   for (node = limbo_die_list; node; node = node->next)
25123     add_sibling_attributes (node->die);
25124   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25125     add_sibling_attributes (ctnode->root_die);
25126
25127   /* Output a terminator label for the .text section.  */
25128   switch_to_section (text_section);
25129   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25130   if (cold_text_section)
25131     {
25132       switch_to_section (cold_text_section);
25133       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25134     }
25135
25136   /* We can only use the low/high_pc attributes if all of the code was
25137      in .text.  */
25138   if (!have_multiple_function_sections 
25139       || (dwarf_version < 3 && dwarf_strict))
25140     {
25141       /* Don't add if the CU has no associated code.  */
25142       if (text_section_used)
25143         {
25144           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
25145           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
25146         }
25147     }
25148   else
25149     {
25150       unsigned fde_idx = 0;
25151       bool range_list_added = false;
25152
25153       if (text_section_used)
25154         add_ranges_by_labels (comp_unit_die (), text_section_label,
25155                               text_end_label, &range_list_added);
25156       if (cold_text_section_used)
25157         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
25158                               cold_end_label, &range_list_added);
25159
25160       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
25161         {
25162           dw_fde_ref fde = &fde_table[fde_idx];
25163
25164           if (!fde->in_std_section)
25165             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
25166                                   fde->dw_fde_end, &range_list_added);
25167           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25168             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
25169                                   fde->dw_fde_second_end, &range_list_added);
25170         }
25171
25172       if (range_list_added)
25173         {
25174           /* We need to give .debug_loc and .debug_ranges an appropriate
25175              "base address".  Use zero so that these addresses become
25176              absolute.  Historically, we've emitted the unexpected
25177              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25178              Emit both to give time for other tools to adapt.  */
25179           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
25180           if (! dwarf_strict && dwarf_version < 4)
25181             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
25182
25183           add_ranges (NULL);
25184         }
25185     }
25186
25187   if (debug_info_level >= DINFO_LEVEL_NORMAL)
25188     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
25189                     debug_line_section_label);
25190
25191   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25192     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
25193
25194   if (have_location_lists)
25195     optimize_location_lists (comp_unit_die ());
25196
25197   /* Output all of the compilation units.  We put the main one last so that
25198      the offsets are available to output_pubnames.  */
25199   for (node = limbo_die_list; node; node = node->next)
25200     output_comp_unit (node->die, 0);
25201
25202   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
25203   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25204     {
25205       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
25206
25207       /* Don't output duplicate types.  */
25208       if (*slot != HTAB_EMPTY_ENTRY)
25209         continue;
25210
25211       /* Add a pointer to the line table for the main compilation unit
25212          so that the debugger can make sense of DW_AT_decl_file
25213          attributes.  */
25214       if (debug_info_level >= DINFO_LEVEL_NORMAL)
25215         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25216                         debug_line_section_label);
25217
25218       output_comdat_type_unit (ctnode);
25219       *slot = ctnode;
25220     }
25221   htab_delete (comdat_type_table);
25222
25223   /* Output the main compilation unit if non-empty or if .debug_macinfo
25224      will be emitted.  */
25225   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
25226
25227   /* Output the abbreviation table.  */
25228   switch_to_section (debug_abbrev_section);
25229   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25230   output_abbrev_section ();
25231
25232   /* Output location list section if necessary.  */
25233   if (have_location_lists)
25234     {
25235       /* Output the location lists info.  */
25236       switch_to_section (debug_loc_section);
25237       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
25238                                    DEBUG_LOC_SECTION_LABEL, 0);
25239       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25240       output_location_lists (comp_unit_die ());
25241     }
25242
25243   /* Output public names table if necessary.  */
25244   if (!VEC_empty (pubname_entry, pubname_table))
25245     {
25246       gcc_assert (info_section_emitted);
25247       switch_to_section (debug_pubnames_section);
25248       output_pubnames (pubname_table);
25249     }
25250
25251   /* Output public types table if necessary.  */
25252   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
25253      It shouldn't hurt to emit it always, since pure DWARF2 consumers
25254      simply won't look for the section.  */
25255   if (!VEC_empty (pubname_entry, pubtype_table))
25256     {
25257       bool empty = false;
25258       
25259       if (flag_eliminate_unused_debug_types)
25260         {
25261           /* The pubtypes table might be emptied by pruning unused items.  */
25262           unsigned i;
25263           pubname_ref p;
25264           empty = true;
25265           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
25266             if (p->die->die_offset != 0)
25267               {
25268                 empty = false;
25269                 break;
25270               }
25271         }
25272       if (!empty)
25273         {
25274           gcc_assert (info_section_emitted);
25275           switch_to_section (debug_pubtypes_section);
25276           output_pubnames (pubtype_table);
25277         }
25278     }
25279
25280   /* Output the address range information if a CU (.debug_info section)
25281      was emitted.  We output an empty table even if we had no functions
25282      to put in it.  This because the consumer has no way to tell the
25283      difference between an empty table that we omitted and failure to
25284      generate a table that would have contained data.  */
25285   if (info_section_emitted)
25286     {
25287       unsigned long aranges_length = size_of_aranges ();
25288
25289       switch_to_section (debug_aranges_section);
25290       output_aranges (aranges_length);
25291     }
25292
25293   /* Output ranges section if necessary.  */
25294   if (ranges_table_in_use)
25295     {
25296       switch_to_section (debug_ranges_section);
25297       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25298       output_ranges ();
25299     }
25300
25301   /* Output the source line correspondence table.  We must do this
25302      even if there is no line information.  Otherwise, on an empty
25303      translation unit, we will generate a present, but empty,
25304      .debug_info section.  IRIX 6.5 `nm' will then complain when
25305      examining the file.  This is done late so that any filenames
25306      used by the debug_info section are marked as 'used'.  */
25307   switch_to_section (debug_line_section);
25308   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25309   if (! DWARF2_ASM_LINE_DEBUG_INFO)
25310     output_line_info ();
25311
25312   /* Have to end the macro section.  */
25313   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25314     {
25315       switch_to_section (debug_macinfo_section);
25316       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25317       if (!VEC_empty (macinfo_entry, macinfo_table))
25318         output_macinfo ();
25319       dw2_asm_output_data (1, 0, "End compilation unit");
25320     }
25321
25322   /* If we emitted any DW_FORM_strp form attribute, output the string
25323      table too.  */
25324   if (debug_str_hash)
25325     htab_traverse (debug_str_hash, output_indirect_string, NULL);
25326 }
25327
25328 #include "gt-dwarf2out.h"