re PR target/52933 (SH Target: Use div0s for integer sign comparisons)
[platform/upstream/gcc.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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 "except.h"
76 #include "dwarf2.h"
77 #include "dwarf2out.h"
78 #include "dwarf2asm.h"
79 #include "toplev.h"
80 #include "ggc.h"
81 #include "md5.h"
82 #include "tm_p.h"
83 #include "diagnostic.h"
84 #include "tree-pretty-print.h"
85 #include "debug.h"
86 #include "target.h"
87 #include "common/common-target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "dumpfile.h"
94 #include "opts.h"
95
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98 static rtx cached_next_real_insn;
99
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102
103 /* Define this macro to be a nonzero value if the directory specifications
104     which are output in the debug info should end with a separator.  */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107    from generating indirect strings in DWARF2 debug information, for instance
108    if your target is stuck with an old version of GDB that is unable to
109    process them properly or uses VMS Debug.  */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
115
116 /* ??? Poison these here until it can be done generically.  They've been
117    totally replaced in this file; make sure it stays that way.  */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
123
124 /* The size of the target's pointer type.  */
125 #ifndef PTR_SIZE
126 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
127 #endif
128
129 /* Array of RTXes referenced by the debugging information, which therefore
130    must be kept around forever.  */
131 static GTY(()) VEC(rtx,gc) *used_rtx_array;
132
133 /* A pointer to the base of a list of incomplete types which might be
134    completed at some later time.  incomplete_types_list needs to be a
135    VEC(tree,gc) because we want to tell the garbage collector about
136    it.  */
137 static GTY(()) VEC(tree,gc) *incomplete_types;
138
139 /* A pointer to the base of a table of references to declaration
140    scopes.  This table is a display which tracks the nesting
141    of declaration scopes at the current scope and containing
142    scopes.  This table is used to find the proper place to
143    define type declaration DIE's.  */
144 static GTY(()) VEC(tree,gc) *decl_scope_table;
145
146 /* Pointers to various DWARF2 sections.  */
147 static GTY(()) section *debug_info_section;
148 static GTY(()) section *debug_abbrev_section;
149 static GTY(()) section *debug_aranges_section;
150 static GTY(()) section *debug_macinfo_section;
151 static GTY(()) section *debug_line_section;
152 static GTY(()) section *debug_loc_section;
153 static GTY(()) section *debug_pubnames_section;
154 static GTY(()) section *debug_pubtypes_section;
155 static GTY(()) section *debug_str_section;
156 static GTY(()) section *debug_ranges_section;
157 static GTY(()) section *debug_frame_section;
158
159 /* Maximum size (in bytes) of an artificially generated label.  */
160 #define MAX_ARTIFICIAL_LABEL_BYTES      30
161
162 /* According to the (draft) DWARF 3 specification, the initial length
163    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
164    bytes are 0xffffffff, followed by the length stored in the next 8
165    bytes.
166
167    However, the SGI/MIPS ABI uses an initial length which is equal to
168    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
169
170 #ifndef DWARF_INITIAL_LENGTH_SIZE
171 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
172 #endif
173
174 /* Round SIZE up to the nearest BOUNDARY.  */
175 #define DWARF_ROUND(SIZE,BOUNDARY) \
176   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
177
178 /* CIE identifier.  */
179 #if HOST_BITS_PER_WIDE_INT >= 64
180 #define DWARF_CIE_ID \
181   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
182 #else
183 #define DWARF_CIE_ID DW_CIE_ID
184 #endif
185
186 DEF_VEC_P (dw_fde_ref);
187 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
188
189 /* A vector for a table that contains frame description
190    information for each routine.  */
191 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
192
193 struct GTY(()) indirect_string_node {
194   const char *str;
195   unsigned int refcount;
196   enum dwarf_form form;
197   char *label;
198 };
199
200 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
201
202 static GTY(()) int dw2_string_counter;
203
204 /* True if the compilation unit places functions in more than one section.  */
205 static GTY(()) bool have_multiple_function_sections = false;
206
207 /* Whether the default text and cold text sections have been used at all.  */
208
209 static GTY(()) bool text_section_used = false;
210 static GTY(()) bool cold_text_section_used = false;
211
212 /* The default cold text section.  */
213 static GTY(()) section *cold_text_section;
214
215 /* Forward declarations for functions defined in this file.  */
216
217 static char *stripattributes (const char *);
218 static void output_call_frame_info (int);
219 static void dwarf2out_note_section_used (void);
220
221 /* Personality decl of current unit.  Used only when assembler does not support
222    personality CFI.  */
223 static GTY(()) rtx current_unit_personality;
224
225 /* Data and reference forms for relocatable data.  */
226 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
227 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
228
229 #ifndef DEBUG_FRAME_SECTION
230 #define DEBUG_FRAME_SECTION     ".debug_frame"
231 #endif
232
233 #ifndef FUNC_BEGIN_LABEL
234 #define FUNC_BEGIN_LABEL        "LFB"
235 #endif
236
237 #ifndef FUNC_END_LABEL
238 #define FUNC_END_LABEL          "LFE"
239 #endif
240
241 #ifndef PROLOGUE_END_LABEL
242 #define PROLOGUE_END_LABEL      "LPE"
243 #endif
244
245 #ifndef EPILOGUE_BEGIN_LABEL
246 #define EPILOGUE_BEGIN_LABEL    "LEB"
247 #endif
248
249 #ifndef FRAME_BEGIN_LABEL
250 #define FRAME_BEGIN_LABEL       "Lframe"
251 #endif
252 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
253 #define CIE_END_LABEL           "LECIE"
254 #define FDE_LABEL               "LSFDE"
255 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
256 #define FDE_END_LABEL           "LEFDE"
257 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
258 #define LINE_NUMBER_END_LABEL   "LELT"
259 #define LN_PROLOG_AS_LABEL      "LASLTP"
260 #define LN_PROLOG_END_LABEL     "LELTP"
261 #define DIE_LABEL_PREFIX        "DW"
262 \f
263 /* Match the base name of a file to the base name of a compilation unit. */
264
265 static int
266 matches_main_base (const char *path)
267 {
268   /* Cache the last query. */
269   static const char *last_path = NULL;
270   static int last_match = 0;
271   if (path != last_path)
272     {
273       const char *base;
274       int length = base_of_path (path, &base);
275       last_path = path;
276       last_match = (length == main_input_baselength
277                     && memcmp (base, main_input_basename, length) == 0);
278     }
279   return last_match;
280 }
281
282 #ifdef DEBUG_DEBUG_STRUCT
283
284 static int
285 dump_struct_debug (tree type, enum debug_info_usage usage,
286                    enum debug_struct_file criterion, int generic,
287                    int matches, int result)
288 {
289   /* Find the type name. */
290   tree type_decl = TYPE_STUB_DECL (type);
291   tree t = type_decl;
292   const char *name = 0;
293   if (TREE_CODE (t) == TYPE_DECL)
294     t = DECL_NAME (t);
295   if (t)
296     name = IDENTIFIER_POINTER (t);
297
298   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
299            criterion,
300            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
301            matches ? "bas" : "hdr",
302            generic ? "gen" : "ord",
303            usage == DINFO_USAGE_DFN ? ";" :
304              usage == DINFO_USAGE_DIR_USE ? "." : "*",
305            result,
306            (void*) type_decl, name);
307   return result;
308 }
309 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
310   dump_struct_debug (type, usage, criterion, generic, matches, result)
311
312 #else
313
314 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
315   (result)
316
317 #endif
318
319 static bool
320 should_emit_struct_debug (tree type, enum debug_info_usage usage)
321 {
322   enum debug_struct_file criterion;
323   tree type_decl;
324   bool generic = lang_hooks.types.generic_p (type);
325
326   if (generic)
327     criterion = debug_struct_generic[usage];
328   else
329     criterion = debug_struct_ordinary[usage];
330
331   if (criterion == DINFO_STRUCT_FILE_NONE)
332     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
333   if (criterion == DINFO_STRUCT_FILE_ANY)
334     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
335
336   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
337
338   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
339     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
340
341   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
342     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
343   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
344 }
345 \f
346 /* Return a pointer to a copy of the section string name S with all
347    attributes stripped off, and an asterisk prepended (for assemble_name).  */
348
349 static inline char *
350 stripattributes (const char *s)
351 {
352   char *stripped = XNEWVEC (char, strlen (s) + 2);
353   char *p = stripped;
354
355   *p++ = '*';
356
357   while (*s && *s != ',')
358     *p++ = *s++;
359
360   *p = '\0';
361   return stripped;
362 }
363
364 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
365    switch to the data section instead, and write out a synthetic start label
366    for collect2 the first time around.  */
367
368 static void
369 switch_to_eh_frame_section (bool back)
370 {
371   tree label;
372
373 #ifdef EH_FRAME_SECTION_NAME
374   if (eh_frame_section == 0)
375     {
376       int flags;
377
378       if (EH_TABLES_CAN_BE_READ_ONLY)
379         {
380           int fde_encoding;
381           int per_encoding;
382           int lsda_encoding;
383
384           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
385                                                        /*global=*/0);
386           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
387                                                        /*global=*/1);
388           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
389                                                         /*global=*/0);
390           flags = ((! flag_pic
391                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
392                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
393                         && (per_encoding & 0x70) != DW_EH_PE_absptr
394                         && (per_encoding & 0x70) != DW_EH_PE_aligned
395                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
396                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
397                    ? 0 : SECTION_WRITE);
398         }
399       else
400         flags = SECTION_WRITE;
401       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
402     }
403 #endif /* EH_FRAME_SECTION_NAME */
404
405   if (eh_frame_section)
406     switch_to_section (eh_frame_section);
407   else
408     {
409       /* We have no special eh_frame section.  Put the information in
410          the data section and emit special labels to guide collect2.  */
411       switch_to_section (data_section);
412
413       if (!back)
414         {
415           label = get_file_function_name ("F");
416           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
417           targetm.asm_out.globalize_label (asm_out_file,
418                                            IDENTIFIER_POINTER (label));
419           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
420         }
421     }
422 }
423
424 /* Switch [BACK] to the eh or debug frame table section, depending on
425    FOR_EH.  */
426
427 static void
428 switch_to_frame_table_section (int for_eh, bool back)
429 {
430   if (for_eh)
431     switch_to_eh_frame_section (back);
432   else
433     {
434       if (!debug_frame_section)
435         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
436                                            SECTION_DEBUG, NULL);
437       switch_to_section (debug_frame_section);
438     }
439 }
440
441 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
442
443 enum dw_cfi_oprnd_type
444 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
445 {
446   switch (cfi)
447     {
448     case DW_CFA_nop:
449     case DW_CFA_GNU_window_save:
450     case DW_CFA_remember_state:
451     case DW_CFA_restore_state:
452       return dw_cfi_oprnd_unused;
453
454     case DW_CFA_set_loc:
455     case DW_CFA_advance_loc1:
456     case DW_CFA_advance_loc2:
457     case DW_CFA_advance_loc4:
458     case DW_CFA_MIPS_advance_loc8:
459       return dw_cfi_oprnd_addr;
460
461     case DW_CFA_offset:
462     case DW_CFA_offset_extended:
463     case DW_CFA_def_cfa:
464     case DW_CFA_offset_extended_sf:
465     case DW_CFA_def_cfa_sf:
466     case DW_CFA_restore:
467     case DW_CFA_restore_extended:
468     case DW_CFA_undefined:
469     case DW_CFA_same_value:
470     case DW_CFA_def_cfa_register:
471     case DW_CFA_register:
472     case DW_CFA_expression:
473       return dw_cfi_oprnd_reg_num;
474
475     case DW_CFA_def_cfa_offset:
476     case DW_CFA_GNU_args_size:
477     case DW_CFA_def_cfa_offset_sf:
478       return dw_cfi_oprnd_offset;
479
480     case DW_CFA_def_cfa_expression:
481       return dw_cfi_oprnd_loc;
482
483     default:
484       gcc_unreachable ();
485     }
486 }
487
488 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
489
490 enum dw_cfi_oprnd_type
491 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
492 {
493   switch (cfi)
494     {
495     case DW_CFA_def_cfa:
496     case DW_CFA_def_cfa_sf:
497     case DW_CFA_offset:
498     case DW_CFA_offset_extended_sf:
499     case DW_CFA_offset_extended:
500       return dw_cfi_oprnd_offset;
501
502     case DW_CFA_register:
503       return dw_cfi_oprnd_reg_num;
504
505     case DW_CFA_expression:
506       return dw_cfi_oprnd_loc;
507
508     default:
509       return dw_cfi_oprnd_unused;
510     }
511 }
512
513 /* Output one FDE.  */
514
515 static void
516 output_fde (dw_fde_ref fde, bool for_eh, bool second,
517             char *section_start_label, int fde_encoding, char *augmentation,
518             bool any_lsda_needed, int lsda_encoding)
519 {
520   const char *begin, *end;
521   static unsigned int j;
522   char l1[20], l2[20];
523
524   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
525                                      /* empty */ 0);
526   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
527                                   for_eh + j);
528   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
529   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
530   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
531     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
532                          " indicating 64-bit DWARF extension");
533   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
534                         "FDE Length");
535   ASM_OUTPUT_LABEL (asm_out_file, l1);
536
537   if (for_eh)
538     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
539   else
540     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
541                            debug_frame_section, "FDE CIE offset");
542
543   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
544   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
545
546   if (for_eh)
547     {
548       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
549       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
550       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
551                                        "FDE initial location");
552       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
553                             end, begin, "FDE address range");
554     }
555   else
556     {
557       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
558       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
559     }
560
561   if (augmentation[0])
562     {
563       if (any_lsda_needed)
564         {
565           int size = size_of_encoded_value (lsda_encoding);
566
567           if (lsda_encoding == DW_EH_PE_aligned)
568             {
569               int offset = (  4         /* Length */
570                             + 4         /* CIE offset */
571                             + 2 * size_of_encoded_value (fde_encoding)
572                             + 1         /* Augmentation size */ );
573               int pad = -offset & (PTR_SIZE - 1);
574
575               size += pad;
576               gcc_assert (size_of_uleb128 (size) == 1);
577             }
578
579           dw2_asm_output_data_uleb128 (size, "Augmentation size");
580
581           if (fde->uses_eh_lsda)
582             {
583               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
584                                            fde->funcdef_number);
585               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
586                                                gen_rtx_SYMBOL_REF (Pmode, l1),
587                                                false,
588                                                "Language Specific Data Area");
589             }
590           else
591             {
592               if (lsda_encoding == DW_EH_PE_aligned)
593                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
594               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
595                                    "Language Specific Data Area (none)");
596             }
597         }
598       else
599         dw2_asm_output_data_uleb128 (0, "Augmentation size");
600     }
601
602   /* Loop through the Call Frame Instructions associated with this FDE.  */
603   fde->dw_fde_current_label = begin;
604   {
605     size_t from, until, i;
606
607     from = 0;
608     until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
609
610     if (fde->dw_fde_second_begin == NULL)
611       ;
612     else if (!second)
613       until = fde->dw_fde_switch_cfi_index;
614     else
615       from = fde->dw_fde_switch_cfi_index;
616
617     for (i = from; i < until; i++)
618       output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i), fde, for_eh);
619   }
620
621   /* If we are to emit a ref/link from function bodies to their frame tables,
622      do it now.  This is typically performed to make sure that tables
623      associated with functions are dragged with them and not discarded in
624      garbage collecting links. We need to do this on a per function basis to
625      cope with -ffunction-sections.  */
626
627 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
628   /* Switch to the function section, emit the ref to the tables, and
629      switch *back* into the table section.  */
630   switch_to_section (function_section (fde->decl));
631   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
632   switch_to_frame_table_section (for_eh, true);
633 #endif
634
635   /* Pad the FDE out to an address sized boundary.  */
636   ASM_OUTPUT_ALIGN (asm_out_file,
637                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
638   ASM_OUTPUT_LABEL (asm_out_file, l2);
639
640   j += 2;
641 }
642
643 /* Return true if frame description entry FDE is needed for EH.  */
644
645 static bool
646 fde_needed_for_eh_p (dw_fde_ref fde)
647 {
648   if (flag_asynchronous_unwind_tables)
649     return true;
650
651   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
652     return true;
653
654   if (fde->uses_eh_lsda)
655     return true;
656
657   /* If exceptions are enabled, we have collected nothrow info.  */
658   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
659     return false;
660
661   return true;
662 }
663
664 /* Output the call frame information used to record information
665    that relates to calculating the frame pointer, and records the
666    location of saved registers.  */
667
668 static void
669 output_call_frame_info (int for_eh)
670 {
671   unsigned int i;
672   dw_fde_ref fde;
673   dw_cfi_ref cfi;
674   char l1[20], l2[20], section_start_label[20];
675   bool any_lsda_needed = false;
676   char augmentation[6];
677   int augmentation_size;
678   int fde_encoding = DW_EH_PE_absptr;
679   int per_encoding = DW_EH_PE_absptr;
680   int lsda_encoding = DW_EH_PE_absptr;
681   int return_reg;
682   rtx personality = NULL;
683   int dw_cie_version;
684
685   /* Don't emit a CIE if there won't be any FDEs.  */
686   if (fde_vec == NULL)
687     return;
688
689   /* Nothing to do if the assembler's doing it all.  */
690   if (dwarf2out_do_cfi_asm ())
691     return;
692
693   /* If we don't have any functions we'll want to unwind out of, don't emit
694      any EH unwind information.  If we make FDEs linkonce, we may have to
695      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
696      want to avoid having an FDE kept around when the function it refers to
697      is discarded.  Example where this matters: a primary function template
698      in C++ requires EH information, an explicit specialization doesn't.  */
699   if (for_eh)
700     {
701       bool any_eh_needed = false;
702
703       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
704         {
705           if (fde->uses_eh_lsda)
706             any_eh_needed = any_lsda_needed = true;
707           else if (fde_needed_for_eh_p (fde))
708             any_eh_needed = true;
709           else if (TARGET_USES_WEAK_UNWIND_INFO)
710             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
711         }
712
713       if (!any_eh_needed)
714         return;
715     }
716
717   /* We're going to be generating comments, so turn on app.  */
718   if (flag_debug_asm)
719     app_enable ();
720
721   /* Switch to the proper frame section, first time.  */
722   switch_to_frame_table_section (for_eh, false);
723
724   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
725   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
726
727   /* Output the CIE.  */
728   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
729   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
730   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
731     dw2_asm_output_data (4, 0xffffffff,
732       "Initial length escape value indicating 64-bit DWARF extension");
733   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
734                         "Length of Common Information Entry");
735   ASM_OUTPUT_LABEL (asm_out_file, l1);
736
737   /* Now that the CIE pointer is PC-relative for EH,
738      use 0 to identify the CIE.  */
739   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
740                        (for_eh ? 0 : DWARF_CIE_ID),
741                        "CIE Identifier Tag");
742
743   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
744      use CIE version 1, unless that would produce incorrect results
745      due to overflowing the return register column.  */
746   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
747   dw_cie_version = 1;
748   if (return_reg >= 256 || dwarf_version > 2)
749     dw_cie_version = 3;
750   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
751
752   augmentation[0] = 0;
753   augmentation_size = 0;
754
755   personality = current_unit_personality;
756   if (for_eh)
757     {
758       char *p;
759
760       /* Augmentation:
761          z      Indicates that a uleb128 is present to size the
762                 augmentation section.
763          L      Indicates the encoding (and thus presence) of
764                 an LSDA pointer in the FDE augmentation.
765          R      Indicates a non-default pointer encoding for
766                 FDE code pointers.
767          P      Indicates the presence of an encoding + language
768                 personality routine in the CIE augmentation.  */
769
770       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
771       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
772       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
773
774       p = augmentation + 1;
775       if (personality)
776         {
777           *p++ = 'P';
778           augmentation_size += 1 + size_of_encoded_value (per_encoding);
779           assemble_external_libcall (personality);
780         }
781       if (any_lsda_needed)
782         {
783           *p++ = 'L';
784           augmentation_size += 1;
785         }
786       if (fde_encoding != DW_EH_PE_absptr)
787         {
788           *p++ = 'R';
789           augmentation_size += 1;
790         }
791       if (p > augmentation + 1)
792         {
793           augmentation[0] = 'z';
794           *p = '\0';
795         }
796
797       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
798       if (personality && per_encoding == DW_EH_PE_aligned)
799         {
800           int offset = (  4             /* Length */
801                         + 4             /* CIE Id */
802                         + 1             /* CIE version */
803                         + strlen (augmentation) + 1     /* Augmentation */
804                         + size_of_uleb128 (1)           /* Code alignment */
805                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
806                         + 1             /* RA column */
807                         + 1             /* Augmentation size */
808                         + 1             /* Personality encoding */ );
809           int pad = -offset & (PTR_SIZE - 1);
810
811           augmentation_size += pad;
812
813           /* Augmentations should be small, so there's scarce need to
814              iterate for a solution.  Die if we exceed one uleb128 byte.  */
815           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
816         }
817     }
818
819   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
820   if (dw_cie_version >= 4)
821     {
822       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
823       dw2_asm_output_data (1, 0, "CIE Segment Size");
824     }
825   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
826   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
827                                "CIE Data Alignment Factor");
828
829   if (dw_cie_version == 1)
830     dw2_asm_output_data (1, return_reg, "CIE RA Column");
831   else
832     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
833
834   if (augmentation[0])
835     {
836       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
837       if (personality)
838         {
839           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
840                                eh_data_format_name (per_encoding));
841           dw2_asm_output_encoded_addr_rtx (per_encoding,
842                                            personality,
843                                            true, NULL);
844         }
845
846       if (any_lsda_needed)
847         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
848                              eh_data_format_name (lsda_encoding));
849
850       if (fde_encoding != DW_EH_PE_absptr)
851         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
852                              eh_data_format_name (fde_encoding));
853     }
854
855   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
856     output_cfi (cfi, NULL, for_eh);
857
858   /* Pad the CIE out to an address sized boundary.  */
859   ASM_OUTPUT_ALIGN (asm_out_file,
860                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
861   ASM_OUTPUT_LABEL (asm_out_file, l2);
862
863   /* Loop through all of the FDE's.  */
864   FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
865     {
866       unsigned int k;
867
868       /* Don't emit EH unwind info for leaf functions that don't need it.  */
869       if (for_eh && !fde_needed_for_eh_p (fde))
870         continue;
871
872       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
873         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
874                     augmentation, any_lsda_needed, lsda_encoding);
875     }
876
877   if (for_eh && targetm.terminate_dw2_eh_frame_info)
878     dw2_asm_output_data (4, 0, "End of Table");
879
880   /* Turn off app to make assembly quicker.  */
881   if (flag_debug_asm)
882     app_disable ();
883 }
884
885 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
886
887 static void
888 dwarf2out_do_cfi_startproc (bool second)
889 {
890   int enc;
891   rtx ref;
892   rtx personality = get_personality_function (current_function_decl);
893
894   fprintf (asm_out_file, "\t.cfi_startproc\n");
895
896   if (personality)
897     {
898       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
899       ref = personality;
900
901       /* ??? The GAS support isn't entirely consistent.  We have to
902          handle indirect support ourselves, but PC-relative is done
903          in the assembler.  Further, the assembler can't handle any
904          of the weirder relocation types.  */
905       if (enc & DW_EH_PE_indirect)
906         ref = dw2_force_const_mem (ref, true);
907
908       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
909       output_addr_const (asm_out_file, ref);
910       fputc ('\n', asm_out_file);
911     }
912
913   if (crtl->uses_eh_lsda)
914     {
915       char lab[20];
916
917       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
918       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
919                                    current_function_funcdef_no);
920       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
921       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
922
923       if (enc & DW_EH_PE_indirect)
924         ref = dw2_force_const_mem (ref, true);
925
926       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
927       output_addr_const (asm_out_file, ref);
928       fputc ('\n', asm_out_file);
929     }
930 }
931
932 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
933    this allocation may be done before pass_final.  */
934
935 dw_fde_ref
936 dwarf2out_alloc_current_fde (void)
937 {
938   dw_fde_ref fde;
939
940   fde = ggc_alloc_cleared_dw_fde_node ();
941   fde->decl = current_function_decl;
942   fde->funcdef_number = current_function_funcdef_no;
943   fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
944   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
945   fde->uses_eh_lsda = crtl->uses_eh_lsda;
946   fde->nothrow = crtl->nothrow;
947   fde->drap_reg = INVALID_REGNUM;
948   fde->vdrap_reg = INVALID_REGNUM;
949
950   /* Record the FDE associated with this function.  */
951   cfun->fde = fde;
952   VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
953
954   return fde;
955 }
956
957 /* Output a marker (i.e. a label) for the beginning of a function, before
958    the prologue.  */
959
960 void
961 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
962                           const char *file ATTRIBUTE_UNUSED)
963 {
964   char label[MAX_ARTIFICIAL_LABEL_BYTES];
965   char * dup_label;
966   dw_fde_ref fde;
967   section *fnsec;
968   bool do_frame;
969
970   current_function_func_begin_label = NULL;
971
972   do_frame = dwarf2out_do_frame ();
973
974   /* ??? current_function_func_begin_label is also used by except.c for
975      call-site information.  We must emit this label if it might be used.  */
976   if (!do_frame
977       && (!flag_exceptions
978           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
979     return;
980
981   fnsec = function_section (current_function_decl);
982   switch_to_section (fnsec);
983   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
984                                current_function_funcdef_no);
985   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
986                           current_function_funcdef_no);
987   dup_label = xstrdup (label);
988   current_function_func_begin_label = dup_label;
989
990   /* We can elide the fde allocation if we're not emitting debug info.  */
991   if (!do_frame)
992     return;
993
994   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
995      emit insns as rtx but bypass the bulk of rest_of_compilation, which
996      would include pass_dwarf2_frame.  If we've not created the FDE yet,
997      do so now.  */
998   fde = cfun->fde;
999   if (fde == NULL)
1000     fde = dwarf2out_alloc_current_fde ();
1001
1002   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1003   fde->dw_fde_begin = dup_label;
1004   fde->dw_fde_current_label = dup_label;
1005   fde->in_std_section = (fnsec == text_section
1006                          || (cold_text_section && fnsec == cold_text_section));
1007
1008   /* We only want to output line number information for the genuine dwarf2
1009      prologue case, not the eh frame case.  */
1010 #ifdef DWARF2_DEBUGGING_INFO
1011   if (file)
1012     dwarf2out_source_line (line, file, 0, true);
1013 #endif
1014
1015   if (dwarf2out_do_cfi_asm ())
1016     dwarf2out_do_cfi_startproc (false);
1017   else
1018     {
1019       rtx personality = get_personality_function (current_function_decl);
1020       if (!current_unit_personality)
1021         current_unit_personality = personality;
1022
1023       /* We cannot keep a current personality per function as without CFI
1024          asm, at the point where we emit the CFI data, there is no current
1025          function anymore.  */
1026       if (personality && current_unit_personality != personality)
1027         sorry ("multiple EH personalities are supported only with assemblers "
1028                "supporting .cfi_personality directive");
1029     }
1030 }
1031
1032 /* Output a marker (i.e. a label) for the end of the generated code
1033    for a function prologue.  This gets called *after* the prologue code has
1034    been generated.  */
1035
1036 void
1037 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1038                         const char *file ATTRIBUTE_UNUSED)
1039 {
1040   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1041
1042   /* Output a label to mark the endpoint of the code generated for this
1043      function.  */
1044   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1045                                current_function_funcdef_no);
1046   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1047                           current_function_funcdef_no);
1048   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1049 }
1050
1051 /* Output a marker (i.e. a label) for the beginning of the generated code
1052    for a function epilogue.  This gets called *before* the prologue code has
1053    been generated.  */
1054
1055 void
1056 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1057                           const char *file ATTRIBUTE_UNUSED)
1058 {
1059   dw_fde_ref fde = cfun->fde;
1060   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1061
1062   if (fde->dw_fde_vms_begin_epilogue)
1063     return;
1064
1065   /* Output a label to mark the endpoint of the code generated for this
1066      function.  */
1067   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1068                                current_function_funcdef_no);
1069   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1070                           current_function_funcdef_no);
1071   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1072 }
1073
1074 /* Output a marker (i.e. a label) for the absolute end of the generated code
1075    for a function definition.  This gets called *after* the epilogue code has
1076    been generated.  */
1077
1078 void
1079 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1080                         const char *file ATTRIBUTE_UNUSED)
1081 {
1082   dw_fde_ref fde;
1083   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1084
1085   last_var_location_insn = NULL_RTX;
1086   cached_next_real_insn = NULL_RTX;
1087
1088   if (dwarf2out_do_cfi_asm ())
1089     fprintf (asm_out_file, "\t.cfi_endproc\n");
1090
1091   /* Output a label to mark the endpoint of the code generated for this
1092      function.  */
1093   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1094                                current_function_funcdef_no);
1095   ASM_OUTPUT_LABEL (asm_out_file, label);
1096   fde = cfun->fde;
1097   gcc_assert (fde != NULL);
1098   if (fde->dw_fde_second_begin == NULL)
1099     fde->dw_fde_end = xstrdup (label);
1100 }
1101
1102 void
1103 dwarf2out_frame_finish (void)
1104 {
1105   /* Output call frame information.  */
1106   if (targetm.debug_unwind_info () == UI_DWARF2)
1107     output_call_frame_info (0);
1108
1109   /* Output another copy for the unwinder.  */
1110   if ((flag_unwind_tables || flag_exceptions)
1111       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1112     output_call_frame_info (1);
1113 }
1114
1115 /* Note that the current function section is being used for code.  */
1116
1117 static void
1118 dwarf2out_note_section_used (void)
1119 {
1120   section *sec = current_function_section ();
1121   if (sec == text_section)
1122     text_section_used = true;
1123   else if (sec == cold_text_section)
1124     cold_text_section_used = true;
1125 }
1126
1127 static void var_location_switch_text_section (void);
1128 static void set_cur_line_info_table (section *);
1129
1130 void
1131 dwarf2out_switch_text_section (void)
1132 {
1133   section *sect;
1134   dw_fde_ref fde = cfun->fde;
1135
1136   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1137
1138   if (!in_cold_section_p)
1139     {
1140       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1141       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1142       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1143     }
1144   else
1145     {
1146       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1147       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1148       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1149     }
1150   have_multiple_function_sections = true;
1151
1152   /* There is no need to mark used sections when not debugging.  */
1153   if (cold_text_section != NULL)
1154     dwarf2out_note_section_used ();
1155
1156   if (dwarf2out_do_cfi_asm ())
1157     fprintf (asm_out_file, "\t.cfi_endproc\n");
1158
1159   /* Now do the real section switch.  */
1160   sect = current_function_section ();
1161   switch_to_section (sect);
1162
1163   fde->second_in_std_section
1164     = (sect == text_section
1165        || (cold_text_section && sect == cold_text_section));
1166
1167   if (dwarf2out_do_cfi_asm ())
1168     dwarf2out_do_cfi_startproc (true);
1169
1170   var_location_switch_text_section ();
1171
1172   if (cold_text_section != NULL)
1173     set_cur_line_info_table (sect);
1174 }
1175 \f
1176 /* And now, the subset of the debugging information support code necessary
1177    for emitting location expressions.  */
1178
1179 /* Data about a single source file.  */
1180 struct GTY(()) dwarf_file_data {
1181   const char * filename;
1182   int emitted_number;
1183 };
1184
1185 typedef struct GTY(()) deferred_locations_struct
1186 {
1187   tree variable;
1188   dw_die_ref die;
1189 } deferred_locations;
1190
1191 DEF_VEC_O(deferred_locations);
1192 DEF_VEC_ALLOC_O(deferred_locations,gc);
1193
1194 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1195
1196 DEF_VEC_P(dw_die_ref);
1197 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1198
1199 /* Location lists are ranges + location descriptions for that range,
1200    so you can track variables that are in different places over
1201    their entire life.  */
1202 typedef struct GTY(()) dw_loc_list_struct {
1203   dw_loc_list_ref dw_loc_next;
1204   const char *begin; /* Label for begin address of range */
1205   const char *end;  /* Label for end address of range */
1206   char *ll_symbol; /* Label for beginning of location list.
1207                       Only on head of list */
1208   const char *section; /* Section this loclist is relative to */
1209   dw_loc_descr_ref expr;
1210   hashval_t hash;
1211   /* True if all addresses in this and subsequent lists are known to be
1212      resolved.  */
1213   bool resolved_addr;
1214   /* True if this list has been replaced by dw_loc_next.  */
1215   bool replaced;
1216   bool emitted;
1217   /* True if the range should be emitted even if begin and end
1218      are the same.  */
1219   bool force;
1220 } dw_loc_list_node;
1221
1222 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1223
1224 /* Convert a DWARF stack opcode into its string name.  */
1225
1226 static const char *
1227 dwarf_stack_op_name (unsigned int op)
1228 {
1229   const char *name = get_DW_OP_name (op);
1230
1231   if (name != NULL)
1232     return name;
1233
1234   return "OP_<unknown>";
1235 }
1236
1237 /* Return a pointer to a newly allocated location description.  Location
1238    descriptions are simple expression terms that can be strung
1239    together to form more complicated location (address) descriptions.  */
1240
1241 static inline dw_loc_descr_ref
1242 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1243                unsigned HOST_WIDE_INT oprnd2)
1244 {
1245   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1246
1247   descr->dw_loc_opc = op;
1248   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1249   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1250   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1251   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1252
1253   return descr;
1254 }
1255
1256 /* Return a pointer to a newly allocated location description for
1257    REG and OFFSET.  */
1258
1259 static inline dw_loc_descr_ref
1260 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1261 {
1262   if (reg <= 31)
1263     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1264                           offset, 0);
1265   else
1266     return new_loc_descr (DW_OP_bregx, reg, offset);
1267 }
1268
1269 /* Add a location description term to a location description expression.  */
1270
1271 static inline void
1272 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1273 {
1274   dw_loc_descr_ref *d;
1275
1276   /* Find the end of the chain.  */
1277   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1278     ;
1279
1280   *d = descr;
1281 }
1282
1283 /* Compare two location operands for exact equality.  */
1284
1285 static bool
1286 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1287 {
1288   if (a->val_class != b->val_class)
1289     return false;
1290   switch (a->val_class)
1291     {
1292     case dw_val_class_none:
1293       return true;
1294     case dw_val_class_addr:
1295       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1296
1297     case dw_val_class_offset:
1298     case dw_val_class_unsigned_const:
1299     case dw_val_class_const:
1300     case dw_val_class_range_list:
1301     case dw_val_class_lineptr:
1302     case dw_val_class_macptr:
1303       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1304       return a->v.val_unsigned == b->v.val_unsigned;
1305
1306     case dw_val_class_loc:
1307       return a->v.val_loc == b->v.val_loc;
1308     case dw_val_class_loc_list:
1309       return a->v.val_loc_list == b->v.val_loc_list;
1310     case dw_val_class_die_ref:
1311       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1312     case dw_val_class_fde_ref:
1313       return a->v.val_fde_index == b->v.val_fde_index;
1314     case dw_val_class_lbl_id:
1315       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1316     case dw_val_class_str:
1317       return a->v.val_str == b->v.val_str;
1318     case dw_val_class_flag:
1319       return a->v.val_flag == b->v.val_flag;
1320     case dw_val_class_file:
1321       return a->v.val_file == b->v.val_file;
1322     case dw_val_class_decl_ref:
1323       return a->v.val_decl_ref == b->v.val_decl_ref;
1324     
1325     case dw_val_class_const_double:
1326       return (a->v.val_double.high == b->v.val_double.high
1327               && a->v.val_double.low == b->v.val_double.low);
1328
1329     case dw_val_class_vec:
1330       {
1331         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1332         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1333
1334         return (a_len == b_len
1335                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1336       }
1337
1338     case dw_val_class_data8:
1339       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1340
1341     case dw_val_class_vms_delta:
1342       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1343               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1344     }
1345   gcc_unreachable ();
1346 }
1347
1348 /* Compare two location atoms for exact equality.  */
1349
1350 static bool
1351 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1352 {
1353   if (a->dw_loc_opc != b->dw_loc_opc)
1354     return false;
1355
1356   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1357      address size, but since we always allocate cleared storage it
1358      should be zero for other types of locations.  */
1359   if (a->dtprel != b->dtprel)
1360     return false;
1361
1362   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1363           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1364 }
1365
1366 /* Compare two complete location expressions for exact equality.  */
1367
1368 bool
1369 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1370 {
1371   while (1)
1372     {
1373       if (a == b)
1374         return true;
1375       if (a == NULL || b == NULL)
1376         return false;
1377       if (!loc_descr_equal_p_1 (a, b))
1378         return false;
1379
1380       a = a->dw_loc_next;
1381       b = b->dw_loc_next;
1382     }
1383 }
1384
1385
1386 /* Add a constant OFFSET to a location expression.  */
1387
1388 static void
1389 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1390 {
1391   dw_loc_descr_ref loc;
1392   HOST_WIDE_INT *p;
1393
1394   gcc_assert (*list_head != NULL);
1395
1396   if (!offset)
1397     return;
1398
1399   /* Find the end of the chain.  */
1400   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1401     ;
1402
1403   p = NULL;
1404   if (loc->dw_loc_opc == DW_OP_fbreg
1405       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1406     p = &loc->dw_loc_oprnd1.v.val_int;
1407   else if (loc->dw_loc_opc == DW_OP_bregx)
1408     p = &loc->dw_loc_oprnd2.v.val_int;
1409
1410   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1411      offset.  Don't optimize if an signed integer overflow would happen.  */
1412   if (p != NULL
1413       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1414           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1415     *p += offset;
1416
1417   else if (offset > 0)
1418     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1419
1420   else
1421     {
1422       loc->dw_loc_next = int_loc_descriptor (-offset);
1423       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1424     }
1425 }
1426
1427 /* Add a constant OFFSET to a location list.  */
1428
1429 static void
1430 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1431 {
1432   dw_loc_list_ref d;
1433   for (d = list_head; d != NULL; d = d->dw_loc_next)
1434     loc_descr_plus_const (&d->expr, offset);
1435 }
1436
1437 #define DWARF_REF_SIZE  \
1438   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1439
1440 static unsigned long int get_base_type_offset (dw_die_ref);
1441
1442 /* Return the size of a location descriptor.  */
1443
1444 static unsigned long
1445 size_of_loc_descr (dw_loc_descr_ref loc)
1446 {
1447   unsigned long size = 1;
1448
1449   switch (loc->dw_loc_opc)
1450     {
1451     case DW_OP_addr:
1452       size += DWARF2_ADDR_SIZE;
1453       break;
1454     case DW_OP_const1u:
1455     case DW_OP_const1s:
1456       size += 1;
1457       break;
1458     case DW_OP_const2u:
1459     case DW_OP_const2s:
1460       size += 2;
1461       break;
1462     case DW_OP_const4u:
1463     case DW_OP_const4s:
1464       size += 4;
1465       break;
1466     case DW_OP_const8u:
1467     case DW_OP_const8s:
1468       size += 8;
1469       break;
1470     case DW_OP_constu:
1471       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1472       break;
1473     case DW_OP_consts:
1474       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1475       break;
1476     case DW_OP_pick:
1477       size += 1;
1478       break;
1479     case DW_OP_plus_uconst:
1480       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1481       break;
1482     case DW_OP_skip:
1483     case DW_OP_bra:
1484       size += 2;
1485       break;
1486     case DW_OP_breg0:
1487     case DW_OP_breg1:
1488     case DW_OP_breg2:
1489     case DW_OP_breg3:
1490     case DW_OP_breg4:
1491     case DW_OP_breg5:
1492     case DW_OP_breg6:
1493     case DW_OP_breg7:
1494     case DW_OP_breg8:
1495     case DW_OP_breg9:
1496     case DW_OP_breg10:
1497     case DW_OP_breg11:
1498     case DW_OP_breg12:
1499     case DW_OP_breg13:
1500     case DW_OP_breg14:
1501     case DW_OP_breg15:
1502     case DW_OP_breg16:
1503     case DW_OP_breg17:
1504     case DW_OP_breg18:
1505     case DW_OP_breg19:
1506     case DW_OP_breg20:
1507     case DW_OP_breg21:
1508     case DW_OP_breg22:
1509     case DW_OP_breg23:
1510     case DW_OP_breg24:
1511     case DW_OP_breg25:
1512     case DW_OP_breg26:
1513     case DW_OP_breg27:
1514     case DW_OP_breg28:
1515     case DW_OP_breg29:
1516     case DW_OP_breg30:
1517     case DW_OP_breg31:
1518       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1519       break;
1520     case DW_OP_regx:
1521       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1522       break;
1523     case DW_OP_fbreg:
1524       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1525       break;
1526     case DW_OP_bregx:
1527       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1528       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1529       break;
1530     case DW_OP_piece:
1531       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1532       break;
1533     case DW_OP_bit_piece:
1534       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1535       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1536       break;
1537     case DW_OP_deref_size:
1538     case DW_OP_xderef_size:
1539       size += 1;
1540       break;
1541     case DW_OP_call2:
1542       size += 2;
1543       break;
1544     case DW_OP_call4:
1545       size += 4;
1546       break;
1547     case DW_OP_call_ref:
1548       size += DWARF_REF_SIZE;
1549       break;
1550     case DW_OP_implicit_value:
1551       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1552               + loc->dw_loc_oprnd1.v.val_unsigned;
1553       break;
1554     case DW_OP_GNU_implicit_pointer:
1555       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1556       break;
1557     case DW_OP_GNU_entry_value:
1558       {
1559         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1560         size += size_of_uleb128 (op_size) + op_size;
1561         break;
1562       }
1563     case DW_OP_GNU_const_type:
1564       {
1565         unsigned long o
1566           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1567         size += size_of_uleb128 (o) + 1;
1568         switch (loc->dw_loc_oprnd2.val_class)
1569           {
1570           case dw_val_class_vec:
1571             size += loc->dw_loc_oprnd2.v.val_vec.length
1572                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1573             break;
1574           case dw_val_class_const:
1575             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1576             break;
1577           case dw_val_class_const_double:
1578             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1579             break;
1580           default:
1581             gcc_unreachable ();
1582           }
1583         break;
1584       }
1585     case DW_OP_GNU_regval_type:
1586       {
1587         unsigned long o
1588           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1589         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1590                 + size_of_uleb128 (o);
1591       }
1592       break;
1593     case DW_OP_GNU_deref_type:
1594       {
1595         unsigned long o
1596           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1597         size += 1 + size_of_uleb128 (o);
1598       }
1599       break;
1600     case DW_OP_GNU_convert:
1601     case DW_OP_GNU_reinterpret:
1602       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1603         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1604       else
1605         {
1606           unsigned long o
1607             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1608           size += size_of_uleb128 (o);
1609         }
1610       break;
1611     case DW_OP_GNU_parameter_ref:
1612       size += 4;
1613       break;
1614     default:
1615       break;
1616     }
1617
1618   return size;
1619 }
1620
1621 /* Return the size of a series of location descriptors.  */
1622
1623 unsigned long
1624 size_of_locs (dw_loc_descr_ref loc)
1625 {
1626   dw_loc_descr_ref l;
1627   unsigned long size;
1628
1629   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1630      field, to avoid writing to a PCH file.  */
1631   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1632     {
1633       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1634         break;
1635       size += size_of_loc_descr (l);
1636     }
1637   if (! l)
1638     return size;
1639
1640   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1641     {
1642       l->dw_loc_addr = size;
1643       size += size_of_loc_descr (l);
1644     }
1645
1646   return size;
1647 }
1648
1649 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1650 static void get_ref_die_offset_label (char *, dw_die_ref);
1651 static unsigned long int get_ref_die_offset (dw_die_ref);
1652
1653 /* Output location description stack opcode's operands (if any).
1654    The for_eh_or_skip parameter controls whether register numbers are
1655    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1656    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1657    info).  This should be suppressed for the cases that have not been converted
1658    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1659
1660 static void
1661 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1662 {
1663   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1664   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1665
1666   switch (loc->dw_loc_opc)
1667     {
1668 #ifdef DWARF2_DEBUGGING_INFO
1669     case DW_OP_const2u:
1670     case DW_OP_const2s:
1671       dw2_asm_output_data (2, val1->v.val_int, NULL);
1672       break;
1673     case DW_OP_const4u:
1674       if (loc->dtprel)
1675         {
1676           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1677           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1678                                                val1->v.val_addr);
1679           fputc ('\n', asm_out_file);
1680           break;
1681         }
1682       /* FALLTHRU */
1683     case DW_OP_const4s:
1684       dw2_asm_output_data (4, val1->v.val_int, NULL);
1685       break;
1686     case DW_OP_const8u:
1687       if (loc->dtprel)
1688         {
1689           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1690           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1691                                                val1->v.val_addr);
1692           fputc ('\n', asm_out_file);
1693           break;
1694         }
1695       /* FALLTHRU */
1696     case DW_OP_const8s:
1697       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1698       dw2_asm_output_data (8, val1->v.val_int, NULL);
1699       break;
1700     case DW_OP_skip:
1701     case DW_OP_bra:
1702       {
1703         int offset;
1704
1705         gcc_assert (val1->val_class == dw_val_class_loc);
1706         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1707
1708         dw2_asm_output_data (2, offset, NULL);
1709       }
1710       break;
1711     case DW_OP_implicit_value:
1712       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1713       switch (val2->val_class)
1714         {
1715         case dw_val_class_const:
1716           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1717           break;
1718         case dw_val_class_vec:
1719           {
1720             unsigned int elt_size = val2->v.val_vec.elt_size;
1721             unsigned int len = val2->v.val_vec.length;
1722             unsigned int i;
1723             unsigned char *p;
1724
1725             if (elt_size > sizeof (HOST_WIDE_INT))
1726               {
1727                 elt_size /= 2;
1728                 len *= 2;
1729               }
1730             for (i = 0, p = val2->v.val_vec.array;
1731                  i < len;
1732                  i++, p += elt_size)
1733               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1734                                    "fp or vector constant word %u", i);
1735           }
1736           break;
1737         case dw_val_class_const_double:
1738           {
1739             unsigned HOST_WIDE_INT first, second;
1740
1741             if (WORDS_BIG_ENDIAN)
1742               {
1743                 first = val2->v.val_double.high;
1744                 second = val2->v.val_double.low;
1745               }
1746             else
1747               {
1748                 first = val2->v.val_double.low;
1749                 second = val2->v.val_double.high;
1750               }
1751             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1752                                  first, NULL);
1753             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1754                                  second, NULL);
1755           }
1756           break;
1757         case dw_val_class_addr:
1758           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1759           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1760           break;
1761         default:
1762           gcc_unreachable ();
1763         }
1764       break;
1765 #else
1766     case DW_OP_const2u:
1767     case DW_OP_const2s:
1768     case DW_OP_const4u:
1769     case DW_OP_const4s:
1770     case DW_OP_const8u:
1771     case DW_OP_const8s:
1772     case DW_OP_skip:
1773     case DW_OP_bra:
1774     case DW_OP_implicit_value:
1775       /* We currently don't make any attempt to make sure these are
1776          aligned properly like we do for the main unwind info, so
1777          don't support emitting things larger than a byte if we're
1778          only doing unwinding.  */
1779       gcc_unreachable ();
1780 #endif
1781     case DW_OP_const1u:
1782     case DW_OP_const1s:
1783       dw2_asm_output_data (1, val1->v.val_int, NULL);
1784       break;
1785     case DW_OP_constu:
1786       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1787       break;
1788     case DW_OP_consts:
1789       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1790       break;
1791     case DW_OP_pick:
1792       dw2_asm_output_data (1, val1->v.val_int, NULL);
1793       break;
1794     case DW_OP_plus_uconst:
1795       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1796       break;
1797     case DW_OP_breg0:
1798     case DW_OP_breg1:
1799     case DW_OP_breg2:
1800     case DW_OP_breg3:
1801     case DW_OP_breg4:
1802     case DW_OP_breg5:
1803     case DW_OP_breg6:
1804     case DW_OP_breg7:
1805     case DW_OP_breg8:
1806     case DW_OP_breg9:
1807     case DW_OP_breg10:
1808     case DW_OP_breg11:
1809     case DW_OP_breg12:
1810     case DW_OP_breg13:
1811     case DW_OP_breg14:
1812     case DW_OP_breg15:
1813     case DW_OP_breg16:
1814     case DW_OP_breg17:
1815     case DW_OP_breg18:
1816     case DW_OP_breg19:
1817     case DW_OP_breg20:
1818     case DW_OP_breg21:
1819     case DW_OP_breg22:
1820     case DW_OP_breg23:
1821     case DW_OP_breg24:
1822     case DW_OP_breg25:
1823     case DW_OP_breg26:
1824     case DW_OP_breg27:
1825     case DW_OP_breg28:
1826     case DW_OP_breg29:
1827     case DW_OP_breg30:
1828     case DW_OP_breg31:
1829       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1830       break;
1831     case DW_OP_regx:
1832       {
1833         unsigned r = val1->v.val_unsigned;
1834         if (for_eh_or_skip >= 0)
1835           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1836         gcc_assert (size_of_uleb128 (r) 
1837                     == size_of_uleb128 (val1->v.val_unsigned));
1838         dw2_asm_output_data_uleb128 (r, NULL);  
1839       }
1840       break;
1841     case DW_OP_fbreg:
1842       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1843       break;
1844     case DW_OP_bregx:
1845       {
1846         unsigned r = val1->v.val_unsigned;
1847         if (for_eh_or_skip >= 0)
1848           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1849         gcc_assert (size_of_uleb128 (r) 
1850                     == size_of_uleb128 (val1->v.val_unsigned));
1851         dw2_asm_output_data_uleb128 (r, NULL);  
1852         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1853       }
1854       break;
1855     case DW_OP_piece:
1856       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1857       break;
1858     case DW_OP_bit_piece:
1859       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1860       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1861       break;
1862     case DW_OP_deref_size:
1863     case DW_OP_xderef_size:
1864       dw2_asm_output_data (1, val1->v.val_int, NULL);
1865       break;
1866
1867     case DW_OP_addr:
1868       if (loc->dtprel)
1869         {
1870           if (targetm.asm_out.output_dwarf_dtprel)
1871             {
1872               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1873                                                    DWARF2_ADDR_SIZE,
1874                                                    val1->v.val_addr);
1875               fputc ('\n', asm_out_file);
1876             }
1877           else
1878             gcc_unreachable ();
1879         }
1880       else
1881         {
1882 #ifdef DWARF2_DEBUGGING_INFO
1883           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1884 #else
1885           gcc_unreachable ();
1886 #endif
1887         }
1888       break;
1889
1890     case DW_OP_GNU_implicit_pointer:
1891       {
1892         char label[MAX_ARTIFICIAL_LABEL_BYTES
1893                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
1894         gcc_assert (val1->val_class == dw_val_class_die_ref);
1895         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1896         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1897         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1898       }
1899       break;
1900
1901     case DW_OP_GNU_entry_value:
1902       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1903       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1904       break;
1905
1906     case DW_OP_GNU_const_type:
1907       {
1908         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1909         gcc_assert (o);
1910         dw2_asm_output_data_uleb128 (o, NULL);
1911         switch (val2->val_class)
1912           {
1913           case dw_val_class_const:
1914             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1915             dw2_asm_output_data (1, l, NULL);
1916             dw2_asm_output_data (l, val2->v.val_int, NULL);
1917             break;
1918           case dw_val_class_vec:
1919             {
1920               unsigned int elt_size = val2->v.val_vec.elt_size;
1921               unsigned int len = val2->v.val_vec.length;
1922               unsigned int i;
1923               unsigned char *p;
1924
1925               l = len * elt_size;
1926               dw2_asm_output_data (1, l, NULL);
1927               if (elt_size > sizeof (HOST_WIDE_INT))
1928                 {
1929                   elt_size /= 2;
1930                   len *= 2;
1931                 }
1932               for (i = 0, p = val2->v.val_vec.array;
1933                    i < len;
1934                    i++, p += elt_size)
1935                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1936                                      "fp or vector constant word %u", i);
1937             }
1938             break;
1939           case dw_val_class_const_double:
1940             {
1941               unsigned HOST_WIDE_INT first, second;
1942               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1943
1944               dw2_asm_output_data (1, 2 * l, NULL);
1945               if (WORDS_BIG_ENDIAN)
1946                 {
1947                   first = val2->v.val_double.high;
1948                   second = val2->v.val_double.low;
1949                 }
1950               else
1951                 {
1952                   first = val2->v.val_double.low;
1953                   second = val2->v.val_double.high;
1954                 }
1955               dw2_asm_output_data (l, first, NULL);
1956               dw2_asm_output_data (l, second, NULL);
1957             }
1958             break;
1959           default:
1960             gcc_unreachable ();
1961           }
1962       }
1963       break;
1964     case DW_OP_GNU_regval_type:
1965       {
1966         unsigned r = val1->v.val_unsigned;
1967         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
1968         gcc_assert (o);
1969         if (for_eh_or_skip >= 0)
1970           {
1971             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1972             gcc_assert (size_of_uleb128 (r)
1973                         == size_of_uleb128 (val1->v.val_unsigned));
1974           }
1975         dw2_asm_output_data_uleb128 (r, NULL);
1976         dw2_asm_output_data_uleb128 (o, NULL);
1977       }
1978       break;
1979     case DW_OP_GNU_deref_type:
1980       {
1981         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
1982         gcc_assert (o);
1983         dw2_asm_output_data (1, val1->v.val_int, NULL);
1984         dw2_asm_output_data_uleb128 (o, NULL);
1985       }
1986       break;
1987     case DW_OP_GNU_convert:
1988     case DW_OP_GNU_reinterpret:
1989       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1990         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1991       else
1992         {
1993           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
1994           gcc_assert (o);
1995           dw2_asm_output_data_uleb128 (o, NULL);
1996         }
1997       break;
1998
1999     case DW_OP_GNU_parameter_ref:
2000       {
2001         unsigned long o;
2002         gcc_assert (val1->val_class == dw_val_class_die_ref);
2003         o = get_ref_die_offset (val1->v.val_die_ref.die);
2004         dw2_asm_output_data (4, o, NULL);
2005       }
2006       break;
2007
2008     default:
2009       /* Other codes have no operands.  */
2010       break;
2011     }
2012 }
2013
2014 /* Output a sequence of location operations.  
2015    The for_eh_or_skip parameter controls whether register numbers are
2016    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2017    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2018    info).  This should be suppressed for the cases that have not been converted
2019    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2020
2021 void
2022 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2023 {
2024   for (; loc != NULL; loc = loc->dw_loc_next)
2025     {
2026       enum dwarf_location_atom opc = loc->dw_loc_opc;
2027       /* Output the opcode.  */
2028       if (for_eh_or_skip >= 0 
2029           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2030         {
2031           unsigned r = (opc - DW_OP_breg0);
2032           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2033           gcc_assert (r <= 31);
2034           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2035         }
2036       else if (for_eh_or_skip >= 0 
2037                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2038         {
2039           unsigned r = (opc - DW_OP_reg0);
2040           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2041           gcc_assert (r <= 31);
2042           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2043         }
2044
2045       dw2_asm_output_data (1, opc,
2046                              "%s", dwarf_stack_op_name (opc));
2047
2048       /* Output the operand(s) (if any).  */
2049       output_loc_operands (loc, for_eh_or_skip);
2050     }
2051 }
2052
2053 /* Output location description stack opcode's operands (if any).
2054    The output is single bytes on a line, suitable for .cfi_escape.  */
2055
2056 static void
2057 output_loc_operands_raw (dw_loc_descr_ref loc)
2058 {
2059   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2060   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2061
2062   switch (loc->dw_loc_opc)
2063     {
2064     case DW_OP_addr:
2065     case DW_OP_implicit_value:
2066       /* We cannot output addresses in .cfi_escape, only bytes.  */
2067       gcc_unreachable ();
2068
2069     case DW_OP_const1u:
2070     case DW_OP_const1s:
2071     case DW_OP_pick:
2072     case DW_OP_deref_size:
2073     case DW_OP_xderef_size:
2074       fputc (',', asm_out_file);
2075       dw2_asm_output_data_raw (1, val1->v.val_int);
2076       break;
2077
2078     case DW_OP_const2u:
2079     case DW_OP_const2s:
2080       fputc (',', asm_out_file);
2081       dw2_asm_output_data_raw (2, val1->v.val_int);
2082       break;
2083
2084     case DW_OP_const4u:
2085     case DW_OP_const4s:
2086       fputc (',', asm_out_file);
2087       dw2_asm_output_data_raw (4, val1->v.val_int);
2088       break;
2089
2090     case DW_OP_const8u:
2091     case DW_OP_const8s:
2092       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2093       fputc (',', asm_out_file);
2094       dw2_asm_output_data_raw (8, val1->v.val_int);
2095       break;
2096
2097     case DW_OP_skip:
2098     case DW_OP_bra:
2099       {
2100         int offset;
2101
2102         gcc_assert (val1->val_class == dw_val_class_loc);
2103         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2104
2105         fputc (',', asm_out_file);
2106         dw2_asm_output_data_raw (2, offset);
2107       }
2108       break;
2109
2110     case DW_OP_regx:
2111       {
2112         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2113         gcc_assert (size_of_uleb128 (r) 
2114                     == size_of_uleb128 (val1->v.val_unsigned));
2115         fputc (',', asm_out_file);
2116         dw2_asm_output_data_uleb128_raw (r);
2117       }
2118       break;
2119       
2120     case DW_OP_constu:
2121     case DW_OP_plus_uconst:
2122     case DW_OP_piece:
2123       fputc (',', asm_out_file);
2124       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2125       break;
2126
2127     case DW_OP_bit_piece:
2128       fputc (',', asm_out_file);
2129       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2130       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2131       break;
2132
2133     case DW_OP_consts:
2134     case DW_OP_breg0:
2135     case DW_OP_breg1:
2136     case DW_OP_breg2:
2137     case DW_OP_breg3:
2138     case DW_OP_breg4:
2139     case DW_OP_breg5:
2140     case DW_OP_breg6:
2141     case DW_OP_breg7:
2142     case DW_OP_breg8:
2143     case DW_OP_breg9:
2144     case DW_OP_breg10:
2145     case DW_OP_breg11:
2146     case DW_OP_breg12:
2147     case DW_OP_breg13:
2148     case DW_OP_breg14:
2149     case DW_OP_breg15:
2150     case DW_OP_breg16:
2151     case DW_OP_breg17:
2152     case DW_OP_breg18:
2153     case DW_OP_breg19:
2154     case DW_OP_breg20:
2155     case DW_OP_breg21:
2156     case DW_OP_breg22:
2157     case DW_OP_breg23:
2158     case DW_OP_breg24:
2159     case DW_OP_breg25:
2160     case DW_OP_breg26:
2161     case DW_OP_breg27:
2162     case DW_OP_breg28:
2163     case DW_OP_breg29:
2164     case DW_OP_breg30:
2165     case DW_OP_breg31:
2166     case DW_OP_fbreg:
2167       fputc (',', asm_out_file);
2168       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2169       break;
2170
2171     case DW_OP_bregx:
2172       {
2173         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2174         gcc_assert (size_of_uleb128 (r) 
2175                     == size_of_uleb128 (val1->v.val_unsigned));
2176         fputc (',', asm_out_file);
2177         dw2_asm_output_data_uleb128_raw (r);
2178         fputc (',', asm_out_file);
2179         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2180       }
2181       break;
2182
2183     case DW_OP_GNU_implicit_pointer:
2184     case DW_OP_GNU_entry_value:
2185     case DW_OP_GNU_const_type:
2186     case DW_OP_GNU_regval_type:
2187     case DW_OP_GNU_deref_type:
2188     case DW_OP_GNU_convert:
2189     case DW_OP_GNU_reinterpret:
2190     case DW_OP_GNU_parameter_ref:
2191       gcc_unreachable ();
2192       break;
2193
2194     default:
2195       /* Other codes have no operands.  */
2196       break;
2197     }
2198 }
2199
2200 void
2201 output_loc_sequence_raw (dw_loc_descr_ref loc)
2202 {
2203   while (1)
2204     {
2205       enum dwarf_location_atom opc = loc->dw_loc_opc;
2206       /* Output the opcode.  */
2207       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2208         {
2209           unsigned r = (opc - DW_OP_breg0);
2210           r = DWARF2_FRAME_REG_OUT (r, 1);
2211           gcc_assert (r <= 31);
2212           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2213         }
2214       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2215         {
2216           unsigned r = (opc - DW_OP_reg0);
2217           r = DWARF2_FRAME_REG_OUT (r, 1);
2218           gcc_assert (r <= 31);
2219           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2220         }
2221       /* Output the opcode.  */
2222       fprintf (asm_out_file, "%#x", opc);
2223       output_loc_operands_raw (loc);
2224
2225       if (!loc->dw_loc_next)
2226         break;
2227       loc = loc->dw_loc_next;
2228
2229       fputc (',', asm_out_file);
2230     }
2231 }
2232
2233 /* This function builds a dwarf location descriptor sequence from a
2234    dw_cfa_location, adding the given OFFSET to the result of the
2235    expression.  */
2236
2237 struct dw_loc_descr_struct *
2238 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2239 {
2240   struct dw_loc_descr_struct *head, *tmp;
2241
2242   offset += cfa->offset;
2243
2244   if (cfa->indirect)
2245     {
2246       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2247       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2248       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2249       add_loc_descr (&head, tmp);
2250       if (offset != 0)
2251         {
2252           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2253           add_loc_descr (&head, tmp);
2254         }
2255     }
2256   else
2257     head = new_reg_loc_descr (cfa->reg, offset);
2258
2259   return head;
2260 }
2261
2262 /* This function builds a dwarf location descriptor sequence for
2263    the address at OFFSET from the CFA when stack is aligned to
2264    ALIGNMENT byte.  */
2265
2266 struct dw_loc_descr_struct *
2267 build_cfa_aligned_loc (dw_cfa_location *cfa,
2268                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2269 {
2270   struct dw_loc_descr_struct *head;
2271   unsigned int dwarf_fp
2272     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2273
2274   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2275   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2276     {
2277       head = new_reg_loc_descr (dwarf_fp, 0);
2278       add_loc_descr (&head, int_loc_descriptor (alignment));
2279       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2280       loc_descr_plus_const (&head, offset);
2281     }
2282   else
2283     head = new_reg_loc_descr (dwarf_fp, offset);
2284   return head;
2285 }
2286 \f
2287 /* And now, the support for symbolic debugging information.  */
2288
2289 /* .debug_str support.  */
2290 static int output_indirect_string (void **, void *);
2291
2292 static void dwarf2out_init (const char *);
2293 static void dwarf2out_finish (const char *);
2294 static void dwarf2out_assembly_start (void);
2295 static void dwarf2out_define (unsigned int, const char *);
2296 static void dwarf2out_undef (unsigned int, const char *);
2297 static void dwarf2out_start_source_file (unsigned, const char *);
2298 static void dwarf2out_end_source_file (unsigned);
2299 static void dwarf2out_function_decl (tree);
2300 static void dwarf2out_begin_block (unsigned, unsigned);
2301 static void dwarf2out_end_block (unsigned, unsigned);
2302 static bool dwarf2out_ignore_block (const_tree);
2303 static void dwarf2out_global_decl (tree);
2304 static void dwarf2out_type_decl (tree, int);
2305 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2306 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2307                                                  dw_die_ref);
2308 static void dwarf2out_abstract_function (tree);
2309 static void dwarf2out_var_location (rtx);
2310 static void dwarf2out_begin_function (tree);
2311 static void dwarf2out_set_name (tree, tree);
2312
2313 /* The debug hooks structure.  */
2314
2315 const struct gcc_debug_hooks dwarf2_debug_hooks =
2316 {
2317   dwarf2out_init,
2318   dwarf2out_finish,
2319   dwarf2out_assembly_start,
2320   dwarf2out_define,
2321   dwarf2out_undef,
2322   dwarf2out_start_source_file,
2323   dwarf2out_end_source_file,
2324   dwarf2out_begin_block,
2325   dwarf2out_end_block,
2326   dwarf2out_ignore_block,
2327   dwarf2out_source_line,
2328   dwarf2out_begin_prologue,
2329 #if VMS_DEBUGGING_INFO
2330   dwarf2out_vms_end_prologue,
2331   dwarf2out_vms_begin_epilogue,
2332 #else
2333   debug_nothing_int_charstar,
2334   debug_nothing_int_charstar,
2335 #endif
2336   dwarf2out_end_epilogue,
2337   dwarf2out_begin_function,
2338   debug_nothing_int,            /* end_function */
2339   dwarf2out_function_decl,      /* function_decl */
2340   dwarf2out_global_decl,
2341   dwarf2out_type_decl,          /* type_decl */
2342   dwarf2out_imported_module_or_decl,
2343   debug_nothing_tree,           /* deferred_inline_function */
2344   /* The DWARF 2 backend tries to reduce debugging bloat by not
2345      emitting the abstract description of inline functions until
2346      something tries to reference them.  */
2347   dwarf2out_abstract_function,  /* outlining_inline_function */
2348   debug_nothing_rtx,            /* label */
2349   debug_nothing_int,            /* handle_pch */
2350   dwarf2out_var_location,
2351   dwarf2out_switch_text_section,
2352   dwarf2out_set_name,
2353   1,                            /* start_end_main_source_file */
2354   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2355 };
2356 \f
2357 /* NOTE: In the comments in this file, many references are made to
2358    "Debugging Information Entries".  This term is abbreviated as `DIE'
2359    throughout the remainder of this file.  */
2360
2361 /* An internal representation of the DWARF output is built, and then
2362    walked to generate the DWARF debugging info.  The walk of the internal
2363    representation is done after the entire program has been compiled.
2364    The types below are used to describe the internal representation.  */
2365
2366 /* Whether to put type DIEs into their own section .debug_types instead
2367    of making them part of the .debug_info section.  Only supported for
2368    Dwarf V4 or higher and the user didn't disable them through
2369    -fno-debug-types-section.  It is more efficient to put them in a
2370    separate comdat sections since the linker will then be able to
2371    remove duplicates.  But not all tools support .debug_types sections
2372    yet.  */
2373
2374 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2375
2376 /* Various DIE's use offsets relative to the beginning of the
2377    .debug_info section to refer to each other.  */
2378
2379 typedef long int dw_offset;
2380
2381 /* Define typedefs here to avoid circular dependencies.  */
2382
2383 typedef struct dw_attr_struct *dw_attr_ref;
2384 typedef struct dw_line_info_struct *dw_line_info_ref;
2385 typedef struct pubname_struct *pubname_ref;
2386 typedef struct dw_ranges_struct *dw_ranges_ref;
2387 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2388 typedef struct comdat_type_struct *comdat_type_node_ref;
2389
2390 /* The entries in the line_info table more-or-less mirror the opcodes
2391    that are used in the real dwarf line table.  Arrays of these entries
2392    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2393    supported.  */
2394
2395 enum dw_line_info_opcode {
2396   /* Emit DW_LNE_set_address; the operand is the label index.  */
2397   LI_set_address,
2398
2399   /* Emit a row to the matrix with the given line.  This may be done
2400      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2401      special opcodes.  */
2402   LI_set_line,
2403
2404   /* Emit a DW_LNS_set_file.  */
2405   LI_set_file,
2406
2407   /* Emit a DW_LNS_set_column.  */
2408   LI_set_column,
2409
2410   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2411   LI_negate_stmt,
2412
2413   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2414   LI_set_prologue_end,
2415   LI_set_epilogue_begin,
2416
2417   /* Emit a DW_LNE_set_discriminator.  */
2418   LI_set_discriminator
2419 };
2420
2421 typedef struct GTY(()) dw_line_info_struct {
2422   enum dw_line_info_opcode opcode;
2423   unsigned int val;
2424 } dw_line_info_entry;
2425
2426 DEF_VEC_O(dw_line_info_entry);
2427 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2428
2429 typedef struct GTY(()) dw_line_info_table_struct {
2430   /* The label that marks the end of this section.  */
2431   const char *end_label;
2432
2433   /* The values for the last row of the matrix, as collected in the table.
2434      These are used to minimize the changes to the next row.  */
2435   unsigned int file_num;
2436   unsigned int line_num;
2437   unsigned int column_num;
2438   int discrim_num;
2439   bool is_stmt;
2440   bool in_use;
2441
2442   VEC(dw_line_info_entry, gc) *entries;
2443 } dw_line_info_table;
2444
2445 typedef dw_line_info_table *dw_line_info_table_p;
2446
2447 DEF_VEC_P(dw_line_info_table_p);
2448 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2449
2450 /* Each DIE attribute has a field specifying the attribute kind,
2451    a link to the next attribute in the chain, and an attribute value.
2452    Attributes are typically linked below the DIE they modify.  */
2453
2454 typedef struct GTY(()) dw_attr_struct {
2455   enum dwarf_attribute dw_attr;
2456   dw_val_node dw_attr_val;
2457 }
2458 dw_attr_node;
2459
2460 DEF_VEC_O(dw_attr_node);
2461 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2462
2463 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2464    The children of each node form a circular list linked by
2465    die_sib.  die_child points to the node *before* the "first" child node.  */
2466
2467 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2468   union die_symbol_or_type_node
2469     {
2470       const char * GTY ((tag ("0"))) die_symbol;
2471       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2472     }
2473   GTY ((desc ("%0.comdat_type_p"))) die_id;
2474   VEC(dw_attr_node,gc) * die_attr;
2475   dw_die_ref die_parent;
2476   dw_die_ref die_child;
2477   dw_die_ref die_sib;
2478   dw_die_ref die_definition; /* ref from a specification to its definition */
2479   dw_offset die_offset;
2480   unsigned long die_abbrev;
2481   int die_mark;
2482   unsigned int decl_id;
2483   enum dwarf_tag die_tag;
2484   /* Die is used and must not be pruned as unused.  */
2485   BOOL_BITFIELD die_perennial_p : 1;
2486   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2487   /* Lots of spare bits.  */
2488 }
2489 die_node;
2490
2491 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2492 #define FOR_EACH_CHILD(die, c, expr) do {       \
2493   c = die->die_child;                           \
2494   if (c) do {                                   \
2495     c = c->die_sib;                             \
2496     expr;                                       \
2497   } while (c != die->die_child);                \
2498 } while (0)
2499
2500 /* The pubname structure */
2501
2502 typedef struct GTY(()) pubname_struct {
2503   dw_die_ref die;
2504   const char *name;
2505 }
2506 pubname_entry;
2507
2508 DEF_VEC_O(pubname_entry);
2509 DEF_VEC_ALLOC_O(pubname_entry, gc);
2510
2511 struct GTY(()) dw_ranges_struct {
2512   /* If this is positive, it's a block number, otherwise it's a
2513      bitwise-negated index into dw_ranges_by_label.  */
2514   int num;
2515 };
2516
2517 /* A structure to hold a macinfo entry.  */
2518
2519 typedef struct GTY(()) macinfo_struct {
2520   unsigned char code;
2521   unsigned HOST_WIDE_INT lineno;
2522   const char *info;
2523 }
2524 macinfo_entry;
2525
2526 DEF_VEC_O(macinfo_entry);
2527 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2528
2529 struct GTY(()) dw_ranges_by_label_struct {
2530   const char *begin;
2531   const char *end;
2532 };
2533
2534 /* The comdat type node structure.  */
2535 typedef struct GTY(()) comdat_type_struct
2536 {
2537   dw_die_ref root_die;
2538   dw_die_ref type_die;
2539   dw_die_ref skeleton_die;
2540   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2541   struct comdat_type_struct *next;
2542 }
2543 comdat_type_node;
2544
2545 /* The limbo die list structure.  */
2546 typedef struct GTY(()) limbo_die_struct {
2547   dw_die_ref die;
2548   tree created_for;
2549   struct limbo_die_struct *next;
2550 }
2551 limbo_die_node;
2552
2553 typedef struct skeleton_chain_struct
2554 {
2555   dw_die_ref old_die;
2556   dw_die_ref new_die;
2557   struct skeleton_chain_struct *parent;
2558 }
2559 skeleton_chain_node;
2560
2561 /* Define a macro which returns nonzero for a TYPE_DECL which was
2562    implicitly generated for a type.
2563
2564    Note that, unlike the C front-end (which generates a NULL named
2565    TYPE_DECL node for each complete tagged type, each array type,
2566    and each function type node created) the C++ front-end generates
2567    a _named_ TYPE_DECL node for each tagged type node created.
2568    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2569    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2570    front-end, but for each type, tagged or not.  */
2571
2572 #define TYPE_DECL_IS_STUB(decl)                         \
2573   (DECL_NAME (decl) == NULL_TREE                        \
2574    || (DECL_ARTIFICIAL (decl)                           \
2575        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2576            /* This is necessary for stub decls that     \
2577               appear in nested inline functions.  */    \
2578            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2579                && (decl_ultimate_origin (decl)          \
2580                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2581
2582 /* Information concerning the compilation unit's programming
2583    language, and compiler version.  */
2584
2585 /* Fixed size portion of the DWARF compilation unit header.  */
2586 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2587   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2588
2589 /* Fixed size portion of the DWARF comdat type unit header.  */
2590 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2591   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2592    + DWARF_OFFSET_SIZE)
2593
2594 /* Fixed size portion of public names info.  */
2595 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2596
2597 /* Fixed size portion of the address range info.  */
2598 #define DWARF_ARANGES_HEADER_SIZE                                       \
2599   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2600                 DWARF2_ADDR_SIZE * 2)                                   \
2601    - DWARF_INITIAL_LENGTH_SIZE)
2602
2603 /* Size of padding portion in the address range info.  It must be
2604    aligned to twice the pointer size.  */
2605 #define DWARF_ARANGES_PAD_SIZE \
2606   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2607                 DWARF2_ADDR_SIZE * 2)                              \
2608    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2609
2610 /* Use assembler line directives if available.  */
2611 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2612 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2613 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2614 #else
2615 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2616 #endif
2617 #endif
2618
2619 /* Minimum line offset in a special line info. opcode.
2620    This value was chosen to give a reasonable range of values.  */
2621 #define DWARF_LINE_BASE  -10
2622
2623 /* First special line opcode - leave room for the standard opcodes.  */
2624 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2625
2626 /* Range of line offsets in a special line info. opcode.  */
2627 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2628
2629 /* Flag that indicates the initial value of the is_stmt_start flag.
2630    In the present implementation, we do not mark any lines as
2631    the beginning of a source statement, because that information
2632    is not made available by the GCC front-end.  */
2633 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2634
2635 /* Maximum number of operations per instruction bundle.  */
2636 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2637 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2638 #endif
2639
2640 /* This location is used by calc_die_sizes() to keep track
2641    the offset of each DIE within the .debug_info section.  */
2642 static unsigned long next_die_offset;
2643
2644 /* Record the root of the DIE's built for the current compilation unit.  */
2645 static GTY(()) dw_die_ref single_comp_unit_die;
2646
2647 /* A list of type DIEs that have been separated into comdat sections.  */
2648 static GTY(()) comdat_type_node *comdat_type_list;
2649
2650 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2651 static GTY(()) limbo_die_node *limbo_die_list;
2652
2653 /* A list of DIEs for which we may have to generate
2654    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2655 static GTY(()) limbo_die_node *deferred_asm_name;
2656
2657 /* Filenames referenced by this compilation unit.  */
2658 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2659
2660 /* A hash table of references to DIE's that describe declarations.
2661    The key is a DECL_UID() which is a unique number identifying each decl.  */
2662 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2663
2664 /* A hash table of references to DIE's that describe COMMON blocks.
2665    The key is DECL_UID() ^ die_parent.  */
2666 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2667
2668 typedef struct GTY(()) die_arg_entry_struct {
2669     dw_die_ref die;
2670     tree arg;
2671 } die_arg_entry;
2672
2673 DEF_VEC_O(die_arg_entry);
2674 DEF_VEC_ALLOC_O(die_arg_entry,gc);
2675
2676 /* Node of the variable location list.  */
2677 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2678   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2679      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2680      in mode of the EXPR_LIST node and first EXPR_LIST operand
2681      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2682      location or NULL for padding.  For larger bitsizes,
2683      mode is 0 and first operand is a CONCAT with bitsize
2684      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2685      NULL as second operand.  */
2686   rtx GTY (()) loc;
2687   const char * GTY (()) label;
2688   struct var_loc_node * GTY (()) next;
2689 };
2690
2691 /* Variable location list.  */
2692 struct GTY (()) var_loc_list_def {
2693   struct var_loc_node * GTY (()) first;
2694
2695   /* Pointer to the last but one or last element of the
2696      chained list.  If the list is empty, both first and
2697      last are NULL, if the list contains just one node
2698      or the last node certainly is not redundant, it points
2699      to the last node, otherwise points to the last but one.
2700      Do not mark it for GC because it is marked through the chain.  */
2701   struct var_loc_node * GTY ((skip ("%h"))) last;
2702
2703   /* Pointer to the last element before section switch,
2704      if NULL, either sections weren't switched or first
2705      is after section switch.  */
2706   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2707
2708   /* DECL_UID of the variable decl.  */
2709   unsigned int decl_id;
2710 };
2711 typedef struct var_loc_list_def var_loc_list;
2712
2713 /* Call argument location list.  */
2714 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2715   rtx GTY (()) call_arg_loc_note;
2716   const char * GTY (()) label;
2717   tree GTY (()) block;
2718   bool tail_call_p;
2719   rtx GTY (()) symbol_ref;
2720   struct call_arg_loc_node * GTY (()) next;
2721 };
2722
2723
2724 /* Table of decl location linked lists.  */
2725 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2726
2727 /* Head and tail of call_arg_loc chain.  */
2728 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2729 static struct call_arg_loc_node *call_arg_loc_last;
2730
2731 /* Number of call sites in the current function.  */
2732 static int call_site_count = -1;
2733 /* Number of tail call sites in the current function.  */
2734 static int tail_call_site_count = -1;
2735
2736 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2737    DIEs.  */
2738 static VEC (dw_die_ref, heap) *block_map;
2739
2740 /* A cached location list.  */
2741 struct GTY (()) cached_dw_loc_list_def {
2742   /* The DECL_UID of the decl that this entry describes.  */
2743   unsigned int decl_id;
2744
2745   /* The cached location list.  */
2746   dw_loc_list_ref loc_list;
2747 };
2748 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2749
2750 /* Table of cached location lists.  */
2751 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2752
2753 /* A pointer to the base of a list of references to DIE's that
2754    are uniquely identified by their tag, presence/absence of
2755    children DIE's, and list of attribute/value pairs.  */
2756 static GTY((length ("abbrev_die_table_allocated")))
2757   dw_die_ref *abbrev_die_table;
2758
2759 /* Number of elements currently allocated for abbrev_die_table.  */
2760 static GTY(()) unsigned abbrev_die_table_allocated;
2761
2762 /* Number of elements in type_die_table currently in use.  */
2763 static GTY(()) unsigned abbrev_die_table_in_use;
2764
2765 /* Size (in elements) of increments by which we may expand the
2766    abbrev_die_table.  */
2767 #define ABBREV_DIE_TABLE_INCREMENT 256
2768
2769 /* A global counter for generating labels for line number data.  */
2770 static unsigned int line_info_label_num;
2771
2772 /* The current table to which we should emit line number information
2773    for the current function.  This will be set up at the beginning of
2774    assembly for the function.  */
2775 static dw_line_info_table *cur_line_info_table;
2776
2777 /* The two default tables of line number info.  */
2778 static GTY(()) dw_line_info_table *text_section_line_info;
2779 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2780
2781 /* The set of all non-default tables of line number info.  */
2782 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
2783
2784 /* A flag to tell pubnames/types export if there is an info section to
2785    refer to.  */
2786 static bool info_section_emitted;
2787
2788 /* A pointer to the base of a table that contains a list of publicly
2789    accessible names.  */
2790 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
2791
2792 /* A pointer to the base of a table that contains a list of publicly
2793    accessible types.  */
2794 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
2795
2796 /* A pointer to the base of a table that contains a list of macro
2797    defines/undefines (and file start/end markers).  */
2798 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
2799
2800 /* True if .debug_macinfo or .debug_macros section is going to be
2801    emitted.  */
2802 #define have_macinfo \
2803   (debug_info_level >= DINFO_LEVEL_VERBOSE \
2804    && !VEC_empty (macinfo_entry, macinfo_table))
2805
2806 /* Array of dies for which we should generate .debug_ranges info.  */
2807 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2808
2809 /* Number of elements currently allocated for ranges_table.  */
2810 static GTY(()) unsigned ranges_table_allocated;
2811
2812 /* Number of elements in ranges_table currently in use.  */
2813 static GTY(()) unsigned ranges_table_in_use;
2814
2815 /* Array of pairs of labels referenced in ranges_table.  */
2816 static GTY ((length ("ranges_by_label_allocated")))
2817      dw_ranges_by_label_ref ranges_by_label;
2818
2819 /* Number of elements currently allocated for ranges_by_label.  */
2820 static GTY(()) unsigned ranges_by_label_allocated;
2821
2822 /* Number of elements in ranges_by_label currently in use.  */
2823 static GTY(()) unsigned ranges_by_label_in_use;
2824
2825 /* Size (in elements) of increments by which we may expand the
2826    ranges_table.  */
2827 #define RANGES_TABLE_INCREMENT 64
2828
2829 /* Whether we have location lists that need outputting */
2830 static GTY(()) bool have_location_lists;
2831
2832 /* Unique label counter.  */
2833 static GTY(()) unsigned int loclabel_num;
2834
2835 /* Unique label counter for point-of-call tables.  */
2836 static GTY(()) unsigned int poc_label_num;
2837
2838 /* Record whether the function being analyzed contains inlined functions.  */
2839 static int current_function_has_inlines;
2840
2841 /* The last file entry emitted by maybe_emit_file().  */
2842 static GTY(()) struct dwarf_file_data * last_emitted_file;
2843
2844 /* Number of internal labels generated by gen_internal_sym().  */
2845 static GTY(()) int label_num;
2846
2847 /* Cached result of previous call to lookup_filename.  */
2848 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2849
2850 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
2851
2852 /* Instances of generic types for which we need to generate debug
2853    info that describe their generic parameters and arguments. That
2854    generation needs to happen once all types are properly laid out so
2855    we do it at the end of compilation.  */
2856 static GTY(()) VEC(tree,gc) *generic_type_instances;
2857
2858 /* Offset from the "steady-state frame pointer" to the frame base,
2859    within the current function.  */
2860 static HOST_WIDE_INT frame_pointer_fb_offset;
2861 static bool frame_pointer_fb_offset_valid;
2862
2863 static VEC (dw_die_ref, heap) *base_types;
2864
2865 /* Forward declarations for functions defined in this file.  */
2866
2867 static int is_pseudo_reg (const_rtx);
2868 static tree type_main_variant (tree);
2869 static int is_tagged_type (const_tree);
2870 static const char *dwarf_tag_name (unsigned);
2871 static const char *dwarf_attr_name (unsigned);
2872 static const char *dwarf_form_name (unsigned);
2873 static tree decl_ultimate_origin (const_tree);
2874 static tree decl_class_context (tree);
2875 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2876 static inline enum dw_val_class AT_class (dw_attr_ref);
2877 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2878 static inline unsigned AT_flag (dw_attr_ref);
2879 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2880 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2881 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2882 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2883 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2884                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2885 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2886                                unsigned int, unsigned char *);
2887 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2888 static hashval_t debug_str_do_hash (const void *);
2889 static int debug_str_eq (const void *, const void *);
2890 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2891 static inline const char *AT_string (dw_attr_ref);
2892 static enum dwarf_form AT_string_form (dw_attr_ref);
2893 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2894 static void add_AT_specification (dw_die_ref, dw_die_ref);
2895 static inline dw_die_ref AT_ref (dw_attr_ref);
2896 static inline int AT_ref_external (dw_attr_ref);
2897 static inline void set_AT_ref_external (dw_attr_ref, int);
2898 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2899 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2900 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2901 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2902                              dw_loc_list_ref);
2903 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2904 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
2905 static inline rtx AT_addr (dw_attr_ref);
2906 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2907 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2908 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2909 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2910                            unsigned HOST_WIDE_INT);
2911 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2912                                unsigned long);
2913 static inline const char *AT_lbl (dw_attr_ref);
2914 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2915 static const char *get_AT_low_pc (dw_die_ref);
2916 static const char *get_AT_hi_pc (dw_die_ref);
2917 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2918 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2919 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2920 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2921 static bool is_cxx (void);
2922 static bool is_fortran (void);
2923 static bool is_ada (void);
2924 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2925 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2926 static void add_child_die (dw_die_ref, dw_die_ref);
2927 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2928 static dw_die_ref lookup_type_die (tree);
2929 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2930 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2931 static void equate_type_number_to_die (tree, dw_die_ref);
2932 static hashval_t decl_die_table_hash (const void *);
2933 static int decl_die_table_eq (const void *, const void *);
2934 static dw_die_ref lookup_decl_die (tree);
2935 static hashval_t common_block_die_table_hash (const void *);
2936 static int common_block_die_table_eq (const void *, const void *);
2937 static hashval_t decl_loc_table_hash (const void *);
2938 static int decl_loc_table_eq (const void *, const void *);
2939 static var_loc_list *lookup_decl_loc (const_tree);
2940 static void equate_decl_number_to_die (tree, dw_die_ref);
2941 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2942 static void print_spaces (FILE *);
2943 static void print_die (dw_die_ref, FILE *);
2944 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2945 static dw_die_ref pop_compile_unit (dw_die_ref);
2946 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2947 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2948 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2949 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2950 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2951 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2952 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2953                                    struct md5_ctx *, int *);
2954 struct checksum_attributes;
2955 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2956 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2957 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2958 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2959 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2960 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2961 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2962 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2963 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2964 static void compute_section_prefix (dw_die_ref);
2965 static int is_type_die (dw_die_ref);
2966 static int is_comdat_die (dw_die_ref);
2967 static int is_symbol_die (dw_die_ref);
2968 static void assign_symbol_names (dw_die_ref);
2969 static void break_out_includes (dw_die_ref);
2970 static int is_declaration_die (dw_die_ref);
2971 static int should_move_die_to_comdat (dw_die_ref);
2972 static dw_die_ref clone_as_declaration (dw_die_ref);
2973 static dw_die_ref clone_die (dw_die_ref);
2974 static dw_die_ref clone_tree (dw_die_ref);
2975 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
2976 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
2977 static void generate_skeleton_bottom_up (skeleton_chain_node *);
2978 static dw_die_ref generate_skeleton (dw_die_ref);
2979 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
2980                                                          dw_die_ref,
2981                                                          dw_die_ref);
2982 static void break_out_comdat_types (dw_die_ref);
2983 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
2984 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
2985 static void copy_decls_for_unworthy_types (dw_die_ref);
2986
2987 static hashval_t htab_cu_hash (const void *);
2988 static int htab_cu_eq (const void *, const void *);
2989 static void htab_cu_del (void *);
2990 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
2991 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
2992 static void add_sibling_attributes (dw_die_ref);
2993 static void build_abbrev_table (dw_die_ref, htab_t);
2994 static void output_location_lists (dw_die_ref);
2995 static int constant_size (unsigned HOST_WIDE_INT);
2996 static unsigned long size_of_die (dw_die_ref);
2997 static void calc_die_sizes (dw_die_ref);
2998 static void calc_base_type_die_sizes (void);
2999 static void mark_dies (dw_die_ref);
3000 static void unmark_dies (dw_die_ref);
3001 static void unmark_all_dies (dw_die_ref);
3002 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3003 static unsigned long size_of_aranges (void);
3004 static enum dwarf_form value_format (dw_attr_ref);
3005 static void output_value_format (dw_attr_ref);
3006 static void output_abbrev_section (void);
3007 static void output_die_symbol (dw_die_ref);
3008 static void output_die (dw_die_ref);
3009 static void output_compilation_unit_header (void);
3010 static void output_comp_unit (dw_die_ref, int);
3011 static void output_comdat_type_unit (comdat_type_node *);
3012 static const char *dwarf2_name (tree, int);
3013 static void add_pubname (tree, dw_die_ref);
3014 static void add_enumerator_pubname (const char *, dw_die_ref);
3015 static void add_pubname_string (const char *, dw_die_ref);
3016 static void add_pubtype (tree, dw_die_ref);
3017 static void output_pubnames (VEC (pubname_entry,gc) *);
3018 static void output_aranges (unsigned long);
3019 static unsigned int add_ranges_num (int);
3020 static unsigned int add_ranges (const_tree);
3021 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3022                                   bool *);
3023 static void output_ranges (void);
3024 static dw_line_info_table *new_line_info_table (void);
3025 static void output_line_info (void);
3026 static void output_file_names (void);
3027 static dw_die_ref base_type_die (tree);
3028 static int is_base_type (tree);
3029 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3030 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3031 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3032 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3033 static int type_is_enum (const_tree);
3034 static unsigned int dbx_reg_number (const_rtx);
3035 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3036 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3037 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3038                                                 enum var_init_status);
3039 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3040                                                      enum var_init_status);
3041 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3042                                          enum var_init_status);
3043 static int is_based_loc (const_rtx);
3044 static int resolve_one_addr (rtx *, void *);
3045 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3046                                                enum var_init_status);
3047 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3048                                         enum var_init_status);
3049 static dw_loc_list_ref loc_list_from_tree (tree, int);
3050 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3051 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3052 static tree field_type (const_tree);
3053 static unsigned int simple_type_align_in_bits (const_tree);
3054 static unsigned int simple_decl_align_in_bits (const_tree);
3055 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3056 static HOST_WIDE_INT field_byte_offset (const_tree);
3057 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3058                                          dw_loc_list_ref);
3059 static void add_data_member_location_attribute (dw_die_ref, tree);
3060 static bool add_const_value_attribute (dw_die_ref, rtx);
3061 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3062 static void insert_double (double_int, unsigned char *);
3063 static void insert_float (const_rtx, unsigned char *);
3064 static rtx rtl_for_decl_location (tree);
3065 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3066                                                    enum dwarf_attribute);
3067 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3068 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3069 static void add_name_attribute (dw_die_ref, const char *);
3070 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3071 static void add_comp_dir_attribute (dw_die_ref);
3072 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3073 static void add_subscript_info (dw_die_ref, tree, bool);
3074 static void add_byte_size_attribute (dw_die_ref, tree);
3075 static void add_bit_offset_attribute (dw_die_ref, tree);
3076 static void add_bit_size_attribute (dw_die_ref, tree);
3077 static void add_prototyped_attribute (dw_die_ref, tree);
3078 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3079 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3080 static void add_src_coords_attributes (dw_die_ref, tree);
3081 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3082 static void push_decl_scope (tree);
3083 static void pop_decl_scope (void);
3084 static dw_die_ref scope_die_for (tree, dw_die_ref);
3085 static inline int local_scope_p (dw_die_ref);
3086 static inline int class_scope_p (dw_die_ref);
3087 static inline int class_or_namespace_scope_p (dw_die_ref);
3088 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3089 static void add_calling_convention_attribute (dw_die_ref, tree);
3090 static const char *type_tag (const_tree);
3091 static tree member_declared_type (const_tree);
3092 #if 0
3093 static const char *decl_start_label (tree);
3094 #endif
3095 static void gen_array_type_die (tree, dw_die_ref);
3096 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3097 #if 0
3098 static void gen_entry_point_die (tree, dw_die_ref);
3099 #endif
3100 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3101 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3102 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3103 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3104 static void gen_formal_types_die (tree, dw_die_ref);
3105 static void gen_subprogram_die (tree, dw_die_ref);
3106 static void gen_variable_die (tree, tree, dw_die_ref);
3107 static void gen_const_die (tree, dw_die_ref);
3108 static void gen_label_die (tree, dw_die_ref);
3109 static void gen_lexical_block_die (tree, dw_die_ref, int);
3110 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3111 static void gen_field_die (tree, dw_die_ref);
3112 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3113 static dw_die_ref gen_compile_unit_die (const char *);
3114 static void gen_inheritance_die (tree, tree, dw_die_ref);
3115 static void gen_member_die (tree, dw_die_ref);
3116 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3117                                                 enum debug_info_usage);
3118 static void gen_subroutine_type_die (tree, dw_die_ref);
3119 static void gen_typedef_die (tree, dw_die_ref);
3120 static void gen_type_die (tree, dw_die_ref);
3121 static void gen_block_die (tree, dw_die_ref, int);
3122 static void decls_for_scope (tree, dw_die_ref, int);
3123 static inline int is_redundant_typedef (const_tree);
3124 static bool is_naming_typedef_decl (const_tree);
3125 static inline dw_die_ref get_context_die (tree);
3126 static void gen_namespace_die (tree, dw_die_ref);
3127 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3128 static dw_die_ref force_decl_die (tree);
3129 static dw_die_ref force_type_die (tree);
3130 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3131 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3132 static struct dwarf_file_data * lookup_filename (const char *);
3133 static void retry_incomplete_types (void);
3134 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3135 static void gen_generic_params_dies (tree);
3136 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3137 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3138 static void splice_child_die (dw_die_ref, dw_die_ref);
3139 static int file_info_cmp (const void *, const void *);
3140 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3141                                      const char *, const char *);
3142 static void output_loc_list (dw_loc_list_ref);
3143 static char *gen_internal_sym (const char *);
3144 static bool want_pubnames (void);
3145
3146 static void prune_unmark_dies (dw_die_ref);
3147 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3148 static void prune_unused_types_mark (dw_die_ref, int);
3149 static void prune_unused_types_walk (dw_die_ref);
3150 static void prune_unused_types_walk_attribs (dw_die_ref);
3151 static void prune_unused_types_prune (dw_die_ref);
3152 static void prune_unused_types (void);
3153 static int maybe_emit_file (struct dwarf_file_data *fd);
3154 static inline const char *AT_vms_delta1 (dw_attr_ref);
3155 static inline const char *AT_vms_delta2 (dw_attr_ref);
3156 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3157                                      const char *, const char *);
3158 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3159 static void gen_remaining_tmpl_value_param_die_attribute (void);
3160 static bool generic_type_p (tree);
3161 static void schedule_generic_params_dies_gen (tree t);
3162 static void gen_scheduled_generic_parms_dies (void);
3163
3164 /* Section names used to hold DWARF debugging information.  */
3165 #ifndef DEBUG_INFO_SECTION
3166 #define DEBUG_INFO_SECTION      ".debug_info"
3167 #endif
3168 #ifndef DEBUG_ABBREV_SECTION
3169 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3170 #endif
3171 #ifndef DEBUG_ARANGES_SECTION
3172 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3173 #endif
3174 #ifndef DEBUG_MACINFO_SECTION
3175 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3176 #endif
3177 #ifndef DEBUG_MACRO_SECTION
3178 #define DEBUG_MACRO_SECTION     ".debug_macro"
3179 #endif
3180 #ifndef DEBUG_LINE_SECTION
3181 #define DEBUG_LINE_SECTION      ".debug_line"
3182 #endif
3183 #ifndef DEBUG_LOC_SECTION
3184 #define DEBUG_LOC_SECTION       ".debug_loc"
3185 #endif
3186 #ifndef DEBUG_PUBNAMES_SECTION
3187 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3188 #endif
3189 #ifndef DEBUG_PUBTYPES_SECTION
3190 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3191 #endif
3192 #ifndef DEBUG_STR_SECTION
3193 #define DEBUG_STR_SECTION       ".debug_str"
3194 #endif
3195 #ifndef DEBUG_RANGES_SECTION
3196 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3197 #endif
3198
3199 /* Standard ELF section names for compiled code and data.  */
3200 #ifndef TEXT_SECTION_NAME
3201 #define TEXT_SECTION_NAME       ".text"
3202 #endif
3203
3204 /* Section flags for .debug_str section.  */
3205 #define DEBUG_STR_SECTION_FLAGS \
3206   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3207    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3208    : SECTION_DEBUG)
3209
3210 /* Labels we insert at beginning sections we can reference instead of
3211    the section names themselves.  */
3212
3213 #ifndef TEXT_SECTION_LABEL
3214 #define TEXT_SECTION_LABEL              "Ltext"
3215 #endif
3216 #ifndef COLD_TEXT_SECTION_LABEL
3217 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3218 #endif
3219 #ifndef DEBUG_LINE_SECTION_LABEL
3220 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3221 #endif
3222 #ifndef DEBUG_INFO_SECTION_LABEL
3223 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3224 #endif
3225 #ifndef DEBUG_ABBREV_SECTION_LABEL
3226 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3227 #endif
3228 #ifndef DEBUG_LOC_SECTION_LABEL
3229 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3230 #endif
3231 #ifndef DEBUG_RANGES_SECTION_LABEL
3232 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3233 #endif
3234 #ifndef DEBUG_MACINFO_SECTION_LABEL
3235 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3236 #endif
3237 #ifndef DEBUG_MACRO_SECTION_LABEL
3238 #define DEBUG_MACRO_SECTION_LABEL       "Ldebug_macro"
3239 #endif
3240
3241
3242 /* Definitions of defaults for formats and names of various special
3243    (artificial) labels which may be generated within this file (when the -g
3244    options is used and DWARF2_DEBUGGING_INFO is in effect.
3245    If necessary, these may be overridden from within the tm.h file, but
3246    typically, overriding these defaults is unnecessary.  */
3247
3248 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3249 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3250 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3251 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3252 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3253 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3254 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3255 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3256 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3257 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3258
3259 #ifndef TEXT_END_LABEL
3260 #define TEXT_END_LABEL          "Letext"
3261 #endif
3262 #ifndef COLD_END_LABEL
3263 #define COLD_END_LABEL          "Letext_cold"
3264 #endif
3265 #ifndef BLOCK_BEGIN_LABEL
3266 #define BLOCK_BEGIN_LABEL       "LBB"
3267 #endif
3268 #ifndef BLOCK_END_LABEL
3269 #define BLOCK_END_LABEL         "LBE"
3270 #endif
3271 #ifndef LINE_CODE_LABEL
3272 #define LINE_CODE_LABEL         "LM"
3273 #endif
3274
3275 \f
3276 /* Return the root of the DIE's built for the current compilation unit.  */
3277 static dw_die_ref
3278 comp_unit_die (void)
3279 {
3280   if (!single_comp_unit_die)
3281     single_comp_unit_die = gen_compile_unit_die (NULL);
3282   return single_comp_unit_die;
3283 }
3284
3285 /* We allow a language front-end to designate a function that is to be
3286    called to "demangle" any name before it is put into a DIE.  */
3287
3288 static const char *(*demangle_name_func) (const char *);
3289
3290 void
3291 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3292 {
3293   demangle_name_func = func;
3294 }
3295
3296 /* Test if rtl node points to a pseudo register.  */
3297
3298 static inline int
3299 is_pseudo_reg (const_rtx rtl)
3300 {
3301   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3302           || (GET_CODE (rtl) == SUBREG
3303               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3304 }
3305
3306 /* Return a reference to a type, with its const and volatile qualifiers
3307    removed.  */
3308
3309 static inline tree
3310 type_main_variant (tree type)
3311 {
3312   type = TYPE_MAIN_VARIANT (type);
3313
3314   /* ??? There really should be only one main variant among any group of
3315      variants of a given type (and all of the MAIN_VARIANT values for all
3316      members of the group should point to that one type) but sometimes the C
3317      front-end messes this up for array types, so we work around that bug
3318      here.  */
3319   if (TREE_CODE (type) == ARRAY_TYPE)
3320     while (type != TYPE_MAIN_VARIANT (type))
3321       type = TYPE_MAIN_VARIANT (type);
3322
3323   return type;
3324 }
3325
3326 /* Return nonzero if the given type node represents a tagged type.  */
3327
3328 static inline int
3329 is_tagged_type (const_tree type)
3330 {
3331   enum tree_code code = TREE_CODE (type);
3332
3333   return (code == RECORD_TYPE || code == UNION_TYPE
3334           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3335 }
3336
3337 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3338
3339 static void
3340 get_ref_die_offset_label (char *label, dw_die_ref ref)
3341 {
3342   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3343 }
3344
3345 /* Return die_offset of a DIE reference to a base type.  */
3346
3347 static unsigned long int
3348 get_base_type_offset (dw_die_ref ref)
3349 {
3350   if (ref->die_offset)
3351     return ref->die_offset;
3352   if (comp_unit_die ()->die_abbrev)
3353     {
3354       calc_base_type_die_sizes ();
3355       gcc_assert (ref->die_offset);
3356     }
3357   return ref->die_offset;
3358 }
3359
3360 /* Return die_offset of a DIE reference other than base type.  */
3361
3362 static unsigned long int
3363 get_ref_die_offset (dw_die_ref ref)
3364 {
3365   gcc_assert (ref->die_offset);
3366   return ref->die_offset;
3367 }
3368
3369 /* Convert a DIE tag into its string name.  */
3370
3371 static const char *
3372 dwarf_tag_name (unsigned int tag)
3373 {
3374   const char *name = get_DW_TAG_name (tag);
3375
3376   if (name != NULL)
3377     return name;
3378
3379   return "DW_TAG_<unknown>";
3380 }
3381
3382 /* Convert a DWARF attribute code into its string name.  */
3383
3384 static const char *
3385 dwarf_attr_name (unsigned int attr)
3386 {
3387   const char *name;
3388
3389   switch (attr)
3390     {
3391 #if VMS_DEBUGGING_INFO
3392     case DW_AT_HP_prologue:
3393       return "DW_AT_HP_prologue";
3394 #else
3395     case DW_AT_MIPS_loop_unroll_factor:
3396       return "DW_AT_MIPS_loop_unroll_factor";
3397 #endif
3398
3399 #if VMS_DEBUGGING_INFO
3400     case DW_AT_HP_epilogue:
3401       return "DW_AT_HP_epilogue";
3402 #else
3403     case DW_AT_MIPS_stride:
3404       return "DW_AT_MIPS_stride";
3405 #endif
3406     }
3407
3408   name = get_DW_AT_name (attr);
3409
3410   if (name != NULL)
3411     return name;
3412
3413   return "DW_AT_<unknown>";
3414 }
3415
3416 /* Convert a DWARF value form code into its string name.  */
3417
3418 static const char *
3419 dwarf_form_name (unsigned int form)
3420 {
3421   const char *name = get_DW_FORM_name (form);
3422
3423   if (name != NULL)
3424     return name;
3425
3426   return "DW_FORM_<unknown>";
3427 }
3428 \f
3429 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3430    instance of an inlined instance of a decl which is local to an inline
3431    function, so we have to trace all of the way back through the origin chain
3432    to find out what sort of node actually served as the original seed for the
3433    given block.  */
3434
3435 static tree
3436 decl_ultimate_origin (const_tree decl)
3437 {
3438   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3439     return NULL_TREE;
3440
3441   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3442      nodes in the function to point to themselves; ignore that if
3443      we're trying to output the abstract instance of this function.  */
3444   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3445     return NULL_TREE;
3446
3447   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3448      most distant ancestor, this should never happen.  */
3449   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3450
3451   return DECL_ABSTRACT_ORIGIN (decl);
3452 }
3453
3454 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3455    of a virtual function may refer to a base class, so we check the 'this'
3456    parameter.  */
3457
3458 static tree
3459 decl_class_context (tree decl)
3460 {
3461   tree context = NULL_TREE;
3462
3463   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3464     context = DECL_CONTEXT (decl);
3465   else
3466     context = TYPE_MAIN_VARIANT
3467       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3468
3469   if (context && !TYPE_P (context))
3470     context = NULL_TREE;
3471
3472   return context;
3473 }
3474 \f
3475 /* Add an attribute/value pair to a DIE.  */
3476
3477 static inline void
3478 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3479 {
3480   /* Maybe this should be an assert?  */
3481   if (die == NULL)
3482     return;
3483
3484   if (die->die_attr == NULL)
3485     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
3486   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
3487 }
3488
3489 static inline enum dw_val_class
3490 AT_class (dw_attr_ref a)
3491 {
3492   return a->dw_attr_val.val_class;
3493 }
3494
3495 /* Add a flag value attribute to a DIE.  */
3496
3497 static inline void
3498 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3499 {
3500   dw_attr_node attr;
3501
3502   attr.dw_attr = attr_kind;
3503   attr.dw_attr_val.val_class = dw_val_class_flag;
3504   attr.dw_attr_val.v.val_flag = flag;
3505   add_dwarf_attr (die, &attr);
3506 }
3507
3508 static inline unsigned
3509 AT_flag (dw_attr_ref a)
3510 {
3511   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3512   return a->dw_attr_val.v.val_flag;
3513 }
3514
3515 /* Add a signed integer attribute value to a DIE.  */
3516
3517 static inline void
3518 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3519 {
3520   dw_attr_node attr;
3521
3522   attr.dw_attr = attr_kind;
3523   attr.dw_attr_val.val_class = dw_val_class_const;
3524   attr.dw_attr_val.v.val_int = int_val;
3525   add_dwarf_attr (die, &attr);
3526 }
3527
3528 static inline HOST_WIDE_INT
3529 AT_int (dw_attr_ref a)
3530 {
3531   gcc_assert (a && AT_class (a) == dw_val_class_const);
3532   return a->dw_attr_val.v.val_int;
3533 }
3534
3535 /* Add an unsigned integer attribute value to a DIE.  */
3536
3537 static inline void
3538 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3539                  unsigned HOST_WIDE_INT unsigned_val)
3540 {
3541   dw_attr_node attr;
3542
3543   attr.dw_attr = attr_kind;
3544   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3545   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3546   add_dwarf_attr (die, &attr);
3547 }
3548
3549 static inline unsigned HOST_WIDE_INT
3550 AT_unsigned (dw_attr_ref a)
3551 {
3552   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3553   return a->dw_attr_val.v.val_unsigned;
3554 }
3555
3556 /* Add an unsigned double integer attribute value to a DIE.  */
3557
3558 static inline void
3559 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3560                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3561 {
3562   dw_attr_node attr;
3563
3564   attr.dw_attr = attr_kind;
3565   attr.dw_attr_val.val_class = dw_val_class_const_double;
3566   attr.dw_attr_val.v.val_double.high = high;
3567   attr.dw_attr_val.v.val_double.low = low;
3568   add_dwarf_attr (die, &attr);
3569 }
3570
3571 /* Add a floating point attribute value to a DIE and return it.  */
3572
3573 static inline void
3574 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3575             unsigned int length, unsigned int elt_size, unsigned char *array)
3576 {
3577   dw_attr_node attr;
3578
3579   attr.dw_attr = attr_kind;
3580   attr.dw_attr_val.val_class = dw_val_class_vec;
3581   attr.dw_attr_val.v.val_vec.length = length;
3582   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3583   attr.dw_attr_val.v.val_vec.array = array;
3584   add_dwarf_attr (die, &attr);
3585 }
3586
3587 /* Add an 8-byte data attribute value to a DIE.  */
3588
3589 static inline void
3590 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3591               unsigned char data8[8])
3592 {
3593   dw_attr_node attr;
3594
3595   attr.dw_attr = attr_kind;
3596   attr.dw_attr_val.val_class = dw_val_class_data8;
3597   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3598   add_dwarf_attr (die, &attr);
3599 }
3600
3601 /* Hash and equality functions for debug_str_hash.  */
3602
3603 static hashval_t
3604 debug_str_do_hash (const void *x)
3605 {
3606   return htab_hash_string (((const struct indirect_string_node *)x)->str);
3607 }
3608
3609 static int
3610 debug_str_eq (const void *x1, const void *x2)
3611 {
3612   return strcmp ((((const struct indirect_string_node *)x1)->str),
3613                  (const char *)x2) == 0;
3614 }
3615
3616 /* Add STR to the indirect string hash table.  */
3617
3618 static struct indirect_string_node *
3619 find_AT_string (const char *str)
3620 {
3621   struct indirect_string_node *node;
3622   void **slot;
3623
3624   if (! debug_str_hash)
3625     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3626                                       debug_str_eq, NULL);
3627
3628   slot = htab_find_slot_with_hash (debug_str_hash, str,
3629                                    htab_hash_string (str), INSERT);
3630   if (*slot == NULL)
3631     {
3632       node = ggc_alloc_cleared_indirect_string_node ();
3633       node->str = ggc_strdup (str);
3634       *slot = node;
3635     }
3636   else
3637     node = (struct indirect_string_node *) *slot;
3638
3639   node->refcount++;
3640   return node;
3641 }
3642
3643 /* Add a string attribute value to a DIE.  */
3644
3645 static inline void
3646 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3647 {
3648   dw_attr_node attr;
3649   struct indirect_string_node *node;
3650
3651   node = find_AT_string (str);
3652
3653   attr.dw_attr = attr_kind;
3654   attr.dw_attr_val.val_class = dw_val_class_str;
3655   attr.dw_attr_val.v.val_str = node;
3656   add_dwarf_attr (die, &attr);
3657 }
3658
3659 static inline const char *
3660 AT_string (dw_attr_ref a)
3661 {
3662   gcc_assert (a && AT_class (a) == dw_val_class_str);
3663   return a->dw_attr_val.v.val_str->str;
3664 }
3665
3666 /* Find out whether a string should be output inline in DIE
3667    or out-of-line in .debug_str section.  */
3668
3669 static enum dwarf_form
3670 AT_string_form (dw_attr_ref a)
3671 {
3672   struct indirect_string_node *node;
3673   unsigned int len;
3674   char label[32];
3675
3676   gcc_assert (a && AT_class (a) == dw_val_class_str);
3677
3678   node = a->dw_attr_val.v.val_str;
3679   if (node->form)
3680     return node->form;
3681
3682   len = strlen (node->str) + 1;
3683
3684   /* If the string is shorter or equal to the size of the reference, it is
3685      always better to put it inline.  */
3686   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3687     return node->form = DW_FORM_string;
3688
3689   /* If we cannot expect the linker to merge strings in .debug_str
3690      section, only put it into .debug_str if it is worth even in this
3691      single module.  */
3692   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3693       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3694       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3695     return node->form = DW_FORM_string;
3696
3697   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3698   ++dw2_string_counter;
3699   node->label = xstrdup (label);
3700
3701   return node->form = DW_FORM_strp;
3702 }
3703
3704 /* Add a DIE reference attribute value to a DIE.  */
3705
3706 static inline void
3707 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3708 {
3709   dw_attr_node attr;
3710
3711 #ifdef ENABLE_CHECKING
3712   gcc_assert (targ_die != NULL);
3713 #else
3714   /* With LTO we can end up trying to reference something we didn't create
3715      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
3716   if (targ_die == NULL)
3717     return;
3718 #endif
3719
3720   attr.dw_attr = attr_kind;
3721   attr.dw_attr_val.val_class = dw_val_class_die_ref;
3722   attr.dw_attr_val.v.val_die_ref.die = targ_die;
3723   attr.dw_attr_val.v.val_die_ref.external = 0;
3724   add_dwarf_attr (die, &attr);
3725 }
3726
3727 /* Change DIE reference REF to point to NEW_DIE instead.  */
3728
3729 static inline void
3730 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3731 {
3732   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3733   ref->dw_attr_val.v.val_die_ref.die = new_die;
3734   ref->dw_attr_val.v.val_die_ref.external = 0;
3735 }
3736
3737 /* Add an AT_specification attribute to a DIE, and also make the back
3738    pointer from the specification to the definition.  */
3739
3740 static inline void
3741 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3742 {
3743   add_AT_die_ref (die, DW_AT_specification, targ_die);
3744   gcc_assert (!targ_die->die_definition);
3745   targ_die->die_definition = die;
3746 }
3747
3748 static inline dw_die_ref
3749 AT_ref (dw_attr_ref a)
3750 {
3751   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3752   return a->dw_attr_val.v.val_die_ref.die;
3753 }
3754
3755 static inline int
3756 AT_ref_external (dw_attr_ref a)
3757 {
3758   if (a && AT_class (a) == dw_val_class_die_ref)
3759     return a->dw_attr_val.v.val_die_ref.external;
3760
3761   return 0;
3762 }
3763
3764 static inline void
3765 set_AT_ref_external (dw_attr_ref a, int i)
3766 {
3767   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3768   a->dw_attr_val.v.val_die_ref.external = i;
3769 }
3770
3771 /* Add an FDE reference attribute value to a DIE.  */
3772
3773 static inline void
3774 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
3775 {
3776   dw_attr_node attr;
3777
3778   attr.dw_attr = attr_kind;
3779   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
3780   attr.dw_attr_val.v.val_fde_index = targ_fde;
3781   add_dwarf_attr (die, &attr);
3782 }
3783
3784 /* Add a location description attribute value to a DIE.  */
3785
3786 static inline void
3787 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
3788 {
3789   dw_attr_node attr;
3790
3791   attr.dw_attr = attr_kind;
3792   attr.dw_attr_val.val_class = dw_val_class_loc;
3793   attr.dw_attr_val.v.val_loc = loc;
3794   add_dwarf_attr (die, &attr);
3795 }
3796
3797 static inline dw_loc_descr_ref
3798 AT_loc (dw_attr_ref a)
3799 {
3800   gcc_assert (a && AT_class (a) == dw_val_class_loc);
3801   return a->dw_attr_val.v.val_loc;
3802 }
3803
3804 static inline void
3805 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
3806 {
3807   dw_attr_node attr;
3808
3809   attr.dw_attr = attr_kind;
3810   attr.dw_attr_val.val_class = dw_val_class_loc_list;
3811   attr.dw_attr_val.v.val_loc_list = loc_list;
3812   add_dwarf_attr (die, &attr);
3813   have_location_lists = true;
3814 }
3815
3816 static inline dw_loc_list_ref
3817 AT_loc_list (dw_attr_ref a)
3818 {
3819   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
3820   return a->dw_attr_val.v.val_loc_list;
3821 }
3822
3823 static inline dw_loc_list_ref *
3824 AT_loc_list_ptr (dw_attr_ref a)
3825 {
3826   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
3827   return &a->dw_attr_val.v.val_loc_list;
3828 }
3829
3830 /* Add an address constant attribute value to a DIE.  */
3831
3832 static inline void
3833 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
3834 {
3835   dw_attr_node attr;
3836
3837   attr.dw_attr = attr_kind;
3838   attr.dw_attr_val.val_class = dw_val_class_addr;
3839   attr.dw_attr_val.v.val_addr = addr;
3840   add_dwarf_attr (die, &attr);
3841 }
3842
3843 /* Get the RTX from to an address DIE attribute.  */
3844
3845 static inline rtx
3846 AT_addr (dw_attr_ref a)
3847 {
3848   gcc_assert (a && AT_class (a) == dw_val_class_addr);
3849   return a->dw_attr_val.v.val_addr;
3850 }
3851
3852 /* Add a file attribute value to a DIE.  */
3853
3854 static inline void
3855 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
3856              struct dwarf_file_data *fd)
3857 {
3858   dw_attr_node attr;
3859
3860   attr.dw_attr = attr_kind;
3861   attr.dw_attr_val.val_class = dw_val_class_file;
3862   attr.dw_attr_val.v.val_file = fd;
3863   add_dwarf_attr (die, &attr);
3864 }
3865
3866 /* Get the dwarf_file_data from a file DIE attribute.  */
3867
3868 static inline struct dwarf_file_data *
3869 AT_file (dw_attr_ref a)
3870 {
3871   gcc_assert (a && AT_class (a) == dw_val_class_file);
3872   return a->dw_attr_val.v.val_file;
3873 }
3874
3875 /* Add a vms delta attribute value to a DIE.  */
3876
3877 static inline void
3878 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
3879                   const char *lbl1, const char *lbl2)
3880 {
3881   dw_attr_node attr;
3882
3883   attr.dw_attr = attr_kind;
3884   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
3885   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
3886   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
3887   add_dwarf_attr (die, &attr);
3888 }
3889
3890 /* Add a label identifier attribute value to a DIE.  */
3891
3892 static inline void
3893 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
3894 {
3895   dw_attr_node attr;
3896
3897   attr.dw_attr = attr_kind;
3898   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3899   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
3900   add_dwarf_attr (die, &attr);
3901 }
3902
3903 /* Add a section offset attribute value to a DIE, an offset into the
3904    debug_line section.  */
3905
3906 static inline void
3907 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
3908                 const char *label)
3909 {
3910   dw_attr_node attr;
3911
3912   attr.dw_attr = attr_kind;
3913   attr.dw_attr_val.val_class = dw_val_class_lineptr;
3914   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
3915   add_dwarf_attr (die, &attr);
3916 }
3917
3918 /* Add a section offset attribute value to a DIE, an offset into the
3919    debug_macinfo section.  */
3920
3921 static inline void
3922 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
3923                const char *label)
3924 {
3925   dw_attr_node attr;
3926
3927   attr.dw_attr = attr_kind;
3928   attr.dw_attr_val.val_class = dw_val_class_macptr;
3929   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
3930   add_dwarf_attr (die, &attr);
3931 }
3932
3933 /* Add an offset attribute value to a DIE.  */
3934
3935 static inline void
3936 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
3937                unsigned HOST_WIDE_INT offset)
3938 {
3939   dw_attr_node attr;
3940
3941   attr.dw_attr = attr_kind;
3942   attr.dw_attr_val.val_class = dw_val_class_offset;
3943   attr.dw_attr_val.v.val_offset = offset;
3944   add_dwarf_attr (die, &attr);
3945 }
3946
3947 /* Add an range_list attribute value to a DIE.  */
3948
3949 static void
3950 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
3951                    long unsigned int offset)
3952 {
3953   dw_attr_node attr;
3954
3955   attr.dw_attr = attr_kind;
3956   attr.dw_attr_val.val_class = dw_val_class_range_list;
3957   attr.dw_attr_val.v.val_offset = offset;
3958   add_dwarf_attr (die, &attr);
3959 }
3960
3961 /* Return the start label of a delta attribute.  */
3962
3963 static inline const char *
3964 AT_vms_delta1 (dw_attr_ref a)
3965 {
3966   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
3967   return a->dw_attr_val.v.val_vms_delta.lbl1;
3968 }
3969
3970 /* Return the end label of a delta attribute.  */
3971
3972 static inline const char *
3973 AT_vms_delta2 (dw_attr_ref a)
3974 {
3975   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
3976   return a->dw_attr_val.v.val_vms_delta.lbl2;
3977 }
3978
3979 static inline const char *
3980 AT_lbl (dw_attr_ref a)
3981 {
3982   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
3983                     || AT_class (a) == dw_val_class_lineptr
3984                     || AT_class (a) == dw_val_class_macptr));
3985   return a->dw_attr_val.v.val_lbl_id;
3986 }
3987
3988 /* Get the attribute of type attr_kind.  */
3989
3990 static dw_attr_ref
3991 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
3992 {
3993   dw_attr_ref a;
3994   unsigned ix;
3995   dw_die_ref spec = NULL;
3996
3997   if (! die)
3998     return NULL;
3999
4000   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4001     if (a->dw_attr == attr_kind)
4002       return a;
4003     else if (a->dw_attr == DW_AT_specification
4004              || a->dw_attr == DW_AT_abstract_origin)
4005       spec = AT_ref (a);
4006
4007   if (spec)
4008     return get_AT (spec, attr_kind);
4009
4010   return NULL;
4011 }
4012
4013 /* Returns the parent of the declaration of DIE.  */
4014
4015 static dw_die_ref
4016 get_die_parent (dw_die_ref die)
4017 {
4018   dw_die_ref t;
4019
4020   if (!die)
4021     return NULL;
4022
4023   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4024       || (t = get_AT_ref (die, DW_AT_specification)))
4025     die = t;
4026
4027   return die->die_parent;
4028 }
4029
4030 /* Return the "low pc" attribute value, typically associated with a subprogram
4031    DIE.  Return null if the "low pc" attribute is either not present, or if it
4032    cannot be represented as an assembler label identifier.  */
4033
4034 static inline const char *
4035 get_AT_low_pc (dw_die_ref die)
4036 {
4037   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4038
4039   return a ? AT_lbl (a) : NULL;
4040 }
4041
4042 /* Return the "high pc" attribute value, typically associated with a subprogram
4043    DIE.  Return null if the "high pc" attribute is either not present, or if it
4044    cannot be represented as an assembler label identifier.  */
4045
4046 static inline const char *
4047 get_AT_hi_pc (dw_die_ref die)
4048 {
4049   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4050
4051   return a ? AT_lbl (a) : NULL;
4052 }
4053
4054 /* Return the value of the string attribute designated by ATTR_KIND, or
4055    NULL if it is not present.  */
4056
4057 static inline const char *
4058 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4059 {
4060   dw_attr_ref a = get_AT (die, attr_kind);
4061
4062   return a ? AT_string (a) : NULL;
4063 }
4064
4065 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4066    if it is not present.  */
4067
4068 static inline int
4069 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4070 {
4071   dw_attr_ref a = get_AT (die, attr_kind);
4072
4073   return a ? AT_flag (a) : 0;
4074 }
4075
4076 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4077    if it is not present.  */
4078
4079 static inline unsigned
4080 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4081 {
4082   dw_attr_ref a = get_AT (die, attr_kind);
4083
4084   return a ? AT_unsigned (a) : 0;
4085 }
4086
4087 static inline dw_die_ref
4088 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4089 {
4090   dw_attr_ref a = get_AT (die, attr_kind);
4091
4092   return a ? AT_ref (a) : NULL;
4093 }
4094
4095 static inline struct dwarf_file_data *
4096 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4097 {
4098   dw_attr_ref a = get_AT (die, attr_kind);
4099
4100   return a ? AT_file (a) : NULL;
4101 }
4102
4103 /* Return TRUE if the language is C++.  */
4104
4105 static inline bool
4106 is_cxx (void)
4107 {
4108   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4109
4110   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4111 }
4112
4113 /* Return TRUE if the language is Fortran.  */
4114
4115 static inline bool
4116 is_fortran (void)
4117 {
4118   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4119
4120   return (lang == DW_LANG_Fortran77
4121           || lang == DW_LANG_Fortran90
4122           || lang == DW_LANG_Fortran95);
4123 }
4124
4125 /* Return TRUE if the language is Ada.  */
4126
4127 static inline bool
4128 is_ada (void)
4129 {
4130   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4131
4132   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4133 }
4134
4135 /* Remove the specified attribute if present.  */
4136
4137 static void
4138 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4139 {
4140   dw_attr_ref a;
4141   unsigned ix;
4142
4143   if (! die)
4144     return;
4145
4146   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4147     if (a->dw_attr == attr_kind)
4148       {
4149         if (AT_class (a) == dw_val_class_str)
4150           if (a->dw_attr_val.v.val_str->refcount)
4151             a->dw_attr_val.v.val_str->refcount--;
4152
4153         /* VEC_ordered_remove should help reduce the number of abbrevs
4154            that are needed.  */
4155         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4156         return;
4157       }
4158 }
4159
4160 /* Remove CHILD from its parent.  PREV must have the property that
4161    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4162
4163 static void
4164 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4165 {
4166   gcc_assert (child->die_parent == prev->die_parent);
4167   gcc_assert (prev->die_sib == child);
4168   if (prev == child)
4169     {
4170       gcc_assert (child->die_parent->die_child == child);
4171       prev = NULL;
4172     }
4173   else
4174     prev->die_sib = child->die_sib;
4175   if (child->die_parent->die_child == child)
4176     child->die_parent->die_child = prev;
4177 }
4178
4179 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4180    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4181
4182 static void
4183 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4184 {
4185   dw_die_ref parent = old_child->die_parent;
4186
4187   gcc_assert (parent == prev->die_parent);
4188   gcc_assert (prev->die_sib == old_child);
4189
4190   new_child->die_parent = parent;
4191   if (prev == old_child)
4192     {
4193       gcc_assert (parent->die_child == old_child);
4194       new_child->die_sib = new_child;
4195     }
4196   else
4197     {
4198       prev->die_sib = new_child;
4199       new_child->die_sib = old_child->die_sib;
4200     }
4201   if (old_child->die_parent->die_child == old_child)
4202     old_child->die_parent->die_child = new_child;
4203 }
4204
4205 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4206
4207 static void
4208 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4209 {
4210   dw_die_ref c;
4211   new_parent->die_child = old_parent->die_child;
4212   old_parent->die_child = NULL;
4213   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4214 }
4215
4216 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4217    matches TAG.  */
4218
4219 static void
4220 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4221 {
4222   dw_die_ref c;
4223
4224   c = die->die_child;
4225   if (c) do {
4226     dw_die_ref prev = c;
4227     c = c->die_sib;
4228     while (c->die_tag == tag)
4229       {
4230         remove_child_with_prev (c, prev);
4231         /* Might have removed every child.  */
4232         if (c == c->die_sib)
4233           return;
4234         c = c->die_sib;
4235       }
4236   } while (c != die->die_child);
4237 }
4238
4239 /* Add a CHILD_DIE as the last child of DIE.  */
4240
4241 static void
4242 add_child_die (dw_die_ref die, dw_die_ref child_die)
4243 {
4244   /* FIXME this should probably be an assert.  */
4245   if (! die || ! child_die)
4246     return;
4247   gcc_assert (die != child_die);
4248
4249   child_die->die_parent = die;
4250   if (die->die_child)
4251     {
4252       child_die->die_sib = die->die_child->die_sib;
4253       die->die_child->die_sib = child_die;
4254     }
4255   else
4256     child_die->die_sib = child_die;
4257   die->die_child = child_die;
4258 }
4259
4260 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4261    is the specification, to the end of PARENT's list of children.
4262    This is done by removing and re-adding it.  */
4263
4264 static void
4265 splice_child_die (dw_die_ref parent, dw_die_ref child)
4266 {
4267   dw_die_ref p;
4268
4269   /* We want the declaration DIE from inside the class, not the
4270      specification DIE at toplevel.  */
4271   if (child->die_parent != parent)
4272     {
4273       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4274
4275       if (tmp)
4276         child = tmp;
4277     }
4278
4279   gcc_assert (child->die_parent == parent
4280               || (child->die_parent
4281                   == get_AT_ref (parent, DW_AT_specification)));
4282
4283   for (p = child->die_parent->die_child; ; p = p->die_sib)
4284     if (p->die_sib == child)
4285       {
4286         remove_child_with_prev (child, p);
4287         break;
4288       }
4289
4290   add_child_die (parent, child);
4291 }
4292
4293 /* Return a pointer to a newly created DIE node.  */
4294
4295 static inline dw_die_ref
4296 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4297 {
4298   dw_die_ref die = ggc_alloc_cleared_die_node ();
4299
4300   die->die_tag = tag_value;
4301
4302   if (parent_die != NULL)
4303     add_child_die (parent_die, die);
4304   else
4305     {
4306       limbo_die_node *limbo_node;
4307
4308       limbo_node = ggc_alloc_cleared_limbo_die_node ();
4309       limbo_node->die = die;
4310       limbo_node->created_for = t;
4311       limbo_node->next = limbo_die_list;
4312       limbo_die_list = limbo_node;
4313     }
4314
4315   return die;
4316 }
4317
4318 /* Return the DIE associated with the given type specifier.  */
4319
4320 static inline dw_die_ref
4321 lookup_type_die (tree type)
4322 {
4323   return TYPE_SYMTAB_DIE (type);
4324 }
4325
4326 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4327    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4328    anonymous type instead the one of the naming typedef.  */
4329
4330 static inline dw_die_ref
4331 strip_naming_typedef (tree type, dw_die_ref type_die)
4332 {
4333   if (type
4334       && TREE_CODE (type) == RECORD_TYPE
4335       && type_die
4336       && type_die->die_tag == DW_TAG_typedef
4337       && is_naming_typedef_decl (TYPE_NAME (type)))
4338     type_die = get_AT_ref (type_die, DW_AT_type);
4339   return type_die;
4340 }
4341
4342 /* Like lookup_type_die, but if type is an anonymous type named by a
4343    typedef[1], return the DIE of the anonymous type instead the one of
4344    the naming typedef.  This is because in gen_typedef_die, we did
4345    equate the anonymous struct named by the typedef with the DIE of
4346    the naming typedef. So by default, lookup_type_die on an anonymous
4347    struct yields the DIE of the naming typedef.
4348
4349    [1]: Read the comment of is_naming_typedef_decl to learn about what
4350    a naming typedef is.  */
4351
4352 static inline dw_die_ref
4353 lookup_type_die_strip_naming_typedef (tree type)
4354 {
4355   dw_die_ref die = lookup_type_die (type);
4356   return strip_naming_typedef (type, die);
4357 }
4358
4359 /* Equate a DIE to a given type specifier.  */
4360
4361 static inline void
4362 equate_type_number_to_die (tree type, dw_die_ref type_die)
4363 {
4364   TYPE_SYMTAB_DIE (type) = type_die;
4365 }
4366
4367 /* Returns a hash value for X (which really is a die_struct).  */
4368
4369 static hashval_t
4370 decl_die_table_hash (const void *x)
4371 {
4372   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4373 }
4374
4375 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
4376
4377 static int
4378 decl_die_table_eq (const void *x, const void *y)
4379 {
4380   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4381 }
4382
4383 /* Return the DIE associated with a given declaration.  */
4384
4385 static inline dw_die_ref
4386 lookup_decl_die (tree decl)
4387 {
4388   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4389 }
4390
4391 /* Returns a hash value for X (which really is a var_loc_list).  */
4392
4393 static hashval_t
4394 decl_loc_table_hash (const void *x)
4395 {
4396   return (hashval_t) ((const var_loc_list *) x)->decl_id;
4397 }
4398
4399 /* Return nonzero if decl_id of var_loc_list X is the same as
4400    UID of decl *Y.  */
4401
4402 static int
4403 decl_loc_table_eq (const void *x, const void *y)
4404 {
4405   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4406 }
4407
4408 /* Return the var_loc list associated with a given declaration.  */
4409
4410 static inline var_loc_list *
4411 lookup_decl_loc (const_tree decl)
4412 {
4413   if (!decl_loc_table)
4414     return NULL;
4415   return (var_loc_list *)
4416     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4417 }
4418
4419 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
4420
4421 static hashval_t
4422 cached_dw_loc_list_table_hash (const void *x)
4423 {
4424   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4425 }
4426
4427 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4428    UID of decl *Y.  */
4429
4430 static int
4431 cached_dw_loc_list_table_eq (const void *x, const void *y)
4432 {
4433   return (((const cached_dw_loc_list *) x)->decl_id
4434           == DECL_UID ((const_tree) y));
4435 }
4436
4437 /* Equate a DIE to a particular declaration.  */
4438
4439 static void
4440 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4441 {
4442   unsigned int decl_id = DECL_UID (decl);
4443   void **slot;
4444
4445   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4446   *slot = decl_die;
4447   decl_die->decl_id = decl_id;
4448 }
4449
4450 /* Return how many bits covers PIECE EXPR_LIST.  */
4451
4452 static int
4453 decl_piece_bitsize (rtx piece)
4454 {
4455   int ret = (int) GET_MODE (piece);
4456   if (ret)
4457     return ret;
4458   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4459               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4460   return INTVAL (XEXP (XEXP (piece, 0), 0));
4461 }
4462
4463 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
4464
4465 static rtx *
4466 decl_piece_varloc_ptr (rtx piece)
4467 {
4468   if ((int) GET_MODE (piece))
4469     return &XEXP (piece, 0);
4470   else
4471     return &XEXP (XEXP (piece, 0), 1);
4472 }
4473
4474 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4475    Next is the chain of following piece nodes.  */
4476
4477 static rtx
4478 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4479 {
4480   if (bitsize <= (int) MAX_MACHINE_MODE)
4481     return alloc_EXPR_LIST (bitsize, loc_note, next);
4482   else
4483     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4484                                                GEN_INT (bitsize),
4485                                                loc_note), next);
4486 }
4487
4488 /* Return rtx that should be stored into loc field for
4489    LOC_NOTE and BITPOS/BITSIZE.  */
4490
4491 static rtx
4492 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4493                       HOST_WIDE_INT bitsize)
4494 {
4495   if (bitsize != -1)
4496     {
4497       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4498       if (bitpos != 0)
4499         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4500     }
4501   return loc_note;
4502 }
4503
4504 /* This function either modifies location piece list *DEST in
4505    place (if SRC and INNER is NULL), or copies location piece list
4506    *SRC to *DEST while modifying it.  Location BITPOS is modified
4507    to contain LOC_NOTE, any pieces overlapping it are removed resp.
4508    not copied and if needed some padding around it is added.
4509    When modifying in place, DEST should point to EXPR_LIST where
4510    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4511    to the start of the whole list and INNER points to the EXPR_LIST
4512    where earlier pieces cover PIECE_BITPOS bits.  */
4513
4514 static void
4515 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4516                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4517                    HOST_WIDE_INT bitsize, rtx loc_note)
4518 {
4519   int diff;
4520   bool copy = inner != NULL;
4521
4522   if (copy)
4523     {
4524       /* First copy all nodes preceding the current bitpos.  */
4525       while (src != inner)
4526         {
4527           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4528                                    decl_piece_bitsize (*src), NULL_RTX);
4529           dest = &XEXP (*dest, 1);
4530           src = &XEXP (*src, 1);
4531         }
4532     }
4533   /* Add padding if needed.  */
4534   if (bitpos != piece_bitpos)
4535     {
4536       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4537                                copy ? NULL_RTX : *dest);
4538       dest = &XEXP (*dest, 1);
4539     }
4540   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4541     {
4542       gcc_assert (!copy);
4543       /* A piece with correct bitpos and bitsize already exist,
4544          just update the location for it and return.  */
4545       *decl_piece_varloc_ptr (*dest) = loc_note;
4546       return;
4547     }
4548   /* Add the piece that changed.  */
4549   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4550   dest = &XEXP (*dest, 1);
4551   /* Skip over pieces that overlap it.  */
4552   diff = bitpos - piece_bitpos + bitsize;
4553   if (!copy)
4554     src = dest;
4555   while (diff > 0 && *src)
4556     {
4557       rtx piece = *src;
4558       diff -= decl_piece_bitsize (piece);
4559       if (copy)
4560         src = &XEXP (piece, 1);
4561       else
4562         {
4563           *src = XEXP (piece, 1);
4564           free_EXPR_LIST_node (piece);
4565         }
4566     }
4567   /* Add padding if needed.  */
4568   if (diff < 0 && *src)
4569     {
4570       if (!copy)
4571         dest = src;
4572       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4573       dest = &XEXP (*dest, 1);
4574     }
4575   if (!copy)
4576     return;
4577   /* Finally copy all nodes following it.  */
4578   while (*src)
4579     {
4580       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4581                                decl_piece_bitsize (*src), NULL_RTX);
4582       dest = &XEXP (*dest, 1);
4583       src = &XEXP (*src, 1);
4584     }
4585 }
4586
4587 /* Add a variable location node to the linked list for DECL.  */
4588
4589 static struct var_loc_node *
4590 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
4591 {
4592   unsigned int decl_id;
4593   var_loc_list *temp;
4594   void **slot;
4595   struct var_loc_node *loc = NULL;
4596   HOST_WIDE_INT bitsize = -1, bitpos = -1;
4597
4598   if (DECL_DEBUG_EXPR_IS_FROM (decl))
4599     {
4600       tree realdecl = DECL_DEBUG_EXPR (decl);
4601       if (realdecl && handled_component_p (realdecl))
4602         {
4603           HOST_WIDE_INT maxsize;
4604           tree innerdecl;
4605           innerdecl
4606             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
4607           if (!DECL_P (innerdecl)
4608               || DECL_IGNORED_P (innerdecl)
4609               || TREE_STATIC (innerdecl)
4610               || bitsize <= 0
4611               || bitpos + bitsize > 256
4612               || bitsize != maxsize)
4613             return NULL;
4614           decl = innerdecl;
4615         }
4616     }
4617
4618   decl_id = DECL_UID (decl);
4619   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
4620   if (*slot == NULL)
4621     {
4622       temp = ggc_alloc_cleared_var_loc_list ();
4623       temp->decl_id = decl_id;
4624       *slot = temp;
4625     }
4626   else
4627     temp = (var_loc_list *) *slot;
4628
4629   /* For PARM_DECLs try to keep around the original incoming value,
4630      even if that means we'll emit a zero-range .debug_loc entry.  */
4631   if (temp->last
4632       && temp->first == temp->last
4633       && TREE_CODE (decl) == PARM_DECL
4634       && GET_CODE (temp->first->loc) == NOTE
4635       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
4636       && DECL_INCOMING_RTL (decl)
4637       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
4638       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
4639          == GET_CODE (DECL_INCOMING_RTL (decl))
4640       && prev_real_insn (temp->first->loc) == NULL_RTX
4641       && (bitsize != -1
4642           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
4643                            NOTE_VAR_LOCATION_LOC (loc_note))
4644           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
4645               != NOTE_VAR_LOCATION_STATUS (loc_note))))
4646     {
4647       loc = ggc_alloc_cleared_var_loc_node ();
4648       temp->first->next = loc;
4649       temp->last = loc;
4650       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4651     }
4652   else if (temp->last)
4653     {
4654       struct var_loc_node *last = temp->last, *unused = NULL;
4655       rtx *piece_loc = NULL, last_loc_note;
4656       int piece_bitpos = 0;
4657       if (last->next)
4658         {
4659           last = last->next;
4660           gcc_assert (last->next == NULL);
4661         }
4662       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
4663         {
4664           piece_loc = &last->loc;
4665           do
4666             {
4667               int cur_bitsize = decl_piece_bitsize (*piece_loc);
4668               if (piece_bitpos + cur_bitsize > bitpos)
4669                 break;
4670               piece_bitpos += cur_bitsize;
4671               piece_loc = &XEXP (*piece_loc, 1);
4672             }
4673           while (*piece_loc);
4674         }
4675       /* TEMP->LAST here is either pointer to the last but one or
4676          last element in the chained list, LAST is pointer to the
4677          last element.  */
4678       if (label && strcmp (last->label, label) == 0)
4679         {
4680           /* For SRA optimized variables if there weren't any real
4681              insns since last note, just modify the last node.  */
4682           if (piece_loc != NULL)
4683             {
4684               adjust_piece_list (piece_loc, NULL, NULL,
4685                                  bitpos, piece_bitpos, bitsize, loc_note);
4686               return NULL;
4687             }
4688           /* If the last note doesn't cover any instructions, remove it.  */
4689           if (temp->last != last)
4690             {
4691               temp->last->next = NULL;
4692               unused = last;
4693               last = temp->last;
4694               gcc_assert (strcmp (last->label, label) != 0);
4695             }
4696           else
4697             {
4698               gcc_assert (temp->first == temp->last
4699                           || (temp->first->next == temp->last
4700                               && TREE_CODE (decl) == PARM_DECL));
4701               memset (temp->last, '\0', sizeof (*temp->last));
4702               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
4703               return temp->last;
4704             }
4705         }
4706       if (bitsize == -1 && NOTE_P (last->loc))
4707         last_loc_note = last->loc;
4708       else if (piece_loc != NULL
4709                && *piece_loc != NULL_RTX
4710                && piece_bitpos == bitpos
4711                && decl_piece_bitsize (*piece_loc) == bitsize)
4712         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
4713       else
4714         last_loc_note = NULL_RTX;
4715       /* If the current location is the same as the end of the list,
4716          and either both or neither of the locations is uninitialized,
4717          we have nothing to do.  */
4718       if (last_loc_note == NULL_RTX
4719           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
4720                             NOTE_VAR_LOCATION_LOC (loc_note)))
4721           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
4722                != NOTE_VAR_LOCATION_STATUS (loc_note))
4723               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
4724                    == VAR_INIT_STATUS_UNINITIALIZED)
4725                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
4726                       == VAR_INIT_STATUS_UNINITIALIZED))))
4727         {
4728           /* Add LOC to the end of list and update LAST.  If the last
4729              element of the list has been removed above, reuse its
4730              memory for the new node, otherwise allocate a new one.  */
4731           if (unused)
4732             {
4733               loc = unused;
4734               memset (loc, '\0', sizeof (*loc));
4735             }
4736           else
4737             loc = ggc_alloc_cleared_var_loc_node ();
4738           if (bitsize == -1 || piece_loc == NULL)
4739             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4740           else
4741             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
4742                                bitpos, piece_bitpos, bitsize, loc_note);
4743           last->next = loc;
4744           /* Ensure TEMP->LAST will point either to the new last but one
4745              element of the chain, or to the last element in it.  */
4746           if (last != temp->last)
4747             temp->last = last;
4748         }
4749       else if (unused)
4750         ggc_free (unused);
4751     }
4752   else
4753     {
4754       loc = ggc_alloc_cleared_var_loc_node ();
4755       temp->first = loc;
4756       temp->last = loc;
4757       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
4758     }
4759   return loc;
4760 }
4761 \f
4762 /* Keep track of the number of spaces used to indent the
4763    output of the debugging routines that print the structure of
4764    the DIE internal representation.  */
4765 static int print_indent;
4766
4767 /* Indent the line the number of spaces given by print_indent.  */
4768
4769 static inline void
4770 print_spaces (FILE *outfile)
4771 {
4772   fprintf (outfile, "%*s", print_indent, "");
4773 }
4774
4775 /* Print a type signature in hex.  */
4776
4777 static inline void
4778 print_signature (FILE *outfile, char *sig)
4779 {
4780   int i;
4781
4782   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
4783     fprintf (outfile, "%02x", sig[i] & 0xff);
4784 }
4785
4786 /* Print the information associated with a given DIE, and its children.
4787    This routine is a debugging aid only.  */
4788
4789 static void
4790 print_die (dw_die_ref die, FILE *outfile)
4791 {
4792   dw_attr_ref a;
4793   dw_die_ref c;
4794   unsigned ix;
4795
4796   print_spaces (outfile);
4797   fprintf (outfile, "DIE %4ld: %s (%p)\n",
4798            die->die_offset, dwarf_tag_name (die->die_tag),
4799            (void*) die);
4800   print_spaces (outfile);
4801   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
4802   fprintf (outfile, " offset: %ld", die->die_offset);
4803   fprintf (outfile, " mark: %d\n", die->die_mark);
4804
4805   if (die->comdat_type_p)
4806     {
4807       print_spaces (outfile);
4808       fprintf (outfile, "  signature: ");
4809       print_signature (outfile, die->die_id.die_type_node->signature);
4810       fprintf (outfile, "\n");
4811     }
4812
4813   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4814     {
4815       print_spaces (outfile);
4816       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
4817
4818       switch (AT_class (a))
4819         {
4820         case dw_val_class_addr:
4821           fprintf (outfile, "address");
4822           break;
4823         case dw_val_class_offset:
4824           fprintf (outfile, "offset");
4825           break;
4826         case dw_val_class_loc:
4827           fprintf (outfile, "location descriptor");
4828           break;
4829         case dw_val_class_loc_list:
4830           fprintf (outfile, "location list -> label:%s",
4831                    AT_loc_list (a)->ll_symbol);
4832           break;
4833         case dw_val_class_range_list:
4834           fprintf (outfile, "range list");
4835           break;
4836         case dw_val_class_const:
4837           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
4838           break;
4839         case dw_val_class_unsigned_const:
4840           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
4841           break;
4842         case dw_val_class_const_double:
4843           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
4844                             HOST_WIDE_INT_PRINT_UNSIGNED")",
4845                    a->dw_attr_val.v.val_double.high,
4846                    a->dw_attr_val.v.val_double.low);
4847           break;
4848         case dw_val_class_vec:
4849           fprintf (outfile, "floating-point or vector constant");
4850           break;
4851         case dw_val_class_flag:
4852           fprintf (outfile, "%u", AT_flag (a));
4853           break;
4854         case dw_val_class_die_ref:
4855           if (AT_ref (a) != NULL)
4856             {
4857               if (AT_ref (a)->comdat_type_p)
4858                 {
4859                   fprintf (outfile, "die -> signature: ");
4860                   print_signature (outfile,
4861                                    AT_ref (a)->die_id.die_type_node->signature);
4862                 }
4863               else if (AT_ref (a)->die_id.die_symbol)
4864                 fprintf (outfile, "die -> label: %s",
4865                          AT_ref (a)->die_id.die_symbol);
4866               else
4867                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
4868               fprintf (outfile, " (%p)", (void *) AT_ref (a));
4869             }
4870           else
4871             fprintf (outfile, "die -> <null>");
4872           break;
4873         case dw_val_class_vms_delta:
4874           fprintf (outfile, "delta: @slotcount(%s-%s)",
4875                    AT_vms_delta2 (a), AT_vms_delta1 (a));
4876           break;
4877         case dw_val_class_lbl_id:
4878         case dw_val_class_lineptr:
4879         case dw_val_class_macptr:
4880           fprintf (outfile, "label: %s", AT_lbl (a));
4881           break;
4882         case dw_val_class_str:
4883           if (AT_string (a) != NULL)
4884             fprintf (outfile, "\"%s\"", AT_string (a));
4885           else
4886             fprintf (outfile, "<null>");
4887           break;
4888         case dw_val_class_file:
4889           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
4890                    AT_file (a)->emitted_number);
4891           break;
4892         case dw_val_class_data8:
4893           {
4894             int i;
4895
4896             for (i = 0; i < 8; i++)
4897               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
4898             break;
4899           }
4900         default:
4901           break;
4902         }
4903
4904       fprintf (outfile, "\n");
4905     }
4906
4907   if (die->die_child != NULL)
4908     {
4909       print_indent += 4;
4910       FOR_EACH_CHILD (die, c, print_die (c, outfile));
4911       print_indent -= 4;
4912     }
4913   if (print_indent == 0)
4914     fprintf (outfile, "\n");
4915 }
4916
4917 /* Print the information collected for a given DIE.  */
4918
4919 DEBUG_FUNCTION void
4920 debug_dwarf_die (dw_die_ref die)
4921 {
4922   print_die (die, stderr);
4923 }
4924
4925 /* Print all DWARF information collected for the compilation unit.
4926    This routine is a debugging aid only.  */
4927
4928 DEBUG_FUNCTION void
4929 debug_dwarf (void)
4930 {
4931   print_indent = 0;
4932   print_die (comp_unit_die (), stderr);
4933 }
4934 \f
4935 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
4936    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
4937    DIE that marks the start of the DIEs for this include file.  */
4938
4939 static dw_die_ref
4940 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
4941 {
4942   const char *filename = get_AT_string (bincl_die, DW_AT_name);
4943   dw_die_ref new_unit = gen_compile_unit_die (filename);
4944
4945   new_unit->die_sib = old_unit;
4946   return new_unit;
4947 }
4948
4949 /* Close an include-file CU and reopen the enclosing one.  */
4950
4951 static dw_die_ref
4952 pop_compile_unit (dw_die_ref old_unit)
4953 {
4954   dw_die_ref new_unit = old_unit->die_sib;
4955
4956   old_unit->die_sib = NULL;
4957   return new_unit;
4958 }
4959
4960 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
4961 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
4962
4963 /* Calculate the checksum of a location expression.  */
4964
4965 static inline void
4966 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
4967 {
4968   int tem;
4969
4970   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
4971   CHECKSUM (tem);
4972   CHECKSUM (loc->dw_loc_oprnd1);
4973   CHECKSUM (loc->dw_loc_oprnd2);
4974 }
4975
4976 /* Calculate the checksum of an attribute.  */
4977
4978 static void
4979 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
4980 {
4981   dw_loc_descr_ref loc;
4982   rtx r;
4983
4984   CHECKSUM (at->dw_attr);
4985
4986   /* We don't care that this was compiled with a different compiler
4987      snapshot; if the output is the same, that's what matters.  */
4988   if (at->dw_attr == DW_AT_producer)
4989     return;
4990
4991   switch (AT_class (at))
4992     {
4993     case dw_val_class_const:
4994       CHECKSUM (at->dw_attr_val.v.val_int);
4995       break;
4996     case dw_val_class_unsigned_const:
4997       CHECKSUM (at->dw_attr_val.v.val_unsigned);
4998       break;
4999     case dw_val_class_const_double:
5000       CHECKSUM (at->dw_attr_val.v.val_double);
5001       break;
5002     case dw_val_class_vec:
5003       CHECKSUM (at->dw_attr_val.v.val_vec);
5004       break;
5005     case dw_val_class_flag:
5006       CHECKSUM (at->dw_attr_val.v.val_flag);
5007       break;
5008     case dw_val_class_str:
5009       CHECKSUM_STRING (AT_string (at));
5010       break;
5011
5012     case dw_val_class_addr:
5013       r = AT_addr (at);
5014       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5015       CHECKSUM_STRING (XSTR (r, 0));
5016       break;
5017
5018     case dw_val_class_offset:
5019       CHECKSUM (at->dw_attr_val.v.val_offset);
5020       break;
5021
5022     case dw_val_class_loc:
5023       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5024         loc_checksum (loc, ctx);
5025       break;
5026
5027     case dw_val_class_die_ref:
5028       die_checksum (AT_ref (at), ctx, mark);
5029       break;
5030
5031     case dw_val_class_fde_ref:
5032     case dw_val_class_vms_delta:
5033     case dw_val_class_lbl_id:
5034     case dw_val_class_lineptr:
5035     case dw_val_class_macptr:
5036       break;
5037
5038     case dw_val_class_file:
5039       CHECKSUM_STRING (AT_file (at)->filename);
5040       break;
5041
5042     case dw_val_class_data8:
5043       CHECKSUM (at->dw_attr_val.v.val_data8);
5044       break;
5045
5046     default:
5047       break;
5048     }
5049 }
5050
5051 /* Calculate the checksum of a DIE.  */
5052
5053 static void
5054 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5055 {
5056   dw_die_ref c;
5057   dw_attr_ref a;
5058   unsigned ix;
5059
5060   /* To avoid infinite recursion.  */
5061   if (die->die_mark)
5062     {
5063       CHECKSUM (die->die_mark);
5064       return;
5065     }
5066   die->die_mark = ++(*mark);
5067
5068   CHECKSUM (die->die_tag);
5069
5070   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5071     attr_checksum (a, ctx, mark);
5072
5073   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5074 }
5075
5076 #undef CHECKSUM
5077 #undef CHECKSUM_STRING
5078
5079 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5080 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5081 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5082 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5083 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5084 #define CHECKSUM_ATTR(FOO) \
5085   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5086
5087 /* Calculate the checksum of a number in signed LEB128 format.  */
5088
5089 static void
5090 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5091 {
5092   unsigned char byte;
5093   bool more;
5094
5095   while (1)
5096     {
5097       byte = (value & 0x7f);
5098       value >>= 7;
5099       more = !((value == 0 && (byte & 0x40) == 0)
5100                 || (value == -1 && (byte & 0x40) != 0));
5101       if (more)
5102         byte |= 0x80;
5103       CHECKSUM (byte);
5104       if (!more)
5105         break;
5106     }
5107 }
5108
5109 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5110
5111 static void
5112 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5113 {
5114   while (1)
5115     {
5116       unsigned char byte = (value & 0x7f);
5117       value >>= 7;
5118       if (value != 0)
5119         /* More bytes to follow.  */
5120         byte |= 0x80;
5121       CHECKSUM (byte);
5122       if (value == 0)
5123         break;
5124     }
5125 }
5126
5127 /* Checksum the context of the DIE.  This adds the names of any
5128    surrounding namespaces or structures to the checksum.  */
5129
5130 static void
5131 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5132 {
5133   const char *name;
5134   dw_die_ref spec;
5135   int tag = die->die_tag;
5136
5137   if (tag != DW_TAG_namespace
5138       && tag != DW_TAG_structure_type
5139       && tag != DW_TAG_class_type)
5140     return;
5141
5142   name = get_AT_string (die, DW_AT_name);
5143
5144   spec = get_AT_ref (die, DW_AT_specification);
5145   if (spec != NULL)
5146     die = spec;
5147
5148   if (die->die_parent != NULL)
5149     checksum_die_context (die->die_parent, ctx);
5150
5151   CHECKSUM_ULEB128 ('C');
5152   CHECKSUM_ULEB128 (tag);
5153   if (name != NULL)
5154     CHECKSUM_STRING (name);
5155 }
5156
5157 /* Calculate the checksum of a location expression.  */
5158
5159 static inline void
5160 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5161 {
5162   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5163      were emitted as a DW_FORM_sdata instead of a location expression.  */
5164   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5165     {
5166       CHECKSUM_ULEB128 (DW_FORM_sdata);
5167       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5168       return;
5169     }
5170
5171   /* Otherwise, just checksum the raw location expression.  */
5172   while (loc != NULL)
5173     {
5174       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5175       CHECKSUM (loc->dw_loc_oprnd1);
5176       CHECKSUM (loc->dw_loc_oprnd2);
5177       loc = loc->dw_loc_next;
5178     }
5179 }
5180
5181 /* Calculate the checksum of an attribute.  */
5182
5183 static void
5184 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5185                        struct md5_ctx *ctx, int *mark)
5186 {
5187   dw_loc_descr_ref loc;
5188   rtx r;
5189
5190   if (AT_class (at) == dw_val_class_die_ref)
5191     {
5192       dw_die_ref target_die = AT_ref (at);
5193
5194       /* For pointer and reference types, we checksum only the (qualified)
5195          name of the target type (if there is a name).  For friend entries,
5196          we checksum only the (qualified) name of the target type or function.
5197          This allows the checksum to remain the same whether the target type
5198          is complete or not.  */
5199       if ((at->dw_attr == DW_AT_type
5200            && (tag == DW_TAG_pointer_type
5201                || tag == DW_TAG_reference_type
5202                || tag == DW_TAG_rvalue_reference_type
5203                || tag == DW_TAG_ptr_to_member_type))
5204           || (at->dw_attr == DW_AT_friend
5205               && tag == DW_TAG_friend))
5206         {
5207           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5208
5209           if (name_attr != NULL)
5210             {
5211               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5212
5213               if (decl == NULL)
5214                 decl = target_die;
5215               CHECKSUM_ULEB128 ('N');
5216               CHECKSUM_ULEB128 (at->dw_attr);
5217               if (decl->die_parent != NULL)
5218                 checksum_die_context (decl->die_parent, ctx);
5219               CHECKSUM_ULEB128 ('E');
5220               CHECKSUM_STRING (AT_string (name_attr));
5221               return;
5222             }
5223         }
5224
5225       /* For all other references to another DIE, we check to see if the
5226          target DIE has already been visited.  If it has, we emit a
5227          backward reference; if not, we descend recursively.  */
5228       if (target_die->die_mark > 0)
5229         {
5230           CHECKSUM_ULEB128 ('R');
5231           CHECKSUM_ULEB128 (at->dw_attr);
5232           CHECKSUM_ULEB128 (target_die->die_mark);
5233         }
5234       else
5235         {
5236           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5237
5238           if (decl == NULL)
5239             decl = target_die;
5240           target_die->die_mark = ++(*mark);
5241           CHECKSUM_ULEB128 ('T');
5242           CHECKSUM_ULEB128 (at->dw_attr);
5243           if (decl->die_parent != NULL)
5244             checksum_die_context (decl->die_parent, ctx);
5245           die_checksum_ordered (target_die, ctx, mark);
5246         }
5247       return;
5248     }
5249
5250   CHECKSUM_ULEB128 ('A');
5251   CHECKSUM_ULEB128 (at->dw_attr);
5252
5253   switch (AT_class (at))
5254     {
5255     case dw_val_class_const:
5256       CHECKSUM_ULEB128 (DW_FORM_sdata);
5257       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5258       break;
5259
5260     case dw_val_class_unsigned_const:
5261       CHECKSUM_ULEB128 (DW_FORM_sdata);
5262       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5263       break;
5264
5265     case dw_val_class_const_double:
5266       CHECKSUM_ULEB128 (DW_FORM_block);
5267       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5268       CHECKSUM (at->dw_attr_val.v.val_double);
5269       break;
5270
5271     case dw_val_class_vec:
5272       CHECKSUM_ULEB128 (DW_FORM_block);
5273       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5274       CHECKSUM (at->dw_attr_val.v.val_vec);
5275       break;
5276
5277     case dw_val_class_flag:
5278       CHECKSUM_ULEB128 (DW_FORM_flag);
5279       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5280       break;
5281
5282     case dw_val_class_str:
5283       CHECKSUM_ULEB128 (DW_FORM_string);
5284       CHECKSUM_STRING (AT_string (at));
5285       break;
5286
5287     case dw_val_class_addr:
5288       r = AT_addr (at);
5289       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5290       CHECKSUM_ULEB128 (DW_FORM_string);
5291       CHECKSUM_STRING (XSTR (r, 0));
5292       break;
5293
5294     case dw_val_class_offset:
5295       CHECKSUM_ULEB128 (DW_FORM_sdata);
5296       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5297       break;
5298
5299     case dw_val_class_loc:
5300       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5301         loc_checksum_ordered (loc, ctx);
5302       break;
5303
5304     case dw_val_class_fde_ref:
5305     case dw_val_class_lbl_id:
5306     case dw_val_class_lineptr:
5307     case dw_val_class_macptr:
5308       break;
5309
5310     case dw_val_class_file:
5311       CHECKSUM_ULEB128 (DW_FORM_string);
5312       CHECKSUM_STRING (AT_file (at)->filename);
5313       break;
5314
5315     case dw_val_class_data8:
5316       CHECKSUM (at->dw_attr_val.v.val_data8);
5317       break;
5318
5319     default:
5320       break;
5321     }
5322 }
5323
5324 struct checksum_attributes
5325 {
5326   dw_attr_ref at_name;
5327   dw_attr_ref at_type;
5328   dw_attr_ref at_friend;
5329   dw_attr_ref at_accessibility;
5330   dw_attr_ref at_address_class;
5331   dw_attr_ref at_allocated;
5332   dw_attr_ref at_artificial;
5333   dw_attr_ref at_associated;
5334   dw_attr_ref at_binary_scale;
5335   dw_attr_ref at_bit_offset;
5336   dw_attr_ref at_bit_size;
5337   dw_attr_ref at_bit_stride;
5338   dw_attr_ref at_byte_size;
5339   dw_attr_ref at_byte_stride;
5340   dw_attr_ref at_const_value;
5341   dw_attr_ref at_containing_type;
5342   dw_attr_ref at_count;
5343   dw_attr_ref at_data_location;
5344   dw_attr_ref at_data_member_location;
5345   dw_attr_ref at_decimal_scale;
5346   dw_attr_ref at_decimal_sign;
5347   dw_attr_ref at_default_value;
5348   dw_attr_ref at_digit_count;
5349   dw_attr_ref at_discr;
5350   dw_attr_ref at_discr_list;
5351   dw_attr_ref at_discr_value;
5352   dw_attr_ref at_encoding;
5353   dw_attr_ref at_endianity;
5354   dw_attr_ref at_explicit;
5355   dw_attr_ref at_is_optional;
5356   dw_attr_ref at_location;
5357   dw_attr_ref at_lower_bound;
5358   dw_attr_ref at_mutable;
5359   dw_attr_ref at_ordering;
5360   dw_attr_ref at_picture_string;
5361   dw_attr_ref at_prototyped;
5362   dw_attr_ref at_small;
5363   dw_attr_ref at_segment;
5364   dw_attr_ref at_string_length;
5365   dw_attr_ref at_threads_scaled;
5366   dw_attr_ref at_upper_bound;
5367   dw_attr_ref at_use_location;
5368   dw_attr_ref at_use_UTF8;
5369   dw_attr_ref at_variable_parameter;
5370   dw_attr_ref at_virtuality;
5371   dw_attr_ref at_visibility;
5372   dw_attr_ref at_vtable_elem_location;
5373 };
5374
5375 /* Collect the attributes that we will want to use for the checksum.  */
5376
5377 static void
5378 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5379 {
5380   dw_attr_ref a;
5381   unsigned ix;
5382
5383   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5384     {
5385       switch (a->dw_attr)
5386         {
5387         case DW_AT_name:
5388           attrs->at_name = a;
5389           break;
5390         case DW_AT_type:
5391           attrs->at_type = a;
5392           break;
5393         case DW_AT_friend:
5394           attrs->at_friend = a;
5395           break;
5396         case DW_AT_accessibility:
5397           attrs->at_accessibility = a;
5398           break;
5399         case DW_AT_address_class:
5400           attrs->at_address_class = a;
5401           break;
5402         case DW_AT_allocated:
5403           attrs->at_allocated = a;
5404           break;
5405         case DW_AT_artificial:
5406           attrs->at_artificial = a;
5407           break;
5408         case DW_AT_associated:
5409           attrs->at_associated = a;
5410           break;
5411         case DW_AT_binary_scale:
5412           attrs->at_binary_scale = a;
5413           break;
5414         case DW_AT_bit_offset:
5415           attrs->at_bit_offset = a;
5416           break;
5417         case DW_AT_bit_size:
5418           attrs->at_bit_size = a;
5419           break;
5420         case DW_AT_bit_stride:
5421           attrs->at_bit_stride = a;
5422           break;
5423         case DW_AT_byte_size:
5424           attrs->at_byte_size = a;
5425           break;
5426         case DW_AT_byte_stride:
5427           attrs->at_byte_stride = a;
5428           break;
5429         case DW_AT_const_value:
5430           attrs->at_const_value = a;
5431           break;
5432         case DW_AT_containing_type:
5433           attrs->at_containing_type = a;
5434           break;
5435         case DW_AT_count:
5436           attrs->at_count = a;
5437           break;
5438         case DW_AT_data_location:
5439           attrs->at_data_location = a;
5440           break;
5441         case DW_AT_data_member_location:
5442           attrs->at_data_member_location = a;
5443           break;
5444         case DW_AT_decimal_scale:
5445           attrs->at_decimal_scale = a;
5446           break;
5447         case DW_AT_decimal_sign:
5448           attrs->at_decimal_sign = a;
5449           break;
5450         case DW_AT_default_value:
5451           attrs->at_default_value = a;
5452           break;
5453         case DW_AT_digit_count:
5454           attrs->at_digit_count = a;
5455           break;
5456         case DW_AT_discr:
5457           attrs->at_discr = a;
5458           break;
5459         case DW_AT_discr_list:
5460           attrs->at_discr_list = a;
5461           break;
5462         case DW_AT_discr_value:
5463           attrs->at_discr_value = a;
5464           break;
5465         case DW_AT_encoding:
5466           attrs->at_encoding = a;
5467           break;
5468         case DW_AT_endianity:
5469           attrs->at_endianity = a;
5470           break;
5471         case DW_AT_explicit:
5472           attrs->at_explicit = a;
5473           break;
5474         case DW_AT_is_optional:
5475           attrs->at_is_optional = a;
5476           break;
5477         case DW_AT_location:
5478           attrs->at_location = a;
5479           break;
5480         case DW_AT_lower_bound:
5481           attrs->at_lower_bound = a;
5482           break;
5483         case DW_AT_mutable:
5484           attrs->at_mutable = a;
5485           break;
5486         case DW_AT_ordering:
5487           attrs->at_ordering = a;
5488           break;
5489         case DW_AT_picture_string:
5490           attrs->at_picture_string = a;
5491           break;
5492         case DW_AT_prototyped:
5493           attrs->at_prototyped = a;
5494           break;
5495         case DW_AT_small:
5496           attrs->at_small = a;
5497           break;
5498         case DW_AT_segment:
5499           attrs->at_segment = a;
5500           break;
5501         case DW_AT_string_length:
5502           attrs->at_string_length = a;
5503           break;
5504         case DW_AT_threads_scaled:
5505           attrs->at_threads_scaled = a;
5506           break;
5507         case DW_AT_upper_bound:
5508           attrs->at_upper_bound = a;
5509           break;
5510         case DW_AT_use_location:
5511           attrs->at_use_location = a;
5512           break;
5513         case DW_AT_use_UTF8:
5514           attrs->at_use_UTF8 = a;
5515           break;
5516         case DW_AT_variable_parameter:
5517           attrs->at_variable_parameter = a;
5518           break;
5519         case DW_AT_virtuality:
5520           attrs->at_virtuality = a;
5521           break;
5522         case DW_AT_visibility:
5523           attrs->at_visibility = a;
5524           break;
5525         case DW_AT_vtable_elem_location:
5526           attrs->at_vtable_elem_location = a;
5527           break;
5528         default:
5529           break;
5530         }
5531     }
5532 }
5533
5534 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
5535
5536 static void
5537 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5538 {
5539   dw_die_ref c;
5540   dw_die_ref decl;
5541   struct checksum_attributes attrs;
5542
5543   CHECKSUM_ULEB128 ('D');
5544   CHECKSUM_ULEB128 (die->die_tag);
5545
5546   memset (&attrs, 0, sizeof (attrs));
5547
5548   decl = get_AT_ref (die, DW_AT_specification);
5549   if (decl != NULL)
5550     collect_checksum_attributes (&attrs, decl);
5551   collect_checksum_attributes (&attrs, die);
5552
5553   CHECKSUM_ATTR (attrs.at_name);
5554   CHECKSUM_ATTR (attrs.at_accessibility);
5555   CHECKSUM_ATTR (attrs.at_address_class);
5556   CHECKSUM_ATTR (attrs.at_allocated);
5557   CHECKSUM_ATTR (attrs.at_artificial);
5558   CHECKSUM_ATTR (attrs.at_associated);
5559   CHECKSUM_ATTR (attrs.at_binary_scale);
5560   CHECKSUM_ATTR (attrs.at_bit_offset);
5561   CHECKSUM_ATTR (attrs.at_bit_size);
5562   CHECKSUM_ATTR (attrs.at_bit_stride);
5563   CHECKSUM_ATTR (attrs.at_byte_size);
5564   CHECKSUM_ATTR (attrs.at_byte_stride);
5565   CHECKSUM_ATTR (attrs.at_const_value);
5566   CHECKSUM_ATTR (attrs.at_containing_type);
5567   CHECKSUM_ATTR (attrs.at_count);
5568   CHECKSUM_ATTR (attrs.at_data_location);
5569   CHECKSUM_ATTR (attrs.at_data_member_location);
5570   CHECKSUM_ATTR (attrs.at_decimal_scale);
5571   CHECKSUM_ATTR (attrs.at_decimal_sign);
5572   CHECKSUM_ATTR (attrs.at_default_value);
5573   CHECKSUM_ATTR (attrs.at_digit_count);
5574   CHECKSUM_ATTR (attrs.at_discr);
5575   CHECKSUM_ATTR (attrs.at_discr_list);
5576   CHECKSUM_ATTR (attrs.at_discr_value);
5577   CHECKSUM_ATTR (attrs.at_encoding);
5578   CHECKSUM_ATTR (attrs.at_endianity);
5579   CHECKSUM_ATTR (attrs.at_explicit);
5580   CHECKSUM_ATTR (attrs.at_is_optional);
5581   CHECKSUM_ATTR (attrs.at_location);
5582   CHECKSUM_ATTR (attrs.at_lower_bound);
5583   CHECKSUM_ATTR (attrs.at_mutable);
5584   CHECKSUM_ATTR (attrs.at_ordering);
5585   CHECKSUM_ATTR (attrs.at_picture_string);
5586   CHECKSUM_ATTR (attrs.at_prototyped);
5587   CHECKSUM_ATTR (attrs.at_small);
5588   CHECKSUM_ATTR (attrs.at_segment);
5589   CHECKSUM_ATTR (attrs.at_string_length);
5590   CHECKSUM_ATTR (attrs.at_threads_scaled);
5591   CHECKSUM_ATTR (attrs.at_upper_bound);
5592   CHECKSUM_ATTR (attrs.at_use_location);
5593   CHECKSUM_ATTR (attrs.at_use_UTF8);
5594   CHECKSUM_ATTR (attrs.at_variable_parameter);
5595   CHECKSUM_ATTR (attrs.at_virtuality);
5596   CHECKSUM_ATTR (attrs.at_visibility);
5597   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
5598   CHECKSUM_ATTR (attrs.at_type);
5599   CHECKSUM_ATTR (attrs.at_friend);
5600
5601   /* Checksum the child DIEs, except for nested types and member functions.  */
5602   c = die->die_child;
5603   if (c) do {
5604     dw_attr_ref name_attr;
5605
5606     c = c->die_sib;
5607     name_attr = get_AT (c, DW_AT_name);
5608     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
5609         && name_attr != NULL)
5610       {
5611         CHECKSUM_ULEB128 ('S');
5612         CHECKSUM_ULEB128 (c->die_tag);
5613         CHECKSUM_STRING (AT_string (name_attr));
5614       }
5615     else
5616       {
5617         /* Mark this DIE so it gets processed when unmarking.  */
5618         if (c->die_mark == 0)
5619           c->die_mark = -1;
5620         die_checksum_ordered (c, ctx, mark);
5621       }
5622   } while (c != die->die_child);
5623
5624   CHECKSUM_ULEB128 (0);
5625 }
5626
5627 #undef CHECKSUM
5628 #undef CHECKSUM_STRING
5629 #undef CHECKSUM_ATTR
5630 #undef CHECKSUM_LEB128
5631 #undef CHECKSUM_ULEB128
5632
5633 /* Generate the type signature for DIE.  This is computed by generating an
5634    MD5 checksum over the DIE's tag, its relevant attributes, and its
5635    children.  Attributes that are references to other DIEs are processed
5636    by recursion, using the MARK field to prevent infinite recursion.
5637    If the DIE is nested inside a namespace or another type, we also
5638    need to include that context in the signature.  The lower 64 bits
5639    of the resulting MD5 checksum comprise the signature.  */
5640
5641 static void
5642 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
5643 {
5644   int mark;
5645   const char *name;
5646   unsigned char checksum[16];
5647   struct md5_ctx ctx;
5648   dw_die_ref decl;
5649   dw_die_ref parent;
5650
5651   name = get_AT_string (die, DW_AT_name);
5652   decl = get_AT_ref (die, DW_AT_specification);
5653   parent = get_die_parent (die);
5654
5655   /* First, compute a signature for just the type name (and its surrounding
5656      context, if any.  This is stored in the type unit DIE for link-time
5657      ODR (one-definition rule) checking.  */
5658
5659   if (is_cxx() && name != NULL)
5660     {
5661       md5_init_ctx (&ctx);
5662
5663       /* Checksum the names of surrounding namespaces and structures.  */
5664       if (parent != NULL)
5665         checksum_die_context (parent, &ctx);
5666
5667       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
5668       md5_process_bytes (name, strlen (name) + 1, &ctx);
5669       md5_finish_ctx (&ctx, checksum);
5670
5671       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
5672     }
5673
5674   /* Next, compute the complete type signature.  */
5675
5676   md5_init_ctx (&ctx);
5677   mark = 1;
5678   die->die_mark = mark;
5679
5680   /* Checksum the names of surrounding namespaces and structures.  */
5681   if (parent != NULL)
5682     checksum_die_context (parent, &ctx);
5683
5684   /* Checksum the DIE and its children.  */
5685   die_checksum_ordered (die, &ctx, &mark);
5686   unmark_all_dies (die);
5687   md5_finish_ctx (&ctx, checksum);
5688
5689   /* Store the signature in the type node and link the type DIE and the
5690      type node together.  */
5691   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
5692           DWARF_TYPE_SIGNATURE_SIZE);
5693   die->comdat_type_p = true;
5694   die->die_id.die_type_node = type_node;
5695   type_node->type_die = die;
5696
5697   /* If the DIE is a specification, link its declaration to the type node
5698      as well.  */
5699   if (decl != NULL)
5700     {
5701       decl->comdat_type_p = true;
5702       decl->die_id.die_type_node = type_node;
5703     }
5704 }
5705
5706 /* Do the location expressions look same?  */
5707 static inline int
5708 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
5709 {
5710   return loc1->dw_loc_opc == loc2->dw_loc_opc
5711          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
5712          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
5713 }
5714
5715 /* Do the values look the same?  */
5716 static int
5717 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
5718 {
5719   dw_loc_descr_ref loc1, loc2;
5720   rtx r1, r2;
5721
5722   if (v1->val_class != v2->val_class)
5723     return 0;
5724
5725   switch (v1->val_class)
5726     {
5727     case dw_val_class_const:
5728       return v1->v.val_int == v2->v.val_int;
5729     case dw_val_class_unsigned_const:
5730       return v1->v.val_unsigned == v2->v.val_unsigned;
5731     case dw_val_class_const_double:
5732       return v1->v.val_double.high == v2->v.val_double.high
5733              && v1->v.val_double.low == v2->v.val_double.low;
5734     case dw_val_class_vec:
5735       if (v1->v.val_vec.length != v2->v.val_vec.length
5736           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
5737         return 0;
5738       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
5739                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
5740         return 0;
5741       return 1;
5742     case dw_val_class_flag:
5743       return v1->v.val_flag == v2->v.val_flag;
5744     case dw_val_class_str:
5745       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
5746
5747     case dw_val_class_addr:
5748       r1 = v1->v.val_addr;
5749       r2 = v2->v.val_addr;
5750       if (GET_CODE (r1) != GET_CODE (r2))
5751         return 0;
5752       return !rtx_equal_p (r1, r2);
5753
5754     case dw_val_class_offset:
5755       return v1->v.val_offset == v2->v.val_offset;
5756
5757     case dw_val_class_loc:
5758       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
5759            loc1 && loc2;
5760            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
5761         if (!same_loc_p (loc1, loc2, mark))
5762           return 0;
5763       return !loc1 && !loc2;
5764
5765     case dw_val_class_die_ref:
5766       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
5767
5768     case dw_val_class_fde_ref:
5769     case dw_val_class_vms_delta:
5770     case dw_val_class_lbl_id:
5771     case dw_val_class_lineptr:
5772     case dw_val_class_macptr:
5773       return 1;
5774
5775     case dw_val_class_file:
5776       return v1->v.val_file == v2->v.val_file;
5777
5778     case dw_val_class_data8:
5779       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
5780
5781     default:
5782       return 1;
5783     }
5784 }
5785
5786 /* Do the attributes look the same?  */
5787
5788 static int
5789 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
5790 {
5791   if (at1->dw_attr != at2->dw_attr)
5792     return 0;
5793
5794   /* We don't care that this was compiled with a different compiler
5795      snapshot; if the output is the same, that's what matters. */
5796   if (at1->dw_attr == DW_AT_producer)
5797     return 1;
5798
5799   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
5800 }
5801
5802 /* Do the dies look the same?  */
5803
5804 static int
5805 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
5806 {
5807   dw_die_ref c1, c2;
5808   dw_attr_ref a1;
5809   unsigned ix;
5810
5811   /* To avoid infinite recursion.  */
5812   if (die1->die_mark)
5813     return die1->die_mark == die2->die_mark;
5814   die1->die_mark = die2->die_mark = ++(*mark);
5815
5816   if (die1->die_tag != die2->die_tag)
5817     return 0;
5818
5819   if (VEC_length (dw_attr_node, die1->die_attr)
5820       != VEC_length (dw_attr_node, die2->die_attr))
5821     return 0;
5822
5823   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
5824     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
5825       return 0;
5826
5827   c1 = die1->die_child;
5828   c2 = die2->die_child;
5829   if (! c1)
5830     {
5831       if (c2)
5832         return 0;
5833     }
5834   else
5835     for (;;)
5836       {
5837         if (!same_die_p (c1, c2, mark))
5838           return 0;
5839         c1 = c1->die_sib;
5840         c2 = c2->die_sib;
5841         if (c1 == die1->die_child)
5842           {
5843             if (c2 == die2->die_child)
5844               break;
5845             else
5846               return 0;
5847           }
5848     }
5849
5850   return 1;
5851 }
5852
5853 /* Do the dies look the same?  Wrapper around same_die_p.  */
5854
5855 static int
5856 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
5857 {
5858   int mark = 0;
5859   int ret = same_die_p (die1, die2, &mark);
5860
5861   unmark_all_dies (die1);
5862   unmark_all_dies (die2);
5863
5864   return ret;
5865 }
5866
5867 /* The prefix to attach to symbols on DIEs in the current comdat debug
5868    info section.  */
5869 static const char *comdat_symbol_id;
5870
5871 /* The index of the current symbol within the current comdat CU.  */
5872 static unsigned int comdat_symbol_number;
5873
5874 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
5875    children, and set comdat_symbol_id accordingly.  */
5876
5877 static void
5878 compute_section_prefix (dw_die_ref unit_die)
5879 {
5880   const char *die_name = get_AT_string (unit_die, DW_AT_name);
5881   const char *base = die_name ? lbasename (die_name) : "anonymous";
5882   char *name = XALLOCAVEC (char, strlen (base) + 64);
5883   char *p;
5884   int i, mark;
5885   unsigned char checksum[16];
5886   struct md5_ctx ctx;
5887
5888   /* Compute the checksum of the DIE, then append part of it as hex digits to
5889      the name filename of the unit.  */
5890
5891   md5_init_ctx (&ctx);
5892   mark = 0;
5893   die_checksum (unit_die, &ctx, &mark);
5894   unmark_all_dies (unit_die);
5895   md5_finish_ctx (&ctx, checksum);
5896
5897   sprintf (name, "%s.", base);
5898   clean_symbol_name (name);
5899
5900   p = name + strlen (name);
5901   for (i = 0; i < 4; i++)
5902     {
5903       sprintf (p, "%.2x", checksum[i]);
5904       p += 2;
5905     }
5906
5907   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
5908   comdat_symbol_number = 0;
5909 }
5910
5911 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
5912
5913 static int
5914 is_type_die (dw_die_ref die)
5915 {
5916   switch (die->die_tag)
5917     {
5918     case DW_TAG_array_type:
5919     case DW_TAG_class_type:
5920     case DW_TAG_interface_type:
5921     case DW_TAG_enumeration_type:
5922     case DW_TAG_pointer_type:
5923     case DW_TAG_reference_type:
5924     case DW_TAG_rvalue_reference_type:
5925     case DW_TAG_string_type:
5926     case DW_TAG_structure_type:
5927     case DW_TAG_subroutine_type:
5928     case DW_TAG_union_type:
5929     case DW_TAG_ptr_to_member_type:
5930     case DW_TAG_set_type:
5931     case DW_TAG_subrange_type:
5932     case DW_TAG_base_type:
5933     case DW_TAG_const_type:
5934     case DW_TAG_file_type:
5935     case DW_TAG_packed_type:
5936     case DW_TAG_volatile_type:
5937     case DW_TAG_typedef:
5938       return 1;
5939     default:
5940       return 0;
5941     }
5942 }
5943
5944 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
5945    Basically, we want to choose the bits that are likely to be shared between
5946    compilations (types) and leave out the bits that are specific to individual
5947    compilations (functions).  */
5948
5949 static int
5950 is_comdat_die (dw_die_ref c)
5951 {
5952   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
5953      we do for stabs.  The advantage is a greater likelihood of sharing between
5954      objects that don't include headers in the same order (and therefore would
5955      put the base types in a different comdat).  jason 8/28/00 */
5956
5957   if (c->die_tag == DW_TAG_base_type)
5958     return 0;
5959
5960   if (c->die_tag == DW_TAG_pointer_type
5961       || c->die_tag == DW_TAG_reference_type
5962       || c->die_tag == DW_TAG_rvalue_reference_type
5963       || c->die_tag == DW_TAG_const_type
5964       || c->die_tag == DW_TAG_volatile_type)
5965     {
5966       dw_die_ref t = get_AT_ref (c, DW_AT_type);
5967
5968       return t ? is_comdat_die (t) : 0;
5969     }
5970
5971   return is_type_die (c);
5972 }
5973
5974 /* Returns 1 iff C is the sort of DIE that might be referred to from another
5975    compilation unit.  */
5976
5977 static int
5978 is_symbol_die (dw_die_ref c)
5979 {
5980   return (is_type_die (c)
5981           || is_declaration_die (c)
5982           || c->die_tag == DW_TAG_namespace
5983           || c->die_tag == DW_TAG_module);
5984 }
5985
5986 /* Returns true iff C is a compile-unit DIE.  */
5987
5988 static inline bool
5989 is_cu_die (dw_die_ref c)
5990 {
5991   return c && c->die_tag == DW_TAG_compile_unit;
5992 }
5993
5994 /* Returns true iff C is a unit DIE of some sort.  */
5995
5996 static inline bool
5997 is_unit_die (dw_die_ref c)
5998 {
5999   return c && (c->die_tag == DW_TAG_compile_unit
6000                || c->die_tag == DW_TAG_partial_unit
6001                || c->die_tag == DW_TAG_type_unit);
6002 }
6003
6004 /* Returns true iff C is a namespace DIE.  */
6005
6006 static inline bool
6007 is_namespace_die (dw_die_ref c)
6008 {
6009   return c && c->die_tag == DW_TAG_namespace;
6010 }
6011
6012 /* Returns true iff C is a class or structure DIE.  */
6013
6014 static inline bool
6015 is_class_die (dw_die_ref c)
6016 {
6017   return c && (c->die_tag == DW_TAG_class_type
6018                || c->die_tag == DW_TAG_structure_type);
6019 }
6020
6021 static char *
6022 gen_internal_sym (const char *prefix)
6023 {
6024   char buf[256];
6025
6026   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6027   return xstrdup (buf);
6028 }
6029
6030 /* Assign symbols to all worthy DIEs under DIE.  */
6031
6032 static void
6033 assign_symbol_names (dw_die_ref die)
6034 {
6035   dw_die_ref c;
6036
6037   if (is_symbol_die (die) && !die->comdat_type_p)
6038     {
6039       if (comdat_symbol_id)
6040         {
6041           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6042
6043           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6044                    comdat_symbol_id, comdat_symbol_number++);
6045           die->die_id.die_symbol = xstrdup (p);
6046         }
6047       else
6048         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6049     }
6050
6051   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6052 }
6053
6054 struct cu_hash_table_entry
6055 {
6056   dw_die_ref cu;
6057   unsigned min_comdat_num, max_comdat_num;
6058   struct cu_hash_table_entry *next;
6059 };
6060
6061 /* Routines to manipulate hash table of CUs.  */
6062 static hashval_t
6063 htab_cu_hash (const void *of)
6064 {
6065   const struct cu_hash_table_entry *const entry =
6066     (const struct cu_hash_table_entry *) of;
6067
6068   return htab_hash_string (entry->cu->die_id.die_symbol);
6069 }
6070
6071 static int
6072 htab_cu_eq (const void *of1, const void *of2)
6073 {
6074   const struct cu_hash_table_entry *const entry1 =
6075     (const struct cu_hash_table_entry *) of1;
6076   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6077
6078   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6079 }
6080
6081 static void
6082 htab_cu_del (void *what)
6083 {
6084   struct cu_hash_table_entry *next,
6085     *entry = (struct cu_hash_table_entry *) what;
6086
6087   while (entry)
6088     {
6089       next = entry->next;
6090       free (entry);
6091       entry = next;
6092     }
6093 }
6094
6095 /* Check whether we have already seen this CU and set up SYM_NUM
6096    accordingly.  */
6097 static int
6098 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6099 {
6100   struct cu_hash_table_entry dummy;
6101   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6102
6103   dummy.max_comdat_num = 0;
6104
6105   slot = (struct cu_hash_table_entry **)
6106     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6107         INSERT);
6108   entry = *slot;
6109
6110   for (; entry; last = entry, entry = entry->next)
6111     {
6112       if (same_die_p_wrap (cu, entry->cu))
6113         break;
6114     }
6115
6116   if (entry)
6117     {
6118       *sym_num = entry->min_comdat_num;
6119       return 1;
6120     }
6121
6122   entry = XCNEW (struct cu_hash_table_entry);
6123   entry->cu = cu;
6124   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6125   entry->next = *slot;
6126   *slot = entry;
6127
6128   return 0;
6129 }
6130
6131 /* Record SYM_NUM to record of CU in HTABLE.  */
6132 static void
6133 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6134 {
6135   struct cu_hash_table_entry **slot, *entry;
6136
6137   slot = (struct cu_hash_table_entry **)
6138     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6139         NO_INSERT);
6140   entry = *slot;
6141
6142   entry->max_comdat_num = sym_num;
6143 }
6144
6145 /* Traverse the DIE (which is always comp_unit_die), and set up
6146    additional compilation units for each of the include files we see
6147    bracketed by BINCL/EINCL.  */
6148
6149 static void
6150 break_out_includes (dw_die_ref die)
6151 {
6152   dw_die_ref c;
6153   dw_die_ref unit = NULL;
6154   limbo_die_node *node, **pnode;
6155   htab_t cu_hash_table;
6156
6157   c = die->die_child;
6158   if (c) do {
6159     dw_die_ref prev = c;
6160     c = c->die_sib;
6161     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6162            || (unit && is_comdat_die (c)))
6163       {
6164         dw_die_ref next = c->die_sib;
6165
6166         /* This DIE is for a secondary CU; remove it from the main one.  */
6167         remove_child_with_prev (c, prev);
6168
6169         if (c->die_tag == DW_TAG_GNU_BINCL)
6170           unit = push_new_compile_unit (unit, c);
6171         else if (c->die_tag == DW_TAG_GNU_EINCL)
6172           unit = pop_compile_unit (unit);
6173         else
6174           add_child_die (unit, c);
6175         c = next;
6176         if (c == die->die_child)
6177           break;
6178       }
6179   } while (c != die->die_child);
6180
6181 #if 0
6182   /* We can only use this in debugging, since the frontend doesn't check
6183      to make sure that we leave every include file we enter.  */
6184   gcc_assert (!unit);
6185 #endif
6186
6187   assign_symbol_names (die);
6188   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6189   for (node = limbo_die_list, pnode = &limbo_die_list;
6190        node;
6191        node = node->next)
6192     {
6193       int is_dupl;
6194
6195       compute_section_prefix (node->die);
6196       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6197                         &comdat_symbol_number);
6198       assign_symbol_names (node->die);
6199       if (is_dupl)
6200         *pnode = node->next;
6201       else
6202         {
6203           pnode = &node->next;
6204           record_comdat_symbol_number (node->die, cu_hash_table,
6205                 comdat_symbol_number);
6206         }
6207     }
6208   htab_delete (cu_hash_table);
6209 }
6210
6211 /* Return non-zero if this DIE is a declaration.  */
6212
6213 static int
6214 is_declaration_die (dw_die_ref die)
6215 {
6216   dw_attr_ref a;
6217   unsigned ix;
6218
6219   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6220     if (a->dw_attr == DW_AT_declaration)
6221       return 1;
6222
6223   return 0;
6224 }
6225
6226 /* Return non-zero if this DIE is nested inside a subprogram.  */
6227
6228 static int
6229 is_nested_in_subprogram (dw_die_ref die)
6230 {
6231   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6232
6233   if (decl == NULL)
6234     decl = die;
6235   return local_scope_p (decl);
6236 }
6237
6238 /* Return non-zero if this DIE contains a defining declaration of a
6239    subprogram.  */
6240
6241 static int
6242 contains_subprogram_definition (dw_die_ref die)
6243 {
6244   dw_die_ref c;
6245
6246   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6247     return 1;
6248   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6249   return 0;
6250 }
6251
6252 /* Return non-zero if this is a type DIE that should be moved to a
6253    COMDAT .debug_types section.  */
6254
6255 static int
6256 should_move_die_to_comdat (dw_die_ref die)
6257 {
6258   switch (die->die_tag)
6259     {
6260     case DW_TAG_class_type:
6261     case DW_TAG_structure_type:
6262     case DW_TAG_enumeration_type:
6263     case DW_TAG_union_type:
6264       /* Don't move declarations, inlined instances, or types nested in a
6265          subprogram.  */
6266       if (is_declaration_die (die)
6267           || get_AT (die, DW_AT_abstract_origin)
6268           || is_nested_in_subprogram (die))
6269         return 0;
6270       /* A type definition should never contain a subprogram definition.  */
6271       gcc_assert (!contains_subprogram_definition (die));
6272       return 1;
6273     case DW_TAG_array_type:
6274     case DW_TAG_interface_type:
6275     case DW_TAG_pointer_type:
6276     case DW_TAG_reference_type:
6277     case DW_TAG_rvalue_reference_type:
6278     case DW_TAG_string_type:
6279     case DW_TAG_subroutine_type:
6280     case DW_TAG_ptr_to_member_type:
6281     case DW_TAG_set_type:
6282     case DW_TAG_subrange_type:
6283     case DW_TAG_base_type:
6284     case DW_TAG_const_type:
6285     case DW_TAG_file_type:
6286     case DW_TAG_packed_type:
6287     case DW_TAG_volatile_type:
6288     case DW_TAG_typedef:
6289     default:
6290       return 0;
6291     }
6292 }
6293
6294 /* Make a clone of DIE.  */
6295
6296 static dw_die_ref
6297 clone_die (dw_die_ref die)
6298 {
6299   dw_die_ref clone;
6300   dw_attr_ref a;
6301   unsigned ix;
6302
6303   clone = ggc_alloc_cleared_die_node ();
6304   clone->die_tag = die->die_tag;
6305
6306   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6307     add_dwarf_attr (clone, a);
6308
6309   return clone;
6310 }
6311
6312 /* Make a clone of the tree rooted at DIE.  */
6313
6314 static dw_die_ref
6315 clone_tree (dw_die_ref die)
6316 {
6317   dw_die_ref c;
6318   dw_die_ref clone = clone_die (die);
6319
6320   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6321
6322   return clone;
6323 }
6324
6325 /* Make a clone of DIE as a declaration.  */
6326
6327 static dw_die_ref
6328 clone_as_declaration (dw_die_ref die)
6329 {
6330   dw_die_ref clone;
6331   dw_die_ref decl;
6332   dw_attr_ref a;
6333   unsigned ix;
6334
6335   /* If the DIE is already a declaration, just clone it.  */
6336   if (is_declaration_die (die))
6337     return clone_die (die);
6338
6339   /* If the DIE is a specification, just clone its declaration DIE.  */
6340   decl = get_AT_ref (die, DW_AT_specification);
6341   if (decl != NULL)
6342     {
6343       clone = clone_die (decl);
6344       if (die->comdat_type_p)
6345         add_AT_die_ref (clone, DW_AT_signature, die);
6346       return clone;
6347     }
6348
6349   clone = ggc_alloc_cleared_die_node ();
6350   clone->die_tag = die->die_tag;
6351
6352   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6353     {
6354       /* We don't want to copy over all attributes.
6355          For example we don't want DW_AT_byte_size because otherwise we will no
6356          longer have a declaration and GDB will treat it as a definition.  */
6357
6358       switch (a->dw_attr)
6359         {
6360         case DW_AT_artificial:
6361         case DW_AT_containing_type:
6362         case DW_AT_external:
6363         case DW_AT_name:
6364         case DW_AT_type:
6365         case DW_AT_virtuality:
6366         case DW_AT_linkage_name:
6367         case DW_AT_MIPS_linkage_name:
6368           add_dwarf_attr (clone, a);
6369           break;
6370         case DW_AT_byte_size:
6371         default:
6372           break;
6373         }
6374     }
6375
6376   if (die->comdat_type_p)
6377     add_AT_die_ref (clone, DW_AT_signature, die);
6378
6379   add_AT_flag (clone, DW_AT_declaration, 1);
6380   return clone;
6381 }
6382
6383 /* Copy the declaration context to the new type unit DIE.  This includes
6384    any surrounding namespace or type declarations.  If the DIE has an
6385    AT_specification attribute, it also includes attributes and children
6386    attached to the specification, and returns a pointer to the original
6387    parent of the declaration DIE.  Returns NULL otherwise.  */
6388
6389 static dw_die_ref
6390 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6391 {
6392   dw_die_ref decl;
6393   dw_die_ref new_decl;
6394   dw_die_ref orig_parent = NULL;
6395
6396   decl = get_AT_ref (die, DW_AT_specification);
6397   if (decl == NULL)
6398     decl = die;
6399   else
6400     {
6401       unsigned ix;
6402       dw_die_ref c;
6403       dw_attr_ref a;
6404
6405       /* The original DIE will be changed to a declaration, and must
6406          be moved to be a child of the original declaration DIE.  */
6407       orig_parent = decl->die_parent;
6408
6409       /* Copy the type node pointer from the new DIE to the original
6410          declaration DIE so we can forward references later.  */
6411       decl->comdat_type_p = true;
6412       decl->die_id.die_type_node = die->die_id.die_type_node;
6413
6414       remove_AT (die, DW_AT_specification);
6415
6416       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
6417         {
6418           if (a->dw_attr != DW_AT_name
6419               && a->dw_attr != DW_AT_declaration
6420               && a->dw_attr != DW_AT_external)
6421             add_dwarf_attr (die, a);
6422         }
6423
6424       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6425     }
6426
6427   if (decl->die_parent != NULL
6428       && !is_unit_die (decl->die_parent))
6429     {
6430       new_decl = copy_ancestor_tree (unit, decl, NULL);
6431       if (new_decl != NULL)
6432         {
6433           remove_AT (new_decl, DW_AT_signature);
6434           add_AT_specification (die, new_decl);
6435         }
6436     }
6437
6438   return orig_parent;
6439 }
6440
6441 /* Generate the skeleton ancestor tree for the given NODE, then clone
6442    the DIE and add the clone into the tree.  */
6443
6444 static void
6445 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6446 {
6447   if (node->new_die != NULL)
6448     return;
6449
6450   node->new_die = clone_as_declaration (node->old_die);
6451
6452   if (node->parent != NULL)
6453     {
6454       generate_skeleton_ancestor_tree (node->parent);
6455       add_child_die (node->parent->new_die, node->new_die);
6456     }
6457 }
6458
6459 /* Generate a skeleton tree of DIEs containing any declarations that are
6460    found in the original tree.  We traverse the tree looking for declaration
6461    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
6462
6463 static void
6464 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6465 {
6466   skeleton_chain_node node;
6467   dw_die_ref c;
6468   dw_die_ref first;
6469   dw_die_ref prev = NULL;
6470   dw_die_ref next = NULL;
6471
6472   node.parent = parent;
6473
6474   first = c = parent->old_die->die_child;
6475   if (c)
6476     next = c->die_sib;
6477   if (c) do {
6478     if (prev == NULL || prev->die_sib == c)
6479       prev = c;
6480     c = next;
6481     next = (c == first ? NULL : c->die_sib);
6482     node.old_die = c;
6483     node.new_die = NULL;
6484     if (is_declaration_die (c))
6485       {
6486         /* Clone the existing DIE, move the original to the skeleton
6487            tree (which is in the main CU), and put the clone, with
6488            all the original's children, where the original came from.  */
6489         dw_die_ref clone = clone_die (c);
6490         move_all_children (c, clone);
6491
6492         replace_child (c, clone, prev);
6493         generate_skeleton_ancestor_tree (parent);
6494         add_child_die (parent->new_die, c);
6495         node.new_die = c;
6496         c = clone;
6497       }
6498     generate_skeleton_bottom_up (&node);
6499   } while (next != NULL);
6500 }
6501
6502 /* Wrapper function for generate_skeleton_bottom_up.  */
6503
6504 static dw_die_ref
6505 generate_skeleton (dw_die_ref die)
6506 {
6507   skeleton_chain_node node;
6508
6509   node.old_die = die;
6510   node.new_die = NULL;
6511   node.parent = NULL;
6512
6513   /* If this type definition is nested inside another type,
6514      always leave at least a declaration in its place.  */
6515   if (die->die_parent != NULL && is_type_die (die->die_parent))
6516     node.new_die = clone_as_declaration (die);
6517
6518   generate_skeleton_bottom_up (&node);
6519   return node.new_die;
6520 }
6521
6522 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6523    declaration.  The original DIE is moved to a new compile unit so that
6524    existing references to it follow it to the new location.  If any of the
6525    original DIE's descendants is a declaration, we need to replace the
6526    original DIE with a skeleton tree and move the declarations back into the
6527    skeleton tree.  */
6528
6529 static dw_die_ref
6530 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6531                                        dw_die_ref prev)
6532 {
6533   dw_die_ref skeleton, orig_parent;
6534
6535   /* Copy the declaration context to the type unit DIE.  If the returned
6536      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6537      that DIE.  */
6538   orig_parent = copy_declaration_context (unit, child);
6539
6540   skeleton = generate_skeleton (child);
6541   if (skeleton == NULL)
6542     remove_child_with_prev (child, prev);
6543   else
6544     {
6545       skeleton->comdat_type_p = true;
6546       skeleton->die_id.die_type_node = child->die_id.die_type_node;
6547
6548       /* If the original DIE was a specification, we need to put
6549          the skeleton under the parent DIE of the declaration.
6550          This leaves the original declaration in the tree, but
6551          it will be pruned later since there are no longer any
6552          references to it.  */
6553       if (orig_parent != NULL)
6554         {
6555           remove_child_with_prev (child, prev);
6556           add_child_die (orig_parent, skeleton);
6557         }
6558       else
6559         replace_child (child, skeleton, prev);
6560     }
6561
6562   return skeleton;
6563 }
6564
6565 /* Traverse the DIE and set up additional .debug_types sections for each
6566    type worthy of being placed in a COMDAT section.  */
6567
6568 static void
6569 break_out_comdat_types (dw_die_ref die)
6570 {
6571   dw_die_ref c;
6572   dw_die_ref first;
6573   dw_die_ref prev = NULL;
6574   dw_die_ref next = NULL;
6575   dw_die_ref unit = NULL;
6576
6577   first = c = die->die_child;
6578   if (c)
6579     next = c->die_sib;
6580   if (c) do {
6581     if (prev == NULL || prev->die_sib == c)
6582       prev = c;
6583     c = next;
6584     next = (c == first ? NULL : c->die_sib);
6585     if (should_move_die_to_comdat (c))
6586       {
6587         dw_die_ref replacement;
6588         comdat_type_node_ref type_node;
6589
6590         /* Create a new type unit DIE as the root for the new tree, and
6591            add it to the list of comdat types.  */
6592         unit = new_die (DW_TAG_type_unit, NULL, NULL);
6593         add_AT_unsigned (unit, DW_AT_language,
6594                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
6595         type_node = ggc_alloc_cleared_comdat_type_node ();
6596         type_node->root_die = unit;
6597         type_node->next = comdat_type_list;
6598         comdat_type_list = type_node;
6599
6600         /* Generate the type signature.  */
6601         generate_type_signature (c, type_node);
6602
6603         /* Copy the declaration context, attributes, and children of the
6604            declaration into the new type unit DIE, then remove this DIE
6605            from the main CU (or replace it with a skeleton if necessary).  */
6606         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
6607         type_node->skeleton_die = replacement;
6608
6609         /* Break out nested types into their own type units.  */
6610         break_out_comdat_types (c);
6611
6612         /* Add the DIE to the new compunit.  */
6613         add_child_die (unit, c);
6614
6615         if (replacement != NULL)
6616           c = replacement;
6617       }
6618     else if (c->die_tag == DW_TAG_namespace
6619              || c->die_tag == DW_TAG_class_type
6620              || c->die_tag == DW_TAG_structure_type
6621              || c->die_tag == DW_TAG_union_type)
6622       {
6623         /* Look for nested types that can be broken out.  */
6624         break_out_comdat_types (c);
6625       }
6626   } while (next != NULL);
6627 }
6628
6629 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
6630
6631 struct decl_table_entry
6632 {
6633   dw_die_ref orig;
6634   dw_die_ref copy;
6635 };
6636
6637 /* Routines to manipulate hash table of copied declarations.  */
6638
6639 static hashval_t
6640 htab_decl_hash (const void *of)
6641 {
6642   const struct decl_table_entry *const entry =
6643     (const struct decl_table_entry *) of;
6644
6645   return htab_hash_pointer (entry->orig);
6646 }
6647
6648 static int
6649 htab_decl_eq (const void *of1, const void *of2)
6650 {
6651   const struct decl_table_entry *const entry1 =
6652     (const struct decl_table_entry *) of1;
6653   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6654
6655   return entry1->orig == entry2;
6656 }
6657
6658 static void
6659 htab_decl_del (void *what)
6660 {
6661   struct decl_table_entry *entry = (struct decl_table_entry *) what;
6662
6663   free (entry);
6664 }
6665
6666 /* Copy DIE and its ancestors, up to, but not including, the compile unit
6667    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
6668    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
6669    to check if the ancestor has already been copied into UNIT.  */
6670
6671 static dw_die_ref
6672 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
6673 {
6674   dw_die_ref parent = die->die_parent;
6675   dw_die_ref new_parent = unit;
6676   dw_die_ref copy;
6677   void **slot = NULL;
6678   struct decl_table_entry *entry = NULL;
6679
6680   if (decl_table)
6681     {
6682       /* Check if the entry has already been copied to UNIT.  */
6683       slot = htab_find_slot_with_hash (decl_table, die,
6684                                        htab_hash_pointer (die), INSERT);
6685       if (*slot != HTAB_EMPTY_ENTRY)
6686         {
6687           entry = (struct decl_table_entry *) *slot;
6688           return entry->copy;
6689         }
6690
6691       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
6692       entry = XCNEW (struct decl_table_entry);
6693       entry->orig = die;
6694       entry->copy = NULL;
6695       *slot = entry;
6696     }
6697
6698   if (parent != NULL)
6699     {
6700       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
6701       if (spec != NULL)
6702         parent = spec;
6703       if (!is_unit_die (parent))
6704         new_parent = copy_ancestor_tree (unit, parent, decl_table);
6705     }
6706
6707   copy = clone_as_declaration (die);
6708   add_child_die (new_parent, copy);
6709
6710   if (decl_table != NULL)
6711     {
6712       /* Record the pointer to the copy.  */
6713       entry->copy = copy;
6714     }
6715
6716   return copy;
6717 }
6718
6719 /* Like clone_tree, but additionally enter all the children into
6720    the hash table decl_table.  */
6721
6722 static dw_die_ref
6723 clone_tree_hash (dw_die_ref die, htab_t decl_table)
6724 {
6725   dw_die_ref c;
6726   dw_die_ref clone = clone_die (die);
6727   struct decl_table_entry *entry;
6728   void **slot = htab_find_slot_with_hash (decl_table, die,
6729                                           htab_hash_pointer (die), INSERT);
6730   /* Assert that DIE isn't in the hash table yet.  If it would be there
6731      before, the ancestors would be necessarily there as well, therefore
6732      clone_tree_hash wouldn't be called.  */
6733   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
6734   entry = XCNEW (struct decl_table_entry);
6735   entry->orig = die;
6736   entry->copy = clone;
6737   *slot = entry;
6738
6739   FOR_EACH_CHILD (die, c,
6740                   add_child_die (clone, clone_tree_hash (c, decl_table)));
6741
6742   return clone;
6743 }
6744
6745 /* Walk the DIE and its children, looking for references to incomplete
6746    or trivial types that are unmarked (i.e., that are not in the current
6747    type_unit).  */
6748
6749 static void
6750 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
6751 {
6752   dw_die_ref c;
6753   dw_attr_ref a;
6754   unsigned ix;
6755
6756   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6757     {
6758       if (AT_class (a) == dw_val_class_die_ref)
6759         {
6760           dw_die_ref targ = AT_ref (a);
6761           void **slot;
6762           struct decl_table_entry *entry;
6763
6764           if (targ->die_mark != 0 || targ->comdat_type_p)
6765             continue;
6766
6767           slot = htab_find_slot_with_hash (decl_table, targ,
6768                                            htab_hash_pointer (targ), INSERT);
6769
6770           if (*slot != HTAB_EMPTY_ENTRY)
6771             {
6772               /* TARG has already been copied, so we just need to
6773                  modify the reference to point to the copy.  */
6774               entry = (struct decl_table_entry *) *slot;
6775               a->dw_attr_val.v.val_die_ref.die = entry->copy;
6776             }
6777           else
6778             {
6779               dw_die_ref parent = unit;
6780               dw_die_ref copy = clone_die (targ);
6781
6782               /* Record in DECL_TABLE that TARG has been copied.
6783                  Need to do this now, before the recursive call,
6784                  because DECL_TABLE may be expanded and SLOT
6785                  would no longer be a valid pointer.  */
6786               entry = XCNEW (struct decl_table_entry);
6787               entry->orig = targ;
6788               entry->copy = copy;
6789               *slot = entry;
6790
6791               FOR_EACH_CHILD (targ, c,
6792                               add_child_die (copy,
6793                                              clone_tree_hash (c, decl_table)));
6794
6795               /* Make sure the cloned tree is marked as part of the
6796                  type unit.  */
6797               mark_dies (copy);
6798
6799               /* If TARG has surrounding context, copy its ancestor tree
6800                  into the new type unit.  */
6801               if (targ->die_parent != NULL
6802                   && !is_unit_die (targ->die_parent))
6803                 parent = copy_ancestor_tree (unit, targ->die_parent,
6804                                              decl_table);
6805
6806               add_child_die (parent, copy);
6807               a->dw_attr_val.v.val_die_ref.die = copy;
6808
6809               /* Make sure the newly-copied DIE is walked.  If it was
6810                  installed in a previously-added context, it won't
6811                  get visited otherwise.  */
6812               if (parent != unit)
6813                 {
6814                   /* Find the highest point of the newly-added tree,
6815                      mark each node along the way, and walk from there.  */
6816                   parent->die_mark = 1;
6817                   while (parent->die_parent
6818                          && parent->die_parent->die_mark == 0)
6819                     {
6820                       parent = parent->die_parent;
6821                       parent->die_mark = 1;
6822                     }
6823                   copy_decls_walk (unit, parent, decl_table);
6824                 }
6825             }
6826         }
6827     }
6828
6829   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
6830 }
6831
6832 /* Copy declarations for "unworthy" types into the new comdat section.
6833    Incomplete types, modified types, and certain other types aren't broken
6834    out into comdat sections of their own, so they don't have a signature,
6835    and we need to copy the declaration into the same section so that we
6836    don't have an external reference.  */
6837
6838 static void
6839 copy_decls_for_unworthy_types (dw_die_ref unit)
6840 {
6841   htab_t decl_table;
6842
6843   mark_dies (unit);
6844   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
6845   copy_decls_walk (unit, unit, decl_table);
6846   htab_delete (decl_table);
6847   unmark_dies (unit);
6848 }
6849
6850 /* Traverse the DIE and add a sibling attribute if it may have the
6851    effect of speeding up access to siblings.  To save some space,
6852    avoid generating sibling attributes for DIE's without children.  */
6853
6854 static void
6855 add_sibling_attributes (dw_die_ref die)
6856 {
6857   dw_die_ref c;
6858
6859   if (! die->die_child)
6860     return;
6861
6862   if (die->die_parent && die != die->die_parent->die_child)
6863     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6864
6865   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6866 }
6867
6868 /* Output all location lists for the DIE and its children.  */
6869
6870 static void
6871 output_location_lists (dw_die_ref die)
6872 {
6873   dw_die_ref c;
6874   dw_attr_ref a;
6875   unsigned ix;
6876
6877   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6878     if (AT_class (a) == dw_val_class_loc_list)
6879       output_loc_list (AT_loc_list (a));
6880
6881   FOR_EACH_CHILD (die, c, output_location_lists (c));
6882 }
6883
6884 /* We want to limit the number of external references, because they are
6885    larger than local references: a relocation takes multiple words, and
6886    even a sig8 reference is always eight bytes, whereas a local reference
6887    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
6888    So if we encounter multiple external references to the same type DIE, we
6889    make a local typedef stub for it and redirect all references there.
6890
6891    This is the element of the hash table for keeping track of these
6892    references.  */
6893
6894 struct external_ref
6895 {
6896   dw_die_ref type;
6897   dw_die_ref stub;
6898   unsigned n_refs;
6899 };
6900
6901 /* Hash an external_ref.  */
6902
6903 static hashval_t
6904 hash_external_ref (const void *p)
6905 {
6906   const struct external_ref *r = (const struct external_ref *)p;
6907   return htab_hash_pointer (r->type);
6908 }
6909
6910 /* Compare external_refs.  */
6911
6912 static int
6913 external_ref_eq (const void *p1, const void *p2)
6914 {
6915   const struct external_ref *r1 = (const struct external_ref *)p1;
6916   const struct external_ref *r2 = (const struct external_ref *)p2;
6917   return r1->type == r2->type;
6918 }
6919
6920 /* Return a pointer to the external_ref for references to DIE.  */
6921
6922 static struct external_ref *
6923 lookup_external_ref (htab_t map, dw_die_ref die)
6924 {
6925   struct external_ref ref, *ref_p;
6926   void ** slot;
6927
6928   ref.type = die;
6929   slot = htab_find_slot (map, &ref, INSERT);
6930   if (*slot != HTAB_EMPTY_ENTRY)
6931     return (struct external_ref *) *slot;
6932
6933   ref_p = XCNEW (struct external_ref);
6934   ref_p->type = die;
6935   *slot = ref_p;
6936   return ref_p;
6937 }
6938
6939 /* Subroutine of optimize_external_refs, below.
6940
6941    If we see a type skeleton, record it as our stub.  If we see external
6942    references, remember how many we've seen.  */
6943
6944 static void
6945 optimize_external_refs_1 (dw_die_ref die, htab_t map)
6946 {
6947   dw_die_ref c;
6948   dw_attr_ref a;
6949   unsigned ix;
6950   struct external_ref *ref_p;
6951
6952   if (is_type_die (die)
6953       && (c = get_AT_ref (die, DW_AT_signature)))
6954     {
6955       /* This is a local skeleton; use it for local references.  */
6956       ref_p = lookup_external_ref (map, c);
6957       ref_p->stub = die;
6958     }
6959
6960   /* Scan the DIE references, and remember any that refer to DIEs from
6961      other CUs (i.e. those which are not marked).  */
6962   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6963     if (AT_class (a) == dw_val_class_die_ref
6964         && (c = AT_ref (a))->die_mark == 0
6965         && is_type_die (c))
6966       {
6967         ref_p = lookup_external_ref (map, c);
6968         ref_p->n_refs++;
6969       }
6970
6971   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
6972 }
6973
6974 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
6975    points to an external_ref, DATA is the CU we're processing.  If we don't
6976    already have a local stub, and we have multiple refs, build a stub.  */
6977
6978 static int
6979 build_local_stub (void **slot, void *data)
6980 {
6981   struct external_ref *ref_p = (struct external_ref *)*slot;
6982
6983   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
6984     {
6985       /* We have multiple references to this type, so build a small stub.
6986          Both of these forms are a bit dodgy from the perspective of the
6987          DWARF standard, since technically they should have names.  */
6988       dw_die_ref cu = (dw_die_ref) data;
6989       dw_die_ref type = ref_p->type;
6990       dw_die_ref stub = NULL;
6991
6992       if (type->comdat_type_p)
6993         {
6994           /* If we refer to this type via sig8, use AT_signature.  */
6995           stub = new_die (type->die_tag, cu, NULL_TREE);
6996           add_AT_die_ref (stub, DW_AT_signature, type);
6997         }
6998       else
6999         {
7000           /* Otherwise, use a typedef with no name.  */
7001           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7002           add_AT_die_ref (stub, DW_AT_type, type);
7003         }
7004
7005       stub->die_mark++;
7006       ref_p->stub = stub;
7007     }
7008   return 1;
7009 }
7010
7011 /* DIE is a unit; look through all the DIE references to see if there are
7012    any external references to types, and if so, create local stubs for
7013    them which will be applied in build_abbrev_table.  This is useful because
7014    references to local DIEs are smaller.  */
7015
7016 static htab_t
7017 optimize_external_refs (dw_die_ref die)
7018 {
7019   htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7020   optimize_external_refs_1 (die, map);
7021   htab_traverse (map, build_local_stub, die);
7022   return map;
7023 }
7024
7025 /* The format of each DIE (and its attribute value pairs) is encoded in an
7026    abbreviation table.  This routine builds the abbreviation table and assigns
7027    a unique abbreviation id for each abbreviation entry.  The children of each
7028    die are visited recursively.  */
7029
7030 static void
7031 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7032 {
7033   unsigned long abbrev_id;
7034   unsigned int n_alloc;
7035   dw_die_ref c;
7036   dw_attr_ref a;
7037   unsigned ix;
7038
7039   /* Scan the DIE references, and replace any that refer to
7040      DIEs from other CUs (i.e. those which are not marked) with
7041      the local stubs we built in optimize_external_refs.  */
7042   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7043     if (AT_class (a) == dw_val_class_die_ref
7044         && (c = AT_ref (a))->die_mark == 0)
7045       {
7046         struct external_ref *ref_p;
7047         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7048
7049         ref_p = lookup_external_ref (extern_map, c);
7050         if (ref_p->stub && ref_p->stub != die)
7051           change_AT_die_ref (a, ref_p->stub);
7052         else
7053           /* We aren't changing this reference, so mark it external.  */
7054           set_AT_ref_external (a, 1);
7055       }
7056
7057   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7058     {
7059       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7060       dw_attr_ref die_a, abbrev_a;
7061       unsigned ix;
7062       bool ok = true;
7063
7064       if (abbrev->die_tag != die->die_tag)
7065         continue;
7066       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7067         continue;
7068
7069       if (VEC_length (dw_attr_node, abbrev->die_attr)
7070           != VEC_length (dw_attr_node, die->die_attr))
7071         continue;
7072
7073       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7074         {
7075           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7076           if ((abbrev_a->dw_attr != die_a->dw_attr)
7077               || (value_format (abbrev_a) != value_format (die_a)))
7078             {
7079               ok = false;
7080               break;
7081             }
7082         }
7083       if (ok)
7084         break;
7085     }
7086
7087   if (abbrev_id >= abbrev_die_table_in_use)
7088     {
7089       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7090         {
7091           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7092           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7093                                             n_alloc);
7094
7095           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7096                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7097           abbrev_die_table_allocated = n_alloc;
7098         }
7099
7100       ++abbrev_die_table_in_use;
7101       abbrev_die_table[abbrev_id] = die;
7102     }
7103
7104   die->die_abbrev = abbrev_id;
7105   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7106 }
7107 \f
7108 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7109
7110 static int
7111 constant_size (unsigned HOST_WIDE_INT value)
7112 {
7113   int log;
7114
7115   if (value == 0)
7116     log = 0;
7117   else
7118     log = floor_log2 (value);
7119
7120   log = log / 8;
7121   log = 1 << (floor_log2 (log) + 1);
7122
7123   return log;
7124 }
7125
7126 /* Return the size of a DIE as it is represented in the
7127    .debug_info section.  */
7128
7129 static unsigned long
7130 size_of_die (dw_die_ref die)
7131 {
7132   unsigned long size = 0;
7133   dw_attr_ref a;
7134   unsigned ix;
7135
7136   size += size_of_uleb128 (die->die_abbrev);
7137   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7138     {
7139       switch (AT_class (a))
7140         {
7141         case dw_val_class_addr:
7142           size += DWARF2_ADDR_SIZE;
7143           break;
7144         case dw_val_class_offset:
7145           size += DWARF_OFFSET_SIZE;
7146           break;
7147         case dw_val_class_loc:
7148           {
7149             unsigned long lsize = size_of_locs (AT_loc (a));
7150
7151             /* Block length.  */
7152             if (dwarf_version >= 4)
7153               size += size_of_uleb128 (lsize);
7154             else
7155               size += constant_size (lsize);
7156             size += lsize;
7157           }
7158           break;
7159         case dw_val_class_loc_list:
7160           size += DWARF_OFFSET_SIZE;
7161           break;
7162         case dw_val_class_range_list:
7163           size += DWARF_OFFSET_SIZE;
7164           break;
7165         case dw_val_class_const:
7166           size += size_of_sleb128 (AT_int (a));
7167           break;
7168         case dw_val_class_unsigned_const:
7169           {
7170             int csize = constant_size (AT_unsigned (a));
7171             if (dwarf_version == 3
7172                 && a->dw_attr == DW_AT_data_member_location
7173                 && csize >= 4)
7174               size += size_of_uleb128 (AT_unsigned (a));
7175             else
7176               size += csize;
7177           }
7178           break;
7179         case dw_val_class_const_double:
7180           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7181           if (HOST_BITS_PER_WIDE_INT >= 64)
7182             size++; /* block */
7183           break;
7184         case dw_val_class_vec:
7185           size += constant_size (a->dw_attr_val.v.val_vec.length
7186                                  * a->dw_attr_val.v.val_vec.elt_size)
7187                   + a->dw_attr_val.v.val_vec.length
7188                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7189           break;
7190         case dw_val_class_flag:
7191           if (dwarf_version >= 4)
7192             /* Currently all add_AT_flag calls pass in 1 as last argument,
7193                so DW_FORM_flag_present can be used.  If that ever changes,
7194                we'll need to use DW_FORM_flag and have some optimization
7195                in build_abbrev_table that will change those to
7196                DW_FORM_flag_present if it is set to 1 in all DIEs using
7197                the same abbrev entry.  */
7198             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7199           else
7200             size += 1;
7201           break;
7202         case dw_val_class_die_ref:
7203           if (AT_ref_external (a))
7204             {
7205               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7206                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7207                  is sized by target address length, whereas in DWARF3
7208                  it's always sized as an offset.  */
7209               if (use_debug_types)
7210                 size += DWARF_TYPE_SIGNATURE_SIZE;
7211               else if (dwarf_version == 2)
7212                 size += DWARF2_ADDR_SIZE;
7213               else
7214                 size += DWARF_OFFSET_SIZE;
7215             }
7216           else
7217             size += DWARF_OFFSET_SIZE;
7218           break;
7219         case dw_val_class_fde_ref:
7220           size += DWARF_OFFSET_SIZE;
7221           break;
7222         case dw_val_class_lbl_id:
7223           size += DWARF2_ADDR_SIZE;
7224           break;
7225         case dw_val_class_lineptr:
7226         case dw_val_class_macptr:
7227           size += DWARF_OFFSET_SIZE;
7228           break;
7229         case dw_val_class_str:
7230           if (AT_string_form (a) == DW_FORM_strp)
7231             size += DWARF_OFFSET_SIZE;
7232           else
7233             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7234           break;
7235         case dw_val_class_file:
7236           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7237           break;
7238         case dw_val_class_data8:
7239           size += 8;
7240           break;
7241         case dw_val_class_vms_delta:
7242           size += DWARF_OFFSET_SIZE;
7243           break;
7244         default:
7245           gcc_unreachable ();
7246         }
7247     }
7248
7249   return size;
7250 }
7251
7252 /* Size the debugging information associated with a given DIE.  Visits the
7253    DIE's children recursively.  Updates the global variable next_die_offset, on
7254    each time through.  Uses the current value of next_die_offset to update the
7255    die_offset field in each DIE.  */
7256
7257 static void
7258 calc_die_sizes (dw_die_ref die)
7259 {
7260   dw_die_ref c;
7261
7262   gcc_assert (die->die_offset == 0
7263               || (unsigned long int) die->die_offset == next_die_offset);
7264   die->die_offset = next_die_offset;
7265   next_die_offset += size_of_die (die);
7266
7267   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7268
7269   if (die->die_child != NULL)
7270     /* Count the null byte used to terminate sibling lists.  */
7271     next_die_offset += 1;
7272 }
7273
7274 /* Size just the base type children at the start of the CU.
7275    This is needed because build_abbrev needs to size locs
7276    and sizing of type based stack ops needs to know die_offset
7277    values for the base types.  */
7278
7279 static void
7280 calc_base_type_die_sizes (void)
7281 {
7282   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7283   unsigned int i;
7284   dw_die_ref base_type;
7285 #if ENABLE_ASSERT_CHECKING
7286   dw_die_ref prev = comp_unit_die ()->die_child;
7287 #endif
7288
7289   die_offset += size_of_die (comp_unit_die ());
7290   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7291     {
7292 #if ENABLE_ASSERT_CHECKING
7293       gcc_assert (base_type->die_offset == 0
7294                   && prev->die_sib == base_type
7295                   && base_type->die_child == NULL
7296                   && base_type->die_abbrev);
7297       prev = base_type;
7298 #endif
7299       base_type->die_offset = die_offset;
7300       die_offset += size_of_die (base_type);
7301     }
7302 }
7303
7304 /* Set the marks for a die and its children.  We do this so
7305    that we know whether or not a reference needs to use FORM_ref_addr; only
7306    DIEs in the same CU will be marked.  We used to clear out the offset
7307    and use that as the flag, but ran into ordering problems.  */
7308
7309 static void
7310 mark_dies (dw_die_ref die)
7311 {
7312   dw_die_ref c;
7313
7314   gcc_assert (!die->die_mark);
7315
7316   die->die_mark = 1;
7317   FOR_EACH_CHILD (die, c, mark_dies (c));
7318 }
7319
7320 /* Clear the marks for a die and its children.  */
7321
7322 static void
7323 unmark_dies (dw_die_ref die)
7324 {
7325   dw_die_ref c;
7326
7327   if (! use_debug_types)
7328     gcc_assert (die->die_mark);
7329
7330   die->die_mark = 0;
7331   FOR_EACH_CHILD (die, c, unmark_dies (c));
7332 }
7333
7334 /* Clear the marks for a die, its children and referred dies.  */
7335
7336 static void
7337 unmark_all_dies (dw_die_ref die)
7338 {
7339   dw_die_ref c;
7340   dw_attr_ref a;
7341   unsigned ix;
7342
7343   if (!die->die_mark)
7344     return;
7345   die->die_mark = 0;
7346
7347   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7348
7349   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7350     if (AT_class (a) == dw_val_class_die_ref)
7351       unmark_all_dies (AT_ref (a));
7352 }
7353
7354 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7355    generated for the compilation unit.  */
7356
7357 static unsigned long
7358 size_of_pubnames (VEC (pubname_entry, gc) * names)
7359 {
7360   unsigned long size;
7361   unsigned i;
7362   pubname_ref p;
7363
7364   size = DWARF_PUBNAMES_HEADER_SIZE;
7365   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7366     if (names != pubtype_table
7367         || p->die->die_offset != 0
7368         || !flag_eliminate_unused_debug_types)
7369       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7370
7371   size += DWARF_OFFSET_SIZE;
7372   return size;
7373 }
7374
7375 /* Return the size of the information in the .debug_aranges section.  */
7376
7377 static unsigned long
7378 size_of_aranges (void)
7379 {
7380   unsigned long size;
7381
7382   size = DWARF_ARANGES_HEADER_SIZE;
7383
7384   /* Count the address/length pair for this compilation unit.  */
7385   if (text_section_used)
7386     size += 2 * DWARF2_ADDR_SIZE;
7387   if (cold_text_section_used)
7388     size += 2 * DWARF2_ADDR_SIZE;
7389   if (have_multiple_function_sections)
7390     {
7391       unsigned fde_idx;
7392       dw_fde_ref fde;
7393
7394       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7395         {
7396           if (!fde->in_std_section)
7397             size += 2 * DWARF2_ADDR_SIZE;
7398           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7399             size += 2 * DWARF2_ADDR_SIZE;
7400         }
7401     }
7402
7403   /* Count the two zero words used to terminated the address range table.  */
7404   size += 2 * DWARF2_ADDR_SIZE;
7405   return size;
7406 }
7407 \f
7408 /* Select the encoding of an attribute value.  */
7409
7410 static enum dwarf_form
7411 value_format (dw_attr_ref a)
7412 {
7413   switch (a->dw_attr_val.val_class)
7414     {
7415     case dw_val_class_addr:
7416       /* Only very few attributes allow DW_FORM_addr.  */
7417       switch (a->dw_attr)
7418         {
7419         case DW_AT_low_pc:
7420         case DW_AT_high_pc:
7421         case DW_AT_entry_pc:
7422         case DW_AT_trampoline:
7423           return DW_FORM_addr;
7424         default:
7425           break;
7426         }
7427       switch (DWARF2_ADDR_SIZE)
7428         {
7429         case 1:
7430           return DW_FORM_data1;
7431         case 2:
7432           return DW_FORM_data2;
7433         case 4:
7434           return DW_FORM_data4;
7435         case 8:
7436           return DW_FORM_data8;
7437         default:
7438           gcc_unreachable ();
7439         }
7440     case dw_val_class_range_list:
7441     case dw_val_class_loc_list:
7442       if (dwarf_version >= 4)
7443         return DW_FORM_sec_offset;
7444       /* FALLTHRU */
7445     case dw_val_class_vms_delta:
7446     case dw_val_class_offset:
7447       switch (DWARF_OFFSET_SIZE)
7448         {
7449         case 4:
7450           return DW_FORM_data4;
7451         case 8:
7452           return DW_FORM_data8;
7453         default:
7454           gcc_unreachable ();
7455         }
7456     case dw_val_class_loc:
7457       if (dwarf_version >= 4)
7458         return DW_FORM_exprloc;
7459       switch (constant_size (size_of_locs (AT_loc (a))))
7460         {
7461         case 1:
7462           return DW_FORM_block1;
7463         case 2:
7464           return DW_FORM_block2;
7465         default:
7466           gcc_unreachable ();
7467         }
7468     case dw_val_class_const:
7469       return DW_FORM_sdata;
7470     case dw_val_class_unsigned_const:
7471       switch (constant_size (AT_unsigned (a)))
7472         {
7473         case 1:
7474           return DW_FORM_data1;
7475         case 2:
7476           return DW_FORM_data2;
7477         case 4:
7478           /* In DWARF3 DW_AT_data_member_location with
7479              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7480              constant, so we need to use DW_FORM_udata if we need
7481              a large constant.  */
7482           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7483             return DW_FORM_udata;
7484           return DW_FORM_data4;
7485         case 8:
7486           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7487             return DW_FORM_udata;
7488           return DW_FORM_data8;
7489         default:
7490           gcc_unreachable ();
7491         }
7492     case dw_val_class_const_double:
7493       switch (HOST_BITS_PER_WIDE_INT)
7494         {
7495         case 8:
7496           return DW_FORM_data2;
7497         case 16:
7498           return DW_FORM_data4;
7499         case 32:
7500           return DW_FORM_data8;
7501         case 64:
7502         default:
7503           return DW_FORM_block1;
7504         }
7505     case dw_val_class_vec:
7506       switch (constant_size (a->dw_attr_val.v.val_vec.length
7507                              * a->dw_attr_val.v.val_vec.elt_size))
7508         {
7509         case 1:
7510           return DW_FORM_block1;
7511         case 2:
7512           return DW_FORM_block2;
7513         case 4:
7514           return DW_FORM_block4;
7515         default:
7516           gcc_unreachable ();
7517         }
7518     case dw_val_class_flag:
7519       if (dwarf_version >= 4)
7520         {
7521           /* Currently all add_AT_flag calls pass in 1 as last argument,
7522              so DW_FORM_flag_present can be used.  If that ever changes,
7523              we'll need to use DW_FORM_flag and have some optimization
7524              in build_abbrev_table that will change those to
7525              DW_FORM_flag_present if it is set to 1 in all DIEs using
7526              the same abbrev entry.  */
7527           gcc_assert (a->dw_attr_val.v.val_flag == 1);
7528           return DW_FORM_flag_present;
7529         }
7530       return DW_FORM_flag;
7531     case dw_val_class_die_ref:
7532       if (AT_ref_external (a))
7533         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7534       else
7535         return DW_FORM_ref;
7536     case dw_val_class_fde_ref:
7537       return DW_FORM_data;
7538     case dw_val_class_lbl_id:
7539       return DW_FORM_addr;
7540     case dw_val_class_lineptr:
7541     case dw_val_class_macptr:
7542       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
7543     case dw_val_class_str:
7544       return AT_string_form (a);
7545     case dw_val_class_file:
7546       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7547         {
7548         case 1:
7549           return DW_FORM_data1;
7550         case 2:
7551           return DW_FORM_data2;
7552         case 4:
7553           return DW_FORM_data4;
7554         default:
7555           gcc_unreachable ();
7556         }
7557
7558     case dw_val_class_data8:
7559       return DW_FORM_data8;
7560
7561     default:
7562       gcc_unreachable ();
7563     }
7564 }
7565
7566 /* Output the encoding of an attribute value.  */
7567
7568 static void
7569 output_value_format (dw_attr_ref a)
7570 {
7571   enum dwarf_form form = value_format (a);
7572
7573   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7574 }
7575
7576 /* Output the .debug_abbrev section which defines the DIE abbreviation
7577    table.  */
7578
7579 static void
7580 output_abbrev_section (void)
7581 {
7582   unsigned long abbrev_id;
7583
7584   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7585     {
7586       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7587       unsigned ix;
7588       dw_attr_ref a_attr;
7589
7590       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7591       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7592                                    dwarf_tag_name (abbrev->die_tag));
7593
7594       if (abbrev->die_child != NULL)
7595         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7596       else
7597         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7598
7599       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7600            ix++)
7601         {
7602           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7603                                        dwarf_attr_name (a_attr->dw_attr));
7604           output_value_format (a_attr);
7605         }
7606
7607       dw2_asm_output_data (1, 0, NULL);
7608       dw2_asm_output_data (1, 0, NULL);
7609     }
7610
7611   /* Terminate the table.  */
7612   dw2_asm_output_data (1, 0, NULL);
7613 }
7614
7615 /* Output a symbol we can use to refer to this DIE from another CU.  */
7616
7617 static inline void
7618 output_die_symbol (dw_die_ref die)
7619 {
7620   const char *sym = die->die_id.die_symbol;
7621
7622   gcc_assert (!die->comdat_type_p);
7623
7624   if (sym == 0)
7625     return;
7626
7627   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7628     /* We make these global, not weak; if the target doesn't support
7629        .linkonce, it doesn't support combining the sections, so debugging
7630        will break.  */
7631     targetm.asm_out.globalize_label (asm_out_file, sym);
7632
7633   ASM_OUTPUT_LABEL (asm_out_file, sym);
7634 }
7635
7636 /* Return a new location list, given the begin and end range, and the
7637    expression.  */
7638
7639 static inline dw_loc_list_ref
7640 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7641               const char *section)
7642 {
7643   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
7644
7645   retlist->begin = begin;
7646   retlist->end = end;
7647   retlist->expr = expr;
7648   retlist->section = section;
7649
7650   return retlist;
7651 }
7652
7653 /* Generate a new internal symbol for this location list node, if it
7654    hasn't got one yet.  */
7655
7656 static inline void
7657 gen_llsym (dw_loc_list_ref list)
7658 {
7659   gcc_assert (!list->ll_symbol);
7660   list->ll_symbol = gen_internal_sym ("LLST");
7661 }
7662
7663 /* Output the location list given to us.  */
7664
7665 static void
7666 output_loc_list (dw_loc_list_ref list_head)
7667 {
7668   dw_loc_list_ref curr = list_head;
7669
7670   if (list_head->emitted)
7671     return;
7672   list_head->emitted = true;
7673
7674   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7675
7676   /* Walk the location list, and output each range + expression.  */
7677   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7678     {
7679       unsigned long size;
7680       /* Don't output an entry that starts and ends at the same address.  */
7681       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
7682         continue;
7683       size = size_of_locs (curr->expr);
7684       /* If the expression is too large, drop it on the floor.  We could
7685          perhaps put it into DW_TAG_dwarf_procedure and refer to that
7686          in the expression, but >= 64KB expressions for a single value
7687          in a single range are unlikely very useful.  */
7688       if (size > 0xffff)
7689         continue;
7690       if (!have_multiple_function_sections)
7691         {
7692           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7693                                 "Location list begin address (%s)",
7694                                 list_head->ll_symbol);
7695           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7696                                 "Location list end address (%s)",
7697                                 list_head->ll_symbol);
7698         }
7699       else
7700         {
7701           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7702                                "Location list begin address (%s)",
7703                                list_head->ll_symbol);
7704           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7705                                "Location list end address (%s)",
7706                                list_head->ll_symbol);
7707         }
7708
7709       /* Output the block length for this list of location operations.  */
7710       gcc_assert (size <= 0xffff);
7711       dw2_asm_output_data (2, size, "%s", "Location expression size");
7712
7713       output_loc_sequence (curr->expr, -1);
7714     }
7715
7716   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7717                        "Location list terminator begin (%s)",
7718                        list_head->ll_symbol);
7719   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7720                        "Location list terminator end (%s)",
7721                        list_head->ll_symbol);
7722 }
7723
7724 /* Output a type signature.  */
7725
7726 static inline void
7727 output_signature (const char *sig, const char *name)
7728 {
7729   int i;
7730
7731   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
7732     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
7733 }
7734
7735 /* Output the DIE and its attributes.  Called recursively to generate
7736    the definitions of each child DIE.  */
7737
7738 static void
7739 output_die (dw_die_ref die)
7740 {
7741   dw_attr_ref a;
7742   dw_die_ref c;
7743   unsigned long size;
7744   unsigned ix;
7745
7746   /* If someone in another CU might refer to us, set up a symbol for
7747      them to point to.  */
7748   if (! die->comdat_type_p && die->die_id.die_symbol)
7749     output_die_symbol (die);
7750
7751   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
7752                                (unsigned long)die->die_offset,
7753                                dwarf_tag_name (die->die_tag));
7754
7755   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7756     {
7757       const char *name = dwarf_attr_name (a->dw_attr);
7758
7759       switch (AT_class (a))
7760         {
7761         case dw_val_class_addr:
7762           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7763           break;
7764
7765         case dw_val_class_offset:
7766           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7767                                "%s", name);
7768           break;
7769
7770         case dw_val_class_range_list:
7771           {
7772             char *p = strchr (ranges_section_label, '\0');
7773
7774             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7775                      a->dw_attr_val.v.val_offset);
7776             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7777                                    debug_ranges_section, "%s", name);
7778             *p = '\0';
7779           }
7780           break;
7781
7782         case dw_val_class_loc:
7783           size = size_of_locs (AT_loc (a));
7784
7785           /* Output the block length for this list of location operations.  */
7786           if (dwarf_version >= 4)
7787             dw2_asm_output_data_uleb128 (size, "%s", name);
7788           else
7789             dw2_asm_output_data (constant_size (size), size, "%s", name);
7790
7791           output_loc_sequence (AT_loc (a), -1);
7792           break;
7793
7794         case dw_val_class_const:
7795           /* ??? It would be slightly more efficient to use a scheme like is
7796              used for unsigned constants below, but gdb 4.x does not sign
7797              extend.  Gdb 5.x does sign extend.  */
7798           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7799           break;
7800
7801         case dw_val_class_unsigned_const:
7802           {
7803             int csize = constant_size (AT_unsigned (a));
7804             if (dwarf_version == 3
7805                 && a->dw_attr == DW_AT_data_member_location
7806                 && csize >= 4)
7807               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
7808             else
7809               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
7810           }
7811           break;
7812
7813         case dw_val_class_const_double:
7814           {
7815             unsigned HOST_WIDE_INT first, second;
7816
7817             if (HOST_BITS_PER_WIDE_INT >= 64)
7818               dw2_asm_output_data (1,
7819                                    HOST_BITS_PER_DOUBLE_INT
7820                                    / HOST_BITS_PER_CHAR,
7821                                    NULL);
7822
7823             if (WORDS_BIG_ENDIAN)
7824               {
7825                 first = a->dw_attr_val.v.val_double.high;
7826                 second = a->dw_attr_val.v.val_double.low;
7827               }
7828             else
7829               {
7830                 first = a->dw_attr_val.v.val_double.low;
7831                 second = a->dw_attr_val.v.val_double.high;
7832               }
7833
7834             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
7835                                  first, name);
7836             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
7837                                  second, NULL);
7838           }
7839           break;
7840
7841         case dw_val_class_vec:
7842           {
7843             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7844             unsigned int len = a->dw_attr_val.v.val_vec.length;
7845             unsigned int i;
7846             unsigned char *p;
7847
7848             dw2_asm_output_data (constant_size (len * elt_size),
7849                                  len * elt_size, "%s", name);
7850             if (elt_size > sizeof (HOST_WIDE_INT))
7851               {
7852                 elt_size /= 2;
7853                 len *= 2;
7854               }
7855             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7856                  i < len;
7857                  i++, p += elt_size)
7858               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7859                                    "fp or vector constant word %u", i);
7860             break;
7861           }
7862
7863         case dw_val_class_flag:
7864           if (dwarf_version >= 4)
7865             {
7866               /* Currently all add_AT_flag calls pass in 1 as last argument,
7867                  so DW_FORM_flag_present can be used.  If that ever changes,
7868                  we'll need to use DW_FORM_flag and have some optimization
7869                  in build_abbrev_table that will change those to
7870                  DW_FORM_flag_present if it is set to 1 in all DIEs using
7871                  the same abbrev entry.  */
7872               gcc_assert (AT_flag (a) == 1);
7873               if (flag_debug_asm)
7874                 fprintf (asm_out_file, "\t\t\t%s %s\n",
7875                          ASM_COMMENT_START, name);
7876               break;
7877             }
7878           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7879           break;
7880
7881         case dw_val_class_loc_list:
7882           {
7883             char *sym = AT_loc_list (a)->ll_symbol;
7884
7885             gcc_assert (sym);
7886             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7887                                    "%s", name);
7888           }
7889           break;
7890
7891         case dw_val_class_die_ref:
7892           if (AT_ref_external (a))
7893             {
7894               if (AT_ref (a)->comdat_type_p)
7895                 {
7896                   comdat_type_node_ref type_node =
7897                     AT_ref (a)->die_id.die_type_node;
7898
7899                   gcc_assert (type_node);
7900                   output_signature (type_node->signature, name);
7901                 }
7902               else
7903                 {
7904                   const char *sym = AT_ref (a)->die_id.die_symbol;
7905                   int size;
7906
7907                   gcc_assert (sym);
7908                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
7909                      length, whereas in DWARF3 it's always sized as an
7910                      offset.  */
7911                   if (dwarf_version == 2)
7912                     size = DWARF2_ADDR_SIZE;
7913                   else
7914                     size = DWARF_OFFSET_SIZE;
7915                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
7916                                          name);
7917                 }
7918             }
7919           else
7920             {
7921               gcc_assert (AT_ref (a)->die_offset);
7922               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7923                                    "%s", name);
7924             }
7925           break;
7926
7927         case dw_val_class_fde_ref:
7928           {
7929             char l1[20];
7930
7931             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7932                                          a->dw_attr_val.v.val_fde_index * 2);
7933             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7934                                    "%s", name);
7935           }
7936           break;
7937
7938         case dw_val_class_vms_delta:
7939           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
7940                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
7941                                     "%s", name);
7942           break;
7943
7944         case dw_val_class_lbl_id:
7945           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7946           break;
7947
7948         case dw_val_class_lineptr:
7949           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7950                                  debug_line_section, "%s", name);
7951           break;
7952
7953         case dw_val_class_macptr:
7954           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7955                                  debug_macinfo_section, "%s", name);
7956           break;
7957
7958         case dw_val_class_str:
7959           if (AT_string_form (a) == DW_FORM_strp)
7960             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7961                                    a->dw_attr_val.v.val_str->label,
7962                                    debug_str_section,
7963                                    "%s: \"%s\"", name, AT_string (a));
7964           else
7965             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7966           break;
7967
7968         case dw_val_class_file:
7969           {
7970             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7971
7972             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7973                                  a->dw_attr_val.v.val_file->filename);
7974             break;
7975           }
7976
7977         case dw_val_class_data8:
7978           {
7979             int i;
7980
7981             for (i = 0; i < 8; i++)
7982               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
7983                                    i == 0 ? "%s" : NULL, name);
7984             break;
7985           }
7986
7987         default:
7988           gcc_unreachable ();
7989         }
7990     }
7991
7992   FOR_EACH_CHILD (die, c, output_die (c));
7993
7994   /* Add null byte to terminate sibling list.  */
7995   if (die->die_child != NULL)
7996     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
7997                          (unsigned long) die->die_offset);
7998 }
7999
8000 /* Output the compilation unit that appears at the beginning of the
8001    .debug_info section, and precedes the DIE descriptions.  */
8002
8003 static void
8004 output_compilation_unit_header (void)
8005 {
8006   int ver = dwarf_version;
8007
8008   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8009     dw2_asm_output_data (4, 0xffffffff,
8010       "Initial length escape value indicating 64-bit DWARF extension");
8011   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8012                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8013                        "Length of Compilation Unit Info");
8014   dw2_asm_output_data (2, ver, "DWARF version number");
8015   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8016                          debug_abbrev_section,
8017                          "Offset Into Abbrev. Section");
8018   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8019 }
8020
8021 /* Output the compilation unit DIE and its children.  */
8022
8023 static void
8024 output_comp_unit (dw_die_ref die, int output_if_empty)
8025 {
8026   const char *secname, *oldsym;
8027   char *tmp;
8028   htab_t extern_map;
8029
8030   /* Unless we are outputting main CU, we may throw away empty ones.  */
8031   if (!output_if_empty && die->die_child == NULL)
8032     return;
8033
8034   /* Even if there are no children of this DIE, we must output the information
8035      about the compilation unit.  Otherwise, on an empty translation unit, we
8036      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8037      will then complain when examining the file.  First mark all the DIEs in
8038      this CU so we know which get local refs.  */
8039   mark_dies (die);
8040
8041   extern_map = optimize_external_refs (die);
8042
8043   build_abbrev_table (die, extern_map);
8044
8045   htab_delete (extern_map);
8046
8047   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8048   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8049   calc_die_sizes (die);
8050
8051   oldsym = die->die_id.die_symbol;
8052   if (oldsym)
8053     {
8054       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8055
8056       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8057       secname = tmp;
8058       die->die_id.die_symbol = NULL;
8059       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8060     }
8061   else
8062     {
8063       switch_to_section (debug_info_section);
8064       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8065       info_section_emitted = true;
8066     }
8067
8068   /* Output debugging information.  */
8069   output_compilation_unit_header ();
8070   output_die (die);
8071
8072   /* Leave the marks on the main CU, so we can check them in
8073      output_pubnames.  */
8074   if (oldsym)
8075     {
8076       unmark_dies (die);
8077       die->die_id.die_symbol = oldsym;
8078     }
8079 }
8080
8081 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8082    and .debug_pubtypes.  This is configured per-target, but can be
8083    overridden by the -gpubnames or -gno-pubnames options.  */
8084
8085 static inline bool
8086 want_pubnames (void)
8087 {
8088   return (debug_generate_pub_sections != -1
8089           ? debug_generate_pub_sections
8090           : targetm.want_debug_pub_sections);
8091 }
8092
8093 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
8094
8095 static void
8096 add_AT_pubnames (dw_die_ref die)
8097 {
8098   if (want_pubnames ())
8099     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8100 }
8101
8102 /* Output a comdat type unit DIE and its children.  */
8103
8104 static void
8105 output_comdat_type_unit (comdat_type_node *node)
8106 {
8107   const char *secname;
8108   char *tmp;
8109   int i;
8110 #if defined (OBJECT_FORMAT_ELF)
8111   tree comdat_key;
8112 #endif
8113   htab_t extern_map;
8114
8115   /* First mark all the DIEs in this CU so we know which get local refs.  */
8116   mark_dies (node->root_die);
8117
8118   extern_map = optimize_external_refs (node->root_die);
8119
8120   build_abbrev_table (node->root_die, extern_map);
8121
8122   htab_delete (extern_map);
8123
8124   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8125   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8126   calc_die_sizes (node->root_die);
8127
8128 #if defined (OBJECT_FORMAT_ELF)
8129   secname = ".debug_types";
8130   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8131   sprintf (tmp, "wt.");
8132   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8133     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8134   comdat_key = get_identifier (tmp);
8135   targetm.asm_out.named_section (secname,
8136                                  SECTION_DEBUG | SECTION_LINKONCE,
8137                                  comdat_key);
8138 #else
8139   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8140   sprintf (tmp, ".gnu.linkonce.wt.");
8141   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8142     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8143   secname = tmp;
8144   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8145 #endif
8146
8147   /* Output debugging information.  */
8148   output_compilation_unit_header ();
8149   output_signature (node->signature, "Type Signature");
8150   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8151                        "Offset to Type DIE");
8152   output_die (node->root_die);
8153
8154   unmark_dies (node->root_die);
8155 }
8156
8157 /* Return the DWARF2/3 pubname associated with a decl.  */
8158
8159 static const char *
8160 dwarf2_name (tree decl, int scope)
8161 {
8162   if (DECL_NAMELESS (decl))
8163     return NULL;
8164   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8165 }
8166
8167 /* Add a new entry to .debug_pubnames if appropriate.  */
8168
8169 static void
8170 add_pubname_string (const char *str, dw_die_ref die)
8171 {
8172   pubname_entry e;
8173
8174   e.die = die;
8175   e.name = xstrdup (str);
8176   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8177 }
8178
8179 static void
8180 add_pubname (tree decl, dw_die_ref die)
8181 {
8182   if (!want_pubnames ())
8183     return;
8184
8185   /* Don't add items to the table when we expect that the consumer will have
8186      just read the enclosing die.  For example, if the consumer is looking at a
8187      class_member, it will either be inside the class already, or will have just
8188      looked up the class to find the member.  Either way, searching the class is
8189      faster than searching the index.  */
8190   if ((TREE_PUBLIC (decl) && !is_class_die (die->die_parent))
8191       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8192     {
8193       const char *name = dwarf2_name (decl, 1);
8194
8195       if (name)
8196         add_pubname_string (name, die);
8197     }
8198 }
8199
8200 /* Add an enumerator to the pubnames section.  */
8201
8202 static void
8203 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8204 {
8205   pubname_entry e;
8206
8207   gcc_assert (scope_name);
8208   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8209   e.die = die;
8210   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8211 }
8212
8213 /* Add a new entry to .debug_pubtypes if appropriate.  */
8214
8215 static void
8216 add_pubtype (tree decl, dw_die_ref die)
8217 {
8218   pubname_entry e;
8219
8220   if (!want_pubnames ())
8221     return;
8222
8223   if ((TREE_PUBLIC (decl)
8224        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8225       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8226     {
8227       tree scope = NULL;
8228       const char *scope_name = "";
8229       const char *sep = is_cxx () ? "::" : ".";
8230       const char *name;
8231
8232       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8233       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8234         {
8235           scope_name = lang_hooks.dwarf_name (scope, 1);
8236           if (scope_name != NULL && scope_name[0] != '\0')
8237             scope_name = concat (scope_name, sep, NULL);
8238           else
8239             scope_name = "";
8240         }
8241
8242       if (TYPE_P (decl))
8243         name = type_tag (decl);
8244       else
8245         name = lang_hooks.dwarf_name (decl, 1);
8246
8247       /* If we don't have a name for the type, there's no point in adding
8248          it to the table.  */
8249       if (name != NULL && name[0] != '\0')
8250         {
8251           e.die = die;
8252           e.name = concat (scope_name, name, NULL);
8253           VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8254         }
8255
8256       /* Although it might be more consistent to add the pubinfo for the
8257          enumerators as their dies are created, they should only be added if the
8258          enum type meets the criteria above.  So rather than re-check the parent
8259          enum type whenever an enumerator die is created, just output them all
8260          here.  This isn't protected by the name conditional because anonymous
8261          enums don't have names.  */
8262       if (die->die_tag == DW_TAG_enumeration_type)
8263         {
8264           dw_die_ref c;
8265
8266           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8267         }
8268     }
8269 }
8270
8271 /* Output the public names table used to speed up access to externally
8272    visible names; or the public types table used to find type definitions.  */
8273
8274 static void
8275 output_pubnames (VEC (pubname_entry, gc) * names)
8276 {
8277   unsigned i;
8278   unsigned long pubnames_length = size_of_pubnames (names);
8279   pubname_ref pub;
8280
8281   if (!want_pubnames () || !info_section_emitted)
8282     return;
8283   if (names == pubname_table)
8284     switch_to_section (debug_pubnames_section);
8285   else
8286     switch_to_section (debug_pubtypes_section);
8287   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8288     dw2_asm_output_data (4, 0xffffffff,
8289       "Initial length escape value indicating 64-bit DWARF extension");
8290   if (names == pubname_table)
8291     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8292                          "Length of Public Names Info");
8293   else
8294     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8295                          "Length of Public Type Names Info");
8296   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8297   dw2_asm_output_data (2, 2, "DWARF Version");
8298   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8299                          debug_info_section,
8300                          "Offset of Compilation Unit Info");
8301   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8302                        "Compilation Unit Length");
8303
8304   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8305     {
8306       /* Enumerator names are part of the pubname table, but the parent
8307          DW_TAG_enumeration_type die may have been pruned.  Don't output
8308          them if that is the case.  */
8309       if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
8310         continue;
8311
8312       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8313       if (names == pubname_table)
8314         gcc_assert (pub->die->die_mark);
8315
8316       if (names != pubtype_table
8317           || pub->die->die_offset != 0
8318           || !flag_eliminate_unused_debug_types)
8319         {
8320           dw_offset die_offset = pub->die->die_offset;
8321
8322           /* If we're putting types in their own .debug_types sections,
8323              the .debug_pubtypes table will still point to the compile
8324              unit (not the type unit), so we want to use the offset of
8325              the skeleton DIE (if there is one).  */
8326           if (pub->die->comdat_type_p && names == pubtype_table)
8327             {
8328               comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
8329
8330               if (type_node != NULL)
8331                 die_offset = (type_node->skeleton_die != NULL
8332                               ? type_node->skeleton_die->die_offset
8333                               : 0);
8334             }
8335
8336           dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
8337
8338           dw2_asm_output_nstring (pub->name, -1, "external name");
8339         }
8340     }
8341
8342   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8343 }
8344
8345 /* Output the information that goes into the .debug_aranges table.
8346    Namely, define the beginning and ending address range of the
8347    text section generated for this compilation unit.  */
8348
8349 static void
8350 output_aranges (unsigned long aranges_length)
8351 {
8352   unsigned i;
8353
8354   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8355     dw2_asm_output_data (4, 0xffffffff,
8356       "Initial length escape value indicating 64-bit DWARF extension");
8357   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8358                        "Length of Address Ranges Info");
8359   /* Version number for aranges is still 2, even in DWARF3.  */
8360   dw2_asm_output_data (2, 2, "DWARF Version");
8361   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8362                          debug_info_section,
8363                          "Offset of Compilation Unit Info");
8364   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8365   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8366
8367   /* We need to align to twice the pointer size here.  */
8368   if (DWARF_ARANGES_PAD_SIZE)
8369     {
8370       /* Pad using a 2 byte words so that padding is correct for any
8371          pointer size.  */
8372       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8373                            2 * DWARF2_ADDR_SIZE);
8374       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8375         dw2_asm_output_data (2, 0, NULL);
8376     }
8377
8378   /* It is necessary not to output these entries if the sections were
8379      not used; if the sections were not used, the length will be 0 and
8380      the address may end up as 0 if the section is discarded by ld
8381      --gc-sections, leaving an invalid (0, 0) entry that can be
8382      confused with the terminator.  */
8383   if (text_section_used)
8384     {
8385       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8386       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8387                             text_section_label, "Length");
8388     }
8389   if (cold_text_section_used)
8390     {
8391       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8392                            "Address");
8393       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8394                             cold_text_section_label, "Length");
8395     }
8396
8397   if (have_multiple_function_sections)
8398     {
8399       unsigned fde_idx;
8400       dw_fde_ref fde;
8401
8402       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8403         {
8404           if (DECL_IGNORED_P (fde->decl))
8405             continue;
8406           if (!fde->in_std_section)
8407             {
8408               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8409                                    "Address");
8410               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8411                                     fde->dw_fde_begin, "Length");
8412             }
8413           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8414             {
8415               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8416                                    "Address");
8417               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8418                                     fde->dw_fde_second_begin, "Length");
8419             }
8420         }
8421     }
8422
8423   /* Output the terminator words.  */
8424   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8425   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8426 }
8427
8428 /* Add a new entry to .debug_ranges.  Return the offset at which it
8429    was placed.  */
8430
8431 static unsigned int
8432 add_ranges_num (int num)
8433 {
8434   unsigned int in_use = ranges_table_in_use;
8435
8436   if (in_use == ranges_table_allocated)
8437     {
8438       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8439       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8440                                     ranges_table_allocated);
8441       memset (ranges_table + ranges_table_in_use, 0,
8442               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8443     }
8444
8445   ranges_table[in_use].num = num;
8446   ranges_table_in_use = in_use + 1;
8447
8448   return in_use * 2 * DWARF2_ADDR_SIZE;
8449 }
8450
8451 /* Add a new entry to .debug_ranges corresponding to a block, or a
8452    range terminator if BLOCK is NULL.  */
8453
8454 static unsigned int
8455 add_ranges (const_tree block)
8456 {
8457   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8458 }
8459
8460 /* Add a new entry to .debug_ranges corresponding to a pair of
8461    labels.  */
8462
8463 static void
8464 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8465                       bool *added)
8466 {
8467   unsigned int in_use = ranges_by_label_in_use;
8468   unsigned int offset;
8469
8470   if (in_use == ranges_by_label_allocated)
8471     {
8472       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8473       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8474                                        ranges_by_label,
8475                                        ranges_by_label_allocated);
8476       memset (ranges_by_label + ranges_by_label_in_use, 0,
8477               RANGES_TABLE_INCREMENT
8478               * sizeof (struct dw_ranges_by_label_struct));
8479     }
8480
8481   ranges_by_label[in_use].begin = begin;
8482   ranges_by_label[in_use].end = end;
8483   ranges_by_label_in_use = in_use + 1;
8484
8485   offset = add_ranges_num (-(int)in_use - 1);
8486   if (!*added)
8487     {
8488       add_AT_range_list (die, DW_AT_ranges, offset);
8489       *added = true;
8490     }
8491 }
8492
8493 static void
8494 output_ranges (void)
8495 {
8496   unsigned i;
8497   static const char *const start_fmt = "Offset %#x";
8498   const char *fmt = start_fmt;
8499
8500   for (i = 0; i < ranges_table_in_use; i++)
8501     {
8502       int block_num = ranges_table[i].num;
8503
8504       if (block_num > 0)
8505         {
8506           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8507           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8508
8509           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8510           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8511
8512           /* If all code is in the text section, then the compilation
8513              unit base address defaults to DW_AT_low_pc, which is the
8514              base of the text section.  */
8515           if (!have_multiple_function_sections)
8516             {
8517               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8518                                     text_section_label,
8519                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8520               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8521                                     text_section_label, NULL);
8522             }
8523
8524           /* Otherwise, the compilation unit base address is zero,
8525              which allows us to use absolute addresses, and not worry
8526              about whether the target supports cross-section
8527              arithmetic.  */
8528           else
8529             {
8530               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8531                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8532               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8533             }
8534
8535           fmt = NULL;
8536         }
8537
8538       /* Negative block_num stands for an index into ranges_by_label.  */
8539       else if (block_num < 0)
8540         {
8541           int lab_idx = - block_num - 1;
8542
8543           if (!have_multiple_function_sections)
8544             {
8545               gcc_unreachable ();
8546 #if 0
8547               /* If we ever use add_ranges_by_labels () for a single
8548                  function section, all we have to do is to take out
8549                  the #if 0 above.  */
8550               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8551                                     ranges_by_label[lab_idx].begin,
8552                                     text_section_label,
8553                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8554               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8555                                     ranges_by_label[lab_idx].end,
8556                                     text_section_label, NULL);
8557 #endif
8558             }
8559           else
8560             {
8561               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8562                                    ranges_by_label[lab_idx].begin,
8563                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8564               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8565                                    ranges_by_label[lab_idx].end,
8566                                    NULL);
8567             }
8568         }
8569       else
8570         {
8571           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8572           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8573           fmt = start_fmt;
8574         }
8575     }
8576 }
8577
8578 /* Data structure containing information about input files.  */
8579 struct file_info
8580 {
8581   const char *path;     /* Complete file name.  */
8582   const char *fname;    /* File name part.  */
8583   int length;           /* Length of entire string.  */
8584   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8585   int dir_idx;          /* Index in directory table.  */
8586 };
8587
8588 /* Data structure containing information about directories with source
8589    files.  */
8590 struct dir_info
8591 {
8592   const char *path;     /* Path including directory name.  */
8593   int length;           /* Path length.  */
8594   int prefix;           /* Index of directory entry which is a prefix.  */
8595   int count;            /* Number of files in this directory.  */
8596   int dir_idx;          /* Index of directory used as base.  */
8597 };
8598
8599 /* Callback function for file_info comparison.  We sort by looking at
8600    the directories in the path.  */
8601
8602 static int
8603 file_info_cmp (const void *p1, const void *p2)
8604 {
8605   const struct file_info *const s1 = (const struct file_info *) p1;
8606   const struct file_info *const s2 = (const struct file_info *) p2;
8607   const unsigned char *cp1;
8608   const unsigned char *cp2;
8609
8610   /* Take care of file names without directories.  We need to make sure that
8611      we return consistent values to qsort since some will get confused if
8612      we return the same value when identical operands are passed in opposite
8613      orders.  So if neither has a directory, return 0 and otherwise return
8614      1 or -1 depending on which one has the directory.  */
8615   if ((s1->path == s1->fname || s2->path == s2->fname))
8616     return (s2->path == s2->fname) - (s1->path == s1->fname);
8617
8618   cp1 = (const unsigned char *) s1->path;
8619   cp2 = (const unsigned char *) s2->path;
8620
8621   while (1)
8622     {
8623       ++cp1;
8624       ++cp2;
8625       /* Reached the end of the first path?  If so, handle like above.  */
8626       if ((cp1 == (const unsigned char *) s1->fname)
8627           || (cp2 == (const unsigned char *) s2->fname))
8628         return ((cp2 == (const unsigned char *) s2->fname)
8629                 - (cp1 == (const unsigned char *) s1->fname));
8630
8631       /* Character of current path component the same?  */
8632       else if (*cp1 != *cp2)
8633         return *cp1 - *cp2;
8634     }
8635 }
8636
8637 struct file_name_acquire_data
8638 {
8639   struct file_info *files;
8640   int used_files;
8641   int max_files;
8642 };
8643
8644 /* Traversal function for the hash table.  */
8645
8646 static int
8647 file_name_acquire (void ** slot, void *data)
8648 {
8649   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8650   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8651   struct file_info *fi;
8652   const char *f;
8653
8654   gcc_assert (fnad->max_files >= d->emitted_number);
8655
8656   if (! d->emitted_number)
8657     return 1;
8658
8659   gcc_assert (fnad->max_files != fnad->used_files);
8660
8661   fi = fnad->files + fnad->used_files++;
8662
8663   /* Skip all leading "./".  */
8664   f = d->filename;
8665   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8666     f += 2;
8667
8668   /* Create a new array entry.  */
8669   fi->path = f;
8670   fi->length = strlen (f);
8671   fi->file_idx = d;
8672
8673   /* Search for the file name part.  */
8674   f = strrchr (f, DIR_SEPARATOR);
8675 #if defined (DIR_SEPARATOR_2)
8676   {
8677     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8678
8679     if (g != NULL)
8680       {
8681         if (f == NULL || f < g)
8682           f = g;
8683       }
8684   }
8685 #endif
8686
8687   fi->fname = f == NULL ? fi->path : f + 1;
8688   return 1;
8689 }
8690
8691 /* Output the directory table and the file name table.  We try to minimize
8692    the total amount of memory needed.  A heuristic is used to avoid large
8693    slowdowns with many input files.  */
8694
8695 static void
8696 output_file_names (void)
8697 {
8698   struct file_name_acquire_data fnad;
8699   int numfiles;
8700   struct file_info *files;
8701   struct dir_info *dirs;
8702   int *saved;
8703   int *savehere;
8704   int *backmap;
8705   int ndirs;
8706   int idx_offset;
8707   int i;
8708
8709   if (!last_emitted_file)
8710     {
8711       dw2_asm_output_data (1, 0, "End directory table");
8712       dw2_asm_output_data (1, 0, "End file name table");
8713       return;
8714     }
8715
8716   numfiles = last_emitted_file->emitted_number;
8717
8718   /* Allocate the various arrays we need.  */
8719   files = XALLOCAVEC (struct file_info, numfiles);
8720   dirs = XALLOCAVEC (struct dir_info, numfiles);
8721
8722   fnad.files = files;
8723   fnad.used_files = 0;
8724   fnad.max_files = numfiles;
8725   htab_traverse (file_table, file_name_acquire, &fnad);
8726   gcc_assert (fnad.used_files == fnad.max_files);
8727
8728   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
8729
8730   /* Find all the different directories used.  */
8731   dirs[0].path = files[0].path;
8732   dirs[0].length = files[0].fname - files[0].path;
8733   dirs[0].prefix = -1;
8734   dirs[0].count = 1;
8735   dirs[0].dir_idx = 0;
8736   files[0].dir_idx = 0;
8737   ndirs = 1;
8738
8739   for (i = 1; i < numfiles; i++)
8740     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8741         && memcmp (dirs[ndirs - 1].path, files[i].path,
8742                    dirs[ndirs - 1].length) == 0)
8743       {
8744         /* Same directory as last entry.  */
8745         files[i].dir_idx = ndirs - 1;
8746         ++dirs[ndirs - 1].count;
8747       }
8748     else
8749       {
8750         int j;
8751
8752         /* This is a new directory.  */
8753         dirs[ndirs].path = files[i].path;
8754         dirs[ndirs].length = files[i].fname - files[i].path;
8755         dirs[ndirs].count = 1;
8756         dirs[ndirs].dir_idx = ndirs;
8757         files[i].dir_idx = ndirs;
8758
8759         /* Search for a prefix.  */
8760         dirs[ndirs].prefix = -1;
8761         for (j = 0; j < ndirs; j++)
8762           if (dirs[j].length < dirs[ndirs].length
8763               && dirs[j].length > 1
8764               && (dirs[ndirs].prefix == -1
8765                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8766               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8767             dirs[ndirs].prefix = j;
8768
8769         ++ndirs;
8770       }
8771
8772   /* Now to the actual work.  We have to find a subset of the directories which
8773      allow expressing the file name using references to the directory table
8774      with the least amount of characters.  We do not do an exhaustive search
8775      where we would have to check out every combination of every single
8776      possible prefix.  Instead we use a heuristic which provides nearly optimal
8777      results in most cases and never is much off.  */
8778   saved = XALLOCAVEC (int, ndirs);
8779   savehere = XALLOCAVEC (int, ndirs);
8780
8781   memset (saved, '\0', ndirs * sizeof (saved[0]));
8782   for (i = 0; i < ndirs; i++)
8783     {
8784       int j;
8785       int total;
8786
8787       /* We can always save some space for the current directory.  But this
8788          does not mean it will be enough to justify adding the directory.  */
8789       savehere[i] = dirs[i].length;
8790       total = (savehere[i] - saved[i]) * dirs[i].count;
8791
8792       for (j = i + 1; j < ndirs; j++)
8793         {
8794           savehere[j] = 0;
8795           if (saved[j] < dirs[i].length)
8796             {
8797               /* Determine whether the dirs[i] path is a prefix of the
8798                  dirs[j] path.  */
8799               int k;
8800
8801               k = dirs[j].prefix;
8802               while (k != -1 && k != (int) i)
8803                 k = dirs[k].prefix;
8804
8805               if (k == (int) i)
8806                 {
8807                   /* Yes it is.  We can possibly save some memory by
8808                      writing the filenames in dirs[j] relative to
8809                      dirs[i].  */
8810                   savehere[j] = dirs[i].length;
8811                   total += (savehere[j] - saved[j]) * dirs[j].count;
8812                 }
8813             }
8814         }
8815
8816       /* Check whether we can save enough to justify adding the dirs[i]
8817          directory.  */
8818       if (total > dirs[i].length + 1)
8819         {
8820           /* It's worthwhile adding.  */
8821           for (j = i; j < ndirs; j++)
8822             if (savehere[j] > 0)
8823               {
8824                 /* Remember how much we saved for this directory so far.  */
8825                 saved[j] = savehere[j];
8826
8827                 /* Remember the prefix directory.  */
8828                 dirs[j].dir_idx = i;
8829               }
8830         }
8831     }
8832
8833   /* Emit the directory name table.  */
8834   idx_offset = dirs[0].length > 0 ? 1 : 0;
8835   for (i = 1 - idx_offset; i < ndirs; i++)
8836     dw2_asm_output_nstring (dirs[i].path,
8837                             dirs[i].length
8838                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
8839                             "Directory Entry: %#x", i + idx_offset);
8840
8841   dw2_asm_output_data (1, 0, "End directory table");
8842
8843   /* We have to emit them in the order of emitted_number since that's
8844      used in the debug info generation.  To do this efficiently we
8845      generate a back-mapping of the indices first.  */
8846   backmap = XALLOCAVEC (int, numfiles);
8847   for (i = 0; i < numfiles; i++)
8848     backmap[files[i].file_idx->emitted_number - 1] = i;
8849
8850   /* Now write all the file names.  */
8851   for (i = 0; i < numfiles; i++)
8852     {
8853       int file_idx = backmap[i];
8854       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8855
8856 #ifdef VMS_DEBUGGING_INFO
8857 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
8858
8859       /* Setting these fields can lead to debugger miscomparisons,
8860          but VMS Debug requires them to be set correctly.  */
8861
8862       int ver;
8863       long long cdt;
8864       long siz;
8865       int maxfilelen = strlen (files[file_idx].path)
8866                                + dirs[dir_idx].length
8867                                + MAX_VMS_VERSION_LEN + 1;
8868       char *filebuf = XALLOCAVEC (char, maxfilelen);
8869
8870       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
8871       snprintf (filebuf, maxfilelen, "%s;%d",
8872                 files[file_idx].path + dirs[dir_idx].length, ver);
8873
8874       dw2_asm_output_nstring
8875         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
8876
8877       /* Include directory index.  */
8878       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8879
8880       /* Modification time.  */
8881       dw2_asm_output_data_uleb128
8882         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
8883           ? cdt : 0,
8884          NULL);
8885
8886       /* File length in bytes.  */
8887       dw2_asm_output_data_uleb128
8888         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
8889           ? siz : 0,
8890          NULL);
8891 #else
8892       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8893                               "File Entry: %#x", (unsigned) i + 1);
8894
8895       /* Include directory index.  */
8896       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8897
8898       /* Modification time.  */
8899       dw2_asm_output_data_uleb128 (0, NULL);
8900
8901       /* File length in bytes.  */
8902       dw2_asm_output_data_uleb128 (0, NULL);
8903 #endif /* VMS_DEBUGGING_INFO */
8904     }
8905
8906   dw2_asm_output_data (1, 0, "End file name table");
8907 }
8908
8909
8910 /* Output one line number table into the .debug_line section.  */
8911
8912 static void
8913 output_one_line_info_table (dw_line_info_table *table)
8914 {
8915   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8916   unsigned int current_line = 1;
8917   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
8918   dw_line_info_entry *ent;
8919   size_t i;
8920
8921   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
8922     {
8923       switch (ent->opcode)
8924         {
8925         case LI_set_address:
8926           /* ??? Unfortunately, we have little choice here currently, and
8927              must always use the most general form.  GCC does not know the
8928              address delta itself, so we can't use DW_LNS_advance_pc.  Many
8929              ports do have length attributes which will give an upper bound
8930              on the address range.  We could perhaps use length attributes
8931              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
8932           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
8933
8934           /* This can handle any delta.  This takes
8935              4+DWARF2_ADDR_SIZE bytes.  */
8936           dw2_asm_output_data (1, 0, "set address %s", line_label);
8937           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8938           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8939           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8940           break;
8941
8942         case LI_set_line:
8943           if (ent->val == current_line)
8944             {
8945               /* We still need to start a new row, so output a copy insn.  */
8946               dw2_asm_output_data (1, DW_LNS_copy,
8947                                    "copy line %u", current_line);
8948             }
8949           else
8950             {
8951               int line_offset = ent->val - current_line;
8952               int line_delta = line_offset - DWARF_LINE_BASE;
8953
8954               current_line = ent->val;
8955               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8956                 {
8957                   /* This can handle deltas from -10 to 234, using the current
8958                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
8959                      This takes 1 byte.  */
8960                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8961                                        "line %u", current_line);
8962                 }
8963               else
8964                 {
8965                   /* This can handle any delta.  This takes at least 4 bytes,
8966                      depending on the value being encoded.  */
8967                   dw2_asm_output_data (1, DW_LNS_advance_line,
8968                                        "advance to line %u", current_line);
8969                   dw2_asm_output_data_sleb128 (line_offset, NULL);
8970                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
8971                 }
8972             }
8973           break;
8974
8975         case LI_set_file:
8976           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
8977           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
8978           break;
8979
8980         case LI_set_column:
8981           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
8982           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
8983           break;
8984
8985         case LI_negate_stmt:
8986           current_is_stmt = !current_is_stmt;
8987           dw2_asm_output_data (1, DW_LNS_negate_stmt,
8988                                "is_stmt %d", current_is_stmt);
8989           break;
8990
8991         case LI_set_prologue_end:
8992           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
8993                                "set prologue end");
8994           break;
8995           
8996         case LI_set_epilogue_begin:
8997           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
8998                                "set epilogue begin");
8999           break;
9000
9001         case LI_set_discriminator:
9002           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9003           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9004           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9005           dw2_asm_output_data_uleb128 (ent->val, NULL);
9006           break;
9007         }
9008     }
9009
9010   /* Emit debug info for the address of the end of the table.  */
9011   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9012   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9013   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9014   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9015
9016   dw2_asm_output_data (1, 0, "end sequence");
9017   dw2_asm_output_data_uleb128 (1, NULL);
9018   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9019 }
9020
9021 /* Output the source line number correspondence information.  This
9022    information goes into the .debug_line section.  */
9023
9024 static void
9025 output_line_info (void)
9026 {
9027   char l1[20], l2[20], p1[20], p2[20];
9028   int ver = dwarf_version;
9029   bool saw_one = false;
9030   int opc;
9031
9032   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9033   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9034   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9035   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9036
9037   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9038     dw2_asm_output_data (4, 0xffffffff,
9039       "Initial length escape value indicating 64-bit DWARF extension");
9040   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9041                         "Length of Source Line Info");
9042   ASM_OUTPUT_LABEL (asm_out_file, l1);
9043
9044   dw2_asm_output_data (2, ver, "DWARF Version");
9045   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9046   ASM_OUTPUT_LABEL (asm_out_file, p1);
9047
9048   /* Define the architecture-dependent minimum instruction length (in bytes).
9049      In this implementation of DWARF, this field is used for information
9050      purposes only.  Since GCC generates assembly language, we have no
9051      a priori knowledge of how many instruction bytes are generated for each
9052      source line, and therefore can use only the DW_LNE_set_address and
9053      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9054      this as '1', which is "correct enough" for all architectures,
9055      and don't let the target override.  */
9056   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9057
9058   if (ver >= 4)
9059     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9060                          "Maximum Operations Per Instruction");
9061   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9062                        "Default is_stmt_start flag");
9063   dw2_asm_output_data (1, DWARF_LINE_BASE,
9064                        "Line Base Value (Special Opcodes)");
9065   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9066                        "Line Range Value (Special Opcodes)");
9067   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9068                        "Special Opcode Base");
9069
9070   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9071     {
9072       int n_op_args;
9073       switch (opc)
9074         {
9075         case DW_LNS_advance_pc:
9076         case DW_LNS_advance_line:
9077         case DW_LNS_set_file:
9078         case DW_LNS_set_column:
9079         case DW_LNS_fixed_advance_pc:
9080         case DW_LNS_set_isa:
9081           n_op_args = 1;
9082           break;
9083         default:
9084           n_op_args = 0;
9085           break;
9086         }
9087
9088       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9089                            opc, n_op_args);
9090     }
9091
9092   /* Write out the information about the files we use.  */
9093   output_file_names ();
9094   ASM_OUTPUT_LABEL (asm_out_file, p2);
9095
9096   if (separate_line_info)
9097     {
9098       dw_line_info_table *table;
9099       size_t i;
9100
9101       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9102         if (table->in_use)
9103           {
9104             output_one_line_info_table (table);
9105             saw_one = true;
9106           }
9107     }
9108   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9109     {
9110       output_one_line_info_table (cold_text_section_line_info);
9111       saw_one = true;
9112     }
9113
9114   /* ??? Some Darwin linkers crash on a .debug_line section with no
9115      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9116      sufficient -- the address column must also be initialized.
9117      Make sure to output at least one set_address/end_sequence pair,
9118      choosing .text since that section is always present.  */
9119   if (text_section_line_info->in_use || !saw_one)
9120     output_one_line_info_table (text_section_line_info);
9121
9122   /* Output the marker for the end of the line number info.  */
9123   ASM_OUTPUT_LABEL (asm_out_file, l2);
9124 }
9125 \f
9126 /* Given a pointer to a tree node for some base type, return a pointer to
9127    a DIE that describes the given type.
9128
9129    This routine must only be called for GCC type nodes that correspond to
9130    Dwarf base (fundamental) types.  */
9131
9132 static dw_die_ref
9133 base_type_die (tree type)
9134 {
9135   dw_die_ref base_type_result;
9136   enum dwarf_type encoding;
9137
9138   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9139     return 0;
9140
9141   /* If this is a subtype that should not be emitted as a subrange type,
9142      use the base type.  See subrange_type_for_debug_p.  */
9143   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9144     type = TREE_TYPE (type);
9145
9146   switch (TREE_CODE (type))
9147     {
9148     case INTEGER_TYPE:
9149       if ((dwarf_version >= 4 || !dwarf_strict)
9150           && TYPE_NAME (type)
9151           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9152           && DECL_IS_BUILTIN (TYPE_NAME (type))
9153           && DECL_NAME (TYPE_NAME (type)))
9154         {
9155           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9156           if (strcmp (name, "char16_t") == 0
9157               || strcmp (name, "char32_t") == 0)
9158             {
9159               encoding = DW_ATE_UTF;
9160               break;
9161             }
9162         }
9163       if (TYPE_STRING_FLAG (type))
9164         {
9165           if (TYPE_UNSIGNED (type))
9166             encoding = DW_ATE_unsigned_char;
9167           else
9168             encoding = DW_ATE_signed_char;
9169         }
9170       else if (TYPE_UNSIGNED (type))
9171         encoding = DW_ATE_unsigned;
9172       else
9173         encoding = DW_ATE_signed;
9174       break;
9175
9176     case REAL_TYPE:
9177       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9178         {
9179           if (dwarf_version >= 3 || !dwarf_strict)
9180             encoding = DW_ATE_decimal_float;
9181           else
9182             encoding = DW_ATE_lo_user;
9183         }
9184       else
9185         encoding = DW_ATE_float;
9186       break;
9187
9188     case FIXED_POINT_TYPE:
9189       if (!(dwarf_version >= 3 || !dwarf_strict))
9190         encoding = DW_ATE_lo_user;
9191       else if (TYPE_UNSIGNED (type))
9192         encoding = DW_ATE_unsigned_fixed;
9193       else
9194         encoding = DW_ATE_signed_fixed;
9195       break;
9196
9197       /* Dwarf2 doesn't know anything about complex ints, so use
9198          a user defined type for it.  */
9199     case COMPLEX_TYPE:
9200       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9201         encoding = DW_ATE_complex_float;
9202       else
9203         encoding = DW_ATE_lo_user;
9204       break;
9205
9206     case BOOLEAN_TYPE:
9207       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9208       encoding = DW_ATE_boolean;
9209       break;
9210
9211     default:
9212       /* No other TREE_CODEs are Dwarf fundamental types.  */
9213       gcc_unreachable ();
9214     }
9215
9216   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9217
9218   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9219                    int_size_in_bytes (type));
9220   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9221   add_pubtype (type, base_type_result);
9222
9223   return base_type_result;
9224 }
9225
9226 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9227    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9228
9229 static inline int
9230 is_base_type (tree type)
9231 {
9232   switch (TREE_CODE (type))
9233     {
9234     case ERROR_MARK:
9235     case VOID_TYPE:
9236     case INTEGER_TYPE:
9237     case REAL_TYPE:
9238     case FIXED_POINT_TYPE:
9239     case COMPLEX_TYPE:
9240     case BOOLEAN_TYPE:
9241       return 1;
9242
9243     case ARRAY_TYPE:
9244     case RECORD_TYPE:
9245     case UNION_TYPE:
9246     case QUAL_UNION_TYPE:
9247     case ENUMERAL_TYPE:
9248     case FUNCTION_TYPE:
9249     case METHOD_TYPE:
9250     case POINTER_TYPE:
9251     case REFERENCE_TYPE:
9252     case NULLPTR_TYPE:
9253     case OFFSET_TYPE:
9254     case LANG_TYPE:
9255     case VECTOR_TYPE:
9256       return 0;
9257
9258     default:
9259       gcc_unreachable ();
9260     }
9261
9262   return 0;
9263 }
9264
9265 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9266    node, return the size in bits for the type if it is a constant, or else
9267    return the alignment for the type if the type's size is not constant, or
9268    else return BITS_PER_WORD if the type actually turns out to be an
9269    ERROR_MARK node.  */
9270
9271 static inline unsigned HOST_WIDE_INT
9272 simple_type_size_in_bits (const_tree type)
9273 {
9274   if (TREE_CODE (type) == ERROR_MARK)
9275     return BITS_PER_WORD;
9276   else if (TYPE_SIZE (type) == NULL_TREE)
9277     return 0;
9278   else if (host_integerp (TYPE_SIZE (type), 1))
9279     return tree_low_cst (TYPE_SIZE (type), 1);
9280   else
9281     return TYPE_ALIGN (type);
9282 }
9283
9284 /* Similarly, but return a double_int instead of UHWI.  */
9285
9286 static inline double_int
9287 double_int_type_size_in_bits (const_tree type)
9288 {
9289   if (TREE_CODE (type) == ERROR_MARK)
9290     return uhwi_to_double_int (BITS_PER_WORD);
9291   else if (TYPE_SIZE (type) == NULL_TREE)
9292     return double_int_zero;
9293   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9294     return tree_to_double_int (TYPE_SIZE (type));
9295   else
9296     return uhwi_to_double_int (TYPE_ALIGN (type));
9297 }
9298
9299 /*  Given a pointer to a tree node for a subrange type, return a pointer
9300     to a DIE that describes the given type.  */
9301
9302 static dw_die_ref
9303 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9304 {
9305   dw_die_ref subrange_die;
9306   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9307
9308   if (context_die == NULL)
9309     context_die = comp_unit_die ();
9310
9311   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9312
9313   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9314     {
9315       /* The size of the subrange type and its base type do not match,
9316          so we need to generate a size attribute for the subrange type.  */
9317       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9318     }
9319
9320   if (low)
9321     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9322   if (high)
9323     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9324
9325   return subrange_die;
9326 }
9327
9328 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9329    entry that chains various modifiers in front of the given type.  */
9330
9331 static dw_die_ref
9332 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9333                    dw_die_ref context_die)
9334 {
9335   enum tree_code code = TREE_CODE (type);
9336   dw_die_ref mod_type_die;
9337   dw_die_ref sub_die = NULL;
9338   tree item_type = NULL;
9339   tree qualified_type;
9340   tree name, low, high;
9341   dw_die_ref mod_scope;
9342
9343   if (code == ERROR_MARK)
9344     return NULL;
9345
9346   /* See if we already have the appropriately qualified variant of
9347      this type.  */
9348   qualified_type
9349     = get_qualified_type (type,
9350                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9351                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9352
9353   if (qualified_type == sizetype
9354       && TYPE_NAME (qualified_type)
9355       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9356     {
9357       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9358
9359       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9360                            && TYPE_PRECISION (t)
9361                            == TYPE_PRECISION (qualified_type)
9362                            && TYPE_UNSIGNED (t)
9363                            == TYPE_UNSIGNED (qualified_type));
9364       qualified_type = t;
9365     }
9366
9367   /* If we do, then we can just use its DIE, if it exists.  */
9368   if (qualified_type)
9369     {
9370       mod_type_die = lookup_type_die (qualified_type);
9371       if (mod_type_die)
9372         return mod_type_die;
9373     }
9374
9375   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9376
9377   /* Handle C typedef types.  */
9378   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9379       && !DECL_ARTIFICIAL (name))
9380     {
9381       tree dtype = TREE_TYPE (name);
9382
9383       if (qualified_type == dtype)
9384         {
9385           /* For a named type, use the typedef.  */
9386           gen_type_die (qualified_type, context_die);
9387           return lookup_type_die (qualified_type);
9388         }
9389       else if (is_const_type < TYPE_READONLY (dtype)
9390                || is_volatile_type < TYPE_VOLATILE (dtype)
9391                || (is_const_type <= TYPE_READONLY (dtype)
9392                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9393                    && DECL_ORIGINAL_TYPE (name) != type))
9394         /* cv-unqualified version of named type.  Just use the unnamed
9395            type to which it refers.  */
9396         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9397                                   is_const_type, is_volatile_type,
9398                                   context_die);
9399       /* Else cv-qualified version of named type; fall through.  */
9400     }
9401
9402   mod_scope = scope_die_for (type, context_die);
9403
9404   if (is_const_type
9405       /* If both is_const_type and is_volatile_type, prefer the path
9406          which leads to a qualified type.  */
9407       && (!is_volatile_type
9408           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9409           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9410     {
9411       mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
9412       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9413     }
9414   else if (is_volatile_type)
9415     {
9416       mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
9417       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9418     }
9419   else if (code == POINTER_TYPE)
9420     {
9421       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
9422       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9423                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9424       item_type = TREE_TYPE (type);
9425       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9426         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9427                          TYPE_ADDR_SPACE (item_type));
9428     }
9429   else if (code == REFERENCE_TYPE)
9430     {
9431       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9432         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
9433                                 type);
9434       else
9435         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
9436       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9437                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9438       item_type = TREE_TYPE (type);
9439       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9440         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9441                          TYPE_ADDR_SPACE (item_type));
9442     }
9443   else if (code == INTEGER_TYPE
9444            && TREE_TYPE (type) != NULL_TREE
9445            && subrange_type_for_debug_p (type, &low, &high))
9446     {
9447       mod_type_die = subrange_type_die (type, low, high, context_die);
9448       item_type = TREE_TYPE (type);
9449     }
9450   else if (is_base_type (type))
9451     mod_type_die = base_type_die (type);
9452   else
9453     {
9454       gen_type_die (type, context_die);
9455
9456       /* We have to get the type_main_variant here (and pass that to the
9457          `lookup_type_die' routine) because the ..._TYPE node we have
9458          might simply be a *copy* of some original type node (where the
9459          copy was created to help us keep track of typedef names) and
9460          that copy might have a different TYPE_UID from the original
9461          ..._TYPE node.  */
9462       if (TREE_CODE (type) != VECTOR_TYPE)
9463         return lookup_type_die (type_main_variant (type));
9464       else
9465         /* Vectors have the debugging information in the type,
9466            not the main variant.  */
9467         return lookup_type_die (type);
9468     }
9469
9470   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9471      don't output a DW_TAG_typedef, since there isn't one in the
9472      user's program; just attach a DW_AT_name to the type.
9473      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9474      if the base type already has the same name.  */
9475   if (name
9476       && ((TREE_CODE (name) != TYPE_DECL
9477            && (qualified_type == TYPE_MAIN_VARIANT (type)
9478                || (!is_const_type && !is_volatile_type)))
9479           || (TREE_CODE (name) == TYPE_DECL
9480               && TREE_TYPE (name) == qualified_type
9481               && DECL_NAME (name))))
9482     {
9483       if (TREE_CODE (name) == TYPE_DECL)
9484         /* Could just call add_name_and_src_coords_attributes here,
9485            but since this is a builtin type it doesn't have any
9486            useful source coordinates anyway.  */
9487         name = DECL_NAME (name);
9488       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9489     }
9490   /* This probably indicates a bug.  */
9491   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9492     {
9493       name = TYPE_NAME (type);
9494       if (name
9495           && TREE_CODE (name) == TYPE_DECL)
9496         name = DECL_NAME (name);
9497       add_name_attribute (mod_type_die,
9498                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
9499     }
9500
9501   if (qualified_type)
9502     equate_type_number_to_die (qualified_type, mod_type_die);
9503
9504   if (item_type)
9505     /* We must do this after the equate_type_number_to_die call, in case
9506        this is a recursive type.  This ensures that the modified_type_die
9507        recursion will terminate even if the type is recursive.  Recursive
9508        types are possible in Ada.  */
9509     sub_die = modified_type_die (item_type,
9510                                  TYPE_READONLY (item_type),
9511                                  TYPE_VOLATILE (item_type),
9512                                  context_die);
9513
9514   if (sub_die != NULL)
9515     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9516
9517   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9518   if (TYPE_ARTIFICIAL (type))
9519     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9520
9521   return mod_type_die;
9522 }
9523
9524 /* Generate DIEs for the generic parameters of T.
9525    T must be either a generic type or a generic function.
9526    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9527
9528 static void
9529 gen_generic_params_dies (tree t)
9530 {
9531   tree parms, args;
9532   int parms_num, i;
9533   dw_die_ref die = NULL;
9534
9535   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9536     return;
9537
9538   if (TYPE_P (t))
9539     die = lookup_type_die (t);
9540   else if (DECL_P (t))
9541     die = lookup_decl_die (t);
9542
9543   gcc_assert (die);
9544
9545   parms = lang_hooks.get_innermost_generic_parms (t);
9546   if (!parms)
9547     /* T has no generic parameter. It means T is neither a generic type
9548        or function. End of story.  */
9549     return;
9550
9551   parms_num = TREE_VEC_LENGTH (parms);
9552   args = lang_hooks.get_innermost_generic_args (t);
9553   for (i = 0; i < parms_num; i++)
9554     {
9555       tree parm, arg, arg_pack_elems;
9556
9557       parm = TREE_VEC_ELT (parms, i);
9558       arg = TREE_VEC_ELT (args, i);
9559       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9560       gcc_assert (parm && TREE_VALUE (parm) && arg);
9561
9562       if (parm && TREE_VALUE (parm) && arg)
9563         {
9564           /* If PARM represents a template parameter pack,
9565              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9566              by DW_TAG_template_*_parameter DIEs for the argument
9567              pack elements of ARG. Note that ARG would then be
9568              an argument pack.  */
9569           if (arg_pack_elems)
9570             template_parameter_pack_die (TREE_VALUE (parm),
9571                                          arg_pack_elems,
9572                                          die);
9573           else
9574             generic_parameter_die (TREE_VALUE (parm), arg,
9575                                    true /* Emit DW_AT_name */, die);
9576         }
9577     }
9578 }
9579
9580 /* Create and return a DIE for PARM which should be
9581    the representation of a generic type parameter.
9582    For instance, in the C++ front end, PARM would be a template parameter.
9583    ARG is the argument to PARM.
9584    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9585    name of the PARM.
9586    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9587    as a child node.  */
9588
9589 static dw_die_ref
9590 generic_parameter_die (tree parm, tree arg,
9591                        bool emit_name_p,
9592                        dw_die_ref parent_die)
9593 {
9594   dw_die_ref tmpl_die = NULL;
9595   const char *name = NULL;
9596
9597   if (!parm || !DECL_NAME (parm) || !arg)
9598     return NULL;
9599
9600   /* We support non-type generic parameters and arguments,
9601      type generic parameters and arguments, as well as
9602      generic generic parameters (a.k.a. template template parameters in C++)
9603      and arguments.  */
9604   if (TREE_CODE (parm) == PARM_DECL)
9605     /* PARM is a nontype generic parameter  */
9606     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9607   else if (TREE_CODE (parm) == TYPE_DECL)
9608     /* PARM is a type generic parameter.  */
9609     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9610   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9611     /* PARM is a generic generic parameter.
9612        Its DIE is a GNU extension. It shall have a
9613        DW_AT_name attribute to represent the name of the template template
9614        parameter, and a DW_AT_GNU_template_name attribute to represent the
9615        name of the template template argument.  */
9616     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9617                         parent_die, parm);
9618   else
9619     gcc_unreachable ();
9620
9621   if (tmpl_die)
9622     {
9623       tree tmpl_type;
9624
9625       /* If PARM is a generic parameter pack, it means we are
9626          emitting debug info for a template argument pack element.
9627          In other terms, ARG is a template argument pack element.
9628          In that case, we don't emit any DW_AT_name attribute for
9629          the die.  */
9630       if (emit_name_p)
9631         {
9632           name = IDENTIFIER_POINTER (DECL_NAME (parm));
9633           gcc_assert (name);
9634           add_AT_string (tmpl_die, DW_AT_name, name);
9635         }
9636
9637       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9638         {
9639           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
9640              TMPL_DIE should have a child DW_AT_type attribute that is set
9641              to the type of the argument to PARM, which is ARG.
9642              If PARM is a type generic parameter, TMPL_DIE should have a
9643              child DW_AT_type that is set to ARG.  */
9644           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
9645           add_type_attribute (tmpl_die, tmpl_type, 0,
9646                               TREE_THIS_VOLATILE (tmpl_type),
9647                               parent_die);
9648         }
9649       else
9650         {
9651           /* So TMPL_DIE is a DIE representing a
9652              a generic generic template parameter, a.k.a template template
9653              parameter in C++ and arg is a template.  */
9654
9655           /* The DW_AT_GNU_template_name attribute of the DIE must be set
9656              to the name of the argument.  */
9657           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
9658           if (name)
9659             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
9660         }
9661
9662       if (TREE_CODE (parm) == PARM_DECL)
9663         /* So PARM is a non-type generic parameter.
9664            DWARF3 5.6.8 says we must set a DW_AT_const_value child
9665            attribute of TMPL_DIE which value represents the value
9666            of ARG.
9667            We must be careful here:
9668            The value of ARG might reference some function decls.
9669            We might currently be emitting debug info for a generic
9670            type and types are emitted before function decls, we don't
9671            know if the function decls referenced by ARG will actually be
9672            emitted after cgraph computations.
9673            So must defer the generation of the DW_AT_const_value to
9674            after cgraph is ready.  */
9675         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
9676     }
9677
9678   return tmpl_die;
9679 }
9680
9681 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
9682    PARM_PACK must be a template parameter pack. The returned DIE
9683    will be child DIE of PARENT_DIE.  */
9684
9685 static dw_die_ref
9686 template_parameter_pack_die (tree parm_pack,
9687                              tree parm_pack_args,
9688                              dw_die_ref parent_die)
9689 {
9690   dw_die_ref die;
9691   int j;
9692
9693   gcc_assert (parent_die && parm_pack);
9694
9695   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
9696   add_name_and_src_coords_attributes (die, parm_pack);
9697   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
9698     generic_parameter_die (parm_pack,
9699                            TREE_VEC_ELT (parm_pack_args, j),
9700                            false /* Don't emit DW_AT_name */,
9701                            die);
9702   return die;
9703 }
9704
9705 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9706    an enumerated type.  */
9707
9708 static inline int
9709 type_is_enum (const_tree type)
9710 {
9711   return TREE_CODE (type) == ENUMERAL_TYPE;
9712 }
9713
9714 /* Return the DBX register number described by a given RTL node.  */
9715
9716 static unsigned int
9717 dbx_reg_number (const_rtx rtl)
9718 {
9719   unsigned regno = REGNO (rtl);
9720
9721   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9722
9723 #ifdef LEAF_REG_REMAP
9724   if (crtl->uses_only_leaf_regs)
9725     {
9726       int leaf_reg = LEAF_REG_REMAP (regno);
9727       if (leaf_reg != -1)
9728         regno = (unsigned) leaf_reg;
9729     }
9730 #endif
9731
9732   regno = DBX_REGISTER_NUMBER (regno);
9733   gcc_assert (regno != INVALID_REGNUM);
9734   return regno;
9735 }
9736
9737 /* Optionally add a DW_OP_piece term to a location description expression.
9738    DW_OP_piece is only added if the location description expression already
9739    doesn't end with DW_OP_piece.  */
9740
9741 static void
9742 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
9743 {
9744   dw_loc_descr_ref loc;
9745
9746   if (*list_head != NULL)
9747     {
9748       /* Find the end of the chain.  */
9749       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
9750         ;
9751
9752       if (loc->dw_loc_opc != DW_OP_piece)
9753         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
9754     }
9755 }
9756
9757 /* Return a location descriptor that designates a machine register or
9758    zero if there is none.  */
9759
9760 static dw_loc_descr_ref
9761 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
9762 {
9763   rtx regs;
9764
9765   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
9766     return 0;
9767
9768   /* We only use "frame base" when we're sure we're talking about the
9769      post-prologue local stack frame.  We do this by *not* running
9770      register elimination until this point, and recognizing the special
9771      argument pointer and soft frame pointer rtx's.
9772      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
9773   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
9774       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
9775     {
9776       dw_loc_descr_ref result = NULL;
9777
9778       if (dwarf_version >= 4 || !dwarf_strict)
9779         {
9780           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
9781                                        initialized);
9782           if (result)
9783             add_loc_descr (&result,
9784                            new_loc_descr (DW_OP_stack_value, 0, 0));
9785         }
9786       return result;
9787     }
9788
9789   regs = targetm.dwarf_register_span (rtl);
9790
9791   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
9792     return multiple_reg_loc_descriptor (rtl, regs, initialized);
9793   else
9794     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
9795 }
9796
9797 /* Return a location descriptor that designates a machine register for
9798    a given hard register number.  */
9799
9800 static dw_loc_descr_ref
9801 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
9802 {
9803   dw_loc_descr_ref reg_loc_descr;
9804
9805   if (regno <= 31)
9806     reg_loc_descr
9807       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
9808   else
9809     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
9810
9811   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9812     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9813
9814   return reg_loc_descr;
9815 }
9816
9817 /* Given an RTL of a register, return a location descriptor that
9818    designates a value that spans more than one register.  */
9819
9820 static dw_loc_descr_ref
9821 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
9822                              enum var_init_status initialized)
9823 {
9824   int nregs, size, i;
9825   unsigned reg;
9826   dw_loc_descr_ref loc_result = NULL;
9827
9828   reg = REGNO (rtl);
9829 #ifdef LEAF_REG_REMAP
9830   if (crtl->uses_only_leaf_regs)
9831     {
9832       int leaf_reg = LEAF_REG_REMAP (reg);
9833       if (leaf_reg != -1)
9834         reg = (unsigned) leaf_reg;
9835     }
9836 #endif
9837   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
9838   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
9839
9840   /* Simple, contiguous registers.  */
9841   if (regs == NULL_RTX)
9842     {
9843       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
9844
9845       loc_result = NULL;
9846       while (nregs--)
9847         {
9848           dw_loc_descr_ref t;
9849
9850           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
9851                                       VAR_INIT_STATUS_INITIALIZED);
9852           add_loc_descr (&loc_result, t);
9853           add_loc_descr_op_piece (&loc_result, size);
9854           ++reg;
9855         }
9856       return loc_result;
9857     }
9858
9859   /* Now onto stupid register sets in non contiguous locations.  */
9860
9861   gcc_assert (GET_CODE (regs) == PARALLEL);
9862
9863   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9864   loc_result = NULL;
9865
9866   for (i = 0; i < XVECLEN (regs, 0); ++i)
9867     {
9868       dw_loc_descr_ref t;
9869
9870       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
9871                                   VAR_INIT_STATUS_INITIALIZED);
9872       add_loc_descr (&loc_result, t);
9873       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
9874       add_loc_descr_op_piece (&loc_result, size);
9875     }
9876
9877   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9878     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9879   return loc_result;
9880 }
9881
9882 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
9883
9884 /* Return a location descriptor that designates a constant i,
9885    as a compound operation from constant (i >> shift), constant shift
9886    and DW_OP_shl.  */
9887
9888 static dw_loc_descr_ref
9889 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
9890 {
9891   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
9892   add_loc_descr (&ret, int_loc_descriptor (shift));
9893   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
9894   return ret;
9895 }
9896
9897 /* Return a location descriptor that designates a constant.  */
9898
9899 static dw_loc_descr_ref
9900 int_loc_descriptor (HOST_WIDE_INT i)
9901 {
9902   enum dwarf_location_atom op;
9903
9904   /* Pick the smallest representation of a constant, rather than just
9905      defaulting to the LEB encoding.  */
9906   if (i >= 0)
9907     {
9908       int clz = clz_hwi (i);
9909       int ctz = ctz_hwi (i);
9910       if (i <= 31)
9911         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
9912       else if (i <= 0xff)
9913         op = DW_OP_const1u;
9914       else if (i <= 0xffff)
9915         op = DW_OP_const2u;
9916       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
9917                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
9918         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
9919            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
9920            while DW_OP_const4u is 5 bytes.  */
9921         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
9922       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
9923                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
9924         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
9925            while DW_OP_const4u is 5 bytes.  */
9926         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
9927       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
9928         op = DW_OP_const4u;
9929       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
9930                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
9931         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
9932            while DW_OP_constu of constant >= 0x100000000 takes at least
9933            6 bytes.  */
9934         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
9935       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
9936                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
9937                   >= HOST_BITS_PER_WIDE_INT)
9938         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
9939            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
9940            while DW_OP_constu takes in this case at least 6 bytes.  */
9941         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
9942       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
9943                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
9944                && size_of_uleb128 (i) > 6)
9945         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
9946         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
9947       else
9948         op = DW_OP_constu;
9949     }
9950   else
9951     {
9952       if (i >= -0x80)
9953         op = DW_OP_const1s;
9954       else if (i >= -0x8000)
9955         op = DW_OP_const2s;
9956       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
9957         {
9958           if (size_of_int_loc_descriptor (i) < 5)
9959             {
9960               dw_loc_descr_ref ret = int_loc_descriptor (-i);
9961               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
9962               return ret;
9963             }
9964           op = DW_OP_const4s;
9965         }
9966       else
9967         {
9968           if (size_of_int_loc_descriptor (i)
9969               < (unsigned long) 1 + size_of_sleb128 (i))
9970             {
9971               dw_loc_descr_ref ret = int_loc_descriptor (-i);
9972               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
9973               return ret;
9974             }
9975           op = DW_OP_consts;
9976         }
9977     }
9978
9979   return new_loc_descr (op, i, 0);
9980 }
9981
9982 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
9983    without actually allocating it.  */
9984
9985 static unsigned long
9986 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
9987 {
9988   return size_of_int_loc_descriptor (i >> shift)
9989          + size_of_int_loc_descriptor (shift)
9990          + 1;
9991 }
9992
9993 /* Return size_of_locs (int_loc_descriptor (i)) without
9994    actually allocating it.  */
9995
9996 static unsigned long
9997 size_of_int_loc_descriptor (HOST_WIDE_INT i)
9998 {
9999   unsigned long s;
10000
10001   if (i >= 0)
10002     {
10003       int clz, ctz;
10004       if (i <= 31)
10005         return 1;
10006       else if (i <= 0xff)
10007         return 2;
10008       else if (i <= 0xffff)
10009         return 3;
10010       clz = clz_hwi (i);
10011       ctz = ctz_hwi (i);
10012       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10013           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10014         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10015                                                     - clz - 5);
10016       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10017                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10018         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10019                                                     - clz - 8);
10020       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10021         return 5;
10022       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10023       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10024           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10025         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10026                                                     - clz - 8);
10027       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10028                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10029         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10030                                                     - clz - 16);
10031       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10032                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10033                && s > 6)
10034         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10035                                                     - clz - 32);
10036       else
10037         return 1 + s;
10038     }
10039   else
10040     {
10041       if (i >= -0x80)
10042         return 2;
10043       else if (i >= -0x8000)
10044         return 3;
10045       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10046         {
10047           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10048             {
10049               s = size_of_int_loc_descriptor (-i) + 1;
10050               if (s < 5)
10051                 return s;
10052             }
10053           return 5;
10054         }
10055       else
10056         {
10057           unsigned long r = 1 + size_of_sleb128 (i);
10058           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10059             {
10060               s = size_of_int_loc_descriptor (-i) + 1;
10061               if (s < r)
10062                 return s;
10063             }
10064           return r;
10065         }
10066     }
10067 }
10068
10069 /* Return loc description representing "address" of integer value.
10070    This can appear only as toplevel expression.  */
10071
10072 static dw_loc_descr_ref
10073 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10074 {
10075   int litsize;
10076   dw_loc_descr_ref loc_result = NULL;
10077
10078   if (!(dwarf_version >= 4 || !dwarf_strict))
10079     return NULL;
10080
10081   litsize = size_of_int_loc_descriptor (i);
10082   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10083      is more compact.  For DW_OP_stack_value we need:
10084      litsize + 1 (DW_OP_stack_value)
10085      and for DW_OP_implicit_value:
10086      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10087   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10088     {
10089       loc_result = int_loc_descriptor (i);
10090       add_loc_descr (&loc_result,
10091                      new_loc_descr (DW_OP_stack_value, 0, 0));
10092       return loc_result;
10093     }
10094
10095   loc_result = new_loc_descr (DW_OP_implicit_value,
10096                               size, 0);
10097   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10098   loc_result->dw_loc_oprnd2.v.val_int = i;
10099   return loc_result;
10100 }
10101
10102 /* Return a location descriptor that designates a base+offset location.  */
10103
10104 static dw_loc_descr_ref
10105 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10106                  enum var_init_status initialized)
10107 {
10108   unsigned int regno;
10109   dw_loc_descr_ref result;
10110   dw_fde_ref fde = cfun->fde;
10111
10112   /* We only use "frame base" when we're sure we're talking about the
10113      post-prologue local stack frame.  We do this by *not* running
10114      register elimination until this point, and recognizing the special
10115      argument pointer and soft frame pointer rtx's.  */
10116   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10117     {
10118       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10119
10120       if (elim != reg)
10121         {
10122           if (GET_CODE (elim) == PLUS)
10123             {
10124               offset += INTVAL (XEXP (elim, 1));
10125               elim = XEXP (elim, 0);
10126             }
10127           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10128                        && (elim == hard_frame_pointer_rtx
10129                            || elim == stack_pointer_rtx))
10130                       || elim == (frame_pointer_needed
10131                                   ? hard_frame_pointer_rtx
10132                                   : stack_pointer_rtx));
10133
10134           /* If drap register is used to align stack, use frame
10135              pointer + offset to access stack variables.  If stack
10136              is aligned without drap, use stack pointer + offset to
10137              access stack variables.  */
10138           if (crtl->stack_realign_tried
10139               && reg == frame_pointer_rtx)
10140             {
10141               int base_reg
10142                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10143                                       ? HARD_FRAME_POINTER_REGNUM
10144                                       : REGNO (elim));
10145               return new_reg_loc_descr (base_reg, offset);
10146             }
10147
10148           gcc_assert (frame_pointer_fb_offset_valid);
10149           offset += frame_pointer_fb_offset;
10150           return new_loc_descr (DW_OP_fbreg, offset, 0);
10151         }
10152     }
10153
10154   regno = DWARF_FRAME_REGNUM (REGNO (reg));
10155
10156   if (!optimize && fde
10157       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10158     {
10159       /* Use cfa+offset to represent the location of arguments passed
10160          on the stack when drap is used to align stack.
10161          Only do this when not optimizing, for optimized code var-tracking
10162          is supposed to track where the arguments live and the register
10163          used as vdrap or drap in some spot might be used for something
10164          else in other part of the routine.  */
10165       return new_loc_descr (DW_OP_fbreg, offset, 0);
10166     }
10167
10168   if (regno <= 31)
10169     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10170                             offset, 0);
10171   else
10172     result = new_loc_descr (DW_OP_bregx, regno, offset);
10173
10174   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10175     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10176
10177   return result;
10178 }
10179
10180 /* Return true if this RTL expression describes a base+offset calculation.  */
10181
10182 static inline int
10183 is_based_loc (const_rtx rtl)
10184 {
10185   return (GET_CODE (rtl) == PLUS
10186           && ((REG_P (XEXP (rtl, 0))
10187                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10188                && CONST_INT_P (XEXP (rtl, 1)))));
10189 }
10190
10191 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10192    failed.  */
10193
10194 static dw_loc_descr_ref
10195 tls_mem_loc_descriptor (rtx mem)
10196 {
10197   tree base;
10198   dw_loc_descr_ref loc_result;
10199
10200   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10201     return NULL;
10202
10203   base = get_base_address (MEM_EXPR (mem));
10204   if (base == NULL
10205       || TREE_CODE (base) != VAR_DECL
10206       || !DECL_THREAD_LOCAL_P (base))
10207     return NULL;
10208
10209   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10210   if (loc_result == NULL)
10211     return NULL;
10212
10213   if (MEM_OFFSET (mem))
10214     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10215
10216   return loc_result;
10217 }
10218
10219 /* Output debug info about reason why we failed to expand expression as dwarf
10220    expression.  */
10221
10222 static void
10223 expansion_failed (tree expr, rtx rtl, char const *reason)
10224 {
10225   if (dump_file && (dump_flags & TDF_DETAILS))
10226     {
10227       fprintf (dump_file, "Failed to expand as dwarf: ");
10228       if (expr)
10229         print_generic_expr (dump_file, expr, dump_flags);
10230       if (rtl)
10231         {
10232           fprintf (dump_file, "\n");
10233           print_rtl (dump_file, rtl);
10234         }
10235       fprintf (dump_file, "\nReason: %s\n", reason);
10236     }
10237 }
10238
10239 /* Helper function for const_ok_for_output, called either directly
10240    or via for_each_rtx.  */
10241
10242 static int
10243 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10244 {
10245   rtx rtl = *rtlp;
10246
10247   if (GET_CODE (rtl) == UNSPEC)
10248     {
10249       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10250          we can't express it in the debug info.  */
10251 #ifdef ENABLE_CHECKING
10252       /* Don't complain about TLS UNSPECs, those are just too hard to
10253          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
10254          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
10255          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
10256       if (XVECLEN (rtl, 0) == 0
10257           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10258           || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
10259         inform (current_function_decl
10260                 ? DECL_SOURCE_LOCATION (current_function_decl)
10261                 : UNKNOWN_LOCATION,
10262 #if NUM_UNSPEC_VALUES > 0
10263                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10264                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10265                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10266                 XINT (rtl, 1));
10267 #else
10268                 "non-delegitimized UNSPEC %d found in variable location",
10269                 XINT (rtl, 1));
10270 #endif
10271 #endif
10272       expansion_failed (NULL_TREE, rtl,
10273                         "UNSPEC hasn't been delegitimized.\n");
10274       return 1;
10275     }
10276
10277   if (targetm.const_not_ok_for_debug_p (rtl))
10278     {
10279       expansion_failed (NULL_TREE, rtl,
10280                         "Expression rejected for debug by the backend.\n");
10281       return 1;
10282     }
10283
10284   if (GET_CODE (rtl) != SYMBOL_REF)
10285     return 0;
10286
10287   if (CONSTANT_POOL_ADDRESS_P (rtl))
10288     {
10289       bool marked;
10290       get_pool_constant_mark (rtl, &marked);
10291       /* If all references to this pool constant were optimized away,
10292          it was not output and thus we can't represent it.  */
10293       if (!marked)
10294         {
10295           expansion_failed (NULL_TREE, rtl,
10296                             "Constant was removed from constant pool.\n");
10297           return 1;
10298         }
10299     }
10300
10301   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10302     return 1;
10303
10304   /* Avoid references to external symbols in debug info, on several targets
10305      the linker might even refuse to link when linking a shared library,
10306      and in many other cases the relocations for .debug_info/.debug_loc are
10307      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10308      to be defined within the same shared library or executable are fine.  */
10309   if (SYMBOL_REF_EXTERNAL_P (rtl))
10310     {
10311       tree decl = SYMBOL_REF_DECL (rtl);
10312
10313       if (decl == NULL || !targetm.binds_local_p (decl))
10314         {
10315           expansion_failed (NULL_TREE, rtl,
10316                             "Symbol not defined in current TU.\n");
10317           return 1;
10318         }
10319     }
10320
10321   return 0;
10322 }
10323
10324 /* Return true if constant RTL can be emitted in DW_OP_addr or
10325    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10326    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10327
10328 static bool
10329 const_ok_for_output (rtx rtl)
10330 {
10331   if (GET_CODE (rtl) == SYMBOL_REF)
10332     return const_ok_for_output_1 (&rtl, NULL) == 0;
10333
10334   if (GET_CODE (rtl) == CONST)
10335     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10336
10337   return true;
10338 }
10339
10340 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10341    if possible, NULL otherwise.  */
10342
10343 static dw_die_ref
10344 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10345 {
10346   dw_die_ref type_die;
10347   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10348
10349   if (type == NULL)
10350     return NULL;
10351   switch (TREE_CODE (type))
10352     {
10353     case INTEGER_TYPE:
10354     case REAL_TYPE:
10355       break;
10356     default:
10357       return NULL;
10358     }
10359   type_die = lookup_type_die (type);
10360   if (!type_die)
10361     type_die = modified_type_die (type, false, false, comp_unit_die ());
10362   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10363     return NULL;
10364   return type_die;
10365 }
10366
10367 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10368    type matching MODE, or, if MODE is narrower than or as wide as
10369    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10370    possible.  */
10371
10372 static dw_loc_descr_ref
10373 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10374 {
10375   enum machine_mode outer_mode = mode;
10376   dw_die_ref type_die;
10377   dw_loc_descr_ref cvt;
10378
10379   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10380     {
10381       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10382       return op;
10383     }
10384   type_die = base_type_for_mode (outer_mode, 1);
10385   if (type_die == NULL)
10386     return NULL;
10387   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10388   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10389   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10390   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10391   add_loc_descr (&op, cvt);
10392   return op;
10393 }
10394
10395 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10396
10397 static dw_loc_descr_ref
10398 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10399                         dw_loc_descr_ref op1)
10400 {
10401   dw_loc_descr_ref ret = op0;
10402   add_loc_descr (&ret, op1);
10403   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10404   if (STORE_FLAG_VALUE != 1)
10405     {
10406       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10407       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10408     }
10409   return ret;
10410 }
10411
10412 /* Return location descriptor for signed comparison OP RTL.  */
10413
10414 static dw_loc_descr_ref
10415 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10416                          enum machine_mode mem_mode)
10417 {
10418   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10419   dw_loc_descr_ref op0, op1;
10420   int shift;
10421
10422   if (op_mode == VOIDmode)
10423     op_mode = GET_MODE (XEXP (rtl, 1));
10424   if (op_mode == VOIDmode)
10425     return NULL;
10426
10427   if (dwarf_strict
10428       && (GET_MODE_CLASS (op_mode) != MODE_INT
10429           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10430     return NULL;
10431
10432   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10433                             VAR_INIT_STATUS_INITIALIZED);
10434   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10435                             VAR_INIT_STATUS_INITIALIZED);
10436
10437   if (op0 == NULL || op1 == NULL)
10438     return NULL;
10439
10440   if (GET_MODE_CLASS (op_mode) != MODE_INT
10441       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10442     return compare_loc_descriptor (op, op0, op1);
10443
10444   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10445     {
10446       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10447       dw_loc_descr_ref cvt;
10448
10449       if (type_die == NULL)
10450         return NULL;
10451       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10452       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10453       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10454       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10455       add_loc_descr (&op0, cvt);
10456       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10457       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10458       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10459       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10460       add_loc_descr (&op1, cvt);
10461       return compare_loc_descriptor (op, op0, op1);
10462     }
10463
10464   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10465   /* For eq/ne, if the operands are known to be zero-extended,
10466      there is no need to do the fancy shifting up.  */
10467   if (op == DW_OP_eq || op == DW_OP_ne)
10468     {
10469       dw_loc_descr_ref last0, last1;
10470       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10471         ;
10472       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10473         ;
10474       /* deref_size zero extends, and for constants we can check
10475          whether they are zero extended or not.  */
10476       if (((last0->dw_loc_opc == DW_OP_deref_size
10477             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10478            || (CONST_INT_P (XEXP (rtl, 0))
10479                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10480                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10481           && ((last1->dw_loc_opc == DW_OP_deref_size
10482                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10483               || (CONST_INT_P (XEXP (rtl, 1))
10484                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10485                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10486         return compare_loc_descriptor (op, op0, op1);
10487
10488       /* EQ/NE comparison against constant in narrower type than
10489          DWARF2_ADDR_SIZE can be performed either as
10490          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10491          DW_OP_{eq,ne}
10492          or
10493          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10494          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10495       if (CONST_INT_P (XEXP (rtl, 1))
10496           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10497           && (size_of_int_loc_descriptor (shift) + 1
10498               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10499               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10500                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10501                                                & GET_MODE_MASK (op_mode))))
10502         {
10503           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10504           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10505           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10506                                     & GET_MODE_MASK (op_mode));
10507           return compare_loc_descriptor (op, op0, op1);
10508         }
10509     }
10510   add_loc_descr (&op0, int_loc_descriptor (shift));
10511   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10512   if (CONST_INT_P (XEXP (rtl, 1)))
10513     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10514   else
10515     {
10516       add_loc_descr (&op1, int_loc_descriptor (shift));
10517       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10518     }
10519   return compare_loc_descriptor (op, op0, op1);
10520 }
10521
10522 /* Return location descriptor for unsigned comparison OP RTL.  */
10523
10524 static dw_loc_descr_ref
10525 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10526                          enum machine_mode mem_mode)
10527 {
10528   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10529   dw_loc_descr_ref op0, op1;
10530
10531   if (op_mode == VOIDmode)
10532     op_mode = GET_MODE (XEXP (rtl, 1));
10533   if (op_mode == VOIDmode)
10534     return NULL;
10535   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10536     return NULL;
10537
10538   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10539     return NULL;
10540
10541   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10542                             VAR_INIT_STATUS_INITIALIZED);
10543   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10544                             VAR_INIT_STATUS_INITIALIZED);
10545
10546   if (op0 == NULL || op1 == NULL)
10547     return NULL;
10548
10549   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10550     {
10551       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10552       dw_loc_descr_ref last0, last1;
10553       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10554         ;
10555       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10556         ;
10557       if (CONST_INT_P (XEXP (rtl, 0)))
10558         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10559       /* deref_size zero extends, so no need to mask it again.  */
10560       else if (last0->dw_loc_opc != DW_OP_deref_size
10561                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10562         {
10563           add_loc_descr (&op0, int_loc_descriptor (mask));
10564           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10565         }
10566       if (CONST_INT_P (XEXP (rtl, 1)))
10567         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10568       /* deref_size zero extends, so no need to mask it again.  */
10569       else if (last1->dw_loc_opc != DW_OP_deref_size
10570                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10571         {
10572           add_loc_descr (&op1, int_loc_descriptor (mask));
10573           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10574         }
10575     }
10576   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10577     {
10578       HOST_WIDE_INT bias = 1;
10579       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10580       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10581       if (CONST_INT_P (XEXP (rtl, 1)))
10582         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10583                                   + INTVAL (XEXP (rtl, 1)));
10584       else
10585         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10586                                             bias, 0));
10587     }
10588   return compare_loc_descriptor (op, op0, op1);
10589 }
10590
10591 /* Return location descriptor for {U,S}{MIN,MAX}.  */
10592
10593 static dw_loc_descr_ref
10594 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10595                        enum machine_mode mem_mode)
10596 {
10597   enum dwarf_location_atom op;
10598   dw_loc_descr_ref op0, op1, ret;
10599   dw_loc_descr_ref bra_node, drop_node;
10600
10601   if (dwarf_strict
10602       && (GET_MODE_CLASS (mode) != MODE_INT
10603           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10604     return NULL;
10605
10606   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10607                             VAR_INIT_STATUS_INITIALIZED);
10608   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10609                             VAR_INIT_STATUS_INITIALIZED);
10610
10611   if (op0 == NULL || op1 == NULL)
10612     return NULL;
10613
10614   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10615   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10616   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10617   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10618     {
10619       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10620         {
10621           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10622           add_loc_descr (&op0, int_loc_descriptor (mask));
10623           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10624           add_loc_descr (&op1, int_loc_descriptor (mask));
10625           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10626         }
10627       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10628         {
10629           HOST_WIDE_INT bias = 1;
10630           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10631           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10632           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10633         }
10634     }
10635   else if (GET_MODE_CLASS (mode) == MODE_INT
10636            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10637     {
10638       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
10639       add_loc_descr (&op0, int_loc_descriptor (shift));
10640       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10641       add_loc_descr (&op1, int_loc_descriptor (shift));
10642       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10643     }
10644   else if (GET_MODE_CLASS (mode) == MODE_INT
10645            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10646     {
10647       dw_die_ref type_die = base_type_for_mode (mode, 0);
10648       dw_loc_descr_ref cvt;
10649       if (type_die == NULL)
10650         return NULL;
10651       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10652       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10653       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10654       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10655       add_loc_descr (&op0, cvt);
10656       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10657       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10658       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10659       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10660       add_loc_descr (&op1, cvt);
10661     }
10662
10663   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
10664     op = DW_OP_lt;
10665   else
10666     op = DW_OP_gt;
10667   ret = op0;
10668   add_loc_descr (&ret, op1);
10669   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10670   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10671   add_loc_descr (&ret, bra_node);
10672   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10673   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
10674   add_loc_descr (&ret, drop_node);
10675   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10676   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
10677   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
10678       && GET_MODE_CLASS (mode) == MODE_INT
10679       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10680     ret = convert_descriptor_to_mode (mode, ret);
10681   return ret;
10682 }
10683
10684 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
10685    but after converting arguments to type_die, afterwards
10686    convert back to unsigned.  */
10687
10688 static dw_loc_descr_ref
10689 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
10690              enum machine_mode mode, enum machine_mode mem_mode)
10691 {
10692   dw_loc_descr_ref cvt, op0, op1;
10693
10694   if (type_die == NULL)
10695     return NULL;
10696   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10697                             VAR_INIT_STATUS_INITIALIZED);
10698   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10699                             VAR_INIT_STATUS_INITIALIZED);
10700   if (op0 == NULL || op1 == NULL)
10701     return NULL;
10702   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10703   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10704   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10705   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10706   add_loc_descr (&op0, cvt);
10707   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10708   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10709   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10710   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10711   add_loc_descr (&op1, cvt);
10712   add_loc_descr (&op0, op1);
10713   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
10714   return convert_descriptor_to_mode (mode, op0);
10715 }
10716
10717 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
10718    const0 is DW_OP_lit0 or corresponding typed constant,
10719    const1 is DW_OP_lit1 or corresponding typed constant
10720    and constMSB is constant with just the MSB bit set
10721    for the mode):
10722        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10723    L1: const0 DW_OP_swap
10724    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
10725        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10726    L3: DW_OP_drop
10727    L4: DW_OP_nop
10728
10729    CTZ is similar:
10730        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10731    L1: const0 DW_OP_swap
10732    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10733        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10734    L3: DW_OP_drop
10735    L4: DW_OP_nop
10736
10737    FFS is similar:
10738        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
10739    L1: const1 DW_OP_swap
10740    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10741        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10742    L3: DW_OP_drop
10743    L4: DW_OP_nop  */
10744
10745 static dw_loc_descr_ref
10746 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
10747                     enum machine_mode mem_mode)
10748 {
10749   dw_loc_descr_ref op0, ret, tmp;
10750   HOST_WIDE_INT valv;
10751   dw_loc_descr_ref l1jump, l1label;
10752   dw_loc_descr_ref l2jump, l2label;
10753   dw_loc_descr_ref l3jump, l3label;
10754   dw_loc_descr_ref l4jump, l4label;
10755   rtx msb;
10756
10757   if (GET_MODE_CLASS (mode) != MODE_INT
10758       || GET_MODE (XEXP (rtl, 0)) != mode
10759       || (GET_CODE (rtl) == CLZ
10760           && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
10761     return NULL;
10762
10763   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10764                             VAR_INIT_STATUS_INITIALIZED);
10765   if (op0 == NULL)
10766     return NULL;
10767   ret = op0;
10768   if (GET_CODE (rtl) == CLZ)
10769     {
10770       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10771         valv = GET_MODE_BITSIZE (mode);
10772     }
10773   else if (GET_CODE (rtl) == FFS)
10774     valv = 0;
10775   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
10776     valv = GET_MODE_BITSIZE (mode);
10777   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10778   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
10779   add_loc_descr (&ret, l1jump);
10780   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
10781   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
10782                             VAR_INIT_STATUS_INITIALIZED);
10783   if (tmp == NULL)
10784     return NULL;
10785   add_loc_descr (&ret, tmp);
10786   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
10787   add_loc_descr (&ret, l4jump);
10788   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
10789                                 ? const1_rtx : const0_rtx,
10790                                 mode, mem_mode,
10791                                 VAR_INIT_STATUS_INITIALIZED);
10792   if (l1label == NULL)
10793     return NULL;
10794   add_loc_descr (&ret, l1label);
10795   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10796   l2label = new_loc_descr (DW_OP_dup, 0, 0);
10797   add_loc_descr (&ret, l2label);
10798   if (GET_CODE (rtl) != CLZ)
10799     msb = const1_rtx;
10800   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10801     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
10802                    << (GET_MODE_BITSIZE (mode) - 1));
10803   else
10804     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
10805                                   << (GET_MODE_BITSIZE (mode)
10806                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
10807   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
10808     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
10809                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
10810                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
10811   else
10812     tmp = mem_loc_descriptor (msb, mode, mem_mode,
10813                               VAR_INIT_STATUS_INITIALIZED);
10814   if (tmp == NULL)
10815     return NULL;
10816   add_loc_descr (&ret, tmp);
10817   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10818   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
10819   add_loc_descr (&ret, l3jump);
10820   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10821                             VAR_INIT_STATUS_INITIALIZED);
10822   if (tmp == NULL)
10823     return NULL;
10824   add_loc_descr (&ret, tmp);
10825   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
10826                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
10827   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10828   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
10829   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10830   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
10831   add_loc_descr (&ret, l2jump);
10832   l3label = new_loc_descr (DW_OP_drop, 0, 0);
10833   add_loc_descr (&ret, l3label);
10834   l4label = new_loc_descr (DW_OP_nop, 0, 0);
10835   add_loc_descr (&ret, l4label);
10836   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10837   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10838   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10839   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10840   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10841   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
10842   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10843   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
10844   return ret;
10845 }
10846
10847 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
10848    const1 is DW_OP_lit1 or corresponding typed constant):
10849        const0 DW_OP_swap
10850    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10851        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10852    L2: DW_OP_drop
10853
10854    PARITY is similar:
10855    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
10856        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
10857    L2: DW_OP_drop  */
10858
10859 static dw_loc_descr_ref
10860 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
10861                          enum machine_mode mem_mode)
10862 {
10863   dw_loc_descr_ref op0, ret, tmp;
10864   dw_loc_descr_ref l1jump, l1label;
10865   dw_loc_descr_ref l2jump, l2label;
10866
10867   if (GET_MODE_CLASS (mode) != MODE_INT
10868       || GET_MODE (XEXP (rtl, 0)) != mode)
10869     return NULL;
10870
10871   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10872                             VAR_INIT_STATUS_INITIALIZED);
10873   if (op0 == NULL)
10874     return NULL;
10875   ret = op0;
10876   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10877                             VAR_INIT_STATUS_INITIALIZED);
10878   if (tmp == NULL)
10879     return NULL;
10880   add_loc_descr (&ret, tmp);
10881   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10882   l1label = new_loc_descr (DW_OP_dup, 0, 0);
10883   add_loc_descr (&ret, l1label);
10884   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
10885   add_loc_descr (&ret, l2jump);
10886   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10887   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
10888   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10889                             VAR_INIT_STATUS_INITIALIZED);
10890   if (tmp == NULL)
10891     return NULL;
10892   add_loc_descr (&ret, tmp);
10893   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10894   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
10895                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
10896   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10897   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
10898                             VAR_INIT_STATUS_INITIALIZED);
10899   add_loc_descr (&ret, tmp);
10900   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
10901   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
10902   add_loc_descr (&ret, l1jump);
10903   l2label = new_loc_descr (DW_OP_drop, 0, 0);
10904   add_loc_descr (&ret, l2label);
10905   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10906   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10907   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10908   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10909   return ret;
10910 }
10911
10912 /* BSWAP (constS is initial shift count, either 56 or 24):
10913        constS const0
10914    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
10915        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
10916        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
10917        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
10918    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
10919
10920 static dw_loc_descr_ref
10921 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
10922                       enum machine_mode mem_mode)
10923 {
10924   dw_loc_descr_ref op0, ret, tmp;
10925   dw_loc_descr_ref l1jump, l1label;
10926   dw_loc_descr_ref l2jump, l2label;
10927
10928   if (GET_MODE_CLASS (mode) != MODE_INT
10929       || BITS_PER_UNIT != 8
10930       || (GET_MODE_BITSIZE (mode) != 32
10931           &&  GET_MODE_BITSIZE (mode) != 64))
10932     return NULL;
10933
10934   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10935                             VAR_INIT_STATUS_INITIALIZED);
10936   if (op0 == NULL)
10937     return NULL;
10938
10939   ret = op0;
10940   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
10941                             mode, mem_mode,
10942                             VAR_INIT_STATUS_INITIALIZED);
10943   if (tmp == NULL)
10944     return NULL;
10945   add_loc_descr (&ret, tmp);
10946   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10947                             VAR_INIT_STATUS_INITIALIZED);
10948   if (tmp == NULL)
10949     return NULL;
10950   add_loc_descr (&ret, tmp);
10951   l1label = new_loc_descr (DW_OP_pick, 2, 0);
10952   add_loc_descr (&ret, l1label);
10953   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
10954                             mode, mem_mode,
10955                             VAR_INIT_STATUS_INITIALIZED);
10956   add_loc_descr (&ret, tmp);
10957   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
10958   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
10959   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
10960   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
10961                             VAR_INIT_STATUS_INITIALIZED);
10962   if (tmp == NULL)
10963     return NULL;
10964   add_loc_descr (&ret, tmp);
10965   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
10966   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
10967   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10968   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
10969   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10970   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
10971   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
10972                             VAR_INIT_STATUS_INITIALIZED);
10973   add_loc_descr (&ret, tmp);
10974   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
10975   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
10976   add_loc_descr (&ret, l2jump);
10977   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
10978                             VAR_INIT_STATUS_INITIALIZED);
10979   add_loc_descr (&ret, tmp);
10980   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
10981   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10982   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
10983   add_loc_descr (&ret, l1jump);
10984   l2label = new_loc_descr (DW_OP_drop, 0, 0);
10985   add_loc_descr (&ret, l2label);
10986   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10987   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
10988   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10989   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
10990   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
10991   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
10992   return ret;
10993 }
10994
10995 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
10996    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
10997    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
10998    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
10999
11000    ROTATERT is similar:
11001    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11002    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11003    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11004
11005 static dw_loc_descr_ref
11006 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11007                        enum machine_mode mem_mode)
11008 {
11009   rtx rtlop1 = XEXP (rtl, 1);
11010   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11011   int i;
11012
11013   if (GET_MODE_CLASS (mode) != MODE_INT)
11014     return NULL;
11015
11016   if (GET_MODE (rtlop1) != VOIDmode
11017       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11018     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11019   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11020                             VAR_INIT_STATUS_INITIALIZED);
11021   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11022                             VAR_INIT_STATUS_INITIALIZED);
11023   if (op0 == NULL || op1 == NULL)
11024     return NULL;
11025   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11026     for (i = 0; i < 2; i++)
11027       {
11028         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11029           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11030                                         mode, mem_mode,
11031                                         VAR_INIT_STATUS_INITIALIZED);
11032         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11033           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11034                                    ? DW_OP_const4u
11035                                    : HOST_BITS_PER_WIDE_INT == 64
11036                                    ? DW_OP_const8u : DW_OP_constu,
11037                                    GET_MODE_MASK (mode), 0);
11038         else
11039           mask[i] = NULL;
11040         if (mask[i] == NULL)
11041           return NULL;
11042         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11043       }
11044   ret = op0;
11045   add_loc_descr (&ret, op1);
11046   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11047   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11048   if (GET_CODE (rtl) == ROTATERT)
11049     {
11050       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11051       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11052                                           GET_MODE_BITSIZE (mode), 0));
11053     }
11054   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11055   if (mask[0] != NULL)
11056     add_loc_descr (&ret, mask[0]);
11057   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11058   if (mask[1] != NULL)
11059     {
11060       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11061       add_loc_descr (&ret, mask[1]);
11062       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11063     }
11064   if (GET_CODE (rtl) == ROTATE)
11065     {
11066       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11067       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11068                                           GET_MODE_BITSIZE (mode), 0));
11069     }
11070   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11071   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11072   return ret;
11073 }
11074
11075 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11076    for DEBUG_PARAMETER_REF RTL.  */
11077
11078 static dw_loc_descr_ref
11079 parameter_ref_descriptor (rtx rtl)
11080 {
11081   dw_loc_descr_ref ret;
11082   dw_die_ref ref;
11083
11084   if (dwarf_strict)
11085     return NULL;
11086   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11087   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11088   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11089   if (ref)
11090     {
11091       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11092       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11093       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11094     }
11095   else
11096     {
11097       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11098       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11099     }
11100   return ret;
11101 }
11102
11103 /* The following routine converts the RTL for a variable or parameter
11104    (resident in memory) into an equivalent Dwarf representation of a
11105    mechanism for getting the address of that same variable onto the top of a
11106    hypothetical "address evaluation" stack.
11107
11108    When creating memory location descriptors, we are effectively transforming
11109    the RTL for a memory-resident object into its Dwarf postfix expression
11110    equivalent.  This routine recursively descends an RTL tree, turning
11111    it into Dwarf postfix code as it goes.
11112
11113    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11114
11115    MEM_MODE is the mode of the memory reference, needed to handle some
11116    autoincrement addressing modes.
11117
11118    Return 0 if we can't represent the location.  */
11119
11120 dw_loc_descr_ref
11121 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11122                     enum machine_mode mem_mode,
11123                     enum var_init_status initialized)
11124 {
11125   dw_loc_descr_ref mem_loc_result = NULL;
11126   enum dwarf_location_atom op;
11127   dw_loc_descr_ref op0, op1;
11128
11129   if (mode == VOIDmode)
11130     mode = GET_MODE (rtl);
11131
11132   /* Note that for a dynamically sized array, the location we will generate a
11133      description of here will be the lowest numbered location which is
11134      actually within the array.  That's *not* necessarily the same as the
11135      zeroth element of the array.  */
11136
11137   rtl = targetm.delegitimize_address (rtl);
11138
11139   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11140     return NULL;
11141
11142   switch (GET_CODE (rtl))
11143     {
11144     case POST_INC:
11145     case POST_DEC:
11146     case POST_MODIFY:
11147       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11148
11149     case SUBREG:
11150       /* The case of a subreg may arise when we have a local (register)
11151          variable or a formal (register) parameter which doesn't quite fill
11152          up an entire register.  For now, just assume that it is
11153          legitimate to make the Dwarf info refer to the whole register which
11154          contains the given subreg.  */
11155       if (!subreg_lowpart_p (rtl))
11156         break;
11157       if (GET_MODE_CLASS (mode) == MODE_INT
11158           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11159           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11160 #ifdef POINTERS_EXTEND_UNSIGNED
11161               || (mode == Pmode && mem_mode != VOIDmode)
11162 #endif
11163              )
11164           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11165         {
11166           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11167                                                GET_MODE (SUBREG_REG (rtl)),
11168                                                mem_mode, initialized);
11169           break;
11170         }
11171       if (dwarf_strict)
11172         break;
11173       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11174         break;
11175       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11176           && (GET_MODE_CLASS (mode) != MODE_INT
11177               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11178         break;
11179       else
11180         {
11181           dw_die_ref type_die;
11182           dw_loc_descr_ref cvt;
11183
11184           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11185                                                GET_MODE (SUBREG_REG (rtl)),
11186                                                mem_mode, initialized);
11187           if (mem_loc_result == NULL)
11188             break;
11189           type_die = base_type_for_mode (mode,
11190                                          GET_MODE_CLASS (mode) == MODE_INT);
11191           if (type_die == NULL)
11192             {
11193               mem_loc_result = NULL;
11194               break;
11195             }
11196           if (GET_MODE_SIZE (mode)
11197               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11198             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11199           else
11200             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11201           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11202           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11203           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11204           add_loc_descr (&mem_loc_result, cvt);
11205         }
11206       break;
11207
11208     case REG:
11209       if (GET_MODE_CLASS (mode) != MODE_INT
11210           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11211 #ifdef POINTERS_EXTEND_UNSIGNED
11212               && (mode != Pmode || mem_mode == VOIDmode)
11213 #endif
11214               ))
11215         {
11216           dw_die_ref type_die;
11217
11218           if (dwarf_strict)
11219             break;
11220           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11221             break;
11222           type_die = base_type_for_mode (mode,
11223                                          GET_MODE_CLASS (mode) == MODE_INT);
11224           if (type_die == NULL)
11225             break;
11226           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11227                                           dbx_reg_number (rtl), 0);
11228           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11229           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11230           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11231           break;
11232         }
11233       /* Whenever a register number forms a part of the description of the
11234          method for calculating the (dynamic) address of a memory resident
11235          object, DWARF rules require the register number be referred to as
11236          a "base register".  This distinction is not based in any way upon
11237          what category of register the hardware believes the given register
11238          belongs to.  This is strictly DWARF terminology we're dealing with
11239          here. Note that in cases where the location of a memory-resident
11240          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11241          OP_CONST (0)) the actual DWARF location descriptor that we generate
11242          may just be OP_BASEREG (basereg).  This may look deceptively like
11243          the object in question was allocated to a register (rather than in
11244          memory) so DWARF consumers need to be aware of the subtle
11245          distinction between OP_REG and OP_BASEREG.  */
11246       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11247         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11248       else if (stack_realign_drap
11249                && crtl->drap_reg
11250                && crtl->args.internal_arg_pointer == rtl
11251                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11252         {
11253           /* If RTL is internal_arg_pointer, which has been optimized
11254              out, use DRAP instead.  */
11255           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11256                                             VAR_INIT_STATUS_INITIALIZED);
11257         }
11258       break;
11259
11260     case SIGN_EXTEND:
11261     case ZERO_EXTEND:
11262       if (GET_MODE_CLASS (mode) != MODE_INT)
11263         break;
11264       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11265                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11266       if (op0 == 0)
11267         break;
11268       else if (GET_CODE (rtl) == ZERO_EXTEND
11269                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11270                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11271                   < HOST_BITS_PER_WIDE_INT
11272                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11273                   to expand zero extend as two shifts instead of
11274                   masking.  */
11275                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11276         {
11277           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11278           mem_loc_result = op0;
11279           add_loc_descr (&mem_loc_result,
11280                          int_loc_descriptor (GET_MODE_MASK (imode)));
11281           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11282         }
11283       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11284         {
11285           int shift = DWARF2_ADDR_SIZE
11286                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11287           shift *= BITS_PER_UNIT;
11288           if (GET_CODE (rtl) == SIGN_EXTEND)
11289             op = DW_OP_shra;
11290           else
11291             op = DW_OP_shr;
11292           mem_loc_result = op0;
11293           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11294           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11295           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11296           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11297         }
11298       else if (!dwarf_strict)
11299         {
11300           dw_die_ref type_die1, type_die2;
11301           dw_loc_descr_ref cvt;
11302
11303           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11304                                           GET_CODE (rtl) == ZERO_EXTEND);
11305           if (type_die1 == NULL)
11306             break;
11307           type_die2 = base_type_for_mode (mode, 1);
11308           if (type_die2 == NULL)
11309             break;
11310           mem_loc_result = op0;
11311           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11312           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11313           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11314           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11315           add_loc_descr (&mem_loc_result, cvt);
11316           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11317           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11318           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11319           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11320           add_loc_descr (&mem_loc_result, cvt);
11321         }
11322       break;
11323
11324     case MEM:
11325       {
11326         rtx new_rtl = avoid_constant_pool_reference (rtl);
11327         if (new_rtl != rtl)
11328           {
11329             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
11330                                                  initialized);
11331             if (mem_loc_result != NULL)
11332               return mem_loc_result;
11333           }
11334       }
11335       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11336                                            get_address_mode (rtl), mode,
11337                                            VAR_INIT_STATUS_INITIALIZED);
11338       if (mem_loc_result == NULL)
11339         mem_loc_result = tls_mem_loc_descriptor (rtl);
11340       if (mem_loc_result != NULL)
11341         {
11342           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11343               || GET_MODE_CLASS (mode) != MODE_INT)
11344             {
11345               dw_die_ref type_die;
11346               dw_loc_descr_ref deref;
11347
11348               if (dwarf_strict)
11349                 return NULL;
11350               type_die
11351                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11352               if (type_die == NULL)
11353                 return NULL;
11354               deref = new_loc_descr (DW_OP_GNU_deref_type,
11355                                      GET_MODE_SIZE (mode), 0);
11356               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11357               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11358               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11359               add_loc_descr (&mem_loc_result, deref);
11360             }
11361           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11362             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11363           else
11364             add_loc_descr (&mem_loc_result,
11365                            new_loc_descr (DW_OP_deref_size,
11366                                           GET_MODE_SIZE (mode), 0));
11367         }
11368       break;
11369
11370     case LO_SUM:
11371       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11372
11373     case LABEL_REF:
11374       /* Some ports can transform a symbol ref into a label ref, because
11375          the symbol ref is too far away and has to be dumped into a constant
11376          pool.  */
11377     case CONST:
11378     case SYMBOL_REF:
11379       if (GET_MODE_CLASS (mode) != MODE_INT
11380           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11381 #ifdef POINTERS_EXTEND_UNSIGNED
11382               && (mode != Pmode || mem_mode == VOIDmode)
11383 #endif
11384               ))
11385         break;
11386       if (GET_CODE (rtl) == SYMBOL_REF
11387           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11388         {
11389           dw_loc_descr_ref temp;
11390
11391           /* If this is not defined, we have no way to emit the data.  */
11392           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11393             break;
11394
11395           /* We used to emit DW_OP_addr here, but that's wrong, since
11396              DW_OP_addr should be relocated by the debug info consumer,
11397              while DW_OP_GNU_push_tls_address operand should not.  */
11398           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11399                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11400           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11401           temp->dw_loc_oprnd1.v.val_addr = rtl;
11402           temp->dtprel = true;
11403
11404           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11405           add_loc_descr (&mem_loc_result, temp);
11406
11407           break;
11408         }
11409
11410       if (!const_ok_for_output (rtl))
11411         break;
11412
11413     symref:
11414       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11415       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11416       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11417       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11418       break;
11419
11420     case CONCAT:
11421     case CONCATN:
11422     case VAR_LOCATION:
11423     case DEBUG_IMPLICIT_PTR:
11424       expansion_failed (NULL_TREE, rtl,
11425                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11426       return 0;
11427
11428     case ENTRY_VALUE:
11429       if (dwarf_strict)
11430         return NULL;
11431       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11432         {
11433           if (GET_MODE_CLASS (mode) != MODE_INT
11434               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11435             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11436                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11437           else
11438             op0
11439               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11440                                         VAR_INIT_STATUS_INITIALIZED);
11441         }
11442       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11443                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11444         {
11445           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11446                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11447           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11448             return NULL;
11449         }
11450       else
11451         gcc_unreachable ();
11452       if (op0 == NULL)
11453         return NULL;
11454       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11455       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11456       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11457       break;
11458
11459     case DEBUG_PARAMETER_REF:
11460       mem_loc_result = parameter_ref_descriptor (rtl);
11461       break;
11462
11463     case PRE_MODIFY:
11464       /* Extract the PLUS expression nested inside and fall into
11465          PLUS code below.  */
11466       rtl = XEXP (rtl, 1);
11467       goto plus;
11468
11469     case PRE_INC:
11470     case PRE_DEC:
11471       /* Turn these into a PLUS expression and fall into the PLUS code
11472          below.  */
11473       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11474                           GEN_INT (GET_CODE (rtl) == PRE_INC
11475                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11476                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11477
11478       /* ... fall through ...  */
11479
11480     case PLUS:
11481     plus:
11482       if (is_based_loc (rtl)
11483           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11484           && GET_MODE_CLASS (mode) == MODE_INT)
11485         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11486                                           INTVAL (XEXP (rtl, 1)),
11487                                           VAR_INIT_STATUS_INITIALIZED);
11488       else
11489         {
11490           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11491                                                VAR_INIT_STATUS_INITIALIZED);
11492           if (mem_loc_result == 0)
11493             break;
11494
11495           if (CONST_INT_P (XEXP (rtl, 1))
11496               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11497             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11498           else
11499             {
11500               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11501                                         VAR_INIT_STATUS_INITIALIZED);
11502               if (op1 == 0)
11503                 break;
11504               add_loc_descr (&mem_loc_result, op1);
11505               add_loc_descr (&mem_loc_result,
11506                              new_loc_descr (DW_OP_plus, 0, 0));
11507             }
11508         }
11509       break;
11510
11511     /* If a pseudo-reg is optimized away, it is possible for it to
11512        be replaced with a MEM containing a multiply or shift.  */
11513     case MINUS:
11514       op = DW_OP_minus;
11515       goto do_binop;
11516
11517     case MULT:
11518       op = DW_OP_mul;
11519       goto do_binop;
11520
11521     case DIV:
11522       if (!dwarf_strict
11523           && GET_MODE_CLASS (mode) == MODE_INT
11524           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11525         {
11526           mem_loc_result = typed_binop (DW_OP_div, rtl,
11527                                         base_type_for_mode (mode, 0),
11528                                         mode, mem_mode);
11529           break;
11530         }
11531       op = DW_OP_div;
11532       goto do_binop;
11533
11534     case UMOD:
11535       op = DW_OP_mod;
11536       goto do_binop;
11537
11538     case ASHIFT:
11539       op = DW_OP_shl;
11540       goto do_shift;
11541
11542     case ASHIFTRT:
11543       op = DW_OP_shra;
11544       goto do_shift;
11545
11546     case LSHIFTRT:
11547       op = DW_OP_shr;
11548       goto do_shift;
11549
11550     do_shift:
11551       if (GET_MODE_CLASS (mode) != MODE_INT)
11552         break;
11553       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11554                                 VAR_INIT_STATUS_INITIALIZED);
11555       {
11556         rtx rtlop1 = XEXP (rtl, 1);
11557         if (GET_MODE (rtlop1) != VOIDmode
11558             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11559                < GET_MODE_BITSIZE (mode))
11560           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11561         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11562                                   VAR_INIT_STATUS_INITIALIZED);
11563       }
11564
11565       if (op0 == 0 || op1 == 0)
11566         break;
11567
11568       mem_loc_result = op0;
11569       add_loc_descr (&mem_loc_result, op1);
11570       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11571       break;
11572
11573     case AND:
11574       op = DW_OP_and;
11575       goto do_binop;
11576
11577     case IOR:
11578       op = DW_OP_or;
11579       goto do_binop;
11580
11581     case XOR:
11582       op = DW_OP_xor;
11583       goto do_binop;
11584
11585     do_binop:
11586       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11587                                 VAR_INIT_STATUS_INITIALIZED);
11588       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11589                                 VAR_INIT_STATUS_INITIALIZED);
11590
11591       if (op0 == 0 || op1 == 0)
11592         break;
11593
11594       mem_loc_result = op0;
11595       add_loc_descr (&mem_loc_result, op1);
11596       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11597       break;
11598
11599     case MOD:
11600       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11601         {
11602           mem_loc_result = typed_binop (DW_OP_mod, rtl,
11603                                         base_type_for_mode (mode, 0),
11604                                         mode, mem_mode);
11605           break;
11606         }
11607
11608       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11609                                 VAR_INIT_STATUS_INITIALIZED);
11610       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11611                                 VAR_INIT_STATUS_INITIALIZED);
11612
11613       if (op0 == 0 || op1 == 0)
11614         break;
11615
11616       mem_loc_result = op0;
11617       add_loc_descr (&mem_loc_result, op1);
11618       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11619       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11620       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11621       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11622       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11623       break;
11624
11625     case UDIV:
11626       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
11627         {
11628           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
11629             {
11630               op = DW_OP_div;
11631               goto do_binop;
11632             }
11633           mem_loc_result = typed_binop (DW_OP_div, rtl,
11634                                         base_type_for_mode (mode, 1),
11635                                         mode, mem_mode);
11636         }
11637       break;
11638
11639     case NOT:
11640       op = DW_OP_not;
11641       goto do_unop;
11642
11643     case ABS:
11644       op = DW_OP_abs;
11645       goto do_unop;
11646
11647     case NEG:
11648       op = DW_OP_neg;
11649       goto do_unop;
11650
11651     do_unop:
11652       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11653                                 VAR_INIT_STATUS_INITIALIZED);
11654
11655       if (op0 == 0)
11656         break;
11657
11658       mem_loc_result = op0;
11659       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11660       break;
11661
11662     case CONST_INT:
11663       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11664 #ifdef POINTERS_EXTEND_UNSIGNED
11665           || (mode == Pmode
11666               && mem_mode != VOIDmode
11667               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
11668 #endif
11669           )
11670         {
11671           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11672           break;
11673         }
11674       if (!dwarf_strict
11675           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
11676               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
11677         {
11678           dw_die_ref type_die = base_type_for_mode (mode, 1);
11679           enum machine_mode amode;
11680           if (type_die == NULL)
11681             return NULL;
11682           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
11683                                  MODE_INT, 0);
11684           if (INTVAL (rtl) >= 0
11685               && amode != BLKmode
11686               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
11687               /* const DW_OP_GNU_convert <XXX> vs.
11688                  DW_OP_GNU_const_type <XXX, 1, const>.  */
11689               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
11690                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
11691             {
11692               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11693               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11694               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11695               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11696               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
11697               add_loc_descr (&mem_loc_result, op0);
11698               return mem_loc_result;
11699             }
11700           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
11701                                           INTVAL (rtl));
11702           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11703           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11704           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11705           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11706             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11707           else
11708             {
11709               mem_loc_result->dw_loc_oprnd2.val_class
11710                 = dw_val_class_const_double;
11711               mem_loc_result->dw_loc_oprnd2.v.val_double
11712                 = shwi_to_double_int (INTVAL (rtl));
11713             }
11714         }
11715       break;
11716
11717     case CONST_DOUBLE:
11718       if (!dwarf_strict)
11719         {
11720           dw_die_ref type_die;
11721
11722           /* Note that a CONST_DOUBLE rtx could represent either an integer
11723              or a floating-point constant.  A CONST_DOUBLE is used whenever
11724              the constant requires more than one word in order to be
11725              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11726           if (mode == VOIDmode
11727               || (GET_MODE (rtl) == VOIDmode
11728                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
11729             break;
11730           type_die = base_type_for_mode (mode,
11731                                          GET_MODE_CLASS (mode) == MODE_INT);
11732           if (type_die == NULL)
11733             return NULL;
11734           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
11735           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11736           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11737           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11738           if (SCALAR_FLOAT_MODE_P (mode))
11739             {
11740               unsigned int length = GET_MODE_SIZE (mode);
11741               unsigned char *array
11742                   = (unsigned char*) ggc_alloc_atomic (length);
11743
11744               insert_float (rtl, array);
11745               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11746               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11747               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11748               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11749             }
11750           else
11751             {
11752               mem_loc_result->dw_loc_oprnd2.val_class
11753                 = dw_val_class_const_double;
11754               mem_loc_result->dw_loc_oprnd2.v.val_double
11755                 = rtx_to_double_int (rtl);
11756             }
11757         }
11758       break;
11759
11760     case EQ:
11761       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
11762       break;
11763
11764     case GE:
11765       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11766       break;
11767
11768     case GT:
11769       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11770       break;
11771
11772     case LE:
11773       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11774       break;
11775
11776     case LT:
11777       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11778       break;
11779
11780     case NE:
11781       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
11782       break;
11783
11784     case GEU:
11785       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
11786       break;
11787
11788     case GTU:
11789       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
11790       break;
11791
11792     case LEU:
11793       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
11794       break;
11795
11796     case LTU:
11797       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
11798       break;
11799
11800     case UMIN:
11801     case UMAX:
11802       if (GET_MODE_CLASS (mode) != MODE_INT)
11803         break;
11804       /* FALLTHRU */
11805     case SMIN:
11806     case SMAX:
11807       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
11808       break;
11809
11810     case ZERO_EXTRACT:
11811     case SIGN_EXTRACT:
11812       if (CONST_INT_P (XEXP (rtl, 1))
11813           && CONST_INT_P (XEXP (rtl, 2))
11814           && ((unsigned) INTVAL (XEXP (rtl, 1))
11815               + (unsigned) INTVAL (XEXP (rtl, 2))
11816               <= GET_MODE_BITSIZE (mode))
11817           && GET_MODE_CLASS (mode) == MODE_INT
11818           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11819           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11820         {
11821           int shift, size;
11822           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11823                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
11824           if (op0 == 0)
11825             break;
11826           if (GET_CODE (rtl) == SIGN_EXTRACT)
11827             op = DW_OP_shra;
11828           else
11829             op = DW_OP_shr;
11830           mem_loc_result = op0;
11831           size = INTVAL (XEXP (rtl, 1));
11832           shift = INTVAL (XEXP (rtl, 2));
11833           if (BITS_BIG_ENDIAN)
11834             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11835                     - shift - size;
11836           if (shift + size != (int) DWARF2_ADDR_SIZE)
11837             {
11838               add_loc_descr (&mem_loc_result,
11839                              int_loc_descriptor (DWARF2_ADDR_SIZE
11840                                                  - shift - size));
11841               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11842             }
11843           if (size != (int) DWARF2_ADDR_SIZE)
11844             {
11845               add_loc_descr (&mem_loc_result,
11846                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11847               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11848             }
11849         }
11850       break;
11851
11852     case IF_THEN_ELSE:
11853       {
11854         dw_loc_descr_ref op2, bra_node, drop_node;
11855         op0 = mem_loc_descriptor (XEXP (rtl, 0),
11856                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
11857                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
11858                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
11859         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11860                                   VAR_INIT_STATUS_INITIALIZED);
11861         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
11862                                   VAR_INIT_STATUS_INITIALIZED);
11863         if (op0 == NULL || op1 == NULL || op2 == NULL)
11864           break;
11865
11866         mem_loc_result = op1;
11867         add_loc_descr (&mem_loc_result, op2);
11868         add_loc_descr (&mem_loc_result, op0);
11869         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11870         add_loc_descr (&mem_loc_result, bra_node);
11871         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11872         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11873         add_loc_descr (&mem_loc_result, drop_node);
11874         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11875         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11876       }
11877       break;
11878
11879     case FLOAT_EXTEND:
11880     case FLOAT_TRUNCATE:
11881     case FLOAT:
11882     case UNSIGNED_FLOAT:
11883     case FIX:
11884     case UNSIGNED_FIX:
11885       if (!dwarf_strict)
11886         {
11887           dw_die_ref type_die;
11888           dw_loc_descr_ref cvt;
11889
11890           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11891                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
11892           if (op0 == NULL)
11893             break;
11894           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
11895               && (GET_CODE (rtl) == FLOAT
11896                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
11897                      <= DWARF2_ADDR_SIZE))
11898             {
11899               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11900                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
11901               if (type_die == NULL)
11902                 break;
11903               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11904               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11905               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11906               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11907               add_loc_descr (&op0, cvt);
11908             }
11909           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
11910           if (type_die == NULL)
11911             break;
11912           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11913           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11914           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11915           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11916           add_loc_descr (&op0, cvt);
11917           if (GET_MODE_CLASS (mode) == MODE_INT
11918               && (GET_CODE (rtl) == FIX
11919                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
11920             {
11921               op0 = convert_descriptor_to_mode (mode, op0);
11922               if (op0 == NULL)
11923                 break;
11924             }
11925           mem_loc_result = op0;
11926         }
11927       break;
11928
11929     case CLZ:
11930     case CTZ:
11931     case FFS:
11932       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
11933       break;
11934
11935     case POPCOUNT:
11936     case PARITY:
11937       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
11938       break;
11939
11940     case BSWAP:
11941       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
11942       break;
11943
11944     case ROTATE:
11945     case ROTATERT:
11946       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
11947       break;
11948
11949     case COMPARE:
11950     case TRUNCATE:
11951       /* In theory, we could implement the above.  */
11952       /* DWARF cannot represent the unsigned compare operations
11953          natively.  */
11954     case SS_MULT:
11955     case US_MULT:
11956     case SS_DIV:
11957     case US_DIV:
11958     case SS_PLUS:
11959     case US_PLUS:
11960     case SS_MINUS:
11961     case US_MINUS:
11962     case SS_NEG:
11963     case US_NEG:
11964     case SS_ABS:
11965     case SS_ASHIFT:
11966     case US_ASHIFT:
11967     case SS_TRUNCATE:
11968     case US_TRUNCATE:
11969     case UNORDERED:
11970     case ORDERED:
11971     case UNEQ:
11972     case UNGE:
11973     case UNGT:
11974     case UNLE:
11975     case UNLT:
11976     case LTGT:
11977     case FRACT_CONVERT:
11978     case UNSIGNED_FRACT_CONVERT:
11979     case SAT_FRACT:
11980     case UNSIGNED_SAT_FRACT:
11981     case SQRT:
11982     case ASM_OPERANDS:
11983     case VEC_MERGE:
11984     case VEC_SELECT:
11985     case VEC_CONCAT:
11986     case VEC_DUPLICATE:
11987     case UNSPEC:
11988     case HIGH:
11989     case FMA:
11990     case STRICT_LOW_PART:
11991     case CONST_VECTOR:
11992     case CONST_FIXED:
11993     case CLRSB:
11994       /* If delegitimize_address couldn't do anything with the UNSPEC, we
11995          can't express it in the debug info.  This can happen e.g. with some
11996          TLS UNSPECs.  */
11997       break;
11998
11999     case CONST_STRING:
12000       resolve_one_addr (&rtl, NULL);
12001       goto symref;
12002
12003     default:
12004 #ifdef ENABLE_CHECKING
12005       print_rtl (stderr, rtl);
12006       gcc_unreachable ();
12007 #else
12008       break;
12009 #endif
12010     }
12011
12012   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12013     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12014
12015   return mem_loc_result;
12016 }
12017
12018 /* Return a descriptor that describes the concatenation of two locations.
12019    This is typically a complex variable.  */
12020
12021 static dw_loc_descr_ref
12022 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12023 {
12024   dw_loc_descr_ref cc_loc_result = NULL;
12025   dw_loc_descr_ref x0_ref
12026     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12027   dw_loc_descr_ref x1_ref
12028     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12029
12030   if (x0_ref == 0 || x1_ref == 0)
12031     return 0;
12032
12033   cc_loc_result = x0_ref;
12034   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12035
12036   add_loc_descr (&cc_loc_result, x1_ref);
12037   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12038
12039   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12040     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12041
12042   return cc_loc_result;
12043 }
12044
12045 /* Return a descriptor that describes the concatenation of N
12046    locations.  */
12047
12048 static dw_loc_descr_ref
12049 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12050 {
12051   unsigned int i;
12052   dw_loc_descr_ref cc_loc_result = NULL;
12053   unsigned int n = XVECLEN (concatn, 0);
12054
12055   for (i = 0; i < n; ++i)
12056     {
12057       dw_loc_descr_ref ref;
12058       rtx x = XVECEXP (concatn, 0, i);
12059
12060       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12061       if (ref == NULL)
12062         return NULL;
12063
12064       add_loc_descr (&cc_loc_result, ref);
12065       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12066     }
12067
12068   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12069     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12070
12071   return cc_loc_result;
12072 }
12073
12074 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12075    for DEBUG_IMPLICIT_PTR RTL.  */
12076
12077 static dw_loc_descr_ref
12078 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12079 {
12080   dw_loc_descr_ref ret;
12081   dw_die_ref ref;
12082
12083   if (dwarf_strict)
12084     return NULL;
12085   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12086               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12087               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12088   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12089   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12090   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12091   if (ref)
12092     {
12093       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12094       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12095       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12096     }
12097   else
12098     {
12099       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12100       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12101     }
12102   return ret;
12103 }
12104
12105 /* Output a proper Dwarf location descriptor for a variable or parameter
12106    which is either allocated in a register or in a memory location.  For a
12107    register, we just generate an OP_REG and the register number.  For a
12108    memory location we provide a Dwarf postfix expression describing how to
12109    generate the (dynamic) address of the object onto the address stack.
12110
12111    MODE is mode of the decl if this loc_descriptor is going to be used in
12112    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12113    allowed, VOIDmode otherwise.
12114
12115    If we don't know how to describe it, return 0.  */
12116
12117 static dw_loc_descr_ref
12118 loc_descriptor (rtx rtl, enum machine_mode mode,
12119                 enum var_init_status initialized)
12120 {
12121   dw_loc_descr_ref loc_result = NULL;
12122
12123   switch (GET_CODE (rtl))
12124     {
12125     case SUBREG:
12126       /* The case of a subreg may arise when we have a local (register)
12127          variable or a formal (register) parameter which doesn't quite fill
12128          up an entire register.  For now, just assume that it is
12129          legitimate to make the Dwarf info refer to the whole register which
12130          contains the given subreg.  */
12131       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12132         loc_result = loc_descriptor (SUBREG_REG (rtl),
12133                                      GET_MODE (SUBREG_REG (rtl)), initialized);
12134       else
12135         goto do_default;
12136       break;
12137
12138     case REG:
12139       loc_result = reg_loc_descriptor (rtl, initialized);
12140       break;
12141
12142     case MEM:
12143       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12144                                        GET_MODE (rtl), initialized);
12145       if (loc_result == NULL)
12146         loc_result = tls_mem_loc_descriptor (rtl);
12147       if (loc_result == NULL)
12148         {
12149           rtx new_rtl = avoid_constant_pool_reference (rtl);
12150           if (new_rtl != rtl)
12151             loc_result = loc_descriptor (new_rtl, mode, initialized);
12152         }
12153       break;
12154
12155     case CONCAT:
12156       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12157                                           initialized);
12158       break;
12159
12160     case CONCATN:
12161       loc_result = concatn_loc_descriptor (rtl, initialized);
12162       break;
12163
12164     case VAR_LOCATION:
12165       /* Single part.  */
12166       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12167         {
12168           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12169           if (GET_CODE (loc) == EXPR_LIST)
12170             loc = XEXP (loc, 0);
12171           loc_result = loc_descriptor (loc, mode, initialized);
12172           break;
12173         }
12174
12175       rtl = XEXP (rtl, 1);
12176       /* FALLTHRU */
12177
12178     case PARALLEL:
12179       {
12180         rtvec par_elems = XVEC (rtl, 0);
12181         int num_elem = GET_NUM_ELEM (par_elems);
12182         enum machine_mode mode;
12183         int i;
12184
12185         /* Create the first one, so we have something to add to.  */
12186         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12187                                      VOIDmode, initialized);
12188         if (loc_result == NULL)
12189           return NULL;
12190         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12191         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12192         for (i = 1; i < num_elem; i++)
12193           {
12194             dw_loc_descr_ref temp;
12195
12196             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12197                                    VOIDmode, initialized);
12198             if (temp == NULL)
12199               return NULL;
12200             add_loc_descr (&loc_result, temp);
12201             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12202             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12203           }
12204       }
12205       break;
12206
12207     case CONST_INT:
12208       if (mode != VOIDmode && mode != BLKmode)
12209         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12210                                                     INTVAL (rtl));
12211       break;
12212
12213     case CONST_DOUBLE:
12214       if (mode == VOIDmode)
12215         mode = GET_MODE (rtl);
12216
12217       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12218         {
12219           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12220
12221           /* Note that a CONST_DOUBLE rtx could represent either an integer
12222              or a floating-point constant.  A CONST_DOUBLE is used whenever
12223              the constant requires more than one word in order to be
12224              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12225           loc_result = new_loc_descr (DW_OP_implicit_value,
12226                                       GET_MODE_SIZE (mode), 0);
12227           if (SCALAR_FLOAT_MODE_P (mode))
12228             {
12229               unsigned int length = GET_MODE_SIZE (mode);
12230               unsigned char *array
12231                   = (unsigned char*) ggc_alloc_atomic (length);
12232
12233               insert_float (rtl, array);
12234               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12235               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12236               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12237               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12238             }
12239           else
12240             {
12241               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12242               loc_result->dw_loc_oprnd2.v.val_double
12243                 = rtx_to_double_int (rtl);
12244             }
12245         }
12246       break;
12247
12248     case CONST_VECTOR:
12249       if (mode == VOIDmode)
12250         mode = GET_MODE (rtl);
12251
12252       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12253         {
12254           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12255           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12256           unsigned char *array = (unsigned char *)
12257             ggc_alloc_atomic (length * elt_size);
12258           unsigned int i;
12259           unsigned char *p;
12260
12261           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12262           switch (GET_MODE_CLASS (mode))
12263             {
12264             case MODE_VECTOR_INT:
12265               for (i = 0, p = array; i < length; i++, p += elt_size)
12266                 {
12267                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12268                   double_int val = rtx_to_double_int (elt);
12269
12270                   if (elt_size <= sizeof (HOST_WIDE_INT))
12271                     insert_int (double_int_to_shwi (val), elt_size, p);
12272                   else
12273                     {
12274                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12275                       insert_double (val, p);
12276                     }
12277                 }
12278               break;
12279
12280             case MODE_VECTOR_FLOAT:
12281               for (i = 0, p = array; i < length; i++, p += elt_size)
12282                 {
12283                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12284                   insert_float (elt, p);
12285                 }
12286               break;
12287
12288             default:
12289               gcc_unreachable ();
12290             }
12291
12292           loc_result = new_loc_descr (DW_OP_implicit_value,
12293                                       length * elt_size, 0);
12294           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12295           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12296           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12297           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12298         }
12299       break;
12300
12301     case CONST:
12302       if (mode == VOIDmode
12303           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12304           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12305           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12306         {
12307           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12308           break;
12309         }
12310       /* FALLTHROUGH */
12311     case SYMBOL_REF:
12312       if (!const_ok_for_output (rtl))
12313         break;
12314     case LABEL_REF:
12315       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12316           && (dwarf_version >= 4 || !dwarf_strict))
12317         {
12318           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12319           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12320           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12321           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12322           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12323         }
12324       break;
12325
12326     case DEBUG_IMPLICIT_PTR:
12327       loc_result = implicit_ptr_descriptor (rtl, 0);
12328       break;
12329
12330     case PLUS:
12331       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12332           && CONST_INT_P (XEXP (rtl, 1)))
12333         {
12334           loc_result
12335             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12336           break;
12337         }
12338       /* FALLTHRU */
12339     do_default:
12340     default:
12341       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12342            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12343            && dwarf_version >= 4)
12344           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12345         {
12346           /* Value expression.  */
12347           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12348           if (loc_result)
12349             add_loc_descr (&loc_result,
12350                            new_loc_descr (DW_OP_stack_value, 0, 0));
12351         }
12352       break;
12353     }
12354
12355   return loc_result;
12356 }
12357
12358 /* We need to figure out what section we should use as the base for the
12359    address ranges where a given location is valid.
12360    1. If this particular DECL has a section associated with it, use that.
12361    2. If this function has a section associated with it, use that.
12362    3. Otherwise, use the text section.
12363    XXX: If you split a variable across multiple sections, we won't notice.  */
12364
12365 static const char *
12366 secname_for_decl (const_tree decl)
12367 {
12368   const char *secname;
12369
12370   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12371     {
12372       tree sectree = DECL_SECTION_NAME (decl);
12373       secname = TREE_STRING_POINTER (sectree);
12374     }
12375   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12376     {
12377       tree sectree = DECL_SECTION_NAME (current_function_decl);
12378       secname = TREE_STRING_POINTER (sectree);
12379     }
12380   else if (cfun && in_cold_section_p)
12381     secname = crtl->subsections.cold_section_label;
12382   else
12383     secname = text_section_label;
12384
12385   return secname;
12386 }
12387
12388 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12389
12390 static bool
12391 decl_by_reference_p (tree decl)
12392 {
12393   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12394            || TREE_CODE (decl) == VAR_DECL)
12395           && DECL_BY_REFERENCE (decl));
12396 }
12397
12398 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12399    for VARLOC.  */
12400
12401 static dw_loc_descr_ref
12402 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12403                enum var_init_status initialized)
12404 {
12405   int have_address = 0;
12406   dw_loc_descr_ref descr;
12407   enum machine_mode mode;
12408
12409   if (want_address != 2)
12410     {
12411       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12412       /* Single part.  */
12413       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12414         {
12415           varloc = PAT_VAR_LOCATION_LOC (varloc);
12416           if (GET_CODE (varloc) == EXPR_LIST)
12417             varloc = XEXP (varloc, 0);
12418           mode = GET_MODE (varloc);
12419           if (MEM_P (varloc))
12420             {
12421               rtx addr = XEXP (varloc, 0);
12422               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12423                                           mode, initialized);
12424               if (descr)
12425                 have_address = 1;
12426               else
12427                 {
12428                   rtx x = avoid_constant_pool_reference (varloc);
12429                   if (x != varloc)
12430                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12431                                                 initialized);
12432                 }
12433             }
12434           else
12435             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12436         }
12437       else
12438         return 0;
12439     }
12440   else
12441     {
12442       if (GET_CODE (varloc) == VAR_LOCATION)
12443         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12444       else
12445         mode = DECL_MODE (loc);
12446       descr = loc_descriptor (varloc, mode, initialized);
12447       have_address = 1;
12448     }
12449
12450   if (!descr)
12451     return 0;
12452
12453   if (want_address == 2 && !have_address
12454       && (dwarf_version >= 4 || !dwarf_strict))
12455     {
12456       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12457         {
12458           expansion_failed (loc, NULL_RTX,
12459                             "DWARF address size mismatch");
12460           return 0;
12461         }
12462       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12463       have_address = 1;
12464     }
12465   /* Show if we can't fill the request for an address.  */
12466   if (want_address && !have_address)
12467     {
12468       expansion_failed (loc, NULL_RTX,
12469                         "Want address and only have value");
12470       return 0;
12471     }
12472
12473   /* If we've got an address and don't want one, dereference.  */
12474   if (!want_address && have_address)
12475     {
12476       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12477       enum dwarf_location_atom op;
12478
12479       if (size > DWARF2_ADDR_SIZE || size == -1)
12480         {
12481           expansion_failed (loc, NULL_RTX,
12482                             "DWARF address size mismatch");
12483           return 0;
12484         }
12485       else if (size == DWARF2_ADDR_SIZE)
12486         op = DW_OP_deref;
12487       else
12488         op = DW_OP_deref_size;
12489
12490       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12491     }
12492
12493   return descr;
12494 }
12495
12496 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12497    if it is not possible.  */
12498
12499 static dw_loc_descr_ref
12500 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12501 {
12502   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12503     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12504   else if (dwarf_version >= 3 || !dwarf_strict)
12505     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12506   else
12507     return NULL;
12508 }
12509
12510 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12511    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12512
12513 static dw_loc_descr_ref
12514 dw_sra_loc_expr (tree decl, rtx loc)
12515 {
12516   rtx p;
12517   unsigned int padsize = 0;
12518   dw_loc_descr_ref descr, *descr_tail;
12519   unsigned HOST_WIDE_INT decl_size;
12520   rtx varloc;
12521   enum var_init_status initialized;
12522
12523   if (DECL_SIZE (decl) == NULL
12524       || !host_integerp (DECL_SIZE (decl), 1))
12525     return NULL;
12526
12527   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12528   descr = NULL;
12529   descr_tail = &descr;
12530
12531   for (p = loc; p; p = XEXP (p, 1))
12532     {
12533       unsigned int bitsize = decl_piece_bitsize (p);
12534       rtx loc_note = *decl_piece_varloc_ptr (p);
12535       dw_loc_descr_ref cur_descr;
12536       dw_loc_descr_ref *tail, last = NULL;
12537       unsigned int opsize = 0;
12538
12539       if (loc_note == NULL_RTX
12540           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12541         {
12542           padsize += bitsize;
12543           continue;
12544         }
12545       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12546       varloc = NOTE_VAR_LOCATION (loc_note);
12547       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12548       if (cur_descr == NULL)
12549         {
12550           padsize += bitsize;
12551           continue;
12552         }
12553
12554       /* Check that cur_descr either doesn't use
12555          DW_OP_*piece operations, or their sum is equal
12556          to bitsize.  Otherwise we can't embed it.  */
12557       for (tail = &cur_descr; *tail != NULL;
12558            tail = &(*tail)->dw_loc_next)
12559         if ((*tail)->dw_loc_opc == DW_OP_piece)
12560           {
12561             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12562                       * BITS_PER_UNIT;
12563             last = *tail;
12564           }
12565         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12566           {
12567             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12568             last = *tail;
12569           }
12570
12571       if (last != NULL && opsize != bitsize)
12572         {
12573           padsize += bitsize;
12574           continue;
12575         }
12576
12577       /* If there is a hole, add DW_OP_*piece after empty DWARF
12578          expression, which means that those bits are optimized out.  */
12579       if (padsize)
12580         {
12581           if (padsize > decl_size)
12582             return NULL;
12583           decl_size -= padsize;
12584           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12585           if (*descr_tail == NULL)
12586             return NULL;
12587           descr_tail = &(*descr_tail)->dw_loc_next;
12588           padsize = 0;
12589         }
12590       *descr_tail = cur_descr;
12591       descr_tail = tail;
12592       if (bitsize > decl_size)
12593         return NULL;
12594       decl_size -= bitsize;
12595       if (last == NULL)
12596         {
12597           HOST_WIDE_INT offset = 0;
12598           if (GET_CODE (varloc) == VAR_LOCATION
12599               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12600             {
12601               varloc = PAT_VAR_LOCATION_LOC (varloc);
12602               if (GET_CODE (varloc) == EXPR_LIST)
12603                 varloc = XEXP (varloc, 0);
12604             }
12605           do 
12606             {
12607               if (GET_CODE (varloc) == CONST
12608                   || GET_CODE (varloc) == SIGN_EXTEND
12609                   || GET_CODE (varloc) == ZERO_EXTEND)
12610                 varloc = XEXP (varloc, 0);
12611               else if (GET_CODE (varloc) == SUBREG)
12612                 varloc = SUBREG_REG (varloc);
12613               else
12614                 break;
12615             }
12616           while (1);
12617           /* DW_OP_bit_size offset should be zero for register
12618              or implicit location descriptions and empty location
12619              descriptions, but for memory addresses needs big endian
12620              adjustment.  */
12621           if (MEM_P (varloc))
12622             {
12623               unsigned HOST_WIDE_INT memsize
12624                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
12625               if (memsize != bitsize)
12626                 {
12627                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
12628                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
12629                     return NULL;
12630                   if (memsize < bitsize)
12631                     return NULL;
12632                   if (BITS_BIG_ENDIAN)
12633                     offset = memsize - bitsize;
12634                 }
12635             }
12636
12637           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
12638           if (*descr_tail == NULL)
12639             return NULL;
12640           descr_tail = &(*descr_tail)->dw_loc_next;
12641         }
12642     }
12643
12644   /* If there were any non-empty expressions, add padding till the end of
12645      the decl.  */
12646   if (descr != NULL && decl_size != 0)
12647     {
12648       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
12649       if (*descr_tail == NULL)
12650         return NULL;
12651     }
12652   return descr;
12653 }
12654
12655 /* Return the dwarf representation of the location list LOC_LIST of
12656    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
12657    function.  */
12658
12659 static dw_loc_list_ref
12660 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
12661 {
12662   const char *endname, *secname;
12663   rtx varloc;
12664   enum var_init_status initialized;
12665   struct var_loc_node *node;
12666   dw_loc_descr_ref descr;
12667   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12668   dw_loc_list_ref list = NULL;
12669   dw_loc_list_ref *listp = &list;
12670
12671   /* Now that we know what section we are using for a base,
12672      actually construct the list of locations.
12673      The first location information is what is passed to the
12674      function that creates the location list, and the remaining
12675      locations just get added on to that list.
12676      Note that we only know the start address for a location
12677      (IE location changes), so to build the range, we use
12678      the range [current location start, next location start].
12679      This means we have to special case the last node, and generate
12680      a range of [last location start, end of function label].  */
12681
12682   secname = secname_for_decl (decl);
12683
12684   for (node = loc_list->first; node; node = node->next)
12685     if (GET_CODE (node->loc) == EXPR_LIST
12686         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
12687       {
12688         if (GET_CODE (node->loc) == EXPR_LIST)
12689           {
12690             /* This requires DW_OP_{,bit_}piece, which is not usable
12691                inside DWARF expressions.  */
12692             if (want_address != 2)
12693               continue;
12694             descr = dw_sra_loc_expr (decl, node->loc);
12695             if (descr == NULL)
12696               continue;
12697           }
12698         else
12699           {
12700             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12701             varloc = NOTE_VAR_LOCATION (node->loc);
12702             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
12703           }
12704         if (descr)
12705           {
12706             bool range_across_switch = false;
12707             /* If section switch happens in between node->label
12708                and node->next->label (or end of function) and
12709                we can't emit it as a single entry list,
12710                emit two ranges, first one ending at the end
12711                of first partition and second one starting at the
12712                beginning of second partition.  */
12713             if (node == loc_list->last_before_switch
12714                 && (node != loc_list->first || loc_list->first->next)
12715                 && current_function_decl)
12716               {
12717                 endname = cfun->fde->dw_fde_end;
12718                 range_across_switch = true;
12719               }
12720             /* The variable has a location between NODE->LABEL and
12721                NODE->NEXT->LABEL.  */
12722             else if (node->next)
12723               endname = node->next->label;
12724             /* If the variable has a location at the last label
12725                it keeps its location until the end of function.  */
12726             else if (!current_function_decl)
12727               endname = text_end_label;
12728             else
12729               {
12730                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12731                                              current_function_funcdef_no);
12732                 endname = ggc_strdup (label_id);
12733               }
12734
12735             *listp = new_loc_list (descr, node->label, endname, secname);
12736             if (TREE_CODE (decl) == PARM_DECL
12737                 && node == loc_list->first
12738                 && GET_CODE (node->loc) == NOTE
12739                 && strcmp (node->label, endname) == 0)
12740               (*listp)->force = true;
12741             listp = &(*listp)->dw_loc_next;
12742
12743             if (range_across_switch)
12744               {
12745                 if (GET_CODE (node->loc) == EXPR_LIST)
12746                   descr = dw_sra_loc_expr (decl, node->loc);
12747                 else
12748                   {
12749                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12750                     varloc = NOTE_VAR_LOCATION (node->loc);
12751                     descr = dw_loc_list_1 (decl, varloc, want_address,
12752                                            initialized);
12753                   }
12754                 gcc_assert (descr);
12755                 /* The variable has a location between NODE->LABEL and
12756                    NODE->NEXT->LABEL.  */
12757                 if (node->next)
12758                   endname = node->next->label;
12759                 else
12760                   endname = cfun->fde->dw_fde_second_end;
12761                 *listp = new_loc_list (descr,
12762                                        cfun->fde->dw_fde_second_begin,
12763                                        endname, secname);
12764                 listp = &(*listp)->dw_loc_next;
12765               }
12766           }
12767       }
12768
12769   /* Try to avoid the overhead of a location list emitting a location
12770      expression instead, but only if we didn't have more than one
12771      location entry in the first place.  If some entries were not
12772      representable, we don't want to pretend a single entry that was
12773      applies to the entire scope in which the variable is
12774      available.  */
12775   if (list && loc_list->first->next)
12776     gen_llsym (list);
12777
12778   return list;
12779 }
12780
12781 /* Return if the loc_list has only single element and thus can be represented
12782    as location description.   */
12783
12784 static bool
12785 single_element_loc_list_p (dw_loc_list_ref list)
12786 {
12787   gcc_assert (!list->dw_loc_next || list->ll_symbol);
12788   return !list->ll_symbol;
12789 }
12790
12791 /* To each location in list LIST add loc descr REF.  */
12792
12793 static void
12794 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
12795 {
12796   dw_loc_descr_ref copy;
12797   add_loc_descr (&list->expr, ref);
12798   list = list->dw_loc_next;
12799   while (list)
12800     {
12801       copy = ggc_alloc_dw_loc_descr_node ();
12802       memcpy (copy, ref, sizeof (dw_loc_descr_node));
12803       add_loc_descr (&list->expr, copy);
12804       while (copy->dw_loc_next)
12805         {
12806           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
12807           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
12808           copy->dw_loc_next = new_copy;
12809           copy = new_copy;
12810         }
12811       list = list->dw_loc_next;
12812     }
12813 }
12814
12815 /* Given two lists RET and LIST
12816    produce location list that is result of adding expression in LIST
12817    to expression in RET on each position in program.
12818    Might be destructive on both RET and LIST.
12819
12820    TODO: We handle only simple cases of RET or LIST having at most one
12821    element. General case would inolve sorting the lists in program order
12822    and merging them that will need some additional work.
12823    Adding that will improve quality of debug info especially for SRA-ed
12824    structures.  */
12825
12826 static void
12827 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
12828 {
12829   if (!list)
12830     return;
12831   if (!*ret)
12832     {
12833       *ret = list;
12834       return;
12835     }
12836   if (!list->dw_loc_next)
12837     {
12838       add_loc_descr_to_each (*ret, list->expr);
12839       return;
12840     }
12841   if (!(*ret)->dw_loc_next)
12842     {
12843       add_loc_descr_to_each (list, (*ret)->expr);
12844       *ret = list;
12845       return;
12846     }
12847   expansion_failed (NULL_TREE, NULL_RTX,
12848                     "Don't know how to merge two non-trivial"
12849                     " location lists.\n");
12850   *ret = NULL;
12851   return;
12852 }
12853
12854 /* LOC is constant expression.  Try a luck, look it up in constant
12855    pool and return its loc_descr of its address.  */
12856
12857 static dw_loc_descr_ref
12858 cst_pool_loc_descr (tree loc)
12859 {
12860   /* Get an RTL for this, if something has been emitted.  */
12861   rtx rtl = lookup_constant_def (loc);
12862
12863   if (!rtl || !MEM_P (rtl))
12864     {
12865       gcc_assert (!rtl);
12866       return 0;
12867     }
12868   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
12869
12870   /* TODO: We might get more coverage if we was actually delaying expansion
12871      of all expressions till end of compilation when constant pools are fully
12872      populated.  */
12873   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
12874     {
12875       expansion_failed (loc, NULL_RTX,
12876                         "CST value in contant pool but not marked.");
12877       return 0;
12878     }
12879   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12880                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
12881 }
12882
12883 /* Return dw_loc_list representing address of addr_expr LOC
12884    by looking for inner INDIRECT_REF expression and turning
12885    it into simple arithmetics.  */
12886
12887 static dw_loc_list_ref
12888 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
12889 {
12890   tree obj, offset;
12891   HOST_WIDE_INT bitsize, bitpos, bytepos;
12892   enum machine_mode mode;
12893   int unsignedp, volatilep = 0;
12894   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12895
12896   obj = get_inner_reference (TREE_OPERAND (loc, 0),
12897                              &bitsize, &bitpos, &offset, &mode,
12898                              &unsignedp, &volatilep, false);
12899   STRIP_NOPS (obj);
12900   if (bitpos % BITS_PER_UNIT)
12901     {
12902       expansion_failed (loc, NULL_RTX, "bitfield access");
12903       return 0;
12904     }
12905   if (!INDIRECT_REF_P (obj))
12906     {
12907       expansion_failed (obj,
12908                         NULL_RTX, "no indirect ref in inner refrence");
12909       return 0;
12910     }
12911   if (!offset && !bitpos)
12912     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
12913   else if (toplev
12914            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
12915            && (dwarf_version >= 4 || !dwarf_strict))
12916     {
12917       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
12918       if (!list_ret)
12919         return 0;
12920       if (offset)
12921         {
12922           /* Variable offset.  */
12923           list_ret1 = loc_list_from_tree (offset, 0);
12924           if (list_ret1 == 0)
12925             return 0;
12926           add_loc_list (&list_ret, list_ret1);
12927           if (!list_ret)
12928             return 0;
12929           add_loc_descr_to_each (list_ret,
12930                                  new_loc_descr (DW_OP_plus, 0, 0));
12931         }
12932       bytepos = bitpos / BITS_PER_UNIT;
12933       if (bytepos > 0)
12934         add_loc_descr_to_each (list_ret,
12935                                new_loc_descr (DW_OP_plus_uconst,
12936                                               bytepos, 0));
12937       else if (bytepos < 0)
12938         loc_list_plus_const (list_ret, bytepos);
12939       add_loc_descr_to_each (list_ret,
12940                              new_loc_descr (DW_OP_stack_value, 0, 0));
12941     }
12942   return list_ret;
12943 }
12944
12945
12946 /* Generate Dwarf location list representing LOC.
12947    If WANT_ADDRESS is false, expression computing LOC will be computed
12948    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
12949    if WANT_ADDRESS is 2, expression computing address useable in location
12950      will be returned (i.e. DW_OP_reg can be used
12951      to refer to register values).  */
12952
12953 static dw_loc_list_ref
12954 loc_list_from_tree (tree loc, int want_address)
12955 {
12956   dw_loc_descr_ref ret = NULL, ret1 = NULL;
12957   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12958   int have_address = 0;
12959   enum dwarf_location_atom op;
12960
12961   /* ??? Most of the time we do not take proper care for sign/zero
12962      extending the values properly.  Hopefully this won't be a real
12963      problem...  */
12964
12965   switch (TREE_CODE (loc))
12966     {
12967     case ERROR_MARK:
12968       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
12969       return 0;
12970
12971     case PLACEHOLDER_EXPR:
12972       /* This case involves extracting fields from an object to determine the
12973          position of other fields.  We don't try to encode this here.  The
12974          only user of this is Ada, which encodes the needed information using
12975          the names of types.  */
12976       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
12977       return 0;
12978
12979     case CALL_EXPR:
12980       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
12981       /* There are no opcodes for these operations.  */
12982       return 0;
12983
12984     case PREINCREMENT_EXPR:
12985     case PREDECREMENT_EXPR:
12986     case POSTINCREMENT_EXPR:
12987     case POSTDECREMENT_EXPR:
12988       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
12989       /* There are no opcodes for these operations.  */
12990       return 0;
12991
12992     case ADDR_EXPR:
12993       /* If we already want an address, see if there is INDIRECT_REF inside
12994          e.g. for &this->field.  */
12995       if (want_address)
12996         {
12997           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
12998                        (loc, want_address == 2);
12999           if (list_ret)
13000             have_address = 1;
13001           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13002                    && (ret = cst_pool_loc_descr (loc)))
13003             have_address = 1;
13004         }
13005         /* Otherwise, process the argument and look for the address.  */
13006       if (!list_ret && !ret)
13007         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13008       else
13009         {
13010           if (want_address)
13011             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13012           return NULL;
13013         }
13014       break;
13015
13016     case VAR_DECL:
13017       if (DECL_THREAD_LOCAL_P (loc))
13018         {
13019           rtx rtl;
13020           enum dwarf_location_atom first_op;
13021           enum dwarf_location_atom second_op;
13022           bool dtprel = false;
13023
13024           if (targetm.have_tls)
13025             {
13026               /* If this is not defined, we have no way to emit the
13027                  data.  */
13028               if (!targetm.asm_out.output_dwarf_dtprel)
13029                 return 0;
13030
13031                /* The way DW_OP_GNU_push_tls_address is specified, we
13032                   can only look up addresses of objects in the current
13033                   module.  We used DW_OP_addr as first op, but that's
13034                   wrong, because DW_OP_addr is relocated by the debug
13035                   info consumer, while DW_OP_GNU_push_tls_address
13036                   operand shouldn't be.  */
13037               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13038                 return 0;
13039               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13040               dtprel = true;
13041               second_op = DW_OP_GNU_push_tls_address;
13042             }
13043           else
13044             {
13045               if (!targetm.emutls.debug_form_tls_address
13046                   || !(dwarf_version >= 3 || !dwarf_strict))
13047                 return 0;
13048               /* We stuffed the control variable into the DECL_VALUE_EXPR
13049                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13050                  no longer appear in gimple code.  We used the control
13051                  variable in specific so that we could pick it up here.  */
13052               loc = DECL_VALUE_EXPR (loc);
13053               first_op = DW_OP_addr;
13054               second_op = DW_OP_form_tls_address;
13055             }
13056
13057           rtl = rtl_for_decl_location (loc);
13058           if (rtl == NULL_RTX)
13059             return 0;
13060
13061           if (!MEM_P (rtl))
13062             return 0;
13063           rtl = XEXP (rtl, 0);
13064           if (! CONSTANT_P (rtl))
13065             return 0;
13066
13067           ret = new_loc_descr (first_op, 0, 0);
13068           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13069           ret->dw_loc_oprnd1.v.val_addr = rtl;
13070           ret->dtprel = dtprel;
13071
13072           ret1 = new_loc_descr (second_op, 0, 0);
13073           add_loc_descr (&ret, ret1);
13074
13075           have_address = 1;
13076           break;
13077         }
13078       /* FALLTHRU */
13079
13080     case PARM_DECL:
13081     case RESULT_DECL:
13082       if (DECL_HAS_VALUE_EXPR_P (loc))
13083         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13084                                    want_address);
13085       /* FALLTHRU */
13086
13087     case FUNCTION_DECL:
13088       {
13089         rtx rtl;
13090         var_loc_list *loc_list = lookup_decl_loc (loc);
13091
13092         if (loc_list && loc_list->first)
13093           {
13094             list_ret = dw_loc_list (loc_list, loc, want_address);
13095             have_address = want_address != 0;
13096             break;
13097           }
13098         rtl = rtl_for_decl_location (loc);
13099         if (rtl == NULL_RTX)
13100           {
13101             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13102             return 0;
13103           }
13104         else if (CONST_INT_P (rtl))
13105           {
13106             HOST_WIDE_INT val = INTVAL (rtl);
13107             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13108               val &= GET_MODE_MASK (DECL_MODE (loc));
13109             ret = int_loc_descriptor (val);
13110           }
13111         else if (GET_CODE (rtl) == CONST_STRING)
13112           {
13113             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13114             return 0;
13115           }
13116         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13117           {
13118             ret = new_loc_descr (DW_OP_addr, 0, 0);
13119             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13120             ret->dw_loc_oprnd1.v.val_addr = rtl;
13121           }
13122         else
13123           {
13124             enum machine_mode mode, mem_mode;
13125
13126             /* Certain constructs can only be represented at top-level.  */
13127             if (want_address == 2)
13128               {
13129                 ret = loc_descriptor (rtl, VOIDmode,
13130                                       VAR_INIT_STATUS_INITIALIZED);
13131                 have_address = 1;
13132               }
13133             else
13134               {
13135                 mode = GET_MODE (rtl);
13136                 mem_mode = VOIDmode;
13137                 if (MEM_P (rtl))
13138                   {
13139                     mem_mode = mode;
13140                     mode = get_address_mode (rtl);
13141                     rtl = XEXP (rtl, 0);
13142                     have_address = 1;
13143                   }
13144                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13145                                           VAR_INIT_STATUS_INITIALIZED);
13146               }
13147             if (!ret)
13148               expansion_failed (loc, rtl,
13149                                 "failed to produce loc descriptor for rtl");
13150           }
13151       }
13152       break;
13153
13154     case MEM_REF:
13155       /* ??? FIXME.  */
13156       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13157         return 0;
13158       /* Fallthru.  */
13159     case INDIRECT_REF:
13160       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13161       have_address = 1;
13162       break;
13163
13164     case COMPOUND_EXPR:
13165       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13166
13167     CASE_CONVERT:
13168     case VIEW_CONVERT_EXPR:
13169     case SAVE_EXPR:
13170     case MODIFY_EXPR:
13171       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13172
13173     case COMPONENT_REF:
13174     case BIT_FIELD_REF:
13175     case ARRAY_REF:
13176     case ARRAY_RANGE_REF:
13177     case REALPART_EXPR:
13178     case IMAGPART_EXPR:
13179       {
13180         tree obj, offset;
13181         HOST_WIDE_INT bitsize, bitpos, bytepos;
13182         enum machine_mode mode;
13183         int unsignedp, volatilep = 0;
13184
13185         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13186                                    &unsignedp, &volatilep, false);
13187
13188         gcc_assert (obj != loc);
13189
13190         list_ret = loc_list_from_tree (obj,
13191                                        want_address == 2
13192                                        && !bitpos && !offset ? 2 : 1);
13193         /* TODO: We can extract value of the small expression via shifting even
13194            for nonzero bitpos.  */
13195         if (list_ret == 0)
13196           return 0;
13197         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13198           {
13199             expansion_failed (loc, NULL_RTX,
13200                               "bitfield access");
13201             return 0;
13202           }
13203
13204         if (offset != NULL_TREE)
13205           {
13206             /* Variable offset.  */
13207             list_ret1 = loc_list_from_tree (offset, 0);
13208             if (list_ret1 == 0)
13209               return 0;
13210             add_loc_list (&list_ret, list_ret1);
13211             if (!list_ret)
13212               return 0;
13213             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13214           }
13215
13216         bytepos = bitpos / BITS_PER_UNIT;
13217         if (bytepos > 0)
13218           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13219         else if (bytepos < 0)
13220           loc_list_plus_const (list_ret, bytepos);
13221
13222         have_address = 1;
13223         break;
13224       }
13225
13226     case INTEGER_CST:
13227       if ((want_address || !host_integerp (loc, 0))
13228           && (ret = cst_pool_loc_descr (loc)))
13229         have_address = 1;
13230       else if (want_address == 2
13231                && host_integerp (loc, 0)
13232                && (ret = address_of_int_loc_descriptor
13233                            (int_size_in_bytes (TREE_TYPE (loc)),
13234                             tree_low_cst (loc, 0))))
13235         have_address = 1;
13236       else if (host_integerp (loc, 0))
13237         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13238       else
13239         {
13240           expansion_failed (loc, NULL_RTX,
13241                             "Integer operand is not host integer");
13242           return 0;
13243         }
13244       break;
13245
13246     case CONSTRUCTOR:
13247     case REAL_CST:
13248     case STRING_CST:
13249     case COMPLEX_CST:
13250       if ((ret = cst_pool_loc_descr (loc)))
13251         have_address = 1;
13252       else
13253       /* We can construct small constants here using int_loc_descriptor.  */
13254         expansion_failed (loc, NULL_RTX,
13255                           "constructor or constant not in constant pool");
13256       break;
13257
13258     case TRUTH_AND_EXPR:
13259     case TRUTH_ANDIF_EXPR:
13260     case BIT_AND_EXPR:
13261       op = DW_OP_and;
13262       goto do_binop;
13263
13264     case TRUTH_XOR_EXPR:
13265     case BIT_XOR_EXPR:
13266       op = DW_OP_xor;
13267       goto do_binop;
13268
13269     case TRUTH_OR_EXPR:
13270     case TRUTH_ORIF_EXPR:
13271     case BIT_IOR_EXPR:
13272       op = DW_OP_or;
13273       goto do_binop;
13274
13275     case FLOOR_DIV_EXPR:
13276     case CEIL_DIV_EXPR:
13277     case ROUND_DIV_EXPR:
13278     case TRUNC_DIV_EXPR:
13279       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13280         return 0;
13281       op = DW_OP_div;
13282       goto do_binop;
13283
13284     case MINUS_EXPR:
13285       op = DW_OP_minus;
13286       goto do_binop;
13287
13288     case FLOOR_MOD_EXPR:
13289     case CEIL_MOD_EXPR:
13290     case ROUND_MOD_EXPR:
13291     case TRUNC_MOD_EXPR:
13292       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13293         {
13294           op = DW_OP_mod;
13295           goto do_binop;
13296         }
13297       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13298       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13299       if (list_ret == 0 || list_ret1 == 0)
13300         return 0;
13301
13302       add_loc_list (&list_ret, list_ret1);
13303       if (list_ret == 0)
13304         return 0;
13305       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13306       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13307       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13308       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13309       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13310       break;
13311
13312     case MULT_EXPR:
13313       op = DW_OP_mul;
13314       goto do_binop;
13315
13316     case LSHIFT_EXPR:
13317       op = DW_OP_shl;
13318       goto do_binop;
13319
13320     case RSHIFT_EXPR:
13321       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13322       goto do_binop;
13323
13324     case POINTER_PLUS_EXPR:
13325     case PLUS_EXPR:
13326       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13327         {
13328           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13329           if (list_ret == 0)
13330             return 0;
13331
13332           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13333           break;
13334         }
13335
13336       op = DW_OP_plus;
13337       goto do_binop;
13338
13339     case LE_EXPR:
13340       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13341         return 0;
13342
13343       op = DW_OP_le;
13344       goto do_binop;
13345
13346     case GE_EXPR:
13347       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13348         return 0;
13349
13350       op = DW_OP_ge;
13351       goto do_binop;
13352
13353     case LT_EXPR:
13354       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13355         return 0;
13356
13357       op = DW_OP_lt;
13358       goto do_binop;
13359
13360     case GT_EXPR:
13361       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13362         return 0;
13363
13364       op = DW_OP_gt;
13365       goto do_binop;
13366
13367     case EQ_EXPR:
13368       op = DW_OP_eq;
13369       goto do_binop;
13370
13371     case NE_EXPR:
13372       op = DW_OP_ne;
13373       goto do_binop;
13374
13375     do_binop:
13376       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13377       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13378       if (list_ret == 0 || list_ret1 == 0)
13379         return 0;
13380
13381       add_loc_list (&list_ret, list_ret1);
13382       if (list_ret == 0)
13383         return 0;
13384       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13385       break;
13386
13387     case TRUTH_NOT_EXPR:
13388     case BIT_NOT_EXPR:
13389       op = DW_OP_not;
13390       goto do_unop;
13391
13392     case ABS_EXPR:
13393       op = DW_OP_abs;
13394       goto do_unop;
13395
13396     case NEGATE_EXPR:
13397       op = DW_OP_neg;
13398       goto do_unop;
13399
13400     do_unop:
13401       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13402       if (list_ret == 0)
13403         return 0;
13404
13405       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13406       break;
13407
13408     case MIN_EXPR:
13409     case MAX_EXPR:
13410       {
13411         const enum tree_code code =
13412           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13413
13414         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13415                       build2 (code, integer_type_node,
13416                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13417                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13418       }
13419
13420       /* ... fall through ...  */
13421
13422     case COND_EXPR:
13423       {
13424         dw_loc_descr_ref lhs
13425           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13426         dw_loc_list_ref rhs
13427           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13428         dw_loc_descr_ref bra_node, jump_node, tmp;
13429
13430         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13431         if (list_ret == 0 || lhs == 0 || rhs == 0)
13432           return 0;
13433
13434         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13435         add_loc_descr_to_each (list_ret, bra_node);
13436
13437         add_loc_list (&list_ret, rhs);
13438         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13439         add_loc_descr_to_each (list_ret, jump_node);
13440
13441         add_loc_descr_to_each (list_ret, lhs);
13442         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13443         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13444
13445         /* ??? Need a node to point the skip at.  Use a nop.  */
13446         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13447         add_loc_descr_to_each (list_ret, tmp);
13448         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13449         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13450       }
13451       break;
13452
13453     case FIX_TRUNC_EXPR:
13454       return 0;
13455
13456     default:
13457       /* Leave front-end specific codes as simply unknown.  This comes
13458          up, for instance, with the C STMT_EXPR.  */
13459       if ((unsigned int) TREE_CODE (loc)
13460           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13461         {
13462           expansion_failed (loc, NULL_RTX,
13463                             "language specific tree node");
13464           return 0;
13465         }
13466
13467 #ifdef ENABLE_CHECKING
13468       /* Otherwise this is a generic code; we should just lists all of
13469          these explicitly.  We forgot one.  */
13470       gcc_unreachable ();
13471 #else
13472       /* In a release build, we want to degrade gracefully: better to
13473          generate incomplete debugging information than to crash.  */
13474       return NULL;
13475 #endif
13476     }
13477
13478   if (!ret && !list_ret)
13479     return 0;
13480
13481   if (want_address == 2 && !have_address
13482       && (dwarf_version >= 4 || !dwarf_strict))
13483     {
13484       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13485         {
13486           expansion_failed (loc, NULL_RTX,
13487                             "DWARF address size mismatch");
13488           return 0;
13489         }
13490       if (ret)
13491         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13492       else
13493         add_loc_descr_to_each (list_ret,
13494                                new_loc_descr (DW_OP_stack_value, 0, 0));
13495       have_address = 1;
13496     }
13497   /* Show if we can't fill the request for an address.  */
13498   if (want_address && !have_address)
13499     {
13500       expansion_failed (loc, NULL_RTX,
13501                         "Want address and only have value");
13502       return 0;
13503     }
13504
13505   gcc_assert (!ret || !list_ret);
13506
13507   /* If we've got an address and don't want one, dereference.  */
13508   if (!want_address && have_address)
13509     {
13510       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13511
13512       if (size > DWARF2_ADDR_SIZE || size == -1)
13513         {
13514           expansion_failed (loc, NULL_RTX,
13515                             "DWARF address size mismatch");
13516           return 0;
13517         }
13518       else if (size == DWARF2_ADDR_SIZE)
13519         op = DW_OP_deref;
13520       else
13521         op = DW_OP_deref_size;
13522
13523       if (ret)
13524         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13525       else
13526         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13527     }
13528   if (ret)
13529     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13530
13531   return list_ret;
13532 }
13533
13534 /* Same as above but return only single location expression.  */
13535 static dw_loc_descr_ref
13536 loc_descriptor_from_tree (tree loc, int want_address)
13537 {
13538   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13539   if (!ret)
13540     return NULL;
13541   if (ret->dw_loc_next)
13542     {
13543       expansion_failed (loc, NULL_RTX,
13544                         "Location list where only loc descriptor needed");
13545       return NULL;
13546     }
13547   return ret->expr;
13548 }
13549
13550 /* Given a value, round it up to the lowest multiple of `boundary'
13551    which is not less than the value itself.  */
13552
13553 static inline HOST_WIDE_INT
13554 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13555 {
13556   return (((value + boundary - 1) / boundary) * boundary);
13557 }
13558
13559 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13560    pointer to the declared type for the relevant field variable, or return
13561    `integer_type_node' if the given node turns out to be an
13562    ERROR_MARK node.  */
13563
13564 static inline tree
13565 field_type (const_tree decl)
13566 {
13567   tree type;
13568
13569   if (TREE_CODE (decl) == ERROR_MARK)
13570     return integer_type_node;
13571
13572   type = DECL_BIT_FIELD_TYPE (decl);
13573   if (type == NULL_TREE)
13574     type = TREE_TYPE (decl);
13575
13576   return type;
13577 }
13578
13579 /* Given a pointer to a tree node, return the alignment in bits for
13580    it, or else return BITS_PER_WORD if the node actually turns out to
13581    be an ERROR_MARK node.  */
13582
13583 static inline unsigned
13584 simple_type_align_in_bits (const_tree type)
13585 {
13586   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13587 }
13588
13589 static inline unsigned
13590 simple_decl_align_in_bits (const_tree decl)
13591 {
13592   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13593 }
13594
13595 /* Return the result of rounding T up to ALIGN.  */
13596
13597 static inline double_int
13598 round_up_to_align (double_int t, unsigned int align)
13599 {
13600   double_int alignd = uhwi_to_double_int (align);
13601   t = double_int_add (t, alignd);
13602   t = double_int_add (t, double_int_minus_one);
13603   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
13604   t = double_int_mul (t, alignd);
13605   return t;
13606 }
13607
13608 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13609    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13610    or return 0 if we are unable to determine what that offset is, either
13611    because the argument turns out to be a pointer to an ERROR_MARK node, or
13612    because the offset is actually variable.  (We can't handle the latter case
13613    just yet).  */
13614
13615 static HOST_WIDE_INT
13616 field_byte_offset (const_tree decl)
13617 {
13618   double_int object_offset_in_bits;
13619   double_int object_offset_in_bytes;
13620   double_int bitpos_int;
13621
13622   if (TREE_CODE (decl) == ERROR_MARK)
13623     return 0;
13624
13625   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13626
13627   /* We cannot yet cope with fields whose positions are variable, so
13628      for now, when we see such things, we simply return 0.  Someday, we may
13629      be able to handle such cases, but it will be damn difficult.  */
13630   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
13631     return 0;
13632
13633   bitpos_int = tree_to_double_int (bit_position (decl));
13634
13635 #ifdef PCC_BITFIELD_TYPE_MATTERS
13636   if (PCC_BITFIELD_TYPE_MATTERS)
13637     {
13638       tree type;
13639       tree field_size_tree;
13640       double_int deepest_bitpos;
13641       double_int field_size_in_bits;
13642       unsigned int type_align_in_bits;
13643       unsigned int decl_align_in_bits;
13644       double_int type_size_in_bits;
13645
13646       type = field_type (decl);
13647       type_size_in_bits = double_int_type_size_in_bits (type);
13648       type_align_in_bits = simple_type_align_in_bits (type);
13649
13650       field_size_tree = DECL_SIZE (decl);
13651
13652       /* The size could be unspecified if there was an error, or for
13653          a flexible array member.  */
13654       if (!field_size_tree)
13655         field_size_tree = bitsize_zero_node;
13656
13657       /* If the size of the field is not constant, use the type size.  */
13658       if (TREE_CODE (field_size_tree) == INTEGER_CST)
13659         field_size_in_bits = tree_to_double_int (field_size_tree);
13660       else
13661         field_size_in_bits = type_size_in_bits;
13662
13663       decl_align_in_bits = simple_decl_align_in_bits (decl);
13664
13665       /* The GCC front-end doesn't make any attempt to keep track of the
13666          starting bit offset (relative to the start of the containing
13667          structure type) of the hypothetical "containing object" for a
13668          bit-field.  Thus, when computing the byte offset value for the
13669          start of the "containing object" of a bit-field, we must deduce
13670          this information on our own. This can be rather tricky to do in
13671          some cases.  For example, handling the following structure type
13672          definition when compiling for an i386/i486 target (which only
13673          aligns long long's to 32-bit boundaries) can be very tricky:
13674
13675          struct S { int field1; long long field2:31; };
13676
13677          Fortunately, there is a simple rule-of-thumb which can be used
13678          in such cases.  When compiling for an i386/i486, GCC will
13679          allocate 8 bytes for the structure shown above.  It decides to
13680          do this based upon one simple rule for bit-field allocation.
13681          GCC allocates each "containing object" for each bit-field at
13682          the first (i.e. lowest addressed) legitimate alignment boundary
13683          (based upon the required minimum alignment for the declared
13684          type of the field) which it can possibly use, subject to the
13685          condition that there is still enough available space remaining
13686          in the containing object (when allocated at the selected point)
13687          to fully accommodate all of the bits of the bit-field itself.
13688
13689          This simple rule makes it obvious why GCC allocates 8 bytes for
13690          each object of the structure type shown above.  When looking
13691          for a place to allocate the "containing object" for `field2',
13692          the compiler simply tries to allocate a 64-bit "containing
13693          object" at each successive 32-bit boundary (starting at zero)
13694          until it finds a place to allocate that 64- bit field such that
13695          at least 31 contiguous (and previously unallocated) bits remain
13696          within that selected 64 bit field.  (As it turns out, for the
13697          example above, the compiler finds it is OK to allocate the
13698          "containing object" 64-bit field at bit-offset zero within the
13699          structure type.)
13700
13701          Here we attempt to work backwards from the limited set of facts
13702          we're given, and we try to deduce from those facts, where GCC
13703          must have believed that the containing object started (within
13704          the structure type). The value we deduce is then used (by the
13705          callers of this routine) to generate DW_AT_location and
13706          DW_AT_bit_offset attributes for fields (both bit-fields and, in
13707          the case of DW_AT_location, regular fields as well).  */
13708
13709       /* Figure out the bit-distance from the start of the structure to
13710          the "deepest" bit of the bit-field.  */
13711       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
13712
13713       /* This is the tricky part.  Use some fancy footwork to deduce
13714          where the lowest addressed bit of the containing object must
13715          be.  */
13716       object_offset_in_bits
13717         = double_int_sub (deepest_bitpos, type_size_in_bits);
13718
13719       /* Round up to type_align by default.  This works best for
13720          bitfields.  */
13721       object_offset_in_bits
13722         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13723
13724       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
13725         {
13726           object_offset_in_bits
13727             = double_int_sub (deepest_bitpos, type_size_in_bits);
13728
13729           /* Round up to decl_align instead.  */
13730           object_offset_in_bits
13731             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13732         }
13733     }
13734   else
13735 #endif /* PCC_BITFIELD_TYPE_MATTERS */
13736     object_offset_in_bits = bitpos_int;
13737
13738   object_offset_in_bytes
13739     = double_int_div (object_offset_in_bits,
13740                       uhwi_to_double_int (BITS_PER_UNIT), true,
13741                       TRUNC_DIV_EXPR);
13742   return double_int_to_shwi (object_offset_in_bytes);
13743 }
13744 \f
13745 /* The following routines define various Dwarf attributes and any data
13746    associated with them.  */
13747
13748 /* Add a location description attribute value to a DIE.
13749
13750    This emits location attributes suitable for whole variables and
13751    whole parameters.  Note that the location attributes for struct fields are
13752    generated by the routine `data_member_location_attribute' below.  */
13753
13754 static inline void
13755 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
13756                              dw_loc_list_ref descr)
13757 {
13758   if (descr == 0)
13759     return;
13760   if (single_element_loc_list_p (descr))
13761     add_AT_loc (die, attr_kind, descr->expr);
13762   else
13763     add_AT_loc_list (die, attr_kind, descr);
13764 }
13765
13766 /* Add DW_AT_accessibility attribute to DIE if needed.  */
13767
13768 static void
13769 add_accessibility_attribute (dw_die_ref die, tree decl)
13770 {
13771   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
13772      children, otherwise the default is DW_ACCESS_public.  In DWARF2
13773      the default has always been DW_ACCESS_public.  */
13774   if (TREE_PROTECTED (decl))
13775     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13776   else if (TREE_PRIVATE (decl))
13777     {
13778       if (dwarf_version == 2
13779           || die->die_parent == NULL
13780           || die->die_parent->die_tag != DW_TAG_class_type)
13781         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
13782     }
13783   else if (dwarf_version > 2
13784            && die->die_parent
13785            && die->die_parent->die_tag == DW_TAG_class_type)
13786     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13787 }
13788
13789 /* Attach the specialized form of location attribute used for data members of
13790    struct and union types.  In the special case of a FIELD_DECL node which
13791    represents a bit-field, the "offset" part of this special location
13792    descriptor must indicate the distance in bytes from the lowest-addressed
13793    byte of the containing struct or union type to the lowest-addressed byte of
13794    the "containing object" for the bit-field.  (See the `field_byte_offset'
13795    function above).
13796
13797    For any given bit-field, the "containing object" is a hypothetical object
13798    (of some integral or enum type) within which the given bit-field lives.  The
13799    type of this hypothetical "containing object" is always the same as the
13800    declared type of the individual bit-field itself (for GCC anyway... the
13801    DWARF spec doesn't actually mandate this).  Note that it is the size (in
13802    bytes) of the hypothetical "containing object" which will be given in the
13803    DW_AT_byte_size attribute for this bit-field.  (See the
13804    `byte_size_attribute' function below.)  It is also used when calculating the
13805    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
13806    function below.)  */
13807
13808 static void
13809 add_data_member_location_attribute (dw_die_ref die, tree decl)
13810 {
13811   HOST_WIDE_INT offset;
13812   dw_loc_descr_ref loc_descr = 0;
13813
13814   if (TREE_CODE (decl) == TREE_BINFO)
13815     {
13816       /* We're working on the TAG_inheritance for a base class.  */
13817       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
13818         {
13819           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
13820              aren't at a fixed offset from all (sub)objects of the same
13821              type.  We need to extract the appropriate offset from our
13822              vtable.  The following dwarf expression means
13823
13824                BaseAddr = ObAddr + *((*ObAddr) - Offset)
13825
13826              This is specific to the V3 ABI, of course.  */
13827
13828           dw_loc_descr_ref tmp;
13829
13830           /* Make a copy of the object address.  */
13831           tmp = new_loc_descr (DW_OP_dup, 0, 0);
13832           add_loc_descr (&loc_descr, tmp);
13833
13834           /* Extract the vtable address.  */
13835           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13836           add_loc_descr (&loc_descr, tmp);
13837
13838           /* Calculate the address of the offset.  */
13839           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
13840           gcc_assert (offset < 0);
13841
13842           tmp = int_loc_descriptor (-offset);
13843           add_loc_descr (&loc_descr, tmp);
13844           tmp = new_loc_descr (DW_OP_minus, 0, 0);
13845           add_loc_descr (&loc_descr, tmp);
13846
13847           /* Extract the offset.  */
13848           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13849           add_loc_descr (&loc_descr, tmp);
13850
13851           /* Add it to the object address.  */
13852           tmp = new_loc_descr (DW_OP_plus, 0, 0);
13853           add_loc_descr (&loc_descr, tmp);
13854         }
13855       else
13856         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
13857     }
13858   else
13859     offset = field_byte_offset (decl);
13860
13861   if (! loc_descr)
13862     {
13863       if (dwarf_version > 2)
13864         {
13865           /* Don't need to output a location expression, just the constant. */
13866           if (offset < 0)
13867             add_AT_int (die, DW_AT_data_member_location, offset);
13868           else
13869             add_AT_unsigned (die, DW_AT_data_member_location, offset);
13870           return;
13871         }
13872       else
13873         {
13874           enum dwarf_location_atom op;
13875
13876           /* The DWARF2 standard says that we should assume that the structure
13877              address is already on the stack, so we can specify a structure
13878              field address by using DW_OP_plus_uconst.  */
13879           op = DW_OP_plus_uconst;
13880           loc_descr = new_loc_descr (op, offset, 0);
13881         }
13882     }
13883
13884   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
13885 }
13886
13887 /* Writes integer values to dw_vec_const array.  */
13888
13889 static void
13890 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
13891 {
13892   while (size != 0)
13893     {
13894       *dest++ = val & 0xff;
13895       val >>= 8;
13896       --size;
13897     }
13898 }
13899
13900 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
13901
13902 static HOST_WIDE_INT
13903 extract_int (const unsigned char *src, unsigned int size)
13904 {
13905   HOST_WIDE_INT val = 0;
13906
13907   src += size;
13908   while (size != 0)
13909     {
13910       val <<= 8;
13911       val |= *--src & 0xff;
13912       --size;
13913     }
13914   return val;
13915 }
13916
13917 /* Writes double_int values to dw_vec_const array.  */
13918
13919 static void
13920 insert_double (double_int val, unsigned char *dest)
13921 {
13922   unsigned char *p0 = dest;
13923   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
13924
13925   if (WORDS_BIG_ENDIAN)
13926     {
13927       p0 = p1;
13928       p1 = dest;
13929     }
13930
13931   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
13932   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
13933 }
13934
13935 /* Writes floating point values to dw_vec_const array.  */
13936
13937 static void
13938 insert_float (const_rtx rtl, unsigned char *array)
13939 {
13940   REAL_VALUE_TYPE rv;
13941   long val[4];
13942   int i;
13943
13944   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
13945   real_to_target (val, &rv, GET_MODE (rtl));
13946
13947   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
13948   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
13949     {
13950       insert_int (val[i], 4, array);
13951       array += 4;
13952     }
13953 }
13954
13955 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
13956    does not have a "location" either in memory or in a register.  These
13957    things can arise in GNU C when a constant is passed as an actual parameter
13958    to an inlined function.  They can also arise in C++ where declared
13959    constants do not necessarily get memory "homes".  */
13960
13961 static bool
13962 add_const_value_attribute (dw_die_ref die, rtx rtl)
13963 {
13964   switch (GET_CODE (rtl))
13965     {
13966     case CONST_INT:
13967       {
13968         HOST_WIDE_INT val = INTVAL (rtl);
13969
13970         if (val < 0)
13971           add_AT_int (die, DW_AT_const_value, val);
13972         else
13973           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
13974       }
13975       return true;
13976
13977     case CONST_DOUBLE:
13978       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
13979          floating-point constant.  A CONST_DOUBLE is used whenever the
13980          constant requires more than one word in order to be adequately
13981          represented.  */
13982       {
13983         enum machine_mode mode = GET_MODE (rtl);
13984
13985         if (SCALAR_FLOAT_MODE_P (mode))
13986           {
13987             unsigned int length = GET_MODE_SIZE (mode);
13988             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
13989
13990             insert_float (rtl, array);
13991             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
13992           }
13993         else
13994           add_AT_double (die, DW_AT_const_value,
13995                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
13996       }
13997       return true;
13998
13999     case CONST_VECTOR:
14000       {
14001         enum machine_mode mode = GET_MODE (rtl);
14002         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14003         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14004         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14005           (length * elt_size);
14006         unsigned int i;
14007         unsigned char *p;
14008
14009         switch (GET_MODE_CLASS (mode))
14010           {
14011           case MODE_VECTOR_INT:
14012             for (i = 0, p = array; i < length; i++, p += elt_size)
14013               {
14014                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14015                 double_int val = rtx_to_double_int (elt);
14016
14017                 if (elt_size <= sizeof (HOST_WIDE_INT))
14018                   insert_int (double_int_to_shwi (val), elt_size, p);
14019                 else
14020                   {
14021                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14022                     insert_double (val, p);
14023                   }
14024               }
14025             break;
14026
14027           case MODE_VECTOR_FLOAT:
14028             for (i = 0, p = array; i < length; i++, p += elt_size)
14029               {
14030                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14031                 insert_float (elt, p);
14032               }
14033             break;
14034
14035           default:
14036             gcc_unreachable ();
14037           }
14038
14039         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14040       }
14041       return true;
14042
14043     case CONST_STRING:
14044       if (dwarf_version >= 4 || !dwarf_strict)
14045         {
14046           dw_loc_descr_ref loc_result;
14047           resolve_one_addr (&rtl, NULL);
14048         rtl_addr:
14049           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14050           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14051           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14052           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14053           add_AT_loc (die, DW_AT_location, loc_result);
14054           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14055           return true;
14056         }
14057       return false;
14058
14059     case CONST:
14060       if (CONSTANT_P (XEXP (rtl, 0)))
14061         return add_const_value_attribute (die, XEXP (rtl, 0));
14062       /* FALLTHROUGH */
14063     case SYMBOL_REF:
14064       if (!const_ok_for_output (rtl))
14065         return false;
14066     case LABEL_REF:
14067       if (dwarf_version >= 4 || !dwarf_strict)
14068         goto rtl_addr;
14069       return false;
14070
14071     case PLUS:
14072       /* In cases where an inlined instance of an inline function is passed
14073          the address of an `auto' variable (which is local to the caller) we
14074          can get a situation where the DECL_RTL of the artificial local
14075          variable (for the inlining) which acts as a stand-in for the
14076          corresponding formal parameter (of the inline function) will look
14077          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14078          exactly a compile-time constant expression, but it isn't the address
14079          of the (artificial) local variable either.  Rather, it represents the
14080          *value* which the artificial local variable always has during its
14081          lifetime.  We currently have no way to represent such quasi-constant
14082          values in Dwarf, so for now we just punt and generate nothing.  */
14083       return false;
14084
14085     case HIGH:
14086     case CONST_FIXED:
14087       return false;
14088
14089     case MEM:
14090       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14091           && MEM_READONLY_P (rtl)
14092           && GET_MODE (rtl) == BLKmode)
14093         {
14094           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14095           return true;
14096         }
14097       return false;
14098
14099     default:
14100       /* No other kinds of rtx should be possible here.  */
14101       gcc_unreachable ();
14102     }
14103   return false;
14104 }
14105
14106 /* Determine whether the evaluation of EXPR references any variables
14107    or functions which aren't otherwise used (and therefore may not be
14108    output).  */
14109 static tree
14110 reference_to_unused (tree * tp, int * walk_subtrees,
14111                      void * data ATTRIBUTE_UNUSED)
14112 {
14113   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14114     *walk_subtrees = 0;
14115
14116   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14117       && ! TREE_ASM_WRITTEN (*tp))
14118     return *tp;
14119   /* ???  The C++ FE emits debug information for using decls, so
14120      putting gcc_unreachable here falls over.  See PR31899.  For now
14121      be conservative.  */
14122   else if (!cgraph_global_info_ready
14123            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14124     return *tp;
14125   else if (TREE_CODE (*tp) == VAR_DECL)
14126     {
14127       struct varpool_node *node = varpool_get_node (*tp);
14128       if (!node || !node->analyzed)
14129         return *tp;
14130     }
14131   else if (TREE_CODE (*tp) == FUNCTION_DECL
14132            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14133     {
14134       /* The call graph machinery must have finished analyzing,
14135          optimizing and gimplifying the CU by now.
14136          So if *TP has no call graph node associated
14137          to it, it means *TP will not be emitted.  */
14138       if (!cgraph_get_node (*tp))
14139         return *tp;
14140     }
14141   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14142     return *tp;
14143
14144   return NULL_TREE;
14145 }
14146
14147 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14148    for use in a later add_const_value_attribute call.  */
14149
14150 static rtx
14151 rtl_for_decl_init (tree init, tree type)
14152 {
14153   rtx rtl = NULL_RTX;
14154
14155   STRIP_NOPS (init);
14156
14157   /* If a variable is initialized with a string constant without embedded
14158      zeros, build CONST_STRING.  */
14159   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14160     {
14161       tree enttype = TREE_TYPE (type);
14162       tree domain = TYPE_DOMAIN (type);
14163       enum machine_mode mode = TYPE_MODE (enttype);
14164
14165       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14166           && domain
14167           && integer_zerop (TYPE_MIN_VALUE (domain))
14168           && compare_tree_int (TYPE_MAX_VALUE (domain),
14169                                TREE_STRING_LENGTH (init) - 1) == 0
14170           && ((size_t) TREE_STRING_LENGTH (init)
14171               == strlen (TREE_STRING_POINTER (init)) + 1))
14172         {
14173           rtl = gen_rtx_CONST_STRING (VOIDmode,
14174                                       ggc_strdup (TREE_STRING_POINTER (init)));
14175           rtl = gen_rtx_MEM (BLKmode, rtl);
14176           MEM_READONLY_P (rtl) = 1;
14177         }
14178     }
14179   /* Other aggregates, and complex values, could be represented using
14180      CONCAT: FIXME!  */
14181   else if (AGGREGATE_TYPE_P (type)
14182            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14183                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14184            || TREE_CODE (type) == COMPLEX_TYPE)
14185     ;
14186   /* Vectors only work if their mode is supported by the target.
14187      FIXME: generic vectors ought to work too.  */
14188   else if (TREE_CODE (type) == VECTOR_TYPE
14189            && !VECTOR_MODE_P (TYPE_MODE (type)))
14190     ;
14191   /* If the initializer is something that we know will expand into an
14192      immediate RTL constant, expand it now.  We must be careful not to
14193      reference variables which won't be output.  */
14194   else if (initializer_constant_valid_p (init, type)
14195            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14196     {
14197       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14198          possible.  */
14199       if (TREE_CODE (type) == VECTOR_TYPE)
14200         switch (TREE_CODE (init))
14201           {
14202           case VECTOR_CST:
14203             break;
14204           case CONSTRUCTOR:
14205             if (TREE_CONSTANT (init))
14206               {
14207                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14208                 bool constant_p = true;
14209                 tree value;
14210                 unsigned HOST_WIDE_INT ix;
14211
14212                 /* Even when ctor is constant, it might contain non-*_CST
14213                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14214                    belong into VECTOR_CST nodes.  */
14215                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14216                   if (!CONSTANT_CLASS_P (value))
14217                     {
14218                       constant_p = false;
14219                       break;
14220                     }
14221
14222                 if (constant_p)
14223                   {
14224                     init = build_vector_from_ctor (type, elts);
14225                     break;
14226                   }
14227               }
14228             /* FALLTHRU */
14229
14230           default:
14231             return NULL;
14232           }
14233
14234       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14235
14236       /* If expand_expr returns a MEM, it wasn't immediate.  */
14237       gcc_assert (!rtl || !MEM_P (rtl));
14238     }
14239
14240   return rtl;
14241 }
14242
14243 /* Generate RTL for the variable DECL to represent its location.  */
14244
14245 static rtx
14246 rtl_for_decl_location (tree decl)
14247 {
14248   rtx rtl;
14249
14250   /* Here we have to decide where we are going to say the parameter "lives"
14251      (as far as the debugger is concerned).  We only have a couple of
14252      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14253
14254      DECL_RTL normally indicates where the parameter lives during most of the
14255      activation of the function.  If optimization is enabled however, this
14256      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14257      that the parameter doesn't really live anywhere (as far as the code
14258      generation parts of GCC are concerned) during most of the function's
14259      activation.  That will happen (for example) if the parameter is never
14260      referenced within the function.
14261
14262      We could just generate a location descriptor here for all non-NULL
14263      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14264      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14265      where DECL_RTL is NULL or is a pseudo-reg.
14266
14267      Note however that we can only get away with using DECL_INCOMING_RTL as
14268      a backup substitute for DECL_RTL in certain limited cases.  In cases
14269      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14270      we can be sure that the parameter was passed using the same type as it is
14271      declared to have within the function, and that its DECL_INCOMING_RTL
14272      points us to a place where a value of that type is passed.
14273
14274      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14275      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14276      because in these cases DECL_INCOMING_RTL points us to a value of some
14277      type which is *different* from the type of the parameter itself.  Thus,
14278      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14279      such cases, the debugger would end up (for example) trying to fetch a
14280      `float' from a place which actually contains the first part of a
14281      `double'.  That would lead to really incorrect and confusing
14282      output at debug-time.
14283
14284      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14285      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14286      are a couple of exceptions however.  On little-endian machines we can
14287      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14288      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14289      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14290      when (on a little-endian machine) a non-prototyped function has a
14291      parameter declared to be of type `short' or `char'.  In such cases,
14292      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14293      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14294      passed `int' value.  If the debugger then uses that address to fetch
14295      a `short' or a `char' (on a little-endian machine) the result will be
14296      the correct data, so we allow for such exceptional cases below.
14297
14298      Note that our goal here is to describe the place where the given formal
14299      parameter lives during most of the function's activation (i.e. between the
14300      end of the prologue and the start of the epilogue).  We'll do that as best
14301      as we can. Note however that if the given formal parameter is modified
14302      sometime during the execution of the function, then a stack backtrace (at
14303      debug-time) will show the function as having been called with the *new*
14304      value rather than the value which was originally passed in.  This happens
14305      rarely enough that it is not a major problem, but it *is* a problem, and
14306      I'd like to fix it.
14307
14308      A future version of dwarf2out.c may generate two additional attributes for
14309      any given DW_TAG_formal_parameter DIE which will describe the "passed
14310      type" and the "passed location" for the given formal parameter in addition
14311      to the attributes we now generate to indicate the "declared type" and the
14312      "active location" for each parameter.  This additional set of attributes
14313      could be used by debuggers for stack backtraces. Separately, note that
14314      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14315      This happens (for example) for inlined-instances of inline function formal
14316      parameters which are never referenced.  This really shouldn't be
14317      happening.  All PARM_DECL nodes should get valid non-NULL
14318      DECL_INCOMING_RTL values.  FIXME.  */
14319
14320   /* Use DECL_RTL as the "location" unless we find something better.  */
14321   rtl = DECL_RTL_IF_SET (decl);
14322
14323   /* When generating abstract instances, ignore everything except
14324      constants, symbols living in memory, and symbols living in
14325      fixed registers.  */
14326   if (! reload_completed)
14327     {
14328       if (rtl
14329           && (CONSTANT_P (rtl)
14330               || (MEM_P (rtl)
14331                   && CONSTANT_P (XEXP (rtl, 0)))
14332               || (REG_P (rtl)
14333                   && TREE_CODE (decl) == VAR_DECL
14334                   && TREE_STATIC (decl))))
14335         {
14336           rtl = targetm.delegitimize_address (rtl);
14337           return rtl;
14338         }
14339       rtl = NULL_RTX;
14340     }
14341   else if (TREE_CODE (decl) == PARM_DECL)
14342     {
14343       if (rtl == NULL_RTX
14344           || is_pseudo_reg (rtl)
14345           || (MEM_P (rtl)
14346               && is_pseudo_reg (XEXP (rtl, 0))
14347               && DECL_INCOMING_RTL (decl)
14348               && MEM_P (DECL_INCOMING_RTL (decl))
14349               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14350         {
14351           tree declared_type = TREE_TYPE (decl);
14352           tree passed_type = DECL_ARG_TYPE (decl);
14353           enum machine_mode dmode = TYPE_MODE (declared_type);
14354           enum machine_mode pmode = TYPE_MODE (passed_type);
14355
14356           /* This decl represents a formal parameter which was optimized out.
14357              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14358              all cases where (rtl == NULL_RTX) just below.  */
14359           if (dmode == pmode)
14360             rtl = DECL_INCOMING_RTL (decl);
14361           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14362                    && SCALAR_INT_MODE_P (dmode)
14363                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14364                    && DECL_INCOMING_RTL (decl))
14365             {
14366               rtx inc = DECL_INCOMING_RTL (decl);
14367               if (REG_P (inc))
14368                 rtl = inc;
14369               else if (MEM_P (inc))
14370                 {
14371                   if (BYTES_BIG_ENDIAN)
14372                     rtl = adjust_address_nv (inc, dmode,
14373                                              GET_MODE_SIZE (pmode)
14374                                              - GET_MODE_SIZE (dmode));
14375                   else
14376                     rtl = inc;
14377                 }
14378             }
14379         }
14380
14381       /* If the parm was passed in registers, but lives on the stack, then
14382          make a big endian correction if the mode of the type of the
14383          parameter is not the same as the mode of the rtl.  */
14384       /* ??? This is the same series of checks that are made in dbxout.c before
14385          we reach the big endian correction code there.  It isn't clear if all
14386          of these checks are necessary here, but keeping them all is the safe
14387          thing to do.  */
14388       else if (MEM_P (rtl)
14389                && XEXP (rtl, 0) != const0_rtx
14390                && ! CONSTANT_P (XEXP (rtl, 0))
14391                /* Not passed in memory.  */
14392                && !MEM_P (DECL_INCOMING_RTL (decl))
14393                /* Not passed by invisible reference.  */
14394                && (!REG_P (XEXP (rtl, 0))
14395                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14396                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14397 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14398                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14399 #endif
14400                      )
14401                /* Big endian correction check.  */
14402                && BYTES_BIG_ENDIAN
14403                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14404                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14405                    < UNITS_PER_WORD))
14406         {
14407           enum machine_mode addr_mode = get_address_mode (rtl);
14408           int offset = (UNITS_PER_WORD
14409                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14410
14411           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14412                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
14413         }
14414     }
14415   else if (TREE_CODE (decl) == VAR_DECL
14416            && rtl
14417            && MEM_P (rtl)
14418            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14419            && BYTES_BIG_ENDIAN)
14420     {
14421       enum machine_mode addr_mode = get_address_mode (rtl);
14422       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14423       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14424
14425       /* If a variable is declared "register" yet is smaller than
14426          a register, then if we store the variable to memory, it
14427          looks like we're storing a register-sized value, when in
14428          fact we are not.  We need to adjust the offset of the
14429          storage location to reflect the actual value's bytes,
14430          else gdb will not be able to display it.  */
14431       if (rsize > dsize)
14432         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14433                            plus_constant (addr_mode, XEXP (rtl, 0),
14434                                           rsize - dsize));
14435     }
14436
14437   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14438      and will have been substituted directly into all expressions that use it.
14439      C does not have such a concept, but C++ and other languages do.  */
14440   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14441     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14442
14443   if (rtl)
14444     rtl = targetm.delegitimize_address (rtl);
14445
14446   /* If we don't look past the constant pool, we risk emitting a
14447      reference to a constant pool entry that isn't referenced from
14448      code, and thus is not emitted.  */
14449   if (rtl)
14450     rtl = avoid_constant_pool_reference (rtl);
14451
14452   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14453      in the current CU, resolve_addr will remove the expression referencing
14454      it.  */
14455   if (rtl == NULL_RTX
14456       && TREE_CODE (decl) == VAR_DECL
14457       && !DECL_EXTERNAL (decl)
14458       && TREE_STATIC (decl)
14459       && DECL_NAME (decl)
14460       && !DECL_HARD_REGISTER (decl)
14461       && DECL_MODE (decl) != VOIDmode)
14462     {
14463       rtl = make_decl_rtl_for_debug (decl);
14464       if (!MEM_P (rtl)
14465           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14466           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14467         rtl = NULL_RTX;
14468     }
14469
14470   return rtl;
14471 }
14472
14473 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14474    returned.  If so, the decl for the COMMON block is returned, and the
14475    value is the offset into the common block for the symbol.  */
14476
14477 static tree
14478 fortran_common (tree decl, HOST_WIDE_INT *value)
14479 {
14480   tree val_expr, cvar;
14481   enum machine_mode mode;
14482   HOST_WIDE_INT bitsize, bitpos;
14483   tree offset;
14484   int unsignedp, volatilep = 0;
14485
14486   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14487      it does not have a value (the offset into the common area), or if it
14488      is thread local (as opposed to global) then it isn't common, and shouldn't
14489      be handled as such.  */
14490   if (TREE_CODE (decl) != VAR_DECL
14491       || !TREE_STATIC (decl)
14492       || !DECL_HAS_VALUE_EXPR_P (decl)
14493       || !is_fortran ())
14494     return NULL_TREE;
14495
14496   val_expr = DECL_VALUE_EXPR (decl);
14497   if (TREE_CODE (val_expr) != COMPONENT_REF)
14498     return NULL_TREE;
14499
14500   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14501                               &mode, &unsignedp, &volatilep, true);
14502
14503   if (cvar == NULL_TREE
14504       || TREE_CODE (cvar) != VAR_DECL
14505       || DECL_ARTIFICIAL (cvar)
14506       || !TREE_PUBLIC (cvar))
14507     return NULL_TREE;
14508
14509   *value = 0;
14510   if (offset != NULL)
14511     {
14512       if (!host_integerp (offset, 0))
14513         return NULL_TREE;
14514       *value = tree_low_cst (offset, 0);
14515     }
14516   if (bitpos != 0)
14517     *value += bitpos / BITS_PER_UNIT;
14518
14519   return cvar;
14520 }
14521
14522 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14523    data attribute for a variable or a parameter.  We generate the
14524    DW_AT_const_value attribute only in those cases where the given variable
14525    or parameter does not have a true "location" either in memory or in a
14526    register.  This can happen (for example) when a constant is passed as an
14527    actual argument in a call to an inline function.  (It's possible that
14528    these things can crop up in other ways also.)  Note that one type of
14529    constant value which can be passed into an inlined function is a constant
14530    pointer.  This can happen for example if an actual argument in an inlined
14531    function call evaluates to a compile-time constant address.
14532
14533    CACHE_P is true if it is worth caching the location list for DECL,
14534    so that future calls can reuse it rather than regenerate it from scratch.
14535    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14536    since we will need to refer to them each time the function is inlined.  */
14537
14538 static bool
14539 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14540                                        enum dwarf_attribute attr)
14541 {
14542   rtx rtl;
14543   dw_loc_list_ref list;
14544   var_loc_list *loc_list;
14545   cached_dw_loc_list *cache;
14546   void **slot;
14547
14548   if (TREE_CODE (decl) == ERROR_MARK)
14549     return false;
14550
14551   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14552               || TREE_CODE (decl) == RESULT_DECL);
14553
14554   /* Try to get some constant RTL for this decl, and use that as the value of
14555      the location.  */
14556
14557   rtl = rtl_for_decl_location (decl);
14558   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14559       && add_const_value_attribute (die, rtl))
14560     return true;
14561
14562   /* See if we have single element location list that is equivalent to
14563      a constant value.  That way we are better to use add_const_value_attribute
14564      rather than expanding constant value equivalent.  */
14565   loc_list = lookup_decl_loc (decl);
14566   if (loc_list
14567       && loc_list->first
14568       && loc_list->first->next == NULL
14569       && NOTE_P (loc_list->first->loc)
14570       && NOTE_VAR_LOCATION (loc_list->first->loc)
14571       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14572     {
14573       struct var_loc_node *node;
14574
14575       node = loc_list->first;
14576       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14577       if (GET_CODE (rtl) == EXPR_LIST)
14578         rtl = XEXP (rtl, 0);
14579       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14580           && add_const_value_attribute (die, rtl))
14581          return true;
14582     }
14583   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14584      list several times.  See if we've already cached the contents.  */
14585   list = NULL;
14586   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14587     cache_p = false;
14588   if (cache_p)
14589     {
14590       cache = (cached_dw_loc_list *)
14591         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14592       if (cache)
14593         list = cache->loc_list;
14594     }
14595   if (list == NULL)
14596     {
14597       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14598       /* It is usually worth caching this result if the decl is from
14599          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
14600       if (cache_p && list && list->dw_loc_next)
14601         {
14602           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14603                                            DECL_UID (decl), INSERT);
14604           cache = ggc_alloc_cleared_cached_dw_loc_list ();
14605           cache->decl_id = DECL_UID (decl);
14606           cache->loc_list = list;
14607           *slot = cache;
14608         }
14609     }
14610   if (list)
14611     {
14612       add_AT_location_description (die, attr, list);
14613       return true;
14614     }
14615   /* None of that worked, so it must not really have a location;
14616      try adding a constant value attribute from the DECL_INITIAL.  */
14617   return tree_add_const_value_attribute_for_decl (die, decl);
14618 }
14619
14620 /* Add VARIABLE and DIE into deferred locations list.  */
14621
14622 static void
14623 defer_location (tree variable, dw_die_ref die)
14624 {
14625   deferred_locations entry;
14626   entry.variable = variable;
14627   entry.die = die;
14628   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
14629 }
14630
14631 /* Helper function for tree_add_const_value_attribute.  Natively encode
14632    initializer INIT into an array.  Return true if successful.  */
14633
14634 static bool
14635 native_encode_initializer (tree init, unsigned char *array, int size)
14636 {
14637   tree type;
14638
14639   if (init == NULL_TREE)
14640     return false;
14641
14642   STRIP_NOPS (init);
14643   switch (TREE_CODE (init))
14644     {
14645     case STRING_CST:
14646       type = TREE_TYPE (init);
14647       if (TREE_CODE (type) == ARRAY_TYPE)
14648         {
14649           tree enttype = TREE_TYPE (type);
14650           enum machine_mode mode = TYPE_MODE (enttype);
14651
14652           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14653             return false;
14654           if (int_size_in_bytes (type) != size)
14655             return false;
14656           if (size > TREE_STRING_LENGTH (init))
14657             {
14658               memcpy (array, TREE_STRING_POINTER (init),
14659                       TREE_STRING_LENGTH (init));
14660               memset (array + TREE_STRING_LENGTH (init),
14661                       '\0', size - TREE_STRING_LENGTH (init));
14662             }
14663           else
14664             memcpy (array, TREE_STRING_POINTER (init), size);
14665           return true;
14666         }
14667       return false;
14668     case CONSTRUCTOR:
14669       type = TREE_TYPE (init);
14670       if (int_size_in_bytes (type) != size)
14671         return false;
14672       if (TREE_CODE (type) == ARRAY_TYPE)
14673         {
14674           HOST_WIDE_INT min_index;
14675           unsigned HOST_WIDE_INT cnt;
14676           int curpos = 0, fieldsize;
14677           constructor_elt *ce;
14678
14679           if (TYPE_DOMAIN (type) == NULL_TREE
14680               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14681             return false;
14682
14683           fieldsize = int_size_in_bytes (TREE_TYPE (type));
14684           if (fieldsize <= 0)
14685             return false;
14686
14687           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14688           memset (array, '\0', size);
14689           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14690             {
14691               tree val = ce->value;
14692               tree index = ce->index;
14693               int pos = curpos;
14694               if (index && TREE_CODE (index) == RANGE_EXPR)
14695                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14696                       * fieldsize;
14697               else if (index)
14698                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14699
14700               if (val)
14701                 {
14702                   STRIP_NOPS (val);
14703                   if (!native_encode_initializer (val, array + pos, fieldsize))
14704                     return false;
14705                 }
14706               curpos = pos + fieldsize;
14707               if (index && TREE_CODE (index) == RANGE_EXPR)
14708                 {
14709                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14710                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
14711                   while (count-- > 0)
14712                     {
14713                       if (val)
14714                         memcpy (array + curpos, array + pos, fieldsize);
14715                       curpos += fieldsize;
14716                     }
14717                 }
14718               gcc_assert (curpos <= size);
14719             }
14720           return true;
14721         }
14722       else if (TREE_CODE (type) == RECORD_TYPE
14723                || TREE_CODE (type) == UNION_TYPE)
14724         {
14725           tree field = NULL_TREE;
14726           unsigned HOST_WIDE_INT cnt;
14727           constructor_elt *ce;
14728
14729           if (int_size_in_bytes (type) != size)
14730             return false;
14731
14732           if (TREE_CODE (type) == RECORD_TYPE)
14733             field = TYPE_FIELDS (type);
14734
14735           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14736             {
14737               tree val = ce->value;
14738               int pos, fieldsize;
14739
14740               if (ce->index != 0)
14741                 field = ce->index;
14742
14743               if (val)
14744                 STRIP_NOPS (val);
14745
14746               if (field == NULL_TREE || DECL_BIT_FIELD (field))
14747                 return false;
14748
14749               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
14750                   && TYPE_DOMAIN (TREE_TYPE (field))
14751                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
14752                 return false;
14753               else if (DECL_SIZE_UNIT (field) == NULL_TREE
14754                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
14755                 return false;
14756               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
14757               pos = int_byte_position (field);
14758               gcc_assert (pos + fieldsize <= size);
14759               if (val
14760                   && !native_encode_initializer (val, array + pos, fieldsize))
14761                 return false;
14762             }
14763           return true;
14764         }
14765       return false;
14766     case VIEW_CONVERT_EXPR:
14767     case NON_LVALUE_EXPR:
14768       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
14769     default:
14770       return native_encode_expr (init, array, size) == size;
14771     }
14772 }
14773
14774 /* Attach a DW_AT_const_value attribute to DIE. The value of the
14775    attribute is the const value T.  */
14776
14777 static bool
14778 tree_add_const_value_attribute (dw_die_ref die, tree t)
14779 {
14780   tree init;
14781   tree type = TREE_TYPE (t);
14782   rtx rtl;
14783
14784   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
14785     return false;
14786
14787   init = t;
14788   gcc_assert (!DECL_P (init));
14789
14790   rtl = rtl_for_decl_init (init, type);
14791   if (rtl)
14792     return add_const_value_attribute (die, rtl);
14793   /* If the host and target are sane, try harder.  */
14794   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
14795            && initializer_constant_valid_p (init, type))
14796     {
14797       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
14798       if (size > 0 && (int) size == size)
14799         {
14800           unsigned char *array = (unsigned char *)
14801             ggc_alloc_cleared_atomic (size);
14802
14803           if (native_encode_initializer (init, array, size))
14804             {
14805               add_AT_vec (die, DW_AT_const_value, size, 1, array);
14806               return true;
14807             }
14808         }
14809     }
14810   return false;
14811 }
14812
14813 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
14814    attribute is the const value of T, where T is an integral constant
14815    variable with static storage duration
14816    (so it can't be a PARM_DECL or a RESULT_DECL).  */
14817
14818 static bool
14819 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
14820 {
14821
14822   if (!decl
14823       || (TREE_CODE (decl) != VAR_DECL
14824           && TREE_CODE (decl) != CONST_DECL)
14825       || (TREE_CODE (decl) == VAR_DECL
14826           && !TREE_STATIC (decl)))
14827     return false;
14828
14829     if (TREE_READONLY (decl)
14830         && ! TREE_THIS_VOLATILE (decl)
14831         && DECL_INITIAL (decl))
14832       /* OK */;
14833     else
14834       return false;
14835
14836   /* Don't add DW_AT_const_value if abstract origin already has one.  */
14837   if (get_AT (var_die, DW_AT_const_value))
14838     return false;
14839
14840   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
14841 }
14842
14843 /* Convert the CFI instructions for the current function into a
14844    location list.  This is used for DW_AT_frame_base when we targeting
14845    a dwarf2 consumer that does not support the dwarf3
14846    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
14847    expressions.  */
14848
14849 static dw_loc_list_ref
14850 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
14851 {
14852   int ix;
14853   dw_fde_ref fde;
14854   dw_loc_list_ref list, *list_tail;
14855   dw_cfi_ref cfi;
14856   dw_cfa_location last_cfa, next_cfa;
14857   const char *start_label, *last_label, *section;
14858   dw_cfa_location remember;
14859
14860   fde = cfun->fde;
14861   gcc_assert (fde != NULL);
14862
14863   section = secname_for_decl (current_function_decl);
14864   list_tail = &list;
14865   list = NULL;
14866
14867   memset (&next_cfa, 0, sizeof (next_cfa));
14868   next_cfa.reg = INVALID_REGNUM;
14869   remember = next_cfa;
14870
14871   start_label = fde->dw_fde_begin;
14872
14873   /* ??? Bald assumption that the CIE opcode list does not contain
14874      advance opcodes.  */
14875   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
14876     lookup_cfa_1 (cfi, &next_cfa, &remember);
14877
14878   last_cfa = next_cfa;
14879   last_label = start_label;
14880
14881   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
14882     {
14883       /* If the first partition contained no CFI adjustments, the
14884          CIE opcodes apply to the whole first partition.  */
14885       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14886                                  fde->dw_fde_begin, fde->dw_fde_end, section);
14887       list_tail =&(*list_tail)->dw_loc_next;
14888       start_label = last_label = fde->dw_fde_second_begin;
14889     }
14890
14891   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
14892     {
14893       switch (cfi->dw_cfi_opc)
14894         {
14895         case DW_CFA_set_loc:
14896         case DW_CFA_advance_loc1:
14897         case DW_CFA_advance_loc2:
14898         case DW_CFA_advance_loc4:
14899           if (!cfa_equal_p (&last_cfa, &next_cfa))
14900             {
14901               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14902                                          start_label, last_label, section);
14903
14904               list_tail = &(*list_tail)->dw_loc_next;
14905               last_cfa = next_cfa;
14906               start_label = last_label;
14907             }
14908           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
14909           break;
14910
14911         case DW_CFA_advance_loc:
14912           /* The encoding is complex enough that we should never emit this.  */
14913           gcc_unreachable ();
14914
14915         default:
14916           lookup_cfa_1 (cfi, &next_cfa, &remember);
14917           break;
14918         }
14919       if (ix + 1 == fde->dw_fde_switch_cfi_index)
14920         {
14921           if (!cfa_equal_p (&last_cfa, &next_cfa))
14922             {
14923               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14924                                          start_label, last_label, section);
14925
14926               list_tail = &(*list_tail)->dw_loc_next;
14927               last_cfa = next_cfa;
14928               start_label = last_label;
14929             }
14930           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14931                                      start_label, fde->dw_fde_end, section);
14932           list_tail = &(*list_tail)->dw_loc_next;
14933           start_label = last_label = fde->dw_fde_second_begin;
14934         }
14935     }
14936
14937   if (!cfa_equal_p (&last_cfa, &next_cfa))
14938     {
14939       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14940                                  start_label, last_label, section);
14941       list_tail = &(*list_tail)->dw_loc_next;
14942       start_label = last_label;
14943     }
14944
14945   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
14946                              start_label,
14947                              fde->dw_fde_second_begin
14948                              ? fde->dw_fde_second_end : fde->dw_fde_end,
14949                              section);
14950
14951   if (list && list->dw_loc_next)
14952     gen_llsym (list);
14953
14954   return list;
14955 }
14956
14957 /* Compute a displacement from the "steady-state frame pointer" to the
14958    frame base (often the same as the CFA), and store it in
14959    frame_pointer_fb_offset.  OFFSET is added to the displacement
14960    before the latter is negated.  */
14961
14962 static void
14963 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
14964 {
14965   rtx reg, elim;
14966
14967 #ifdef FRAME_POINTER_CFA_OFFSET
14968   reg = frame_pointer_rtx;
14969   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
14970 #else
14971   reg = arg_pointer_rtx;
14972   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
14973 #endif
14974
14975   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
14976   if (GET_CODE (elim) == PLUS)
14977     {
14978       offset += INTVAL (XEXP (elim, 1));
14979       elim = XEXP (elim, 0);
14980     }
14981
14982   frame_pointer_fb_offset = -offset;
14983
14984   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
14985      in which to eliminate.  This is because it's stack pointer isn't 
14986      directly accessible as a register within the ISA.  To work around
14987      this, assume that while we cannot provide a proper value for
14988      frame_pointer_fb_offset, we won't need one either.  */
14989   frame_pointer_fb_offset_valid
14990     = ((SUPPORTS_STACK_ALIGNMENT
14991         && (elim == hard_frame_pointer_rtx
14992             || elim == stack_pointer_rtx))
14993        || elim == (frame_pointer_needed
14994                    ? hard_frame_pointer_rtx
14995                    : stack_pointer_rtx));
14996 }
14997
14998 /* Generate a DW_AT_name attribute given some string value to be included as
14999    the value of the attribute.  */
15000
15001 static void
15002 add_name_attribute (dw_die_ref die, const char *name_string)
15003 {
15004   if (name_string != NULL && *name_string != 0)
15005     {
15006       if (demangle_name_func)
15007         name_string = (*demangle_name_func) (name_string);
15008
15009       add_AT_string (die, DW_AT_name, name_string);
15010     }
15011 }
15012
15013 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15014    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15015    of TYPE accordingly.
15016
15017    ??? This is a temporary measure until after we're able to generate
15018    regular DWARF for the complex Ada type system.  */
15019
15020 static void 
15021 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15022                                      dw_die_ref context_die)
15023 {
15024   tree dtype;
15025   dw_die_ref dtype_die;
15026
15027   if (!lang_hooks.types.descriptive_type)
15028     return;
15029
15030   dtype = lang_hooks.types.descriptive_type (type);
15031   if (!dtype)
15032     return;
15033
15034   dtype_die = lookup_type_die (dtype);
15035   if (!dtype_die)
15036     {
15037       gen_type_die (dtype, context_die);
15038       dtype_die = lookup_type_die (dtype);
15039       gcc_assert (dtype_die);
15040     }
15041
15042   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15043 }
15044
15045 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15046
15047 static void
15048 add_comp_dir_attribute (dw_die_ref die)
15049 {
15050   const char *wd = get_src_pwd ();
15051   char *wd1;
15052
15053   if (wd == NULL)
15054     return;
15055
15056   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15057     {
15058       int wdlen;
15059
15060       wdlen = strlen (wd);
15061       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15062       strcpy (wd1, wd);
15063       wd1 [wdlen] = DIR_SEPARATOR;
15064       wd1 [wdlen + 1] = 0;
15065       wd = wd1;
15066     }
15067
15068     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15069 }
15070
15071 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15072    default.  */
15073
15074 static int
15075 lower_bound_default (void)
15076 {
15077   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15078     {
15079     case DW_LANG_C:
15080     case DW_LANG_C89:
15081     case DW_LANG_C99:
15082     case DW_LANG_C_plus_plus:
15083     case DW_LANG_ObjC:
15084     case DW_LANG_ObjC_plus_plus:
15085     case DW_LANG_Java:
15086       return 0;
15087     case DW_LANG_Fortran77:
15088     case DW_LANG_Fortran90:
15089     case DW_LANG_Fortran95:
15090       return 1;
15091     case DW_LANG_UPC:
15092     case DW_LANG_D:
15093     case DW_LANG_Python:
15094       return dwarf_version >= 4 ? 0 : -1;
15095     case DW_LANG_Ada95:
15096     case DW_LANG_Ada83:
15097     case DW_LANG_Cobol74:
15098     case DW_LANG_Cobol85:
15099     case DW_LANG_Pascal83:
15100     case DW_LANG_Modula2:
15101     case DW_LANG_PLI:
15102       return dwarf_version >= 4 ? 1 : -1;
15103     default:
15104       return -1;
15105     }
15106 }
15107
15108 /* Given a tree node describing an array bound (either lower or upper) output
15109    a representation for that bound.  */
15110
15111 static void
15112 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15113 {
15114   switch (TREE_CODE (bound))
15115     {
15116     case ERROR_MARK:
15117       return;
15118
15119     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15120     case INTEGER_CST:
15121       {
15122         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15123         int dflt;
15124
15125         /* Use the default if possible.  */
15126         if (bound_attr == DW_AT_lower_bound
15127             && host_integerp (bound, 0)
15128             && (dflt = lower_bound_default ()) != -1
15129             && tree_low_cst (bound, 0) == dflt)
15130           ;
15131
15132         /* Otherwise represent the bound as an unsigned value with the
15133            precision of its type.  The precision and signedness of the
15134            type will be necessary to re-interpret it unambiguously.  */
15135         else if (prec < HOST_BITS_PER_WIDE_INT)
15136           {
15137             unsigned HOST_WIDE_INT mask
15138               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15139             add_AT_unsigned (subrange_die, bound_attr,
15140                              TREE_INT_CST_LOW (bound) & mask);
15141           }
15142         else if (prec == HOST_BITS_PER_WIDE_INT
15143                  || TREE_INT_CST_HIGH (bound) == 0)
15144           add_AT_unsigned (subrange_die, bound_attr,
15145                            TREE_INT_CST_LOW (bound));
15146         else
15147           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15148                          TREE_INT_CST_LOW (bound));
15149       }
15150       break;
15151
15152     CASE_CONVERT:
15153     case VIEW_CONVERT_EXPR:
15154       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15155       break;
15156
15157     case SAVE_EXPR:
15158       break;
15159
15160     case VAR_DECL:
15161     case PARM_DECL:
15162     case RESULT_DECL:
15163       {
15164         dw_die_ref decl_die = lookup_decl_die (bound);
15165
15166         /* ??? Can this happen, or should the variable have been bound
15167            first?  Probably it can, since I imagine that we try to create
15168            the types of parameters in the order in which they exist in
15169            the list, and won't have created a forward reference to a
15170            later parameter.  */
15171         if (decl_die != NULL)
15172           {
15173             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15174             break;
15175           }
15176       }
15177       /* FALLTHRU */
15178
15179     default:
15180       {
15181         /* Otherwise try to create a stack operation procedure to
15182            evaluate the value of the array bound.  */
15183
15184         dw_die_ref ctx, decl_die;
15185         dw_loc_list_ref list;
15186
15187         list = loc_list_from_tree (bound, 2);
15188         if (list == NULL || single_element_loc_list_p (list))
15189           {
15190             /* If DW_AT_*bound is not a reference nor constant, it is
15191                a DWARF expression rather than location description.
15192                For that loc_list_from_tree (bound, 0) is needed.
15193                If that fails to give a single element list,
15194                fall back to outputting this as a reference anyway.  */
15195             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15196             if (list2 && single_element_loc_list_p (list2))
15197               {
15198                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15199                 break;
15200               }
15201           }
15202         if (list == NULL)
15203           break;
15204
15205         if (current_function_decl == 0)
15206           ctx = comp_unit_die ();
15207         else
15208           ctx = lookup_decl_die (current_function_decl);
15209
15210         decl_die = new_die (DW_TAG_variable, ctx, bound);
15211         add_AT_flag (decl_die, DW_AT_artificial, 1);
15212         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15213         add_AT_location_description (decl_die, DW_AT_location, list);
15214         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15215         break;
15216       }
15217     }
15218 }
15219
15220 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15221    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15222    Note that the block of subscript information for an array type also
15223    includes information about the element type of the given array type.  */
15224
15225 static void
15226 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15227 {
15228   unsigned dimension_number;
15229   tree lower, upper;
15230   dw_die_ref subrange_die;
15231
15232   for (dimension_number = 0;
15233        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15234        type = TREE_TYPE (type), dimension_number++)
15235     {
15236       tree domain = TYPE_DOMAIN (type);
15237
15238       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15239         break;
15240
15241       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15242          and (in GNU C only) variable bounds.  Handle all three forms
15243          here.  */
15244       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15245       if (domain)
15246         {
15247           /* We have an array type with specified bounds.  */
15248           lower = TYPE_MIN_VALUE (domain);
15249           upper = TYPE_MAX_VALUE (domain);
15250
15251           /* Define the index type.  */
15252           if (TREE_TYPE (domain))
15253             {
15254               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15255                  TREE_TYPE field.  We can't emit debug info for this
15256                  because it is an unnamed integral type.  */
15257               if (TREE_CODE (domain) == INTEGER_TYPE
15258                   && TYPE_NAME (domain) == NULL_TREE
15259                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15260                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15261                 ;
15262               else
15263                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15264                                     type_die);
15265             }
15266
15267           /* ??? If upper is NULL, the array has unspecified length,
15268              but it does have a lower bound.  This happens with Fortran
15269                dimension arr(N:*)
15270              Since the debugger is definitely going to need to know N
15271              to produce useful results, go ahead and output the lower
15272              bound solo, and hope the debugger can cope.  */
15273
15274           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15275           if (upper)
15276             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15277         }
15278
15279       /* Otherwise we have an array type with an unspecified length.  The
15280          DWARF-2 spec does not say how to handle this; let's just leave out the
15281          bounds.  */
15282     }
15283 }
15284
15285 static void
15286 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15287 {
15288   dw_die_ref decl_die;
15289   unsigned size;
15290
15291   switch (TREE_CODE (tree_node))
15292     {
15293     case ERROR_MARK:
15294       size = 0;
15295       break;
15296     case ENUMERAL_TYPE:
15297     case RECORD_TYPE:
15298     case UNION_TYPE:
15299     case QUAL_UNION_TYPE:
15300       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
15301           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
15302         {
15303           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
15304           return;
15305         }
15306       size = int_size_in_bytes (tree_node);
15307       break;
15308     case FIELD_DECL:
15309       /* For a data member of a struct or union, the DW_AT_byte_size is
15310          generally given as the number of bytes normally allocated for an
15311          object of the *declared* type of the member itself.  This is true
15312          even for bit-fields.  */
15313       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15314       break;
15315     default:
15316       gcc_unreachable ();
15317     }
15318
15319   /* Note that `size' might be -1 when we get to this point.  If it is, that
15320      indicates that the byte size of the entity in question is variable.  We
15321      have no good way of expressing this fact in Dwarf at the present time,
15322      so just let the -1 pass on through.  */
15323   add_AT_unsigned (die, DW_AT_byte_size, size);
15324 }
15325
15326 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15327    which specifies the distance in bits from the highest order bit of the
15328    "containing object" for the bit-field to the highest order bit of the
15329    bit-field itself.
15330
15331    For any given bit-field, the "containing object" is a hypothetical object
15332    (of some integral or enum type) within which the given bit-field lives.  The
15333    type of this hypothetical "containing object" is always the same as the
15334    declared type of the individual bit-field itself.  The determination of the
15335    exact location of the "containing object" for a bit-field is rather
15336    complicated.  It's handled by the `field_byte_offset' function (above).
15337
15338    Note that it is the size (in bytes) of the hypothetical "containing object"
15339    which will be given in the DW_AT_byte_size attribute for this bit-field.
15340    (See `byte_size_attribute' above).  */
15341
15342 static inline void
15343 add_bit_offset_attribute (dw_die_ref die, tree decl)
15344 {
15345   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15346   tree type = DECL_BIT_FIELD_TYPE (decl);
15347   HOST_WIDE_INT bitpos_int;
15348   HOST_WIDE_INT highest_order_object_bit_offset;
15349   HOST_WIDE_INT highest_order_field_bit_offset;
15350   HOST_WIDE_INT bit_offset;
15351
15352   /* Must be a field and a bit field.  */
15353   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15354
15355   /* We can't yet handle bit-fields whose offsets are variable, so if we
15356      encounter such things, just return without generating any attribute
15357      whatsoever.  Likewise for variable or too large size.  */
15358   if (! host_integerp (bit_position (decl), 0)
15359       || ! host_integerp (DECL_SIZE (decl), 1))
15360     return;
15361
15362   bitpos_int = int_bit_position (decl);
15363
15364   /* Note that the bit offset is always the distance (in bits) from the
15365      highest-order bit of the "containing object" to the highest-order bit of
15366      the bit-field itself.  Since the "high-order end" of any object or field
15367      is different on big-endian and little-endian machines, the computation
15368      below must take account of these differences.  */
15369   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15370   highest_order_field_bit_offset = bitpos_int;
15371
15372   if (! BYTES_BIG_ENDIAN)
15373     {
15374       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15375       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15376     }
15377
15378   bit_offset
15379     = (! BYTES_BIG_ENDIAN
15380        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15381        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15382
15383   if (bit_offset < 0)
15384     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15385   else
15386     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15387 }
15388
15389 /* For a FIELD_DECL node which represents a bit field, output an attribute
15390    which specifies the length in bits of the given field.  */
15391
15392 static inline void
15393 add_bit_size_attribute (dw_die_ref die, tree decl)
15394 {
15395   /* Must be a field and a bit field.  */
15396   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15397               && DECL_BIT_FIELD_TYPE (decl));
15398
15399   if (host_integerp (DECL_SIZE (decl), 1))
15400     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15401 }
15402
15403 /* If the compiled language is ANSI C, then add a 'prototyped'
15404    attribute, if arg types are given for the parameters of a function.  */
15405
15406 static inline void
15407 add_prototyped_attribute (dw_die_ref die, tree func_type)
15408 {
15409   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15410       && prototype_p (func_type))
15411     add_AT_flag (die, DW_AT_prototyped, 1);
15412 }
15413
15414 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15415    by looking in either the type declaration or object declaration
15416    equate table.  */
15417
15418 static inline dw_die_ref
15419 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15420 {
15421   dw_die_ref origin_die = NULL;
15422
15423   if (TREE_CODE (origin) != FUNCTION_DECL)
15424     {
15425       /* We may have gotten separated from the block for the inlined
15426          function, if we're in an exception handler or some such; make
15427          sure that the abstract function has been written out.
15428
15429          Doing this for nested functions is wrong, however; functions are
15430          distinct units, and our context might not even be inline.  */
15431       tree fn = origin;
15432
15433       if (TYPE_P (fn))
15434         fn = TYPE_STUB_DECL (fn);
15435
15436       fn = decl_function_context (fn);
15437       if (fn)
15438         dwarf2out_abstract_function (fn);
15439     }
15440
15441   if (DECL_P (origin))
15442     origin_die = lookup_decl_die (origin);
15443   else if (TYPE_P (origin))
15444     origin_die = lookup_type_die (origin);
15445
15446   /* XXX: Functions that are never lowered don't always have correct block
15447      trees (in the case of java, they simply have no block tree, in some other
15448      languages).  For these functions, there is nothing we can really do to
15449      output correct debug info for inlined functions in all cases.  Rather
15450      than die, we'll just produce deficient debug info now, in that we will
15451      have variables without a proper abstract origin.  In the future, when all
15452      functions are lowered, we should re-add a gcc_assert (origin_die)
15453      here.  */
15454
15455   if (origin_die)
15456     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15457   return origin_die;
15458 }
15459
15460 /* We do not currently support the pure_virtual attribute.  */
15461
15462 static inline void
15463 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15464 {
15465   if (DECL_VINDEX (func_decl))
15466     {
15467       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15468
15469       if (host_integerp (DECL_VINDEX (func_decl), 0))
15470         add_AT_loc (die, DW_AT_vtable_elem_location,
15471                     new_loc_descr (DW_OP_constu,
15472                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15473                                    0));
15474
15475       /* GNU extension: Record what type this method came from originally.  */
15476       if (debug_info_level > DINFO_LEVEL_TERSE
15477           && DECL_CONTEXT (func_decl))
15478         add_AT_die_ref (die, DW_AT_containing_type,
15479                         lookup_type_die (DECL_CONTEXT (func_decl)));
15480     }
15481 }
15482 \f
15483 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15484    given decl.  This used to be a vendor extension until after DWARF 4
15485    standardized it.  */
15486
15487 static void
15488 add_linkage_attr (dw_die_ref die, tree decl)
15489 {
15490   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15491
15492   /* Mimic what assemble_name_raw does with a leading '*'.  */
15493   if (name[0] == '*')
15494     name = &name[1];
15495
15496   if (dwarf_version >= 4)
15497     add_AT_string (die, DW_AT_linkage_name, name);
15498   else
15499     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15500 }
15501
15502 /* Add source coordinate attributes for the given decl.  */
15503
15504 static void
15505 add_src_coords_attributes (dw_die_ref die, tree decl)
15506 {
15507   expanded_location s;
15508
15509   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15510     return;
15511   s = expand_location (DECL_SOURCE_LOCATION (decl));
15512   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15513   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15514 }
15515
15516 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15517
15518 static void
15519 add_linkage_name (dw_die_ref die, tree decl)
15520 {
15521   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15522        && TREE_PUBLIC (decl)
15523        && !DECL_ABSTRACT (decl)
15524        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15525        && die->die_tag != DW_TAG_member)
15526     {
15527       /* Defer until we have an assembler name set.  */
15528       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15529         {
15530           limbo_die_node *asm_name;
15531
15532           asm_name = ggc_alloc_cleared_limbo_die_node ();
15533           asm_name->die = die;
15534           asm_name->created_for = decl;
15535           asm_name->next = deferred_asm_name;
15536           deferred_asm_name = asm_name;
15537         }
15538       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15539         add_linkage_attr (die, decl);
15540     }
15541 }
15542
15543 /* Add a DW_AT_name attribute and source coordinate attribute for the
15544    given decl, but only if it actually has a name.  */
15545
15546 static void
15547 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15548 {
15549   tree decl_name;
15550
15551   decl_name = DECL_NAME (decl);
15552   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15553     {
15554       const char *name = dwarf2_name (decl, 0);
15555       if (name)
15556         add_name_attribute (die, name);
15557       if (! DECL_ARTIFICIAL (decl))
15558         add_src_coords_attributes (die, decl);
15559
15560       add_linkage_name (die, decl);
15561     }
15562
15563 #ifdef VMS_DEBUGGING_INFO
15564   /* Get the function's name, as described by its RTL.  This may be different
15565      from the DECL_NAME name used in the source file.  */
15566   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15567     {
15568       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15569                    XEXP (DECL_RTL (decl), 0));
15570       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15571     }
15572 #endif /* VMS_DEBUGGING_INFO */
15573 }
15574
15575 #ifdef VMS_DEBUGGING_INFO
15576 /* Output the debug main pointer die for VMS */
15577
15578 void
15579 dwarf2out_vms_debug_main_pointer (void)
15580 {
15581   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15582   dw_die_ref die;
15583
15584   /* Allocate the VMS debug main subprogram die.  */
15585   die = ggc_alloc_cleared_die_node ();
15586   die->die_tag = DW_TAG_subprogram;
15587   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15588   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15589                                current_function_funcdef_no);
15590   add_AT_lbl_id (die, DW_AT_entry_pc, label);
15591
15592   /* Make it the first child of comp_unit_die ().  */
15593   die->die_parent = comp_unit_die ();
15594   if (comp_unit_die ()->die_child)
15595     {
15596       die->die_sib = comp_unit_die ()->die_child->die_sib;
15597       comp_unit_die ()->die_child->die_sib = die;
15598     }
15599   else
15600     {
15601       die->die_sib = die;
15602       comp_unit_die ()->die_child = die;
15603     }
15604 }
15605 #endif /* VMS_DEBUGGING_INFO */
15606
15607 /* Push a new declaration scope.  */
15608
15609 static void
15610 push_decl_scope (tree scope)
15611 {
15612   VEC_safe_push (tree, gc, decl_scope_table, scope);
15613 }
15614
15615 /* Pop a declaration scope.  */
15616
15617 static inline void
15618 pop_decl_scope (void)
15619 {
15620   VEC_pop (tree, decl_scope_table);
15621 }
15622
15623 /* walk_tree helper function for uses_local_type, below.  */
15624
15625 static tree
15626 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
15627 {
15628   if (!TYPE_P (*tp))
15629     *walk_subtrees = 0;
15630   else
15631     {
15632       tree name = TYPE_NAME (*tp);
15633       if (name && DECL_P (name) && decl_function_context (name))
15634         return *tp;
15635     }
15636   return NULL_TREE;
15637 }
15638
15639 /* If TYPE involves a function-local type (including a local typedef to a
15640    non-local type), returns that type; otherwise returns NULL_TREE.  */
15641
15642 static tree
15643 uses_local_type (tree type)
15644 {
15645   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
15646   return used;
15647 }
15648
15649 /* Return the DIE for the scope that immediately contains this type.
15650    Non-named types that do not involve a function-local type get global
15651    scope.  Named types nested in namespaces or other types get their
15652    containing scope.  All other types (i.e. function-local named types) get
15653    the current active scope.  */
15654
15655 static dw_die_ref
15656 scope_die_for (tree t, dw_die_ref context_die)
15657 {
15658   dw_die_ref scope_die = NULL;
15659   tree containing_scope;
15660
15661   /* Non-types always go in the current scope.  */
15662   gcc_assert (TYPE_P (t));
15663
15664   /* Use the scope of the typedef, rather than the scope of the type
15665      it refers to.  */
15666   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
15667     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
15668   else
15669     containing_scope = TYPE_CONTEXT (t);
15670
15671   /* Use the containing namespace if there is one.  */
15672   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15673     {
15674       if (context_die == lookup_decl_die (containing_scope))
15675         /* OK */;
15676       else if (debug_info_level > DINFO_LEVEL_TERSE)
15677         context_die = get_context_die (containing_scope);
15678       else
15679         containing_scope = NULL_TREE;
15680     }
15681
15682   /* Ignore function type "scopes" from the C frontend.  They mean that
15683      a tagged type is local to a parmlist of a function declarator, but
15684      that isn't useful to DWARF.  */
15685   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
15686     containing_scope = NULL_TREE;
15687
15688   if (SCOPE_FILE_SCOPE_P (containing_scope))
15689     {
15690       /* If T uses a local type keep it local as well, to avoid references
15691          to function-local DIEs from outside the function.  */
15692       if (current_function_decl && uses_local_type (t))
15693         scope_die = context_die;
15694       else
15695         scope_die = comp_unit_die ();
15696     }
15697   else if (TYPE_P (containing_scope))
15698     {
15699       /* For types, we can just look up the appropriate DIE.  */
15700       if (debug_info_level > DINFO_LEVEL_TERSE)
15701         scope_die = get_context_die (containing_scope);
15702       else
15703         {
15704           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
15705           if (scope_die == NULL)
15706             scope_die = comp_unit_die ();
15707         }
15708     }
15709   else
15710     scope_die = context_die;
15711
15712   return scope_die;
15713 }
15714
15715 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
15716
15717 static inline int
15718 local_scope_p (dw_die_ref context_die)
15719 {
15720   for (; context_die; context_die = context_die->die_parent)
15721     if (context_die->die_tag == DW_TAG_inlined_subroutine
15722         || context_die->die_tag == DW_TAG_subprogram)
15723       return 1;
15724
15725   return 0;
15726 }
15727
15728 /* Returns nonzero if CONTEXT_DIE is a class.  */
15729
15730 static inline int
15731 class_scope_p (dw_die_ref context_die)
15732 {
15733   return (context_die
15734           && (context_die->die_tag == DW_TAG_structure_type
15735               || context_die->die_tag == DW_TAG_class_type
15736               || context_die->die_tag == DW_TAG_interface_type
15737               || context_die->die_tag == DW_TAG_union_type));
15738 }
15739
15740 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
15741    whether or not to treat a DIE in this context as a declaration.  */
15742
15743 static inline int
15744 class_or_namespace_scope_p (dw_die_ref context_die)
15745 {
15746   return (class_scope_p (context_die)
15747           || (context_die && context_die->die_tag == DW_TAG_namespace));
15748 }
15749
15750 /* Many forms of DIEs require a "type description" attribute.  This
15751    routine locates the proper "type descriptor" die for the type given
15752    by 'type', and adds a DW_AT_type attribute below the given die.  */
15753
15754 static void
15755 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
15756                     int decl_volatile, dw_die_ref context_die)
15757 {
15758   enum tree_code code  = TREE_CODE (type);
15759   dw_die_ref type_die  = NULL;
15760
15761   /* ??? If this type is an unnamed subrange type of an integral, floating-point
15762      or fixed-point type, use the inner type.  This is because we have no
15763      support for unnamed types in base_type_die.  This can happen if this is
15764      an Ada subrange type.  Correct solution is emit a subrange type die.  */
15765   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
15766       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
15767     type = TREE_TYPE (type), code = TREE_CODE (type);
15768
15769   if (code == ERROR_MARK
15770       /* Handle a special case.  For functions whose return type is void, we
15771          generate *no* type attribute.  (Note that no object may have type
15772          `void', so this only applies to function return types).  */
15773       || code == VOID_TYPE)
15774     return;
15775
15776   type_die = modified_type_die (type,
15777                                 decl_const || TYPE_READONLY (type),
15778                                 decl_volatile || TYPE_VOLATILE (type),
15779                                 context_die);
15780
15781   if (type_die != NULL)
15782     add_AT_die_ref (object_die, DW_AT_type, type_die);
15783 }
15784
15785 /* Given an object die, add the calling convention attribute for the
15786    function call type.  */
15787 static void
15788 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
15789 {
15790   enum dwarf_calling_convention value = DW_CC_normal;
15791
15792   value = ((enum dwarf_calling_convention)
15793            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
15794
15795   if (is_fortran ()
15796       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
15797     {
15798       /* DWARF 2 doesn't provide a way to identify a program's source-level
15799         entry point.  DW_AT_calling_convention attributes are only meant
15800         to describe functions' calling conventions.  However, lacking a
15801         better way to signal the Fortran main program, we used this for 
15802         a long time, following existing custom.  Now, DWARF 4 has 
15803         DW_AT_main_subprogram, which we add below, but some tools still
15804         rely on the old way, which we thus keep.  */
15805       value = DW_CC_program;
15806
15807       if (dwarf_version >= 4 || !dwarf_strict)
15808         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
15809     }
15810
15811   /* Only add the attribute if the backend requests it, and
15812      is not DW_CC_normal.  */
15813   if (value && (value != DW_CC_normal))
15814     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
15815 }
15816
15817 /* Given a tree pointer to a struct, class, union, or enum type node, return
15818    a pointer to the (string) tag name for the given type, or zero if the type
15819    was declared without a tag.  */
15820
15821 static const char *
15822 type_tag (const_tree type)
15823 {
15824   const char *name = 0;
15825
15826   if (TYPE_NAME (type) != 0)
15827     {
15828       tree t = 0;
15829
15830       /* Find the IDENTIFIER_NODE for the type name.  */
15831       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
15832           && !TYPE_NAMELESS (type))
15833         t = TYPE_NAME (type);
15834
15835       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
15836          a TYPE_DECL node, regardless of whether or not a `typedef' was
15837          involved.  */
15838       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15839                && ! DECL_IGNORED_P (TYPE_NAME (type)))
15840         {
15841           /* We want to be extra verbose.  Don't call dwarf_name if
15842              DECL_NAME isn't set.  The default hook for decl_printable_name
15843              doesn't like that, and in this context it's correct to return
15844              0, instead of "<anonymous>" or the like.  */
15845           if (DECL_NAME (TYPE_NAME (type))
15846               && !DECL_NAMELESS (TYPE_NAME (type)))
15847             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
15848         }
15849
15850       /* Now get the name as a string, or invent one.  */
15851       if (!name && t != 0)
15852         name = IDENTIFIER_POINTER (t);
15853     }
15854
15855   return (name == 0 || *name == '\0') ? 0 : name;
15856 }
15857
15858 /* Return the type associated with a data member, make a special check
15859    for bit field types.  */
15860
15861 static inline tree
15862 member_declared_type (const_tree member)
15863 {
15864   return (DECL_BIT_FIELD_TYPE (member)
15865           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
15866 }
15867
15868 /* Get the decl's label, as described by its RTL. This may be different
15869    from the DECL_NAME name used in the source file.  */
15870
15871 #if 0
15872 static const char *
15873 decl_start_label (tree decl)
15874 {
15875   rtx x;
15876   const char *fnname;
15877
15878   x = DECL_RTL (decl);
15879   gcc_assert (MEM_P (x));
15880
15881   x = XEXP (x, 0);
15882   gcc_assert (GET_CODE (x) == SYMBOL_REF);
15883
15884   fnname = XSTR (x, 0);
15885   return fnname;
15886 }
15887 #endif
15888 \f
15889 /* These routines generate the internal representation of the DIE's for
15890    the compilation unit.  Debugging information is collected by walking
15891    the declaration trees passed in from dwarf2out_decl().  */
15892
15893 static void
15894 gen_array_type_die (tree type, dw_die_ref context_die)
15895 {
15896   dw_die_ref scope_die = scope_die_for (type, context_die);
15897   dw_die_ref array_die;
15898
15899   /* GNU compilers represent multidimensional array types as sequences of one
15900      dimensional array types whose element types are themselves array types.
15901      We sometimes squish that down to a single array_type DIE with multiple
15902      subscripts in the Dwarf debugging info.  The draft Dwarf specification
15903      say that we are allowed to do this kind of compression in C, because
15904      there is no difference between an array of arrays and a multidimensional
15905      array.  We don't do this for Ada to remain as close as possible to the
15906      actual representation, which is especially important against the language
15907      flexibilty wrt arrays of variable size.  */
15908
15909   bool collapse_nested_arrays = !is_ada ();
15910   tree element_type;
15911
15912   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
15913      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
15914   if (TYPE_STRING_FLAG (type)
15915       && TREE_CODE (type) == ARRAY_TYPE
15916       && is_fortran ()
15917       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
15918     {
15919       HOST_WIDE_INT size;
15920
15921       array_die = new_die (DW_TAG_string_type, scope_die, type);
15922       add_name_attribute (array_die, type_tag (type));
15923       equate_type_number_to_die (type, array_die);
15924       size = int_size_in_bytes (type);
15925       if (size >= 0)
15926         add_AT_unsigned (array_die, DW_AT_byte_size, size);
15927       else if (TYPE_DOMAIN (type) != NULL_TREE
15928                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
15929                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
15930         {
15931           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
15932           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
15933
15934           size = int_size_in_bytes (TREE_TYPE (szdecl));
15935           if (loc && size > 0)
15936             {
15937               add_AT_location_description (array_die, DW_AT_string_length, loc);
15938               if (size != DWARF2_ADDR_SIZE)
15939                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15940             }
15941         }
15942       return;
15943     }
15944
15945   array_die = new_die (DW_TAG_array_type, scope_die, type);
15946   add_name_attribute (array_die, type_tag (type));
15947   equate_type_number_to_die (type, array_die);
15948
15949   if (TREE_CODE (type) == VECTOR_TYPE)
15950     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
15951
15952   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
15953   if (is_fortran ()
15954       && TREE_CODE (type) == ARRAY_TYPE
15955       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
15956       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
15957     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
15958
15959 #if 0
15960   /* We default the array ordering.  SDB will probably do
15961      the right things even if DW_AT_ordering is not present.  It's not even
15962      an issue until we start to get into multidimensional arrays anyway.  If
15963      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
15964      then we'll have to put the DW_AT_ordering attribute back in.  (But if
15965      and when we find out that we need to put these in, we will only do so
15966      for multidimensional arrays.  */
15967   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
15968 #endif
15969
15970   if (TREE_CODE (type) == VECTOR_TYPE)
15971     {
15972       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
15973       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
15974       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
15975       add_bound_info (subrange_die, DW_AT_upper_bound,
15976                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
15977     }
15978   else
15979     add_subscript_info (array_die, type, collapse_nested_arrays);
15980
15981   /* Add representation of the type of the elements of this array type and
15982      emit the corresponding DIE if we haven't done it already.  */
15983   element_type = TREE_TYPE (type);
15984   if (collapse_nested_arrays)
15985     while (TREE_CODE (element_type) == ARRAY_TYPE)
15986       {
15987         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
15988           break;
15989         element_type = TREE_TYPE (element_type);
15990       }
15991
15992   add_type_attribute (array_die, element_type, 0, 0, context_die);
15993
15994   add_gnat_descriptive_type_attribute (array_die, type, context_die);
15995   if (TYPE_ARTIFICIAL (type))
15996     add_AT_flag (array_die, DW_AT_artificial, 1);
15997
15998   if (get_AT (array_die, DW_AT_name))
15999     add_pubtype (type, array_die);
16000 }
16001
16002 static dw_loc_descr_ref
16003 descr_info_loc (tree val, tree base_decl)
16004 {
16005   HOST_WIDE_INT size;
16006   dw_loc_descr_ref loc, loc2;
16007   enum dwarf_location_atom op;
16008
16009   if (val == base_decl)
16010     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16011
16012   switch (TREE_CODE (val))
16013     {
16014     CASE_CONVERT:
16015       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16016     case VAR_DECL:
16017       return loc_descriptor_from_tree (val, 0);
16018     case INTEGER_CST:
16019       if (host_integerp (val, 0))
16020         return int_loc_descriptor (tree_low_cst (val, 0));
16021       break;
16022     case INDIRECT_REF:
16023       size = int_size_in_bytes (TREE_TYPE (val));
16024       if (size < 0)
16025         break;
16026       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16027       if (!loc)
16028         break;
16029       if (size == DWARF2_ADDR_SIZE)
16030         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16031       else
16032         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16033       return loc;
16034     case POINTER_PLUS_EXPR:
16035     case PLUS_EXPR:
16036       if (host_integerp (TREE_OPERAND (val, 1), 1)
16037           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16038              < 16384)
16039         {
16040           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16041           if (!loc)
16042             break;
16043           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16044         }
16045       else
16046         {
16047           op = DW_OP_plus;
16048         do_binop:
16049           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16050           if (!loc)
16051             break;
16052           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16053           if (!loc2)
16054             break;
16055           add_loc_descr (&loc, loc2);
16056           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16057         }
16058       return loc;
16059     case MINUS_EXPR:
16060       op = DW_OP_minus;
16061       goto do_binop;
16062     case MULT_EXPR:
16063       op = DW_OP_mul;
16064       goto do_binop;
16065     case EQ_EXPR:
16066       op = DW_OP_eq;
16067       goto do_binop;
16068     case NE_EXPR:
16069       op = DW_OP_ne;
16070       goto do_binop;
16071     default:
16072       break;
16073     }
16074   return NULL;
16075 }
16076
16077 static void
16078 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16079                       tree val, tree base_decl)
16080 {
16081   dw_loc_descr_ref loc;
16082
16083   if (host_integerp (val, 0))
16084     {
16085       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16086       return;
16087     }
16088
16089   loc = descr_info_loc (val, base_decl);
16090   if (!loc)
16091     return;
16092
16093   add_AT_loc (die, attr, loc);
16094 }
16095
16096 /* This routine generates DIE for array with hidden descriptor, details
16097    are filled into *info by a langhook.  */
16098
16099 static void
16100 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16101                           dw_die_ref context_die)
16102 {
16103   dw_die_ref scope_die = scope_die_for (type, context_die);
16104   dw_die_ref array_die;
16105   int dim;
16106
16107   array_die = new_die (DW_TAG_array_type, scope_die, type);
16108   add_name_attribute (array_die, type_tag (type));
16109   equate_type_number_to_die (type, array_die);
16110
16111   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16112   if (is_fortran ()
16113       && info->ndimensions >= 2)
16114     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16115
16116   if (info->data_location)
16117     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16118                           info->base_decl);
16119   if (info->associated)
16120     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16121                           info->base_decl);
16122   if (info->allocated)
16123     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16124                           info->base_decl);
16125
16126   for (dim = 0; dim < info->ndimensions; dim++)
16127     {
16128       dw_die_ref subrange_die
16129         = new_die (DW_TAG_subrange_type, array_die, NULL);
16130
16131       if (info->dimen[dim].lower_bound)
16132         {
16133           /* If it is the default value, omit it.  */
16134           int dflt;
16135
16136           if (host_integerp (info->dimen[dim].lower_bound, 0)
16137               && (dflt = lower_bound_default ()) != -1
16138               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16139             ;
16140           else
16141             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16142                                   info->dimen[dim].lower_bound,
16143                                   info->base_decl);
16144         }
16145       if (info->dimen[dim].upper_bound)
16146         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16147                               info->dimen[dim].upper_bound,
16148                               info->base_decl);
16149       if (info->dimen[dim].stride)
16150         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16151                               info->dimen[dim].stride,
16152                               info->base_decl);
16153     }
16154
16155   gen_type_die (info->element_type, context_die);
16156   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16157
16158   if (get_AT (array_die, DW_AT_name))
16159     add_pubtype (type, array_die);
16160 }
16161
16162 #if 0
16163 static void
16164 gen_entry_point_die (tree decl, dw_die_ref context_die)
16165 {
16166   tree origin = decl_ultimate_origin (decl);
16167   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16168
16169   if (origin != NULL)
16170     add_abstract_origin_attribute (decl_die, origin);
16171   else
16172     {
16173       add_name_and_src_coords_attributes (decl_die, decl);
16174       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16175                           0, 0, context_die);
16176     }
16177
16178   if (DECL_ABSTRACT (decl))
16179     equate_decl_number_to_die (decl, decl_die);
16180   else
16181     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16182 }
16183 #endif
16184
16185 /* Walk through the list of incomplete types again, trying once more to
16186    emit full debugging info for them.  */
16187
16188 static void
16189 retry_incomplete_types (void)
16190 {
16191   int i;
16192
16193   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16194     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16195                                   DINFO_USAGE_DIR_USE))
16196       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16197 }
16198
16199 /* Determine what tag to use for a record type.  */
16200
16201 static enum dwarf_tag
16202 record_type_tag (tree type)
16203 {
16204   if (! lang_hooks.types.classify_record)
16205     return DW_TAG_structure_type;
16206
16207   switch (lang_hooks.types.classify_record (type))
16208     {
16209     case RECORD_IS_STRUCT:
16210       return DW_TAG_structure_type;
16211
16212     case RECORD_IS_CLASS:
16213       return DW_TAG_class_type;
16214
16215     case RECORD_IS_INTERFACE:
16216       if (dwarf_version >= 3 || !dwarf_strict)
16217         return DW_TAG_interface_type;
16218       return DW_TAG_structure_type;
16219
16220     default:
16221       gcc_unreachable ();
16222     }
16223 }
16224
16225 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16226    include all of the information about the enumeration values also. Each
16227    enumerated type name/value is listed as a child of the enumerated type
16228    DIE.  */
16229
16230 static dw_die_ref
16231 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16232 {
16233   dw_die_ref type_die = lookup_type_die (type);
16234
16235   if (type_die == NULL)
16236     {
16237       type_die = new_die (DW_TAG_enumeration_type,
16238                           scope_die_for (type, context_die), type);
16239       equate_type_number_to_die (type, type_die);
16240       add_name_attribute (type_die, type_tag (type));
16241       if (dwarf_version >= 4 || !dwarf_strict)
16242         {
16243           if (ENUM_IS_SCOPED (type))
16244             add_AT_flag (type_die, DW_AT_enum_class, 1);
16245           if (ENUM_IS_OPAQUE (type))
16246             add_AT_flag (type_die, DW_AT_declaration, 1);
16247         }
16248     }
16249   else if (! TYPE_SIZE (type))
16250     return type_die;
16251   else
16252     remove_AT (type_die, DW_AT_declaration);
16253
16254   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16255      given enum type is incomplete, do not generate the DW_AT_byte_size
16256      attribute or the DW_AT_element_list attribute.  */
16257   if (TYPE_SIZE (type))
16258     {
16259       tree link;
16260
16261       TREE_ASM_WRITTEN (type) = 1;
16262       add_byte_size_attribute (type_die, type);
16263       if (TYPE_STUB_DECL (type) != NULL_TREE)
16264         {
16265           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16266           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16267         }
16268
16269       /* If the first reference to this type was as the return type of an
16270          inline function, then it may not have a parent.  Fix this now.  */
16271       if (type_die->die_parent == NULL)
16272         add_child_die (scope_die_for (type, context_die), type_die);
16273
16274       for (link = TYPE_VALUES (type);
16275            link != NULL; link = TREE_CHAIN (link))
16276         {
16277           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16278           tree value = TREE_VALUE (link);
16279
16280           add_name_attribute (enum_die,
16281                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16282
16283           if (TREE_CODE (value) == CONST_DECL)
16284             value = DECL_INITIAL (value);
16285
16286           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16287             /* DWARF2 does not provide a way of indicating whether or
16288                not enumeration constants are signed or unsigned.  GDB
16289                always assumes the values are signed, so we output all
16290                values as if they were signed.  That means that
16291                enumeration constants with very large unsigned values
16292                will appear to have negative values in the debugger.  */
16293             add_AT_int (enum_die, DW_AT_const_value,
16294                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16295         }
16296
16297       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16298       if (TYPE_ARTIFICIAL (type))
16299         add_AT_flag (type_die, DW_AT_artificial, 1);
16300     }
16301   else
16302     add_AT_flag (type_die, DW_AT_declaration, 1);
16303
16304   add_pubtype (type, type_die);
16305
16306   return type_die;
16307 }
16308
16309 /* Generate a DIE to represent either a real live formal parameter decl or to
16310    represent just the type of some formal parameter position in some function
16311    type.
16312
16313    Note that this routine is a bit unusual because its argument may be a
16314    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16315    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16316    node.  If it's the former then this function is being called to output a
16317    DIE to represent a formal parameter object (or some inlining thereof).  If
16318    it's the latter, then this function is only being called to output a
16319    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16320    argument type of some subprogram type.
16321    If EMIT_NAME_P is true, name and source coordinate attributes
16322    are emitted.  */
16323
16324 static dw_die_ref
16325 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16326                           dw_die_ref context_die)
16327 {
16328   tree node_or_origin = node ? node : origin;
16329   tree ultimate_origin;
16330   dw_die_ref parm_die
16331     = new_die (DW_TAG_formal_parameter, context_die, node);
16332
16333   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16334     {
16335     case tcc_declaration:
16336       ultimate_origin = decl_ultimate_origin (node_or_origin);
16337       if (node || ultimate_origin)
16338         origin = ultimate_origin;
16339       if (origin != NULL)
16340         add_abstract_origin_attribute (parm_die, origin);
16341       else if (emit_name_p)
16342         add_name_and_src_coords_attributes (parm_die, node);
16343       if (origin == NULL
16344           || (! DECL_ABSTRACT (node_or_origin)
16345               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16346                                            decl_function_context
16347                                                             (node_or_origin))))
16348         {
16349           tree type = TREE_TYPE (node_or_origin);
16350           if (decl_by_reference_p (node_or_origin))
16351             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16352                                 context_die);
16353           else
16354             add_type_attribute (parm_die, type,
16355                                 TREE_READONLY (node_or_origin),
16356                                 TREE_THIS_VOLATILE (node_or_origin),
16357                                 context_die);
16358         }
16359       if (origin == NULL && DECL_ARTIFICIAL (node))
16360         add_AT_flag (parm_die, DW_AT_artificial, 1);
16361
16362       if (node && node != origin)
16363         equate_decl_number_to_die (node, parm_die);
16364       if (! DECL_ABSTRACT (node_or_origin))
16365         add_location_or_const_value_attribute (parm_die, node_or_origin,
16366                                                node == NULL, DW_AT_location);
16367
16368       break;
16369
16370     case tcc_type:
16371       /* We were called with some kind of a ..._TYPE node.  */
16372       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16373       break;
16374
16375     default:
16376       gcc_unreachable ();
16377     }
16378
16379   return parm_die;
16380 }
16381
16382 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16383    children DW_TAG_formal_parameter DIEs representing the arguments of the
16384    parameter pack.
16385
16386    PARM_PACK must be a function parameter pack.
16387    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16388    must point to the subsequent arguments of the function PACK_ARG belongs to.
16389    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16390    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16391    following the last one for which a DIE was generated.  */
16392
16393 static dw_die_ref
16394 gen_formal_parameter_pack_die  (tree parm_pack,
16395                                 tree pack_arg,
16396                                 dw_die_ref subr_die,
16397                                 tree *next_arg)
16398 {
16399   tree arg;
16400   dw_die_ref parm_pack_die;
16401
16402   gcc_assert (parm_pack
16403               && lang_hooks.function_parameter_pack_p (parm_pack)
16404               && subr_die);
16405
16406   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16407   add_src_coords_attributes (parm_pack_die, parm_pack);
16408
16409   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16410     {
16411       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16412                                                                  parm_pack))
16413         break;
16414       gen_formal_parameter_die (arg, NULL,
16415                                 false /* Don't emit name attribute.  */,
16416                                 parm_pack_die);
16417     }
16418   if (next_arg)
16419     *next_arg = arg;
16420   return parm_pack_die;
16421 }
16422
16423 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16424    at the end of an (ANSI prototyped) formal parameters list.  */
16425
16426 static void
16427 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16428 {
16429   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16430 }
16431
16432 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16433    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16434    parameters as specified in some function type specification (except for
16435    those which appear as part of a function *definition*).  */
16436
16437 static void
16438 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16439 {
16440   tree link;
16441   tree formal_type = NULL;
16442   tree first_parm_type;
16443   tree arg;
16444
16445   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16446     {
16447       arg = DECL_ARGUMENTS (function_or_method_type);
16448       function_or_method_type = TREE_TYPE (function_or_method_type);
16449     }
16450   else
16451     arg = NULL_TREE;
16452
16453   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16454
16455   /* Make our first pass over the list of formal parameter types and output a
16456      DW_TAG_formal_parameter DIE for each one.  */
16457   for (link = first_parm_type; link; )
16458     {
16459       dw_die_ref parm_die;
16460
16461       formal_type = TREE_VALUE (link);
16462       if (formal_type == void_type_node)
16463         break;
16464
16465       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16466       parm_die = gen_formal_parameter_die (formal_type, NULL,
16467                                            true /* Emit name attribute.  */,
16468                                            context_die);
16469       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16470           && link == first_parm_type)
16471         {
16472           add_AT_flag (parm_die, DW_AT_artificial, 1);
16473           if (dwarf_version >= 3 || !dwarf_strict)
16474             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16475         }
16476       else if (arg && DECL_ARTIFICIAL (arg))
16477         add_AT_flag (parm_die, DW_AT_artificial, 1);
16478
16479       link = TREE_CHAIN (link);
16480       if (arg)
16481         arg = DECL_CHAIN (arg);
16482     }
16483
16484   /* If this function type has an ellipsis, add a
16485      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16486   if (formal_type != void_type_node)
16487     gen_unspecified_parameters_die (function_or_method_type, context_die);
16488
16489   /* Make our second (and final) pass over the list of formal parameter types
16490      and output DIEs to represent those types (as necessary).  */
16491   for (link = TYPE_ARG_TYPES (function_or_method_type);
16492        link && TREE_VALUE (link);
16493        link = TREE_CHAIN (link))
16494     gen_type_die (TREE_VALUE (link), context_die);
16495 }
16496
16497 /* We want to generate the DIE for TYPE so that we can generate the
16498    die for MEMBER, which has been defined; we will need to refer back
16499    to the member declaration nested within TYPE.  If we're trying to
16500    generate minimal debug info for TYPE, processing TYPE won't do the
16501    trick; we need to attach the member declaration by hand.  */
16502
16503 static void
16504 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16505 {
16506   gen_type_die (type, context_die);
16507
16508   /* If we're trying to avoid duplicate debug info, we may not have
16509      emitted the member decl for this function.  Emit it now.  */
16510   if (TYPE_STUB_DECL (type)
16511       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16512       && ! lookup_decl_die (member))
16513     {
16514       dw_die_ref type_die;
16515       gcc_assert (!decl_ultimate_origin (member));
16516
16517       push_decl_scope (type);
16518       type_die = lookup_type_die_strip_naming_typedef (type);
16519       if (TREE_CODE (member) == FUNCTION_DECL)
16520         gen_subprogram_die (member, type_die);
16521       else if (TREE_CODE (member) == FIELD_DECL)
16522         {
16523           /* Ignore the nameless fields that are used to skip bits but handle
16524              C++ anonymous unions and structs.  */
16525           if (DECL_NAME (member) != NULL_TREE
16526               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16527               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16528             {
16529               gen_type_die (member_declared_type (member), type_die);
16530               gen_field_die (member, type_die);
16531             }
16532         }
16533       else
16534         gen_variable_die (member, NULL_TREE, type_die);
16535
16536       pop_decl_scope ();
16537     }
16538 }
16539 \f
16540 /* Forward declare these functions, because they are mutually recursive
16541   with their set_block_* pairing functions.  */
16542 static void set_decl_origin_self (tree);
16543 static void set_decl_abstract_flags (tree, int);
16544
16545 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
16546    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
16547    that it points to the node itself, thus indicating that the node is its
16548    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
16549    the given node is NULL, recursively descend the decl/block tree which
16550    it is the root of, and for each other ..._DECL or BLOCK node contained
16551    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
16552    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
16553    values to point to themselves.  */
16554
16555 static void
16556 set_block_origin_self (tree stmt)
16557 {
16558   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
16559     {
16560       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
16561
16562       {
16563         tree local_decl;
16564
16565         for (local_decl = BLOCK_VARS (stmt);
16566              local_decl != NULL_TREE;
16567              local_decl = DECL_CHAIN (local_decl))
16568           if (! DECL_EXTERNAL (local_decl))
16569             set_decl_origin_self (local_decl);  /* Potential recursion.  */
16570       }
16571
16572       {
16573         tree subblock;
16574
16575         for (subblock = BLOCK_SUBBLOCKS (stmt);
16576              subblock != NULL_TREE;
16577              subblock = BLOCK_CHAIN (subblock))
16578           set_block_origin_self (subblock);     /* Recurse.  */
16579       }
16580     }
16581 }
16582
16583 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
16584    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
16585    node to so that it points to the node itself, thus indicating that the
16586    node represents its own (abstract) origin.  Additionally, if the
16587    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
16588    the decl/block tree of which the given node is the root of, and for
16589    each other ..._DECL or BLOCK node contained therein whose
16590    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
16591    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
16592    point to themselves.  */
16593
16594 static void
16595 set_decl_origin_self (tree decl)
16596 {
16597   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
16598     {
16599       DECL_ABSTRACT_ORIGIN (decl) = decl;
16600       if (TREE_CODE (decl) == FUNCTION_DECL)
16601         {
16602           tree arg;
16603
16604           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16605             DECL_ABSTRACT_ORIGIN (arg) = arg;
16606           if (DECL_INITIAL (decl) != NULL_TREE
16607               && DECL_INITIAL (decl) != error_mark_node)
16608             set_block_origin_self (DECL_INITIAL (decl));
16609         }
16610     }
16611 }
16612 \f
16613 /* Given a pointer to some BLOCK node, and a boolean value to set the
16614    "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
16615    the given block, and for all local decls and all local sub-blocks
16616    (recursively) which are contained therein.  */
16617
16618 static void
16619 set_block_abstract_flags (tree stmt, int setting)
16620 {
16621   tree local_decl;
16622   tree subblock;
16623   unsigned int i;
16624
16625   BLOCK_ABSTRACT (stmt) = setting;
16626
16627   for (local_decl = BLOCK_VARS (stmt);
16628        local_decl != NULL_TREE;
16629        local_decl = DECL_CHAIN (local_decl))
16630     if (! DECL_EXTERNAL (local_decl))
16631       set_decl_abstract_flags (local_decl, setting);
16632
16633   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
16634     {
16635       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
16636       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
16637           || TREE_CODE (local_decl) == PARM_DECL)
16638         set_decl_abstract_flags (local_decl, setting);
16639     }
16640
16641   for (subblock = BLOCK_SUBBLOCKS (stmt);
16642        subblock != NULL_TREE;
16643        subblock = BLOCK_CHAIN (subblock))
16644     set_block_abstract_flags (subblock, setting);
16645 }
16646
16647 /* Given a pointer to some ..._DECL node, and a boolean value to set the
16648    "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
16649    given decl, and (in the case where the decl is a FUNCTION_DECL) also
16650    set the abstract flags for all of the parameters, local vars, local
16651    blocks and sub-blocks (recursively) to the same setting.  */
16652
16653 static void
16654 set_decl_abstract_flags (tree decl, int setting)
16655 {
16656   DECL_ABSTRACT (decl) = setting;
16657   if (TREE_CODE (decl) == FUNCTION_DECL)
16658     {
16659       tree arg;
16660
16661       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
16662         DECL_ABSTRACT (arg) = setting;
16663       if (DECL_INITIAL (decl) != NULL_TREE
16664           && DECL_INITIAL (decl) != error_mark_node)
16665         set_block_abstract_flags (DECL_INITIAL (decl), setting);
16666     }
16667 }
16668
16669 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16670    may later generate inlined and/or out-of-line instances of.  */
16671
16672 static void
16673 dwarf2out_abstract_function (tree decl)
16674 {
16675   dw_die_ref old_die;
16676   tree save_fn;
16677   tree context;
16678   int was_abstract;
16679   htab_t old_decl_loc_table;
16680   htab_t old_cached_dw_loc_list_table;
16681   int old_call_site_count, old_tail_call_site_count;
16682   struct call_arg_loc_node *old_call_arg_locations;
16683
16684   /* Make sure we have the actual abstract inline, not a clone.  */
16685   decl = DECL_ORIGIN (decl);
16686
16687   old_die = lookup_decl_die (decl);
16688   if (old_die && get_AT (old_die, DW_AT_inline))
16689     /* We've already generated the abstract instance.  */
16690     return;
16691
16692   /* We can be called while recursively when seeing block defining inlined subroutine
16693      DIE.  Be sure to not clobber the outer location table nor use it or we would
16694      get locations in abstract instantces.  */
16695   old_decl_loc_table = decl_loc_table;
16696   decl_loc_table = NULL;
16697   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16698   cached_dw_loc_list_table = NULL;
16699   old_call_arg_locations = call_arg_locations;
16700   call_arg_locations = NULL;
16701   old_call_site_count = call_site_count;
16702   call_site_count = -1;
16703   old_tail_call_site_count = tail_call_site_count;
16704   tail_call_site_count = -1;
16705
16706   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16707      we don't get confused by DECL_ABSTRACT.  */
16708   if (debug_info_level > DINFO_LEVEL_TERSE)
16709     {
16710       context = decl_class_context (decl);
16711       if (context)
16712         gen_type_die_for_member
16713           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16714     }
16715
16716   /* Pretend we've just finished compiling this function.  */
16717   save_fn = current_function_decl;
16718   current_function_decl = decl;
16719   push_cfun (DECL_STRUCT_FUNCTION (decl));
16720
16721   was_abstract = DECL_ABSTRACT (decl);
16722   set_decl_abstract_flags (decl, 1);
16723   dwarf2out_decl (decl);
16724   if (! was_abstract)
16725     set_decl_abstract_flags (decl, 0);
16726
16727   current_function_decl = save_fn;
16728   decl_loc_table = old_decl_loc_table;
16729   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16730   call_arg_locations = old_call_arg_locations;
16731   call_site_count = old_call_site_count;
16732   tail_call_site_count = old_tail_call_site_count;
16733   pop_cfun ();
16734 }
16735
16736 /* Helper function of premark_used_types() which gets called through
16737    htab_traverse.
16738
16739    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16740    marked as unused by prune_unused_types.  */
16741
16742 static int
16743 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16744 {
16745   tree type;
16746   dw_die_ref die;
16747
16748   type = (tree) *slot;
16749   die = lookup_type_die (type);
16750   if (die != NULL)
16751     die->die_perennial_p = 1;
16752   return 1;
16753 }
16754
16755 /* Helper function of premark_types_used_by_global_vars which gets called
16756    through htab_traverse.
16757
16758    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16759    marked as unused by prune_unused_types. The DIE of the type is marked
16760    only if the global variable using the type will actually be emitted.  */
16761
16762 static int
16763 premark_types_used_by_global_vars_helper (void **slot,
16764                                           void *data ATTRIBUTE_UNUSED)
16765 {
16766   struct types_used_by_vars_entry *entry;
16767   dw_die_ref die;
16768
16769   entry = (struct types_used_by_vars_entry *) *slot;
16770   gcc_assert (entry->type != NULL
16771               && entry->var_decl != NULL);
16772   die = lookup_type_die (entry->type);
16773   if (die)
16774     {
16775       /* Ask cgraph if the global variable really is to be emitted.
16776          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
16777       struct varpool_node *node = varpool_get_node (entry->var_decl);
16778       if (node && node->analyzed)
16779         {
16780           die->die_perennial_p = 1;
16781           /* Keep the parent DIEs as well.  */
16782           while ((die = die->die_parent) && die->die_perennial_p == 0)
16783             die->die_perennial_p = 1;
16784         }
16785     }
16786   return 1;
16787 }
16788
16789 /* Mark all members of used_types_hash as perennial.  */
16790
16791 static void
16792 premark_used_types (void)
16793 {
16794   if (cfun && cfun->used_types_hash)
16795     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
16796 }
16797
16798 /* Mark all members of types_used_by_vars_entry as perennial.  */
16799
16800 static void
16801 premark_types_used_by_global_vars (void)
16802 {
16803   if (types_used_by_vars_hash)
16804     htab_traverse (types_used_by_vars_hash,
16805                    premark_types_used_by_global_vars_helper, NULL);
16806 }
16807
16808 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
16809    for CA_LOC call arg loc node.  */
16810
16811 static dw_die_ref
16812 gen_call_site_die (tree decl, dw_die_ref subr_die,
16813                    struct call_arg_loc_node *ca_loc)
16814 {
16815   dw_die_ref stmt_die = NULL, die;
16816   tree block = ca_loc->block;
16817
16818   while (block
16819          && block != DECL_INITIAL (decl)
16820          && TREE_CODE (block) == BLOCK)
16821     {
16822       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
16823         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
16824       if (stmt_die)
16825         break;
16826       block = BLOCK_SUPERCONTEXT (block);
16827     }
16828   if (stmt_die == NULL)
16829     stmt_die = subr_die;
16830   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
16831   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
16832   if (ca_loc->tail_call_p)
16833     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
16834   if (ca_loc->symbol_ref)
16835     {
16836       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
16837       if (tdie)
16838         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
16839       else
16840         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
16841     }
16842   return die;
16843 }
16844
16845 /* Generate a DIE to represent a declared function (either file-scope or
16846    block-local).  */
16847
16848 static void
16849 gen_subprogram_die (tree decl, dw_die_ref context_die)
16850 {
16851   tree origin = decl_ultimate_origin (decl);
16852   dw_die_ref subr_die;
16853   tree outer_scope;
16854   dw_die_ref old_die = lookup_decl_die (decl);
16855   int declaration = (current_function_decl != decl
16856                      || class_or_namespace_scope_p (context_die));
16857
16858   premark_used_types ();
16859
16860   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
16861      started to generate the abstract instance of an inline, decided to output
16862      its containing class, and proceeded to emit the declaration of the inline
16863      from the member list for the class.  If so, DECLARATION takes priority;
16864      we'll get back to the abstract instance when done with the class.  */
16865
16866   /* The class-scope declaration DIE must be the primary DIE.  */
16867   if (origin && declaration && class_or_namespace_scope_p (context_die))
16868     {
16869       origin = NULL;
16870       gcc_assert (!old_die);
16871     }
16872
16873   /* Now that the C++ front end lazily declares artificial member fns, we
16874      might need to retrofit the declaration into its class.  */
16875   if (!declaration && !origin && !old_die
16876       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
16877       && !class_or_namespace_scope_p (context_die)
16878       && debug_info_level > DINFO_LEVEL_TERSE)
16879     old_die = force_decl_die (decl);
16880
16881   if (origin != NULL)
16882     {
16883       gcc_assert (!declaration || local_scope_p (context_die));
16884
16885       /* Fixup die_parent for the abstract instance of a nested
16886          inline function.  */
16887       if (old_die && old_die->die_parent == NULL)
16888         add_child_die (context_die, old_die);
16889
16890       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16891       add_abstract_origin_attribute (subr_die, origin);
16892       /*  This is where the actual code for a cloned function is.
16893           Let's emit linkage name attribute for it.  This helps
16894           debuggers to e.g, set breakpoints into
16895           constructors/destructors when the user asks "break
16896           K::K".  */
16897       add_linkage_name (subr_die, decl);
16898     }
16899   else if (old_die)
16900     {
16901       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16902       struct dwarf_file_data * file_index = lookup_filename (s.file);
16903
16904       if (!get_AT_flag (old_die, DW_AT_declaration)
16905           /* We can have a normal definition following an inline one in the
16906              case of redefinition of GNU C extern inlines.
16907              It seems reasonable to use AT_specification in this case.  */
16908           && !get_AT (old_die, DW_AT_inline))
16909         {
16910           /* Detect and ignore this case, where we are trying to output
16911              something we have already output.  */
16912           return;
16913         }
16914
16915       /* If the definition comes from the same place as the declaration,
16916          maybe use the old DIE.  We always want the DIE for this function
16917          that has the *_pc attributes to be under comp_unit_die so the
16918          debugger can find it.  We also need to do this for abstract
16919          instances of inlines, since the spec requires the out-of-line copy
16920          to have the same parent.  For local class methods, this doesn't
16921          apply; we just use the old DIE.  */
16922       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
16923           && (DECL_ARTIFICIAL (decl)
16924               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
16925                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
16926                       == (unsigned) s.line))))
16927         {
16928           subr_die = old_die;
16929
16930           /* Clear out the declaration attribute and the formal parameters.
16931              Do not remove all children, because it is possible that this
16932              declaration die was forced using force_decl_die(). In such
16933              cases die that forced declaration die (e.g. TAG_imported_module)
16934              is one of the children that we do not want to remove.  */
16935           remove_AT (subr_die, DW_AT_declaration);
16936           remove_AT (subr_die, DW_AT_object_pointer);
16937           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
16938         }
16939       else
16940         {
16941           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16942           add_AT_specification (subr_die, old_die);
16943           add_pubname (decl, subr_die);
16944           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
16945             add_AT_file (subr_die, DW_AT_decl_file, file_index);
16946           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
16947             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
16948         }
16949     }
16950   else
16951     {
16952       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
16953
16954       if (TREE_PUBLIC (decl))
16955         add_AT_flag (subr_die, DW_AT_external, 1);
16956
16957       add_name_and_src_coords_attributes (subr_die, decl);
16958       add_pubname (decl, subr_die);
16959       if (debug_info_level > DINFO_LEVEL_TERSE)
16960         {
16961           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
16962           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
16963                               0, 0, context_die);
16964         }
16965
16966       add_pure_or_virtual_attribute (subr_die, decl);
16967       if (DECL_ARTIFICIAL (decl))
16968         add_AT_flag (subr_die, DW_AT_artificial, 1);
16969
16970       add_accessibility_attribute (subr_die, decl);
16971     }
16972
16973   if (declaration)
16974     {
16975       if (!old_die || !get_AT (old_die, DW_AT_inline))
16976         {
16977           add_AT_flag (subr_die, DW_AT_declaration, 1);
16978
16979           /* If this is an explicit function declaration then generate
16980              a DW_AT_explicit attribute.  */
16981           if (lang_hooks.decls.function_decl_explicit_p (decl)
16982               && (dwarf_version >= 3 || !dwarf_strict))
16983             add_AT_flag (subr_die, DW_AT_explicit, 1);
16984
16985           /* The first time we see a member function, it is in the context of
16986              the class to which it belongs.  We make sure of this by emitting
16987              the class first.  The next time is the definition, which is
16988              handled above.  The two may come from the same source text.
16989
16990              Note that force_decl_die() forces function declaration die. It is
16991              later reused to represent definition.  */
16992           equate_decl_number_to_die (decl, subr_die);
16993         }
16994     }
16995   else if (DECL_ABSTRACT (decl))
16996     {
16997       if (DECL_DECLARED_INLINE_P (decl))
16998         {
16999           if (cgraph_function_possibly_inlined_p (decl))
17000             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17001           else
17002             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17003         }
17004       else
17005         {
17006           if (cgraph_function_possibly_inlined_p (decl))
17007             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17008           else
17009             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17010         }
17011
17012       if (DECL_DECLARED_INLINE_P (decl)
17013           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17014         add_AT_flag (subr_die, DW_AT_artificial, 1);
17015
17016       equate_decl_number_to_die (decl, subr_die);
17017     }
17018   else if (!DECL_EXTERNAL (decl))
17019     {
17020       HOST_WIDE_INT cfa_fb_offset;
17021
17022       if (!old_die || !get_AT (old_die, DW_AT_inline))
17023         equate_decl_number_to_die (decl, subr_die);
17024
17025       if (!flag_reorder_blocks_and_partition)
17026         {
17027           dw_fde_ref fde = cfun->fde;
17028           if (fde->dw_fde_begin)
17029             {
17030               /* We have already generated the labels.  */
17031               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17032               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17033             }
17034           else
17035             {
17036               /* Create start/end labels and add the range.  */
17037               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17038               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17039                                            current_function_funcdef_no);
17040               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17041               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17042                                            current_function_funcdef_no);
17043               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17044             }
17045
17046 #if VMS_DEBUGGING_INFO
17047       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17048          Section 2.3 Prologue and Epilogue Attributes:
17049          When a breakpoint is set on entry to a function, it is generally
17050          desirable for execution to be suspended, not on the very first
17051          instruction of the function, but rather at a point after the
17052          function's frame has been set up, after any language defined local
17053          declaration processing has been completed, and before execution of
17054          the first statement of the function begins. Debuggers generally
17055          cannot properly determine where this point is.  Similarly for a
17056          breakpoint set on exit from a function. The prologue and epilogue
17057          attributes allow a compiler to communicate the location(s) to use.  */
17058
17059       {
17060         if (fde->dw_fde_vms_end_prologue)
17061           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17062             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17063
17064         if (fde->dw_fde_vms_begin_epilogue)
17065           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17066             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17067       }
17068 #endif
17069
17070         }
17071       else
17072         {
17073           /* Generate pubnames entries for the split function code ranges.  */
17074           dw_fde_ref fde = cfun->fde;
17075
17076           if (fde->dw_fde_second_begin)
17077             {
17078               if (dwarf_version >= 3 || !dwarf_strict)
17079                 {
17080                   /* We should use ranges for non-contiguous code section 
17081                      addresses.  Use the actual code range for the initial
17082                      section, since the HOT/COLD labels might precede an 
17083                      alignment offset.  */
17084                   bool range_list_added = false;
17085                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17086                                         fde->dw_fde_end, &range_list_added);
17087                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17088                                         fde->dw_fde_second_end,
17089                                         &range_list_added);
17090                   if (range_list_added)
17091                     add_ranges (NULL);
17092                 }
17093               else
17094                 {
17095                   /* There is no real support in DW2 for this .. so we make
17096                      a work-around.  First, emit the pub name for the segment
17097                      containing the function label.  Then make and emit a
17098                      simplified subprogram DIE for the second segment with the
17099                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17100                      linkage name for the second die so that gdb will find both
17101                      sections when given "b foo".  */
17102                   const char *name = NULL;
17103                   tree decl_name = DECL_NAME (decl);
17104                   dw_die_ref seg_die;
17105
17106                   /* Do the 'primary' section.   */
17107                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17108                                  fde->dw_fde_begin);
17109                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17110                                  fde->dw_fde_end);
17111
17112                   /* Build a minimal DIE for the secondary section.  */
17113                   seg_die = new_die (DW_TAG_subprogram,
17114                                      subr_die->die_parent, decl);
17115
17116                   if (TREE_PUBLIC (decl))
17117                     add_AT_flag (seg_die, DW_AT_external, 1);
17118
17119                   if (decl_name != NULL 
17120                       && IDENTIFIER_POINTER (decl_name) != NULL)
17121                     {
17122                       name = dwarf2_name (decl, 1);
17123                       if (! DECL_ARTIFICIAL (decl))
17124                         add_src_coords_attributes (seg_die, decl);
17125
17126                       add_linkage_name (seg_die, decl);
17127                     }
17128                   gcc_assert (name != NULL);
17129                   add_pure_or_virtual_attribute (seg_die, decl);
17130                   if (DECL_ARTIFICIAL (decl))
17131                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17132
17133                   name = concat ("__second_sect_of_", name, NULL); 
17134                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17135                                  fde->dw_fde_second_begin);
17136                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17137                                  fde->dw_fde_second_end);
17138                   add_name_attribute (seg_die, name);
17139                   if (want_pubnames ())
17140                     add_pubname_string (name, seg_die);
17141                 }
17142             }
17143           else
17144             {
17145               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17146               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17147             }
17148         }
17149
17150       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17151
17152       /* We define the "frame base" as the function's CFA.  This is more
17153          convenient for several reasons: (1) It's stable across the prologue
17154          and epilogue, which makes it better than just a frame pointer,
17155          (2) With dwarf3, there exists a one-byte encoding that allows us
17156          to reference the .debug_frame data by proxy, but failing that,
17157          (3) We can at least reuse the code inspection and interpretation
17158          code that determines the CFA position at various points in the
17159          function.  */
17160       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17161         {
17162           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17163           add_AT_loc (subr_die, DW_AT_frame_base, op);
17164         }
17165       else
17166         {
17167           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17168           if (list->dw_loc_next)
17169             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17170           else
17171             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17172         }
17173
17174       /* Compute a displacement from the "steady-state frame pointer" to
17175          the CFA.  The former is what all stack slots and argument slots
17176          will reference in the rtl; the later is what we've told the
17177          debugger about.  We'll need to adjust all frame_base references
17178          by this displacement.  */
17179       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17180
17181       if (cfun->static_chain_decl)
17182         add_AT_location_description (subr_die, DW_AT_static_link,
17183                  loc_list_from_tree (cfun->static_chain_decl, 2));
17184     }
17185
17186   /* Generate child dies for template paramaters.  */
17187   if (debug_info_level > DINFO_LEVEL_TERSE)
17188     gen_generic_params_dies (decl);
17189
17190   /* Now output descriptions of the arguments for this function. This gets
17191      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17192      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17193      `...' at the end of the formal parameter list.  In order to find out if
17194      there was a trailing ellipsis or not, we must instead look at the type
17195      associated with the FUNCTION_DECL.  This will be a node of type
17196      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17197      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17198      an ellipsis at the end.  */
17199
17200   /* In the case where we are describing a mere function declaration, all we
17201      need to do here (and all we *can* do here) is to describe the *types* of
17202      its formal parameters.  */
17203   if (debug_info_level <= DINFO_LEVEL_TERSE)
17204     ;
17205   else if (declaration)
17206     gen_formal_types_die (decl, subr_die);
17207   else
17208     {
17209       /* Generate DIEs to represent all known formal parameters.  */
17210       tree parm = DECL_ARGUMENTS (decl);
17211       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17212       tree generic_decl_parm = generic_decl
17213                                 ? DECL_ARGUMENTS (generic_decl)
17214                                 : NULL;
17215
17216       /* Now we want to walk the list of parameters of the function and
17217          emit their relevant DIEs.
17218
17219          We consider the case of DECL being an instance of a generic function
17220          as well as it being a normal function.
17221
17222          If DECL is an instance of a generic function we walk the
17223          parameters of the generic function declaration _and_ the parameters of
17224          DECL itself. This is useful because we want to emit specific DIEs for
17225          function parameter packs and those are declared as part of the
17226          generic function declaration. In that particular case,
17227          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17228          That DIE has children DIEs representing the set of arguments
17229          of the pack. Note that the set of pack arguments can be empty.
17230          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17231          children DIE.
17232
17233          Otherwise, we just consider the parameters of DECL.  */
17234       while (generic_decl_parm || parm)
17235         {
17236           if (generic_decl_parm
17237               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17238             gen_formal_parameter_pack_die (generic_decl_parm,
17239                                            parm, subr_die,
17240                                            &parm);
17241           else if (parm)
17242             {
17243               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17244
17245               if (parm == DECL_ARGUMENTS (decl)
17246                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17247                   && parm_die
17248                   && (dwarf_version >= 3 || !dwarf_strict))
17249                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17250
17251               parm = DECL_CHAIN (parm);
17252             }
17253
17254           if (generic_decl_parm)
17255             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17256         }
17257
17258       /* Decide whether we need an unspecified_parameters DIE at the end.
17259          There are 2 more cases to do this for: 1) the ansi ... declaration -
17260          this is detectable when the end of the arg list is not a
17261          void_type_node 2) an unprototyped function declaration (not a
17262          definition).  This just means that we have no info about the
17263          parameters at all.  */
17264       if (prototype_p (TREE_TYPE (decl)))
17265         {
17266           /* This is the prototyped case, check for....  */
17267           if (stdarg_p (TREE_TYPE (decl)))
17268             gen_unspecified_parameters_die (decl, subr_die);
17269         }
17270       else if (DECL_INITIAL (decl) == NULL_TREE)
17271         gen_unspecified_parameters_die (decl, subr_die);
17272     }
17273
17274   /* Output Dwarf info for all of the stuff within the body of the function
17275      (if it has one - it may be just a declaration).  */
17276   outer_scope = DECL_INITIAL (decl);
17277
17278   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17279      a function.  This BLOCK actually represents the outermost binding contour
17280      for the function, i.e. the contour in which the function's formal
17281      parameters and labels get declared. Curiously, it appears that the front
17282      end doesn't actually put the PARM_DECL nodes for the current function onto
17283      the BLOCK_VARS list for this outer scope, but are strung off of the
17284      DECL_ARGUMENTS list for the function instead.
17285
17286      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17287      the LABEL_DECL nodes for the function however, and we output DWARF info
17288      for those in decls_for_scope.  Just within the `outer_scope' there will be
17289      a BLOCK node representing the function's outermost pair of curly braces,
17290      and any blocks used for the base and member initializers of a C++
17291      constructor function.  */
17292   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17293     {
17294       int call_site_note_count = 0;
17295       int tail_call_site_note_count = 0;
17296
17297       /* Emit a DW_TAG_variable DIE for a named return value.  */
17298       if (DECL_NAME (DECL_RESULT (decl)))
17299         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17300
17301       current_function_has_inlines = 0;
17302       decls_for_scope (outer_scope, subr_die, 0);
17303
17304       if (call_arg_locations && !dwarf_strict)
17305         {
17306           struct call_arg_loc_node *ca_loc;
17307           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17308             {
17309               dw_die_ref die = NULL;
17310               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17311               rtx arg, next_arg;
17312
17313               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17314                    arg; arg = next_arg)
17315                 {
17316                   dw_loc_descr_ref reg, val;
17317                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17318                   dw_die_ref cdie, tdie = NULL;
17319
17320                   next_arg = XEXP (arg, 1);
17321                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17322                       && next_arg
17323                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17324                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17325                       && REGNO (XEXP (XEXP (arg, 0), 0))
17326                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17327                     next_arg = XEXP (next_arg, 1);
17328                   if (mode == VOIDmode)
17329                     {
17330                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17331                       if (mode == VOIDmode)
17332                         mode = GET_MODE (XEXP (arg, 0));
17333                     }
17334                   if (mode == VOIDmode || mode == BLKmode)
17335                     continue;
17336                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17337                     {
17338                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17339                       tloc = XEXP (XEXP (arg, 0), 1);
17340                       continue;
17341                     }
17342                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17343                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17344                     {
17345                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17346                       tlocc = XEXP (XEXP (arg, 0), 1);
17347                       continue;
17348                     }
17349                   reg = NULL;
17350                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17351                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17352                                               VAR_INIT_STATUS_INITIALIZED);
17353                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17354                     {
17355                       rtx mem = XEXP (XEXP (arg, 0), 0);
17356                       reg = mem_loc_descriptor (XEXP (mem, 0),
17357                                                 get_address_mode (mem),
17358                                                 GET_MODE (mem),
17359                                                 VAR_INIT_STATUS_INITIALIZED);
17360                     }
17361                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17362                            == DEBUG_PARAMETER_REF)
17363                     {
17364                       tree tdecl
17365                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17366                       tdie = lookup_decl_die (tdecl);
17367                       if (tdie == NULL)
17368                         continue;
17369                     }
17370                   else
17371                     continue;
17372                   if (reg == NULL
17373                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17374                          != DEBUG_PARAMETER_REF)
17375                     continue;
17376                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17377                                             VOIDmode,
17378                                             VAR_INIT_STATUS_INITIALIZED);
17379                   if (val == NULL)
17380                     continue;
17381                   if (die == NULL)
17382                     die = gen_call_site_die (decl, subr_die, ca_loc);
17383                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17384                                   NULL_TREE);
17385                   if (reg != NULL)
17386                     add_AT_loc (cdie, DW_AT_location, reg);
17387                   else if (tdie != NULL)
17388                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17389                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17390                   if (next_arg != XEXP (arg, 1))
17391                     {
17392                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17393                       if (mode == VOIDmode)
17394                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17395                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17396                                                             0), 1),
17397                                                 mode, VOIDmode,
17398                                                 VAR_INIT_STATUS_INITIALIZED);
17399                       if (val != NULL)
17400                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17401                     }
17402                 }
17403               if (die == NULL
17404                   && (ca_loc->symbol_ref || tloc))
17405                 die = gen_call_site_die (decl, subr_die, ca_loc);
17406               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17407                 {
17408                   dw_loc_descr_ref tval = NULL;
17409
17410                   if (tloc != NULL_RTX)
17411                     tval = mem_loc_descriptor (tloc,
17412                                                GET_MODE (tloc) == VOIDmode
17413                                                ? Pmode : GET_MODE (tloc),
17414                                                VOIDmode,
17415                                                VAR_INIT_STATUS_INITIALIZED);
17416                   if (tval)
17417                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17418                   else if (tlocc != NULL_RTX)
17419                     {
17420                       tval = mem_loc_descriptor (tlocc,
17421                                                  GET_MODE (tlocc) == VOIDmode
17422                                                  ? Pmode : GET_MODE (tlocc),
17423                                                  VOIDmode,
17424                                                  VAR_INIT_STATUS_INITIALIZED);
17425                       if (tval)
17426                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17427                                     tval);
17428                     }
17429                 }
17430               if (die != NULL)
17431                 {
17432                   call_site_note_count++;
17433                   if (ca_loc->tail_call_p)
17434                     tail_call_site_note_count++;
17435                 }
17436             }
17437         }
17438       call_arg_locations = NULL;
17439       call_arg_loc_last = NULL;
17440       if (tail_call_site_count >= 0
17441           && tail_call_site_count == tail_call_site_note_count
17442           && !dwarf_strict)
17443         {
17444           if (call_site_count >= 0
17445               && call_site_count == call_site_note_count)
17446             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17447           else
17448             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17449         }
17450       call_site_count = -1;
17451       tail_call_site_count = -1;
17452     }
17453   /* Add the calling convention attribute if requested.  */
17454   add_calling_convention_attribute (subr_die, decl);
17455
17456 }
17457
17458 /* Returns a hash value for X (which really is a die_struct).  */
17459
17460 static hashval_t
17461 common_block_die_table_hash (const void *x)
17462 {
17463   const_dw_die_ref d = (const_dw_die_ref) x;
17464   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17465 }
17466
17467 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17468    as decl_id and die_parent of die_struct Y.  */
17469
17470 static int
17471 common_block_die_table_eq (const void *x, const void *y)
17472 {
17473   const_dw_die_ref d = (const_dw_die_ref) x;
17474   const_dw_die_ref e = (const_dw_die_ref) y;
17475   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17476 }
17477
17478 /* Generate a DIE to represent a declared data object.
17479    Either DECL or ORIGIN must be non-null.  */
17480
17481 static void
17482 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17483 {
17484   HOST_WIDE_INT off = 0;
17485   tree com_decl;
17486   tree decl_or_origin = decl ? decl : origin;
17487   tree ultimate_origin;
17488   dw_die_ref var_die;
17489   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17490   dw_die_ref origin_die;
17491   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17492                       || class_or_namespace_scope_p (context_die));
17493   bool specialization_p = false;
17494
17495   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17496   if (decl || ultimate_origin)
17497     origin = ultimate_origin;
17498   com_decl = fortran_common (decl_or_origin, &off);
17499
17500   /* Symbol in common gets emitted as a child of the common block, in the form
17501      of a data member.  */
17502   if (com_decl)
17503     {
17504       dw_die_ref com_die;
17505       dw_loc_list_ref loc;
17506       die_node com_die_arg;
17507
17508       var_die = lookup_decl_die (decl_or_origin);
17509       if (var_die)
17510         {
17511           if (get_AT (var_die, DW_AT_location) == NULL)
17512             {
17513               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17514               if (loc)
17515                 {
17516                   if (off)
17517                     {
17518                       /* Optimize the common case.  */
17519                       if (single_element_loc_list_p (loc)
17520                           && loc->expr->dw_loc_opc == DW_OP_addr
17521                           && loc->expr->dw_loc_next == NULL
17522                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17523                              == SYMBOL_REF)
17524                         {
17525                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17526                           loc->expr->dw_loc_oprnd1.v.val_addr
17527                             = plus_constant (GET_MODE (x), x , off);
17528                         }
17529                       else
17530                         loc_list_plus_const (loc, off);
17531                     }
17532                   add_AT_location_description (var_die, DW_AT_location, loc);
17533                   remove_AT (var_die, DW_AT_declaration);
17534                 }
17535             }
17536           return;
17537         }
17538
17539       if (common_block_die_table == NULL)
17540         common_block_die_table
17541           = htab_create_ggc (10, common_block_die_table_hash,
17542                              common_block_die_table_eq, NULL);
17543
17544       com_die_arg.decl_id = DECL_UID (com_decl);
17545       com_die_arg.die_parent = context_die;
17546       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17547       loc = loc_list_from_tree (com_decl, 2);
17548       if (com_die == NULL)
17549         {
17550           const char *cnam
17551             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17552           void **slot;
17553
17554           com_die = new_die (DW_TAG_common_block, context_die, decl);
17555           add_name_and_src_coords_attributes (com_die, com_decl);
17556           if (loc)
17557             {
17558               add_AT_location_description (com_die, DW_AT_location, loc);
17559               /* Avoid sharing the same loc descriptor between
17560                  DW_TAG_common_block and DW_TAG_variable.  */
17561               loc = loc_list_from_tree (com_decl, 2);
17562             }
17563           else if (DECL_EXTERNAL (decl))
17564             add_AT_flag (com_die, DW_AT_declaration, 1);
17565           if (want_pubnames ())
17566             add_pubname_string (cnam, com_die); /* ??? needed? */
17567           com_die->decl_id = DECL_UID (com_decl);
17568           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17569           *slot = (void *) com_die;
17570         }
17571       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17572         {
17573           add_AT_location_description (com_die, DW_AT_location, loc);
17574           loc = loc_list_from_tree (com_decl, 2);
17575           remove_AT (com_die, DW_AT_declaration);
17576         }
17577       var_die = new_die (DW_TAG_variable, com_die, decl);
17578       add_name_and_src_coords_attributes (var_die, decl);
17579       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17580                           TREE_THIS_VOLATILE (decl), context_die);
17581       add_AT_flag (var_die, DW_AT_external, 1);
17582       if (loc)
17583         {
17584           if (off)
17585             {
17586               /* Optimize the common case.  */
17587               if (single_element_loc_list_p (loc)
17588                   && loc->expr->dw_loc_opc == DW_OP_addr
17589                   && loc->expr->dw_loc_next == NULL
17590                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17591                 {
17592                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
17593                   loc->expr->dw_loc_oprnd1.v.val_addr
17594                     = plus_constant (GET_MODE (x), x, off);
17595                 }
17596               else
17597                 loc_list_plus_const (loc, off);
17598             }
17599           add_AT_location_description (var_die, DW_AT_location, loc);
17600         }
17601       else if (DECL_EXTERNAL (decl))
17602         add_AT_flag (var_die, DW_AT_declaration, 1);
17603       equate_decl_number_to_die (decl, var_die);
17604       return;
17605     }
17606
17607   /* If the compiler emitted a definition for the DECL declaration
17608      and if we already emitted a DIE for it, don't emit a second
17609      DIE for it again. Allow re-declarations of DECLs that are
17610      inside functions, though.  */
17611   if (old_die && declaration && !local_scope_p (context_die))
17612     return;
17613
17614   /* For static data members, the declaration in the class is supposed
17615      to have DW_TAG_member tag; the specification should still be
17616      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17617   if (declaration && class_scope_p (context_die))
17618     var_die = new_die (DW_TAG_member, context_die, decl);
17619   else
17620     var_die = new_die (DW_TAG_variable, context_die, decl);
17621
17622   origin_die = NULL;
17623   if (origin != NULL)
17624     origin_die = add_abstract_origin_attribute (var_die, origin);
17625
17626   /* Loop unrolling can create multiple blocks that refer to the same
17627      static variable, so we must test for the DW_AT_declaration flag.
17628
17629      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17630      copy decls and set the DECL_ABSTRACT flag on them instead of
17631      sharing them.
17632
17633      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17634
17635      ??? The declare_in_namespace support causes us to get two DIEs for one
17636      variable, both of which are declarations.  We want to avoid considering
17637      one to be a specification, so we must test that this DIE is not a
17638      declaration.  */
17639   else if (old_die && TREE_STATIC (decl) && ! declaration
17640            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17641     {
17642       /* This is a definition of a C++ class level static.  */
17643       add_AT_specification (var_die, old_die);
17644       specialization_p = true;
17645       if (DECL_NAME (decl))
17646         {
17647           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17648           struct dwarf_file_data * file_index = lookup_filename (s.file);
17649
17650           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17651             add_AT_file (var_die, DW_AT_decl_file, file_index);
17652
17653           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17654             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17655
17656           if (old_die->die_tag == DW_TAG_member)
17657             add_linkage_name (var_die, decl);
17658         }
17659     }
17660   else
17661     add_name_and_src_coords_attributes (var_die, decl);
17662
17663   if ((origin == NULL && !specialization_p)
17664       || (origin != NULL
17665           && !DECL_ABSTRACT (decl_or_origin)
17666           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17667                                        decl_function_context
17668                                                         (decl_or_origin))))
17669     {
17670       tree type = TREE_TYPE (decl_or_origin);
17671
17672       if (decl_by_reference_p (decl_or_origin))
17673         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17674       else
17675         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17676                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17677     }
17678
17679   if (origin == NULL && !specialization_p)
17680     {
17681       if (TREE_PUBLIC (decl))
17682         add_AT_flag (var_die, DW_AT_external, 1);
17683
17684       if (DECL_ARTIFICIAL (decl))
17685         add_AT_flag (var_die, DW_AT_artificial, 1);
17686
17687       add_accessibility_attribute (var_die, decl);
17688     }
17689
17690   if (declaration)
17691     add_AT_flag (var_die, DW_AT_declaration, 1);
17692
17693   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17694     equate_decl_number_to_die (decl, var_die);
17695
17696   if (! declaration
17697       && (! DECL_ABSTRACT (decl_or_origin)
17698           /* Local static vars are shared between all clones/inlines,
17699              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17700              already set.  */
17701           || (TREE_CODE (decl_or_origin) == VAR_DECL
17702               && TREE_STATIC (decl_or_origin)
17703               && DECL_RTL_SET_P (decl_or_origin)))
17704       /* When abstract origin already has DW_AT_location attribute, no need
17705          to add it again.  */
17706       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17707     {
17708       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17709           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17710         defer_location (decl_or_origin, var_die);
17711       else
17712         add_location_or_const_value_attribute (var_die, decl_or_origin,
17713                                                decl == NULL, DW_AT_location);
17714       add_pubname (decl_or_origin, var_die);
17715     }
17716   else
17717     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17718 }
17719
17720 /* Generate a DIE to represent a named constant.  */
17721
17722 static void
17723 gen_const_die (tree decl, dw_die_ref context_die)
17724 {
17725   dw_die_ref const_die;
17726   tree type = TREE_TYPE (decl);
17727
17728   const_die = new_die (DW_TAG_constant, context_die, decl);
17729   add_name_and_src_coords_attributes (const_die, decl);
17730   add_type_attribute (const_die, type, 1, 0, context_die);
17731   if (TREE_PUBLIC (decl))
17732     add_AT_flag (const_die, DW_AT_external, 1);
17733   if (DECL_ARTIFICIAL (decl))
17734     add_AT_flag (const_die, DW_AT_artificial, 1);
17735   tree_add_const_value_attribute_for_decl (const_die, decl);
17736 }
17737
17738 /* Generate a DIE to represent a label identifier.  */
17739
17740 static void
17741 gen_label_die (tree decl, dw_die_ref context_die)
17742 {
17743   tree origin = decl_ultimate_origin (decl);
17744   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17745   rtx insn;
17746   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17747
17748   if (origin != NULL)
17749     add_abstract_origin_attribute (lbl_die, origin);
17750   else
17751     add_name_and_src_coords_attributes (lbl_die, decl);
17752
17753   if (DECL_ABSTRACT (decl))
17754     equate_decl_number_to_die (decl, lbl_die);
17755   else
17756     {
17757       insn = DECL_RTL_IF_SET (decl);
17758
17759       /* Deleted labels are programmer specified labels which have been
17760          eliminated because of various optimizations.  We still emit them
17761          here so that it is possible to put breakpoints on them.  */
17762       if (insn
17763           && (LABEL_P (insn)
17764               || ((NOTE_P (insn)
17765                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17766         {
17767           /* When optimization is enabled (via -O) some parts of the compiler
17768              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17769              represent source-level labels which were explicitly declared by
17770              the user.  This really shouldn't be happening though, so catch
17771              it if it ever does happen.  */
17772           gcc_assert (!INSN_DELETED_P (insn));
17773
17774           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17775           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17776         }
17777       else if (insn
17778                && NOTE_P (insn)
17779                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
17780                && CODE_LABEL_NUMBER (insn) != -1)
17781         {
17782           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
17783           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17784         }
17785     }
17786 }
17787
17788 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
17789    attributes to the DIE for a block STMT, to describe where the inlined
17790    function was called from.  This is similar to add_src_coords_attributes.  */
17791
17792 static inline void
17793 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17794 {
17795   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17796
17797   if (dwarf_version >= 3 || !dwarf_strict)
17798     {
17799       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17800       add_AT_unsigned (die, DW_AT_call_line, s.line);
17801     }
17802 }
17803
17804
17805 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
17806    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
17807
17808 static inline void
17809 add_high_low_attributes (tree stmt, dw_die_ref die)
17810 {
17811   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17812
17813   if (BLOCK_FRAGMENT_CHAIN (stmt)
17814       && (dwarf_version >= 3 || !dwarf_strict))
17815     {
17816       tree chain, superblock = NULL_TREE;
17817       dw_die_ref pdie;
17818       dw_attr_ref attr = NULL;
17819
17820       if (inlined_function_outer_scope_p (stmt))
17821         {
17822           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17823                                        BLOCK_NUMBER (stmt));
17824           add_AT_lbl_id (die, DW_AT_entry_pc, label);
17825         }
17826
17827       /* Optimize duplicate .debug_ranges lists or even tails of
17828          lists.  If this BLOCK has same ranges as its supercontext,
17829          lookup DW_AT_ranges attribute in the supercontext (and
17830          recursively so), verify that the ranges_table contains the
17831          right values and use it instead of adding a new .debug_range.  */
17832       for (chain = stmt, pdie = die;
17833            BLOCK_SAME_RANGE (chain);
17834            chain = BLOCK_SUPERCONTEXT (chain))
17835         {
17836           dw_attr_ref new_attr;
17837
17838           pdie = pdie->die_parent;
17839           if (pdie == NULL)
17840             break;
17841           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
17842             break;
17843           new_attr = get_AT (pdie, DW_AT_ranges);
17844           if (new_attr == NULL
17845               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
17846             break;
17847           attr = new_attr;
17848           superblock = BLOCK_SUPERCONTEXT (chain);
17849         }
17850       if (attr != NULL
17851           && (ranges_table[attr->dw_attr_val.v.val_offset
17852                            / 2 / DWARF2_ADDR_SIZE].num
17853               == BLOCK_NUMBER (superblock))
17854           && BLOCK_FRAGMENT_CHAIN (superblock))
17855         {
17856           unsigned long off = attr->dw_attr_val.v.val_offset
17857                               / 2 / DWARF2_ADDR_SIZE;
17858           unsigned long supercnt = 0, thiscnt = 0;
17859           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
17860                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17861             {
17862               ++supercnt;
17863               gcc_checking_assert (ranges_table[off + supercnt].num
17864                                    == BLOCK_NUMBER (chain));
17865             }
17866           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
17867           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
17868                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
17869             ++thiscnt;
17870           gcc_assert (supercnt >= thiscnt);
17871           add_AT_range_list (die, DW_AT_ranges,
17872                              (off + supercnt - thiscnt)
17873                              * 2 * DWARF2_ADDR_SIZE);
17874           return;
17875         }
17876
17877       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
17878
17879       chain = BLOCK_FRAGMENT_CHAIN (stmt);
17880       do
17881         {
17882           add_ranges (chain);
17883           chain = BLOCK_FRAGMENT_CHAIN (chain);
17884         }
17885       while (chain);
17886       add_ranges (NULL);
17887     }
17888   else
17889     {
17890       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17891                                    BLOCK_NUMBER (stmt));
17892       add_AT_lbl_id (die, DW_AT_low_pc, label);
17893       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
17894                                    BLOCK_NUMBER (stmt));
17895       add_AT_lbl_id (die, DW_AT_high_pc, label);
17896     }
17897 }
17898
17899 /* Generate a DIE for a lexical block.  */
17900
17901 static void
17902 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
17903 {
17904   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
17905
17906   if (call_arg_locations)
17907     {
17908       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17909         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17910                                BLOCK_NUMBER (stmt) + 1);
17911       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
17912     }
17913
17914   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
17915     add_high_low_attributes (stmt, stmt_die);
17916
17917   decls_for_scope (stmt, stmt_die, depth);
17918 }
17919
17920 /* Generate a DIE for an inlined subprogram.  */
17921
17922 static void
17923 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
17924 {
17925   tree decl;
17926
17927   /* The instance of function that is effectively being inlined shall not
17928      be abstract.  */
17929   gcc_assert (! BLOCK_ABSTRACT (stmt));
17930
17931   decl = block_ultimate_origin (stmt);
17932
17933   /* Emit info for the abstract instance first, if we haven't yet.  We
17934      must emit this even if the block is abstract, otherwise when we
17935      emit the block below (or elsewhere), we may end up trying to emit
17936      a die whose origin die hasn't been emitted, and crashing.  */
17937   dwarf2out_abstract_function (decl);
17938
17939   if (! BLOCK_ABSTRACT (stmt))
17940     {
17941       dw_die_ref subr_die
17942         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
17943
17944       if (call_arg_locations)
17945         {
17946           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17947             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17948                                    BLOCK_NUMBER (stmt) + 1);
17949           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
17950         }
17951       add_abstract_origin_attribute (subr_die, decl);
17952       if (TREE_ASM_WRITTEN (stmt))
17953         add_high_low_attributes (stmt, subr_die);
17954       add_call_src_coords_attributes (stmt, subr_die);
17955
17956       decls_for_scope (stmt, subr_die, depth);
17957       current_function_has_inlines = 1;
17958     }
17959 }
17960
17961 /* Generate a DIE for a field in a record, or structure.  */
17962
17963 static void
17964 gen_field_die (tree decl, dw_die_ref context_die)
17965 {
17966   dw_die_ref decl_die;
17967
17968   if (TREE_TYPE (decl) == error_mark_node)
17969     return;
17970
17971   decl_die = new_die (DW_TAG_member, context_die, decl);
17972   add_name_and_src_coords_attributes (decl_die, decl);
17973   add_type_attribute (decl_die, member_declared_type (decl),
17974                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
17975                       context_die);
17976
17977   if (DECL_BIT_FIELD_TYPE (decl))
17978     {
17979       add_byte_size_attribute (decl_die, decl);
17980       add_bit_size_attribute (decl_die, decl);
17981       add_bit_offset_attribute (decl_die, decl);
17982     }
17983
17984   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
17985     add_data_member_location_attribute (decl_die, decl);
17986
17987   if (DECL_ARTIFICIAL (decl))
17988     add_AT_flag (decl_die, DW_AT_artificial, 1);
17989
17990   add_accessibility_attribute (decl_die, decl);
17991
17992   /* Equate decl number to die, so that we can look up this decl later on.  */
17993   equate_decl_number_to_die (decl, decl_die);
17994 }
17995
17996 #if 0
17997 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
17998    Use modified_type_die instead.
17999    We keep this code here just in case these types of DIEs may be needed to
18000    represent certain things in other languages (e.g. Pascal) someday.  */
18001
18002 static void
18003 gen_pointer_type_die (tree type, dw_die_ref context_die)
18004 {
18005   dw_die_ref ptr_die
18006     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18007
18008   equate_type_number_to_die (type, ptr_die);
18009   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18010   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18011 }
18012
18013 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18014    Use modified_type_die instead.
18015    We keep this code here just in case these types of DIEs may be needed to
18016    represent certain things in other languages (e.g. Pascal) someday.  */
18017
18018 static void
18019 gen_reference_type_die (tree type, dw_die_ref context_die)
18020 {
18021   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18022
18023   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18024     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18025   else
18026     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18027
18028   equate_type_number_to_die (type, ref_die);
18029   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18030   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18031 }
18032 #endif
18033
18034 /* Generate a DIE for a pointer to a member type.  */
18035
18036 static void
18037 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18038 {
18039   dw_die_ref ptr_die
18040     = new_die (DW_TAG_ptr_to_member_type,
18041                scope_die_for (type, context_die), type);
18042
18043   equate_type_number_to_die (type, ptr_die);
18044   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18045                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18046   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18047 }
18048
18049 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18050 DEF_VEC_P(dchar_p);
18051 DEF_VEC_ALLOC_P(dchar_p,heap);
18052
18053 static char *producer_string;
18054
18055 /* Return a heap allocated producer string including command line options
18056    if -grecord-gcc-switches.  */
18057
18058 static char *
18059 gen_producer_string (void)
18060 {
18061   size_t j;
18062   VEC(dchar_p, heap) *switches = NULL;
18063   const char *language_string = lang_hooks.name;
18064   char *producer, *tail;
18065   const char *p;
18066   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18067   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18068
18069   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18070     switch (save_decoded_options[j].opt_index)
18071       {
18072       case OPT_o:
18073       case OPT_d:
18074       case OPT_dumpbase:
18075       case OPT_dumpdir:
18076       case OPT_auxbase:
18077       case OPT_auxbase_strip:
18078       case OPT_quiet:
18079       case OPT_version:
18080       case OPT_v:
18081       case OPT_w:
18082       case OPT_L:
18083       case OPT_D:
18084       case OPT_I:
18085       case OPT_U:
18086       case OPT_SPECIAL_unknown:
18087       case OPT_SPECIAL_ignore:
18088       case OPT_SPECIAL_program_name:
18089       case OPT_SPECIAL_input_file:
18090       case OPT_grecord_gcc_switches:
18091       case OPT_gno_record_gcc_switches:
18092       case OPT__output_pch_:
18093       case OPT_fdiagnostics_show_location_:
18094       case OPT_fdiagnostics_show_option:
18095       case OPT_fdiagnostics_show_caret:
18096       case OPT_fverbose_asm:
18097       case OPT____:
18098       case OPT__sysroot_:
18099       case OPT_nostdinc:
18100       case OPT_nostdinc__:
18101         /* Ignore these.  */
18102         continue;
18103       default:
18104         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18105                              == '-');
18106         switch (save_decoded_options[j].canonical_option[0][1])
18107           {
18108           case 'M':
18109           case 'i':
18110           case 'W':
18111             continue;
18112           case 'f':
18113             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18114                          "dump", 4) == 0)
18115               continue;
18116             break;
18117           default:
18118             break;
18119           }
18120         VEC_safe_push (dchar_p, heap, switches,
18121                        save_decoded_options[j].orig_option_with_args_text);
18122         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18123         break;
18124       }
18125
18126   producer = XNEWVEC (char, plen + 1 + len + 1);
18127   tail = producer;
18128   sprintf (tail, "%s %s", language_string, version_string);
18129   tail += plen;
18130
18131   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18132     {
18133       len = strlen (p);
18134       *tail = ' ';
18135       memcpy (tail + 1, p, len);
18136       tail += len + 1;
18137     }
18138
18139   *tail = '\0';
18140   VEC_free (dchar_p, heap, switches);
18141   return producer;
18142 }
18143
18144 /* Generate the DIE for the compilation unit.  */
18145
18146 static dw_die_ref
18147 gen_compile_unit_die (const char *filename)
18148 {
18149   dw_die_ref die;
18150   const char *language_string = lang_hooks.name;
18151   int language;
18152
18153   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18154
18155   if (filename)
18156     {
18157       add_name_attribute (die, filename);
18158       /* Don't add cwd for <built-in>.  */
18159       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18160         add_comp_dir_attribute (die);
18161     }
18162
18163   if (producer_string == NULL)
18164     producer_string = gen_producer_string ();
18165   add_AT_string (die, DW_AT_producer, producer_string);
18166
18167   /* If our producer is LTO try to figure out a common language to use
18168      from the global list of translation units.  */
18169   if (strcmp (language_string, "GNU GIMPLE") == 0)
18170     {
18171       unsigned i;
18172       tree t;
18173       const char *common_lang = NULL;
18174
18175       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18176         {
18177           if (!TRANSLATION_UNIT_LANGUAGE (t))
18178             continue;
18179           if (!common_lang)
18180             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18181           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18182             ;
18183           else if (strncmp (common_lang, "GNU C", 5) == 0
18184                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18185             /* Mixing C and C++ is ok, use C++ in that case.  */
18186             common_lang = "GNU C++";
18187           else
18188             {
18189               /* Fall back to C.  */
18190               common_lang = NULL;
18191               break;
18192             }
18193         }
18194
18195       if (common_lang)
18196         language_string = common_lang;
18197     }
18198
18199   language = DW_LANG_C89;
18200   if (strcmp (language_string, "GNU C++") == 0)
18201     language = DW_LANG_C_plus_plus;
18202   else if (strcmp (language_string, "GNU F77") == 0)
18203     language = DW_LANG_Fortran77;
18204   else if (strcmp (language_string, "GNU Pascal") == 0)
18205     language = DW_LANG_Pascal83;
18206   else if (dwarf_version >= 3 || !dwarf_strict)
18207     {
18208       if (strcmp (language_string, "GNU Ada") == 0)
18209         language = DW_LANG_Ada95;
18210       else if (strcmp (language_string, "GNU Fortran") == 0)
18211         language = DW_LANG_Fortran95;
18212       else if (strcmp (language_string, "GNU Java") == 0)
18213         language = DW_LANG_Java;
18214       else if (strcmp (language_string, "GNU Objective-C") == 0)
18215         language = DW_LANG_ObjC;
18216       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18217         language = DW_LANG_ObjC_plus_plus;
18218       else if (dwarf_version >= 5 || !dwarf_strict)
18219         {
18220           if (strcmp (language_string, "GNU Go") == 0)
18221             language = DW_LANG_Go;
18222         }
18223     }
18224   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
18225   else if (strcmp (language_string, "GNU Fortran") == 0)
18226     language = DW_LANG_Fortran90;
18227
18228   add_AT_unsigned (die, DW_AT_language, language);
18229
18230   switch (language)
18231     {
18232     case DW_LANG_Fortran77:
18233     case DW_LANG_Fortran90:
18234     case DW_LANG_Fortran95:
18235       /* Fortran has case insensitive identifiers and the front-end
18236          lowercases everything.  */
18237       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18238       break;
18239     default:
18240       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18241       break;
18242     }
18243   return die;
18244 }
18245
18246 /* Generate the DIE for a base class.  */
18247
18248 static void
18249 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18250 {
18251   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18252
18253   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18254   add_data_member_location_attribute (die, binfo);
18255
18256   if (BINFO_VIRTUAL_P (binfo))
18257     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18258
18259   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18260      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18261      the default has always been DW_ACCESS_private.  */
18262   if (access == access_public_node)
18263     {
18264       if (dwarf_version == 2
18265           || context_die->die_tag == DW_TAG_class_type)
18266       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18267     }
18268   else if (access == access_protected_node)
18269     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18270   else if (dwarf_version > 2
18271            && context_die->die_tag != DW_TAG_class_type)
18272     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18273 }
18274
18275 /* Generate a DIE for a class member.  */
18276
18277 static void
18278 gen_member_die (tree type, dw_die_ref context_die)
18279 {
18280   tree member;
18281   tree binfo = TYPE_BINFO (type);
18282   dw_die_ref child;
18283
18284   /* If this is not an incomplete type, output descriptions of each of its
18285      members. Note that as we output the DIEs necessary to represent the
18286      members of this record or union type, we will also be trying to output
18287      DIEs to represent the *types* of those members. However the `type'
18288      function (above) will specifically avoid generating type DIEs for member
18289      types *within* the list of member DIEs for this (containing) type except
18290      for those types (of members) which are explicitly marked as also being
18291      members of this (containing) type themselves.  The g++ front- end can
18292      force any given type to be treated as a member of some other (containing)
18293      type by setting the TYPE_CONTEXT of the given (member) type to point to
18294      the TREE node representing the appropriate (containing) type.  */
18295
18296   /* First output info about the base classes.  */
18297   if (binfo)
18298     {
18299       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18300       int i;
18301       tree base;
18302
18303       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18304         gen_inheritance_die (base,
18305                              (accesses ? VEC_index (tree, accesses, i)
18306                               : access_public_node), context_die);
18307     }
18308
18309   /* Now output info about the data members and type members.  */
18310   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18311     {
18312       /* If we thought we were generating minimal debug info for TYPE
18313          and then changed our minds, some of the member declarations
18314          may have already been defined.  Don't define them again, but
18315          do put them in the right order.  */
18316
18317       child = lookup_decl_die (member);
18318       if (child)
18319         splice_child_die (context_die, child);
18320       else
18321         gen_decl_die (member, NULL, context_die);
18322     }
18323
18324   /* Now output info about the function members (if any).  */
18325   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18326     {
18327       /* Don't include clones in the member list.  */
18328       if (DECL_ABSTRACT_ORIGIN (member))
18329         continue;
18330
18331       child = lookup_decl_die (member);
18332       if (child)
18333         splice_child_die (context_die, child);
18334       else
18335         gen_decl_die (member, NULL, context_die);
18336     }
18337 }
18338
18339 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18340    is set, we pretend that the type was never defined, so we only get the
18341    member DIEs needed by later specification DIEs.  */
18342
18343 static void
18344 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18345                                 enum debug_info_usage usage)
18346 {
18347   dw_die_ref type_die = lookup_type_die (type);
18348   dw_die_ref scope_die = 0;
18349   int nested = 0;
18350   int complete = (TYPE_SIZE (type)
18351                   && (! TYPE_STUB_DECL (type)
18352                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18353   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18354   complete = complete && should_emit_struct_debug (type, usage);
18355
18356   if (type_die && ! complete)
18357     return;
18358
18359   if (TYPE_CONTEXT (type) != NULL_TREE
18360       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18361           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18362     nested = 1;
18363
18364   scope_die = scope_die_for (type, context_die);
18365
18366   if (! type_die || (nested && is_cu_die (scope_die)))
18367     /* First occurrence of type or toplevel definition of nested class.  */
18368     {
18369       dw_die_ref old_die = type_die;
18370
18371       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18372                           ? record_type_tag (type) : DW_TAG_union_type,
18373                           scope_die, type);
18374       equate_type_number_to_die (type, type_die);
18375       if (old_die)
18376         add_AT_specification (type_die, old_die);
18377       else
18378         add_name_attribute (type_die, type_tag (type));
18379     }
18380   else
18381     remove_AT (type_die, DW_AT_declaration);
18382
18383   /* Generate child dies for template paramaters.  */
18384   if (debug_info_level > DINFO_LEVEL_TERSE
18385       && COMPLETE_TYPE_P (type))
18386     schedule_generic_params_dies_gen (type);
18387
18388   /* If this type has been completed, then give it a byte_size attribute and
18389      then give a list of members.  */
18390   if (complete && !ns_decl)
18391     {
18392       /* Prevent infinite recursion in cases where the type of some member of
18393          this type is expressed in terms of this type itself.  */
18394       TREE_ASM_WRITTEN (type) = 1;
18395       add_byte_size_attribute (type_die, type);
18396       if (TYPE_STUB_DECL (type) != NULL_TREE)
18397         {
18398           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18399           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18400         }
18401
18402       /* If the first reference to this type was as the return type of an
18403          inline function, then it may not have a parent.  Fix this now.  */
18404       if (type_die->die_parent == NULL)
18405         add_child_die (scope_die, type_die);
18406
18407       push_decl_scope (type);
18408       gen_member_die (type, type_die);
18409       pop_decl_scope ();
18410
18411       add_gnat_descriptive_type_attribute (type_die, type, context_die);
18412       if (TYPE_ARTIFICIAL (type))
18413         add_AT_flag (type_die, DW_AT_artificial, 1);
18414
18415       /* GNU extension: Record what type our vtable lives in.  */
18416       if (TYPE_VFIELD (type))
18417         {
18418           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18419
18420           gen_type_die (vtype, context_die);
18421           add_AT_die_ref (type_die, DW_AT_containing_type,
18422                           lookup_type_die (vtype));
18423         }
18424     }
18425   else
18426     {
18427       add_AT_flag (type_die, DW_AT_declaration, 1);
18428
18429       /* We don't need to do this for function-local types.  */
18430       if (TYPE_STUB_DECL (type)
18431           && ! decl_function_context (TYPE_STUB_DECL (type)))
18432         VEC_safe_push (tree, gc, incomplete_types, type);
18433     }
18434
18435   if (get_AT (type_die, DW_AT_name))
18436     add_pubtype (type, type_die);
18437 }
18438
18439 /* Generate a DIE for a subroutine _type_.  */
18440
18441 static void
18442 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18443 {
18444   tree return_type = TREE_TYPE (type);
18445   dw_die_ref subr_die
18446     = new_die (DW_TAG_subroutine_type,
18447                scope_die_for (type, context_die), type);
18448
18449   equate_type_number_to_die (type, subr_die);
18450   add_prototyped_attribute (subr_die, type);
18451   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18452   gen_formal_types_die (type, subr_die);
18453
18454   if (get_AT (subr_die, DW_AT_name))
18455     add_pubtype (type, subr_die);
18456 }
18457
18458 /* Generate a DIE for a type definition.  */
18459
18460 static void
18461 gen_typedef_die (tree decl, dw_die_ref context_die)
18462 {
18463   dw_die_ref type_die;
18464   tree origin;
18465
18466   if (TREE_ASM_WRITTEN (decl))
18467     return;
18468
18469   TREE_ASM_WRITTEN (decl) = 1;
18470   type_die = new_die (DW_TAG_typedef, context_die, decl);
18471   origin = decl_ultimate_origin (decl);
18472   if (origin != NULL)
18473     add_abstract_origin_attribute (type_die, origin);
18474   else
18475     {
18476       tree type;
18477
18478       add_name_and_src_coords_attributes (type_die, decl);
18479       if (DECL_ORIGINAL_TYPE (decl))
18480         {
18481           type = DECL_ORIGINAL_TYPE (decl);
18482
18483           gcc_assert (type != TREE_TYPE (decl));
18484           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18485         }
18486       else
18487         {
18488           type = TREE_TYPE (decl);
18489
18490           if (is_naming_typedef_decl (TYPE_NAME (type)))
18491             {
18492               /* Here, we are in the case of decl being a typedef naming
18493                  an anonymous type, e.g:
18494                      typedef struct {...} foo;
18495                  In that case TREE_TYPE (decl) is not a typedef variant
18496                  type and TYPE_NAME of the anonymous type is set to the
18497                  TYPE_DECL of the typedef. This construct is emitted by
18498                  the C++ FE.
18499
18500                  TYPE is the anonymous struct named by the typedef
18501                  DECL. As we need the DW_AT_type attribute of the
18502                  DW_TAG_typedef to point to the DIE of TYPE, let's
18503                  generate that DIE right away. add_type_attribute
18504                  called below will then pick (via lookup_type_die) that
18505                  anonymous struct DIE.  */
18506               if (!TREE_ASM_WRITTEN (type))
18507                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18508
18509               /* This is a GNU Extension.  We are adding a
18510                  DW_AT_linkage_name attribute to the DIE of the
18511                  anonymous struct TYPE.  The value of that attribute
18512                  is the name of the typedef decl naming the anonymous
18513                  struct.  This greatly eases the work of consumers of
18514                  this debug info.  */
18515               add_linkage_attr (lookup_type_die (type), decl);
18516             }
18517         }
18518
18519       add_type_attribute (type_die, type, TREE_READONLY (decl),
18520                           TREE_THIS_VOLATILE (decl), context_die);
18521
18522       if (is_naming_typedef_decl (decl))
18523         /* We want that all subsequent calls to lookup_type_die with
18524            TYPE in argument yield the DW_TAG_typedef we have just
18525            created.  */
18526         equate_type_number_to_die (type, type_die);
18527
18528       add_accessibility_attribute (type_die, decl);
18529     }
18530
18531   if (DECL_ABSTRACT (decl))
18532     equate_decl_number_to_die (decl, type_die);
18533
18534   if (get_AT (type_die, DW_AT_name))
18535     add_pubtype (decl, type_die);
18536 }
18537
18538 /* Generate a DIE for a struct, class, enum or union type.  */
18539
18540 static void
18541 gen_tagged_type_die (tree type,
18542                      dw_die_ref context_die,
18543                      enum debug_info_usage usage)
18544 {
18545   int need_pop;
18546
18547   if (type == NULL_TREE
18548       || !is_tagged_type (type))
18549     return;
18550
18551   /* If this is a nested type whose containing class hasn't been written
18552      out yet, writing it out will cover this one, too.  This does not apply
18553      to instantiations of member class templates; they need to be added to
18554      the containing class as they are generated.  FIXME: This hurts the
18555      idea of combining type decls from multiple TUs, since we can't predict
18556      what set of template instantiations we'll get.  */
18557   if (TYPE_CONTEXT (type)
18558       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18559       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18560     {
18561       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18562
18563       if (TREE_ASM_WRITTEN (type))
18564         return;
18565
18566       /* If that failed, attach ourselves to the stub.  */
18567       push_decl_scope (TYPE_CONTEXT (type));
18568       context_die = lookup_type_die (TYPE_CONTEXT (type));
18569       need_pop = 1;
18570     }
18571   else if (TYPE_CONTEXT (type) != NULL_TREE
18572            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18573     {
18574       /* If this type is local to a function that hasn't been written
18575          out yet, use a NULL context for now; it will be fixed up in
18576          decls_for_scope.  */
18577       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18578       /* A declaration DIE doesn't count; nested types need to go in the
18579          specification.  */
18580       if (context_die && is_declaration_die (context_die))
18581         context_die = NULL;
18582       need_pop = 0;
18583     }
18584   else
18585     {
18586       context_die = declare_in_namespace (type, context_die);
18587       need_pop = 0;
18588     }
18589
18590   if (TREE_CODE (type) == ENUMERAL_TYPE)
18591     {
18592       /* This might have been written out by the call to
18593          declare_in_namespace.  */
18594       if (!TREE_ASM_WRITTEN (type))
18595         gen_enumeration_type_die (type, context_die);
18596     }
18597   else
18598     gen_struct_or_union_type_die (type, context_die, usage);
18599
18600   if (need_pop)
18601     pop_decl_scope ();
18602
18603   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18604      it up if it is ever completed.  gen_*_type_die will set it for us
18605      when appropriate.  */
18606 }
18607
18608 /* Generate a type description DIE.  */
18609
18610 static void
18611 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18612                          enum debug_info_usage usage)
18613 {
18614   struct array_descr_info info;
18615
18616   if (type == NULL_TREE || type == error_mark_node)
18617     return;
18618
18619   if (TYPE_NAME (type) != NULL_TREE
18620       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18621       && is_redundant_typedef (TYPE_NAME (type))
18622       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18623     /* The DECL of this type is a typedef we don't want to emit debug
18624        info for but we want debug info for its underlying typedef.
18625        This can happen for e.g, the injected-class-name of a C++
18626        type.  */
18627     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18628
18629   /* If TYPE is a typedef type variant, let's generate debug info
18630      for the parent typedef which TYPE is a type of.  */
18631   if (typedef_variant_p (type))
18632     {
18633       if (TREE_ASM_WRITTEN (type))
18634         return;
18635
18636       /* Prevent broken recursion; we can't hand off to the same type.  */
18637       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18638
18639       /* Give typedefs the right scope.  */
18640       context_die = scope_die_for (type, context_die);
18641
18642       TREE_ASM_WRITTEN (type) = 1;
18643
18644       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18645       return;
18646     }
18647
18648   /* If type is an anonymous tagged type named by a typedef, let's
18649      generate debug info for the typedef.  */
18650   if (is_naming_typedef_decl (TYPE_NAME (type)))
18651     {
18652       /* Use the DIE of the containing namespace as the parent DIE of
18653          the type description DIE we want to generate.  */
18654       if (DECL_CONTEXT (TYPE_NAME (type))
18655           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18656         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18657       
18658       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18659       return;
18660     }
18661
18662   /* If this is an array type with hidden descriptor, handle it first.  */
18663   if (!TREE_ASM_WRITTEN (type)
18664       && lang_hooks.types.get_array_descr_info
18665       && lang_hooks.types.get_array_descr_info (type, &info)
18666       && (dwarf_version >= 3 || !dwarf_strict))
18667     {
18668       gen_descr_array_type_die (type, &info, context_die);
18669       TREE_ASM_WRITTEN (type) = 1;
18670       return;
18671     }
18672
18673   /* We are going to output a DIE to represent the unqualified version
18674      of this type (i.e. without any const or volatile qualifiers) so
18675      get the main variant (i.e. the unqualified version) of this type
18676      now.  (Vectors are special because the debugging info is in the
18677      cloned type itself).  */
18678   if (TREE_CODE (type) != VECTOR_TYPE)
18679     type = type_main_variant (type);
18680
18681   if (TREE_ASM_WRITTEN (type))
18682     return;
18683
18684   switch (TREE_CODE (type))
18685     {
18686     case ERROR_MARK:
18687       break;
18688
18689     case POINTER_TYPE:
18690     case REFERENCE_TYPE:
18691       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18692          ensures that the gen_type_die recursion will terminate even if the
18693          type is recursive.  Recursive types are possible in Ada.  */
18694       /* ??? We could perhaps do this for all types before the switch
18695          statement.  */
18696       TREE_ASM_WRITTEN (type) = 1;
18697
18698       /* For these types, all that is required is that we output a DIE (or a
18699          set of DIEs) to represent the "basis" type.  */
18700       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18701                                 DINFO_USAGE_IND_USE);
18702       break;
18703
18704     case OFFSET_TYPE:
18705       /* This code is used for C++ pointer-to-data-member types.
18706          Output a description of the relevant class type.  */
18707       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18708                                         DINFO_USAGE_IND_USE);
18709
18710       /* Output a description of the type of the object pointed to.  */
18711       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18712                                         DINFO_USAGE_IND_USE);
18713
18714       /* Now output a DIE to represent this pointer-to-data-member type
18715          itself.  */
18716       gen_ptr_to_mbr_type_die (type, context_die);
18717       break;
18718
18719     case FUNCTION_TYPE:
18720       /* Force out return type (in case it wasn't forced out already).  */
18721       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18722                                         DINFO_USAGE_DIR_USE);
18723       gen_subroutine_type_die (type, context_die);
18724       break;
18725
18726     case METHOD_TYPE:
18727       /* Force out return type (in case it wasn't forced out already).  */
18728       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18729                                         DINFO_USAGE_DIR_USE);
18730       gen_subroutine_type_die (type, context_die);
18731       break;
18732
18733     case ARRAY_TYPE:
18734       gen_array_type_die (type, context_die);
18735       break;
18736
18737     case VECTOR_TYPE:
18738       gen_array_type_die (type, context_die);
18739       break;
18740
18741     case ENUMERAL_TYPE:
18742     case RECORD_TYPE:
18743     case UNION_TYPE:
18744     case QUAL_UNION_TYPE:
18745       gen_tagged_type_die (type, context_die, usage);
18746       return;
18747
18748     case VOID_TYPE:
18749     case INTEGER_TYPE:
18750     case REAL_TYPE:
18751     case FIXED_POINT_TYPE:
18752     case COMPLEX_TYPE:
18753     case BOOLEAN_TYPE:
18754       /* No DIEs needed for fundamental types.  */
18755       break;
18756
18757     case NULLPTR_TYPE:
18758     case LANG_TYPE:
18759       /* Just use DW_TAG_unspecified_type.  */
18760       {
18761         dw_die_ref type_die = lookup_type_die (type);
18762         if (type_die == NULL)
18763           {
18764             tree name = TYPE_NAME (type);
18765             if (TREE_CODE (name) == TYPE_DECL)
18766               name = DECL_NAME (name);
18767             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18768             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18769             equate_type_number_to_die (type, type_die);
18770           }
18771       }
18772       break;
18773
18774     default:
18775       gcc_unreachable ();
18776     }
18777
18778   TREE_ASM_WRITTEN (type) = 1;
18779 }
18780
18781 static void
18782 gen_type_die (tree type, dw_die_ref context_die)
18783 {
18784   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18785 }
18786
18787 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18788    things which are local to the given block.  */
18789
18790 static void
18791 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18792 {
18793   int must_output_die = 0;
18794   bool inlined_func;
18795
18796   /* Ignore blocks that are NULL.  */
18797   if (stmt == NULL_TREE)
18798     return;
18799
18800   inlined_func = inlined_function_outer_scope_p (stmt);
18801
18802   /* If the block is one fragment of a non-contiguous block, do not
18803      process the variables, since they will have been done by the
18804      origin block.  Do process subblocks.  */
18805   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18806     {
18807       tree sub;
18808
18809       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18810         gen_block_die (sub, context_die, depth + 1);
18811
18812       return;
18813     }
18814
18815   /* Determine if we need to output any Dwarf DIEs at all to represent this
18816      block.  */
18817   if (inlined_func)
18818     /* The outer scopes for inlinings *must* always be represented.  We
18819        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
18820     must_output_die = 1;
18821   else
18822     {
18823       /* Determine if this block directly contains any "significant"
18824          local declarations which we will need to output DIEs for.  */
18825       if (debug_info_level > DINFO_LEVEL_TERSE)
18826         /* We are not in terse mode so *any* local declaration counts
18827            as being a "significant" one.  */
18828         must_output_die = ((BLOCK_VARS (stmt) != NULL
18829                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18830                            && (TREE_USED (stmt)
18831                                || TREE_ASM_WRITTEN (stmt)
18832                                || BLOCK_ABSTRACT (stmt)));
18833       else if ((TREE_USED (stmt)
18834                 || TREE_ASM_WRITTEN (stmt)
18835                 || BLOCK_ABSTRACT (stmt))
18836                && !dwarf2out_ignore_block (stmt))
18837         must_output_die = 1;
18838     }
18839
18840   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18841      DIE for any block which contains no significant local declarations at
18842      all.  Rather, in such cases we just call `decls_for_scope' so that any
18843      needed Dwarf info for any sub-blocks will get properly generated. Note
18844      that in terse mode, our definition of what constitutes a "significant"
18845      local declaration gets restricted to include only inlined function
18846      instances and local (nested) function definitions.  */
18847   if (must_output_die)
18848     {
18849       if (inlined_func)
18850         {
18851           /* If STMT block is abstract, that means we have been called
18852              indirectly from dwarf2out_abstract_function.
18853              That function rightfully marks the descendent blocks (of
18854              the abstract function it is dealing with) as being abstract,
18855              precisely to prevent us from emitting any
18856              DW_TAG_inlined_subroutine DIE as a descendent
18857              of an abstract function instance. So in that case, we should
18858              not call gen_inlined_subroutine_die.
18859
18860              Later though, when cgraph asks dwarf2out to emit info
18861              for the concrete instance of the function decl into which
18862              the concrete instance of STMT got inlined, the later will lead
18863              to the generation of a DW_TAG_inlined_subroutine DIE.  */
18864           if (! BLOCK_ABSTRACT (stmt))
18865             gen_inlined_subroutine_die (stmt, context_die, depth);
18866         }
18867       else
18868         gen_lexical_block_die (stmt, context_die, depth);
18869     }
18870   else
18871     decls_for_scope (stmt, context_die, depth);
18872 }
18873
18874 /* Process variable DECL (or variable with origin ORIGIN) within
18875    block STMT and add it to CONTEXT_DIE.  */
18876 static void
18877 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18878 {
18879   dw_die_ref die;
18880   tree decl_or_origin = decl ? decl : origin;
18881
18882   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18883     die = lookup_decl_die (decl_or_origin);
18884   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18885            && TYPE_DECL_IS_STUB (decl_or_origin))
18886     die = lookup_type_die (TREE_TYPE (decl_or_origin));
18887   else
18888     die = NULL;
18889
18890   if (die != NULL && die->die_parent == NULL)
18891     add_child_die (context_die, die);
18892   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18893     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18894                                          stmt, context_die);
18895   else
18896     gen_decl_die (decl, origin, context_die);
18897 }
18898
18899 /* Generate all of the decls declared within a given scope and (recursively)
18900    all of its sub-blocks.  */
18901
18902 static void
18903 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18904 {
18905   tree decl;
18906   unsigned int i;
18907   tree subblocks;
18908
18909   /* Ignore NULL blocks.  */
18910   if (stmt == NULL_TREE)
18911     return;
18912
18913   /* Output the DIEs to represent all of the data objects and typedefs
18914      declared directly within this block but not within any nested
18915      sub-blocks.  Also, nested function and tag DIEs have been
18916      generated with a parent of NULL; fix that up now.  */
18917   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
18918     process_scope_var (stmt, decl, NULL_TREE, context_die);
18919   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18920     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
18921                        context_die);
18922
18923   /* If we're at -g1, we're not interested in subblocks.  */
18924   if (debug_info_level <= DINFO_LEVEL_TERSE)
18925     return;
18926
18927   /* Output the DIEs to represent all sub-blocks (and the items declared
18928      therein) of this block.  */
18929   for (subblocks = BLOCK_SUBBLOCKS (stmt);
18930        subblocks != NULL;
18931        subblocks = BLOCK_CHAIN (subblocks))
18932     gen_block_die (subblocks, context_die, depth + 1);
18933 }
18934
18935 /* Is this a typedef we can avoid emitting?  */
18936
18937 static inline int
18938 is_redundant_typedef (const_tree decl)
18939 {
18940   if (TYPE_DECL_IS_STUB (decl))
18941     return 1;
18942
18943   if (DECL_ARTIFICIAL (decl)
18944       && DECL_CONTEXT (decl)
18945       && is_tagged_type (DECL_CONTEXT (decl))
18946       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
18947       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
18948     /* Also ignore the artificial member typedef for the class name.  */
18949     return 1;
18950
18951   return 0;
18952 }
18953
18954 /* Return TRUE if TYPE is a typedef that names a type for linkage
18955    purposes. This kind of typedefs is produced by the C++ FE for
18956    constructs like:
18957
18958    typedef struct {...} foo;
18959
18960    In that case, there is no typedef variant type produced for foo.
18961    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
18962    struct type.  */
18963
18964 static bool
18965 is_naming_typedef_decl (const_tree decl)
18966 {
18967   if (decl == NULL_TREE
18968       || TREE_CODE (decl) != TYPE_DECL
18969       || !is_tagged_type (TREE_TYPE (decl))
18970       || DECL_IS_BUILTIN (decl)
18971       || is_redundant_typedef (decl)
18972       /* It looks like Ada produces TYPE_DECLs that are very similar
18973          to C++ naming typedefs but that have different
18974          semantics. Let's be specific to c++ for now.  */
18975       || !is_cxx ())
18976     return FALSE;
18977
18978   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
18979           && TYPE_NAME (TREE_TYPE (decl)) == decl
18980           && (TYPE_STUB_DECL (TREE_TYPE (decl))
18981               != TYPE_NAME (TREE_TYPE (decl))));
18982 }
18983
18984 /* Returns the DIE for a context.  */
18985
18986 static inline dw_die_ref
18987 get_context_die (tree context)
18988 {
18989   if (context)
18990     {
18991       /* Find die that represents this context.  */
18992       if (TYPE_P (context))
18993         {
18994           context = TYPE_MAIN_VARIANT (context);
18995           return strip_naming_typedef (context, force_type_die (context));
18996         }
18997       else
18998         return force_decl_die (context);
18999     }
19000   return comp_unit_die ();
19001 }
19002
19003 /* Returns the DIE for decl.  A DIE will always be returned.  */
19004
19005 static dw_die_ref
19006 force_decl_die (tree decl)
19007 {
19008   dw_die_ref decl_die;
19009   unsigned saved_external_flag;
19010   tree save_fn = NULL_TREE;
19011   decl_die = lookup_decl_die (decl);
19012   if (!decl_die)
19013     {
19014       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19015
19016       decl_die = lookup_decl_die (decl);
19017       if (decl_die)
19018         return decl_die;
19019
19020       switch (TREE_CODE (decl))
19021         {
19022         case FUNCTION_DECL:
19023           /* Clear current_function_decl, so that gen_subprogram_die thinks
19024              that this is a declaration. At this point, we just want to force
19025              declaration die.  */
19026           save_fn = current_function_decl;
19027           current_function_decl = NULL_TREE;
19028           gen_subprogram_die (decl, context_die);
19029           current_function_decl = save_fn;
19030           break;
19031
19032         case VAR_DECL:
19033           /* Set external flag to force declaration die. Restore it after
19034            gen_decl_die() call.  */
19035           saved_external_flag = DECL_EXTERNAL (decl);
19036           DECL_EXTERNAL (decl) = 1;
19037           gen_decl_die (decl, NULL, context_die);
19038           DECL_EXTERNAL (decl) = saved_external_flag;
19039           break;
19040
19041         case NAMESPACE_DECL:
19042           if (dwarf_version >= 3 || !dwarf_strict)
19043             dwarf2out_decl (decl);
19044           else
19045             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19046             decl_die = comp_unit_die ();
19047           break;
19048
19049         case TRANSLATION_UNIT_DECL:
19050           decl_die = comp_unit_die ();
19051           break;
19052
19053         default:
19054           gcc_unreachable ();
19055         }
19056
19057       /* We should be able to find the DIE now.  */
19058       if (!decl_die)
19059         decl_die = lookup_decl_die (decl);
19060       gcc_assert (decl_die);
19061     }
19062
19063   return decl_die;
19064 }
19065
19066 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19067    always returned.  */
19068
19069 static dw_die_ref
19070 force_type_die (tree type)
19071 {
19072   dw_die_ref type_die;
19073
19074   type_die = lookup_type_die (type);
19075   if (!type_die)
19076     {
19077       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19078
19079       type_die = modified_type_die (type, TYPE_READONLY (type),
19080                                     TYPE_VOLATILE (type), context_die);
19081       gcc_assert (type_die);
19082     }
19083   return type_die;
19084 }
19085
19086 /* Force out any required namespaces to be able to output DECL,
19087    and return the new context_die for it, if it's changed.  */
19088
19089 static dw_die_ref
19090 setup_namespace_context (tree thing, dw_die_ref context_die)
19091 {
19092   tree context = (DECL_P (thing)
19093                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19094   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19095     /* Force out the namespace.  */
19096     context_die = force_decl_die (context);
19097
19098   return context_die;
19099 }
19100
19101 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19102    type) within its namespace, if appropriate.
19103
19104    For compatibility with older debuggers, namespace DIEs only contain
19105    declarations; all definitions are emitted at CU scope.  */
19106
19107 static dw_die_ref
19108 declare_in_namespace (tree thing, dw_die_ref context_die)
19109 {
19110   dw_die_ref ns_context;
19111
19112   if (debug_info_level <= DINFO_LEVEL_TERSE)
19113     return context_die;
19114
19115   /* If this decl is from an inlined function, then don't try to emit it in its
19116      namespace, as we will get confused.  It would have already been emitted
19117      when the abstract instance of the inline function was emitted anyways.  */
19118   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19119     return context_die;
19120
19121   ns_context = setup_namespace_context (thing, context_die);
19122
19123   if (ns_context != context_die)
19124     {
19125       if (is_fortran ())
19126         return ns_context;
19127       if (DECL_P (thing))
19128         gen_decl_die (thing, NULL, ns_context);
19129       else
19130         gen_type_die (thing, ns_context);
19131     }
19132   return context_die;
19133 }
19134
19135 /* Generate a DIE for a namespace or namespace alias.  */
19136
19137 static void
19138 gen_namespace_die (tree decl, dw_die_ref context_die)
19139 {
19140   dw_die_ref namespace_die;
19141
19142   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19143      they are an alias of.  */
19144   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19145     {
19146       /* Output a real namespace or module.  */
19147       context_die = setup_namespace_context (decl, comp_unit_die ());
19148       namespace_die = new_die (is_fortran ()
19149                                ? DW_TAG_module : DW_TAG_namespace,
19150                                context_die, decl);
19151       /* For Fortran modules defined in different CU don't add src coords.  */
19152       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19153         {
19154           const char *name = dwarf2_name (decl, 0);
19155           if (name)
19156             add_name_attribute (namespace_die, name);
19157         }
19158       else
19159         add_name_and_src_coords_attributes (namespace_die, decl);
19160       if (DECL_EXTERNAL (decl))
19161         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19162       equate_decl_number_to_die (decl, namespace_die);
19163     }
19164   else
19165     {
19166       /* Output a namespace alias.  */
19167
19168       /* Force out the namespace we are an alias of, if necessary.  */
19169       dw_die_ref origin_die
19170         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19171
19172       if (DECL_FILE_SCOPE_P (decl)
19173           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19174         context_die = setup_namespace_context (decl, comp_unit_die ());
19175       /* Now create the namespace alias DIE.  */
19176       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19177       add_name_and_src_coords_attributes (namespace_die, decl);
19178       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19179       equate_decl_number_to_die (decl, namespace_die);
19180     }
19181   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
19182   if (want_pubnames ())
19183     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19184 }
19185
19186 /* Generate Dwarf debug information for a decl described by DECL.
19187    The return value is currently only meaningful for PARM_DECLs,
19188    for all other decls it returns NULL.  */
19189
19190 static dw_die_ref
19191 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19192 {
19193   tree decl_or_origin = decl ? decl : origin;
19194   tree class_origin = NULL, ultimate_origin;
19195
19196   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19197     return NULL;
19198
19199   switch (TREE_CODE (decl_or_origin))
19200     {
19201     case ERROR_MARK:
19202       break;
19203
19204     case CONST_DECL:
19205       if (!is_fortran () && !is_ada ())
19206         {
19207           /* The individual enumerators of an enum type get output when we output
19208              the Dwarf representation of the relevant enum type itself.  */
19209           break;
19210         }
19211
19212       /* Emit its type.  */
19213       gen_type_die (TREE_TYPE (decl), context_die);
19214
19215       /* And its containing namespace.  */
19216       context_die = declare_in_namespace (decl, context_die);
19217
19218       gen_const_die (decl, context_die);
19219       break;
19220
19221     case FUNCTION_DECL:
19222       /* Don't output any DIEs to represent mere function declarations,
19223          unless they are class members or explicit block externs.  */
19224       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19225           && DECL_FILE_SCOPE_P (decl_or_origin)
19226           && (current_function_decl == NULL_TREE
19227               || DECL_ARTIFICIAL (decl_or_origin)))
19228         break;
19229
19230 #if 0
19231       /* FIXME */
19232       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19233          on local redeclarations of global functions.  That seems broken.  */
19234       if (current_function_decl != decl)
19235         /* This is only a declaration.  */;
19236 #endif
19237
19238       /* If we're emitting a clone, emit info for the abstract instance.  */
19239       if (origin || DECL_ORIGIN (decl) != decl)
19240         dwarf2out_abstract_function (origin
19241                                      ? DECL_ORIGIN (origin)
19242                                      : DECL_ABSTRACT_ORIGIN (decl));
19243
19244       /* If we're emitting an out-of-line copy of an inline function,
19245          emit info for the abstract instance and set up to refer to it.  */
19246       else if (cgraph_function_possibly_inlined_p (decl)
19247                && ! DECL_ABSTRACT (decl)
19248                && ! class_or_namespace_scope_p (context_die)
19249                /* dwarf2out_abstract_function won't emit a die if this is just
19250                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19251                   that case, because that works only if we have a die.  */
19252                && DECL_INITIAL (decl) != NULL_TREE)
19253         {
19254           dwarf2out_abstract_function (decl);
19255           set_decl_origin_self (decl);
19256         }
19257
19258       /* Otherwise we're emitting the primary DIE for this decl.  */
19259       else if (debug_info_level > DINFO_LEVEL_TERSE)
19260         {
19261           /* Before we describe the FUNCTION_DECL itself, make sure that we
19262              have its containing type.  */
19263           if (!origin)
19264             origin = decl_class_context (decl);
19265           if (origin != NULL_TREE)
19266             gen_type_die (origin, context_die);
19267
19268           /* And its return type.  */
19269           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19270
19271           /* And its virtual context.  */
19272           if (DECL_VINDEX (decl) != NULL_TREE)
19273             gen_type_die (DECL_CONTEXT (decl), context_die);
19274
19275           /* Make sure we have a member DIE for decl.  */
19276           if (origin != NULL_TREE)
19277             gen_type_die_for_member (origin, decl, context_die);
19278
19279           /* And its containing namespace.  */
19280           context_die = declare_in_namespace (decl, context_die);
19281         }
19282
19283       /* Now output a DIE to represent the function itself.  */
19284       if (decl)
19285         gen_subprogram_die (decl, context_die);
19286       break;
19287
19288     case TYPE_DECL:
19289       /* If we are in terse mode, don't generate any DIEs to represent any
19290          actual typedefs.  */
19291       if (debug_info_level <= DINFO_LEVEL_TERSE)
19292         break;
19293
19294       /* In the special case of a TYPE_DECL node representing the declaration
19295          of some type tag, if the given TYPE_DECL is marked as having been
19296          instantiated from some other (original) TYPE_DECL node (e.g. one which
19297          was generated within the original definition of an inline function) we
19298          used to generate a special (abbreviated) DW_TAG_structure_type,
19299          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19300          should be actually referencing those DIEs, as variable DIEs with that
19301          type would be emitted already in the abstract origin, so it was always
19302          removed during unused type prunning.  Don't add anything in this
19303          case.  */
19304       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19305         break;
19306
19307       if (is_redundant_typedef (decl))
19308         gen_type_die (TREE_TYPE (decl), context_die);
19309       else
19310         /* Output a DIE to represent the typedef itself.  */
19311         gen_typedef_die (decl, context_die);
19312       break;
19313
19314     case LABEL_DECL:
19315       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19316         gen_label_die (decl, context_die);
19317       break;
19318
19319     case VAR_DECL:
19320     case RESULT_DECL:
19321       /* If we are in terse mode, don't generate any DIEs to represent any
19322          variable declarations or definitions.  */
19323       if (debug_info_level <= DINFO_LEVEL_TERSE)
19324         break;
19325
19326       /* Output any DIEs that are needed to specify the type of this data
19327          object.  */
19328       if (decl_by_reference_p (decl_or_origin))
19329         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19330       else
19331         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19332
19333       /* And its containing type.  */
19334       class_origin = decl_class_context (decl_or_origin);
19335       if (class_origin != NULL_TREE)
19336         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19337
19338       /* And its containing namespace.  */
19339       context_die = declare_in_namespace (decl_or_origin, context_die);
19340
19341       /* Now output the DIE to represent the data object itself.  This gets
19342          complicated because of the possibility that the VAR_DECL really
19343          represents an inlined instance of a formal parameter for an inline
19344          function.  */
19345       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19346       if (ultimate_origin != NULL_TREE
19347           && TREE_CODE (ultimate_origin) == PARM_DECL)
19348         gen_formal_parameter_die (decl, origin,
19349                                   true /* Emit name attribute.  */,
19350                                   context_die);
19351       else
19352         gen_variable_die (decl, origin, context_die);
19353       break;
19354
19355     case FIELD_DECL:
19356       /* Ignore the nameless fields that are used to skip bits but handle C++
19357          anonymous unions and structs.  */
19358       if (DECL_NAME (decl) != NULL_TREE
19359           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19360           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19361         {
19362           gen_type_die (member_declared_type (decl), context_die);
19363           gen_field_die (decl, context_die);
19364         }
19365       break;
19366
19367     case PARM_DECL:
19368       if (DECL_BY_REFERENCE (decl_or_origin))
19369         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19370       else
19371         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19372       return gen_formal_parameter_die (decl, origin,
19373                                        true /* Emit name attribute.  */,
19374                                        context_die);
19375
19376     case NAMESPACE_DECL:
19377     case IMPORTED_DECL:
19378       if (dwarf_version >= 3 || !dwarf_strict)
19379         gen_namespace_die (decl, context_die);
19380       break;
19381
19382     default:
19383       /* Probably some frontend-internal decl.  Assume we don't care.  */
19384       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19385       break;
19386     }
19387
19388   return NULL;
19389 }
19390 \f
19391 /* Output debug information for global decl DECL.  Called from toplev.c after
19392    compilation proper has finished.  */
19393
19394 static void
19395 dwarf2out_global_decl (tree decl)
19396 {
19397   /* Output DWARF2 information for file-scope tentative data object
19398      declarations, file-scope (extern) function declarations (which
19399      had no corresponding body) and file-scope tagged type declarations
19400      and definitions which have not yet been forced out.  */
19401   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19402     dwarf2out_decl (decl);
19403 }
19404
19405 /* Output debug information for type decl DECL.  Called from toplev.c
19406    and from language front ends (to record built-in types).  */
19407 static void
19408 dwarf2out_type_decl (tree decl, int local)
19409 {
19410   if (!local)
19411     dwarf2out_decl (decl);
19412 }
19413
19414 /* Output debug information for imported module or decl DECL.
19415    NAME is non-NULL name in the lexical block if the decl has been renamed.
19416    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19417    that DECL belongs to.
19418    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19419 static void
19420 dwarf2out_imported_module_or_decl_1 (tree decl,
19421                                      tree name,
19422                                      tree lexical_block,
19423                                      dw_die_ref lexical_block_die)
19424 {
19425   expanded_location xloc;
19426   dw_die_ref imported_die = NULL;
19427   dw_die_ref at_import_die;
19428
19429   if (TREE_CODE (decl) == IMPORTED_DECL)
19430     {
19431       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19432       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19433       gcc_assert (decl);
19434     }
19435   else
19436     xloc = expand_location (input_location);
19437
19438   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19439     {
19440       at_import_die = force_type_die (TREE_TYPE (decl));
19441       /* For namespace N { typedef void T; } using N::T; base_type_die
19442          returns NULL, but DW_TAG_imported_declaration requires
19443          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19444       if (!at_import_die)
19445         {
19446           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19447           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19448           at_import_die = lookup_type_die (TREE_TYPE (decl));
19449           gcc_assert (at_import_die);
19450         }
19451     }
19452   else
19453     {
19454       at_import_die = lookup_decl_die (decl);
19455       if (!at_import_die)
19456         {
19457           /* If we're trying to avoid duplicate debug info, we may not have
19458              emitted the member decl for this field.  Emit it now.  */
19459           if (TREE_CODE (decl) == FIELD_DECL)
19460             {
19461               tree type = DECL_CONTEXT (decl);
19462
19463               if (TYPE_CONTEXT (type)
19464                   && TYPE_P (TYPE_CONTEXT (type))
19465                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19466                                                 DINFO_USAGE_DIR_USE))
19467                 return;
19468               gen_type_die_for_member (type, decl,
19469                                        get_context_die (TYPE_CONTEXT (type)));
19470             }
19471           at_import_die = force_decl_die (decl);
19472         }
19473     }
19474
19475   if (TREE_CODE (decl) == NAMESPACE_DECL)
19476     {
19477       if (dwarf_version >= 3 || !dwarf_strict)
19478         imported_die = new_die (DW_TAG_imported_module,
19479                                 lexical_block_die,
19480                                 lexical_block);
19481       else
19482         return;
19483     }
19484   else
19485     imported_die = new_die (DW_TAG_imported_declaration,
19486                             lexical_block_die,
19487                             lexical_block);
19488
19489   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19490   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19491   if (name)
19492     add_AT_string (imported_die, DW_AT_name,
19493                    IDENTIFIER_POINTER (name));
19494   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19495 }
19496
19497 /* Output debug information for imported module or decl DECL.
19498    NAME is non-NULL name in context if the decl has been renamed.
19499    CHILD is true if decl is one of the renamed decls as part of
19500    importing whole module.  */
19501
19502 static void
19503 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19504                                    bool child)
19505 {
19506   /* dw_die_ref at_import_die;  */
19507   dw_die_ref scope_die;
19508
19509   if (debug_info_level <= DINFO_LEVEL_TERSE)
19510     return;
19511
19512   gcc_assert (decl);
19513
19514   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19515      We need decl DIE for reference and scope die. First, get DIE for the decl
19516      itself.  */
19517
19518   /* Get the scope die for decl context. Use comp_unit_die for global module
19519      or decl. If die is not found for non globals, force new die.  */
19520   if (context
19521       && TYPE_P (context)
19522       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19523     return;
19524
19525   if (!(dwarf_version >= 3 || !dwarf_strict))
19526     return;
19527
19528   scope_die = get_context_die (context);
19529
19530   if (child)
19531     {
19532       gcc_assert (scope_die->die_child);
19533       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19534       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19535       scope_die = scope_die->die_child;
19536     }
19537
19538   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19539   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19540
19541 }
19542
19543 /* Write the debugging output for DECL.  */
19544
19545 void
19546 dwarf2out_decl (tree decl)
19547 {
19548   dw_die_ref context_die = comp_unit_die ();
19549
19550   switch (TREE_CODE (decl))
19551     {
19552     case ERROR_MARK:
19553       return;
19554
19555     case FUNCTION_DECL:
19556       /* What we would really like to do here is to filter out all mere
19557          file-scope declarations of file-scope functions which are never
19558          referenced later within this translation unit (and keep all of ones
19559          that *are* referenced later on) but we aren't clairvoyant, so we have
19560          no idea which functions will be referenced in the future (i.e. later
19561          on within the current translation unit). So here we just ignore all
19562          file-scope function declarations which are not also definitions.  If
19563          and when the debugger needs to know something about these functions,
19564          it will have to hunt around and find the DWARF information associated
19565          with the definition of the function.
19566
19567          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19568          nodes represent definitions and which ones represent mere
19569          declarations.  We have to check DECL_INITIAL instead. That's because
19570          the C front-end supports some weird semantics for "extern inline"
19571          function definitions.  These can get inlined within the current
19572          translation unit (and thus, we need to generate Dwarf info for their
19573          abstract instances so that the Dwarf info for the concrete inlined
19574          instances can have something to refer to) but the compiler never
19575          generates any out-of-lines instances of such things (despite the fact
19576          that they *are* definitions).
19577
19578          The important point is that the C front-end marks these "extern
19579          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19580          them anyway. Note that the C++ front-end also plays some similar games
19581          for inline function definitions appearing within include files which
19582          also contain `#pragma interface' pragmas.  */
19583       if (DECL_INITIAL (decl) == NULL_TREE)
19584         return;
19585
19586       /* If we're a nested function, initially use a parent of NULL; if we're
19587          a plain function, this will be fixed up in decls_for_scope.  If
19588          we're a method, it will be ignored, since we already have a DIE.  */
19589       if (decl_function_context (decl)
19590           /* But if we're in terse mode, we don't care about scope.  */
19591           && debug_info_level > DINFO_LEVEL_TERSE)
19592         context_die = NULL;
19593       break;
19594
19595     case VAR_DECL:
19596       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19597          declaration and if the declaration was never even referenced from
19598          within this entire compilation unit.  We suppress these DIEs in
19599          order to save space in the .debug section (by eliminating entries
19600          which are probably useless).  Note that we must not suppress
19601          block-local extern declarations (whether used or not) because that
19602          would screw-up the debugger's name lookup mechanism and cause it to
19603          miss things which really ought to be in scope at a given point.  */
19604       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19605         return;
19606
19607       /* For local statics lookup proper context die.  */
19608       if (TREE_STATIC (decl)
19609           && DECL_CONTEXT (decl)
19610           && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
19611         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19612
19613       /* If we are in terse mode, don't generate any DIEs to represent any
19614          variable declarations or definitions.  */
19615       if (debug_info_level <= DINFO_LEVEL_TERSE)
19616         return;
19617       break;
19618
19619     case CONST_DECL:
19620       if (debug_info_level <= DINFO_LEVEL_TERSE)
19621         return;
19622       if (!is_fortran () && !is_ada ())
19623         return;
19624       if (TREE_STATIC (decl) && decl_function_context (decl))
19625         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19626       break;
19627
19628     case NAMESPACE_DECL:
19629     case IMPORTED_DECL:
19630       if (debug_info_level <= DINFO_LEVEL_TERSE)
19631         return;
19632       if (lookup_decl_die (decl) != NULL)
19633         return;
19634       break;
19635
19636     case TYPE_DECL:
19637       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19638       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19639         return;
19640
19641       /* Don't bother trying to generate any DIEs to represent any of the
19642          normal built-in types for the language we are compiling.  */
19643       if (DECL_IS_BUILTIN (decl))
19644         return;
19645
19646       /* If we are in terse mode, don't generate any DIEs for types.  */
19647       if (debug_info_level <= DINFO_LEVEL_TERSE)
19648         return;
19649
19650       /* If we're a function-scope tag, initially use a parent of NULL;
19651          this will be fixed up in decls_for_scope.  */
19652       if (decl_function_context (decl))
19653         context_die = NULL;
19654
19655       break;
19656
19657     default:
19658       return;
19659     }
19660
19661   gen_decl_die (decl, NULL, context_die);
19662 }
19663
19664 /* Write the debugging output for DECL.  */
19665
19666 static void
19667 dwarf2out_function_decl (tree decl)
19668 {
19669   dwarf2out_decl (decl);
19670   call_arg_locations = NULL;
19671   call_arg_loc_last = NULL;
19672   call_site_count = -1;
19673   tail_call_site_count = -1;
19674   VEC_free (dw_die_ref, heap, block_map);
19675   htab_empty (decl_loc_table);
19676   htab_empty (cached_dw_loc_list_table);
19677 }
19678
19679 /* Output a marker (i.e. a label) for the beginning of the generated code for
19680    a lexical block.  */
19681
19682 static void
19683 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19684                        unsigned int blocknum)
19685 {
19686   switch_to_section (current_function_section ());
19687   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19688 }
19689
19690 /* Output a marker (i.e. a label) for the end of the generated code for a
19691    lexical block.  */
19692
19693 static void
19694 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19695 {
19696   switch_to_section (current_function_section ());
19697   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19698 }
19699
19700 /* Returns nonzero if it is appropriate not to emit any debugging
19701    information for BLOCK, because it doesn't contain any instructions.
19702
19703    Don't allow this for blocks with nested functions or local classes
19704    as we would end up with orphans, and in the presence of scheduling
19705    we may end up calling them anyway.  */
19706
19707 static bool
19708 dwarf2out_ignore_block (const_tree block)
19709 {
19710   tree decl;
19711   unsigned int i;
19712
19713   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19714     if (TREE_CODE (decl) == FUNCTION_DECL
19715         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19716       return 0;
19717   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19718     {
19719       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19720       if (TREE_CODE (decl) == FUNCTION_DECL
19721           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19722       return 0;
19723     }
19724
19725   return 1;
19726 }
19727
19728 /* Hash table routines for file_hash.  */
19729
19730 static int
19731 file_table_eq (const void *p1_p, const void *p2_p)
19732 {
19733   const struct dwarf_file_data *const p1 =
19734     (const struct dwarf_file_data *) p1_p;
19735   const char *const p2 = (const char *) p2_p;
19736   return filename_cmp (p1->filename, p2) == 0;
19737 }
19738
19739 static hashval_t
19740 file_table_hash (const void *p_p)
19741 {
19742   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19743   return htab_hash_string (p->filename);
19744 }
19745
19746 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19747    dwarf2out.c) and return its "index".  The index of each (known) filename is
19748    just a unique number which is associated with only that one filename.  We
19749    need such numbers for the sake of generating labels (in the .debug_sfnames
19750    section) and references to those files numbers (in the .debug_srcinfo
19751    and.debug_macinfo sections).  If the filename given as an argument is not
19752    found in our current list, add it to the list and assign it the next
19753    available unique index number.  In order to speed up searches, we remember
19754    the index of the filename was looked up last.  This handles the majority of
19755    all searches.  */
19756
19757 static struct dwarf_file_data *
19758 lookup_filename (const char *file_name)
19759 {
19760   void ** slot;
19761   struct dwarf_file_data * created;
19762
19763   /* Check to see if the file name that was searched on the previous
19764      call matches this file name.  If so, return the index.  */
19765   if (file_table_last_lookup
19766       && (file_name == file_table_last_lookup->filename
19767           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19768     return file_table_last_lookup;
19769
19770   /* Didn't match the previous lookup, search the table.  */
19771   slot = htab_find_slot_with_hash (file_table, file_name,
19772                                    htab_hash_string (file_name), INSERT);
19773   if (*slot)
19774     return (struct dwarf_file_data *) *slot;
19775
19776   created = ggc_alloc_dwarf_file_data ();
19777   created->filename = file_name;
19778   created->emitted_number = 0;
19779   *slot = created;
19780   return created;
19781 }
19782
19783 /* If the assembler will construct the file table, then translate the compiler
19784    internal file table number into the assembler file table number, and emit
19785    a .file directive if we haven't already emitted one yet.  The file table
19786    numbers are different because we prune debug info for unused variables and
19787    types, which may include filenames.  */
19788
19789 static int
19790 maybe_emit_file (struct dwarf_file_data * fd)
19791 {
19792   if (! fd->emitted_number)
19793     {
19794       if (last_emitted_file)
19795         fd->emitted_number = last_emitted_file->emitted_number + 1;
19796       else
19797         fd->emitted_number = 1;
19798       last_emitted_file = fd;
19799
19800       if (DWARF2_ASM_LINE_DEBUG_INFO)
19801         {
19802           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19803           output_quoted_string (asm_out_file,
19804                                 remap_debug_filename (fd->filename));
19805           fputc ('\n', asm_out_file);
19806         }
19807     }
19808
19809   return fd->emitted_number;
19810 }
19811
19812 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19813    That generation should happen after function debug info has been
19814    generated. The value of the attribute is the constant value of ARG.  */
19815
19816 static void
19817 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19818 {
19819   die_arg_entry entry;
19820
19821   if (!die || !arg)
19822     return;
19823
19824   if (!tmpl_value_parm_die_table)
19825     tmpl_value_parm_die_table
19826       = VEC_alloc (die_arg_entry, gc, 32);
19827
19828   entry.die = die;
19829   entry.arg = arg;
19830   VEC_safe_push (die_arg_entry, gc,
19831                  tmpl_value_parm_die_table,
19832                  &entry);
19833 }
19834
19835 /* Return TRUE if T is an instance of generic type, FALSE
19836    otherwise.  */
19837
19838 static bool
19839 generic_type_p (tree t)
19840 {
19841   if (t == NULL_TREE || !TYPE_P (t))
19842     return false;
19843   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
19844 }
19845
19846 /* Schedule the generation of the generic parameter dies for the
19847   instance of generic type T. The proper generation itself is later
19848   done by gen_scheduled_generic_parms_dies. */
19849
19850 static void
19851 schedule_generic_params_dies_gen (tree t)
19852 {
19853   if (!generic_type_p (t))
19854     return;
19855
19856   if (generic_type_instances == NULL)
19857     generic_type_instances = VEC_alloc (tree, gc, 256);
19858
19859   VEC_safe_push (tree, gc, generic_type_instances, t);
19860 }
19861
19862 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19863    by append_entry_to_tmpl_value_parm_die_table. This function must
19864    be called after function DIEs have been generated.  */
19865
19866 static void
19867 gen_remaining_tmpl_value_param_die_attribute (void)
19868 {
19869   if (tmpl_value_parm_die_table)
19870     {
19871       unsigned i;
19872       die_arg_entry *e;
19873
19874       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
19875         tree_add_const_value_attribute (e->die, e->arg);
19876     }
19877 }
19878
19879 /* Generate generic parameters DIEs for instances of generic types
19880    that have been previously scheduled by
19881    schedule_generic_params_dies_gen. This function must be called
19882    after all the types of the CU have been laid out.  */
19883
19884 static void
19885 gen_scheduled_generic_parms_dies (void)
19886 {
19887   unsigned i;
19888   tree t;
19889
19890   if (generic_type_instances == NULL)
19891     return;
19892   
19893   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
19894     gen_generic_params_dies (t);
19895 }
19896
19897
19898 /* Replace DW_AT_name for the decl with name.  */
19899
19900 static void
19901 dwarf2out_set_name (tree decl, tree name)
19902 {
19903   dw_die_ref die;
19904   dw_attr_ref attr;
19905   const char *dname;
19906
19907   die = TYPE_SYMTAB_DIE (decl);
19908   if (!die)
19909     return;
19910
19911   dname = dwarf2_name (name, 0);
19912   if (!dname)
19913     return;
19914
19915   attr = get_AT (die, DW_AT_name);
19916   if (attr)
19917     {
19918       struct indirect_string_node *node;
19919
19920       node = find_AT_string (dname);
19921       /* replace the string.  */
19922       attr->dw_attr_val.v.val_str = node;
19923     }
19924
19925   else
19926     add_name_attribute (die, dname);
19927 }
19928
19929 /* Called by the final INSN scan whenever we see a var location.  We
19930    use it to drop labels in the right places, and throw the location in
19931    our lookup table.  */
19932
19933 static void
19934 dwarf2out_var_location (rtx loc_note)
19935 {
19936   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
19937   struct var_loc_node *newloc;
19938   rtx next_real, next_note;
19939   static const char *last_label;
19940   static const char *last_postcall_label;
19941   static bool last_in_cold_section_p;
19942   static rtx expected_next_loc_note;
19943   tree decl;
19944   bool var_loc_p;
19945
19946   if (!NOTE_P (loc_note))
19947     {
19948       if (CALL_P (loc_note))
19949         {
19950           call_site_count++;
19951           if (SIBLING_CALL_P (loc_note))
19952             tail_call_site_count++;
19953         }
19954       return;
19955     }
19956
19957   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
19958   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
19959     return;
19960
19961   /* Optimize processing a large consecutive sequence of location
19962      notes so we don't spend too much time in next_real_insn.  If the
19963      next insn is another location note, remember the next_real_insn
19964      calculation for next time.  */
19965   next_real = cached_next_real_insn;
19966   if (next_real)
19967     {
19968       if (expected_next_loc_note != loc_note)
19969         next_real = NULL_RTX;
19970     }
19971
19972   next_note = NEXT_INSN (loc_note);
19973   if (! next_note
19974       || INSN_DELETED_P (next_note)
19975       || GET_CODE (next_note) != NOTE
19976       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
19977           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
19978     next_note = NULL_RTX;
19979
19980   if (! next_real)
19981     next_real = next_real_insn (loc_note);
19982
19983   if (next_note)
19984     {
19985       expected_next_loc_note = next_note;
19986       cached_next_real_insn = next_real;
19987     }
19988   else
19989     cached_next_real_insn = NULL_RTX;
19990
19991   /* If there are no instructions which would be affected by this note,
19992      don't do anything.  */
19993   if (var_loc_p
19994       && next_real == NULL_RTX
19995       && !NOTE_DURING_CALL_P (loc_note))
19996     return;
19997
19998   if (next_real == NULL_RTX)
19999     next_real = get_last_insn ();
20000
20001   /* If there were any real insns between note we processed last time
20002      and this note (or if it is the first note), clear
20003      last_{,postcall_}label so that they are not reused this time.  */
20004   if (last_var_location_insn == NULL_RTX
20005       || last_var_location_insn != next_real
20006       || last_in_cold_section_p != in_cold_section_p)
20007     {
20008       last_label = NULL;
20009       last_postcall_label = NULL;
20010     }
20011
20012   if (var_loc_p)
20013     {
20014       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20015       newloc = add_var_loc_to_decl (decl, loc_note,
20016                                     NOTE_DURING_CALL_P (loc_note)
20017                                     ? last_postcall_label : last_label);
20018       if (newloc == NULL)
20019         return;
20020     }
20021   else
20022     {
20023       decl = NULL_TREE;
20024       newloc = NULL;
20025     }
20026
20027   /* If there were no real insns between note we processed last time
20028      and this note, use the label we emitted last time.  Otherwise
20029      create a new label and emit it.  */
20030   if (last_label == NULL)
20031     {
20032       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20033       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20034       loclabel_num++;
20035       last_label = ggc_strdup (loclabel);
20036     }
20037
20038   if (!var_loc_p)
20039     {
20040       struct call_arg_loc_node *ca_loc
20041         = ggc_alloc_cleared_call_arg_loc_node ();
20042       rtx prev = prev_real_insn (loc_note), x;
20043       ca_loc->call_arg_loc_note = loc_note;
20044       ca_loc->next = NULL;
20045       ca_loc->label = last_label;
20046       gcc_assert (prev
20047                   && (CALL_P (prev)
20048                       || (NONJUMP_INSN_P (prev)
20049                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20050                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20051       if (!CALL_P (prev))
20052         prev = XVECEXP (PATTERN (prev), 0, 0);
20053       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20054       x = PATTERN (prev);
20055       if (GET_CODE (x) == PARALLEL)
20056         x = XVECEXP (x, 0, 0);
20057       if (GET_CODE (x) == SET)
20058         x = SET_SRC (x);
20059       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20060         {
20061           x = XEXP (XEXP (x, 0), 0);
20062           if (GET_CODE (x) == SYMBOL_REF
20063               && SYMBOL_REF_DECL (x)
20064               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20065             ca_loc->symbol_ref = x;
20066         }
20067       ca_loc->block = insn_scope (prev);
20068       if (call_arg_locations)
20069         call_arg_loc_last->next = ca_loc;
20070       else
20071         call_arg_locations = ca_loc;
20072       call_arg_loc_last = ca_loc;
20073     }
20074   else if (!NOTE_DURING_CALL_P (loc_note))
20075     newloc->label = last_label;
20076   else
20077     {
20078       if (!last_postcall_label)
20079         {
20080           sprintf (loclabel, "%s-1", last_label);
20081           last_postcall_label = ggc_strdup (loclabel);
20082         }
20083       newloc->label = last_postcall_label;
20084     }
20085
20086   last_var_location_insn = next_real;
20087   last_in_cold_section_p = in_cold_section_p;
20088 }
20089
20090 /* Note in one location list that text section has changed.  */
20091
20092 static int
20093 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20094 {
20095   var_loc_list *list = (var_loc_list *) *slot;
20096   if (list->first)
20097     list->last_before_switch
20098       = list->last->next ? list->last->next : list->last;
20099   return 1;
20100 }
20101
20102 /* Note in all location lists that text section has changed.  */
20103
20104 static void
20105 var_location_switch_text_section (void)
20106 {
20107   if (decl_loc_table == NULL)
20108     return;
20109
20110   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20111 }
20112
20113 /* Create a new line number table.  */
20114
20115 static dw_line_info_table *
20116 new_line_info_table (void)
20117 {
20118   dw_line_info_table *table;
20119
20120   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20121   table->file_num = 1;
20122   table->line_num = 1;
20123   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20124
20125   return table;
20126 }
20127
20128 /* Lookup the "current" table into which we emit line info, so
20129    that we don't have to do it for every source line.  */
20130
20131 static void
20132 set_cur_line_info_table (section *sec)
20133 {
20134   dw_line_info_table *table;
20135
20136   if (sec == text_section)
20137     table = text_section_line_info;
20138   else if (sec == cold_text_section)
20139     {
20140       table = cold_text_section_line_info;
20141       if (!table)
20142         {
20143           cold_text_section_line_info = table = new_line_info_table ();
20144           table->end_label = cold_end_label;
20145         }
20146     }
20147   else
20148     {
20149       const char *end_label;
20150
20151       if (flag_reorder_blocks_and_partition)
20152         {
20153           if (in_cold_section_p)
20154             end_label = crtl->subsections.cold_section_end_label;
20155           else
20156             end_label = crtl->subsections.hot_section_end_label;
20157         }
20158       else
20159         {
20160           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20161           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20162                                        current_function_funcdef_no);
20163           end_label = ggc_strdup (label);
20164         }
20165
20166       table = new_line_info_table ();
20167       table->end_label = end_label;
20168
20169       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20170     }
20171
20172   if (DWARF2_ASM_LINE_DEBUG_INFO)
20173     table->is_stmt = (cur_line_info_table
20174                       ? cur_line_info_table->is_stmt
20175                       : DWARF_LINE_DEFAULT_IS_STMT_START);
20176   cur_line_info_table = table;
20177 }
20178
20179
20180 /* We need to reset the locations at the beginning of each
20181    function. We can't do this in the end_function hook, because the
20182    declarations that use the locations won't have been output when
20183    that hook is called.  Also compute have_multiple_function_sections here.  */
20184
20185 static void
20186 dwarf2out_begin_function (tree fun)
20187 {
20188   section *sec = function_section (fun);
20189
20190   if (sec != text_section)
20191     have_multiple_function_sections = true;
20192
20193   if (flag_reorder_blocks_and_partition && !cold_text_section)
20194     {
20195       gcc_assert (current_function_decl == fun);
20196       cold_text_section = unlikely_text_section ();
20197       switch_to_section (cold_text_section);
20198       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20199       switch_to_section (sec);
20200     }
20201
20202   dwarf2out_note_section_used ();
20203   call_site_count = 0;
20204   tail_call_site_count = 0;
20205
20206   set_cur_line_info_table (sec);
20207 }
20208
20209 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20210
20211 static void
20212 push_dw_line_info_entry (dw_line_info_table *table,
20213                          enum dw_line_info_opcode opcode, unsigned int val)
20214 {
20215   dw_line_info_entry e;
20216   e.opcode = opcode;
20217   e.val = val;
20218   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20219 }
20220
20221 /* Output a label to mark the beginning of a source code line entry
20222    and record information relating to this source line, in
20223    'line_info_table' for later output of the .debug_line section.  */
20224 /* ??? The discriminator parameter ought to be unsigned.  */
20225
20226 static void
20227 dwarf2out_source_line (unsigned int line, const char *filename,
20228                        int discriminator, bool is_stmt)
20229 {
20230   unsigned int file_num;
20231   dw_line_info_table *table;
20232
20233   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20234     return;
20235
20236   /* The discriminator column was added in dwarf4.  Simplify the below
20237      by simply removing it if we're not supposed to output it.  */
20238   if (dwarf_version < 4 && dwarf_strict)
20239     discriminator = 0;
20240
20241   table = cur_line_info_table;
20242   file_num = maybe_emit_file (lookup_filename (filename));
20243
20244   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20245      the debugger has used the second (possibly duplicate) line number
20246      at the beginning of the function to mark the end of the prologue.
20247      We could eliminate any other duplicates within the function.  For
20248      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20249      that second line number entry.  */
20250   /* Recall that this end-of-prologue indication is *not* the same thing
20251      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20252      to which the hook corresponds, follows the last insn that was 
20253      emitted by gen_prologue.  What we need is to precede the first insn
20254      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20255      insn that corresponds to something the user wrote.  These may be
20256      very different locations once scheduling is enabled.  */
20257
20258   if (0 && file_num == table->file_num
20259       && line == table->line_num
20260       && discriminator == table->discrim_num
20261       && is_stmt == table->is_stmt)
20262     return;
20263
20264   switch_to_section (current_function_section ());
20265
20266   /* If requested, emit something human-readable.  */
20267   if (flag_debug_asm)
20268     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20269
20270   if (DWARF2_ASM_LINE_DEBUG_INFO)
20271     {
20272       /* Emit the .loc directive understood by GNU as.  */
20273       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
20274          file_num, line, is_stmt, discriminator */
20275       fputs ("\t.loc ", asm_out_file);
20276       fprint_ul (asm_out_file, file_num);
20277       putc (' ', asm_out_file);
20278       fprint_ul (asm_out_file, line);
20279       putc (' ', asm_out_file);
20280       putc ('0', asm_out_file);
20281
20282       if (is_stmt != table->is_stmt)
20283         {
20284           fputs (" is_stmt ", asm_out_file);
20285           putc (is_stmt ? '1' : '0', asm_out_file);
20286         }
20287       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20288         {
20289           gcc_assert (discriminator > 0);
20290           fputs (" discriminator ", asm_out_file);
20291           fprint_ul (asm_out_file, (unsigned long) discriminator);
20292         }
20293       putc ('\n', asm_out_file);
20294     }
20295   else
20296     {
20297       unsigned int label_num = ++line_info_label_num;
20298
20299       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20300
20301       push_dw_line_info_entry (table, LI_set_address, label_num);
20302       if (file_num != table->file_num)
20303         push_dw_line_info_entry (table, LI_set_file, file_num);
20304       if (discriminator != table->discrim_num)
20305         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20306       if (is_stmt != table->is_stmt)
20307         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20308       push_dw_line_info_entry (table, LI_set_line, line);
20309     }
20310
20311   table->file_num = file_num;
20312   table->line_num = line;
20313   table->discrim_num = discriminator;
20314   table->is_stmt = is_stmt;
20315   table->in_use = true;
20316 }
20317
20318 /* Record the beginning of a new source file.  */
20319
20320 static void
20321 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20322 {
20323   if (flag_eliminate_dwarf2_dups)
20324     {
20325       /* Record the beginning of the file for break_out_includes.  */
20326       dw_die_ref bincl_die;
20327
20328       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20329       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20330     }
20331
20332   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20333     {
20334       macinfo_entry e;
20335       e.code = DW_MACINFO_start_file;
20336       e.lineno = lineno;
20337       e.info = ggc_strdup (filename);
20338       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20339     }
20340 }
20341
20342 /* Record the end of a source file.  */
20343
20344 static void
20345 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20346 {
20347   if (flag_eliminate_dwarf2_dups)
20348     /* Record the end of the file for break_out_includes.  */
20349     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20350
20351   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20352     {
20353       macinfo_entry e;
20354       e.code = DW_MACINFO_end_file;
20355       e.lineno = lineno;
20356       e.info = NULL;
20357       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20358     }
20359 }
20360
20361 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20362    the tail part of the directive line, i.e. the part which is past the
20363    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20364
20365 static void
20366 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20367                   const char *buffer ATTRIBUTE_UNUSED)
20368 {
20369   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20370     {
20371       macinfo_entry e;
20372       /* Insert a dummy first entry to be able to optimize the whole
20373          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20374       if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20375         {
20376           e.code = 0;
20377           e.lineno = 0;
20378           e.info = NULL;
20379           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20380         }
20381       e.code = DW_MACINFO_define;
20382       e.lineno = lineno;
20383       e.info = ggc_strdup (buffer);
20384       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20385     }
20386 }
20387
20388 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20389    the tail part of the directive line, i.e. the part which is past the
20390    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20391
20392 static void
20393 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20394                  const char *buffer ATTRIBUTE_UNUSED)
20395 {
20396   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20397     {
20398       macinfo_entry e;
20399       /* Insert a dummy first entry to be able to optimize the whole
20400          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20401       if (VEC_empty (macinfo_entry, macinfo_table) && lineno <= 1)
20402         {
20403           e.code = 0;
20404           e.lineno = 0;
20405           e.info = NULL;
20406           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20407         }
20408       e.code = DW_MACINFO_undef;
20409       e.lineno = lineno;
20410       e.info = ggc_strdup (buffer);
20411       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20412     }
20413 }
20414
20415 /* Routines to manipulate hash table of CUs.  */
20416
20417 static hashval_t
20418 htab_macinfo_hash (const void *of)
20419 {
20420   const macinfo_entry *const entry =
20421     (const macinfo_entry *) of;
20422
20423   return htab_hash_string (entry->info);
20424 }
20425
20426 static int
20427 htab_macinfo_eq (const void *of1, const void *of2)
20428 {
20429   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20430   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20431
20432   return !strcmp (entry1->info, entry2->info);
20433 }
20434
20435 /* Output a single .debug_macinfo entry.  */
20436
20437 static void
20438 output_macinfo_op (macinfo_entry *ref)
20439 {
20440   int file_num;
20441   size_t len;
20442   struct indirect_string_node *node;
20443   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20444   struct dwarf_file_data *fd;
20445
20446   switch (ref->code)
20447     {
20448     case DW_MACINFO_start_file:
20449       fd = lookup_filename (ref->info);
20450       file_num = maybe_emit_file (fd);
20451       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20452       dw2_asm_output_data_uleb128 (ref->lineno,
20453                                    "Included from line number %lu", 
20454                                    (unsigned long) ref->lineno);
20455       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20456       break;
20457     case DW_MACINFO_end_file:
20458       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20459       break;
20460     case DW_MACINFO_define:
20461     case DW_MACINFO_undef:
20462       len = strlen (ref->info) + 1;
20463       if (!dwarf_strict
20464           && len > DWARF_OFFSET_SIZE
20465           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20466           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20467         {
20468           ref->code = ref->code == DW_MACINFO_define
20469                       ? DW_MACRO_GNU_define_indirect
20470                       : DW_MACRO_GNU_undef_indirect;
20471           output_macinfo_op (ref);
20472           return;
20473         }
20474       dw2_asm_output_data (1, ref->code,
20475                            ref->code == DW_MACINFO_define
20476                            ? "Define macro" : "Undefine macro");
20477       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20478                                    (unsigned long) ref->lineno);
20479       dw2_asm_output_nstring (ref->info, -1, "The macro");
20480       break;
20481     case DW_MACRO_GNU_define_indirect:
20482     case DW_MACRO_GNU_undef_indirect:
20483       node = find_AT_string (ref->info);
20484       if (node->form != DW_FORM_strp)
20485         {
20486           char label[32];
20487           ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20488           ++dw2_string_counter;
20489           node->label = xstrdup (label);
20490           node->form = DW_FORM_strp;
20491         }
20492       dw2_asm_output_data (1, ref->code,
20493                            ref->code == DW_MACRO_GNU_define_indirect
20494                            ? "Define macro indirect"
20495                            : "Undefine macro indirect");
20496       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20497                                    (unsigned long) ref->lineno);
20498       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20499                              debug_str_section, "The macro: \"%s\"",
20500                              ref->info);
20501       break;
20502     case DW_MACRO_GNU_transparent_include:
20503       dw2_asm_output_data (1, ref->code, "Transparent include");
20504       ASM_GENERATE_INTERNAL_LABEL (label,
20505                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20506       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20507       break;
20508     default:
20509       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20510                ASM_COMMENT_START, (unsigned long) ref->code);
20511       break;
20512     }
20513 }
20514
20515 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20516    other compilation unit .debug_macinfo sections.  IDX is the first
20517    index of a define/undef, return the number of ops that should be
20518    emitted in a comdat .debug_macinfo section and emit
20519    a DW_MACRO_GNU_transparent_include entry referencing it.
20520    If the define/undef entry should be emitted normally, return 0.  */
20521
20522 static unsigned
20523 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20524                         htab_t *macinfo_htab)
20525 {
20526   macinfo_entry *first, *second, *cur, *inc;
20527   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20528   unsigned char checksum[16];
20529   struct md5_ctx ctx;
20530   char *grp_name, *tail;
20531   const char *base;
20532   unsigned int i, count, encoded_filename_len, linebuf_len;
20533   void **slot;
20534
20535   first = VEC_index (macinfo_entry, macinfo_table, idx);
20536   second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
20537
20538   /* Optimize only if there are at least two consecutive define/undef ops,
20539      and either all of them are before first DW_MACINFO_start_file
20540      with lineno {0,1} (i.e. predefined macro block), or all of them are
20541      in some included header file.  */
20542   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20543     return 0;
20544   if (VEC_empty (macinfo_entry, files))
20545     {
20546       if (first->lineno > 1 || second->lineno > 1)
20547         return 0;
20548     }
20549   else if (first->lineno == 0)
20550     return 0;
20551
20552   /* Find the last define/undef entry that can be grouped together
20553      with first and at the same time compute md5 checksum of their
20554      codes, linenumbers and strings.  */
20555   md5_init_ctx (&ctx);
20556   for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20557     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20558       break;
20559     else if (VEC_empty (macinfo_entry, files) && cur->lineno > 1)
20560       break;
20561     else
20562       {
20563         unsigned char code = cur->code;
20564         md5_process_bytes (&code, 1, &ctx);
20565         checksum_uleb128 (cur->lineno, &ctx);
20566         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20567       }
20568   md5_finish_ctx (&ctx, checksum);
20569   count = i - idx;
20570
20571   /* From the containing include filename (if any) pick up just
20572      usable characters from its basename.  */
20573   if (VEC_empty (macinfo_entry, files))
20574     base = "";
20575   else
20576     base = lbasename (VEC_last (macinfo_entry, files)->info);
20577   for (encoded_filename_len = 0, i = 0; base[i]; i++)
20578     if (ISIDNUM (base[i]) || base[i] == '.')
20579       encoded_filename_len++;
20580   /* Count . at the end.  */
20581   if (encoded_filename_len)
20582     encoded_filename_len++;
20583
20584   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20585   linebuf_len = strlen (linebuf);
20586
20587   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
20588   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20589                          + 16 * 2 + 1);
20590   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20591   tail = grp_name + 4;
20592   if (encoded_filename_len)
20593     {
20594       for (i = 0; base[i]; i++)
20595         if (ISIDNUM (base[i]) || base[i] == '.')
20596           *tail++ = base[i];
20597       *tail++ = '.';
20598     }
20599   memcpy (tail, linebuf, linebuf_len);
20600   tail += linebuf_len;
20601   *tail++ = '.';
20602   for (i = 0; i < 16; i++)
20603     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20604
20605   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20606      in the empty vector entry before the first define/undef.  */
20607   inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
20608   inc->code = DW_MACRO_GNU_transparent_include;
20609   inc->lineno = 0;
20610   inc->info = ggc_strdup (grp_name);
20611   if (*macinfo_htab == NULL)
20612     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20613   /* Avoid emitting duplicates.  */
20614   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20615   if (*slot != NULL)
20616     {
20617       inc->code = 0;
20618       inc->info = NULL;
20619       /* If such an entry has been used before, just emit
20620          a DW_MACRO_GNU_transparent_include op.  */
20621       inc = (macinfo_entry *) *slot;
20622       output_macinfo_op (inc);
20623       /* And clear all macinfo_entry in the range to avoid emitting them
20624          in the second pass.  */
20625       for (i = idx;
20626            VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20627            && i < idx + count;
20628            i++)
20629         {
20630           cur->code = 0;
20631           cur->info = NULL;
20632         }
20633     }
20634   else
20635     {
20636       *slot = inc;
20637       inc->lineno = htab_elements (*macinfo_htab);
20638       output_macinfo_op (inc);
20639     }
20640   return count;
20641 }
20642
20643 /* Output macinfo section(s).  */
20644
20645 static void
20646 output_macinfo (void)
20647 {
20648   unsigned i;
20649   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20650   macinfo_entry *ref;
20651   VEC (macinfo_entry, gc) *files = NULL;
20652   htab_t macinfo_htab = NULL;
20653
20654   if (! length)
20655     return;
20656
20657   /* output_macinfo* uses these interchangeably.  */
20658   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20659               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20660               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20661               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20662
20663   /* For .debug_macro emit the section header.  */
20664   if (!dwarf_strict)
20665     {
20666       dw2_asm_output_data (2, 4, "DWARF macro version number");
20667       if (DWARF_OFFSET_SIZE == 8)
20668         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20669       else
20670         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20671       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20672                              debug_line_section, NULL);
20673     }
20674
20675   /* In the first loop, it emits the primary .debug_macinfo section
20676      and after each emitted op the macinfo_entry is cleared.
20677      If a longer range of define/undef ops can be optimized using
20678      DW_MACRO_GNU_transparent_include, the
20679      DW_MACRO_GNU_transparent_include op is emitted and kept in
20680      the vector before the first define/undef in the range and the
20681      whole range of define/undef ops is not emitted and kept.  */
20682   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20683     {
20684       switch (ref->code)
20685         {
20686         case DW_MACINFO_start_file:
20687           VEC_safe_push (macinfo_entry, gc, files, ref);
20688           break;
20689         case DW_MACINFO_end_file:
20690           if (!VEC_empty (macinfo_entry, files))
20691             VEC_pop (macinfo_entry, files);
20692           break;
20693         case DW_MACINFO_define:
20694         case DW_MACINFO_undef:
20695           if (!dwarf_strict
20696               && HAVE_COMDAT_GROUP
20697               && VEC_length (macinfo_entry, files) != 1
20698               && i > 0
20699               && i + 1 < length
20700               && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
20701             {
20702               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20703               if (count)
20704                 {
20705                   i += count - 1;
20706                   continue;
20707                 }
20708             }
20709           break;
20710         case 0:
20711           /* A dummy entry may be inserted at the beginning to be able
20712              to optimize the whole block of predefined macros.  */
20713           if (i == 0)
20714             continue;
20715         default:
20716           break;
20717         }
20718       output_macinfo_op (ref);
20719       ref->info = NULL;
20720       ref->code = 0;
20721     }
20722
20723   if (macinfo_htab == NULL)
20724     return;
20725
20726   htab_delete (macinfo_htab);
20727
20728   /* If any DW_MACRO_GNU_transparent_include were used, on those
20729      DW_MACRO_GNU_transparent_include entries terminate the
20730      current chain and switch to a new comdat .debug_macinfo
20731      section and emit the define/undef entries within it.  */
20732   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20733     switch (ref->code)
20734       {
20735       case 0:
20736         continue;
20737       case DW_MACRO_GNU_transparent_include:
20738         {
20739           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20740           tree comdat_key = get_identifier (ref->info);
20741           /* Terminate the previous .debug_macinfo section.  */
20742           dw2_asm_output_data (1, 0, "End compilation unit");
20743           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20744                                          SECTION_DEBUG
20745                                          | SECTION_LINKONCE,
20746                                          comdat_key);
20747           ASM_GENERATE_INTERNAL_LABEL (label,
20748                                        DEBUG_MACRO_SECTION_LABEL,
20749                                        ref->lineno);
20750           ASM_OUTPUT_LABEL (asm_out_file, label);
20751           ref->code = 0;
20752           ref->info = NULL;
20753           dw2_asm_output_data (2, 4, "DWARF macro version number");
20754           if (DWARF_OFFSET_SIZE == 8)
20755             dw2_asm_output_data (1, 1, "Flags: 64-bit");
20756           else
20757             dw2_asm_output_data (1, 0, "Flags: 32-bit");
20758         }
20759         break;
20760       case DW_MACINFO_define:
20761       case DW_MACINFO_undef:
20762         output_macinfo_op (ref);
20763         ref->code = 0;
20764         ref->info = NULL;
20765         break;
20766       default:
20767         gcc_unreachable ();
20768       }
20769 }
20770
20771 /* Set up for Dwarf output at the start of compilation.  */
20772
20773 static void
20774 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20775 {
20776   /* Allocate the file_table.  */
20777   file_table = htab_create_ggc (50, file_table_hash,
20778                                 file_table_eq, NULL);
20779
20780   /* Allocate the decl_die_table.  */
20781   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20782                                     decl_die_table_eq, NULL);
20783
20784   /* Allocate the decl_loc_table.  */
20785   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20786                                     decl_loc_table_eq, NULL);
20787
20788   /* Allocate the cached_dw_loc_list_table.  */
20789   cached_dw_loc_list_table
20790     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20791                        cached_dw_loc_list_table_eq, NULL);
20792
20793   /* Allocate the initial hunk of the decl_scope_table.  */
20794   decl_scope_table = VEC_alloc (tree, gc, 256);
20795
20796   /* Allocate the initial hunk of the abbrev_die_table.  */
20797   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20798     (ABBREV_DIE_TABLE_INCREMENT);
20799   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20800   /* Zero-th entry is allocated, but unused.  */
20801   abbrev_die_table_in_use = 1;
20802
20803   /* Allocate the pubtypes and pubnames vectors.  */
20804   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20805   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20806
20807   incomplete_types = VEC_alloc (tree, gc, 64);
20808
20809   used_rtx_array = VEC_alloc (rtx, gc, 32);
20810
20811   debug_info_section = get_section (DEBUG_INFO_SECTION,
20812                                     SECTION_DEBUG, NULL);
20813   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20814                                       SECTION_DEBUG, NULL);
20815   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20816                                        SECTION_DEBUG, NULL);
20817   debug_macinfo_section = get_section (dwarf_strict
20818                                        ? DEBUG_MACINFO_SECTION
20819                                        : DEBUG_MACRO_SECTION,
20820                                        SECTION_DEBUG, NULL);
20821   debug_line_section = get_section (DEBUG_LINE_SECTION,
20822                                     SECTION_DEBUG, NULL);
20823   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20824                                    SECTION_DEBUG, NULL);
20825   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20826                                         SECTION_DEBUG, NULL);
20827   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20828                                         SECTION_DEBUG, NULL);
20829   debug_str_section = get_section (DEBUG_STR_SECTION,
20830                                    DEBUG_STR_SECTION_FLAGS, NULL);
20831   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20832                                       SECTION_DEBUG, NULL);
20833   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20834                                      SECTION_DEBUG, NULL);
20835
20836   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20837   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20838                                DEBUG_ABBREV_SECTION_LABEL, 0);
20839   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20840   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20841                                COLD_TEXT_SECTION_LABEL, 0);
20842   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20843
20844   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20845                                DEBUG_INFO_SECTION_LABEL, 0);
20846   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20847                                DEBUG_LINE_SECTION_LABEL, 0);
20848   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20849                                DEBUG_RANGES_SECTION_LABEL, 0);
20850   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20851                                dwarf_strict
20852                                ? DEBUG_MACINFO_SECTION_LABEL
20853                                : DEBUG_MACRO_SECTION_LABEL, 0);
20854
20855   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20856     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20857
20858   switch_to_section (text_section);
20859   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20860
20861   /* Make sure the line number table for .text always exists.  */
20862   text_section_line_info = new_line_info_table ();
20863   text_section_line_info->end_label = text_end_label;
20864 }
20865
20866 /* Called before compile () starts outputtting functions, variables
20867    and toplevel asms into assembly.  */
20868
20869 static void
20870 dwarf2out_assembly_start (void)
20871 {
20872   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20873       && dwarf2out_do_cfi_asm ()
20874       && (!(flag_unwind_tables || flag_exceptions)
20875           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20876     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20877 }
20878
20879 /* A helper function for dwarf2out_finish called through
20880    htab_traverse.  Emit one queued .debug_str string.  */
20881
20882 static int
20883 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20884 {
20885   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20886
20887   if (node->form == DW_FORM_strp)
20888     {
20889       switch_to_section (debug_str_section);
20890       ASM_OUTPUT_LABEL (asm_out_file, node->label);
20891       assemble_string (node->str, strlen (node->str) + 1);
20892     }
20893
20894   return 1;
20895 }
20896
20897 #if ENABLE_ASSERT_CHECKING
20898 /* Verify that all marks are clear.  */
20899
20900 static void
20901 verify_marks_clear (dw_die_ref die)
20902 {
20903   dw_die_ref c;
20904
20905   gcc_assert (! die->die_mark);
20906   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20907 }
20908 #endif /* ENABLE_ASSERT_CHECKING */
20909
20910 /* Clear the marks for a die and its children.
20911    Be cool if the mark isn't set.  */
20912
20913 static void
20914 prune_unmark_dies (dw_die_ref die)
20915 {
20916   dw_die_ref c;
20917
20918   if (die->die_mark)
20919     die->die_mark = 0;
20920   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20921 }
20922
20923 /* Given DIE that we're marking as used, find any other dies
20924    it references as attributes and mark them as used.  */
20925
20926 static void
20927 prune_unused_types_walk_attribs (dw_die_ref die)
20928 {
20929   dw_attr_ref a;
20930   unsigned ix;
20931
20932   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20933     {
20934       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20935         {
20936           /* A reference to another DIE.
20937              Make sure that it will get emitted.
20938              If it was broken out into a comdat group, don't follow it.  */
20939           if (! AT_ref (a)->comdat_type_p
20940               || a->dw_attr == DW_AT_specification)
20941             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20942         }
20943       /* Set the string's refcount to 0 so that prune_unused_types_mark
20944          accounts properly for it.  */
20945       if (AT_class (a) == dw_val_class_str)
20946         a->dw_attr_val.v.val_str->refcount = 0;
20947     }
20948 }
20949
20950 /* Mark the generic parameters and arguments children DIEs of DIE.  */
20951
20952 static void
20953 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
20954 {
20955   dw_die_ref c;
20956
20957   if (die == NULL || die->die_child == NULL)
20958     return;
20959   c = die->die_child;
20960   do
20961     {
20962       switch (c->die_tag)
20963         {
20964         case DW_TAG_template_type_param:
20965         case DW_TAG_template_value_param:
20966         case DW_TAG_GNU_template_template_param:
20967         case DW_TAG_GNU_template_parameter_pack:
20968           prune_unused_types_mark (c, 1);
20969           break;
20970         default:
20971           break;
20972         }
20973       c = c->die_sib;
20974     } while (c && c != die->die_child);
20975 }
20976
20977 /* Mark DIE as being used.  If DOKIDS is true, then walk down
20978    to DIE's children.  */
20979
20980 static void
20981 prune_unused_types_mark (dw_die_ref die, int dokids)
20982 {
20983   dw_die_ref c;
20984
20985   if (die->die_mark == 0)
20986     {
20987       /* We haven't done this node yet.  Mark it as used.  */
20988       die->die_mark = 1;
20989       /* If this is the DIE of a generic type instantiation,
20990          mark the children DIEs that describe its generic parms and
20991          args.  */
20992       prune_unused_types_mark_generic_parms_dies (die);
20993
20994       /* We also have to mark its parents as used.
20995          (But we don't want to mark our parents' kids due to this.)  */
20996       if (die->die_parent)
20997         prune_unused_types_mark (die->die_parent, 0);
20998
20999       /* Mark any referenced nodes.  */
21000       prune_unused_types_walk_attribs (die);
21001
21002       /* If this node is a specification,
21003          also mark the definition, if it exists.  */
21004       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21005         prune_unused_types_mark (die->die_definition, 1);
21006     }
21007
21008   if (dokids && die->die_mark != 2)
21009     {
21010       /* We need to walk the children, but haven't done so yet.
21011          Remember that we've walked the kids.  */
21012       die->die_mark = 2;
21013
21014       /* If this is an array type, we need to make sure our
21015          kids get marked, even if they're types.  If we're
21016          breaking out types into comdat sections, do this
21017          for all type definitions.  */
21018       if (die->die_tag == DW_TAG_array_type
21019           || (use_debug_types
21020               && is_type_die (die) && ! is_declaration_die (die)))
21021         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21022       else
21023         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21024     }
21025 }
21026
21027 /* For local classes, look if any static member functions were emitted
21028    and if so, mark them.  */
21029
21030 static void
21031 prune_unused_types_walk_local_classes (dw_die_ref die)
21032 {
21033   dw_die_ref c;
21034
21035   if (die->die_mark == 2)
21036     return;
21037
21038   switch (die->die_tag)
21039     {
21040     case DW_TAG_structure_type:
21041     case DW_TAG_union_type:
21042     case DW_TAG_class_type:
21043       break;
21044
21045     case DW_TAG_subprogram:
21046       if (!get_AT_flag (die, DW_AT_declaration)
21047           || die->die_definition != NULL)
21048         prune_unused_types_mark (die, 1);
21049       return;
21050
21051     default:
21052       return;
21053     }
21054
21055   /* Mark children.  */
21056   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21057 }
21058
21059 /* Walk the tree DIE and mark types that we actually use.  */
21060
21061 static void
21062 prune_unused_types_walk (dw_die_ref die)
21063 {
21064   dw_die_ref c;
21065
21066   /* Don't do anything if this node is already marked and
21067      children have been marked as well.  */
21068   if (die->die_mark == 2)
21069     return;
21070
21071   switch (die->die_tag)
21072     {
21073     case DW_TAG_structure_type:
21074     case DW_TAG_union_type:
21075     case DW_TAG_class_type:
21076       if (die->die_perennial_p)
21077         break;
21078
21079       for (c = die->die_parent; c; c = c->die_parent)
21080         if (c->die_tag == DW_TAG_subprogram)
21081           break;
21082
21083       /* Finding used static member functions inside of classes
21084          is needed just for local classes, because for other classes
21085          static member function DIEs with DW_AT_specification
21086          are emitted outside of the DW_TAG_*_type.  If we ever change
21087          it, we'd need to call this even for non-local classes.  */
21088       if (c)
21089         prune_unused_types_walk_local_classes (die);
21090
21091       /* It's a type node --- don't mark it.  */
21092       return;
21093
21094     case DW_TAG_const_type:
21095     case DW_TAG_packed_type:
21096     case DW_TAG_pointer_type:
21097     case DW_TAG_reference_type:
21098     case DW_TAG_rvalue_reference_type:
21099     case DW_TAG_volatile_type:
21100     case DW_TAG_typedef:
21101     case DW_TAG_array_type:
21102     case DW_TAG_interface_type:
21103     case DW_TAG_friend:
21104     case DW_TAG_variant_part:
21105     case DW_TAG_enumeration_type:
21106     case DW_TAG_subroutine_type:
21107     case DW_TAG_string_type:
21108     case DW_TAG_set_type:
21109     case DW_TAG_subrange_type:
21110     case DW_TAG_ptr_to_member_type:
21111     case DW_TAG_file_type:
21112       if (die->die_perennial_p)
21113         break;
21114
21115       /* It's a type node --- don't mark it.  */
21116       return;
21117
21118     default:
21119       /* Mark everything else.  */
21120       break;
21121   }
21122
21123   if (die->die_mark == 0)
21124     {
21125       die->die_mark = 1;
21126
21127       /* Now, mark any dies referenced from here.  */
21128       prune_unused_types_walk_attribs (die);
21129     }
21130
21131   die->die_mark = 2;
21132
21133   /* Mark children.  */
21134   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21135 }
21136
21137 /* Increment the string counts on strings referred to from DIE's
21138    attributes.  */
21139
21140 static void
21141 prune_unused_types_update_strings (dw_die_ref die)
21142 {
21143   dw_attr_ref a;
21144   unsigned ix;
21145
21146   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21147     if (AT_class (a) == dw_val_class_str)
21148       {
21149         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21150         s->refcount++;
21151         /* Avoid unnecessarily putting strings that are used less than
21152            twice in the hash table.  */
21153         if (s->refcount
21154             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21155           {
21156             void ** slot;
21157             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21158                                              htab_hash_string (s->str),
21159                                              INSERT);
21160             gcc_assert (*slot == NULL);
21161             *slot = s;
21162           }
21163       }
21164 }
21165
21166 /* Remove from the tree DIE any dies that aren't marked.  */
21167
21168 static void
21169 prune_unused_types_prune (dw_die_ref die)
21170 {
21171   dw_die_ref c;
21172
21173   gcc_assert (die->die_mark);
21174   prune_unused_types_update_strings (die);
21175
21176   if (! die->die_child)
21177     return;
21178
21179   c = die->die_child;
21180   do {
21181     dw_die_ref prev = c;
21182     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21183       if (c == die->die_child)
21184         {
21185           /* No marked children between 'prev' and the end of the list.  */
21186           if (prev == c)
21187             /* No marked children at all.  */
21188             die->die_child = NULL;
21189           else
21190             {
21191               prev->die_sib = c->die_sib;
21192               die->die_child = prev;
21193             }
21194           return;
21195         }
21196
21197     if (c != prev->die_sib)
21198       prev->die_sib = c;
21199     prune_unused_types_prune (c);
21200   } while (c != die->die_child);
21201 }
21202
21203 /* Remove dies representing declarations that we never use.  */
21204
21205 static void
21206 prune_unused_types (void)
21207 {
21208   unsigned int i;
21209   limbo_die_node *node;
21210   comdat_type_node *ctnode;
21211   pubname_ref pub;
21212   dw_die_ref base_type;
21213
21214 #if ENABLE_ASSERT_CHECKING
21215   /* All the marks should already be clear.  */
21216   verify_marks_clear (comp_unit_die ());
21217   for (node = limbo_die_list; node; node = node->next)
21218     verify_marks_clear (node->die);
21219   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21220     verify_marks_clear (ctnode->root_die);
21221 #endif /* ENABLE_ASSERT_CHECKING */
21222
21223   /* Mark types that are used in global variables.  */
21224   premark_types_used_by_global_vars ();
21225
21226   /* Set the mark on nodes that are actually used.  */
21227   prune_unused_types_walk (comp_unit_die ());
21228   for (node = limbo_die_list; node; node = node->next)
21229     prune_unused_types_walk (node->die);
21230   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21231     {
21232       prune_unused_types_walk (ctnode->root_die);
21233       prune_unused_types_mark (ctnode->type_die, 1);
21234     }
21235
21236   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
21237      are unusual in that they are pubnames that are the children of pubtypes.
21238      They should only be marked via their parent DW_TAG_enumeration_type die,
21239      not as roots in themselves.  */
21240   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21241     if (pub->die->die_tag != DW_TAG_enumerator)
21242       prune_unused_types_mark (pub->die, 1);
21243   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21244     prune_unused_types_mark (base_type, 1);
21245
21246   if (debug_str_hash)
21247     htab_empty (debug_str_hash);
21248   prune_unused_types_prune (comp_unit_die ());
21249   for (node = limbo_die_list; node; node = node->next)
21250     prune_unused_types_prune (node->die);
21251   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21252     prune_unused_types_prune (ctnode->root_die);
21253
21254   /* Leave the marks clear.  */
21255   prune_unmark_dies (comp_unit_die ());
21256   for (node = limbo_die_list; node; node = node->next)
21257     prune_unmark_dies (node->die);
21258   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21259     prune_unmark_dies (ctnode->root_die);
21260 }
21261
21262 /* Set the parameter to true if there are any relative pathnames in
21263    the file table.  */
21264 static int
21265 file_table_relative_p (void ** slot, void *param)
21266 {
21267   bool *p = (bool *) param;
21268   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21269   if (!IS_ABSOLUTE_PATH (d->filename))
21270     {
21271       *p = true;
21272       return 0;
21273     }
21274   return 1;
21275 }
21276
21277 /* Routines to manipulate hash table of comdat type units.  */
21278
21279 static hashval_t
21280 htab_ct_hash (const void *of)
21281 {
21282   hashval_t h;
21283   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21284
21285   memcpy (&h, type_node->signature, sizeof (h));
21286   return h;
21287 }
21288
21289 static int
21290 htab_ct_eq (const void *of1, const void *of2)
21291 {
21292   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21293   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21294
21295   return (! memcmp (type_node_1->signature, type_node_2->signature,
21296                     DWARF_TYPE_SIGNATURE_SIZE));
21297 }
21298
21299 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21300    to the location it would have been added, should we know its
21301    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21302    probably improve compactness of debug info, removing equivalent
21303    abbrevs, and hide any differences caused by deferring the
21304    computation of the assembler name, triggered by e.g. PCH.  */
21305
21306 static inline void
21307 move_linkage_attr (dw_die_ref die)
21308 {
21309   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21310   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21311
21312   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21313               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21314
21315   while (--ix > 0)
21316     {
21317       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21318
21319       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21320         break;
21321     }
21322
21323   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21324     {
21325       VEC_pop (dw_attr_node, die->die_attr);
21326       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21327     }
21328 }
21329
21330 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21331    referenced from typed stack ops and count how often they are used.  */
21332
21333 static void
21334 mark_base_types (dw_loc_descr_ref loc)
21335 {
21336   dw_die_ref base_type = NULL;
21337
21338   for (; loc; loc = loc->dw_loc_next)
21339     {
21340       switch (loc->dw_loc_opc)
21341         {
21342         case DW_OP_GNU_regval_type:
21343         case DW_OP_GNU_deref_type:
21344           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21345           break;
21346         case DW_OP_GNU_convert:
21347         case DW_OP_GNU_reinterpret:
21348           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21349             continue;
21350           /* FALLTHRU */
21351         case DW_OP_GNU_const_type:
21352           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21353           break;
21354         case DW_OP_GNU_entry_value:
21355           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21356           continue;
21357         default:
21358           continue;
21359         }
21360       gcc_assert (base_type->die_parent == comp_unit_die ());
21361       if (base_type->die_mark)
21362         base_type->die_mark++;
21363       else
21364         {
21365           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21366           base_type->die_mark = 1;
21367         }
21368     }
21369 }
21370
21371 /* Comparison function for sorting marked base types.  */
21372
21373 static int
21374 base_type_cmp (const void *x, const void *y)
21375 {
21376   dw_die_ref dx = *(const dw_die_ref *) x;
21377   dw_die_ref dy = *(const dw_die_ref *) y;
21378   unsigned int byte_size1, byte_size2;
21379   unsigned int encoding1, encoding2;
21380   if (dx->die_mark > dy->die_mark)
21381     return -1;
21382   if (dx->die_mark < dy->die_mark)
21383     return 1;
21384   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21385   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21386   if (byte_size1 < byte_size2)
21387     return 1;
21388   if (byte_size1 > byte_size2)
21389     return -1;
21390   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21391   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21392   if (encoding1 < encoding2)
21393     return 1;
21394   if (encoding1 > encoding2)
21395     return -1;
21396   return 0;
21397 }
21398
21399 /* Move base types marked by mark_base_types as early as possible
21400    in the CU, sorted by decreasing usage count both to make the
21401    uleb128 references as small as possible and to make sure they
21402    will have die_offset already computed by calc_die_sizes when
21403    sizes of typed stack loc ops is computed.  */
21404
21405 static void
21406 move_marked_base_types (void)
21407 {
21408   unsigned int i;
21409   dw_die_ref base_type, die, c;
21410
21411   if (VEC_empty (dw_die_ref, base_types))
21412     return;
21413
21414   /* Sort by decreasing usage count, they will be added again in that
21415      order later on.  */
21416   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21417   die = comp_unit_die ();
21418   c = die->die_child;
21419   do
21420     {
21421       dw_die_ref prev = c;
21422       c = c->die_sib;
21423       while (c->die_mark)
21424         {
21425           remove_child_with_prev (c, prev);
21426           /* As base types got marked, there must be at least
21427              one node other than DW_TAG_base_type.  */
21428           gcc_assert (c != c->die_sib);
21429           c = c->die_sib;
21430         }
21431     }
21432   while (c != die->die_child);
21433   gcc_assert (die->die_child);
21434   c = die->die_child;
21435   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21436     {
21437       base_type->die_mark = 0;
21438       base_type->die_sib = c->die_sib;
21439       c->die_sib = base_type;
21440       c = base_type;
21441     }
21442 }
21443
21444 /* Helper function for resolve_addr, attempt to resolve
21445    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21446    SYMBOL_REFs refer to variables emitted in the current CU.  */
21447
21448 static int
21449 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21450 {
21451   rtx rtl = *addr;
21452
21453   if (GET_CODE (rtl) == CONST_STRING)
21454     {
21455       size_t len = strlen (XSTR (rtl, 0)) + 1;
21456       tree t = build_string (len, XSTR (rtl, 0));
21457       tree tlen = size_int (len - 1);
21458       TREE_TYPE (t)
21459         = build_array_type (char_type_node, build_index_type (tlen));
21460       rtl = lookup_constant_def (t);
21461       if (!rtl || !MEM_P (rtl))
21462         return 1;
21463       rtl = XEXP (rtl, 0);
21464       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21465       *addr = rtl;
21466       return 0;
21467     }
21468
21469   if (GET_CODE (rtl) == SYMBOL_REF
21470       && SYMBOL_REF_DECL (rtl))
21471     {
21472       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21473         {
21474           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21475             return 1;
21476         }
21477       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21478         return 1;
21479     }
21480
21481   if (GET_CODE (rtl) == CONST
21482       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21483     return 1;
21484
21485   return 0;
21486 }
21487
21488 /* Helper function for resolve_addr, handle one location
21489    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21490    the location list couldn't be resolved.  */
21491
21492 static bool
21493 resolve_addr_in_expr (dw_loc_descr_ref loc)
21494 {
21495   dw_loc_descr_ref keep = NULL;
21496   for (; loc; loc = loc->dw_loc_next)
21497     switch (loc->dw_loc_opc)
21498       {
21499       case DW_OP_addr:
21500         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21501           return false;
21502         break;
21503       case DW_OP_const4u:
21504       case DW_OP_const8u:
21505         if (loc->dtprel
21506             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21507           return false;
21508         break;
21509       case DW_OP_plus_uconst:
21510         if (size_of_loc_descr (loc)
21511             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21512               + 1
21513             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21514           {
21515             dw_loc_descr_ref repl
21516               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21517             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21518             add_loc_descr (&repl, loc->dw_loc_next);
21519             *loc = *repl;
21520           }
21521         break;
21522       case DW_OP_implicit_value:
21523         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21524             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21525           return false;
21526         break;
21527       case DW_OP_GNU_implicit_pointer:
21528       case DW_OP_GNU_parameter_ref:
21529         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21530           {
21531             dw_die_ref ref
21532               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21533             if (ref == NULL)
21534               return false;
21535             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21536             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21537             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21538           }
21539         break;
21540       case DW_OP_GNU_const_type:
21541       case DW_OP_GNU_regval_type:
21542       case DW_OP_GNU_deref_type:
21543       case DW_OP_GNU_convert:
21544       case DW_OP_GNU_reinterpret:
21545         while (loc->dw_loc_next
21546                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21547           {
21548             dw_die_ref base1, base2;
21549             unsigned enc1, enc2, size1, size2;
21550             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21551                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21552               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21553             else if (loc->dw_loc_oprnd1.val_class
21554                      == dw_val_class_unsigned_const)
21555               break;
21556             else
21557               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21558             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21559                 == dw_val_class_unsigned_const)
21560               break;
21561             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21562             gcc_assert (base1->die_tag == DW_TAG_base_type
21563                         && base2->die_tag == DW_TAG_base_type);
21564             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21565             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21566             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21567             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21568             if (size1 == size2
21569                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21570                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21571                      && loc != keep)
21572                     || enc1 == enc2))
21573               {
21574                 /* Optimize away next DW_OP_GNU_convert after
21575                    adjusting LOC's base type die reference.  */
21576                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21577                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21578                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21579                 else
21580                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21581                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21582                 continue;
21583               }
21584             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21585                point typed stack entry.  */
21586             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21587               keep = loc->dw_loc_next;
21588             break;
21589           }
21590         break;
21591       default:
21592         break;
21593       }
21594   return true;
21595 }
21596
21597 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21598    an address in .rodata section if the string literal is emitted there,
21599    or remove the containing location list or replace DW_AT_const_value
21600    with DW_AT_location and empty location expression, if it isn't found
21601    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21602    to something that has been emitted in the current CU.  */
21603
21604 static void
21605 resolve_addr (dw_die_ref die)
21606 {
21607   dw_die_ref c;
21608   dw_attr_ref a;
21609   dw_loc_list_ref *curr, *start, loc;
21610   unsigned ix;
21611
21612   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21613     switch (AT_class (a))
21614       {
21615       case dw_val_class_loc_list:
21616         start = curr = AT_loc_list_ptr (a);
21617         loc = *curr;
21618         gcc_assert (loc);
21619         /* The same list can be referenced more than once.  See if we have
21620            already recorded the result from a previous pass.  */
21621         if (loc->replaced)
21622           *curr = loc->dw_loc_next;
21623         else if (!loc->resolved_addr)
21624           {
21625             /* As things stand, we do not expect or allow one die to
21626                reference a suffix of another die's location list chain.
21627                References must be identical or completely separate.
21628                There is therefore no need to cache the result of this
21629                pass on any list other than the first; doing so
21630                would lead to unnecessary writes.  */
21631             while (*curr)
21632               {
21633                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21634                 if (!resolve_addr_in_expr ((*curr)->expr))
21635                   {
21636                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21637                     if (next && (*curr)->ll_symbol)
21638                       {
21639                         gcc_assert (!next->ll_symbol);
21640                         next->ll_symbol = (*curr)->ll_symbol;
21641                       }
21642                     *curr = next;
21643                   }
21644                 else
21645                   {
21646                     mark_base_types ((*curr)->expr);
21647                     curr = &(*curr)->dw_loc_next;
21648                   }
21649               }
21650             if (loc == *start)
21651               loc->resolved_addr = 1;
21652             else
21653               {
21654                 loc->replaced = 1;
21655                 loc->dw_loc_next = *start;
21656               }
21657           }
21658         if (!*start)
21659           {
21660             remove_AT (die, a->dw_attr);
21661             ix--;
21662           }
21663         break;
21664       case dw_val_class_loc:
21665         {
21666           dw_loc_descr_ref l = AT_loc (a);
21667           /* For -gdwarf-2 don't attempt to optimize
21668              DW_AT_data_member_location containing
21669              DW_OP_plus_uconst - older consumers might
21670              rely on it being that op instead of a more complex,
21671              but shorter, location description.  */
21672           if ((dwarf_version > 2
21673                || a->dw_attr != DW_AT_data_member_location
21674                || l == NULL
21675                || l->dw_loc_opc != DW_OP_plus_uconst
21676                || l->dw_loc_next != NULL)
21677               && !resolve_addr_in_expr (l))
21678             {
21679               remove_AT (die, a->dw_attr);
21680               ix--;
21681             }
21682           else
21683             mark_base_types (l);
21684         }
21685         break;
21686       case dw_val_class_addr:
21687         if (a->dw_attr == DW_AT_const_value
21688             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21689           {
21690             remove_AT (die, a->dw_attr);
21691             ix--;
21692           }
21693         if (die->die_tag == DW_TAG_GNU_call_site
21694             && a->dw_attr == DW_AT_abstract_origin)
21695           {
21696             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21697             dw_die_ref tdie = lookup_decl_die (tdecl);
21698             if (tdie == NULL
21699                 && DECL_EXTERNAL (tdecl)
21700                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21701               {
21702                 force_decl_die (tdecl);
21703                 tdie = lookup_decl_die (tdecl);
21704               }
21705             if (tdie)
21706               {
21707                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21708                 a->dw_attr_val.v.val_die_ref.die = tdie;
21709                 a->dw_attr_val.v.val_die_ref.external = 0;
21710               }
21711             else
21712               {
21713                 remove_AT (die, a->dw_attr);
21714                 ix--;
21715               }
21716           }
21717         break;
21718       default:
21719         break;
21720       }
21721
21722   FOR_EACH_CHILD (die, c, resolve_addr (c));
21723 }
21724 \f
21725 /* Helper routines for optimize_location_lists.
21726    This pass tries to share identical local lists in .debug_loc
21727    section.  */
21728
21729 /* Iteratively hash operands of LOC opcode.  */
21730
21731 static inline hashval_t
21732 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21733 {
21734   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21735   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21736
21737   switch (loc->dw_loc_opc)
21738     {
21739     case DW_OP_const4u:
21740     case DW_OP_const8u:
21741       if (loc->dtprel)
21742         goto hash_addr;
21743       /* FALLTHRU */
21744     case DW_OP_const1u:
21745     case DW_OP_const1s:
21746     case DW_OP_const2u:
21747     case DW_OP_const2s:
21748     case DW_OP_const4s:
21749     case DW_OP_const8s:
21750     case DW_OP_constu:
21751     case DW_OP_consts:
21752     case DW_OP_pick:
21753     case DW_OP_plus_uconst:
21754     case DW_OP_breg0:
21755     case DW_OP_breg1:
21756     case DW_OP_breg2:
21757     case DW_OP_breg3:
21758     case DW_OP_breg4:
21759     case DW_OP_breg5:
21760     case DW_OP_breg6:
21761     case DW_OP_breg7:
21762     case DW_OP_breg8:
21763     case DW_OP_breg9:
21764     case DW_OP_breg10:
21765     case DW_OP_breg11:
21766     case DW_OP_breg12:
21767     case DW_OP_breg13:
21768     case DW_OP_breg14:
21769     case DW_OP_breg15:
21770     case DW_OP_breg16:
21771     case DW_OP_breg17:
21772     case DW_OP_breg18:
21773     case DW_OP_breg19:
21774     case DW_OP_breg20:
21775     case DW_OP_breg21:
21776     case DW_OP_breg22:
21777     case DW_OP_breg23:
21778     case DW_OP_breg24:
21779     case DW_OP_breg25:
21780     case DW_OP_breg26:
21781     case DW_OP_breg27:
21782     case DW_OP_breg28:
21783     case DW_OP_breg29:
21784     case DW_OP_breg30:
21785     case DW_OP_breg31:
21786     case DW_OP_regx:
21787     case DW_OP_fbreg:
21788     case DW_OP_piece:
21789     case DW_OP_deref_size:
21790     case DW_OP_xderef_size:
21791       hash = iterative_hash_object (val1->v.val_int, hash);
21792       break;
21793     case DW_OP_skip:
21794     case DW_OP_bra:
21795       {
21796         int offset;
21797
21798         gcc_assert (val1->val_class == dw_val_class_loc);
21799         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21800         hash = iterative_hash_object (offset, hash);
21801       }
21802       break;
21803     case DW_OP_implicit_value:
21804       hash = iterative_hash_object (val1->v.val_unsigned, hash);
21805       switch (val2->val_class)
21806         {
21807         case dw_val_class_const:
21808           hash = iterative_hash_object (val2->v.val_int, hash);
21809           break;
21810         case dw_val_class_vec:
21811           {
21812             unsigned int elt_size = val2->v.val_vec.elt_size;
21813             unsigned int len = val2->v.val_vec.length;
21814
21815             hash = iterative_hash_object (elt_size, hash);
21816             hash = iterative_hash_object (len, hash);
21817             hash = iterative_hash (val2->v.val_vec.array,
21818                                    len * elt_size, hash);
21819           }
21820           break;
21821         case dw_val_class_const_double:
21822           hash = iterative_hash_object (val2->v.val_double.low, hash);
21823           hash = iterative_hash_object (val2->v.val_double.high, hash);
21824           break;
21825         case dw_val_class_addr:
21826           hash = iterative_hash_rtx (val2->v.val_addr, hash);
21827           break;
21828         default:
21829           gcc_unreachable ();
21830         }
21831       break;
21832     case DW_OP_bregx:
21833     case DW_OP_bit_piece:
21834       hash = iterative_hash_object (val1->v.val_int, hash);
21835       hash = iterative_hash_object (val2->v.val_int, hash);
21836       break;
21837     case DW_OP_addr:
21838     hash_addr:
21839       if (loc->dtprel)
21840         {
21841           unsigned char dtprel = 0xd1;
21842           hash = iterative_hash_object (dtprel, hash);
21843         }
21844       hash = iterative_hash_rtx (val1->v.val_addr, hash);
21845       break;
21846     case DW_OP_GNU_implicit_pointer:
21847       hash = iterative_hash_object (val2->v.val_int, hash);
21848       break;
21849     case DW_OP_GNU_entry_value:
21850       hash = hash_loc_operands (val1->v.val_loc, hash);
21851       break;
21852     case DW_OP_GNU_regval_type:
21853     case DW_OP_GNU_deref_type:
21854       {
21855         unsigned int byte_size
21856           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21857         unsigned int encoding
21858           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21859         hash = iterative_hash_object (val1->v.val_int, hash);
21860         hash = iterative_hash_object (byte_size, hash);
21861         hash = iterative_hash_object (encoding, hash);
21862       }
21863       break;
21864     case DW_OP_GNU_convert:
21865     case DW_OP_GNU_reinterpret:
21866       if (val1->val_class == dw_val_class_unsigned_const)
21867         {
21868           hash = iterative_hash_object (val1->v.val_unsigned, hash);
21869           break;
21870         }
21871       /* FALLTHRU */
21872     case DW_OP_GNU_const_type:
21873       {
21874         unsigned int byte_size
21875           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21876         unsigned int encoding
21877           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21878         hash = iterative_hash_object (byte_size, hash);
21879         hash = iterative_hash_object (encoding, hash);
21880         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21881           break;
21882         hash = iterative_hash_object (val2->val_class, hash);
21883         switch (val2->val_class)
21884           {
21885           case dw_val_class_const:
21886             hash = iterative_hash_object (val2->v.val_int, hash);
21887             break;
21888           case dw_val_class_vec:
21889             {
21890               unsigned int elt_size = val2->v.val_vec.elt_size;
21891               unsigned int len = val2->v.val_vec.length;
21892
21893               hash = iterative_hash_object (elt_size, hash);
21894               hash = iterative_hash_object (len, hash);
21895               hash = iterative_hash (val2->v.val_vec.array,
21896                                      len * elt_size, hash);
21897             }
21898             break;
21899           case dw_val_class_const_double:
21900             hash = iterative_hash_object (val2->v.val_double.low, hash);
21901             hash = iterative_hash_object (val2->v.val_double.high, hash);
21902             break;
21903           default:
21904             gcc_unreachable ();
21905           }
21906       }
21907       break;
21908
21909     default:
21910       /* Other codes have no operands.  */
21911       break;
21912     }
21913   return hash;
21914 }
21915
21916 /* Iteratively hash the whole DWARF location expression LOC.  */
21917
21918 static inline hashval_t
21919 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
21920 {
21921   dw_loc_descr_ref l;
21922   bool sizes_computed = false;
21923   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
21924   size_of_locs (loc);
21925
21926   for (l = loc; l != NULL; l = l->dw_loc_next)
21927     {
21928       enum dwarf_location_atom opc = l->dw_loc_opc;
21929       hash = iterative_hash_object (opc, hash);
21930       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
21931         {
21932           size_of_locs (loc);
21933           sizes_computed = true;
21934         }
21935       hash = hash_loc_operands (l, hash);
21936     }
21937   return hash;
21938 }
21939
21940 /* Compute hash of the whole location list LIST_HEAD.  */
21941
21942 static inline void
21943 hash_loc_list (dw_loc_list_ref list_head)
21944 {
21945   dw_loc_list_ref curr = list_head;
21946   hashval_t hash = 0;
21947
21948   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
21949     {
21950       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
21951       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
21952       if (curr->section)
21953         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
21954                                hash);
21955       hash = hash_locs (curr->expr, hash);
21956     }
21957   list_head->hash = hash;
21958 }
21959
21960 /* Return true if X and Y opcodes have the same operands.  */
21961
21962 static inline bool
21963 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
21964 {
21965   dw_val_ref valx1 = &x->dw_loc_oprnd1;
21966   dw_val_ref valx2 = &x->dw_loc_oprnd2;
21967   dw_val_ref valy1 = &y->dw_loc_oprnd1;
21968   dw_val_ref valy2 = &y->dw_loc_oprnd2;
21969
21970   switch (x->dw_loc_opc)
21971     {
21972     case DW_OP_const4u:
21973     case DW_OP_const8u:
21974       if (x->dtprel)
21975         goto hash_addr;
21976       /* FALLTHRU */
21977     case DW_OP_const1u:
21978     case DW_OP_const1s:
21979     case DW_OP_const2u:
21980     case DW_OP_const2s:
21981     case DW_OP_const4s:
21982     case DW_OP_const8s:
21983     case DW_OP_constu:
21984     case DW_OP_consts:
21985     case DW_OP_pick:
21986     case DW_OP_plus_uconst:
21987     case DW_OP_breg0:
21988     case DW_OP_breg1:
21989     case DW_OP_breg2:
21990     case DW_OP_breg3:
21991     case DW_OP_breg4:
21992     case DW_OP_breg5:
21993     case DW_OP_breg6:
21994     case DW_OP_breg7:
21995     case DW_OP_breg8:
21996     case DW_OP_breg9:
21997     case DW_OP_breg10:
21998     case DW_OP_breg11:
21999     case DW_OP_breg12:
22000     case DW_OP_breg13:
22001     case DW_OP_breg14:
22002     case DW_OP_breg15:
22003     case DW_OP_breg16:
22004     case DW_OP_breg17:
22005     case DW_OP_breg18:
22006     case DW_OP_breg19:
22007     case DW_OP_breg20:
22008     case DW_OP_breg21:
22009     case DW_OP_breg22:
22010     case DW_OP_breg23:
22011     case DW_OP_breg24:
22012     case DW_OP_breg25:
22013     case DW_OP_breg26:
22014     case DW_OP_breg27:
22015     case DW_OP_breg28:
22016     case DW_OP_breg29:
22017     case DW_OP_breg30:
22018     case DW_OP_breg31:
22019     case DW_OP_regx:
22020     case DW_OP_fbreg:
22021     case DW_OP_piece:
22022     case DW_OP_deref_size:
22023     case DW_OP_xderef_size:
22024       return valx1->v.val_int == valy1->v.val_int;
22025     case DW_OP_skip:
22026     case DW_OP_bra:
22027       gcc_assert (valx1->val_class == dw_val_class_loc
22028                   && valy1->val_class == dw_val_class_loc
22029                   && x->dw_loc_addr == y->dw_loc_addr);
22030       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22031     case DW_OP_implicit_value:
22032       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22033           || valx2->val_class != valy2->val_class)
22034         return false;
22035       switch (valx2->val_class)
22036         {
22037         case dw_val_class_const:
22038           return valx2->v.val_int == valy2->v.val_int;
22039         case dw_val_class_vec:
22040           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22041                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22042                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22043                             valx2->v.val_vec.elt_size
22044                             * valx2->v.val_vec.length) == 0;
22045         case dw_val_class_const_double:
22046           return valx2->v.val_double.low == valy2->v.val_double.low
22047                  && valx2->v.val_double.high == valy2->v.val_double.high;
22048         case dw_val_class_addr:
22049           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22050         default:
22051           gcc_unreachable ();
22052         }
22053     case DW_OP_bregx:
22054     case DW_OP_bit_piece:
22055       return valx1->v.val_int == valy1->v.val_int
22056              && valx2->v.val_int == valy2->v.val_int;
22057     case DW_OP_addr:
22058     hash_addr:
22059       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22060     case DW_OP_GNU_implicit_pointer:
22061       return valx1->val_class == dw_val_class_die_ref
22062              && valx1->val_class == valy1->val_class
22063              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22064              && valx2->v.val_int == valy2->v.val_int;
22065     case DW_OP_GNU_entry_value:
22066       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22067     case DW_OP_GNU_const_type:
22068       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22069           || valx2->val_class != valy2->val_class)
22070         return false;
22071       switch (valx2->val_class)
22072         {
22073         case dw_val_class_const:
22074           return valx2->v.val_int == valy2->v.val_int;
22075         case dw_val_class_vec:
22076           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22077                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22078                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22079                             valx2->v.val_vec.elt_size
22080                             * valx2->v.val_vec.length) == 0;
22081         case dw_val_class_const_double:
22082           return valx2->v.val_double.low == valy2->v.val_double.low
22083                  && valx2->v.val_double.high == valy2->v.val_double.high;
22084         default:
22085           gcc_unreachable ();
22086         }
22087     case DW_OP_GNU_regval_type:
22088     case DW_OP_GNU_deref_type:
22089       return valx1->v.val_int == valy1->v.val_int
22090              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22091     case DW_OP_GNU_convert:
22092     case DW_OP_GNU_reinterpret:
22093       if (valx1->val_class != valy1->val_class)
22094         return false;
22095       if (valx1->val_class == dw_val_class_unsigned_const)
22096         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22097       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22098     case DW_OP_GNU_parameter_ref:
22099       return valx1->val_class == dw_val_class_die_ref
22100              && valx1->val_class == valy1->val_class
22101              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22102     default:
22103       /* Other codes have no operands.  */
22104       return true;
22105     }
22106 }
22107
22108 /* Return true if DWARF location expressions X and Y are the same.  */
22109
22110 static inline bool
22111 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22112 {
22113   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22114     if (x->dw_loc_opc != y->dw_loc_opc
22115         || x->dtprel != y->dtprel
22116         || !compare_loc_operands (x, y))
22117       break;
22118   return x == NULL && y == NULL;
22119 }
22120
22121 /* Return precomputed hash of location list X.  */
22122
22123 static hashval_t
22124 loc_list_hash (const void *x)
22125 {
22126   return ((const struct dw_loc_list_struct *) x)->hash;
22127 }
22128
22129 /* Return 1 if location lists X and Y are the same.  */
22130
22131 static int
22132 loc_list_eq (const void *x, const void *y)
22133 {
22134   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22135   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22136   if (a == b)
22137     return 1;
22138   if (a->hash != b->hash)
22139     return 0;
22140   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22141     if (strcmp (a->begin, b->begin) != 0
22142         || strcmp (a->end, b->end) != 0
22143         || (a->section == NULL) != (b->section == NULL)
22144         || (a->section && strcmp (a->section, b->section) != 0)
22145         || !compare_locs (a->expr, b->expr))
22146       break;
22147   return a == NULL && b == NULL;
22148 }
22149
22150 /* Recursively optimize location lists referenced from DIE
22151    children and share them whenever possible.  */
22152
22153 static void
22154 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22155 {
22156   dw_die_ref c;
22157   dw_attr_ref a;
22158   unsigned ix;
22159   void **slot;
22160
22161   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22162     if (AT_class (a) == dw_val_class_loc_list)
22163       {
22164         dw_loc_list_ref list = AT_loc_list (a);
22165         /* TODO: perform some optimizations here, before hashing
22166            it and storing into the hash table.  */
22167         hash_loc_list (list);
22168         slot = htab_find_slot_with_hash (htab, list, list->hash,
22169                                          INSERT);
22170         if (*slot == NULL)
22171           *slot = (void *) list;
22172         else
22173           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22174       }
22175
22176   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22177 }
22178
22179 /* Optimize location lists referenced from DIE
22180    children and share them whenever possible.  */
22181
22182 static void
22183 optimize_location_lists (dw_die_ref die)
22184 {
22185   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22186   optimize_location_lists_1 (die, htab);
22187   htab_delete (htab);
22188 }
22189 \f
22190 /* Output stuff that dwarf requires at the end of every file,
22191    and generate the DWARF-2 debugging info.  */
22192
22193 static void
22194 dwarf2out_finish (const char *filename)
22195 {
22196   limbo_die_node *node, *next_node;
22197   comdat_type_node *ctnode;
22198   htab_t comdat_type_table;
22199   unsigned int i;
22200
22201   /* PCH might result in DW_AT_producer string being restored from the
22202      header compilation, fix it up if needed.  */
22203   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22204   if (strcmp (AT_string (producer), producer_string) != 0)
22205     {
22206       struct indirect_string_node *node = find_AT_string (producer_string);
22207       producer->dw_attr_val.v.val_str = node;
22208     }
22209
22210   gen_scheduled_generic_parms_dies ();
22211   gen_remaining_tmpl_value_param_die_attribute ();
22212
22213   /* Add the name for the main input file now.  We delayed this from
22214      dwarf2out_init to avoid complications with PCH.  */
22215   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22216   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
22217     add_comp_dir_attribute (comp_unit_die ());
22218   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22219     {
22220       bool p = false;
22221       htab_traverse (file_table, file_table_relative_p, &p);
22222       if (p)
22223         add_comp_dir_attribute (comp_unit_die ());
22224     }
22225
22226   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22227     {
22228       add_location_or_const_value_attribute (
22229         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22230         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22231         false,
22232         DW_AT_location);
22233     }
22234
22235   /* Traverse the limbo die list, and add parent/child links.  The only
22236      dies without parents that should be here are concrete instances of
22237      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22238      For concrete instances, we can get the parent die from the abstract
22239      instance.  */
22240   for (node = limbo_die_list; node; node = next_node)
22241     {
22242       dw_die_ref die = node->die;
22243       next_node = node->next;
22244
22245       if (die->die_parent == NULL)
22246         {
22247           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22248
22249           if (origin && origin->die_parent)
22250             add_child_die (origin->die_parent, die);
22251           else if (is_cu_die (die))
22252             ;
22253           else if (seen_error ())
22254             /* It's OK to be confused by errors in the input.  */
22255             add_child_die (comp_unit_die (), die);
22256           else
22257             {
22258               /* In certain situations, the lexical block containing a
22259                  nested function can be optimized away, which results
22260                  in the nested function die being orphaned.  Likewise
22261                  with the return type of that nested function.  Force
22262                  this to be a child of the containing function.
22263
22264                  It may happen that even the containing function got fully
22265                  inlined and optimized out.  In that case we are lost and
22266                  assign the empty child.  This should not be big issue as
22267                  the function is likely unreachable too.  */
22268               gcc_assert (node->created_for);
22269
22270               if (DECL_P (node->created_for))
22271                 origin = get_context_die (DECL_CONTEXT (node->created_for));
22272               else if (TYPE_P (node->created_for))
22273                 origin = scope_die_for (node->created_for, comp_unit_die ());
22274               else
22275                 origin = comp_unit_die ();
22276
22277               add_child_die (origin, die);
22278             }
22279         }
22280     }
22281
22282   limbo_die_list = NULL;
22283
22284 #if ENABLE_ASSERT_CHECKING
22285   {
22286     dw_die_ref die = comp_unit_die (), c;
22287     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22288   }
22289 #endif
22290   resolve_addr (comp_unit_die ());
22291   move_marked_base_types ();
22292
22293   for (node = deferred_asm_name; node; node = node->next)
22294     {
22295       tree decl = node->created_for;
22296       /* When generating LTO bytecode we can not generate new assembler
22297          names at this point and all important decls got theirs via
22298          free-lang-data.  */
22299       if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
22300           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22301         {
22302           add_linkage_attr (node->die, decl);
22303           move_linkage_attr (node->die);
22304         }
22305     }
22306
22307   deferred_asm_name = NULL;
22308
22309   /* Walk through the list of incomplete types again, trying once more to
22310      emit full debugging info for them.  */
22311   retry_incomplete_types ();
22312
22313   if (flag_eliminate_unused_debug_types)
22314     prune_unused_types ();
22315
22316   /* Generate separate COMDAT sections for type DIEs. */
22317   if (use_debug_types)
22318     {
22319       break_out_comdat_types (comp_unit_die ());
22320
22321       /* Each new type_unit DIE was added to the limbo die list when created.
22322          Since these have all been added to comdat_type_list, clear the
22323          limbo die list.  */
22324       limbo_die_list = NULL;
22325
22326       /* For each new comdat type unit, copy declarations for incomplete
22327          types to make the new unit self-contained (i.e., no direct
22328          references to the main compile unit).  */
22329       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22330         copy_decls_for_unworthy_types (ctnode->root_die);
22331       copy_decls_for_unworthy_types (comp_unit_die ());
22332
22333       /* In the process of copying declarations from one unit to another,
22334          we may have left some declarations behind that are no longer
22335          referenced.  Prune them.  */
22336       prune_unused_types ();
22337     }
22338
22339   /* Generate separate CUs for each of the include files we've seen.
22340      They will go into limbo_die_list.  */
22341   if (flag_eliminate_dwarf2_dups)
22342     break_out_includes (comp_unit_die ());
22343
22344   /* Traverse the DIE's and add add sibling attributes to those DIE's
22345      that have children.  */
22346   add_sibling_attributes (comp_unit_die ());
22347   for (node = limbo_die_list; node; node = node->next)
22348     add_sibling_attributes (node->die);
22349   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22350     add_sibling_attributes (ctnode->root_die);
22351
22352   /* Output a terminator label for the .text section.  */
22353   switch_to_section (text_section);
22354   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22355   if (cold_text_section)
22356     {
22357       switch_to_section (cold_text_section);
22358       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22359     }
22360
22361   /* We can only use the low/high_pc attributes if all of the code was
22362      in .text.  */
22363   if (!have_multiple_function_sections 
22364       || (dwarf_version < 3 && dwarf_strict))
22365     {
22366       /* Don't add if the CU has no associated code.  */
22367       if (text_section_used)
22368         {
22369           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22370           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22371         }
22372     }
22373   else
22374     {
22375       unsigned fde_idx;
22376       dw_fde_ref fde;
22377       bool range_list_added = false;
22378
22379       if (text_section_used)
22380         add_ranges_by_labels (comp_unit_die (), text_section_label,
22381                               text_end_label, &range_list_added);
22382       if (cold_text_section_used)
22383         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22384                               cold_end_label, &range_list_added);
22385
22386       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22387         {
22388           if (DECL_IGNORED_P (fde->decl))
22389             continue;
22390           if (!fde->in_std_section)
22391             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22392                                   fde->dw_fde_end, &range_list_added);
22393           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22394             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22395                                   fde->dw_fde_second_end, &range_list_added);
22396         }
22397
22398       if (range_list_added)
22399         {
22400           /* We need to give .debug_loc and .debug_ranges an appropriate
22401              "base address".  Use zero so that these addresses become
22402              absolute.  Historically, we've emitted the unexpected
22403              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22404              Emit both to give time for other tools to adapt.  */
22405           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22406           if (! dwarf_strict && dwarf_version < 4)
22407             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22408
22409           add_ranges (NULL);
22410         }
22411     }
22412
22413   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22414     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22415                     debug_line_section_label);
22416
22417   if (have_macinfo)
22418     add_AT_macptr (comp_unit_die (),
22419                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22420                    macinfo_section_label);
22421
22422   if (have_location_lists)
22423     optimize_location_lists (comp_unit_die ());
22424
22425   /* Output all of the compilation units.  We put the main one last so that
22426      the offsets are available to output_pubnames.  */
22427   for (node = limbo_die_list; node; node = node->next)
22428     output_comp_unit (node->die, 0);
22429
22430   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22431   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22432     {
22433       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22434
22435       /* Don't output duplicate types.  */
22436       if (*slot != HTAB_EMPTY_ENTRY)
22437         continue;
22438
22439       /* Add a pointer to the line table for the main compilation unit
22440          so that the debugger can make sense of DW_AT_decl_file
22441          attributes.  */
22442       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22443         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22444                         debug_line_section_label);
22445
22446       output_comdat_type_unit (ctnode);
22447       *slot = ctnode;
22448     }
22449   htab_delete (comdat_type_table);
22450
22451   add_AT_pubnames (comp_unit_die ());
22452
22453   /* Output the main compilation unit if non-empty or if .debug_macinfo
22454      or .debug_macro will be emitted.  */
22455   output_comp_unit (comp_unit_die (), have_macinfo);
22456
22457   /* Output the abbreviation table.  */
22458   if (abbrev_die_table_in_use != 1)
22459     {
22460       switch_to_section (debug_abbrev_section);
22461       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22462       output_abbrev_section ();
22463     }
22464
22465   /* Output location list section if necessary.  */
22466   if (have_location_lists)
22467     {
22468       /* Output the location lists info.  */
22469       switch_to_section (debug_loc_section);
22470       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22471                                    DEBUG_LOC_SECTION_LABEL, 0);
22472       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22473       output_location_lists (comp_unit_die ());
22474     }
22475
22476   /* Output public names and types tables if necessary.  */
22477   output_pubnames (pubname_table);
22478   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22479      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22480      simply won't look for the section.  */
22481   output_pubnames (pubtype_table);
22482
22483   /* Output the address range information if a CU (.debug_info section)
22484      was emitted.  We output an empty table even if we had no functions
22485      to put in it.  This because the consumer has no way to tell the
22486      difference between an empty table that we omitted and failure to
22487      generate a table that would have contained data.  */
22488   if (info_section_emitted)
22489     {
22490       unsigned long aranges_length = size_of_aranges ();
22491
22492       switch_to_section (debug_aranges_section);
22493       output_aranges (aranges_length);
22494     }
22495
22496   /* Output ranges section if necessary.  */
22497   if (ranges_table_in_use)
22498     {
22499       switch_to_section (debug_ranges_section);
22500       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22501       output_ranges ();
22502     }
22503
22504   /* Have to end the macro section.  */
22505   if (have_macinfo)
22506     {
22507       switch_to_section (debug_macinfo_section);
22508       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22509       output_macinfo ();
22510       dw2_asm_output_data (1, 0, "End compilation unit");
22511     }
22512
22513   /* Output the source line correspondence table.  We must do this
22514      even if there is no line information.  Otherwise, on an empty
22515      translation unit, we will generate a present, but empty,
22516      .debug_info section.  IRIX 6.5 `nm' will then complain when
22517      examining the file.  This is done late so that any filenames
22518      used by the debug_info section are marked as 'used'.  */
22519   switch_to_section (debug_line_section);
22520   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22521   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22522     output_line_info ();
22523
22524   /* If we emitted any DW_FORM_strp form attribute, output the string
22525      table too.  */
22526   if (debug_str_hash)
22527     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22528 }
22529
22530 #include "gt-dwarf2out.h"